Sparse VRAM: async ring-buffered uploads, cached write-watch ranges, and coherent headroom #45

Manually merged
github merged 1 commit from gh-pr/45/head into gh-pr/45/base 2026-11-26 04:06:05 +00:00
Owner

@⁠raylitebull on GitHub · 2026-11-22 20:35 UTC · original
raylitebull:sparse-vram-perf-and-coherent-headroom → main · +1295 −317 · 9 files · 1 commits · merged 2026-11-22 20:53 UTC

Summary

Follow-up to #40 (rebased onto 5dd2d56, keeping bb9f0b5's COHERENT_KEPT handling for committed console textures untouched) addressing the two follow-up items noted after merge:

1. Sparse Window Upload & Write-Watch Overhead (sparse.rs, title_textures.rs, uma.rs, kernelx.rs, real.rs)

  • Asynchronous ring-buffered staging (d3d12x::sparse): replaces the single staging buffer + synchronous wait_idle() (plus per-wait CreateEventW/CloseHandle) on every submit with a 64 MiB ring staging buffer, 16 upload command allocators/lists, a cached reusable Win32 event, and a dedicated 1 MiB pre-zeroed tile-clear buffer (zero_buf). Uploads submit asynchronously without stalling the CPU or holding STATE across GPU completion; title queues continue to GPU-wait on s.fence. Readbacks use a dedicated rb_alloc/rb_list and still wait before CPU copy-back.
  • Fast per-tile dirty scanning: unifies mapped, holes, and sent into a per-tile TileEntry (pool tile + u16 hole bitmask + lazy per-page sent hashes) in an FxHashMap (1 lookup per 64 KiB tile instead of 33 SipHash lookups), replaces the serial chunk hash with a 4-way parallel hash + compile-time ZERO_HASHES, skips allocating hash arrays on all-zero pages, scans directly into a reusable scratch_runs buffer, and removes per-run COM AddRef/Release clones in upload loops.
  • Batched UpdateTileMappings: groups tile mappings by pool heap and merges contiguous resource/heap runs in map_tiles and unmap_tiles.
  • Cached write-watch ranges & zero-alloc submit scans (title_textures.rs, uma.rs, kernelx.rs): caches watch_ranges keyed by atomic generation counters (sparse::mapped_gen, TEXTURES_GEN, kernelx::graphics_reservations_gen), reuses thread-local GetWriteWatch and dirty-page buffers, exposes Uma::watch_regions() so contiguous sparse windows 0..2 are scanned as a single OS reservation, and skips title_textures::refresh on empty ExecuteCommandLists(0, null).

2. Coherent Window 3 Headroom (real.rs, bind.rs, console_list.rs)

  • Preserves bb9f0b5's COHERENT_KEPT (128 MiB) guard on place_console_texture.
  • Stops blanket-routing D3D12_HEAP_TYPE_CUSTOM + D3D12_CPU_PAGE_PROPERTY_WRITE_BACK heaps and non-COPY_DEST buffers to coherent Window 3 (on Xbox UMA this is general CPU-cached graphics memory); only small explicit D3D12_HEAP_TYPE_READBACK heaps/buffers (<= 64 MiB) and small non-UAV COPY_DEST CUSTOM + WRITE_BACK buffers (<= 16 MiB) prefer Window 3 while COHERENT_KEPT remains free. Larger or excess readback allocations spill cleanly to sparse VRAM and are tracked in SPARSE_READBACK so Map(0, NULL, ...) still reads back from VRAM automatically.
  • Packs small AllocateUploadMemory (#148) calls into the command list Arena and routes large ones to sparse VRAM instead of burning 64 KiB coherent grains per call (updating console_list::free_uma to free sparse blocks via sparse::free).
  • Updates bind::count logging to show both coherent used/total MB and sparse used MB.

Testing

Verified on Windows 11 with an AMD Radeon RX 9070 XT:

  • Added unit tests:
    • sparse::tests::scan_page_lazy_alloc_and_coalesce
    • sparse::tests::ring_planner_handles_wrap_and_large_chunks
    • real::readback_routing_tests::custom_write_back_only_stays_coherent_for_small_copy_dest_buffers
    • uma::tests::runtime_layout_places_sparse_in_0_to_2_and_committed_in_3 (watch_regions() assertion)
  • Added live GPU e2e tests in tests/testtitle/src/gpu.rs:
    • coherent_window_headroom: verifies general CUSTOM + WRITE_BACK buffers/heaps and oversized READBACK buffers (> 64 MiB) land in sparse VRAM while COPY_DEST buffers read back automatically on Map(0, NULL, ...).
    • sparse_async_ring_upload_stress: streams 80 MiB across 20 back-to-back pipelined submits (wrapping both the 64 MiB staging ring and 16 upload slots with zero CPU waits between submits) + reused-tile zero_buf clearing. Release run measured 228 MB uploaded across 32 submits in 83 ms upload / 13 ms chunk scan / 0 ms lock wait (~2.9 GB/s sustained across the 64 MiB steady-state slice).
  • .\scripts\test.ps1: 329 lib passed, 0 failed; all 46 xcl_testtitle e2e checks + disasm_e2e + rpf_e2e passed.
  • cargo build --release --workspace & cargo test --release --test e2e: clean build, all checks passed.
**@⁠raylitebull** on GitHub · 2026-11-22 20:35 UTC · [original](https://github.com/xcl-dev/xcl/pull/45) `raylitebull:sparse-vram-perf-and-coherent-headroom` → `main` · +1295 −317 · 9 files · 1 commits · merged 2026-11-22 20:53 UTC ## Summary Follow-up to #40 (rebased onto `5dd2d56`, keeping `bb9f0b5`'s `COHERENT_KEPT` handling for committed console textures untouched) addressing the two follow-up items noted after merge: ### 1. Sparse Window Upload & Write-Watch Overhead (`sparse.rs`, `title_textures.rs`, `uma.rs`, `kernelx.rs`, `real.rs`) - **Asynchronous ring-buffered staging (`d3d12x::sparse`)**: replaces the single staging buffer + synchronous `wait_idle()` (plus per-wait `CreateEventW`/`CloseHandle`) on every submit with a 64 MiB ring staging buffer, 16 upload command allocators/lists, a cached reusable Win32 event, and a dedicated 1 MiB pre-zeroed tile-clear buffer (`zero_buf`). Uploads submit asynchronously without stalling the CPU or holding `STATE` across GPU completion; title queues continue to GPU-wait on `s.fence`. Readbacks use a dedicated `rb_alloc`/`rb_list` and still wait before CPU copy-back. - **Fast per-tile dirty scanning**: unifies `mapped`, `holes`, and `sent` into a per-tile `TileEntry` (pool tile + `u16` hole bitmask + lazy per-page sent hashes) in an `FxHashMap` (1 lookup per 64 KiB tile instead of 33 SipHash lookups), replaces the serial chunk hash with a 4-way parallel hash + compile-time `ZERO_HASHES`, skips allocating hash arrays on all-zero pages, scans directly into a reusable `scratch_runs` buffer, and removes per-run COM `AddRef`/`Release` clones in upload loops. - **Batched `UpdateTileMappings`**: groups tile mappings by pool heap and merges contiguous resource/heap runs in `map_tiles` and `unmap_tiles`. - **Cached write-watch ranges & zero-alloc submit scans (`title_textures.rs`, `uma.rs`, `kernelx.rs`)**: caches `watch_ranges` keyed by atomic generation counters (`sparse::mapped_gen`, `TEXTURES_GEN`, `kernelx::graphics_reservations_gen`), reuses thread-local `GetWriteWatch` and dirty-page buffers, exposes `Uma::watch_regions()` so contiguous sparse windows 0..2 are scanned as a single OS reservation, and skips `title_textures::refresh` on empty `ExecuteCommandLists(0, null)`. ### 2. Coherent Window 3 Headroom (`real.rs`, `bind.rs`, `console_list.rs`) - Preserves `bb9f0b5`'s `COHERENT_KEPT` (128 MiB) guard on `place_console_texture`. - Stops blanket-routing `D3D12_HEAP_TYPE_CUSTOM` + `D3D12_CPU_PAGE_PROPERTY_WRITE_BACK` heaps and non-`COPY_DEST` buffers to coherent Window 3 (on Xbox UMA this is general CPU-cached graphics memory); only small explicit `D3D12_HEAP_TYPE_READBACK` heaps/buffers (`<= 64 MiB`) and small non-UAV `COPY_DEST` `CUSTOM + WRITE_BACK` buffers (`<= 16 MiB`) prefer Window 3 while `COHERENT_KEPT` remains free. Larger or excess readback allocations spill cleanly to sparse VRAM and are tracked in `SPARSE_READBACK` so `Map(0, NULL, ...)` still reads back from VRAM automatically. - Packs small `AllocateUploadMemory` (#148) calls into the command list `Arena` and routes large ones to sparse VRAM instead of burning 64 KiB coherent grains per call (updating `console_list::free_uma` to free sparse blocks via `sparse::free`). - Updates `bind::count` logging to show both `coherent used/total MB` and `sparse used MB`. ## Testing Verified on Windows 11 with an AMD Radeon RX 9070 XT: - Added unit tests: - `sparse::tests::scan_page_lazy_alloc_and_coalesce` - `sparse::tests::ring_planner_handles_wrap_and_large_chunks` - `real::readback_routing_tests::custom_write_back_only_stays_coherent_for_small_copy_dest_buffers` - `uma::tests::runtime_layout_places_sparse_in_0_to_2_and_committed_in_3` (`watch_regions()` assertion) - Added live GPU e2e tests in `tests/testtitle/src/gpu.rs`: - `coherent_window_headroom`: verifies general `CUSTOM + WRITE_BACK` buffers/heaps and oversized `READBACK` buffers (> 64 MiB) land in sparse VRAM while `COPY_DEST` buffers read back automatically on `Map(0, NULL, ...)`. - `sparse_async_ring_upload_stress`: streams 80 MiB across 20 back-to-back pipelined submits (wrapping both the 64 MiB staging ring and 16 upload slots with zero CPU waits between submits) + reused-tile `zero_buf` clearing. Release run measured **228 MB uploaded across 32 submits in 83 ms upload / 13 ms chunk scan / 0 ms lock wait** (~2.9 GB/s sustained across the 64 MiB steady-state slice). - `.\scripts\test.ps1`: **329 lib passed, 0 failed**; all **46 `xcl_testtitle` e2e checks** + `disasm_e2e` + `rpf_e2e` passed. - `cargo build --release --workspace` & `cargo test --release --test e2e`: clean build, all checks passed.
Follow-up to PR #40 addressing the two remaining issues noted after merge:

1. Sparse window upload & write-watch overhead (XDefiant ~10 GB in 55 s):
   - Replace per-submit synchronous staging + wait_idle() in d3d12x::sparse with an asynchronous 64 MiB ring staging buffer, 16 upload command allocators/lists, a reusable Win32 fence event, and a dedicated 1 MiB pre-zeroed tile-clear buffer (zero CPU stall on uploads; title queues GPU-wait on the upload fence as before).
   - Unify mapped tile tracking into per-tile TileEntry (pool tile + u16 hole bitmask + lazy per-page sent hashes) backed by FxHashMap, replace the serial chunk hash with a 4-way parallel hash + compile-time ZERO_HASHES, and scan directly into a reusable dirty-run scratch buffer without per-page Vec/Box allocations on zero pages.
   - Batch UpdateTileMappings by pool heap with contiguous run merging in map_tiles/unmap_tiles, and eliminate per-run COM AddRef/Release clones in upload loops.
   - Cache watch_ranges in title_textures keyed by atomic generation counters (sparse mapped tiles, textures, graphics reservations), reuse thread-local GetWriteWatch/dirty buffers, merge contiguous sparse windows in Uma::watch_regions(), and skip refresh on empty ExecuteCommandLists(0, null).

2. Coherent Window 3 headroom (complementing bb9f0b5's COHERENT_KEPT for textures):
   - Stop blanket-routing D3D12_HEAP_TYPE_CUSTOM + D3D12_CPU_PAGE_PROPERTY_WRITE_BACK heaps and non-COPY_DEST buffers to coherent Window 3 (on Xbox UMA, CUSTOM+WRITE_BACK is general CPU-cached graphics memory); route them to sparse VRAM and spill oversized/excess readback allocations to sparse VRAM once coherent headroom reaches COHERENT_KEPT, while tracking SPARSE_READBACK so Map(0, NULL, ...) still syncs GPU writes back automatically.
   - Pack small AllocateUploadMemory (#148) calls into the command list Arena and route large ones to sparse VRAM instead of burning 64 KiB coherent grains per call.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:06:04 +00:00
github manually merged commit afaf413f25 into gh-pr/45/base 2026-11-26 04:06:05 +00:00
Author
Owner

@⁠ivall on GitHub · 2026-11-22 20:55 UTC · original

Thanks, merged! I tested it on top of current main (which also has 4de9016 and the launcher menu). It merged cleanly and all tests pass, e2e and your new ones included.

Title With #45 main before
XDefiant (menu) 60 fps (the game's cap); sparse uploads 1.9 s per ~47 s instead of 15.6 s ~53 fps
Unicorn Overlord fine, 58–60 fps, coherent 384/512 MB same
Minecraft Preview fine, 45–57 fps similar
Fortnite (into the menu) fine, frame rate as on main (40.7 vs 37.0 fps; 9.1 vs 8.7 while loading) –

The picture was checked on screen in all four, and the logs have no errors or skipped draws. The write-watch scan still costs about the same CPU time in XDefiant (~31 s summed over ~47 s), but it no longer holds frames back.

Two things to keep in mind (neither showed up in these titles):

  • GPU writes to CUSTOM + WRITE_BACK buffers in the sparse windows. Most of these buffers now live in the sparse windows. The CPU sees what the GPU wrote there only through Map (a read range, or Map(0, NULL) for the small COPY_DEST ones). A title that maps a buffer once and then reads GPU results from it every frame (UAV-written counters, readback buffers over 64 MiB) gets stale data. On the console that memory is coherent.
  • The 5 s ring wait. wait_for waits at most 5 s (WaitForSingleObject(self.event, 5000)) before a ring range is reused. After a longer GPU stall the staging memory would be overwritten while it is still being copied; an infinite wait, or skipping reuse when the fence hasn't passed, would be safer.
**@⁠ivall** on GitHub · 2026-11-22 20:55 UTC · [original](https://github.com/xcl-dev/xcl/pull/45#issuecomment-7085653821) Thanks, merged! I tested it on top of current `main` (which also has 4de9016 and the launcher menu). It merged cleanly and all tests pass, e2e and your new ones included. | Title | With #45 | `main` before | |---|---|---| | XDefiant (menu) | **60 fps** (the game's cap); sparse uploads 1.9 s per ~47 s instead of 15.6 s | ~53 fps | | Unicorn Overlord | fine, 58–60 fps, coherent 384/512 MB | same | | Minecraft Preview | fine, 45–57 fps | similar | | Fortnite (into the menu) | fine, frame rate as on `main` (40.7 vs 37.0 fps; 9.1 vs 8.7 while loading) | – | The picture was checked on screen in all four, and the logs have no errors or skipped draws. The write-watch scan still costs about the same CPU time in XDefiant (~31 s summed over ~47 s), but it no longer holds frames back. Two things to keep in mind (neither showed up in these titles): - **GPU writes to `CUSTOM + WRITE_BACK` buffers in the sparse windows.** Most of these buffers now live in the sparse windows. The CPU sees what the GPU wrote there only through `Map` (a read range, or `Map(0, NULL)` for the small `COPY_DEST` ones). A title that maps a buffer once and then reads GPU results from it every frame (UAV-written counters, readback buffers over 64 MiB) gets stale data. On the console that memory is coherent. - **The 5 s ring wait.** `wait_for` waits at most 5 s (`WaitForSingleObject(self.event, 5000)`) before a ring range is reused. After a longer GPU stall the staging memory would be overwritten while it is still being copied; an infinite wait, or skipping reuse when the fence hasn't passed, would be safer.
This discussion has been locked. Commenting is limited to contributors.
No description provided.