GPU: pool persistently mapped ReadBack buffers and fix texback hazards #876

Closed
Starchaser1024 wants to merge 0 commits from upstream-pr/876 into main
Starchaser1024 commented 2026-11-28 08:25:30 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Starchaser1024 on 2026-11-28T08:25:30Z.


Summary

Fixes two GPU-to-CPU texture readback coherency hazards and eliminates steady-state D3D12_HEAP_TYPE_READBACK resource creation/destruction, GetCopyableFootprints COM calls, Map/Unmap, and tiling heap allocations across VICTORY_TEXTURE_BACK and VICTORY_READ_BACK (both on by default).

Coherency Fixes

  1. card_steps::writers (crates/victory-gpu/src/host/card_steps.rs):
    • Host::send_late records sent.texture_back_ranges into line.written because compute-written textures (such as the ocean height and displacement maps) are not tiled into title memory until texback::land runs on flight landing.
    • card_steps::writers previously pushed Writer { shadow: None } for flight.lists[i].host().deferred().backs while omitting flight.texture_back_ranges, allowing card_steps::holder to serve stale pre-compute bytes from any covering Shadow buffer before texback::land completed.
  2. host::joinable (crates/victory-gpu/src/host.rs):
    • joinable (VICTORY_JOIN_PARTS) previously checked declared_writes, deferred.writes, and deferred.backs, omitting deferred.texture_backs. Including deferred.texture_backs prevents merging part N (writing a readback texture) with part N + 1 reading overlapping title bytes on the CPU (Step::Fill, Step::Copy, Step::Texture).

Readback Pooling & Zero-Alloc Tiling (crates/victory-gpu/src/texback.rs, host.rs, list.rs)

  • texback::ReadBack: persistently mapped D3D12_HEAP_TYPE_READBACK buffers (Map once at creation, Unmap on final drop), bucketed by size.max(4096).next_power_of_two(), capped at 64 MiB (32 MiB on limited host RAM), trimmed under allocator_trim and cleared under residency::pressured(), gated by VICTORY_READBACK_POOL (default 1).
  • GPU lifetime safety: ReadBack::recycle returns completed buffers straight to READBACK_POOL inside HostList::reset() / list::recycle (ran lists), whereas Drop for ReadBack retires buffers via host::retire so aborted or unreset lists never race in-flight GPU CopyTextureRegion / CopyBufferRegion writes.
  • texback::record & texback::land: caches Arc<Footprints> keyed by (Width, Height, DepthOrArraySize, MipLevels, Format) (0 steady-state GetCopyableFootprints calls or Vec allocations) and reuses a 4 MiB-capped thread-local tiling buffer instead of vec![0u8; size] per readback.

Benchmark & Verification (RTX 5060 Ti 16 GB, Windows 11)

texback::tests::gpu_texback_and_read_backs_bit_exact_and_pool_benchmark (50 iterations of 256x256 R32G32_FLOAT 512 KiB CopyTextureRegion + Swizzle::SW_4KB_S tiling via texback::land + 5x2 RGBA32F exposure read_backs + HostList::reset(), verifying bit-exact detiled texels and pitched row preservation):

Profile Metric VICTORY_READBACK_POOL=0 VICTORY_READBACK_POOL=1 Speedup
--release CPU record + reset (50 iters) 30.31 ms (609.3 us median) 0.67 ms (14.5 us median) 45.5x (42.0x median)
--release End-to-end incl. GPU execute + land 38.14 ms (750.8 us median) 7.79 ms (149.1 us median) 4.90x (5.04x median)
debug CPU record + reset (50 iters) 45.48 ms (918.6 us median) 2.48 ms (51.0 us median) 18.4x (18.0x median)
  • host::card_steps::tests::flight_texture_back_ranges_refuse_stale_shadow_holder: pass.
  • host::batch::joinable_waits_for_texture_backs_and_joins_disjoint_parts: pass.
  • cargo test -p victory-gpu --locked: 458 passed, 0 failed.
  • cargo clippy --workspace --all-targets --all-features --locked: 0 warnings in changed files.
  • cargo xtask policy: 294 files ok.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5538294298 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/876). Originally posted by [Starchaser1024](https://github.com/Starchaser1024) on 2026-11-28T08:25:30Z. --- ## Summary Fixes two GPU-to-CPU texture readback coherency hazards and eliminates steady-state `D3D12_HEAP_TYPE_READBACK` resource creation/destruction, `GetCopyableFootprints` COM calls, `Map`/`Unmap`, and tiling heap allocations across `VICTORY_TEXTURE_BACK` and `VICTORY_READ_BACK` (both on by default). ### Coherency Fixes 1. **`card_steps::writers` (`crates/victory-gpu/src/host/card_steps.rs`)**: - `Host::send_late` records `sent.texture_back_ranges` into `line.written` because compute-written textures (such as the ocean height and displacement maps) are not tiled into title memory until `texback::land` runs on flight landing. - `card_steps::writers` previously pushed `Writer { shadow: None }` for `flight.lists[i].host().deferred().backs` while omitting `flight.texture_back_ranges`, allowing `card_steps::holder` to serve stale pre-compute bytes from any covering `Shadow` buffer before `texback::land` completed. 2. **`host::joinable` (`crates/victory-gpu/src/host.rs`)**: - `joinable` (`VICTORY_JOIN_PARTS`) previously checked `declared_writes`, `deferred.writes`, and `deferred.backs`, omitting `deferred.texture_backs`. Including `deferred.texture_backs` prevents merging part `N` (writing a readback texture) with part `N + 1` reading overlapping title bytes on the CPU (`Step::Fill`, `Step::Copy`, `Step::Texture`). ### Readback Pooling & Zero-Alloc Tiling (`crates/victory-gpu/src/texback.rs`, `host.rs`, `list.rs`) - **`texback::ReadBack`**: persistently mapped `D3D12_HEAP_TYPE_READBACK` buffers (`Map` once at creation, `Unmap` on final drop), bucketed by `size.max(4096).next_power_of_two()`, capped at `64 MiB` (`32 MiB` on limited host RAM), trimmed under `allocator_trim` and cleared under `residency::pressured()`, gated by `VICTORY_READBACK_POOL` (default `1`). - **GPU lifetime safety**: `ReadBack::recycle` returns completed buffers straight to `READBACK_POOL` inside `HostList::reset()` / `list::recycle` (`ran` lists), whereas `Drop for ReadBack` retires buffers via `host::retire` so aborted or unreset lists never race in-flight GPU `CopyTextureRegion` / `CopyBufferRegion` writes. - **`texback::record` & `texback::land`**: caches `Arc<Footprints>` keyed by `(Width, Height, DepthOrArraySize, MipLevels, Format)` (`0` steady-state `GetCopyableFootprints` calls or `Vec` allocations) and reuses a `4 MiB`-capped thread-local tiling buffer instead of `vec![0u8; size]` per readback. ## Benchmark & Verification (RTX 5060 Ti 16 GB, Windows 11) `texback::tests::gpu_texback_and_read_backs_bit_exact_and_pool_benchmark` (50 iterations of `256x256` `R32G32_FLOAT` `512 KiB` `CopyTextureRegion` + `Swizzle::SW_4KB_S` tiling via `texback::land` + `5x2` `RGBA32F` exposure `read_backs` + `HostList::reset()`, verifying bit-exact detiled texels and pitched row preservation): | Profile | Metric | `VICTORY_READBACK_POOL=0` | `VICTORY_READBACK_POOL=1` | Speedup | | :--- | :--- | ---: | ---: | ---: | | `--release` | CPU `record + reset` (50 iters) | **30.31 ms** (`609.3 us` median) | **0.67 ms** (`14.5 us` median) | **45.5x** (`42.0x` median) | | `--release` | End-to-end incl. GPU execute + land | **38.14 ms** (`750.8 us` median) | **7.79 ms** (`149.1 us` median) | **4.90x** (`5.04x` median) | | `debug` | CPU `record + reset` (50 iters) | **45.48 ms** (`918.6 us` median) | **2.48 ms** (`51.0 us` median) | **18.4x** (`18.0x` median) | - `host::card_steps::tests::flight_texture_back_ranges_refuse_stale_shadow_holder`: pass. - `host::batch::joinable_waits_for_texture_backs_and_joins_disjoint_parts`: pass. - `cargo test -p victory-gpu --locked`: 458 passed, 0 failed. - `cargo clippy --workspace --all-targets --all-features --locked`: 0 warnings in changed files. - `cargo xtask policy`: 294 files ok.

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!876
No description provided.