Skip to content

Route D3D12 buffer heaps, XMem graphics memory, and descriptor heaps to sparse VRAM - #40

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
raylitebull:sparse-vram-pools
Nov 22, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
raylitebull:sparse-vram-pools

Conversation

@raylitebull

Copy link
Copy Markdown
Member

Summary

Extends the sparse-window VRAM path (d3d12x::sparse + gpu::uma) to the three remaining system-RAM pools — D3D12 buffer heaps/buffers, XMem graphics memory, and console descriptor heaps — while preserving CPU Map() pointer coherence, 32-bit descriptor table addressing, GPU→CPU readback, and Arena pool headroom.

Runtime UMA Layout (crates/xcl/src/gpu/uma.rs)

  • Adds Uma::for_runtime (used by uma::get()):
    • Windows 0–2 (12 GiB VA): write-watched sparse VRAM windows (CreateReservedResource tiled onto 64 MiB D3D12_HEAP_TYPE_DEFAULT pools). Window 0 reserves 512 MiB of top headroom (DESC_HEADROOM) for 32-bit descriptor heaps; allocations <= 4 GiB never straddle a 4 GiB window boundary, and sparse_reserve prefers Windows 1–2.
    • Window 3 (capped at 512 MiB): committed coherent system RAM (OpenExistingHeapFromAddress), preserving the 64 MiB dedicated Arena pool (
      eserve_pool) for per-draw io/
      es/64-bit s[0:1] argument tables as well as READBACK / CUSTOM(WRITE_BACK) heaps and buffers.
  • Keeps Uma::new and Uma::with_windows unchanged for existing unit/GPU tests.

Sparse VRAM Pool & Readback (crates/xcl/src/hle/d3d12x/sparse.rs, itle_textures.rs)

  • Updates sparse::init to initialize every sparse window by index (k, raw, gpu_va).
  • Adds sparse::alloc, sparse::alloc_in(window, ...), and sparse::free.
  • Adds GPU→CPU readback (
    eadback_after_execute,
    eadback_now) backed by a lazy D3D12_HEAP_TYPE_READBACK staging buffer +
    b_fence, refreshing 64-byte sent hashes so readback copies do not trigger redundant re-uploads.
  • Fixes a WAW hazard in �efore_execute by flushing o_zero tile clears before uploading CPU-dirty runs, clamps upload/
    eadback_ranges at window boundaries, sanitizes reserved buffer flags in place_buffer, and wraps internal sparse CreateHeap/ExecuteCommandLists calls in itle_textures::own_work.

Phase 1 — D3D12 Buffer Heaps (crates/xcl/src/hle/d3d12x/real.rs)

  • Indexes UMA_HEAPS (Vec<Option>) and UMA_BUFFER by shader window k.
  • Routes non-readback CreateHeap / CreateHeap1 buffer heaps (HEAP_UMA) and non-readback committed console buffers (place_console_buffer) onto sparse::alloc + sparse::place_buffer, keeping D3D12_HEAP_TYPE_READBACK and CUSTOM(WRITE_BACK) on Window 3 coherent RAM.
  • Preserves persistent Map() pointer coherence (
    es_map returns the write-watched console address and calls sparse::readback_now for non-empty read ranges) and releases sparse-owned resources via sparse::free.

Phase 2 — XMem Graphics Memory (crates/xcl/src/hle/kernelx.rs, list_x.rs,

eal.rs)

  • Migrates XMemAllocDefault/XMemFreeDefault, VirtualAlloc(MEM_GRAPHICS) / XMemVirtualAlloc (commit and reserve), and AllocateTitlePhysicalPages / FreeTitlePhysicalPages (fixing page_home multi-window bounds) onto the sparse VRAM pool, honoring XALLOC_PAGESIZE_4MB (page_size == 2) alignment/rounding.
  • Tracks sparse-window command-list writes (Write32/64BitValue*, timestamps, FillMemoryWith*BitValueX, CopyMemoryToMemoryX) behind a fast-path atomic guard (SPARSE_WRITE_LISTS) and runs sparse::readback_after_execute in q_execute_command_lists and kickoff_x.

Phase 3 — Descriptor Heaps (crates/xcl/src/hle/d3d12x/console_heap.rs)

  • Moves console descriptor heap backing storage into Window 0 sparse VRAM via sparse::alloc_in(0, ...) (skipping write_bytes on freshly committed zero pages to avoid write-watch pollution), preserving 32-bit hi = 0 descriptor table addressing (u2), T#/V#/S# translation, and command-list replay offsets.

Testing

Verified on Windows 11 with an AMD Radeon RX 9070 XT (D3D12 SM 6.6 + DXC):

  • Added gpu_sparse_vram_descriptors_buffers_and_xmem (crates/xcl/src/gpu/recomp/tests.rs): live DXC SM 6.6 translated RDNA2 compute shader reading a 64-bit s[0:1] root constant from Window 3, loading V# descriptors via a 32-bit hi = 0 table pointer from Window 0 sparse VRAM, reading sparse XMem buffers, and verifying live descriptor + buffer mutations across submits.
  • Added sparse_buffer_heap_coherence ( ests/testtitle/src/gpu.rs): placed D3D12 buffer heap aliasing (including sub-64 KiB offsets), persistent Map() coherence across submits, and explicit Map(Some(range)) readback; also added a 4 MiB XMemAlloc check and fixed an existing byte-vs-u32 offset bug (*p.add(0x1_0000 / 4)) in ests/testtitle/src/main.rs exposed by exact sparse commits.
  • Added
    untime_layout_places_sparse_in_0_to_2_and_committed_in_3 (crates/xcl/src/gpu/uma.rs).
  • .\scripts\test.ps1: 325 passed, 0 failed (lib unit + live GPU tests, e2e with all 44 xcl_testtitle checks, disasm_e2e,
    pf_e2e).
  • cargo build --release --workspace: clean build, 0 errors.

@ivall
ivall merged commit bb43b5f into xcl-dev:main Nov 22, 2026
@raylitebull
raylitebull deleted the sparse-vram-pools branch November 22, 2026 20:02
@ivall

ivall commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Thanks, merged! I tested it on retail titles before merging (RX 9070): Minecraft, Unicorn Overlord and Fortnite were fine straight away, but XDefiant rendered nothing but black frames, while main without the PR showed its menu.

Cause: XDefiant's very first allocation is a graphics reservation of 0x200410000 bytes (8 GiB + 4 MiB). That is more than windows 1..2 hold, so sparse_reserve fell back to the start of window 0 and the reservation took all of it, DESC_HEADROOM included (the headroom only kept XCL's own sparse_alloc out, not title reservations). 0.8 s later every console descriptor heap failed:

console descriptor heap: no room in UMA for 131072 slots
console descriptor heap: no room in UMA for 442368 slots
console descriptor heap: no room in UMA for 256 slots
console descriptor heap: no room in UMA for 512 slots

The game kept drawing (no skipped draws, nothing else in the log), just without descriptors.

Fix on top of the merge in acbbc55:

  • The descriptor range is now the 512 MB right after window 0's first 64 KiB (Uma::desc_range). Those first 64 KiB stay unused, so no descriptor table sits at console address 0.
  • sparse_reserve, sparse_alloc and the fallbacks never hand that range to the title. Only sparse_alloc_in(0) uses it, falling back to the rest of window 0 if it ever fills up.
  • With the range at the start of the window, the title still gets one contiguous 11.5 GiB range.
  • New test a_reservation_larger_than_windows_1_and_2_leaves_window_0_room_for_descriptor_heaps. Your layout test and the test title's XMem check no longer expect the first graphics allocation at the region's start.

After the fix all four titles work. Two things I noticed that may be worth a look:

  • Unicorn Overlord fills the 512 MB coherent window within its first two seconds; its draws still run because the arena pool is set aside, but there is no headroom left.
  • XDefiant spends about a third of its time uploading CPU writes into the sparse windows (~10 GB in 55 s). That was already the case before this PR, for its own reservations.

ivall added a commit that referenced this pull request Nov 22, 2026
… in system memory

The texture archive viewer is gone (the workspace exclude, the README
tools row and two code comments pointed at it). The README's "performance
work (GPU buffers live in system memory)" item is out of date since the
title's graphics memory lives in video memory (PR #40).
@ivall

ivall commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Follow-up on the two notes above, in bb9f0b5:

  • Unicorn Overlord and the coherent window: it was committed console textures (place_console_texture). Unicorn makes about 2.2 GB of them, and 37 of them already filled the 512 MB. They now take the window only while 128 MB stay free (COHERENT_KEPT), and beyond that they are plain PC textures, as most of them already were. The window stops at 384 MB and the picture is unchanged. I also tried putting them in the sparse windows instead: Unicorn was fine, but XDefiant rendered black again (warm shader cache, nothing in the log). I don't understand that one yet; it's noted in HANDOFF in case you want to dig.
  • XDefiant's upload cost: no frame-rate cost so far. Its menu runs at ~53 fps, against ~50 before this PR (the log now prints the frame rate every 300 PresentX frames). The CPU time is mostly the per-submit write-watch scan over the mapped tiles (~0.8 ms per submit at ~800 submits/s). I left it alone: merging upload runs could overwrite GPU-written bytes next to a CPU-written chunk, and skipping scans would give the GPU stale data.

XCL_TRACE_MEM=1 now also logs each coherent-window allocation of 1 MB or more, and each failed one, with the XCL call site.

ivall pushed a commit that referenced this pull request Nov 22, 2026
…and coherent headroom

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.
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