gpu(shadow): offload >=1 MiB exact views & late buffers to GPU compare and optimize CPU fallback #14

Manually merged
github merged 1 commit from github-bot/gh-14 into mega-upgrade-v3 2026-12-04 20:20:04 +00:00 AGit
Owner

@⁠MahanYTT on GitHub · 2026-12-04 18:48 UTC · original
MahanYTT:perf/shadow-cpu-compare → mega-upgrade-v3 · +341 −62 · 1 files · 1 commits · merged 2026-12-04 20:18 UTC

Root Cause

Even with VICTORY_CARD_ALL=1 and VICTORY_CARD_BUFFERS_MIN_KB=64, title buffer copies >= 1 MiB could still fall back to CPU byte-compare (shader writes looked for on the CPU per frame: 174.5 MB in 12 ranges ... (0.0 MB snapshotted), of them 174.5 MB in copies of 1 MiB or more):

  1. Off-element buffer views (ExactCache::get_or_create / shadow::exact) never received a Card side: VICTORY_VIEW_BYTES (on by default) creates Shadows up to EXACT_MOST (64 MiB) with window = false and card = None, bypassing buffer_sides / card_for_counted completely. Under VICTORY_RELAY=2, overlapping flights hit pending.contains(&range) (0.0 MB snapshotted) while every flight still ran CPU find_all byte-compare across the full view.
  2. Early/refused title buffers in BUFFERS had no late promotion path: Unlike LOOSE windows (card_late_windows), a title buffer created before Host::card_ready() finished (e.g. under VICTORY_MADE_ASIDE=1) or during transient VRAM pressure remained uncarded for its entire lifetime.
  3. Shadow::relay_compared did not skip placed copies (self.place.is_some()): Unlike Shadow::writing and Shadow::copy_back_with, relay_compared still counted placed copies in BACK_CLASS (with 0.0 MB snapshotted) and find_all still locked shadow.before.
  4. CPU compare fallback overhead (find_all / find_pieces): Allocated a Box<dyn FnOnce()> per 1 MiB chunk, contended on a single shared Arc<Mutex<Found>>, sorted results, and woke the worker pool even for empty or single-chunk scans.

Changes (crates/victory-gpu/src/shadow.rs)

  • ExactCache (shadow::exact): Promote views >= 1 MiB (EXACT_CARD_MIN..=EXACT_MOST) to two-sided card copies via buffer_sides(..., card_for_counted, ...) so shader writes use GPU hash/compare. Keep smaller views on the fast ExactPool recycling path, and ensure carded entries are retired rather than recycled into ExactPool.
  • Late promotion for title buffers (card_late_buffers): Track eligible uncarded buffers (UNCARDED_BUFFERS) and promote them (largest first, up to LATE_AT_ONCE per submission) in let_go_idle once Host::card_ready() is ready and no command list / pending shader write holds them.
  • Shadow::relay_compared & find_all / find_pieces: Skip self.place.is_some() and empty ranges in relay_compared and find_all; fast-path empty and single-chunk scans in find_pieces; replace per-chunk Box jobs + shared mutex + sort with lock-free per-chunk slots over wide_each_scoped.
  • Unit tests: Added find_pieces_fast_paths_empty_and_single_chunk_scans and exact_card_and_buffer_promotion_eligibility in wide_tests.

Verification

  • cargo clippy --target x86_64-pc-windows-msvc -p victory-gpu -- -D warnings: pass (0 warnings)
  • cargo clippy --target x86_64-pc-windows-msvc -p victory-gpu --tests -- -D warnings: pass (0 warnings)
  • Standalone rustc --edition 2024 --test on find_pieces, exact_card_eligible, and card_buffer_promotable tests: 3/3 passed
**@⁠MahanYTT** on GitHub · 2026-12-04 18:48 UTC · [original](https://github.com/MahanYTT/VIctory-MA/pull/14) `MahanYTT:perf/shadow-cpu-compare` → `mega-upgrade-v3` · +341 −62 · 1 files · 1 commits · merged 2026-12-04 20:18 UTC ## Root Cause Even with `VICTORY_CARD_ALL=1` and `VICTORY_CARD_BUFFERS_MIN_KB=64`, title buffer copies `>= 1 MiB` could still fall back to CPU byte-compare (`shader writes looked for on the CPU per frame: 174.5 MB in 12 ranges ... (0.0 MB snapshotted), of them 174.5 MB in copies of 1 MiB or more`): 1. **Off-element buffer views (`ExactCache::get_or_create` / `shadow::exact`) never received a `Card` side**: `VICTORY_VIEW_BYTES` (on by default) creates `Shadow`s up to `EXACT_MOST` (64 MiB) with `window = false` and `card = None`, bypassing `buffer_sides` / `card_for_counted` completely. Under `VICTORY_RELAY=2`, overlapping flights hit `pending.contains(&range)` (0.0 MB snapshotted) while every flight still ran CPU `find_all` byte-compare across the full view. 2. **Early/refused title buffers in `BUFFERS` had no late promotion path**: Unlike `LOOSE` windows (`card_late_windows`), a title buffer created before `Host::card_ready()` finished (e.g. under `VICTORY_MADE_ASIDE=1`) or during transient VRAM pressure remained uncarded for its entire lifetime. 3. **`Shadow::relay_compared` did not skip placed copies (`self.place.is_some()`)**: Unlike `Shadow::writing` and `Shadow::copy_back_with`, `relay_compared` still counted placed copies in `BACK_CLASS` (with 0.0 MB snapshotted) and `find_all` still locked `shadow.before`. 4. **CPU compare fallback overhead (`find_all` / `find_pieces`)**: Allocated a `Box<dyn FnOnce()>` per 1 MiB chunk, contended on a single shared `Arc<Mutex<Found>>`, sorted results, and woke the worker pool even for empty or single-chunk scans. ## Changes (`crates/victory-gpu/src/shadow.rs`) - **`ExactCache` (`shadow::exact`)**: Promote views `>= 1 MiB` (`EXACT_CARD_MIN..=EXACT_MOST`) to two-sided card copies via `buffer_sides(..., card_for_counted, ...)` so shader writes use GPU hash/compare. Keep smaller views on the fast `ExactPool` recycling path, and ensure carded entries are retired rather than recycled into `ExactPool`. - **Late promotion for title buffers (`card_late_buffers`)**: Track eligible uncarded buffers (`UNCARDED_BUFFERS`) and promote them (largest first, up to `LATE_AT_ONCE` per submission) in `let_go_idle` once `Host::card_ready()` is ready and no command list / pending shader write holds them. - **`Shadow::relay_compared` & `find_all` / `find_pieces`**: Skip `self.place.is_some()` and empty ranges in `relay_compared` and `find_all`; fast-path empty and single-chunk scans in `find_pieces`; replace per-chunk `Box` jobs + shared mutex + sort with lock-free per-chunk slots over `wide_each_scoped`. - **Unit tests**: Added `find_pieces_fast_paths_empty_and_single_chunk_scans` and `exact_card_and_buffer_promotion_eligibility` in `wide_tests`. ## Verification - `cargo clippy --target x86_64-pc-windows-msvc -p victory-gpu -- -D warnings`: pass (0 warnings) - `cargo clippy --target x86_64-pc-windows-msvc -p victory-gpu --tests -- -D warnings`: pass (0 warnings) - Standalone `rustc --edition 2024 --test` on `find_pieces`, `exact_card_eligible`, and `card_buffer_promotable` tests: 3/3 passed
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 18:49:19 +00:00
github manually merged commit 4ba58378c8 into mega-upgrade-v3 2026-12-04 20:20:04 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.