GPU: validate CopyTextureRegion subresources, source boxes, and destination bounds #3194

Open
github-bot wants to merge 1 commit from github-bot/gh-3194 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 11:55 UTC · original
fuzeofficlal:fix/gpu-copy-texture-region-validation → main · +341 −14 · 1 files · 1 commits · open

Problem

copy_texture_region and read_back in crates/victory-gpu/src/list.rs previously forwarded title locations, subresource indices, source boxes, and destination coordinates directly to ID3D12GraphicsCommandList::CopyTextureRegion without validating D3D12's copy contract:

  1. Out-of-bounds subresource indices (including 3D textures where DepthOrArraySize is depth rather than array slice count, and multi-plane depth/stencil formats), invalid CopyLocation::kind values, zero-sized PlacedFootprint extents, source boxes extending past source bounds, destination coordinates extending past destination bounds, or unaligned block-compressed (BC) coordinates/boxes cause ID3D12GraphicsCommandList::Close() to fail with E_INVALIDARG (0x80070057) even on retail D3D12 (without the Debug Layer) on both hardware and WARP, aborting HostList::close().
  2. Invalid copies also allocated upload/readback buffers, emitted resource barriers (ensure_state), and marked texture::host_writes before failing.

Fix

  • Add copy_subresources using ID3D12Device::CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, ...) for exact D3D12 PlaneCount and handling TEXTURE3D (slices = 1) vs 1D/2D array textures accurately.
  • Add copy_extent_sub, copy_extent_fp, and validate_copy_region:
    • Align block-compressed mip and footprint extents up to block size (next_multiple_of(block)), preserving valid BC 1x1/2x2 tail mips as well as cross-format block-compatible copies ((w / src.block) * dst.block).
    • Enforce block alignment on pSrcBox (left, right, top, bottom) and destination (DstX, DstY) coordinates, non-zero PlacedFootprint extents, and overflow-safe source/destination bounds across (x, y, z).
    • Treat empty or inverted pSrcBox (left >= right || top >= bottom || front >= back) as legal D3D12 no-ops.
  • Apply validation across T2T, B2T, and T2B (read_back) before allocating upload/readback buffers, emitting ensure_state, or marking texture::host_writes, and keep the source ID3D12Resource handle alive across the CopyTextureRegion call in both B2T and T2T branches.

Verification

Added list::tests::copy_texture_region_validates_subresources_boxes_and_bounds testing 16 invalid retail E_INVALIDARG cases (T2T, B2T, T2B, BC alignment/tail bounds, 3D subresources/Z bounds, 2-plane depth/stencil OOB, wrong location kind, zero-width footprint) and 9 valid transfers (empty/inverted no-op boxes, T2T mip 1, BC 2x2/1x1 tail mips with 4x4 & 1x1 footprints, T2B readback, 3D z-slice, 2-plane stencil):

  • Before fix (origin/main): fails list.Close() with HRESULT(0x80070057) (E_INVALIDARG) on retail RTX 5090 (no Debug Layer), Debug RTX 5090, and Debug WARP.
  • After fix:
    • Targeted test passes on hardware default, hardware --features diagnostics + VICTORY_D3D12_DEBUG=1, and WARP --features diagnostics + VICTORY_D3D12_DEBUG=1 with host.log_messages() == None.
    • Full cargo test -p victory-gpu --lib --locked passes on hardware default (230 passed), hardware diagnostics + DEBUG=1 (244 passed), and WARP diagnostics + DEBUG=1 (246 passed).
    • cargo clippy --workspace --all-targets --locked -- -D warnings (default and --all-features) and xtask policy checks pass cleanly.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 11:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/3194) `fuzeofficlal:fix/gpu-copy-texture-region-validation` → `main` · +341 −14 · 1 files · 1 commits · open ### Problem `copy_texture_region` and `read_back` in `crates/victory-gpu/src/list.rs` previously forwarded title locations, subresource indices, source boxes, and destination coordinates directly to `ID3D12GraphicsCommandList::CopyTextureRegion` without validating D3D12's copy contract: 1. Out-of-bounds subresource indices (including 3D textures where `DepthOrArraySize` is depth rather than array slice count, and multi-plane depth/stencil formats), invalid `CopyLocation::kind` values, zero-sized `PlacedFootprint` extents, source boxes extending past source bounds, destination coordinates extending past destination bounds, or unaligned block-compressed (BC) coordinates/boxes cause `ID3D12GraphicsCommandList::Close()` to fail with `E_INVALIDARG` (`0x80070057`) even on retail D3D12 (without the Debug Layer) on both hardware and WARP, aborting `HostList::close()`. 2. Invalid copies also allocated upload/readback buffers, emitted resource barriers (`ensure_state`), and marked `texture::host_writes` before failing. ### Fix - Add `copy_subresources` using `ID3D12Device::CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, ...)` for exact D3D12 `PlaneCount` and handling `TEXTURE3D` (`slices = 1`) vs 1D/2D array textures accurately. - Add `copy_extent_sub`, `copy_extent_fp`, and `validate_copy_region`: - Align block-compressed mip and footprint extents up to block size (`next_multiple_of(block)`), preserving valid BC 1x1/2x2 tail mips as well as cross-format block-compatible copies (`(w / src.block) * dst.block`). - Enforce block alignment on `pSrcBox` (`left`, `right`, `top`, `bottom`) and destination `(DstX, DstY)` coordinates, non-zero `PlacedFootprint` extents, and overflow-safe source/destination bounds across `(x, y, z)`. - Treat empty or inverted `pSrcBox` (`left >= right || top >= bottom || front >= back`) as legal D3D12 no-ops. - Apply validation across T2T, B2T, and T2B (`read_back`) before allocating upload/readback buffers, emitting `ensure_state`, or marking `texture::host_writes`, and keep the source `ID3D12Resource` handle alive across the `CopyTextureRegion` call in both B2T and T2T branches. ### Verification Added `list::tests::copy_texture_region_validates_subresources_boxes_and_bounds` testing 16 invalid retail `E_INVALIDARG` cases (T2T, B2T, T2B, BC alignment/tail bounds, 3D subresources/Z bounds, 2-plane depth/stencil OOB, wrong location kind, zero-width footprint) and 9 valid transfers (empty/inverted no-op boxes, T2T mip 1, BC 2x2/1x1 tail mips with 4x4 & 1x1 footprints, T2B readback, 3D z-slice, 2-plane stencil): - **Before fix (`origin/main`)**: fails `list.Close()` with `HRESULT(0x80070057)` (`E_INVALIDARG`) on retail RTX 5090 (no Debug Layer), Debug RTX 5090, and Debug WARP. - **After fix**: - Targeted test passes on hardware default, hardware `--features diagnostics` + `VICTORY_D3D12_DEBUG=1`, and WARP `--features diagnostics` + `VICTORY_D3D12_DEBUG=1` with `host.log_messages() == None`. - Full `cargo test -p victory-gpu --lib --locked` passes on hardware default (`230 passed`), hardware `diagnostics + DEBUG=1` (`244 passed`), and WARP `diagnostics + DEBUG=1` (`246 passed`). - `cargo clippy --workspace --all-targets --locked -- -D warnings` (default and `--all-features`) and `xtask` policy checks pass cleanly.
copy_texture_region and read_back previously forwarded title locations, subresource indices, source boxes, and destination coordinates directly to ID3D12GraphicsCommandList::CopyTextureRegion without validating D3D12's copy contract:

- Out-of-bounds subresource indices (including 3D textures where DepthOrArraySize is depth rather than array slices, and multi-plane depth/stencil formats), wrong CopyLocation::kind values, zero-sized PlacedFootprint extents, source boxes extending past source bounds, destination coordinates extending past destination bounds, or unaligned block-compressed (BC) coordinates/boxes cause ID3D12GraphicsCommandList::Close to fail with E_INVALIDARG even on retail D3D12 (without the Debug Layer) on both hardware and WARP, aborting HostList::close.
- Invalid copies also allocated upload/readback buffers, emitted state transitions, and marked host_writes before failing.

Query D3D12_FEATURE_FORMAT_INFO for each format's exact PlaneCount, compute block-aligned mip extents (preserving valid BC 1x1/2x2 tail mips, cross-format block-compatible copies, 3D z-slices, and 2-plane depth/stencil copies), treat empty or inverted source boxes as legal D3D12 no-ops, and validate T2T, B2T, and T2B (read_back) transfers before allocating staging buffers, emitting barriers, or marking host_writes.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:57:05 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 12:07 UTC · original · approved these changes

Verified on RTX 5090 against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib list:: -- --test-threads=1: 5/5 passed (including copy_texture_region_validates_subresources_boxes_and_bounds).
  • cargo test -p victory-gpu --lib resource:: -- --test-threads=1: 18/18 passed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly validates CopyTextureRegion subresource indices, source boxes, and destination bounds.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 12:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/3194#pullrequestreview-6073857317) · **approved these changes** Verified on RTX 5090 against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib list:: -- --test-threads=1`: 5/5 passed (including `copy_texture_region_validates_subresources_boxes_and_bounds`). - `cargo test -p victory-gpu --lib resource:: -- --test-threads=1`: 18/18 passed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly validates `CopyTextureRegion` subresource indices, source boxes, and destination bounds. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 14:24 UTC · original

WARP verification of #3194 at 4b224882 (base 63bcca4, origin/main). Software adapter only; synthetic tests only; no GPU, game files, dumps, shader names or hashes.

Environment: Windows 11 Pro 10.0.26200; WARP d3d10warp.dll 10.0.26100.9818; Windows SDK dxil.dll 10.0.19041.5609 on PATH; rustc 1.98.1. VICTORY_WARP=1 VICTORY_D3D12_WARP=1. A throwaway probe (reverted) asserted the adapter description is "Microsoft Basic Render Driver" before any device was created; it held in every run.

Commands (exact head, --features diagnostics, because that is where the WARP switch lives):

cargo test -j 4 -p victory-gpu --lib --features diagnostics --locked list::tests::copy_texture_region_validates_subresources_boxes_and_bounds
cargo test -j 4 -p victory-gpu --lib --features diagnostics --locked

Head: the new test list::tests::copy_texture_region_validates_subresources_boxes_and_bounds passes (1 passed, 0 failed). Full victory-gpu lib: 247 passed, 0 failed, 1 ignored.

Control: origin/main's list.rs with only this PR's test module appended. Result: the test fails with list.Close() E_INVALIDARG (0x80070057) at the first invalid case, t2t_oob_dst_sub.

Test report only; no source changes. AI use: Claude Code (Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-26 14:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/3194#issuecomment-7194770979) **WARP verification of #3194 at `4b224882`** (base `63bcca4`, `origin/main`). Software adapter only; synthetic tests only; no GPU, game files, dumps, shader names or hashes. **Environment:** Windows 11 Pro 10.0.26200; WARP `d3d10warp.dll` 10.0.26100.9818; Windows SDK `dxil.dll` 10.0.19041.5609 on `PATH`; rustc 1.98.1. `VICTORY_WARP=1 VICTORY_D3D12_WARP=1`. A throwaway probe (reverted) asserted the adapter description is "Microsoft Basic Render Driver" before any device was created; it held in every run. **Commands** (exact head, `--features diagnostics`, because that is where the WARP switch lives): ``` cargo test -j 4 -p victory-gpu --lib --features diagnostics --locked list::tests::copy_texture_region_validates_subresources_boxes_and_bounds cargo test -j 4 -p victory-gpu --lib --features diagnostics --locked ``` **Head:** the new test `list::tests::copy_texture_region_validates_subresources_boxes_and_bounds` passes (1 passed, 0 failed). Full `victory-gpu` lib: 247 passed, 0 failed, 1 ignored. **Control:** `origin/main`'s `list.rs` with only this PR's test module appended. Result: the test fails with `list.Close()` `E_INVALIDARG` (`0x80070057`) at the first invalid case, `t2t_oob_dst_sub`. Test report only; no source changes. AI use: Claude Code (Claude Sonnet 5.5).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3194/head:github-bot/gh-3194'
git switch 'github-bot/gh-3194'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3194'
git switch 'github-bot/gh-3194'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3194'
git switch 'github-bot/gh-3194'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3194'
git switch 'main'
git merge --squash 'github-bot/gh-3194'
git switch 'main'
git merge --ff-only 'github-bot/gh-3194'
git switch 'main'
git merge 'github-bot/gh-3194'
git push origin 'main'
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!3194
No description provided.