Skip to content

Shaders: cube samples with gradients, and image_sample_d_cl - #563

Closed
SAitken91 wants to merge 1 commit into
Marvisak:mainfrom
SAitken91:shader/cube-gradients-and-sample-d-cl
Closed

SAitken91 wants to merge 1 commit into
Marvisak:mainfrom
SAitken91:shader/cube-gradients-and-sample-d-cl

Conversation

@SAitken91

Copy link
Copy Markdown
Contributor

image_sample_d_cl was refused by name (27 programs in shaders.rpf, including PS_DeferredTextured). Behind it, every one of those programs also hit a cube sample with explicit gradients, refused as "a cube sample with texel coordinates, gradients or a level query". Both now translate to sampleGrad.

Change

  • Cube gradients. On GFX10, a cube's gradients are of the face coordinates (s, t): four addresses rather than six, so the coordinates start at index 4. The old count of six read past the address registers. The new cube_gradient turns the face gradients into the direction's gradients through the face's linear part of cube_direction (u = 2s − 3, v = 2t − 3; the major axis doesn't move). This mirrors the existing table term by term.
  • _cl. The level-of-detail clamp follows the coordinates and is passed as sampleGrad's clamp, which was undef before.
  • Cube samples with texel coordinates or a level query are still refused, as before.

Results

victory translate --validate shaders.rpf: vertex/pixel validated 12172 → 12197. Both errors are gone from the list. The other 2 of the 27 stop at unrelated cases.

Testing

  • cargo test -p victory-shader: 69 passed. Clippy and rustfmt are clean.
  • Not compared against the console's sampling at runtime. The gradient conversion follows from cube_direction being affine per face, but no draw has been checked. A differential test (needs VICTORY_CONSOLE_DXC) would confirm it.

This is independent of #539 and #554.

Written with the help of Claude (AI).

🤖 Generated with Claude Code

image_sample_d_cl was refused by name, and behind it every program hit
a cube sample with explicit gradients, refused as "a cube sample with
... gradients". Both now translate to sampleGrad:

- A cube's gradients on GFX10 are of the face coordinates (s, t), four
  addresses rather than six, so the coordinates follow at 4. They become
  the direction's gradients through the face's linear part of
  cube_direction (u = 2s - 3, v = 2t - 3; the major axis does not move),
  in the new cube_gradient.
- `_cl`: the level-of-detail clamp follows the coordinates and is passed
  as sampleGrad's clamp.

translate --validate on shaders.rpf: vertex/pixel 12172 -> 12197
validated; both errors are gone from the list. Not yet compared against
the console's sampling at runtime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KoLolX3

KoLolX3 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Check on Linux, not Windows: pass. Head 10ac6ceac merged onto main d2a4bae1f (clean merge).

  • Native tests of the seven crates that build on Linux (core, formats, game, loader, pe, pipeline, shader): PR 92 passed, 7 failed, main 92 passed, 7 failed.
  • Windows cross type-check of the whole workspace (cargo xwin check --workspace --target x86_64-pc-windows-msvc, covers victory-gpu, victory-hle and the app): PR ok, main ok.
  • Tests this PR adds: 0 (so the native result only shows the existing tests still pass; the change itself is not covered by a test here).
  • rustfmt: unformatted hunks, PR 168, main 168.
  • Failing on main here as well (not this PR): 7 tests in victory-loader and victory-pe that parse their own test binary as a PE image, which is an ELF file on Linux.
  • Not run: victory-gpu / victory-hle tests and the game itself.

On: AMD Ryzen 7 5700, 62 GB RAM, Artix Linux 7.3.4-artix1-2, rustc 1.98.1 (the repo's pinned toolchain), cargo-xwin 0.24.1. No game data. Automated check only: the code itself was not reviewed.

@iurysf

iurysf commented Nov 24, 2026

Copy link
Copy Markdown

Tested offline against main d2a4bae with victory translate --list --validate over the title's shaders.rpf + executable (26,096 programs), dxil.dll 1.9.2602.17:

  • Translation failures: 3,733 on main -> 3,708 with this PR (-25, all pixel). "image_sample_d_cl" disappears (27 -> 0), and so does "an image address" (2 -> 0).
  • Emitted: vertex+pixel 12,284 -> 12,309 (+25, all validated), matching your count. 185 modules rejected on both; no program that validated on main fails now.

AI-assisted.

@slint6614-dev

Copy link
Copy Markdown

Measured on the full retail shader pack (26,503 containers) with victory translate --validate, against main d2a4bae (emitted / validated):

VS/PS CS MS cargo test -p victory-shader
main 12272 / 12172 1693 / 1670 8191 / 8129 ok
#694 12272 / 12172 1705 / 1682 8191 / 8129 ok
#563 12297 / 12197 1693 / 1670 8191 / 8129 ok
#160 12345 / 12237 1705 / 1682 8191 / 8129 ok
#136 12619 / 12619 1796 / 1697 8355 / 8302 ok

Windows 11, dxil.dll from SDK 10.0.26100.

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 10ac6ce on Windows 11, RTX 5070, MSVC: the workspace passes (195 outside victory-shader; victory-shader --lib with --test-threads=1: 69). victory translate <title exe> --validate --pipelines gives the same counts before and after: 12 of 12 programs emitted, 12 validated, 12 pipelines. None of the 12 has cube gradients or _cl.

  • I checked cube_gradient against cube_direction term by term. Each u = 2s − 3 / v = 2t − 3 entry becomes 2·ds / 2·dt with the same sign, and the ±1 major-axis entries become 0. That is the Jacobian of the face-to-direction map.
  • Four gradient addresses for a cube on GFX10 (ds/dx, dt/dx, ds/dy, dt/dy) is right. The old six read two coordinate registers as gradients.
  • _cl reads the clamp right after the coordinates, which matches the MIMG address order (gradients, coordinates, clamp), and is passed as sampleGrad's clamp.

No issues found.

AI-assisted (Claude Code).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
… and image_sample_d_cl

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

Independent check of head 10ac6ce, read on its own base and built merged onto main ffe310e (clean merge). I read the comments above first; @mercuryVM already checked cube_gradient against cube_direction term by term and I get the same. This adds the references for the two hardware claims and what is still untested.

References

  • Four gradient addresses on a cube. LLVM IntrinsicsAMDGPU.td (release/18.x, line 640): AMDGPUDimCube : AMDGPUDimProps<0x3, "cube", "CUBE", ["s", "t"], ["face"]>. Gradients are per coordinate name, not per slice name, so a cube has two per direction and the coordinates start at address 4, as the PR has it. With the clamp that is 4 + 3 + 1 = 8 addresses, the count reported for the title's samples on Shader: pack a16 image addresses as the hardware does; image_sample_d_cl #318.
  • What the gradients are of. Mesa ac_nir_lower_tex.c (mesa-24.0.0, prepare_cube_coords, lines 107-147): with derivatives, sc and tc are multiplied by 1 / |ma| first, the derivatives are converted "to 2D derivatives" of those same quotients, and only then is 1.5 added. So the hardware's gradients are of the face coordinates s and t in [1, 2], and u = 2s - 3 gives du = 2 ds. The PR's factor of two and its zero for the major axis are the exact inverse for a direction whose major component is +-1, which is what cube_direction produces.
  • The clamp. It follows the coordinates in the address list (same file, AMDGPUDimProfile.AddrArgs, lines 739-746: the extra arguments, the gradients, the coordinates and slice, then LodClampMip, which is clamp for the _cl samples), and sampleGrad's last operand is the minimum-LOD clamp, so passing it there is the same quantity.

Limits I see, none new defects of this PR

  • a16. image_sample_d_cl takes the path image_sample_d has on main, where with a16 the gradients are read as packed 16-bit halves. @r4tedx's note on Shader: pack a16 image addresses as the hardware does; image_sample_d_cl #318 (LLVM's gfx10_asm_mimg.s: gradients stay 32-bit unless the opcode is a _g16 one) applies to both names. The title's _cl samples are reported as not a16, so this is latent, but _cl with a16 now translates where it was refused by name.
  • Cube arrays. cube_gradient compares the face to 0..5 as cube_direction does, so the existing shortcut (the face operand of a cube array adds 8 x the slice) covers gradients too.
  • image_sample_d_cl_o and the comparison forms are still refused by name.

Not tested by anything in this PR. It adds no test, and no test on main samples a cube with gradients, so the suite result below only says nothing else moved. #160 has the same rule (also called cube_gradient) with a GPU test for it, cube_gradients_are_the_face_coordinates in its image_tests.rs, on a cube whose faces and levels each hold their own value. The two PRs edit the same match arm and cannot both land as they are; if this one goes first, that test is the one to carry over. I did not port it: it needs #160's fixture.

Suite. NVIDIA RTX 3070, driver 616.56, Windows 11, Rust 1.98.1, SDK 10.0.26100 dxil.dll validating, cargo test -p victory-shader -- --test-threads=1: main ffe310e + this PR, 120 passed, 0 failed, 1 ignored (the bitcode benchmark).

Not checked: the title (no game files here), AMD hardware, any sampled result.

AI-assisted (Claude Code).

Machad3x pushed a commit to Machad3x/VIctory that referenced this pull request Nov 24, 2026
…the _o and _cl forms already here)

A cube's gradients are of the face coordinates, two per direction; the
descriptor-cube and level-query work was already in. Kept the _o handling
and let the LOD clamp follow for image_sample_d_cl_o too.

Offline validated 42 -> 45; 'cube sample with texel coordinates, gradients or
a level query' 3 -> 0.
LAKSHYA-cmd-tech added a commit to LAKSHYA-cmd-tech/VIctory that referenced this pull request Nov 24, 2026
…-cube-shaders

Shaders: fix GFX10 independent a16/g16 gradient fields and cube gradients (Marvisak#1209, Marvisak#563)
@MamaLeVrai

Copy link
Copy Markdown

Heads-up: this PR and #1004 both edit the cube-sample refusal in emit/lower.rs and conflict with each other. Merged together only texel coordinates are still refused; that resolution is in the bundle #1319 (clippy and tests pass). AI-assisted (Claude Code); no game data.

simnJS pushed a commit to MiamiGame/XTP that referenced this pull request Nov 24, 2026
Image samples and gathers now lower by their form (comparison,
derivatives, coarse or 16-bit derivatives, level, level 0, bias, level
clamp, texel offset) instead of a fixed list of opcodes. The address
dwords follow LLVM's ImageDimIntrinsics and Mesa's ac_build_image_opcode:
offset, bias and reference a dword each, then the derivatives (16-bit
ones two per dword in the _g16 opcodes, each direction its own), the
coordinates and the level or clamp (a16 packs those, not the
derivatives). The decoder counted 32-bit derivatives for the _g16 forms
and took a16 to pack the derivatives; it now sizes them by the opcode.

Newly lowered:
- every texel-offset form; DXIL immediates when the offset word is a
  constant in -8..7, else (past -8..7, or per lane) the coordinates move
  by offset / size at a level known at translation;
- _cl (the DXIL clamp argument), _cd (as _d), _g16;
- image_sample_c, _c_cl and their _o forms in compute as well as pixel
  shaders; image_sample_c_l(_o) at level 0, image_sample_c_b(_cl)(_o)
  with a bias known to be 0;
- image_gather4_l(_o) (at a level known to be 0 a gather, at a level
  known only at run time four samples of the footprint's texels at their
  centres), image_gather4_c_lz(_o), image_gather4_c_l(_o) at level 0
  (textureGatherCmp);
- cube samples with gradients (image_sample_d, _d_cl, _cd and the _g16
  forms): a cube's derivatives are those of its two face coordinates,
  which become the direction's through the face's linear part of
  cube_direction;
- a16 biased samples and a16 comparisons;
- image_store of some channels: the data VGPRs are the dmask's channels,
  the others are written as 0.
Still refused, each with its reason: gathers at an implicit level or with
a bias, comparisons with derivatives, at a level or with a bias not known
to be 0 (SM 6.7/6.8 operations), offsets past -8..7 or per lane at a
level not known at translation, comparison and cube gathers at a level
not known to be 0.

XTP adaptation: emit/lower/image.rs (S0b) takes the PR's image function
as is (XTP's equalled upstream ffe310e, the PR's base) with the 64-bit
atomic branch of the #539 port put back; `Module::known` (bitcode.rs) is
not taken, the existing `int_value` reads the constants (XTP holds every
register dword as an i32). The test host gains textures with mip levels,
cube textures with mip levels and R32F textures (emit/host.rs); the
decoder fix and its test are in gfx10. emit/image_tests.rs takes the
PR's tests; the #539 port's atomic tests become its module `atomic64`
and now take the GPU one at a time with the others (the PR's last
commit: XTP's VALU tests already share that lock).

Review notes, kept as shortcuts in the code: the four-sample gather at
a run-time level reads the footprint of the level rounded to the nearest
and clamped (a sampler bias or range that moves the level reads another
grid); offsets emulated by moving coordinates assume the sampler keeps
that level; partial stores write 0 to the channels the dmask leaves out
(the PR's model, checked on the host, not on the console). Comparison
samples with implicit derivatives in compute are sampleCmp, which
validator 1.6 refuses there and the drivers run (the test lets that
error through).

Survey (shaders.rpf, any host, every translation validated): 23,387 ->
23,472 translated, 85 new: 44 a16 biased samples, 25 image_sample_d_cl
(all on cubes: #563's 25), 16 image_gather4_l (at a run-time level);
77 accepted by dxil.dll, 8 stop at "Container part 'Program Input
Signature' does not match", a class the pack had before (15 programs).
None lost, no translation that existed changed. 68 programs past these
blockers stop at the next one (48 scalar memory read as data).

This covers Marvisak/VIctory#563 (head 10ac6ceac9284caa6dd5d4bad3166687f8d0c1ac) by SAitken91:
the same cube gradients (the same table) and image_sample_d_cl, written
independently; the PR's own commit 2cf3ccd says the 25 programs are the
title's. Nothing of #563 is left to port after this.

Ported from Marvisak/VIctory#160 (head 5ce85c5e70878e9208a1cc02c2b65966a1316653) by r4tedx
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: Saitken91 <98485913+SAitken91@users.noreply.github.com>
@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

image_sample_d_cl and cube samples with gradients are in main (0b35b7f, 4f57f46), the cube case done as SampleLevel from the face-space gradients because SampleGrad on a cube cannot reproduce them (diff_tests::cube_gradients_pick_the_level).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants