From d275207689bbd16f76edc4f6e907064581073f4d Mon Sep 17 00:00:00 2001 From: raylitebull <287713799+raylitebull@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:23:55 +0000 Subject: [PATCH 1/2] D3D12.X: count CPU-backed title textures as they come and go update_lengths, called under the TEXTURES lock on every placement, release, retirement and uncover, counted the CPU-backed entries by walking all of by_res. A retail title keeps tens of thousands of entries there (55k+ in the amd-dev measurement), so every texture create and release paid for a full scan while holding the lock that submits and recording also take. Table now keeps cpu_backed_count up to date where by_res changes (insert_active / remove_active), and update_lengths stores it. Ported from amd-dev 4d6d4a77. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- crates/xcl/src/hle/d3d12x/title_textures.rs | 85 +++++++++++++++++++-- 1 file changed, 78 insertions(+), 7 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index d809b1c2a2..bf76800860 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -157,6 +157,9 @@ struct Table { /// 16 MiB blocks of the textures' sources -> how many active ones cover it: the part of the /// watched ranges that textures make (cheap to rebuild from, unlike all textures) coarse: BTreeMap, + /// the entries of `by_res` with `Entry::cpu_backed` (`CPU_TEXTURES_LEN`), counted as they come + /// and go: a title has tens of thousands, and every placement and release counted them anew + cpu_backed_count: usize, } /// the size of `Table::coarse`'s blocks @@ -186,9 +189,31 @@ impl Table { src_pieces: BTreeMap::new(), pending_res: std::collections::BTreeSet::new(), coarse: BTreeMap::new(), + cpu_backed_count: 0, } } + /// `by_res.insert`, counting `cpu_backed_count`. + fn insert_active(&mut self, res: usize, e: Entry) { + if e.cpu_backed { + self.cpu_backed_count += 1; + } + if let Some(prev) = self.by_res.insert(res, e) { + if prev.cpu_backed { + self.cpu_backed_count -= 1; + } + } + } + + /// `by_res.remove`, counting `cpu_backed_count`. + fn remove_active(&mut self, res: usize) -> Option { + let old = self.by_res.remove(&res)?; + if old.cpu_backed { + self.cpu_backed_count -= 1; + } + Some(old) + } + fn link_src(&mut self, res: usize, src: usize, size: usize) { for p in (src >> SRC_PIECE_SHIFT)..=((src + size.max(1) - 1) >> SRC_PIECE_SHIFT) { self.src_pieces.entry(p).or_default().push(res); @@ -293,7 +318,7 @@ impl Table { let (mut pending_delta, mut retired, mut to_unmap) = (0isize, 0usize, Vec::new()); for (ova, ores, _) in self.active_overlaps(va, end) { self.by_va.remove(&(ova, ores)); - if let Some(old) = self.by_res.remove(&ores) { + if let Some(old) = self.remove_active(ores) { self.unlink_src(ores, old.src, old.tex.size() as usize); if old.pending { pending_delta -= 1; @@ -329,7 +354,7 @@ impl Table { let end = va.saturating_add(entry.tex.size() as usize); let mut pending_delta = 0isize; let mut to_unmap = Vec::new(); - if let Some(old) = self.by_res.remove(&res) { + if let Some(old) = self.remove_active(res) { self.unlink_src(res, old.src, old.tex.size() as usize); self.by_va.remove(&(old.va, res)); if old.pending { @@ -344,7 +369,7 @@ impl Table { continue; } self.by_va.remove(&(ova, ores)); - if let Some(mut old) = self.by_res.remove(&ores) { + if let Some(mut old) = self.remove_active(ores) { self.unlink_src(ores, old.src, old.tex.size() as usize); if old.pending { pending_delta -= 1; @@ -382,7 +407,7 @@ impl Table { self.by_va.insert((va, res), end); let (src, size) = (entry.src, entry.tex.size() as usize); self.link_src(res, src, size); - self.by_res.insert(res, entry); + self.insert_active(res, entry); (pending_delta, to_unmap) } @@ -397,7 +422,7 @@ impl Table { let st = (old.states.len() == 1).then(|| old.states[0]); return (true, 0, Vec::new(), st); } - let Some(old) = self.by_res.remove(&res) else { return (false, 0, Vec::new(), None) }; + let Some(old) = self.remove_active(res) else { return (false, 0, Vec::new(), None) }; self.unlink_src(res, old.src, old.tex.size() as usize); let st = (old.states.len() == 1).then(|| old.states[0]); self.by_va.remove(&(old.va, res)); @@ -435,7 +460,7 @@ impl Table { if e.pending { self.pending_res.insert(sres); } - self.by_res.insert(sres, e); + self.insert_active(sres, e); } } if self.shadowed.is_empty() { @@ -513,7 +538,8 @@ unsafe fn committed_allocation_bytes(device: *mut c_void, resource: *mut c_void, fn update_lengths(t: &Table) { TEXTURES_LEN.store(t.by_res.len(), Ordering::Relaxed); - CPU_TEXTURES_LEN.store(t.by_res.values().filter(|e| e.cpu_backed).count(), Ordering::Relaxed); + debug_assert_eq!(t.cpu_backed_count, t.by_res.values().filter(|e| e.cpu_backed).count(), "a by_res change bypassed insert_active / remove_active"); + CPU_TEXTURES_LEN.store(t.cpu_backed_count, Ordering::Relaxed); } /// Whether a non-depth render target / UAV texture is small enough to be a CPU-filled glyph or UI @@ -6123,6 +6149,51 @@ pub(super) mod tests { assert!(!tbl.by_res.contains_key(&10) && !tbl.by_res.contains_key(&11) && tbl.by_res.contains_key(&12)); } + /// `Table::cpu_backed_count` (`CPU_TEXTURES_LEN`) follows the CPU-backed active textures + /// through placements, a texture placed again, shadowing, releases and retirement, as counting + /// them all does. + #[test] + fn the_cpu_backed_count_follows_the_active_textures() { + let tex = Arc::new(xg::Texture::new(&console_desc(3, 64, 64, 1, 1, 28, 0x105)).unwrap()); + let entry = |va: usize, cpu_backed: bool| Entry { + device: 0, + committed_bytes: 0, + va, + src: va, + in_gpu_view: false, + paged: false, + tex: Arc::clone(&tex), + states: vec![D3D12_RESOURCE_STATE_COMMON], + pending: false, + gpu_written: false, + written_call: 0, + gpu_writers: Vec::new(), + cpu_backed, + placed: true, + unmapped: false, + generation: 0, + known: Default::default(), + target: false, + }; + let counted = |t: &Table| t.by_res.values().filter(|e| e.cpu_backed).count(); + let mut tbl = Table::new(); + tbl.insert(1, entry(0x10_0000, true)); + tbl.insert(2, entry(0x10_4000, false)); + tbl.insert(3, entry(0x10_8000, true)); + assert_eq!((tbl.cpu_backed_count, counted(&tbl)), (2, 2)); + // (the same range over 1 shadows it; 3 placed again replaces itself) + tbl.insert(4, entry(0x10_0000, false)); + tbl.insert(3, entry(0x10_8000, true)); + assert!(tbl.shadowed.contains_key(&1)); + assert_eq!(tbl.cpu_backed_count, counted(&tbl)); + tbl.remove(4); + assert_eq!(tbl.cpu_backed_count, counted(&tbl)); + tbl.remove(2); + assert_eq!(tbl.cpu_backed_count, counted(&tbl)); + tbl.retire_range(0x10_0000, 0x10_C000); + assert_eq!((tbl.cpu_backed_count, counted(&tbl), tbl.by_res.len()), (0, 0, 0)); + } + /// Real GPU (#248): placed title textures share `vram::Pages` so streaming unreleased /// overlapping placements stays bounded in video memory, superseded textures never corrupt /// newer placements on shared tiles, 4 KiB-aligned neighbours never false-alias, and mip From c36eedf62f728ec1dea825b0bd6c240e3999fbb4 Mon Sep 17 00:00:00 2001 From: raylitebull <287713799+raylitebull@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:26:28 +0000 Subject: [PATCH 2/2] D3D12.X: read title texture generations without the TEXTURES lock capture_transitions (every barrier recorded on a list while textures are kept in step) and capture_gpu_write (every copy and clear into one) took the TEXTURES mutex to read one registration generation each. Submits hold that lock while they choose the textures to upload, so recording threads waited behind them. RES_GENERATIONS mirrors the generation of every active or shadowed entry of TEXTURES. Table writes it under the TEXTURES lock where an entry is placed and where it leaves (release, retirement), and the two capture paths read it under a shared lock. Tables made by tests leave it alone. Ported from amd-dev 4d6d4a77, which measured this together with its other lock changes on amd-dev (draw recording 168 -> 48 us avg on an RX 9070 XT); this part was not measured on its own or on main. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- crates/xcl/src/hle/d3d12x/title_textures.rs | 91 +++++++++++++++++++-- 1 file changed, 82 insertions(+), 9 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index bf76800860..f8fb90719c 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -19,10 +19,10 @@ //! transitions of these textures are captured in each physical command-list segment's immutable //! submission effects and applied in submission order – per subresource once one is named. -use std::collections::{BTreeMap, BTreeSet}; +use std::collections::{BTreeMap, BTreeSet, HashMap}; use std::ffi::c_void; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, RwLock}; use windows::core::Interface; use windows::Win32::Graphics::Direct3D12::*; @@ -148,6 +148,9 @@ struct Table { /// Largest byte span currently in `shadowed` (bounds range queries) max_shadowed_size: usize, next_generation: u64, + /// the process's table (`TEXTURES`): its entries' generations are mirrored in `RES_GENERATIONS` + /// (a test's own table leaves the mirror alone) + global: bool, /// the active textures by the 1 MiB pieces of their source (`Entry::src`) they cover: which of /// them lie under a written page. A title has tens of thousands of textures; a submit asks /// about the few its dirty pages touch @@ -178,7 +181,12 @@ fn in_view_window(src: usize) -> bool { } impl Table { + #[cfg(test)] const fn new() -> Self { + Self::with_global(false) + } + + const fn with_global(global: bool) -> Self { Table { by_res: BTreeMap::new(), by_va: BTreeMap::new(), @@ -186,6 +194,7 @@ impl Table { shadowed_by_va: BTreeMap::new(), max_shadowed_size: 0, next_generation: 0, + global, src_pieces: BTreeMap::new(), pending_res: std::collections::BTreeSet::new(), coarse: BTreeMap::new(), @@ -214,6 +223,16 @@ impl Table { Some(old) } + /// `res` leaves the table: out of `RES_GENERATIONS` too (`global`). + fn forget_generation(&self, res: usize) { + if !self.global { + return; + } + if let Some(m) = RES_GENERATIONS.write().unwrap_or_else(|e| e.into_inner()).as_mut() { + m.remove(&res); + } + } + fn link_src(&mut self, res: usize, src: usize, size: usize) { for p in (src >> SRC_PIECE_SHIFT)..=((src + size.max(1) - 1) >> SRC_PIECE_SHIFT) { self.src_pieces.entry(p).or_default().push(res); @@ -319,6 +338,7 @@ impl Table { for (ova, ores, _) in self.active_overlaps(va, end) { self.by_va.remove(&(ova, ores)); if let Some(old) = self.remove_active(ores) { + self.forget_generation(ores); self.unlink_src(ores, old.src, old.tex.size() as usize); if old.pending { pending_delta -= 1; @@ -332,6 +352,7 @@ impl Table { for (sva, sres, _) in self.shadowed_overlaps(va, end) { self.shadowed_by_va.remove(&(sva, sres)); if let Some(old) = self.shadowed.remove(&sres) { + self.forget_generation(sres); if old.placed && !old.unmapped { to_unmap.push(sres); } @@ -407,6 +428,10 @@ impl Table { self.by_va.insert((va, res), end); let (src, size) = (entry.src, entry.tex.size() as usize); self.link_src(res, src, size); + if self.global { + let mut g = RES_GENERATIONS.write().unwrap_or_else(|e| e.into_inner()); + g.get_or_insert_with(HashMap::new).insert(res, entry.generation); + } self.insert_active(res, entry); (pending_delta, to_unmap) } @@ -415,6 +440,7 @@ impl Table { /// first). Returns `(was_present, pending_delta, unmapped_res_to_remap, uniform_state)`. fn remove(&mut self, res: usize) -> (bool, isize, Vec, Option) { if let Some(old) = self.shadowed.remove(&res) { + self.forget_generation(res); self.shadowed_by_va.remove(&(old.va, res)); if self.shadowed.is_empty() { self.max_shadowed_size = 0; @@ -423,6 +449,7 @@ impl Table { return (true, 0, Vec::new(), st); } let Some(old) = self.remove_active(res) else { return (false, 0, Vec::new(), None) }; + self.forget_generation(res); self.unlink_src(res, old.src, old.tex.size() as usize); let st = (old.states.len() == 1).then(|| old.states[0]); self.by_va.remove(&(old.va, res)); @@ -502,7 +529,12 @@ unsafe fn clone_resources(ptrs: &[usize]) -> Vec { } /// PC texture → its place in the title's memory -static TEXTURES: Mutex = Mutex::new(Table::new()); +static TEXTURES: Mutex
= Mutex::new(Table::with_global(true)); +/// The registration generation of each texture of `TEXTURES` (active or shadowed), written under +/// its lock: `capture_transitions` (every barrier of a list) and `capture_gpu_write` (every copy +/// and clear) read it under a shared lock instead of waiting for `TEXTURES`, which submits hold +/// while they choose the textures to upload. +static RES_GENERATIONS: RwLock>> = RwLock::new(None); // The dirty-page scan consumes write-watch state for every submission thread. // Keep the scan, pending-list update and GPU uploads together. static REFRESH: Mutex<()> = Mutex::new(()); @@ -539,6 +571,16 @@ unsafe fn committed_allocation_bytes(device: *mut c_void, resource: *mut c_void, fn update_lengths(t: &Table) { TEXTURES_LEN.store(t.by_res.len(), Ordering::Relaxed); debug_assert_eq!(t.cpu_backed_count, t.by_res.values().filter(|e| e.cpu_backed).count(), "a by_res change bypassed insert_active / remove_active"); + #[cfg(debug_assertions)] + if t.global { + let g = RES_GENERATIONS.read().unwrap_or_else(|e| e.into_inner()); + let m = g.as_ref(); + debug_assert!( + m.map_or(0, |m| m.len()) == t.by_res.len() + t.shadowed.len() + && t.by_res.iter().chain(&t.shadowed).all(|(r, e)| m.and_then(|m| m.get(r)) == Some(&e.generation)), + "RES_GENERATIONS missed a change of TEXTURES" + ); + } CPU_TEXTURES_LEN.store(t.cpu_backed_count, Ordering::Relaxed); } @@ -943,9 +985,9 @@ pub(super) fn capture_gpu_write(res: usize, subresource: u32) -> Option