Repository navigation
mesh: sample through sampler heap entries in the bounded runtime path - #2220
Conversation
Part of R0Y00#1998 (item E). Opt-in --sampler-heap-sgpr lowers image_sample_lz with a sampler loaded by s_load_dwordx4 from the sampler heap base plus a constant offset, or a register offset that every input case makes concrete. It reads the gfx2hlsl sampler table (qh[2048], s0 space1, 16-byte entries, entry 2047 reserved). Unbounded offsets, changed or mixed sampler words, static sampler words and heap base arithmetic are refused by name. Adds a source-built gfx1013 fixture with CPU, LLVM 15 binary and fenced readback checks (offscreen_check --mesh-heap-sampler).
tedin7
left a comment
There was a problem hiding this comment.
Review of head 261ee6305ffec7123a69a541a449c90aaffb8005 (comment only, not an approval).
Scope: 11 files, +957/-9. Opt-in sampler-heap path in tools/mesh_dynamic_aot.py (image_sample_lz through an s_load_dwordx4 sampler from the heap base, offset made concrete per input case), new fixture tests/mesh_heap_sampler.s, checker tests/mesh_heap_sampler_check.py, a D3D12 draw in tests/mesh_heap_sampler_draw.cpp, CTest and workflow wiring. Title is neutral and the change reads clean-room.
Commands executed locally (Linux):
python3 tests/mesh_heap_sampler_check.py: pass. Three input cases read heap entries 1, 2, 3; entry 2046 accepted; 16 unproven forms refused by name; 3 guard-removal controls fail as required; self-test passed.python3 tests/docs_links_check.py: pass (18 files, 408 links).python3 tests/bidi_controls_check.py: pass (1496 files).git diff --check origin/main...HEAD: clean.- Mutation 1:
heap_entryreturnsoffset // 16 + 1. The checker fails (assert entry_of(...) == program_offset(word) // 16). Killed. - Mutation 2: disabled the "static sampler words are not translated" refusal. The checker fails on the expected refusal message. Killed.
- Worktree restored after each mutation (
git checkout -- .).
Findings
- P1: none.
- P2: none.
- nit:
heap_entryaccepts offsets below16 * 2047, so entry 2046 is valid and 2047 (the fallback) is refused. This is correct and tested. A one-line comment on why2046is the last title entry would help readers. - nit:
descriptor_operandreturns the first descriptor-typed register only. That is fine for refusal, but the error then names one token even when several operands are descriptors.
Not verified by me: mesh_heap_sampler_draw_* (D3D12 WARP readback) needs Windows and was not run here. At review time the Windows CI jobs were still pending and the Linux Python job was green. Please require the Windows draw test to pass before merge.
Semantics: the sampler stays a table index, not a raw descriptor. The path refuses a runtime geometry word as offset, mixing via s_cselect, partial or reordered words, and the static-words form. That matches the stated heap model.
AI-assisted; commands listed were executed locally.
tedin7
left a comment
There was a problem hiding this comment.
Re-review of head 8fd3b3a77040a7450658e9bfa64782baef0c79c5 (comment only, not an approval). The previous review was on 261ee63.
What changed since 261ee63: two merges of main. The only PR-owned edits are in tools/mesh_dynamic_aot.py (+9/-4), made to follow main's new Wave.scalar(token, allow_undef) / UNDEF: RuntimeWave.scalar forwards allow_undef, s_cmp_eq_u32 falls back to the base class when an operand is UNDEF, and v_cndmask_b32 reads its mask only when exec is non-zero. The last merge only moves the mesh sampler checks next to the mesh fixtures in CMake/AGENTS/docs. The sampler-heap lowering, fixture and draw test are unchanged. Title is neutral, clean-room, mergeable.
Commands executed locally (Linux):
python3 tests/mesh_heap_sampler_check.py: pass (3 input cases read entries 1, 2, 3; entry 2046 accepted; 16 unproven forms refused; 3 guard-removal controls fail as required).python3 tests/mesh_dynamic_aot_check.py,mesh_dynamic_geometry_check.py,mesh_lane_spill_check.py,mesh_multi_check.py,mesh_shadow_check.py,mesh_amplification_refusal_check.py: all pass. The othermesh_*_check.pyneed a dxc/renderer/backend argument (Windows) and were not run.python3 tests/docs_links_check.py: pass (18 files, 419 links).python3 tests/bidi_controls_check.py: pass (1525 files).git diff --check origin/main...HEAD: clean.- Mutation A:
heap_entryreturnsoffset // 16 + 1.mesh_heap_sampler_check.pyfails with an AssertionError. Killed. - Mutation B: replace
if left is UNDEF or right is UNDEF:withif False:inRuntimeWave.scalar_op. None of the Linux checks that run without arguments fail. Survived on Linux. - Mutation C: drop the
if self.exec else 0guard in the runtimev_cndmask_b32.mesh_dynamic_aot_check.pyfails. Killed. - Worktree restored after each mutation (
git checkout -- .).
Findings
- P1: none.
- P2: none.
- nit: the new
UNDEFfallback inRuntimeWave.scalar_op(s_cmp_eq_u32with an undefined operand) has no Linux test that reaches it (mutation B survived). A two-line case inmesh_dynamic_aot_check.pywith an undefined SGPR compare would pin it. - nit (carried over): a one-line comment on why 2046 is the last title entry in
heap_entry, anddescriptor_operandnames only the first descriptor register in its refusal.
Not verified by me: the D3D12 WARP readback mesh_heap_sampler_draw_* and the Windows jobs. At review time CI showed the Linux Python job green and the Windows jobs pending. Please require the Windows draw tests (WARP and hardware-skip semantics) to pass on this head before merge.
AI-assisted; commands listed were executed locally.
Part of #1998.
Change
Item E: a mesh program that samples a texture with a sampler loaded from the sampler heap.
tools/mesh_dynamic_aot.pyhad no image path, so these programs stopped at the descriptor load. This change reuses the opt-in sampler table from #1217 and #1237 for this case, as described in the claim comment.Opt-in. The path is off by default. To turn it on, pass
--sampler-heap-sgpr <n>(Python:sampler_heap_sgpr=n). It names the entry SGPR that holds the 32-bit sampler heap base. Without it, the output and the refusals are unchanged.What is lowered.
image_sample_lzof a 2D texture, any non-zero dmask. The sampler comes froms_load_dwordx4from the heap base (zero high word) plus one of two offsets:The table. The sample reads
SamplerState qh[2048] : register(s0, space1). This is the same table and contract asgfx2hlsl.pywithX2W_SAMPLER_HEAP=1: entry n is the heap entry at byte 16 n, and entry 2047 is reserved for the host fallback. The CPU check asserts that the size, space and fallback constants equalgfx2hlsl.py's.Constant indices. Every offset is proven at translation time, so each variant indexes
qh[n]with a compile-time constant and never reaches 2047. This differs fromgfx2hlsl.py, which still refuses constant offsets and indexes the table at run time. startup: translate run-time sampler heap indexing into a bounded table #1217 saw WARP 10.0.19041 read entry 0 for a constantqh[1]. On the adapters below, constant entries 1, 2 and 3 read their own samplers.The texture. A texture descriptor is needed to sample at all. The eight words at byte
0x40of thes17table becomeTexture2D<float4> meshTexture : register(t1). This is the minimum binding for the fixture. The heap SGPR option and this texture slot are choices of this PR; they can follow a different layout if preferred.Refused by name, each with a test (16 checks):
Unsupported scalar instruction s_load_dwordx8;Invalid sampler heap SGPR 17;Unbounded sampler heap offset: runtime geometry word 0 in s5;0x38,0x7ff0);Sampler heap word in s41 changed after the load by s_and_b32;Sampler operand s[40:43] no longer holds the four words of one heap load;Sampler heap word in s40 mixed with another value by s_cselect_b32. Scalar branches are concrete per input case here, sos_cselectis the only join form;image_sample_l, a dmask that does not match the destination, and an extra modifier (a16).s_movcopies of the loaded words are kept.Shared file.
tools/mesh_entry_aot.pygains one dispatch line forimage_*and a baseimage_opthat keeps the existing message (Unsupported mesh instruction ...). The static lowerer is unchanged.Runner.
offscreen_check --mesh-heap-sampler <ms> <ps> <fixture>(tests/mesh_heap_sampler_draw.cpp) builds the 2,048-entry table with the stand-in'ssnapshot_samplers/create_sampler_snapshot_heapfromtranslated_sampler_bindings.h. Its resolver starts fromdynamic_sampler()and setsMinLOD/MaxLODfrom word 1 itself (see Findings).Overlap: #2142, #2149, #2151 and #2152 touch
tools/mesh_entry_aot.py,tools/mesh_dynamic_aot.pyor the same test lists, and #2050 touches the same test lists.git merge-treeof this branch with each of their current heads is clean, and so is the merge with currentmain(b0669e06).Verification
Windows 11 26200, MSVC 19.44,
devpreset, D3D12 debug layer on, DXC from Windows SDK 10.0.26100.0. Tested commit261ee630onmain77521c4b.Readback. Both draw tests passed with no capability skips:
mesh_heap_sampler_draw_hardwareon NVIDIA GeForce RTX 3070 (10de:2484, driver 32.0.16.2101);mesh_heap_sampler_draw_warpon WARP 10.0.26100.9818.The draw mode also ran three times per adapter, and all six runs printed the same lines. All five inputs (
0x10,0x20,0x30,0x0,0x18) matched the independent model on 1,024 of 1,024 pixels:0x10(register offset, entry 1: clamp, LOD 0) readff1ec8c8;0x20(register offset, entry 2: wrap,MIN_LOD1) readffc8c81e;0x30(entry 3: clamp,MIN_LOD1) and readffc81ec8.The
MIN_LODdifference between entries comes from the fixture's resolver. It shows that each path reaches its own entry; it does not show LOD clamping in the runtime table.Negative controls. Each control must exit 1 with 1,024 mismatches and the first pixel the model predicts. All did on both adapters:
0x10readffc8c81eand0x20readff1ec8c8;qh[n]replaced byqh[0], the all-zero sampler words that gfx2hlsl's shortcut uses: all five inputs readff1ec81e;s5, and the translator's guard is patched to return offset 0. Input0x30then readff1ec81einstead offfc81ec8.Debug layer. Every draw reported
debug_errors=0. The only message is D3D12 warning 1361 ("ComparisonFunc is D3D12_COMPARISON_FUNC_ALWAYS, but D3D12_FILTER_MIN_MAG_MIP_POINT is not a comparison filter"), reported asdebug_warnings=1024per draw.decode_sampler()causes it, throughdynamic_sampler()andborder_sampler(). The existingstartup_sampler_heap_warpandstartup_sampler_heap_hardwarechecks show the same: on both adapters every debug-layer message they produce is 1361. The new check accepts only 1361 lines: a wrapper that injects one more warning turned it red. A run with the debug layer reported unavailable passes, because it then reads the pixels only.CPU mode. One translation per input case (
qh[1],qh[2],qh[3]), with the counts, indices and per-vertex attributes checked. The last title entry0x7fe0is accepted, and ans_movcopy of the four words gives the same HLSL. The 16 refusals above are checked by message. Each guard was then removed in turn (offset alignment and range, bounded offset, four words of one load), and the check failed at the matching refusal. Againstmain's translator, the fixture is refused withUnsupported scalar instruction s_load_dwordx8.Binary mode. I ran this locally with LLVM 15.0.7. The clang 15 integrated assembler stood in for
llvm-mc, withllvm-objcopyandLLVM-C.dllfrom the same release, as in gpu: light a translated mesh draw with an interpolated normal and N·L #749 and gpu: cast a shadow with a translated depth pass and SampleCmpLevelZero #773.lower. It exits 1 with the refusal without the opt-in, and exits 2 for--sampler-heap-sgpr 17.mesh_entry,mesh_dynamic_geometry,mesh_lit,mesh_shadowandmesh_multipass.Wider runs.
ctest --preset dev --no-tests=error -R "^(mesh_|compute_mesh_texture_|offscreen_|startup_sampler_heap)" -V: 62/62 passed with no skips. The twostartup_sampler_heap_wrapped_*tests passed after I built thed3d12_xs_shimtarget, which my first run had not built. No run reported a debug-layer error. These checks also pass:mesh_dynamic_aot_check,mesh_dynamic_geometry_check,mesh_lane_spill_check,mesh_multi_check;pso_readiness_check,startup_sampler_heap_check,docs_links_check;git diff --check.Not run:
releasepreset;image_sample_l/_b/_cand other dimensions in mesh programs (refused);Evidence and follow-up
The gfx1013 program
tests/mesh_heap_sampler.sis original, written for this test, and MIT. No game files. It draws a quad whose PARAM0 is one sample at (1.25, 0.5) of a 4x1 RGBA8 texture with a 2x1 second level. The four heap entries differ in address mode (CLAMP_Xwrap or clamp) and LOD clamp (MIN_LOD0 or 1), so each path reads a different texel. The expected colors come from a model in the test, independent of the translator, that covers:0x10and0x20as the register offset, any other word to0x30;MIN_LOD/MAX_LOD, point mip.Field layout follows the RDNA2 ISA image sampler descriptor.
image_sample_lzis a sample at LOD 0 (LLVMllvm.amdgcn.image.sample.lz), lowered asSampleLevel(..., 0). D3D clamps that LOD with the sampler'sMinLODandMaxLOD.Findings, outside this change (tracked in #2221):
decode_sampler()intranslated_sampler_bindings.hdoes not readMIN_LOD/MAX_LOD(word 1). The runtime table therefore ignores a title's LOD clamp. The fixture decodes both fields itself.ComparisonFunc = ALWAYSon non-comparison samplers, which is the source of the 1361 warnings above.qh[1]. On WARP 10.0.26100.9818 the constant indicesqh[1],qh[2]andqh[3]read their own entries.docs/experiments.mddocuments the opt-in, the refusals and the commands. The new test is registered in CMake (mesh_heap_sampler_cpu,mesh_heap_sampler_draw_{hardware,warp}) and in the Ubuntu LLVM 15 and Linux Python jobs.Checklist
AI-assisted.