tombox-xshader: declare raw buffers as ByteAddressBuffer and address them by byte #346

Closed
github-bot wants to merge 1 commit from gh-pr/346/head into gh-pr/346/base
Owner

@⁠GDAgent on GitHub · 2026-11-23 22:28 UTC · original
GDAgent:fix/xshader-raw-buffers-and-strides → main · +184 −26 · 6 files · 1 commits · closed without merging

Library / area: tombox-xshader, MUBUF/MTBUF buffer accesses (hlsl.rs, hlsl/atomic.rs, hlsl/mtbuf.rs, new hlsl/bufref.rs)

What was wrong

Every title buffer reached through MUBUF/MTBUF is declared StructuredBuffer<TbElem_*> with an element guessed from the touched extent (usually 16 bytes), also when PSV0 says the resource is raw (SRVRaw 4 / UAVRaw 7). Titles create raw views for those (StructureByteStride 0), so on the PC the element index is scaled by a stride the view does not have.

D3D12 GPU-based validation shows it directly, e.g. on The Matrix Awakens (native Windows 11; not run under Proton):

GPU-BASED VALIDATION: Dispatch, Structured buffer byte stride mismatch: ... Descriptor Type: UAV,
StructureByteStride in UAV/SRV desc: 0, ByteStride for struct used in shader: 16
[ EXECUTION ERROR #1387: GPU_BASED_VALIDATION_STRUCTURED_BUFFER_STRIDE_MISMATCH ]

In that title the persistent Nanite culling kernels (PersistentClusterCull) keep their node and cluster-batch queues in such raw UAVs (PSV0 type=7 kind=11 at u2/u3/u4).

The change

  • buf_is_raw looks the slot up in PSV0 (register + space, not bindless).
  • Raw buffers are declared RWByteAddressBuffer / ByteAddressBuffer; all accesses use the byte address: dword loads/stores (Load/Store), sub-dword loads/stores (masked InterlockedAnd/InterlockedOr on the containing dword), atomics including the compare-exchange loops of smin/smax/inc/dec, MTBUF loads/stores, and scalar loads from the SRV/UAV table (tb_sbw_* becomes Load).
  • A small BufRef (element expression or raw buffer + byte address) carries the target into atomic_rmw and the sub-dword path, so structured and typed buffers emit exactly the HLSL they emitted before.
  • A raw buffer addressed with IDXEN is refused ("raw buffer ... addressed with IDXEN is unsupported"): the element stride of a raw descriptor is not known, and guessing it is what this PR removes.

Tests

  • tests/exec.rs: the exec harness creates a raw view (R32_TYPELESS, D3D12_BUFFER_UAV_FLAG_RAW) when the translated shader declares RWByteAddressBuffer.
    • buffer_mubuf runs its 178 non-IDXEN, non-format cases a second time with a raw PSV0 entry against that raw view (mubuf_raw_view): byte/short/dword loads and stores, D16 variants, all atomics.
    • mubuf_atomic_scalar_offset: the raw group now runs against a raw view with plain byte addresses (its expected values used to replicate the extent heuristic); IDXEN forms are not run for raw buffers.
  • tests/synthetic.rs: raw SRV -> ByteAddressBuffer + Load (DXC-compiled); structured SRV keeps StructuredBuffer<TbElem_sbuf0>; raw + IDXEN refused for an atomic and a store; the existing raw UAV store + atomic test now expects ubuf0.Store( / ubuf0.InterlockedOr(.
  • Full cargo test -p tombox-xshader --no-fail-fast on WARP: all pass (exec 128 passed, 2 ignored).
  • TOMBOX_EXEC_HW=1 on an AMD Radeon RX 6600 XT (RDNA2): buffer_mubuf (including mubuf_raw_view), mubuf_atomic_scalar_offset and mubuf_scalar_offset pass.

In a title

The Matrix Awakens, native Windows 11 (not run under Proton), D3D12 debug layer with GPU-based validation, same point in the title (second presented frame):

build STRUCTURED_BUFFER_STRIDE_MISMATCH other GPU-based validation errors
before 26 (6 of them raw views, desc: 0) 0
this PR + the FSSM stride follow-up (structured strides, separate PR) 0 over a 600 s run 0

The after-run used a local diagnostic that limits the 2040-group persistent culling dispatch to 8 groups, because that dispatch still exceeds the GPU timeout on this RDNA2 card (under investigation, not changed by this PR). With correct bindings the title still does not reach ray dispatches; this PR only fixes the addressing.

Not verified / limits

  • Raw buffers with IDXEN are refused rather than translated.
  • Overlaps textually with #251 (64-bit MUBUF atomics, same atomic site and atomic.rs) and with #199 (globallycoherent declarations): whichever lands second needs a rebase; with #199 the raw declaration becomes globallycoherent RWByteAddressBuffer for coherent UAVs (done and used in the title runs above).
**@⁠GDAgent** on GitHub · 2026-11-23 22:28 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/346) `GDAgent:fix/xshader-raw-buffers-and-strides` → `main` · +184 −26 · 6 files · 1 commits · closed without merging **Library / area:** tombox-xshader, MUBUF/MTBUF buffer accesses (`hlsl.rs`, `hlsl/atomic.rs`, `hlsl/mtbuf.rs`, new `hlsl/bufref.rs`) ### What was wrong Every title buffer reached through MUBUF/MTBUF is declared `StructuredBuffer<TbElem_*>` with an element guessed from the touched extent (usually 16 bytes), also when PSV0 says the resource is **raw** (`SRVRaw` 4 / `UAVRaw` 7). Titles create raw views for those (`StructureByteStride` 0), so on the PC the element index is scaled by a stride the view does not have. D3D12 GPU-based validation shows it directly, e.g. on The Matrix Awakens (native Windows 11; not run under Proton): ``` GPU-BASED VALIDATION: Dispatch, Structured buffer byte stride mismatch: ... Descriptor Type: UAV, StructureByteStride in UAV/SRV desc: 0, ByteStride for struct used in shader: 16 [ EXECUTION ERROR #1387: GPU_BASED_VALIDATION_STRUCTURED_BUFFER_STRIDE_MISMATCH ] ``` In that title the persistent Nanite culling kernels (`PersistentClusterCull`) keep their node and cluster-batch queues in such raw UAVs (PSV0 `type=7 kind=11` at u2/u3/u4). ### The change - `buf_is_raw` looks the slot up in PSV0 (register + space, not bindless). - Raw buffers are declared `RWByteAddressBuffer` / `ByteAddressBuffer`; all accesses use the byte address: dword loads/stores (`Load`/`Store`), sub-dword loads/stores (masked `InterlockedAnd`/`InterlockedOr` on the containing dword), atomics including the compare-exchange loops of `smin`/`smax`/`inc`/`dec`, MTBUF loads/stores, and scalar loads from the SRV/UAV table (`tb_sbw_*` becomes `Load`). - A small `BufRef` (element expression or raw buffer + byte address) carries the target into `atomic_rmw` and the sub-dword path, so structured and typed buffers emit **exactly the HLSL they emitted before**. - A raw buffer addressed with **IDXEN** is refused ("raw buffer ... addressed with IDXEN is unsupported"): the element stride of a raw descriptor is not known, and guessing it is what this PR removes. ### Tests - `tests/exec.rs`: the exec harness creates a raw view (`R32_TYPELESS`, `D3D12_BUFFER_UAV_FLAG_RAW`) when the translated shader declares `RWByteAddressBuffer`. - `buffer_mubuf` runs its 178 non-IDXEN, non-format cases a second time with a raw PSV0 entry against that raw view (`mubuf_raw_view`): byte/short/dword loads and stores, D16 variants, all atomics. - `mubuf_atomic_scalar_offset`: the raw group now runs against a raw view with plain byte addresses (its expected values used to replicate the extent heuristic); IDXEN forms are not run for raw buffers. - `tests/synthetic.rs`: raw SRV -> `ByteAddressBuffer` + `Load` (DXC-compiled); structured SRV keeps `StructuredBuffer<TbElem_sbuf0>`; raw + IDXEN refused for an atomic and a store; the existing raw UAV store + atomic test now expects `ubuf0.Store(` / `ubuf0.InterlockedOr(`. - Full `cargo test -p tombox-xshader --no-fail-fast` on WARP: all pass (exec 128 passed, 2 ignored). - `TOMBOX_EXEC_HW=1` on an AMD Radeon RX 6600 XT (RDNA2): `buffer_mubuf` (including `mubuf_raw_view`), `mubuf_atomic_scalar_offset` and `mubuf_scalar_offset` pass. ### In a title The Matrix Awakens, native Windows 11 (not run under Proton), D3D12 debug layer with GPU-based validation, same point in the title (second presented frame): | build | `STRUCTURED_BUFFER_STRIDE_MISMATCH` | other GPU-based validation errors | |---|---|---| | before | 26 (6 of them raw views, `desc: 0`) | 0 | | this PR + the FSSM stride follow-up (structured strides, separate PR) | 0 over a 600 s run | 0 | The after-run used a local diagnostic that limits the 2040-group persistent culling dispatch to 8 groups, because that dispatch still exceeds the GPU timeout on this RDNA2 card (under investigation, not changed by this PR). With correct bindings the title still does not reach ray dispatches; this PR only fixes the addressing. ### Not verified / limits - Raw buffers with IDXEN are refused rather than translated. - Overlaps textually with #251 (64-bit MUBUF atomics, same atomic site and `atomic.rs`) and with #199 (`globallycoherent` declarations): whichever lands second needs a rebase; with #199 the raw declaration becomes `globallycoherent RWByteAddressBuffer` for coherent UAVs (done and used in the title runs above).
Buffers that PSV0 lists as raw (SRVRaw 4, UAVRaw 7) were declared as
StructuredBuffer<TbElem> with a guessed 16-byte element. Titles create
raw views for them (StructureByteStride 0), so on the PC every element
index was scaled by the wrong stride. D3D12 GPU-based validation reports
it as GPU_BASED_VALIDATION_STRUCTURED_BUFFER_STRIDE_MISMATCH ("desc: 0,
ByteStride for struct used in shader: 16"). In The Matrix Awakens the
persistent Nanite culling queues are such buffers.

Raw buffers are now declared (RW)ByteAddressBuffer and every MUBUF and
MTBUF access to them (dword loads and stores, sub-dword loads and
stores, atomics including the compare-exchange loops, scalar loads from
the SRV/UAV table) uses Load/Store/Interlocked* at the byte address. A
raw buffer addressed with IDXEN is refused, because the element stride
of a raw descriptor is not known. Structured and typed buffers are
unchanged and emit the same HLSL as before.

Tests:
- exec: buffer_mubuf runs its 178 non-IDXEN, non-format cases a second
  time against a real raw view (R32_TYPELESS, RAW flag);
  mubuf_atomic_scalar_offset runs its raw group against a raw view with
  plain byte addresses. The harness creates a raw view when the shader
  declares RWByteAddressBuffer.
- synthetic: raw SRV -> ByteAddressBuffer + Load (DXC-compiled),
  structured SRV keeps its element struct, raw + IDXEN is refused for an
  atomic and a store; the raw UAV store + atomic test now expects
  ubuf0.Store and ubuf0.InterlockedOr.

Full tombox-xshader suite on WARP: all pass (exec 128 passed, 2 ignored).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:19:46 +00:00
github-bot closed this pull request 2026-11-26 06:19:46 +00:00
Author
Owner

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

Applied on main in 36d9c51 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:50 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/346#issuecomment-7164368462) Applied on main in 36d9c51 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.