tombox-xshader, d3d12_xs: run wave64 compute programs as one 64-lane host wave #204
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!204
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-204"
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 01:06 UTC · original
GDAgent:fix/xshader-wave64-wavesize→main· +590 −40 · 12 files · 1 commits · openLibrary / area:
tombox-xshader(hlsl.rs,hlsl/ds_ext.rs,pack_key.rs, newwave_probe.rs,xshader_prelaunch,xprecompile,xshader_corpus,tests/exec_mem/wave64.rs) andd3d12_xs(shader_xlat.rs,cbv_copy.rstest 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_barrierand 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 doesbuffer_atomic_add, writes the result withds_write_b32, and every lane reads it back withds_read_b32; a ready mask is built withds_or_b32the same way.The translation lost both properties:
[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 forcs_6_0andcs_6_6);groupsharedload above another lane's predicatedgroupsharedstore, 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 (OPTIONS1lane range, then one loop-free dispatch where every thread must report 64 lanes and its own lane index).pack_key::Modegainswave64, written to the runtime mode line and read byxshader_prelaunch, so runtime and prelaunch pack keys still match; mode lines without it read aswave64=0.pack_key::host_waveadds[WaveSize(64)]and raises the profile to at leastcs_6_6for wave64 compute programs when the host supports it; runtime, prelaunch,xprecompileandxshader_corpusall go through it.Translated::wave64marks wave64 compute programs.A compute LDS store or atomic (not GDS) is followed by
GroupMemoryBarrier()only when both hold:s_barrieron the way (a walk over the guest program's forward branches and back-edges,ds_ext::lds_hazard_stores);lds_orderalready insertsGroupMemoryBarrierWithGroupSync()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_b32in wave64 compute programs addresslane & (WaveGetLaneCount() - 1)instead oflane & 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 existingguest_lanerules.Runtime switch
TOMBOX_WAVE64_HOST=0turns the whole change off for A/B runs: no[WaveSize(64)], no added LDS fences, and the previouslane & 31rule.Modegainswave64_path. It is written to the runtime mode line and read byxshader_prelaunch;xprecompileandxshader_corpususe it too.pack_key::finalizeremoves the fences and restores the lane mask, so both DXC and FXC see the previous HLSL.index_ctx) now includes the wave64 mode and the switch, so an index entry from one mode is never used in the other.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:v_readlane/v_writelanelane 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=1runs the hardware cases):tests/exec_mem/wave64.rs:buffer_atomic_addandds_write_b32unders_and_saveexec_b64and all lanesds_read_b32the 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 withoutWaveSize(64), lanes 32..63 do;v_readlane_b32from lane 40 andv_writelane_b32to lane 45 on a 64-lane host wave;cs_6_6.TOMBOX_WAVE64_HOST=0the handoff program has no added fence, noWaveSize(64)and the& 31ulane mask, it still compiles, and its key differs from the switched-on one;Modeline round trip withwave64_path; prelaunch takes the recorded switch from the runtime mode line.s_barrierseparates the store from every read, when no LDS read follows, or for a top-level store and read (which keeps the existingGroupMemoryBarrierWithGroupSync()).wave_probeverdicts,Modeline round trip (old lines read aswave64=0),host_wave, prelaunch mode resolution, and runtime/prelaunch key equality withwave64on.maind8d37e4:cargo test -p tombox-xshader --no-fail-fast354 passed, 0 failed (WARP).--test exec wave64withTOMBOX_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(withTOMBOX_REMOVED_EXIT_MS=off): 643 passed, 4 failed, 69 ignored. The same 4 fail onmainwithout 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, andxs_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 currentmain.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 atcs_6_6with[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-xshaderpassesmissingwith a note@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: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.
@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.
@GDAgent on GitHub · 2026-11-23 09:47 UTC · original
Rebased on
d8d37e4and pushed as0f56aa7. 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:ds_ext::lds_hazard_storeswalks 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 ans_barrier.lds_orderalready puts aGroupMemoryBarrierWithGroupSync()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 agroupsharedload 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:v_readlane/v_writelanelane mask.A new translation test pins the placement:
s_barrierseparates 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 --libhas the same 4 failures asmainhere.@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.
@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:
TOMBOX_WAVE64_HOST=0would 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.Nothing from this PR goes into a release until the Trackmania A/B is done.
@GDAgent on GitHub · 2026-11-23 10:06 UTC · original
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.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'sv_readlane/v_writelanelane 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 tomain, and post here when it's pushed.@GDAgent on GitHub · 2026-11-23 10:39 UTC · original
Pushed as
65bf460, with the switch:TOMBOX_WAVE64_HOST=0turns the whole change off: no[WaveSize(64)], no added LDS fences, and the previouslane & 31rule forv_readlane/v_writelane. Without the variable (or with any other value) it stays on. The wave probe honours it as well.Modegainswave64_path, written to the runtime mode line and read byxshader_prelaunch;xprecompileandxshader_corpususe it too.pack_key::finalizeremoves the fences and restores the lane mask, so DXC and FXC both get the previous HLSL.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.main.WaveSize(64)and the& 31umask, still compiles, and gets a different key;Moderound-trips withwave64_path; prelaunch takes the recorded switch from the runtime mode line.Results on
d8d37e4:tombox-xshader354 passed, 0 failed (WARP); wave64 hardware tests 6/6 (RDNA2, gfx1032);d3d12_xs --lib643 passed, with the same 4 failures asmainhere. The branch also merges cleanly with the currentmain.@kiwe147 on GitHub · 2026-11-23 18:57 UTC · original
Tested head
65bf460216, applied withgit apply(whitespace ignored, the line endings of my checkout differ) onmainf89e410, 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.
tombox-xshaderlib +opcodes,synthetic,mesh,linkage,mixed_table,ps_linkage,centroid,gcn_dims,amp,xshader_cli,isa_sweepexecsuite on WARPTOMBOX_EXEC_HW=1(RTX 3080)d3d12_xs --lib -- --test-threads=1main: the console-dump fixture test needs console DLLs)d3d12_xs --lib hw_ -- --ignored --test-threads=1main: theupload_ww_hwtests need the built xg_xs/xg_x dll)No new failure against
mainf89e410in any of these. This is one combined tree, not a run of this PR alone.AI-assisted: yes (Claude Code)
@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:
TOMBOX_EXEC_HW=1);v_readlane/v_writelaneacross 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.
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.Merge
Merge the changes and update on Forgejo.