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

Open
github-bot wants to merge 2 commits from github-bot/gh-336 into main AGit
Owner

@⁠GDAgent on GitHub · 2026-11-23 20:25 UTC · original
GDAgent:feat/d3d12-tbglobal-windows → main · +1111 −96 · 12 files · 2 commits · open

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

**@⁠GDAgent** on GitHub · 2026-11-23 20:25 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/336) `GDAgent:feat/d3d12-tbglobal-windows` → `main` · +1111 −96 · 12 files · 2 commits · open **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
gpu_va keeps the GPU and CPU address ranges of every buffer and texture
it was told about, but nothing removed them when the title released the
resource. A range stayed until a newer resource overlapped it, so a
lookup could return a resource that no longer exists. Code that turns
such an entry into a descriptor then passes a destroyed resource to the
driver: in The Matrix Awakens (Xbox Series X build) this crashed inside
CreateShaderResourceView about 31 s into loading, reading the vtable of
the freed resource (AV READ at 0x50, the vtable pointer was 0).

note_resource now attaches a small private-data object to each resource
(SetPrivateDataInterface with its own GUID), once per resource. The
runtime releases it when the resource is destroyed, and its last Release
removes that resource's ranges (recorded per resource at insert time, so
an entry of another resource at the same start is left alone), its
texture VA, width and state. If SetPrivateDataInterface fails, nothing is
forgotten and the next note retries.

Tests: forget removes only the ranges of that resource, including a
smaller range nested at the same start as another resource's; a real
committed buffer noted twice is found by lookup and is gone after its
last Release (the test fails without the watcher: the destroyed buffer is
still returned).
Shaders that load or store through a 64-bit GPU virtual address (flat_* and
global_* instructions, image_bvh*_intersect_ray) were refused because
d3d12_xs had nothing to bind in space 4000003.

tombox-xshader:
- TbGlobal now has 64 windows (was 2): a CBV with the window table and a
  per-window writable flag, 64 raw SRVs (read-only buffers), 64 raw UAVs
  (UAV-capable buffers) and one fault UAV, reached through one descriptor
  table (1 root DWORD). Reads use the SRV window when the buffer is not
  writable, so upload and read-only buffers work without a UAV.
- Window lookup loops over the number of windows in use (tb_msin.z).
- A shader that uses both global memory and the BVH intersect
  instructions now reads the BVH through TbGlobal instead of also needing
  the separate TbGmem window, so it needs one binding. The BVH helper keeps
  the same traversal; only its loads go through tb_bvh_load1/3/4.

d3d12_xs:
- global_bind appends the TbGlobal 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 tail ring, and on
  SetComputeRootSignature / SetGraphicsRootSignature writes the window CBV
  and descriptors for up to 64 buffers from the gpu_va map and binds the
  table. It is opt-in: only TOMBOX_GLOBAL_BIND=1 (any value but 0)
  turns it on; without it the old refusal stays and nothing changes.
- gpu_va remembers which buffers were created with ALLOW_UNORDERED_ACCESS
  and exposes a snapshot of the tracked buffers.
- The real-device CreateRootSignature hook (GDS, TbGlobal, centroid
  binding, console part stripping) was installed only together with the
  descriptor-trace hooks (TOMBOX_RTCAP), so without it no root signature
  ever got the table. It is now installed on its own when TbGlobal binding
  is on (or with TOMBOX_RTCAP / the pass-capture tables, as before); the
  trace hooks stay behind TOMBOX_RTCAP.
- The TbGlobal descriptor ranges used flags the serializer rejects
  (DATA_VOLATILE | DATA_STATIC_WHILE_SET_AT_EXECUTE), so patching silently
  fell back to the unpatched blob. They are now DESCRIPTORS_VOLATILE |
  DATA_VOLATILE, and the serializer's error text is logged on failure.

Tests: root-signature append plan and limits, range flags, patching through
the host serializer, heap growth, window selection order, window table
layout, a translation test for global memory plus BVH in one shader, and
the exec tests for global windows, routing, narrow loads at a window end
(updated for 64 windows and the SRV/UAV split).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:19:23 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:51 UTC · original

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.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:51 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/336#issuecomment-7164387009) 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 has changes conflicting with the target branch.
  • crates/compat/d3d12_xs/src/gpu_va.rs
  • crates/tombox-xshader/src/hlsl.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/336/head:github-bot/gh-336'
git switch 'github-bot/gh-336'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-336'
git switch 'github-bot/gh-336'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-336'
git switch 'github-bot/gh-336'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-336'
git switch 'main'
git merge --squash 'github-bot/gh-336'
git switch 'main'
git merge --ff-only 'github-bot/gh-336'
git switch 'main'
git merge 'github-bot/gh-336'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.