GPU: a marked page's copy-back writes only the bytes the shaders changed, with plain stores #2918
No reviewers
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!2918
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2918"
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?
@ZeOs360 on GitHub · 2026-11-25 23:55 UTC · original
ZeOs360:pr/marked-copyback-v8→main· +612 −45 · 4 files · 3 commits · openWhat
After a submission whose shaders marked a page as written through views,
Shadow::copy_back_markedwrites the copy's whole 64 KiB page into titlememory when the title had not written the page since the copy took it
(the
saved == Nonearm). The title's threads write those pages too, sinceits records share the GPU heap. A title write that lands after that check
and before the write is put back to the copy's older bytes. Where the page
was saved, the merged page is written whole as well, so a title write
between its read and the write is undone the same way. This is the
failure
Shadow::copy_backalready merges against: its comment records thecopy's bytes going back over a fence list the title then read as -1.
This PR changes the copy-back to:
the title did to it (
Tracked::finish, which replacesunarmandsaved). A title write lands wholly before that (the page changed, itsbytes from before saved if armed) or after it;
bytes from before, with no runs joined across gaps. A title write
anywhere else in the page stays, and one made after the read is not undone;
and guard it first and read it again after, so the copy also gets a title
write that landed meanwhile.
Why plain stores
Exact runs are many and small. In the prologue's intro on
940d32ftherewere about 3.3 million runs per frame, about 3.6 bytes each (on current
main, 1,400 to 1,900 runs per marked page, 7 to 10 bytes each). OneReadProcessMemoryper run (about 0.4 µs each, more under load) made themarked copy-back take 1.1 to 3.5 s per frame for 1 to 11 MB (measured on
940d32f).The runs are written with plain stores. The first commit does it with
watch::store, arep movsbroutine with unwind data. A destination pagethat is not writable faults into the existing write handler:
a title's own guard page) ends the store. The handler resumes the routine
at its end, and what is left goes through the kernel (
host::write_mapped)as before.
The second commit makes exact runs cheaper:
watch::store_runswrites all of a page's runs in one call (runs of upto 16 bytes a byte at a time, longer ones with
rep movsb). A fault endsthe call, which returns the run it was at; that run and the rest go
through the kernel.
changed_runscompares 64 bytes at once (SSE2) into a mask of the bytesthat differ, and reads the runs from the mask's runs of set bits: a few
steps per run, none per byte (tested against the byte-by-byte search).
the runs are merged only where it holds the page in part or cannot read
it again. The page and the list of runs are kept by the thread, not made
per page.
Cost in the intro's scene, averaged over the frames that copied back more
than 500 marked pages (one run of the game each; the third commit changes
only tests):
13f91ca219878amain(whole-page writes)On the current base exact runs cost about 13 µs a page more than the
whole-page write: about 16 ms a frame at about 1,200 marked pages, on
frames of a second or more. The runs differ from run to run of the game
(1,400 to 1,900 a page), so these single runs are rough.
Relation to other PRs
bytes (
SPAN_GAP) to keep the kernel writes affordable, which stillrewrites the unchanged bytes in those gaps. With plain stores, exact runs
(gap 0) cost about 1.25 times the whole-page write (see the table above),
so no gap is needed. This PR does not use #2242's code.
perf/copy-back-spans) with the same fallback idea. That work has no PR,and this one was written independently.
mainmerged since: armed page runs (#2147), saving apage before any guard is opened and updating saved bytes on a partial
refresh (#2239, #2377), and partial and edge pages (#2172).
Tests
shadow::tests::marked_copy_back_keeps_a_title_write_made_meanwhilewrites the page from the title while the copy-back runs, once while the
page is armed and once after the copy-back asked. With the whole-page
write, both cases fail, and the title's bytes come back as
ff ff ff ff.With this change both pass.
watch::tests::stores_mark_guarded_pages_and_stop_at_unwritable_oneswrites short and long runs and covers four destinations: a guarded page
(opened and marked changed, also from inside
rep movsb), a read-onlypage, a decommitted page, and a title guard page, plus empty and reversed
runs.
shadow::tests::changed_runs_are_those_byte_by_bytechecks the64-byte search against the byte-by-byte one on random data of many
lengths and densities, and on runs at block edges, in the tail and in
slices that do not start on 16 bytes.
title memory themselves (eight in
shadow, one inhost) hold offsubmissions (
Host::no_submissions, tests only), asHost::executedoes around its own copy-back. Another test's submission in parallel
could refresh their copies or take their marks meanwhile, which is why
partial_refresh_under_marked_range_updates_saved_bytessometimes failedon
main; the longer copy-back of this PR made it and two more fail in13 of 30 parallel runs. With the lock: no failure in 40 parallel runs of
the crate's tests.
cargo test -p victory-gpu: 234 passed.cargo clippy --workspace --all-targets -- -D warnings, with and without--all-features: clean.cargo xtask policy: ok.victory-shadertests fail on my machine with a DXIL validationerror (
rectangles_get_their_fourth_corner,elements_sharing_a_row_keep_their_order). This PR does not touch thatcrate, and it is identical to
mainthere.In game
Machine: RX 7800 XT 16 GB, Ryzen 5 5600, 16 GB RAM, Windows 11, Series X
profile, diagnostics builds. My own copy of the title was used; only log
lines, sizes and timings are reported here.
13f91cawith this PR, the intro's scene ran 493 s (one runwith the first commit) without the -1 read, against 487 s for one stock
run. Neither run hit it, so this says nothing either way about the crash;
it shows the change runs in game.
940d32f, one of two stock runs hit a title read of address -1 atimage+0x4f400a9, about 64 frames into the intro's scene. Builds with the
view-mark fixes ran up to about 332 frames there without it. Those builds
carried more than this change, so this does not show that this change
alone removes that crash.
mainI make no in-game claim about the -1 read: one runcannot show whether it became rarer. The race is still in the code, and
the test above reproduces it.
CreateCommittedResourceforshared (CUSTOM, L0) buffers began failing with E_OUTOFMEMORY after 2.5
to 5 minutes, and the run ended 3.5 to 9.5 minutes in. This happens on
mainas well and is unrelated to this change. After the failuresbegan, some runs stopped within a minute with "a host command list did
not close" (once with E_OUTOFMEMORY as its error). That happened with
this PR, and also with a build that does not contain it; stock
mainand the first commit alone ran about 6 more minutes, until the title
stopped. So I take it to follow from the failed allocations, not from
this change. The cost numbers above come from the first minutes in the
intro, before a run was stopped by me or by that failure.
AI-assisted: written with Claude Code (Claude Opus 5.5) and reviewed by
Gemini 4 Neon. I read the code and the reviews, and ran the in-game tests
on my machine.
@Kajanos on GitHub · 2026-11-26 00:07 UTC · original
Windows 11 / NVIDIA test run, head
eed0c767c8merged ontomain63bcca43c6.Setup: Windows 11 Home build 26300, RTX 5080, NVIDIA 621.40, rustc 1.98.1 MSVC, VS 2022 Build Tools, Windows SDK 10.0.26100 (
dxil.dllfrom the SDK).cargo test --locked -p victory-gpu: 234 passed, 0 failed, 1 ignored (main: 231).Not run: the retail title, clippy, and other crates.
AI-assisted (Claude Code)
@MeMako on GitHub · 2026-11-26 03:30 UTC · original
Independent verification of
eed0c76on AMD Ryzen 9 7950X + AMD Radeon RX 9070 XT (adapter 0, driver32.0.32029.2010, 32 GB RAM, Windows 1110.0.26200x64).CI checks & tests:
cargo clippy -p victory-gpu -p victory --all-targets --locked -- -D warnings(and--all-features): passcargo test -p victory-gpu --locked: 243 passed, 0 failed (includingmarked_copy_back_keeps_a_title_write_made_meanwhile,stores_mark_guarded_pages_and_stop_at_unwritable_ones, andchanged_runs_are_those_byte_by_byte)Live title run (release build, warm shader & pipeline cache):
(Note: merged cleanly onto a local RX 9070 XT branch on
842b0cbwith a separate-handle workaround equivalent to #2122,DYNAMIC = 1 << 19, #2051, #2472, and a local timing breakdown ofcopy_back_marks; before/after compared against the exact same branch without #2918.)draws >= 500)n=50)n=97)2,4442,66297.9 MB(whole pages)35.5 MB(changed bytes only)copy_back_markstime (median)54.9 ms(22.9 µs/page)81.7 ms(32.2 µs/page,+9.3 µs/page)read 0xffffffffffffffff)1(previous run also hit1)0across 8+ minutes in worldConsistent with your Ryzen 5 5600 measurements (
+13 µs/pagevs+9.3 µs/pagehere on a 7950X), and eliminates ~62 MB/frame of whole-page overwrites over unchanged title memory.No game data, shader names, or screenshots are included, per
docs/asset-policy.md. Hardware runs were performed by me on my 7950X / RX 9070 XT; comment drafted with Antigravity.@Marvisak on GitHub · 2026-11-26 04:31 UTC · original · edited 2026-11-26 07:23 UTC
Automated triage for the maintainers.
Needs work: needs the maintainer's decision (unchanged). Thanks for the comparison. I checked the one factual point that bears on the choice: #2242 currently does not merge with main (conflicts), while this PR merges cleanly. The head is unchanged, and the code in
watch.rsandshadow.rscopy_back_markedstill reads correctly.The open questions are for the maintainer:
global_asm!store routine, with SEH unwind data and a vectored handler that moves RIP, is acceptable;The measured cost per marked page (1.25x to 1.4x) is lower than what is reported for #2242.
CI: passing.
Generated by Claude Code
@ZeOs360 on GitHub · 2026-11-26 04:44 UTC · original
To help with the choice the triage asks for, how this PR and #2242 differ:
copy_back_markedcopy_back_marked,copy_back_pageand the snapshotcopy_backstore_runs); at a page that is not writable they stop, and the rest goes through the kernel copyshadow.rs)On the
global_asm!: exact runs need one write per run, and a kernel copy costs about 0.4 us each; r4tedx measured exact runs through it at 509 us per page with one changed word per 64 bytes (synthetic, on #2242). Plain stores need a way past a store into a page the title unmapped or protected meanwhile. Here the guard handler opens our own guarded pages as usual; any other write fault between the routine's labels moves RIP to the routine's exit, which returns the run it was at. r4tedx's fork branchperf/copy-back-spans, mentioned on #2242 before this PR, uses the same approach (an inlinerep movsbthat the handler steps past).If you'd rather not have the assembly, this can go back to a kernel copy per run (at the cost above), or be closed in favor of #2242 once it is rebased, whichever you prefer. If this PR is the one you keep, I'll bring
copy_back_pageand the snapshot path over to the same pieces in a follow-up, which covers what #2242 adds.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.