GPU: a deferred copy of memory the submission's shaders write runs after the copy-backs #2810
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!2810
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2810"
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?
@kotekompotek on GitHub · 2026-11-25 21:12 UTC · original
kotekompotek:gpu/deferred-copy-after-gpu-writes→main· +668 −182 · 2 files · 4 commits · openSummary
Fixes #2781.
A title
CopyBufferRegion(or bufferCopyResource) recorded behind a dispatch ran its deferred CPU copy at submission preparation — beforeExecuteCommandLists— while the shader's writes reach title memory only in the post-fence copy-back. The copy therefore carried stale bytes. Symmetrically, a copy writing a destination the shaders also wrote had its result overwritten by the copy-back.Deferred copies/fills now check the title ranges the submission's lists have declared GPU-written so far (
Batch::gpu_writes, shared across all lists of the submission, so a copy in a later list sees an earlier list's writes). A copy touching a written range is queued onBatch::lateand runs after the copy-backs but before the bottom-of-pipe writes that signal completion to the title. A copy recorded before the write in step order still runs at once, matching the console's asynchronous GPU ordering.Behaviour changes to state
copy_mappedwrites zeros for unmapped source pages, and stops at the first unmapped page of the destination. This is the buffer copies' existing fallback, now shared byCopyMemoryToMemoryX.std::ptr::copy(memmove). The deferred path copies through the process-memory read/write pair, which makes no promise for overlapping source and destination. Overlapping copies are already undefined behaviour for the console API this implements (CopyMemoryToMemoryXis the title's memcpy), so no overlap handling is added; non-overlapping copies are unaffected.Later commits on this head
2180802— chained deferral: later copies touching a pending deferred range (either end) defer in order;Step::Fillstays at record time (its destination is the list's own upload memory); aGpuWriteover a pending destination clips the deferred copy.889d785—CopyMemoryToMemoryXgoes throughdefer_copylike the buffer copies; the last unconditionalStep::Run/HostList::deferusers are removed.e2a68d9— same-submission reads:Host::executesplitsExecuteCommandListsbefore a reader's segment and runs pending late copies/refreshes in between, so a reader in a later list sees the copy's bytes (a reader in the same list still sees stale bytes — no split point inside one list's step walk; stated in the test). Deferred sources are tracked and later writers to them defer in order (late_sourced).Test plan
host::copy_order::a_copy_past_a_gpu_write_sees_what_it_wrote— end-to-end throughHost::execute.host::copy_order::deferred_copies_chain_and_a_later_gpu_write_wins— chained copies, fills, GPU-write clipping.host::copy_order::a_late_copy_keeps_its_source_until_it_reads_it— source protection.host::copy_order::a_read_of_a_late_copy_sees_it_in_a_split_submission— split submissions; documents the same-list limit.cargo clippy -p victory-gpu --all-targetscleanGenerated with Devin
@Venom2054 on GitHub · 2026-11-25 21:21 UTC · original
I ran the five-case guest CopyBufferRegion test from 34fc326c twice on this PR's head, 9ba63034, using a physical RTX 5070 Ti (driver 621.01) with the D3D12 debug layer enabled. All five cases passed both times.
I used the same guest test and controls on all three revisions. 5bf64642 is the parent of 34fc326c. This PR branches from
13f91cae. The GPU and CPU source checks returned 55667788, adjacent sentinels stayed intact, and the device remained healthy. Debug message 1419 was already queued before the cases began. This verifies four-byte buffer-copy ordering on hardware. The in-game effect on BVH construction and lighting remains unmeasured.@fuzeofficlal on GitHub · 2026-11-25 21:30 UTC · original
Verified
9ba63034on Windows 11 (RTX 5090 + WARP,--features diagnostics):cargo xtask policy: 184 files ok;cargo clippy(default &--features diagnostics): 0 warnings.host::copy_order+memory::tests(excluding pre-existing baselinelists_share): 5/5 passed.--features diagnostics): 197 passed / 1 ignored, plusdynamic_slotsandlists_shareindividually passed (199/199).git merge-tree: clean againstorigin/main13f91ca, #2608, and #2611.One ordering edge case worth flagging in the same
Batchsubmission:batch.latedefers a copy whosesrcordstoverlapsbatch.gpu_writes, but does not record the deferred copy's owndstrange (B) as pending for later ops in the same batch (gpu_writtenonly checksbatch.gpu_writes). WhenB,C,Dare disjoint from shader-writtenA, three same-batch sequences can still run out of recording order:shader1 writes A -> Copy(dst=B, src=A) [deferred to batch.late] -> Copy(dst=C, src=B):gpu_written(B)is false, soB -> Cexecutes eagerly beforeA -> Bruns, copying staleBintoC.shader1 writes A -> Copy(dst=B, src=A) [deferred] -> shader2 reads B(Step::Refresh(B)/Step::Fill):Refreshruns inbatch.flush()beforeExecuteCommandLists, whileA -> Bruns after copy-back, soshader2sees staleB.shader1 writes A -> Copy(dst=B, src=A) [deferred] -> Copy(dst=B, src=D):D -> Bruns eagerly, thenbatch.laterunsA -> Bafterward and overwritesD's data.Tracking deferred
dstranges (so subsequent copies touchingBalso defer in order, and a shader read ofBforces a batch split/flush) would close all three chains.@HLVM04 on GitHub · 2026-11-25 23:56 UTC · original
An in-title count for the open "in-title verification" item, from the fork (fxceless/victory-fork), not this PR's code. The fork has the same order:
Host::submitruns each list's deferred CPU copies (run_deferred->copy_mapped) beforeExecuteCommandLists, and shader writes reach title memory only later.RTX 5070 Ti, Series S profile, one instrumented 200 s run:
One run for the counts, so they will vary with the scene.
@kotekompotek on GitHub · 2026-11-26 00:29 UTC · original
Useful data — thanks for instrumenting it in-title. The ~25% chained rate is exactly the case the
gpu_writes/lateordering covers: a late copy's destination stays pending for later readers because the copy runs after the copy-backs but before the writes that signal completion, so the chain can't read back a not-yet-written destination. Good to hear the same idea costs nothing measurable at 60 fps — matches the expectation (the deferred list just reorders work the submitting thread already did).The black-skin chase being unaffected is consistent with this fix's scope — it only removes stale reads at submission boundaries, not visual artifacts with other causes.
@AwsAli05 on GitHub · 2026-11-25 23:15 UTC · original · reviewed
The late-copy check misses dependencies through an earlier late copy. I added a final
pool 1 -> pool 3to the existing copy-order fixture atbcd9329, after it recordedpool 0 -> pool 1. Pool 0 is the GPU-written range. On the RTX 5070 Ti, pool 1 correctly received0xBB, while pool 3 received its old0x00instead of0xBB.The second copy runs during preparation because neither of its ranges is in
gpu_writes. A pending late copy's destination needs to participate in the dependency check so later consumers wait too. Please add the chained-copy regression alongside the direct cases.AI-assisted review and local synthetic test.
@Marvisak on GitHub · 2026-11-26 02:19 UTC · original · edited 2026-11-26 08:25 UTC
Automated triage for the maintainers.
Thanks for
889d785.copy_memory(CopyMemoryToMemoryX,list.rs) now records throughHostList::defer_copy, so it gets the samegpu_written/late_pendingordering as the buffer copies, and the write-watch guard ofcopy_mapped.Step::RunandHostList::deferlose their last caller and are removed.a_memory_copy_past_a_gpu_write_sees_what_it_wrotecovers the call through the real list entry point. That part reads right.Two behaviour changes come with it. Please state them in the description:
copy_mappednow writes zeros for unmapped source pages, and stops at the first unmapped page of the destination.std::ptr::copy, which is overlap-safe.copy_mappedgoes throughReadProcessMemory, which makes no promise for overlapping source and destination. If the title can callCopyMemoryToMemoryXon overlapping ranges, that needs handling.The two gaps from the last triage are still open on this head (
Batchinhost.rs):Refresh/Heap/Memoryrange over a deferred destination goes tolate_refresh, which runs afterExecuteCommandLists. A shader later in the same submission that reads that destination still reads the old shadow. Splitting the batch at that point would fix it. HLVM04's count in the thread shows this chain occurs in the title (about 25% of such copies).Step::Copythat writes that source, with neither range GPU-written or pending, still runs at once. Recording deferred sources as pending for later writers would close it.Tests that fail on this head for those two sequences, and pass with the fix, would change the verdict.
CI: passing.
Generated by Claude Code
@kotekompotek on GitHub · 2026-11-26 06:27 UTC · original
Both findings addressed in
2180802:Step::Fillno longer defers — back to an unconditionalcopy_mappedat record time. A fill's destination is the list's own upload memory, read by its commands while it runs; it cannot wait for the copy-backs. A GPU-written source stays old, as before the deferral.Batchnow tracks each deferred copy's title destination as pending. A laterStep::Copywhose source or destination touches a pending range defers in order behind it (so it reads the deferred bytes, and the later write still wins by ordering). A laterStep::Refresh/Heap/Memoryrange touching one is queued inlate_refreshand runs right after the late copies. And aStep::GpuWriterecorded over a pending destination clips the covered part of the deferred copy — console order has the later shader write win.copy_ordergaineddeferred_copies_chain_and_a_later_gpu_write_wins, which discriminates all three: the chained copy reads0xBB(not the stale pool byte), the fill lands0xAA(old source — proving it ran at record time), and the range written by a laterGpuWritekeeps the shader's0xEE.@kotekompotek on GitHub · 2026-11-26 06:53 UTC · original
CI failure on
2180802is a pre-existing flake, not this change:frame::tests::waits_end_on_ticks_and_count_framespolls the origin with a zero time-out twice in a row, and a host that wakes the thread a whole 2 ms tick late between the asks makes the second one return S_OK (the failure's left/right). #3080 hardens it.@kotekompotek on GitHub · 2026-11-26 07:54 UTC · original
889d785— same defect, adjacent callsite:CopyMemoryToMemoryXran as the only step kind that writes title memory outside the deferred copies, so it read a GPU-written source stale, lost a GPU-written destination, and did not chain past a deferred copy's destination. It now goes throughdefer_copy, which also guards its write from the write-watch and gives its unmapped pages the buffer copies' fallback; the now-unused title-memory step kind is removed.a_memory_copy_past_a_gpu_write_sees_what_it_wrotecovers it through the real list calls.@kotekompotek on GitHub · 2026-11-26 08:40 UTC · original
Both remaining gaps are addressed in
e2a68d9:late_refreshnote carries the run-index of its reader.Host::executesplitsExecuteCommandListsbefore a reader's segment and runs the pending late copies and refreshes in between, so a reader in a later list sees the copy's bytes. Phase-run refreshes also skip ranges that segment's shaders write, so they can no longer clobber GPU output. Limit, stated in the test: a reader in the same list as the producer still sees stale bytes — there is no split point inside one list's step walk.latetracks each deferred copy's source range, and aStep::Copywhose destination touches one defers in order behind it (late_sourced), so the later writer cannot clobber the bytes the deferred copy is waiting to take.clip_latekeeps shrinking both the tracked destination and source.copy_ordergained two discriminators:a_late_copy_keeps_its_source_until_it_reads_it(a writer to a deferred source defers; the late copy still reads0xBB) anda_read_of_a_late_copy_sees_it_in_a_split_submission(samples shadow bytes at each list's boundary:[0x55, 0xBB]across two lists, and documents the same-list limit). All 4copy_ordertests pass; clippy clean.The other 5 failures in a local
-p victory-gpurun reproduce on the base commit on this hardware (GTX 1050-class):a_texture_written_in_one_format…,compute_view_marks_rebind_after_depth_twin_conversion,dynamic_slots_are_held_until_released,lists_share…(the known baseline), and thewaits_end_on_ticks…flake.@kotekompotek on GitHub · 2026-11-26 17:25 UTC · original
Both behaviour changes are now stated in the description (unmapped pages copied as zeros; overlap undefined, matching the console API's memcpy semantics). The two gaps were addressed in
e2a68d9, pushed after the triage head889d785: same-submission readers see deferred bytes through split submissions, and deferred sources defer later writers in order - with the discriminating tests listed above.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.