shader: SampleLevel(0) for constant-coordinate nonuniform IMAGE_SAMPLE #3496

Merged
Leyukaka merged 1 commit from upstream-pr/3496 into main 2026-11-30 02:20:22 +00:00
Leyukaka commented 2026-11-29 17:58:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T17:58:41Z.


Fixes #3489.

Cause

native_fixed_mesh57_waterfall_fine_hardware fails on one hardware adapter because of how its driver handles the emitted shader, not because of the lowering.

The pixel shader emitted at 26df1826 is correct per D3D (dxc -dumpbin of the cached container):

  • both coordinates are the literal 0.5;
  • the gradients passed to sampleGrad are DerivCoarseX/Y of that constant, so they are zero;
  • the resource handle comes from createHandleFromHeap with nonUniformIndex=true;
  • the shader is straight-line, with no branch around the derivatives or the sample.

The defined result is mip 0 on every pixel. WARP returns it. On the hardware adapter, in each 2x2 quad the top-left and bottom-right pixels return mip 1 of the correct texture:

pixel (0,0): got 0xff0000ff, expected 0xffff0000
pixel (2,0): got 0xff0000ff, expected 0xffff0000
pixel (1,1): got 0xff00ff00, expected 0xffff0000
pixel (3,1): got 0xff00ff00, expected 0xffff0000

This is deterministic: 10/10 runs, same four pixels.

Experiment: with the fixture's IMAGE_SAMPLE replaced by IMAGE_SAMPLE_LZ (lowered to sampleLevel at level 0, same nonuniform heap index, same views and sampler), the test passes 10/10 on the same adapter. Descriptor selection under the nonuniform index is therefore correct. What misbehaves on that driver is SampleGrad with zero gradients under a nonuniform heap index.

The regular waterfall test does not exercise this: it expects mip 1, the last level of the 2-level view, so a LOD that is too high is masked.

Change

In src/gfx1013_direct_compute.inl, for a plain nonuniform IMAGE_SAMPLE (op 32 only) whose every coordinate operand is a fully parsed, finite decimal literal, emit sampleLevel at level 0 instead of the coarse derivatives and sampleGrad.

  • The bias, gradient, compare, LOD and clamp variants are not affected: the path requires the existing quad_gradient condition, which is op 32 with a nonuniform selection.
  • A coordinate that is an SSA value, undef, or a non-finite float (for A16, a non-finite half in either 16-bit half) keeps the previous sampleGrad emission.
  • The A16 and/lshr split moves after the check, unchanged.
  • src/gfx1013_compute_native.cpp gains #include <charconv> for std::from_chars, because the .inl is included in that file.

tests/native_direct_container_check.py adds two waterfall pixel shaders: constant U must produce exactly one sampleLevel and no sampleGrad; position-varying U must produce exactly one sampleGrad and no sampleLevel. Both must keep the nonuniform handle.

Evidence

Base 26df1826, head 6a9d2a02.

Host: Windows 10.0.26300, NVIDIA GeForce RTX 5080, driver 32.0.16.2101. WARP: Microsoft Basic Render Driver. The D3D12 debug layer is not installed on that host, so there is no validation output.

  • ctest -j 1 -R '^(native_fixed_mesh57_|native_direct_container$|native_direct_mesh$)': 22/22 passed, no skips.
  • native_fixed_mesh57_waterfall_fine_hardware --repeat until-fail:10: 10/10 passed. Baseline at 26df1826: 0/10.
  • The regular waterfall test (expects mip 1) still passes on WARP and on hardware.

Mutations:

Mutant Result
sampleLevel for every nonuniform IMAGE_SAMPLE, constant or not regular waterfall fails on WARP and hardware (mip 0 on all 8 pixels); native_direct_container fails
new path disabled waterfall_fine_hardware fails with the same 4 pixels as the baseline; native_direct_container fails

Not covered

  • This is a workaround for one driver behaviour. It is correct per D3D for constant coordinates, but it does not fix the driver behaviour itself: a nonuniform IMAGE_SAMPLE with non-constant coordinates still emits sampleGrad, and no test here measures an intermediate LOD on that path.
  • A heap sampler whose MipLODBias exceeds its MinLOD could select a different mip with SampleLevel(0) than with the zero-gradient SampleGrad. This case is not measured and the path is not restricted to zero-bias samplers.
  • Measured on one hardware adapter and one driver version only.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5553497443 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3496). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-29T17:58:41Z. --- Fixes #3489. ## Cause `native_fixed_mesh57_waterfall_fine_hardware` fails on one hardware adapter because of how its driver handles the emitted shader, not because of the lowering. The pixel shader emitted at 26df1826 is correct per D3D (`dxc -dumpbin` of the cached container): - both coordinates are the literal 0.5; - the gradients passed to `sampleGrad` are `DerivCoarseX/Y` of that constant, so they are zero; - the resource handle comes from `createHandleFromHeap` with `nonUniformIndex=true`; - the shader is straight-line, with no branch around the derivatives or the sample. The defined result is mip 0 on every pixel. WARP returns it. On the hardware adapter, in each 2x2 quad the top-left and bottom-right pixels return mip 1 of the correct texture: ``` pixel (0,0): got 0xff0000ff, expected 0xffff0000 pixel (2,0): got 0xff0000ff, expected 0xffff0000 pixel (1,1): got 0xff00ff00, expected 0xffff0000 pixel (3,1): got 0xff00ff00, expected 0xffff0000 ``` This is deterministic: 10/10 runs, same four pixels. Experiment: with the fixture's `IMAGE_SAMPLE` replaced by `IMAGE_SAMPLE_LZ` (lowered to `sampleLevel` at level 0, same nonuniform heap index, same views and sampler), the test passes 10/10 on the same adapter. Descriptor selection under the nonuniform index is therefore correct. What misbehaves on that driver is `SampleGrad` with zero gradients under a nonuniform heap index. The regular waterfall test does not exercise this: it expects mip 1, the last level of the 2-level view, so a LOD that is too high is masked. ## Change In `src/gfx1013_direct_compute.inl`, for a plain nonuniform `IMAGE_SAMPLE` (op 32 only) whose every coordinate operand is a fully parsed, finite decimal literal, emit `sampleLevel` at level 0 instead of the coarse derivatives and `sampleGrad`. - The bias, gradient, compare, LOD and clamp variants are not affected: the path requires the existing `quad_gradient` condition, which is op 32 with a nonuniform selection. - A coordinate that is an SSA value, `undef`, or a non-finite float (for A16, a non-finite half in either 16-bit half) keeps the previous `sampleGrad` emission. - The A16 `and`/`lshr` split moves after the check, unchanged. - `src/gfx1013_compute_native.cpp` gains `#include <charconv>` for `std::from_chars`, because the `.inl` is included in that file. `tests/native_direct_container_check.py` adds two waterfall pixel shaders: constant U must produce exactly one `sampleLevel` and no `sampleGrad`; position-varying U must produce exactly one `sampleGrad` and no `sampleLevel`. Both must keep the nonuniform handle. ## Evidence Base 26df1826, head 6a9d2a02. Host: Windows 10.0.26300, NVIDIA GeForce RTX 5080, driver 32.0.16.2101. WARP: Microsoft Basic Render Driver. The D3D12 debug layer is not installed on that host, so there is no validation output. - `ctest -j 1 -R '^(native_fixed_mesh57_|native_direct_container$|native_direct_mesh$)'`: 22/22 passed, no skips. - `native_fixed_mesh57_waterfall_fine_hardware --repeat until-fail:10`: 10/10 passed. Baseline at 26df1826: 0/10. - The regular waterfall test (expects mip 1) still passes on WARP and on hardware. Mutations: | Mutant | Result | |---|---| | `sampleLevel` for every nonuniform `IMAGE_SAMPLE`, constant or not | regular waterfall fails on WARP and hardware (mip 0 on all 8 pixels); `native_direct_container` fails | | new path disabled | `waterfall_fine_hardware` fails with the same 4 pixels as the baseline; `native_direct_container` fails | ## Not covered - This is a workaround for one driver behaviour. It is correct per D3D for constant coordinates, but it does not fix the driver behaviour itself: a nonuniform `IMAGE_SAMPLE` with non-constant coordinates still emits `sampleGrad`, and no test here measures an intermediate LOD on that path. - A heap sampler whose `MipLODBias` exceeds its `MinLOD` could select a different mip with `SampleLevel(0)` than with the zero-gradient `SampleGrad`. This case is not measured and the path is not restricted to zero-bias samplers. - Measured on one hardware adapter and one driver version only.
Not-Saii commented 2026-11-29 23:48:27 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Not-Saii on 2026-11-29T23:48:27Z.

Review: APPROVED


Reviewed at head 6a9d2a0212 with a static read, then a build and test run in an isolated Windows Sandbox with networking off. I found nothing blocking. There are two non-blocking requests and a few nits.

The change is narrow. At base, a nonuniform plain IMAGE_SAMPLE already emitted coarse derivatives of its coordinates plus sampleGrad. This PR changes only the case where every coordinate is a finite literal: a zero-gradient sampleGrad becomes sampleLevel at LOD 0. That adds no new reliance on helper lanes. The bias, gradient, compare, LOD and clamp variants stay out, because quad_gradient requires op 32.

Why a literal coordinate is the same in every lane. A literal operand has the same value in every DXIL lane by construction, helper lanes included, and base already differentiated that same literal. On the paths I traced, a register holds a decimal literal only when it was written with no predicate (vector_write with an empty graphics_predicate), when it came from a uniform full-wave v_writelane island, or when it is a scalar. A predicated write becomes a select SSA value, fails the from_chars check and falls back to sampleGrad. The helper_result_unobserved shortcut cannot leave covered-only values in a coordinate register here. Image instructions are lane observers, and their address VGPRs count as touched. So the coarse derivatives really are zero. I found no path where a literal coordinate differs across the quad.

SHOULD

  1. src/gfx1013_direct_compute.inl:1215-1218 and the 0.0 LOD argument: SampleLevel(0) matches zero-gradient SampleGrad only when the sampler's MipLODBias is no greater than its MinLOD. Zero gradients give log2(0) + bias = -inf, which clamps to MinLOD. LOD 0 gives clamp(0 + bias, MinLOD, MaxLOD). The runtime builds D3D samplers with a guest-supplied MipLODBias (src/startup_stubs/d3d12_xs_device.cpp:5512). So, by the D3D LOD rules, a positive-bias sampler on this path would select a coarser mip than before on every adapter, to work around one driver. This is derived from the LOD rules, not measured. The PR body lists this case as unmeasured. Two requests:
    • Make the new comment name sampler bias as the exception. As written, it says the sampler LOD limits are unchanged, which suggests the result is identical in all cases.
    • Question: does sampleLevel with a large negative LOD also pass on the affected adapter? For example -32.0, below the [-16, 15.99] bias range. That clamps to MinLOD exactly as zero gradients do, whatever the bias, so the divergence goes away. If it doesn't pass there, consider limiting the path to static samplers with zero bias.
  2. tests/native_direct_container_check.py:861-867: the new cases cover the 32-bit, 2D, all-literal branch and the SSA fallback. Nothing covers the A16 branch, where both halves are checked, or the non-finite fallback (a literal +inf U must keep sampleGrad). As far as I can see, a mutant that drops the exponent checks, or checks only the low half for A16, would pass every test here. One A16 constant case and one non-finite case would cover them.

NIT

  1. src/gfx1013_direct_compute.inl:1220: array dimensions also require the slice coordinate to be a literal, although slices are not differentiated. That is conservative and fine. A one-line comment would stop someone from "fixing" it later.
  2. PR body: the ctest command leaves out --preset dev. Add it so the command reproduces as written.
  3. The new container cases check IR text only. The hardware behaviour rests on the existing native_fixed_mesh57_waterfall_fine_hardware.

What each test proves

  • native_direct_container (new cases): with literal U/V on a nonuniform waterfall sample, the shader emits exactly one sampleLevel and no sampleGrad. With position-varying U it emits exactly one sampleGrad and no sampleLevel. Both keep the nonuniform createHandleFromHeap. This is the test that catches a revert, as shown below.
  • native_fixed_mesh57_waterfall_fine_{warp,hardware} (existing): this is the pixel-comparison test for the derivative/helper-lane case. Constant coordinates on a 2-mip 2x2 view with a point sampler must read mip 0 on all 8 pixels.
  • native_fixed_mesh57_waterfall_{warp,hardware} (existing): position-varying coordinates still read mip 1 on all 8 pixels, so the non-constant path still uses real gradients.
  • The other native_fixed_mesh57_* tests and native_direct_mesh are regression coverage for the shared lowering.

Evidence (Windows Sandbox, networking off)

  • Commit: 6a9d2a02, exported with git archive. Merge base 26df1826.
  • Configure: cmake --preset dev -DX2W_WITH_OPUS=OFF -DINTAKE_PYTHON=<venv python> -DOFFSCREEN_AGILITY_620_DIR=<Agility 1.620> -DOFFSCREEN_WARP_DIR=<WARP 1.0.22> (Debug)
  • Build: cmake --build --preset dev -j 16 --target compile_dxil startup_indirect_draw_indexed
  • Test, with X2W_D3D12_DEBUG=1: ctest --preset dev --no-tests=error --output-on-failure -V -R '^(native_fixed_mesh57_.*|native_direct_container|native_direct_mesh)$'
  • Head: 22 passed, 0 failed, 0 skipped (77). The 22 are 10 _warp, 10 _hardware, native_direct_container and native_direct_mesh.
  • Adapters as the fixtures print them: NVIDIA GeForce RTX 4090 (sandbox vGPU), driver 32.0.16.1088; Microsoft Basic Render Driver (WARP), 1.0.22.0.
  • OS: Windows 11 Enterprise 10.0.26100 (sandbox guest).
  • Debug layer: available (D3D12GetDebugInterface hr=0x00000000). All 20 native_fixed_mesh57_* tests ran with it enabled (X2W_D3D12_DEBUG=1) and printed debug layer: 0x00000000 enabled. No D3D12 ERROR, CORRUPTION or WARNING lines appeared in the logs. On this path the fixture does not fail on info-queue messages, so this shows only that none were printed. The two Python container tests create no device.
  • Base comparison: nothing failed at head, so no base rerun was needed.

Mutation. I applied the mutation to a scratch copy only, never to the branch. It sets sample_at_base_level = false && …, which restores the base emission. The rebuild check confirmed the outputs changed: x2w_shader_compiler, compile_dxil.exe and d3d12_xs.dll relinked. The hardware fixture links d3d12_xs_shim (d3d12_xs.dll), which links the translator, so the fixture ran the mutant.

  • native_direct_container failed at line 866, the sampleLevel count assertion. 21 passed, 1 failed, 0 skipped.
  • native_fixed_mesh57_waterfall_fine_hardware passed under the mutant on the RTX 4090: mip 0 on every pixel. On the RTX 4090 through the sandbox vGPU, with driver 32.0.16.1088 and the debug layer on, the driver behaviour from the linked issue did not reproduce. The hardware evidence for the fix is therefore the author's RTX 5080 run alone. I did not reproduce it.
  • After restoring the head file (sha256 matched head) and rebuilding: 22 passed, 0 failed, 0 skipped.

CI

Run 46292578328 on merge 0b083e8 (head 6a9d2a02 into 77d63db8). The Windows pr preset passed 82/82, the Linux Python and gfx1013 fixture jobs passed, and the /O2 job was skipped. The pr preset contains no native_* tests, and no Linux check list includes native_direct_container_check.py. CI ran none of this PR's tests. The results above come from the sandbox run only.

Approving. The SHOULD items are follow-up quality points, not blockers. The bias divergence is documented in the PR body and only applies to positive-bias samplers.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6105454240 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3496#pullrequestreview-6105454240). Originally posted by [Not-Saii](https://github.com/Not-Saii) on 2026-11-29T23:48:27Z. **Review:** APPROVED --- Reviewed at head 6a9d2a0212b67c8faaa8fc91a28d834c20245950 with a static read, then a build and test run in an isolated Windows Sandbox with networking off. I found nothing blocking. There are two non-blocking requests and a few nits. The change is narrow. At base, a nonuniform plain `IMAGE_SAMPLE` already emitted coarse derivatives of its coordinates plus `sampleGrad`. This PR changes only the case where every coordinate is a finite literal: a zero-gradient `sampleGrad` becomes `sampleLevel` at LOD 0. That adds no new reliance on helper lanes. The bias, gradient, compare, LOD and clamp variants stay out, because `quad_gradient` requires op 32. **Why a literal coordinate is the same in every lane.** A literal operand has the same value in every DXIL lane by construction, helper lanes included, and base already differentiated that same literal. On the paths I traced, a register holds a decimal literal only when it was written with no predicate (`vector_write` with an empty `graphics_predicate`), when it came from a uniform full-wave `v_writelane` island, or when it is a scalar. A predicated write becomes a `select` SSA value, fails the `from_chars` check and falls back to `sampleGrad`. The `helper_result_unobserved` shortcut cannot leave covered-only values in a coordinate register here. Image instructions are lane observers, and their address VGPRs count as touched. So the coarse derivatives really are zero. I found no path where a literal coordinate differs across the quad. ### SHOULD 1. `src/gfx1013_direct_compute.inl:1215-1218` and the `0.0` LOD argument: `SampleLevel(0)` matches zero-gradient `SampleGrad` only when the sampler's `MipLODBias` is no greater than its `MinLOD`. Zero gradients give `log2(0) + bias` = -inf, which clamps to `MinLOD`. LOD 0 gives `clamp(0 + bias, MinLOD, MaxLOD)`. The runtime builds D3D samplers with a guest-supplied `MipLODBias` (`src/startup_stubs/d3d12_xs_device.cpp:5512`). So, by the D3D LOD rules, a positive-bias sampler on this path would select a coarser mip than before on every adapter, to work around one driver. This is derived from the LOD rules, not measured. The PR body lists this case as unmeasured. Two requests: - Make the new comment name sampler bias as the exception. As written, it says the sampler LOD limits are unchanged, which suggests the result is identical in all cases. - Question: does `sampleLevel` with a large negative LOD also pass on the affected adapter? For example -32.0, below the [-16, 15.99] bias range. That clamps to `MinLOD` exactly as zero gradients do, whatever the bias, so the divergence goes away. If it doesn't pass there, consider limiting the path to static samplers with zero bias. 2. `tests/native_direct_container_check.py:861-867`: the new cases cover the 32-bit, 2D, all-literal branch and the SSA fallback. Nothing covers the A16 branch, where both halves are checked, or the non-finite fallback (a literal +inf U must keep `sampleGrad`). As far as I can see, a mutant that drops the exponent checks, or checks only the low half for A16, would pass every test here. One A16 constant case and one non-finite case would cover them. ### NIT 3. `src/gfx1013_direct_compute.inl:1220`: array dimensions also require the slice coordinate to be a literal, although slices are not differentiated. That is conservative and fine. A one-line comment would stop someone from "fixing" it later. 4. PR body: the ctest command leaves out `--preset dev`. Add it so the command reproduces as written. 5. The new container cases check IR text only. The hardware behaviour rests on the existing `native_fixed_mesh57_waterfall_fine_hardware`. ### What each test proves - `native_direct_container` (new cases): with literal U/V on a nonuniform waterfall sample, the shader emits exactly one `sampleLevel` and no `sampleGrad`. With position-varying U it emits exactly one `sampleGrad` and no `sampleLevel`. Both keep the nonuniform `createHandleFromHeap`. This is the test that catches a revert, as shown below. - `native_fixed_mesh57_waterfall_fine_{warp,hardware}` (existing): this is the pixel-comparison test for the derivative/helper-lane case. Constant coordinates on a 2-mip 2x2 view with a point sampler must read mip 0 on all 8 pixels. - `native_fixed_mesh57_waterfall_{warp,hardware}` (existing): position-varying coordinates still read mip 1 on all 8 pixels, so the non-constant path still uses real gradients. - The other `native_fixed_mesh57_*` tests and `native_direct_mesh` are regression coverage for the shared lowering. ### Evidence (Windows Sandbox, networking off) - Commit: 6a9d2a02, exported with `git archive`. Merge base 26df1826. - Configure: `cmake --preset dev -DX2W_WITH_OPUS=OFF -DINTAKE_PYTHON=<venv python> -DOFFSCREEN_AGILITY_620_DIR=<Agility 1.620> -DOFFSCREEN_WARP_DIR=<WARP 1.0.22>` (Debug) - Build: `cmake --build --preset dev -j 16 --target compile_dxil startup_indirect_draw_indexed` - Test, with `X2W_D3D12_DEBUG=1`: `ctest --preset dev --no-tests=error --output-on-failure -V -R '^(native_fixed_mesh57_.*|native_direct_container|native_direct_mesh)$'` - Head: **22 passed, 0 failed, 0 skipped (77)**. The 22 are 10 `_warp`, 10 `_hardware`, `native_direct_container` and `native_direct_mesh`. - Adapters as the fixtures print them: NVIDIA GeForce RTX 4090 (sandbox vGPU), driver 32.0.16.1088; Microsoft Basic Render Driver (WARP), 1.0.22.0. - OS: Windows 11 Enterprise 10.0.26100 (sandbox guest). - Debug layer: available (`D3D12GetDebugInterface` hr=0x00000000). All 20 `native_fixed_mesh57_*` tests ran with it enabled (`X2W_D3D12_DEBUG=1`) and printed `debug layer: 0x00000000 enabled`. No D3D12 ERROR, CORRUPTION or WARNING lines appeared in the logs. On this path the fixture does not fail on info-queue messages, so this shows only that none were printed. The two Python container tests create no device. - Base comparison: nothing failed at head, so no base rerun was needed. **Mutation.** I applied the mutation to a scratch copy only, never to the branch. It sets `sample_at_base_level = false && …`, which restores the base emission. The rebuild check confirmed the outputs changed: `x2w_shader_compiler`, `compile_dxil.exe` and `d3d12_xs.dll` relinked. The hardware fixture links `d3d12_xs_shim` (`d3d12_xs.dll`), which links the translator, so the fixture ran the mutant. - `native_direct_container` **failed** at line 866, the `sampleLevel` count assertion. 21 passed, 1 failed, 0 skipped. - `native_fixed_mesh57_waterfall_fine_hardware` **passed** under the mutant on the RTX 4090: mip 0 on every pixel. On the RTX 4090 through the sandbox vGPU, with driver 32.0.16.1088 and the debug layer on, the driver behaviour from the linked issue did not reproduce. The hardware evidence for the fix is therefore the author's RTX 5080 run alone. I did not reproduce it. - After restoring the head file (sha256 matched head) and rebuilding: 22 passed, 0 failed, 0 skipped. ### CI Run 46292578328 on merge 0b083e8 (head 6a9d2a02 into 77d63db8). The Windows `pr` preset passed 82/82, the Linux Python and gfx1013 fixture jobs passed, and the /O2 job was skipped. The `pr` preset contains no `native_*` tests, and no Linux check list includes `native_direct_container_check.py`. **CI ran none of this PR's tests.** The results above come from the sandbox run only. Approving. The SHOULD items are follow-up quality points, not blockers. The bias divergence is documented in the PR body and only applies to positive-bias samplers.
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
R0Y00/Xbox2Windows!3496
No description provided.