GPU: copy textures from GPU-backed buffer shadows #3338

Open
github-bot wants to merge 1 commit from github-bot/gh-3338 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 19:03 UTC · original
fuzeofficlal:fix/gpu-copy-texture-from-gpu-buffer → main · +955 −15 · 4 files · 1 commits · open

Summary

CopyTextureRegion (buffer to texture) previously always allocated a temporary upload buffer and filled it from CPU title memory at submission preparation time (HostList::defer_fill). Whenever a title wrote texel data into a buffer on the GPU—through a compute/graphics shader UAV or ClearUnorderedAccessViewUint/Float—and copied that buffer into a texture in the same command list or submission, CopyTextureRegion uploaded stale pre-GPU CPU memory instead of the GPU-written bytes.

Changes

  • shadow.rs: Add shadow::existing(address, len) to look up an already-created host Shadow copy (in BUFFERS or LOOSE) without allocating shadows for pure CPU upload buffers.
  • resource.rs: Add resource::buffer_unordered(this) to identify buffers created with D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS.
  • list.rs: In copy_texture_region (buffer -> texture):
    • Resolve a GPU-backed buffer's host shadow via shadow::find (when UAV-capable, covering out-of-order multi-threaded command list recording) or shadow::existing (for buffers inside an already-shadowed heap or window).
    • Refresh CPU changes into the shadow via shadow.refresh (also pinning Arc<Shadow> until list reset), issue a UAV barrier so prior GPU writes are visible, and copy directly from shadow.buffer at shadow.offset(data) when 512-byte aligned, or stage through a default_buffer (CopyBufferRegion + COPY_DEST -> COPY_SOURCE) when the shadow offset is not 512-byte aligned.
    • Keep pure CPU upload buffers without shadows on the existing upload-fill path with zero extra shadow allocations and intact per-call CopyBufferRegion ordering.
  • draw.rs: Track buffer copies promoted to COPY_SOURCE in State::copy_sources (cleared on reset/cut) so clear_unordered transitions them from COPY_SOURCE -> UNORDERED_ACCESS when needed, and returns cleared buffer copies to COMMON afterward without breaking prior implicit read promotions (e.g., root SRV / vertex buffer reads on the same buffer).

Testing

Added 7 end-to-end D3D12 tests in crates/victory-gpu/src/list.rs (list::tests::copy_texture_from_gpu_buffer::*) covering compute shader UAV writes at non-zero footprint offsets, ClearUnorderedAccessViewUint before and after CopyTextureRegion, out-of-order multi-list recording within one submission, non-512-byte-aligned shadow offsets, mixed CPU+GPU writes with source_box and destination (x, y, z), root SRV + CopyTextureRegion state compatibility on the same buffer, and pure CPU upload buffer ordering without shadow allocation. All 7 tests fail before the fix (5 GPU-write cases fail with stale CPU bytes) and pass on both hardware D3D12 and WARP with VICTORY_D3D12_DEBUG=1 and zero D3D12 debug errors, along with full victory-gpu test suites, workspace clippy (default and --all-features), formatting checks, and xtask policy.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 19:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/3338) `fuzeofficlal:fix/gpu-copy-texture-from-gpu-buffer` → `main` · +955 −15 · 4 files · 1 commits · open ## Summary `CopyTextureRegion` (buffer to texture) previously always allocated a temporary upload buffer and filled it from CPU title memory at submission preparation time (`HostList::defer_fill`). Whenever a title wrote texel data into a buffer on the GPU—through a compute/graphics shader UAV or `ClearUnorderedAccessViewUint`/`Float`—and copied that buffer into a texture in the same command list or submission, `CopyTextureRegion` uploaded stale pre-GPU CPU memory instead of the GPU-written bytes. ## Changes - **`shadow.rs`**: Add `shadow::existing(address, len)` to look up an already-created host `Shadow` copy (in `BUFFERS` or `LOOSE`) without allocating shadows for pure CPU upload buffers. - **`resource.rs`**: Add `resource::buffer_unordered(this)` to identify buffers created with `D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS`. - **`list.rs`**: In `copy_texture_region` (buffer -> texture): - Resolve a GPU-backed buffer's host shadow via `shadow::find` (when UAV-capable, covering out-of-order multi-threaded command list recording) or `shadow::existing` (for buffers inside an already-shadowed heap or window). - Refresh CPU changes into the shadow via `shadow.refresh` (also pinning `Arc<Shadow>` until list reset), issue a UAV barrier so prior GPU writes are visible, and copy directly from `shadow.buffer` at `shadow.offset(data)` when 512-byte aligned, or stage through a `default_buffer` (`CopyBufferRegion` + `COPY_DEST -> COPY_SOURCE`) when the shadow offset is not 512-byte aligned. - Keep pure CPU upload buffers without shadows on the existing upload-fill path with zero extra shadow allocations and intact per-call `CopyBufferRegion` ordering. - **`draw.rs`**: Track buffer copies promoted to `COPY_SOURCE` in `State::copy_sources` (cleared on `reset`/`cut`) so `clear_unordered` transitions them from `COPY_SOURCE -> UNORDERED_ACCESS` when needed, and returns cleared buffer copies to `COMMON` afterward without breaking prior implicit read promotions (e.g., root SRV / vertex buffer reads on the same buffer). ## Testing Added 7 end-to-end D3D12 tests in `crates/victory-gpu/src/list.rs` (`list::tests::copy_texture_from_gpu_buffer::*`) covering compute shader UAV writes at non-zero footprint offsets, `ClearUnorderedAccessViewUint` before and after `CopyTextureRegion`, out-of-order multi-list recording within one submission, non-512-byte-aligned shadow offsets, mixed CPU+GPU writes with `source_box` and destination `(x, y, z)`, root SRV + `CopyTextureRegion` state compatibility on the same buffer, and pure CPU upload buffer ordering without shadow allocation. All 7 tests fail before the fix (5 GPU-write cases fail with stale CPU bytes) and pass on both hardware D3D12 and WARP with `VICTORY_D3D12_DEBUG=1` and zero D3D12 debug errors, along with full `victory-gpu` test suites, workspace `clippy` (default and `--all-features`), formatting checks, and `xtask policy`.
Buffer-to-texture CopyTextureRegion previously always staged data from CPU title memory via HostList::defer_fill at submission preparation time. Any prior GPU write to the source buffer in the same command list or submission (such as compute UAV writes or ClearUnorderedAccessViewUint/Float) was silently ignored, uploading stale pre-GPU CPU bytes instead.

When the source buffer is UAV-capable or already backed by an existing host shadow copy, CopyTextureRegion now refreshes CPU changes into that shadow, emits a UAV barrier, and copies directly from the shadow buffer on the GPU (staging through a default buffer when the shadow offset is not 512-byte aligned). Buffer UAV clears also return the host buffer copy to COMMON after clearing and transition out of COPY_SOURCE if previously copied in the same list segment, while pure CPU upload buffers remain on the upload-fill path without allocating shadows.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 19:06:54 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 19:08 UTC · original · approved these changes

LGTM — verified on RTX 5090 (Windows / D3D12) against origin/main (63bcca4).

  • Copying textures directly from GPU-backed buffer shadows (handling non-512-aligned offsets, mixed CPU/GPU writes, UAV clears, root SRV reads, and out-of-order command lists) is thoroughly covered and passes cleanly on discrete NVIDIA hardware.
  • cargo test -p victory-gpu --lib list:: -- --test-threads=1: 11 passed, 0 failed (all 7 copy_texture_from_gpu_buffer::* tests passed).
  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 20 passed, 0 failed.
  • cargo test -p victory-gpu --lib resource:: -- --test-threads=1: 18 passed, 0 failed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 19:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/3338#pullrequestreview-6079334846) · **approved these changes** LGTM — verified on RTX 5090 (Windows / D3D12) against `origin/main` (`63bcca4`). - Copying textures directly from GPU-backed buffer shadows (handling non-512-aligned offsets, mixed CPU/GPU writes, UAV clears, root SRV reads, and out-of-order command lists) is thoroughly covered and passes cleanly on discrete NVIDIA hardware. - `cargo test -p victory-gpu --lib list:: -- --test-threads=1`: 11 passed, 0 failed (all 7 `copy_texture_from_gpu_buffer::*` tests passed). - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1`: 20 passed, 0 failed. - `cargo test -p victory-gpu --lib resource:: -- --test-threads=1`: 18 passed, 0 failed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 19:16 UTC · original

WARP/CPU verification of head 5d0b0dfd (claim: #1633)

Environment: Windows 11, WARP only (VICTORY_WARP=1, VICTORY_D3D12_WARP=1), dxil.dll from Windows SDK 10.0.26100.0 (VICTORY_DXIL), no physical GPU, no game files. -j 4, --locked, separate target dir. Control: origin/main @ 63bcca43. Merge: git merge-tree against current origin/main is clean (head is 1 commit ahead, 0 behind).

Check main 63bcca43 PR 5d0b0dfd
cargo test -p victory-gpu 231 passed, 0 failed, 1 ignored 238 passed, 0 failed, 1 ignored (+7)
cargo clippy -p victory-gpu --all-targets -- -D warnings clean clean
xtask policy 191 files ok 191 files ok

The 7 added tests (copy_texture_region_*) all pass on WARP. They do not run on control: with the PR's draw.rs/resource.rs/shadow.rs reverted to main, the test module fails to compile (buffer_unordered, shadow::existing, note_copy_source missing), so they depend on the fix. I did not review the design beyond that. Synthetic/unit tests only.

AI disclosure: run by Claude Code (Claude Sonnet 5.5), coordinated by Antigravity.

**@⁠oni-swr** on GitHub · 2026-11-26 19:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/3338#issuecomment-7201398710) **WARP/CPU verification of head `5d0b0dfd`** (claim: #1633) Environment: Windows 11, **WARP only** (`VICTORY_WARP=1`, `VICTORY_D3D12_WARP=1`), `dxil.dll` from Windows SDK 10.0.26100.0 (`VICTORY_DXIL`), no physical GPU, no game files. `-j 4`, `--locked`, separate target dir. Control: `origin/main` @ `63bcca43`. Merge: `git merge-tree` against current `origin/main` is clean (head is 1 commit ahead, 0 behind). | Check | main `63bcca43` | PR `5d0b0dfd` | |---|---|---| | `cargo test -p victory-gpu` | 231 passed, 0 failed, 1 ignored | 238 passed, 0 failed, 1 ignored (+7) | | `cargo clippy -p victory-gpu --all-targets -- -D warnings` | clean | clean | | `xtask policy` | 191 files ok | 191 files ok | The 7 added tests (`copy_texture_region_*`) all pass on WARP. They do not run on control: with the PR's `draw.rs`/`resource.rs`/`shadow.rs` reverted to main, the test module fails to compile (`buffer_unordered`, `shadow::existing`, `note_copy_source` missing), so they depend on the fix. I did not review the design beyond that. Synthetic/unit tests only. AI disclosure: run by Claude Code (Claude Sonnet 5.5), coordinated by Antigravity.
Author
Owner

@⁠kalberts0089-hash on GitHub · 2026-11-27 10:00 UTC · original

NVIDIA test report: RTX 4070 Ti (desktop, driver 621.01), Windows 11 Pro, rustc 1.98.1. Head 5d0b0df (base main 63bcca4). Claimed in #1633.

Confirmed:

  • On main, with this PR's 7 copy_texture_from_gpu_buffer tests put on it unchanged (minus the one shadow::existing assertion, which main can't build): 5 fail with stale bytes (for example texel (0, 0) got 0x0, expected 0xa0000000), and the pure-CPU and root-SRV cases pass. So #785 reproduces on this card.
  • On the head: 7/7 pass, also with VICTORY_D3D12_DEBUG=1 and with VICTORY_D3D12_DEBUG=1 VICTORY_D3D12_GBV=1 (--features diagnostics), with no D3D12 messages.
  • One sequence the tests don't have, added locally: a dispatch writes the buffer through a table UAV, CopyTextureRegion from it, a second dispatch writes it again, a second copy, then the read-back, all in one list. It passes under the debug layer and GBV.
  • The debug layer was on in these runs: a deliberately wrong barrier in the same test produced two D3D12 errors.
  • cargo test -p victory-gpu --lib -- --test-threads=1: 236 passed, 2 failed, 1 ignored (main: 229 / 2 / 1). The 2 failures are the known capped-heap ones (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views) and fail the same way on main; unrelated to this PR.

A design question, not something I could make fail:

  • clear_unordered now transitions the buffer it clears: COPY_SOURCE to UNORDERED_ACCESS when it was a copy source (draw.rs:1095-1101), and UNORDERED_ACCESS to COMMON after every buffer clear (draw.rs:1114-1119). For a title buffer that resource is the host copy of the title's memory. 315cea1 says those copies are never transitioned ("common between submissions, promoted by every use, ordered by unordered-access barriers"), and #1374 was closed for bringing such transitions back. Is this case meant to be an exception, or would the copy need to go through a buffer the list owns, as that closing note suggests?
  • On the evidence: in my wrong-barrier probe the debug layer gave the buffer's current state as COPY_SOURCE "(assumed at first use)", although a dispatch had written it through a table UAV earlier in the same list. So the plain debug layer doesn't see UAV use through a descriptor table, and zero debug errors says less here than it usually does about the write-then-copy order. GBV reported nothing either; I don't know whether its state tracking covers this.

Not tested: cargo test -p victory-shader (this PR doesn't touch it), the ExecuteIndirect and vertex-buffer sequences from the #1374 thread, and a title (needs the game).

Synthetic tests only, no game files. AI-assisted (Claude Code).

**@⁠kalberts0089-hash** on GitHub · 2026-11-27 10:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/3338#issuecomment-7215355370) NVIDIA test report: RTX 4070 Ti (desktop, driver 621.01), Windows 11 Pro, rustc 1.98.1. Head `5d0b0df` (base main `63bcca4`). Claimed in #1633. Confirmed: - On main, with this PR's 7 `copy_texture_from_gpu_buffer` tests put on it unchanged (minus the one `shadow::existing` assertion, which main can't build): 5 fail with stale bytes (for example `texel (0, 0) got 0x0, expected 0xa0000000`), and the pure-CPU and root-SRV cases pass. So #785 reproduces on this card. - On the head: 7/7 pass, also with `VICTORY_D3D12_DEBUG=1` and with `VICTORY_D3D12_DEBUG=1 VICTORY_D3D12_GBV=1` (`--features diagnostics`), with no D3D12 messages. - One sequence the tests don't have, added locally: a dispatch writes the buffer through a table UAV, `CopyTextureRegion` from it, a second dispatch writes it again, a second copy, then the read-back, all in one list. It passes under the debug layer and GBV. - The debug layer was on in these runs: a deliberately wrong barrier in the same test produced two D3D12 errors. - `cargo test -p victory-gpu --lib -- --test-threads=1`: 236 passed, 2 failed, 1 ignored (main: 229 / 2 / 1). The 2 failures are the known capped-heap ones (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`) and fail the same way on main; unrelated to this PR. A design question, not something I could make fail: - `clear_unordered` now transitions the buffer it clears: COPY_SOURCE to UNORDERED_ACCESS when it was a copy source (`draw.rs:1095-1101`), and UNORDERED_ACCESS to COMMON after every buffer clear (`draw.rs:1114-1119`). For a title buffer that resource is the host copy of the title's memory. 315cea1 says those copies are never transitioned ("common between submissions, promoted by every use, ordered by unordered-access barriers"), and #1374 was closed for bringing such transitions back. Is this case meant to be an exception, or would the copy need to go through a buffer the list owns, as that closing note suggests? - On the evidence: in my wrong-barrier probe the debug layer gave the buffer's current state as COPY_SOURCE "(assumed at first use)", although a dispatch had written it through a table UAV earlier in the same list. So the plain debug layer doesn't see UAV use through a descriptor table, and zero debug errors says less here than it usually does about the write-then-copy order. GBV reported nothing either; I don't know whether its state tracking covers this. Not tested: `cargo test -p victory-shader` (this PR doesn't touch it), the `ExecuteIndirect` and vertex-buffer sequences from the #1374 thread, and a title (needs the game). Synthetic tests only, no game files. AI-assisted (Claude Code).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3338/head:github-bot/gh-3338'
git switch 'github-bot/gh-3338'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3338'
git switch 'github-bot/gh-3338'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3338'
git switch 'github-bot/gh-3338'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3338'
git switch 'main'
git merge --squash 'github-bot/gh-3338'
git switch 'main'
git merge --ff-only 'github-bot/gh-3338'
git switch 'main'
git merge 'github-bot/gh-3338'
git push origin 'main'
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!3338
No description provided.