GPU: fix marked copy-back across shared 64 KiB pages and sub-page copies #2432

Closed
fuzeofficlal wants to merge 1 commit from upstream-pr/2434 into main
fuzeofficlal commented 2026-11-25 13:27:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T13:27:20Z.


Summary

copy_back_marks / Shadow::copy_back_marked (introduced in cd94d35 / 940d32f) had two coupled page-tracking bugs affecting sub-allocated buffers that share a 64 KiB title page or do not span a full 64 KiB page:

  1. Cross-copy watch::writing self-sabotage on shared 64 KiB pages:
    When two Shadow copies share a 64 KiB page page (e.g. sub-allocated buffers [page, page + 4096) and [page + 4096, page + 8192)) and shaders write both in the same submission via heap-indexed UAVs (where lists do not declare ranges, so neither Tracked is armed), copy_back_marks processed them sequentially:

    • The first copy's watch::writing opened page / PAGE and marked page / PAGE as changed in every holder in g.holders[page / PAGE], including the second copy.
    • The second copy then saw saved.is_none() && t.changed(page / PAGE) and dropped its GPU writes (MarkedKept), mistaking the first copy's write-back for a concurrent title CPU write.
    • Even if the first copy re-took the page, the second copy's subsequent watch::writing immediately re-opened page / PAGE and marked the first copy changed again.
  2. Sub-64-KiB / unaligned-end copies never re-taking their page after copy_back_marked:
    copy_back_marked gated t.taken(&[page / PAGE]) on from == page && to == page + PAGE, even though [from, to) (page.max(start)..(page + PAGE).min(end)) is already 100% of self's tracked range within page (matching Tracked::take and Shadow::copy_back_page). Every placed buffer with len < 64 KiB or len % 64 KiB != 0 therefore left its page unguarded and marked changed in its own Tracked after every marked write-back, causing redundant CPU->GPU re-uploads and dropping a subsequent marked write when only a sub-slice view was refreshed.

Changes

  • Split copy_back_marks in crates/victory-gpu/src/shadow.rs into three phases:
    1. Shadow::prepare_marked: unarms page / PAGE, skips unarmed pages already changed before any write-back (MarkedKept), and snapshots each slice's baseline [from, to) (saved slice when present, else current title memory) before any watch::writing opens shared pages.
    2. MarkedSlice::commit: fast-paths unchanged unsaved slices (*host == self.before[..]), otherwise merges GPU changes against before into current title memory and writes back under watch::writing.
    3. Post-write re-take: after all Writing guards drop, calls t.taken(&[page / PAGE]) for every synced slice (including sub-page copies), unless a later write in the batch overlapped its byte range.
  • Added unit tests shared_page_marked_copies_do_not_drop_or_dirty_each_other and subpage_marked_copy_retakes_its_page_across_rounds.

Verification

  • cargo test -p victory-gpu --lib --locked -- shadow (6 passed)
  • cargo clippy -p victory-gpu --all-targets --no-default-features --locked -- -D warnings
  • cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings
  • cargo xtask policy
<!-- greyfinch-sync:Marvisak/VIctory:issue:6605552857 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2434). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T13:27:20Z. --- ## Summary `copy_back_marks` / `Shadow::copy_back_marked` (introduced in `cd94d35` / `940d32f`) had two coupled page-tracking bugs affecting sub-allocated buffers that share a 64 KiB title page or do not span a full 64 KiB page: 1. **Cross-copy `watch::writing` self-sabotage on shared 64 KiB pages**: When two `Shadow` copies share a 64 KiB page `page` (e.g. sub-allocated buffers `[page, page + 4096)` and `[page + 4096, page + 8192)`) and shaders write both in the same submission via heap-indexed UAVs (where lists do not declare ranges, so neither `Tracked` is `armed`), `copy_back_marks` processed them sequentially: - The first copy's `watch::writing` opened `page / PAGE` and marked `page / PAGE` as `changed` in **every** holder in `g.holders[page / PAGE]`, including the second copy. - The second copy then saw `saved.is_none() && t.changed(page / PAGE)` and dropped its GPU writes (`MarkedKept`), mistaking the first copy's write-back for a concurrent title CPU write. - Even if the first copy re-took the page, the second copy's subsequent `watch::writing` immediately re-opened `page / PAGE` and marked the first copy `changed` again. 2. **Sub-64-KiB / unaligned-end copies never re-taking their page after `copy_back_marked`**: `copy_back_marked` gated `t.taken(&[page / PAGE])` on `from == page && to == page + PAGE`, even though `[from, to)` (`page.max(start)..(page + PAGE).min(end)`) is already 100% of `self`'s tracked range within `page` (matching `Tracked::take` and `Shadow::copy_back_page`). Every placed buffer with `len < 64 KiB` or `len % 64 KiB != 0` therefore left its page unguarded and marked `changed` in its own `Tracked` after every marked write-back, causing redundant CPU->GPU re-uploads and dropping a subsequent marked write when only a sub-slice view was refreshed. ## Changes - Split `copy_back_marks` in `crates/victory-gpu/src/shadow.rs` into three phases: 1. **`Shadow::prepare_marked`**: unarms `page / PAGE`, skips unarmed pages already changed before any write-back (`MarkedKept`), and snapshots each slice's baseline `[from, to)` (`saved` slice when present, else current title memory) before any `watch::writing` opens shared pages. 2. **`MarkedSlice::commit`**: fast-paths unchanged unsaved slices (`*host == self.before[..]`), otherwise merges GPU changes against `before` into current title memory and writes back under `watch::writing`. 3. **Post-write re-take**: after all `Writing` guards drop, calls `t.taken(&[page / PAGE])` for every synced slice (including sub-page copies), unless a later write in the batch overlapped its byte range. - Added unit tests `shared_page_marked_copies_do_not_drop_or_dirty_each_other` and `subpage_marked_copy_retakes_its_page_across_rounds`. ## Verification - `cargo test -p victory-gpu --lib --locked -- shadow` (6 passed) - `cargo clippy -p victory-gpu --all-targets --no-default-features --locked -- -D warnings` - `cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings` - `cargo xtask policy`
DenRakEiw commented 2026-11-25 14:06:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T14:02:06Z.

Review: APPROVED


Verified against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Fixing marked copy-back across shared 64 KiB pages and sub-page copies passes all shadow copy-back tests cleanly.
  • cargo test -p victory-gpu shadow::: 6/6 passed on RTX 5090, 6/6 passed on WARP (including shared_page_marked_copies_do_not_drop_or_dirty_each_other and subpage_marked_copy_retakes_its_page_across_rounds).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6059018168 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2434#pullrequestreview-6059018168). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T14:02:06Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP: - Fixing marked copy-back across shared 64 KiB pages and sub-page copies passes all shadow copy-back tests cleanly. - `cargo test -p victory-gpu shadow::`: 6/6 passed on RTX 5090, 6/6 passed on WARP (including `shared_page_marked_copies_do_not_drop_or_dirty_each_other` and `subpage_marked_copy_retakes_its_page_across_rounds`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
oni-swr commented 2026-11-25 14:07:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T13:50:13Z.


Verification — head 794ab08e5a3a50660e40a71d39d6d316cdcc5861

Environment: Windows 11 Pro 10.0.26200, rustc/cargo 1.98.1, DXC/dxil 1.8.2502.11 (Windows Kits 10.0.26100.0 x64). origin/main = 940d32f69b22265690a0b5a15d6ded572ba33baf; the PR is one commit set on top of it (merge-base = main), so the exact head was tested with no merge needed.

  • cargo test -p victory-gpu --lib --features diagnostics -- --test-threads=1: 148 passed, 0 failed, 2 ignored (+2). New shared_page_marked_copies_do_not_drop_or_dirty_each_other and subpage_marked_copy_retakes_its_page_across_rounds ran and passed. (Both return early when no host device exists; under WARP one exists. I did not run them against main as a negative control.)
  • clippy with and without diagnostics: clean. cargo xtask policy: ok.
  • GPU tests ran with VICTORY_D3D12_WARP=1 and --features diagnostics (the switch has no effect without that feature), -- --test-threads=1; warp_adapter_accepts_only_software ran and passed (adapter vendor 0x1414, software flag). Baseline origin/main for the same command: 146 passed, 0 failed, 2 ignored.

Overlap with #2437 / #2377 (all based on 940d32f):

  • #2434 removes Shadow::copy_back_marked (replaced by prepare_marked + MarkedSlice::commit). #2437 adds a sync_snapshot call inside that function and its test calls copy_back_marked; #2377's test calls it too. git merge of #2437 or #2377 onto #2434 conflicts in shadow.rs (production hunk, not only tests), and the tests would no longer compile even after resolving.
  • #2434 does not touch the Shadow::before snapshot, so the declared-range scenario of #2437 (#2436) does not look addressed by it (reasoned from the diff, not run); #2437's fix would need re-porting into commit/prepare_marked.
  • Suggest landing one first and rebasing the others; #2434 is the larger restructure.

Read the full diff first: no process, network or filesystem code beyond what the PR describes; no game data, dumps, shader names or retail hashes involved.

Verified by Claude Code (Claude Sonnet 5.5), an AI assistant; this is a test report, not a code review or approval.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7164360980 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2434#issuecomment-7164360980). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-25T13:50:13Z. --- **Verification — head `794ab08e5a3a50660e40a71d39d6d316cdcc5861`** **Environment:** Windows 11 Pro 10.0.26200, rustc/cargo 1.98.1, DXC/dxil 1.8.2502.11 (Windows Kits 10.0.26100.0 x64). `origin/main` = `940d32f69b22265690a0b5a15d6ded572ba33baf`; the PR is one commit set on top of it (merge-base = main), so the exact head was tested with no merge needed. - `cargo test -p victory-gpu --lib --features diagnostics -- --test-threads=1`: **148 passed, 0 failed, 2 ignored** (+2). New `shared_page_marked_copies_do_not_drop_or_dirty_each_other` and `subpage_marked_copy_retakes_its_page_across_rounds` ran and passed. (Both return early when no host device exists; under WARP one exists. I did not run them against `main` as a negative control.) - clippy with and without `diagnostics`: clean. `cargo xtask policy`: ok. - GPU tests ran with `VICTORY_D3D12_WARP=1` and `--features diagnostics` (the switch has no effect without that feature), `-- --test-threads=1`; `warp_adapter_accepts_only_software` ran and passed (adapter vendor 0x1414, software flag). Baseline `origin/main` for the same command: 146 passed, 0 failed, 2 ignored. **Overlap with #2437 / #2377 (all based on `940d32f`):** - #2434 removes `Shadow::copy_back_marked` (replaced by `prepare_marked` + `MarkedSlice::commit`). #2437 adds a `sync_snapshot` call inside that function and its test calls `copy_back_marked`; #2377's test calls it too. `git merge` of #2437 or #2377 onto #2434 conflicts in `shadow.rs` (production hunk, not only tests), and the tests would no longer compile even after resolving. - #2434 does not touch the `Shadow::before` snapshot, so the declared-range scenario of #2437 (#2436) does not look addressed by it (reasoned from the diff, not run); #2437's fix would need re-porting into `commit`/`prepare_marked`. - Suggest landing one first and rebasing the others; #2434 is the larger restructure. Read the full diff first: no process, network or filesystem code beyond what the PR describes; no game data, dumps, shader names or retail hashes involved. _Verified by Claude Code (Claude Sonnet 5.5), an AI assistant; this is a test report, not a code review or approval._
kotekompotek commented 2026-11-25 14:07:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T14:01:20Z.


Reviewed at current head — the prepare/commit split is the right shape for both bugs:

  • All prepare_markeds run before any watch::writing/write_mapped, so a copy sharing a 64 KiB page no longer sees another copy's write-back as a changed title write. The saved/changed gate moved to prepare exactly where it must.
  • The unified merge loop in commit reproduces both old branches: was_saved → per-byte GPU-wins merge against the watch baseline; !was_saved → before is title memory, so h != b are precisely the GPU bytes and the loop writes them over — same as the old wholesale write_mapped(host).
  • Re-take gating (was_saved || wrote_on_page) && !overlapped_later correctly drops the full-page requirement — [from,to) is already the copy's whole tracked slice inside page — and stays conservative where a later batch entry overlapped the slice.

Interplay note for triage: this does not subsume #2437 — commit does not advance before (declared-range snapshot) segments after writing, so the #2436 race remains. Whichever lands second will need the sync_snapshot call carried into commit (after the merge loop, same invariant: host equals title memory there).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7164637461 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2434#issuecomment-7164637461). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T14:01:20Z. --- Reviewed at current head — the prepare/commit split is the right shape for both bugs: - All `prepare_marked`s run before any `watch::writing`/`write_mapped`, so a copy sharing a 64 KiB page no longer sees another copy's write-back as a `changed` title write. The `saved`/`changed` gate moved to prepare exactly where it must. - The unified merge loop in `commit` reproduces both old branches: `was_saved` → per-byte GPU-wins merge against the watch baseline; `!was_saved` → `before` is title memory, so `h != b` are precisely the GPU bytes and the loop writes them over — same as the old wholesale `write_mapped(host)`. - Re-take gating (`was_saved || wrote_on_page) && !overlapped_later` correctly drops the full-page requirement — `[from,to)` is already the copy's whole tracked slice inside `page` — and stays conservative where a later batch entry overlapped the slice. Interplay note for triage: this does not subsume #2437 — `commit` does not advance `before` (declared-range snapshot) segments after writing, so the #2436 race remains. Whichever lands second will need the `sync_snapshot` call carried into `commit` (after the merge loop, same invariant: `host` equals title memory there).
catminou4 commented 2026-11-25 15:03:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T14:11:14Z.

Review: COMMENTED


Marked copy-back across shared 64 KiB pages fixed. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Same shadow.rs copy-back area as #2280/#2409/#2437 — growing cluster, convergence suggested.

<!-- greyfinch-sync:Marvisak/VIctory:review:6059176211 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2434#pullrequestreview-6059176211). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T14:11:14Z. **Review:** COMMENTED --- Marked copy-back across shared 64 KiB pages fixed. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Same shadow.rs copy-back area as #2280/#2409/#2437 — growing cluster, convergence suggested.
Marvisak commented 2026-11-25 16:09:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T16:06:13Z.


Automated triage for the maintainers.

Merge candidate, with a merge-order note.

Both bugs are real on main. First, watch::writing in one copy's marked write-back marks the page changed in every holder. A second copy on the same 64 KiB page then hits saved.is_none() && changed and drops its GPU writes. Second, copy_back_marked re-takes only whole pages, but Tracked::take already counts a copy's whole slice of a page as whole. In copy_back_marks, prepare_marked now runs for every page before any write-back, MarkedSlice::commit does the merge, and the re-take comes after all Writing guards drop, skipping slices a later entry overlapped. The merge in commit matches both old branches. The two tests cover these cases.

Relation to #2437 (merge candidate): this PR removes Shadow::copy_back_marked, which is where #2437 adds sync_snapshot, so the two conflict in production code. This PR does not fix #2437's case (#2436), because commit does not advance Shadow::before. Whichever lands second must call sync_snapshot(from - start, host) at the end of commit and adapt #2437's test to copy_back_marks. #2377's test also calls copy_back_marked.

Nit: the doc on gpu_writes (shadow.rs ~line 270) still links Shadow::copy_back_marked.

CI: check passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167596201 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2434#issuecomment-7167596201). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T16:06:13Z. --- <!-- victory-triage v1 head=4ac4d3a228819dc79ba9c5698b272b84bd22fbca verdict=merge-candidate at=2026-11-25T16:43:20Z --> Automated triage for the maintainers. **Merge candidate**, with a merge-order note. Both bugs are real on `main`. First, `watch::writing` in one copy's marked write-back marks the page changed in every holder. A second copy on the same 64 KiB page then hits `saved.is_none() && changed` and drops its GPU writes. Second, `copy_back_marked` re-takes only whole pages, but `Tracked::take` already counts a copy's whole slice of a page as whole. In `copy_back_marks`, `prepare_marked` now runs for every page before any write-back, `MarkedSlice::commit` does the merge, and the re-take comes after all `Writing` guards drop, skipping slices a later entry overlapped. The merge in `commit` matches both old branches. The two tests cover these cases. Relation to #2437 (merge candidate): this PR removes `Shadow::copy_back_marked`, which is where #2437 adds `sync_snapshot`, so the two conflict in production code. This PR does not fix #2437's case (#2436), because `commit` does not advance `Shadow::before`. Whichever lands second must call `sync_snapshot(from - start, host)` at the end of `commit` and adapt #2437's test to `copy_back_marks`. #2377's test also calls `copy_back_marked`. Nit: the doc on `gpu_writes` (shadow.rs ~line 270) still links `Shadow::copy_back_marked`. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 23:04:30 +00:00
Marvisak commented 2026-11-25 23:05:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:09:41Z.


Automated triage for the maintainers, second look (a review against current main, with builds and tests where they could change the verdict).

Closing because the new code leaves copies stale, although both bugs are real (the two tests fail on main). In commit, a slice with no saved bytes whose copy equals its baseline returns early without reading title memory, and is then re-taken whenever an earlier entry wrote the same page, even if that write landed under it: with an outer copy writing a byte inside an inner copy that was marked but unchanged, the inner copy's refresh copies 0 bytes and keeps the old value. Re-taking pages only after every commit also widens the window in which a title write is lost, and holding a 64 KiB buffer per marked page costs about 100 MB a world frame. It also removes the function #2437 fixes. A narrower fix for the two cases would be welcome.

If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176012873 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2434#issuecomment-7176012873). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:09:41Z. --- Automated triage for the maintainers, second look (a review against current `main`, with builds and tests where they could change the verdict). Closing because the new code leaves copies stale, although both bugs are real (the two tests fail on `main`). In `commit`, a slice with no saved bytes whose copy equals its baseline returns early without reading title memory, and is then re-taken whenever an earlier entry wrote the same page, even if that write landed under it: with an outer copy writing a byte inside an inner copy that was marked but unchanged, the inner copy's refresh copies 0 bytes and keeps the old value. Re-taking pages only after every commit also widens the window in which a title write is lost, and holding a 64 KiB buffer per marked page costs about 100 MB a world frame. It also removes the function #2437 fixes. A narrower fix for the two cases would be welcome. If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

Pull request closed

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!2432
No description provided.