GPU: copy-back writes whole 64 KiB pieces back and can undo the title's own writes #721
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory#721
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
@Dufftwt on GitHub · 2026-11-24 08:26 UTC · original
What's wrong
Shadow::copy_back(crates/victory-gpu/src/shadow.rs:190) brings the GPU's writes back into title memory one 64 KiB piece at a time:read_into).write_mapped(:225-228).The title's threads keep running during all three steps. If one writes any byte of the piece that the GPU didn't touch, between steps 1 and 3, step 3 overwrites it with the older value.
The function's own comment says why this matters: it copies back only the shader-written bytes because putting older bytes back "broke the title's own records (a fence list read as -1)". The merge loop does that per byte, but the write-back still covers the whole piece, so the same corruption can happen through this race.
Example
copy_backruns, a title thread bumps a counter at byte 64 of the same page.write_mappedputs the old counter back. The title's increment is lost.Evidence
A test reproduces it with real write-watched memory and a host copy. A title thread keeps bumping a word in each of 64 pages while
copy_backwrites a shader byte back into each page. Onmain(dd54de2), on GitHub'swindows-latest:The test watches one word in each of the 64 pages: 63 of the title's writes were put back to an older value. The test is below in case it helps whoever picks this up. The same module also holds the test for #722.
Tests (append to
crates/victory-gpu/src/shadow.rs)Suggested fix
In the merge loop, call
watch::writingandwrite_mappedonly on each run of bytes the GPU actually changed (*h != *o && *t != *h), not on allnbytes. The copy can keep taking*h = *tas now.Related
copy_backbut all keep the whole-piece write.@xstraok on GitHub · 2026-11-24 09:18 UTC · original
A fix along the lines suggested here, with the first test from this issue: https://github.com/xstraok/VIctory/tree/copy-back-runs (
a42f374on8e79c80). Opening a PR is refused for my account right now (does not have the correct permissions to execute CreatePullRequest), so it is here instead.copy_backnow writes back only the runs of bytes the GPU changed. They are written with plain stores afterwatch::writingopens the piece's guards. AWriteProcessMemoryper run made copy-back take 2.4–26 s per world frame under Wine (0.05–0.5 s before). The piece was just read through the kernel, so it is mapped; a title unmapping it before the stores would fault (markedshortcut:).Tested (Arch Linux, RX 9060 XT, Mesa 26.2.6 RADV, Proton Experimental + vkd3d-proton,
x86_64-pc-windows-gnu, tests run under Wine):cargo test -p victory-gpu --lib: 49 passed, 2 failed, 1 ignored; the same two fail onmainunder Wine (argument_clamp_builds,a_rejected_blob_gives_an_empty_library)8e79c80+ #190 + #454 + #431 + #493: without the fix the title froze after ~4 min in the world with two threads spinning in a hash-table probe (+0x50eb81a..+0x50eb83d); with it, ~10 min in the world to my time limit, no freeze, copy-back median 84 ms/frame. One run each, and the link between the freeze and this race is a guess.Not tested: Windows, NVIDIA, release builds.
Written with Claude Code (Claude Opus 5.5) from runs on my machine.
@Hiiva on GitHub · 2026-11-24 09:30 UTC · original
I reproduced both this race and #722 with deterministic synthetic tests on native Windows/MSVC, then added a combined fix on top of #261:
github.com/Hiiva/VIctory@d44fa0053bThe test injects a title write immediately after copy_back reads a piece. Before the changes, that write is overwritten. Restricting write-back to GPU-changed byte runs fixes this test while the separate write-watch refresh test still fails. Keeping the earlier copied epochs then fixes #722: the next refresh sees the concurrent title write instead of accepting the older host snapshot as current.
The four shadow tests pass, diagnostics Clippy passes with warnings denied, and the integrated workspace suite passes. These tests use tiny WARP allocations without shader dispatch. The branch retains scoped kernel-write leases (#723).
This implementation retains write_mapped for each changed run, preserving the existing unmapped-memory handling. xstraok's Wine timing above makes the possible cost clear. The integrated native Windows build has reached menus, intro and setup, but I have not yet measured world copy-back performance. The epoch fix also deliberately refreshes pages written by copy-back again.
@Vaximous on GitHub · 2026-11-24 13:59 UTC · original
Runtime evidence for this race, plus one more ready branch.
Branch: https://github.com/Vaximous/VIctory/compare/main...fix/copy-back-changed-runs (commit ba8548a, on
ffe310e). It is the fix suggested above:watch::writingandwrite_mappednow run only on each run of bytes the GPU changed. It also adds a deterministic test (a_title_write_during_copy_back_stays), where a hook between copy-back's read and its write performs a title write. With the whole-piece write the test fails; with the fix it passes. One file,crates/victory-gpu/src/shadow.rs, +88/-8.cargo test -p victory-gpu: 52 passed (1 ignored). Clippy-D warningsis clean.Runtime (RTX 4070 Ti, Windows 11): measured on our local integration build (main plus a set of open PRs, #261 among them), not on plain main. The title crashed in the world at title+0x247a098: a pointer that a title thread had just stored read back as null. With this change the crash did not come back in 5 runs.
Follow-up, not included: when a shader's writes are scattered, each run becomes its own kernel call. In the world that cost 1.3-1.9 s per frame of copy-back. Locally we store runs directly once a piece has more than 8 of them. That relies on guard handling from our integration build, so it is left out of this branch.
Related: #261 (earlier comment: #261), #722.
Written with Claude Code (Claude Opus 5.5); tested locally.
@Dufftwt on GitHub · 2026-11-24 14:01 UTC · original
@Vaximous thanks. A crash in the world on Windows that stops with the change is the first in-game sign on Windows that this race matters. Until now we only had xstraok's freeze under Wine.
The per-run cost applies to my branch too (
Dufftwt:fix/copy-back-only-gpu-bytes): it also makes onewrite_mappedcall, so oneWriteProcessMemory, per run. That makes four branches for this issue, and none of them is fast and tested on Windows yet:copy-back-runs(a42f374)d44fa00(on #261)write_mappedper runfix/copy-back-changed-runs(ba8548a)write_mappedper runfix/copy-back-only-gpu-bytes(3b2181a)write_mappedper runOne question about your path for more than 8 runs, since it decides whether plain stores need a fallback: what happens if the title unmaps a page, or makes it read-only, after copy-back has read it? Our vectored handler only takes stores into blocks it guarded itself and passes any other fault on. So the process would crash where
write_mappedskips the page.@Machad3x on GitHub · 2026-11-24 14:12 UTC · original
Fix for this, with your test (it failed 3/3 before, passes 3/3 after):
github.com/Machad3x/VIctory@3800387a5con the fork branch claude/gta6-shader-translation-crashes-705550. Shadow::copy_back now collects, per 64 KiB piece, the runs of bytes the GPU changed (host != before and title != host) and writes only those with write_mapped; a piece with more than 64 runs is still written whole to bound the kernel calls (marked shortcut). We hit this on an RTX 3080 Ti: after an in-place compute kernel using two root UAVs of the same address, the title faulted reading a pointer slot with -1 in its high dword. PR creation is limited to collaborators, so this is the diff for whoever lands it.@Dufftwt on GitHub · 2026-11-24 14:20 UTC · original
I've fixed the per-run cost in
Dufftwt:fix/copy-back-only-gpu-bytes(67125b3). For each 64 KiB piece, oneVirtualQuerychecks that the GPU's runs lie in committed read-write memory, and if so, they're stored directly. Elsewhere each run still goes throughwrite_mapped. A read-only page is left alone, as before, instead of faulting. A new test covers that.This was one
copy_backof 64 pages on GitHub'swindows-latest, in a release build, 3 rounds each:write_mappedper runSo scattered writes now cost about what whole pieces did.
@Machad3x thanks for the fix and the in-game report. One catch with writing pieces of more than 64 runs whole: it keeps the race exactly in the scattered pieces. Vaximous's numbers suggest those are common in the world.
@Dufftwt on GitHub · 2026-11-24 15:10 UTC · original
PRs are open again, so the fix is now #1024 (
Dufftwt:fix/copy-back-only-gpu-bytes,67125b3). It writes back only the GPU's runs, stored directly after one memory query per piece, with the timings above, and it also fixes #722.@Marvisak on GitHub · 2026-11-25 01:47 UTC · original
Automated triage for the maintainers.
Confirmed on current
main(940d32f), including the two newest commits.cd94d35adds a second copy-back path for pages that shaders mark as written, and940d32fstops that path from saving the page for its own copy. Both paths incrates/victory-gpu/src/shadow.rsstill read the piece, merge the GPU's bytes, and write the whole piece back withhost::write_mapped:Shadow::copy_back_or_not(the snapshot path, used bycopy_back), per 64 KiB piece;Shadow::copy_back_marked(the new marked-page path), per 64 KiB page;Shadow::copy_back_pagelikewise writes the whole page.A title write to bytes the GPU didn't change, landing between the
read_intoand thewrite_mapped, is still overwritten with the older value.watch::writingopens the guards only after the read. The new arm/saved mechanism keeps what the page held before the title's first write, which protects the shaders' bytes, not a title write made during the copy-back itself.The fix is in an open pull request: #1024 (by the reporter, also fixes #722). It was written against
ffe310eand is labelled as needing a rebase. The rebase should also cover the newcopy_back_markedandcopy_back_page, which have the same whole-piece write. The fork branches in the comments (xstraok, Hiiva, Vaximous, Machad3x) are alternatives to it.Generated by Claude Code