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

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

@⁠raylitebull on GitHub · 2026-11-22 19:32 UTC · original
raylitebull:sparse-vram-pools → main · +1204 −173 · 10 files · 1 commits · merged 2026-11-22 20:01 UTC

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.
**@⁠raylitebull** on GitHub · 2026-11-22 19:32 UTC · [original](https://github.com/xcl-dev/xcl/pull/40) `raylitebull:sparse-vram-pools` → `main` · +1204 −173 · 10 files · 1 commits · merged 2026-11-22 20:01 UTC ## 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<usize>>) 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:05:48 +00:00
github manually merged commit 4a8344cc3f into gh-pr/40/base 2026-11-26 04:05:50 +00:00
Author
Owner

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

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** on GitHub · 2026-11-22 20:02 UTC · [original](https://github.com/xcl-dev/xcl/pull/40#issuecomment-7084895930) 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.
Author
Owner

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

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** on GitHub · 2026-11-22 20:22 UTC · [original](https://github.com/xcl-dev/xcl/pull/40#issuecomment-7085192836) 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.