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

Closed
GDAgent wants to merge 1 commit from upstream-pr/346 into main
GDAgent commented 2026-11-23 22:28:37 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by GDAgent on 2026-11-23T22:28:37Z.


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).
<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:6570133157 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/346). Originally posted by [GDAgent](https://github.com/GDAgent) on 2026-11-23T22:28:37Z. --- **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).
omonkeyz commented 2026-11-25 13:50:31 +00:00 (Migrated from github.com)

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


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

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7164368462 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/346#issuecomment-7164368462). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-25T13:50:31Z. --- 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

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!346
No description provided.