shader: SampleLevel(0) for constant-coordinate nonuniform IMAGE_SAMPLE #3496
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3496"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T17:58:41Z.
Fixes #3489.
Cause
native_fixed_mesh57_waterfall_fine_hardwarefails on one hardware adapter because of how its driver handles the emitted shader, not because of the lowering.The pixel shader emitted at
26df1826is correct per D3D (dxc -dumpbinof the cached container):sampleGradareDerivCoarseX/Yof that constant, so they are zero;createHandleFromHeapwithnonUniformIndex=true;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:
This is deterministic: 10/10 runs, same four pixels.
Experiment: with the fixture's
IMAGE_SAMPLEreplaced byIMAGE_SAMPLE_LZ(lowered tosampleLevelat 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 isSampleGradwith 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 nonuniformIMAGE_SAMPLE(op 32 only) whose every coordinate operand is a fully parsed, finite decimal literal, emitsampleLevelat level 0 instead of the coarse derivatives andsampleGrad.quad_gradientcondition, which is op 32 with a nonuniform selection.undef, or a non-finite float (for A16, a non-finite half in either 16-bit half) keeps the previoussampleGrademission.and/lshrsplit moves after the check, unchanged.src/gfx1013_compute_native.cppgains#include <charconv>forstd::from_chars, because the.inlis included in that file.tests/native_direct_container_check.pyadds two waterfall pixel shaders: constant U must produce exactly onesampleLeveland nosampleGrad; position-varying U must produce exactly onesampleGradand nosampleLevel. Both must keep the nonuniform handle.Evidence
Base
26df1826, head6a9d2a02.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 at26df1826: 0/10.Mutations:
sampleLevelfor every nonuniformIMAGE_SAMPLE, constant or notnative_direct_containerfailswaterfall_fine_hardwarefails with the same 4 pixels as the baseline;native_direct_containerfailsNot covered
IMAGE_SAMPLEwith non-constant coordinates still emitssampleGrad, and no test here measures an intermediate LOD on that path.MipLODBiasexceeds itsMinLODcould select a different mip withSampleLevel(0)than with the zero-gradientSampleGrad. This case is not measured and the path is not restricted to zero-bias samplers.Imported from R0Y00/Xbox2Windows. Originally posted by Not-Saii on 2026-11-29T23:48:27Z.
Review: APPROVED
Reviewed at head
6a9d2a0212with 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_SAMPLEalready emitted coarse derivatives of its coordinates plussampleGrad. This PR changes only the case where every coordinate is a finite literal: a zero-gradientsampleGradbecomessampleLevelat LOD 0. That adds no new reliance on helper lanes. The bias, gradient, compare, LOD and clamp variants stay out, becausequad_gradientrequires 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_writewith an emptygraphics_predicate), when it came from a uniform full-wavev_writelaneisland, or when it is a scalar. A predicated write becomes aselectSSA value, fails thefrom_charscheck and falls back tosampleGrad. Thehelper_result_unobservedshortcut 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
src/gfx1013_direct_compute.inl:1215-1218and the0.0LOD argument:SampleLevel(0)matches zero-gradientSampleGradonly when the sampler'sMipLODBiasis no greater than itsMinLOD. Zero gradients givelog2(0) + bias= -inf, which clamps toMinLOD. LOD 0 givesclamp(0 + bias, MinLOD, MaxLOD). The runtime builds D3D samplers with a guest-suppliedMipLODBias(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:sampleLevelwith a large negative LOD also pass on the affected adapter? For example -32.0, below the [-16, 15.99] bias range. That clamps toMinLODexactly 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.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 keepsampleGrad). 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
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.--preset dev. Add it so the command reproduces as written.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 onesampleLeveland nosampleGrad. With position-varying U it emits exactly onesampleGradand nosampleLevel. Both keep the nonuniformcreateHandleFromHeap. 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.native_fixed_mesh57_*tests andnative_direct_meshare regression coverage for the shared lowering.Evidence (Windows Sandbox, networking off)
6a9d2a02, exported withgit archive. Merge base26df1826.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)cmake --build --preset dev -j 16 --target compile_dxil startup_indirect_draw_indexedX2W_D3D12_DEBUG=1:ctest --preset dev --no-tests=error --output-on-failure -V -R '^(native_fixed_mesh57_.*|native_direct_container|native_direct_mesh)$'_warp, 10_hardware,native_direct_containerandnative_direct_mesh.D3D12GetDebugInterfacehr=0x00000000). All 20native_fixed_mesh57_*tests ran with it enabled (X2W_D3D12_DEBUG=1) and printeddebug 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.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.exeandd3d12_xs.dllrelinked. The hardware fixture linksd3d12_xs_shim(d3d12_xs.dll), which links the translator, so the fixture ran the mutant.native_direct_containerfailed at line 866, thesampleLevelcount assertion. 21 passed, 1 failed, 0 skipped.native_fixed_mesh57_waterfall_fine_hardwarepassed 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.CI
Run 46292578328 on merge 0b083e8 (head
6a9d2a02into77d63db8). The Windowsprpreset passed 82/82, the Linux Python and gfx1013 fixture jobs passed, and the /O2 job was skipped. Theprpreset contains nonative_*tests, and no Linux check list includesnative_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.