From 2898c907d7e457e315ae091e268f99e09a6420f0 Mon Sep 17 00:00:00 2001 From: Jeffrey C <990135+JeffreyBytes@users.noreply.github.com> Date: Wed, 2 Dec 2026 19:18:34 -0600 Subject: [PATCH] d3d12: cache GPU tile spans apart from single pages (#666) Since 106120ca a host that substitutes section imports backs queue page mappings with private memory, and gpu_tile imports many pages one at a time. Those single-page pieces stay live while a GpuTile holds them, and they shared one PageCache with the cluster windows of shared_window. Once 4,096 pieces were live the cache stopped indexing new keys, so a new window was imported again for every page in it. On an RX 6700 XT (RADV) ProjectScarlett reached 18 GiB of GTT within a minute of the setup screens, and the next submission failed with "Not enough memory for command submission" and VK_ERROR_DEVICE_LOST. Single pages and spans now have separate caches with their own bounds. Opening an uncached piece no longer holds the cache lock. ProjectScarlett on the same card: before, 655 windows took 4,599 imports and the device was lost at 170-194 s. After, adapter system memory peaks at 4.4 GiB and the title runs 444 s with no device loss, until the known main-thread watchdog (#215). --- runtime/d3d12/src/device/heap.rs | 2 +- runtime/d3d12/src/device/heap/pages.rs | 12 +++----- runtime/d3d12/src/device/heap_page_cache.rs | 26 ++++++++++++++++ .../d3d12/src/tests/device/heap_page_cache.rs | 30 +++++++++++++++++++ 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/runtime/d3d12/src/device/heap.rs b/runtime/d3d12/src/device/heap.rs index 741bb38d..a11679f9 100644 --- a/runtime/d3d12/src/device/heap.rs +++ b/runtime/d3d12/src/device/heap.rs @@ -1184,7 +1184,7 @@ pub(super) extern "system" fn unregister_pages(device: &Device, token: usize) { #[path = "heap_page_cache.rs"] mod heap_page_cache; -use heap_page_cache::{PageCache, span_key}; +use heap_page_cache::{PageCache, PieceCaches, span_key}; struct PagePool { start: u64, diff --git a/runtime/d3d12/src/device/heap/pages.rs b/runtime/d3d12/src/device/heap/pages.rs index 03e3fe5e..028177b3 100644 --- a/runtime/d3d12/src/device/heap/pages.rs +++ b/runtime/d3d12/src/device/heap/pages.rs @@ -53,7 +53,7 @@ pub(crate) struct PagePools { // 1.2 MiB about five times a frame each, and its recording allocators // keep every import for 216 frames: about 4,500 live buffers. pub(super) loose: std::sync::Mutex>, - pub(super) pieces: std::sync::Mutex>, + pub(super) pieces: std::sync::Mutex>, } impl PagePools { @@ -117,17 +117,12 @@ impl PagePools { } Ok(Arc::new(PieceHeap { native })) }; - let key = if bytes == u64::from(D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT) { - Some(address) - } else { - span_key(address, bytes) - }; - if let Some(key) = key { + let mut caches = self.pieces.lock().unwrap_or_else(|e| e.into_inner()); + if let Some((pieces, key)) = caches.cache_for(address, bytes) { use std::sync::atomic::AtomicU64; static OPEN: AtomicU64 = AtomicU64::new(0); static HIT: AtomicU64 = AtomicU64::new(0); let mut opened = false; - let mut pieces = self.pieces.lock().unwrap_or_else(|e| e.into_inner()); let result = pieces.get_or_open( key, |_| true, @@ -145,6 +140,7 @@ impl PagePools { } return result; } + drop(caches); open() } diff --git a/runtime/d3d12/src/device/heap_page_cache.rs b/runtime/d3d12/src/device/heap_page_cache.rs index 03111e68..684fc85b 100644 --- a/runtime/d3d12/src/device/heap_page_cache.rs +++ b/runtime/d3d12/src/device/heap_page_cache.rs @@ -70,6 +70,32 @@ impl PageCache { } } +/// Pieces of GPU tiles and scattered imports, cached by the pages they cover. +/// Single pages and spans keep separate bounds: a queue page mapping holds its +/// single-page piece live, and thousands of those must not keep a span shared +/// by many pages out of its cache, or each page imports the span again. +pub(super) struct PieceCaches { + pages: PageCache, + spans: PageCache, +} + +impl Default for PieceCaches { + fn default() -> Self { + Self { pages: PageCache::default(), spans: PageCache::default() } + } +} + +impl PieceCaches { + /// The cache and key for an import of `bytes` at `base`, or `None` when no + /// key describes the range and it is never shared. + pub(super) fn cache_for(&mut self, base: u64, bytes: u64) -> Option<(&mut PageCache, u64)> { + if bytes == PAGE { + return base.is_multiple_of(PAGE).then_some((&mut self.pages, base)); + } + span_key(base, bytes).map(|key| (&mut self.spans, key)) + } +} + #[cfg(test)] #[path = "../tests/device/heap_page_cache.rs"] mod tests; diff --git a/runtime/d3d12/src/tests/device/heap_page_cache.rs b/runtime/d3d12/src/tests/device/heap_page_cache.rs index 308eb411..c2a2f818 100644 --- a/runtime/d3d12/src/tests/device/heap_page_cache.rs +++ b/runtime/d3d12/src/tests/device/heap_page_cache.rs @@ -123,3 +123,33 @@ fn failed_imports_are_not_cached_and_live_entries_are_bounded() { assert_eq!(cache.entries.len(), LIMIT - SWEEP + 1); assert_eq!(cache.sweep.len(), cache.entries.len()); } + +#[test] +fn live_single_pages_do_not_keep_a_span_out_of_the_cache() { + // #666: a queue page mapping holds its single-page piece live, and once + // LIMIT of those filled a shared cache, a cluster window was never cached. + // Every page of the window then imported the whole window again. + let mut pieces = PieceCaches::default(); + let pages: Vec<_> = (0..LIMIT as u64) + .map(|n| { + let (cache, key) = pieces.cache_for(n * PAGE, PAGE).unwrap(); + cache.get_or_open(key, |_| true, || Ok::<_, ()>(Arc::new(n))).unwrap() + }) + .collect(); + let window = 0x1_0000_0000; + let (cache, key) = pieces.cache_for(window, 32 * PAGE).unwrap(); + let first = cache.get_or_open(key, |_| true, || Ok::<_, ()>(Arc::new(u64::MAX))).unwrap(); + let (cache, key) = pieces.cache_for(window, 32 * PAGE).unwrap(); + let again = + cache.get_or_open(key, |_| true, || Err(())).expect("the live window must be found, not imported again"); + assert!(Arc::ptr_eq(&first, &again)); + drop(pages); +} + +#[test] +fn piece_caches_refuse_ranges_no_key_describes() { + let mut pieces = PieceCaches::::default(); + assert!(pieces.cache_for(0x1_0000_0000 + 4096, PAGE).is_none()); + assert!(pieces.cache_for(0x1_0000_0000, 2 * PAGE + 1).is_none()); + assert!(pieces.cache_for(0x1_0000_0000, PAGE * PAGE).is_none()); +}