diff --git a/README.md b/README.md index 2a957e95e..9a4f221be 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,8 @@ next steps: | **Video** | ✅ | the game's Bink 2 console DLL decodes its movies (720p / 1080p) on the CPU; the game's own YUV → RGB display shaders (`PS_Bink…` in its pack) translate like any other. Bink's GPU-assisted mode needs no Bink-specific support: its compute shaders ship with the game and go through the recompiler too (no game seen using it yet) | a game movie decodes frame by frame, picture checked | | **Tools** | ✅ | `xcl inspect`, `xcl run`, `xcl disasm` (RDNA2, console shader containers), `xcl shaders` (pack statistics, recompile / compile runs), `xcl rpf` | – | +Completely free tiled-texture VRAM pools are released after their tiles finish cooling; one pool stays available for reuse. + ## Controls A **gamepad is recommended**: console games are made for one, and their menus and controls expect it. diff --git a/crates/xcl/src/hle/d3d12x/vram.rs b/crates/xcl/src/hle/d3d12x/vram.rs index 8b806d5f8..a658d8b95 100644 --- a/crates/xcl/src/hle/d3d12x/vram.rs +++ b/crates/xcl/src/hle/d3d12x/vram.rs @@ -226,7 +226,7 @@ struct Pages { /// signalled after each mapping; title queues wait for it before their next work fence: ID3D12Fence, value: u64, - pools: Vec, + pools: Vec>, free: Vec, /// tiles given back, reusable after `COOL` cooling: std::collections::VecDeque<(Tile, std::time::Instant)>, @@ -276,9 +276,10 @@ impl Pages { }) } - /// Pages no resource used for `IDLE` go back to the pools. + /// Sweep idle pages, drain cooled tiles and release excess completely free pool heaps. fn sweep(&mut self, now: std::time::Instant) { sweep_idle(&mut self.idle, &mut self.pages, &mut self.extra_pages, &mut self.cooling, now); + release_empty_pools(&mut self.pools, &mut self.free, &mut self.cooling, now); } unsafe fn take_tile(&mut self, dev: &ID3D12Device) -> Option { @@ -300,8 +301,7 @@ impl Pages { crate::xlog_warn!("D3D12.X: no video memory for more pages of title textures ({e})"); return None; } - self.pools.push(heap?); - let h = self.pools.len() as u32 - 1; + let h = insert_pool(&mut self.pools, heap?); self.free.extend((1..POOL_PAGES).rev().map(|t| (h, t))); Some((h, 0)) } @@ -323,7 +323,7 @@ impl Pages { 1, Some(&coord), Some(®ion), - &self.pools[h as usize], + self.pools[h as usize].as_ref(), ranges.len() as u32, Some(flags.as_ptr()), Some(offsets.as_ptr()), @@ -444,6 +444,51 @@ impl Pages { } } +/// Place `heap` in the first vacated pool slot, or append a new slot. +fn insert_pool(pools: &mut Vec>, heap: H) -> u32 { + match pools.iter().position(Option::is_none) { + Some(i) => { + pools[i] = Some(heap); + i as u32 + } + None => { + pools.push(Some(heap)); + pools.len() as u32 - 1 + } + } +} + +/// Move expired cooling tiles back to `free` and drop any excess pool heap whose all `POOL_PAGES` +/// tiles are free, keeping at least one live pool warm to avoid heap-creation thrashing. +fn release_empty_pools(pools: &mut [Option], free: &mut Vec, cooling: &mut std::collections::VecDeque<(Tile, std::time::Instant)>, now: std::time::Instant) { + while cooling.front().is_some_and(|(_, at)| now.saturating_duration_since(*at) >= COOL) { + if let Some((t, _)) = cooling.pop_front() { + free.push(t); + } + } + let mut live = pools.iter().filter(|h| h.is_some()).count(); + if free.len() < POOL_PAGES as usize || live <= 1 { + return; + } + let mut counts = vec![0u32; pools.len()]; + for &(h, _) in free.iter() { + if let Some(c) = counts.get_mut(h as usize) { + *c += 1; + } + } + let mut dropped = false; + for (h, &c) in counts.iter().enumerate() { + if c == POOL_PAGES && pools[h].is_some() && live > 1 { + pools[h] = None; + live -= 1; + dropped = true; + } + } + if dropped { + free.retain(|&(h, _)| pools.get(h as usize).is_some_and(Option::is_some)); + } +} + /// The ranges of one UpdateTileMappings call for pool heap `heap` over a resource whose overall /// tile i goes to `tiles[i]`: (mapped, tile offset in the heap, count) – runs of consecutive /// tiles of the heap, and skipped runs for tiles of other heaps. @@ -612,14 +657,14 @@ pub(super) static VRAM_TEST_LOCK: Mutex<()> = Mutex::new(()); pub(super) fn page_bytes_for(device: usize) -> (usize, usize) { 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) + ((p.pages.len() + p.extra_pages.len()) * PAGE, p.pools.iter().filter(|h| h.is_some()).count() * POOL_PAGES as usize * PAGE) } /// (bytes of pages in use by tiled textures, bytes of pool heaps) – `XCL_ACTIVITY` pub fn page_bytes() -> (usize, usize) { let g = PAGES.lock().unwrap(); g.values().flatten().fold((0, 0), |(used, pools), p| { - (used + (p.pages.len() + p.extra_pages.len()) * PAGE, pools + p.pools.len() * POOL_PAGES as usize * PAGE) + (used + (p.pages.len() + p.extra_pages.len()) * PAGE, pools + p.pools.iter().filter(|h| h.is_some()).count() * POOL_PAGES as usize * PAGE) }) } @@ -752,6 +797,42 @@ mod tests { assert_eq!(pages.len(), 199_998); } + /// Empty 64 MiB tile pools are released once their tiles finish cooling, at least one pool + /// stays warm, and newly allocated pools reuse vacated slots. + #[test] + fn empty_tile_pools_are_released_after_cooling_and_slots_reused() { + let t0 = Instant::now(); + let mut pools: Vec> = Vec::new(); + assert_eq!(insert_pool(&mut pools, 10), 0); + assert_eq!(insert_pool(&mut pools, 20), 1); + assert_eq!(insert_pool(&mut pools, 30), 2); + // Pool 0 has 1 tile in use; pool 1 is cooling; pool 2 is completely free. + let mut free: Vec = (1..POOL_PAGES).map(|t| (0, t)).chain((0..POOL_PAGES).map(|t| (2, t))).collect(); + let mut cooling: std::collections::VecDeque<(Tile, Instant)> = (0..POOL_PAGES).map(|t| ((1, t), t0)).collect(); + + // Before COOL elapses: pool 2 drops, pool 1 stays while cooling, pool 0 stays (1 tile live). + release_empty_pools(&mut pools, &mut free, &mut cooling, t0 + COOL - Duration::from_millis(1)); + assert_eq!(pools, vec![Some(10), Some(20), None]); + assert!(free.iter().all(|&(h, _)| h == 0) && free.len() == POOL_PAGES as usize - 1); + assert_eq!(cooling.len(), POOL_PAGES as usize); + + // Once COOL elapses: pool 1 drains from cooling and drops; pool 0 stays. + release_empty_pools(&mut pools, &mut free, &mut cooling, t0 + COOL); + assert_eq!(pools, vec![Some(10), None, None]); + assert!(cooling.is_empty()); + assert_eq!(free.len(), POOL_PAGES as usize - 1); + + // Even when pool 0's last tile returns, one warm pool is kept. + free.push((0, 0)); + release_empty_pools(&mut pools, &mut free, &mut cooling, t0 + COOL + Duration::from_secs(1)); + assert_eq!(pools, vec![Some(10), None, None]); + assert_eq!(free.len(), POOL_PAGES as usize); + + // A new pool reuses the first vacated slot instead of growing the vec. + assert_eq!(insert_pool(&mut pools, 40), 1); + assert_eq!(pools, vec![Some(10), Some(40), None]); + } + /// Any 64 KiB page boundary inside `[va, va + bytes)` belongs to that range alone, so /// non-overlapping ranges never share an owned page even when only 4 KiB aligned. #[test]