From 277b9efb0af0202889b0f063b36da6c66e5488e2 Mon Sep 17 00:00:00 2001 From: Prime1997 <215761102+Prime1997@users.noreply.github.com> Date: Sun, 29 Nov 2026 06:58:40 +0000 Subject: [PATCH 1/2] D3D12.X: a buffers-only heap gets a 64 KiB PC heap, not one of its full size Every heap the title makes gets a PC heap of its full size, and a heap that may hold buffers also gets a block of unified memory as large when its first buffer is placed: buffers go in the block (console code reads them at their addresses), textures in the PC heap. A heap made with ALLOW_ONLY_BUFFERS (both texture kinds denied) never holds a texture, so its PC heap was video memory of the heap's whole size that nothing was placed in, on top of the block. Such a heap now takes its block at CreateHeap and gets a 64 KiB PC heap; GetDesc still answers with the title's size. A buffer that cannot go in the block (and does not fit in the 64 KiB) gets memory of its own of the heap's kind, as a texture past a heap's end already does. When unified memory is full the heap is made as before. XCL_SMALL_BUFFER_HEAPS=0 turns it off. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DaZV7vMq866ni7pGnJC2Ew --- README.md | 3 + crates/xcl/src/hle/d3d12x/real.rs | 119 +++++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d55abf4300..0da317770f 100644 --- a/README.md +++ b/README.md @@ -397,6 +397,9 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper. those on which the GPU completed at least one operation. For each, it names the translated pipeline bound at the last operation DRED saw complete and those bound after it, by their console shaders' hashes (`cs`, `vs` / `ps`, `ms` / `ps`, `vs` / `gs` / `ps`). +- A heap the title makes for buffers only keeps its buffers in unified memory and gets a 64 KiB PC + heap instead of a second one of its full size (video memory nothing was placed in). + `XCL_SMALL_BUFFER_HEAPS=0` gives it the full-size PC heap again. - `XCL_TEXTURE_LEVEL_PAGES=1` gives sampled title textures backing as their mip levels stream in. Zero-filled mip uploads in a batch share one staging span and still clear any mapped tiles. - `XCL_DSTORAGE_PARALLEL_DECOMPRESS=1` enables parallel CPU decompression for file-backed zlib diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index bd9cd731cf..38c5ac3eb0 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4030,7 +4030,7 @@ fn overrides(kind: Kind) -> Vec<(&'static str, usize)> { ]), Kind::PipelineState => v.push(("Release", pso_release as usize)), Kind::DescriptorHeap => v.push(("Release", heap_release as usize)), - Kind::Heap => v.push(("Release", mem_heap_release as usize)), + Kind::Heap => v.extend_from_slice(&[("Release", mem_heap_release as usize), ("GetDesc", mem_heap_get_desc as usize)]), Kind::Fence => v.extend_from_slice(&[ ("Signal", fence_signal as usize), ("GetCompletedValue", fence_get_completed_value as usize), @@ -4458,7 +4458,7 @@ unsafe fn placed_in_heap_x(this: *mut c_void, heap: usize, offset: usize, desc: // heap's kind; the console's aliasing of that one placement is lost. let is_texture = u32::from_le_bytes(x[0..4].try_into().unwrap()) != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32; let hr3 = match heap_desc { - Some(h) if hr2 < 0 && is_texture => Some(dev_create_committed_resource(this, &h.Properties as *const _ as usize, 0, d.as_ptr() as usize, st as usize, clear_d, riid, ppv)), + Some(h) if hr2 < 0 && (is_texture || is_small_buffer_heap(heap)) => Some(dev_create_committed_resource(this, &h.Properties as *const _ as usize, 0, d.as_ptr() as usize, st as usize, clear_d, riid, ppv)), _ => None, }; static N: AtomicU64 = AtomicU64::new(0); @@ -4618,6 +4618,9 @@ const PC_HEAP_FLAGS: u32 = 0x3FED; /// field atlas heap) is made the PC's way: known flags only, the alignment the PC has that /// covers it, an unknown type as DEFAULT. Logged with the console's values. pub(super) unsafe extern "system" fn dev_create_heap(this: *mut c_void, desc: usize, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { + if let Some(hr) = small_buffer_heap(desc, ppv, |d| dev_create_heap_pc(this, d, riid, ppv)) { + return hr; + } let hr = dev_create_heap_pc(this, desc, riid, ppv); if hr >= 0 { note_heap_uma(desc, ppv); @@ -4649,6 +4652,9 @@ pub(super) unsafe extern "system" fn dev_create_heap(this: *mut c_void, desc: us creator!(dev_create_heap1_pc, "CreateHeap1", Kind::Heap, [a1, a2]); pub(super) unsafe extern "system" fn dev_create_heap1(this: *mut c_void, desc: usize, session: usize, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { + if let Some(hr) = small_buffer_heap(desc, ppv, |d| dev_create_heap1_pc(this, d, session, riid, ppv)) { + return hr; + } let hr = dev_create_heap1_pc(this, desc, session, riid, ppv); if hr >= 0 { note_heap_uma(desc, ppv); @@ -4665,6 +4671,9 @@ struct HeapUma { /// in it read back on `Map(0, NULL, ...)`. cpu_readback: bool, block: Option, + /// a buffers-only heap whose PC heap is `SMALL_BUFFER_HEAP` (`small_buffer_heap`): its + /// buffers are all in `block` + small: bool, } /// heap → its backing block (allocated lazily on the first placed buffer; sparse VRAM for @@ -4700,10 +4709,91 @@ unsafe fn note_heap_uma(desc: usize, ppv: *mut *mut c_void) { prefer_coherent: is_rb && size <= COHERENT_READBACK_MAX, cpu_readback: is_rb || is_custom_wb, block: None, + small: false, }, ); } +/// The PC heap of a buffers-only heap whose block of unified memory is taken at once. +const SMALL_BUFFER_HEAP: u64 = 0x1_0000; + +/// `D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS`: no texture of either kind (and buffers not denied) +fn buffers_only(flags: i32) -> bool { + const DENY_BUFFERS: i32 = 0x4; + const DENY_TEXTURES: i32 = 0x40 | 0x80; + flags & DENY_TEXTURES == DENY_TEXTURES && flags & DENY_BUFFERS == 0 +} + +/// `XCL_SMALL_BUFFER_HEAPS=0`: a buffers-only heap gets a PC heap of its full size again. +fn small_buffer_heaps() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| std::env::var("XCL_SMALL_BUFFER_HEAPS").map_or(true, |v| v.trim() != "0")) +} + +/// A heap the title made for buffers only (`buffers_only`): each of its buffers is placed in its +/// block of unified memory (`heap_block`), so a PC heap of its size held video memory that +/// nothing was placed in, besides the block's own. The block is taken now and the PC heap is +/// `SMALL_BUFFER_HEAP` (`mem_heap_get_desc` still answers with the title's size); a buffer that +/// cannot go in the block gets memory of its own (`placed_in_heap_x`). None: not such a heap, +/// or no block (unified memory full): made as before. `create` makes a PC heap from a +/// description. +unsafe fn small_buffer_heap(desc: usize, ppv: *mut *mut c_void, create: impl FnOnce(usize) -> HRESULT) -> Option { + if !small_buffer_heaps() || super::title_textures::is_own_work() || desc == 0 || ppv.is_null() + || !crate::gpu::shader::readable(desc, std::mem::size_of::()) + { + return None; + } + let title: D3D12_HEAP_DESC = std::ptr::read_unaligned(desc as *const D3D12_HEAP_DESC); + if !buffers_only(title.Flags.0) || title.SizeInBytes <= SMALL_BUFFER_HEAP || crate::gpu::uma::get().is_none() { + return None; + } + // the PC's way of the description (as `dev_create_heap`'s second try), at the small size + let mut d = title; + d.Flags = D3D12_HEAP_FLAGS(d.Flags.0 & PC_HEAP_FLAGS as i32); + d.Alignment = 0; + if !(1..=5).contains(&d.Properties.Type.0) { + d.Properties = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }; + } + d.SizeInBytes = SMALL_BUFFER_HEAP; + let hr = create(&d as *const _ as usize); + if hr < 0 || (*ppv).is_null() { + return None; + } + let heap = *ppv as usize; + note_heap_uma(desc, ppv); + let marked = HEAP_UMA.lock().unwrap().as_mut().and_then(|m| m.get_mut(&heap)).map(|e| e.small = true).is_some(); + if !marked || heap_block(heap).is_none() { + // (Release forgets the heap's entry: `mem_heap_release`) + let release: RefFn = std::mem::transmute(*vt(*ppv).add(2)); + release(*ppv); + *ppv = std::ptr::null_mut(); + return None; + } + static N: AtomicU64 = AtomicU64::new(0); + if N.fetch_add(1, Ordering::Relaxed) < 4 { + crate::xlog_info!("D3D12.X: buffers-only heap of {:#x} bytes: its buffers in unified memory, a PC heap of {SMALL_BUFFER_HEAP:#x}", title.SizeInBytes); + } + Some(hr) +} + +/// Whether `heap` is a buffers-only heap with a small PC heap (`small_buffer_heap`). +fn is_small_buffer_heap(heap: usize) -> bool { + HEAP_UMA.lock().unwrap().as_ref().and_then(|m| m.get(&heap)).is_some_and(|e| e.small) +} + +/// ID3D12Heap::GetDesc: a buffers-only heap with a small PC heap (`small_buffer_heap`) has the +/// size the title made it with. +unsafe extern "system" fn mem_heap_get_desc(this: *mut c_void, out: *mut D3D12_HEAP_DESC) -> *mut D3D12_HEAP_DESC { + type F = unsafe extern "system" fn(*mut c_void, *mut D3D12_HEAP_DESC) -> *mut D3D12_HEAP_DESC; + let f: F = std::mem::transmute(orig_fn(this, pc_slot!("ID3D12Heap", "GetDesc"))); + let r = f(this, out); + let title = HEAP_UMA.lock().unwrap().as_ref().and_then(|m| m.get(&(this as usize))).filter(|e| e.small).map(|e| e.size); + if let (Some(size), false) = (title, r.is_null()) { + (*r).SizeInBytes = size as u64; + } + r +} + /// The block of unified memory standing for `heap` and whether it may hold CPU-readback buffers /// (taken now if it has none yet; None: not a heap that takes one, or unified memory is full). fn heap_block(heap: usize) -> Option<(usize, bool)> { @@ -14988,16 +15078,37 @@ mod heap_release_order_tests { ); } + #[test] + fn only_a_heap_that_denies_both_kinds_of_texture_is_buffers_only() { + assert!(buffers_only(0xC0), "ALLOW_ONLY_BUFFERS"); + assert!(buffers_only(0xC2), "with a console flag (Fortnite's)"); + assert!(!buffers_only(0x40), "render targets denied, other textures allowed"); + assert!(!buffers_only(0x80), "other textures denied, render targets allowed"); + assert!(!buffers_only(0x0), "everything"); + assert!(!buffers_only(0xC4), "buffers denied too"); + } + + #[test] + fn a_small_buffer_heap_is_known_by_its_entry() { + let (small, other) = (0x5A11_0000usize, 0x5A12_0000usize); + let entry = |small| HeapUma { size: 0x40_0000, align: 0x1_0000, prefer_coherent: false, cpu_readback: false, block: Some(0x1000_0000), small }; + HEAP_UMA.lock().unwrap().get_or_insert_with(HashMap::new).extend([(small, entry(true)), (other, entry(false))]); + assert!(is_small_buffer_heap(small)); + assert!(!is_small_buffer_heap(other)); + assert!(!is_small_buffer_heap(0x5A13_0000), "a heap without an entry"); + HEAP_UMA.lock().unwrap().as_mut().unwrap().retain(|&k, _| k != small && k != other); + } + #[test] fn memory_heap_release_keeps_replacement_at_reused_address() { fn replace(addr: usize) { - let replacement = HeapUma { size: 0x2_0000, align: 0x1_0000, prefer_coherent: false, cpu_readback: false, block: None }; + let replacement = HeapUma { size: 0x2_0000, align: 0x1_0000, prefer_coherent: false, cpu_readback: false, block: None, small: false }; HEAP_UMA.lock().unwrap().get_or_insert_with(HashMap::new).insert(addr, replacement); } let heap = fake(1, false, replace); let addr = heap as usize; - let old = HeapUma { size: 0x1_0000, align: 0x1_0000, prefer_coherent: false, cpu_readback: false, block: None }; + let old = HeapUma { size: 0x1_0000, align: 0x1_0000, prefer_coherent: false, cpu_readback: false, block: None, small: false }; HEAP_UMA.lock().unwrap().get_or_insert_with(HashMap::new).insert(addr, old); unsafe { assert_eq!(mem_heap_release(heap.cast()), 0); From e8f6939359232ce5272d2ff3e01c109ede2bd192 Mon Sep 17 00:00:00 2001 From: Prime1997 <215761102+Prime1997@users.noreply.github.com> Date: Sun, 29 Nov 2026 07:58:58 +0000 Subject: [PATCH 2/2] D3D12.X: a small buffers-only heap reports its size rounded to 64 KiB A buffers-only heap with a 64 KiB PC heap answered GetDesc with the title's exact size, and its PC heap was made with alignment 0 whatever the title asked. A heap made the usual way from a description the PC refuses has its size rounded up to 64 KiB and an alignment above 64 KiB promoted to 4 MiB (dev_create_heap), and d3d12_create_heap's console_heap_descs_refused_by_host_are_normalized_by_xcl checks exactly that: it failed on the RTX 3060 Ti with 0x12345 where 0x20000 was expected. The small heap now does the same. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DaZV7vMq866ni7pGnJC2Ew --- crates/xcl/src/hle/d3d12x/real.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 38c5ac3eb0..d125676253 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4750,7 +4750,7 @@ unsafe fn small_buffer_heap(desc: usize, ppv: *mut *mut c_void, create: impl FnO // the PC's way of the description (as `dev_create_heap`'s second try), at the small size let mut d = title; d.Flags = D3D12_HEAP_FLAGS(d.Flags.0 & PC_HEAP_FLAGS as i32); - d.Alignment = 0; + d.Alignment = if d.Alignment <= 0x1_0000 { 0 } else { D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT as u64 }; if !(1..=5).contains(&d.Properties.Type.0) { d.Properties = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }; } @@ -4782,14 +4782,15 @@ fn is_small_buffer_heap(heap: usize) -> bool { } /// ID3D12Heap::GetDesc: a buffers-only heap with a small PC heap (`small_buffer_heap`) has the -/// size the title made it with. +/// size the title made it with, rounded up to 64 KiB as `dev_create_heap` makes a heap of a size +/// the PC refuses. unsafe extern "system" fn mem_heap_get_desc(this: *mut c_void, out: *mut D3D12_HEAP_DESC) -> *mut D3D12_HEAP_DESC { type F = unsafe extern "system" fn(*mut c_void, *mut D3D12_HEAP_DESC) -> *mut D3D12_HEAP_DESC; let f: F = std::mem::transmute(orig_fn(this, pc_slot!("ID3D12Heap", "GetDesc"))); let r = f(this, out); let title = HEAP_UMA.lock().unwrap().as_ref().and_then(|m| m.get(&(this as usize))).filter(|e| e.small).map(|e| e.size); if let (Some(size), false) = (title, r.is_null()) { - (*r).SizeInBytes = size as u64; + (*r).SizeInBytes = (size as u64).div_ceil(0x1_0000) * 0x1_0000; } r }