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

Open
RVZO6 wants to merge 1 commit from upstream-pr/3031 into main
RVZO6 commented 2026-11-26 03:59:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-26T03:59:48Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:issue:6619197091 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3031). Originally posted by [RVZO6](https://github.com/RVZO6) on 2026-11-26T03:59:48Z. --- 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](https://github.com/RVZO6/VIctory/actions/runs/45748741503) (green, combined Wave 10 check on `63bcca4`). - **Not run:** the title; any hardware GPU. AI-assisted: T3 Code (Antigravity, gemini-4-neon).
pickles976 commented 2026-11-26 04:06:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pickles976 on 2026-11-26T04:05:16Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7182282253 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3031#issuecomment-7182282253). Originally posted by [pickles976](https://github.com/pickles976) on 2026-11-26T04:05:16Z. --- 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 commented 2026-11-26 05:03:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T04:11:55Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6068446349 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3031#pullrequestreview-6068446349). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T04:11:55Z. **Review:** APPROVED --- 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 commented 2026-11-26 05:03:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T04:39:12Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6068626460 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3031#pullrequestreview-6068626460). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T04:39:12Z. **Review:** APPROVED --- 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.
Dukestar69 commented 2026-11-26 07:01:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T06:50:00Z.

Review: APPROVED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6069687231 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3031#pullrequestreview-6069687231). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T06:50:00Z. **Review:** APPROVED --- 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).
Marvisak commented 2026-11-26 07:01:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T06:17:45Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7184288304 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3031#issuecomment-7184288304). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T06:17:45Z. --- <!-- victory-triage v1 head=1de058caa2f34b407766df930f7ef49793cf68f1 verdict=merge-candidate at=2026-11-26T06:22:00Z --> 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](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3031:upstream-pr/3031'
git switch 'upstream-pr/3031'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/3031'
git switch 'upstream-pr/3031'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3031'
git switch 'upstream-pr/3031'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3031'
git switch 'main'
git merge --squash 'upstream-pr/3031'
git switch 'main'
git merge --ff-only 'upstream-pr/3031'
git switch 'main'
git merge 'upstream-pr/3031'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!3029
No description provided.