Skip to content

GPU, Shaders: honor thread dimensions in direct DispatchX (#2750) - #2841

Open
brainbudt wants to merge 1 commit into
Marvisak:mainfrom
brainbudt:fix/dispatchx-thread-dimensions
Open

brainbudt wants to merge 1 commit into
Marvisak:mainfrom
brainbudt:fix/dispatchx-thread-dimensions

Conversation

@brainbudt

Copy link
Copy Markdown

Upstream DispatchX forwards its arguments as D3D12 group counts for every flag value. A synthetic 64×1×1 kernel asked for 65 threads with flag 0x20 writes past slot 64. Issue #2750 reports the same flag on BVH builder requests.

What changed

  • Interpret bit 5 (0x20, AMD USE_THREAD_DIMENSIONS) as thread dimensions and compute host group counts with ceiling division per axis.
  • For incomplete groups, lazily create a separate compute pipeline and pass the requested dimensions via root constants; an entry guard masks out excess lanes before guest code (including its first EXEC ballot).
  • Ordinary Dispatch, flag-clear DispatchX, and complete groups continue using the existing pipeline with byte-identical DXIL.
  • Refuses partial groups containing a workgroup barrier and requests exceeding D3D12's 65,535 per-axis group limit.
  • See AMD's register reference, p. 196, GPU/BVH: DispatchX ignores flag 0x20 — possible thread-count vs. group-count mismatch in builder dispatches #2750, and fxceless/victory-fork@36a2257.

Verification

  • Windows WARP: tested actual command-list entry points with one guest pipeline and separate 4,160-word output buffers. DispatchX(65, 1, 1, 0x20) wrote exactly slots 0..64 (upstream failed at slot 65); following ordinary 2-group Dispatch and flag-clear 2-group DispatchX each wrote 128; flag-clear 65 groups wrote 4,160.
  • Microsoft DXIL Validator (1.9.2609.5): accepted both ordinary (byte-identical to upstream) and bounded shaders.
  • Linux x86-64 & Windows MSVC: release shader integration tests, isolated group-dimension test, cargo check --workspace --target x86_64-pc-windows-msvc --tests, and cargo clippy --workspace --target x86_64-pc-windows-msvc --tests -- -D warnings passed.

Reviewed with mistral-loop: 2 rounds with Mistral Large 4 and DeepSeek V4.1 Flash (Mistral Hosted) on high. Verdict: approved.

Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).

@brainbudt
brainbudt force-pushed the fix/dispatchx-thread-dimensions branch 2 times, most recently from bc40f7a to bc677ec Compare November 25, 2026 21:56

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct: thread-groups conversion uses the bound pipeline's own group size with div_ceil per axis, rejects zero/overflow (>65535 groups or u32::MAX threads) into a dropped dispatch rather than an invalid call, and binds the ORIGINAL thread dims into the shader's user data — which is what DispatchX's console callers pass and read. Partial-group handling via the bounded plan is covered by the hardware test that asserts only requested threads write. The const-generic THREADS split keeps the group-dispatch path unchanged. LGTM.

@Venom2054

Copy link
Copy Markdown

Tested 274cdadb on a physical RTX 5070 Ti, driver 621.01, through the Windows D3D12 command-list entry points.

  • Wave32, 0x8020. I replayed the X dimension of 82,606 seen in the GPU/BVH: DispatchX ignores flag 0x20 — possible thread-count vs. group-count mismatch in builder dispatches #2750 prologue trace with a synthetic guest shader that stores its entry popcount(EXEC_LO). D3D12 counted 82,624 invocations (2,582 groups). Exactly 82,606 slots were written. The last 14 active lanes each stored 14, and the remaining 18 slots in that group kept their sentinel. An ordinary two-group dispatch after a partial dispatch on the same list and UAV wrote all 64 slots with a population of 32.
  • Three-dimensional bounds. Using 8 × 8 × 4 groups and a guest shader with no bounds check, requests of 16 × 13 × 8, 16 × 16 × 5, 10 × 16 × 5 and 10 × 13 × 5 wrote exactly 1,664, 1,280, 800 and 650 cells respectively. I checked the coordinate value in every cell of the 16 × 16 × 8 volume and all 17 guard words for each case. The exactly divisible 0x20 control and ordinary dispatches before and after wrote all 2,048 cells.

The PR's four original readbacks also reproduced on hardware (65 / 128 / 128 / 4,160). As a negative control on bc677ec1, sending ordinary groups for the partial-Y case made the test fail at exactly (0, 13, 0), the first cell outside the requested volume.

The attached patch adds the synthetic 3D regression test. To repeat it on Windows, apply it to the PR branch and run cargo test --profile diag -p victory-gpu --lib dispatch_x_3d_writes_only_requested_voxels -- --ignored --nocapture. I haven't measured in-game lighting or frame time with this PR. Test and patch prepared with Codex.

native-dispatchx-3d-tests.patch.txt

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent Windows WARP verification of 274cdadb against current main (f3578e38). I confirmed Microsoft Basic Render Driver (vendor 0x1414, device 0x008c, software flag 0x2) with VICTORY_D3D12_WARP=1 and diagnostics enabled.

  • The synthetic command-list test passed on the PR head. The 65-thread DispatchX with bit 5 set wrote exactly 65 slots. Ordinary two-group Dispatch and the two bit-5-clear DispatchX controls wrote 128, 128, and 4,160 slots, respectively.
  • The same readback fixture on current main failed at zero-based slot 65, which held 1 instead of 0.
  • The shader integration tests passed. The SDK DXIL validator (1.8.2502.11) accepted both ordinary and bounded DXIL, and the partial-dispatch group-barrier rejection test passed.

AI-assisted with Codex (gpt-6.2-sol). Claude Code (claude-opus-5-5) prepared the initial WARP guard and main comparison harness. Synthetic data only.

@Machad3x

Copy link
Copy Markdown

Hardware result at head 274cdad, cherry-picked onto a stack that also carries the console flow for wave64 programs (#439 / #2214), RTX 3080 Ti, driver 621.24.

With the two together, translate_partial_dispatch panics at world load (bitcode.rs, "forward reference outside a phi") for a program that numbers its threads as the console does: dispatch_guard calls thread_id, which returns the console_flat value cached by the body, in a block that does not dominate the guard. Resetting that cache beside self.user_data = None in dispatch_guard fixes it; a synthetic wave64 program with v_readlane_b32 translated as a partial dispatch for a 32-lane target reproduces it. main has no such cache today, so this matters when the two meet.

With that fix (and #1273): one 7-minute run in play, no device removal; in the scene at a window, GPU step median 299 ms against 307 ms before (one run each).

Ran with AI assistance (Claude Code).

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Verified 274cdadb on Windows 11 (RTX 5090 + WARP + SDK 10.0.26100.0 dxil.dll) against main (f3578e3):

  • cargo test --locked -p victory-shader --test dispatch_threads: 2/2 passed on RTX 5090 and WARP (including dxil.dll validation of the guarded entry -> body / abort split).
  • victory-gpu: thread_dimensions_cover_partial_groups_on_each_axis passed (1/1); --ignored dispatch_x_writes_only_requested_threads passed on RTX 5090 (0.46s) and WARP (0.31s).
  • cargo clippy --locked (victory-shader default & --features host,diagnostics; victory-gpu default & --features diagnostics, --all-targets -- -D warnings): 0 warnings; cargo xtask policy: 193 files ok.
  • Lazy Shader::partial compilation leaves normal Dispatch, non-0x20 DispatchX, and exact-multiple dispatches untouched, and State::bind_dispatch switches root/PSO cleanly between full and partial variants.
  • Heads-up for maintainers: crates/victory-shader/src/emit/lower.rs reaches 523,090 bytes here — 1,198 bytes shy of the 512 KiB (524,288-byte) xtask policy limit (#2836 frees 821 bytes if landed alongside).

Looks solid and ready to merge.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Verdict: merge candidate. dispatch_x in crates/victory-gpu/src/list.rs now reads bit 5 of the flags (USE_THREAD_DIMENSIONS, from AMD's public register reference) as thread counts instead of group counts. thread_groups uses ceiling division and refuses more than 65,535 groups per axis. Incomplete groups use a lazily made pipeline whose entry guard (Lower::dispatch_guard in emit/lower.rs) compares global thread IDs with the requested counts, which are passed through DISPATCH_THREADS user data. Ordinary dispatches keep the existing DXIL, and partial dispatches with s_barrier are refused. Hardware runs on an RTX 5070 Ti and RTX 5090, plus a WARP run, confirm exact writes, with main failing the same fixture at slot 65. There is no other open PR for this.

Two notes for the maintainer:

CI: passing. Recommending a look, not approving.


Generated by Claude Code

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested merged onto origin/main (63bcca4) on Windows 11 x64:

  • cargo test -p victory-shader --test dispatch_threads: 2 passed, 0 failed.
  • cargo test -p victory-gpu --lib / cargo clippy -p victory-gpu --all-targets -- -D warnings: fails to compile against current origin/main (63bcca4) because depth_bounds_test.rs (landed on main) initializes pipeline::Translated without the new threads field:
    error[E0063]: missing field `threads` in initializer of `pipeline::Translated`
      --> crates\victory-gpu\src\pipeline\depth_bounds_test.rs:58:43
    
  • cargo xtask policy: 194 files ok.

Rebasing onto current main and adding threads: None in crates/victory-gpu/src/pipeline/depth_bounds_test.rs:58 should make the test target build cleanly.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

@brainbudt

Copy link
Copy Markdown
Author

Note

🤖 Gemini 4 Neon responding on behalf of brainbudt

Thanks @Dukestar69 — rebased onto origin/main (63bcca4) and added threads: None in crates/victory-gpu/src/pipeline/depth_bounds_test.rs (head c2ed5d1).

@brainbudt
brainbudt force-pushed the fix/dispatchx-thread-dimensions branch from 274cdad to c2ed5d1 Compare November 26, 2026 10:22

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested c2ed5d14 locally on Windows 11 (RTX 5090 + WARP, VICTORY_D3D12_DEBUG=1, and Windows SDK 10.0.26100.0\x64\dxil.dll).

Verification:

  • cargo xtask policy and cargo clippy -p victory-shader -p victory-gpu --tests -- -D warnings (default and --all-features): clean.
  • cargo test -p victory-shader --test dispatch_threads with SDK dxil.dll: 2/2 passed (both full and partial-dispatch DXIL containers validate cleanly with IDxcValidator, including both UserData CBV handle annotations fixed up by replace_const).
  • list::dispatch_tests::thread_dimensions_cover_partial_groups_on_each_axis: passed.
  • list::dispatch_tests::dispatch_x_writes_only_requested_threads -- --ignored: passed on RTX 5090, RTX 5090 + VICTORY_D3D12_DEBUG=1 (--features diagnostics), and WARP + VICTORY_D3D12_DEBUG=1 (--features diagnostics).
  • placed_uav_tests, pipeline::make::tests (2/2), and draw::tests (20/20 on RTX 5090): all passed.

The entry-guard structure is sound: ir::build_bounded already rejects branches to block 0 so moving guest inputs/body to block 1 has no entry-PHI hazard, library::name keys PSOs by serialized root signature and DXIL bytes so full and partial pipelines do not collide, State::bind_dispatch rebinds by raw COM pointer when toggling between full and partial variants on one guest pipeline, exact multiples stay on the unguarded pipeline (preserving shaders with s_barrier), and divergent barriers in partial dispatches are cleanly refused. Looks ready to merge.

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…atch merge

Reconcile four cross-PR interactions across the 32 merged PRs:
- Marvisak#2953/Marvisak#2975 vs Marvisak#2811: read `self.mode16.round` in `div_fixup.rs` and `div_scale.rs`
- Marvisak#2841 vs Marvisak#2177: pass `host_heap_views` in `pipeline.rs` partial-dispatch translation and `dispatch_threads.rs`
- Marvisak#2841 vs Marvisak#2999: use guest group dimensions (`self.rows.or(self.s.threads)`) in `dispatch_guard`
- Marvisak#2177 vs merged test fixtures: add `host_heap_views: 0` to `Parts` test initializers
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…atch merge

Reconcile four cross-PR interactions across the 32 merged PRs:
- Marvisak#2953/Marvisak#2975 vs Marvisak#2811: read `self.mode16.round` in `div_fixup.rs` and `div_scale.rs`
- Marvisak#2841 vs Marvisak#2177: pass `host_heap_views` in `pipeline.rs` partial-dispatch translation and `dispatch_threads.rs`
- Marvisak#2841 vs Marvisak#2999: use guest group dimensions (`self.rows.or(self.s.threads)`) in `dispatch_guard`
- Marvisak#2177 vs merged test fixtures: add `host_heap_views: 0` to `Parts` test initializers
@Jackster

Jackster commented Nov 26, 2026 •

Copy link
Copy Markdown

AMD verification of c2ed5d1 on a Radeon RX 7900 XTX (gfx1100, 32.0.32029.2010), Windows 11, hardware adapter:

  • dispatch_x_writes_only_requested_threads passes on the hardware adapter: 65 threads with flag 0x20 write exactly slots 0..64, and the ordinary and flag-clear controls keep their old counts.
  • --test dispatch_threads: 2/2.
  • cargo test -p victory-gpu --lib: 232 passed / 0 failed / 2 ignored.
  • Control on 63bcca4, the same fixture with thread_groups removed: fails at slot 65 (slot 65, expected 65 writes), so the upstream forwarding reproduces on this card.

Synthetic only, no game files. AI-assisted.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants