GPU: implement CopyTextureRegion texture-to-buffer readbacks #67
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!67
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/67/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@alessiodeia on GitHub · 2026-11-24 00:07 UTC · original
alessiodeia:feat/texture-to-buffer-copy→main· +790 −18 · 3 files · 10 commits · closed without mergingWhat
Implements the previously stubbed texture-to-buffer path in
CopyTextureRegion(texture to buffer copies not implemented):crates/victory-gpu/src/host.rs): addsHost::readback_buffer(mappedD3D12_HEAP_TYPE_READBACK),HostList::readback, andHostList::defer_readback. After the GPU submission fence completes (and before bottom-of-pipe completion writes), completed readbacks are written into title memory viawatch::writing+write_mapped.crates/victory-gpu/src/resource.rs,crates/victory-gpu/src/list.rs): adds checkedPlacedFootprint::layout(), translates console formats withpc_format, aligns the stagingRowPitchto 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 toCOPY_SOURCEbefore recordingCopyTextureRegion.crates/victory-gpu/src/resource.rs): uploads a 4×4R8G8B8A8_UNORMpattern to a host texture and verifies three texture-to-buffer readbacks throughExecuteCommandLists: 256-aligned at a non-zero offset (256), unaligned packedrow_pitch = 16at offset 32, and a 2×2source_boxcrop(1, 1)..(3, 3).Tested
Windows 11 x64 (26300), Rust
1.98.1(--locked), NVIDIA GeForce RTX 2070 SUPER (driver617.42):cargo test -p victory-gpu --locked --offline: 37 passed, 0 failed (includingcopy_texture_region_reads_back_into_title_bufferon hardware GPU).cargo test --workspace --locked --offline(skipping sandboxed socket testonly_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.
@raffclar on GitHub · 2026-11-24 00:11 UTC · original
Reviewing
@alessiodeia on GitHub · 2026-11-24 00:23 UTC · original
@raffclar Good catch — verified in
resource.rs(element(118..=120) == (3, 1)whilepc_format(118..=120) == 45..=47haselement == (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:copy_texture_regionreadbacks withelement(host_format) != element(dst.footprint.format), skipping mismatched substitute formats cleanly with a one-timetracing::warn!instead of mis-sizing staging memory or failingClose().118incopy_texture_region_reads_back_into_title_bufferverifying 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), andcargo xtask policy(148 files ok).@raffclar on GitHub · 2026-11-24 00:24 UTC · original
Approved
@jeremygold02 on GitHub · 2026-11-24 00:38 UTC · original · edited 2026-11-24 00:51 UTC
I checked
b5c38faon 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.@DaviTostes on GitHub · 2026-11-24 00:55 UTC · original
Tested
b5c38faunder Wine 11.22 + vkd3d-proton 3.1 on Arch Linux x86_64 (AMD RX 9060 XT, RADV 26.3.1), Windows build withcargo xwin ... --target x86_64-pc-windows-msvc: the PR's own test fails here, but only because of #128.victory_gpu: 34 passed, 3 failed:resource::tests::copy_texture_region_reads_back_into_title_buffer— the new test, all readback rows zero;argument_clamp_builds,buffer_data_reaches_the_host_texture).copy_mapped'sVirtualQueryguard skips under vkd3d-proton, so the texture is zero before any readback happens. With the per-step guard bypassed locally (title_dst = false), this test passes.No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).
@alessiodeia on GitHub · 2026-11-24 01:02 UTC · original
@jeremygold02 @martingouv @DaviTostes Thanks all for the thorough testing!
b5c38faso it can merge cleanly first without conflicting with @jeremygold02's follow-up #137 (219e3ce), which cleanly addresses the partial-region / non-zero(x, y, z)sentinel case both @jeremygold02 and @martingouv flagged.219e3ce) on Windows 11 + RTX 2070 SUPER (driver617.42):cargo test -p victory-gpu37 passed, 0 failed,cargo clippy -p victory-gpu --all-targets -- -D warningsclean,cargo xtask policyok.@MichaelSmithAI on GitHub · 2026-11-24 10:43 UTC · original
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.@raffclar on GitHub · 2026-11-24 00:14 UTC · original · edited 2026-11-24 00:15 UTC
On
crates/victory-gpu/src/list.rsline 555 (outdated):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.
@CaravelaLabs on GitHub · 2026-11-24 21:04 UTC · original · edited 2026-11-24 22:36 UTC
@alessiodeia this was fine on its own at
b5c38fa, and #32 at9c5328d. Those two did not compile together. This PR is now9ea5068on main70564ae; I have not re-compiled that pair. On9ea5068,host_liststill returns a reference, and the two readback calls still passlist(now lines 570-571), so the clippy note below still applies.Checked on main
ffe310e,b5c38faand9c5328d. Each passed a Windows workspace check on its own. Merged, the texture-to-buffer readback did:Those calls, as checked:
github.com/Marvisak/VIctory@b5c38fa9c2/crates/victory-gpu/src/list.rs (L567-L568)listthere is whathost_listreturned. On #32 that is anArc<HostList>.uploadandensure_statetake&HostList, so the check stops with E0308: expected&HostList, foundArc<HostList>. @AndyLatimer already passes&listat 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_liststill returns a reference:&listis a needless borrow, andcargo clippy -D warningsrejects 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), onffe310e/b5c38fa/9c5328d. No tests run.AI-assisted; Windows workspace check of that two-PR merge, run on this machine.
@alessiodeia on GitHub · 2026-11-24 21:47 UTC · original
Updated for today's
main(70564ae). The head is now9ea5068: a merge ofmainplus one small commit.b5c38fais still an ancestor, so #137 still stacks on it.What
mainbroke:watch::writing()now returns a#[must_use]Writingguard that has to live while the write happens. The two readback writes inhost.rscalled it as a statement, so the merge no longer compiled under-D warnings.9ea5068holds the guard across each write withlet _writing = crate::watch::writing(..), the same patternshadow.rsandcopy_mappeduse onmain.Verified on Windows 11 with an RTX 2070 SUPER (driver 621.40):
-D warnings, with and without--all-features: cleancargo xtask policy: okcargo test -p victory-gpu:copy_texture_region_reads_back_into_title_bufferpasses. Two other tests fail here,descriptor::host::tests::dynamic_slots_are_held_until_releasedandmemory::tests::lists_share_a_table_and_copies_keep_their_views. They fail the same way on clean70564aeon 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
mainhost_liststill returns&HostList.upload(&list, ..)/ensure_state(&list, ..)then failcargo clippy -D warningswithneedless_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&listat these two call sites that it already uses at the other copy sites.@CaravelaLabs on GitHub · 2026-11-24 22:04 UTC · original
@alessiodeia thanks, you're right.
I only type-checked the borrow. While
host_liststill returns a reference,&listfailscargo clippy -D warningsas 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.
@Marvisak on GitHub · 2026-11-25 00:23 UTC · original · edited 2026-11-25 15:54 UTC
Automated triage for the maintainers.
This pull request no longer merges cleanly into
main(conflicts incrates/victory-gpu/src/host.rs,crates/victory-gpu/src/list.rs,crates/victory-gpu/src/resource.rs). It will be reviewed once it is rebased onto the currentmain. The earlier verdict (merge-candidate) was for an older state ofmain.Generated by Claude Code
@alessiodeia on GitHub · 2026-11-25 00:41 UTC · original
Thanks for the review. Addressed in
1e59257:219e3ce,31979ad) are merged into this branch with their authorship kept. The deferred readback now writes only the rowsy..y+heightand the bytesx*element..(x+width)*elementthe copy covers, so title bytes outside the box are untouched, as D3D12 leaves them.copy_texture_region_reads_back_into_title_buffernow has three partial cases, each prefilled with0xa5. A 2×2 box at the origin and a 4×4 copy at (1, 1) of a 6×6 footprint come from #137. I added the case you asked for: the same 2×2 box atx = 1in a 4×4 footprint. Every byte outside each box must keep the sentinel.9ea5068), it fails atpixel (2, 0)in the 4×4 footprint (the sentinel is overwritten). With the fix it passes.watch::writingguard is held across both write paths (whole-block and per-row), as before.main(4118f33) with no conflicts. The only conflict was between #137 and main'sWritingguard inhost.rs: #137's per-box logic is kept, withlet _writing = …around the write.Verified on Windows 11 with an RTX 2070 SUPER (driver 621.40):
-D warnings, default and--all-features: cleancargo xtask policy: okcopy_texture_region_reads_back_into_title_buffer: passes on hardwarecargo 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 cleanmainon this card (NVIDIA's capped view heap), so they're unrelated to this PR.rustfmt --checkonresource.rsshows the same 15 differences asmainand none in this PR's code.The
&listchange for #32 still waits for whichever of the two lands second.@alessiodeia on GitHub · 2026-11-25 21:34 UTC · original
Closing as superseded by
main.5cb63d8("GPU: texture to buffer copies read back into the title's buffer") implements texture-to-bufferCopyTextureRegionthe same way: readback memory laid out as the title's footprint, copied into the title's buffer after the list runs, insidewatch::writing. It also covers the blocking point from the triage.PlacedFootprint::rows(at, size)writes only each row'sx * element .. (x + w) * elementbytes of the region, so bytes outside a partial box are left alone, as in @jeremygold02's fix. Merging this branch now would duplicate that code in another form.If the sentinel cases from this PR are wanted as extra coverage (a 2×2 box at the origin, the same box at
x = 1in a 4×4 footprint, and a whole copy at (1, 1) of a 6×6, each prefilled with0xa5), I can port them as a test-only PR on top ofread_back. Ask here and I will.Thanks @raffclar for the review, and @jeremygold02, @martingouv, @DaviTostes, @MichaelSmithAI and @CaravelaLabs for testing.
Pull request closed