Skip to content

GPU: route upload buffers to GPU_UPLOAD VRAM under budget and move indirect arguments to DEFAULT - #3228

Open
0xKaji wants to merge 2 commits into
Marvisak:mainfrom
0xKaji:perf/upload-buffers-vram
Open

0xKaji wants to merge 2 commits into
Marvisak:mainfrom
0xKaji:perf/upload-buffers-vram

Conversation

@0xKaji

@0xKaji 0xKaji commented Nov 26, 2026

Copy link
Copy Markdown

Summary

Routes CPU-write-only / GPU-read-only upload buffers (Host::upload_buffer: texture staging chunks/buffers, command-list scratch, descriptor strides/marks/addresses, title-memory page tables) to D3D12_HEAP_TYPE_GPU_UPLOAD (VRAM via Resizable BAR) while local VRAM stays below 80% of its DXGI budget, and moves GPU-only indirect argument buffers (HostList::arguments) to D3D12_HEAP_TYPE_DEFAULT.

Changes

  • ReBAR GPU_UPLOAD routing (host.rs): Checks D3D12_FEATURE_D3D12_OPTIONS16.GPUUploadHeapSupported once at device creation and routes Host::upload_buffer to D3D12_HEAP_TYPE_GPU_UPLOAD (D3D12_RESOURCE_STATE_COMMON) when local VRAM usage + allocation fits within 80% of DXGI local budget (refreshed every 250 ms or on >= 4 MiB allocations), falling back cleanly to D3D12_HEAP_TYPE_UPLOAD in system RAM. Toggleable via VICTORY_UPLOAD_VRAM=0.
  • GPU-only indirect arguments (host.rs, indirect.rs): Allocates HostList::arguments via Host::default_uav_buffer (D3D12_HEAP_TYPE_DEFAULT + ALLOW_UNORDERED_ACCESS) instead of CPU-mapped shared_buffer; updates indirect.rs tests to read back via a READBACK buffer.
  • Idle pool cleanup (host.rs, list.rs): Adds HostList::release_idle_buffers() called from list::recycle() so command lists parked in SPARE drop oversized arguments and trim scratch chunks.

Measured A/B (Linux Wine 11.22 + vkd3d-proton, RTX 3060 8 GiB ReBAR, 16 GiB RAM, Series S + #2112)

  • VICTORY_UPLOAD_VRAM=0 → VICTORY_UPLOAD_VRAM=1:
    • GPU system memory: 4,180 MB → 3,697 MB (-483 MB)
    • Process commit: 3,337 MB → 3,046 MB (-291 MB)
    • Working set / Peak RSS: 14,510 MB / 13,959 MiB → 13,357 MB / 12,847 MiB (-1,112 MiB RSS)
    • Upload allocation split: 0 MB VRAM / 177 MB RAM → 59 MB VRAM / 0 MB RAM
    • Median GPU frame time: 6.0 ms → 4.9 ms, median FPS: 28.9 → 30.0 (no regression).

Verification

  • cargo xwin clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings
  • cargo xwin clippy --workspace --all-targets --all-features --locked --target x86_64-pc-windows-msvc -- -D warnings
  • cargo test -p xtask --locked (6/6 passed)
  • Wine unit tests: upload_vram_fits_checks_80_percent_headroom, list_scratch_grows_to_fit_and_reuses_fenced_chunks (2/2 passed)

Partial Overlaps / Related PRs

  • Complementary to #3102, #2993, and #2875, which move shared_buffer shadow copies of title memory to VRAM; this PR targets upload_buffer and HostList::arguments instead.
  • Minor neighbor overlap with #2972 on HostList::scratch trimming (release_idle_buffers here only runs when a list is parked in SPARE, and does not alter HostList::reset or Scratch).

@potatoe0100

Copy link
Copy Markdown

Tested ef8d719 merged onto main 63bcca4 on an RTX 4060 8 GB (Ada, PCIe 4.0 x8, driver 621.14, Windows 11 Pro 26300, Resizable BAR on). No game files, so synthetic tests only.

Tests: cargo test -p victory-gpu -p victory-shader --lib --locked -- --test-threads=1 (skipping heap_direct_texture_reads_mark_their_heap_index, #2571). Same result as main on this card: the new upload_vram_fits_checks_80_percent_headroom, list_scratch_grows_to_fit_and_reuses_fenced_chunks and all indirect::tests pass. The only failures are the 5 known capped-heap ones (#1835), which fail on main too. OPTIONS16.GPUUploadHeapSupported is true here, so the GPU_UPLOAD path really ran.

Costs measured on this card (a throwaway --release probe, not part of the PR):

per buffer / rate
DEFAULT 64 KiB create + release (default_uav_buffer) 0.25 ms
UPLOAD 64 KiB create + map + release 0.18 ms
GPU_UPLOAD 64 KiB create + map + release 0.31 ms
CPU write, 64 MiB memcpy into UPLOAD 55.9 GB/s
CPU write, 64 MiB memcpy into GPU_UPLOAD 10.4 GB/s

Two things this suggests, for whoever reviews:

  1. release_idle_buffers() runs on every list::recycle(). It drops the indirect arguments buffer and trims scratch to at most one chunk. list.rs notes the title resets up to 21 lists a frame, so a recycled list that does indirect work makes a new DEFAULT buffer each time (about 0.25 ms here). A list that needs a few MiB of scratch also regrows it 64 KiB → 4 MiB, about 7 allocations. Before this PR both were kept across reuse. Would it be enough to trim only lists that have sat in SPARE for a while, or only when the buffer is well above what was used?
  2. Writes from the CPU into GPU_UPLOAD go over PCIe, so they run about 5x slower than into system memory on this x8 card. That cost now lands on the recording thread for texture staging chunks and list.upload copies. That's fine when the GPU copy saved is the bottleneck, but on a card with a narrow link it may move time into "preparing", which is already the largest part of a frame in the forks' measurements. A per-caller choice (VRAM for small, often-rewritten tables; system memory for large staging writes) might be worth measuring.

AI-assisted (Claude)

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on RTX 5090 (Resizable BAR / GPU_UPLOAD capable) against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib host::tests -- --test-threads=1 --skip dynamic_slots_are_held_until_released: passed.
  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 20/20 passed; indirect::: 6/6 passed; shadow::: 10/10 passed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly routes upload buffers to GPU_UPLOAD VRAM within budget and moves indirect arguments accordingly.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

@0xKaji

0xKaji commented Nov 26, 2026

Copy link
Copy Markdown
Author

Thanks for the RTX 4060 PCIe x8 measurements! Updated list::recycle() so the first HOT_SPARE = 24 lists in SPARE only trim oversized buffers (arguments > 256 KiB or scratch > 2 chunks) instead of releasing them on every recycle, keeping normal indirect argument and scratch buffers warm across frames. On point (2), VICTORY_UPLOAD_VRAM=0 remains available as a runtime escape hatch for narrow PCIe links if needed.

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good — clean, well-guarded routing of upload allocations to D3D12_HEAP_TYPE_GPU_UPLOAD under VRAM headroom and migration of indirect argument buffers to D3D12_HEAP_TYPE_DEFAULT.

Checked crates/victory-gpu/src/host.rs, indirect.rs, and list.rs, including every upload_buffer caller across the crate:

  • GPUUploadHeapSupported is probed via D3D12_FEATURE_D3D12_OPTIONS16 (host.rs:211-220), gated by VICTORY_UPLOAD_VRAM and the 80% local-VRAM headroom check (upload_vram_fits, refreshed every 250 ms or on allocations >= 4 MiB), with clean cache invalidation and fallback to D3D12_HEAP_TYPE_UPLOAD if CreateCommittedResource or Map fails.
  • Moving HostList::arguments (host.rs:2315) to default_uav_buffer (DEFAULT + ALLOW_UNORDERED_ACCESS, initial state COMMON) aligns with indirect.rs's existing COMMON -> UNORDERED_ACCESS -> INDIRECT_ARGUMENT -> COMMON transitions (indirect.rs:213-250), and indirect::tests cleanly read back via a readback staging buffer.
  • Hot/cold spare trimming in list::recycle (list.rs:283-295, host.rs:1830-1869) keeps normal warm buffers resident while dropping oversized indirect buffers (> 256 KiB) and excess scratch chunks.

Verification (b60d234a):

  • cargo xtask policy and cargo clippy -p victory-gpu (default and --features diagnostics, --tests -- -D warnings): passed.
  • indirect::tests:: (6/6) and host::tests::upload_vram_fits_checks_80_percent_headroom: passed on both hardware GPU and WARP with VICTORY_D3D12_DEBUG=1 and VICTORY_D3D12_GBV=1.
  • Live multi-state probe on hardware GPU (DEBUG=1 + GBV=1) confirmed allocated_heap_type = D3D12_HEAP_TYPE_GPU_UPLOAD (and clean fallback to D3D12_HEAP_TYPE_UPLOAD with VICTORY_UPLOAD_VRAM=0), verified a single GPU_UPLOAD buffer in COMMON used simultaneously as CopyBufferRegion source + root CBV + root SRV in one command list with zero barriers (debug_err = None), verified HostList::arguments uses DEFAULT and trim_oversized_buffers/release_idle_buffers behave as expected, and passed draw::tests (20/20), texture::tests (2/2), list::tests (4/4), and descriptor::host::tests (14/14) under DEBUG=1 + GBV=1.

aleksesipenko pushed a commit to aleksesipenko/VIctory-RTX that referenced this pull request Nov 27, 2026
…e recycle, independent DXGI HUD & step-back-safe CPU meter

Port three verified community improvements cleanly on top of 7b57565 for
VIctory-RTX (16 GB Ada + DXR 1.1):

1. Budgeted ReBAR GPU_UPLOAD routing & HOT_SPARE=24 warm list recycle
   (GPL-3.0 provenance: Marvisak/VIctory#3228):
   - Query D3D12_FEATURE_D3D12_OPTIONS16::GPUUploadHeapSupported once at
     device creation (Host::gpu_upload_supported) and reuse it from
     Host::mapped_card_memory.
   - Route Host::upload_buffer to D3D12_HEAP_TYPE_GPU_UPLOAD (COMMON
     initial state, resident + mapped, with automatic fallback to system-RAM
     UPLOAD and VICTORY_UPLOAD_VRAM=0 escape hatch).
   - Enforce a conservative 16 GB + DXR 1.1 headroom ceiling: <= 75% of the
     local OS budget when RT is off, and <= 75% of min(local_budget,
     VICTORY_DXR_VRAM_CAP_MB) minus unallocated DXR AS slab headroom
     (crate::dxr::slab_headroom()) when VICTORY_NATIVE_RT=1, charging
     64 KiB-aligned physical footprints against the 250 ms cached window
     (refreshing live on >= 4 MiB allocations).
   - Keep HostList::indirect_args and �atch_args (already in DEFAULT
     UAV VRAM with COMMON <-> UNORDERED_ACCESS -> INDIRECT_ARGUMENT ->
     COMMON state tracking in �ictory-rtx) warm for the first
     HOT_SPARE = 24 recycled lists (	rim_oversized_buffers) while
     releasing idle buffers beyond HOT_SPARE (
elease_idle_buffers),
     preserving VICTORY_BURY_ASIDE, Detached::Shared / dxr_reusable,
     and 
eset_closed().

2. Independent DXGI local/non-local memory sampling & HUD distinction
   (GPL-3.0 provenance: Belobug/VIctory-Belobug-AMD#348):
   - Split DXGI LOCAL and NON_LOCAL sampling in Host::sample_video_memory
     and Host::query_video_memory_groups so a single-segment failure does
     not zero out the other segment.
   - Update host/panel.rs to show local usage vs OS budget, adapter
     dedicated VRAM capacity vs non-local/shared spill (N/A on missing
     sample vs
aleksesipenko pushed a commit to aleksesipenko/VIctory-RTX that referenced this pull request Nov 27, 2026
…tion, LIFO hot-spare pool, bury-aside drops, resident GPU_UPLOAD, CPU counter step-back & multi-group sizing)

Address 1 P1 and 5 P2 findings from Terra review of d35d011:

1. [P1] Atomic pre-allocation VRAM reservation (`host.rs`):
   - Replace unsynchronized headroom check+charge with `UploadBudget` +
     `Mutex<UploadBudgetState>` and RAII `UploadBudgetGuard`.
   - Check the 75% / DXR ceiling (`min(local_budget, VICTORY_DXR_VRAM_CAP_MB)`
     minus `dxr::slab_headroom()`) and reserve 64 KiB-aligned `in_flight_bytes`
     atomically BEFORE calling `CreateCommittedResource` (releasing the lock
     before D3D12 / DXGI calls).
   - Carry unreflected committed bytes across concurrent DXGI refreshes within
     the 250 ms window (subtracting observed `dxgi_increase` and expiring after
     250 ms) while always adding `in_flight_bytes`, rolling back cleanly on
     `Drop` or `fail_and_invalidate()`.
   - Add 16-thread concurrent reservation + rollback unit test.

2. [P2] LIFO warm-first `HOT_SPARE = 24` ordering (`list.rs`):
   - Switch `IDLE` to `IdlePool<T>` backed by `VecDeque` tracking `hot_len`
     strictly under the pool lock: hot lists (up to `HOT_SPARE`) push to the
     back (popped first by `pop_back()`), while cold trimmed lists push to the
     front (`push_front()`).
   - Add unit test parking 30 lists and asserting all 24 hot lists pop before
     cold lists and hot slots refill at the back.

3. [P2] Route trimmed/idle COM buffers through `VICTORY_BURY_ASIDE` (`host.rs`, `list.rs`):
   - Return detached `Vec<ID3D12Resource>` from `HostList::trim_oversized_buffers()`
     and `HostList::release_idle_buffers()` without dropping COM objects under
     locks, and route them through `host::bury_resources()` (sharing the
     `victory-bury` worker thread when `VICTORY_BURY_ASIDE` is enabled).

4. [P2] Eliminate per-allocation GPU fence wait on `GPU_UPLOAD` (`host.rs`):
   - Create `D3D12_HEAP_TYPE_GPU_UPLOAD` buffers resident by default with
     `D3D12_HEAP_FLAG_NONE` and map directly without `CREATE_NOT_RESIDENT` /
     `MakeResident` / `mapped()` COPY-queue fence stalls.

5. [P2] Treat any backward-stepping CPU counter as `0.0%` idle (`host/panel.rs`):
   - Track per-core `CpuTimes { idle, kernel, user }` and return `0.0%` when
     any of `idle`, `kernel`, or `user` steps backward (or total delta <= 0)
     rather than producing a false `100%` spike when `idle` steps back.

6. [P2] Cover all Windows processor groups in `CpuMeter` (`host/panel.rs`):
   - Size initial buffer with `GetActiveProcessorCount(ALL_PROCESSOR_GROUPS)`
     and loop/grow on `STATUS_INFO_LENGTH_MISMATCH` up to `MAX_CPUS = 1024`,
     with unit test covering multi-group buffer growth and cap termination.

Provenance (GPL-3.0):
- Marvisak/VIctory#3228
- Belobug/VIctory-Belobug-AMD#348
- Belobug/VIctory-Belobug-AMD#325
aleksesipenko pushed a commit to aleksesipenko/VIctory-RTX that referenced this pull request Nov 27, 2026
…y across refreshes and failures

Address Terra Round-2 accounting edge cases in `UploadBudget` (`host.rs`):

- Replace `reserved_since_sample` + shared window timer with per-commit
  `recent_commits: VecDeque<(Instant, u64)>` + `recent_commit_bytes: u64`,
  pruning each commit only after its own 250 ms TTL so a commit at t=249 ms
  survives a DXGI refresh at t=251 ms.
- Never subtract unrelated `dxgi_increase` deltas from `recent_commit_bytes`:
  always compute `dxgi_used + in_flight_bytes + recent_commit_bytes`
  conservatively so texture/DXR VRAM growth cannot cancel unreflected
  `GPU_UPLOAD` commits.
- Keep `recent_commits` / `recent_commit_bytes` intact across both
  `fail_and_invalidate()` and DXGI query errors (which fail closed to
  system-RAM `UPLOAD`).
- Add deterministic CPU unit tests for all four cases: failure preserving
  prior commits, per-commit 250 ms TTL across sample refresh, unrelated DXGI
  growth non-cancellation, and DXGI error fail-closed preservation.

Provenance (GPL-3.0):
- Marvisak/VIctory#3228
aleksesipenko pushed a commit to aleksesipenko/VIctory-RTX that referenced this pull request Nov 27, 2026
Follow-up fix from Round-3 Terra review on UploadBudget (GPL-3.0, Marvisak/VIctory#3228):
- Change UploadBudgetState::prune_commits to compare commit timestamps against the
  cached DXGI sample's own sampled_at timestamp rather than now, preventing an older
  sample (e.g. t=249 ms) from dropping an unreflected commit at t=250 ms.
- In UploadBudget::reserve's fast path, if the request does not fit yet against the
  cached sample while the oldest unpruned commit has aged >= 250 ms by now, trigger
  a live DXGI refresh instead of waiting another 250 ms.
- Add deterministic test upload_budget_does_not_prune_commit_against_older_sample
  covering stale-sample rejection at t=250 ms, fail-closed DXGI error without pruning,
  and immediate pruning once a fresh t=250 ms sample reports freed VRAM.
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.

4 participants