Shaders: gradient samples with a level clamp, and cube gradients in face space #1280

Closed
nicogunvis-boop wants to merge 1 commit from upstream-pr/1280 into main
nicogunvis-boop commented 2026-11-24 17:05:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nicogunvis-boop on 2026-11-24T17:05:20Z.


What

image_sample_d_cl (explicit gradients plus a level-of-detail clamp) was refused, and most retail uses of it are on cubes, whose gradient samples were refused as well. Example from the title (disassembled locally, registers only):

image_sample_d_cl v[66:68], [v83, v84, v85, v86, v68, v67, v66, v70], s[20:27], s[60:63] dmask:0x7 dim:SQ_RSRC_IMG_CUBE

Eight addresses on a cube: two face-space gradients per direction (ds/dx, dt/dx, ds/dy, dt/dy), the face coordinates s, t, face, then the clamp. This matches image_addr_dwords ((3, 2) for CUBE plus one for cl).

Change (crates/victory-shader/src/emit/lower.rs)

  • image_sample_d_cl lowers through the image_sample_d path. The clamp is read after the coordinates (and array slice) and passed as sampleGrad's clamp operand, which was undef.
  • Cube gradients: the gradient count per direction is 2 for cubes, not 3. cube_direction(s, t, face) is affine in s and t on one face (each component is a constant, ±(2s-3) or ±(2t-3)), so cube_direction(s + ds, t + dt, face) - cube_direction(s, t, face) is the exact direction gradient. Those two 3-component gradients go to sampleGrad on the cube.
  • The cube refusal keeps texel coordinates and level queries: "a cube sample with texel coordinates or a level query".
  • Tests: a_gradient_sample_with_a_level_clamp_reads_the_clamp_after_the_coordinates (2D) and a_cube_gradient_sample_takes_face_space_gradients (cube), both synthetic. Before the change both are refused.

Measured

victory translate --validate on the retail package (Windows 11, dxil.dll from SDK 10.0.26100):

before after
refused image_sample_d_cl 29 0
of those, validated 0 25
VS/PS emitted 12,243 12,268
VS/PS validated 12,167 12,192

The other 4 now stop on "a cross-lane operation outside compute". No other program's outcome changed (25,891 compared). The programs are mostly PS_DeferredTextured*, PS_MirrorReflection*, PS_TexturedLit* and PS_VoxelizeVisiblity*.

cargo test -p victory-shader --lib emit::lower: 13 passed. cargo clippy -p victory-shader --all-targets -- -D warnings: clean.

Not yet checked in a running world frame. Tested on an RTX 4070. Independent of #1245.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6587193899 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1280). Originally posted by [nicogunvis-boop](https://github.com/nicogunvis-boop) on 2026-11-24T17:05:20Z. --- ## What `image_sample_d_cl` (explicit gradients plus a level-of-detail clamp) was refused, and most retail uses of it are on **cubes**, whose gradient samples were refused as well. Example from the title (disassembled locally, registers only): ``` image_sample_d_cl v[66:68], [v83, v84, v85, v86, v68, v67, v66, v70], s[20:27], s[60:63] dmask:0x7 dim:SQ_RSRC_IMG_CUBE ``` Eight addresses on a cube: two face-space gradients per direction (`ds/dx, dt/dx, ds/dy, dt/dy`), the face coordinates `s, t, face`, then the clamp. This matches `image_addr_dwords` (`(3, 2)` for CUBE plus one for `cl`). ## Change (`crates/victory-shader/src/emit/lower.rs`) - `image_sample_d_cl` lowers through the `image_sample_d` path. The clamp is read after the coordinates (and array slice) and passed as `sampleGrad`'s clamp operand, which was `undef`. - Cube gradients: the gradient count per direction is 2 for cubes, not 3. `cube_direction(s, t, face)` is affine in `s` and `t` on one face (each component is a constant, `±(2s-3)` or `±(2t-3)`), so `cube_direction(s + ds, t + dt, face) - cube_direction(s, t, face)` is the exact direction gradient. Those two 3-component gradients go to `sampleGrad` on the cube. - The cube refusal keeps texel coordinates and level queries: "a cube sample with texel coordinates or a level query". - Tests: `a_gradient_sample_with_a_level_clamp_reads_the_clamp_after_the_coordinates` (2D) and `a_cube_gradient_sample_takes_face_space_gradients` (cube), both synthetic. Before the change both are refused. ## Measured `victory translate --validate` on the retail package (Windows 11, dxil.dll from SDK 10.0.26100): | | before | after | |---|---:|---:| | refused `image_sample_d_cl` | 29 | 0 | | of those, validated | 0 | 25 | | VS/PS emitted | 12,243 | 12,268 | | VS/PS validated | 12,167 | 12,192 | The other 4 now stop on "a cross-lane operation outside compute". No other program's outcome changed (25,891 compared). The programs are mostly `PS_DeferredTextured*`, `PS_MirrorReflection*`, `PS_TexturedLit*` and `PS_VoxelizeVisiblity*`. `cargo test -p victory-shader --lib emit::lower`: 13 passed. `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. Not yet checked in a running world frame. Tested on an RTX 4070. Independent of #1245.
shortselow-byte commented 2026-11-24 18:06:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by shortselow-byte on 2026-11-24T17:54:45Z.


For triage: git merge-tree against the current heads shows this PR conflicts in emit/lower.rs (the gradient part of Lower::image) with three open PRs:

  • #1209 (ae0b48e), which now also rewrites how Lower::image reads gradient VGPRs under a16/g16;
  • #318, the a16 address layout (it packs gradients under a16, the opposite of #1209);
  • #138, gradient samples on cube images, the same feature as this PR's cube part.

It merges cleanly with #694, which also lowers image_sample_d_cl (same "image_sample_d" | "image_sample_d_cl" arm, so git takes one), but both add their own clamp handling.

Whichever lands first, the gradient addressing in Lower::image will need one reconciled version.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139883559 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1280#issuecomment-7139883559). Originally posted by [shortselow-byte](https://github.com/shortselow-byte) on 2026-11-24T17:54:45Z. --- For triage: `git merge-tree` against the current heads shows this PR conflicts in `emit/lower.rs` (the gradient part of `Lower::image`) with three open PRs: - #1209 (`ae0b48e`), which now also rewrites how `Lower::image` reads gradient VGPRs under a16/g16; - #318, the a16 address layout (it packs gradients under a16, the opposite of #1209); - #138, gradient samples on cube images, the same feature as this PR's cube part. It merges cleanly with #694, which also lowers `image_sample_d_cl` (same `"image_sample_d" | "image_sample_d_cl"` arm, so git takes one), but both add their own clamp handling. Whichever lands first, the gradient addressing in `Lower::image` will need one reconciled version.
acetonahard-code commented 2026-11-24 21:28:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by acetonahard-code on 2026-11-24T21:12:07Z.


Tested on Windows 11, RTX 4070 SUPER (driver 621.14), with the repo toolchain, merged into current main (ffe310e):

cargo test -p victory-shader passed failed ignored
main 120 0 1
main + this PR 122 0 1
  • The 2 new tests pass.
  • No GPU test skipped for lack of a device.
  • cargo clippy --all-targets -- -D warnings, with and without --all-features: clean.

I didn't test with game data.


Tested locally with an AI-assisted script (Claude Code); I checked the results before posting.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7144613915 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1280#issuecomment-7144613915). Originally posted by [acetonahard-code](https://github.com/acetonahard-code) on 2026-11-24T21:12:07Z. --- Tested on Windows 11, RTX 4070 SUPER (driver 621.14), with the repo toolchain, merged into current `main` (ffe310e): | `cargo test -p victory-shader` | passed | failed | ignored | |---|---|---|---| | `main` | 120 | 0 | 1 | | `main` + this PR | 122 | 0 | 1 | - The 2 new tests pass. - No GPU test skipped for lack of a device. - `cargo clippy --all-targets -- -D warnings`, with and without `--all-features`: clean. I didn't test with game data. --- <sub>Tested locally with an AI-assisted script (Claude Code); I checked the results before posting.</sub>
upstream-sync closed this pull request 2026-11-25 01:06:08 +00:00
Marvisak commented 2026-11-25 01:08:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:36:36Z.


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 gradients are in main (0b35b7fe, 4f57f46f), the cube case as SampleLevel from the face-space gradients.

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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148691105 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1280#issuecomment-7148691105). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:36:36Z. --- 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 gradients are in main (0b35b7fe, 4f57f46f), the cube case as SampleLevel from the face-space gradients. 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.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!1280
No description provided.