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

Closed
github-bot wants to merge 0 commits from github-bot/gh-876 into main AGit
Owner

@⁠Starchaser1024 on GitHub · 2026-11-28 08:25 UTC · original
Starchaser1024:fix/texback-pool-and-hazards → main · +929 −90 · 4 files · 1 commits · closed without merging

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.
**@⁠Starchaser1024** on GitHub · 2026-11-28 08:25 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/876) `Starchaser1024:fix/texback-pool-and-hazards` → `main` · +929 −90 · 4 files · 1 commits · closed without merging ## 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.
Fix two GPU-to-CPU texture readback coherency bugs and eliminate per-frame
D3D12_HEAP_TYPE_READBACK allocation, footprint query, map/unmap, and scratch
allocation churn across VICTORY_TEXTURE_BACK and VICTORY_READ_BACK:

- card_steps::writers: push Writer { shadow: None } for every range in
  flight.texture_back_ranges so holder refuses to copy stale pre-compute
  Shadow bytes while a flight's texture readback has not yet landed.
- host::joinable: include deferred.texture_backs ranges in written so part
  joining never prepares CPU reads (Step::Fill / Step::Copy / Step::Texture)
  ahead of an earlier part's pending texture readback.
- texback::ReadBack: pool persistently mapped D3D12_HEAP_TYPE_READBACK
  buffers bucketed by size.max(4096).next_power_of_two(), capped at 64 MiB
  (32 MiB on limited host RAM), trimmed under allocator_trim and host memory
  pressure, gated by VICTORY_READBACK_POOL (default 1). Recycle straight to
  the pool on HostList::reset() after fence completion and retire via
  host::retire on Drop so aborted/unreset lists never race in-flight GPU
  copies.
- texback::record / land: cache Arc<Footprints> per texture shape and reuse
  a 4 MiB-capped thread-local tiling buffer, removing per-readback COM
  GetCopyableFootprints, Map/Unmap, and 512 KiB heap zero-fills.
- Add unit tests for card_steps::writers + holder, host::joinable, ReadBack
  pool/retire/footprints invariants, and a 50-iteration RTX 5060 Ti GPU
  bit-exactness test & A/B benchmark (38.14 ms -> 7.79 ms end-to-end, 4.90x;
  30.31 ms -> 0.67 ms CPU record+reset, 45.48x).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 14:29:19 +00:00
github-bot changed title from GPU: pool persistently mapped ReadBack buffers and fix texback hazards to [pr-fix][pr-performance][memory][NVIDIA] GPU: pool persistently mapped ReadBack buffers and fix texback hazards 2026-11-29 01:55:15 +00:00
github-bot force-pushed github-bot/gh-876 from 6dc6a5ed2c to 1da999c40c 2026-11-29 02:01:22 +00:00 Compare
github-bot changed title from [pr-fix][pr-performance][memory][NVIDIA] GPU: pool persistently mapped ReadBack buffers and fix texback hazards to GPU: pool persistently mapped ReadBack buffers and fix texback hazards 2026-11-29 02:28:36 +00:00
github-bot closed this pull request 2026-11-29 07:48:21 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!876
No description provided.