Skip to content

GPU: batch and parallelize descriptor heap syncs - #3055

Draft
MeMako wants to merge 4 commits into
Marvisak:mainfrom
MeMako:pr/sync-heaps-perf
Draft

MeMako wants to merge 4 commits into
Marvisak:mainfrom
MeMako:pr/sync-heaps-perf

Conversation

@MeMako

@MeMako MeMako commented Nov 26, 2026 •

Copy link
Copy Markdown

Draft — Part 3 of the triage-requested 3-part split.

Problem

descriptor::sync_heaps() runs before every submission on the submitting thread:

  1. Each dirty 64 KiB heap page is processed individually (HostHeap::sync_slots), allocating three temporary Vecs per page (records, current, stale) and locking VIEWS and self.made once per page while looking up VIEWS even for empty unchanged slots.
  2. Each changed slot is mirrored serially via HostHeap::mirror, taking self.made twice and VIEWS, self.kept, RETIRED, and self.viewed once per slot, running D3D12 Create*View serially, and calling shadow::for_view / shadow::find up to three times per buffer descriptor (prepare, make, and bindless::seen).

Fix (Clean 4-Commit Stack)

  1. 24618c9 (Part 1, GPU: batch dirty heap page ranges under one diff pass in sync_heaps #3139): Batches dirty page ranges and diffs directly against title memory, self.made, and VIEWS under one lock of each (made -> VIEWS), clamping with past_cap.max(last - 1) and skipping VIEWS for empty unchanged slots.
  2. 5825419 (Part 2): Adds shadow::find_existing_batch (placed cleanly after for_view so for_view keeps its doc comment), PreparedBuffer, prepare_resolved, make_with (removing duplicate GetDesc() in buffer_uav), bindless::Viewed::set_with / seen_entry, and tests for find_existing_batch and WHILE_PREPARING retries (sync_ranges_retries_slot_rewritten_while_preparing).
  3. cae33f6 (Part 3): Parallelizes dirty page probing (pages.len() >= 16), Phase 1 diff (total >= 32_768), Phase 2 texture preparation, and Phase 3 D3D12 descriptor creation (ready.len() >= 64) using the crate's existing crate::texture::in_parallel helper (max 4 workers, zero unsafe in parallel result collection). Holds self.made once across Phase 3, re-checks both live title slot bytes and VIEWS generation under made -> VIEWS (retrying concurrently rewritten or released slots via HostHeap::mirror after dropping self.made), and applies self.kept, host::retire_all, and bindless::Viewed::set_batch in one lock each.
  4. 566a27a (by @passerbyyhx, via MeMako/VIctory#1): Non-#[ignore] test a_sixteen_page_heap_sync_preserves_sparse_views_at_the_boundaries covering both the 16-page parallel probe and 32,768-slot Phase 1 parallel diff thresholds.

Related PRs

Verification & Standalone Benchmarks

Host: Windows 11 (10.0.26200), AMD Ryzen 9 7950X, AMD Radeon RX 9070 XT (RDNA 4, driver 32.0.32029.2010), base main 63bcca4.

  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean.
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean.
  • cargo test --workspace --locked -- --test-threads=1: all passed, 0 failed (victory-gpu: 236 passed, victory-shader: 305 passed, xtask: 6 passed).
  • cargo run --locked -p xtask -- policy: asset policy: 191 files ok.

Standalone benchmark (--release, world_frame_sync_heaps, 700,416 slots across 342 dirty 64 KiB pages, 9-round medians, main vs. Part 3 alone):

Case main (63bcca4) median Part 3 (566a27a) median Speedup
Scan-only (0 changed / 700k scanned) 1.48 ms 0.72 ms 2.06x
Typical (6,000 changed / 700k scanned) 5.80 ms 4.31 ms 1.35x
Heavy (14,000 changed / 700k scanned) 10.32 ms (max 11.17 ms) 8.25 ms (max 8.67 ms) 1.25x

(A standalone live main vs. Part 3 title run — without other PRs — will be added before marking this PR ready for review.)

Hardware runs and tests were performed by me on my 7950X / RX 9070 XT; PR and code drafted with Antigravity.

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

Reviewed head 1a09c6fe line-by-line (lock ordering, disjoint-slot parallelism, MRU buffer lookup, and resource retirement) and verified locally on Windows 11 (NVIDIA GeForce RTX 5090 + WARP, Rust 1.98.1 MSVC):

  • RTX 5090: sync_ranges_mirrors_and_clears_many_slots_in_parallel, find_existing_batch_matches_find_for_nested_and_adjacent_buffers, and descriptor:: (22/22, skipping only the known RTX 1M-view heap cap test dynamic_slots_are_held_until_released) all passed.
  • WARP (VICTORY_D3D12_WARP=1): sync_ranges_mirrors_and_clears_many_slots_in_parallel, find_existing_batch_matches_find_for_nested_and_adjacent_buffers, and descriptor:: (23/23) all passed.
  • Clippy: cargo clippy -p victory-gpu --locked --all-targets -- -D warnings (default and --features diagnostics) — 0 warnings.

Lock ordering matches existing paths (SHADER_HEAPS -> self.made -> VIEWS in Phase 1; self.made dropped for Phase 2 find_existing_batch / texture::in_parallel; self.made -> self.kept then host::retire_all after releasing self.kept, and self.made -> self.viewed.inner in Phase 3). Sorting/merging raw_clamped into disjoint intervals ensures worker threads write disjoint 0..self.count descriptor slots, and Phase 3 re-checks both the live title slot bytes and made[index] != key before writing.

Minor non-blocking notes:

  1. In HostHeap::sync_ranges, if last > self.count { past_cap = Some(last - 1); } overwrites past_cap rather than taking .max(last - 1). Current callers pass sorted ranges, but taking the max keeps HIGHEST consistent regardless of input order.
  2. Between Phase 2 and Phase 3, if another thread calls resource::release -> descriptor::forget (bumping VIEWS generation without overwriting heap slot bytes), the now == raw check still passes with the snapshot View (same window as HostHeap::mirror on main); re-checking VIEWS generation when building ready would tighten that window.
  3. Non-#[ignore] coverage for the pages.len() >= 16 parallel probe path, the total >= 32_768 Phase 1 parallel diff path, and a Phase 2->3 retry trigger (via WHILE_PREPARING) would be nice additions.

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

Verified the concurrency design end-to-end: Phase 1 diffs under the same made→VIEWS lock order the serial path used; Phase 2 prepares outside locks (find_existing_batch is provably find-equivalent for buffer-resident queries — innermost containing = first .rev() match with end >= end, and the MRU's 'address < next_key_start' guarantees no deeper key; loose/ambiguous falls back to find); Phase 3 re-reads every record and retries changed slots through mirror — the TOCTOU that mattered. Ranges are sorted/merged so parallel slot writes are strictly disjoint; made is held across the whole apply; stride/mark/address/viewed/kept keep their own locking as in mirror. past_cap preserves the beyond-count path. sync_workers caps at half-parallelism ≤6 — leaves the title threads headroom. The 256-slot test discriminates make/clear/rewrite over split ranges.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Verdict: needs work. sync_heaps cost is a real target. However, this PR rewrites HostHeap::sync_slots into a three-phase multithreaded sync_ranges (about 800 lines in descriptor/host.rs). It also changes lock scope: self.made is now held across descriptor creation and the kept/RETIRED/viewed updates. That is too much to review as one change. Specific points:

  • Thread spawning per call. sync_heaps (page probing) and both sync_ranges phases use std::thread::scope plus scope.spawn. That creates fresh OS threads on every sync, before every submission, about 19 times a frame by your own figure. The crate already has a worker helper (crate::texture::in_parallel, used here for textures). Use it, or measure the spawn cost in the live case.
  • Misplaced doc comment. In crates/victory-gpu/src/shadow.rs, find_existing_batch is inserted between for_view's doc comment and for_view. for_view loses its documentation, and the new function inherits it.
  • Split the PR. First, the batched page probing and the single-lock diff, with no thread changes. Second, the batched buffer resolution (find_existing_batch / PreparedBuffer). Third, parallel descriptor creation. Each part can then be measured and reviewed on its own, especially the held-made lock order and the concurrent set_stride/set_mark/note_address writes.
  • Benchmark scope. The benchmark is synthetic, and the live figures come from a branch combined with GPU/Bindless: O(1) texture forget reverse index, FxHash maps, and identical-rewrite fast-path #2940. Please give main against this PR alone in the live case.

CI: check passing.


Generated by Claude Code

@MeMako

MeMako commented Nov 26, 2026

Copy link
Copy Markdown
Author

Updated #3055 per the triage review and @fuzeofficlal's notes, and marked it Draft while splitting it into three reviewable PRs:

  • 3-part split: Opened Part 1 as GPU: batch dirty heap page ranges under one diff pass in sync_heaps #3139 (pr/sync-heaps-1-batched-diff, +155/-36, batched page probing + single-lock zero-alloc diff, zero thread or lock-scope changes). Part 2 (pr/sync-heaps-2-batched-buffers) is pushed on my fork and will be opened once GPU: batch dirty heap page ranges under one diff pass in sync_heaps #3139 lands. #3055 now holds Part 3 cleanly stacked on top.
  • Threading (crate::texture::in_parallel): Replaced all custom std::thread::scope / scope.spawn loops in page probing, Phase 1 diff, and Phase 3 descriptor creation with crate::texture::in_parallel (max 4 workers).
  • Fixed shadow.rs doc comment: Moved find_existing_batch after for_view with its own doc comment so for_view keeps its documentation.
  • Reviewer notes & test coverage: Applied @fuzeofficlal's three notes (past_cap.max(last - 1), Phase 3 VIEWS generation re-check under made -> VIEWS, and non-#[ignore] WHILE_PREPARING retry test sync_ranges_retries_slot_rewritten_while_preparing), and included @passerbyyhx's MeMako/VIctory#1 commit (566a27a) covering both the 16-page and 32,768-slot parallel thresholds.
  • Benchmarks: Updated the PR description to standalone main (63bcca4) vs. Part 3 (566a27a) numbers only (no mixed #2940 figures). A standalone live main vs. Part 3 run will follow before un-drafting.

Hardware runs and tests were performed by me on my 7950X / RX 9070 XT; comment drafted with Antigravity.

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.

5 participants