GPU: copy-back puts back only what shaders wrote, in two more cases #261
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/261"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by Maraco91 on 2026-11-24T01:53:20Z.
What
Shadow::copy_backcan 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_backtakes 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;
writingthen 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.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_backleaves the copy holding the title's bytes (*h = *t), but the snapshot kept the old ones. A second range over the same piece (host.rsonly 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.rsonly:writingworks out the parts of the range that no earlier range of the submission declared (new helperoutside). 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_backgives 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
writingmoved under anif, 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.rshad 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 withthe title's newer byte: left: 1, right: 2.cargo test -p victory-gpu --lib: 45 passed, 0 failed, 1 ignored, three runs in a row.cargo clippy -p victory-gpu --tests: nothing inshadow.rs.rustfmt --check: nothing in the lines changed (one existing difference, thehostclosure line inwriting).cargo xtask policy: 151 files ok.git merge-tree): clean with #159 and #185; conflicts with #124, which restructures the same two functions (the snapshot moves into aBeforestruct). 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
upstream-sync referenced this pull request2026-11-24 06:52:50 +00:00
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_bytesdeclares 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_byteschecks 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.
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 inZwReadVirtualMemory). 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.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
upstream-sync referenced this pull request2026-11-25 02:05:54 +00:00
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.