Skip to content

Shaders: coalesce title-memory dirty marks within a wave - #2755

Open
brainbudt wants to merge 1 commit into
Marvisak:mainfrom
brainbudt:perf/shader-wave-dedupe-marks
Open

brainbudt wants to merge 1 commit into
Marvisak:mainfrom
brainbudt:perf/shader-wave-dedupe-marks

Conversation

@brainbudt

Copy link
Copy Markdown

Lanes writing the same title-memory page repeatedly issue atomicOr against the same dirty-bitmap word in system RAM, serializing GPU dispatches (reported in #2695).

What changed

Verification

  • Windows WARP (32-thread, two-store fixture): bitmap atomics dropped from 128 → 1 (repeated page), 128 → 9 (32 pages sharing a bitmap word), and 128 → 32 (32 distinct words), with matching bitmap and title-data readbacks.
  • Microsoft DXIL Validator: accepted synthetic compute, pixel, vertex, and mesh shaders containing the new marks.
  • Cross-platform: Windows x86_64-pc-windows-msvc cargo check --tests passed; macOS ARM64 & Linux x86-64 release test suites (159 passed) and Clippy -D warnings passed (tested with the Build and test on Linux; a CI job keeps it so; VICTORY_LOG_FILE #2670 validator guards).

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

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Reviewed and verified 26a05508 on Windows 11 (RTX 5090 + WARP + Windows SDK 10.0.26100.0 dxil.dll / dxc.exe) against main (13f91ca).

Code review

  • EXEC / guard safety: Lower::memory builds marks (write/active/e.miss) and narrows control flow with begin_guarded(marks) before entering mark_memory_page, so only lanes that actually need to mark participate in waveActiveAllEqual, waveActiveBit(Or), and waveIsFirstLane. When all participating lanes target the same bitmap dword (same), one elected lane (waveIsFirstLane) issues the pre-check + combined atomicBinOp(OR); when divergent, select falls back cleanly to per-lane bit masks and writers with no under-marking.
  • Cross-page writes: the nested begin_guarded(Some(different)) cleanly isolates only lanes whose write crosses into a second page and coalesces those as well, skipping the second wave op entirely for single-page writes and misses.
  • Pre-check monotonicity & stages: since the dirty bitmap only transitions 0 -> 1 on GPU during a submission (cleared on CPU beforehand), a stale L1 rawBufferLoad reading 0 only causes a benign redundant atomicOr, whereas reading 1 guarantees the bit is already set in device memory—no under-marking is possible. All wave intrinsics precede the per-writer branch, WAVE_OPS (SFI0 0x4000) is set without pinning WaveSize (compatible with both 32-lane and 64-lane hosts), and pixel helper lanes are excluded by SM 6.6 wave ops and discard UAV atomics.
  • (Note on merge compatibility): clean git merge-tree against 13f91ca, #2668, #2724, and #2761; touches the same write && bytes > 1 tail-page section of Lower::memory as #2484 (which switches the tail-page lookup to a 64-bit second page-table lookup across 256 MiB / 4 GiB boundaries—easy to reconcile in either merge order).

Verification (Windows 11, RTX 5090 + WARP + dxil.dll)

  • cargo test --locked -p victory-shader --lib --features host emit::tests: 19 passed, 0 failed (including both new tests verifying SFI0 0x4000, dxc -dumpbin IR, and WARP execution across shift = 2/16/21).
  • emit::memory_tests: WARP (VICTORY_WARP=1) 4/4 passed; RTX 5090 3/3 passed (skipping only the pre-existing 13f91ca 0xc0000005 baseline in heap_direct_texture_reads_mark_their_heap_index).
  • Full victory-shader --features host (excluding slow valu_tests & that baseline test): 264 passed, 3 failed (all 3 are pre-existing RTX 5090 13f91ca baselines, 0 regressions).
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default & --features host): 0 warnings; cargo xtask policy: 184 files ok.

@kotekompotek

Copy link
Copy Markdown

Verified the wave logic: WaveActiveAllEqual is a wave-uniform reduction so can't diverge inside the guard; when false every lane falls back to its own atomic (identical to today). The stale-load fast path can only produce an extra OR (idempotent), never a lost mark - the comment says exactly this. Nesting the crossing guard (first != last) inside the marks guard is per-lane correct for per-lane spans. One trade worth noting: the same=false case now pays a rawBufferLoad + atomic where it paid only the atomic - wins big on uniform writes, mildly worse on fully divergent ones. DXIL ops 110/115/120 and the WAVE_OPS flag look right.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent WARP verification: PR head 26a0550

Environment: Windows 11, rustc/cargo 1.98.1, debug build, VICTORY_WARP=1 VICTORY_D3D12_WARP=1 (Microsoft WARP only, no physical GPU), Windows SDK 10.0.26100.0 dxil.dll 1.8.2502.11 on PATH. Tested the PR head itself (single commit directly on origin/main 13f91ca, so identical to the merge tree). All commands cargo ... --locked.

Baseline on origin/main: test -p victory-shader --lib --features host (and host,diagnostics, and with default features): 267 passed, 3 failed, 3 ignored. The 3 pre-existing failures are WARP-only: emit::synthetic_tests::{a_mask_joined_with_data_at_a_uniform_loop_is_its_number, permutes_read_the_lane_named_and_zero_from_lanes_left_out} (hardware: no pipeline for a wave of 32) and gathers_at_a_level_match_the_hardware (lane mismatch). clippy --all-targets --features host,diagnostics -D warnings: clean.

On this PR:

  • test -p victory-shader --lib --features host: 269 passed, 3 failed, 3 ignored. --features host,diagnostics: the same. Default features: the same. The 3 failures are exactly the baseline ones. +2 new tests, both passing.
  • clippy --locked -p victory-shader --all-targets --features host,diagnostics -- -D warnings: clean, and also clean with default features.
  • dxc.exe (SDK 10.0.26100.0) was present, so the dump_dxil IR-order assertions of title_page_marks_use_wave_ops_and_guard_the_atomic actually ran.
  • Negative control: restored emit/lower.rs from origin/main: title_page_marks_use_wave_ops_and_guard_the_atomic fails (the WAVE_OPS flag assert, mod.rs:1673), then restored the PR file. title_page_marks_keep_every_lanes_bit (a runtime result check) also passes on origin/main: it guards against a regression in the dirty bits rather than detecting the old code.

Review: one lane per shared bitmap word ORs the whole wave's bits, and otherwise each lane keeps its own word and bit; the atomic runs only when the bits are not already set. I traced it: waveActiveAllEqual on the word index, waveActiveBit OR (kind 1) over the active lanes, waveIsFirstLane, then a stale-read-safe test before the OR. The second page of a write is marked only when it differs (via a nested guard). I found no correctness issues. The wave ops now run in the access guard, so correctness depends on the active-lane semantics of waveActive* inside branches. The runtime test covers shifts 2, 16 and 21 with 32 lanes, wave size 32 only (WARP), and not a wave of 64. Housekeeping: dump_dxil now panics on dxc spawn or write failures instead of returning None, and the cfg(windows) moves make default non-Windows builds of the tests compile.

Not tested: a physical GPU, any retail title or game behavior, a release build, non-Windows targets, wave64 execution.

AI disclosure: this verification was run by Claude Code (Claude Sonnet 5.5). No game files, dumps, shader names or retail hashes were used.

@brainbudt
brainbudt force-pushed the perf/shader-wave-dedupe-marks branch from 6b9ece1 to 347a399 Compare November 25, 2026 22:03

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Lower::mark_memory_bit in crates/victory-shader/src/emit/lower.rs now combines title-memory dirty marks within a wave. When all active lanes target the same bitmap dword, one elected lane ORs the wave's bits (waveActiveAllEqual, waveActiveBit OR, waveIsFirstLane). Otherwise each lane keeps its own bit. In both cases the atomic is skipped when a plain load already shows every bit set. Both callers in Lower::memory run it inside the access guard, and because bits only go from 0 to 1 during a submission, a stale load can cause at most an extra OR, never a lost mark. Notes for the maintainer: it was tested on wave32 WARP only, not with wave64 or pixel shaders with helper lanes on hardware. Fully divergent writes now pay an extra load. The cfg(all(test, windows)) and test-import changes in emit/mod.rs are unrelated cleanups. CI: check passing. This is a recommendation for a maintainer to look, not an approval.


Generated by Claude Code

@Anzymanz

Copy link
Copy Markdown

From a tester working with Claude (AI-assisted, Claude Code): an in-title measurement on AMD/Linux (RX 9070 XT, RADV via vkd3d-proton), this PR with #2484 on 13f91ca + #1997 + #454 + #2644 against the same base, 30 s of the opening cutscene each, VICTORY_GPU_TIMING=1. Of the two costliest kernels in our world frames, both XboxBvhBuilder.dll compute programs that write title memory: 28918b7150a7aad5 goes from 0.77 to 0.57 ms per dispatch, caa78b8ca62bc3d6 stays at 0.36-0.37 ms. No new fault in the run. The full comparison (with #2175 and a control arm) is on #2804: #2804 (comment)

@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 (NVIDIA GeForce RTX 5090 + WARP, dxil.dll validator active):

  • cargo test -p victory-shader --lib title_page_marks: 2 passed, 0 failed (title_page_marks_use_wave_ops_and_guard_the_atomic and title_page_marks_keep_every_lanes_bit).
  • cargo test -p victory-shader --lib emit::memory_tests -- --skip heap_direct_texture_reads_mark_their_heap_index: 4 passed, 0 failed (RTX 5090 hardware + WARP).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

github-actions Bot pushed a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
A benchmark (ignored by default) and a check, in HLSL that does what
translated programs do when they store to title memory: look the address
up in the page table (an upload heap), set the bits of the pages written
in the list's bitmap with atomics, store into the host copy. Variants:
main's two unconditional atomics per store; a load first and an atomic
only for a clear bit (Marvisak#2167's approach); one atomic per wave for lanes
sharing a bitmap word (Marvisak#2755's); both; none. Each with the bitmap in
system memory, as now, and in video memory; four ways of writing.

RTX 5070 Ti, driver 621.24, ms per dispatch of 131,072 threads storing
16 dwords each (bitmap clear before the dispatch / already set):

                            one page      linear  page/lane  scattered
  main, system bitmap      79.8/76.5   75.8/75.0  1595/1583  336/337
  test-first, system         1.6/0.33   11.5/0.35    21/20    13.0/6.7
  wave, system               1.7/0.33   11.4/0.36    22/22    13.5/6.8
  main, video bitmap        0.31/0.31   0.32/0.32    20/20     6.4/6.5
  test-first, video         0.30/0.30   0.32/0.32    20/21     6.8/6.7
  no marks                  0.30/0.29   0.32/0.32    20/19     6.5/6.5

Atomics on a bitmap in system memory are what cost: up to 250 times the
dispatch without marks. Loading first removes them once a page is marked;
one atomic per wave adds nothing on this GPU. A bitmap in video memory
costs nothing even with main's unconditional atomics, first marks
included. What is left in "page per lane" and "scattered" is the stores
into the host copy in system memory.

`every_variant_marks_the_pages_main_marks` checks every variant, in both
places, sets exactly main's bits. `dxc.exe` comes from the Windows SDK;
without it or a GPU both tests do nothing.

    cargo test -p victory-gpu --release --lib marks_bench -- --ignored --nocapture
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
Jineggegg added a commit to Jineggegg/VIctory that referenced this pull request Nov 26, 2026
Adapted from brainbudt, Marvisak#2755 (347a399). Preserve the e815cef end-address lookup. Exercise both uniform and divergent EXEC and compare the bitmap on hardware and WARP.
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.

7 participants