Skip to content

perf(gpu,shader): add opt-in raw CBV reads, sharded submit journal, and fine write guards - #3455

Open
legayclightning22 wants to merge 4 commits into
Marvisak:mainfrom
legayclightning22:perf/renderer-ports-cbv-and-guards
Open

legayclightning22 wants to merge 4 commits into
Marvisak:mainfrom
legayclightning22:perf/renderer-ports-cbv-and-guards

Conversation

@legayclightning22

Copy link
Copy Markdown

Summary

Adds opt-in raw constant-buffer reads (VICTORY_PORT_CBV), sharded submit journaling (VICTORY_PORT_JOURNAL), and finer-grained write guards (VICTORY_PORT_GUARD) while keeping default behavior safe and preserving bounded root descriptor tables.

Changes

  • Support opt-in raw CBV reads (ROOT_CBV_SPACE), sharded submit journal updates (SHARDS), and per-block write guards (GUARDS / BUSY) across victory-gpu and victory-shader.
  • Keep all new port flags safely gated (0/off by default) and bounded for low-memory configurations.
  • Add unit tests covering sharded journaling, raw CBV flag/lowering guards, and write-guard synchronization.

Verification

  • cargo test --locked -p victory-gpu --lib and cargo test --locked -p victory-shader --lib
  • cargo clippy --locked -p victory-gpu -p victory-shader --all-targets --all-features -- -D warnings
  • cargo xtask policy

@oni-swr

oni-swr commented Nov 27, 2026

Copy link
Copy Markdown

WARP test report (Windows, software adapter only; no GPU, no game files). AI-assisted, Claude.

  • OS: Windows 11 Pro, build 10.0.26200
  • Adapter: Microsoft Basic Render Driver (WARP, flagged software)
  • dxil.dll: Windows SDK 10.0.26100.0, file version 1.8.2502.11
  • rustc: 1.98.1 (48a229cea 2026-09-01), --locked
  • Env: VICTORY_WARP=1, VICTORY_D3D12_WARP=1
  • Base: 63bcca4 (main). Head: 0c72875 (this PR, a fast-forward of main, no conflicts)
Run victory-gpu (--features diagnostics, --test-threads=1) victory-shader
main 246 passed, 0 failed, 1 ignored 298 passed, 4 failed, 3 ignored
main + PR, flags off 252 passed, 0 failed, 1 ignored 300 passed, 4 failed, 3 ignored
main + PR, VICTORY_PORT_CBV=1 VICTORY_PORT_JOURNAL=1 VICTORY_PORT_GUARD=1 251 passed, 1 failed, 1 ignored 300 passed, 4 failed, 3 ignored
  • The same 4 victory-shader tests fail on main and on main + PR (all in emit::synthetic_tests: lane permutes, gathers, a mask-at-uniform-loop case). They look WARP-related (wave size) and are not caused by this PR.
  • New tests: 6 in victory-gpu and 2 in victory-shader. All 8 pass with the flags off. With the flags on, 7 of 8 pass.
  • The failing one is port_journal::tests::without_the_switch_every_page_is_fresh, which fails only when VICTORY_PORT_JOURNAL=1 is in the environment. It is not hermetic: enable(false) can only force the flag on, so port_journal() still reads the env switch and claim returns [] instead of [8, 9]. A test-only override that can force the flag off (like CONSTANTS_RAW_HERE: Option<bool>) would fix it.
  • None of the new tests create a device. They are pure logic, so none of them actually exercises WARP. WARP only runs the existing suite.

Notes from reading the diff:

  1. VICTORY_PORT_GUARD=1 alone changes nothing. port_guard() is only read inside the port_journal::on() branch of retake, so the shared-lock path needs the journal as well. light() defaults to on, so with the journal on the shared lock is used even without the guard flag.
  2. With VICTORY_PORT_CBV=1 the shader reads the root CBV through root_srv_handle, and the shader-cache key is changed. I could not see the host-side root SRV binding in this diff. If it already exists elsewhere, ignore this. Otherwise the flag-on path has no test for it.
  3. The new port_cbv::raw_ok and port_journal::shards_of are only compiled for tests, so they cover code that is not used outside tests. The claim comment about a "journal's lock" has no matching outer lock in the code.
  4. In claim, a page is noted before its mark is done. A second thread claiming the same page in that window gets an empty list while the first thread has not yet marked it. This may be harmless, but I did not check it.
  5. Nothing about the flags-off behaviour looked different from main. The Mutex to RwLock change on GUARDS is the only default-path change, and the suites above pass with it.

Counts only; no game data.

@futurepro62

Copy link
Copy Markdown

NVIDIA full-suite run, to go with the reports above by oni-swr, since none of them names a GPU run. Tested exact head 0c72875 (on its base 63bcca4).

Tested

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@KainanP

KainanP commented Nov 27, 2026

Copy link
Copy Markdown

Contention numbers for this PR from a shared benchmark (for #3425).

Method. The benchmark guard_contention and the check refreshes_under_writes_lose_nothing from #3516, split out as a standalone test-only commit (KainanP/VIctory perf/guard-bench: no behaviour change, guards() only times a lock it finds taken in test builds), applied unchanged to this PR's head (0c72875) and to main (63bcca4). 256 MB write-watched pool in 64 KiB pages, copies of 32 pages, 1,400 pages dirtied before each refresh, title threads writing a random word every 20 µs while the copies are taken and copied. Release builds, i7-14650HX (24 threads), Windows 11. 31 rounds, builds interleaved and rotated each round, each run = median of 5 refreshes after a warm-up; table = medians over the 31 runs, plus the median (and quartiles) of the per-round PR/main ratio. Foreign load: other agents' rustc jobs were running (≈8 % of the CPU in a spot sample); the paired ratio is what to read.

Config Refresh ms (main → PR) Writers in their writes, ms Writers waiting for GUARDS, ms PR/main, writes PR/main, wait
1 writer 17.4 → 18.4 5.1 → 5.7 2.8 → 2.9 1.05 (0.97–1.27) 1.05 (0.96–1.23)
4 writers 29.1 → 31.0 37.8 → 37.7 24.3 → 23.4 0.99 (0.92–1.02) 1.00 (0.93–1.04)
4 writers, refresh on 4 threads 9.2 → 9.5 28.3 → 29.6 25.7 → 26.9 1.05 (0.96–1.10) 1.05 (0.98–1.10)
4 writers, twin views (aliased) 30.2 → 31.8 56.4 → 59.0 40.1 → 41.5 1.02 (0.92–1.19) 1.02 (0.94–1.17)
4 writers, copies armed (first writes save the page) 43.2 → 41.7 145.3 → 135.3 119.4 → 112.1 0.99 (0.94–1.07) 0.99 (0.93–1.07)

Run with VICTORY_PORT_GUARD=1 VICTORY_PORT_JOURNAL=1 (light refresh on, the default).

Reading. No measurable change in any config (paired ratios 0.99–1.05). That's expected rather than a verdict on the PR: the shared lock is only taken in retake, while the waits this bench measures are the write-fault handler and Tracked::take, both of which still take GUARDS exclusively (guards() → write()). If the fault handler's read-mostly part could run under read(), this bench would show it.

Correctness. refreshes_under_writes_lose_nothing passes on this head (3 × 1,000 rounds), as do the existing watch:: tests. It doesn't call retake, so the shared-lock path itself isn't covered by it.

AI use: Claude Code (Claude Opus 5.5).

@legayclightning22

Copy link
Copy Markdown
Author

Thanks @oni-swr, @futurepro62, and @KainanP for the WARP, RTX 5070 Ti, and contention benchmark reports! Pushed a428469 making the thread-local flag overrides Option<bool> so without_the_switch_every_page_is_fresh passes hermetically even when VICTORY_PORT_JOURNAL=1 is set in the environment, cleaning up the #[cfg(test)]-only helpers and stale claim lock comment, and clarifying VICTORY_PORT_GUARD / VICTORY_PORT_JOURNAL gating. Re-verified victory-gpu and victory-shader tests (both default and with all flags set), clippy --all-targets --all-features -- -D warnings, and cargo xtask policy.

@upipibi

upipibi commented Nov 27, 2026 •

Copy link
Copy Markdown

A note on the write-guard part (VICTORY_PORT_GUARD), from reading a4284695 and a local benchmark: as it stands, the switch does not touch the path the fork's measured gain came from.

  • Write faults stay exclusive. The fault handler on_write (crates/victory-gpu/src/watch.rs:978) takes guards() (:994), which is GUARDS.write() (:260-262) whatever the switch says. So each fault still waits behind every other fault and refresh, as with the old Mutex.
  • The one shared use needs the journal on. guards_shared() (:265-267) is used outside tests only in the narrow mark at :903-904, inside if crate::port_journal::on() (:898). With VICTORY_PORT_GUARD=1 alone, nothing takes the lock shared.
  • No per-block claims. The description lists "per-block write guards (GUARDS / BUSY)", but BUSY does not appear in the diff.

For comparison, port 6 in the fork this is ported from (gleaf-6/VIctory-7900XTX- 42ddb597, Belobug's watch.rs) takes the lock shared in the faults (open_faulted), the refreshes and publish. Each block's state there sits in atomic words with a claim per block, and only mapping changes, kernel writes and frees take it alone. Its measurement (36-41 → 2-6 ms a frame of all threads' waiting, at 352-392 write faults a frame) is from that fault path. On upstream's own stack, #3425's frame budget (RTX 5090) puts the same cost at 52 ms a frame: the title's write-fault handler queueing behind GUARDS, which our own paths hold about 49 ms a frame.

Benchmark (local only, not proposed): an ignored test in watch::tests. 1, 4, 8 and 16 threads write one byte per 4 KiB into freshly taken pool(64) memory, so each first write per block faults through on_write, 40 rounds each. PORT_GUARD_IN_TEST is set from the variable on every writer thread, since the handler reads it on the faulting thread. Release build, 3 alternating runs per setting:

threads VICTORY_PORT_GUARD=0, µs a round =1, µs a round
1 865 / 925 / 911 922 / 911 / 1194
4 627 / 686 / 639 664 / 694 / 843
8 822 / 894 / 829 807 / 863 / 1161
16 981 / 1152 / 1387 1034 / 1024 / 1356

There is no difference beyond run-to-run noise (the machine had other GPU work running), as expected with the handler on the exclusive lock. With the fork's change, this is the benchmark that should move.

AI-assisted (Claude Code, Claude Opus 5.5 read the code, wrote and ran the local benchmark).

@legayclightning22
legayclightning22 force-pushed the perf/renderer-ports-cbv-and-guards branch from a428469 to 09da498 Compare November 28, 2026 03:30
@legayclightning22

Copy link
Copy Markdown
Author

Thanks @upipibi — pushed an updated commit decoupling VICTORY_PORT_GUARD=1 from VICTORY_PORT_JOURNAL in crates/victory-gpu/src/watch.rs so fault/refresh/publish paths take guards_shared() with per-block atomic BUSY claims while keeping VICTORY_PORT_GUARD=0 identical to main.

Remove the DirtyRanges snapshot machinery and reservation batching that a local watch.rs copy brought onto this branch. Keep the shared guards lock and atomic claims on fault/refresh/publish/remark paths, and assert that remarking works with the port journal off.
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