GPU: a refresh batch takes the written pages of all its copies in one round of the guard lock #3585
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3585"
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 Marvisak/VIctory. Originally posted by neonbyte1 on 2026-11-27T19:07:59Z.
What's wrong
A submission's refreshes are batched per copy (
Batch,crates/victory-gpu/src/host.rs:2399), butBatch::flush(host.rs:2440) then brings each copy up to date on its own:shadow.update(from, to - from)per copy (host.rs:2447) →Shadow::update_tracked(shadow.rs:607) →Tracked::take_split(watch.rs:478). Each take with a written page in its range takes the global guard lock (watch.rs:503), guards the pages' other mappings, makes the pages read-only and drops them from the copy's changed set.The copies only ever read pages recorded as written, so the volume is small (~130 MB a frame here). The cost is the lock: a world frame makes one round of
watch::GUARDSper copy with written pages, 3,229 rounds a frame in our runs. The title's write-fault handler and marked copy-back hold the same lock, so these rounds mostly wait: 7.8 ms a frame inchange checks: … waiting for the guard lock, on the submitting thread, beforeExecuteCommandLists.(The
copies per frameline'sMB checked— 6.3 GB a frame here — is the span asked, not bytes compared:take_splitlooks only at the copy's set of written pages in it.)The fix
Batch::flushworks in three phases:shadow::plan→Tracked::written): for each copy, the written pages in its asked range, read under the copy's own set lock only. A copy with nothing written there stops here. Memory that is not tracked is copied whole, as before.shadow::refresh_planned→watch::take_written): the whole pages of all copies, sorted and deduplicated, in one hold of the guard lock. Each page's other mappings are guarded once, consecutive pages across copies share oneVirtualProtect, then each copy's set drops the pages taken.Shadow::copy_taken, the former tail ofupdate_tracked): per copy, one copy per contiguous run, zeros for unmapped runs, untracked parts whole, snapshot sync where this submission's shaders write.Tracked::take_splitis nowwritten+take_writtenfor one tracker, andShadow::updateisplan+refresh_plannedfor one copy, so there is one take and one refresh path. No switch: the guarantee is the one a single take gives (below), the existing take tests now run throughtake_written, and the change was measured in the title behind a switch first.Why no write is missed. A page leaves a copy's set only under the guard lock, once it is guarded at every mapping (pages that can't be guarded stay changed and are still copied). Copies are read after the take. So a title write before the guard is in the copy; a write after it faults and marks the page again. Writers that don't fault (kernel reads, DirectStorage, remaps, copy-back) mark under the guard lock, so before the page leaves the set (it's in the copy) or after (it's changed again). A page written between the look-up and the take isn't in the plan and stays changed. A page only partly in the asked range is copied in part and stays changed. The window between take and copy is longer than before, but correctness never depended on its length.
The copies line gets
N guard-lock roundsnext to the guard-lock wait (C::TakeRounds, diagnostics only).Run splitting looks up unmapped pages by binary search, removing the O(n²)
unmapped.containsthat #3156 also fixes.Tests
CPU only,
watch.rs:a_batched_take_takes_the_written_pages_of_every_tracker:batched_refreshes_under_concurrent_writes_lose_no_write:taken = whole): both tests fail;GPU, skipped without a device:
host::batch::overlapping_copies_in_one_batch_each_take_the_written_pages(two overlapping copies in one batch, each gets its written pages; a later write is seen by both). The existinga_copy_is_refreshed_once_per_submission_by_the_pages_writtencovers one copy.In the title
RTX 5090, Linux, GE-Proton11-14 Wine with its vkd3d-proton. Stack as listed in #3425, plus overlap and our pending early-release/barrier changes. Prison yard, diag build, two adjacent on/off pairs of the same exe (the change behind a switch there), 420 s each:
Pictures unchanged.
Related
VirtualProtectoutside it. This PR cuts the number of rounds instead; they compose, with a textual overlap intake_split.mainby the dirty bit.None of them batches takes across copies.
Verification
cargo clippy --workspace --all-targets -- -D warnings, with and without--features diagnostics(x86_64-pc-windows-msvc): clean.cargo xtask policy:asset policy: 191 files ok.origin/main.cargo test -p victory-gpu --lib watch::under Wine, with and withoutdiagnostics: 17 passed. Also passes on the default parallel test threads: the round and protection counts the batch test checks are per thread.left: (1, 0)andpage … differs from its copy and is not changed); the second fails the concurrent one.--test-threads=1, filtershost::batch:: shadow:: watch:: host::title_copies:: --skip shadow::run_tests): 31 passed, 0 failed. That ishost::batch2 (both batch tests),host::title_copies2,shadow::tests9 plus 1 othershadow::test, andwatch::tests17.AI-assisted (Claude); measured on my machine, no game data shared.
Imported from Marvisak/VIctory. Originally posted by pickles976 on 2026-11-27T19:43:57Z.
Offscreen Wine/NVIDIA verification of exact production
af08b8b11f28adeb1e355620cfa7ea20751e8491. Only local test-only changes make bothhost::batchfixtures require a physical NVIDIA DXGI adapter and successful host-copy allocation/mapping instead of returning early.cargo test -p victory-gpu --features diagnostics --lib -- watch::tests:: host::batch:: --nocapture --test-threads=1: 19 passed, 0 failed/ignored, 0.78 s after a fresh intended-worktree 9.08 s build.Coverage distinction: these tests initialize the real RTX 4060 Laptop D3D12 device and inspect persistent host-buffer mappings. They do not dispatch a shader or submit a GPU command list. This is guarding/copy-consistency coverage, not a GPU-execution, loading frame-time, contention, peak-RAM, or title performance measurement. I have not reproduced the separate RTX 5090 frame-time numbers or performed a matched parent benchmark here.
Environment: Linux 6.17, 16 GiB RAM, RTX 4060 Laptop 8 GiB/NVIDIA 590.48.1, Wine 11, NVIDIA masking disabled; diagnostic-relaxed-vtable vkd3d
2576c0b4adfae4a+(not stock certification). Original scope/global safeguards retained. Evidence:.compat-runtime/logs/verify-pr3585-batched-refresh-20261127T194039Z/(console, compilation provenance, result and resource samples). No production edits, title launch, imagery or assets published.AI-assisted verification.
28 Nov CPU-only follow-through, same production
af08b8b11f28adeb1e355620cfa7ea20751e8491: a temporary release-mode benchmark compares 64 singletontake_splitcalls against onetake_writtenbatch on the same head, not a parent/head comparison. Each case tracks 64 MiB (64 trackers × 16 tracked 64-KiB pages), alternates order for nine measured pairs after a warm-up pair, and checks every source word plus complete copy ranges/clean state. Writes/faults and word verification are outside the timer.The median reductions are 14.6% / 8.6% in this uncontended synthetic workload; ranges overlap, especially with gaps. This supports the batching mechanism but does not predict a title-frame gain: our actual scene has overlapping watches/protection/fault-handler time, contention and different ranges. No GPU device was initialized or command dispatched in this new measurement. Fresh intended-tree compile36.84s; benchmark1/1 pass in0.92s. The temporary probe was archived then removed, leaving production unchanged.
Evidence
.compat-runtime/logs/verify-pr3585-protection-bench-20261128T074631Z/contains command/provenance, raw per-leg timings, measurement JSON and probe source. Command:cargo test --release -p victory-gpu --features diagnostics --lib watch::tests::lab_protection_batch_measurement -- --ignored --exact --nocapture --test-threads=1through the existing Windows-MSVC/Wine wrapper. Same Linux/Wine/16-GiB laptop environment; safety limits unchanged. Title promotion remains parked after a separately reported GPU timeout, not attributed to this unintegrated PR. AI-assisted measurement/report: omp, OpenAI GPT-6 Astra.Mixed-runtime integration, 28 November 08:03 UTC
Ported this production change onto the existing dirty local runtime (base
3ed7d725f6895fe1a235eb6fa8ff2485261ca151), preserving its in-place early return, pending before-images and #3343 guard-opening path. This is not an exact-head result. The localBatch::flushadaptation borrows the refresh vector during planning, then drains it after dropping the plan, retaining its allocation instead ofmem::takediscarding capacity. The diagnostic counter array derives its size from the last counter variant. No shader changes or safety-cutoff changes.watch::tests::: 19/19 pass, 0 ignored, 0.75 s after fresh intended-tree 7.42 s compilation. Includes batched concurrent writers/aliases, saved before-images, and existing guard-opening/decommitted-middle regressions. One fixture initializes the actual NVIDIA device: despite the evidence directory'scpulabel, this invocation was not strictly CPU-only.host::batch::: 2/2 pass, 0 ignored, 0.65 s after another fresh 7.42 s compilation. Both require physical NVIDIA identity and successful host-copy mapping.VICTORY_IN_PLACE=0; the overlapping copies each receive their changed pages and see subsequent writes. These fixtures allocate/map native resources and copy through CPU mappings; they do not execute command lists or dispatch shaders.Same Wine 11 / RTX 4060 Laptop / NVIDIA 590.48.1 / diagnostic-relaxed vkd3d
2576c0b4adfae4a+stack, not stock certification. Peak sampled GPU use 140/154 MiB; minimum available RAM 11067/11085 MiB. Evidence:verify-mixed3585-tracking-cpu-20261128T080113Z/andverify-mixed3585-mapped-batch-20261128T080318Z/(console.log,result.json).Commands used the existing Wine wrapper,
test -p victory-gpu --features diagnostics --lib watch::tests:: -- --nocapture --test-threads=1, then the same command withhost::batch::. These are narrow correctness gates, not an in-title speedup or asynchronous GPU-lifetime certification. No new release/title run: the previously reported Xid109 / pending-submission stall still blocks runtime promotion; this report does not attribute it to this PR.Imported from Marvisak/VIctory. Originally posted by neonbyte1 on 2026-11-27T19:45:12Z.
@pickles976 thanks for checking the exact head on a second NVIDIA card, and for spelling out the coverage boundary: right, these fixtures exercise the guard/copy consistency with a real device's host mappings, not GPU execution. The frame-time effect is only from our 5090 runs (two adjacent pairs) and is stated as such in the description.
AI-assisted (Claude); no game data shared.
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-27T19:56:57Z.
I checked the diagnostics overlap with #3594. The exact heads
af08b8band377cd2dconflict on the counter array length. Tested pair keepsTakeRoundsand the three copy-back counters, deriving the array length from the final counter.On Windows WARP the same two-frame synthetic window printed one guard-lock round per frame and copy-back averages of 1.0 eligible, 0.5 wrote bytes and 1.0 withheld. The next window printed zero for all four figures. The existing synthetic byte checks passed, and the diagnostics suite passed 249 project GPU tests plus the SDK identity check, with one ignored.
Strict GPU Clippy passed for default and all features. The asset policy check passed.
@neonbyte1 and the maintainers can use the tested conflict resolution if both changes are applied. Please retain Git authors on cherry-pick or rebase. If you fold my work into another commit, please include
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.Imported from Marvisak/VIctory. Originally posted by neonbyte1 on 2026-11-27T20:02:30Z.
@andrewpozdnakov7-jpg thanks, that saves a rebase. I'll keep #3585 independent of #3594 (it's a one-line counter overlap, not a behaviour one); whichever lands second takes your
3cdd467resolution, with your authorship kept. If #3594 merges first I'll rebase #3585 onto it the same way.AI-assisted (Claude); no game data shared.
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.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.