gpu: shadow: remember up to 16 whole-taken views under one lock, index carded copies, and cut per-refresh allocations #368
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!368
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-368"
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?
@lucamzanon on GitHub · 2026-12-03 20:45 UTC · original
lucamzanon:xmem/7-shadow-copy→main· +1227 −203 · 1 files · 3 commits · openSummary
Eliminates massive redundant per-frame CPU→GPU buffer re-copies and lock/allocation overhead in
crates/victory-gpu/src/shadow.rs(the only file touched).Root Causes & Changes
5b96671— Remember up to 16 whole-taken views under one lock (Taken/Whole):rangedremembered only 2 whole-taken ranges (last + longest). Any buffer read through ≥3 views (e.g., the big heap copy refreshed in 14 ranges/frame) re-copied the unaligned end pages of views 3..14 every single frame, even when unmodified.copied+ranged, previously taken 2–3× per refresh) into a singleTakenlock, and reuses thread-local scratch buffers incopy_stale.8e1368c— Address-indexedCARDEDcopies, enumJob, batchedtake, cachedpeek:send_copiedpreviously allocated aVec<Arc>of every carded copy (Weak::upgrade= 2 atomic RMWs each) on everyStep::Copy. Now kept sorted by start address with prefix-maxreachfor O(log N) lookup + short walk, copying outside the table lock.wide_each/widereplaced ~18 heap allocations per call with an enumJob(zero per-thread boxing).takebatches per-page runs/dirty-lock/counters into thread-local scratch + 1 lock per taking.peekat record time now uses and populates the per-threadFOUNDcache.Verification & Moving A/B/B/A Benchmarks (RX 9070 XT 16 GB / Ryzen 7 5700X, Linux RADV)
shadow::tests pass (11 new, including randomized model-check ofWhole, eviction,CARDEDtable vs oracle, 4-view write-watch, and >16-view stress) + 2/2host::refresh_tests.cargo check,cargo test --no-run(x86_64-pc-windows-msvc), andclippy --no-deps -p victory-gpu(0 lints inshadow.rs) clean.VICTORY_BENCH=rt, 2560x1440 → 4K FSR 4 Quality, no Frame Generation, repo-built Mesa 26.3.1 RADV + vkd3d-proton, 94,199 warm pipelines, interleavedA -> B -> B -> Awith deterministic moving gameplay (diag+VICTORY_KEYS_FILE).In-Game Moving A/B/B/A Results
main(842767a)3m x 4, moving)24.50 fps(24.3/24.7), 1% low11.8529.95 fps(29.5/30.4), 1% low14.75+22.24%avg /+24.47%1% low4m x 4, moving)27.40 fps(26.8/28.0), 1% low9.9032.50 fps(32.6/32.4), 1% low10.45+18.61%avg /+5.56%1% lowHUD Telemetry Snapshot (Matched Boat Scene, ~2,540 draws/frame)
MB/f up):5.2 MB/f→0.6 MB/f(-88% redundant upload traffic)2.3 ms/f→1.7 ms/f(Prison:8.3 ms/f→3.2 ms/f, Transfers29.2→5.3 MB/f)34.5 ms→30.4 ms(Hitches: 2→0)@Kal0s-design on GitHub · 2026-12-04 03:10 UTC · original
Tested
8e1368cnatively on Windows 11 (MSVC), real D3D12 with one NVIDIA desktop card plus WARP, against the merge base842767a.I think there's a regression in
Shadow::takefrom the second commit.sendsnow collects(offset, len)but
card_changed_runstakes(from, to)and drops anything wherefrom >= to. Past the first page of a copy almost every run gets dropped, and in the first page the range marked is[offset, len)instead of[offset, offset + len). The CPU side gets the new bytes but they never go to the card side. This only hits copies withchanges_onlyset, so theby_addressones (directory, arena, streamed structures in memory.rs) and anything afterkeep_stores. The PR's own tests don't reach it becauseShadow::unbackedleaveschanges_onlyoff, soviews_testsgo throughcopy().A small local test (not pushed) shows it. It makes an unbacked copy, calls
keep_stores(), thentake(start + 2*PAGE + 100, start + 2*PAGE + 300, false)and looks atcard.dirty.[]where[(2*PAGE+100, 2*PAGE+300)]is expected(off, off + len)in both pushes it passes on this PR too, including a second check with one changed dword andknown = trueThe fix is just those two lines. I'd also suggest adding a
keep_storesvariant of the views test so this path is covered. Since part of the drop in MB/f up might come from these sends being lost, it would be worth measuring again after the fix.The rest looked fine to me.
Wholekeeps the same meaning the two-slotrangedhad. Nothing resets it outsidecopy_staleandnothing_new, same as before, so reused addresses behave as on main (not better, not worse). Eviction only costs recopies, never stale data, and an out-of-ordernowonly makes it more conservative.CARDEDordering withreachas a prefix max is correct, dead entries only make the walk a bit longer, andsend_copieddrops the table lock before copying like before. No new lock nesting that I could find.peekthroughFOUNDuses the same remember-under-read-lock andBUFFERS_CHANGEDcheck aslookup.Tests
shadow::5 runs each, 22/22 on the PR and 11/11 on basevictory-gpu --libwith--test-threads=4, two runs each, 320 pass on the PR and 309 on base, with the same 5 failures on both (the twoindirect::host_list_*, the twopipeline::hostlibrary loads, and the flakywatch::tests::the_rest_of_the_one_asking_has_a_number_of_its_own). Nothing newI didn't run any game here, so I can't say anything about the fps numbers.
AI-assisted: yes
@lucamzanon on GitHub · 2026-12-04 04:45 UTC · original
Great catch @Kal0s-design — thank you!
Fixed both lines in
30150dd((at - start, at - start + n)and(at - start + a, at - start + a + len)) and addedtake_marks_dirty_end_offsets_after_keep_storescovering both the whole-page and changed-dword paths.Re-ran the full moving A/B/B/A (
VICTORY_BENCH=rt, 1440p → 4K FSR 4 Quality, no FG, RX 9070 XT / Ryzen 7 5700X) on30150ddvs842767a— you were spot on that the huge+18%/+22%earlier was mostly from the droppedchanges_onlysends:842767a(A)30150dd(B)3m x 4, moving)24.90 fps(25.2/24.6), 1% low11.5025.85 fps(24.8/26.9), 1% low11.30+3.82%avg /-1.74%1% low4m x 4, moving)27.70 fps(28.2/27.2), 1% low10.0027.65 fps(27.4/27.9), 1% low9.90-0.18%avg /-1.00%1% lowZero crashes on all 8 runs. At 4K RT (heavily GPU-bound on RX 9070 XT), Prison still shows a real
+3.8%uplift while Boat is neutral.@Luxuride on GitHub · 2026-12-04 12:20 UTC · original
The only PR in this batch with both a measured win and a model-checked test suite — worth the rebase.
What I verified:
taken → card.dirty(crates/victory-gpu/src/shadow.rs:2149) andCARDED → card.dirty(shadow.rs:482-507),card_dirtynever takestakenwhile holdingcard.dirty, andsend_copiedcopies outside theCARDEDlock viaSENDING. No cycle, no defect found.Job::Eachwith an atomic cursor and aLeftcount/Condvar, splitting long runs into 256 KiB parts throughwide_each, is a good way to keep the wide path without per-item boxes.Two things to fix before it goes in:
(at - start, n)into a dirty-range list that expects an end — at both the whole-page and the dword-run paths — and the regression test only covers those two.(offset, end)pairs cross four layers inshadow.rs; a newtype or an assertion on the invariant is what stops the next one. Your own history is the argument.Wholeeviction is unconditional.WHOLE_MOST = 16withinsertdropping subsumed ranges and evicting the smallest: a title cycling through 17+ whole-taken views per frame silently goes back to over-copying. That is an acceptable answer, but it should be written down where the constant is, not discovered.Also: 1210 lines in one file, three commits that each say something different — ask the maintainer to land them as-is rather than squashed.
173 behind, no conflict.
30150dd9bbto1d068d7422@lucamzanon on GitHub · 2026-12-04 12:57 UTC · original
Thanks @Luxuride! Rebased onto
main(0fd7614, keeping all 3 commits separate as suggested) and updated commit 3 (1d068d7) with both items:debug_assert!(from <= to && to <= len)insidecard_changed_runsso any(offset, len)mistake or out-of-bounds range panics immediately in debug/tests instead of being silently dropped.WHOLE_MOST = 16.Re-verified 23/23
shadow::tests under Wine (debug build, with the newdebug_assert!active) and 0 clippy warnings inshadow.rs.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.