Repository navigation
Conversation
|
On the retail title the batch does not move it, and the time is in view creation, not the scan Same setup as my note on #2821 (Windows 11, RX 7900 XTX, 300 s world runs, same save, same cache).
-8% on the sync, not the 2.43x the scan benchmark suggests: that gain needs many unchanged slots to skip, and in the world nearly every slot looked at has changed, so there is little to skip. The frame coming out lower I put down to scene spread in a single run. What the sync is doing, from counters I added to
So the cost is the ~47k For scale, deferring the read-only ones (constant buffers, buffer shader resources, non-UAV memory views) to the submission that first reads them took the sync from 261 ms to 126-151 ms and the frame from 1,025 ms to 859-880 ms at the same draw count. That prototype is not correct yet: a skipped slot keeps its previous descriptor, and constant-buffer slots have no read mark to bring them back (the tree marks texture reads; a local change also marks buffer loads; CBVs are the gap). Suggestion: a "changed vs unchanged" line in the benchmark would predict which titles gain from the batch. Written with an AI assistant. |
|
Thanks for running this on a real world trace and breaking down the A few notes on those numbers:
Comment drafted with Antigravity. |
Dukestar69
left a comment
There was a problem hiding this comment.
Verified on RTX 5090 against origin/main (63bcca4):
cargo test -p victory-gpu --lib descriptor:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released: 22/22 passed (includingsync_ranges_merges_overlapping_and_out_of_order_ranges).cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.
Cleanly batches dirty heap page ranges under one diff pass in sync_heaps.
Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
|
Follow-up: with the read marks in place, the deferral does not hold up When I wrote the note above I had only the upper bound: skipping read-only slots without making them later. With the translator now marking buffer and constant-buffer heap reads, the host makes a marked slot after the submission that read it, and the saving largely disappears:
-4%, not -25%. The work moves rather than disappearing: the frame needs ~32k distinct views either way, and deferring them only decides when they are made. One correction to my earlier note that may save someone time: a heap constant buffer is not a kind-13 handle. Shaders read one as a raw buffer view (kind 11, So for this PR's premise: batching helps only stale-heavy workloads (measured: not this one), and deferral helps only if the slots are never made at all (they are). What is left is the number and cost of the views a frame genuinely needs. Written with an AI assistant. |
|
Thanks for the follow-up test — that's a really useful result, and the raw-view (kind 11) note on heap CBVs is great to have documented too. Ruling out deferral actually lines up directly with why this work was split into three parts per triage feedback on Draft Comment drafted with Antigravity. |
…der one diff pass in sync_heaps
imjustprism
left a comment
There was a problem hiding this comment.
ran 24618c9 in the title and read the diff. rx 9070 xt, world, medians per frame:
| per frame | without, 5 runs | with this pr, 1 run |
|---|---|---|
heap sync (sync_heaps), ms |
10.6 to 14.3 | 12.1 |
| slots scanned | 308,000 to 369,000 | 333,000 |
63bcca4 plus #2122 (this card needs it) plus a local timer around sync_heaps. two of the five runs without are plain; the other three have #2051 merged in, which does not touch this path. diag build, series-x, the same scripted key presses, warm shader and pipeline caches, quiet machine, 12-minute runs, medians over 47 to 74 world frame lines per run. no errors and no device removed. one run with the pr, so all i can say is: no change i can measure here. that fits your own table: at 1.48 ms per 700,000 unchanged slots on main, the scan is under 1 ms of the 12 ms here (an estimate from your figure, not a measurement), so the batch can save a few tenths of a millisecond at most. the rest is making the slots that changed, as the comments above found.
what i checked from the description:
sync_ranges_merges_overlapping_and_out_of_order_rangesand the otherdescriptor::hosttests pass here.- "zero thread changes": yes, nothing starts a thread.
- the benchmark table: the benchmark is not in the diff, so i could not run it.
- "zero lock-scope changes": the code does change it, see the first point.
points from reading the diff:
- lock scope,
descriptor/host.rs:731and:732. on main each 64 KiB page read its records with no lock, then tookVIEWSalone, let it go, and tookmadealone. nowmadeandVIEWSare held together across every range of the heap, with the reads of title memory inside (host.rs:737). the order is the onemirroralready uses (host.rs:790), so nothing can deadlock. but a title thread that records a view (descriptor.rs:504) or releases a resource (descriptor.rs:535) takesVIEWSwithoutSHADER_HEAPS: on main it could get in between two pages, now it waits for the whole heap. the hold is short, about your scan time, and i did not measure the wait. either the description and the doc onsync_rangesshould say so (the doc also says it skipsVIEWSfor empty slots, whileVIEWSstays locked and only the lookup is skipped), or the records can be read into a buffer before the locks are taken, which keeps the old scope. - a question on the probe,
descriptor.rs:189. each page is probed with a 4-byte read and its records are read later with plain reads. on main the read came right after the probe of that page. now it comes after all pages are probed, two sorts, and the waits for both locks. the comment there says the title unmaps heap pages; if it can do that while another thread submits, the window for a fault is wider than it was. the race itself is older than this pr. do you know whether that can happen? reading each range withread_intobefore the locks would close it, and it is the same change as above. - the new test passes with the sort and the merge taken out of
sync_ranges(i ran it that way: 1 passed), because a slot that is listed twice is mirrored twice with the same result. so it pins the result but not the merging. its doc says "adjacent", and no two of its ranges touch;(0, 16), (16, 32)would cover that. - the ranges are sorted and merged in
sync_heaps(descriptor.rs:202) and again insync_ranges(host.rs:722), afterTracked::takealready sorted its runs (watch.rs:464).sync_heapsis the only caller outside tests, so one of the two is enough.
nothing here is a wrong result: each slot's stale decision matches main, the clamp to count holds, and last - 1 runs only when last is above count.
Adapt the readable-range diff from Marvisak/VIctory PR Marvisak#3139 to the paged NVIDIA heap. Keep native SRV creation, bindless refresh bookkeeping and retirement on the existing path. A shared registry guard allows recording during prepare, with exact tag, ID and generation checks before publication.
|
Thanks for the review and the 9070 XT run. Good catches all around, pushed fixes in
Re: the benchmark, Re-ran |
AndyLatimer
left a comment
There was a problem hiding this comment.
Tested f799496 locally against origin/main (63bcca4, clean merge) on Windows 11, NVIDIA GeForce RTX 3060 Ti, Rust 1.98.1 MSVC. Two bugs found and reproduced with synthetic tests:
-
sync_rangesdrops committed 4 KiB OS pages inside partially uncommitted 64 KiB blocks (crates/victory-gpu/src/descriptor/host.rs):sync_rangesreads each span once perwatch::PAGE(64 KiB) viaread_into. If a 64 KiB chunk contains a committed 4 KiB OS page followed by an uncommitted 4 KiB page,ReadProcessMemoryfails for the entire 64 KiB read and skips mirroring valid descriptors on the committed 4 KiB page.- Reproduced on
f799496withsync_ranges_mirrors_committed_4k_page_beside_uncommitted_page(1 committed 4 KiB page + uncommitted remainder in a 64 KiB reservation):sync_rangesreturned0scanned slots (expected128) and left slot 2 unmirrored. - Falling back to 4 KiB (
4096) chunks only when the 64 KiBread_intofails preserves the normal 64 KiB fast path while mirroring committed 4 KiB pages.
-
merge_rangesrecordspast_capon empty or inverted ranges (crates/victory-gpu/src/descriptor/host.rs):if last > capruns without checkingfirst < last, somerge_ranges(&[(70, 70), (80, 75)], 64)returns([], Some(74))instead of([], None)and triggers a spurious"title slot past the host heap"warning viaself.mirror(highest, ...).- (Also worth noting: probing only
at(highest)suppresses that diagnostic if a mapped past-cap range ends in an unmapped tail page.)
With the patch below applied on top of f799496:
- Both new regression tests (
sync_ranges_mirrors_committed_4k_page_beside_uncommitted_pageand themerge_rangesempty/inverted case) fail onf799496and pass with the patch. cargo test -p victory-gpu --lib --locked descriptor:: -- --skip dynamic_slots_are_held_until_released --test-threads=1: 24 passed, 0 failed (skipping only the pre-existing NVIDIA 1M heap-cap failure).cargo clippy -p victory-gpu --all-targets --locked -- -D warnings(default and--all-features) andrustfmt --edition 2024 --check: clean.
Patch and regression tests against f799496
diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs
index 6be649d..9801b0f 100644
--- a/crates/victory-gpu/src/descriptor/host.rs
+++ b/crates/victory-gpu/src/descriptor/host.rs
@@ -718,9 +718,46 @@ impl HostHeap {
}
let total_slots: usize = clamped.iter().map(|&(a, b)| (b - a) as usize).sum();
let page_size = crate::watch::PAGE as u64;
+ const OS_PAGE: u64 = 4096;
let mut page_buf: Vec<u8> = Vec::new();
let mut readable: Vec<(u32, u32)> = Vec::with_capacity(clamped.len());
let mut records: Vec<[u32; 3]> = Vec::with_capacity(total_slots);
+ let mut read_span = |a: u32, b: u32| -> bool {
+ let addr = at(a);
+ let n = (b - a) as usize;
+ let bytes = (n - 1) * step as usize + 12;
+ if page_buf.len() < bytes {
+ page_buf.resize(bytes, 0);
+ }
+ if !crate::host::read_into(addr as *const u8, &mut page_buf[..bytes]) {
+ return false;
+ }
+ readable.push((a, b));
+ for k in 0..n {
+ let off = k * step as usize;
+ records.push([
+ u32::from_ne_bytes([
+ page_buf[off],
+ page_buf[off + 1],
+ page_buf[off + 2],
+ page_buf[off + 3],
+ ]),
+ u32::from_ne_bytes([
+ page_buf[off + 4],
+ page_buf[off + 5],
+ page_buf[off + 6],
+ page_buf[off + 7],
+ ]),
+ u32::from_ne_bytes([
+ page_buf[off + 8],
+ page_buf[off + 9],
+ page_buf[off + 10],
+ page_buf[off + 11],
+ ]),
+ ]);
+ }
+ true
+ };
for &(first, last) in &clamped {
let mut i = first;
while i < last {
@@ -730,35 +767,17 @@ impl HostHeap {
// is not page-aligned and may straddle a page boundary).
let fit = ((next_page - addr) / step).max(1);
let j = last.min(i.saturating_add(fit as u32));
- let n = (j - i) as usize;
- let bytes = (n - 1) * step as usize + 12;
- if page_buf.len() < bytes {
- page_buf.resize(bytes, 0);
- }
- if crate::host::read_into(addr as *const u8, &mut page_buf[..bytes]) {
- readable.push((i, j));
- for k in 0..n {
- let off = k * step as usize;
- records.push([
- u32::from_ne_bytes([
- page_buf[off],
- page_buf[off + 1],
- page_buf[off + 2],
- page_buf[off + 3],
- ]),
- u32::from_ne_bytes([
- page_buf[off + 4],
- page_buf[off + 5],
- page_buf[off + 6],
- page_buf[off + 7],
- ]),
- u32::from_ne_bytes([
- page_buf[off + 8],
- page_buf[off + 9],
- page_buf[off + 10],
- page_buf[off + 11],
- ]),
- ]);
+ if !read_span(i, j) {
+ // A 64 KiB chunk may straddle committed and uncommitted 4 KiB
+ // OS pages; retry per 4 KiB page so committed slots survive.
+ let mut sub = i;
+ while sub < j {
+ let sub_addr = at(sub);
+ let next_os = (sub_addr & !(OS_PAGE - 1)) + OS_PAGE;
+ let sub_fit = ((next_os - sub_addr) / step).max(1);
+ let sub_end = j.min(sub.saturating_add(sub_fit as u32));
+ let _ = read_span(sub, sub_end);
+ sub = sub_end;
}
}
i = j;
@@ -1918,7 +1937,7 @@ fn merge_ranges(ranges: &[(u32, u32)], cap: u32) -> (Vec<(u32, u32)>, Option<u32
let mut past_cap: Option<u32> = None;
let mut raw: Vec<(u32, u32)> = Vec::with_capacity(ranges.len());
for &(first, last) in ranges {
- if last > cap {
+ if first < last && last > cap {
let high = last - 1;
past_cap = Some(past_cap.map_or(high, |m| m.max(high)));
}
@@ -2593,6 +2612,7 @@ mod tests {
(vec![(0, 64)], Some(67))
);
assert_eq!(merge_ranges(&[(70, 80), (10, 10)], 64), (vec![], Some(79)));
+ assert_eq!(merge_ranges(&[(70, 70), (80, 75)], 64), (vec![], None));
}
/// `sync_ranges` merges overlapping, adjacent, and out-of-order ranges
@@ -2654,4 +2674,53 @@ mod tests {
crate::shadow::unregister(pool, base);
crate::watch::tests::free(pool);
}
+
+ /// When a 64 KiB heap block has a committed 4 KiB page followed by an
+ /// uncommitted 4 KiB page, `sync_ranges` must still mirror the descriptors
+ /// on the committed 4 KiB page instead of dropping the whole 64 KiB chunk.
+ #[test]
+ fn sync_ranges_mirrors_committed_4k_page_beside_uncommitted_page() {
+ use windows_sys::Win32::System::Memory::{
+ MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE, VirtualAlloc, VirtualFree,
+ };
+ let Some(heap) = HostHeap::new(false, 256) else {
+ return;
+ };
+ assert!(heap.view_addresses().is_some());
+ let pool = crate::watch::tests::pool(1);
+ let base = pool as u64;
+ crate::shadow::register(pool, base, 64 << 10);
+
+ // SAFETY: reserve one 64 KiB page and commit only its first 4 KiB OS page.
+ let mem = unsafe {
+ VirtualAlloc(std::ptr::null(), crate::watch::PAGE, MEM_RESERVE, PAGE_READWRITE)
+ };
+ assert!(!mem.is_null());
+ // SAFETY: committing the first 4 KiB page inside our reservation.
+ let committed = unsafe { VirtualAlloc(mem, 4096, MEM_COMMIT, PAGE_READWRITE) };
+ assert_eq!(committed, mem);
+
+ let step = 32u64;
+ let start = mem as u64;
+ let mut resource = [0u32; 14];
+ (resource[0], resource[4]) = (1, 64);
+ let mut view = [0u32; 16];
+ (view[1], view[2], view[6], view[7]) =
+ (1, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, 16, 4);
+ (view[10], view[11]) = (base as u32, (base >> 32) as u32);
+ // Slot 2 lies in the committed first 4 KiB page; slots 128..256 lie in
+ // the uncommitted second 4 KiB page of the same 64 KiB watch::PAGE.
+ crate::descriptor::create_placed_srv(std::ptr::null(), &resource, &view, start + 2 * step);
+
+ let synced = heap.sync_ranges(&[(0, 256)], start, step);
+ let got = heap.address_at(2);
+
+ // SAFETY: releasing our reservation.
+ unsafe { VirtualFree(mem, 0, MEM_RELEASE) };
+ crate::shadow::unregister(pool, base);
+ crate::watch::tests::free(pool);
+
+ assert_eq!(synced, 128, "committed 4 KiB page slots should be scanned");
+ assert_eq!(got, Some(base), "descriptor on committed 4 KiB page must be mirrored");
+ }
}Synthetic tests only; AI-assisted review.
|
Thanks for catching both edge cases and providing the repros! Pushed fixes in
Verified with |
Part 1 of the triage-requested 3-part split of #3055: batches dirty 64 KiB heap page ranges, pre-reads mapped pages via
read_intointo a reusable buffer before locking, and diffs all dirty slot ranges under oneself.made->VIEWSlock with zero thread changes. Part 2 (batched buffer lookup viafind_existing_batch/PreparedBuffer) will be opened once Part 1 lands, and #3055 stays as a Draft for Part 3 (parallel sync viacrate::texture::in_parallel).Problem
In
descriptor::sync_heaps(), each dirty 64 KiB heap page callsHostHeap::sync_slotsindividually. Every page allocates three temporaryVecs (records,current,stale), locksVIEWSandself.madeseparately, and checksVIEWSeven for empty unchanged slots (tag != VIEW_TAG && made == 0).Fix
crates/victory-gpu/src/descriptor.rs:sync_heaps()and passes them in one call toHostHeap::sync_ranges, adding the returned readable slot count toSyncSlotsunderdiagnostics.views_current(&[[u32; 3]]) -> Vec<bool>withwith_views(|views| ...), avoiding the per-pageVec<bool>allocation.crates/victory-gpu/src/descriptor/host.rs:merge_ranges(ranges, cap)to clamp, sort, and merge overlapping/adjacent ranges once (preserving the capped-heap warning viapast_cap.max(last - 1)).HostHeap::sync_ranges(&self, ranges: &[(u32, u32)], start: u64, step: u64) -> u64(keepingsync_slotsunder#[cfg(test)]as a 1-line wrapper).crate::host::read_intobefore taking any locks (closing the probe-to-read concurrent-unmap window and removingunsafereads fromsync_ranges), then holdsself.made->VIEWS(same lock order asmirror) across the pure in-memory diff pass while skipping theVIEWSlookup for slots withtag != VIEW_TAG.HostHeap::mirrorpath with no thread changes.merge_ranges_sorts_clamps_and_merges_adjacent_and_overlappingandsync_ranges_merges_overlapping_and_out_of_order_ranges.Verification & 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 -p victory-gpu --lib --locked -- --test-threads=1: 233 passed, 0 failed, 1 ignored (includingmerge_ranges_sorts_clamps_and_merges_adjacent_and_overlappingandsync_ranges_merges_overlapping_and_out_of_order_ranges).cargo run --locked -p xtask -- policy:asset policy: 191 files ok.Standalone benchmark (
--release,world_frame_sync_heapson Draft #3055, 700,416 slots across 342 dirty 64 KiB pages, 9-round medians,mainvs. Part 1 alone):main(63bcca4) median24618c9) median0changed / 700k scanned)6,000changed / 700k scanned)14,000changed / 700k scanned)Hardware runs and tests were performed by me on my 7950X / RX 9070 XT; PR and code drafted with Antigravity.