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

Merged
lukaslw-lab merged 3 commits from upstream-pr/3619 into main 2026-12-01 00:19:34 +00:00
lukaslw-lab commented 2026-11-30 15:37:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-30T15:37:10Z.


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 #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 #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.

  • Rebased onto #3638. The slot 86 selector path (texture_heap_slot_map) and the root-constant route are unchanged.
  • #3531 reworks the prepare_native_mesh57 block where this PR adds its 3-line refusal; whichever lands second needs a small rebase.
  • Kind bit 16 is new. #3544's dimension table uses 1 + image dimension; 16 stays outside that range so one table can serve both.

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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5568432290 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3619). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-30T15:37:10Z. --- ## 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 #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 #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. - Rebased onto #3638. The slot 86 selector path (`texture_heap_slot_map`) and the root-constant route are unchanged. - #3531 reworks the `prepare_native_mesh57` block where this PR adds its 3-line refusal; whichever lands second needs a small rebase. - Kind bit 16 is new. #3544's dimension table uses 1 + image dimension; 16 stays outside that range so one table can serve both. ## 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 - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] 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](https://claude.com/claude-code)
R0Y00 commented 2026-11-30 19:33:55 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-30T19:33:55Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6119156287 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3619#pullrequestreview-6119156287). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-30T19:33:55Z. **Review:** COMMENTED --- 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 commented 2026-11-30 19:51:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-30T19:51:12Z.


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

  • The conflicts were additive: the layout keyword, the compile_dxil report line and the result field. #3638's heap_entry_offsets checks the same thing as this PR's own set (a value shifted left by 5 or more), so the token route now uses it and the duplicate is gone. The slot 86 selector branch (texture_heap_slot_map) and the root-constant route are unchanged.
  • Also in this push, as promised in the #3631 review: the kind check is now (kind & 16) != 0, so one view id can also carry #3631's raw bit (0x20).

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7290033633 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3619#issuecomment-7290033633). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-30T19:51:12Z. --- Rebased onto main `a0e126c2` (#3638). New head `c076b273`. - The conflicts were additive: the layout keyword, the `compile_dxil` report line and the result field. #3638's `heap_entry_offsets` checks the same thing as this PR's own set (a value shifted left by 5 or more), so the token route now uses it and the duplicate is gone. The slot 86 selector branch (`texture_heap_slot_map`) and the root-constant route are unchanged. - Also in this push, as promised in the #3631 review: the kind check is now `(kind & 16) != 0`, so one view id can also carry #3631's raw bit (0x20). `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 commented 2026-12-01 00:04:11 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-12-01T00:04:11Z.

Review: APPROVED


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.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6122773169 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3619#pullrequestreview-6122773169). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-12-01T00:04:11Z. **Review:** APPROVED --- 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`.
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!3619
No description provided.