Skip to content

Shaders: guard mesh output bounds, init all primitives, and omit sync on divergent s_barrier - #587

Open
FibbS6 wants to merge 1 commit into
Marvisak:mainfrom
FibbS6:fix/mesh-output-bounds-and-divergent-barrier
Open

FibbS6 wants to merge 1 commit into
Marvisak:mainfrom
FibbS6:fix/mesh-output-bounds-and-divergent-barrier

Conversation

@FibbS6

@FibbS6 FibbS6 commented Nov 24, 2026

Copy link
Copy Markdown

Problem

On NVIDIA D3D12, translated mesh shaders and compute shaders can fault or hang when:

  1. Mesh output vertex/primitive indices exceed declared limits or primitives are left uninitialized.
  2. s_barrier is emitted inside divergent control flow, which deadlocks on 32-lane NVIDIA warps when not all lanes reach the barrier.

Fix

  • Guard mesh output writes against declared bounds and ensure all primitives are initialized.
  • Detect s_barrier instructions inside divergent control flow and omit the full threadgroup execution sync (keeping memory fence semantics).

Verification

  • cargo xtask policy — passed (151 files ok)
  • cargo clippy -p victory-shader --lib -- -D warnings — 0 warnings
  • cargo test -p victory-shader -- --test-threads=1 — 69 passed, 0 failed

@LAKSHYA-cmd-tech LAKSHYA-cmd-tech 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.

Verified and tested on Windows 11 x64 (D3D12 WARP + Hardware).

  • Confirmed mesh output initialization: passes = mesh.max_primitives.div_ceil(threads.max(1)) properly ensures all primitives up to max_primitives are initialized even when threadgroup size is smaller than primitive count.
  • Vertex index culling �ad = (x < 0) || (i >= max_vertices) properly prevents out-of-bounds indices from faulting host rasterizers.
  • Confirmed divergent control flow barrier analysis via divergent_barriers: stripping SYNC_THREAD_GROUP (bit 0) while retaining memory fences (mode = 8 | (writes_uavs ? 2 : 0)) prevents threadgroup deadlock when warps diverge across conditional exits on 32-lane hosts.
  • All 69 �ictory-shader tests pass cleanly ( est result: ok. 69 passed; 0 failed).
  • cargo clippy -p victory-shader --lib -- -D warnings clean (0 warnings).
  • cargo xtask policy clean (151 files ok).

@ghost

ghost commented Nov 24, 2026

Copy link
Copy Markdown

In-game data point for 1052a90 on AMD RDNA4 (RX 9060 XT), stacked on #160: without this PR, mesh 792314eae4f7da77 (with pixel 7bdefdfde461f6e3, which #160 makes translate) fills the G-buffer and depth targets almost completely with near-constant values; with it, those targets match the control again (about 5% covered). Numbers: #160 (comment)

AI-assisted (Claude Code, Claude Opus 5.5); no game data.

@CrooLyyCheck

Copy link
Copy Markdown

RTX 5080 (native Windows 11, driver 621.24): still DEVICE_HUNG in the world with this PR

I tested 1052a90 merged locally onto main ffe310e (clean merge, nothing else applied). Setup: diag build with VICTORY_D3D12_DRED=1, empty shader cache, Xbox Series 1.0.2.1 (build unknown (b99f876b…)), Series X mode. cargo test --workspace --locked: 342 passed, 0 failed, 6 ignored.

Build Time in the world before the hang Where DRED stops
ffe310e 117 s list 1 of 11, command 289 of 422 (op 42): mesh 37f363f0a5dcd806 / pixel 4a45195a99ac14f7
ffe310e + this PR 33 s (the first mission's title card was on screen) command 166 of 1507 (op 42): mesh 38ad582f523fd7cf / pixel d42a2d8a9d2c954a

Both runs ended in 0x887A0006 with no DRED page fault. Windows logged nvlddmkm event 153 (TDR) both times.

This is one run per build, so the shorter time may be noise. On this card the hang is not fixed, and it still moves between mesh programs, as described in #71.

@dpalacios69

Copy link
Copy Markdown
Contributor

The mesh bounds guards and multi-pass emitIndices initialization look solid (and @nullmarsh's RX 9060 XT numbers are a great signal). Two questions on the divergent_barriers part (lower.rs:6622–6708):

  1. Over-approximation on SYNC_THREAD_GROUP: divergent_barriers strips SYNC_THREAD_GROUP (bit 0) whenever any divergent branch has one path reaching the barrier and the other reaching End (can_reach[t] && can_bypass[f]). That matches both an early-exit guard (if (lane < N) return; followed by s_barrier + LDS reads) and a divergent loop exit whose back-edge reaches an s_barrier in the loop body. Leaving only the memory fence (mode = 8 | 2) removes threadgroup synchronization and can introduce silent LDS data races.
  2. Scope & test coverage: Could you share what shader pattern motivated stripping SYNC_THREAD_GROUP, and add unit tests covering both the mesh bounds/init guards and divergent_barriers? Since the mesh bounds fix already has real AMD hardware confirmation while divergent_barriers has only been exercised on WARP/existing tests so far, would it make sense to split divergent_barriers into its own PR so the mesh fix can land independently?

Static code review only; AI-assisted.

@catminou4 catminou4 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.

Test-merge with current main (ffe310e) is clean; cargo test -p victory-shader --locked → 96 passed / 0 failed / 1 ignored (macOS arm64, rustc 1.98.1). Caveat: all three changes are mesh-shader-only paths, and mesh execution tests are Windows-gated, so the green run only proves no regression on non-mesh lowering — the new logic itself is unverified by anything I can run here.

Static review (looks careful, but needs Windows-side validation):

  • divergent_barriers: backwards reachability from each s_barrier block vs. backwards reachability from LaneExit::End blocks without crossing the barrier, then flags the barrier if any non-uniform LaneExit::Branch has one side reaching it and the other bypassing. Fence-only emission (dropping SYNC_THREAD_GROUP bit 0) for those blocks is the right deadlock avoidance for 32-lane hosts. Two things to confirm on hardware/Windows: (a) a barrier flagged fence-only still orders shared-memory accesses correctly for the lanes that do reach it, and (b) the can_bypass BFS seeds from ALL End blocks — including ends inside loops that reconverge before the barrier; if such an end can reach the barrier again, the flag may be over-conservative (safe direction, just slower).
  • Mesh bounds guards: vertex/primitive exports now AND the existing guard with flat < max_vertices/max_primitives only when threads > limit — correct and free when the launch fits. emit_indices additionally culls any of the 3 vertex indices >= max_vertices (IUGE) alongside the existing negative check, zeroing all indices via bad — out-of-range indices can no longer fault the rasterizer. The per-pass emitIndices loop in setMeshOutputCounts (div_ceil(max_primitives, threads), guard only on the overflowing pass) matches DXIL's one-primitive-per-call shape.

None of this is testable on macOS — I'd want a Windows mesh-shader smoke (or the existing #[cfg(windows)] valu tests extended with a divergent-barrier case) before this lands.

@mercuryVM mercuryVM 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 1052a90 on Windows 11, RTX 5070, MSVC: cargo test -p victory-shader -- --test-threads=1 69 passed, and cargo clippy -p victory-shader --all-targets is clean. victory translate --validate --pipelines on the local title executable still reports 12 of 12 programs. That executable has no mesh programs, so the mesh half isn't exercised there. The PR adds no tests of its own.

The mesh bounds guards look right: writes past max_vertices/max_primitives are masked, every primitive slot gets an emitIndices, and indices at or past max_vertices are culled. One concern before approving:

  • lower.rs (s_barrier with fence_only_barrier): dropping SYNC_THREAD_GROUP swaps a host deadlock for a silent data race. The console code put a barrier there because later instructions read LDS or UAV data another wave wrote before it. With only the fence bits, a warp can run past the barrier and read stale data, which is far harder to diagnose than a hang. A more honest option is to refuse the program ("a barrier under a per-lane branch") so it shows up in translate counts, or to move the barrier to the point after the divergent region where all warps meet. If you keep the fence-only form, please log it once per program at warn and add a test pinning which blocks divergent_barriers selects.
  • A test for the mesh guards with threads > max_primitives would help too. A two-pass emitIndices loop is easy to get off by one.

AI-assisted (Claude Code).

@EduBertozzi

Copy link
Copy Markdown
Contributor

On the divergent_barriers part: where the divergence comes from, which may be why the RTX 5080 above still hangs.

On the console, s_barrier is a scalar instruction. A wave runs it whatever EXEC holds, and it skips the block around it only when every lane of the wave is off (s_cbranch_execz). HLSL already requires group barriers in group-uniform control flow. So a barrier the console compiler placed under an EXEC branch is reached by every wave of the group whenever any of its lanes is on.

The divergence comes from our side. The per-lane form turns EXEC into per-lane branches, and on 32-lane hosts a console wave runs as two host waves. If one half has no lane on the path, it skips the barrier that its console wave reaches. divergent_barriers sees the per-lane branch and can't tell that apart from a barrier that is really divergent. Dropping SYNC_THREAD_GROUP there removes the ordering the console relies on, as dpalacios69 noted above: LDS written before the barrier, by other waves or by the other half, isn't guaranteed to be there after it. That makes it a data race instead of a hang. Every mesh program in the title is wave64 (per the shaders.rpf count on #578), so on NVIDIA this applies to all of them.

What would keep both, ordering and no hang, is running such programs so that both halves take every branch together: #439's console flow. That works for compute today in groups of up to 64 threads. Mesh shaders can't declare a wave size, which is why I'm prototyping a compute form of mesh programs (#3) that could use it. On #778 I've asked for a census of the title's mesh group sizes to see whether they fit.

So +1 to splitting. The bounds and initialization guards have hardware evidence (RX 9060 XT above) and could land on their own. The barrier change would need a test where a lane-dependent if guards an s_barrier with LDS written before it and read after it. That test would show the race this form allows.

AI-assisted (Claude Code); reasoning from the code and the console's ISA semantics, not a title run.

@Ilokaxt

Ilokaxt commented Nov 24, 2026

Copy link
Copy Markdown

On an RTX 3070 Ti Laptop (wave lanes 32..32, NVIDIA driver 610.88, Windows 11 build 26300) this PR removes the world hang in wave64 mesh programs, without skipping them.

Tree: main ffe310e + #439 + #431 + #579 + this PR, plus a local fix that creates the device on the high-performance adapter. Diagnostics build, VICTORY_SHADER_LOOP_LIMIT=1000, DRED on, scripted Enter presses, 420 s.

world draws kept per frame (median) world fps device removed
without this PR, VICTORY_SKIP_WAVE64=mesh 352 0.4 no
without this PR, mesh programs enabled – – yes, in the world (see #778)
with this PR, mesh programs enabled 1746 0.5–0.7 no, in 420 s

The opening credits render correctly in the world. The run ended still inside the credits, so I have not seen the scene behind them yet.

@Ilokaxt

Ilokaxt commented Nov 24, 2026

Copy link
Copy Markdown

Correction to my note above: two longer runs (900 s budget) with the same tree (main ffe310e + #439 + #431 + #579 + #587, mesh programs enabled) did hang, in a mesh dispatch each time:

My 420 s run had simply not reached them. So #587 does not remove the mesh hangs on the RTX 3070 Ti. It only let the world run longer with the mesh programs enabled (about 1,400–2,000 draws kept per frame) before one of them hangs. Sorry for the noise.

Machad3x pushed a commit to Machad3x/VIctory-nvidia-3080ti that referenced this pull request Nov 24, 2026
…imitives, omit sync on divergent s_barrier

Resolved the append-at-end conflict in lower.rs with the later lds/gather helpers.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Machad3x pushed a commit to Machad3x/VIctory-nvidia-3080ti that referenced this pull request Nov 24, 2026
… 32-lane hosts (diagnostics)

Test switch for Marvisak#578's refusal: with it a run shows what those programs do on
an NVIDIA host once other causes are removed (Marvisak#587 merged: the hang stays, 4.3 s
of GPU per frame, device removed; with VICTORY_SHADER_LOOP_LIMIT=64 the same
run stays up, 3700 draws per frame at 90 ms of GPU). Diagnostics builds only.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 24, 2026
… initialised, no group sync at a barrier only some lanes reach)

By FibbS6. One textual conflict in emit/lower.rs (its `divergent_barriers`
beside Marvisak#453's `interp_mode`; both kept).

One change to the PR's behaviour, in `Lower::new`: its fence-only barriers
are not applied in the console flow of the wave64 stack (Marvisak#233, Marvisak#320,
Marvisak#439). There every lane of a wave runs every block, so the lanes come to a
barrier together, and the stack relies on the group sync to order one
wave's LDS accesses. Per lane, the PR's rule stands.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 24, 2026
Marvisak#587 came without tests. Two, neither needing a GPU:

- mesh_primitives_past_the_lanes_are_culled_first (emit/mod.rs): for four
  pairs of lane count and declared maximum, the module validates and has
  one `emitIndices` per pass over the maximum plus the export's.
- a_barrier_only_some_lanes_reach_is_a_fence_without_the_group_sync
  (emit/lower.rs): a barrier under a branch on the thread's id is emitted
  with mode 8 per lane; a barrier every lane reaches keeps mode 9.

For them, test builds record the `dx.op` calls of the module a thread
finished last (`shader::last_calls`: opcode and constant first argument).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 24, 2026
A tester bisected an access violation inside AMD's driver, seconds into
the world on an RX 9060 XT, to this tree's merge of Marvisak#483 (7ef2c41). The
cause is not known. With VICTORY_HEAP_GUARD=0 the host passes u32::MAX
where shaders read their heaps' guard slots, so the same shaders redirect
no index: one run with it tells a fault in the shaders' shape from one in
reads of the guard slot. The default is unchanged.

INTEGRATION.md: update 6 (Marvisak#587 with its tests, the switch, the AMD
problem and its workaround, the test run).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Xeltax added a commit to Xeltax/VIcky that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
…tives, and omit sync on divergent s_barrier
This was referenced Nov 24, 2026
KainanP pushed a commit to KainanP/VIctory that referenced this pull request Nov 24, 2026
…fied together

Merges Marvisak#156 Marvisak#162 Marvisak#168 Marvisak#169 Marvisak#368 Marvisak#379 Marvisak#456 Marvisak#587 Marvisak#990 Marvisak#1021 Marvisak#1048 Marvisak#1052 Marvisak#1053
Marvisak#1095 Marvisak#1142 Marvisak#1190 Marvisak#1211 Marvisak#1223 Marvisak#1259 Marvisak#1293 on top of the bundle of Marvisak#1402
(which is on Marvisak#1319, on main ffe310e), each head unchanged. Hunks that
conflicted in emit/lower.rs, emit/valu_tests.rs and emit/diff_tests.rs were
merged by keeping both sides (additions next to each other); a PR was kept
only if cargo clippy -p victory-shader --all-targets -- -D warnings still
passed with it. No instruction name is handled in two places.

Marvisak#98 is not in this version: an offline measurement on a retail dump showed
it loses 31 programs that validate on Marvisak#1402.

cargo clippy --workspace --all-targets --locked -- -D warnings, with and
without --all-features, is clean and cargo test --workspace --locked passes
(381 passed, 6 ignored; main has 342 passed, 6 ignored).

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

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/lower.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

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.