tombox-xshader, d3d12_xs: run wave64 compute programs as one 64-lane host wave #204

Open
github-bot wants to merge 1 commit from github-bot/gh-204 into main AGit
Owner

@⁠GDAgent on GitHub · 2026-11-23 01:06 UTC · original
GDAgent:fix/xshader-wave64-wavesize → main · +590 −40 · 12 files · 1 commits · open

Library / area: tombox-xshader (hlsl.rs, hlsl/ds_ext.rs, pack_key.rs, new wave_probe.rs, xshader_prelaunch, xprecompile, xshader_corpus, tests/exec_mem/wave64.rs) and d3d12_xs (shader_xlat.rs, cbv_copy.rs test literal)

Exports changed (exports.txt rows): none

Source of the behaviour: RDNA2 ISA and console shader disassembly. A console compute program compiled for wave64 with a 64-thread group is one wave, so the console compiler emits no s_barrier and lanes hand data to each other through LDS in program order. Example from the Nanite persistent cluster-cull shader in The Matrix Awakens: lane 0 does buffer_atomic_add, writes the result with ds_write_b32, and every lane reads it back with ds_read_b32; a ready mask is built with ds_or_b32 the same way.

The translation lost both properties:

  • the host driver picks the wave size of a [numthreads(64, 1, 1)] compute shader itself; on an RDNA2 GPU (gfx1032) it ran these as two independent 32-lane waves (measured: WaveGetLaneCount() is 32 for cs_6_0 and cs_6_6);
  • even inside one host wave, the host compiler moved a groupshared load above another lane's predicated groupshared store, because nothing in the HLSL orders accesses of different threads.

Changes:

  • wave_probe: measures once per process whether the device runs a [WaveSize(64)] group of 64 as one wave (OPTIONS1 lane range, then one loop-free dispatch where every thread must report 64 lanes and its own lane index).

  • pack_key::Mode gains wave64, written to the runtime mode line and read by xshader_prelaunch, so runtime and prelaunch pack keys still match; mode lines without it read as wave64=0. pack_key::host_wave adds [WaveSize(64)] and raises the profile to at least cs_6_6 for wave64 compute programs when the host supports it; runtime, prelaunch, xprecompile and xshader_corpus all go through it. Translated::wave64 marks wave64 compute programs.

  • A compute LDS store or atomic (not GDS) is followed by GroupMemoryBarrier() only when both hold:

    • another LDS read is reachable from it without an s_barrier on the way (a walk over the guest program's forward branches and back-edges, ds_ext::lds_hazard_stores);
    • the store is emitted inside control flow (a structured loop or branch, or the block dispatcher). At the top level, lds_order already inserts GroupMemoryBarrierWithGroupSync() before the next LDS read or the next block, but it cannot do that inside control flow.

    Stores whose reads sit behind an s_barrier, stores that are never read back, and top-level stores keep today's output. The rule does not depend on the wave size: the reorder this fixes happens inside one host wave.

  • v_readlane_b32/v_writelane_b32 in wave64 compute programs address lane & (WaveGetLaneCount() - 1) instead of lane & 31, so lanes 32..63 are reachable on a 64-lane host wave. On a 32-lane host wave this is the same as before, and other stages (whose host wave size the driver still chooses) keep the existing guest_lane rules.

  • Runtime switch TOMBOX_WAVE64_HOST=0 turns the whole change off for A/B runs: no [WaveSize(64)], no added LDS fences, and the previous lane & 31 rule.

    • Mode gains wave64_path. It is written to the runtime mode line and read by xshader_prelaunch; xprecompile and xshader_corpus use it too.
    • When the switch is off, pack_key::finalize removes the fences and restores the lane mask, so both DXC and FXC see the previous HLSL.
    • The pack key hashes the final HLSL, so the two modes never share a compiled shader. The runtime's guest-shader index context (index_ctx) now includes the wave64 mode and the switch, so an index entry from one mode is never used in the other.
    • With the switch off, all 35382 translated shaders of The Matrix Awakens' archive are byte-identical to main.

Without a 64-lane host wave (no WaveSize(64) support, for example 32-lane hardware or WARP, or no DXC) the translation is unchanged apart from the LDS fences.

Effect on The Matrix Awakens' shader archive (Xbox Series X build; 35382 translated shaders, HLSL compared with main), switch on:

  • byte-identical: every VS, PS, GS and MS, and 2374 of 3209 compute programs;
  • 734 compute programs (621 of them wave64) gain 7429 fences; the previous every-store rule gave 995 programs 13666 fences;
  • 101 wave64 compute programs only change in the v_readlane/v_writelane lane mask.

Relation to earlier PRs: #65 (closed) read the PSV0 wave lane bounds to choose the guest wave size; this PR keeps that choice and makes the host run the chosen size. The wave32-guest lane rules from #62 are untouched.

Tests (TOMBOX_EXEC_HW=1 runs the hardware cases):

  • tests/exec_mem/wave64.rs:
    • a guest wave64 loop of 64 iterations where lane 0 does buffer_atomic_add and ds_write_b32 under s_and_saveexec_b64 and all lanes ds_read_b32 the value. With both changes all 64 lanes read the current value in every iteration. Without the fence 63 lanes read the previous iteration's value; with the fence but without WaveSize(64), lanes 32..63 do;
    • v_readlane_b32 from lane 40 and v_writelane_b32 to lane 45 on a 64-lane host wave;
    • the wave64 marker, the attribute and the profile appear only on a wave64 host, and DXC compiles the result at cs_6_6.
    • the switch: with TOMBOX_WAVE64_HOST=0 the handoff program has no added fence, no WaveSize(64) and the & 31u lane mask, it still compiles, and its key differs from the switched-on one; Mode line round trip with wave64_path; prelaunch takes the recorded switch from the runtime mode line.
    • fence placement: one fence for the predicated store read back in the same loop; none when an s_barrier separates the store from every read, when no LDS read follows, or for a top-level store and read (which keeps the existing GroupMemoryBarrierWithGroupSync()).
  • unit tests: wave_probe verdicts, Mode line round trip (old lines read as wave64=0), host_wave, prelaunch mode resolution, and runtime/prelaunch key equality with wave64 on.
  • On main d8d37e4: cargo test -p tombox-xshader --no-fail-fast 354 passed, 0 failed (WARP). --test exec wave64 with TOMBOX_EXEC_HW=1: 6/6 on an RDNA2 GPU (gfx1032). With the fence disabled, the handoff case fails again (63 of 64 lanes read stale values). cargo test -p d3d12_xs --lib -- --test-threads=1 (with TOMBOX_REMOVED_EXIT_MS=off): 643 passed, 4 failed, 69 ignored. The same 4 fail on main without this change on this machine: alias::tests::msaa_depth_with_a_larger_pc_layout_never_overlaps_the_next_title_placement, placed_offset::tests::uneven_views_get_exact_helpers_and_msaa_shares_the_alias_heap, title_shadow::tests::buffer_then_texture_migrates_to_one_local_heap_and_keeps_bytes, and xs_exports::console_fixed_tests::fixed_result_dxgix_exports_match_every_console_d3d12_xs (the last one needs console DLLs). The branch still merges cleanly with the current main.
  • Native Windows 11; not run under Proton.

Not verified in a title: the earlier every-store version of this change was run in The Matrix Awakens on a separate test branch; the narrowed fence rule has not been run in a title yet. The probe reported 64-lane waves, the persistent cluster-cull shader (wave64, 64 threads, no s_barrier, 10 LDS fences, the same 10 under the narrowed rule) and the Nanite main pass (73 fences under the earlier every-store rule) both compiled at cs_6_6 with [WaveSize(64)], and the title got past loading to its intro video. It is not shown that the cluster cull now finishes without the loop guard: those runs used a loop guard, and the longer run later hit a device hang in a different compute shader that is still being investigated. No title has been checked for visual correctness with this change.

  • cargo test -p tombox-xshader passes
  • No game or console files and no binaries included
  • No fake success; anything not possible is left missing with a note
**@⁠GDAgent** on GitHub · 2026-11-23 01:06 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/204) `GDAgent:fix/xshader-wave64-wavesize` → `main` · +590 −40 · 12 files · 1 commits · open **Library / area:** `tombox-xshader` (`hlsl.rs`, `hlsl/ds_ext.rs`, `pack_key.rs`, new `wave_probe.rs`, `xshader_prelaunch`, `xprecompile`, `xshader_corpus`, `tests/exec_mem/wave64.rs`) and `d3d12_xs` (`shader_xlat.rs`, `cbv_copy.rs` test literal) **Exports changed (exports.txt rows):** none **Source of the behaviour:** RDNA2 ISA and console shader disassembly. A console compute program compiled for wave64 with a 64-thread group is one wave, so the console compiler emits no `s_barrier` and lanes hand data to each other through LDS in program order. Example from the Nanite persistent cluster-cull shader in The Matrix Awakens: lane 0 does `buffer_atomic_add`, writes the result with `ds_write_b32`, and every lane reads it back with `ds_read_b32`; a ready mask is built with `ds_or_b32` the same way. The translation lost both properties: - the host driver picks the wave size of a `[numthreads(64, 1, 1)]` compute shader itself; on an RDNA2 GPU (gfx1032) it ran these as two independent 32-lane waves (measured: `WaveGetLaneCount()` is 32 for `cs_6_0` and `cs_6_6`); - even inside one host wave, the host compiler moved a `groupshared` load above another lane's predicated `groupshared` store, because nothing in the HLSL orders accesses of different threads. Changes: - `wave_probe`: measures once per process whether the device runs a `[WaveSize(64)]` group of 64 as one wave (`OPTIONS1` lane range, then one loop-free dispatch where every thread must report 64 lanes and its own lane index). - `pack_key::Mode` gains `wave64`, written to the runtime mode line and read by `xshader_prelaunch`, so runtime and prelaunch pack keys still match; mode lines without it read as `wave64=0`. `pack_key::host_wave` adds `[WaveSize(64)]` and raises the profile to at least `cs_6_6` for wave64 compute programs when the host supports it; runtime, prelaunch, `xprecompile` and `xshader_corpus` all go through it. `Translated::wave64` marks wave64 compute programs. - A compute LDS store or atomic (not GDS) is followed by `GroupMemoryBarrier()` only when both hold: - another LDS read is reachable from it without an `s_barrier` on the way (a walk over the guest program's forward branches and back-edges, `ds_ext::lds_hazard_stores`); - the store is emitted inside control flow (a structured loop or branch, or the block dispatcher). At the top level, `lds_order` already inserts `GroupMemoryBarrierWithGroupSync()` before the next LDS read or the next block, but it cannot do that inside control flow. Stores whose reads sit behind an `s_barrier`, stores that are never read back, and top-level stores keep today's output. The rule does not depend on the wave size: the reorder this fixes happens inside one host wave. - `v_readlane_b32`/`v_writelane_b32` in wave64 compute programs address `lane & (WaveGetLaneCount() - 1)` instead of `lane & 31`, so lanes 32..63 are reachable on a 64-lane host wave. On a 32-lane host wave this is the same as before, and other stages (whose host wave size the driver still chooses) keep the existing `guest_lane` rules. - **Runtime switch `TOMBOX_WAVE64_HOST=0`** turns the whole change off for A/B runs: no `[WaveSize(64)]`, no added LDS fences, and the previous `lane & 31` rule. - `Mode` gains `wave64_path`. It is written to the runtime mode line and read by `xshader_prelaunch`; `xprecompile` and `xshader_corpus` use it too. - When the switch is off, `pack_key::finalize` removes the fences and restores the lane mask, so both DXC and FXC see the previous HLSL. - The pack key hashes the final HLSL, so the two modes never share a compiled shader. The runtime's guest-shader index context (`index_ctx`) now includes the wave64 mode and the switch, so an index entry from one mode is never used in the other. - With the switch off, all 35382 translated shaders of The Matrix Awakens' archive are byte-identical to `main`. Without a 64-lane host wave (no `WaveSize(64)` support, for example 32-lane hardware or WARP, or no DXC) the translation is unchanged apart from the LDS fences. Effect on The Matrix Awakens' shader archive (Xbox Series X build; 35382 translated shaders, HLSL compared with `main`), switch on: - byte-identical: every VS, PS, GS and MS, and 2374 of 3209 compute programs; - 734 compute programs (621 of them wave64) gain 7429 fences; the previous every-store rule gave 995 programs 13666 fences; - 101 wave64 compute programs only change in the `v_readlane`/`v_writelane` lane mask. Relation to earlier PRs: #65 (closed) read the PSV0 wave lane bounds to choose the guest wave size; this PR keeps that choice and makes the host run the chosen size. The wave32-guest lane rules from #62 are untouched. Tests (`TOMBOX_EXEC_HW=1` runs the hardware cases): - `tests/exec_mem/wave64.rs`: - a guest wave64 loop of 64 iterations where lane 0 does `buffer_atomic_add` and `ds_write_b32` under `s_and_saveexec_b64` and all lanes `ds_read_b32` the value. With both changes all 64 lanes read the current value in every iteration. Without the fence 63 lanes read the previous iteration's value; with the fence but without `WaveSize(64)`, lanes 32..63 do; - `v_readlane_b32` from lane 40 and `v_writelane_b32` to lane 45 on a 64-lane host wave; - the wave64 marker, the attribute and the profile appear only on a wave64 host, and DXC compiles the result at `cs_6_6`. - the switch: with `TOMBOX_WAVE64_HOST=0` the handoff program has no added fence, no `WaveSize(64)` and the `& 31u` lane mask, it still compiles, and its key differs from the switched-on one; `Mode` line round trip with `wave64_path`; prelaunch takes the recorded switch from the runtime mode line. - fence placement: one fence for the predicated store read back in the same loop; none when an `s_barrier` separates the store from every read, when no LDS read follows, or for a top-level store and read (which keeps the existing `GroupMemoryBarrierWithGroupSync()`). - unit tests: `wave_probe` verdicts, `Mode` line round trip (old lines read as `wave64=0`), `host_wave`, prelaunch mode resolution, and runtime/prelaunch key equality with `wave64` on. - On `main` `d8d37e4`: `cargo test -p tombox-xshader --no-fail-fast` 354 passed, 0 failed (WARP). `--test exec wave64` with `TOMBOX_EXEC_HW=1`: 6/6 on an RDNA2 GPU (gfx1032). With the fence disabled, the handoff case fails again (63 of 64 lanes read stale values). `cargo test -p d3d12_xs --lib -- --test-threads=1` (with `TOMBOX_REMOVED_EXIT_MS=off`): 643 passed, 4 failed, 69 ignored. The same 4 fail on `main` without this change on this machine: `alias::tests::msaa_depth_with_a_larger_pc_layout_never_overlaps_the_next_title_placement`, `placed_offset::tests::uneven_views_get_exact_helpers_and_msaa_shares_the_alias_heap`, `title_shadow::tests::buffer_then_texture_migrates_to_one_local_heap_and_keeps_bytes`, and `xs_exports::console_fixed_tests::fixed_result_dxgix_exports_match_every_console_d3d12_xs` (the last one needs console DLLs). The branch still merges cleanly with the current `main`. - Native Windows 11; not run under Proton. **Not verified in a title:** the earlier every-store version of this change was run in The Matrix Awakens on a separate test branch; the narrowed fence rule has not been run in a title yet. The probe reported 64-lane waves, the persistent cluster-cull shader (wave64, 64 threads, no `s_barrier`, 10 LDS fences, the same 10 under the narrowed rule) and the Nanite main pass (73 fences under the earlier every-store rule) both compiled at `cs_6_6` with `[WaveSize(64)]`, and the title got past loading to its intro video. It is **not** shown that the cluster cull now finishes without the loop guard: those runs used a loop guard, and the longer run later hit a device hang in a different compute shader that is still being investigated. No title has been checked for visual correctness with this change. - [x] `cargo test -p tombox-xshader` passes - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note
A console compute program compiled for wave64 with a 64-thread group is
a single wave, so the console compiler emits no s_barrier and lanes
hand data to each other through LDS in program order (for example one
lane does a buffer atomic, writes the result to LDS, and every lane reads
it back). The translation lost both properties:
- the host driver picks the wave size of a [numthreads(64, 1, 1)]
  compute shader itself; on an RDNA2 GPU it ran these as two independent
  32-lane waves (measured: WaveGetLaneCount() 32 for cs_6_0 and cs_6_6);
- even within one host wave, the host compiler moved a groupshared load
  above another lane's predicated groupshared store, since nothing in the
  HLSL orders accesses of different threads.

Changes:
- wave_probe: measures, once per process, whether the device runs a
  [WaveSize(64)] group of 64 as one wave (OPTIONS1 lane range, then a
  dispatch where every thread must report 64 lanes and its own lane
  index).
- pack_key::Mode gains wave64 (written to the runtime mode line, read by
  xshader_prelaunch, so runtime and prelaunch keys still match).
  pack_key::host_wave adds [WaveSize(64)] and raises the profile to at
  least cs_6_6 for wave64 compute programs when the host supports it;
  runtime, prelaunch, xprecompile and xshader_corpus all use it.
  Translated.wave64 marks wave64 compute programs.
- A compute LDS store or atomic (not GDS) is followed by
  GroupMemoryBarrier() only when another LDS read is reachable from it
  without an s_barrier on the way (a forward/back-edge walk over the
  guest program) and the store is emitted inside control flow. Top-level
  stores are already ordered by the GroupMemoryBarrierWithGroupSync that
  lds_order inserts before the next LDS read or the next block, and
  stores whose reads sit behind s_barrier keep today's output.
- v_readlane/v_writelane in wave64 compute programs address
  lane & (lane count - 1) instead of lane & 31, so lanes 32..63 are
  reachable on a 64-lane host wave (unchanged on a 32-lane host wave and
  in other stages, whose host wave size the driver still chooses).

TOMBOX_WAVE64_HOST=0 switches the whole change off at run time for A/B
comparisons: no [WaveSize(64)], no added LDS fences and the previous
lane & 31 rule. Mode gains wave64_path (in the runtime mode line, read
by xshader_prelaunch, also used by xprecompile and xshader_corpus);
pack_key::finalize removes the fences and restores the lane mask when
it is off, so both compilers see the previous HLSL. The pack key covers
the final HLSL, so the two modes never share a compiled shader, and the
runtime's guest-shader index context now includes the wave64 mode and
the path switch, so an index entry from one mode is never used in the
other. With the switch off, all 35382 translated shaders of The Matrix
Awakens' archive are byte-identical to main.

Without a 64-lane host wave (no WaveSize(64) support, or DXC missing)
the translation is unchanged apart from the LDS fences. Over The Matrix
Awakens' shader archive (35382 translated shaders) the HLSL is byte-identical to
main for every VS/PS/GS/MS and for 2374 of 3209 compute programs; 734
compute programs gain 7429 fences (the previous every-store rule gave
995 programs 13666 fences) and 101 wave64 programs only get the
lane-count mask.

Tests (tests/exec_mem/wave64.rs, hardware cases with TOMBOX_EXEC_HW=1):
- a guest wave64 loop of 64 iterations where lane 0 does
  buffer_atomic_add and ds_write_b32 under s_and_saveexec_b64 and all
  lanes ds_read_b32 the value: with both changes all 64 lanes read the
  current value in every iteration; without the fence 63 lanes read the
  previous iteration's value, with the fence but without WaveSize(64)
  lanes 32..63 do;
- v_readlane_b32 from lane 40 and v_writelane_b32 to lane 45 on a
  64-lane host wave;
- the wave64 marker, the attribute and profile only on a wave64 host,
  and DXC compiling the result at cs_6_6;
- the fence is placed for the predicated store read back in the same
  loop, and not when an s_barrier separates store and reads, when no
  LDS read follows, or for a top-level store and read;
- the switch: fences, lane mask and WaveSize gone with TOMBOX_WAVE64_HOST=0
  and the result compiles, mode line round trip, key difference;
- wave_probe verdicts, Mode line round trip (old lines read as wave64=0),
  host_wave, prelaunch mode resolution, and runtime/prelaunch key
  equality with wave64 on.

Not verified in a title yet. Native Windows 11; not run under Proton.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:13:48 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 01:41 UTC · original

Thanks, this is a careful change and the reasoning is well documented. It's on hold for now, not rejected. It adds a GroupMemoryBarrier after every LDS store and atomic in every compute program, on all hosts. It also changes the pack key and the compute profile, so it affects GPU time and shader cache keys for every title. Trackmania is in the middle of a performance bisect. This will get its own A/B run once that is done.

One question while it waits: could the fences be limited to wave64 compute programs, or to LDS stores whose address can be read by another lane in the same wave without an s_barrier in between? Then wave32 programs and 32-lane hosts would keep today's output.

**@⁠omonkeyz** on GitHub · 2026-11-23 01:41 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/204#issuecomment-7089340301) Thanks, this is a careful change and the reasoning is well documented. It's on hold for now, not rejected. It adds a GroupMemoryBarrier after every LDS store and atomic in every compute program, on all hosts. It also changes the pack key and the compute profile, so it affects GPU time and shader cache keys for every title. Trackmania is in the middle of a performance bisect. This will get its own A/B run once that is done. One question while it waits: could the fences be limited to wave64 compute programs, or to LDS stores whose address can be read by another lane in the same wave without an s_barrier in between? Then wave32 programs and 32-lane hosts would keep today's output.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 01:57 UTC · original

Follow-up from the graphics lead: the pack_key mode field is fine and can stay. Please scope the GroupMemoryBarrier to the cases that need it: wave64 emulated on 32-lane hosts, or cross-lane LDS hazards that the analysis actually finds. Don't add it after every LDS store in all programs. After that, it gets an A/B run on Trackmania.

**@⁠omonkeyz** on GitHub · 2026-11-23 01:57 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/204#issuecomment-7089549771) Follow-up from the graphics lead: the pack_key mode field is fine and can stay. Please scope the GroupMemoryBarrier to the cases that need it: wave64 emulated on 32-lane hosts, or cross-lane LDS hazards that the analysis actually finds. Don't add it after every LDS store in all programs. After that, it gets an A/B run on Trackmania.
Author
Owner

@⁠thatirishguyyy on GitHub · 2026-11-23 09:26 UTC · original

I tested the fence placement on an RX 9070 XT at 315a25b. The probe confirmed one 64-lane host wave. Removing the injected fence still produced stale LDS results in 63/64 lanes. In a synthetic two-store variant, keeping only the fence at the cross-lane handoff matched all 64 lanes and the counter in 3/3 runs. Keeping only the fence for the unrelated store gave 63/64 stale results.

A scratch translator variant that uses pending LDS-write state to fence before a wave64 loop read also passed the handoff and the other four focused checks, including a wave32 control with no added fence. That is fixture-level evidence for narrower placement. Address overlap, broader control flow, and Trackmania performance remain untested. AI-assisted (Codex). No game files used.

**@⁠thatirishguyyy** on GitHub · 2026-11-23 09:26 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/204#issuecomment-7096654285) I tested the fence placement on an RX 9070 XT at `315a25b`. The probe confirmed one 64-lane host wave. Removing the injected fence still produced stale LDS results in 63/64 lanes. In a synthetic two-store variant, keeping only the fence at the cross-lane handoff matched all 64 lanes and the counter in 3/3 runs. Keeping only the fence for the unrelated store gave 63/64 stale results. A scratch translator variant that uses pending LDS-write state to fence before a wave64 loop read also passed the handoff and the other four focused checks, including a wave32 control with no added fence. That is fixture-level evidence for narrower placement. Address overlap, broader control flow, and Trackmania performance remain untested. AI-assisted (Codex). No game files used.
Author
Owner

@⁠GDAgent on GitHub · 2026-11-23 09:47 UTC · original

Rebased on d8d37e4 and pushed as 0f56aa7. The fence is now scoped as you asked. pack_key, the probe and [WaveSize(64)] are unchanged.

An LDS store or atomic in a compute program (not GDS) gets GroupMemoryBarrier() only when both of these hold:

  1. The analysis finds a cross-lane hazard. ds_ext::lds_hazard_stores walks the guest program's forward branches and back-edges from the store. It only counts the store if it reaches an LDS read without passing an s_barrier.
  2. The store is emitted inside control flow (a structured loop or branch, or the block dispatcher). At the top level, lds_order already puts a GroupMemoryBarrierWithGroupSync() before the next LDS read or block. It can't do that inside control flow, and that is where the measured handoff is.

So stores that are never read back, stores whose reads sit behind an s_barrier, and top-level stores keep today's output. I didn't tie the rule to wave64 or to 32-lane hosts. The reorder it fixes happens inside one host wave (the host compiler hoists a groupshared load above another lane's predicated store), so a 64-lane host still needs it. On a 32-lane host, a fence can't order two separate waves anyway.

I measured it on one title's shader archive (35382 translated shaders), comparing the HLSL with main:

  • Byte-identical: every VS, PS, GS and MS, and 2374 of 3209 compute programs. That includes every wave32 compute program without such a hazard.
  • 734 compute programs (621 of them wave64) gain 7429 fences. The previous every-store rule gave 995 programs 13666 fences.
  • 101 wave64 programs only change in the v_readlane/v_writelane lane mask.

A new translation test pins the placement:

  • One fence for the predicated store that is read back in the same loop.
  • None when an s_barrier separates the store from its reads, when nothing reads LDS afterwards, or for a top-level store and read.

On an RDNA2 GPU the handoff test still gets all 64 lanes right. With the fence disabled, 63 of 64 lanes read stale values again. Suite results are in the description; d3d12_xs --lib has the same 4 failures as main here.

@⁠thatirishguyyy thanks for testing on the 9070 XT. Your two-store result matches this version: a store gets a fence only if an LDS read can follow it without an s_barrier, so an unrelated store that is never read back gets none. A store that only its own lane reads back still gets a fence; that would need address analysis, which this doesn't do. A fence before the read (your scratch variant) would also work. I kept it on the store side because one fence there also covers reads reached through a back-edge.

The narrowed rule hasn't been run in a title yet, and there's no Trackmania A/B from me.

**@⁠GDAgent** on GitHub · 2026-11-23 09:47 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/204#issuecomment-7097105701) Rebased on `d8d37e4` and pushed as `0f56aa7`. The fence is now scoped as you asked. `pack_key`, the probe and `[WaveSize(64)]` are unchanged. An LDS store or atomic in a compute program (not GDS) gets `GroupMemoryBarrier()` only when both of these hold: 1. **The analysis finds a cross-lane hazard.** `ds_ext::lds_hazard_stores` walks the guest program's forward branches and back-edges from the store. It only counts the store if it reaches an LDS read without passing an `s_barrier`. 2. **The store is emitted inside control flow** (a structured loop or branch, or the block dispatcher). At the top level, `lds_order` already puts a `GroupMemoryBarrierWithGroupSync()` before the next LDS read or block. It can't do that inside control flow, and that is where the measured handoff is. So stores that are never read back, stores whose reads sit behind an `s_barrier`, and top-level stores keep today's output. I didn't tie the rule to wave64 or to 32-lane hosts. The reorder it fixes happens inside one host wave (the host compiler hoists a `groupshared` load above another lane's predicated store), so a 64-lane host still needs it. On a 32-lane host, a fence can't order two separate waves anyway. I measured it on one title's shader archive (35382 translated shaders), comparing the HLSL with `main`: - Byte-identical: every VS, PS, GS and MS, and 2374 of 3209 compute programs. That includes every wave32 compute program without such a hazard. - 734 compute programs (621 of them wave64) gain 7429 fences. The previous every-store rule gave 995 programs 13666 fences. - 101 wave64 programs only change in the `v_readlane`/`v_writelane` lane mask. A new translation test pins the placement: - One fence for the predicated store that is read back in the same loop. - None when an `s_barrier` separates the store from its reads, when nothing reads LDS afterwards, or for a top-level store and read. On an RDNA2 GPU the handoff test still gets all 64 lanes right. With the fence disabled, 63 of 64 lanes read stale values again. Suite results are in the description; `d3d12_xs --lib` has the same 4 failures as `main` here. @⁠thatirishguyyy thanks for testing on the 9070 XT. Your two-store result matches this version: a store gets a fence only if an LDS read can follow it without an `s_barrier`, so an unrelated store that is never read back gets none. A store that only its own lane reads back still gets a fence; that would need address analysis, which this doesn't do. A fence before the read (your scratch variant) would also work. I kept it on the store side because one fence there also covers reads reached through a back-edge. The narrowed rule hasn't been run in a title yet, and there's no Trackmania A/B from me.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 09:57 UTC · original

Thanks for the rebase and the narrower fence rule, and thanks to thatirishguyyy for the hardware runs. Two questions before we can A/B this on Trackmania:

  1. Could the change sit behind a runtime switch? For example, TOMBOX_WAVE64_HOST=0 would restore today's path: 32-lane host waves and today's fence placement. The translation-cache key would need to include the mode so cached shaders from one mode are never used in the other. That lets us compare both paths in a single build.
  2. Which title is the 35,382-shader archive you measured? We'll run our own Trackmania corpus compare either way, but it helps to know whether your numbers already cover it.

Nothing from this PR goes into a release until the Trackmania A/B is done.

**@⁠omonkeyz** on GitHub · 2026-11-23 09:57 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/204#issuecomment-7097312233) Thanks for the rebase and the narrower fence rule, and thanks to thatirishguyyy for the hardware runs. Two questions before we can A/B this on Trackmania: 1. Could the change sit behind a runtime switch? For example, `TOMBOX_WAVE64_HOST=0` would restore today's path: 32-lane host waves and today's fence placement. The translation-cache key would need to include the mode so cached shaders from one mode are never used in the other. That lets us compare both paths in a single build. 2. Which title is the 35,382-shader archive you measured? We'll run our own Trackmania corpus compare either way, but it helps to know whether your numbers already cover it. Nothing from this PR goes into a release until the Trackmania A/B is done.
Author
Owner

@⁠GDAgent on GitHub · 2026-11-23 10:06 UTC · original

  1. The archive is from The Matrix Awakens (Xbox Series X build, UE5; its main .pak, 35444 unique shader containers, 35382 of them translate). So the numbers don't cover Trackmania; your own corpus compare is still needed. The hardware runs here are on an RDNA2 GPU (gfx1032), native Windows 11.

  2. Yes. I'm adding the switch now, using your name TOMBOX_WAVE64_HOST=0. It will restore today's path completely: no [WaveSize(64)], today's fence placement and today's v_readlane/v_writelane lane mask. The mode goes into the pack/cache key, so shaders cached in one mode are never used in the other. I'll check that, with the switch off, the whole archive translates byte-identically to main, and post here when it's pushed.

**@⁠GDAgent** on GitHub · 2026-11-23 10:06 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/204#issuecomment-7097495089) 2. The archive is from The Matrix Awakens (Xbox Series X build, UE5; its main `.pak`, 35444 unique shader containers, 35382 of them translate). So the numbers don't cover Trackmania; your own corpus compare is still needed. The hardware runs here are on an RDNA2 GPU (gfx1032), native Windows 11. 1. Yes. I'm adding the switch now, using your name `TOMBOX_WAVE64_HOST=0`. It will restore today's path completely: no `[WaveSize(64)]`, today's fence placement and today's `v_readlane`/`v_writelane` lane mask. The mode goes into the pack/cache key, so shaders cached in one mode are never used in the other. I'll check that, with the switch off, the whole archive translates byte-identically to `main`, and post here when it's pushed.
Author
Owner

@⁠GDAgent on GitHub · 2026-11-23 10:39 UTC · original

Pushed as 65bf460, with the switch:

  • TOMBOX_WAVE64_HOST=0 turns the whole change off: no [WaveSize(64)], no added LDS fences, and the previous lane & 31 rule for v_readlane/v_writelane. Without the variable (or with any other value) it stays on. The wave probe honours it as well.
  • Cache separation: Mode gains wave64_path, written to the runtime mode line and read by xshader_prelaunch; xprecompile and xshader_corpus use it too.
    • When the switch is off, pack_key::finalize removes the fences and restores the lane mask, so DXC and FXC both get the previous HLSL.
    • The pack key hashes that final HLSL, so the two modes never share a compiled shader.
    • While adding this I found that the runtime's guest-shader index (index_ctx, which maps a guest shader straight to a pack entry without translating) didn't include the wave64 mode. It now includes both the host wave64 verdict and the switch, so an index entry from one mode is never used in the other.
  • Check: with the switch off, all 35382 translated shaders of the Matrix archive are byte-identical to main.
  • Tests: with the switch off, the handoff program has no fence, no WaveSize(64) and the & 31u mask, still compiles, and gets a different key; Mode round-trips with wave64_path; prelaunch takes the recorded switch from the runtime mode line.

Results on d8d37e4: tombox-xshader 354 passed, 0 failed (WARP); wave64 hardware tests 6/6 (RDNA2, gfx1032); d3d12_xs --lib 643 passed, with the same 4 failures as main here. The branch also merges cleanly with the current main.

**@⁠GDAgent** on GitHub · 2026-11-23 10:39 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/204#issuecomment-7098292231) Pushed as `65bf460`, with the switch: - **`TOMBOX_WAVE64_HOST=0`** turns the whole change off: no `[WaveSize(64)]`, no added LDS fences, and the previous `lane & 31` rule for `v_readlane`/`v_writelane`. Without the variable (or with any other value) it stays on. The wave probe honours it as well. - **Cache separation:** `Mode` gains `wave64_path`, written to the runtime mode line and read by `xshader_prelaunch`; `xprecompile` and `xshader_corpus` use it too. - When the switch is off, `pack_key::finalize` removes the fences and restores the lane mask, so DXC and FXC both get the previous HLSL. - The pack key hashes that final HLSL, so the two modes never share a compiled shader. - While adding this I found that the runtime's guest-shader index (`index_ctx`, which maps a guest shader straight to a pack entry without translating) didn't include the wave64 mode. It now includes both the host wave64 verdict and the switch, so an index entry from one mode is never used in the other. - **Check:** with the switch off, all 35382 translated shaders of the Matrix archive are byte-identical to `main`. - **Tests:** with the switch off, the handoff program has no fence, no `WaveSize(64)` and the `& 31u` mask, still compiles, and gets a different key; `Mode` round-trips with `wave64_path`; prelaunch takes the recorded switch from the runtime mode line. Results on `d8d37e4`: `tombox-xshader` 354 passed, 0 failed (WARP); wave64 hardware tests 6/6 (RDNA2, gfx1032); `d3d12_xs --lib` 643 passed, with the same 4 failures as `main` here. The branch also merges cleanly with the current `main`.
Author
Owner

@⁠kiwe147 on GitHub · 2026-11-23 18:57 UTC · original

Tested head 65bf460216, applied with git apply (whitespace ignored, the line endings of my checkout differ) on main f89e410, together with #233, #234 (one tree; the PRs touch different files). It applied without conflicts and built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, NVIDIA GeForce RTX 3080 (driver 621.24), MSVC, Rust 1.91.1.

run result
tombox-xshader lib + opcodes, synthetic, mesh, linkage, mixed_table, ps_linkage, centroid, gcn_dims, amp, xshader_cli, isa_sweep 211 passed, 0 failed
the tests the PRs of this tree add, in the exec suite on WARP 5 passed, 0 failed
the same tests with TOMBOX_EXEC_HW=1 (RTX 3080) 5 passed, 0 failed
d3d12_xs --lib -- --test-threads=1 667 passed, 1 failed (the same 1 fail on main: the console-dump fixture test needs console DLLs)
d3d12_xs --lib hw_ -- --ignored --test-threads=1 53 passed, 9 failed (the same 9 fail on main: the upload_ww_hw tests need the built xg_xs/xg_x dll)

No new failure against main f89e410 in any of these. This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

**@⁠kiwe147** on GitHub · 2026-11-23 18:57 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/204#issuecomment-7110886789) Tested head `65bf460216`, applied with `git apply` (whitespace ignored, the line endings of my checkout differ) on `main` `f89e410`, together with #233, #234 (one tree; the PRs touch different files). It applied without conflicts and built. Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, NVIDIA GeForce RTX 3080 (driver 621.24), MSVC, Rust 1.91.1. | run | result | |---|---| | `tombox-xshader` lib + `opcodes`, `synthetic`, `mesh`, `linkage`, `mixed_table`, `ps_linkage`, `centroid`, `gcn_dims`, `amp`, `xshader_cli`, `isa_sweep` | 211 passed, 0 failed | | the tests the PRs of this tree add, in the `exec` suite on WARP | 5 passed, 0 failed | | the same tests with `TOMBOX_EXEC_HW=1` (RTX 3080) | 5 passed, 0 failed | | `d3d12_xs --lib -- --test-threads=1` | 667 passed, 1 failed (the same 1 fail on `main`: the console-dump fixture test needs console DLLs) | | `d3d12_xs --lib hw_ -- --ignored --test-threads=1` | 53 passed, 9 failed (the same 9 fail on `main`: the `upload_ww_hw` tests need the built xg_xs/xg_x dll) | No new failure against `main` `f89e410` in any of these. This is one combined tree, not a run of this PR alone. AI-assisted: yes (Claude Code)
Author
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-25 15:31 UTC · original

@⁠GDAgent, are you still working on this? It now conflicts with main. If you've moved on, I can:

  • rebase it;
  • rerun its tests on an AMD Radeon RX 9060 XT (TOMBOX_EXEC_HW=1);
  • check its wave64 lane rules on gfx1036 silicon: v_readlane/v_writelane across the two 32-lane halves, and LDS order within one wave.

I'd keep your authorship, and I won't push anything to your branch without your OK.

**@⁠SchmashinPrunes** on GitHub · 2026-11-25 15:31 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/204#issuecomment-7166963732) @⁠GDAgent, are you still working on this? It now conflicts with main. If you've moved on, I can: - rebase it; - rerun its tests on an AMD Radeon RX 9060 XT (`TOMBOX_EXEC_HW=1`); - check its wave64 lane rules on gfx1036 silicon: `v_readlane`/`v_writelane` across the two 32-lane halves, and LDS order within one wave. I'd keep your authorship, and I won't push anything to your branch without your OK.
This pull request has changes conflicting with the target branch.
  • crates/tombox-xshader/src/hlsl.rs
  • crates/tombox-xshader/tests/exec_mem/mod.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/204/head:github-bot/gh-204'
git switch 'github-bot/gh-204'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-204'
git switch 'github-bot/gh-204'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-204'
git switch 'github-bot/gh-204'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-204'
git switch 'main'
git merge --squash 'github-bot/gh-204'
git switch 'main'
git merge --ff-only 'github-bot/gh-204'
git switch 'main'
git merge 'github-bot/gh-204'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.