tombox-xshader: amplification payload byte/short stores were lost on hardware; edge-case AS tests (stacked on #313) #343

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

@⁠MeNoSkills on GitHub · 2026-11-23 22:05 UTC · original
MeNoSkills:test/amp-edge → main · +329 −9 · 2 files · 3 commits · closed without merging

What

Stacked on #313: the first commit in this branch is #313's harness, only the top commit is new. Opened as a draft until #313 is on main.

An amplification shader that stores a byte or a short into the payload ring never got that store to the mesh stage on hardware. The partial store was merged with InterlockedAnd / InterlockedOr on the groupshared payload variable, and on an RTX 4070 Ti those atomics are lost: the payload keeps its old dword. The same atomics on the args dwords work. The partial store into the payload is now a plain read-modify-write (hlsl/amp.rs). Every thread of the group runs the same stores, so every thread writes the same merged value.

Tests added (tests/amp.rs, on the synthetic AS + MS pair)

  • gpu_synthetic_amplification_group_count_zero_launches_nothing: AS group 0 writes count 0 and group 1 writes count 1, only group 1's pixel is drawn; both groups at 0 draw nothing.
  • gpu_synthetic_amplification_four_mesh_groups_each_see_their_own_payload: count 4 from each AS group fills all 8 pixels, each with its own AS group's payload and its own mesh group index.
  • gpu_synthetic_amplification_payload_stride_rounds_up_to_sixteen: a 40-byte payload has a 48-byte ring stride (as in the console fixture as2); AS group 1's entry at 48 is accepted and 0 global misses.
  • gpu_synthetic_amplification_group_count_from_a_byte_store: the group count written with global_store_byte into the zeroed args dword.
  • gpu_synthetic_amplification_sub_dword_payload_stores_merge: global_store_byte, _short, _byte_d16_hi and _short_d16_hi into the payload merge into the dword already there. This is the test that fails without the fix: all four stores were dropped.
  • gpu_synthetic_mesh_sub_dword_payload_loads_extend_correctly (second commit): the mesh stage reads the payload with global_load_ubyte/sbyte/ushort/sshort, including unaligned 16-bit loads at +13; zero extension for the u forms and sign extension for the s forms. No defect found, it was only covered by a compile test.

synth_as keeps its signature; synth_as_with takes an optional tail and a payload stride, and synth_pair_with a payload size.

Testing

  • TOMBOX_EXEC_HW=1 cargo test --locked --release -p tombox-xshader --test amp --test mesh --test synthetic on an RTX 4070 Ti (Windows 10 19045): all pass (amp 8 passed, 4 ignored fixture tests).
  • Without the hlsl/amp.rs change the sub-dword payload test fails with the payload unchanged ([0x100, 0, 0, 0x5a5a] where [0xab00, 0, 0xbeef0000, 0x7e575acd] is expected).
  • WARP on this machine reports MeshShaderTier 0, so the GPU tests need TOMBOX_EXEC_HW=1 here; I could not check the WARP path. If the atomics only fail on NVIDIA, a different mapping may be preferable and I am happy to change it.
  • pr_gate.py --base upstream/main: 0 problems.

Limits

Hand-written instruction sequences, not console compiler output; no game files involved. The plain read-modify-write can in theory race between threads that store different bytes of the same dword; in these tests all threads of the group run in lockstep on one warp.

**@⁠MeNoSkills** on GitHub · 2026-11-23 22:05 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/343) `MeNoSkills:test/amp-edge` → `main` · +329 −9 · 2 files · 3 commits · closed without merging ## What Stacked on #313: the first commit in this branch is #313's harness, only the top commit is new. Opened as a draft until #313 is on main. An amplification shader that stores a byte or a short into the payload ring never got that store to the mesh stage on hardware. The partial store was merged with `InterlockedAnd` / `InterlockedOr` on the groupshared payload variable, and on an RTX 4070 Ti those atomics are lost: the payload keeps its old dword. The same atomics on the args dwords work. The partial store into the payload is now a plain read-modify-write (`hlsl/amp.rs`). Every thread of the group runs the same stores, so every thread writes the same merged value. ## Tests added (`tests/amp.rs`, on the synthetic AS + MS pair) - `gpu_synthetic_amplification_group_count_zero_launches_nothing`: AS group 0 writes count 0 and group 1 writes count 1, only group 1's pixel is drawn; both groups at 0 draw nothing. - `gpu_synthetic_amplification_four_mesh_groups_each_see_their_own_payload`: count 4 from each AS group fills all 8 pixels, each with its own AS group's payload and its own mesh group index. - `gpu_synthetic_amplification_payload_stride_rounds_up_to_sixteen`: a 40-byte payload has a 48-byte ring stride (as in the console fixture as2); AS group 1's entry at 48 is accepted and 0 global misses. - `gpu_synthetic_amplification_group_count_from_a_byte_store`: the group count written with `global_store_byte` into the zeroed args dword. - `gpu_synthetic_amplification_sub_dword_payload_stores_merge`: `global_store_byte`, `_short`, `_byte_d16_hi` and `_short_d16_hi` into the payload merge into the dword already there. This is the test that fails without the fix: all four stores were dropped. - `gpu_synthetic_mesh_sub_dword_payload_loads_extend_correctly` (second commit): the mesh stage reads the payload with `global_load_ubyte/sbyte/ushort/sshort`, including unaligned 16-bit loads at +13; zero extension for the u forms and sign extension for the s forms. No defect found, it was only covered by a compile test. `synth_as` keeps its signature; `synth_as_with` takes an optional tail and a payload stride, and `synth_pair_with` a payload size. ## Testing - `TOMBOX_EXEC_HW=1 cargo test --locked --release -p tombox-xshader --test amp --test mesh --test synthetic` on an RTX 4070 Ti (Windows 10 19045): all pass (amp 8 passed, 4 ignored fixture tests). - Without the `hlsl/amp.rs` change the sub-dword payload test fails with the payload unchanged ([0x100, 0, 0, 0x5a5a] where [0xab00, 0, 0xbeef0000, 0x7e575acd] is expected). - WARP on this machine reports MeshShaderTier 0, so the GPU tests need `TOMBOX_EXEC_HW=1` here; I could not check the WARP path. If the atomics only fail on NVIDIA, a different mapping may be preferable and I am happy to change it. - `pr_gate.py --base upstream/main`: 0 problems. ## Limits Hand-written instruction sequences, not console compiler output; no game files involved. The plain read-modify-write can in theory race between threads that store different bytes of the same dword; in these tests all threads of the group run in lockstep on one warp.
All four tests in tests/amp.rs need the console_xsc fixtures from local
title dumps. Without them, no amplification shader ever runs on a GPU,
and CI checks the AS path only at the HLSL/DXC level.

This adds a hand-assembled AS + MS pair and renders it on WARP. The pair
follows the ring ABI the translator decodes: s2/s3 = args/payload ring
>> 8, s4 = group id, and the MS payload window in s[10:11].

- The AS shifts the ring bases left by 8 and offsets them by its group.
  It stores a 16-byte payload with global_store_dwordx4 and the mesh
  group count with global_store_dword. Only dword 0 of the args entry is
  written, so the open Y/Z encoding question in BLOCKERS.md doesn't
  affect the test.
- The MS reads the payload with global_load_dwordx4 through s[10:11]. It
  draws one pixel-wide triangle per mesh group at x = payload[0] + group
  and exports the payload tag, its group index and the AS group.
- Two AS groups launch 1 and 2 mesh groups. They draw pixels 0, 4 and 5
  with no global-memory misses, the same pattern as the as3/ms3 fixture
  test.
- A second case loads the group count and the tag as root constants
  through s[0:1] with s_load_dwordx2. This is the memory-based root ABI
  (rootmem.rs) that amplification shaders must use. It checks that
  pixels 0, 1, 4 and 5 are drawn.

render() is split into render_pair(), which takes translated shaders and
optional root constants. The fixture test still goes through render().
WARP renders take the same lock that tests/mesh.rs uses.

Passes on WARP, and with TOMBOX_EXEC_HW=1 on an RTX 3050 Laptop GPU.
The translator itself is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A byte or short store into the amplification payload ring merged with
InterlockedAnd/InterlockedOr on the groupshared payload variable. On an
RTX 4070 Ti those atomics never reach the mesh stage: the payload keeps its
old dword. The same atomics on the args dwords work. The partial store is
now a plain read-modify-write; every thread of the group runs the same
stores, so each writes the same merged value.

tests/amp.rs, on top of the synthetic AS+MS pair, adds:
- group count 0 launches no mesh group (and 0 from both AS groups draws nothing);
- 4 mesh groups per AS group, each sees its own AS group's payload;
- a 40-byte payload with the 48-byte ring stride;
- group count written with global_store_byte;
- global_store_byte / short / byte_d16_hi / short_d16_hi into the payload
  (this one fails without the fix).

The AS builder gains an optional tail and a payload stride; synth_as keeps
its signature.
The mesh stage reads the AS payload with global_load_ubyte/sbyte/ushort/sshort
through the s[10:11] window. Until now only a compile test covered these. The
synthetic pair writes dword 3 as 0x808182f3 from the AS and the MS overwrites
two exported components with sub-dword loads at offsets 12..15, including
unaligned 16-bit loads at +13: zero extension for the u forms, sign extension
for the s forms. All four combinations match on an RTX 4070 Ti (no defect found).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:19:38 +00:00
github-bot closed this pull request 2026-11-26 06:19:38 +00:00
Author
Owner

@⁠BuHHuTlyx on GitHub · 2026-11-23 22:20 UTC · original

I reproduced the original partial-store failure on an RTX 3070 (32.0.16.2124, Windows 11 24H2, DXC 1.9.2609.5). With the old emitter and this PR's tests, the payload stayed unchanged on hardware and the test passed on WARP. At 2f055b6, the existing amp tests passed on both backends (9 passed, 4 fixture tests ignored).

I added a synthetic case where different lanes write four disjoint bytes of the same payload dword. The expected word is 0x14131211. This PR's plain read-modify-write produced 0x00005a11 on the RTX 3070 and 0x14005a5a on WARP in my runs.

A prototype plus the regression test keeps the masked atomics on a separate flat groupshared array, then copies its dwords into the mesh payload after the group barrier. That passed all 10 amp tests on each backend, including both partial-store cases. A direct compare-exchange loop on the payload itself reached CreatePipelineState(AS+MS) with 0x8007000e on the RTX 3070.

The staging experiment needs a shared-memory budget decision. Its two arrays and dispatch arguments use 2 * align16(payload_bytes) + 16 bytes even before any other shared declarations, which exceeds the 32 KiB budget at the 16 KiB payload limit. DirectX mesh shader limits.

**@⁠BuHHuTlyx** on GitHub · 2026-11-23 22:20 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/343#issuecomment-7115638663) I reproduced the original partial-store failure on an RTX 3070 (`32.0.16.2124`, Windows 11 24H2, DXC 1.9.2609.5). With the old emitter and this PR's tests, the payload stayed unchanged on hardware and the test passed on WARP. At `2f055b6`, the existing `amp` tests passed on both backends (9 passed, 4 fixture tests ignored). I added a synthetic case where different lanes write four disjoint bytes of the same payload dword. The expected word is `0x14131211`. This PR's plain read-modify-write produced `0x00005a11` on the RTX 3070 and `0x14005a5a` on WARP in my runs. [A prototype plus the regression test](https://github.com/BuHHuTlyx/xbox-emulator/commit/c9dd81489d4062b47b0361aa2824b3a2913e0a61) keeps the masked atomics on a separate flat groupshared array, then copies its dwords into the mesh payload after the group barrier. That passed all 10 `amp` tests on each backend, including both partial-store cases. A direct compare-exchange loop on the payload itself reached `CreatePipelineState(AS+MS)` with `0x8007000e` on the RTX 3070. The staging experiment needs a shared-memory budget decision. Its two arrays and dispatch arguments use `2 * align16(payload_bytes) + 16` bytes even before any other shared declarations, which exceeds the 32 KiB budget at the 16 KiB payload limit. [DirectX mesh shader limits](https://microsoft.github.io/DirectX-Specs/d3d/MeshShader.html#mesh-shader-output-size-limits).
Author
Owner

@⁠BuHHuTlyx on GitHub · 2026-11-23 22:40 UTC · original

I reduced this to two handwritten HLSL shaders using the existing test harness. The amplification shader initializes a payload word to 0x12345678, then uses InterlockedAnd and InterlockedOr to assemble 0x14131211 from four byte lanes. The mesh shader displays the received word.

  • On the RTX 3070 (32.0.16.2124), the direct atomics left 0x12345678 with both constant and group-dependent indexing. A normal read and an atomic read from the AS after the barrier also reported the old value when copied to a second payload word.
  • With atomics on a separate groupshared array and a plain copy into the payload, the RTX 3070 returned 0x14131211. WARP returned the expected value for every variant.
  • DXC 1.9.2609.5 emitted atomicrmw and and atomicrmw or in every variant. The results matched with default optimization and -Od.

Run cargo test --locked -p tombox-xshader --test amp handwritten_amplification_payload_atomics -- --exact --nocapture on the repro branch. Set TOMBOX_EXEC_HW=1 for the hardware device or leave it unset for WARP. The test asserts the staged control and reports hardware-specific direct results.

**@⁠BuHHuTlyx** on GitHub · 2026-11-23 22:40 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/343#issuecomment-7116038697) I reduced this to [two handwritten HLSL shaders](https://github.com/BuHHuTlyx/xbox-emulator/commit/94472f665b93d4a256a016d974167cd40c8720bc) using the existing test harness. The amplification shader initializes a payload word to `0x12345678`, then uses `InterlockedAnd` and `InterlockedOr` to assemble `0x14131211` from four byte lanes. The mesh shader displays the received word. - On the RTX 3070 (`32.0.16.2124`), the direct atomics left `0x12345678` with both constant and group-dependent indexing. A normal read and an atomic read from the AS after the barrier also reported the old value when copied to a second payload word. - With atomics on a separate groupshared array and a plain copy into the payload, the RTX 3070 returned `0x14131211`. WARP returned the expected value for every variant. - DXC 1.9.2609.5 emitted `atomicrmw and` and `atomicrmw or` in every variant. The results matched with default optimization and `-Od`. Run `cargo test --locked -p tombox-xshader --test amp handwritten_amplification_payload_atomics -- --exact --nocapture` on the repro branch. Set `TOMBOX_EXEC_HW=1` for the hardware device or leave it unset for WARP. The test asserts the staged control and reports hardware-specific direct results.
Author
Owner

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

Thanks. Your tests and sub-dword load cases landed through #345, which replaces the plain read-modify-write with a staging ring and atomics after byte stores from different lanes were lost on hardware.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:51 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/343#issuecomment-7164393460) Thanks. Your tests and sub-dword load cases landed through #345, which replaces the plain read-modify-write with a staging ring and atomics after byte stores from different lanes were lost on hardware.

Pull request closed

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