Skip to content

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

Open
Therecalloftheonion wants to merge 1 commit into
Marvisak:mainfrom
Therecalloftheonion:pr/depth-twin-direct
Open

Therecalloftheonion wants to merge 1 commit into
Marvisak:mainfrom
Therecalloftheonion:pr/depth-twin-direct

Conversation

@Therecalloftheonion

Copy link
Copy Markdown

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.

A depth twin copied every subresource through a scratch buffer, so a D32 twin needed a full extra texture or two. Subresources of one format family now copy straight between the depth texture and the twin; the scratch is made only for converted 16-bit planes and only when one is needed. D32 twins now exist at all: the previous exact-format check rejected them, so shader writes to D32 depth were dropped.

Add a hardware test that copies a D32 depth texture into its twin, reads it back, and checks every texel and the device state. On the RX 6700 XT the copy is accepted and the texels match; WARP is not the only path exercised.
@passerbyyhx

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Owner

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

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.

5 participants