Skip to content

shader: read GPU-selected mesh heap buffers through view tokens - #3619

Merged
R0Y00 merged 3 commits into
R0Y00:mainfrom
lukaslw-lab:shader/mesh-heap-token-reads
Dec 1, 2026
Merged

R0Y00 merged 3 commits into
R0Y00:mainfrom
lukaslw-lab:shader/mesh-heap-token-reads

Conversation

@lukaslw-lab

@lukaslw-lab lukaslw-lab commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Change

The slot 57 mesh frontend refuses a heap buffer read whose slot comes from GPU data: mesh heap slot lacks masked root constant provenance. This is the largest C++ refusal by refused DispatchMesh calls in a 360 s native run (#3416). The program loads a dword from a root-table buffer SRV, shifts it left by 5, loads the descriptor at that heap byte offset and reads it with buffer_load_format_x … idxen.

With a new opt-in .mslayout keyword, heap_tokens (next to validate_uint_heap_reads), the translator reads such a descriptor through its view token, as #3544 does for compute textures. It never uses the title's value as a slot. Per read:

  • the heap byte offset must be in heap_entry_offsets (a value shifted left by 5 or more, the 32-byte alignment proof runtime: wire native slot 86 mesh bindings and direct DispatchMesh #3638 added for the slot 86 Texture1D map);
  • the shader loads the token at that address (word 0 magic 0x56573258, word 1 view id) and the id's entry in a per-id kind table, and tests bit 16 (a one-channel 32-bit typed buffer). A bit test, so one view id can also carry other kind bits, such as the raw-buffer bit 0x20 from native: raw buffer loads through heap view tokens (#3023 stage 3, translator) #3631;
  • if the magic matches, id < count and (kind & 16) != 0, it reads ResourceDescriptorHeap[base + id] as a typed U32 buffer. Otherwise it reads the null slot, and the first lane of the wave adds one to a miss counter.

The runtime provides the token memory and kind table (raw SRVs), the miss counter (raw UAV) and base, count, null (cbuffer b1, kept apart from b0 so the root-constant layout is unchanged). compile_dxil prints requires_heap_tokens srv=… kinds_srv=… miss_uav=… words=b1 reads=N.

Translator only: native preparation does not set the option and refuses such a program by name (MS heap token reads are not bound yet). Binding is a follow-up. Refs #2413, #3416, #3023.

Verification

Commit c076b273 (on main a0e126c2; native_direct_mesh re-run there; the other runs below were at 5246f670, which differs only in the doc paragraph) on Windows 10 22H2 (build 19045), Visual Studio 2026 (MSVC 19.52.37029), Windows SDK 10.0.26100, CMake 4.3.1, Python 3.13.7, DXC 1.9.2609.5. GPU: NVIDIA GeForce RTX 3080 Ti, driver 32.0.16.1047 (610.47), debug layer on.

ctest --test-dir out\build\release --no-tests=error -R "^native_direct_mesh$" --output-on-failure
100% tests passed, 0 tests failed out of 1

ctest --test-dir out\build\release -j1 -R "native_direct|compile_dxil_refusals|mesh86_cache_cli|mesh86_srv_proof|indirect_mesh_cache"
native_direct_mesh, native_direct_container, native_compile_dxil_refusals: Passed
startup_indirect_mesh_cache_{heap,texture_map,heap_wide}_hardware: Failed, serialize root failed (HRESULT 0x80070057)
startup_indirect_mesh_cache_*_warp: Skipped

The three hardware failures are the same on a0e126c2 in the same tree (0% tests passed, 3 tests failed out of 3): the Windows 10 in-box runtime on this host. They pass on the Agility SDK runtime.

Per-case compile_dxil results inside native_direct_mesh at 5246f670 (same code):

Path Accepted Refused with the expected diagnostic
Root-constant selector typed-heap; token-root-route (heap_tokens on, no token views) overwritten-heap-word
Slot 86 Texture1D slot map (#3638) mesh-texture-heap-slot-map -without-map, -unaligned, -wrong-dimension, -overwritten
Heap tokens token, token-twice (reads=2), token-wave32 token-no-contract, token-unvalidated, token-shift4, token-unshifted, token-raw-read, token-overwritten

Mesh hardware tests on the Agility SDK runtime (a second tree configured with -DOFFSCREEN_AGILITY_620_DIR=<Agility 1.620.2>; the Windows 10 in-box runtime rejects ms_6_6 mesh shaders on this host, with or without this change):

ctest --test-dir out\build\release-agility --no-tests=error -j2 -R "^(native_fixed_mesh57_.*_hardware|native_fixed_mesh57_hardware|native_mesh57_outcomes_hardware|startup_indirect_mesh_cache_(heap|texture_map|heap_wide|constants_wide)?_?hardware)$"
100% tests passed, 0 tests failed out of 19

Evidence and follow-up

Fixtures are source-built gfx1013 words in tests/native_direct_mesh_check.py (MIT). They check:

  • the module: the magic compare, the kind bit test (and i32 kind, 16 + icmp ne … 0, no icmp eq … 16), id < count, waveIsFirstLane + atomic add, the b1 load, a heap index that is a select of base + id or null, and the token read at selector << 5;
  • two reads sharing one set of views (reads=2), a wave32 entry, and the unchanged root-constant route with heap_tokens on;
  • six refusals (negative controls): no keyword, the keyword without validated heap reads, a shift by 4, an offset without a shift, a raw read and an overwritten descriptor word.

On the captured programs behind the #3416 table (raw .m57 + .mslayout + .root from a 360 s native run, compile_dxil <dxc dir> <cache> gfx1013-raw-ms <key>.m57 <out>), heap_tokens moves the top two from mesh heap slot lacks masked root constant provenance (+0x2a0 / +0x2a8) to v_readfirstlane_b32 (+0x470 / +0x474). The next three keep their refusals (+0x370, +0x46c, +0x70c) with and without the keyword. (Measured at 500b0cda, before the rebase.)

Follow-up (runtime, separate PR): bind the token memory, kind table, miss UAV and b1 words in prepare_native_mesh57, set heap_tokens there, and add a hardware fixture with valid, wrong-magic, out-of-range-id and wrong-kind tokens (including an id with only the raw bit 0x20). On the title programs the next refusal after this one is v_readfirstlane_b32 (#3416).

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: prepared with Claude Code; the commands and results above were run on the author's PC.

🤖 Generated with Claude Code

A slot 57 mesh program may load a buffer descriptor from the resource heap
at a byte offset chosen by GPU data (a dword read from a root-table buffer
SRV, shifted left by 5). With the opt-in `.mslayout` keyword `heap_tokens`
(next to `validate_uint_heap_reads`) the translator reads the view token at
that address instead of trusting the title's value: magic, view id and a
per-id kind (16: one-channel 32-bit typed buffer). A valid token reads
ResourceDescriptorHeap[base + id]; anything else reads the null slot and
the first lane counts a miss. base, count and null come from cbuffer b1.

Translator only: native preparation does not set the option and refuses a
program with token reads by name. Refs R0Y00#2413, R0Y00#3023.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The current head conflicts with the mesh frontend and binding metadata that landed in #3638. Please rebase the opt-in slot 57 heap_tokens work while preserving the existing slot 86 selector path and its controls. Please show the exact-head native_direct_mesh result for both the existing selector path and the new token accept/refuse cases. #3416 tracks this first-refusal family.

@lukaslw-lab
lukaslw-lab force-pushed the shader/mesh-heap-token-reads branch from 500b0cd to 5246f67 Compare November 30, 2026 19:49
The token guard compared the kind word with heap_token_r32_uint_buffer (16).
A view id can carry other kind bits as well, such as the raw-buffer bit from
R0Y00#3631, so test (kind & 16) != 0 instead. The fixture checks the and/icmp ne
pair and that no equality test against 16 remains.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukaslw-lab
lukaslw-lab force-pushed the shader/mesh-heap-token-reads branch from 5246f67 to c076b27 Compare November 30, 2026 19:50
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

Rebased onto main a0e126c2 (#3638). New head c076b273.

native_direct_mesh at c076b273: Passed (1/1). Per-case compile_dxil results from the same test at 5246f670 (same code; c076b273 only changes the doc paragraph):

Path Accepted Refused with the expected diagnostic
Root-constant selector typed-heap; token-root-route (heap_tokens on, no token views) overwritten-heap-word
Slot 86 Texture1D slot map mesh-texture-heap-slot-map -without-map, -unaligned, -wrong-dimension, -overwritten
Heap tokens token, token-twice (reads=2), token-wave32 token-no-contract, token-unvalidated, token-shift4, token-unshifted, token-raw-read, token-overwritten

Mesh hardware tests on the Agility 1.620.2 runtime (RTX 3080 Ti, driver 610.47, debug layer on): 19/19 passed, including startup_indirect_mesh_cache_{heap,texture_map,heap_wide,constants_wide}_hardware. On the in-box Windows 10 runtime, startup_indirect_mesh_cache_{heap,texture_map,heap_wide}_hardware fail with serialize root failed (0x80070057), the same as on plain a0e126c2.

@Leyukaka Leyukaka 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.

Read at e4b126cc, which is c076b273 plus a merge of main; diff only. The token route is taken only with layout.heap_tokens (src/gfx1013_direct_mesh.inl:1101-1108), the root-constant route is unchanged, and the runtime refuses token reads until their binding exists (src/startup_stubs/native_mesh57.inl:405-407).

  • NIT src/gfx1013_direct_mesh.inl:122-124: with count == 0, in_range is false, so the null slot is chosen correctly. However, clamped becomes count - 1 = 0xffffffff, and the kind load reads byte 0xfffffffc of kinds. A descriptor-bound view returns 0 for that read, but a root SRV has no bounds check. Selecting 0 instead of count - 1 for an out-of-range id avoids depending on how the runtime follow-up binds kinds_srv.

@R0Y00
R0Y00 merged commit 2d5ab30 into R0Y00:main Dec 1, 2026
5 checks passed
@lukaslw-lab
lukaslw-lab deleted the shader/mesh-heap-token-reads branch December 1, 2026 01:11
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