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

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3194 into main
fuzeofficlal commented 2026-11-26 11:55:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T11:55:39Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6626111875 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3194). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T11:55:39Z. --- ### 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.
Dukestar69 commented 2026-11-26 13:04:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T12:07:13Z.

Review: APPROVED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6073857317 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3194#pullrequestreview-6073857317). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T12:07:13Z. **Review:** APPROVED --- 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).
oni-swr commented 2026-11-26 15:02:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T14:24:13Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7194770979 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3194#issuecomment-7194770979). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-26T14:24:13Z. --- **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 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/3194:upstream-pr/3194'
git switch 'upstream-pr/3194'

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/3194'
git switch 'upstream-pr/3194'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3194'
git switch 'upstream-pr/3194'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3194'
git switch 'main'
git merge --squash 'upstream-pr/3194'
git switch 'main'
git merge --ff-only 'upstream-pr/3194'
git switch 'main'
git merge 'upstream-pr/3194'
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!3192
No description provided.