tombox-xshader: declare raw buffers as ByteAddressBuffer and address them by byte #346
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
library
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/omonkeyz--xbox-emulator!346
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/346/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 mergingLibrary / area: tombox-xshader, MUBUF/MTBUF buffer accesses (
hlsl.rs,hlsl/atomic.rs,hlsl/mtbuf.rs, newhlsl/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 (SRVRaw4 /UAVRaw7). Titles create raw views for those (StructureByteStride0), 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):
In that title the persistent Nanite culling kernels (
PersistentClusterCull) keep their node and cluster-batch queues in such raw UAVs (PSV0type=7 kind=11at u2/u3/u4).The change
buf_is_rawlooks the slot up in PSV0 (register + space, not bindless).RWByteAddressBuffer/ByteAddressBuffer; all accesses use the byte address: dword loads/stores (Load/Store), sub-dword loads/stores (maskedInterlockedAnd/InterlockedOron the containing dword), atomics including the compare-exchange loops ofsmin/smax/inc/dec, MTBUF loads/stores, and scalar loads from the SRV/UAV table (tb_sbw_*becomesLoad).BufRef(element expression or raw buffer + byte address) carries the target intoatomic_rmwand the sub-dword path, so structured and typed buffers emit exactly the HLSL they emitted before.Tests
tests/exec.rs: the exec harness creates a raw view (R32_TYPELESS,D3D12_BUFFER_UAV_FLAG_RAW) when the translated shader declaresRWByteAddressBuffer.buffer_mubufruns 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 keepsStructuredBuffer<TbElem_sbuf0>; raw + IDXEN refused for an atomic and a store; the existing raw UAV store + atomic test now expectsubuf0.Store(/ubuf0.InterlockedOr(.cargo test -p tombox-xshader --no-fail-faston WARP: all pass (exec 128 passed, 2 ignored).TOMBOX_EXEC_HW=1on an AMD Radeon RX 6600 XT (RDNA2):buffer_mubuf(includingmubuf_raw_view),mubuf_atomic_scalar_offsetandmubuf_scalar_offsetpass.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):
STRUCTURED_BUFFER_STRIDE_MISMATCHdesc: 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
atomic.rs) and with #199 (globallycoherentdeclarations): whichever lands second needs a rebase; with #199 the raw declaration becomesgloballycoherent RWByteAddressBufferfor 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).@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.Pull request closed