Skip to content

feat(shader): retry refused 64-thread wave64 compute with whole-wave control flow - #435

Merged
pragyaangaur merged 5 commits into
pragyaangaur:mainfrom
Kolkka:shader/literal-halves-retry
Nov 27, 2026
Merged

pragyaangaur merged 5 commits into
pragyaangaur:mainfrom
Kolkka:shader/literal-halves-retry

Conversation

@Kolkka

@Kolkka Kolkka commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

In halves (#180), a console wave64 program runs as two host waves of 32 lanes. Some programs are still refused at a cross-half gate: a lane mask read as data, LDS one wave shares without a barrier, or a reduction, first-lane read or lane read across the halves. In GTA VI (Series S) 107 compute shaders were refused this way.

How

  • When the halves translation of a wave64 compute program of exactly 64 threads is refused at one of those gates, the program is translated again with whole-wave ("literal") control flow (structure::split_literal):
    • Every lane follows the console wave's blocks.
    • Raw EXEC guards vector instructions.
    • Per-lane branch predicates become whole-wave votes (Atom::Any, a 64-lane ballot through the existing exchange).
  • Details:
    • Mask equality (s_cmp_eq on lane masks) sets SCC only when all lanes compare equal, so its scc0/scc1 polarity is the vote's inverse.
    • v_writelane_b32 writes its lane regardless of EXEC.
    • The existing LDS hazard pass runs on the literal control flow. Barriers go outside per-lane EXEC guards.
    • Cross-half operations need proven group convergence (group_uniform in barriers.rs), and lane reads need a uniform selector. First-lane reads ballot EXEC before choosing the source half. FP16 reductions keep the original order and rounding.
  • Only programs the halves translation refuses are retried, so programs that translate today are unchanged.
  • VICTORY_LITERAL_HALVES=0 turns the retry off and gets its own shader cache key.
  • lower.rs stays under the asset policy's 512 KiB limit (521,528 bytes).

Still refused

  • Groups other than exactly 64 threads. In GTA VI that is 30 of the 39 that remain.
  • Programs that hit another lowering limit or whose convergence cannot be proved.

Tests

  • New tests in halves_tests.rs. 12 synthetic programs run on the host GPU, and every lane matches the expected console result.
  • cargo clippy --workspace --all-targets -- -D warnings, with and without --all-features, for x86_64-pc-windows-gnullvm.
  • cargo test --workspace: RESULT_NATIVE
  • WINEDLLOVERRIDES="d3d12,d3d12core,dxgi=b" tools/macos/test-wine.sh --workspace --no-fail-fast with D3DMetal 4.0 beta 3 on an M5: RESULT_WINE
  • In the game (M5 MacBook Air, 16 GB), refused compute shaders went from 107 to 39 in the same run.

Cost: only for programs refused before. They pay a 64-lane ballot per lane-dependent branch, and the barriers the hazard pass adds.

…control flow

When the narrow-wave translation refuses a wave64 compute program of
exactly 64 threads at a cross-half gate (a lane mask read as data, LDS
one wave shares without a barrier, a reduction, first-lane or lane read
across the halves), it is translated again with whole-wave control flow:
every lane follows the console wave's blocks, raw EXEC guards vector
instructions, and per-lane branch predicates become whole-wave votes
(Atom::Any). Mask equality sets SCC only when all lanes compare equal,
v_writelane_b32 writes its lane outside EXEC, and LDS barriers go outside
per-lane guards. Ported onto halves from an earlier
split-waves prototype. VICTORY_LITERAL_HALVES=0 turns the retry off (own cache key).

An offline replay of Grand Theft Auto VI's Series S shaders recovers 68 of
the 107 compute shaders refused before. Tests: 378 victory-shader and
364 victory-gpu under Wine/D3DMetal, 223 victory-shader native.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017tiHubszVmvo5uoUAc34Br
@Kolkka Kolkka changed the title Shaders: literal halves retry for 64-thread wave64 compute on 32-lane hosts feat(shader): retry refused 64-thread wave64 compute with whole-wave control flow Nov 26, 2026
@Kolkka
Kolkka force-pushed the shader/literal-halves-retry branch from de49289 to 9a4652e Compare November 26, 2026 21:14
@evan-kolberg

Copy link
Copy Markdown
Contributor

Independent M4 Pro result on D3DMetal 4.0 beta 3, using Kolkka’s updated #435 head 9a4652e on Mac bf99e728 with Lyrnc’s #3365 lowering. The generated cross-half read was refused with retry disabled and matched all 128 lanes with retry enabled. MODE set before the EXEC split survived a conversion after restore. The signed pairs were 0x0bad0000 / 0x0bad8000 in MODE 4 and 0x0bad0001 / 0x0bad8001 in MODE 12. Exact #435 head · Lyrnc’s #3365

The already accepted tagged half reduction and broadcast copy from earpjonathan and draggygaggy-maker also kept 8/8 DXIL pairs byte-identical with retry allowed or disabled, across 32/64/96/128 threads. All 640 hardware lane results matched. This covers the tested ordinary conversion after EXEC restore. earpjonathan’s #425 · draggygaggy-maker’s #3342

Prepared and tested with Codex.

@hackgods

Copy link
Copy Markdown
Contributor

In-game test on an M5 Pro (18-core CPU, 20-core GPU, 64 GB), macOS 27.2.1, CrossOver Wine with D3DMetal 4.0b3, Series X, build 1.0.2.1 from my own dump. Diagnostics build, same prologue autosave and scripted input in both runs. Text and numbers only, no game data.

main bf99e72 + #409 + #401 same + #435 + #433
compute shaders "does not translate" 110 39
draws dropped without a host pipeline, per frame 94–106 ~53
indirect calls dropped, per frame (all indirect dispatch) 68 21
indirect dispatches with work in one captured frame 62 of 895 169 of 950

What it changes on screen: without #435, the producers of the opaque-lighting indirect records were among the refused wave64 programs. So the lighting dispatches ran with zero groups, and the lit HDR target kept pixels from shots many frames old. That showed up as the stale, ghosted layers and the flat dark player silhouette that testers keep reporting, plus overexposure metered from a near-black scene. With #435 the cell scene is correctly lit, the stale layers are gone, and walking and camera input through the movement tutorial look right.

I applied #433 together with #435 and have not separated their effects. Still refused here, by dispatches per frame: b22a16a178976e1b (~25), ceb5eb68cd99cb07 (~8), 82a2ad6d07417d36 (~7), and a few at 1–2.

Prepared with Claude Code.

@earpjonathan

Copy link
Copy Markdown
Contributor

In-game check on an M5 Pro (macOS 27.0.1, CrossOver 26.3 Wine with the repo's current patches, D3DMetal 4.0 beta 3, Series X), with #435 applied on main at bf99e72. Same opening and scripted input each run.

Only #435 was needed for the picture. #433 on top made no visible difference.

Prepared with Claude Code.

@pragyaangaur

Copy link
Copy Markdown
Owner

Thanks for this. Many pull requests landed today, and this one now conflicts with main. Could you merge or rebase main into it, rerun the checks and ping here? I will review and merge it as soon as CI is green.

@hackgods

Copy link
Copy Markdown
Contributor

Heads-up for the rebase, now that #408 is on main. In our local stack (#435 + #408 + #401 on bf99e72, M5 Pro, in-game), #408's cap-escape rule refused 11–15 programs that #435's literal-halves retry otherwise translates. Among them were the tile-list passes eae3498e6553afa8 and b8f3f7979d6bbc50 and several lighting-chain programs. Untranslatable compute went 39 → 50, and the world drew black with only the HUD; with #408 left out it was lit again. The details are in the comment on #408.

Our resolution of the lower.rs/barriers.rs conflict may differ from yours, so this may not reproduce. But after rebasing it's worth checking the count of "an s_barrier where a lane may have left the program early at a loop's iteration limit" refusals. We can run the rebased head in-game here and report the counts and picture.

Prepared with Claude Code.

@pragyaangaur
pragyaangaur merged commit dc50a12 into pragyaangaur:main Nov 27, 2026
1 check passed
hackgods added a commit to hackgods/VIctory-macOS that referenced this pull request Nov 27, 2026
DispatchX(x, y, z, flags) with flag 0x20 (the console's dispatch
initiator bit "use thread dimensions") gives thread counts. The host
ignored the flags and ran x * y * z groups, so a ray-tracing structure
builder stage dispatched this way ran up to 64 times its threads and
stored over the structures after its own.

The host now runs the groups that hold the threads (div_ceil by the
program's numthreads, kept in pipeline::Translated). Where the last
group along x is a part of one, the dispatch takes a second form of the
pipeline (pipeline::thread_limited), made on the first such dispatch:
its shader translated again with emit::Options::thread_limits, under a
cache key of its own (bit 9 of translation_build, after pragyaangaur#511's halves
apart and pragyaangaur#506's cap barriers). In that form a compute program of one dimension whose lanes
never wait for each other (no s_barrier, no barriers for narrow host
waves, not in 32-lane halves that pass values, not in literal halves,
whose group must take one path) reads a user-data word,
emit::USER_DISPATCH_THREADS_X, the threads along x that run, and the
lanes past it leave before anything else, so wave counts, first-lane
reads and stores see only the lanes the console starts. The host sets
the word after binding. Every other translation is as before, so
programs no such dispatch reaches keep their DXIL; containers of
one-dimensional compute programs are kept for the second translation
(at most the ~22 MB all compute containers came to in a world run).
The lowering's part is in lower/thread_limits.rs and lower/retry.rs,
so lower.rs stays under the file limit. Each pipeline's first dispatch
counted in threads is logged ("a dispatch counted in threads"), with a
warning where a part of a group runs whole. VICTORY_THREAD_DIMENSIONS=0
takes the counts for groups again.

Ported from DexNom/VIctory bc3cd4b0 (DexNom: the flag, and the lanes
past the count leaving in two builder programs chosen by digest), as
carried in the cool_fox fork's 810efe5 (RyanLockwoood/VIctory-cool_fox-
LockWood). This tree has no builder detection, hence the form made on
demand.

Tests: halves_tests::the_lanes_past_a_count_of_threads_do_not_run
(wave32 and wave64 on the host, counts 0 to 128 over two groups of 64);
halves_tests::only_programs_whose_lanes_never_wait_leave_past_a_count_
of_threads (pragyaangaur#435's whole-wave retry included: a program it takes reads
no count); thread_dispatch_tests::a_dispatch_counted_in_threads_runs_
only_those_threads, from a console container through State::dispatch_x:
threads 70 to 127 write nothing, the pipeline's own translation reads
no count (fails with VICTORY_THREAD_DIMENSIONS=0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hackgods added a commit to hackgods/VIctory-macOS that referenced this pull request Nov 27, 2026
DispatchX(x, y, z, flags) with flag 0x20 (the console's dispatch
initiator bit "use thread dimensions") gives thread counts. The host
ignored the flags and ran x * y * z groups, so a ray-tracing structure
builder stage dispatched this way ran up to 64 times its threads and
stored over the structures after its own.

The host now runs the groups that hold the threads (div_ceil by the
program's numthreads, kept in pipeline::Translated). Where the last
group along x is a part of one, the dispatch takes a second form of the
pipeline (pipeline::thread_limited), made on the first such dispatch:
its shader translated again with emit::Options::thread_limits, under a
cache key of its own (bit 9 of translation_build, after pragyaangaur#511's halves
apart and pragyaangaur#506's cap barriers). In that form a compute program of one dimension whose lanes
never wait for each other (no s_barrier, no barriers for narrow host
waves, not in 32-lane halves that pass values, not in literal halves,
whose group must take one path) reads a user-data word,
emit::USER_DISPATCH_THREADS_X, the threads along x that run, and the
lanes past it leave before anything else, so wave counts, first-lane
reads and stores see only the lanes the console starts. The host sets
the word after binding. Every other translation is as before, so
programs no such dispatch reaches keep their DXIL; containers of
one-dimensional compute programs are kept for the second translation
(at most the ~22 MB all compute containers came to in a world run).
The lowering's part is in lower/thread_limits.rs and lower/retry.rs,
so lower.rs stays under the file limit. Each pipeline's first dispatch
counted in threads is logged ("a dispatch counted in threads"), with a
warning where a part of a group runs whole. VICTORY_THREAD_DIMENSIONS=0
takes the counts for groups again.

Ported from DexNom/VIctory bc3cd4b0 (DexNom: the flag, and the lanes
past the count leaving in two builder programs chosen by digest), as
carried in the cool_fox fork's 810efe5 (RyanLockwoood/VIctory-cool_fox-
LockWood). This tree has no builder detection, hence the form made on
demand.

Tests: halves_tests::the_lanes_past_a_count_of_threads_do_not_run
(wave32 and wave64 on the host, counts 0 to 128 over two groups of 64);
halves_tests::only_programs_whose_lanes_never_wait_leave_past_a_count_
of_threads (pragyaangaur#435's whole-wave retry included: a program it takes reads
no count); thread_dispatch_tests::a_dispatch_counted_in_threads_runs_
only_those_threads, from a console container through State::dispatch_x:
threads 70 to 127 write nothing, the pipeline's own translation reads
no count (fails with VICTORY_THREAD_DIMENSIONS=0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants