Skip to content

GPU: pool HostList device-local UAV buffers and wire ahead::card_if_ready - #884

Open
Starchaser1024 wants to merge 1 commit into
trustno1-coder:mainfrom
Starchaser1024:perf/uav-buffer-pool
Open

Starchaser1024 wants to merge 1 commit into
trustno1-coder:mainfrom
Starchaser1024:perf/uav-buffer-pool

Conversation

@Starchaser1024

Copy link
Copy Markdown

Summary

Pools COMMON-state device-local scratch UAV buffers used by HostList (card_budget, indirect_scratch, batch_place, and place::between) across list resets and drops, and wires ahead::card_if_ready() into Host::uav_buffer — addressing the remaining per-list device-buffer churn noted in #57 (~13,500 buffers / 7.5 s of synchronous CreateCommittedResource + MakeResident + mapped() fence waits per 5 minutes, amplified now that VICTORY_ALLOCATOR_TRIM is on by default).

Design & Safety Invariants

  1. Preserving Shadow zero-initialization vs transient HostList scratch (crates/victory-gpu/src/host/uav_pool.rs, host.rs, ahead.rs):
    • Shadow (take, dword_runs, by_address) and ambient rely on Host::uav_buffer returning a freshly zero-initialized buffer. Host::uav_buffer therefore strictly preserves zero-initialized semantics (consulting ahead::card_if_ready() for 64 KiB pre-made zeroed buffers once CARD_WANTED is active, without spawning victory-ahead during victory warm).
    • uav_pool::take_uav(host, size) serves the four transient HostList scratch consumers (make_card_budget, indirect_scratch, batch_place, and place::between), each of which overwrites its region before reading.
  2. Exact Width power-of-two bucketing:
    • UAV_SPARE keys by exact GetDesc().Width in power-of-two multiples of 64 KiB (64 KiB minimum up to 16 MiB per item), so card_budget (64 KiB), batch_args (64 KiB), indirect_scratch, and place::between share unified buckets without altering GetDesc().Width expectations.
  3. D3D12 resource state & in-flight GPU safety:
    • HostList::between tracks buffers in Deferred::between_uavs and batch-transitions them back to D3D12_RESOURCE_STATE_COMMON in HostList::close() (between_common), matching card_budget_out, indirect_common, and batches_common.
    • On reset() or Drop for HostList (IDLE_MOST overflow and VICTORY_ALLOCATOR_TRIM), buffers pass through retire_uavs: entering UAV_SPARE immediately when no submissions are in flight, or waiting in RETIRED_UAVS until bury(finished) observes their fence completion.
  4. Caps, pressure trimming & switch:
    • Capped at 256 MiB (128 MiB on <= 32 GiB hosts), gated by VICTORY_UAV_POOL (default 1, =0 disables pooling), and hooked into Host::sweep() and trim_spare_chunks() (16 MiB warm floor under mild pressure / allocator_trim, 0 under critical commit or >= 95% VRAM).

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

host::uav_pool::tests::bench_host_list_uav_pool_ab (7 rounds each):

Workload VICTORY_UAV_POOL=0 VICTORY_UAV_POOL=1 Speedup
Steady-state reset() + 4x between copies across 64 HostLists (256 between buffers/round) 125.01 ms median 0.47 ms median 264.9x (-124.54 ms)
Full 64-HostList create + record (card_budget + batch_place + 2x between) + reset + drop churn 175.97 ms median (1,792 D3D12 UAVs made) 49.15 ms median (0 D3D12 UAVs made, 1,792 pool hits) 3.58x (-126.82 ms)
  • host::uav_pool::tests::host_list_uav_pool_lifecycle_retire_trim_and_info_queue: verified under VICTORY_D3D12_DEBUG=1 (0 ID3D12InfoQueue errors across card_budget, mid-list indirect_scratch growth, batch_place, between copy, reset(), in-flight retirement, and trim).
  • cargo test -p victory-gpu --locked: 457 passed, 0 failed.
  • cargo clippy --workspace --all-targets --all-features --locked: 0 warnings in changed files.
  • cargo xtask policy: 295 files ok.

- Add `UAV_SPARE` (`crates/victory-gpu/src/host/uav_pool.rs`) to pool
  `COMMON`-state device-local scratch UAV buffers for `HostList::card_budget`,
  `indirect_scratch`, `batch_place`, and `place::between` across resets and
  list drops (`IDLE_MOST` overflow and `VICTORY_ALLOCATOR_TRIM` idle trim),
  eliminating thousands of synchronous `CreateCommittedResource` +
  `MakeResident` + `mapped()` stalls.
- Key `UAV_SPARE` by exact `Width` in power-of-two multiples of 64 KiB (up to
  16 MiB per item) while keeping `Host::uav_buffer` strictly zero-initialized
  for `Shadow` callers; transition `between` buffers back to `COMMON` at
  `HostList::close` and gate dropped list returns behind `RETIRED_UAVS` until
  in-flight GPU submissions finish.
- Cap `UAV_SPARE` at 256 MiB (128 MiB on `<= 32 GiB` hosts), add
  `VICTORY_UAV_POOL` (`=0` disables pooling), and trim idle or pressured
  entries in `Host::sweep` and `trim_spare_chunks` with a 16 MiB warm floor.
- Add `ahead::card_if_ready()` so `Host::uav_buffer(64 KiB)` reuses pre-made
  64 KiB card buffers once `CARD_WANTED` is active without spawning
  `victory-ahead` during `victory warm`.
@Alxspb

Alxspb commented Nov 28, 2026

Copy link
Copy Markdown

Build break when this is merged on top of #1012 (found while building a play-test stack; both PRs are currently MERGEABLE against main, but not with each other).

error[E0061]: this method takes 2 arguments but 1 argument was supplied
  --> crates\victory-gpu\src\host\uav_pool.rs:96:83

#1012 gave Spare::take a predicate (host.rs: fn take(&mut self, size: u64, matches: impl Fn(&T) -> bool)) because SPARE holds two classes of chunk — upload (card_writable = false) and card-writable — and mixing them would push a GPU-writable buffer into the CPU-only path. uav_pool.rs still calls take(size).

For UAV_SPARE the predicate is genuinely a no-op, so the call site can adapt without changing what this pool does: every supplier puts in the same class of buffer (DEFAULT heap + ALLOW_UNORDERED_ACCESS, retired only after the recordings that wrote them have run, so they come back in COMMON state) — between/place_copy via ahead::card, indirect_scratch, batch_args, card_budget, drain_retired_uavs; the upload and shared chunks live in host.rs's own SPARE and never enter this one. So:

-        && let Some(buffer) = UAV_SPARE.lock().unwrap_or_else(|e| e.into_inner()).take(size)
+        && let Some(buffer) = UAV_SPARE
+            .lock()
+            .unwrap_or_else(|e| e.into_inner())
+            .take(size, |_| true)

I run exactly that in a 12-PR stack (#1002 #989 #1005 #65 #1014 #884 #941 #816 #797 #933 #934 #614 #1012) — green build here: https://github.com/Alxspb/VIctory-NVIDIA/actions/runs/46145821590 (the failing run before the fix: https://github.com/Alxspb/VIctory-NVIDIA/actions/runs/46145596521). Verified on an RTX 5060 Ti 16 GB / i5-13600KF, Series S job + DLSS Quality + 2x frame generation; the pool's own debug-layer reuse test is part of that stack, and VICTORY_UAV_POOL=0 is kept as the A/B switch.

If you would rather take the other direction (a take_where(size, pred) in host.rs so the old one-argument take stays), say so and I'll send the two-line change against your branch.

@Starchaser1024

Copy link
Copy Markdown
Author

Thanks @Alxspb for testing #884 in your RTX 5060 Ti play-test stack and flagging the interaction with #1012!

Agreed — keeping 1-arg Spare::take(size) (delegating to take_where(size, pred) on #1012's side) is the cleanest path so both #884 and #1012 compile standalone against current main and merge cleanly in either order. And if #1012 lands on main first with the 2-arg signature, I'll rebase uav_pool.rs to .take(size, |_| true) right away.

Alxspb added a commit to Alxspb/VIctory-NVIDIA that referenced this pull request Nov 28, 2026
…are matcher

Merged-in trustno1-coder#1184 met trustno1-coder#933 lock-free copied[] and trustno1-coder#884 Spare::take matcher; adapt both calls instead of reintroducing a lock.
kadiratesdev added a commit to kadiratesdev/VIctory-NVIDIA that referenced this pull request Nov 28, 2026
…er the submission fence

# Conflicts:
#	crates/victory-gpu/src/host.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants