GPU: a marked copy-back advances the snapshot of declared ranges over its page #2437

Open
github-bot wants to merge 1 commit from github-bot/gh-2437 into main AGit
Owner

@⁠BastienRodz on GitHub · 2026-11-25 13:28 UTC · original
BastienRodz:gpu/copy-back-advances-snapshot → main · +47 −0 · 1 files · 1 commits · open

What's wrong

copy_back_marks runs before the declared ranges' copy-backs (host.rs). A marked copy-back puts the shaders' bytes back, and the copy takes the title's others. But the snapshot (Shadow::before) of a declared range over the same page keeps the bytes from before the submission. That range's copy_back then sees the title's bytes in the copy as differing from the snapshot, takes them for shader writes, and puts them back over anything the title wrote since the marked copy-back. Since cd94d35, a page can be both marked and inside a declared range (a heap view and a root UAV or a clear on the same buffer), and the title's threads keep writing these buffers during copy-back.

What the fix does

At the end of copy_back_marked, the new sync_snapshot copies the page's resulting bytes into any snapshot segment over it, so a later copy-back finds nothing new there. This is what update_busy already does for refreshes: "The same bytes go into the snapshot writing took, so copy-back neither takes them for the GPU's nor puts them back over a later title write." Nothing changes when no declared range covers the page.

Tests

New shadow::tests::a_declared_copy_back_after_a_marked_one_keeps_newer_title_writes: one page, marked and declared. A shader writes byte 10 and the title writes byte 40 during the submission. After the marked copy-back, the title writes 10 and 40 again, and the declared copy-back must write nothing.

  • On main (940d32f) it fails: the declared copy-back writes the page back (left: 65536, right: 0), restoring the older bytes.
  • With the fix it and the other shadow::tests pass (5), as do watch::tests (6). Run under Wine 10 with vkd3d-proton 3.1 on llvmpipe; without a host device the test returns early.
  • windows-latest run on the fork: https://github.com/BastienRodz/VIctory/actions/runs/45623926434

Not tested

No title run, and no AMD/NVIDIA GPU. How often the title writes a byte again between these two steps is not measured.

#261 has the companion case: two overlapping declared ranges, where copy_back does not advance its own snapshot. That PR predates marked copy-back. This one only touches copy_back_marked, so the two don't overlap.

Fixes #2436

AI-assisted (Gemini 4 Neon found and wrote it, Claude Code reviewed, narrowed and verified it). No game data used.

🤖 Generated with Claude Code

**@⁠BastienRodz** on GitHub · 2026-11-25 13:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/2437) `BastienRodz:gpu/copy-back-advances-snapshot` → `main` · +47 −0 · 1 files · 1 commits · open ### What's wrong `copy_back_marks` runs before the declared ranges' copy-backs (`host.rs`). A marked copy-back puts the shaders' bytes back, and the copy takes the title's others. But the snapshot (`Shadow::before`) of a declared range over the same page keeps the bytes from before the submission. That range's `copy_back` then sees the title's bytes in the copy as differing from the snapshot, takes them for shader writes, and puts them back over anything the title wrote since the marked copy-back. Since `cd94d35`, a page can be both marked and inside a declared range (a heap view and a root UAV or a clear on the same buffer), and the title's threads keep writing these buffers during copy-back. ### What the fix does At the end of `copy_back_marked`, the new `sync_snapshot` copies the page's resulting bytes into any snapshot segment over it, so a later copy-back finds nothing new there. This is what `update_busy` already does for refreshes: "The same bytes go into the snapshot `writing` took, so copy-back neither takes them for the GPU's nor puts them back over a later title write." Nothing changes when no declared range covers the page. ### Tests New `shadow::tests::a_declared_copy_back_after_a_marked_one_keeps_newer_title_writes`: one page, marked and declared. A shader writes byte 10 and the title writes byte 40 during the submission. After the marked copy-back, the title writes 10 and 40 again, and the declared copy-back must write nothing. - On `main` (940d32f) it fails: the declared copy-back writes the page back (`left: 65536`, `right: 0`), restoring the older bytes. - With the fix it and the other `shadow::tests` pass (5), as do `watch::tests` (6). Run under Wine 10 with vkd3d-proton 3.1 on llvmpipe; without a host device the test returns early. - windows-latest run on the fork: https://github.com/BastienRodz/VIctory/actions/runs/45623926434 ### Not tested No title run, and no AMD/NVIDIA GPU. How often the title writes a byte again between these two steps is not measured. #261 has the companion case: two overlapping declared ranges, where `copy_back` does not advance its own snapshot. That PR predates marked copy-back. This one only touches `copy_back_marked`, so the two don't overlap. Fixes #2436 AI-assisted (Gemini 4 Neon found and wrote it, Claude Code reviewed, narrowed and verified it). No game data used. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
copy_back_marks runs before the copy-backs of declared ranges. It puts a
marked page's GPU bytes back, and the copy takes the title's others
(`*h = *t`), but the snapshot of a declared range over that page
(Shadow::before) kept the bytes from before the submission. That range's
copy-back then found the title's bytes the copy had taken differing from
the snapshot, took them for the GPU's, and put them back over what the
title had written in between.

copy_back_marked now brings the snapshot over the page to the copy's
bytes (sync_snapshot).

Co-Authored-By: Gemini 4 Neon <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:05:45 +00:00
Author
Owner

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

Verification — head 13c6aa2e142178f09a697276b01388201f8349c7

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: 147 passed, 0 failed, 2 ignored (+1). New shadow::tests::a_declared_copy_back_after_a_marked_one_keeps_newer_title_writes ran and passed.
  • Negative control under WARP: with the sync_snapshot call removed that test fails (left: 65536, right: 0), matching the PR description, so it exercises the fix rather than returning early.
  • 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.
  • Base is main; it fixes #2436.

Overlap with #2434 / #2377:

  • Conflicts with #2434 (production code: #2434 removes copy_back_marked, where this PR adds its call; see my note there). Not combined-tested for that reason.
  • With #2377 the only conflict is both PRs appending a test at the same spot in shadow.rs; code hunks (copy_back_marked tail vs copy_back_page + watch.rs) merge cleanly.

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/2437#issuecomment-7164361737) **Verification — head `13c6aa2e142178f09a697276b01388201f8349c7`** **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`: **147 passed, 0 failed, 2 ignored** (+1). New `shadow::tests::a_declared_copy_back_after_a_marked_one_keeps_newer_title_writes` ran and passed. - Negative control under WARP: with the `sync_snapshot` call removed that test fails (`left: 65536, right: 0`), matching the PR description, so it exercises the fix rather than returning early. - 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. - Base is `main`; it fixes #2436. **Overlap with #2434 / #2377:** - Conflicts with #2434 (production code: #2434 removes `copy_back_marked`, where this PR adds its call; see my note there). Not combined-tested for that reason. - With #2377 the only conflict is both PRs appending a test at the same spot in `shadow.rs`; code hunks (`copy_back_marked` tail vs `copy_back_page` + `watch.rs`) merge cleanly. 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

@⁠BastienRodz on GitHub · 2026-11-25 13:52 UTC · original

Thanks @⁠oni-swr, the negative control under WARP is useful. On #2434: the two don't contradict each other. That PR splits copy_back_marked into phases, and this one only needs the copy's final bytes for the page to reach the snapshot after the page has been written back. If #2434 lands first, I'll rebase and move the sync_snapshot call into its write-back phase.

**@⁠BastienRodz** on GitHub · 2026-11-25 13:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/2437#issuecomment-7164407674) Thanks @⁠oni-swr, the negative control under WARP is useful. On #2434: the two don't contradict each other. That PR splits `copy_back_marked` into phases, and this one only needs the copy's final bytes for the page to reach the snapshot after the page has been written back. If #2434 lands first, I'll rebase and move the `sync_snapshot` call into its write-back phase.
Author
Owner

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

Verified on Windows 10 / GTX 1050 at 13c6aa2: cargo test -p victory-gpu --lib shadow::tests — 5/5 pass, including the new a_declared_copy_back_after_a_marked_one_keeps_newer_title_writes on real hardware (host GPU present, so find did not early-return).

The fix matches the issue's analysis: by the time sync_snapshot runs, host already equals what title memory holds (the merge loop leaves *h = *t either way, or written is None because they were already equal), so advancing before segments to host is exactly "the same bytes go into the snapshot" that update_busy documents. Declared copy-backs after it then see before == memory and cannot mistake the title's bytes for the GPU's.

Also checked: the early changed-kept path writes nothing to memory, so skipping the snapshot advance there is consistent; and sync_snapshot locking before cannot deadlock — copy_back_marks and the declared copy_backs run on the same submission thread with separate lock acquisitions.

**@⁠kotekompotek** on GitHub · 2026-11-25 14:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/2437#issuecomment-7164621184) Verified on Windows 10 / GTX 1050 at `13c6aa2`: `cargo test -p victory-gpu --lib shadow::tests` — 5/5 pass, including the new `a_declared_copy_back_after_a_marked_one_keeps_newer_title_writes` on real hardware (host GPU present, so `find` did not early-return). The fix matches the issue's analysis: by the time `sync_snapshot` runs, `host` already equals what title memory holds (the merge loop leaves `*h = *t` either way, or `written` is `None` because they were already equal), so advancing `before` segments to `host` is exactly "the same bytes go into the snapshot" that `update_busy` documents. Declared copy-backs after it then see `before == memory` and cannot mistake the title's bytes for the GPU's. Also checked: the early `changed`-kept path writes nothing to memory, so skipping the snapshot advance there is consistent; and `sync_snapshot` locking `before` cannot deadlock — `copy_back_marks` and the declared `copy_back`s run on the same submission thread with separate lock acquisitions.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 15:34 UTC · original

Overlap with #2513, WARP/CPU only (addendum to the existing verification of 13c6aa2e)

Same defect (#2436), same file (victory-gpu/src/shadow.rs). Tested on Windows 11 Pro 10.0.26200, rustc 1.98.1, VICTORY_D3D12_WARP=1 + --features diagnostics (WARP; no physical GPU), origin/main 940d32f.

  • #2513 (head 0b92934e) advances the declared range's snapshot inline inside if let Some(bytes) = written; this PR calls sync_snapshot unconditionally. The two merge textually clean onto 940d32f (merge commit 7e9e3fb); there: 6 shadow::tests pass, full lib 148 passed, 0 failed, 2 ignored; clippy and policy clean. With #2513's loop disabled in that merge, both PRs' tests still pass, so this PR alone covers #2513's test.
  • Reverse is not true: on #2513's head a throwaway zero-write test (title writes 0x11 at byte 40, no shader write, copy_back_marked returns 0, title writes 0x22, declared copy_back) fails (declared pass writes 65536 bytes, byte reads 0x11). On this PR's head it passes (declared pass writes 0 bytes).
  • Full details and #2513 numbers are on #2513.

AI use: Claude Code (Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-25 15:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/2437#issuecomment-7167043986) **Overlap with #2513, WARP/CPU only (addendum to the existing verification of `13c6aa2e`)** Same defect (#2436), same file (`victory-gpu/src/shadow.rs`). Tested on Windows 11 Pro 10.0.26200, rustc 1.98.1, `VICTORY_D3D12_WARP=1` + `--features diagnostics` (WARP; no physical GPU), `origin/main` `940d32f`. - #2513 (head `0b92934e`) advances the declared range's snapshot inline inside `if let Some(bytes) = written`; this PR calls `sync_snapshot` unconditionally. The two merge textually clean onto `940d32f` (merge commit `7e9e3fb`); there: 6 `shadow::tests` pass, full lib 148 passed, 0 failed, 2 ignored; clippy and policy clean. With #2513's loop disabled in that merge, both PRs' tests still pass, so this PR alone covers #2513's test. - Reverse is not true: on #2513's head a throwaway zero-write test (title writes 0x11 at byte 40, no shader write, `copy_back_marked` returns 0, title writes 0x22, declared `copy_back`) fails (declared pass writes 65536 bytes, byte reads 0x11). On this PR's head it passes (declared pass writes 0 bytes). - Full details and #2513 numbers are on #2513. AI use: Claude Code (Claude Sonnet 5.5).
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-25 15:47 UTC · original

Rebased onto main da82e44. Main added clean/dirty_bit next to where sync_snapshot goes; I kept both, and the call still sits at the end of copy_back_marked. On the new main the test still fails without it (left: 65536, right: 0) and passes with it; shadow:: 9/9.

**@⁠BastienRodz** on GitHub · 2026-11-25 15:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/2437#issuecomment-7167321013) Rebased onto main da82e44. Main added `clean`/`dirty_bit` next to where `sync_snapshot` goes; I kept both, and the call still sits at the end of `copy_back_marked`. On the new main the test still fails without it (`left: 65536`, `right: 0`) and passes with it; `shadow::` 9/9.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 16:38 UTC · original · edited 2026-11-26 03:24 UTC

Automated triage for the maintainers.

Verdict: merge candidate. Rebased at 60d81cf, which merges cleanly into main; the change is the same as the version reviewed before. At the end of Shadow::copy_back_marked (crates/victory-gpu/src/shadow.rs) the new sync_snapshot copies the page's final copy bytes into any declared-range snapshot segment over it. Without that, the declared copy_back that follows takes the title's bytes for shader writes and writes them back over newer title writes (#2436). It uses the same mapping-relative offsets and segment loop as sync_before in update_busy. The test a_declared_copy_back_after_a_marked_one_keeps_newer_title_writes fails without the call (reported by the author and by oni-swr under WARP) and passes on hardware (kotekompotek).

The overlapping #2434 and #2513 have been closed, and #2377 has been merged, so nothing else open competes with this.

CI: passing (check).


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 16:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/2437#issuecomment-7168297475) · edited 2026-11-26 03:24 UTC <!-- victory-triage v1 head=60d81cfbb11df642e207d0691119a2d4f11e433b verdict=merge-candidate at=2026-11-26T03:24:08Z --> Automated triage for the maintainers. **Verdict: merge candidate.** Rebased at `60d81cf`, which merges cleanly into `main`; the change is the same as the version reviewed before. At the end of `Shadow::copy_back_marked` (`crates/victory-gpu/src/shadow.rs`) the new `sync_snapshot` copies the page's final copy bytes into any declared-range snapshot segment over it. Without that, the declared `copy_back` that follows takes the title's bytes for shader writes and writes them back over newer title writes (#2436). It uses the same mapping-relative offsets and segment loop as `sync_before` in `update_busy`. The test `a_declared_copy_back_after_a_marked_one_keeps_newer_title_writes` fails without the call (reported by the author and by oni-swr under WARP) and passes on hardware (kotekompotek). The overlapping #2434 and #2513 have been closed, and #2377 has been merged, so nothing else open competes with this. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2437/head:github-bot/gh-2437'
git switch 'github-bot/gh-2437'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2437'
git switch 'github-bot/gh-2437'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2437'
git switch 'github-bot/gh-2437'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2437'
git switch 'main'
git merge --squash 'github-bot/gh-2437'
git switch 'main'
git merge --ff-only 'github-bot/gh-2437'
git switch 'main'
git merge 'github-bot/gh-2437'
git push origin 'main'
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!2437
No description provided.