Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/d3d12/src/device/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 4 additions & 8 deletions runtime/d3d12/src/device/heap/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PageCache<ImportedHeap>>,
pub(super) pieces: std::sync::Mutex<PageCache<PieceHeap>>,
pub(super) pieces: std::sync::Mutex<PieceCaches<PieceHeap>>,
}

impl PagePools {
Expand Down Expand Up @@ -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,
Expand All @@ -145,6 +140,7 @@ impl PagePools {
}
return result;
}
drop(caches);
open()
}

Expand Down
26 changes: 26 additions & 0 deletions runtime/d3d12/src/device/heap_page_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,32 @@ impl<T> PageCache<T> {
}
}

/// 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<T> {
pages: PageCache<T>,
spans: PageCache<T>,
}

impl<T> Default for PieceCaches<T> {
fn default() -> Self {
Self { pages: PageCache::default(), spans: PageCache::default() }
}
}

impl<T> PieceCaches<T> {
/// 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<T>, 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;
30 changes: 30 additions & 0 deletions runtime/d3d12/src/tests/device/heap_page_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u32>::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());
}
Loading