Skip to content

GPU: back indirect arguments in VRAM, use RwLock for SHADER_HEAPS, and batch copy_back_marks - #3320

Open
danielibabet wants to merge 2 commits into
Marvisak:mainfrom
danielibabet:pr/perf-copyback-heaps-indirect
Open

danielibabet wants to merge 2 commits into
Marvisak:mainfrom
danielibabet:pr/perf-copyback-heaps-indirect

Conversation

@danielibabet

Copy link
Copy Markdown

Summary

Three independent CPU/GPU contention and barrier fixes on top of main:

  1. VRAM-backed indirect argument buffers + drop redundant post-clamp UAV barrier (indirect.rs, host.rs)
    • Add Host::uav_buffer (D3D12_HEAP_TYPE_DEFAULT, ALLOW_UNORDERED_ACCESS, COMMON) and back HostList::arguments in VRAM instead of L0 system memory (shared_buffer), avoiding PCIe round-trips for both clamp UAV stores and ExecuteIndirect reads.
    • Remove the redundant uav_barrier() (pResource = None) right before transitioning out from UNORDERED_ACCESS to INDIRECT_ARGUMENT in indirect::bound (the resource transition already synchronizes out, and the next bound call issues the pre-dispatch uav_barrier() for budget).
    • Update indirect::tests::clamped and argument_buffers_move_from_their_current_states to read back out via readback.
  2. Eliminate SHADER_HEAPS mutex contention during descriptor mirroring (descriptor.rs)
    • Switch SHADER_HEAPS from Mutex to RwLock: mirror (title threads), sync_heaps (submission thread), and all read-only queries take .read(), while Drop and create take .write(), so sync_heaps no longer stalls title threads writing descriptors (MirrorWaitUs).
  3. Reuse scratch buffer and batch Tracked::taken in copy_back_marks (shadow.rs)
    • Reuse a single pooled(PAGE) scratch buffer across all marked pages in copy_back_marks instead of heap-allocating and zeroing vec![0u8; n] per 64 KiB page.
    • Batch t.taken(&list) once per Shadow instead of calling VirtualProtect / taking the GUARDS lock page by page.

Verification

  • cargo test -p victory-gpu --locked -- --test-threads=1 --skip dynamic_slots_are_held_until_released --skip lists_share_a_table_and_copies_keep_their_views: 229 passed; 0 failed; 1 ignored; 2 filtered out
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warnings
  • cargo run -p xtask --locked -- policy: asset policy: 191 files ok

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

Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):

  • Backing indirect argument buffers in VRAM (with CopyBufferRegion readback in tests), switching SHADER_HEAPS to RwLock, and reusing title_buf + batching t.taken(&synced) in copy_back_marks are clean and sound.
  • cargo test -p victory-gpu --lib indirect:: -- --test-threads=1 (RTX 5090): 6 passed, 0 failed.
  • cargo test -p victory-gpu --lib descriptor:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released (RTX 5090): 21 passed, 0 failed.
  • cargo test -p victory-gpu --lib shadow:: -- --test-threads=1 (RTX 5090): 10 passed, 0 failed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo clippy -p victory-gpu --features diagnostics --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I checked the DEFAULT-heap argument buffers with the #3307 + #3197 + #2996 composition. They conflict in allocation and in tests that directly mapped the argument buffer. Exact integration 6be1c86 keeps the DEFAULT heap, cached width and shared argument state, and uses a real readback copy for those tests.

Verified app-local Agility 1.620.2/WARP 1.0.22 passed all 15 indirect tests. The mixed test alternates window and single clamps in one list, grows the buffer with 6,000 records, checks the output, and replays transitions back to COMMON. The translated 24-group mesh test and full GPU diagnostics (259 passed, 0 failed, 1 ignored) passed with VICTORY_CLAMP_WINDOW=0 and 1. Strict GPU Clippy passed with default and all features. Asset policy passed. This verifies DEFAULT-heap behavior on WARP. Discrete-GPU placement and PCIe timing still need a hardware run.

@danielibabet the integration is available if #3307 lands first. Please retain its Git author if using the resolution, or add Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com> if folding it into another commit. AI-assisted with Codex.

@danielibabet

Copy link
Copy Markdown
Author

Thanks @andrewpozdnakov7-jpg for verifying against #3307 + #3197 + #2996 and preparing the integration (\6be1c86)! If #3307 lands before #3320, we'll rebase #3320 onto it and make sure to preserve your Git authorship / \Co-authored-by.

@borosf

borosf commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Independent local verification on Windows 11 x64, Intel Arc B580 12 GB (12 cores), at this PR's head 332461e:

cargo test -p victory-gpu --lib --locked --no-fail-fast -- --test-threads=1
cargo clippy -p victory-gpu --all-targets --locked -- -D warnings
cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings

Result: victory-gpu lib: 2 known baseline failures; the waits_end_on_ticks timing test is flaky under load (this PR does not touch frame.rs).

Both clippy runs exit 0 (-D warnings, both feature sets). The B580 baseline failures pristine main also hits are: victory-gpu descriptor::host::tests::dynamic_slots_are_held_until_released + memory::tests::lists_share_a_table_and_copies_keep_their_views (#1744/#1829); victory-shader emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros + emit::valu_tests::fspx_programs_run_with_the_hardwares_dot_products. No PR regressions.

Synthetic tests only; no game data used. Full log kept locally. — borosf

@Machad3x

Copy link
Copy Markdown

A data point on the SHADER_HEAPS RwLock change, measured on one Windows machine (RTX 3080 Ti, Ryzen 7 5700X) running a commercial title:

We tried the same idea on our branch: SHADER_HEAPS as an RwLock, with readers taking a shared lock.

Metric (per measurement window) Mutex RwLock
Wait on view writes 209 ms 0 ms
Heap sync time 43 ms 160 ms
Frame time 309-321 ms 408-445 ms

So we dropped it. Our reading: the contention moved into the per-heap sync instead of disappearing, and frame time got worse overall.

We are now testing per-slot locks (64 striped locks, atomic "made" keys with a busy sentinel) together with the RwLock, and will report numbers.

This work was done with AI assistance (Claude).

@danielibabet

Copy link
Copy Markdown
Author

Thanks for sharing those real-title numbers @Machad3x! Moving contention into per-heap sync and regressing frame time (309-321 ms -> 408-445 ms) is definitely not worth keeping.

I've reverted the SHADER_HEAPS RwLock change from this PR in 3afe123 (descriptor.rs back to main), leaving just the VRAM-backed indirect argument buffers and batched copy_back_marks (verified with cargo clippy -p victory-gpu --all-targets -- -D warnings and cargo xtask policy — 191 files ok).

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

Clean, focused change now that the SHADER_HEAPS RwLock edit has been reverted in 3afe123b.

Checked both remaining parts:

  • Backing HostList::arguments with a DEFAULT-heap UAV buffer (crates/victory-gpu/src/host.rs:2252, 2715) and dropping the redundant post-clamp uav_barrier() before the UNORDERED_ACCESS -> INDIRECT_ARGUMENT transition (crates/victory-gpu/src/indirect.rs:232-236): bound still opens with [uav_barrier(), COMMON -> UNORDERED_ACCESS] (ordering both earlier title UAV writes and previous budget writes), the explicit transition synchronizes out for ExecuteIndirect and capture::read_indirect, and done() transitions out back to COMMON.
  • Reusing a single pooled(PAGE) scratch buffer and batching t.taken(&list) per Shadow in copy_back_marks (crates/victory-gpu/src/shadow.rs:491, 949-986) matches copy_back_or_not's batching model and keeps title_buf[..n] within PAGE.

Verification on origin/main (63bcca43):

  • cargo xtask policy: ok (191 files ok)
  • cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (default and --features diagnostics): clean
  • cargo test --locked -p victory-gpu --features diagnostics -- indirect:: shadow:: capture:: --test-threads=1 with VICTORY_D3D12_DEBUG=1: 27/27 passed on RTX 5090 and 27/27 passed on WARP
  • Additional live probe running two back-to-back indirect::bound -> capture::read_indirect -> indirect::done cycles on the same DEFAULT-heap arguments buffer under VICTORY_D3D12_DEBUG=1: zero debug layer errors on both RTX 5090 and WARP.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I retested the integration with your 3afe123 lock rollback in 6229e40. In the combined tree, descriptor.rs is byte-for-byte identical to main. The default-heap argument buffer, shared clamp state, mesh flattening and batch copy-back changes are still present.

On app-local WARP, all 15 indirect tests and the 24-group mesh readback passed with VICTORY_CLAMP_WINDOW=0 and 1. The full GPU suite reported 259 passed and one ignored under each setting. Strict GPU Clippy in both configurations and the asset policy check passed. These results verify the buffer and draw behavior on a synthetic workload. The real-title frame times are from @Machad3x's measurement above.

@danielibabet, this updates the previous combined branch if #3307 lands first. Please keep the Git author or use Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com> if you fold the resolution into another commit.

@neonbyte1

Copy link
Copy Markdown

Measured in game on an RTX 5090 (driver 620.73.04), Linux, GE-Proton11-14 Wine with its vkd3d-proton, GTA VI's opening prison yard, warm cache; base is main plus the unmerged PRs this setup needs (#3312, #3318, #2472 and others, listed in #3425). This PR merged on top, 600 s runs, medians of the last 30 frame-rate lines, against a control run of the same base taken next to it (frame times drift by up to ~25 ms between runs hours apart).

Merged here as its current head 3afe123b reduced to the part that still applies on this stack: indirect arguments in VRAM plus the dropped post-clamp UAV barrier (the copy-back third overlaps #3246/#3341, and the RwLock part was withdrawn).

Frame Preparing Copy-back
control 402 ms 81 ms 53 ms
+ this PR (reduced) 409 ms 83 ms 52.7 ms

No gain here. Two test regressions on the GPU (vkd3d-proton, RTX 5090), both passing on the base: indirect::tests::clamped_records_hold_every_word (indirect.rs:838) and indirect::tests::spread_records_split_large_x_and_keep_small_unspread (:1074) fail with Map → 0x80070057. The argument buffer moved to a DEFAULT heap (Host::uav_buffer), and these two tests still map it directly; a third site, read_words(out, …) at :1396 in hoisted_transitions…, has the same pattern but is masked by an unrelated failure. They need the read-back copy the PR already uses elsewhere. Test-only: no production path maps an argument buffer.

AI-assisted (Claude); measured on my machine, no game data shared.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

Thanks @neonbyte1. I checked the named tests against upstream 63bcca4, #3320 3afe123, #3307 f23c88f and our published combination. None of those trees contains the three test functions from your report. The two readbacks in #3320 itself already use the copy to a readback heap.

Could you link the commit or branch that adds clamped_records_hold_every_word and spread_records_split_large_x_and_keep_small_unspread? The test-source commit is enough. That will give us a base for a test-only follow-up using the existing readback path.

AI-assisted with Codex.

@KainanP

KainanP commented Nov 27, 2026

Copy link
Copy Markdown

One concern with the copy_back_marks batching at head 3afe123b (read from the code, not run).

A title write can be lost between a page's copy-back and its batched taken.

  • shadow.rs:525-530: when copy_back_marked writes page P back, it holds watch::writing(from, n). Drop for Writing (watch.rs:786-793) calls changed(), which marks P changed and gives it write access back. The page is then only pushed onto synced.
  • shadow.rs:959-971 / :980-985: t.taken(&list) runs only when the loop reaches a page of another Shadow, or at the end. taken (watch.rs:545-560) protects the pages and then does changed.remove(&page) and self.changed.taken(page).

Scenario: P is copied back and left open. The loop goes on to P+1 … P+k of the same Shadow. Each of those costs a 64 KiB read_into, a compare or merge, maybe a write_mapped, and two guard-lock rounds. Meanwhile a title thread writes P. P is open, so there is no fault and the write isn't recorded anywhere. Then taken([P, …]) clears P's changed mark. The host copy keeps the shaders' bytes and never picks up the title's write. Nothing re-marks it later, because the page is guarded again and clean.

main has the same race. There, though, taken runs right after each page's write, so the window is a few microseconds. Here it lasts as long as the rest of that Shadow's marked pages take, which can be thousands of pages per frame on the scenes in #3425.

Possible fixes, keeping most of the gain:

  • Call t.taken(&[page]) right away for pages where written.is_some(), and batch only the pages that were already equal (those were never opened, so there is no window).
  • Or hold the Writing guards until the batched taken, so P stays in busy and a title write is seen through changed. This needs checking against protect's busy filter, which would then skip P.

AI use: Claude Code (Claude Opus 5.5).

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.

8 participants