GPU: copy-back puts back only what shaders wrote, in two more cases #261

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

@⁠Maraco91 on GitHub · 2026-11-24 01:53 UTC · original
Maraco91:gpu/copy-back-keeps-newer-title-bytes → main · +152 −26 · 1 files · 1 commits · open

What

Shadow::copy_back can still write an older byte of the host copy over a byte the title wrote while the GPU ran: the failure its own comment describes ("putting back the copy's older bytes there broke the title's own records"), through two paths that are left.

copy_back takes every byte of a declared range that differs from the snapshot (before) for a shader's write, and puts it into title memory unless the title's byte already equals it. So anything else that makes the copy differ from the snapshot goes back as if a shader had written it.

1. A range declared inside a gap the snapshot already spans. There is one snapshot per copy and submission, grown to span every declared range, so it also holds the gaps between them, as the copy was when the snapshot grew over them. If a later range of the same submission lies in such a gap, its refresh runs after that and brings the title's current bytes into the copy; writing then finds the snapshot already spans the range and returns. For every byte that refresh changed, copy ≠ snapshot. If the title writes that byte again before the copy-back, the refresh's value is written over it.

update(A) writing(A)   update(C) writing(C)      snapshot = [A .. gap B .. C], B as last copied
update(B)                                        copy[B] = the title's bytes now (byte k: 0 -> 1)
writing(B)                                       returns: already spanned; snapshot[k] is still 0
   ... GPU runs; the title sets byte k to 2 ...
copy_back(B)                                     copy 1 != snapshot 0, title 2 != 1  =>  title byte k = 1

The world declares views of its 174 MB heap dozens of times per submission, in no particular order, so ranges inside earlier gaps are the normal case there.

2. Two declared ranges that overlap. For each 64 KiB piece it handles, copy_back leaves the copy holding the title's bytes (*h = *t), but the snapshot kept the old ones. A second range over the same piece (host.rs only folds identical ranges) then sees the bytes the first pass took from the title as shader writes, and puts them back over whatever the title wrote between the two passes.

The fix, in shadow.rs only:

  • writing works out the parts of the range that no earlier range of the submission declared (new helper outside). A refresh leaves declared ranges alone, so only those parts can have been refreshed since the snapshot took them; where the snapshot already held them, they are taken again from the copy. No shader has written yet at that point: every list's deferred steps run before the submission executes.
  • copy_back gives the snapshot the title's bytes along with the copy (*o = *t), so a piece that was handled no longer differs.

Cost: a byte is copied a second time only if it was first captured as part of a gap and declared later, at most once per submission; ranges declared again, and ranges that extend the snapshot, cost what they did. The body of writing moved under an if, hence the size of the diff.

I have not run a title (I have no game files), so I cannot say which of its symptoms this accounts for; what it does is intermittent corruption of title memory in buffers shaders write.

Tested

Windows 11 Pro 10.0.26200, NVIDIA RTX 3080 Ti (driver 32.0.16.1656), rustc 1.98.1 (x86_64-pc-windows-msvc), no game data.

shadow.rs had no tests. Two new ones, on a real host buffer (Host::shared_buffer; they return early without a host GPU, like the crate's other device tests) over three pages of plain heap memory standing in for title memory (not write-watched, so refreshes copy whole ranges). "The title writes" and "a shader writes" are single-byte writes into that memory and into the mapped copy.

  • a_range_between_earlier_ones_keeps_the_titles_newer_bytes: the sequence in the diagram. Checks the title's newer byte survives; controls: a byte the title wrote before the refresh and not after keeps its value, and the two bytes "the shader" wrote (one in an outer range, one in the late range) do go back to title memory; the snapshot is released at the end.
  • overlapping_ranges_keep_the_titles_newer_bytes: [a, a+1 page) and [a, a+2 pages) declared, a shader's byte and a title's byte in the first page, the title's byte written again between the two copy-backs. Checks the newer byte survives and the shader's byte went back.

Results:

  • main (d2a4bae) + the tests only: cargo test -p victory-gpu --lib shadow::tests: both fail with the title's newer byte: left: 1, right: 2.
  • with the fix: cargo test -p victory-gpu --lib: 45 passed, 0 failed, 1 ignored, three runs in a row.
  • cargo clippy -p victory-gpu --tests: nothing in shadow.rs. rustfmt --check: nothing in the lines changed (one existing difference, the host closure line in writing).
  • cargo xtask policy: 151 files ok.
  • Trial merges (git merge-tree): clean with #159 and #185; conflicts with #124, which restructures the same two functions (the snapshot moves into a Before struct). The two changes are independent in what they do; whichever goes in second needs a rebase, and I will do this one's if #124 goes first.

Not tested: a title run; frame time in the world.

AI-assisted (Claude Code); the tests above were run on the machine described.

🤖 Generated with Claude Code

**@⁠Maraco91** on GitHub · 2026-11-24 01:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/261) `Maraco91:gpu/copy-back-keeps-newer-title-bytes` → `main` · +152 −26 · 1 files · 1 commits · open ## What `Shadow::copy_back` can still write an older byte of the host copy over a byte the title wrote while the GPU ran: the failure its own comment describes ("putting back the copy's older bytes there broke the title's own records"), through two paths that are left. `copy_back` takes every byte of a declared range that differs from the snapshot (`before`) for a shader's write, and puts it into title memory unless the title's byte already equals it. So anything else that makes the copy differ from the snapshot goes back as if a shader had written it. **1. A range declared inside a gap the snapshot already spans.** There is one snapshot per copy and submission, grown to span every declared range, so it also holds the gaps between them, as the copy was when the snapshot grew over them. If a later range of the same submission lies in such a gap, its refresh runs after that and brings the title's current bytes into the copy; `writing` then finds the snapshot already spans the range and returns. For every byte that refresh changed, copy ≠ snapshot. If the title writes that byte again before the copy-back, the refresh's value is written over it. ``` update(A) writing(A) update(C) writing(C) snapshot = [A .. gap B .. C], B as last copied update(B) copy[B] = the title's bytes now (byte k: 0 -> 1) writing(B) returns: already spanned; snapshot[k] is still 0 ... GPU runs; the title sets byte k to 2 ... copy_back(B) copy 1 != snapshot 0, title 2 != 1 => title byte k = 1 ``` The world declares views of its 174 MB heap dozens of times per submission, in no particular order, so ranges inside earlier gaps are the normal case there. **2. Two declared ranges that overlap.** For each 64 KiB piece it handles, `copy_back` leaves the copy holding the title's bytes (`*h = *t`), but the snapshot kept the old ones. A second range over the same piece (`host.rs` only folds identical ranges) then sees the bytes the first pass took from the title as shader writes, and puts them back over whatever the title wrote between the two passes. The fix, in `shadow.rs` only: - `writing` works out the parts of the range that no earlier range of the submission declared (new helper `outside`). A refresh leaves declared ranges alone, so only those parts can have been refreshed since the snapshot took them; where the snapshot already held them, they are taken again from the copy. No shader has written yet at that point: every list's deferred steps run before the submission executes. - `copy_back` gives the snapshot the title's bytes along with the copy (`*o = *t`), so a piece that was handled no longer differs. Cost: a byte is copied a second time only if it was first captured as part of a gap and declared later, at most once per submission; ranges declared again, and ranges that extend the snapshot, cost what they did. The body of `writing` moved under an `if`, hence the size of the diff. I have not run a title (I have no game files), so I cannot say which of its symptoms this accounts for; what it does is intermittent corruption of title memory in buffers shaders write. ## Tested Windows 11 Pro 10.0.26200, NVIDIA RTX 3080 Ti (driver 32.0.16.1656), rustc 1.98.1 (`x86_64-pc-windows-msvc`), no game data. `shadow.rs` had no tests. Two new ones, on a real host buffer (`Host::shared_buffer`; they return early without a host GPU, like the crate's other device tests) over three pages of plain heap memory standing in for title memory (not write-watched, so refreshes copy whole ranges). "The title writes" and "a shader writes" are single-byte writes into that memory and into the mapped copy. - `a_range_between_earlier_ones_keeps_the_titles_newer_bytes`: the sequence in the diagram. Checks the title's newer byte survives; controls: a byte the title wrote before the refresh and not after keeps its value, and the two bytes "the shader" wrote (one in an outer range, one in the late range) do go back to title memory; the snapshot is released at the end. - `overlapping_ranges_keep_the_titles_newer_bytes`: `[a, a+1 page)` and `[a, a+2 pages)` declared, a shader's byte and a title's byte in the first page, the title's byte written again between the two copy-backs. Checks the newer byte survives and the shader's byte went back. Results: - `main` (d2a4bae) + the tests only: `cargo test -p victory-gpu --lib shadow::tests`: **both fail** with `the title's newer byte: left: 1, right: 2`. - with the fix: `cargo test -p victory-gpu --lib`: **45 passed, 0 failed, 1 ignored**, three runs in a row. - `cargo clippy -p victory-gpu --tests`: nothing in `shadow.rs`. `rustfmt --check`: nothing in the lines changed (one existing difference, the `host` closure line in `writing`). - `cargo xtask policy`: 151 files ok. - Trial merges (`git merge-tree`): clean with #159 and #185; **conflicts with #124**, which restructures the same two functions (the snapshot moves into a `Before` struct). The two changes are independent in what they do; whichever goes in second needs a rebase, and I will do this one's if #124 goes first. Not tested: a title run; frame time in the world. AI-assisted (Claude Code); the tests above were run on the machine described. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
After a submission, copy_back writes into title memory the bytes of a
declared range that differ from the snapshot taken before the GPU ran,
unless the title's byte already equals the copy's. That is meant to be
what shaders wrote and nothing else: the title's threads write the same
memory while the GPU runs. Two things made other bytes differ from the
snapshot, and the copy's older value then went back over a byte the
title had written since:

- The snapshot spans the gaps between the ranges declared so far, taken
  from the copy as it was then. A range declared later inside such a gap
  was refreshed after that, so every byte its refresh brought in looked
  like a shader's write. writing now takes the parts of a range no
  earlier range declared again, where the snapshot already held them.
- copy_back leaves the copy holding the title's bytes for each piece it
  handled, but the snapshot kept the old ones, so a second declared range
  overlapping the first saw those bytes as a shader's. The snapshot now
  takes them too.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:57:04 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 06:03 UTC · original

Tested current PR head a2243adc60d86e1540989447981a8e8e5c27f2fe.

  • cargo test --locked -p victory-gpu --lib shadow -- --test-threads=1: 2 passed, 44 filtered out. a_range_between_earlier_ones_keeps_the_titles_newer_bytes declares outer pages before the middle page, after an earlier snapshot already spans that gap; it checks that refreshes and copy-backs keep the title's newer middle-page bytes while returning shader-written bytes. overlapping_ranges_keep_the_titles_newer_bytes checks the same protection when two declared ranges overlap and the title updates a byte between their copy-backs (copy-back tests).

The fixtures use a host shared buffer and return early if no host GPU is available, so the result is conditional on that resource. They do not run a title.

**@⁠jeremygold02** on GitHub · 2026-11-24 06:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/261#issuecomment-7123530129) Tested current PR head `a2243adc60d86e1540989447981a8e8e5c27f2fe`. - `cargo test --locked -p victory-gpu --lib shadow -- --test-threads=1`: 2 passed, 44 filtered out. `a_range_between_earlier_ones_keeps_the_titles_newer_bytes` declares outer pages before the middle page, after an earlier snapshot already spans that gap; it checks that refreshes and copy-backs keep the title's newer middle-page bytes while returning shader-written bytes. `overlapping_ranges_keep_the_titles_newer_bytes` checks the same protection when two declared ranges overlap and the title updates a byte between their copy-backs ([copy-back tests](https://github.com/Marvisak/VIctory/blob/a2243adc60d86e1540989447981a8e8e5c27f2fe/crates/victory-gpu/src/shadow.rs)). The fixtures use a host shared buffer and return early if no host GPU is available, so the result is conditional on that resource. They do not run a title.
Author
Owner

@⁠Vaximous on GitHub · 2026-11-24 11:01 UTC · original

Supporting data for this PR's direction. On main (and on my local branch before the fix), copy-back reads a 64 KiB piece of title memory, merges the GPU's bytes in and writes the whole piece back. The title keeps CPU-side records inside its GPU heap, so any title write elsewhere in that piece between the read and the write is undone. In GTA VI that showed up as a title crash: a hash-map value pointer the title had just stored reverted to null, and several worker threads faulted on it at once.

Writing back only the runs of bytes the shader changed stopped it: about 1,100 s of world time across 5 runs with no recurrence.

One performance note: one WriteProcessMemory-style call per run cost 1.3-2.2 s/frame in the world on my machine (88% of the submit thread in ZwReadVirtualMemory). Batching helps. When a piece has more than 8 runs, I store them as plain writes into the page just read, still leaving the gap bytes untouched. That brought copy-back back to ~200 ms/frame.

**@⁠Vaximous** on GitHub · 2026-11-24 11:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/261#issuecomment-7129547238) Supporting data for this PR's direction. On main (and on my local branch before the fix), copy-back reads a 64 KiB piece of title memory, merges the GPU's bytes in and writes the whole piece back. The title keeps CPU-side records inside its GPU heap, so any title write elsewhere in that piece between the read and the write is undone. In GTA VI that showed up as a title crash: a hash-map value pointer the title had just stored reverted to null, and several worker threads faulted on it at once. Writing back only the runs of bytes the shader changed stopped it: about 1,100 s of world time across 5 runs with no recurrence. One performance note: one `WriteProcessMemory`-style call per run cost 1.3-2.2 s/frame in the world on my machine (88% of the submit thread in `ZwReadVirtualMemory`). Batching helps. When a piece has more than 8 runs, I store them as plain writes into the page just read, still leaving the gap bytes untouched. That brought copy-back back to ~200 ms/frame.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:24 UTC · original

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-gpu/src/shadow.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/261#issuecomment-7148476486) <!-- victory-triage v1 head=a2243adc60d86e1540989447981a8e8e5c27f2fe verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-gpu/src/shadow.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/shadow.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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