Skip to content

GPU: read same-list GPU writes in placed buffer-to-texture copies - #1374

Closed
fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/placed-copy-after-gpu-write-785
Closed

fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/placed-copy-after-gpu-write-785

Conversation

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Fixes #785.

Summary

  • Detect when every byte read by a placed CopyTextureRegion footprint or pSrcBox (including pitched multi-row boxes and partial subsequent overwrites on the same Shadow buffer) was produced on the GPU earlier in the same command list without an intervening CPU write (HostList::written_shadow + PlacedFootprint::box_reads).
  • Source the placed texture copy directly from the GPU-written Shadow buffer (UNORDERED_ACCESS -> COPY_SOURCE -> UNORDERED_ACCESS), or rebase non-512-byte-aligned placed offsets onto a temporary aligned host buffer on the GPU while preserving D3D12_BOX-relative byte offsets.
  • Forward guest CopyBufferRegion and buffer CopyResource on the GPU when their source bytes come from an earlier GPU write in the same command list, keeping deferred CPU upload snapshots unchanged otherwise.

Verification

  • Added unit tests for block-compressed / 3D volume box_reads and end-to-end synthetic GPU readback tests covering whole-texture copies, D3D12_BOX sub-box copies, unaligned placed offsets, pitched multi-row GPU writes, partial GPU overwrites, and chained Dispatch -> CopyBufferRegion -> CopyTextureRegion:
    • NVIDIA GeForce RTX 5090 (VICTORY_D3D12_DEBUG=1): passed (0 debug errors)
    • AMD Radeon(TM) Graphics (VICTORY_D3D12_DEBUG=1): passed (0 debug errors)
    • Microsoft D3D12 WARP (VICTORY_D3D12_DEBUG=1): passed (0 debug errors)
  • cargo clippy --workspace --all-targets --all-features and cargo xtask policy: passed.

@wbkepler

Copy link
Copy Markdown
Contributor

I audited 5dd1e71 against main. The placed path orders correctly when the source copy really is in UNORDERED_ACCESS. The explicit UNORDERED_ACCESS -> COPY_SOURCE transition waits for the earlier dispatch's writes, and the copy reads them on the GPU with no CPU round trip. The pending range also keeps the submission-time refresh from overwriting them. I found one state problem, in the buffer-forwarding part:

record_gpu_buffer_copy infers the destination's state from Step::GpuWrite only (list.rs:505, has_gpu_write). A shadow buffer can also sit in explicit UNORDERED_ACCESS with no GpuWrite step. indirect::done (indirect.rs:186, called from draw.rs:1031/1055) leaves the argument buffer there, and the indirect path only records a Refresh for it. This sequence in one list breaks:

  1. ExecuteIndirect with arguments in title buffer B. B is now explicitly UNORDERED_ACCESS.
  2. A dispatch writes buffer A as a UAV.
  3. CopyBufferRegion from A to B, for example refilling the arguments for a second indirect pass.

Step 3 takes the dst_was_uav == false branch. It records CopyBufferRegion into B while B is in UNORDERED_ACCESS; a buffer that was explicitly transitioned out of COMMON is not implicitly promoted to COPY_DEST. It then records a COPY_DEST -> UNORDERED_ACCESS barrier whose before-state doesn't match. The debug layer should flag both, and nothing orders step 1's argument reads and clamp writes before the copy. Before this PR the copy happened on the CPU at submission, so B's GPU state was never touched.

The same thing can happen the other way, though it's less likely. readable() records the GpuWrite inside bind(), and the draw can still be dropped after that: table_failed at draw.rs:463, a missing index buffer copy, or the indirect !ran path. So a GpuWrite step doesn't guarantee the buffer was ever promoted to UAV in this list. In that case the source-side UNORDERED_ACCESS -> COPY_SOURCE barriers name a state the buffer isn't in; it is still COMMON.

Suggestion: track, per list, which shadow buffers are known to be in explicit UNORDERED_ACCESS. Set it from indirect::done, from this PR's restore barriers, and from UAV bindings only once the draw or dispatch is actually recorded. Use that instead of has_gpu_write. A focused test for the indirect-then-copy sequence above under VICTORY_D3D12_DEBUG=1 would cover it.

AI-assisted review (Claude Code); checked on Windows 11 / RTX 5090.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; this is source review of a large diff plus the PR's own Windows tests.

Placed buffer-to-texture copies now read same-list GPU writes: has_gpu_write/written_shadow walk the deferred Step::GpuWrite records backwards, require every requested range to be covered by writes into ONE shadow (Arc::ptr_eq, mixed shadows → None), subtract covered ranges, and only then fall back to CPU copies. The interval subtraction and the chosen-shadow consistency check are the right conservatism. 3 tests including a debug-layer guard. Please confirm on Windows that the placed-copy path actually hits this (the tests look host-gated).

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Recommendation: merge candidate (rebased; reviewed again against current main).

When the bytes a placed CopyTextureRegion reads, or a CopyBufferRegion / buffer CopyResource reads, were written on the GPU earlier in the same list, this PR copies them on the GPU (list.rs: placed_copy_source, copy_buffer_bytes, record_gpu_buffer_copy). Otherwise it keeps the CPU snapshot taken at submission. I checked these points:

  • HostList::written_shadow gives up on any overlapping deferred CPU copy or Step::Run.
  • PlacedFootprint::box_reads computes block-compressed and volume rows correctly, matching the new unit test.
  • The new unordered set lives in Deferred, so Reset clears it.
  • @wbkepler's state-tracking points are handled. mark/commit/discard_gpu_writes wrap every draw and dispatch path in draw.rs, indirect::done marks its buffers, and the count buffer gets UAV <-> INDIRECT_ARGUMENT barriers.

CI check passes on this head. The barrier correctness rests on the author's debug-layer runs (RTX 5090, AMD, WARP). Given the size (about 900 lines are GPU tests), a maintainer run with VICTORY_D3D12_DEBUG=1 on a real frame would be worthwhile.


Generated by Claude Code

@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Rebased onto 940d32f (64afee48) and addressed both explicit UNORDERED_ACCESS state-tracking cases pointed out by @wbkepler:

  1. Added per-list explicit UNORDERED_ACCESS host-buffer tracking (Deferred::unordered via set_unordered / is_unordered), marked from indirect::done, GPU copy restore barriers, and committed UAV bindings.
  2. Guarded draw, draw_indexed, dispatch, dispatch_mesh, execute_indirect, and clear_unordered with mark_gpu_writes / commit_gpu_writes / discard_gpu_writes so dropped draws or dispatches neither leave unexecuted Step::GpuWrite records behind nor transition COMMON buffers from UNORDERED_ACCESS.
  3. Added gpu_buffer_copy_tracks_indirect_and_dropped_uav_states covering ExecuteIndirect(B) -> Dispatch(UAV A) -> CopyBufferRegion(A -> B) -> CopyTextureRegion(B -> tex) and dropped-draw fallback under VICTORY_D3D12_DEBUG=1 (0 debug layer errors on RTX 5090, AMD Radeon Graphics, and WARP).

@fuzeofficlal
fuzeofficlal force-pushed the fix/placed-copy-after-gpu-write-785 branch from 64afee4 to 7593572 Compare November 25, 2026 05:15
@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Pushed 7593572e tightening two follow-up details on top of 64afee48:

  1. Separate ExecuteIndirect pCountBuffer transitions: when counter is distinct from args and is tracked in explicit UNORDERED_ACCESS on the current list (is_unordered), execute_indirect now wraps it in symmetric UNORDERED_ACCESS -> INDIRECT_ARGUMENT -> UNORDERED_ACCESS barriers.
  2. Full two-pass ExecuteIndirect + Reset end-to-end verification on Windows (@wbkepler, @catminou4): gpu_buffer_copy_tracks_indirect_and_dropped_uav_states now executes real ExecuteIndirect(buf_b) ([1, 1, 1]) -> dropped draw (discard_gpu_writes) -> UAV Dispatch(buf_a) -> CopyBufferRegion(buf_a -> buf_b) -> second ExecuteIndirect(buf_b) ([3, 1, 1], verifying atomic tally 1 + 3 = 4) -> CopyTextureRegion(buf_b -> tex) -> Reset reuse across RTX 5090, AMD Radeon Graphics, and WARP with VICTORY_D3D12_DEBUG=1 (0 debug layer errors).

Use the backing Shadow buffer in CopyTextureRegion when an earlier GPU
command in the same command list produced every byte read by the placed
footprint or source box without an intervening CPU write, rebasing
unaligned placed offsets on the GPU while preserving box-relative
offsets.

Also forward guest CopyBufferRegion and buffer CopyResource on the GPU
when their source range comes from an earlier GPU write in the same
list, tracking explicit per-list UNORDERED_ACCESS buffer state across
recorded shader writes, indirect execution (including separate count
buffers), dropped draws or dispatches, command-list resets, and GPU
copies while leaving deferred CPU snapshots intact otherwise.

Closes Marvisak#785.
@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers, second look (a review against current main, with builds and tests where they could change the verdict).

Closing because it is wrong against current main. Its own test gpu_buffer_copy_tracks_indirect_and_dropped_uav_states, run with the debug layer, fails on main plus this PR with three D3D12 state errors in the sequence raised in the thread: ExecuteIndirect(B), a dispatch that writes A, CopyBufferRegion(A to B), ExecuteIndirect(B). Since 315cea1 the indirect clamp reads the title's records through a root SRV, which puts B's copy in a shader-resource state this PR does not model. More broadly, it brings back explicit transitions on the copies of title buffers, which that commit removed by design after they caused state errors in the world. The gap in #785 is real in principle; a fix would have to keep that rule (for example by copying through a buffer the list owns) and needs a case from the title first.

If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Placed buffer-to-texture copy reads stale data after a same-list GPU write

4 participants