Skip to content

shader: run wave64 compute programs in 32-lane halves (port of Marvisak/VIctory#2633) - #180

Merged
pragyaangaur merged 5 commits into
pragyaangaur:mainfrom
earpjonathan:shader/wave64-halves
Nov 26, 2026
Merged

pragyaangaur merged 5 commits into
pragyaangaur:mainfrom
earpjonathan:shader/wave64-halves

Conversation

@earpjonathan

Copy link
Copy Markdown
Contributor

This ports chippy01's Marvisak/VIctory#2633 (open upstream), which runs wave64 compute programs in 32-lane halves on hosts of 32-lane waves. Both of its commits are cherry-picked with their authorship kept. A third commit, mine, adjusts its tests for D3DMetal.

Why it matters on a Mac

D3DMetal reports WaveLaneCountMin and WaveLaneCountMax of 32 (M5 Pro, D3DMetal 4.0 beta 3, as #148 also found). Unlike NVIDIA, it doesn't refuse a WaveSize(64) pipeline. It makes the pipeline, and the dispatch writes nothing. On main, every console wave64 program that uses the console's lanes declares WaveSize(64), so on a Mac all of them silently do nothing. #148 lists these as not fixed there: "programs that do use wave ops still declare WaveSize(64) (166 translations in my cache)".

Marvisak/VIctory#2633 checks the host's lane counts (D3D12_OPTIONS1), so D3DMetal gets the halves without anything Mac-specific. Each host wave holds 32 lanes of one console wave. DPP rows, ds_swizzle, ds_bpermute, v_mbcnt, v_writelane and masks held as numbers follow the console's lanes. Reads across the two halves are refused where they are lowered, each with its reason: ballots, v_readlane of a lane that may be in the other half, and whole-wave reductions. A group of 33 to 64 threads with LDS is refused too. The upstream PR has the details and its numbers on an RTX 3070 Ti.

Ketan-Kapse ran Marvisak/VIctory#2633 on top of #148 on an M4 Pro with D3DMetal 3.0 (#172). The compute programs still declaring WaveSize(64) went from 139 to 0, and world frames went from 0.2-0.4 fps to about 1 fps (with #158 as well).

Merging with this repository

  • emit::Options and lower::Parts keep atomic_locks and bias_as_gradients next to the new host_wave32. Every test initializer has all three.
  • shader_cache: build() now keys on host_wave32, as upstream does. The per-option builds for atomic locks and gradient bias are made from build(), so they key on it too.
  • victory translate keeps its non-Windows stub, and passes --wave32 on Windows.
  • The README here has no status table or commands list, so its upstream hunks are left out.

The test change (mine)

Three halves_tests also run the console's own form, one wave64, on each adapter. On NVIDIA that pipeline is refused and the test skips it. D3DMetal makes it and runs nothing, so all three failed with zeros. A host of 32-lane waves only ever runs the halves, so on such a host that form is now skipped whatever the host does with it. Elsewhere the tests are unchanged.

Tested

  • Under Wine with D3DMetal 4.0 beta 3 (M5 Pro, macOS 27.0.1), the 7 halves_tests pass. The halves give the console's results on every lane: 64 lanes of the mixed program, 64 of the DPP row masks, and 32 and 128 of the first-lane reads.
  • Wine, tools/macos/test-wine.sh --workspace --no-fail-fast: 850 passed, 0 failed, 9 ignored. That is main's 843 plus the 7 halves tests.
  • Native cargo test --workspace: 454 passed, 0 failed, 15 ignored.
  • cargo clippy -p victory-shader -p victory-gpu -p victory --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings, with and without --all-features: no warnings. cargo xtask policy: 217 files ok. No new rustfmt diffs in the files merged by hand.

Not known

None of this ran in the game.

🤖 Generated with Claude Code

chippy01 and others added 3 commits November 25, 2026 23:07
…-lane waves

What was wrong: a console wave64 compute program that needs the
console's lanes (cross-lane reads, masks as numbers, LDS one wave shares)
declares WaveSize(64). NVIDIA and Intel run waves of 32 lanes at most
(WaveLaneCountMax 32) and refuse it: on an RTX 3070 Ti (driver
32.0.16.2124) the debug layer gives "Shader wants wave lane count of 64
but maximum wave lane count for device is 32" (id 622), and every such
pipeline is refused with E_INVALIDARG. A Series S run to the loading
screen refused 100 compute pipelines, 95 of them for this.

What changed: on such a host, a wave64 program that needs the console's
lanes is translated for waves of 32 (`emit::Options::host_wave32`;
`emit::lower::halves`). The thread relabelling gives each host wave a
32-aligned run of thread ids, so host wave 2w + h is console wave w's
lanes 32h..32h + 31. What a lane reads within its half is exact: DPP
rows, ds_swizzle, and ds_bpermute_b32, which the console's wave64
permutes within each half on its own (RDNA2 ISA; measured in #573). A
lane's bit of a mask held as a number, v_writelane_b32 and v_mbcnt take
the console lane. Reads across the halves have no host form and are
refused where they are lowered, with a reason that names the host:
ballots (masks read as data), v_readlane_b32 of a lane that may be in the
other half, whole-wave reductions and prefix sums; one a program never
uses costs nothing (the lowering's retry with the value missing). A group
of 33 to 64 threads with LDS, which the console compiler orders without
barriers as one wave, is refused too. v_readfirstlane_b32 reads the host
wave's first lane, as it already does in every program without console
lanes (marked as that shortcut).

victory-gpu asks the device once (OPTIONS1) and translates for it; the
shader cache keys on it, so entries made for 64-lane hosts are not read.
`victory translate --wave32` translates for such a host offline; with
--pipelines it follows the GPU that makes them.

Approach after DexNom's fork (DexNom/Victory 5f4c3df: the host's wave
width and the relabelling into halves) and #2176 (n-romanovich: the
setting in the cache key); here the setting is a translation option, and
cross-half reads are refused at the sites that make them instead of by
instruction name, which keeps programs whose crossing value is unused.

Evidence, `translate --list --pipelines` over shaders.rpf, the
executable, XboxBvhBuilder.dll and the FidelityFX DLL, RTX 3070 Ti:

| | main 67e8ae1 | this |
|---|---|---|
| compute programs emitted | 2,733 | 2,372 |
| compute pipelines made | 2,328 | 2,372 |
| refused by the driver | 405 | 0 |

No program that made a pipeline on main fails to now (25,347 pipelines,
all kept). In the title (Series S mode, diagnostics build, to the
loading screen, 2.5 min): no pipeline refused, against 100 on main; the
113 programs that do not translate say why (51 LDS, the rest mostly
ballots). The 44 new ones include the depth pyramids (occlusion),
the sky and atmosphere gathers (scattering_transmittance_system),
shader binning and the mipmap generators. The 361 still refused now say
why: 192 LDS in a one-wave group, 107 ballots, 48 lane reads across
halves, 20 reductions (those need an exchange between the two host waves,
a change of its own).

Tests (halves_tests): one wave64 program (DPP, ds_swizzle, ds_bpermute,
v_mbcnt, v_writelane to lane 40, a constant mask's high dword) runs on
every hardware adapter, in halves and, where the adapter has 64-lane
waves, as one wave64: all 64 lanes give the console's results on the RTX
3070 Ti (halves) and the Radeon iGPU of a Ryzen 9 7900X (RDNA2: halves
and wave64). The refusals and the LDS rule each have an offline test;
the hand-encoded words are checked against the disassembler.

Found on the way, not changed here: a wave64 mask built by two
s_mov_b32 (low and high dword written apart) reads its low dword for all
64 lanes (`Lower::pair_high` looks for the high dword in the instruction
that wrote the low one); the same with and without this change, on AMD's
own wave64 too.

Co-Authored-By: Claude Opus 5.5
(cherry picked from commit 360f8aa910d96f18310fbeda3b90f2613f19e1c9)
…sole wave

What was wrong: two results in halves were wrong in the upper host wave
(console lanes 32-63). Measured before this change on an RTX 3070 Ti
(driver 32.0.16.2124) and the Radeon iGPU of a Ryzen 9 7900X (RDNA2),
32 of 64 lanes wrong on both, while the Radeon's own wave64 form gave the
console's results:

- v_readfirstlane_b32 read the host wave's first lane, which in the upper
  half is console lane 32 or later: a first-lane read of the thread id gave
  32 in lanes 32-63, the console 0 (reported on #2633 by justrealyme).
- A DPP row mask was applied by the host lane's row, 0 or 1: with
  row_mask:0xc, lanes 32-63 (the console's rows 2 and 3) kept their old
  value instead of taking the DPP result.

What changed:

- A first-lane read in halves is kept for a value proven the same in every
  lane of the console wave (halves::lane_uniform: values made from scalars,
  constants and such values, under conditions that are themselves alike;
  LDS reads, and buffer loads through a scalar descriptor, at alike
  addresses; the thread id shifted past the console wave) and in groups of
  32 threads or fewer, which one host wave holds whole. Any other is
  refused with its reason; one nothing uses costs nothing. The analysis is
  justrealyme's (chippy01/VIctory#1), ported from fxceless/victory-fork
  (#497 there). Added here: buffer loads (a typed buffer's element the
  compiler loads with a vector load, then reads the first lane of), the
  dwords a waterfall loop reads (each lane's own, so not alike), and the
  small-group exception.
- DPP row masks take the row from the console lane.
- The GPU tests share one dispatch helper.

Evidence, translate --list --wave32 over shaders.rpf, the executable,
XboxBvhBuilder.dll and the FidelityFX DLL: compute programs emitted
2,372 -> 2,369. The 3 no longer emitted read the first lane of a value the
analysis cannot show is alike (scattering_transmittance_system's cloud
multiple-scattering cache: a value joined from a branch on a per-lane
compare; two ptx_prerender light-atlas programs: a select on a mask made
from EXEC, in a loop), so in halves they could compute per-half values. 14
programs refused before give this reason first now; no other outcome
changes. The analysis as chippy01/VIctory#1 has it refuses 12: the
small-group exception keeps 5 of them, buffer loads 4. No program of the title uses a DPP row mask
other than 0xF in halves, so that fix changes none of its translations.

Tests (emit::halves_tests):

- dpp_row_masks_name_the_console_rows_on_every_gpu (new): row_mask:0xc;
  0 of 64 lanes differ on both GPUs (32 before).
- first_lanes_kept_in_halves_match_the_console_on_every_gpu (new): the
  thread id's first lane in a group of 32 and l >> 6 in a group of 128;
  0 lanes differ on both GPUs.
- reads_across_32_lane_halves_are_refused_with_their_reason: the thread
  id's first lane is refused in a group of 64 and kept in a group of 32;
  an unused one costs nothing; a buffer dword loaded at an offset alike in
  every lane is kept, at the thread id's offset refused.
- the_test_programs_are_what_their_comments_say: every hand-encoded
  program checked against the disassembler.

Checks: cargo clippy --workspace --all-targets --locked -- -D warnings,
and with --all-features: clean. cargo xtask policy: ok. cargo test -p
victory-shader --lib -- --test-threads=1: 273 passed, 3 failed, the same 3
as on main on this NVIDIA machine (two stride_tests,
gathers_at_a_level_match_the_hardware);
heap_direct_texture_reads_mark_their_heap_index skipped as before. lower.rs
is 495,122 bytes.

Co-Authored-By: justrealyme <82029927+justrealyme@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit b15a57b5cfdb87f3e570229f1f78cc914aecbee6)
D3DMetal reports waves of 32 lanes (WaveLaneCountMin and Max 32), so
VIctory translates for halves there. But unlike NVIDIA, it makes the
pipeline of a WaveSize(64) program and then runs nothing, so the
console's own form wrote zeros in all three tests that run it. A host of
32-lane waves only ever runs the halves, so that form is now skipped
there whatever the host does with it. Under Wine on an M5 Pro, the
halves give the console's results on every lane (64, 32 and 128 threads).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…halves port

pragyaangaur#148 and the port of Marvisak/VIctory#2633 both decide a compute
program's WaveSize. Combined: pragyaangaur#148's barrier check runs first. A program
then asks for the console's 64 lanes only for wave operations, or for
one-wave LDS that no barrier could sync. On a host of 32-lane waves those
are two halves, which refuse LDS only where pragyaangaur#148 placed no barriers. So
a one-wave group with both wave operations and LDS that pragyaangaur#148 syncs now
runs in halves. Before, pragyaangaur#148 alone left it at WaveSize(64), which
D3DMetal does not run, and the port refused it.

New halves tests: a one-wave group of 64 whose lanes read the LDS dword
of the lane 32 away gets one barrier and WaveSize(32) with narrow waves,
and is refused without them. Under Wine on D3DMetal 4.0 beta 3 (M5 Pro)
every lane reads what the other half wrote. With the barrier taken out,
32 of 64 lanes read stale values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earpjonathan

Copy link
Copy Markdown
Contributor Author

#148 and this PR conflict in lower.rs, emit::Options and the test initializers. A merge of #148 into this branch is on the fork as shader/wave64-halves-with-148 (6056f93), for whichever of the two lands second. Ketan-Kapse ran them in the same order in #172.

The one real choice is the wave size rule. #148's barrier check runs first. A program then asks for the console's 64 lanes only for wave operations, or for one-wave LDS that no barrier could sync. On a host of 32-lane waves those 64 lanes are two halves, and the halves refuse LDS only where #148 placed no barriers. So a one-wave group with both wave operations and LDS that #148 can sync now runs in halves. Before, #148 left it at WaveSize(64), which D3DMetal doesn't run, and this PR refused it.

New tests on that branch:

  • lds_of_a_one_wave64_group_synced_by_barriers_runs_in_halves: in halves with fix(shader): run one-wave wave64 LDS groups on 32-lane hosts instead of skipping them #148's option, a one-wave group of 64 that reads the LDS dword of the lane 32 away gets one barrier and WaveSize(32). Without the option it is refused, as before.
  • lds_synced_in_halves_matches_the_console_on_every_gpu: that program under Wine on D3DMetal 4.0 beta 3 (M5 Pro). All 64 lanes read what the lane in the other half wrote. With the barrier taken out, 32 of the 64 read stale values, so the test does catch a missing barrier.

On that branch: Wine 857 passed, 0 failed, 9 ignored; native 458 passed, 0 failed, 15 ignored; clippy, with and without --all-features, no warnings.

AeroUp pushed a commit to AeroUp/VIctory-macOS that referenced this pull request Nov 26, 2026
… and patch Series S FSR4 to native upscaler
@pragyaangaur

Copy link
Copy Markdown
Owner

Thanks. Several PRs landed on main today and this one now conflicts. Could you rebase on main and push? It is approved to merge as soon as CI is green on the rebased branch.

@pragyaangaur
pragyaangaur merged commit 1974611 into pragyaangaur:main Nov 26, 2026
1 check passed
@pragyaangaur

Copy link
Copy Markdown
Owner

I fetched your resolution branch (6056f93) as the base, merged current main into it (this also resolved the conflicts with #161's mesh split in lower.rs and shader_cache.rs), checked clippy on the Windows target and the shader tests, and CI is green. This is merged as a squash. Thank you for the port.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:gpu Host GPU runtime: resources, copies, presenting area:shader Shader translator d3dmetal A difference in Apple's D3DMetal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants