GPU: back shadow buffers, page tables, and dirty bitmaps in VRAM on discrete GPUs #3312

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

@⁠danielibabet on GitHub · 2026-11-26 17:03 UTC · original
danielibabet:pr/vram-default-buffers → main · +633 −49 · 3 files · 1 commits · open

Summary

Addresses part of #3028 on discrete GPUs where GPUUploadHeapSupported is false (e.g., NVIDIA RTX 4070 / Ada Lovelace without ReBAR enabled).

Root Cause

On discrete GPUs without GPUUploadHeapSupported, Host::shared_buffer (D3D12_HEAP_TYPE_CUSTOM, WRITE_BACK, MEMORY_POOL_L0) and Host::upload_buffer (MEMORY_POOL_L0) place all shadow buffers, title-memory page tables, and atomic dirty-page bitmaps in system RAM over PCIe. Every shader buffer load, UAV store, page-table lookup, and InterlockedOr atomic crosses the PCIe bus instead of hitting local GDDR6X / L2.

Changes

  • host.rs:
    • Added Host::is_uma() (D3D12_FEATURE_ARCHITECTURE) and Host::vram_shadow_budget() (25% of local DXGI_MEMORY_SEGMENT_GROUP_LOCAL budget on discrete GPUs so streaming textures are not evicted).
    • Added Host::default_uav_buffer(), reusable 64 MiB UPLOAD ring (upload_vram_copies), and reusable 32 MiB READBACK ring (readback_vram_copies) synchronized via run_copy_list.
  • memory.rs:
    • Backed Shared::table (title-memory page table) and Bitmap (title-memory dirty-page bitmap) with default_uav_buffer on discrete GPUs, synced pre/post submission via CopyBufferRegion.
  • shadow.rs:
    • Backed shadow::bitmap (VIEW_WRITES / VIEW_READS) with default_uav_buffer on discrete GPUs.
    • Backed Shadow allocations (find, loose, Arena) up to vram_shadow_budget() (<= 128 MiB per allocation) in D3D12_HEAP_TYPE_DEFAULT VRAM with demand-committed CpuMem (alloc_zeroed) instead of duplicating L0 D3D12 resources, falling back cleanly to shared_buffer once the VRAM budget is reached or on UMA/WARP adapters.

Measured Results (NVIDIA RTX 4070 12 GB, Windows 11, i9-14900KF)

  • GPU command timing (VICTORY_GPU_TIMING=1) with VRAM-backed shadow buffers + page tables/bitmaps:
    • Total GPU command time: 297.6 ms/frame -> 90.5–91.6 ms/frame (3.25x faster)
    • Top compute shaders in world scenes dropped from 75.81 ms / 30.22 ms / 19.29 ms / 14.26 ms down to < 0.33–1 ms.
  • Pure release runs with bounded 2.5 GB VRAM budget (no texture eviction):
    • on the GPU (5.1k–5.3k draws/frame): ~405–417 ms -> ~285–323 ms (total frame time ~545 ms -> ~440 ms).

Verification

  • cargo test -p victory-gpu --locked -- --test-threads=1 --skip dynamic_slots_are_held_until_released --skip lists_share_a_table_and_copies_keep_their_views: 229 passed, 0 failed (the 2 skipped tests require HEAP_VIEWS <= 1_000_000 or WARP on this NVIDIA driver; tested separately on WARP in PR #3218).
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warnings.
  • cargo run -p xtask --locked -- policy: asset policy: 191 files ok.
**@⁠danielibabet** on GitHub · 2026-11-26 17:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/3312) `danielibabet:pr/vram-default-buffers` → `main` · +633 −49 · 3 files · 1 commits · open ## Summary Addresses part of #3028 on discrete GPUs where `GPUUploadHeapSupported` is `false` (e.g., NVIDIA RTX 4070 / Ada Lovelace without ReBAR enabled). ### Root Cause On discrete GPUs without `GPUUploadHeapSupported`, `Host::shared_buffer` (`D3D12_HEAP_TYPE_CUSTOM`, `WRITE_BACK`, `MEMORY_POOL_L0`) and `Host::upload_buffer` (`MEMORY_POOL_L0`) place all shadow buffers, title-memory page tables, and atomic dirty-page bitmaps in system RAM over PCIe. Every shader buffer load, UAV store, page-table lookup, and `InterlockedOr` atomic crosses the PCIe bus instead of hitting local GDDR6X / L2. ### Changes - **`host.rs`**: - Added `Host::is_uma()` (`D3D12_FEATURE_ARCHITECTURE`) and `Host::vram_shadow_budget()` (25% of local `DXGI_MEMORY_SEGMENT_GROUP_LOCAL` budget on discrete GPUs so streaming textures are not evicted). - Added `Host::default_uav_buffer()`, reusable 64 MiB `UPLOAD` ring (`upload_vram_copies`), and reusable 32 MiB `READBACK` ring (`readback_vram_copies`) synchronized via `run_copy_list`. - **`memory.rs`**: - Backed `Shared::table` (title-memory page table) and `Bitmap` (title-memory dirty-page bitmap) with `default_uav_buffer` on discrete GPUs, synced pre/post submission via `CopyBufferRegion`. - **`shadow.rs`**: - Backed `shadow::bitmap` (`VIEW_WRITES` / `VIEW_READS`) with `default_uav_buffer` on discrete GPUs. - Backed `Shadow` allocations (`find`, `loose`, `Arena`) up to `vram_shadow_budget()` (`<= 128 MiB` per allocation) in `D3D12_HEAP_TYPE_DEFAULT` VRAM with demand-committed `CpuMem` (`alloc_zeroed`) instead of duplicating `L0` D3D12 resources, falling back cleanly to `shared_buffer` once the VRAM budget is reached or on UMA/WARP adapters. ## Measured Results (NVIDIA RTX 4070 12 GB, Windows 11, i9-14900KF) - **GPU command timing (`VICTORY_GPU_TIMING=1`)** with VRAM-backed shadow buffers + page tables/bitmaps: - Total GPU command time: **297.6 ms/frame -> 90.5–91.6 ms/frame (3.25x faster)** - Top compute shaders in world scenes dropped from **75.81 ms / 30.22 ms / 19.29 ms / 14.26 ms** down to **< 0.33–1 ms**. - **Pure release runs with bounded 2.5 GB VRAM budget** (no texture eviction): - `on the GPU` (5.1k–5.3k draws/frame): **~405–417 ms -> ~285–323 ms** (total frame time **~545 ms -> ~440 ms**). ## Verification - `cargo test -p victory-gpu --locked -- --test-threads=1 --skip dynamic_slots_are_held_until_released --skip lists_share_a_table_and_copies_keep_their_views`: **229 passed, 0 failed** (the 2 skipped tests require `HEAP_VIEWS <= 1_000_000` or WARP on this NVIDIA driver; tested separately on WARP in PR #3218). - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: **0 warnings**. - `cargo run -p xtask --locked -- policy`: **asset policy: 191 files ok**.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 17:03:25 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 17:07 UTC · original · approved these changes

Reviewed the diff and verified locally on Windows 11 x64 with a discrete NVIDIA RTX 5090 (is_uma() == false, exercising the VRAM-backed shadow buffers, page tables, and dirty bitmaps path):

  • cargo test -p victory-gpu --lib shadow:: -- --test-threads=1 (RTX 5090): 10 passed, 0 failed.
  • cargo test -p victory-gpu --lib memory::tests:: -- --test-threads=1 --skip lists_share_a_table_and_copies_keep_their_views (RTX 5090): 7 passed, 0 failed (pre-existing #1744 test skipped).
  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1 (RTX 5090): 20 passed, 0 failed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo clippy -p victory-gpu --features diagnostics --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 17:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/3312#pullrequestreview-6078068308) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64 with a discrete NVIDIA RTX 5090 (`is_uma() == false`, exercising the VRAM-backed shadow buffers, page tables, and dirty bitmaps path): - `cargo test -p victory-gpu --lib shadow:: -- --test-threads=1` (RTX 5090): 10 passed, 0 failed. - `cargo test -p victory-gpu --lib memory::tests:: -- --test-threads=1 --skip lists_share_a_table_and_copies_keep_their_views` (RTX 5090): 7 passed, 0 failed (pre-existing #1744 test skipped). - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1` (RTX 5090): 20 passed, 0 failed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo clippy -p victory-gpu --features diagnostics --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 20:58 UTC · original · requested changes

Moving hot shadow buffers, page tables, and dirty bitmaps into DEFAULT VRAM on discrete GPUs (!is_uma()) while keeping UMA/APU/WARP on shared memory is a great direction, and the clippy / policy checks and WARP (UMA=true) suite pass cleanly.

Testing on a discrete GPU (NVIDIA GeForce RTX 5090, UMA=false, vram_shadow_budget=8,236,302,336 bytes, VICTORY_D3D12_DEBUG=1) versus WARP (UMA=true) and probing an integrated GPU (AMD Radeon(TM) Graphics, UMA=true) uncovered four blocking correctness/synchronization bugs on the discrete VRAM path:

1. Silent data loss for marked-page UAV writes (VIEW_WRITES) into VRAM shadows (crates/victory-gpu/src/host.rs:1315-1339)

Host::execute only schedules VRAM -> CpuMem readbacks for explicit Step::GpuWrite entries before running shadow::copy_back_marks. Any shader write through a heap/bindless/marked UAV that sets a bit in VIEW_WRITES without a matching Step::GpuWrite leaves Shadow::data (CpuMem) holding the old pre-GPU bytes; Shadow::copy_back_marked then compares stale CpuMem against title memory and writes back zero bytes.

  • Reproduced live: dispatching a compute shader writing 0x1337c0de into a VRAM shadow and setting its view_writes(false) bit:
    • RTX 5090 (is_uma=false): gpu_word=0x1337c0de, title_word=0x00000000 (write lost).
    • WARP (is_uma=true): gpu_word=0x1337c0de, title_word=0x1337c0de.

2. Silent data loss for title-memory shader writes via the page table (crates/victory-gpu/src/host.rs:1315-1339, crates/victory-gpu/src/memory.rs:689-691)

Similarly, shaders writing through the title-memory page table mark pages in map.dirty (no Step::GpuWrite), and memory::bind's defer_after closure calls copies[k].copy_back_page(at). Because those dirty pages are never copied back from VRAM into copies[k].data (CpuMem) after sync_vram_post reads back the dirty bitmap, copy_back_page sees unchanged CpuMem and writes nothing to title memory. (Existing test a_submission_that_missed_leaves_the_copies_at_once did not catch this because its non-missed branch only checked GPU-side read(), not CPU title().)

  • Reproduced live using the memory::tests::WRITE shader storing 0xcafef00d through the page table:
    • RTX 5090 (is_uma=false): gpu_word=0xcafef00d, title_word=0x00000000 (write lost).
    • WARP (is_uma=true): gpu_word=0xcafef00d, title_word=0xcafef00d.

3. Global ACTIVE_BITMAPS.drain(..) drops dirty/missed bitmaps across separate submissions (crates/victory-gpu/src/memory.rs:205, 239-254, 695-703)

memory::bind pushes (vram, ram) into the process-global ACTIVE_BITMAPS at record time, and sync_vram_post drains the entire vector on whichever Host::execute runs next:

  • If lists L1 and L2 are both recorded with memory::bind and submitted separately (host.execute(&[&L1]) then host.execute(&[&L2])), submitting L1 drains L2's bitmap too. When L2 later executes, its VRAM dirty bitmap is never zeroed before L2 and never copied back to RAM after L2, silently dropping both L2's missed-region withholding and its page writebacks.
  • Conversely, if a recorded list is reset/dropped without being executed, ACTIVE_BITMAPS still holds clones of a bitmap already returned to BITMAP_POOL.
  • Reproduced live: recording L1 and L2 (L2 setting region-1 missed bit in map2.dirty), then executing [&L1] followed by [&L2]:
    • RTX 5090 (is_uma=false): missed_regions_delta=0 (L2's miss completely lost).
    • WARP (is_uma=true): missed_regions_delta=1.
  • Fix: associate the (vram, ram) bitmap pair with the specific HostList (Deferred) rather than a global vec, and only zero/read back the bitmaps belonging to runs.

4. ID3D12CommandAllocator::Reset called while previous copy list is still executing (crates/victory-gpu/src/host.rs:2861, 2959-2970, 2983-3017)

upload_vram_copies submits its final flush(..., wait = false) on static CMD's single (alloc, list) without signaling/waiting on a fence, and run_copy_list unconditionally calls let _ = alloc.Reset(); on the next call (also discarding Reset/Close HRESULTs). This immediately violates D3D12 allocator lifetime rules whenever another copy runs before the previous GPU submission finishes (e.g., Shadow::read_host/write_host calling flush_test_vram_uploads() immediately followed by test_readback_buffer/test_upload_buffer, or Host::execute(&[])).

  • Reproduced live on RTX 5090 with VICTORY_D3D12_DEBUG=1: calling sh.copy(...) + sh.read_host(...) produced 16/16 D3D12 Debug Layer errors:
    ID3D12CommandAllocator::Reset: A command allocator ... is being reset before previous executions associated with the allocator have completed. (0/16 on WARP).
  • Relatedly, in flush_test_vram_uploads (host.rs:2963-2967), falling back to let mut dummy = 0u64; when self.submitted.try_lock() fails can signal the monotonic self.fence to 1 while the fence is already past 1, causing SetEventOnCompletion(1, ...) to return immediately without waiting.

Additional notes

  • Unconditional eager bitmap allocation & extra fence wait per submission (crates/victory-gpu/src/shadow.rs:970-975, host.rs:2798, 2867): has_vram_bitmaps() calls bitmap(false) and bitmap(true) on every Host::execute, eagerly allocating both bitmaps on the first submission and forcing both upload_vram_copies and a second synchronous readback_vram_copies + fence wait on every single submission on discrete GPUs even when no shader uses view write marks or VRAM shadows. Consider checking whether either OnceLock is actually initialized (and whether the submission used view marks/reads) instead.
  • Budget accounting race & leak (crates/victory-gpu/src/shadow.rs:133-157): try_vram_shadow checks VRAM_BYTES.load(Relaxed) + size > budget before fetch_add (non-atomic TOCTOU across threads), and if CpuMem::new(size) fails at line 154, VRAM_BYTES is never decremented. Also note Shadow::peek (under diagnostics) still reads self.data without a VRAM readback.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 20:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/3312#pullrequestreview-6080415208) · **requested changes** Moving hot shadow buffers, page tables, and dirty bitmaps into `DEFAULT` VRAM on discrete GPUs (`!is_uma()`) while keeping UMA/APU/WARP on shared memory is a great direction, and the `clippy` / `policy` checks and WARP (`UMA=true`) suite pass cleanly. Testing on a discrete GPU (`NVIDIA GeForce RTX 5090`, `UMA=false`, `vram_shadow_budget=8,236,302,336` bytes, `VICTORY_D3D12_DEBUG=1`) versus WARP (`UMA=true`) and probing an integrated GPU (`AMD Radeon(TM) Graphics`, `UMA=true`) uncovered **four blocking correctness/synchronization bugs** on the discrete VRAM path: ### 1. Silent data loss for marked-page UAV writes (`VIEW_WRITES`) into VRAM shadows (`crates/victory-gpu/src/host.rs:1315-1339`) `Host::execute` only schedules VRAM -> `CpuMem` readbacks for explicit `Step::GpuWrite` entries before running `shadow::copy_back_marks`. Any shader write through a heap/bindless/marked UAV that sets a bit in `VIEW_WRITES` without a matching `Step::GpuWrite` leaves `Shadow::data` (`CpuMem`) holding the old pre-GPU bytes; `Shadow::copy_back_marked` then compares stale `CpuMem` against title memory and writes back zero bytes. - **Reproduced live**: dispatching a compute shader writing `0x1337c0de` into a VRAM shadow and setting its `view_writes(false)` bit: - RTX 5090 (`is_uma=false`): `gpu_word=0x1337c0de`, **`title_word=0x00000000`** (write lost). - WARP (`is_uma=true`): `gpu_word=0x1337c0de`, `title_word=0x1337c0de`. ### 2. Silent data loss for title-memory shader writes via the page table (`crates/victory-gpu/src/host.rs:1315-1339`, `crates/victory-gpu/src/memory.rs:689-691`) Similarly, shaders writing through the title-memory page table mark pages in `map.dirty` (no `Step::GpuWrite`), and `memory::bind`'s `defer_after` closure calls `copies[k].copy_back_page(at)`. Because those dirty pages are never copied back from VRAM into `copies[k].data` (`CpuMem`) after `sync_vram_post` reads back the dirty bitmap, `copy_back_page` sees unchanged `CpuMem` and writes nothing to title memory. (Existing test `a_submission_that_missed_leaves_the_copies_at_once` did not catch this because its non-missed branch only checked GPU-side `read()`, not CPU `title()`.) - **Reproduced live** using the `memory::tests::WRITE` shader storing `0xcafef00d` through the page table: - RTX 5090 (`is_uma=false`): `gpu_word=0xcafef00d`, **`title_word=0x00000000`** (write lost). - WARP (`is_uma=true`): `gpu_word=0xcafef00d`, `title_word=0xcafef00d`. ### 3. Global `ACTIVE_BITMAPS.drain(..)` drops dirty/missed bitmaps across separate submissions (`crates/victory-gpu/src/memory.rs:205, 239-254, 695-703`) `memory::bind` pushes `(vram, ram)` into the process-global `ACTIVE_BITMAPS` at **record** time, and `sync_vram_post` drains the entire vector on whichever `Host::execute` runs next: - If lists `L1` and `L2` are both recorded with `memory::bind` and submitted separately (`host.execute(&[&L1])` then `host.execute(&[&L2])`), submitting `L1` drains `L2`'s bitmap too. When `L2` later executes, its VRAM dirty bitmap is never zeroed before `L2` and never copied back to RAM after `L2`, silently dropping both `L2`'s missed-region withholding and its page writebacks. - Conversely, if a recorded list is reset/dropped without being executed, `ACTIVE_BITMAPS` still holds clones of a bitmap already returned to `BITMAP_POOL`. - **Reproduced live**: recording `L1` and `L2` (`L2` setting region-1 missed bit in `map2.dirty`), then executing `[&L1]` followed by `[&L2]`: - RTX 5090 (`is_uma=false`): **`missed_regions_delta=0`** (`L2`'s miss completely lost). - WARP (`is_uma=true`): `missed_regions_delta=1`. - Fix: associate the `(vram, ram)` bitmap pair with the specific `HostList` (`Deferred`) rather than a global vec, and only zero/read back the bitmaps belonging to `runs`. ### 4. `ID3D12CommandAllocator::Reset` called while previous copy list is still executing (`crates/victory-gpu/src/host.rs:2861, 2959-2970, 2983-3017`) `upload_vram_copies` submits its final `flush(..., wait = false)` on `static CMD`'s single `(alloc, list)` without signaling/waiting on a fence, and `run_copy_list` unconditionally calls `let _ = alloc.Reset();` on the next call (also discarding `Reset`/`Close` HRESULTs). This immediately violates D3D12 allocator lifetime rules whenever another copy runs before the previous GPU submission finishes (e.g., `Shadow::read_host`/`write_host` calling `flush_test_vram_uploads()` immediately followed by `test_readback_buffer`/`test_upload_buffer`, or `Host::execute(&[])`). - **Reproduced live on RTX 5090 with `VICTORY_D3D12_DEBUG=1`**: calling `sh.copy(...)` + `sh.read_host(...)` produced **16/16** D3D12 Debug Layer errors: `ID3D12CommandAllocator::Reset: A command allocator ... is being reset before previous executions associated with the allocator have completed.` (0/16 on WARP). - Relatedly, in `flush_test_vram_uploads` (`host.rs:2963-2967`), falling back to `let mut dummy = 0u64;` when `self.submitted.try_lock()` fails can signal the monotonic `self.fence` to `1` while the fence is already past `1`, causing `SetEventOnCompletion(1, ...)` to return immediately without waiting. ### Additional notes - **Unconditional eager bitmap allocation & extra fence wait per submission (`crates/victory-gpu/src/shadow.rs:970-975`, `host.rs:2798, 2867`)**: `has_vram_bitmaps()` calls `bitmap(false)` and `bitmap(true)` on every `Host::execute`, eagerly allocating both bitmaps on the first submission and forcing both `upload_vram_copies` and a second synchronous `readback_vram_copies` + fence wait on every single submission on discrete GPUs even when no shader uses view write marks or VRAM shadows. Consider checking whether either `OnceLock` is actually initialized (and whether the submission used view marks/reads) instead. - **Budget accounting race & leak (`crates/victory-gpu/src/shadow.rs:133-157`)**: `try_vram_shadow` checks `VRAM_BYTES.load(Relaxed) + size > budget` before `fetch_add` (non-atomic TOCTOU across threads), and if `CpuMem::new(size)` fails at line 154, `VRAM_BYTES` is never decremented. Also note `Shadow::peek` (under `diagnostics`) still reads `self.data` without a VRAM readback.
This pull request can be merged automatically.
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/3312/head:github-bot/gh-3312'
git switch 'github-bot/gh-3312'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3312'
git switch 'github-bot/gh-3312'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3312'
git switch 'github-bot/gh-3312'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3312'
git switch 'main'
git merge --squash 'github-bot/gh-3312'
git switch 'main'
git merge --ff-only 'github-bot/gh-3312'
git switch 'main'
git merge 'github-bot/gh-3312'
git push origin 'main'
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/Marvisak--VIctory!3312
No description provided.