tombox-xshader: size structured buffer elements from the FSSM stride (stacked on #289) #347

Closed
github-bot wants to merge 3 commits from gh-pr/347/head into gh-pr/347/base
Owner

@⁠GDAgent on GitHub · 2026-11-23 22:43 UTC · original
GDAgent:feat/xshader-fssm-buffer-strides → main · +947 −8 · 9 files · 3 commits · closed without merging

Stacked on #289 (FSSM binding table parser). Review only the last commit, tombox-xshader: size structured buffer elements from the FSSM stride; the two commits below it are #289.

Library / area: tombox-xshader, structured buffer declarations (hlsl.rs: sbuf_words, new fssm_buffer_strides)

What was wrong

The element of a translated StructuredBuffer<TbElem_*> is guessed from the byte extent the ISA touches (sbuf_words, documented as a heuristic, TECHNICAL_DEBT). D3D12 takes the stride from the HLSL element type, so when the guess differs from the title's view every element after the first is read and written at the wrong address. GPU-based validation on The Matrix Awakens (native Windows 11; not run under Proton) reports, among others:

StructureByteStride in UAV/SRV desc: 44, ByteStride for struct used in shader: 48
StructureByteStride in UAV/SRV desc: 32, ByteStride for struct used in shader: 16
StructureByteStride in UAV/SRV desc: 8,  ByteStride for struct used in shader: 16
StructureByteStride in UAV/SRV desc: 16, ByteStride for struct used in shader: 4
[ EXECUTION ERROR #1387: GPU_BASED_VALIDATION_STRUCTURED_BUFFER_STRIDE_MISMATCH ]

One example is the 44-byte queue-state buffer of the persistent Nanite culling kernels: their buffer_atomic_add at byte offsets 8, 12, 16 and 20 became ubuf1[0].w[2], ubuf1[0].w[3], ubuf1[1].w[0], ubuf1[1].w[1] with a 16-byte element, i.e. bytes 8, 12, 44, 48 on a 44-byte view.

The change

The FSSM binding table (#289) records the original stride of every SRV and UAV it binds (for that kernel: 12, 44, 16, 800, 8, and 1 for its raw buffers). fssm_buffer_strides collects the strides per (SRV/UAV, register) once per shader; sbuf_words uses that stride when there is exactly one for the slot's register (space 0) and it is a multiple of 4. Everything else keeps the extent heuristic: registers with conflicting strides, strides 0/1 (textures, raw buffers; raw buffers are handled by #346), slots in other spaces, bindless slots, and shaders without a readable table.

After the change the kernel above declares uint w[11] for the queue state and its atomics land on w[2], w[3], w[4], w[5] (bytes 8..20), as on the console.

Tests

  • tests/fssm_bind.rs: buffer_elements_take_the_fssm_stride (FSSM stride 44 -> uint w[11], indexed by 11 words, DXC-compiled); conflicting_fssm_strides_keep_the_extent_heuristic.
  • Full cargo test -p tombox-xshader --no-fail-fast on WARP: all pass (exec 128 passed, 2 ignored; lib 122 passed, 3 ignored).

In a title

The Matrix Awakens with this PR and #346, D3D12 debug layer with GPU-based validation, same point in the title (second presented frame): 26 STRUCTURED_BUFFER_STRIDE_MISMATCH errors before, 0 GPU-based validation errors over a 600 s run after. 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).

Not verified / limits

  • Only space-0 registers: the FSSM table carries no register space.
  • The FSSM stride is taken as the view stride; the validation run above found no case where they differ, but titles could bind a view with another stride.
**@⁠GDAgent** on GitHub · 2026-11-23 22:43 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/347) `GDAgent:feat/xshader-fssm-buffer-strides` → `main` · +947 −8 · 9 files · 3 commits · closed without merging **Stacked on #289** (FSSM binding table parser). Review only the last commit, `tombox-xshader: size structured buffer elements from the FSSM stride`; the two commits below it are #289. **Library / area:** tombox-xshader, structured buffer declarations (`hlsl.rs`: `sbuf_words`, new `fssm_buffer_strides`) ### What was wrong The element of a translated `StructuredBuffer<TbElem_*>` is guessed from the byte extent the ISA touches (`sbuf_words`, documented as a heuristic, `TECHNICAL_DEBT`). D3D12 takes the stride from the HLSL element type, so when the guess differs from the title's view every element after the first is read and written at the wrong address. GPU-based validation on The Matrix Awakens (native Windows 11; not run under Proton) reports, among others: ``` StructureByteStride in UAV/SRV desc: 44, ByteStride for struct used in shader: 48 StructureByteStride in UAV/SRV desc: 32, ByteStride for struct used in shader: 16 StructureByteStride in UAV/SRV desc: 8, ByteStride for struct used in shader: 16 StructureByteStride in UAV/SRV desc: 16, ByteStride for struct used in shader: 4 [ EXECUTION ERROR #1387: GPU_BASED_VALIDATION_STRUCTURED_BUFFER_STRIDE_MISMATCH ] ``` One example is the 44-byte queue-state buffer of the persistent Nanite culling kernels: their `buffer_atomic_add` at byte offsets 8, 12, 16 and 20 became `ubuf1[0].w[2]`, `ubuf1[0].w[3]`, `ubuf1[1].w[0]`, `ubuf1[1].w[1]` with a 16-byte element, i.e. bytes 8, 12, 44, 48 on a 44-byte view. ### The change The FSSM binding table (#289) records the original stride of every SRV and UAV it binds (for that kernel: 12, 44, 16, 800, 8, and 1 for its raw buffers). `fssm_buffer_strides` collects the strides per `(SRV/UAV, register)` once per shader; `sbuf_words` uses that stride when there is exactly one for the slot's register (space 0) and it is a multiple of 4. Everything else keeps the extent heuristic: registers with conflicting strides, strides 0/1 (textures, raw buffers; raw buffers are handled by #346), slots in other spaces, bindless slots, and shaders without a readable table. After the change the kernel above declares `uint w[11]` for the queue state and its atomics land on `w[2]`, `w[3]`, `w[4]`, `w[5]` (bytes 8..20), as on the console. ### Tests - `tests/fssm_bind.rs`: `buffer_elements_take_the_fssm_stride` (FSSM stride 44 -> `uint w[11]`, indexed by 11 words, DXC-compiled); `conflicting_fssm_strides_keep_the_extent_heuristic`. - Full `cargo test -p tombox-xshader --no-fail-fast` on WARP: all pass (exec 128 passed, 2 ignored; lib 122 passed, 3 ignored). ### In a title The Matrix Awakens with this PR and #346, D3D12 debug layer with GPU-based validation, same point in the title (second presented frame): 26 `STRUCTURED_BUFFER_STRIDE_MISMATCH` errors before, 0 GPU-based validation errors over a 600 s run after. 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). ### Not verified / limits - Only space-0 registers: the FSSM table carries no register space. - The FSSM stride is taken as the view stride; the validation run above found no case where they differ, but titles could bind a view with another stride.
The console shader compiler stores the shader's root-binding layout inside
the FSSM container, next to the code, whether or not the DXBC carries an
RTS0 chunk. fssm_bind::parse reads it:

- 0xE8 / 0xF8: parameter list (pointer + 0x10, count), 5-dword entries
  (kind, dword offset, count, register, param << 8 | user-data slot).
  Kinds: 0 root constants (count = num32), 1 root descriptor (2 SGPRs),
  3 descriptor table, 4 static sampler (index, register), 5 resource heap
  base, 6 sampler heap base. 0xE8 = DEADBEEF means no table.
- 0xF0 / 0xFC: resource list, one entry per descriptor the shader uses:
  (source << 8 | class, dword offset in the table, register, byte stride,
  param << 16 | slot << 8 | descriptor dwords). Class 0 SRV, 1 sampler,
  2 CBV, 3 UAV (not the D3D12 range order). Table strides are 8 dwords for
  CBV/SRV/UAV and 4 for samplers.
- 0xD6: user SGPR count; it equals the end of the last binding.
- 0x18..0x60: exactly one hardware-stage slot is set. 0x28 (merged ES
  part of a vertex shader), 0x30 (GS) and 0x58 (MS) start user data after
  8 system SGPRs; 0x38 (hardware VS), 0x40 (PS) and none (CS) at s0.

The parser refuses anything that does not fit: unknown kinds, gaps in the
SGPR layout, a user SGPR count that does not end at the last binding,
resources that name a parameter of another kind or slot, and a register
used at two table positions (the table has no register space field).

Bindings::root_signature builds a RootSig with one single-descriptor range
per used descriptor (space 0); Bindings::disagreements compares the table
with a real root signature.

Evidence (The Matrix Awakens, all 35,442 shaders with an RTS0, native
Windows 11, CPU only): every table parses, covers every RTS0 parameter,
and every used descriptor resolves to the same range type and register in
the RTS0. The stage-slot rule matches the SGPRs the code reads in every
shader where that can be checked.
translate_with_fssm_root / translate_linked_with_fssm_root translate a
shader that has no RTS0 (and no root signature supplied by the caller)
with the root signature built from its FSSM binding table, placing the
first parameter at the user-data base the table gives. Shaders with a
root signature are translated exactly as before.

Opt-in only:
- TOMBOX_FSSM_ROOT_SIG=1 makes hlsl::translate_linked use the table when
  no root signature is known (added to the d3d12_xs translation-index
  bypass list like TOMBOX_INFER_ROOT_SIG);
- xscan --fssm-root 1 sweeps an archive with the fallback;
- xscan --fssm-check 1 translates every shader that has an RTS0 a second
  time from its table alone and reports agreement and HLSL differences;
- xshader --fssm-root 1 ignores the RTS0 and prints the decoded table.

Static samplers (kind 4) and descriptor-heap bases (kinds 5/6) are parsed
and reported, but the translator still refuses literal samplers and
heap-indexed loads; those need their own work.

Matrix Awakens cross-check (xscan --fssm-check 1, 35,442 shaders with an
RTS0): see the PR description for the per-stage table.
The element of a translated StructuredBuffer is guessed from the byte
extent the ISA touches (a heuristic marked TECHNICAL_DEBT). D3D12 takes
the stride from the HLSL element type, so a wrong guess moves every
element after the first; GPU-based validation reports it as
STRUCTURED_BUFFER_STRIDE_MISMATCH, e.g. "desc: 44, ByteStride for
struct used in shader: 48" and "desc: 32 ... 16".

The FSSM binding table records the original stride of every SRV and UAV
it binds. When it names one stride (a multiple of 4) for the register of
a structured buffer, that stride is the element size. Registers with
conflicting strides, strides 0/1 (textures, raw buffers) and shaders
without a readable table keep the extent heuristic.

Tests (tests/fssm_bind.rs): a buffer with FSSM stride 44 is declared as
uint w[11] and indexed by 11 words (DXC-compiled); two conflicting
strides for one register fall back to the heuristic.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:19:48 +00:00
github-bot closed this pull request 2026-11-26 06:19:48 +00:00
Author
Owner

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

Applied on main in a618736 (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/347#issuecomment-7164369388) Applied on main in a618736 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.
Author
Owner

@⁠tyr0nsur on GitHub · 2026-11-25 20:48 UTC · original

I ran your FSSM binding suite on Windows x64 at a618736df7 and extended the conflict fixture to compile the three resulting pixel shaders with Microsoft DXC. With strides [44, 44] the emitted element has 11 DWORDs. Both [44, 32] and [32, 44] fall back to one DWORD. All 15 tests passed. The smallest addition to the existing fixture is to loop over those three pairs and pass each HLSL result to its existing compile helper. This caught an order-dependent stride choice in our separate DXIL emitter. Thanks for publishing the original regression.

**@⁠tyr0nsur** on GitHub · 2026-11-25 20:48 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/347#issuecomment-7174208328) I ran your FSSM binding suite on Windows x64 at a618736df799d21d9a565d08893355fc3c96fdf5 and extended the conflict fixture to compile the three resulting pixel shaders with Microsoft DXC. With strides [44, 44] the emitted element has 11 DWORDs. Both [44, 32] and [32, 44] fall back to one DWORD. All 15 tests passed. The smallest addition to the existing fixture is to loop over those three pairs and pass each HLSL result to its existing compile helper. This caught an order-dependent stride choice in our separate DXIL emitter. Thanks for publishing the original regression.

Pull request closed

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