From 24fdb7b9297e47af1433a7c1069436becfe7a724 Mon Sep 17 00:00:00 2001 From: Kalmarv <59708937+Kalmarv@users.noreply.github.com> Date: Fri, 27 Nov 2026 03:26:31 -0500 Subject: [PATCH] D3D12.X: XCL_SMALL_TEXTURES=1 puts small sampled textures on 4 KiB placements A sampled texture the shared 64 KiB pages do not take (vram::place_pages refuses it; a 1D texture cannot be a reserved resource) was a committed resource, which takes at least 64 KiB of video memory. On the title the committed fallback held 8,361 x 64 KiB = 523 MiB (XCL_COMMITTED_AUDIT), mostly ~5k Texture1D RGBA8_SRGB textures of 4 KiB or less. With the switch, such a texture goes on a 4 KiB placement in a 4 MiB DEFAULT heap of XCL's own (ALLOW_ONLY_NON_RT_DS_TEXTURES), made through the adopting CreatePlacedResource. Only when the PC allows it: no RT/DS/UAV flags, one sample, and GetResourceAllocationInfo returning 4 KiB alignment for the description asking for it (most detailed level within 64 KiB). Otherwise, and when no heap can take it, it is committed as before. Its units are free again where late_release lets its last reference go (the queues are past the work before it); with XCL_LATE_RELEASE=0 the switch does nothing (logged once), as uploads of XCL's into a texture could still be in flight at the title's release. XCL asks no residency priority for the heaps, as for a committed texture (logged per heap). Measured on the title (RTX 5070 Ti, one 480 s run per arm, same base and env, with and without the switch): 8,192 small textures live in 58 MiB of 15 heaps, against at least 512 MiB as committed resources; local video memory over 250-310 s 18,332 vs 18,793 MB (-461 MB) with equal sparse pools and target pages. Still 3.1 GB over the budget there, and the frame rate did not move. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/device_x.rs | 6 +- crates/xcl/src/hle/d3d12x/late_release.rs | 1 + crates/xcl/src/hle/d3d12x/mod.rs | 1 + crates/xcl/src/hle/d3d12x/real.rs | 1 + crates/xcl/src/hle/d3d12x/small_tex.rs | 247 ++++++++++++++++++++++ 5 files changed, 254 insertions(+), 2 deletions(-) create mode 100644 crates/xcl/src/hle/d3d12x/small_tex.rs diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 0a0de0b0c2..f306d69c55 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -1148,7 +1148,9 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, placed_hr = super::vram::place_pages(this, va as usize, &pc, state, clear, console_bytes, riid, ppv); } } - let hr = match placed_hr { + // (a small one the shared pages do not take: on a 4 KiB placement, `XCL_SMALL_TEXTURES=1`) + let small_hr = if placed_hr.is_none() && !buffer { super::small_tex::place(this, &std::ptr::read_unaligned(d.as_ptr() as *const D3D12_RESOURCE_DESC), state, clear, riid, ppv) } else { None }; + let hr = match placed_hr.or(small_hr) { Some(hr) => hr, None => { let heap = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }; @@ -1161,7 +1163,7 @@ pub(super) unsafe extern "system" fn create_placed_resource_x(this: *mut c_void, Ok(()) => { real::note_title_address(*ppv as usize, va as usize); super::depth_uav::note_created(*ppv, &x, Some(va as usize), state); - super::lut3d::created(if placed_hr.is_some() { "CreatePlacedResourceX (sampled: on shared pages)" } else { "CreatePlacedResourceX (sampled: committed)" }, *ppv, Some(&x), Some(va as usize), true, state); + super::lut3d::created(if placed_hr.is_some() { "CreatePlacedResourceX (sampled: on shared pages)" } else if small_hr.is_some() { "CreatePlacedResourceX (sampled: 4 KiB placement)" } else { "CreatePlacedResourceX (sampled: committed)" }, *ppv, Some(&x), Some(va as usize), true, state); return hr; } Err(e) if once(&M, 4) => { diff --git a/crates/xcl/src/hle/d3d12x/late_release.rs b/crates/xcl/src/hle/d3d12x/late_release.rs index 3973ef4f23..62cc1c7669 100644 --- a/crates/xcl/src/hle/d3d12x/late_release.rs +++ b/crates/xcl/src/hle/d3d12x/late_release.rs @@ -262,6 +262,7 @@ pub unsafe fn drain_known(seal_now: bool) { // lock of this module's and does not wait for the sparse state's.) super::sparse::forget_buffer(res); super::placements::gone(res); + super::small_tex::gone(res); let f: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(super::real::orig_fn(res as *mut c_void, 2)); f(res as *mut c_void); } diff --git a/crates/xcl/src/hle/d3d12x/mod.rs b/crates/xcl/src/hle/d3d12x/mod.rs index bbfa841051..80d4a800ed 100644 --- a/crates/xcl/src/hle/d3d12x/mod.rs +++ b/crates/xcl/src/hle/d3d12x/mod.rs @@ -42,6 +42,7 @@ pub mod real; mod scatter; mod shader_cache_session; mod sharded; +mod small_tex; pub mod sparse; mod standin; mod targets; diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 570ef64a10..59297cfb7f 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -1991,6 +1991,7 @@ unsafe extern "system" fn res_release(this: *mut c_void) -> u32 { // (a held one is gone when `late_release` lets it go) if !held { super::placements::gone(this as usize); + super::small_tex::gone(this as usize); } super::depth_uav::forget(this as usize); super::title_textures::forget(this as usize); diff --git a/crates/xcl/src/hle/d3d12x/small_tex.rs b/crates/xcl/src/hle/d3d12x/small_tex.rs new file mode 100644 index 0000000000..0a2ba2a855 --- /dev/null +++ b/crates/xcl/src/hle/d3d12x/small_tex.rs @@ -0,0 +1,247 @@ +//! Small sampled textures of the title that get no shared pages (`vram::place_pages` refuses them: +//! a 1D texture cannot be a reserved resource) on 4 KiB placements in heaps of XCL's own +//! (`XCL_SMALL_TEXTURES=1`), instead of a committed resource each: a committed resource takes at +//! least 64 KiB of video memory, and a title's thousands of tiny lookup textures (a few hundred +//! bytes each) took ~300 MiB that way. +//! +//! Only what the PC itself allows: no render target, depth or UAV flags, one sample, and +//! `GetResourceAllocationInfo` giving `D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT` (4 KiB) for the +//! description asking for it (its most detailed level fits in 64 KiB). Anything else, and a +//! texture the heaps here cannot take, is made committed as before. +//! +//! A texture's units are free again once its last reference goes (`gone`, where `late_release` +//! lets it go: the queues are past the work submitted before; with `XCL_LATE_RELEASE=0` nothing +//! is placed here). The heaps stay (4 MiB each). + +use std::collections::HashMap; +use std::ffi::c_void; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::Mutex; + +use windows::core::Interface; +use windows::Win32::Graphics::Direct3D12::*; + +use crate::util::HRESULT; + +/// a placement unit: the small-resource alignment +const UNIT: u64 = D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT as u64; +/// one heap of XCL's own +const HEAP_BYTES: u64 = 4 << 20; +const UNITS: usize = (HEAP_BYTES / UNIT) as usize; + +/// Whether small sampled textures go on 4 KiB placements (`XCL_SMALL_TEXTURES=1`). +pub(super) fn enabled() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| std::env::var("XCL_SMALL_TEXTURES").is_ok_and(|v| v.trim() == "1")) +} + +/// The units of one heap: first fit of a run of `n`. +struct Units(Vec); + +impl Units { + fn new() -> Self { + Units(vec![false; UNITS]) + } + + fn take(&mut self, n: usize) -> Option { + let mut run = 0; + for i in 0..self.0.len() { + run = if self.0[i] { 0 } else { run + 1 }; + if run == n { + let first = i + 1 - n; + self.0[first..=i].iter_mut().for_each(|u| *u = true); + return Some(first); + } + } + None + } + + fn give(&mut self, first: usize, n: usize) { + self.0[first..first + n].iter_mut().for_each(|u| *u = false); + } +} + +struct Pool { + /// the device the heaps are of + dev: usize, + heaps: Vec<(ID3D12Heap, Units)>, + /// live texture → (heap, first unit, units) + live: HashMap, + units_live: usize, +} + +static POOL: Mutex> = Mutex::new(None); + +/// The units `desc` takes on a 4 KiB placement, or None when the PC does not place it so. +pub(super) unsafe fn units_for(dev: &ID3D12Device, desc: &D3D12_RESOURCE_DESC) -> Option { + let shut = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; + if desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER || desc.Dimension == D3D12_RESOURCE_DIMENSION_UNKNOWN || (desc.Flags & shut).0 != 0 || desc.SampleDesc.Count != 1 { + return None; + } + let mut small = *desc; + small.Alignment = UNIT; + let info = dev.GetResourceAllocationInfo(0, &[small]); + (info.Alignment == UNIT && info.SizeInBytes != u64::MAX && info.SizeInBytes > 0 && info.SizeInBytes <= D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT as u64) + .then(|| info.SizeInBytes.div_ceil(UNIT) as usize) +} + +/// A sampled texture `desc` on a 4 KiB placement in a heap of XCL's own (device `this`), made as +/// `CreatePlacedResource` (adopted as any resource); None when it is not placed so (the caller makes +/// it committed). +pub(super) unsafe fn place(this: *mut c_void, desc: &D3D12_RESOURCE_DESC, state: u32, clear: *const c_void, riid: *const windows_sys::core::GUID, ppv: *mut *mut c_void) -> Option { + // (with `XCL_LATE_RELEASE=0` a texture's units would be free at the title's release, while an + // upload of XCL's into it can still be in flight: committed then) + if !enabled() { + return None; + } + if !super::late_release::enabled() { + static ONCE: std::sync::Once = std::sync::Once::new(); + ONCE.call_once(|| crate::xlog_warn!("D3D12.X: XCL_SMALL_TEXTURES=1 does nothing with XCL_LATE_RELEASE=0: small textures stay committed")); + return None; + } + let dev = ID3D12Device::from_raw_borrowed(&this)?; + let n = units_for(dev, desc)?; + let mut small = *desc; + small.Alignment = UNIT; + // the units first (the heap stays while the pool does); the resource is made outside the lock + let (heap_raw, heap_index, first) = { + let mut g = POOL.lock().unwrap(); + let pool = g.get_or_insert_with(|| Pool { dev: this as usize, heaps: Vec::new(), live: HashMap::new(), units_live: 0 }); + if pool.dev != this as usize { + return None; + } + let found = pool.heaps.iter_mut().enumerate().find_map(|(k, (h, u))| u.take(n).map(|first| (h.as_raw() as usize, k, first))); + match found { + Some(f) => f, + None => { + let hd = D3D12_HEAP_DESC { + SizeInBytes: HEAP_BYTES, + Properties: D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, + Alignment: 0, + Flags: D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES, + }; + let mut heap: Option = None; + if dev.CreateHeap(&hd, &mut heap).is_err() { + return None; + } + let heap = heap?; + let mut units = Units::new(); + let first = units.take(n)?; + let raw = heap.as_raw() as usize; + pool.heaps.push((heap, units)); + let k = pool.heaps.len(); + if k <= 4 || k.is_power_of_two() { + // (XCL asks no residency priority for these heaps or for a committed texture, + // with or without XCL_VRAM_HOT_PRIORITY) + crate::xlog_info!("D3D12.X: small textures: heap {k} of 4 MiB made, residency priority: default, no override (as for a committed texture)"); + } + (raw, k - 1, first) + } + } + }; + let hr = super::real::dev_create_placed_resource(this, heap_raw, first * UNIT as usize, &small as *const _ as usize, state as usize, clear as usize, riid, ppv); + let mut g = POOL.lock().unwrap(); + let pool = g.as_mut().unwrap(); + if hr < 0 || (*ppv).is_null() { + pool.heaps[heap_index].1.give(first, n); + static FAILED: AtomicU64 = AtomicU64::new(0); + if FAILED.fetch_add(1, Ordering::Relaxed) < 4 { + crate::xlog_warn!("D3D12.X: a small texture's 4 KiB placement failed ({hr:#x}); it is made committed"); + } + return None; + } + pool.live.insert(*ppv as usize, (heap_index, first, n)); + pool.units_live += n; + static PLACED: AtomicU64 = AtomicU64::new(0); + let k = PLACED.fetch_add(1, Ordering::Relaxed) + 1; + if k.is_power_of_two() && k >= 64 { + crate::xlog_info!( + "D3D12.X: {k} small textures on 4 KiB placements so far; {} live in {} KiB of {} heaps of 4 MiB (committed: at least {} KiB)", + pool.live.len(), + pool.units_live * UNIT as usize >> 10, + pool.heaps.len(), + pool.live.len() * 64 + ); + } + Some(hr) +} + +/// The texture `res` is destroyed next: its units are free. +pub(super) fn gone(res: usize) { + if !enabled() { + return; + } + let mut g = POOL.lock().unwrap(); + if let Some(pool) = g.as_mut() { + if let Some((k, first, n)) = pool.live.remove(&res) { + pool.heaps[k].1.give(first, n); + pool.units_live -= n; + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_SAMPLE_DESC}; + + /// Units go first fit and come back: a freed run is taken again, a run longer than any gap is not. + #[test] + fn units_are_taken_first_fit_and_given_back() { + let mut u = Units::new(); + assert_eq!(u.take(1), Some(0)); + assert_eq!(u.take(4), Some(1)); + assert_eq!(u.take(1), Some(5)); + u.give(1, 4); + assert_eq!(u.take(2), Some(1)); + assert_eq!(u.take(3), Some(6), "the 2-unit gap left at 3 is too small"); + assert_eq!(u.take(2), Some(3)); + let mut full = Units::new(); + assert_eq!(full.take(UNITS), Some(0)); + assert_eq!(full.take(1), None); + full.give(UNITS - 2, 2); + assert_eq!(full.take(3), None); + assert_eq!(full.take(2), Some(UNITS - 2)); + } + + fn tex(dim: D3D12_RESOURCE_DIMENSION, w: u64, h: u32, format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT, flags: D3D12_RESOURCE_FLAGS) -> D3D12_RESOURCE_DESC { + D3D12_RESOURCE_DESC { + Dimension: dim, + Alignment: 0, + Width: w, + Height: h, + DepthOrArraySize: 1, + MipLevels: 1, + Format: format, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Layout: D3D12_TEXTURE_LAYOUT_UNKNOWN, + Flags: flags, + } + } + + /// The PC's rules decide: a tiny 1D or 2D sampled texture is placed on 4 KiB, a render target + /// or a texture over 64 KiB is not, and a buffer is never. + #[test] + fn only_textures_the_pc_places_on_4_kib_are_taken() { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_12_0; + let mut dev: Option = None; + if unsafe { D3D12CreateDevice(None, D3D_FEATURE_LEVEL_12_0, &mut dev) }.is_err() { + return crate::gpu::recomp::runner::skip_test("no D3D12 device"); + } + let dev = dev.unwrap(); + unsafe { + // 256 texels of RGBA8: 1 KiB of texels, on a 4 KiB placement of the driver's size (8 KiB + // on an RTX 5070 Ti with driver 610.74) + let n = units_for(&dev, &tex(D3D12_RESOURCE_DIMENSION_TEXTURE1D, 256, 1, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D3D12_RESOURCE_FLAG_NONE)); + assert!(n.is_some_and(|n| (1..=16).contains(&n)), "{n:?}"); + // 64x64 RGBA8: 16 KiB + let n = units_for(&dev, &tex(D3D12_RESOURCE_DIMENSION_TEXTURE2D, 64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE)); + assert!(n.is_some_and(|n| (4..=16).contains(&n)), "{n:?}"); + assert_eq!(units_for(&dev, &tex(D3D12_RESOURCE_DIMENSION_TEXTURE2D, 64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)), None); + assert_eq!(units_for(&dev, &tex(D3D12_RESOURCE_DIMENSION_TEXTURE2D, 512, 512, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE)), None); + let mut buf = tex(D3D12_RESOURCE_DIMENSION_BUFFER, 4096, 1, windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE); + buf.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + assert_eq!(units_for(&dev, &buf), None); + } + } +}