GPU: copy depth twins directly instead of through a scratch #2735

Open
Therecalloftheonion wants to merge 1 commit from upstream-pr/2737 into main
Therecalloftheonion commented 2026-11-25 19:23:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Therecalloftheonion on 2026-11-25T19:23:47Z.


What

Subresources of one format family copy straight between a depth texture and its writable twin instead of through a scratch buffer. D32 twins now exist at all: the previous exact-format check rejected them, so shader writes to D32 depth were dropped. The scratch remains for converted 16-bit planes and is created only when a route needs conversion.

Why

GPU memory: a twin otherwise allocates a full scratch buffer on top of the twin texture. A D32 twin now takes one texture instead of two, and a depth-stencil texture's stencil plane (same format family) no longer goes through the scratch.

Testing

  • New hardware test a_d32_twin_copies_directly_and_takes_no_scratch: creates a D32 depth texture and its twin, uploads known texels, performs the real depth-to-color copy, reads it back, checks every texel, and checks the device was not removed. Passes on the RX 6700 XT.
  • cargo test -p victory-gpu --lib: 196 passed.

Not tested on NVIDIA; the direct copy relies on DXGI format-family compatibility, as the D3D12 copy rules allow.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6611992309 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2737). Originally posted by [Therecalloftheonion](https://github.com/Therecalloftheonion) on 2026-11-25T19:23:47Z. --- ## What Subresources of one format family copy straight between a depth texture and its writable twin instead of through a scratch buffer. D32 twins now exist at all: the previous exact-format check rejected them, so shader writes to D32 depth were dropped. The scratch remains for converted 16-bit planes and is created only when a route needs conversion. ## Why GPU memory: a twin otherwise allocates a full scratch buffer on top of the twin texture. A D32 twin now takes one texture instead of two, and a depth-stencil texture's stencil plane (same format family) no longer goes through the scratch. ## Testing - New hardware test `a_d32_twin_copies_directly_and_takes_no_scratch`: creates a D32 depth texture and its twin, uploads known texels, performs the real depth-to-color copy, reads it back, checks every texel, and checks the device was not removed. Passes on the RX 6700 XT. - `cargo test -p victory-gpu --lib`: 196 passed. Not tested on NVIDIA; the direct copy relies on DXGI format-family compatibility, as the D3D12 copy rules allow.
passerbyyhx commented 2026-11-25 20:06:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-11-25T19:59:52Z.


On a35eb773, twin::tests::a_d32_twin_copies_directly_and_takes_no_scratch passed on a GeForce RTX 4090 Laptop GPU (driver 621.14, Windows 11, Rust 1.98.1 MSVC). I ran the unmodified PR head first, then repeated the test with a diagnostic print after the final assertions to confirm it reached readback. The D3D12 device LUID resolved to the NVIDIA adapter. All 16 texels of the generated 4×4 D32 texture matched after the depth-to-color copy. The test recorded one direct copy and no scratch buffer. Both runs used the synthetic fixture and no game files.

cargo test --locked --offline -p victory-gpu --lib twin::tests::a_d32_twin_copies_directly_and_takes_no_scratch -- --exact --test-threads=1 --nocapture

Test run and summary assisted by Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7173042099 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2737#issuecomment-7173042099). Originally posted by [passerbyyhx](https://github.com/passerbyyhx) on 2026-11-25T19:59:52Z. --- On `a35eb773`, `twin::tests::a_d32_twin_copies_directly_and_takes_no_scratch` passed on a GeForce RTX 4090 Laptop GPU (driver 621.14, Windows 11, Rust 1.98.1 MSVC). I ran the unmodified PR head first, then repeated the test with a diagnostic print after the final assertions to confirm it reached readback. The D3D12 device LUID resolved to the NVIDIA adapter. All 16 texels of the generated 4×4 D32 texture matched after the depth-to-color copy. The test recorded one direct copy and no scratch buffer. Both runs used the synthetic fixture and no game files. `cargo test --locked --offline -p victory-gpu --lib twin::tests::a_d32_twin_copies_directly_and_takes_no_scratch -- --exact --test-threads=1 --nocapture` Test run and summary assisted by Codex.
kotekompotek commented 2026-11-25 20:06:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T20:06:22Z.


Good win: same-family subresources (D32's R32 plane, D24S8's pieces) now copy texture-to-texture with no scratch - a D32 twin drops a full texture of GPU memory. The scratch is only allocated when a route actually converts (at > 0), and the copy loop splits direct routes from converted ones cleanly. The 'whole subresource for depth-stencil' comment matches D3D12's restriction.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7173198091 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2737#issuecomment-7173198091). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T20:06:22Z. --- Good win: same-family subresources (D32's R32 plane, D24S8's pieces) now copy texture-to-texture with no scratch - a D32 twin drops a full texture of GPU memory. The scratch is only allocated when a route actually converts (at > 0), and the copy loop splits direct routes from converted ones cleanly. The 'whole subresource for depth-stencil' comment matches D3D12's restriction.
fuzeofficlal commented 2026-11-25 21:03:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T20:10:03Z.


Reviewed and verified a35eb773 (merged cleanly onto main 13f91ca) on Windows 11 (RTX 5090 + WARP).

Code review

  • Relaxing same in twin.rs:173 to resource::family(df[d].Footprint.Format) == resource::family(td.Format) cleanly enables zero-scratch direct CopyTextureRegion (whole-subresource, multi-mip/array safe) for D32_FLOAT/R32_TYPELESS, D16_UNORM, and the R8_TYPELESS stencil plane of R32G8X24_TYPELESS, while keeping the smaller scratch + compute conversion only for the 16-bit converted depth plane (118..=120) and returning true for list.need_rebind() only when compute conversion runs.
  • Note on MSAA depth textures (synergy with #2173): #2737 does not guard dd.SampleDesc.Count > 1 before calling CreateCommittedResource with ALLOW_UNORDERED_ACCESS (which D3D12 rejects for multisampled resources). On the flip side, #2173 guards MSAA and typed D16/D32 but still allocates a scratch buffer for same-family copies. git merge-tree between #2737 and #2173 (as well as #485 and #559) is completely conflict-free, so the two compose cleanly.

Verification (Windows 11, RTX 5090 + WARP)

  • RTX 5090 (--features diagnostics): twin::tests 3/3 passed (including a_d32_twin_copies_directly_and_takes_no_scratch), draw::tests --test-threads=1 11/11 passed.
  • WARP (VICTORY_D3D12_WARP=1, --features diagnostics --lib -- --test-threads=1): 199 passed, 0 failed, 1 ignored.
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings (default & --features diagnostics): 0 warnings; cargo xtask policy: 184 files ok.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7173280136 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2737#issuecomment-7173280136). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T20:10:03Z. --- Reviewed and verified `a35eb773` (merged cleanly onto `main` `13f91ca`) on Windows 11 (RTX 5090 + WARP). ### Code review - Relaxing `same` in `twin.rs:173` to `resource::family(df[d].Footprint.Format) == resource::family(td.Format)` cleanly enables zero-scratch direct `CopyTextureRegion` (whole-subresource, multi-mip/array safe) for `D32_FLOAT`/`R32_TYPELESS`, `D16_UNORM`, and the `R8_TYPELESS` stencil plane of `R32G8X24_TYPELESS`, while keeping the smaller scratch + compute conversion only for the 16-bit converted depth plane (`118..=120`) and returning `true` for `list.need_rebind()` only when compute conversion runs. - **Note on MSAA depth textures (synergy with `#2173`)**: `#2737` does not guard `dd.SampleDesc.Count > 1` before calling `CreateCommittedResource` with `ALLOW_UNORDERED_ACCESS` (which D3D12 rejects for multisampled resources). On the flip side, `#2173` guards MSAA and typed D16/D32 but still allocates a scratch buffer for same-family copies. `git merge-tree` between `#2737` and `#2173` (as well as `#485` and `#559`) is completely conflict-free, so the two compose cleanly. ### Verification (Windows 11, RTX 5090 + WARP) - **RTX 5090 (`--features diagnostics`)**: `twin::tests` **3/3 passed** (including `a_d32_twin_copies_directly_and_takes_no_scratch`), `draw::tests --test-threads=1` **11/11 passed**. - **WARP (`VICTORY_D3D12_WARP=1`, `--features diagnostics --lib -- --test-threads=1`)**: **199 passed, 0 failed, 1 ignored**. - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` (default & `--features diagnostics`): **0 warnings**; `cargo xtask policy`: **184 files ok**.
passerbyyhx commented 2026-11-25 21:03:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-11-25T20:18:13Z.


I ran the two branches together on a GeForce RTX 4090 Laptop GPU (driver 621.14). Merging #2173 at cdd11bd2 into a35eb773 gave tree 017a5f2d88135b5740feeac8dbb7989338b250ac without conflicts. The original D32 direct-copy and typed-depth tests both passed. A second run with test-only prints confirmed the adapter by LUID and recorded the readbacks.

  • Single-sample D32: all 16 texels matched after one direct copy, with no scratch buffer.
  • Typed depth round-trip at three sampled coordinates: D16 read 0.2500038 and wrote back 0xbfff. D32 read 0.25 and wrote back 0x3f400000 (0.75).
  • 4× MSAA: the request returned no twin and no writable view. The D3D12 debug info queue held zero messages immediately before and after the request.

Both synthetic fixtures ran with diagnostics in the same build. No game files were used. Test run and summary assisted by Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7173468997 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2737#issuecomment-7173468997). Originally posted by [passerbyyhx](https://github.com/passerbyyhx) on 2026-11-25T20:18:13Z. --- I ran the two branches together on a GeForce RTX 4090 Laptop GPU (driver 621.14). Merging #2173 at `cdd11bd2` into `a35eb773` gave tree `017a5f2d88135b5740feeac8dbb7989338b250ac` without conflicts. The original D32 direct-copy and typed-depth tests both passed. A second run with test-only prints confirmed the adapter by LUID and recorded the readbacks. - Single-sample D32: all 16 texels matched after one direct copy, with no scratch buffer. - Typed depth round-trip at three sampled coordinates: D16 read `0.2500038` and wrote back `0xbfff`. D32 read `0.25` and wrote back `0x3f400000` (`0.75`). - 4× MSAA: the request returned no twin and no writable view. The D3D12 debug info queue held zero messages immediately before and after the request. Both synthetic fixtures ran with diagnostics in the same build. No game files were used. Test run and summary assisted by Codex.
Marvisak commented 2026-11-26 00:03:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T23:22:42Z.


Automated triage for the maintainers.

This PR conflicts with the current main in crates/victory-gpu/src/twin.rs, which has changed since the PR's base. It will be reviewed once it is rebased onto main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7177477695 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2737#issuecomment-7177477695). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T23:22:42Z. --- <!-- victory-triage v1 head=a35eb773771d06fa13693e0ba87325964255ccc0 verdict=needs-rebase at=2026-11-25T23:22:35Z --> Automated triage for the maintainers. This PR conflicts with the current `main` in `crates/victory-gpu/src/twin.rs`, which has changed since the PR's base. It will be reviewed once it is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2737:upstream-pr/2737'
git switch 'upstream-pr/2737'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2737'
git switch 'upstream-pr/2737'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2737'
git switch 'upstream-pr/2737'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2737'
git switch 'main'
git merge --squash 'upstream-pr/2737'
git switch 'main'
git merge --ff-only 'upstream-pr/2737'
git switch 'main'
git merge 'upstream-pr/2737'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2735
No description provided.