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

Closed
github-bot wants to merge 1 commit from gh-pr/2434/head into gh-pr/2434/base
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 13:27 UTC · original
fuzeofficlal:fix/gpu-marked-copyback-shared-and-subpage → main · +210 −49 · 1 files · 1 commits · closed without merging

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
**@⁠fuzeofficlal** on GitHub · 2026-11-25 13:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/2434) `fuzeofficlal:fix/gpu-marked-copyback-shared-and-subpage` → `main` · +210 −49 · 1 files · 1 commits · closed without merging ## 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`
copy_back_marks previously ran watch::writing and t.taken per marked
entry inside a single loop, causing two bugs:
1. When multiple Shadow buffers share one 64 KiB title page, an earlier
   copy's watch::writing marks that page changed on every sibling
   holder on the page, causing the next unarmed copy on that page to
   mistake the copy-back for a title write and drop its GPU writes.
2. Sub-64-KiB and unaligned-end Shadow copies skipped t.taken because
   their slice did not span the full 64 KiB page, leaving the page
   unguarded and marked changed so later sub-slice refreshes dropped
   subsequent marked GPU writes.

Split copy_back_marks into prepare (unarm and snapshot baselines before
any watch::writing), commit (merge and write back changed bytes), and
re-take phases (unconditionally re-guard every synced slice's page after
all Writing guards drop, unless a later write in the batch overlapped
its bytes).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:05:40 +00:00
github-bot closed this pull request 2026-11-26 05:05:40 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 13:50 UTC · original

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.

**@⁠oni-swr** on GitHub · 2026-11-25 13:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/2434#issuecomment-7164360980) **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._
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 14:01 UTC · original

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

**@⁠kotekompotek** on GitHub · 2026-11-25 14:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/2434#issuecomment-7164637461) 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).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 16:06 UTC · original · edited 2026-11-25 16:45 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 16:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/2434#issuecomment-7167596201) · edited 2026-11-25 16:45 UTC <!-- 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)_
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 22:09 UTC · original

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.

**@⁠Marvisak** on GitHub · 2026-11-25 22:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/2434#issuecomment-7176012873) 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

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2434
No description provided.