perf(gpu): add opt-in GPU compute prelude texture detiling (VICTORY_GPU_DETILE) #249

Closed
Vladimir-Human wants to merge 541 commits from upstream-pr/249 into main
Vladimir-Human commented 2026-11-27 12:12:26 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by Vladimir-Human on 2026-11-27T12:12:26Z.


Summary

Adds an opt-in GPU compute texture detiler (VICTORY_GPU_DETILE=1, off by default; VICTORY_GPU_DETILE=alt toggles 20 s off/on) that offloads compatible texture detiling from CPU threads to a D3D12 cs_5_1 compute pass recorded in HostList::prelude.

Instead of detiling element runs on the CPU into the upload buffer, enabled stagings copy raw tiled bytes sequentially into upload memory (alongside the held-level bitmask and the layout's pattern tables), dispatch the compute detiler in the list's prelude into a device-local UAV buffer (COMMON -> UNORDERED_ACCESS -> COMMON), and let the list's existing CopyTextureRegion commands copy from that linear buffer into the host texture. Unsupported layouts (all XOR modes _T/_X), background copy-queue uploads (ahead), movie planes, and allocation-pressured stagings automatically fall back to the existing CPU AVX2/SSE2 DetilePool path (#191).

Design adapted from fxceless/victory-fork commit 3461f52 (VICTORY_GPU_DETILE), with Vice-Tay-specific extensions:

  • Pooled upload base offset (Staging::base): binds buffer.GetGPUVirtualAddress() + self.base as Root SRV and uses (&gpu.linear, 0) vs (&self.buffer, self.base) for CopyTextureRegion.
  • Held-level mip clamping (first / level0): sizes raw staging from layout.bytes_from(first), validates layout.bytes_from(mip) <= raw.tiled_bytes, and maps each subresource to its exact console mip index.
  • Exact 4-byte-rounded UAV sizing (Plan::linear_bytes): guarantees 32-bit RWByteAddressBuffer::Store stays strictly in bounds even on 1-byte / 2-byte last rows whose D3D12 total size is not 4-byte aligned.
  • Division-free compute shader: precomputes power-of-two shifts/masks (bw_log2, bh_log2, slab_log2, n_log2, pitch / block_w) and table offsets into 24 root constants (0 integer divides in the shader).
  • Copy-queue ahead safety: ahead::claim requests CPU-only staging (allow_gpu = false + !s.is_gpu()) so D3D12_COMMAND_LIST_TYPE_COPY lists never receive a GPU-detiled staging.
  • Fence-safe, pressure-aware SPARE_LINEAR pool: tracks after <= finished against host::submissions() before reusing device-local linear buffers, respects residency growth gates, and trims spares in let_go_idle().
  • Per-submission deduplication & prelude-pool integration (#211): deduplicates compute passes across multiple lists in the same submission (needs_detile / record_detile), skips empty preludes when an earlier list already detiled the texture, and records inside prelude_pool::Slot::record.

Files changed

  • crates/victory-formats/src/texture.rs: Swizzle::name(), Layout::detile_params(), Layout::pattern_tables(), DetileParams::address(), and exhaustive addressing/XOR-refusal unit test (detile_params_address_every_element_as_offset_does).
  • crates/victory-gpu/src/gpudetile.rs: self-contained cs_5_1 compute detiler, staging layout helpers, dispatch planner (jobs), prelude recorder (record), per-frame meter line (meter_line), and HW + WARP + prelude integration tests.
  • crates/victory-gpu/src/texture.rs: Staging::gpu, SPARE_LINEAR pool, raw copy path in Staging::detile, copy_source(), needs_detile() / record_detile(), and automatic CPU fallback.
  • crates/victory-gpu/src/texture/ahead.rs: keep background copy-queue uploads CPU-detiled.
  • crates/victory-gpu/src/host.rs: Deferred::detiles, HostList::detile_first, HostList::prelude(this_submission), HostList::untouched, and gpudetile::meter_line(frames).
  • crates/victory-gpu/src/lib.rs: register mod gpudetile;.

Verification (rustc 1.98.1, VICTORY_D3D12_DEBUG=1)

  • cargo run -p xtask --locked -- policy (asset policy: 313 files ok) & cargo test -p xtask --locked: 11/11 passed
  • cargo clippy -p victory-formats -p victory-gpu --all-targets --locked -- -D warnings: 0 warnings
  • cargo clippy -p victory-formats -p victory-gpu --all-targets --all-features --locked -- -D warnings: 0 warnings
  • cargo test -p victory-formats --locked: 48 passed, 0 failed, 6 ignored
  • D3D12 HW (VICTORY_D3D12_DEBUG=1):
    • gpudetile::: 5/5 passed (25-case + 2 on-demand mask byte-exact matrix across RGBA8, BC1, BC3, BC5, BC7, R8, R16, R16G16B16A16F, R32G32B32A32F, POT + NPOT, 256B/4KB/64KB S+D, LINEAR, mip tails, arrays, 3D thick/linear, level0/first mip-clamping, pooled raw_base offset; 9 XOR mode fallbacks; end-to-end HostList::prelude upload + multi-list dedup + mip 0 & tail readback + dynamic v2 update; ahead CPU-only check)
    • gpudetile:: with VICTORY_PRELUDE_POOL=1: 5/5 passed
    • gpudetile:: with VICTORY_D3D12_GBV=1: 5/5 passed
    • gpudetile:: & texture:: (--features diagnostics): 5/5 & 8/8 passed
    • texture:: (VICTORY_GPU_DETILE=0 & VICTORY_GPU_DETILE=1): 8/8 passed
  • D3D12 WARP (VICTORY_D3D12_WARP=1, VICTORY_D3D12_DEBUG=1):
    • gpudetile::: 5/5 passed
    • texture:: (VICTORY_GPU_DETILE=1): 8/8 passed
<!-- greyfinch-sync:TaynanGT/Vice-Tay:issue:5526413068 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/249). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-27T12:12:26Z. --- ## Summary Adds an opt-in GPU compute texture detiler (`VICTORY_GPU_DETILE=1`, off by default; `VICTORY_GPU_DETILE=alt` toggles 20 s off/on) that offloads compatible texture detiling from CPU threads to a D3D12 `cs_5_1` compute pass recorded in `HostList::prelude`. Instead of detiling element runs on the CPU into the upload buffer, enabled stagings copy raw tiled bytes sequentially into upload memory (alongside the held-level bitmask and the layout's pattern tables), dispatch the compute detiler in the list's prelude into a device-local UAV buffer (`COMMON -> UNORDERED_ACCESS -> COMMON`), and let the list's existing `CopyTextureRegion` commands copy from that linear buffer into the host texture. Unsupported layouts (all XOR modes `_T`/`_X`), background copy-queue uploads (`ahead`), movie planes, and allocation-pressured stagings automatically fall back to the existing CPU AVX2/SSE2 `DetilePool` path (`#191`). Design adapted from `fxceless/victory-fork` commit `3461f52` (`VICTORY_GPU_DETILE`), with `Vice-Tay`-specific extensions: - **Pooled upload base offset (`Staging::base`):** binds `buffer.GetGPUVirtualAddress() + self.base` as Root SRV and uses `(&gpu.linear, 0)` vs `(&self.buffer, self.base)` for `CopyTextureRegion`. - **Held-level mip clamping (`first` / `level0`):** sizes raw staging from `layout.bytes_from(first)`, validates `layout.bytes_from(mip) <= raw.tiled_bytes`, and maps each subresource to its exact console mip index. - **Exact 4-byte-rounded UAV sizing (`Plan::linear_bytes`):** guarantees 32-bit `RWByteAddressBuffer::Store` stays strictly in bounds even on 1-byte / 2-byte last rows whose D3D12 `total` size is not 4-byte aligned. - **Division-free compute shader:** precomputes power-of-two shifts/masks (`bw_log2`, `bh_log2`, `slab_log2`, `n_log2`, `pitch / block_w`) and table offsets into 24 root constants (0 integer divides in the shader). - **Copy-queue `ahead` safety:** `ahead::claim` requests CPU-only staging (`allow_gpu = false` + `!s.is_gpu()`) so `D3D12_COMMAND_LIST_TYPE_COPY` lists never receive a GPU-detiled staging. - **Fence-safe, pressure-aware `SPARE_LINEAR` pool:** tracks `after <= finished` against `host::submissions()` before reusing device-local linear buffers, respects `residency` growth gates, and trims spares in `let_go_idle()`. - **Per-submission deduplication & prelude-pool integration (`#211`):** deduplicates compute passes across multiple lists in the same submission (`needs_detile` / `record_detile`), skips empty preludes when an earlier list already detiled the texture, and records inside `prelude_pool::Slot::record`. ## Files changed - `crates/victory-formats/src/texture.rs`: `Swizzle::name()`, `Layout::detile_params()`, `Layout::pattern_tables()`, `DetileParams::address()`, and exhaustive addressing/XOR-refusal unit test (`detile_params_address_every_element_as_offset_does`). - `crates/victory-gpu/src/gpudetile.rs`: self-contained `cs_5_1` compute detiler, staging layout helpers, dispatch planner (`jobs`), prelude recorder (`record`), per-frame meter line (`meter_line`), and HW + WARP + prelude integration tests. - `crates/victory-gpu/src/texture.rs`: `Staging::gpu`, `SPARE_LINEAR` pool, raw copy path in `Staging::detile`, `copy_source()`, `needs_detile()` / `record_detile()`, and automatic CPU fallback. - `crates/victory-gpu/src/texture/ahead.rs`: keep background copy-queue uploads CPU-detiled. - `crates/victory-gpu/src/host.rs`: `Deferred::detiles`, `HostList::detile_first`, `HostList::prelude(this_submission)`, `HostList::untouched`, and `gpudetile::meter_line(frames)`. - `crates/victory-gpu/src/lib.rs`: register `mod gpudetile;`. ## Verification (`rustc 1.98.1`, `VICTORY_D3D12_DEBUG=1`) - `cargo run -p xtask --locked -- policy` (`asset policy: 313 files ok`) & `cargo test -p xtask --locked`: **11/11 passed** - `cargo clippy -p victory-formats -p victory-gpu --all-targets --locked -- -D warnings`: **0 warnings** - `cargo clippy -p victory-formats -p victory-gpu --all-targets --all-features --locked -- -D warnings`: **0 warnings** - `cargo test -p victory-formats --locked`: **48 passed, 0 failed, 6 ignored** - D3D12 HW (`VICTORY_D3D12_DEBUG=1`): - `gpudetile::`: **5/5 passed** (25-case + 2 on-demand mask byte-exact matrix across RGBA8, BC1, BC3, BC5, BC7, R8, R16, R16G16B16A16F, R32G32B32A32F, POT + NPOT, 256B/4KB/64KB S+D, LINEAR, mip tails, arrays, 3D thick/linear, `level0`/`first` mip-clamping, pooled `raw_base` offset; 9 XOR mode fallbacks; end-to-end `HostList::prelude` upload + multi-list dedup + mip 0 & tail readback + dynamic v2 update; `ahead` CPU-only check) - `gpudetile::` with `VICTORY_PRELUDE_POOL=1`: **5/5 passed** - `gpudetile::` with `VICTORY_D3D12_GBV=1`: **5/5 passed** - `gpudetile::` & `texture::` (`--features diagnostics`): **5/5** & **8/8 passed** - `texture::` (`VICTORY_GPU_DETILE=0` & `VICTORY_GPU_DETILE=1`): **8/8 passed** - D3D12 WARP (`VICTORY_D3D12_WARP=1`, `VICTORY_D3D12_DEBUG=1`): - `gpudetile::`: **5/5 passed** - `texture::` (`VICTORY_GPU_DETILE=1`): **8/8 passed**
TaynanGT commented 2026-11-27 12:19:47 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-27T12:19:47Z.


@Vladimir-Human The copy-queue fallback and fence-gated reuse are the critical boundaries for this opt-in path. I’ll start with the CPU-versus-GPU byte checks at held mip levels and at the last narrow row, then keep the in-game comparison as a separate measurement with the same textures and camera.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7218112681 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/249#issuecomment-7218112681). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-27T12:19:47Z. --- @Vladimir-Human The copy-queue fallback and fence-gated reuse are the critical boundaries for this opt-in path. I’ll start with the CPU-versus-GPU byte checks at held mip levels and at the last narrow row, then keep the in-game comparison as a separate measurement with the same textures and camera.
Vladimir-Human commented 2026-11-27 12:59:19 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by Vladimir-Human on 2026-11-27T12:59:19Z.


@TaynanGT Thanks — here are the exact checkpoints for those four boundaries:

  • Held-mip range (level0/first): crates/victory-gpu/src/texture.rs:2252-2255 (first = read_from(&layout, first).max(level0).min(mips - 1), read = layout.bytes_from(first)) and crates/victory-gpu/src/gpudetile.rs:361 (layout.bytes_from(mip) <= raw.tiled_bytes), covered byte-exactly on HW and WARP by gpudetile::tests::gpu_detile_matches_cpu_on_hardware (gpudetile.rs:923), gpu_detile_matches_cpu_on_warp (gpudetile.rs:917), and prelude_uploads_detiled_textures_byte_exact (gpudetile.rs:980, including level0 == first == 2 and level0 == 0, first == 2).
  • Narrow-row 4-byte rounding: crates/victory-gpu/src/gpudetile.rs:369-386, 445 (words = row_bytes.div_ceil(4), written_end = last_row + words * 4, Plan::linear_bytes = max_written_end.next_multiple_of(4)) and crates/victory-gpu/src/texture.rs:2294 (total.max(p.linear_bytes).next_multiple_of(4)), keeping 1B/2B last-row UAV writes strictly in bounds.
  • Copy-queue CPU-only fallback: crates/victory-gpu/src/texture/ahead.rs:125-129 (super::staging(..., false) + !s.is_gpu() filter) and crates/victory-gpu/src/texture.rs:2232, 2289 (allow_gpu), verified by gpudetile::tests::ahead_copy_queue_never_uses_gpu_staging (gpudetile.rs:1170).
  • Fence-gated SPARE_LINEAR reuse: crates/victory-gpu/src/texture.rs:175-195 (entry.after <= finished against host::submissions()), texture.rs:607-622 (returns to SPARE_LINEAR tagged with started under spare_linear_limit()), and texture.rs:656-659 (let_go_idle -> trim_spare_linear).
<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7218920144 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/249#issuecomment-7218920144). Originally posted by [Vladimir-Human](https://github.com/Vladimir-Human) on 2026-11-27T12:59:19Z. --- @TaynanGT Thanks — here are the exact checkpoints for those four boundaries: - **Held-mip range (`level0`/`first`):** `crates/victory-gpu/src/texture.rs:2252-2255` (`first = read_from(&layout, first).max(level0).min(mips - 1)`, `read = layout.bytes_from(first)`) and `crates/victory-gpu/src/gpudetile.rs:361` (`layout.bytes_from(mip) <= raw.tiled_bytes`), covered byte-exactly on HW and WARP by `gpudetile::tests::gpu_detile_matches_cpu_on_hardware` (`gpudetile.rs:923`), `gpu_detile_matches_cpu_on_warp` (`gpudetile.rs:917`), and `prelude_uploads_detiled_textures_byte_exact` (`gpudetile.rs:980`, including `level0 == first == 2` and `level0 == 0, first == 2`). - **Narrow-row 4-byte rounding:** `crates/victory-gpu/src/gpudetile.rs:369-386, 445` (`words = row_bytes.div_ceil(4)`, `written_end = last_row + words * 4`, `Plan::linear_bytes = max_written_end.next_multiple_of(4)`) and `crates/victory-gpu/src/texture.rs:2294` (`total.max(p.linear_bytes).next_multiple_of(4)`), keeping 1B/2B last-row UAV writes strictly in bounds. - **Copy-queue CPU-only fallback:** `crates/victory-gpu/src/texture/ahead.rs:125-129` (`super::staging(..., false)` + `!s.is_gpu()` filter) and `crates/victory-gpu/src/texture.rs:2232, 2289` (`allow_gpu`), verified by `gpudetile::tests::ahead_copy_queue_never_uses_gpu_staging` (`gpudetile.rs:1170`). - **Fence-gated `SPARE_LINEAR` reuse:** `crates/victory-gpu/src/texture.rs:175-195` (`entry.after <= finished` against `host::submissions()`), `texture.rs:607-622` (returns to `SPARE_LINEAR` tagged with `started` under `spare_linear_limit()`), and `texture.rs:656-659` (`let_go_idle` -> `trim_spare_linear`).
TaynanGT commented 2026-11-27 13:08:54 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-27T13:08:54Z.


@Vladimir-Human The byte-exact held-mip checks on hardware and WARP cover the rounding case. The copy-queue fallback and after <= finished condition are the right boundaries for the next runtime comparison. Use the same scene and compare upload time and bytes along with the rendered frame times.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7219117741 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/249#issuecomment-7219117741). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-27T13:08:54Z. --- @Vladimir-Human The byte-exact held-mip checks on hardware and WARP cover the rounding case. The copy-queue fallback and `after <= finished` condition are the right boundaries for the next runtime comparison. Use the same scene and compare upload time and bytes along with the rendered frame times.
TaynanGT commented 2026-11-30 17:15:01 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-30T17:15:01Z.


main has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current main and open a new PR; I'll review it there.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7285960430 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/249#issuecomment-7285960430). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-30T17:15:01Z. --- `main` has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current `main` and open a new PR; I'll review it there.

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
TaynanGT/Vice-Tay!249
No description provided.