Skip to content

GPU: implement CopyTextureRegion texture-to-buffer readbacks - #67

Open
alessiodeia wants to merge 10 commits into
Marvisak:mainfrom
alessiodeia:feat/texture-to-buffer-copy
Open

alessiodeia wants to merge 10 commits into
Marvisak:mainfrom
alessiodeia:feat/texture-to-buffer-copy

Conversation

@alessiodeia

Copy link
Copy Markdown
Contributor

What

Implements the previously stubbed texture-to-buffer path in CopyTextureRegion (texture to buffer copies not implemented):

  • Readback staging (crates/victory-gpu/src/host.rs): adds Host::readback_buffer (mapped D3D12_HEAP_TYPE_READBACK), HostList::readback, and HostList::defer_readback. After the GPU submission fence completes (and before bottom-of-pipe completion writes), completed readbacks are written into title memory via watch::writing + write_mapped.
  • Footprint layout & row-pitch alignment (crates/victory-gpu/src/resource.rs, crates/victory-gpu/src/list.rs): adds checked PlacedFootprint::layout(), translates console formats with pc_format, aligns the staging RowPitch to 256 bytes (D3D12_TEXTURE_DATA_PITCH_ALIGNMENT), and copies row-by-row when a title footprint uses an unaligned row pitch. Uploads CPU-written source textures (texture::upload) and transitions them to COPY_SOURCE before recording CopyTextureRegion.
  • End-to-end GPU test (crates/victory-gpu/src/resource.rs): uploads a 4×4 R8G8B8A8_UNORM pattern to a host texture and verifies three texture-to-buffer readbacks through ExecuteCommandLists: 256-aligned at a non-zero offset (256), unaligned packed row_pitch = 16 at offset 32, and a 2×2 source_box crop (1, 1)..(3, 3).

Tested

Windows 11 x64 (26300), Rust 1.98.1 (--locked), NVIDIA GeForce RTX 2070 SUPER (driver 617.42):

  • cargo test -p victory-gpu --locked --offline: 37 passed, 0 failed (including copy_texture_region_reads_back_into_title_buffer on hardware GPU).
  • cargo test --workspace --locked --offline (skipping sandboxed socket test only_loopback_is_reachable): all passed.
  • cargo clippy -p victory-gpu --all-targets --locked --offline -- -D warnings (both default and --features diagnostics): 0 warnings.
  • cargo --offline xtask policy: asset policy: 148 files ok.

AI-assisted; no game assets were used.

Implements the previously stubbed texture-to-buffer path in CopyTextureRegion:

- Add mapped D3D12_HEAP_TYPE_READBACK staging allocation (Host::readback_buffer, HostList::readback) and post-submission copy-back (HostList::defer_readback) that writes completed GPU readbacks into title memory via watch::writing + write_mapped before bottom-of-pipe completion writes.
- Align staging RowPitch to 256 bytes (D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) and copy row by row when a title footprint uses an unaligned row pitch; translate console formats with pc_format and check footprint arithmetic against overflow.
- Upload CPU-written source textures (texture::upload) and transition to COPY_SOURCE before recording CopyTextureRegion.
- Add an end-to-end GPU test in victory-gpu covering 256-aligned readback at a non-zero offset, unaligned packed row_pitch (16 bytes), and partial 2x2 source_box cropping.

Co-authored-by: BlueKaiman <360454391+BlueKaiman@users.noreply.github.com>
@raffclar

Copy link
Copy Markdown
Contributor

Reviewing

Comment thread crates/victory-gpu/src/list.rs Outdated
let mut to_location = dst;
to_location.resource = buffer.as_raw().cast_const().cast();
to_location.footprint.offset = 0;
to_location.footprint.format = crate::resource::pc_format(dst.footprint.format);

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.

Okay, verify this but I believe title formats 118–120 use 3 bytes per texel. Our PC GPUs will store them as 4 bytes per texel so check this one out.

Addresses review feedback on Marvisak#67: console formats 118..=120 (3-byte D16_UNORM_S8_UINT and its typeless views) map to 4-byte PC substitutes (45..=47). Check element(pc_format(format)) == element(format) before staging a texture-to-buffer readback so mismatched formats are skipped with a warning rather than mis-sizing the staging buffer or writing the wrong texel layout, and cover format 118 in the readback test.

Co-authored-by: BlueKaiman <360454391+BlueKaiman@users.noreply.github.com>
@alessiodeia

Copy link
Copy Markdown
Contributor Author

@raffclar Good catch — verified in resource.rs (element(118..=120) == (3, 1) while pc_format(118..=120) == 45..=47 has element == (4, 1)), so raw readback into a 3-byte footprint would both under-size the staging buffer and copy the wrong texel layout.

Fixed in b5c38fa:

  • Guarded copy_texture_region readbacks with element(host_format) != element(dst.footprint.format), skipping mismatched substitute formats cleanly with a one-time tracing::warn! instead of mis-sizing staging memory or failing Close().
  • Added a regression case for format 118 in copy_texture_region_reads_back_into_title_buffer verifying the command list closes/executes cleanly and leaves the destination buffer untouched.

Re-tested with cargo test -p victory-gpu --locked --offline (37 passed on RTX 2070 SUPER), cargo clippy -p victory-gpu --all-targets -- -D warnings (default + --features diagnostics, 0 warnings), and cargo xtask policy (148 files ok).

@alessiodeia
alessiodeia requested a review from raffclar November 24, 2026 00:23
@raffclar

Copy link
Copy Markdown
Contributor

Approved

@jeremygold02

jeremygold02 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

I checked b5c38fa on a physical RTX 5070, driver 621.01, with a synthetic 4×4 RGBA8 texture. The aligned, packed, cropped and format-skip assertions all passed.

One additional valid partial copy loses neighboring pixels. I prefilled a 4×4 destination with 0xA5, then copied a 2×2 source box into its top-left corner. All 4 requested pixels matched the source. All 12 pixels outside the box lost their prefilled values. The device remained healthy. The comparison counts only actual texels, excluding row padding.

The deferred host readback copies the width and line count of the destination footprint even when the GPU wrote a smaller rectangle into staging. The correction is in upstream draft #137, which depends on this PR. At 219e3ce, all 4 requested pixels matched and all 12 neighbors retained their values. A whole 4×4 source copied to (1,1) in a packed 6×6 destination also matched all 36 expected pixels. All 37 GPU library tests and diagnostics Clippy passed. These tests used synthetic textures.

Copy only the rows, columns and depth slices written into the staging footprint. Preserve surrounding texels for boxed and whole-subresource copies into larger destinations.

Refs: Marvisak#67
@DaviTostes

Copy link
Copy Markdown

Tested b5c38fa under Wine 11.22 + vkd3d-proton 3.1 on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build with cargo xwin ... --target x86_64-pc-windows-msvc: the PR's own test fails here, but only because of #128.

No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).

@alessiodeia

Copy link
Copy Markdown
Contributor Author

@jeremygold02 @martingouv @DaviTostes Thanks all for the thorough testing!

@MichaelSmithAI

Copy link
Copy Markdown

We ran the Wine retest suggested above on main ffe310e, keeping main’s deferred staging upload intact. On an RTX 4090 with Wine/vkd3d-proton, #67 read all 16 aligned pixels, all 16 packed pixels and all four pixels in the 2×2 crop correctly. This run needed no local guard bypass.

We also ran #137’s partial-copy control on the same setup. With #67 alone, untouched destination pixel (2,0) changed from [0xa5; 4] to zero. Adding #137 preserved the neighbors and passed all 52 destination-pixel checks across the two layouts. That confirms the fix for partial copies on this Wine/NVIDIA setup. The original Wine report used AMD.

Apply the existing texture ownership check before staging title memory. Exercise a GPU-cleared linear render target and a CPU-populated linear texture through the guest copy path.

Refs: Marvisak#67
PastiiJay added a commit to PastiiJay/klbf-lab that referenced this pull request Nov 24, 2026
PastiiJay added a commit to PastiiJay/klbf-lab that referenced this pull request Nov 24, 2026
Conflicts: list.rs kept the base's prepare_footprint_upload (packed 16-bit
widening) and the buffer-to-texture path that uses it, with Marvisak#67's readback
branch ahead of it; the readback now uploads the source from title memory
only when title_written, like the texture-to-texture path, so a render
target is not overwritten by stale staging. resource.rs kept both tests
(unordered_writes_reach_a_depth_target and the readback test).
PastiiJay added a commit to PastiiJay/klbf-lab that referenced this pull request Nov 24, 2026
zebraso added a commit to zebraso/VIctory that referenced this pull request Nov 24, 2026
…PipelineStateX (console caches): E_NOTIMPL, cleared out, named
@CaravelaLabs

CaravelaLabs commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

@alessiodeia this was fine on its own at b5c38fa, and #32 at 9c5328d. Those two did not compile together. This PR is now 9ea5068 on main 70564ae; I have not re-compiled that pair. On 9ea5068, host_list still returns a reference, and the two readback calls still pass list (now lines 570-571), so the clippy note below still applies.

Checked on main ffe310e, b5c38fa and 9c5328d. Each passed a Windows workspace check on its own. Merged, the texture-to-buffer readback did:

crate::texture::upload(list, src.resource, from_tex);
ensure_state(list, src.resource, COPY_SOURCE);

Those calls, as checked:

crate::texture::upload(list, src.resource, from_tex);
ensure_state(list, src.resource, COPY_SOURCE);

list there is what host_list returned. On #32 that is an Arc<HostList>. upload and ensure_state take &HostList, so the check stops with E0308: expected &HostList, found Arc<HostList>. @AndyLatimer already passes &list at the other copy sites for that reason.

Borrowing both arguments is enough for the type check on that merged tree. It cannot land on this PR while host_list still returns a reference: &list is a needless borrow, and cargo clippy -D warnings rejects it. Whichever of these two lands second has to add the &. Thanks @alessiodeia for catching that.

Compile only (cargo xwin check --workspace --all-targets --all-features --locked, Windows target), on ffe310e / b5c38fa / 9c5328d. No tests run.

AI-assisted; Windows workspace check of that two-PR merge, run on this machine.

alessiodeia and others added 2 commits November 24, 2026 22:47
…acks

main's watch::writing now returns a Writing guard that must live while
the write happens. Keep it across each readback write.

Co-authored-by: BlueKaiman <360454391+BlueKaiman@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alessiodeia

Copy link
Copy Markdown
Contributor Author

Updated for today's main (70564ae). The head is now 9ea5068: a merge of main plus one small commit. b5c38fa is still an ancestor, so #137 still stacks on it.

What main broke: watch::writing() now returns a #[must_use] Writing guard that has to live while the write happens. The two readback writes in host.rs called it as a statement, so the merge no longer compiled under -D warnings. 9ea5068 holds the guard across each write with let _writing = crate::watch::writing(..), the same pattern shadow.rs and copy_mapped use on main.

Verified on Windows 11 with an RTX 2070 SUPER (driver 621.40):

  • workspace clippy -D warnings, with and without --all-features: clean
  • cargo xtask policy: ok
  • cargo test -p victory-gpu: copy_texture_region_reads_back_into_title_buffer passes. Two other tests fail here, descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views. They fail the same way on clean 70564ae on this card (NumDescriptors >= HEAP_VIEWS / past the dynamic slots, the view heap is capped on NVIDIA), so they're unrelated to this PR.

@CaravelaLabs, thanks for re-checking against #32. I tried the borrow here, but on current main host_list still returns &HostList. upload(&list, ..) / ensure_state(&list, ..) then fail cargo clippy -D warnings with needless_borrow, so this PR can't carry it without failing CI. Whichever lands second has to adapt. If #32 merges first I'll add the & here right away. If this lands first, #32 needs the same &list at these two call sites that it already uses at the other copy sites.

@CaravelaLabs

Copy link
Copy Markdown
Contributor

@alessiodeia thanks, you're right.

I only type-checked the borrow. While host_list still returns a reference, &list fails cargo clippy -D warnings as a needless borrow, so this PR can't carry it without failing CI. I've corrected the note. Whichever of these two lands second adds the & at those two calls.

AI-assisted.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
PastiiJay pushed a commit to PastiiJay/klbf-lab that referenced this pull request Nov 25, 2026
Addresses review feedback on Marvisak#67: console formats 118..=120 (3-byte D16_UNORM_S8_UINT and its typeless views) map to 4-byte PC substitutes (45..=47). Check element(pc_format(format)) == element(format) before staging a texture-to-buffer readback so mismatched formats are skipped with a warning rather than mis-sizing the staging buffer or writing the wrong texel layout, and cover format 118 in the readback test.

Co-authored-by: BlueKaiman <360454391+BlueKaiman@users.noreply.github.com>
PastiiJay added a commit to PastiiJay/klbf-lab that referenced this pull request Nov 25, 2026
Conflicts: list.rs kept the base's prepare_footprint_upload (packed 16-bit
widening) and the buffer-to-texture path that uses it, with Marvisak#67's readback
branch ahead of it; the readback now uploads the source from title memory
only when title_written, like the texture-to-texture path, so a render
target is not overwritten by stale staging. resource.rs kept both tests
(unordered_writes_reach_a_depth_target and the readback test).
PastiiJay added a commit to PastiiJay/klbf-lab that referenced this pull request Nov 25, 2026

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work. This implements texture-to-buffer CopyTextureRegion (copy_texture_region in list.rs) via a readback buffer, copied into title memory after the submission completes (completion loop in host.rs); the format-size guard and watch::writing guard look right.

Blocking: the deferred copy writes the full destination footprint (every line at full row_bytes from PlacedFootprint::layout) regardless of x/y/z and the source box, so a copy that fills only part of the footprint overwrites title bytes outside the box with readback memory the GPU never wrote. D3D12 leaves those bytes untouched; two testers here reproduced it. The fix is in draft #137: please fold it in (or land both together) and add a sentinel test for a partial box (e.g. x = 1, a 2×2 box into a 4×4 footprint). Whichever of this and #32 lands second also needs the &list change discussed above.

CI: the check run failed, but only on two victory-shader tests this PR does not touch.


Generated by Claude Code

alessiodeia and others added 4 commits November 25, 2026 01:36
# Conflicts:
#	crates/victory-gpu/src/host.rs
…ytes around it

The readback test now also writes the crop box at a non-zero x into a
4x4 footprint and checks that every byte outside the box keeps the 0xa5
sentinel, the partial-copy case requested in review.

Co-authored-by: BlueKaiman <360454391+BlueKaiman@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alessiodeia

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed in 1e59257:

Verified on Windows 11 with an RTX 2070 SUPER (driver 621.40):

  • workspace clippy -D warnings, default and --all-features: clean
  • cargo xtask policy: ok
  • copy_texture_region_reads_back_into_title_buffer: passes on hardware
  • cargo test -p victory-gpu: 140 passed, 2 failed. Both failures (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views) also fail on clean main on this card (NVIDIA's capped view heap), so they're unrelated to this PR.
  • rustfmt --check on resource.rs shows the same 15 differences as main and none in this PR's code.

The &list change for #32 still waits for whichever of the two lands second.

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.

7 participants