GPU: batch and parallelize descriptor heap syncs #3053
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3055"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by MeMako on 2026-11-26T05:40:25Z.
Problem
descriptor::sync_heaps()runs before every submission on the submitting thread:HostHeap::sync_slots), allocating three temporaryVecs per page (records,current,stale) and lockingVIEWSandself.madeonce per page while looking upVIEWSeven for empty unchanged slots.HostHeap::mirror, takingself.madetwice andVIEWS,self.kept,RETIRED, andself.viewedonce per slot, running D3D12Create*Viewserially, and callingshadow::for_view/shadow::findup to three times per buffer descriptor (prepare,make, andbindless::seen).Fix (Clean 4-Commit Stack)
24618c9(Part 1, #3139): Batches dirty page ranges and diffs directly against title memory,self.made, andVIEWSunder one lock of each (made->VIEWS), clamping withpast_cap.max(last - 1)and skippingVIEWSfor empty unchanged slots.5825419(Part 2): Addsshadow::find_existing_batch(placed cleanly afterfor_viewsofor_viewkeeps its doc comment),PreparedBuffer,prepare_resolved,make_with(removing duplicateGetDesc()inbuffer_uav),bindless::Viewed::set_with/seen_entry, and tests forfind_existing_batchandWHILE_PREPARINGretries (sync_ranges_retries_slot_rewritten_while_preparing).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 existingcrate::texture::in_parallelhelper (max 4 workers, zerounsafein parallel result collection). Holdsself.madeonce across Phase 3, re-checks both live title slot bytes andVIEWSgeneration undermade->VIEWS(retrying concurrently rewritten or released slots viaHostHeap::mirrorafter droppingself.made), and appliesself.kept,host::retire_all, andbindless::Viewed::set_batchin one lock each.566a27a(by@passerbyyhx, viaMeMako/VIctory#1): Non-#[ignore]testa_sixteen_page_heap_sync_preserves_sparse_views_at_the_boundariescovering both the 16-page parallel probe and 32,768-slot Phase 1 parallel diff thresholds.Related PRs
bindlessFxMap+ O(1)forget): orthogonal;bindless.rs'sInnercontainers are untouched here.SHADER_HEAPS/BUFFERS/VIEWSRwLock): orthogonal; lock types are unchanged here, and holdingself.madeacross Phase 3 keepssync_rangessound even under concurrentSHADER_HEAPSreaders.Verification & Standalone Benchmarks
Host: Windows 11 (
10.0.26200), AMD Ryzen 9 7950X, AMD Radeon RX 9070 XT (RDNA 4, driver32.0.32029.2010), basemain63bcca4.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,mainvs. Part 3 alone):main(63bcca4) median566a27a) median0changed / 700k scanned)6,000changed / 700k scanned)14,000changed / 700k scanned)(A standalone live
mainvs. 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.
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T05:54:05Z.
Review: APPROVED
Reviewed head
1a09c6feline-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):sync_ranges_mirrors_and_clears_many_slots_in_parallel,find_existing_batch_matches_find_for_nested_and_adjacent_buffers, anddescriptor::(22/22, skipping only the known RTX 1M-view heap cap testdynamic_slots_are_held_until_released) all passed.VICTORY_D3D12_WARP=1):sync_ranges_mirrors_and_clears_many_slots_in_parallel,find_existing_batch_matches_find_for_nested_and_adjacent_buffers, anddescriptor::(23/23) all passed.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 -> VIEWSin Phase 1;self.madedropped for Phase 2find_existing_batch/texture::in_parallel;self.made -> self.keptthenhost::retire_allafter releasingself.kept, andself.made -> self.viewed.innerin Phase 3). Sorting/mergingraw_clampedinto disjoint intervals ensures worker threads write disjoint0..self.countdescriptor slots, and Phase 3 re-checks both the live title slot bytes andmade[index] != keybefore writing.Minor non-blocking notes:
HostHeap::sync_ranges,if last > self.count { past_cap = Some(last - 1); }overwritespast_caprather than taking.max(last - 1). Current callers pass sorted ranges, but taking the max keepsHIGHESTconsistent regardless of input order.resource::release->descriptor::forget(bumpingVIEWSgeneration without overwriting heap slot bytes), thenow == rawcheck still passes with the snapshotView(same window asHostHeap::mirroronmain); re-checkingVIEWSgeneration when buildingreadywould tighten that window.#[ignore]coverage for thepages.len() >= 16parallel probe path, thetotal >= 32_768Phase 1 parallel diff path, and a Phase 2->3retrytrigger (viaWHILE_PREPARING) would be nice additions.Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T06:04:00Z.
Review: APPROVED
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.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T06:19:03Z.
Automated triage for the maintainers.
Verdict: needs work.
sync_heapscost is a real target. However, this PR rewritesHostHeap::sync_slotsinto a three-phase multithreadedsync_ranges(about 800 lines indescriptor/host.rs). It also changes lock scope:self.madeis now held across descriptor creation and thekept/RETIRED/viewedupdates. That is too much to review as one change. Specific points:sync_heaps(page probing) and bothsync_rangesphases usestd::thread::scopeplusscope.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.crates/victory-gpu/src/shadow.rs,find_existing_batchis inserted betweenfor_view's doc comment andfor_view.for_viewloses its documentation, and the new function inherits it.find_existing_batch/PreparedBuffer). Third, parallel descriptor creation. Each part can then be measured and reviewed on its own, especially the held-madelock order and the concurrentset_stride/set_mark/note_addresswrites.mainagainst this PR alone in the live case.CI:
checkpassing.Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by MeMako on 2026-11-26T09:57:10Z.
Updated
#3055per the triage review and@fuzeofficlal's notes, and marked it Draft while splitting it into three reviewable PRs: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 #3139 lands.#3055now holds Part 3 cleanly stacked on top.crate::texture::in_parallel): Replaced all customstd::thread::scope/scope.spawnloops in page probing, Phase 1 diff, and Phase 3 descriptor creation withcrate::texture::in_parallel(max 4 workers).shadow.rsdoc comment: Movedfind_existing_batchafterfor_viewwith its own doc comment sofor_viewkeeps its documentation.@fuzeofficlal's three notes (past_cap.max(last - 1), Phase 3VIEWSgeneration re-check undermade -> VIEWS, and non-#[ignore]WHILE_PREPARINGretry testsync_ranges_retries_slot_rewritten_while_preparing), and included@passerbyyhx'sMeMako/VIctory#1commit (566a27a) covering both the 16-page and 32,768-slot parallel thresholds.main(63bcca4) vs. Part 3 (566a27a) numbers only (no mixed#2940figures). A standalone livemainvs. 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.
Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-27T12:31:12Z.
Contention numbers for the
SHADER_HEAPSwait in #3425, this PR (566a27a) against its base63bcca4:sync_heaps, RTX, 12 runsIt was faster in all four setups. No views were lost or stale. The full
victory-gpusuite on RTX fails only the two tests thatmainalso fails here (dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views).The lock is still held for the whole pass, so title threads still wait for the entire (shorter) sync. The per-write p99 is 7.8 ms, against 9.7 ms on the base.
Bench:
sync_contention_bench(#[ignore]d; commitf34bcfb8, cherry-picks onto any tree). A placed, write-watched view heap of 2^20 slots (the host heap caps it at 934,464 here). Each frame, one thread rewrites 200 random 64 KiB pages (50 changed records each) and runs onesync_heaps, while 5 title threads write a placed-buffer SRV every 20 µs into pages of their own. "Wait" is the title threads' time in view writes beyond the uncontended 2–3 µs, summed per frame. Release build, RTX 4060 Laptop, runs interleaved with the base, medians of 30 frames per run. The bench also checks that every view written before a sync is the slot's view address after it, and that every slot is right after a final quiet sync.AI use: Claude Code (Claude Opus 5.5).
Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-27T13:06:20Z.
Follow-up to my contention numbers above, this time in the
sync_slotsshape #3425 reports (18.1 ms/frame for ~384k slots).Bench:
sync_slots_benchonbench/sync-slots(f233390e, on top off34bcfb8). Each frame, 192 random 64 KiB pages (393,216 slots, the "384k slots" of #3425) of a 2^20-slot placed, write-watched view heap are written. 0, ~9,500 or ~85,000 records among them change, and onesync_heapsruns. Each shape runs without and with 5 title threads writing a view every 20 µs. It checks every changed slot's view address after each sync, and every slot at the end. RTX 4060 Laptop. Base63bcca4and the PRs ran interleaved (7 runs for the ~9.5k row, 3 for the others). Ratios are per-run against base. #3265, which doesn't touch the sync, gave ×0.81-1.04 in the same runs, so read anything within ~±15% as noise.sync_heapsThis is the only one of the open heap-sync PRs that's faster in every shape here. That covers #3139, #2971 and #3265 (control); #3356 was slower in the earlier contention runs. 0 lost and 0 stale slots in every run.
Where base's time goes, from the same bench with
SYNC_SLOTS_DIRECT=1(callingsync_slotsper page, without the write-watch walk):mirroris ~1 µs on its own (9.5k changed: 10-12 ms).So the in-game 18.1 ms is mostly
mirrorof changed slots, not the scan. Your batched buffer lookup and parallel make are aimed at the right part.AI use: Claude Code (Claude Opus 5.5).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.