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.
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.