diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index d809b1c2a2..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 @@ -157,6 +160,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 @@ -175,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(), @@ -183,9 +194,42 @@ 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(), + 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) + } + + /// `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); } } @@ -293,7 +337,8 @@ 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.forget_generation(ores); self.unlink_src(ores, old.src, old.tex.size() as usize); if old.pending { pending_delta -= 1; @@ -307,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); } @@ -329,7 +375,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 +390,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 +428,11 @@ 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); + 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) } @@ -390,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; @@ -397,7 +448,8 @@ 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.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)); @@ -435,7 +487,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() { @@ -477,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(()); @@ -513,7 +570,18 @@ 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"); + #[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); } /// Whether a non-depth render target / UAV texture is small enough to be a CPU-filled glyph or UI @@ -917,9 +985,9 @@ pub(super) fn capture_gpu_write(res: usize, subresource: u32) -> Option