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

Merged
raylitebull merged 1 commit from upstream-pr/45 into main 2026-11-22 20:53:41 +00:00
raylitebull commented 2026-11-22 20:35:28 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by raylitebull on 2026-11-22T20:35:28Z.


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.
<!-- greyfinch-sync:ivall/xcl:issue:5443119412 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/45). Originally posted by [raylitebull](https://github.com/raylitebull) on 2026-11-22T20:35:28Z. --- ## 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.
upstream-sync closed this pull request 2026-11-24 06:56:53 +00:00
ivall commented 2026-11-24 07:00:20 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-22T20:55:49Z.


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.
<!-- greyfinch-sync:ivall/xcl:comment:7085653821 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/45#issuecomment-7085653821). Originally posted by [ivall](https://github.com/ivall) on 2026-11-22T20:55:49Z. --- 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.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
xcl-dev/xcl!44
No description provided.