fix(gpu): preserve source and destination pixels in texture readbacks #137

Closed
github-bot wants to merge 4 commits from gh-pr/137/head into gh-pr/137/base
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 00:51 UTC · original
jeremygold02:codex/preserve-partial-readbacks → main · +664 −18 · 3 files · 4 commits · closed without merging

Depends on #67. Please merge #67 first. This branch adds two commits on top of #67 at b5c38fa. The first preserves pixels outside the destination region. The second preserves the GPU-written source during readback. GitHub’s full diff also includes the two commits from #67.

A texture-to-buffer copy may write only part of its destination. The GPU writes the requested rectangle into staging. The host then copies the entire destination footprint back into title memory, replacing pixels outside the rectangle. This follow-up copies only the affected row spans in each depth slice, using the source box or the selected subresource’s extent and the destination coordinates.

On a physical RTX 5070 (driver 621.01), I prefilled a 4×4 RGBA8 destination and copied a 2×2 source box into its top-left corner. At the dependency head all 4 requested pixels matched and 0 of the 12 neighboring pixels retained their values. With this change 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. Both cases are included in the synthetic test.

All 37 victory-gpu library tests passed on Windows at the follow-up head. Diagnostics Clippy with warnings denied and git diff --check passed. The original hardware verification on #67 records the comparison.

Integration with main at d2a4bae. The locally tested Git tree is identical to the clean merge of this branch with d2a4bae, including the merged test-module fix from #129. The GPU library suites passed with 44 default and 46 diagnostics tests, including the readback regression in both. Each configuration ignored one benchmark. Both library configurations also passed cargo check on the earlier GitHub merge preview. AI-assisted.

RTX 5070 mission-loading verification. Two interactive runs using the combined tree above reached a guest texture-to-buffer readback. In each run, a local probe queried the D3D12 queue fence and observed the requested submission value complete before copying two row spans (96 bytes total) into guest memory. The first run stopped at that checkpoint. The second continued for 20 more CPU present calls before manual closure. The synthetic sentinel test above remains the evidence that neighboring pixels are preserved. The live runs exercised #67 and this follow-up together. Later rendering and frame time remain unresolved.

GPU-written source regression at 31979ad2. On a physical RTX 5070 (driver 621.01), clearing a linear-layout 4×4 RGBA8 render target to magenta and immediately copying it through the guest texture-to-buffer path returned 00 00 00 00 at pixel (0,0) on 219e3ce. The readback path uploaded stale title memory over the GPU clear. Applying the existing texture::title_written check before the upload preserves all 16 magenta pixels. A separate CPU-populated linear texture returned all 16 expected pixels. The regression uses synthetic inputs. All 37 default GPU library tests, diagnostics Clippy with warnings denied, and git diff --check passed on Windows. The earlier mission trace lacks source layout and last-writer information for its two readback spans, so it cannot establish whether the mission exercised this case.

**@⁠jeremygold02** on GitHub · 2026-11-24 00:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/137) `jeremygold02:codex/preserve-partial-readbacks` → `main` · +664 −18 · 3 files · 4 commits · closed without merging **Depends on #67. Please merge #67 first.** This branch adds two commits on top of #67 at `b5c38fa`. The [first](https://github.com/jeremygold02/VIctory/commit/219e3cefc1b234fe6f8c93252a6ceae735a6897f) preserves pixels outside the destination region. The [second](https://github.com/jeremygold02/VIctory/commit/31979ad2a749e147ad6cd8e3219b0894c8002d71) preserves the GPU-written source during readback. GitHub’s full diff also includes the two commits from #67. A texture-to-buffer copy may write only part of its destination. The GPU writes the requested rectangle into staging. The host then copies the entire destination footprint back into title memory, replacing pixels outside the rectangle. This follow-up copies only the affected row spans in each depth slice, using the source box or the selected subresource’s extent and the destination coordinates. On a physical RTX 5070 (driver 621.01), I prefilled a 4×4 RGBA8 destination and copied a 2×2 source box into its top-left corner. At the dependency head all 4 requested pixels matched and 0 of the 12 neighboring pixels retained their values. With this change 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. Both cases are included in the synthetic test. All 37 `victory-gpu` library tests passed on Windows at the follow-up head. Diagnostics Clippy with warnings denied and `git diff --check` passed. The [original hardware verification on #67](https://git.gtahub.st/github/Marvisak--VIctory/pulls/67) records the comparison. **Integration with main at `d2a4bae`.** The locally tested Git tree is identical to the clean merge of this branch with `d2a4bae`, including the merged test-module fix from #129. The GPU library suites passed with 44 default and 46 diagnostics tests, including the readback regression in both. Each configuration ignored one benchmark. Both library configurations also passed `cargo check` on the earlier GitHub merge preview. AI-assisted. **RTX 5070 mission-loading verification.** Two interactive runs using the combined tree above reached a guest texture-to-buffer readback. In each run, a local probe queried the D3D12 queue fence and observed the requested submission value complete before copying two row spans (96 bytes total) into guest memory. The first run stopped at that checkpoint. The second continued for 20 more CPU present calls before manual closure. The synthetic sentinel test above remains the evidence that neighboring pixels are preserved. The live runs exercised #67 and this follow-up together. Later rendering and frame time remain unresolved. **GPU-written source regression at `31979ad2`.** On a physical RTX 5070 (driver 621.01), clearing a linear-layout 4×4 RGBA8 render target to magenta and immediately copying it through the guest texture-to-buffer path returned `00 00 00 00` at pixel (0,0) on `219e3ce`. The readback path uploaded stale title memory over the GPU clear. Applying the existing `texture::title_written` check before the upload preserves all 16 magenta pixels. A separate CPU-populated linear texture returned all 16 expected pixels. The regression uses synthetic inputs. All 37 default GPU library tests, diagnostics Clippy with warnings denied, and `git diff --check` passed on Windows. The earlier mission trace lacks source layout and last-writer information for its two readback spans, so it cannot establish whether the mission exercised this case.
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 <[email protected]>
Addresses review feedback on #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 <[email protected]>
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/VIctory#67
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: #67
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:51:45 +00:00
github-bot closed this pull request 2026-11-26 03:51:45 +00:00
Author
Owner

@⁠ghost on GitHub · 2026-11-24 01:12 UTC · original

AMD run: head 219e3ce. cargo test --workspace on main 9aef3d9 + #129 (main's victory-gpu tests don't compile without it) + this PR, AMD Radeon RX 9060 XT (RDNA4), Windows 11 Pro 26200, Rust 1.98.1, debug: copy_texture_region_reads_back_into_title_buffer and the rest of victory-gpu pass.

One run of the suite died with 0xC0000005 in victory_shader's tests. That's not from this PR (it doesn't touch that crate): it's the intermittent parallel-GPU-test crash on main, now #181.

AI use

  • Claude Code (Claude Opus 5.5) ran the commands and wrote this report from the logs; I checked it before posting.
**@⁠ghost** on GitHub · 2026-11-24 01:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/137#issuecomment-7118870054) AMD run: head `219e3ce`. `cargo test --workspace` on `main` `9aef3d9` + #129 (main's `victory-gpu` tests don't compile without it) + this PR, AMD Radeon RX 9060 XT (RDNA4), Windows 11 Pro 26200, Rust 1.98.1, debug: `copy_texture_region_reads_back_into_title_buffer` and the rest of `victory-gpu` pass. One run of the suite died with `0xC0000005` in `victory_shader`'s tests. That's not from this PR (it doesn't touch that crate): it's the intermittent parallel-GPU-test crash on `main`, now #181. **AI use** - Claude Code (Claude Opus 5.5) ran the commands and wrote this report from the logs; I checked it before posting.
Author
Owner

@⁠miniongk on GitHub · 2026-11-24 05:11 UTC · original

Tested 219e3ce (including #67) combined with #413, #320, #483 and #89 on an RTX 5080, driver 621.24, Windows 11 26300, local Xbox Series 1.0.2.1.

  • resource::tests::copy_texture_region_reads_back_into_title_buffer passed on hardware in 0.23 s.
  • cargo test --workspace on the integrated tree: 291 passed, 0 failed, 4 ignored.
  • On the real title path, the previous texture to buffer copies not implemented warning is gone.
  • The run still reaches the same explicit null-write fatal path in title code as without this patch. So the readback path is exercised, but it is not sufficient to resolve that later failure.

Only aggregate counts and error classes shared; no game data or raw game logs. AI-assisted (Kimi Code).

**@⁠miniongk** on GitHub · 2026-11-24 05:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/137#issuecomment-7122716365) Tested `219e3ce` (including #67) combined with #413, #320, #483 and #89 on an RTX 5080, driver 621.24, Windows 11 26300, local Xbox Series 1.0.2.1. - `resource::tests::copy_texture_region_reads_back_into_title_buffer` passed on hardware in 0.23 s. - `cargo test --workspace` on the integrated tree: 291 passed, 0 failed, 4 ignored. - On the real title path, the previous `texture to buffer copies not implemented` warning is gone. - The run still reaches the same explicit null-write fatal path in title code as without this patch. So the readback path is exercised, but it is not sufficient to resolve that later failure. Only aggregate counts and error classes shared; no game data or raw game logs. AI-assisted (Kimi Code).
Author
Owner

@⁠alessiodeia on GitHub · 2026-11-25 00:41 UTC · original

@⁠jeremygold02, the maintainer's triage on #67 asked to fold this fix into #67, so I've merged your two commits (219e3ce, 31979ad) into its branch with your authorship intact (#67 head 1e59257). I also added the requested sentinel case (a 2×2 box at x = 1 in a 4×4 footprint) next to your two partial cases, and merged current main. The only conflict was main's new watch::writing guard in host.rs. I kept your per-box logic and held the guard across the write. Thanks for the fix. If #67 lands, this draft should have nothing left to add, but I'll leave closing it to you.

**@⁠alessiodeia** on GitHub · 2026-11-25 00:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/137#issuecomment-7148772218) @⁠jeremygold02, the maintainer's triage on #67 asked to fold this fix into #67, so I've merged your two commits (`219e3ce`, `31979ad`) into its branch with your authorship intact (#67 head `1e59257`). I also added the requested sentinel case (a 2×2 box at `x = 1` in a 4×4 footprint) next to your two partial cases, and merged current `main`. The only conflict was main's new `watch::writing` guard in `host.rs`. I kept your per-box logic and held the guard across the write. Thanks for the fix. If #67 lands, this draft should have nothing left to add, but I'll leave closing it to you.
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-25 02:52 UTC · original · edited 2026-11-25 02:54 UTC

@⁠alessiodeia Thanks for folding both fixes into #67 and adding the offset sentinel case. I verified that both of my commits, 219e3ce and 31979ad, are in #67 at 1e59257. I've closed this draft so review can continue in #67.

**@⁠jeremygold02** on GitHub · 2026-11-25 02:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/137#issuecomment-7151304701) · edited 2026-11-25 02:54 UTC @⁠alessiodeia Thanks for folding both fixes into #67 and adding the offset sentinel case. I verified that both of my commits, `219e3ce` and `31979ad`, are in #67 at `1e59257`. I've closed this draft so review can continue in #67.

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!137
No description provided.