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

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

@⁠Vladimir-Human on GitHub · 2026-11-27 12:12 UTC · original
Vladimir-Human:perf/gpu-compute-prelude-detile → main · +1828 −14 · 6 files · 1 commits · open

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
**@⁠Vladimir-Human** on GitHub · 2026-11-27 12:12 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/249) `Vladimir-Human:perf/gpu-compute-prelude-detile` → `main` · +1828 −14 · 6 files · 1 commits · open ## 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**
Offload compatible texture detiling from CPU threads to a D3D12 compute pass
recorded in the command list's prelude (`VICTORY_GPU_DETILE=1`, opt-in;
`VICTORY_GPU_DETILE=alt` alternates 20s off/on), while preserving automatic
fallback to the CPU AVX2/SSE2 `DetilePool` path for unsupported modes,
background copy-queue uploads (`ahead`), movie planes, and memory pressure.

Key changes:
- `victory-formats`: add `Swizzle::name()`, `Layout::detile_params()`,
  `Layout::pattern_tables()`, and `DetileParams::address()` matching
  `Layout::offset()` / `Layout::detile()` across all addressable thin/thick
  non-XOR modes, linear layouts, mip tails, arrays, and 3D volumes, while
  rejecting all XOR swizzle modes (`_T`, `_X`).
- `victory-gpu/src/gpudetile.rs`: self-contained `cs_5_1` compute detiler
  (division-free power-of-two shift/mask addressing, 24 root constants + root
  SRV/UAV), pooled upload staging layout (`raw_layout`, `write_tables`,
  `write_mask`), 4-byte-rounded UAV sizing (`Plan::linear_bytes`), prelude
  dispatch recording (`COMMON -> UAV -> COMMON`), and per-frame meter line.
- `victory-gpu/src/texture.rs` & `texture/ahead.rs`: integrate GPU staging
  with `Staging::base` pooled offsets (`buffer_va + base`), held-level mip
  clamping (`first`/`level0` with `layout.bytes_from(first)`), on-demand
  held-level zeroing masks, fence-safe pressure-aware `SPARE_LINEAR` buffer
  pooling trimmed in `let_go_idle()`, and explicit CPU-only staging for
  copy-queue `ahead::claim`.
- `victory-gpu/src/host.rs`: record deduplicated per-submission compute
  detile passes in `HostList::prelude(this_submission)` (skipping empty
  preludes when an earlier list in the submission already detiled the texture)
  and log detiling stats in `Meter`.

Design adapted from `fxceless/victory-fork` commit `3461f52`
(`VICTORY_GPU_DETILE`), extended for `Vice-Tay`'s pooled staging base
offsets, held-level mip ranges (`first`/`level0`), copy-queue `ahead` safety,
exact 4-byte-rounded UAV buffer sizing, fence-safe pressure-aware linear
pooling, and division-free shift/mask shader addressing.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 12:23:23 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 12:19 UTC · original

@⁠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.

**@⁠TaynanGT** on GitHub · 2026-11-27 12:19 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/249#issuecomment-7218112681) @⁠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.
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-27 12:59 UTC · original

@⁠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).
**@⁠Vladimir-Human** on GitHub · 2026-11-27 12:59 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/249#issuecomment-7218920144) @⁠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`).
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 13:08 UTC · original

@⁠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** on GitHub · 2026-11-27 13:08 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/249#issuecomment-7219117741) @⁠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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/249/head:github-bot/gh-249'
git switch 'github-bot/gh-249'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-249'
git switch 'github-bot/gh-249'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-249'
git switch 'github-bot/gh-249'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-249'
git switch 'main'
git merge --squash 'github-bot/gh-249'
git switch 'main'
git merge --ff-only 'github-bot/gh-249'
git switch 'main'
git merge 'github-bot/gh-249'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.