Skip to content

mesh: sample through sampler heap entries in the bounded runtime path - #2220

Merged
R0Y00 merged 3 commits into
R0Y00:mainfrom
roansystemlabs:shader/1998-mesh-heap-sampler
Nov 25, 2026
Merged

R0Y00 merged 3 commits into
R0Y00:mainfrom
roansystemlabs:shader/1998-mesh-heap-sampler

Conversation

@roansystemlabs

@roansystemlabs roansystemlabs commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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.py had 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_lz of a 2D texture, any non-zero dmask. The sampler comes from s_load_dwordx4 from the heap base (zero high word) plus one of two offsets:

    • a constant offset;
    • a register offset that each input case makes concrete through the existing geometry-word comparisons. This is the bounded form.
  • The table. The sample reads SamplerState qh[2048] : register(s0, space1). This is the same table and contract as gfx2hlsl.py with X2W_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 equal gfx2hlsl.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 from gfx2hlsl.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 constant qh[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 0x40 of the s17 table become Texture2D<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):

    • without the opt-in, the fixture still stops at Unsupported scalar instruction s_load_dwordx8;
    • an entry SGPR as the heap base: Invalid sampler heap SGPR 17;
    • unbounded index: Unbounded sampler heap offset: runtime geometry word 0 in s5;
    • a constant offset that is not one of the 2047 title entries (0x38, 0x7ff0);
    • a sampler word changed after the load: Sampler heap word in s41 changed after the load by s_and_b32;
    • an overwritten word: Sampler operand s[40:43] no longer holds the four words of one heap load;
    • mixed static and heap use in one join: Sampler heap word in s40 mixed with another value by s_cselect_b32. Scalar branches are concrete per input case here, so s_cselect is the only join form;
    • static sampler words;
    • a heap word moved into a VGPR;
    • arithmetic on the heap base;
    • a non-zero high address word;
    • an overwritten texture descriptor;
    • image_sample_l, a dmask that does not match the destination, and an extra modifier (a16).

    s_mov copies of the loaded words are kept.

  • Shared file. tools/mesh_entry_aot.py gains one dispatch line for image_* and a base image_op that 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's snapshot_samplers / create_sampler_snapshot_heap from translated_sampler_bindings.h. Its resolver starts from dynamic_sampler() and sets MinLOD / MaxLOD from word 1 itself (see Findings).

Overlap: #2142, #2149, #2151 and #2152 touch tools/mesh_entry_aot.py, tools/mesh_dynamic_aot.py or the same test lists, and #2050 touches the same test lists. git merge-tree of this branch with each of their current heads is clean, and so is the merge with current main (b0669e06).

Verification

Windows 11 26200, MSVC 19.44, dev preset, D3D12 debug layer on, DXC from Windows SDK 10.0.26100.0. Tested commit 261ee630 on main 77521c4b.

python -B tests/mesh_heap_sampler_check.py
python -B tests/mesh_heap_sampler_check.py binary llvm-mc-15 llvm-objcopy-15 libLLVM-15.so.1
ctest --preset dev --no-tests=error -R "^mesh_heap_sampler_" -V
  • Readback. Both draw tests passed with no capability skips:

    • mesh_heap_sampler_draw_hardware on NVIDIA GeForce RTX 3070 (10de:2484, driver 32.0.16.2101);
    • mesh_heap_sampler_draw_warp on 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) read ff1ec8c8;
    • 0x20 (register offset, entry 2: wrap, MIN_LOD 1) read ffc8c81e;
    • the three other words take the constant offset 0x30 (entry 3: clamp, MIN_LOD 1) and read ffc81ec8.

    The MIN_LOD difference 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:

    • entries 1 and 2 swapped in the table: 0x10 read ffc8c81e and 0x20 read ff1ec8c8;
    • the default-sampler shortcut, i.e. the translated shader with every qh[n] replaced by qh[0], the all-zero sampler words that gfx2hlsl's shortcut uses: all five inputs read ff1ec81e;
    • the bounded-offset guard removed: the constant path is changed to load through s5, and the translator's guard is patched to return offset 0. Input 0x30 then read ff1ec81e instead of ffc81ec8.
  • 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 as debug_warnings=1024 per draw. decode_sampler() causes it, through dynamic_sampler() and border_sampler(). The existing startup_sampler_heap_warp and startup_sampler_heap_hardware checks 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 entry 0x7fe0 is accepted, and an s_mov copy 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. Against main's translator, the fixture is refused with Unsupported 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, with llvm-objcopy and LLVM-C.dll from 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.

    • The 300-byte program reassembles to the embedded bytes.
    • LLVM's decode equals the Python decoder's line for line.
    • The command line produces the same HLSL as lower. It exits 1 with the refusal without the opt-in, and exits 2 for --sampler-heap-sgpr 17.
    • The existing binary modes of mesh_entry, mesh_dynamic_geometry, mesh_lit, mesh_shadow and mesh_multi pass.
  • 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 two startup_sampler_heap_wrapped_* tests passed after I built the d3d12_xs_shim target, 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:

    • the Ubuntu LLVM 15 job and the Linux Python job (the new test is added to both);
    • the release preset;
    • AMD and Intel GPUs;
    • image_sample_l / _b / _c and other dimensions in mesh programs (refused);
    • performance.

Evidence and follow-up

The gfx1013 program tests/mesh_heap_sampler.s is 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_X wrap or clamp) and LOD clamp (MIN_LOD 0 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:

  • the program's control flow: 0x10 and 0x20 as the register offset, any other word to 0x30;
  • the sampler fields: wrap or clamp, LOD 0 clamped by MIN_LOD / MAX_LOD, point mip.

Field layout follows the RDNA2 ISA image sampler descriptor. image_sample_lz is a sample at LOD 0 (LLVM llvm.amdgcn.image.sample.lz), lowered as SampleLevel(..., 0). D3D clamps that LOD with the sampler's MinLOD and MaxLOD.

Findings, outside this change (tracked in #2221):

  • decode_sampler() in translated_sampler_bindings.h does not read MIN_LOD / MAX_LOD (word 1). The runtime table therefore ignores a title's LOD clamp. The fixture decodes both fields itself.
  • The same helper sets ComparisonFunc = ALWAYS on non-comparison samplers, which is the source of the 1361 warnings above.
  • startup: translate run-time sampler heap indexing into a bounded table #1217 reported that WARP 10.0.19041 read entry 0 for a compile-time-constant qh[1]. On WARP 10.0.26100.9818 the constant indices qh[1], qh[2] and qh[3] read their own entries.

docs/experiments.md documents 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

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

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 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_entry returns offset // 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_entry accepts offsets below 16 * 2047, so entry 2046 is valid and 2047 (the fallback) is refused. This is correct and tested. A one-line comment on why 2046 is the last title entry would help readers.
  • nit: descriptor_operand returns 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 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 other mesh_*_check.py need 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_entry returns offset // 16 + 1. mesh_heap_sampler_check.py fails with an AssertionError. Killed.
  • Mutation B: replace if left is UNDEF or right is UNDEF: with if False: in RuntimeWave.scalar_op. None of the Linux checks that run without arguments fail. Survived on Linux.
  • Mutation C: drop the if self.exec else 0 guard in the runtime v_cndmask_b32. mesh_dynamic_aot_check.py fails. Killed.
  • Worktree restored after each mutation (git checkout -- .).

Findings

  • P1: none.
  • P2: none.
  • nit: the new UNDEF fallback in RuntimeWave.scalar_op (s_cmp_eq_u32 with an undefined operand) has no Linux test that reaches it (mutation B survived). A two-line case in mesh_dynamic_aot_check.py with 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, and descriptor_operand names 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.

@R0Y00
R0Y00 merged commit 1cc858f into R0Y00:main Nov 25, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants