d3d12_xs, tombox-xshader: bind 64-bit GPU-VA global memory through a 64-window TbGlobal table #336

Open
GDAgent wants to merge 2 commits from upstream-pr/336 into main
GDAgent commented 2026-11-23 20:25:10 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by GDAgent on 2026-11-23T20:25:10Z.


Library / area: d3d12_xs (global_bind.rs, device_wrap.rs, gpu_va.rs, cmdlist_x.rs), tombox-xshader (global_abi.rs, hlsl/global.rs, hlsl.rs)

Exports changed (exports.txt rows): none

Builds on #283 (gpu_va forgets destroyed resources). The window table is filled from the gpu_va buffer map, so without #283 a destroyed buffer can end up in a descriptor (that is the crash #283 fixes). This branch is stacked on #283's commit; review only the second commit (git diff 71cec92..); I will rebase onto main once #283 lands.

Source of the behaviour: RDNA2 flat/global memory instructions and the BVH intersect instructions address memory by 64-bit GPU VA. The console runtime relies on that; the PC runtime has no such addressing. Seen with the console xdxr runtime (compute shader 07edd3df1713c3c0, see docs/BLOCKERS.md) and with the ray-tracing state objects of The Matrix Awakens (Xbox Series X build), native Windows 11 on an RDNA2 GPU (gfx1032); not run under Proton.

What

Until now such shaders were refused (global_bind::refusal: "d3d12_xs does not bind yet"). With this change they can run when TOMBOX_GLOBAL_BIND=1 is set; without it nothing changes (same refusal, no heap growth, no root-signature change). The translator already emitted a TbGlobal window table (space4000003, 2 windows), but nothing created the table or filled it.

tombox-xshader:

  • TbGlobal has 64 windows. One descriptor table (1 root DWORD): CBV(b0) with the window base/size/writable flag, 64 raw SRVs (t0..t63, read-only buffers), 64 raw UAVs (u0..u63, buffers created with ALLOW_UNORDERED_ACCESS) and the fault record (u64). A read uses the SRV when the window is not writable, so upload and read-only buffers work without a UAV; stores to a read-only window are counted as faults.
  • Window lookup loops over the windows in use (tb_msin.z) instead of a fixed unrolled pair, and indexes the arrays with NonUniformResourceIndex on SM 6+.
  • A shader that uses global memory and image_bvh*_intersect_ray reads the BVH through TbGlobal and no longer also needs the separate TbGmem window (needs_gmem is false then). The traversal code is unchanged; only its loads go through tb_bvh_load1/3/4. A BVH-only shader keeps the TbGmem path byte for byte.

d3d12_xs:

  • global_bind appends the table to every non-local root signature that has a spare root DWORD (re-serialized through the host serializer), grows every shader-visible CBV_SRV_UAV heap by a ring of 8 tables (1040 descriptors) so the table lives in the heap the title already set, and on SetComputeRootSignature / SetGraphicsRootSignature writes the window CBV and descriptors for up to 64 buffers from the gpu_va map (UAV-capable buffers first, then read-only ones, address order within each group) and binds the table. All of this is opt-in: it runs only when TOMBOX_GLOBAL_BIND is set to a value other than 0.
  • gpu_va remembers which buffers allow UAV access and exposes a snapshot of the tracked buffers.
  • The real-device CreateRootSignature hook (GDS parameter, TbGlobal table, centroid binding, console part stripping) was only installed together with the descriptor-trace hooks (TOMBOX_RTCAP), so in a normal run no root signature got the table and compute PSOs that read global memory failed with E_INVALIDARG ("CBV b0 in space 4000003 not bound by the root signature"). The hook is now installed on its own when TbGlobal binding is on (and, as before, with TOMBOX_RTCAP or the pass-capture tables); the descriptor-trace hooks stay behind TOMBOX_RTCAP. With the switch off, the hook is installed exactly when it was before.
  • The first version of the table used range flags the serializer rejects (DATA_VOLATILE | DATA_STATIC_WHILE_SET_AT_EXECUTE), so the patch silently fell back to the unpatched blob. The ranges are now DESCRIPTORS_VOLATILE | DATA_VOLATILE and the serializer's message is logged on failure.

Limits (by design in this change)

  • Only 64 buffers are visible to a shader at a time. A VA outside the 64 chosen windows reads as 0 and is counted in the fault record; nothing reads that record back yet, so such a miss is silent. Titles that keep far more than 64 buffers alive need a smarter choice of windows than "UAV-capable first, then address order" (the selection function takes explicit priority windows for a caller that knows which buffers matter; nothing uses that yet).
  • The windows are chosen when the root signature is set. A buffer created after that point in the same command list is not visible, and descriptors are written on the CPU timeline into a ring of 8 tables, so a list that sets more than 8 root signatures before it executes reuses the oldest table.
  • A window covers at most u32::MAX bytes of a buffer.

Tests

  • cargo test -p tombox-xshader --test opcodes bvh: 1 passed
  • cargo test -p tombox-xshader --lib global_abi: 1 passed
  • cargo test -p tombox-xshader --test exec exec_mem::global (WARP and TOMBOX_EXEC_HW=1): 4 passed on WARP and 4 passed on the RDNA2 GPU. They run a shader against a window table with one read-only SRV window, one writable UAV window and the fault record (63 further windows are null descriptors), including a store into the read-only window that must fault and leave its data unchanged, route flat/global/scratch instructions and check narrow loads at the end of a window. The tests were updated for the 64-window table and the SRV/UAV split.
  • cargo test -p tombox-xshader --test exec exec_mem::bvh (WARP and TOMBOX_EXEC_HW=1): 1 passed on WARP and 1 passed on the RDNA2 GPU. Unchanged tests; they check that the BVH-only path still gives the same results after the helper was split.
  • New opcodes test: a shader with a flat load and image_bvh64_intersect_ray translates with needs_global and without needs_gmem, and reads the BVH through tb_gmem_load.
  • d3d12_xs unit tests (global_bind): the append plan uses 1 root DWORD and respects the DWORD limit and local root signatures, the ranges use valid volatile flags, patching goes through the host serializer (round trip), heap growth only touches shader-visible CBV_SRV_UAV heaps, window selection puts explicit windows first, then UAV-capable, then read-only buffers, without duplicates, and the switch is opt-in (unset, empty and 0 are off). 7 passed (and the 10 gpu_va tests from #283 pass with the extra field).
  • TOMBOX_REMOVED_EXIT_MS=off cargo test -p d3d12_xs --lib -- --test-threads=1: 672 passed, 4 failed, 71 ignored. The 4 failures are the ones that fail on main here too: alias::tests::msaa_depth_with_a_larger_pc_layout_never_overlaps_the_next_title_placement, placed_offset::tests::uneven_views_get_exact_helpers_and_msaa_shares_the_alias_heap, title_shadow::tests::buffer_then_texture_migrates_to_one_local_heap_and_keeps_bytes, xs_exports::console_fixed_tests::fixed_result_dxgix_exports_match_every_console_d3d12_xs (needs the console DLL). dred::tests::report_is_one_shot_and_needs_an_armed_device passes when run without TOMBOX_REMOVED_EXIT_MS.
  • Native Windows 11; not run under Proton.

Not verified

  • Not verified in a title. The Matrix Awakens was run with this table on (with a local test branch that contained this change plus an unsubmitted ray-tracing runtime: root signatures were patched and the backing buffers and heap ring were created without a device error), but those runs never got as far as a dispatch that reads global memory, so no title shader has read data through a window yet.

  • The xdxr device tests need console DLLs and were not run, so it is not known whether 07edd3df1713c3c0 and its siblings now run.

  • Opt-in on purpose: with TOMBOX_GLOBAL_BIND=1 every shader-visible heap grows by 1040 descriptors and every non-local root signature with a spare DWORD gets one more root parameter, in every title, and the real-device root-signature hook (which also applies the GDS and centroid patches) is installed without TOMBOX_RTCAP. No title run compared that before and after, so it should become the default only once a title confirms it.

  • The unit tests run with the switch forced on (enabled() is true under cfg(test)); the off path is covered by the parse test and by the unchanged refusal.

  • The fault record is written but not read.

  • Tests pass

  • No game or console files and no binaries included

  • No fake success; anything not possible is left missing with a note

<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:6568120634 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/336). Originally posted by [GDAgent](https://github.com/GDAgent) on 2026-11-23T20:25:10Z. --- **Library / area:** `d3d12_xs` (`global_bind.rs`, `device_wrap.rs`, `gpu_va.rs`, `cmdlist_x.rs`), `tombox-xshader` (`global_abi.rs`, `hlsl/global.rs`, `hlsl.rs`) **Exports changed (exports.txt rows):** none **Builds on #283** (`gpu_va` forgets destroyed resources). The window table is filled from the `gpu_va` buffer map, so without #283 a destroyed buffer can end up in a descriptor (that is the crash #283 fixes). This branch is stacked on #283's commit; review only the second commit (`git diff 71cec92..`); I will rebase onto `main` once #283 lands. **Source of the behaviour:** RDNA2 flat/global memory instructions and the BVH intersect instructions address memory by 64-bit GPU VA. The console runtime relies on that; the PC runtime has no such addressing. Seen with the console xdxr runtime (compute shader `07edd3df1713c3c0`, see `docs/BLOCKERS.md`) and with the ray-tracing state objects of The Matrix Awakens (Xbox Series X build), native Windows 11 on an RDNA2 GPU (gfx1032); not run under Proton. ## What Until now such shaders were refused (`global_bind::refusal`: "d3d12_xs does not bind yet"). With this change they can run when `TOMBOX_GLOBAL_BIND=1` is set; **without it nothing changes** (same refusal, no heap growth, no root-signature change). The translator already emitted a `TbGlobal` window table (`space4000003`, 2 windows), but nothing created the table or filled it. `tombox-xshader`: - `TbGlobal` has 64 windows. One descriptor table (1 root DWORD): `CBV(b0)` with the window base/size/writable flag, 64 raw SRVs (`t0..t63`, read-only buffers), 64 raw UAVs (`u0..u63`, buffers created with `ALLOW_UNORDERED_ACCESS`) and the fault record (`u64`). A read uses the SRV when the window is not writable, so upload and read-only buffers work without a UAV; stores to a read-only window are counted as faults. - Window lookup loops over the windows in use (`tb_msin.z`) instead of a fixed unrolled pair, and indexes the arrays with `NonUniformResourceIndex` on SM 6+. - A shader that uses global memory and `image_bvh*_intersect_ray` reads the BVH through `TbGlobal` and no longer also needs the separate `TbGmem` window (`needs_gmem` is false then). The traversal code is unchanged; only its loads go through `tb_bvh_load1/3/4`. A BVH-only shader keeps the `TbGmem` path byte for byte. `d3d12_xs`: - `global_bind` appends the table to every non-local root signature that has a spare root DWORD (re-serialized through the host serializer), grows every shader-visible `CBV_SRV_UAV` heap by a ring of 8 tables (1040 descriptors) so the table lives in the heap the title already set, and on `SetComputeRootSignature` / `SetGraphicsRootSignature` writes the window CBV and descriptors for up to 64 buffers from the `gpu_va` map (UAV-capable buffers first, then read-only ones, address order within each group) and binds the table. All of this is **opt-in**: it runs only when `TOMBOX_GLOBAL_BIND` is set to a value other than `0`. - `gpu_va` remembers which buffers allow UAV access and exposes a snapshot of the tracked buffers. - The real-device `CreateRootSignature` hook (GDS parameter, TbGlobal table, centroid binding, console part stripping) was only installed together with the descriptor-trace hooks (`TOMBOX_RTCAP`), so in a normal run no root signature got the table and compute PSOs that read global memory failed with `E_INVALIDARG` ("CBV b0 in space 4000003 not bound by the root signature"). The hook is now installed on its own when TbGlobal binding is on (and, as before, with `TOMBOX_RTCAP` or the pass-capture tables); the descriptor-trace hooks stay behind `TOMBOX_RTCAP`. With the switch off, the hook is installed exactly when it was before. - The first version of the table used range flags the serializer rejects (`DATA_VOLATILE | DATA_STATIC_WHILE_SET_AT_EXECUTE`), so the patch silently fell back to the unpatched blob. The ranges are now `DESCRIPTORS_VOLATILE | DATA_VOLATILE` and the serializer's message is logged on failure. ## Limits (by design in this change) - Only 64 buffers are visible to a shader at a time. A VA outside the 64 chosen windows reads as 0 and is counted in the fault record; nothing reads that record back yet, so such a miss is silent. Titles that keep far more than 64 buffers alive need a smarter choice of windows than "UAV-capable first, then address order" (the selection function takes explicit priority windows for a caller that knows which buffers matter; nothing uses that yet). - The windows are chosen when the root signature is set. A buffer created after that point in the same command list is not visible, and descriptors are written on the CPU timeline into a ring of 8 tables, so a list that sets more than 8 root signatures before it executes reuses the oldest table. - A window covers at most `u32::MAX` bytes of a buffer. ## Tests - `cargo test -p tombox-xshader --test opcodes bvh`: 1 passed - `cargo test -p tombox-xshader --lib global_abi`: 1 passed - `cargo test -p tombox-xshader --test exec exec_mem::global` (WARP and `TOMBOX_EXEC_HW=1`): 4 passed on WARP and 4 passed on the RDNA2 GPU. They run a shader against a window table with one read-only SRV window, one writable UAV window and the fault record (63 further windows are null descriptors), including a store into the read-only window that must fault and leave its data unchanged, route flat/global/scratch instructions and check narrow loads at the end of a window. The tests were updated for the 64-window table and the SRV/UAV split. - `cargo test -p tombox-xshader --test exec exec_mem::bvh` (WARP and `TOMBOX_EXEC_HW=1`): 1 passed on WARP and 1 passed on the RDNA2 GPU. Unchanged tests; they check that the BVH-only path still gives the same results after the helper was split. - New `opcodes` test: a shader with a flat load and `image_bvh64_intersect_ray` translates with `needs_global` and without `needs_gmem`, and reads the BVH through `tb_gmem_load`. - `d3d12_xs` unit tests (`global_bind`): the append plan uses 1 root DWORD and respects the DWORD limit and local root signatures, the ranges use valid volatile flags, patching goes through the host serializer (round trip), heap growth only touches shader-visible `CBV_SRV_UAV` heaps, window selection puts explicit windows first, then UAV-capable, then read-only buffers, without duplicates, and the switch is opt-in (unset, empty and `0` are off). 7 passed (and the 10 `gpu_va` tests from #283 pass with the extra field). - `TOMBOX_REMOVED_EXIT_MS=off cargo test -p d3d12_xs --lib -- --test-threads=1`: 672 passed, 4 failed, 71 ignored. The 4 failures are the ones that fail on `main` here too: `alias::tests::msaa_depth_with_a_larger_pc_layout_never_overlaps_the_next_title_placement`, `placed_offset::tests::uneven_views_get_exact_helpers_and_msaa_shares_the_alias_heap`, `title_shadow::tests::buffer_then_texture_migrates_to_one_local_heap_and_keeps_bytes`, `xs_exports::console_fixed_tests::fixed_result_dxgix_exports_match_every_console_d3d12_xs` (needs the console DLL). `dred::tests::report_is_one_shot_and_needs_an_armed_device` passes when run without `TOMBOX_REMOVED_EXIT_MS`. - Native Windows 11; not run under Proton. ## Not verified - **Not verified in a title.** The Matrix Awakens was run with this table on (with a local test branch that contained this change plus an unsubmitted ray-tracing runtime: root signatures were patched and the backing buffers and heap ring were created without a device error), but those runs never got as far as a dispatch that reads global memory, so no title shader has read data through a window yet. - The xdxr device tests need console DLLs and were not run, so it is not known whether `07edd3df1713c3c0` and its siblings now run. - Opt-in on purpose: with `TOMBOX_GLOBAL_BIND=1` every shader-visible heap grows by 1040 descriptors and every non-local root signature with a spare DWORD gets one more root parameter, in every title, and the real-device root-signature hook (which also applies the GDS and centroid patches) is installed without `TOMBOX_RTCAP`. No title run compared that before and after, so it should become the default only once a title confirms it. - The unit tests run with the switch forced on (`enabled()` is true under `cfg(test)`); the off path is covered by the parse test and by the unchanged refusal. - The fault record is written but not read. - [x] Tests pass - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note
omonkeyz commented 2026-11-25 13:51:17 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-25T13:51:17Z.


Thanks for the large piece of work. Two problems block it: (1) the TbGlobal tables and window CBs are written on the CPU at record time into a global ring of 8 with volatile descriptors and no fence, so any list with more than 8 root-signature binds before it executes overwrites data earlier commands still read on the GPU; each bind needs its own slots until the list's fence completes (per-list linear allocation, recycled on completion). (2) In bind_now, the branch that skips binding when the title heap lacks the tail returns false only while once(8) is true; afterwards it binds a GPU handle from the unbound fallback heap. Please also drop 71cec92 (already on main as #283) and the inline comments.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7164387009 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/336#issuecomment-7164387009). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-25T13:51:17Z. --- Thanks for the large piece of work. Two problems block it: (1) the TbGlobal tables and window CBs are written on the CPU at record time into a global ring of 8 with volatile descriptors and no fence, so any list with more than 8 root-signature binds before it executes overwrites data earlier commands still read on the GPU; each bind needs its own slots until the list's fence completes (per-list linear allocation, recycled on completion). (2) In bind_now, the branch that skips binding when the title heap lacks the tail returns false only while once(8) is true; afterwards it binds a GPU handle from the unbound fallback heap. Please also drop 71cec92 (already on main as #283) and the inline comments.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/336:upstream-pr/336'
git switch 'upstream-pr/336'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/336'
git switch 'upstream-pr/336'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/336'
git switch 'upstream-pr/336'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/336'
git switch 'main'
git merge --squash 'upstream-pr/336'
git switch 'main'
git merge --ff-only 'upstream-pr/336'
git switch 'main'
git merge 'upstream-pr/336'
git push origin 'main'
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
omonkeyz/xbox-emulator!336
No description provided.