Skip to content

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

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
raylitebull:sparse-vram-perf-and-coherent-headroom
Nov 22, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
raylitebull:sparse-vram-perf-and-coherent-headroom

Conversation

@raylitebull

@raylitebull raylitebull commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

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 (Report: a crash reported during a report write is kept; Config: title_memory in any case (#106) #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.

…and coherent headroom

Follow-up to PR xcl-dev#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 (xcl-dev#148) calls into the command list Arena and route large ones to sparse VRAM instead of burning 64 KiB coherent grains per call.
@ivall
ivall merged commit e2f185b into xcl-dev:main Nov 22, 2026
@ivall

ivall commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

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.

grahamster2 added a commit to grahamster2/xcl that referenced this pull request Nov 26, 2026
…o memory first

A tile listed with no video memory (XCL_SPARSE_LAZY=1) got some at the CPU's
first bytes, a buffer placed on it, or a memory operation recorded on a list -
and the last only when memory was not short (materialize without force). On
tiled resources tier 2 a write to a tile with no memory is dropped: a shader's
store, a copy, a fence value the title then waits for. The Series X profile on
a 10 GB card hung (DEVICE_HUNG at 209 s, 38111 tiles listed with no memory).

Now every route xcl sees a GPU write take asks for the tiles (sparse::gpu_write,
taken by the next submit, before the list runs, forced like a placed buffer):
- memory operations on a list (immediates, Write32/64, fills, CopyMemoryToMemory,
  timestamps), as before but forced;
- buffer UAVs and their counters (CreateUnorderedAccessView, the view by
  address xcl-dev#71 with its counter at +0x30);
- root UAVs (SetCompute/GraphicsRootUnorderedAccessView, the console lists' xcl-dev#45),
  from the address as far as the tiles are committed (4 MiB at most);
- copies and query resolves into a console buffer (note_copy_into);
- dispatches and draws whose console shaders may store to memory
  (gpu::effects::may_write_memory; unknown: may): their root views and the 64-bit
  addresses among their root constants (bind_abi, two-pass stages, console lists).
A thread asks once for tiles it asked for lately, until tiles are listed again.
Bytes the CPU owed a tile (starved) go up at the submit that gives it memory.

The sparse windows' line counts "N tiles made resident for GPU writes (M found
none)"; the first of each route that met a tile with no memory is logged, and a
GPU write that found none is warned of.

Tests (sparse::tests, real GPU): a UAV on a lazy range makes it resident (a root
UAV too); a copy destination does; a read-only SRV does not, nor a root SRV of
shaders that store nothing; effects::may_write_memory tells stores from loads.
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.

2 participants