From 2441d59415277fafc0023c2b26c71502d3c0dadf Mon Sep 17 00:00:00 2001 From: raylitebull <287713799+raylitebull@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:31:34 +0100 Subject: [PATCH 1/5] D3D12.X: pack sub-64 KiB sampled textures into 4 KiB-aligned VRAM heaps --- crates/xcl/src/hle/d3d12x/mod.rs | 1 + crates/xcl/src/hle/d3d12x/title_textures.rs | 28 +- crates/xcl/src/hle/d3d12x/vram.rs | 30 +- crates/xcl/src/hle/d3d12x/vram_small.rs | 525 ++++++++++++++++++++ 4 files changed, 570 insertions(+), 14 deletions(-) create mode 100644 crates/xcl/src/hle/d3d12x/vram_small.rs diff --git a/crates/xcl/src/hle/d3d12x/mod.rs b/crates/xcl/src/hle/d3d12x/mod.rs index 8886624632..62c9d5b6be 100644 --- a/crates/xcl/src/hle/d3d12x/mod.rs +++ b/crates/xcl/src/hle/d3d12x/mod.rs @@ -33,6 +33,7 @@ mod standin; pub mod title_list; pub mod title_textures; pub mod vram; +mod vram_small; use std::ffi::c_void; use std::sync::OnceLock; diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index c5727658c7..402cdbd700 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -2162,10 +2162,12 @@ mod tests { assert_eq!(read_sub(&old_res, 0), subresources(base, &xg::Texture::new(&old_desc).unwrap())[0].data, "reactivated unmapped texture remapped cleanly"); drop(old_res); - // 3. 4 KiB-aligned adjacent textures inside one 64 KiB page + straddling page boundary, - // plus reclaiming dead extra_pages when overwritten by a larger placement without Release. + // 3. 4 KiB-aligned adjacent small textures pack into 4 KiB sub-allocated slots (no 64 KiB + // bloat per small texture), reuse the exact slot at the same va without crosstalk, and + // >= 64 KiB unaligned textures reclaim dead extra_pages immediately when overwritten. let d16 = console_desc(3, 64, 64, 1, 1, 28, 0x105); // 4KB_S, 16 KiB let d64 = console_desc(3, 128, 128, 1, 1, 28, 0x105); // 4KB_S, 64 KiB + let adj_base_pages = dev_pages(); let adj_specs = [(0x0000usize, d16, 0x11u8), (0x4000, d16, 0x22), (0x8000, d16, 0x33), (0xC000, d64, 0x44), (0x1_C000, d16, 0x55)]; let adj: Vec<_> = adj_specs.iter().map(|&(off, d, _)| place_tex(base + off, &d)).collect(); for &(off, d, seed) in &adj_specs { @@ -2176,14 +2178,24 @@ mod tests { assert_eq!(read_sub(r, 0), subresources(base + off, &xg::Texture::new(&d).unwrap())[0].data, "4 KiB neighbour at +{off:#x} crosstalk"); } let pages_before_reuse = dev_pages(); + let adj_delta = pages_before_reuse - adj_base_pages; + assert!(adj_delta <= 128 * 1024, "four 16 KiB + one 64 KiB textures should use <= 128 KiB, got {adj_delta} B"); let reused = place_tex(base + 0x4000, &d16); - assert_eq!(dev_pages(), pages_before_reuse, "reusing unaligned va must share extra_pages tile"); - // Overwrite the first 64 KiB (covering +0x0000, +0x4000 (both adj[1] & reused), +0x8000, +0xC000) - // WITHOUT releasing adj/reused: dead extra_pages (+0x4000, +0x8000) must drop immediately! - let cover = place_tex(base, &d64); + assert_eq!(dev_pages(), pages_before_reuse, "reusing unaligned va must share backing slot"); + drop((reused, adj)); + + // Reclaiming dead extra_pages immediately when a >= 64 KiB unaligned tiled texture is + // overwritten by a larger placement without Release: + let d64_mips = console_desc(3, 128, 128, 1, 8, 28, 0x105); // 2 tiles (mip 0 + packed mips) + let d128 = console_desc(3, 256, 128, 1, 1, 28, 0x109); // 128 KiB, 2 tiles + let anchor = place_tex(base, &d64); + let unaligned_a = place_tex(base + 0x4000, &d64_mips); + let unaligned_b = place_tex(base + 0x4000, &d64_mips); + let pages_with_extra = dev_pages(); + let cover = place_tex(base, &d128); let pages_after_cover = dev_pages(); - assert!(pages_after_cover < pages_before_reuse, "superseded unaligned extra_pages must be reclaimed immediately ({pages_before_reuse} -> {pages_after_cover})"); - drop((cover, reused, adj)); + assert!(pages_after_cover < pages_with_extra, "superseded unaligned extra_pages must be reclaimed immediately ({pages_with_extra} -> {pages_after_cover})"); + drop((cover, unaligned_b, unaligned_a, anchor)); // 4. Full mip chain (standard + packed mips, RGBA8 & BC7), tiny all-packed mips, // 6-slice cubemap (exercises extra_pages reuse), and 3D volume texture (Tier 3). diff --git a/crates/xcl/src/hle/d3d12x/vram.rs b/crates/xcl/src/hle/d3d12x/vram.rs index 35fc67d205..eb16b7e063 100644 --- a/crates/xcl/src/hle/d3d12x/vram.rs +++ b/crates/xcl/src/hle/d3d12x/vram.rs @@ -140,7 +140,9 @@ pub(super) fn is_placed_texture(resource: usize) -> bool { pub(super) fn forget(resource: usize) { let placed = PLACED_TEXTURES.lock().unwrap().as_mut().is_some_and(|textures| textures.remove(&resource)); if placed { - forget_pages(resource); + if !super::vram_small::forget(resource) { + forget_pages(resource); + } if let Some(id) = ARENA_OF.lock().unwrap().as_mut().and_then(|m| m.remove(&resource)) { ARENAS.lock().unwrap().release(id, std::time::Instant::now()); } @@ -1210,17 +1212,26 @@ pub(super) unsafe fn before_execute(queue: &ID3D12CommandQueue) { #[cfg(test)] pub(super) static VRAM_TEST_LOCK: Mutex<()> = Mutex::new(()); +/// Whether `va` already has a live 64 KiB page on `device` (e.g., an existing GPU-owned render +/// target that a sampled view at the same 64 KiB-aligned address should alias). +fn has_live_page(device: usize, va: usize) -> bool { + let g = PAGES.lock().unwrap(); + g.get(&device).and_then(|p| p.as_ref()).is_some_and(|p| p.pages.contains_key(&va)) +} + #[cfg(test)] pub(super) fn page_bytes_for(device: usize) -> (usize, usize) { + let (su, sp) = super::vram_small::bytes_for(device); let g = PAGES.lock().unwrap(); - let Some(Some(p)) = g.get(&device) else { return (0, 0) }; - ((p.pages.len() + p.extra_pages.len()) * PAGE, p.pools.len() * POOL_PAGES as usize * PAGE) + let Some(Some(p)) = g.get(&device) else { return (su, sp) }; + ((p.pages.len() + p.extra_pages.len()) * PAGE + su, p.pools.len() * POOL_PAGES as usize * PAGE + sp) } /// (bytes of pages in use by tiled textures, bytes of pool heaps) – `XCL_ACTIVITY` pub fn page_bytes() -> (usize, usize) { + let (su, sp) = super::vram_small::bytes(); let g = PAGES.lock().unwrap(); - g.values().flatten().fold((0, 0), |(used, pools), p| { + g.values().flatten().fold((su, sp), |(used, pools), p| { (used + (p.pages.len() + p.extra_pages.len()) * PAGE, pools + p.pools.len() * POOL_PAGES as usize * PAGE) }) } @@ -1232,8 +1243,9 @@ fn tier2(dev: &ID3D12Device) -> bool { } /// A sampled texture of the title at `va` (`desc`: the PC description, `console_bytes` its size on -/// the console) backed by shared 64 KiB video-memory pages; None when reserved placement is not -/// available (the caller falls back to a committed resource). +/// the console) backed by shared 4 KiB small-resource heaps (when < 64 KiB and not aliasing an +/// existing 64 KiB GPU page) or shared 64 KiB video-memory pages; None when neither applies (the +/// caller falls back to a committed resource). #[allow(clippy::too_many_arguments)] pub unsafe fn place_pages( this: *mut c_void, @@ -1252,6 +1264,12 @@ pub unsafe fn place_pages( if console_has_packed_3d_mips(desc) { return None; } + if !(va % PAGE == 0 && has_live_page(dev.as_raw() as usize, va)) { + if let Some(hr) = super::vram_small::place(dev, this, va, desc, state, clear, console_bytes, riid, ppv) { + PLACED_TEXTURES.lock().unwrap().get_or_insert_with(HashSet::new).insert(*ppv as usize); + return Some(hr); + } + } // (one slice: the tiles of an array's packed levels are not asked about here) let lazy = level_pages() && desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D && desc.DepthOrArraySize == 1 && desc.SampleDesc.Count == 1 && desc.Flags == D3D12_RESOURCE_FLAG_NONE; place_on_pages(dev, this, va, desc, state, clear, console_bytes, false, lazy, riid, ppv) diff --git a/crates/xcl/src/hle/d3d12x/vram_small.rs b/crates/xcl/src/hle/d3d12x/vram_small.rs new file mode 100644 index 0000000000..3a1daf8b45 --- /dev/null +++ b/crates/xcl/src/hle/d3d12x/vram_small.rs @@ -0,0 +1,525 @@ +//! 4 KiB sub-allocated default heaps for small (< 64 KiB) sampled textures. +//! +//! A console title packs thousands of small mip-tails, icons, decals and LUTs at 256 B – 4 KiB +//! offsets inside its texture pools. Backing each one with a 64 KiB reserved resource in +//! `vram::Pages` wastes up to 16× video memory (one full 64 KiB `extra_pages` tile per small +//! texture) and submits a `CreateReservedResource` + `GetResourceTiling` + `UpdateTileMappings` +//! round trip for every placement. +//! +//! Direct3D 12 supports 4 KiB placement (`D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT`) for +//! single-sample non-RT/DS textures whose allocation info fits under 64 KiB. Placing those in +//! shared 16 MiB default heaps packs up to 4 096 textures into one heap with zero tile mappings, +//! shares the exact `(heap, offset)` across repeated placements at the same `(va, size)` so +//! same-address aliases see each other's uploads, and recycles released blocks by size class +//! after `IDLE` + `COOL`. `XCL_VRAM_SMALL=0` disables this path. + +use std::collections::{BTreeMap, HashMap, VecDeque}; +use std::ffi::c_void; +use std::sync::Mutex; +use std::time::{Duration, Instant}; + +use windows::core::Interface; +use windows::Win32::Graphics::Direct3D12::*; + +use crate::util::HRESULT; + +/// Allocation unit: 4 KiB (`D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT`). +const UNIT: usize = 0x1000; +/// Largest small-resource size handled here: 60 KiB (`15 * 4 KiB < 64 KiB`). +const MAX_UNITS: u32 = 15; +/// Units per shared default heap: 16 MiB (`4096 * 4 KiB`). +const POOL_UNITS: u32 = 4096; +/// Keep an unreferenced `(va, units)` slot this long so immediate re-placements reuse it. +const IDLE: Duration = Duration::from_secs(2); +/// Wait before reusing a freed block for a different address (in-flight GPU work). +const COOL: Duration = Duration::from_secs(2); + +/// Key identifying a shared small-texture backing slot: `(title_va, size_in_4k_units)`. +type SlotKey = (usize, u32); + +/// Block location inside a device's small pools: `(pool_index, offset_in_4k_units, size_in_4k_units)`. +type Block = (u32, u32, u32); + +struct Pool { + heap: H, + /// Bump pointer in 4 KiB units (`0..=POOL_UNITS`). + next_unit: u32, +} + +struct Slot { + pool: u32, + offset: u32, + users: u32, + idle_since: Option, +} + +struct SmallPools { + pools: Vec>, + /// Per-size-class (`1..=MAX_UNITS`) reusable blocks `(pool, offset)`. + free: [Vec<(u32, u32)>; (MAX_UNITS as usize) + 1], + /// Blocks given back, reusable after `COOL`. + cooling: VecDeque<(Block, Instant)>, + /// Live or idle-kept `(va, units)` → backing block. + slots: HashMap, + /// Idle slots, oldest first (`sweep_idle`). + idle: VecDeque<(SlotKey, Instant)>, + /// Live resource raw pointer → `(va, units)`. + resources: HashMap, + /// Total 4 KiB units currently held by `slots` (live + idle-kept). + used_units: usize, +} + +impl SmallPools { + fn new() -> Self { + SmallPools { + pools: Vec::new(), + free: std::array::from_fn(|_| Vec::new()), + cooling: VecDeque::new(), + slots: HashMap::new(), + idle: VecDeque::new(), + resources: HashMap::new(), + used_units: 0, + } + } + + fn sweep_idle(&mut self, now: Instant) { + while self.idle.front().is_some_and(|&(_, since)| now.duration_since(since) >= IDLE) { + let Some((key, since)) = self.idle.pop_front() else { break }; + if self.slots.get(&key).is_some_and(|s| s.idle_since == Some(since)) { + if let Some(s) = self.slots.remove(&key) { + self.used_units -= key.1 as usize; + self.cooling.push_back(((s.pool, s.offset, key.1), now)); + } + } + } + } + + fn drain_cooling(&mut self, now: Instant) { + while self.cooling.front().is_some_and(|&(_, at)| now.duration_since(at) >= COOL) { + if let Some(((pool, offset, units), _)) = self.cooling.pop_front() { + self.free[units as usize].push((pool, offset)); + } + } + } + + /// Try to acquire an existing shared slot for `(va, units)` or allocate a block from existing + /// pools / free lists without creating a new D3D12 heap. + fn try_acquire(&mut self, key: SlotKey, now: Instant) -> Option<(H, u32)> { + self.sweep_idle(now); + if let Some(s) = self.slots.get_mut(&key) { + s.users += 1; + s.idle_since = None; + return Some((self.pools[s.pool as usize].heap.clone(), s.offset)); + } + let units = key.1; + debug_assert!((1..=MAX_UNITS).contains(&units)); + self.drain_cooling(now); + let (pool, offset) = if let Some(b) = self.free[units as usize].pop() { + b + } else if let Some((idx, p)) = self.pools.iter_mut().enumerate().rev().find(|(_, p)| p.next_unit + units <= POOL_UNITS) { + let off = p.next_unit; + p.next_unit += units; + (idx as u32, off) + } else { + return None; + }; + let heap = self.pools[pool as usize].heap.clone(); + self.slots.insert(key, Slot { pool, offset, users: 1, idle_since: None }); + self.used_units += units as usize; + Some((heap, offset)) + } + + /// Register a newly created pool heap and acquire `(va, units)` (re-checking whether another + /// thread raced and already created the same slot while the heap was being made). + fn add_pool_and_acquire(&mut self, heap: H, key: SlotKey, now: Instant) -> (H, u32) { + let pool_idx = self.pools.len() as u32; + self.pools.push(Pool { heap, next_unit: 0 }); + if let Some(got) = self.try_acquire(key, now) { + return got; + } + // `pool_idx` has `next_unit == 0` and `units <= MAX_UNITS < POOL_UNITS`, so `try_acquire` + // above always succeeds. + unreachable!("fresh pool {pool_idx} must fit {} units", key.1); + } + + fn note_resource(&mut self, res: usize, key: SlotKey) { + self.resources.insert(res, key); + } + + /// Release one user of `key`. When `rollback` is true (creation failed before the GPU ever saw + /// the resource), the block returns directly to `free` if this was its only user. + fn release_key(&mut self, key: SlotKey, now: Instant, rollback: bool) { + let Some(s) = self.slots.get_mut(&key) else { return }; + s.users = s.users.saturating_sub(1); + if s.users == 0 { + if rollback { + if let Some(s) = self.slots.remove(&key) { + self.used_units -= key.1 as usize; + self.free[key.1 as usize].push((s.pool, s.offset)); + } + } else { + s.idle_since = Some(now); + self.idle.push_back((key, now)); + } + } + } + + fn forget(&mut self, res: usize, now: Instant) -> bool { + let Some(key) = self.resources.remove(&res) else { + return false; + }; + self.release_key(key, now, false); + true + } + + fn bytes(&self) -> (usize, usize) { + (self.used_units * UNIT, self.pools.len() * POOL_UNITS as usize * UNIT) + } +} + +// D3D12 heaps are free-threaded. +unsafe impl Send for SmallPools {} + +static SMALL_POOLS: Mutex>> = Mutex::new(BTreeMap::new()); + +fn enabled() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| std::env::var("XCL_VRAM_SMALL").map_or(true, |v| v.trim() != "0")) +} + +/// Whether `desc` and `console_bytes` are eligible for 4 KiB small-resource sub-allocation. +fn candidate(desc: &D3D12_RESOURCE_DESC, console_bytes: Option) -> bool { + const PAGE: u64 = 0x1_0000; + desc.Layout == D3D12_TEXTURE_LAYOUT_UNKNOWN + && desc.SampleDesc.Count <= 1 + && desc.Flags.0 & 0x7 == 0 + && matches!(desc.Dimension, D3D12_RESOURCE_DIMENSION_TEXTURE2D | D3D12_RESOURCE_DIMENSION_TEXTURE3D) + && console_bytes.is_some_and(|b| b > 0 && b < PAGE) +} + +/// Place a sub-64 KiB sampled texture at 4 KiB granularity in a shared default heap. +/// Returns `None` if the texture is not a small resource (the caller falls back to `vram::Pages` +/// or a committed resource). +#[allow(clippy::too_many_arguments)] +pub(super) unsafe fn place( + dev: &ID3D12Device, + this: *mut c_void, + va: usize, + desc: &D3D12_RESOURCE_DESC, + state: u32, + clear: *const c_void, + console_bytes: Option, + riid: *const windows_sys::core::GUID, + ppv: *mut *mut c_void, +) -> Option { + if !enabled() || !candidate(desc, console_bytes) { + return None; + } + let small_desc = D3D12_RESOURCE_DESC { Alignment: D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT as u64, ..*desc }; + let info = dev.GetResourceAllocationInfo(0, &[small_desc]); + if info.SizeInBytes == u64::MAX || info.SizeInBytes == 0 || info.Alignment != UNIT as u64 || info.SizeInBytes >= 0x1_0000 { + return None; + } + let units = (info.SizeInBytes as usize).div_ceil(UNIT) as u32; + if !(1..=MAX_UNITS).contains(&units) { + return None; + } + let dev_key = dev.as_raw() as usize; + let key = (va, units); + let now = Instant::now(); + + let (heap, offset_units) = { + let mut g = SMALL_POOLS.lock().unwrap(); + let pools = g.entry(dev_key).or_insert_with(SmallPools::new); + pools.try_acquire(key, now) + } + .or_else(|| { + // Create a new 16 MiB default heap outside `SMALL_POOLS.lock()`. + let heap_desc = D3D12_HEAP_DESC { + SizeInBytes: POOL_UNITS as u64 * UNIT as u64, + Properties: D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, + // 64 KiB heap alignment is valid for 4 KiB-aligned small resources at any 4 KiB offset. + Alignment: D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT as u64, + Flags: D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES, + }; + let mut new_heap: Option = None; + if let Err(e) = super::title_textures::own_work(|| dev.CreateHeap(&heap_desc, &mut new_heap)) { + crate::xlog_warn!("D3D12.X: small texture pool CreateHeap: {e}"); + return None; + } + let new_heap = new_heap?; + let mut g = SMALL_POOLS.lock().unwrap(); + let pools = g.entry(dev_key).or_insert_with(SmallPools::new); + Some(pools.add_pool_and_acquire(new_heap, key, Instant::now())) + })?; + + let hr = super::real::dev_create_placed_resource( + this, + heap.as_raw() as usize, + offset_units as usize * UNIT, + &small_desc as *const _ as usize, + state as usize, + clear as usize, + riid, + ppv, + ); + if hr < 0 || (*ppv).is_null() { + let mut g = SMALL_POOLS.lock().unwrap(); + if let Some(pools) = g.get_mut(&dev_key) { + pools.release_key(key, Instant::now(), true); + } + return None; + } + let raw = *ppv as usize; + SMALL_POOLS.lock().unwrap().entry(dev_key).or_insert_with(SmallPools::new).note_resource(raw, key); + Some(hr) +} + +/// Release a small-pool texture on COM release. Returns `true` if `resource` belonged to a small +/// pool (so `vram::forget` can skip locking `PAGES`). +pub(super) fn forget(resource: usize) -> bool { + let mut g = SMALL_POOLS.lock().unwrap(); + let now = Instant::now(); + for pools in g.values_mut() { + if pools.forget(resource, now) { + return true; + } + } + false +} + +/// `(used_bytes, pool_bytes)` across all devices. +pub(super) fn bytes() -> (usize, usize) { + let g = SMALL_POOLS.lock().unwrap(); + g.values().fold((0, 0), |(used, pools), p| { + let (u, t) = p.bytes(); + (used + u, pools + t) + }) +} + +#[cfg(test)] +pub(super) fn bytes_for(device: usize) -> (usize, usize) { + let g = SMALL_POOLS.lock().unwrap(); + g.get(&device).map_or((0, 0), SmallPools::bytes) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn small_pool_packs_shares_and_recycles_by_size_class() { + let t0 = Instant::now(); + let at = |ms: u64| t0 + Duration::from_millis(ms); + let mut p: SmallPools = SmallPools::new(); + let alloc = |p: &mut SmallPools, va: usize, units: u32, res: usize, now: Instant| -> (u32, u32) { + let got = p.try_acquire((va, units), now).unwrap_or_else(|| { + let id = p.pools.len() as u32; + p.add_pool_and_acquire(id, (va, units), now) + }); + p.note_resource(res, (va, units)); + got + }; + + // Sixteen 4 KiB textures pack into 64 KiB (not 16 * 64 KiB = 1 MiB). + for i in 0..16usize { + let (h, off) = alloc(&mut p, 0x10_0000 + i * UNIT, 1, 100 + i, at(0)); + assert_eq!((h, off), (0, i as u32)); + } + assert_eq!(p.bytes(), (16 * UNIT, POOL_UNITS as usize * UNIT)); + + // Re-placing the exact same (va, units) shares the exact same (heap, offset) with zero growth. + let (h_dup, off_dup) = alloc(&mut p, 0x10_4000, 1, 200, at(10)); + assert_eq!((h_dup, off_dup), (0, 4)); + assert_eq!(p.bytes().0, 16 * UNIT); + + // Releasing one of the two aliases keeps the slot alive; releasing both makes it idle. + assert!(p.forget(200, at(20))); + assert_eq!(p.slots[&(0x10_4000, 1)].users, 1); + assert!(p.forget(104, at(30))); + assert_eq!(p.slots[&(0x10_4000, 1)].users, 0); + + // Re-acquiring within IDLE reuses the exact same slot. + let (h_re, off_re) = alloc(&mut p, 0x10_4000, 1, 201, at(1000)); + assert_eq!((h_re, off_re), (0, 4)); + assert!(p.forget(201, at(1100))); + + // After IDLE (2 s) + COOL (2 s), the 4 KiB block is recycled for a new address. + let (h_wait, off_wait) = alloc(&mut p, 0x20_0000, 1, 202, at(3200)); + assert_eq!((h_wait, off_wait), (0, 16), "still cooling at +2.1s, must not reuse yet"); + let (h_rec, off_rec) = alloc(&mut p, 0x20_1000, 1, 203, at(5300)); + assert_eq!((h_rec, off_rec), (0, 4), "block (0, 4) recycled after IDLE + COOL"); + + // Rollback on creation failure immediately returns the block to the free list. + let (h_fail, off_fail) = p.try_acquire((0x30_0000, 4), at(5400)).unwrap(); + assert_eq!((h_fail, off_fail), (0, 17)); + p.release_key((0x30_0000, 4), at(5400), true); + let (h_next, off_next) = alloc(&mut p, 0x30_4000, 4, 204, at(5400)); + assert_eq!((h_next, off_next), (0, 17)); + } + + #[test] + fn candidate_filters_large_gpu_owned_and_msaa_textures() { + let mut d = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: 64, + Height: 64, + DepthOrArraySize: 1, + MipLevels: 1, + SampleDesc: windows::Win32::Graphics::Dxgi::Common::DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Layout: D3D12_TEXTURE_LAYOUT_UNKNOWN, + Flags: D3D12_RESOURCE_FLAG_NONE, + ..Default::default() + }; + assert!(candidate(&d, Some(16 * 1024))); + assert!(!candidate(&d, Some(64 * 1024)), "64 KiB stays on vram::Pages"); + assert!(!candidate(&d, None)); + assert!(!candidate(&d, Some(0))); + d.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + assert!(!candidate(&d, Some(16 * 1024))); + d.Flags = D3D12_RESOURCE_FLAG_NONE; + d.SampleDesc.Count = 4; + assert!(!candidate(&d, Some(16 * 1024))); + d.SampleDesc.Count = 1; + d.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + assert!(!candidate(&d, Some(16 * 1024))); + } + + /// On the GPU: 512 small 4 KiB (64x64 BC1) and 16 KiB (64x64 RGBA8) sampled textures pack at + /// 4 KiB granularity inside a single 16 MiB heap (16x and 4x smaller than 64 KiB tiles), + /// same-address placements alias their pixels on hardware, adjacent 4 KiB textures have zero + /// crosstalk, and a small sampled texture over an existing 64 KiB GPU target falls back to + /// `vram::Pages` so target aliasing is preserved. + #[test] + fn gpu_small_textures_pack_at_4k_granularity_and_preserve_pixels() { + let _vram_guard = super::super::vram::VRAM_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::Common::*; + use windows::Win32::Graphics::Dxgi::*; + unsafe { + let factory: IDXGIFactory4 = CreateDXGIFactory1().expect("DXGI factory"); + let Ok(adapter) = factory.EnumAdapters1(0) else { return }; + if adapter.GetDesc1().is_ok_and(|d| d.Flags & DXGI_ADAPTER_FLAG_SOFTWARE.0 as u32 != 0) { + return; + } + let mut dev: Option = None; + let _ = D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_11_0, &mut dev); + let Some(dev) = dev else { return }; + super::super::real::adopt(dev.as_raw(), super::super::real::Kind::Device); + let dev_key = dev.as_raw() as usize; + let riid = &ID3D12Resource::IID as *const windows::core::GUID as *const windows_sys::core::GUID; + + let desc_of = |w: u64, h: u32, fmt: DXGI_FORMAT, flags: D3D12_RESOURCE_FLAGS| D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: w, + Height: h, + DepthOrArraySize: 1, + MipLevels: 1, + Format: fmt, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Layout: D3D12_TEXTURE_LAYOUT_UNKNOWN, + Flags: flags, + ..Default::default() + }; + let place_sampled = |va: usize, d: &D3D12_RESOURCE_DESC, cbytes: u64| -> ID3D12Resource { + let mut p = std::ptr::null_mut(); + let hr = super::super::vram::place_pages(dev.as_raw(), va, d, D3D12_RESOURCE_STATE_COMMON.0 as u32, std::ptr::null(), Some(cbytes), riid, &mut p); + assert!(hr.is_some_and(|h| h >= 0) && !p.is_null(), "place_pages at {va:#x} failed: {hr:x?}"); + assert!(super::super::vram::is_placed_texture(p as usize)); + ID3D12Resource::from_raw(p) + }; + + // 1. 512 x 64x64 BC1 (2 KiB on console -> 4 KiB small resource on PC): must pack into + // 2 MiB (16x smaller than 512 x 64 KiB = 32 MiB!) inside a single 16 MiB heap. + let bc1 = desc_of(64, 64, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_NONE); + let (small0, _) = bytes_for(dev_key); + let (pages0, _) = super::super::vram::page_bytes_for(dev_key); + let bc1_base = 0x60_0000_0000usize; + let mut bc1_res = Vec::with_capacity(512); + for k in 0..512usize { + bc1_res.push(place_sampled(bc1_base + k * 0x1000, &bc1, 2048)); + } + let (small_after_bc1, small_pool_after_bc1) = bytes_for(dev_key); + let bc1_small_delta = small_after_bc1 - small0; + let bc1_total_delta = super::super::vram::page_bytes_for(dev_key).0 - pages0; + println!("512 x 64x64 BC1: +{bc1_small_delta} B in small pools (pool={small_pool_after_bc1} B), +{bc1_total_delta} B total (vs 33554432 B as 64 KiB tiles)"); + assert_eq!(bc1_small_delta, 512 * UNIT, "each 64x64 BC1 must take 4 KiB in small pool"); + assert_eq!(bc1_total_delta, 512 * UNIT, "zero 64 KiB Pages tiles allocated for 4 KiB BC1 textures"); + drop(bc1_res); + + // 2. Hardware upload + readback: two adjacent 64x64 RGBA8 (16 KiB) textures and a + // same-address alias verify zero crosstalk and exact pixel sharing on the GPU. + let rgba = desc_of(64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE); + let va_a = 0x61_0000_1000usize; + let va_b = 0x61_0000_5000usize; + let a1 = place_sampled(va_a, &rgba, 16384); + let (small_after_a1, _) = bytes_for(dev_key); + let a2 = place_sampled(va_a, &rgba, 16384); + assert_eq!(bytes_for(dev_key).0, small_after_a1, "same-va small texture must share slot"); + let b = place_sampled(va_b, &rgba, 16384); + assert_eq!(bytes_for(dev_key).0 - small_after_a1, 16384, "64x64 RGBA8 must take 16 KiB"); + + let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC::default()).unwrap(); + let fill_a = vec![0x3Cu8; 64 * 64 * 4]; + let fill_b = vec![0xA5u8; 64 * 64 * 4]; + let sub = |data: Vec| crate::gpu::texture::Subresource { rows: 64, row_bytes: 64 * 4, depth: 1, data }; + let batch = vec![ + (a2.clone(), vec![sub(fill_a.clone())], vec![D3D12_RESOURCE_STATE_COMMON]), + (b.clone(), vec![sub(fill_b.clone())], vec![D3D12_RESOURCE_STATE_COMMON]), + ]; + let failed = crate::gpu::texture::upload_batch(&dev, &queue, &batch).unwrap(); + assert!(failed.is_empty(), "upload failed: {failed:?}"); + + let readback_tex = |r: &ID3D12Resource| -> Vec { + let rd = r.GetDesc(); + let mut fp = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); + let (mut rows, mut row_bytes, mut total) = (0u32, 0u64, 0u64); + dev.GetCopyableFootprints(&rd, 0, 1, 0, Some(&mut fp), Some(&mut rows), Some(&mut row_bytes), Some(&mut total)); + let mut rb: Option = None; + dev.CreateCommittedResource( + &D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_READBACK, ..Default::default() }, + D3D12_HEAP_FLAG_NONE, + &D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, Width: total, Height: 1, DepthOrArraySize: 1, MipLevels: 1, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, ..Default::default() + }, + D3D12_RESOURCE_STATE_COPY_DEST, None, &mut rb, + ).unwrap(); + let rb = rb.unwrap(); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap(); + let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap(); + let dst = D3D12_TEXTURE_COPY_LOCATION { pResource: std::mem::transmute_copy(&rb), Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { PlacedFootprint: fp } }; + let src = D3D12_TEXTURE_COPY_LOCATION { pResource: std::mem::transmute_copy(r), Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: 0 } }; + list.CopyTextureRegion(&dst, 0, 0, 0, &src, None); + list.Close().unwrap(); + queue.ExecuteCommandLists(&[Some(list.cast().unwrap())]); + crate::gpu::texture::wait(&dev, &queue).unwrap(); + let mut p = std::ptr::null_mut(); + rb.Map(0, None, Some(&mut p)).unwrap(); + let mut out = Vec::with_capacity(rows as usize * row_bytes as usize); + for y in 0..rows as usize { + out.extend_from_slice(std::slice::from_raw_parts((p as *const u8).add(y * fp.Footprint.RowPitch as usize), row_bytes as usize)); + } + rb.Unmap(0, None); + out + }; + + assert_eq!(readback_tex(&a1), fill_a, "same-va alias a1 must see pixels uploaded to a2"); + assert_eq!(readback_tex(&b), fill_b, "adjacent 4 KiB texture b must not crosstalk with a"); + drop((b, a2, a1)); + + // 3. A small sampled texture at a 64 KiB-aligned va occupied by an existing GPU-owned + // render target must stay on vram::Pages (not vram_small) so it aliases the target. + let rt_va = 0x62_0000_0000usize; + let rt_desc = desc_of(64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET); + let mut rt_raw = std::ptr::null_mut(); + let hr = super::super::vram::place(dev.as_raw(), rt_va, &rt_desc, D3D12_RESOURCE_STATE_RENDER_TARGET.0 as u32, std::ptr::null(), Some(16384), riid, &mut rt_raw); + assert!(hr.is_some_and(|h| h >= 0) && !rt_raw.is_null()); + let rt = ID3D12Resource::from_raw(rt_raw); + let small_before_alias = bytes_for(dev_key).0; + let sampled_view = place_sampled(rt_va, &rgba, 16384); + assert_eq!(bytes_for(dev_key).0, small_before_alias, "sampled view over live GPU target must alias vram::Pages, not vram_small"); + drop((sampled_view, rt)); + } + } +} From ed7443e6c7f7c9c4a2a1e91c2b4205d7f0df398f Mon Sep 17 00:00:00 2001 From: raylitebull <287713799+raylitebull@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:04:04 +0100 Subject: [PATCH 2/5] D3D12.X: guard concurrent small-pool creation race and honor XCL_ADAPTER in small-texture test --- crates/xcl/src/hle/d3d12x/vram_small.rs | 29 ++++++++++++++++++------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/vram_small.rs b/crates/xcl/src/hle/d3d12x/vram_small.rs index 3a1daf8b45..26a0fb1ac5 100644 --- a/crates/xcl/src/hle/d3d12x/vram_small.rs +++ b/crates/xcl/src/hle/d3d12x/vram_small.rs @@ -129,9 +129,14 @@ impl SmallPools { Some((heap, offset)) } - /// Register a newly created pool heap and acquire `(va, units)` (re-checking whether another - /// thread raced and already created the same slot while the heap was being made). + /// Register a newly created pool heap and acquire `(va, units)`. + /// Re-checks `try_acquire` first under the lock so two threads racing to create a new 16 MiB + /// pool do not push a redundant empty heap (the extra `heap` drops cleanly if another thread + /// already added room or created the same slot). fn add_pool_and_acquire(&mut self, heap: H, key: SlotKey, now: Instant) -> (H, u32) { + if let Some(got) = self.try_acquire(key, now) { + return got; + } let pool_idx = self.pools.len() as u32; self.pools.push(Pool { heap, next_unit: 0 }); if let Some(got) = self.try_acquire(key, now) { @@ -356,6 +361,13 @@ mod tests { p.release_key((0x30_0000, 4), at(5400), true); let (h_next, off_next) = alloc(&mut p, 0x30_4000, 4, 204, at(5400)); assert_eq!((h_next, off_next), (0, 17)); + + // Concurrent CreateHeap race: if room already exists in pool 0, `add_pool_and_acquire` + // must reuse existing room instead of pushing a redundant empty 16 MiB pool. + let pools_before = p.pools.len(); + let (h_raced, _) = p.add_pool_and_acquire(999, (0x40_0000, 1), at(5500)); + assert_eq!(h_raced, 0); + assert_eq!(p.pools.len(), pools_before, "must not push redundant pool when room already exists"); } #[test] @@ -397,13 +409,14 @@ mod tests { use windows::Win32::Graphics::Dxgi::Common::*; use windows::Win32::Graphics::Dxgi::*; unsafe { - let factory: IDXGIFactory4 = CreateDXGIFactory1().expect("DXGI factory"); - let Ok(adapter) = factory.EnumAdapters1(0) else { return }; - if adapter.GetDesc1().is_ok_and(|d| d.Flags & DXGI_ADAPTER_FLAG_SOFTWARE.0 as u32 != 0) { - return; - } + let factory: IDXGIFactory6 = CreateDXGIFactory1().expect("DXGI factory"); + let sel = crate::gpu::adapter::Selector::from_env_or("auto"); let mut dev: Option = None; - let _ = D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_11_0, &mut dev); + for (adapter, _) in crate::gpu::adapter::candidates(&factory, &sel) { + if D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_11_0, &mut dev).is_ok() && dev.is_some() { + break; + } + } let Some(dev) = dev else { return }; super::super::real::adopt(dev.as_raw(), super::super::real::Kind::Device); let dev_key = dev.as_raw() as usize; From 9e4c7c575511bab3ab5dd20b3d081a3564633fed Mon Sep 17 00:00:00 2001 From: raylitebull <287713799+raylitebull@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:20:57 +0100 Subject: [PATCH 3/5] D3D12.X: fix extra_pages reclamation check in placed_title_textures test --- crates/xcl/src/hle/d3d12x/title_textures.rs | 29 ++++++++++++++++----- crates/xcl/src/hle/d3d12x/vram.rs | 6 +++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 402cdbd700..2b58967020 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -1957,6 +1957,15 @@ mod tests { assert!(tbl.by_res.contains_key(&10) && tbl.by_res.contains_key(&11)); assert_eq!(tbl.insert(12, entry(0x20_0000, &tex_64k, true, false)), (-1, vec![])); assert!(!tbl.by_res.contains_key(&10) && !tbl.by_res.contains_key(&11) && tbl.by_res.contains_key(&12)); + // Unaligned 88 KiB mip-chain siblings at +0x4000 own page +0x1_0000 plus 1 extra_page; + // an aligned 64 KiB cover at +0x1_0000 supersedes both without allocating a new base page. + let tex_88k = Arc::new(xg::Texture::new(&console_desc(3, 128, 128, 1, 8, 28, 0x105)).unwrap()); + assert_eq!(tex_88k.size(), 0x1_6000); + assert_eq!(tbl.insert(30, entry(0x30_4000, &tex_88k, true, true)), (1, vec![])); + assert_eq!(tbl.insert(31, entry(0x30_4000, &tex_88k, true, true)), (0, vec![])); + let (_, mut unmap_unaligned) = tbl.insert(32, entry(0x31_0000, &tex_64k, true, true)); + unmap_unaligned.sort_unstable(); + assert_eq!(unmap_unaligned, vec![30, 31]); } /// Real GPU (#248): placed title textures share `vram::Pages` so streaming unreleased @@ -2185,17 +2194,25 @@ mod tests { drop((reused, adj)); // Reclaiming dead extra_pages immediately when a >= 64 KiB unaligned tiled texture is - // overwritten by a larger placement without Release: - let d64_mips = console_desc(3, 128, 128, 1, 8, 28, 0x105); // 2 tiles (mip 0 + packed mips) - let d128 = console_desc(3, 256, 128, 1, 1, 28, 0x109); // 128 KiB, 2 tiles - let anchor = place_tex(base, &d64); + // overwritten by an overlapping placement without Release: + // `unaligned_a`/`b` at `base + 0x4000` (88 KiB, 2 tiles) own page `base + 0x1_0000` + // plus 1 `extra_pages` tile; `cover` at `base + 0x1_0000` reuses that already-live + // owned page (+0 new pages) while superseding both unaligned siblings and reclaiming + // their `extra_pages` tile immediately (-64 KiB). + let d64_mips = console_desc(3, 128, 128, 1, 8, 28, 0x105); // 88 KiB, 2 tiles (mip 0 + packed mips) + let extra_before = super::super::vram::extra_page_bytes_for(dev_key); let unaligned_a = place_tex(base + 0x4000, &d64_mips); + let extra_after_a = super::super::vram::extra_page_bytes_for(dev_key); + assert!(extra_after_a > extra_before, "unaligned >= 64 KiB tiled texture must allocate extra_pages ({extra_before} -> {extra_after_a})"); let unaligned_b = place_tex(base + 0x4000, &d64_mips); + assert_eq!(super::super::vram::extra_page_bytes_for(dev_key), extra_after_a, "reusing unaligned va must share extra_pages tile"); let pages_with_extra = dev_pages(); - let cover = place_tex(base, &d128); + let cover = place_tex(base + 0x1_0000, &d64); let pages_after_cover = dev_pages(); + let extra_after_cover = super::super::vram::extra_page_bytes_for(dev_key); + assert_eq!(extra_after_cover, extra_before, "superseded unaligned extra_pages must drop back to baseline ({extra_after_a} -> {extra_after_cover})"); assert!(pages_after_cover < pages_with_extra, "superseded unaligned extra_pages must be reclaimed immediately ({pages_with_extra} -> {pages_after_cover})"); - drop((cover, unaligned_b, unaligned_a, anchor)); + drop((cover, unaligned_b, unaligned_a)); // 4. Full mip chain (standard + packed mips, RGBA8 & BC7), tiny all-packed mips, // 6-slice cubemap (exercises extra_pages reuse), and 3D volume texture (Tier 3). diff --git a/crates/xcl/src/hle/d3d12x/vram.rs b/crates/xcl/src/hle/d3d12x/vram.rs index eb16b7e063..414b67d781 100644 --- a/crates/xcl/src/hle/d3d12x/vram.rs +++ b/crates/xcl/src/hle/d3d12x/vram.rs @@ -1227,6 +1227,12 @@ pub(super) fn page_bytes_for(device: usize) -> (usize, usize) { ((p.pages.len() + p.extra_pages.len()) * PAGE + su, p.pools.len() * POOL_PAGES as usize * PAGE + sp) } +#[cfg(test)] +pub(super) fn extra_page_bytes_for(device: usize) -> usize { + let g = PAGES.lock().unwrap(); + g.get(&device).and_then(|p| p.as_ref()).map_or(0, |p| p.extra_pages.len() * PAGE) +} + /// (bytes of pages in use by tiled textures, bytes of pool heaps) – `XCL_ACTIVITY` pub fn page_bytes() -> (usize, usize) { let (su, sp) = super::vram_small::bytes(); From 5c58caa24ab92cc69a049f8ef9601d3bcd196809 Mon Sep 17 00:00:00 2001 From: raylitebull <287713799+raylitebull@users.noreply.github.com> Date: Wed, 25 Nov 2026 02:16:10 +0100 Subject: [PATCH 4/5] D3D12.X: flush idle VRAM pages/small-pool slots between GPU tests and ignore expired idle pages --- crates/xcl/src/hle/d3d12x/title_textures.rs | 4 ++ crates/xcl/src/hle/d3d12x/vram.rs | 43 +++++++++++++++++---- crates/xcl/src/hle/d3d12x/vram_small.rs | 37 +++++++++++++++++- 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 2b58967020..8747ed7d82 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -2102,6 +2102,7 @@ mod tests { // 1. Streaming 512 unreleased 1 MiB textures (512 MiB placed) across 16 MiB stays bounded // with zero additional page growth after the 16 MiB working set is covered. let dev_key = dev.as_raw() as usize; + super::super::vram::flush_idle_for(dev_key); let dev_pages = || super::super::vram::page_bytes_for(dev_key).0; let stream_desc = console_desc(3, 512, 512, 1, 1, 28, 0x109); // 64KB_S, 1 MiB let (pages0, vram0, kept0) = (dev_pages(), local_vram(), counts().0); @@ -2174,6 +2175,7 @@ mod tests { // 3. 4 KiB-aligned adjacent small textures pack into 4 KiB sub-allocated slots (no 64 KiB // bloat per small texture), reuse the exact slot at the same va without crosstalk, and // >= 64 KiB unaligned textures reclaim dead extra_pages immediately when overwritten. + super::super::vram::flush_idle_for(dev_key); let d16 = console_desc(3, 64, 64, 1, 1, 28, 0x105); // 4KB_S, 16 KiB let d64 = console_desc(3, 128, 128, 1, 1, 28, 0x105); // 4KB_S, 64 KiB let adj_base_pages = dev_pages(); @@ -2199,6 +2201,7 @@ mod tests { // plus 1 `extra_pages` tile; `cover` at `base + 0x1_0000` reuses that already-live // owned page (+0 new pages) while superseding both unaligned siblings and reclaiming // their `extra_pages` tile immediately (-64 KiB). + super::super::vram::flush_idle_for(dev_key); let d64_mips = console_desc(3, 128, 128, 1, 8, 28, 0x105); // 88 KiB, 2 tiles (mip 0 + packed mips) let extra_before = super::super::vram::extra_page_bytes_for(dev_key); let unaligned_a = place_tex(base + 0x4000, &d64_mips); @@ -2216,6 +2219,7 @@ mod tests { // 4. Full mip chain (standard + packed mips, RGBA8 & BC7), tiny all-packed mips, // 6-slice cubemap (exercises extra_pages reuse), and 3D volume texture (Tier 3). + super::super::vram::flush_idle_for(dev_key); let shapes = [ ("2D 512x512 RGBA8 10 mips", console_desc(3, 512, 512, 1, 10, 28, 0x109)), ("2D 1024x1024 BC7 11 mips", console_desc(3, 1024, 1024, 1, 11, 98, 0x109)), diff --git a/crates/xcl/src/hle/d3d12x/vram.rs b/crates/xcl/src/hle/d3d12x/vram.rs index 414b67d781..73e6fd68ea 100644 --- a/crates/xcl/src/hle/d3d12x/vram.rs +++ b/crates/xcl/src/hle/d3d12x/vram.rs @@ -1212,25 +1212,54 @@ pub(super) unsafe fn before_execute(queue: &ID3D12CommandQueue) { #[cfg(test)] pub(super) static VRAM_TEST_LOCK: Mutex<()> = Mutex::new(()); -/// Whether `va` already has a live 64 KiB page on `device` (e.g., an existing GPU-owned render -/// target that a sampled view at the same 64 KiB-aligned address should alias). +/// Whether `va` already has a live (or still within `IDLE`) 64 KiB page on `device` (e.g., an +/// existing GPU-owned render target that a sampled view at the same 64 KiB-aligned address should +/// alias). fn has_live_page(device: usize, va: usize) -> bool { let g = PAGES.lock().unwrap(); - g.get(&device).and_then(|p| p.as_ref()).is_some_and(|p| p.pages.contains_key(&va)) + let now = std::time::Instant::now(); + g.get(&device) + .and_then(|p| p.as_ref()) + .is_some_and(|p| p.pages.get(&va).is_some_and(|pg| pg.idle_since.is_none_or(|s| now.duration_since(s) < IDLE))) } #[cfg(test)] pub(super) fn page_bytes_for(device: usize) -> (usize, usize) { let (su, sp) = super::vram_small::bytes_for(device); - let g = PAGES.lock().unwrap(); - let Some(Some(p)) = g.get(&device) else { return (su, sp) }; + let mut g = PAGES.lock().unwrap(); + let Some(Some(p)) = g.get_mut(&device) else { return (su, sp) }; + p.sweep(std::time::Instant::now()); ((p.pages.len() + p.extra_pages.len()) * PAGE + su, p.pools.len() * POOL_PAGES as usize * PAGE + sp) } #[cfg(test)] pub(super) fn extra_page_bytes_for(device: usize) -> usize { - let g = PAGES.lock().unwrap(); - g.get(&device).and_then(|p| p.as_ref()).map_or(0, |p| p.extra_pages.len() * PAGE) + let mut g = PAGES.lock().unwrap(); + let Some(Some(p)) = g.get_mut(&device) else { return 0 }; + p.sweep(std::time::Instant::now()); + p.extra_pages.len() * PAGE +} + +/// Immediately evict all unreferenced idle pages and small-pool slots on `device` so GPU tests +/// sharing the process's singleton `ID3D12Device` do not observe stale `IDLE` expiries mid-test. +#[cfg(test)] +pub(super) fn flush_idle_for(device: usize) { + super::vram_small::flush_idle_for(device); + let mut g = PAGES.lock().unwrap(); + if let Some(Some(p)) = g.get_mut(&device) { + let now = std::time::Instant::now(); + while let Some((key, since)) = p.idle.pop_front() { + let still_idle = |pg: &Page| pg.idle_since == Some(since); + let page = match key { + PageKey::Own(a) if p.pages.get(&a).is_some_and(still_idle) => p.pages.remove(&a), + PageKey::Extra(va, i) if p.extra_pages.get(&(va, i)).is_some_and(still_idle) => p.extra_pages.remove(&(va, i)), + _ => None, + }; + if let Some(pg) = page { + p.pool.push_cooling(pg.tile, now); + } + } + } } /// (bytes of pages in use by tiled textures, bytes of pool heaps) – `XCL_ACTIVITY` diff --git a/crates/xcl/src/hle/d3d12x/vram_small.rs b/crates/xcl/src/hle/d3d12x/vram_small.rs index 26a0fb1ac5..c77428d04a 100644 --- a/crates/xcl/src/hle/d3d12x/vram_small.rs +++ b/crates/xcl/src/hle/d3d12x/vram_small.rs @@ -180,6 +180,18 @@ impl SmallPools { fn bytes(&self) -> (usize, usize) { (self.used_units * UNIT, self.pools.len() * POOL_UNITS as usize * UNIT) } + + #[cfg(test)] + fn flush_idle(&mut self, now: Instant) { + while let Some((key, since)) = self.idle.pop_front() { + if self.slots.get(&key).is_some_and(|s| s.idle_since == Some(since)) { + if let Some(s) = self.slots.remove(&key) { + self.used_units -= key.1 as usize; + self.cooling.push_back(((s.pool, s.offset, key.1), now)); + } + } + } + } } // D3D12 heaps are free-threaded. @@ -304,8 +316,18 @@ pub(super) fn bytes() -> (usize, usize) { #[cfg(test)] pub(super) fn bytes_for(device: usize) -> (usize, usize) { - let g = SMALL_POOLS.lock().unwrap(); - g.get(&device).map_or((0, 0), SmallPools::bytes) + let mut g = SMALL_POOLS.lock().unwrap(); + let Some(p) = g.get_mut(&device) else { return (0, 0) }; + p.sweep_idle(Instant::now()); + p.bytes() +} + +#[cfg(test)] +pub(super) fn flush_idle_for(device: usize) { + let mut g = SMALL_POOLS.lock().unwrap(); + if let Some(p) = g.get_mut(&device) { + p.flush_idle(Instant::now()); + } } #[cfg(test)] @@ -368,6 +390,14 @@ mod tests { let (h_raced, _) = p.add_pool_and_acquire(999, (0x40_0000, 1), at(5500)); assert_eq!(h_raced, 0); assert_eq!(p.pools.len(), pools_before, "must not push redundant pool when room already exists"); + + // `flush_idle` clears unreferenced idle slots while keeping live (`users > 0`) slots intact. + let used_before_flush = p.bytes().0; + let _ = alloc(&mut p, 0x50_0000, 2, 205, at(5600)); + assert!(p.forget(205, at(5610))); + assert_eq!(p.bytes().0, used_before_flush + 2 * UNIT); + p.flush_idle(at(5620)); + assert_eq!(p.bytes().0, used_before_flush, "flush_idle must reclaim unreferenced slots immediately"); } #[test] @@ -444,6 +474,7 @@ mod tests { // 1. 512 x 64x64 BC1 (2 KiB on console -> 4 KiB small resource on PC): must pack into // 2 MiB (16x smaller than 512 x 64 KiB = 32 MiB!) inside a single 16 MiB heap. + super::super::vram::flush_idle_for(dev_key); let bc1 = desc_of(64, 64, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_NONE); let (small0, _) = bytes_for(dev_key); let (pages0, _) = super::super::vram::page_bytes_for(dev_key); @@ -462,6 +493,7 @@ mod tests { // 2. Hardware upload + readback: two adjacent 64x64 RGBA8 (16 KiB) textures and a // same-address alias verify zero crosstalk and exact pixel sharing on the GPU. + super::super::vram::flush_idle_for(dev_key); let rgba = desc_of(64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE); let va_a = 0x61_0000_1000usize; let va_b = 0x61_0000_5000usize; @@ -523,6 +555,7 @@ mod tests { // 3. A small sampled texture at a 64 KiB-aligned va occupied by an existing GPU-owned // render target must stay on vram::Pages (not vram_small) so it aliases the target. + super::super::vram::flush_idle_for(dev_key); let rt_va = 0x62_0000_0000usize; let rt_desc = desc_of(64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET); let mut rt_raw = std::ptr::null_mut(); From 5005620c77108f747ceceda6be3253d5dfdb2d56 Mon Sep 17 00:00:00 2001 From: raylitebull <287713799+raylitebull@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:43:12 +0100 Subject: [PATCH 5/5] D3D12.X: use runner::skip_test for adapter capability guards in small-texture GPU test --- crates/xcl/src/hle/d3d12x/vram_small.rs | 32 ++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/d3d12x/vram_small.rs b/crates/xcl/src/hle/d3d12x/vram_small.rs index c77428d04a..e33ec692bd 100644 --- a/crates/xcl/src/hle/d3d12x/vram_small.rs +++ b/crates/xcl/src/hle/d3d12x/vram_small.rs @@ -447,7 +447,17 @@ mod tests { break; } } - let Some(dev) = dev else { return }; + let Some(dev) = dev else { + crate::gpu::recomp::runner::skip_test("no D3D12 device"); + return; + }; + let mut opts = D3D12_FEATURE_DATA_D3D12_OPTIONS::default(); + if dev.CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &mut opts as *mut _ as *mut c_void, std::mem::size_of_val(&opts) as u32).is_err() + || opts.ResourceHeapTier.0 < 2 + { + crate::gpu::recomp::runner::skip_test(&format!("resource heap tier {} < 2", opts.ResourceHeapTier.0)); + return; + } super::super::real::adopt(dev.as_raw(), super::super::real::Kind::Device); let dev_key = dev.as_raw() as usize; let riid = &ID3D12Resource::IID as *const windows::core::GUID as *const windows_sys::core::GUID; @@ -476,6 +486,14 @@ mod tests { // 2 MiB (16x smaller than 512 x 64 KiB = 32 MiB!) inside a single 16 MiB heap. super::super::vram::flush_idle_for(dev_key); let bc1 = desc_of(64, 64, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_NONE); + let bc1_info = dev.GetResourceAllocationInfo(0, &[D3D12_RESOURCE_DESC { Alignment: D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT as u64, ..bc1 }]); + if bc1_info.SizeInBytes == u64::MAX || bc1_info.SizeInBytes == 0 || bc1_info.Alignment != UNIT as u64 || bc1_info.SizeInBytes >= 0x1_0000 { + crate::gpu::recomp::runner::skip_test(&format!( + "adapter does not support 4 KiB small-resource placement for 64x64 BC1 (align={}, size={})", + bc1_info.Alignment, bc1_info.SizeInBytes + )); + return; + } let (small0, _) = bytes_for(dev_key); let (pages0, _) = super::super::vram::page_bytes_for(dev_key); let bc1_base = 0x60_0000_0000usize; @@ -495,6 +513,14 @@ mod tests { // same-address alias verify zero crosstalk and exact pixel sharing on the GPU. super::super::vram::flush_idle_for(dev_key); let rgba = desc_of(64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE); + let rgba_info = dev.GetResourceAllocationInfo(0, &[D3D12_RESOURCE_DESC { Alignment: D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT as u64, ..rgba }]); + if rgba_info.SizeInBytes != 16384 || rgba_info.Alignment != UNIT as u64 { + crate::gpu::recomp::runner::skip_test(&format!( + "step 2: adapter does not support 4 KiB 16 KiB placement for 64x64 RGBA8 (align={}, size={})", + rgba_info.Alignment, rgba_info.SizeInBytes + )); + return; + } let va_a = 0x61_0000_1000usize; let va_b = 0x61_0000_5000usize; let a1 = place_sampled(va_a, &rgba, 16384); @@ -555,6 +581,10 @@ mod tests { // 3. A small sampled texture at a 64 KiB-aligned va occupied by an existing GPU-owned // render target must stay on vram::Pages (not vram_small) so it aliases the target. + if opts.TiledResourcesTier.0 < D3D12_TILED_RESOURCES_TIER_2.0 { + crate::gpu::recomp::runner::skip_test(&format!("step 3: tiled resources tier {} < 2", opts.TiledResourcesTier.0)); + return; + } super::super::vram::flush_idle_for(dev_key); let rt_va = 0x62_0000_0000usize; let rt_desc = desc_of(64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);