Shaders: scale unnormalized sample gradients and honor FORCE_UNNORMALIZED #3716

Open
Moon0755 wants to merge 1 commit from upstream-pr/3716 into main
Moon0755 commented 2026-11-28 10:46:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Moon0755 on 2026-11-28T10:46:42Z.


Fixes two unnormalized texture sampling bugs in DXIL lowering:

  1. image_sample_d / image_sample_d_cl with unnormalized coordinates (crates/victory-shader/src/emit/lower.rs): dx.op.getDimensions scaled coordinates cs[0..spatial] by (W, H, D), while explicit derivatives ddx/ddy (g[0..spatial] and g[3..3+spatial]) were passed unscaled to dx.op.sampleGrad.f32. In RDNA2 unnormalized mode, gradients are also in texel units, so leaving them unscaled overestimates LOD by log2(dim) (selecting mip 4 [4.0] on a 16×16 texture instead of mips [0.0, 2.0]). Both g[0..spatial] and g[3..3+spatial] are now divided by level-0 spatial dimensions.
  2. SQ_IMG_SAMP_WORD0 bit 15 (FORCE_UNNORMALIZED) on immediate samplers (crates/victory-shader/src/emit/lower.rs): RDNA2 enables texel coordinates whenever inst.unorm || S#.FORCE_UNNORMALIZED (bit 15 of word 0). force_unnormalized now reads bit 15 across Source::Constant and constant-pair Source::Phi sampler handles (returning an explicit Err if a Phi mixes unnormalized and normalized immediate samplers), and applies the dimension scaling (or rejection on 2D image_gather4_l(_o) and cube samples) consistently with inst.unorm.

Changes

  • crates/victory-shader/src/emit/lower.rs (522,176 B < 524,288 B): add force_unnormalized, compute unnormalized = *unorm || force_unnormalized(ss)?.unwrap_or(false), scale g[0..spatial] and g[3..3+spatial] by level-0 dimensions in op == 63.
  • crates/victory-shader/src/emit/stride_tests.rs: add mixed_force_unnormalized_sampler_phi_is_refused.
  • crates/victory-shader/src/emit/synthetic_tests.rs: add synthetic GPU/WARP tests unnormalized_explicit_gradients_select_their_mips (image_sample_d and image_sample_d_cl on mipmapped 2D View::Levels(16, 5)) and sampler_force_unnormalized_makes_texel_coordinates (image_sample with S#.word0 = 0x8092).

Verification

  • Red-before-fix on upstream/main (63bcca4): all 3 tests fail (0 passed, 3 failed):
    • unnormalized_explicit_gradients_select_their_mips: left: [4.0, 4.0], right: [0.0, 2.0]
    • sampler_force_unnormalized_makes_texel_coordinates: left: [256.0], right: [150.0]
    • mixed_force_unnormalized_sampler_phi_is_refused: mixed texel-coordinate modes must be refused
  • After fix:
    • cargo test --locked -p victory-shader --lib --features host -- unnormalized: 3 passed, 0 failed (local hardware adapter + WARP).
    • cargo test --locked -p victory-shader --lib --features host -- image_addresses_of_16_bits_and_clamps: 1 passed, 0 failed.
    • cargo clippy --locked -p victory-shader --all-targets --features host -- -D warnings: clean (exit 0).
    • cargo run --locked -p xtask -- policy: asset policy: 191 files ok.

AI-assisted; synthetic tests only, no game files.

— Devin & Neon

<!-- greyfinch-sync:Marvisak/VIctory:issue:5539318754 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3716). Originally posted by [Moon0755](https://github.com/Moon0755) on 2026-11-28T10:46:42Z. --- Fixes two unnormalized texture sampling bugs in DXIL lowering: 1. **`image_sample_d` / `image_sample_d_cl` with unnormalized coordinates (`crates/victory-shader/src/emit/lower.rs`)**: `dx.op.getDimensions` scaled coordinates `cs[0..spatial]` by `(W, H, D)`, while explicit derivatives `ddx`/`ddy` (`g[0..spatial]` and `g[3..3+spatial]`) were passed unscaled to `dx.op.sampleGrad.f32`. In RDNA2 unnormalized mode, gradients are also in texel units, so leaving them unscaled overestimates LOD by `log2(dim)` (selecting mip 4 `[4.0]` on a 16×16 texture instead of mips `[0.0, 2.0]`). Both `g[0..spatial]` and `g[3..3+spatial]` are now divided by level-0 spatial dimensions. 2. **`SQ_IMG_SAMP_WORD0` bit 15 (`FORCE_UNNORMALIZED`) on immediate samplers (`crates/victory-shader/src/emit/lower.rs`)**: RDNA2 enables texel coordinates whenever `inst.unorm || S#.FORCE_UNNORMALIZED` (bit 15 of word 0). `force_unnormalized` now reads bit 15 across `Source::Constant` and constant-pair `Source::Phi` sampler handles (returning an explicit `Err` if a `Phi` mixes unnormalized and normalized immediate samplers), and applies the dimension scaling (or rejection on 2D `image_gather4_l(_o)` and cube samples) consistently with `inst.unorm`. ### Changes - **`crates/victory-shader/src/emit/lower.rs`** (`522,176 B` < `524,288 B`): add `force_unnormalized`, compute `unnormalized = *unorm || force_unnormalized(ss)?.unwrap_or(false)`, scale `g[0..spatial]` and `g[3..3+spatial]` by level-0 dimensions in `op == 63`. - **`crates/victory-shader/src/emit/stride_tests.rs`**: add `mixed_force_unnormalized_sampler_phi_is_refused`. - **`crates/victory-shader/src/emit/synthetic_tests.rs`**: add synthetic GPU/WARP tests `unnormalized_explicit_gradients_select_their_mips` (`image_sample_d` and `image_sample_d_cl` on mipmapped 2D `View::Levels(16, 5)`) and `sampler_force_unnormalized_makes_texel_coordinates` (`image_sample` with `S#.word0 = 0x8092`). ### Verification - **Red-before-fix on `upstream/main` (`63bcca4`)**: all 3 tests fail (**0 passed, 3 failed**): - `unnormalized_explicit_gradients_select_their_mips`: `left: [4.0, 4.0], right: [0.0, 2.0]` - `sampler_force_unnormalized_makes_texel_coordinates`: `left: [256.0], right: [150.0]` - `mixed_force_unnormalized_sampler_phi_is_refused`: `mixed texel-coordinate modes must be refused` - **After fix**: - `cargo test --locked -p victory-shader --lib --features host -- unnormalized`: **3 passed, 0 failed** (local hardware adapter + WARP). - `cargo test --locked -p victory-shader --lib --features host -- image_addresses_of_16_bits_and_clamps`: **1 passed, 0 failed**. - `cargo clippy --locked -p victory-shader --all-targets --features host -- -D warnings`: **clean** (exit 0). - `cargo run --locked -p xtask -- policy`: `asset policy: 191 files ok`. AI-assisted; synthetic tests only, no game files. — Devin & Neon
oni-swr commented 2026-11-28 13:11:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-28T13:11:37Z.


Independent software-only WARP verification of #3716.

  • Pinned main 63bcca43c6a00a758510398fa43096ce7e4f7df0, PR head cd3cf73e59ff9ded9a00c09579158d5e550831bc, and GitHub test merge 0eacc6c312b85a10b430589120976fcff4171609. The test merge has those exact parents. The Windows CI check passed for the pinned head.
  • Windows 11 25H2 (26200.9974), Rust 1.98.1, Windows SDK 10.0.26100.0 x64 dxil.dll (file version 1.8.2502.11). Both VICTORY_WARP=1 and VICTORY_D3D12_WARP=1 were set. All adapter calls in the shader harness ran on WARP, including passes labeled “hardware” by the tests. Cargo used -j 4.
  • Targeted unnormalized tests. Main with only the two PR test files overlaid had 0 passed, 3 failed. Explicit gradients selected mips [4, 4] instead of [0, 2], the immediate sampler returned 256 instead of 150, and mixed immediate sampler modes were accepted. All three passed on the test merge.
  • Full shader library suite with host. Clean main had 298 passed, 4 failed, 3 ignored. The test merge had 301 passed, 4 failed, 3 ignored. The same four tests failed on both revisions, covering three wave-32 pipeline cases and one gather comparison.
  • Shader Clippy with warnings denied passed. The asset policy passed for 191 files. lower.rs was 522,176 bytes, 2,112 bytes under the 524,288-byte limit.

Synthetic tests only. AI-assisted with OpenAI Codex (gpt-6.2-sol), which ran the verification. The original Gemini 4 Neon launch hit quota and the Claude Sonnet 5.5 restart hit an authentication error before either ran tests.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7239760263 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3716#issuecomment-7239760263). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-28T13:11:37Z. --- Independent software-only WARP verification of #3716. - Pinned main `63bcca43c6a00a758510398fa43096ce7e4f7df0`, PR head `cd3cf73e59ff9ded9a00c09579158d5e550831bc`, and GitHub test merge `0eacc6c312b85a10b430589120976fcff4171609`. The test merge has those exact parents. The [Windows CI check](https://github.com/Marvisak/VIctory/actions/runs/46136769603/job/140953705218) passed for the pinned head. - Windows 11 25H2 (26200.9974), Rust 1.98.1, Windows SDK 10.0.26100.0 x64 `dxil.dll` (file version 1.8.2502.11). Both `VICTORY_WARP=1` and `VICTORY_D3D12_WARP=1` were set. All adapter calls in the shader harness ran on WARP, including passes labeled “hardware” by the tests. Cargo used `-j 4`. - **Targeted unnormalized tests**. Main with only the two PR test files overlaid had **0 passed, 3 failed**. Explicit gradients selected mips `[4, 4]` instead of `[0, 2]`, the immediate sampler returned `256` instead of `150`, and mixed immediate sampler modes were accepted. All three passed on the test merge. - **Full shader library suite with `host`**. Clean main had **298 passed, 4 failed, 3 ignored**. The test merge had **301 passed, 4 failed, 3 ignored**. The same four tests failed on both revisions, covering three wave-32 pipeline cases and one gather comparison. - Shader Clippy with warnings denied passed. The asset policy passed for **191 files**. `lower.rs` was **522,176 bytes**, 2,112 bytes under the 524,288-byte limit. Synthetic tests only. AI-assisted with OpenAI Codex (`gpt-6.2-sol`), which ran the verification. The original Gemini 4 Neon launch hit quota and the Claude Sonnet 5.5 restart hit an authentication error before either ran tests.
upipibi commented 2026-11-28 15:30:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-28T15:30:04Z.


NVIDIA row for this PR: it passes on Ampere, and the three new tests fail on this card without the fix. This adds a hardware run to oni-swr's WARP-only one.

RTX 3070 (desktop), driver 616.56, Windows 11 Pro 10.0.26100, debug. Head cd3cf73 on main 63bcca4 (one commit, clean).

  • synthetic_tests::unnormalized_explicit_gradients_select_their_mips, synthetic_tests::sampler_force_unnormalized_makes_texel_coordinates and stride_tests::mixed_force_unnormalized_sampler_phi_is_refused: 3 passed, 0 failed, in 3 of 3 runs. The two synthetic tests run on the hardware adapter and then on WARP. The mixed-phi test only translates and uses no device.

  • run_unnormalized_shader skips an adapter it cannot open and prints nothing, so a pass alone does not show that WARP ran. One extra run with VICTORY_WARP=1, where every device is WARP, also passed 3 of 3.

  • Negative control: the same tests with main's crates/victory-shader/src/emit/lower.rs (git diff --stat HEAD: 1 file, 3 insertions, 39 deletions) gave 0 passed, 3 failed, in 3 of 3 runs. The failures are the ones the description quotes: hardware: explicit gradients gave [4.0, 4.0], want [0.0, 2.0]; hardware: FORCE_UNNORMALIZED gave [256.0], want [150.0]; and "mixed texel-coordinate modes must be refused". The VICTORY_WARP=1 control fails with the same values. Its messages still say "hardware", but that run is WARP.

  • cargo test --locked -p victory-shader --lib -- --skip heap_direct_texture_reads_mark_their_heap_index:

    • this PR: 301 passed, 3 failed, 3 ignored;
    • main 63bcca4: 298 passed, 3 failed, 3 ignored.

    The 3 failures are the same on both: stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros and synthetic_tests::gathers_at_a_level_match_the_hardware. The skipped test crashes the binary on this card (open issue #2571).

Nothing NVIDIA-specific: the card gives the values the description and oni-swr's WARP run give, with and without the fix.

Not tested: a title run; --features host (the PR's own command; the run above uses the default features, which build the host checks here too).

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7241513223 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3716#issuecomment-7241513223). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-28T15:30:04Z. --- NVIDIA row for this PR: it passes on Ampere, and the three new tests fail on this card without the fix. This adds a hardware run to oni-swr's WARP-only one. RTX 3070 (desktop), driver 616.56, Windows 11 Pro 10.0.26100, debug. Head `cd3cf73` on main `63bcca4` (one commit, clean). - `synthetic_tests::unnormalized_explicit_gradients_select_their_mips`, `synthetic_tests::sampler_force_unnormalized_makes_texel_coordinates` and `stride_tests::mixed_force_unnormalized_sampler_phi_is_refused`: 3 passed, 0 failed, in 3 of 3 runs. The two synthetic tests run on the hardware adapter and then on WARP. The mixed-phi test only translates and uses no device. - `run_unnormalized_shader` skips an adapter it cannot open and prints nothing, so a pass alone does not show that WARP ran. One extra run with `VICTORY_WARP=1`, where every device is WARP, also passed 3 of 3. - Negative control: the same tests with main's `crates/victory-shader/src/emit/lower.rs` (`git diff --stat HEAD`: 1 file, 3 insertions, 39 deletions) gave 0 passed, 3 failed, in 3 of 3 runs. The failures are the ones the description quotes: `hardware: explicit gradients` gave `[4.0, 4.0]`, want `[0.0, 2.0]`; `hardware: FORCE_UNNORMALIZED` gave `[256.0]`, want `[150.0]`; and "mixed texel-coordinate modes must be refused". The `VICTORY_WARP=1` control fails with the same values. Its messages still say "hardware", but that run is WARP. - `cargo test --locked -p victory-shader --lib -- --skip heap_direct_texture_reads_mark_their_heap_index`: - this PR: 301 passed, 3 failed, 3 ignored; - main `63bcca4`: 298 passed, 3 failed, 3 ignored. The 3 failures are the same on both: `stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride`, `stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros` and `synthetic_tests::gathers_at_a_level_match_the_hardware`. The skipped test crashes the binary on this card (open issue #2571). Nothing NVIDIA-specific: the card gives the values the description and oni-swr's WARP run give, with and without the fix. Not tested: a title run; `--features host` (the PR's own command; the run above uses the default features, which build the host checks here too). AI-assisted (Claude Code, Claude Opus 5.5).
ColorMeBoom commented 2026-11-28 15:51:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ColorMeBoom on 2026-11-28T15:51:48Z.


Hardware run: RX 5700 XT (Navi 10, RDNA1, 32–64 lanes, driver 32.0.21045.29003, Windows 11 26200), head cd3cf73 on 63bcca4.

  • sampler_force_unnormalized_makes_texel_coordinates, unnormalized_explicit_gradients_select_their_mips and mixed_force_unnormalized_sampler_phi_is_refused pass.
  • Full victory-shader --lib --features host: 305 passed, 0 failed, 3 ignored.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7241792806 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3716#issuecomment-7241792806). Originally posted by [ColorMeBoom](https://github.com/ColorMeBoom) on 2026-11-28T15:51:48Z. --- Hardware run: RX 5700 XT (Navi 10, RDNA1, 32–64 lanes, driver 32.0.21045.29003, Windows 11 26200), head `cd3cf73` on `63bcca4`. - `sampler_force_unnormalized_makes_texel_coordinates`, `unnormalized_explicit_gradients_select_their_mips` and `mixed_force_unnormalized_sampler_phi_is_refused` pass. - Full `victory-shader --lib --features host`: 305 passed, 0 failed, 3 ignored. AI use: Claude Code (Claude Opus 5.5).
kilolimaVI commented 2026-11-28 21:56:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kilolimaVI on 2026-11-28T21:56:10Z.


AMD Radeon 780M (RDNA3) + WARP, Windows 11, head cd3cf73. Before (main 63bcca4 + this PR's tests): all three fail; sampler_force_unnormalized_makes_texel_coordinates reads 256.0 for 150.0 on the hardware adapter, and the mixed-mode phi is not refused. After: 3/3 pass, 5 of 5 runs. Full suite: 554 passed. AI-assisted (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7246379299 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3716#issuecomment-7246379299). Originally posted by [kilolimaVI](https://github.com/kilolimaVI) on 2026-11-28T21:56:10Z. --- AMD Radeon 780M (RDNA3) + WARP, Windows 11, head `cd3cf73`. Before (main `63bcca4` + this PR's tests): all three fail; `sampler_force_unnormalized_makes_texel_coordinates` reads 256.0 for 150.0 on the hardware adapter, and the mixed-mode phi is not refused. After: 3/3 pass, 5 of 5 runs. Full suite: 554 passed. AI-assisted (Claude).
andrewpozdnakov7-jpg commented 2026-11-28 22:38:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T22:38:14Z.


I checked whether cd3cf73 can land with the two FP16 changes in #3617 and #3717. Git merges them cleanly with the verified rounding follow-up. Even so, xtask policy rejects the resulting lower.rs at 524,561 bytes. The repository limit is 524,288 bytes.

Combined 2045456 includes the exact sampling PR, the tested FP16 integration and the test split in #3797. Its lower.rs is 472,291 bytes. The asset policy passes on all 192 files. All three sampling regressions, both FP16 regressions and the 33 lowerer tests passed locally, using Microsoft WARP for the graphics cases. Both strict shader Clippy configurations passed.

The maintainer can land #3797 before taking the sampling and FP16 changes together. Please retain the Git author on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding the changes into another commit.

AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7246875331 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3716#issuecomment-7246875331). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-28T22:38:14Z. --- I checked whether `cd3cf73` can land with the two FP16 changes in #3617 and #3717. Git merges them cleanly with the verified rounding follow-up. Even so, `xtask policy` rejects the resulting `lower.rs` at 524,561 bytes. The repository limit is 524,288 bytes. [Combined `2045456`](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/20454567c840a2db01166eb4c0e319fa0dee86f4) includes the exact sampling PR, the [tested FP16 integration](https://github.com/Marvisak/VIctory/pull/3717#issuecomment-7246508662) and the test split in #3797. Its `lower.rs` is 472,291 bytes. The asset policy passes on all 192 files. All three sampling regressions, both FP16 regressions and the 33 lowerer tests passed locally, using Microsoft WARP for the graphics cases. Both strict shader Clippy configurations passed. The maintainer can land #3797 before taking the sampling and FP16 changes together. Please retain the Git author on cherry-pick or rebase, or add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>` if folding the changes into another commit. AI-assisted with OpenAI Codex.
kilolimaVI commented 2026-11-29 14:04:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kilolimaVI on 2026-11-29T14:04:57Z.


AMD row, from the integration runs on a Radeon 780M (RDNA3, driver 32.0.13022.3006, Windows 11): this PR at cd3cf73 is part of kilolimaVI/VIctory integration/shader-6 through integration/shader-10 (with #3097, #2999, #3617, #3717, #1315, #2398, #385, #1262, #3401). On each of those trees the whole victory-shader lib passed with 0 failures (318 to 354 tests), which includes unnormalized_explicit_gradients_select_their_mips, sampler_force_unnormalized_makes_texel_coordinates and mixed_force_unnormalized_sampler_phi_is_refused; victory-gpu (diagnostics) 246 passed. So the values upipibi got on the RTX 3070 hold on AMD too.

Its one conflict there is with #2999: both append tests at the end of synthetic_tests.rs (kept both).

AI-assisted (Claude).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7257025422 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3716#issuecomment-7257025422). Originally posted by [kilolimaVI](https://github.com/kilolimaVI) on 2026-11-29T14:04:57Z. --- AMD row, from the integration runs on a Radeon 780M (RDNA3, driver 32.0.13022.3006, Windows 11): this PR at `cd3cf73` is part of `kilolimaVI/VIctory` `integration/shader-6` through `integration/shader-10` (with #3097, #2999, #3617, #3717, #1315, #2398, #385, #1262, #3401). On each of those trees the whole victory-shader lib passed with 0 failures (318 to 354 tests), which includes `unnormalized_explicit_gradients_select_their_mips`, `sampler_force_unnormalized_makes_texel_coordinates` and `mixed_force_unnormalized_sampler_phi_is_refused`; victory-gpu (diagnostics) 246 passed. So the values upipibi got on the RTX 3070 hold on AMD too. Its one conflict there is with #2999: both append tests at the end of `synthetic_tests.rs` (kept both). AI-assisted (Claude).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3716:upstream-pr/3716'
git switch 'upstream-pr/3716'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3716'
git switch 'upstream-pr/3716'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3716'
git switch 'upstream-pr/3716'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3716'
git switch 'main'
git merge --squash 'upstream-pr/3716'
git switch 'main'
git merge --ff-only 'upstream-pr/3716'
git switch 'main'
git merge 'upstream-pr/3716'
git push origin 'main'
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!3716
No description provided.