perf(rt): prioritize VRAM for BVH arenas and use GPU-ordered single-buffer PC-RAM spill #290

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

@⁠Vladimir-Human on GitHub · 2026-11-27 13:48 UTC · original
Vladimir-Human:perf/rt-vram-arenas-and-port-rt → main · +815 −55 · 4 files · 1 commits · open

Narrowed strictly to VRAM BVH arena budgeting/reclamation and GPU-ordered single-buffer PC-RAM spill per review feedback. The opt-in VICTORY_PORT_RT=1 forward-fill path and VICTORY_RT_STAMPS=1 diagnostics have been split out into standalone PR #299.

Summary (4 files in crates/victory-gpu)

  • crates/victory-gpu/src/host.rs:
    • Adds host::drain_retired() to flush matured retired COM resources whose fence has completed before checking VRAM headroom.
    • Drains shadow::direct_dirty() inside Host::card_before() via a transient upload buffer and GPU-ordered CopyBufferRegion, keeping _synced and _direct_uploads alive in CardPlan until GPU completion (no unsynchronized CPU overwrites into in-flight GPU reads).
    • Adds drain_retired_releases_matured_com_resource_after_fence verifying actual COM resource release after fence completion while retaining in-flight entries.
  • crates/victory-gpu/src/resource.rs:
    • Adds lock-free atomic request_vram_eviction(bytes) consumed on the submitting thread in let_go_idle(), avoiding MEMORY -> TEXTURES lock nesting and avoiding any phantom VRAM budget credit before eviction actually releases resources.
  • crates/victory-gpu/src/shadow.rs:
    • Avoids allocating a duplicate 64 MiB staging buffer when a by-address arena spills to PC RAM (by_address_pc_ram_fallback_uses_single_buffer_without_staging).
    • Adds Shadow::stage_direct(), coalesce_direct_runs() (sorted, disjoint, newest-wins coalescing for overlapping CPU writes), and direct_dirty(), verified by coalesce_direct_runs_is_disjoint_and_newest_wins and GPU CopyBufferRegion upload tests.
    • Decouples by-address VRAM arena accounting (BY_ADDRESS_CARD_BYTES) with allocation/drop coverage (by_address_vram_arena_tracks_and_releases_card_bytes).
  • crates/victory-gpu/src/memory.rs:
    • Calls host::drain_retired() before evaluating real has_room(host.video_memory()) for by-address arenas; when VRAM is still tight, signals resource::request_vram_eviction(len * 2) for the submitting thread and falls back cleanly to single-buffer PC RAM.
    • Makes delayed_free monotonically bury_lag-aware via compute_delayed_until(...) (delayed_free_holds_pc_ram_slot_until_flight_completes).
    • Exposes is_in_pc PC-RAM arena telemetry in Memory::report.

Verification

Run with VICTORY_D3D12_DEBUG=1 (0 D3D12 debug layer errors):

  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings (default & --features diagnostics): 0 warnings
  • HW default (memory::tests, by_address, coalesce_direct_runs, drain_retired_releases): 10/10
  • HW --features diagnostics (same suite): 11/11
  • HW --features diagnostics parallel stress (descriptor::host::tests + drain_retired_releases concurrently without --test-threads=1): 49/49
  • WARP (VICTORY_D3D12_WARP=1, --features diagnostics): 11/11
  • xtask policy clean (315 files ok); cargo test -p xtask --locked: 11/11

Same-Route A/B Measurement Plan (Local VRAM + Private Bytes + Frame Times)

No live title capture is present in this automated test environment; to evaluate on hardware against main (with VICTORY_PORT_RT unset):

  1. Record a fixed RT-heavy camera route on main and on this branch.
  2. Log together on both runs:
    • Local VRAM usage (Host::video_memory() / BY_ADDRESS_CARD_BYTES and DXGI local CurrentUsage)
    • Process private bytes (reflecting elimination of duplicate 64 MiB staging allocations on PC-RAM spilled arenas)
    • Memory::report PC-RAM arena count (is_in_pc)
    • Frame times (median / p95 / p99 ms)
**@⁠Vladimir-Human** on GitHub · 2026-11-27 13:48 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/290) `Vladimir-Human:perf/rt-vram-arenas-and-port-rt` → `main` · +815 −55 · 4 files · 1 commits · open Narrowed strictly to VRAM BVH arena budgeting/reclamation and GPU-ordered single-buffer PC-RAM spill per review feedback. The opt-in `VICTORY_PORT_RT=1` forward-fill path and `VICTORY_RT_STAMPS=1` diagnostics have been split out into standalone PR #299. ### Summary (4 files in `crates/victory-gpu`) - **`crates/victory-gpu/src/host.rs`**: - Adds `host::drain_retired()` to flush matured retired COM resources whose fence has completed before checking VRAM headroom. - Drains `shadow::direct_dirty()` inside `Host::card_before()` via a transient upload buffer and GPU-ordered `CopyBufferRegion`, keeping `_synced` and `_direct_uploads` alive in `CardPlan` until GPU completion (no unsynchronized CPU overwrites into in-flight GPU reads). - Adds `drain_retired_releases_matured_com_resource_after_fence` verifying actual COM resource release after fence completion while retaining in-flight entries. - **`crates/victory-gpu/src/resource.rs`**: - Adds lock-free atomic `request_vram_eviction(bytes)` consumed on the submitting thread in `let_go_idle()`, avoiding `MEMORY -> TEXTURES` lock nesting and avoiding any phantom VRAM budget credit before eviction actually releases resources. - **`crates/victory-gpu/src/shadow.rs`**: - Avoids allocating a duplicate 64 MiB `staging` buffer when a by-address arena spills to PC RAM (`by_address_pc_ram_fallback_uses_single_buffer_without_staging`). - Adds `Shadow::stage_direct()`, `coalesce_direct_runs()` (sorted, disjoint, newest-wins coalescing for overlapping CPU writes), and `direct_dirty()`, verified by `coalesce_direct_runs_is_disjoint_and_newest_wins` and GPU `CopyBufferRegion` upload tests. - Decouples by-address VRAM arena accounting (`BY_ADDRESS_CARD_BYTES`) with allocation/drop coverage (`by_address_vram_arena_tracks_and_releases_card_bytes`). - **`crates/victory-gpu/src/memory.rs`**: - Calls `host::drain_retired()` before evaluating real `has_room(host.video_memory())` for by-address arenas; when VRAM is still tight, signals `resource::request_vram_eviction(len * 2)` for the submitting thread and falls back cleanly to single-buffer PC RAM. - Makes `delayed_free` monotonically `bury_lag`-aware via `compute_delayed_until(...)` (`delayed_free_holds_pc_ram_slot_until_flight_completes`). - Exposes `is_in_pc` PC-RAM arena telemetry in `Memory::report`. ### Verification Run with `VICTORY_D3D12_DEBUG=1` (`0` D3D12 debug layer errors): - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` (default & `--features diagnostics`): **0 warnings** - HW default (`memory::tests`, `by_address`, `coalesce_direct_runs`, `drain_retired_releases`): **10/10** - HW `--features diagnostics` (same suite): **11/11** - HW `--features diagnostics` parallel stress (`descriptor::host::tests` + `drain_retired_releases` concurrently without `--test-threads=1`): **49/49** - WARP (`VICTORY_D3D12_WARP=1`, `--features diagnostics`): **11/11** - `xtask policy` clean (`315 files ok`); `cargo test -p xtask --locked`: **11/11** ### Same-Route A/B Measurement Plan (Local VRAM + Private Bytes + Frame Times) No live title capture is present in this automated test environment; to evaluate on hardware against `main` (with `VICTORY_PORT_RT` unset): 1. Record a fixed RT-heavy camera route on `main` and on this branch. 2. Log together on both runs: - Local VRAM usage (`Host::video_memory()` / `BY_ADDRESS_CARD_BYTES` and DXGI local `CurrentUsage`) - Process private bytes (reflecting elimination of duplicate 64 MiB `staging` allocations on PC-RAM spilled arenas) - `Memory::report` PC-RAM arena count (`is_in_pc`) - Frame times (median / p95 / p99 ms)
Keeps ray-tracing BVH arenas in local VRAM longer, removes duplicate 64 MiB
staging allocations on PC-RAM spill with GPU-ordered uploads, adds residency
telemetry, and ports the opt-in `VICTORY_PORT_RT=1` submission-stall fix and
`VICTORY_RT_STAMPS=1` dispatch profiler:

1. VRAM priority & single-buffer PC-RAM spill (`resource.rs`, `shadow.rs`,
   `memory.rs`, `host.rs`):
   - Adds `host::drain_retired()` (drops already-matured retired resources
     safely without holding `TEXTURES` or waiting on the GPU) before
     re-checking real DXGI `video_memory()` headroom; when VRAM is still full,
     signals `resource::request_vram_eviction(len * 2)` consumed on the single
     submitting thread inside `resource::let_go_idle()` (preserving its >= 5 s
     idle window and avoiding `MEMORY -> TEXTURES` lock nesting) without
     crediting unfreed `RETIRED` bytes against real DXGI budget.
   - Decouples `BY_ADDRESS_CARD_BYTES` from `WINDOW_CARD_BYTES` (preventing
     64 MiB RT arenas from starving loose non-RT windows under
     `VICTORY_CARD_WINDOWS_MB`) and rolls back/decrements `BY_ADDRESS_USED`
     cleanly across error paths and `Drop`.
   - When spilled to PC RAM, allocates a single mapped `shared_buffer` with no
     duplicate 64 MiB `staging` buffer; stages only changed dword runs via
     `Shadow::stage_direct()` (and coalesces overlapping writes into strictly
     disjoint runs) to be uploaded via GPU-ordered `CopyBufferRegion` in
     `Host::card_before` using a tiny transient upload buffer, so CPU writes
     never race live GPU-readable shared memory and adjacent GPU-written dwords
     stay intact.
   - Defers recycled single-buffer PC-RAM slots in `Memory::gone()` via
     `delayed_free` using a strictly monotonic target incorporating
     `bury_lag()` (minimum 1) and `cut_for` headroom so recording and split
     submissions never read overwritten slots. Frees reserved virtual memory
     if `Memory::another()` fails to back an arena.
   - Adds `is_in_pc()` telemetry logging PC-RAM copy and arena counts in
     `Memory::report()` (adapted from avatarmanz).
2. Opt-in `VICTORY_PORT_RT=1` & `VICTORY_RT_STAMPS=1` (`port_rt.rs`,
   `host/rtstamps.rs`, `host/card_steps.rs`, `host/place.rs`, `host.rs`,
   `draw.rs`):
   - Ports `avatarmanz`'s `VICTORY_PORT_RT=1` placed-copy ordering (rejecting
     upload-backed placed copies whose source was written by an earlier
     card-taken copy), list-cut counters, and same-list
     `HostList::forward_fill`, plus diagnostics-gated `VICTORY_RT_STAMPS=1`
     per-dispatch GPU timestamps (with zero-frequency guard).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 14:01:51 +00:00
Author
Owner

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

@⁠Vladimir-Human Separate the arena budget and RAM-spill comparison from the submission-stall switch. The critical checks are actual budget reclamation after the fence, newest-wins upload ordering, and a same-route A/B with local VRAM, private bytes and frame times recorded together.

**@⁠TaynanGT** on GitHub · 2026-11-27 13:51 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/290#issuecomment-7220037527) @⁠Vladimir-Human Separate the arena budget and RAM-spill comparison from the submission-stall switch. The critical checks are actual budget reclamation after the fence, newest-wins upload ordering, and a same-route A/B with local VRAM, private bytes and frame times recorded together.
github-bot changed title from perf(rt): prioritize VRAM for BVH arenas, avoid duplicate PC-RAM staging, and port VICTORY_PORT_RT to perf(rt): prioritize VRAM for BVH arenas and use GPU-ordered single-buffer PC-RAM spill 2026-11-27 14:43:25 +00:00
github-bot force-pushed github-bot/gh-290 from c6ce32bd96 to 46d7a3ebdc 2026-11-27 14:43:26 +00:00 Compare
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-27 14:40 UTC · original

@⁠TaynanGT Split and narrowed as requested:

  1. Separated the submission-stall switch: Moved VICTORY_PORT_RT=1 (forward_fill, Does::Placed.from_upload ordering guard) and diagnostics-gated VICTORY_RT_STAMPS=1 into standalone PR #299. #290 is now strictly the 4 files for VRAM BVH arena budgeting/reclamation and GPU-ordered single-buffer PC-RAM spill (host.rs, memory.rs, resource.rs, shadow.rs).
  2. Actual budget reclamation after the fence:
    • by_address_inner() drains matured retired COM resources via host::drain_retired() before checking real has_room(host.video_memory()) (no phantom VRAM credit; eviction requests go lock-free via resource::request_vram_eviction() to let_go_idle() on the submitting thread).
    • Verified with host::batch::drain_retired_releases_matured_com_resource_after_fence (actual COM Release of matured entries while retaining in-flight entries; also verified under parallel stress with descriptor::host::tests 49/49), by_address_vram_arena_tracks_and_releases_card_bytes (decoupled BY_ADDRESS_CARD_BYTES accounting on alloc/drop), and memory::tests::delayed_free_holds_pc_ram_slot_until_flight_completes (monotonic bury_lag-aware deferral).
  3. Newest-wins upload ordering:
    • PC-RAM spilled arenas avoid allocating a duplicate 64 MiB staging buffer and instead queue writes through Shadow::stage_direct(), merged by coalesce_direct_runs() into sorted, disjoint, newest-wins byte ranges, then uploaded in Host::card_before() via transient upload buffer + GPU-ordered CopyBufferRegion (keeping _synced and _direct_uploads alive in CardPlan until GPU completion).
    • Verified on both HW and WARP (VICTORY_D3D12_DEBUG=1, 0 debug errors) by coalesce_direct_runs_is_disjoint_and_newest_wins and by_address_pc_ram_fallback_uses_single_buffer_without_staging.
  4. Same-route A/B measurement: Because this automated environment has no live title capture, I have not claimed live frame-time numbers; the PR description lists the exact same-route A/B capture recording local VRAM (video_memory / BY_ADDRESS_CARD_BYTES), process private bytes, Memory::report is_in_pc counts, and frame times together on main vs #290 independently of #299.
**@⁠Vladimir-Human** on GitHub · 2026-11-27 14:40 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/290#issuecomment-7221085281) @⁠TaynanGT Split and narrowed as requested: 1. **Separated the submission-stall switch:** Moved `VICTORY_PORT_RT=1` (`forward_fill`, `Does::Placed.from_upload` ordering guard) and diagnostics-gated `VICTORY_RT_STAMPS=1` into standalone PR #299. `#290` is now strictly the 4 files for VRAM BVH arena budgeting/reclamation and GPU-ordered single-buffer PC-RAM spill (`host.rs`, `memory.rs`, `resource.rs`, `shadow.rs`). 2. **Actual budget reclamation after the fence:** - `by_address_inner()` drains matured retired COM resources via `host::drain_retired()` before checking real `has_room(host.video_memory())` (no phantom VRAM credit; eviction requests go lock-free via `resource::request_vram_eviction()` to `let_go_idle()` on the submitting thread). - Verified with `host::batch::drain_retired_releases_matured_com_resource_after_fence` (actual COM `Release` of matured entries while retaining in-flight entries; also verified under parallel stress with `descriptor::host::tests` **49/49**), `by_address_vram_arena_tracks_and_releases_card_bytes` (decoupled `BY_ADDRESS_CARD_BYTES` accounting on alloc/drop), and `memory::tests::delayed_free_holds_pc_ram_slot_until_flight_completes` (monotonic `bury_lag`-aware deferral). 3. **Newest-wins upload ordering:** - PC-RAM spilled arenas avoid allocating a duplicate 64 MiB `staging` buffer and instead queue writes through `Shadow::stage_direct()`, merged by `coalesce_direct_runs()` into sorted, disjoint, newest-wins byte ranges, then uploaded in `Host::card_before()` via transient upload buffer + GPU-ordered `CopyBufferRegion` (keeping `_synced` and `_direct_uploads` alive in `CardPlan` until GPU completion). - Verified on both HW and WARP (`VICTORY_D3D12_DEBUG=1`, 0 debug errors) by `coalesce_direct_runs_is_disjoint_and_newest_wins` and `by_address_pc_ram_fallback_uses_single_buffer_without_staging`. 4. **Same-route A/B measurement:** Because this automated environment has no live title capture, I have not claimed live frame-time numbers; the PR description lists the exact same-route A/B capture recording local VRAM (`video_memory` / `BY_ADDRESS_CARD_BYTES`), process private bytes, `Memory::report` `is_in_pc` counts, and frame times together on `main` vs `#290` independently of `#299`.
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 14:49 UTC · original

@⁠Vladimir-Human The fence release test and newest-wins checks cover the two hazards in the spill path. For the game run, log local VRAM, private bytes, spill count and p10 frame time together. Keep #299 disabled for that pair so the change in allocation policy is the only variable.

**@⁠TaynanGT** on GitHub · 2026-11-27 14:49 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/290#issuecomment-7221267268) @⁠Vladimir-Human The fence release test and newest-wins checks cover the two hazards in the spill path. For the game run, log local VRAM, private bytes, spill count and p10 frame time together. Keep #299 disabled for that pair so the change in allocation policy is the only variable.
github-bot force-pushed github-bot/gh-290 from 46d7a3ebdc to 315c946d8d 2026-11-27 20:13:11 +00:00 Compare
Author
Owner

@⁠hamdanpor-ctrl on GitHub · 2026-11-27 23:24 UTC · original

The new test shadow::copied_and_take_tests::by_address_pc_ram_fallback_uses_single_buffer_without_staging fails every time on my laptop: 5 of 5 parallel runs of cargo test -p victory-gpu --locked.

panicked at crates\victory-gpu\src\shadow.rs:7925:9
assertion `left == right` failed
  left: 65536
 right: 0

Line 7925 on that tree is assert_eq!(BY_ADDRESS_USED.load(Ordering::Relaxed), before_used);, so the 64 KiB PC-RAM fallback arena was counted in BY_ADDRESS_USED, which the test expects to stay unchanged. The other counter checks before it passed.

  • PC: RTX 3050 Laptop GPU 4 GB (driver 617.42) with Intel Iris Xe, i7-12700H, 16 GB, Windows 11 Pro; Rust 1.98.1
  • Tree: this PR's own diff (315c946 against its merge base) applied on main at fe7c6e5
  • Clippy with and without --all-features: clean; the other 533 victory-gpu tests passed

Not tried with the PR's own base alone; happy to run anything that helps.

AI-assisted (Claude Code).

**@⁠hamdanpor-ctrl** on GitHub · 2026-11-27 23:24 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/290#issuecomment-7230117703) The new test `shadow::copied_and_take_tests::by_address_pc_ram_fallback_uses_single_buffer_without_staging` fails every time on my laptop: 5 of 5 parallel runs of `cargo test -p victory-gpu --locked`. ``` panicked at crates\victory-gpu\src\shadow.rs:7925:9 assertion `left == right` failed left: 65536 right: 0 ``` Line 7925 on that tree is `assert_eq!(BY_ADDRESS_USED.load(Ordering::Relaxed), before_used);`, so the 64 KiB PC-RAM fallback arena was counted in `BY_ADDRESS_USED`, which the test expects to stay unchanged. The other counter checks before it passed. - PC: RTX 3050 Laptop GPU 4 GB (driver 617.42) with Intel Iris Xe, i7-12700H, 16 GB, Windows 11 Pro; Rust 1.98.1 - Tree: this PR's own diff (`315c946` against its merge base) applied on `main` at `fe7c6e5` - Clippy with and without `--all-features`: clean; the other 533 `victory-gpu` tests passed Not tried with the PR's own base alone; happy to run anything that helps. AI-assisted (Claude Code).
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/290/head:github-bot/gh-290'
git switch 'github-bot/gh-290'

Merge

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