gpu/shadow: O(dirty) CopiedPages bitmask, lock-free mirrors & AVX2 merge/diff (2/4 of #51) #57
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/57"
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 PastiiJay/klbf-lab. Originally posted by Vladimir-Human on 2026-11-26T00:39:55Z.
Split 2/4 of #51 cleanly on top of
beta(independent of #56, Part 3 ReBAR, and Part 4 Lazy Copy-Back).Summary of Changes (
crates/victory-gpu/src/shadow.rs)CopiedPagesbitmask +SmallRunsinline coalescer: pairs per-page copied stamps with a 64-page-per-worduncopied_bitsbitmask and inline 8-entrySmallRunssoShadow::copy_staleskips clean spans 64 pages at a time, plans coalesced copy runs underself.copied, and releases the mutex before issuing memory copies.Shadow::has_pending(AtomicBool) andCard::{has_dirty, has_sent}(AtomicBools with Release/Acquire ordering incard_dirtyandcard_merge_relay) to avoid mutex traffic on read-only windows and clean carded copies.has_avx2()runtime dispatch forfirst_difference,last_difference,changed_runs,merge_piece,merge_sync_card, andmerge_back_piece, eliminatingback_piece's per-piecemaskallocation.WideDoneworker completion, panic propagation, and TLS scratch reuse: movescopy_wideandShadow::differingonto the kept worker pool withWideDone, adaptiveWIDE_MIN_SPLITbatching, nestedIN_WIDEreentrancy guard, andSCRATCH_TITLE/parts+restbuffer reuse.copied_and_simd_tests::{plan_stale_matches_oracle_across_boundaries_and_partial_init, simd_diff_and_merge_helpers_match_scalar_oracles, nested_wide_runs_without_deadlock, wide_propagates_worker_panic_without_deadlocking_pool}and#[ignore] bench_copied_and_simd.Verification (
VICTORY_D3D12_DEBUG=1)cargo test -p victory-gpu-> passedcargo test -p victory-gpu --all-features-> passedcargo run -p xtask -- policy --staged->asset policy: 1 files ok(shadow.rs= 222,729 B)Related: #51, #56
Imported from PastiiJay/klbf-lab. Originally posted by HandshakeHackin on 2026-11-26T02:53:02Z.
I tested
ea3f336on a Windows x64 runner that confirmed AVX2 was available. Across 22,380 placements of a changed byte range (including empty spans) in 31–97 byte buffers, the first/last differences and exact changed runs matched the expected values. With CPU edits at bytes 30, 31, 32, 63 and 64 where present, the copy-back merge also preserved those edits, returned the expected disjoint ranges, and left the host and title buffers byte-identical to the oracle. This exercises all-changed 32-byte blocks, ends of vectors and scalar tails using synthetic bytes. Test and logImported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-26T10:42:14Z.
Thanks for the 22k-placement check against the oracle, that's thorough. We'll test the AVX2 path in game.
Imported from PastiiJay/klbf-lab. Originally posted by Proudnoob4 on 2026-11-26T14:39:27Z.
Live in-game GTA 6 runtime oracle audit + 10-minute benchmark (
0mismatches,-65%noting the runs,-82%finding the pages)We ported this full shadow/watch optimization stack (
O(dirty)TOUCHED_CARDS, 64-page-per-wordCopiedPagesbitmask, lock-freehas_pending, directmerge_under_writes,FOUND_LOOSEthread-local cache, in-placejoined(), and AVX2first_difference/last_difference/merge_piece/merge_sync_card/changed_runs) into our RTX 4080 / i5-13600K (Windows 10) GTA 6 Series S build and verified it both for live correctness and steady-state performance:10-minute live GTA 6 runtime oracle audit (
31,249log lines, real gameplay frames):first_difference/last_difference/changed_runs/merge_piece/merge_sync_card,TOUCHED_CARDSdirty scan,CopiedPagesbitmask,FOUND_LOOSEcache, and in-placejoined()) to compare against the scalar / stock reference on live frames.0mismatches (ERROR=0) across the entire 10-minute live run.10-minute release benchmark (RTX 4080 16 GB, i5-13600K, Win10,
render_scale=0.6667,VICTORY_CARD_BUFFERS_MIN_KB=64,VICTORY_FETCH_CHECK=1,VICTORY_SKIP_WC_MIN_KB=1024, RT skipped):VICTORY_LATE_WAIT=2(212pages looked at,77,290changed runs,2.38 MB):finding the pages:~0.28 ms -> 0.05 ms(-82%)noting the runs:~3.7 ms -> 1.28 ms(-65%)comparing pages:1.93 mswall (1.65 msreading +6.71 mscomparing/putting in across worker threads)n=104, tail-40n=40):32.00 fpsmedian (31.50p10,32.60p90,32.70tail-40 max,33.20overall max;0tail frames>55 ms)4,505draws/frame,317pieces landed/frame (4.5 MBdirty 4 KiB pages,0.84 MBdwords)t=140..600s):0/230black frames,120/120clean consecutive burst frames (4 fpscontact sheets att=420–435sandt=520–535s).The AVX2 paths and
O(dirty)bookkeeping held up 100% clean in real GTA 6 gameplay!Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-26T14:49:11Z.
Thanks @Proudnoob4, a 10-minute live oracle with zero mismatches is the strongest check this one has had, and the drop in page-finding time is a nice bonus. If you have the setup ready, the same oracle-plus-benchmark on #59 (lazy copy-back) would be the most useful next run, since it touches the same shadow path.
Imported from PastiiJay/klbf-lab. Originally posted by Proudnoob4 on 2026-11-26T14:58:58Z.
@PastiiJay Sounds like a plan! We're running a 10-minute verification pass on our current batch (
fxceless#1184+#1274+#1276) right now, and right after that we'll port#59(VICTORY_LAZY_COPYBACK=1) onto our tree, run the live GTA 6 coherence/oracle + 10-minute benchmark on RTX 4080 / i5-13600K (Win10), and post the full measured results and visual check on#59.Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-26T15:18:56Z.
Sounds good, thanks. We will watch #59 for the results; a visual check of the same scenes with and without
VICTORY_LAZY_COPYBACK=1would be the part we care about most.Imported from PastiiJay/klbf-lab. Originally posted by Vladimir-Human on 2026-11-27T08:33:27Z.
Backported the concurrency hardening identified by
@efsebayernand@TaynanGT(TaynanGT/Vice-Tay#192,c289a95) in7399812:self.copiedacross copies inShadow::copy_stale: keeps thecopiedmutex locked fromplan_stalethroughself.copy(...)so concurrent refreshes cannot observe stampedat == nowpages before their bytes land or race on writingself.data.card_dirty: returns early when!c.has_dirtyinstead of locking and pruningc.senton clean copies.Verified locally on Windows (
VICTORY_D3D12_DEBUG=1):cargo clippy -p victory-gpu --all-targets&--features diagnostics: 0 warnings incrates/victory-gpu/src/shadow.rs(only pre-existing upstream warnings in untouched files)cargo test -p victory-gpu --lib shadow:: -- --test-threads=1:32 passed; 0 failed; 1 ignoredcargo test -p victory-gpu --features diagnostics --lib shadow:: -- --test-threads=1:32 passed; 0 failed; 1 ignoredImported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-27T09:52:38Z.
Thanks @Vladimir-Human, and credit to @efsebayern and @TaynanGT for the hardening. We'll review it with the next beta batch. Could you rebase on the current head if it no longer merges cleanly by then?
Imported from PastiiJay/klbf-lab. Originally posted by TaynanGT on 2026-11-27T09:58:21Z.
@PastiiJay
7399812also merges cleanly againstbetaat6cce57e. It keeps theCopiedPageslock through the actual copy, matching the guard that landed in Vice-Tay #192. That is the part to keep together if the next beta head needs a rebase.Imported from PastiiJay/klbf-lab. Originally posted by Vladimir-Human on 2026-11-27T10:44:17Z.
Thanks @PastiiJay and @TaynanGT! Confirmed
7399812merges cleanly against currentbeta(6cce57e), and I will rebase promptly (keeping theCopiedPageslock scope acrossself.copy(...)intact) whenever the next beta batch lands.Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-27T10:50:24Z.
Thanks @Vladimir-Human for the lock-scope note on
CopiedPages, that's exactly the part we're checking. Could you say which tests you ran aroundself.copy(...)on your side?Imported from PastiiJay/klbf-lab. Originally posted by TaynanGT on 2026-11-27T10:51:41Z.
@Vladimir-Human The AVX2 comparison against the scalar result is a useful regression to carry with the copy lock when the rebase is needed. That keeps the buffer-content check tied to the new head.
Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-27T10:52:04Z.
We'll take them onto the AMD line one at a time with an A/B bench. Thanks, the tests you added make that much easier.
Imported from PastiiJay/klbf-lab. Originally posted by TaynanGT on 2026-11-27T10:58:58Z.
@PastiiJay The scalar fixture covers both the AVX2 tails and CPU edits that occur before copy-back. It can travel with that piece of the AMD port, then the scene comparison can focus on timing.
Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-27T11:11:35Z.
checked it against our AMD line: our copy_stale already holds
copiedfor the whole copy and card_dirty has no dirty/sent split, so that race isn't there. thanks for pointing at it, it was worth the check.Imported from PastiiJay/klbf-lab. Originally posted by TaynanGT on 2026-11-27T11:16:51Z.
@PastiiJay The AMD branch already has the copy serialization that change was meant to protect. The scalar oracle can therefore check the AVX2 port against your current implementation.
Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-27T14:04:24Z.
Thanks @TaynanGT, good to know the scalar oracle can check the AVX2 port against what's already on the AMD branch. We'll use it as the reference when we port this.
Imported from PastiiJay/klbf-lab. Originally posted by TaynanGT on 2026-11-27T14:25:12Z.
@PastiiJay For the AVX2 port, run the scalar and vector paths over the same dirty-page map and compare both the merged bytes and the untouched tail. That’ll catch a boundary difference even if the whole-page cases pass.
Imported from PastiiJay/klbf-lab. Originally posted by PastiiJay on 2026-11-27T14:27:02Z.
Good tip, comparing the untouched tail too. We'll add that check when we port it.
Imported from PastiiJay/klbf-lab. Originally posted by HandshakeHackin on 2026-11-27T14:38:11Z.
I added the sparse-page case @TaynanGT suggested as a test-only commit on
7399812. It marks pages 0, 2 and 3 dirty, starts seven bytes into page 0, and stops 31, 32 or 33 bytes into page 3. It compares the full merged buffers against the scalar implementation and separately checks the prefix, clean page and unused suffix against their original bytes.The regression passed on a Windows x64 runner with AVX2. Deliberately dropping the scalar remainder in the AVX2 merge made the new test fail. Windows run, including the negative control
Following the suffix suggestion below, the updated test fills the suffix with a distinct byte in each input. The complete destination still matched the scalar result. Extending just the final merge range by one byte made the test fail, as did dropping the scalar remainder. Both negative controls on AVX2
Imported from PastiiJay/klbf-lab. Originally posted by TaynanGT on 2026-11-27T14:38:58Z.
@PastiiJay Give the unchanged suffix a different sentinel in each input, then compare the entire destination. That makes it easy to spot a vector write that crosses the dirty range.
Imported from PastiiJay/klbf-lab. Originally posted by Vladimir-Human on 2026-11-28T18:24:53Z.
Superseded by https://github.com/PastiiJay/klbf-lab/pull/184 cleanly ported onto
origin/stable-amd(75022ba).Imported from PastiiJay/klbf-lab. Originally posted by TaynanGT on 2026-11-29T11:25:40Z.
Thanks for carrying the AVX2 work forward to #184. The sparse-page and unaligned-tail negative controls from HandshakeHackin are particularly valuable for checking the port without relying on performance results alone.
Pull request closed