fix(list): legalise cross-family CopyTextureRegion and guard bounds/self-copy #4521

Closed
Andreww2009 wants to merge 1 commit from upstream-pr/4521 into main
Andreww2009 commented 2026-11-30 21:50:29 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Andreww2009 on 2026-11-30T21:50:29Z.


Why

copy_texture_region (crates/victory-gpu/src/list.rs) currently forwards CopyTextureRegion directly to D3D12 for both the primary destination and every placed_twins entry without validating format compatibility, twin self-copies, empty boxes, block alignment, or subresource/mip bounds. On PC D3D12 (unlike Xbox D3D12.X), recording an invalid CopyTextureRegion silently poisons the open command list and aborts the session at HostList::close() with E_INVALIDARG (0x80070057).

Concrete failure cases addressed:

  1. Cross-BC-family copies (e.g. BC3_TYPELESS into a BC7_TYPELESS placed twin — placed_twins matches by 16-byte block size only, #3901): forbidden directly by D3D12 CopyTextureRegion.
  2. Buffer-footprint format mismatch on placed twins: from.footprint.format holds dst's format (BC3), not the twin's format (BC7).
  3. Sub-block BC source_box coordinates (e.g. 2x2 or 1x1 box on small BC mips) not rounded up to 4x4 block multiples.
  4. Twin self-copy (when other == src and host_sub == src_sub), empty source boxes, unaligned BC destination offsets, or out-of-bounds subresource/region coordinates.

Change

  • crates/victory-gpu/src/host.rs (HostList::copy_texture_via_bounce): records a legal 2-step GPU bounce through a pooled D3D12_HEAP_TYPE_DEFAULT scratch buffer from uav_pool::take_uav (COMMON -> COPY_DEST -> COPY_SOURCE), sized to the exact copy region (not the whole subresource) and pushed onto between_uavs so HostList::close() transitions it back to COMMON for UAV_SPARE reuse.
  • crates/victory-gpu/src/list.rs (record_texture_copy, direct_texture_copy_compatible, scale_copy_extent, subresource_extent, typed_copy_format):
    • Preserves direct CopyTextureRegion for same-typeless-family pairs and D3D12's native 64/128-bit uncompressed <-> BC alias copies (R32G32B32A32_* <-> BC2/3/5/6H/7, R32G32_*/R16G16B16A16_* <-> BC1/4) with exact 4x block-to-texel bounds scaling (both texture and raw footprint sources).
    • Rewrites PlacedFootprint.format to dst's concrete typed format when copying a buffer footprint across equal-block-size families (BC3 footprint -> BC7 twin).
    • Bounces cross-family equal-block-size texture-to-texture copies (BC3 -> BC7) through HostList::copy_texture_via_bounce.
    • Rounds source_box.right / bottom up to 4x4 multiples on sub-block BC mips (2x2, 1x1), skips empty boxes and twin self-copies, and cleanly rejects out-of-bounds/incompatible copies with rate-limited diagnostics rather than failing Close() with 0x80070057.
  • crates/victory-gpu/src/resource.rs: exposes PlacedFootprint fields as pub(crate).
  • Tests (list::copy_texture_region_tests): 4 unit + RTX 5080 hardware tests covering BC3_UNORM footprint -> BC7_TYPELESS, BC3_TYPELESS -> BC7_TYPELESS bounce (both full mip 0 and boxed 2x2 mip 3), direct R32G32B32A32_UINT (4x4) -> BC7_TYPELESS (16x16), and invalid offset/subresource rejection — verified byte-for-byte with 0 D3D12 debug layer errors.

Relation to #4080

#4080 solves the cross-BC-family placed_twins case (#3901) by bouncing the entire source subresource through a scratch buffer for twins only. This PR differs in three ways:

  1. Sizes the UAV_SPARE bounce buffer to the exact source_box region rather than copying the full subresource for small boxed copies.
  2. Applies the same D3D12 legality checks (cross-family bounce, footprint rewrite, 4x uncompressed<->BC scaling, empty-box, twin self-copy, block-alignment, and mip bounds guards) uniformly to both the primary copy and placed_twins.
  3. Intentionally leaves primary host_resource lookup untouched (no overlap with #4126).

Verification

  • Hardware: Lenovo Legion 9 18IAX10 — Intel Core Ultra 9 275HX, NVIDIA RTX 5080 Laptop 16 GB GDDR7 (driver 621.14), Windows 11 x64 (Rust 1.98.1, pinned).
  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics --locked copy_texture_region_tests: 4 passed, 0 failed, 0 D3D12 debug layer errors, 0 warnings in modified files.
  • cargo xtask policy --staged clean (3 files ok), git diff --check clean.
  • Live scripted 1440p Series X + DLSS Quality runs on v2026.11.30.03 (base, VICTORY_SUBMIT_SPLIT=1, and 146 s DLSS FG 2x): 0 skipped copies, 0 0x80070057 failures, 0 crashes (where the same scripted route previously hit 0x80070057 at +104.6 s without this fix).

Original contribution under GPL-3.0-or-later.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5574397447 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4521). Originally posted by [Andreww2009](https://github.com/Andreww2009) on 2026-11-30T21:50:29Z. --- ## Why `copy_texture_region` (`crates/victory-gpu/src/list.rs`) currently forwards `CopyTextureRegion` directly to D3D12 for both the primary destination and every `placed_twins` entry without validating format compatibility, twin self-copies, empty boxes, block alignment, or subresource/mip bounds. On PC D3D12 (unlike Xbox D3D12.X), recording an invalid `CopyTextureRegion` silently poisons the open command list and aborts the session at `HostList::close()` with `E_INVALIDARG (0x80070057)`. Concrete failure cases addressed: 1. **Cross-BC-family copies** (e.g. `BC3_TYPELESS` into a `BC7_TYPELESS` placed twin — `placed_twins` matches by 16-byte block size only, #3901): forbidden directly by D3D12 `CopyTextureRegion`. 2. **Buffer-footprint format mismatch on placed twins:** `from.footprint.format` holds `dst`'s format (`BC3`), not the twin's format (`BC7`). 3. **Sub-block BC `source_box` coordinates** (e.g. `2x2` or `1x1` box on small BC mips) not rounded up to 4x4 block multiples. 4. **Twin self-copy** (when `other == src` and `host_sub == src_sub`), **empty source boxes**, **unaligned BC destination offsets**, or **out-of-bounds subresource/region coordinates**. ## Change - **`crates/victory-gpu/src/host.rs` (`HostList::copy_texture_via_bounce`):** records a legal 2-step GPU bounce through a pooled `D3D12_HEAP_TYPE_DEFAULT` scratch buffer from `uav_pool::take_uav` (`COMMON -> COPY_DEST -> COPY_SOURCE`), sized to the **exact copy region** (not the whole subresource) and pushed onto `between_uavs` so `HostList::close()` transitions it back to `COMMON` for `UAV_SPARE` reuse. - **`crates/victory-gpu/src/list.rs` (`record_texture_copy`, `direct_texture_copy_compatible`, `scale_copy_extent`, `subresource_extent`, `typed_copy_format`):** - Preserves direct `CopyTextureRegion` for same-typeless-family pairs **and** D3D12's native 64/128-bit uncompressed <-> BC alias copies (`R32G32B32A32_*` <-> `BC2/3/5/6H/7`, `R32G32_*`/`R16G16B16A16_*` <-> `BC1/4`) with exact `4x` block-to-texel bounds scaling (both texture and raw footprint sources). - Rewrites `PlacedFootprint.format` to `dst`'s concrete typed format when copying a buffer footprint across equal-block-size families (`BC3` footprint -> `BC7` twin). - Bounces cross-family equal-block-size texture-to-texture copies (`BC3 -> BC7`) through `HostList::copy_texture_via_bounce`. - Rounds `source_box.right` / `bottom` up to 4x4 multiples on sub-block BC mips (`2x2`, `1x1`), skips empty boxes and twin self-copies, and cleanly rejects out-of-bounds/incompatible copies with rate-limited diagnostics rather than failing `Close()` with `0x80070057`. - **`crates/victory-gpu/src/resource.rs`:** exposes `PlacedFootprint` fields as `pub(crate)`. - **Tests (`list::copy_texture_region_tests`):** 4 unit + RTX 5080 hardware tests covering `BC3_UNORM` footprint -> `BC7_TYPELESS`, `BC3_TYPELESS -> BC7_TYPELESS` bounce (both full mip 0 and boxed `2x2` mip 3), direct `R32G32B32A32_UINT (4x4) -> BC7_TYPELESS (16x16)`, and invalid offset/subresource rejection — verified byte-for-byte with 0 D3D12 debug layer errors. ## Relation to #4080 #4080 solves the cross-BC-family `placed_twins` case (#3901) by bouncing the **entire source subresource** through a scratch buffer for twins only. This PR differs in three ways: 1. Sizes the `UAV_SPARE` bounce buffer to the **exact `source_box` region** rather than copying the full subresource for small boxed copies. 2. Applies the same D3D12 legality checks (cross-family bounce, footprint rewrite, 4x uncompressed<->BC scaling, empty-box, twin self-copy, block-alignment, and mip bounds guards) uniformly to **both** the primary copy and `placed_twins`. 3. Intentionally leaves primary `host_resource` lookup untouched (no overlap with #4126). ## Verification - Hardware: Lenovo Legion 9 18IAX10 — Intel Core Ultra 9 275HX, NVIDIA RTX 5080 Laptop 16 GB GDDR7 (driver 621.14), Windows 11 x64 (`Rust 1.98.1`, pinned). - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics --locked copy_texture_region_tests`: **4 passed, 0 failed**, 0 D3D12 debug layer errors, 0 warnings in modified files. - `cargo xtask policy --staged` clean (`3 files ok`), `git diff --check` clean. - Live scripted 1440p Series X + DLSS Quality runs on `v2026.11.30.03` (base, `VICTORY_SUBMIT_SPLIT=1`, and 146 s DLSS FG 2x): **0 skipped copies, 0 `0x80070057` failures, 0 crashes** (where the same scripted route previously hit `0x80070057` at +104.6 s without this fix). *Original contribution under GPL-3.0-or-later.*
IRennie74 commented 2026-11-30 22:39:08 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by IRennie74 on 2026-11-30T22:39:08Z.


Some data on how often this abort happens on a low-end PC (Ryzen 5 5600X, RTX 3060 12 GB, Series S, walking route). a host command list did not close (E_INVALIDARG) ended 3 of 18 walking runs on a base without #4080, each with texture memory near full. It did not appear in 11 runs of main + #4080 (+ #4040, #4226, #4199, #4255, #4027). That fits #4080 already covering the case that hits there. A guard on the primary copy, as here, is still worth having.

Points on the diff:

  • It is against main without #4080. Both rewrite the placed_twins loop of copy_texture_region with their own scratch-buffer bounce (#4080 through twin_plan and copy_scratch, or the title's buffer retagged; this one the box through copy_texture_via_bounce), so they will conflict. Building this on top of #4080, so that there is one bounce helper, would make it much easier to take both.
  • record_texture_copy turns an invalid copy into a skipped one (a warn! for the first 16, then every 1024th). That is much better than losing the list, but a skipped copy leaves stale texels, which shows up as ghosting rather than a crash. A counter on the existing card-copies line (next to twin_copy_left_out's) would make those visible in normal logs, not only the first 16.
  • The empty-box early return now comes before the placed_twins loop and the twin_use/host_written bookkeeping. An empty box is a no-op for the twins too, so that is fine, but it also skips the diagnostics note_op text for that call.
  • Encoding slip: the doc comment of the ClearUnorderedAccessView handler changes … to … (list.rs, hunk @@ -2235,7 +2596,7). It looks like the file went through a Latin-1 round trip, so it may be worth checking the rest of the file for the same thing.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7294127494 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4521#issuecomment-7294127494). Originally posted by [IRennie74](https://github.com/IRennie74) on 2026-11-30T22:39:08Z. --- Some data on how often this abort happens on a low-end PC (Ryzen 5 5600X, RTX 3060 12 GB, Series S, walking route). `a host command list did not close` (`E_INVALIDARG`) ended 3 of 18 walking runs on a base without #4080, each with texture memory near full. It did not appear in 11 runs of main + #4080 (+ #4040, #4226, #4199, #4255, #4027). That fits #4080 already covering the case that hits there. A guard on the primary copy, as here, is still worth having. Points on the diff: - It is against main without #4080. Both rewrite the `placed_twins` loop of `copy_texture_region` with their own scratch-buffer bounce (#4080 through `twin_plan` and `copy_scratch`, or the title's buffer retagged; this one the box through `copy_texture_via_bounce`), so they will conflict. Building this on top of #4080, so that there is one bounce helper, would make it much easier to take both. - `record_texture_copy` turns an invalid copy into a skipped one (a `warn!` for the first 16, then every 1024th). That is much better than losing the list, but a skipped copy leaves stale texels, which shows up as ghosting rather than a crash. A counter on the existing card-copies line (next to `twin_copy_left_out`'s) would make those visible in normal logs, not only the first 16. - The empty-box early `return` now comes before the `placed_twins` loop and the `twin_use`/`host_written` bookkeeping. An empty box is a no-op for the twins too, so that is fine, but it also skips the diagnostics `note_op` text for that call. - Encoding slip: the doc comment of the `ClearUnorderedAccessView` handler changes `…` to `…` (list.rs, hunk `@@ -2235,7 +2596,7`). It looks like the file went through a Latin-1 round trip, so it may be worth checking the rest of the file for the same thing.
Andreww2009 commented 2026-12-01 09:46:30 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Andreww2009 on 2026-12-01T09:46:30Z.


Thanks for the careful review and RTX 3060 testing, @IRennie74! Rebased onto latest main (5fabb46e) and addressed all three points in the updated commit:

  1. Fixed the UTF-8 ellipsis encoding slip in crates/victory-gpu/src/list.rs (0 mojibake occurrences verified).
  2. Moved self.note_op(...) ahead of the empty-box early return in CopyTextureRegion.
  3. Exposed COPIES_SKIPPED alongside TWIN_COPIES_LEFT_OUT and SELF_COPIES in the periodic host frame summary (texture copies left out: {} primary, {} twin, {} self).

Re-tested on RTX 5080 Laptop (2560x1440, Series X, DLSS Quality): cargo test -p victory-gpu --lib copy_texture_region (4/4 under VICTORY_D3D12_DEBUG=1, 0 debug errors), 14 live runs including two 15-minute soaks (No-RT 25,881 frames and DXR 1.1 17,011 frames) with 0 0x80070057 aborts and 0 primary copies left out.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7307187242 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4521#issuecomment-7307187242). Originally posted by [Andreww2009](https://github.com/Andreww2009) on 2026-12-01T09:46:30Z. --- Thanks for the careful review and RTX 3060 testing, @IRennie74! Rebased onto latest `main` (`5fabb46e`) and addressed all three points in the updated commit: 1. Fixed the UTF-8 ellipsis encoding slip in `crates/victory-gpu/src/list.rs` (0 mojibake occurrences verified). 2. Moved `self.note_op(...)` ahead of the empty-box early return in `CopyTextureRegion`. 3. Exposed `COPIES_SKIPPED` alongside `TWIN_COPIES_LEFT_OUT` and `SELF_COPIES` in the periodic host frame summary (`texture copies left out: {} primary, {} twin, {} self`). Re-tested on RTX 5080 Laptop (`2560x1440`, Series X, DLSS Quality): `cargo test -p victory-gpu --lib copy_texture_region` (`4/4` under `VICTORY_D3D12_DEBUG=1`, 0 debug errors), 14 live runs including two 15-minute soaks (No-RT `25,881` frames and DXR 1.1 `17,011` frames) with `0` `0x80070057` aborts and `0 primary` copies left out.
tillo88 commented 2026-12-01 12:10:05 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by tillo88 on 2026-12-01T12:10:05Z.


Crash and speed check of #4521 (head 987401cc) merged on top of v2026.11.30.03 (it applied there without conflicts), against the bare release on the same bench and route.

Crashes (warm-up runs included for the count):

build runs list-close / device-lost failures
v2026.11.30.03 27 5: 3 at the prison→plane cutscene cut (+2:15 to +2:18), 1 at +2:40, 1 at +2:49
+ #4521 7 0

Base errors were a host command list did not close (0x80070057) (2) and relay: the device was lost ... (0x887A0006) (3). With #4521 all 7 runs went past the cut and ran the whole ~6 min route. At the base rate (5 of 27), 0 of 7 by chance is about 24%, so this is not conclusive on its own. Together with #4080, #3043 and #3979 (same kind of fix, same release), 0 of 30 runs stopped against 5 of 27 on the bare release, about 2% by chance (Fisher's exact test).

The PR logs nothing when it bounces a copy, and none of its skip/reject warnings appeared in any run, so the logs cannot show how often the bounce path ran or whether any copy was dropped.

Speed (measured runs only, warm-up excluded):

build runs fps (range) submitting thread ms refresh ms p95 ms p99 ms
v2026.11.30.03 15 32.02 (31.72–32.46) 20.51 6.42 47.7 62.7
+ #4521 6 32.02 (31.29–32.36) 20.51 6.38 48.3 64.2

Same mean fps as the base: no measurable difference. One run was at 31.29 fps (below the slowest base run) and one had p95 54.2 ms; the other five had p95 46.6–47.4 ms, so the higher mean p95 comes from that single run.

Setup: Ryzen 7 9800X3D, RTX 5070 Ti 16 GB (driver 621.40), 32 GB RAM, Windows 11, release build, Series X 1440p, DLSS Quality, frame generation off, ray tracing off. Route: same save from the start of the game, prison cutscene, plane cutscene, first boat section, about 6 minutes per run.

Measured unattended on a replay bench (same save, recorded controller input via VICTORY_INPUT_REPLAY); numbers and text prepared with Claude.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7310748240 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4521#issuecomment-7310748240). Originally posted by [tillo88](https://github.com/tillo88) on 2026-12-01T12:10:05Z. --- Crash and speed check of #4521 (head 987401cc) merged on top of v2026.11.30.03 (it applied there without conflicts), against the bare release on the same bench and route. **Crashes** (warm-up runs included for the count): | build | runs | list-close / device-lost failures | |---|---|---| | v2026.11.30.03 | 27 | 5: 3 at the prison→plane cutscene cut (+2:15 to +2:18), 1 at +2:40, 1 at +2:49 | | + #4521 | 7 | 0 | Base errors were `a host command list did not close (0x80070057)` (2) and `relay: the device was lost ... (0x887A0006)` (3). With #4521 all 7 runs went past the cut and ran the whole ~6 min route. At the base rate (5 of 27), 0 of 7 by chance is about 24%, so this is not conclusive on its own. Together with #4080, #3043 and #3979 (same kind of fix, same release), 0 of 30 runs stopped against 5 of 27 on the bare release, about 2% by chance (Fisher's exact test). The PR logs nothing when it bounces a copy, and none of its skip/reject warnings appeared in any run, so the logs cannot show how often the bounce path ran or whether any copy was dropped. **Speed** (measured runs only, warm-up excluded): | build | runs | fps (range) | submitting thread ms | refresh ms | p95 ms | p99 ms | |---|---|---|---|---|---|---| | v2026.11.30.03 | 15 | 32.02 (31.72–32.46) | 20.51 | 6.42 | 47.7 | 62.7 | | + #4521 | 6 | 32.02 (31.29–32.36) | 20.51 | 6.38 | 48.3 | 64.2 | Same mean fps as the base: no measurable difference. One run was at 31.29 fps (below the slowest base run) and one had p95 54.2 ms; the other five had p95 46.6–47.4 ms, so the higher mean p95 comes from that single run. Setup: Ryzen 7 9800X3D, RTX 5070 Ti 16 GB (driver 621.40), 32 GB RAM, Windows 11, release build, Series X 1440p, DLSS Quality, frame generation off, ray tracing off. Route: same save from the start of the game, prison cutscene, plane cutscene, first boat section, about 6 minutes per run. Measured unattended on a replay bench (same save, recorded controller input via VICTORY_INPUT_REPLAY); numbers and text prepared with Claude.
Andreww2009 commented 2026-12-01 14:29:39 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Andreww2009 on 2026-12-01T14:29:39Z.


Thanks for the RTX 5070 Ti replay verification, @tillo88 — 0/7 vs 5/27 aborts with identical 32.02 fps is great confirmation!

One quick note on logging: your test ran the earlier commit 987401cc. The latest rebased head (0745fad9, addressing @IRennie74's review) now exposes COPIES_SKIPPED on the periodic host frame summary line (texture copies left out: {} primary, {} twin, {} self) so any skipped primary copy is visible in normal release logs.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7314340841 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4521#issuecomment-7314340841). Originally posted by [Andreww2009](https://github.com/Andreww2009) on 2026-12-01T14:29:39Z. --- Thanks for the RTX 5070 Ti replay verification, @tillo88 — `0/7` vs `5/27` aborts with identical `32.02` fps is great confirmation! One quick note on logging: your test ran the earlier commit `987401cc`. The latest rebased head (`0745fad9`, addressing @IRennie74's review) now exposes `COPIES_SKIPPED` on the periodic host frame summary line (`texture copies left out: {} primary, {} twin, {} self`) so any skipped primary copy is visible in normal release logs.
trustno1-coder commented 2026-12-01 18:38:51 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-12-01T18:38:51Z.


Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7321271676 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4521#issuecomment-7321271676). Originally posted by [trustno1-coder](https://github.com/trustno1-coder) on 2026-12-01T18:38:51Z. --- Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

Pull request closed

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
trustno1-coder/VIctory-NVIDIA!4521
No description provided.