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..d125676253 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,92 @@ 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 = 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() }; + } + 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, 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).div_ceil(0x1_0000) * 0x1_0000; + } + 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 +15079,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);