GPU: avoid quadratic shadow snapshotting in Shadow::writing #3318
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3318
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3318"
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?
@danielibabet on GitHub · 2026-11-26 17:28 UTC · original
danielibabet:pr/linear-shadow-snapshots→main· +123 −50 · 1 files · 2 commits · openSummary
Shadow::writinginsert only uncovered gaps viapartition_pointinstead of draining and re-copying already-snapshotted bytes on every overlappingGpuWrite(O(N^2)->O(N)per frame).pooled(PIECE)/give_backfor the title readback scratch buffer inShadow::copy_back_or_notand iterate across adjacent snapshot segments covering[from, to)without needing contiguous merged vectors.overlapping_snapshots_never_recopy_and_cross_segment_copy_backverifying disjoint gap insertion and multi-segment / cross-64 KiB writeback merging.Motivation & Measurements
With
VICTORY_GPU_TIMING=1on RTX 4070 (12 GB, i9-14900KF, Windows 11), frames with repeated overlapping UAV/GPU writes to large buffers (e.g. a 64 MiB buffer) were re-snapshotting 626–918 MB/frame inShadow::writing.preparingCPU time dropped from ~120–145 ms/frame down to 82.5–84.1 ms/frame (~40 ms/frame saved at 5,185 draws/frame).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:230 passed; 0 failed; 1 ignored; 2 filtered outcargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warningscargo run -p xtask --locked -- policy:asset policy: 191 files ok@AurumCarlos on GitHub · 2026-11-26 17:48 UTC · original · reviewed
Same problem, same function as #3211 (yumaste, open, approved), so the two will conflict; worth linking it and agreeing which one goes forward. On this version:
nis now also clipped toseg_end, so a 64 KiB piece that spans two snapshot segments (exactly what gap insertion produces) is compared in parts withn < PIECE, andcompared.insertnever runs for it. A later overlapping declared range then compares and writes back the same piece again. #3211 keeps the rule by assembling a fragmented piece's old bytes (changed_piece) before comparing.copy_backlands; it doesn't cover a second overlappingcopy_backon a piece split across segments, which is where this differs frommain.@Dukestar69 on GitHub · 2026-11-26 17:50 UTC · original · approved these changes
Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):
Shadow::writing(instead of draining and re-copying already-snapshotted segments) and iterating across segments incopy_back_or_notcleanly eliminates quadratic snapshot copies while preserving cross-segment copy-back semantics.cargo test -p victory-gpu --lib shadow:: -- --test-threads=1(RTX 5090): 11 passed, 0 failed (includingoverlapping_snapshots_never_recopy_and_cross_segment_copy_back).cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@oni-swr on GitHub · 2026-11-26 17:55 UTC · original
Independent verification of PR head
e6324bc9a0d41ca2975c190f4a2791689116d871against controlmain63bcca43c6a00a758510398fa43096ce7e4f7df0(merge base is main itself, so it merges cleanly).Environment: Windows 11 x64, CPU/WARP only (
VICTORY_WARP=1,VICTORY_D3D12_WARP=1, Windows SDK 10.0.26100.0 x64dxil.dllon PATH,-- --test-threads=1). No physical GPU was used and no game files were involved.-j 4, separate target dir.Diff read in full (
crates/victory-gpu/src/shadow.rsonly):writingnow walks the segment list and snapshots only the uncovered gaps instead of merging and re-copying overlapped segments.copy_backnow walks the segments across 64 KiB pieces, skips uncovered holes, and uses a pooled buffer that it gives back. A new test checks the segments stay sorted and disjoint, that total bytes equal the range (no re-copy), and that GPU writes merge without clobbering title writes. I found no bug. The diagnostics counters now record only newly copied bytes, which is a deliberate behaviour change. Note: the new test returns early when no host GPU copy is available, so on a machine without one it passes without exercising anything. It did run here on WARP.Results (PR head / main control):
cargo test -p victory-gpu --lib shadow: 12 passed, 0 failed, 0 ignored / 11 passed, 0 failed, 0 ignoredcargo test -p victory-gpu --lib: PR 231 passed, 1 failed, 1 ignored. Control 230 passed, 1 failed, 1 ignored.frame::tests::waits_end_on_ticks_and_count_frames(frame.rs:231). It fails on unmodified main in this environment, so it is not caused by this PR (it matches theTEST-FRAME-WAITSitem in #1633).cargo clippy -p victory-gpu --all-targets -- -D warnings: cleancargo run -p xtask -- policy:asset policy: 191 files okAI disclosure: this verification was run by Claude Code (Claude Sonnet 5.5).
@danielibabet on GitHub · 2026-11-26 18:19 UTC · original
Good catch @oni-swr! Updated \Shadow::copy_back_or_not\ so \compared.insert\ runs once per whole 64 KiB piece before walking the segments inside that piece, and extended \overlapping_snapshots_never_recopy_and_cross_segment_copy_back\ to verify a second overlapping \copy_back\ skips a split 64 KiB piece. Re-ran \ictory-gpu\ tests (230 passed, 0 failed; 2 skipped), strict clippy, and \xtask policy\ (191 files ok).
@borosf on GitHub · 2026-11-26 19:03 UTC · original
Independent local verification on Windows 11 x64, Intel Arc B580 12 GB (12 cores), at this PR's head
cb7347b:Result: victory-gpu lib: 2 known baseline failures only; the new shadow::tests::overlapping_snapshots_never_recopy_and_cross_segment_copy_back test passed.
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
@Desamodeth on GitHub · 2026-11-26 21:00 UTC · original · reviewed
On
cb7347b8the number of gaps matters even when the total snapshot is small. For 2,048 adjacent 32-byte writes in one shadow, the lastkeep_writesalone took 1.09–1.18 ms. It returned 2,048 buffers. Eachgive_backsorts and scans the growing pool under its mutex, and the next submission scans the pool again on checkout. With #3211 the same final call returned one capture buffer.CPU time for capture plus retirement, with the same pool kept warm across submissions:
63bcca43ebd01cddTo reproduce, seed each pool with two touched buffers the size of the full span. For each submission call
writing(base + i * width, width, false)in ascending order, thenkeep_writesfor each range in the same order. Time the final return separately. Replacingkeep_writeswithcopy_backon unchanged bytes preserved the small-range result (2.72–2.77 ms here, 1.35–1.40 ms on main, 0.77–0.86 ms on #3211). One 64 KiB declaration took about 1 µs across all three on the keep path.I’d favor #3211’s single capture buffer if these changes are consolidated. Both PRs deliver the large-range gain. The single buffer also avoids a separate pool operation per small gap.
Windows x64, Ryzen 7 9800X3D, rustc 1.98.1
-C opt-level=3. Isolated production snapshot, pending-set and pool code, one thread, synthetic host bytes. Medians of 11 interleaved warmed samples in each of two opposite-order runs. Checked captured contents, coverage and empty pending/snapshot state after retirement. The in-game distribution of range sizes and the effect on FPS were not measured. AI-assisted with Codex.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.