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

Closed
github-bot wants to merge 1 commit from gh-pr/1374/head into gh-pr/1374/base
Owner

@⁠fuzeofficlal on GitHub · 2026-11-24 17:39 UTC · original
fuzeofficlal:fix/placed-copy-after-gpu-write-785 → main · +1402 −25 · 5 files · 1 commits · closed without merging

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-24 17:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/1374) `fuzeofficlal:fix/placed-copy-after-gpu-write-785` → `main` · +1402 −25 · 5 files · 1 commits · closed without merging 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.
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 #785.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:29:20 +00:00
github-bot closed this pull request 2026-11-26 04:29:20 +00:00
Author
Owner

@⁠wbkepler on GitHub · 2026-11-24 20:48 UTC · original

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.

**@⁠wbkepler** on GitHub · 2026-11-24 20:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/1374#issuecomment-7144084881) 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._
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:24 UTC · original · edited 2026-11-25 16:17 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 01:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/1374#issuecomment-7149516229) · edited 2026-11-25 16:17 UTC <!-- victory-triage v1 head=71f5dd7ec9427a792a9df7cfa493a73581d5f57b verdict=merge-candidate at=2026-11-25T16:17:28Z --> 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](https://claude.ai/code)_
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 04:06 UTC · original

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** on GitHub · 2026-11-25 04:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/1374#issuecomment-7152529096) 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).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 05:15 UTC · original

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).
**@⁠fuzeofficlal** on GitHub · 2026-11-25 05:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/1374#issuecomment-7153604933) 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).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 22:00 UTC · original

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 315cea18 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** on GitHub · 2026-11-25 22:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/1374#issuecomment-7175820909) 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 315cea18 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.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1374
No description provided.