Recompiler: honor S# FORCE_UNORMALIZED and scale unnormalized image_sample_d gradients in direct DXIL #3254

Closed
Kalmarv wants to merge 1 commit from upstream-pr/3254 into main
Kalmarv commented 2026-11-29 01:10:47 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T01:10:47Z.


Honor RDNA2 sampler FORCE_UNORMALIZED (S# word 0 bit 15) on direct-DXIL image_sample* / image_gather4* instructions, and scale explicit image_sample_d gradients (dx, dy) by mip dimensions when sampling with unnormalized coordinates (UNORM=1 or FORCE_UNORMALIZED=1).

On RDNA2, a sampler descriptor (S#) can set FORCE_UNORMALIZED (w[0][15]), causing texel-space coordinates even when the MIMG instruction itself has UNORM=0 (and for image_sample_d, explicit gradients (dx, dy) are also in texel units rather than normalized [0, 1] UV space). Previously, direct DXIL only normalized coordinates when d.unorm was set on the instruction and left image_sample_d gradients unscaled, so FORCE_UNORMALIZED=1 samplers read wrong texels and unnormalized image_sample_d selected the wrong mip level.

What is in it

  • crates/xcl/src/gpu/dxil/lower_image.rs:
    • Scale explicit (dx, dy) gradients by mip dimensions (sizes[0..spatial_n]) in mimg_unorm_sample so image_sample_d with UNORM=1 or static FORCE_UNORMALIZED=1 computes correct normalized derivatives.
    • Check static sampler w[0] & (1 << 15) in emit_mimg to route static FORCE_UNORMALIZED=1 directly to mimg_unorm_sample (zero runtime branch) while keeping zero getDimensions overhead when static FORCE_UNORMALIZED=0.
    • For dynamic heap S# on non-cube dimensions in sample_body, test wave-uniform s0[15] via lw.guarded to divide spatial coordinates (and explicit gradients for has_d) by getDimensions(tex, level) when FORCE_UNORMALIZED=1.
    • Add DXC 1.9 validation test force_unorm_tests::static_and_dynamic_force_unormalized_validate and guarded RTX GPU test force_unorm_tests::gpu_force_unormalized_sample_and_unorm_sample_d.

Tested

  • Hardware / toolchain: NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074), AMD Ryzen 9 9950X, Windows 11 Pro (build 26200), rustc 1.99.0 (b940084d7 2026-09-28), DXC 1.9 (XCL_DXC_DIR), debug for tests and release for xcl-smoke.
  • Targeted tests (cargo test -p xcl --lib -- force_unorm_tests gather4_lod_tests heap_bounds_tests --nocapture): 8 passed, 0 failed (0 skips on RTX 5070 Ti; verified FORCE_UNORMALIZED=1 IMAGE_SAMPLE_LZ, FORCE_UNORMALIZED=0, UNORM=1 IMAGE_SAMPLE_D mip 0, and FORCE_UNORMALIZED=1 IMAGE_SAMPLE_D mip 1).
  • Full workspace (cargo test --workspace -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::): 2601 passed, 0 failed, 49 ignored.
  • SKIPPED GPU tests on this adapter (26 total): 21 no wave64 (RTX 5070 Ti is wave32), 3 wave32 per-lane #1426 f16/cvt-f16 refusals, 2 no heap windows (opt-in XCL_HEAP_WINDOWS=1).
  • Retail title (ea3b5e4d) 150 s regression run below (0 device removals, 0 crashes, 1605 frames vs 1695 on main, 32312 MB peak commit vs 31546 MB on main).

Regression run

xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB
xcl-smoke v1
commit: ff6be42ecbb9a67fdc348a4082ba5a04e5f10ba7
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1605 (PresentX 1605)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 32312 MB

Not tested

  • AMD RDNA / wave64 GPUs, 1D/3D/array dynamic FORCE_UNORMALIZED variants on hardware (covered by DXIL validation; GPU test exercises 2D IMAGE_SAMPLE_LZ and IMAGE_SAMPLE_D), and retail titles with FORCE_UNORMALIZED=1 samplers (ea3b5e4d smoke is a regression check).
  • uma_driver::tests:: (skipped locally due to pre-existing 0x7C10003 exit on NVIDIA) and test_title_runs_and_game_folder_is_untouched.

AI use

  • Antigravity (Gemini) used for root-cause verification, implementation, test authoring, and local verification; all diffs, tests, and smoke outputs reviewed and run locally.
<!-- greyfinch-sync:ivall/xcl:issue:5546319671 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3254). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-29T01:10:47Z. --- Honor RDNA2 sampler `FORCE_UNORMALIZED` (`S#` word 0 bit 15) on direct-DXIL `image_sample*` / `image_gather4*` instructions, and scale explicit `image_sample_d` gradients `(dx, dy)` by mip dimensions when sampling with unnormalized coordinates (`UNORM=1` or `FORCE_UNORMALIZED=1`). On RDNA2, a sampler descriptor (`S#`) can set `FORCE_UNORMALIZED` (`w[0][15]`), causing texel-space coordinates even when the `MIMG` instruction itself has `UNORM=0` (and for `image_sample_d`, explicit gradients `(dx, dy)` are also in texel units rather than normalized `[0, 1]` UV space). Previously, direct DXIL only normalized coordinates when `d.unorm` was set on the instruction and left `image_sample_d` gradients unscaled, so `FORCE_UNORMALIZED=1` samplers read wrong texels and unnormalized `image_sample_d` selected the wrong mip level. **What is in it** - `crates/xcl/src/gpu/dxil/lower_image.rs`: - Scale explicit `(dx, dy)` gradients by mip dimensions (`sizes[0..spatial_n]`) in `mimg_unorm_sample` so `image_sample_d` with `UNORM=1` or static `FORCE_UNORMALIZED=1` computes correct normalized derivatives. - Check static sampler `w[0] & (1 << 15)` in `emit_mimg` to route static `FORCE_UNORMALIZED=1` directly to `mimg_unorm_sample` (zero runtime branch) while keeping zero `getDimensions` overhead when static `FORCE_UNORMALIZED=0`. - For dynamic heap `S#` on non-cube dimensions in `sample_body`, test wave-uniform `s0[15]` via `lw.guarded` to divide spatial coordinates (and explicit gradients for `has_d`) by `getDimensions(tex, level)` when `FORCE_UNORMALIZED=1`. - Add DXC 1.9 validation test `force_unorm_tests::static_and_dynamic_force_unormalized_validate` and guarded RTX GPU test `force_unorm_tests::gpu_force_unormalized_sample_and_unorm_sample_d`. **Tested** - Hardware / toolchain: NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074), AMD Ryzen 9 9950X, Windows 11 Pro (build 26200), `rustc 1.99.0 (b940084d7 2026-09-28)`, DXC 1.9 (`XCL_DXC_DIR`), debug for tests and release for `xcl-smoke`. - Targeted tests (`cargo test -p xcl --lib -- force_unorm_tests gather4_lod_tests heap_bounds_tests --nocapture`): 8 passed, 0 failed (0 skips on RTX 5070 Ti; verified `FORCE_UNORMALIZED=1 IMAGE_SAMPLE_LZ`, `FORCE_UNORMALIZED=0`, `UNORM=1 IMAGE_SAMPLE_D` mip 0, and `FORCE_UNORMALIZED=1 IMAGE_SAMPLE_D` mip 1). - Full workspace (`cargo test --workspace -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::`): 2601 passed, 0 failed, 49 ignored. - `SKIPPED` GPU tests on this adapter (26 total): 21 `no wave64` (RTX 5070 Ti is wave32), 3 wave32 per-lane `#1426` f16/cvt-f16 refusals, 2 `no heap windows` (opt-in `XCL_HEAP_WINDOWS=1`). - Retail title (`ea3b5e4d`) 150 s regression run below (`0` device removals, `0` crashes, `1605` frames vs `1695` on `main`, `32312 MB` peak commit vs `31546 MB` on `main`). **Regression run** ``` xcl-smoke v1 commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1695 (PresentX 1695) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 31546 MB ``` ``` xcl-smoke v1 commit: ff6be42ecbb9a67fdc348a4082ba5a04e5f10ba7 gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1605 (PresentX 1605) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 32312 MB ``` **Not tested** - AMD RDNA / wave64 GPUs, 1D/3D/array dynamic `FORCE_UNORMALIZED` variants on hardware (covered by DXIL validation; GPU test exercises 2D `IMAGE_SAMPLE_LZ` and `IMAGE_SAMPLE_D`), and retail titles with `FORCE_UNORMALIZED=1` samplers (`ea3b5e4d` smoke is a regression check). - `uma_driver::tests::` (skipped locally due to pre-existing `0x7C10003` exit on NVIDIA) and `test_title_runs_and_game_folder_is_untouched`. **AI use** - Antigravity (Gemini) used for root-cause verification, implementation, test authoring, and local verification; all diffs, tests, and smoke outputs reviewed and run locally.
Foniass commented 2026-12-01 04:53:00 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T04:53:00Z.


Review (head ff6be42e, diff read, not a merge-check; it conflicts with main, so not built or run):

  • The rebase changes one line of logic, not just text. Since 4250e761 (waterfall collapse, the cause of the conflict next to the new s0 line), waterfall::collapse removes the v_readfirstlane writes to s[sb..sb+3] in a collapsed sampler loop. So the new let s0 = self.sreg(ssamp) (lower_image.rs ~1177) reads a stale SGPR there, and its bit 15 decides whether coordinates and gradients are divided by the texture size. Read word 0 from the per-lane VGPR instead (vreg(waterfall_ssamp[pc] - 3), as mimg_s3 does); the branch then varies per lane. (Main's mimg_gather4_lod reads s0-s2 the same stale way, an existing bug.)
  • The HLSL backend (recomp/emit.rs ~2808) still ignores FORCE_UNORMALIZED, so the backends disagree.
  • The dynamic check adds a branch and a conditional getDimensions to every non-cube sample with a heap S#; the PR's 150 s smoke run shows 1605 vs 1695 frames and +766 MB peak commit, unexplained.
  • The static-S# path is only checked by DXIL validation, not on a GPU. AI use says "Gemini" without the exact model.

Fine: not superseded (on main FORCE_UNORMALIZED is only honoured in gather4_l); dividing gradients by the mip-0 size in mimg_unorm_sample, adding offsets after normalising and skipping cubes look right; the GPU test covers the dynamic FORCE_UNORMALIZED sample, UNORM=1 sample_d and FORCE_UNORMALIZED sample_d, and would fail without the fix.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7301199726 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3254#issuecomment-7301199726). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T04:53:00Z. --- Review (head ff6be42e, diff read, not a merge-check; it conflicts with main, so not built or run): - **The rebase changes one line of logic, not just text.** Since 4250e761 (waterfall collapse, the cause of the conflict next to the new `s0` line), `waterfall::collapse` removes the `v_readfirstlane` writes to `s[sb..sb+3]` in a collapsed sampler loop. So the new `let s0 = self.sreg(ssamp)` (`lower_image.rs` ~1177) reads a stale SGPR there, and its bit 15 decides whether coordinates and gradients are divided by the texture size. Read word 0 from the per-lane VGPR instead (`vreg(waterfall_ssamp[pc] - 3)`, as `mimg_s3` does); the branch then varies per lane. (Main's `mimg_gather4_lod` reads s0-s2 the same stale way, an existing bug.) - The HLSL backend (`recomp/emit.rs` ~2808) still ignores FORCE_UNORMALIZED, so the backends disagree. - The dynamic check adds a branch and a conditional `getDimensions` to every non-cube sample with a heap S#; the PR's 150 s smoke run shows 1605 vs 1695 frames and +766 MB peak commit, unexplained. - The static-S# path is only checked by DXIL validation, not on a GPU. AI use says "Gemini" without the exact model. Fine: not superseded (on main FORCE_UNORMALIZED is only honoured in `gather4_l`); dividing gradients by the mip-0 size in `mimg_unorm_sample`, adding offsets after normalising and skipping cubes look right; the GPU test covers the dynamic FORCE_UNORMALIZED sample, UNORM=1 `sample_d` and FORCE_UNORMALIZED `sample_d`, and would fail without the fix. AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Kalmarv commented 2026-12-02 20:34:06 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T20:34:06Z.


Consolidated into #3728 (Direct DXIL: MIMG operand layout fixes (A16 bias VGPR, S# FORCE_UNORMALIZED)), which includes the S# FORCE_UNORMALIZED / unnormalized image_sample_d gradient fix and addresses the review feedback on collapsed 4-dword sampler waterfall loops via mimg_s0 plus a dedicated GPU regression test. Closing this PR as superseded.

(Commented via Antigravity (Gemini 4 Neon).)

<!-- greyfinch-sync:ivall/xcl:comment:7357947096 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3254#issuecomment-7357947096). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-12-02T20:34:06Z. --- Consolidated into #3728 (`Direct DXIL: MIMG operand layout fixes (A16 bias VGPR, S# FORCE_UNORMALIZED)`), which includes the `S#` `FORCE_UNORMALIZED` / unnormalized `image_sample_d` gradient fix and addresses the review feedback on collapsed 4-dword sampler waterfall loops via `mimg_s0` plus a dedicated GPU regression test. Closing this PR as superseded. *(Commented via Antigravity (Gemini 4 Neon).)*

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
xcl-dev/xcl!3254
No description provided.