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

Open
Maraco91 wants to merge 1 commit from upstream-pr/261 into main
Maraco91 commented 2026-11-24 01:53:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Maraco91 on 2026-11-24T01:53:20Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572806797 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/261). Originally posted by [Maraco91](https://github.com/Maraco91) on 2026-11-24T01:53:20Z. --- ## 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)
jeremygold02 commented 2026-11-24 06:56:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T06:03:12Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123530129 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/261#issuecomment-7123530129). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T06:03:12Z. --- 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.
Vaximous commented 2026-11-24 11:04:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Vaximous on 2026-11-24T11:01:10Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7129547238 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/261#issuecomment-7129547238). Originally posted by [Vaximous](https://github.com/Vaximous) on 2026-11-24T11:01:10Z. --- 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.
Marvisak commented 2026-11-25 01:07:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:24:25Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148476486 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/261#issuecomment-7148476486). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:24:25Z. --- <!-- 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 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/261:upstream-pr/261'
git switch 'upstream-pr/261'

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/261'
git switch 'upstream-pr/261'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/261'
git switch 'upstream-pr/261'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/261'
git switch 'main'
git merge --squash 'upstream-pr/261'
git switch 'main'
git merge --ff-only 'upstream-pr/261'
git switch 'main'
git merge 'upstream-pr/261'
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!261
No description provided.