Skip to content

GPU/Shadow: advance saved before snapshot after merge_writeback in copy_back_or_not - #3031

Open
RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/shadow-copyback-stale-before
Open

RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/shadow-copyback-stale-before

Conversation

@RVZO6

@RVZO6 RVZO6 commented Nov 26, 2026

Copy link
Copy Markdown

In Shadow::copy_back_or_not() (crates/victory-gpu/src/shadow.rs:414-454), compared deduplicates only whole 64 KiB PIECE chunks (if n == PIECE && !compared.insert(...)). When two overlapping ranges in the same submission include a partial range (< 64 KiB) — e.g. [0, 0x1000) followed by [0, 0x2000) or [0x1000, 0x2000) followed by [0, PAGE) — the second copy_back() re-compares those bytes against the saved before snapshot segment.

Because merge_writeback(title, host, old) copies concurrent CPU writes from title into host without updating old (before), a subsequent overlapping copy_back() in the same submission sees host[i] != old[i] on those CPU-written bytes, mistakes the earlier CPU write for a GPU write, and clobbers newer CPU writes in title memory back to the stale value.

Update old.copy_from_slice(host) immediately after merge_writeback so already-merged bytes match the snapshot on subsequent overlapping copy-backs. Disjoint from #2914 (copy_back_marks) and #2848 (Back buffer pooling).

Verification

  • Added overlapping_copy_backs_do_not_clobber_newer_cpu_writes covering both partial_then_wider ([0, 0x1000) -> [0, 0x2000)) and partial_then_whole_page ([0x1000, 0x2000) -> [0, PAGE)) on D3D12 WARP SM 6.6.
  • Official Windows MSVC + WARP SM 6.6 CI (Rust 1.98.1, default + --tests + --features diagnostics clippy, xtask policy, full workspace tests): RVZO6/VIctory CI run #45748741503 (green, combined Wave 10 check on 63bcca4).
  • Not run: the title; any hardware GPU.

AI-assisted: T3 Code (Antigravity, gemini-4-neon).

…py_back_or_not

In Shadow::copy_back_or_not(), `compared` deduplicates only whole 64 KiB
PIECE chunks; partial ranges (< 64 KiB) or a partial range followed by an
overlapping wider or full-page range in the same submission re-inspect
bytes against the saved `before` snapshot segment.

Because `merge_writeback(title, host, old)` copies CPU writes from `title`
into `host` without updating `old` (`before`), a subsequent overlapping
`copy_back()` in the same submission sees `host[i] != old[i]` on those
CPU-written bytes, mistakes the earlier CPU write for a GPU write, and
overwrites newer CPU writes in title memory back to the stale value.

Update `old.copy_from_slice(host)` immediately after `merge_writeback`
so already-merged bytes match the snapshot on subsequent overlapping
copy-backs. Disjoint from Marvisak#2914 and Marvisak#2848.

Assisted-by: T3 Code (Antigravity, gemini-4-neon)
@pickles976

Copy link
Copy Markdown

Independent hardware verification at exact clean head 1de058caa2f34b407766df930f7ef49793cf68f1: Windows-MSVC victory-gpu regression shadow::tests::overlapping_copy_backs_do_not_clobber_newer_cpu_writes 1/1 PASS (0.79 s) under Wine 11.0/vkd3d-proton 3.2.0 on an NVIDIA RTX 4060 Laptop GPU (8 GiB), 16 GiB RAM. DXVK explicitly enumerated the laptop GPU and vkd3d initialized a hardware D3D12 device; this test reached the live Shadow path rather than the find() == None early-return branch. It exercises both partial-then-wider and partial-then-whole-page overlapping ranges and verifies the first GPU byte remains while the newer CPU byte survives the second copy-back.

I ran it inside the 9 GiB cgroup / 512 MiB swap safety scope with global available RAM >=2 GiB, GPU used <=6 GiB, disk >=10 GiB; no guard stop. Local log .compat-runtime/logs/verify-pr3031-wine-rtx4060-shadow-copyback.log. No retail title was launched; this is a synthetic GPU-shadow correctness check, not evidence the 16 GiB loader bottleneck is fixed. No objections from the focused Wine/NVIDIA fixture.

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks clean and sound on 1de058ca.

  • Advancing old.copy_from_slice(host) right after merge_writeback (and outside if merge_writeback(...), since merge_writeback still copies CPU bytes into host even when title is unchanged) accurately prevents a later overlapping copy-back in the same submission from mistaking earlier CPU writes in host for GPU writes and clobbering newer title writes.
  • Because Shadow::writing merges overlapping or touching ranges into a single segments entry, mutating old in place updates the shared snapshot seen by any subsequent overlapping copy_back in that submission, while read_into == false leaves both host and old untouched.
  • Both overlap shapes (partial_then_wider and partial_then_whole_page) are well-covered by the new regression test.

Note: the red CI run on 1de058ca failed on an unrelated flaky test (descriptor::host::tests::views_bound_while_another_slot_gets_its_texture_do_not_wait at descriptor/host.rs:2011); all shadow::tests including the new test passed.

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — advancing the saved snapshot after merge_writeback (old.copy_from_slice(host)) keeps a second overlapping copy-back from re-detecting CPU-written bytes as GPU writes. iter_mut/&mut before are minimal; the test covers both partial-then-wider and partial-then-whole-page.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

This fixes a real bug in crates/victory-gpu/src/shadow.rs::copy_back_or_not. merge_writeback sets host to the merged title bytes but leaves the before snapshot unchanged. compared only skips whole 64 KiB pieces, so a second, overlapping partial range in the same submission compares host with the stale before again. It then treats the CPU's earlier bytes as GPU writes and puts them back over newer CPU writes. The one-line fix, old.copy_from_slice(host) after the merge, matches what update_tracked's sync_before already does for the snapshot. The new test covers both overlap shapes.

CI: check is failing, but only in descriptor::host::tests::views_bound_while_another_slot_gets_its_texture_do_not_wait ("both made"). That test does not touch shadow.rs, and #3035 reports it as intermittently failing on unmodified main, so a re-run is probably enough. Please confirm before merging.


Generated by Claude Code

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):

  • Diff review: Advancing old (old.copy_from_slice(host)) after merge_writeback in Shadow::copy_back_or_not ensures a subsequent overlapping copy-back in the same submission does not mistake merged CPU writes for GPU writes and clobber newer CPU writes.
  • cargo test -p victory-gpu shadow::tests -- --test-threads=1 (RTX 5090 & WARP): 10 passed, 0 failed on both (including overlapping_copy_backs_do_not_clobber_newer_cpu_writes).
  • 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).

clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 26, 2026
…pump coalescing & 16-bit/indirect completeness (Wave 29A)

Adapts and integrates:
- GPU-to-CPU title-window write-back with retire-ticket fencing, xmem dirty-stamp + byte-level CPU-write precedence, and LEONIDA_TITLE_WRITEBACK=0 kill switch (ubiquitarianism/leonida#2129, GPL-3.0).
- Slot-133 shadow GPU-write preservation via 256-byte piece sums (LEONIDA_SHADOW_KEEP, ubiquitarianism/leonida#2163, GPL-3.0), saved-snapshot advancement after partial merge (Marvisak/VIctory#3031, GPL-3.0), and read-only hash skip for foreign PAGE_READONLY pages (ubiquitarianism/leonida#1897, GPL-3.0).
- Held-batch fence-pump fix: snapshot has_shader_work / internal_staging on ExecuteCommandLists thread in VulkanQueue::Deferred and coalesce redundant back-to-back refresh_shadows() / refresh_console_heaps() inside a single QueueHolds::pump_ready() sweep when xmem epochs and shadow registry are unchanged.
- Transient dedicated command-pool renewal for long recordings above LEONIDA_CMD_POOL_RENEW (default 512, Rubenpannen/VIctory-RTX5070@5df4fb3, GPL-3.0).
- Complete 16-bit packed colour widening for B5G6R5_UNORM RT/UAV, B4G4R4A4_UNORM RT/UAV, and B5G5R5A1_UNORM RT/UAV (Marvisak/VIctory#2936, GPL-3.0; californiaAlmonds/OpenScarlett@ee7153c, MIT).
- Compound graphics ExecuteIndirect host-visible expansion, Tier 1.1 incrementing-constant stride fix, and D3D12 state-leakage binding reset (ubiquitarianism/leonida#1937, GPL-3.0).

AI-assisted; verified on NVIDIA GeForce RTX 3080 (Windows 11, VS 18 MSVC 19.51) with LEONIDA_VK_VALIDATION=1 (18/18 CTests passed, 0 validation errors).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants