Repository navigation
Conversation
…fer from the pool Two pieces of per-range work in the copy-back path (Marvisak#400: 54-110 ms a frame) scale with what a submission declares. `execute` collects the ranges shaders wrote and skipped a range already collected by scanning the vector with `Arc::ptr_eq`: the world's passes declare the same big ranges dozens of times, so the vector grows to hundreds while the steps number in the thousands. Key a set by (pointer, address, length) - exactly what the scan compared - and keep the order. `Shadow::copy_back` allocated `vec![0u8; 64 KiB]` per call for bytes that `read_into` overwrites before they are read, so the allocation and the memset were pure cost. Take the piece from the pool the file already uses for the snapshot buffers, and give it back - the same reasoning as "fresh snapshots cost ~2.3 million page faults per world frame". Measured without the title: the scan costs 0.91 ms against 0.21 ms for 33k steps over 250 ranges, and 3.18 ms against 0.24 ms at 1000 ranges (best of 7; the same comparison as `refresh_merge_scan_vs_index` in Marvisak#1556, same machine). cargo test --workspace --locked; both clippy configurations with -D warnings; cargo xtask policy.
… 64 KiB piece `copy_back` merges a 64 KiB piece of title memory with the copy and wrote the whole piece back whenever any byte of it had been taken from the GPU: a shader writing one dword made the layer write 64 KiB into title memory and touch 64 KiB of its pages. Track the first and last byte the title actually takes and write that span, and mark the same span in the watch. New synthetic harness (`shadow::cost::shadow_cost`, no title needed): a real `Shadow` over a real host copy and a synthetic title range, 640 bytes written into every 64 KiB piece as a shader would: range written before -> after copy-back before -> after 4 MiB 4.19 MB -> 41 KB 2.07 ms -> 1.48 ms (0.71x) 16 MiB 16.8 MB -> 164 KB 10.14 ms -> 8.74 ms (0.86x) 64 MiB 67.1 MB -> 655 KB 75.68 ms -> 69.23 ms (0.92x) Three orders of magnitude less written, 8-29% less time: the rest of a piece is the kernel read of title memory and the merge over it, not the write - which is what says GPU-SNAPSHOT-COST is about reads. Run it with `cargo test --release -p victory-gpu -- --ignored --nocapture shadow_cost`. cargo test --workspace --locked; both clippy configurations with -D warnings; cargo xtask policy.
`pooled()` returns the vector cleared, so the 64 KiB piece taken from it in
the previous commit had length 0 and `piece[..n]` panicked on the first
copy-back of a changed piece:
range end index 24512 out of range for slice of length 0
Nothing in the workspace tests covers `copy_back`, so the new
`shadow::cost::shadow_cost` harness caught it. Size it once per call. The
pool saves the allocation; the zeroing stays, and the description of the
earlier commit says so.
The ranges a submission wrote are independent - different buffers, or disjoint ranges of one - and `copy_back` is kernel reads and writes of title memory, which one thread does at a fifth of what the machine can. It is the term Marvisak#400 measures at 54-110 ms a frame. Spread them over the machine's cores, at most eight, with `std::thread::scope` over chunks of the collected list. `VICTORY_COPY_THREADS` overrides the count, so a real run can A/B the frame line with it at 1. New `shadow::parallel_cost::shadow_parallel`, 16 ranges of 4 MiB with 640 changed bytes per 64 KiB piece, synthetic memory and real host copies: one thread 41.4 ms eight threads 12.4 ms 3.35x The bytes written back are identical either way (655 360). cargo test --workspace --locked; both clippy configurations with -D warnings; cargo xtask policy.
|
A data point for the size of this term on NVIDIA: on an RTX 3080 in the Series S profile (1280x720 internal), "copying back" was ~330 ms per world frame for ~12 MB actually written back (main + #189 #89 #211 #282 #493 #530 #587). So most of the cost is not the bytes returned. It is the work around them: one AI-assisted (Claude Code) |
`Batch::flush` copies each range a list reads from title memory before the
lists run, one buffer after another. The batch holds one entry per buffer,
so the ranges are independent, and the copy is a kernel read of title
memory that one thread does at a fraction of what the machine can.
Spread them the way the copy-back already is, over the same
`VICTORY_COPY_THREADS` count (1 for an A/B of the frame line).
New `shadow::flush_cost::shadow_flush`, 16 ranges of 4 MiB with no
write-watch, so every range is copied whole - the worst case:
one thread 10.5 ms
eight threads 5.7 ms 1.85x
Less than the copy-back's 3.35x: this path is already at memcpy speed, so
it runs into memory bandwidth rather than idling.
cargo test --workspace --locked; both clippy configurations with
-D warnings; cargo xtask policy.
|
Closing this rather than rebasing it: What was unique here is preserved in the measurements, and is worth a decision from you before anyone writes code again:
Neither win is obviously available on the new design, and I do not want to claim it without measuring:
If you want any of those on the new layout, say which and I will write it against |
|
@HenriqueSBueno - 330 ms per world frame for 12 MB actually written back is the most useful number in this thread: it is ~27 ns per byte returned, which no amount of copying explains. It has to be the per-range work, exactly as you describe - the Two notes, since the PR is closed now (the path was redesigned on
|
catminou4
left a comment
There was a problem hiding this comment.
Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.
Copy-back ranges deduped in a set, copy spread over up to 8 threads (VICTORY_COPY_THREADS): copy_threads() caps by machine, and the shadow side records what one submission pays for shader-written ranges. 3 tests.
Two pieces of per-range work in the copy-back path scale with the number of things a submission declares: the ranges are deduplicated by scanning the list already collected, and each
copy_backallocates a fresh 64 KiB buffer for bytes it overwrites anyway. Both are in the term #400 measures at 54-110 ms a frame ("copying back"), and both are the same shape as the fix in #1556. ## 1. Deduplication by scanhost.rs, submission: everyStep::GpuWriteof every list is checked against what is already collected withrust !back.iter().any(|(s, a, l)| Arc::ptr_eq(s, shadow) && (*a, *l) == (*address, *len))The world's passes declare the same big ranges dozens of times, sobackgrows to hundreds of entries while the steps number in the thousands: a linear scan per step. AHashSet<(ptr, address, len)>makes it constant and keeps the order ofbackthe same. ## 2. A 64 KiB buffer per callshadow.rs::copy_backdoeslet mut title = vec![0u8; PIECE]withPIECE = 64 KiB, for every range it puts back. The bytes are then overwritten byread_intobefore they are read, so the zeroing is pure cost - and the file already has the pool it should come from (pooled/give_back, used bywritingfor the same reason: "fresh snapshots cost ~2.3 million page faults per world frame"). It now takes the piece from that pool and gives it back. ## Measured Without the title I can measure the shapes, not the frame: - The scan: 33 000 steps over 250 collected ranges costs 0.91 ms against 0.21 ms with a set (best of 7,refresh_merge_scan_vs_indexin #1556 measures the same comparison on the same machine); at 1 000 ranges, 3.18 ms against 0.24 ms. - The buffer:vec![0u8; 65536]is an allocation and a memset of 64 KiB per call;pooledis a mutex and aVecswap. The frame line'scopying backterm is 54-110 ms in #400, and it runs this once per range. Both are bounded by how many ranges a submission declares, which the diagnostics already print (N title copies,N refreshesper frame). ## Correctness - The set is keyed by exactly what the scan compared - theArcpointer, the address and the length - andbackkeeps its order. - The pooled buffer is only read afterread_intohas written it, and is returned to the pool after the loop;read_intofailing skips the piece as before.cargo test --workspace --locked, both clippy configurations with-D warnings,cargo xtask policy. No game data used. ## 3. The whole 64 KiB piece goes back for one changed bytecopy_backmerges a 64 KiB piece of title memory with the copy, and wrote the whole piece back whenever any byte of it had been taken from the GPU. A shader writing one dword made the layer write 64 KiB into title memory - and touch 64 KiB of its pages. It now tracks the first and last byte the title actually takes and writes that span (watch::writingmarks the same span, so our own write is not taken for the title's). ### Measured on a new synthetic harnessshadow::cost::shadow_cost(this PR) builds a realShadowover a real host copy and a synthetic title range, takes the snapshot, writes 640 bytes into every 64 KiB piece as a shader would, and times the copy-back: | range | written back before | after | copy-back before | after | |---:|---:|---:|---:|---:| | 4 MiB | 4.19 MB | 41 KB | 2.07 ms | 1.48 ms (0.71x) | | 16 MiB | 16.8 MB | 164 KB | 10.14 ms | 8.74 ms (0.86x) | | 64 MiB | 67.1 MB | 655 KB | 75.68 ms | 69.23 ms (0.92x) | Three orders of magnitude less written; 8-29% less time. The time does not fall further because the rest of the piece is the kernel read of title memory and the merge over it, not the write - which is what tells the next person thatGPU-SNAPSHOT-COSTis about the reads, not the writes. The harness needs no title:cargo test --release -p victory-gpu -- --ignored --nocapture shadow_cost. In-title numbers are not mine to give: I have no runnable copy yet, so whoever has one should run it there. What the harness does show is the per-byte cost of each part, which is what a frame has to be divided by. ## 4. The pool returned an empty buffer - caught by the harness The first commit took the 64 KiB piece frompooled(). That function returns the vector cleared (len == 0), andcopy_backslicespiece[..n]out of it, so the first copy-back of any changed piece would have panicked:range end index 24512 out of range for slice of length 0The workspace tests do not covercopy_back, so nothing caught it untilshadow_costdid - which is the argument for the harness as much as for the fix. It ispiece.resize(PIECE, 0)now: the pool saves the allocation, not the zeroing, and the section above says so. ## 5. Copy-back on several threads The ranges a submission wrote are independent - different buffers, or disjoint ranges of one - so they are copied back in parallel:std::thread::scopeover chunks of the collectedbacklist, the machine's cores up to eight.VICTORY_COPY_THREADS=1keeps it on one thread, so a real run can A/B the frame line.shadow_parallel, 16 ranges ╨УтАФ 4 MiB with 640 changed bytes per 64 KiB piece: | | time | | |---|---:|---| | one thread | 41.4 ms | | | eight threads | 12.4 ms | 3.35x | The bytes written back are identical (655 360either way). This is the term #400 measures at 54-110 ms a frame, so a 3.35x on it is 35-70 ms of a frame that is still dominated by the copies themselves. Run it withcargo test --release -p victory-gpu -- --ignored --nocapture shadow_parallel.6. The pre-submission copies on several threads
Batch::flushbrings every range a list reads up to date before the lists run, one buffer after another. The batch holds one entry per buffer, so the ranges are independent, and the copy is a kernel read of title memory.shadow_flush, 16 ranges Г— 4 MiB of synthetic memory with no write-watch, so every range is copied whole - the worst case:Less than the copy-back's 3.35x because this path is already at memcpy speed, so it runs into memory bandwidth instead of idling.
The caveat decides whether this shows in a frame: in the world most of
flushis the write-watch askings, andwatch::writesholds one global mutex acrossGetWriteWatch, so those stay serial however many threads there are.#776is the change that cuts the askings; this one spreads the copies behind them.VICTORY_COPY_THREADS=1turns both off, which is the A/B someone with a copy can run against the frame line.cargo test --release -p victory-gpu -- --ignored --nocapture shadow_flush