Skip to content

GPU and shaders: handle DispatchX USE_THREAD_DIMENSIONS partial thread groups (#16) - #646

Open
mynzjerko wants to merge 1 commit into
fxceless:mainfrom
mynzjerko:fix/dispatch-x-thread-dimensions
Open

mynzjerko wants to merge 1 commit into
fxceless:mainfrom
mynzjerko:fix/dispatch-x-thread-dimensions

Conversation

@mynzjerko

Copy link
Copy Markdown

Problem

In crates/victory-gpu/src/list.rs, DispatchX(x, y, z, flags) ignored flags and forwarded (x, y, z) directly to ID3D12GraphicsCommandList::Dispatch(x, y, z) as thread-group counts.

The ray-tracing structure builder calls DispatchX with bit 5 (0x20 = COMPUTE_DISPATCH_INITIATOR::USE_THREAD_DIMENSIONS, e.g. 0x0020 / 0x8020) set. In that mode:

  1. (x, y, z) are raw thread counts, not thread-group counts.
  2. Trailing lanes of partial tail groups (DTid >= count) are masked off in initial hardware EXEC.

Treating raw thread counts as full 64-thread groups caused:

Fix

victory-gpu

  • crates/victory-gpu/src/pipeline.rs: store compute [numthreads(x, y, z)] on Translated.
  • crates/victory-gpu/src/list.rs: forward flags from dispatch_x to state::dispatch_x.
  • crates/victory-gpu/src/draw.rs: when flags & 0x20 != 0 (USE_THREAD_DIMENSIONS), skip zero-sized dispatches, convert raw thread counts to host group counts via div_ceil, and supply any non-multiple axis limits through host user-data dword USER_DISPATCH_THREADS (0x104, 0 = no cap for normal Dispatch or exact multiples).

victory-shader

  • crates/victory-shader/src/emit/shader.rs & mod.rs: define and export USER_DISPATCH_THREADS = 0x104.
  • crates/victory-shader/src/emit/lower.rs: add dispatch_threads_prologue for spilled compute programs (all ray-tracing structure builder stages) along any axis with group dimension > 1:
    • Early-exit mode (barrier-free, exchange.is_none(), entry block has no back-edge — native Wave64 hosts and Wave32 programs): branch inactive lanes out to Ret after entry inputs (groupId, relabelled thread_id), and place entry-EXEC ballots/SCC tests in body_entry so hardware EXEC, s_bcnt1_i32_b64(exec), wave reductions, and stores see the exact active lane set with zero per-instruction guard overhead.
    • Predicated mode (exchange.is_some() for wave64-on-wave32 hosts, or programs containing s_barrier): keep all threads alive so GroupMemoryBarrierWithGroupSync / exchange_halves never deadlock (even when limit <= 32 leaves the upper 32-lane half with 0 active lanes), while predicating memory/LDS accesses (effect_guard), active ballots (mask_dword), scc_some, wave_wide, and whole-wave reduction masks (section_val).

Verification

  • Added unit test draw::tests::dispatch_x_thread_dimensions_convert_to_groups_and_partial_limits in crates/victory-gpu/src/draw.rs.
  • Added hardware GPU test emit::wave64_tests::spilled_compute_masks_partial_dispatch_threads_on_wave64_and_wave32 in crates/victory-shader/src/emit/wave64_tests.rs testing s_bcnt1_i32_b64(exec), whole-wave OR, and unconditional per-lane buffer_store_dwordx2 at limit = 45, 1, 0:
    • Verified on AMD Radeon RX 9070 for both host_wave=64 (early-exit) and host_wave=32 (predicated exchange_halves, no barrier hang at limit = 1), both validated by dxil.dll.
  • cargo test -p victory-shader --lib (215 passed, 0 failed), cargo test -p victory-gpu (119 passed, 0 failed), cargo xtask policy (asset policy: 169 files ok).
  • Live verification with VICTORY_BVH_BUILDER=1 on AMD Radeon RX 9070: eliminates the BVH over-dispatch memory corruption crash (previously crashing within ~7–14 s on empty box-node bounds); sustained 2+ minutes / 8,900+ submissions of builder execution with zero CPU memory corruption.

Refs #16.

AI-assisted (Antigravity); verified locally on an AMD Radeon RX 9070. No game data.

@Revens2

Revens2 commented Nov 25, 2026

Copy link
Copy Markdown

RTX 5070 (32-lane): in-game check of this PR with #36 and #579 on a26859f: 2 of 2 runs held 600 s with VICTORY_BVH_BUILDER=1, no exception.

Runs: diag build, VICTORY_RELAY=2, builder started ~107 s in, so ~8 min with it running; world reached, 0 first-chance exceptions, 0 device removals, 0.3-0.4 fps. On the same tree with the earlier VICTORY_THREAD_DIMS variant (Yorivel's 2521646, which launches ceil(x/threads) groups and lets late lanes leave at entry), both runs ended 4-6 s after builder runs (one at +0x8f92a, read of all ones). Details on #16. Two runs per arm, so a lead.

Tests on that tree (cargo test --workspace --locked): 536 passed, 10 ignored, 1 failed: xtask policy::tests::repository_passes_policy, because #36 + this PR together make lower.rs 528,884 bytes against the 524,288 limit (each is under it alone on its own base; main is 514,840). Merging both needs room in that limit or a split of the file.

AI-assisted (Claude Code, Claude Sonnet 5.5): the runs are mine, on my own copy of the title. Counts only, no game data.

VibeClauding added a commit to VibeClauding/victory-fork that referenced this pull request Nov 25, 2026
… 0x105

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016TFkzoCQk3ZUibQWPNRBXE
VibeClauding added a commit to VibeClauding/victory-fork that referenced this pull request Nov 25, 2026
…her codes 0x104/0x105/0x106

- fxceless#646 (USER_DISPATCH_THREADS), fxceless#834 (USER_GROUP_BASE) and our
  USER_VIEW_SLOT each took 0x104; here they are 0x104, 0x106, 0x105.
- record_clamp_with takes the scratch buffer's state as record_clamp_as
  does since fxceless#220, and bound_split tracks it like clamp_into.
- The split read asked.x, which since fxceless#220 is the mesh records' count
  (a scalar, 0 for dispatches): every full dispatch got 65,535 more
  groups and twice the charge. It reads the record's x now, as the
  second record's store does. warp_split_runs_every_group_of_a_wide_
  dispatch_once passes again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016TFkzoCQk3ZUibQWPNRBXE
VibeClauding added a commit to VibeClauding/victory-fork that referenced this pull request Nov 25, 2026
…celess#834, after fxceless#646's 0x104), USER_VIEW_SLOT 0x106

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016TFkzoCQk3ZUibQWPNRBXE
@pr0vId3nce

Copy link
Copy Markdown

RTX 4080 (32-lane): held 546 s with the builder on, no exceptions, and the shade is lit

Setup: RTX 4080 16 GB (driver 32.0.16.1088), Ryzen 9 7900X, 32 GB, Windows 11 26200, diag build, Series X profile. Tree: main 0b0074e0b + #156 (so the builder's three prefix-sum passes translate on 32 lanes) + this PR's 170c24269; one textual conflict between the two in emit/mod.rs's export list, nothing else. VICTORY_BVH_BUILDER=1, a checkpoint in the first gameplay scene, 540 s limit.

  • the ray-tracing structure builder runs: all of its programs have host pipelines pipelines=53, then 546 s to the time limit with 0 first-chance and 0 unhandled exceptions. Without the thread-count handling, four of four earlier runs here (c96b4f2 and 0b0074e with Ray tracing: shadow-ray traces and the full structure builder translate on 32-lane GPUs #156) ended in 0xc0000005 shortly after that line (8 to 10 s in the two I timed).
  • The picture: by the last captured frame (56 frames into the world) the character's back and the room's walls are lit where main leaves them black, with a few blocky tiles along the lower edge. Judged by eye against main on the same checkpoint; I have no console reference.
  • Cost: 5.2 to 6.7 s a frame, 37.6 GB peak commit.

Two card tests beside the PR's own, both on the RTX 4080 and on a Ryzen 7900X iGPU (gfx1036), host waves of 32 (neither device took the 64-lane form of these programs, so that mode did not run here):

  • the PR's spilled_compute_masks_partial_dispatch_threads_on_wave64_and_wave32: passes;
  • one I wrote for what the builder's pass does and that test does not: two groups of 64 with the limit falling in the second, and the console compiler's wave total and prefix sum, which turn every lane on (s_or_saveexec_b64 -1) and mask by the saved EXEC. Limits 70, 100, 33, 1 and none: all 256 dwords right. I can send it as a patch if it is wanted.

Independent of the thread (found before I read its later half), in case it helps a reviewer:

  • Only the programs at DLL 0x25c40 and 0x28770 (28918b7150a7aad5, caa78b8ca62bc3d6) come through DispatchX with 0x20: 2,244 and 4,002 of the 12,589 builder dispatches before the crash.
  • The sizes say x is threads: three levels of one structure are 205,568, 69,632 and 27,264 bytes long (differences of root constant 0) and their x are 1,606, 544 and 213; 28918b71 gives each thread 128 bytes (group << 13 for 64 threads). As groups, 1,606 covers 13 MB.
  • Dropping stages one at a time gave Revens2's result: without those two, 428 s with no fault; with either, a crash.

What comes next on this card: no number left for another copy … COPIES=1023 comes two seconds into the world and shaders touched pages that had no copy 50 times in the run, so part of the structures reads as zeros. In a trace of another run the builder's root views alone name 529 distinct 4 MiB windows in its first frame and 838 over 58 frames. The frame time is the per-list refresh and compare of those copies, as #284 describes.

One thing for whoever merges: main + #156 + this PR puts lower.rs at 530,706 bytes, over the 512 KiB limit of cargo xtask policy (Revens2 saw the same with #36).

AI-assisted (Claude Code); measured on my machine. No game data.

…d groups (fxceless#16)

Console DispatchX(x, y, z, flags) sets COMPUTE_DISPATCH_INITIATOR bit 5 (0x20, USE_THREAD_DIMENSIONS) when passing raw thread counts instead of thread-group counts, with hardware EXEC masking trailing lanes in partial tail groups. Previously list.rs ignored flags and passed raw thread counts as full groups, causing up to 64x over-dispatch and wrong entry EXEC popcount/reductions in the ray-tracing structure builder.

- victory-gpu: forward DispatchX flags, record compute [numthreads] on Translated, convert thread dimensions with div_ceil, and pass partial axis limits via USER_DISPATCH_THREADS (0x104).
- victory-shader: add entry partial-thread masking for spilled compute shaders (early-exit before body_entry when barrier- and exchange-free, or predicated active masking for group barriers and wave64-over-wave32 LDS exchange so both 32-lane halves stay alive for group_sync).
- Tests: unit test for thread-to-group conversion in draw.rs and hardware GPU test verifying entry s_bcnt1_i32_b64(exec), whole-wave OR, and lane bounds for both host_max_wave=64 and host_max_wave=32.
@mynzjerko

Copy link
Copy Markdown
Author

Thanks @Revens2 and @pr0vId3nce for the thorough 32-lane hardware and in-game verifications (RTX 5070 600 s and RTX 4080 546 s with zero exceptions and lit shade)!

I've rebased this PR onto latest \main\ (\ca8829f, now commit \36a2257) to resolve the conflict in \crates/victory-gpu/src/draw.rs's unit tests, and moved \effect_guard\ and \dispatch_threads_prologue\ into a small child module \crates/victory-shader/src/emit/dispatch.rs\ (same #[path = ...]\ pattern as \intersect.rs). That brings \lower.rs\ down to 517,044 bytes (+2,204 bytes over \main's 514,840, leaving >7 KiB of headroom under the 512 KiB \xtask policy\ limit) so it combines more easily with #36 / #156.

Re-verified on RX 9070:

  • \cargo xtask policy: \�sset policy: 182 files ok\
  • \cargo test -p victory-shader --locked: 217 passed, 0 failed, 2 ignored
  • \cargo test -p victory-gpu --locked: 183 passed, 0 failed, 5 ignored
  • \cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean

@pr0vId3nce I'd gladly include your multi-group wave-reduction / prefix-sum test (limits 70, 100, 33, 1, none) if you'd like to share it as a patch or gist.

@Dathinel

Copy link
Copy Markdown
Contributor

Two portable RTX 3050 Laptop (wave32) checks for review at 36a2257, using synthetic inputs:

  • Two-group buffer test and patch. Each group has its own 512-byte output region. At limits 65 through 128 and the uncapped control, the first group stays full and the second group returns the expected active count and wave OR. All 16,640 words matched. This is a reduction test; the per-lane prefix sum mentioned above is separate.
  • Texture readback, image and patch. The same tail wrote to a 64 x 2 RGBA32_FLOAT UAV. All 33,280 channel values matched. At limit 96, disabling just the partial-dispatch guard on image stores made the last 32 texels of row 2 change; the other 96 texels were identical.

Hardware runs used the PR integrated onto local 504db0e. Both standalone patches apply to the PR head. No game files or scene-quality measurements in these tests. AI-assisted.

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.

4 participants