Skip to content
Open
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
1 change: 1 addition & 0 deletions crates/xcl/src/hle/d3d12x/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 41 additions & 8 deletions crates/xcl/src/hle/d3d12x/title_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2093,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);
Expand Down Expand Up @@ -2162,10 +2172,13 @@ 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.
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();
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 {
Expand All @@ -2176,17 +2189,37 @@ 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 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).
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);
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 + 0x1_0000, &d64);
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));
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));

// 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)),
Expand Down
67 changes: 60 additions & 7 deletions crates/xcl/src/hle/d3d12x/vram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -1210,17 +1212,61 @@ 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 (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();
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 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 (su, sp) = super::vram_small::bytes_for(device);
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 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`
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)
})
}
Expand All @@ -1232,8 +1278,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,
Expand All @@ -1252,6 +1299,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)
Expand Down
Loading
Loading