diff --git a/crates/xcl/Cargo.toml b/crates/xcl/Cargo.toml index 4788e3ac9e..2718ae4179 100644 --- a/crates/xcl/Cargo.toml +++ b/crates/xcl/Cargo.toml @@ -11,6 +11,7 @@ path = "src/lib.rs" [features] pipeline-resolution-bench = [] +texture-selection-bench = [] [[bin]] name = "xcl" diff --git a/crates/xcl/benches/runtime.rs b/crates/xcl/benches/runtime.rs index 7d8ce1176c..ddb67e3ef5 100644 --- a/crates/xcl/benches/runtime.rs +++ b/crates/xcl/benches/runtime.rs @@ -158,6 +158,96 @@ fn allocator_benches(c: &mut Criterion) { group.finish(); } +#[cfg(feature = "texture-selection-bench")] +fn texture_selection_benches(c: &mut Criterion) { + use xcl::hle::d3d12x::title_textures::{TextureSelectionBench, TextureSelectionScenario as Scenario}; + + let mut group = c.benchmark_group("runtime/texture_selection"); + for entries in [1_024usize, 4_096, 16_384] { + for (name, scenario) in [ + ("empty_gate", Scenario::Empty), + ("pending_sparse", Scenario::SparsePending), + ("pending_all", Scenario::AllPending), + ("gpu_written_all", Scenario::AllGpuWritten), + ("dirty_1", Scenario::OneDirty), + ("dirty_sparse_64", Scenario::SparseDirty), + ("dirty_dense_4096", Scenario::DenseDirty), + ("mixed_aliases", Scenario::MixedAliases), + ] { + let fixture = TextureSelectionBench::new(entries, scenario); + let operations = if matches!(scenario, Scenario::Empty) { 1 } else { entries }; + group.throughput(Throughput::Elements(operations as u64)); + group.bench_function(&format!("{name}_{entries}"), |b| { + b.iter(|| black_box(fixture.scan())); + }); + } + } + + const ENTRIES: usize = 4_096; + const BATCH: usize = 64; + let workers = std::thread::available_parallelism().map_or(4, |count| count.get()) + .saturating_sub(1).clamp(2, 8); + group.throughput(Throughput::Elements((ENTRIES * BATCH * workers) as u64)); + for shared in [true, false] { + let pool = TextureSelectionWorkers::new(ENTRIES, workers, BATCH, shared); + let name = if shared { "mixed_shared_lock" } else { "mixed_distinct_locks" }; + group.bench_function(&format!("{name}_{workers}"), |b| b.iter(|| pool.run())); + } + group.finish(); +} + +#[cfg(feature = "texture-selection-bench")] +struct TextureSelectionWorkers { + start: Arc, + done: Arc, + stop: Arc, + threads: Vec>, +} + +#[cfg(feature = "texture-selection-bench")] +impl TextureSelectionWorkers { + fn new(entries: usize, workers: usize, batch: usize, shared: bool) -> Self { + use xcl::hle::d3d12x::title_textures::{TextureSelectionBench, TextureSelectionScenario}; + + let start = Arc::new(Barrier::new(workers + 1)); + let done = Arc::new(Barrier::new(workers + 1)); + let stop = Arc::new(AtomicBool::new(false)); + let common = shared.then(|| Arc::new(TextureSelectionBench::new(entries, TextureSelectionScenario::MixedAliases))); + let threads = (0..workers).map(|worker| { + let fixture = common.clone().unwrap_or_else(|| Arc::new(TextureSelectionBench::new(entries, TextureSelectionScenario::MixedAliases))); + let (start, done, stop) = (start.clone(), done.clone(), stop.clone()); + std::thread::Builder::new().name(format!("texture-select-bench-{worker}")).spawn(move || loop { + start.wait(); + if stop.load(Ordering::Acquire) { break } + let mut checksum = 0usize; + for _ in 0..batch { + let (matches, ids) = fixture.scan(); + checksum = checksum.wrapping_add(matches ^ ids); + } + black_box(checksum); + done.wait(); + }).expect("texture selection benchmark worker") + }).collect(); + Self { start, done, stop, threads } + } + + fn run(&self) { + self.start.wait(); + self.done.wait(); + } +} + +#[cfg(feature = "texture-selection-bench")] +impl Drop for TextureSelectionWorkers { + fn drop(&mut self) { + self.stop.store(true, Ordering::Release); + self.start.wait(); + for thread in self.threads.drain(..) { + thread.join().expect("texture selection benchmark worker panicked"); + } + } +} + #[cfg(feature = "pipeline-resolution-bench")] fn pipeline_resolution_benches(c: &mut Criterion) { let fixture = Arc::new(xcl::hle::d3d12x::pso_async::PipelineResolutionBench::new()); @@ -588,6 +678,8 @@ fn main() { manifest_and_config_benches, vfs_benches, allocator_benches, + #[cfg(feature = "texture-selection-bench")] + texture_selection_benches, #[cfg(feature = "pipeline-resolution-bench")] pipeline_resolution_benches, #[cfg(feature = "pipeline-resolution-bench")] diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 5e0241d4cb..818e3547a0 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -843,6 +843,25 @@ fn watched_texture_span(src: usize, bytes: u64) -> Option<(usize, usize)> { Some((src & !0xFFF, end)) } +#[inline(always)] +fn texture_needs_upload( + cpu_backed: bool, + pending: bool, + gpu_written: bool, + dirty_overlap: impl FnOnce() -> bool, +) -> bool { + if !cpu_backed { + return false; + } + if pending { + return true; + } + if gpu_written { + return false; + } + dirty_overlap() +} + fn cached_watch_ranges(u: &crate::gpu::uma::Uma, measure: bool) -> Arc { let key = ( super::sparse::mapped_gen(), @@ -1082,20 +1101,18 @@ unsafe fn refresh_once(queue: *mut c_void, mut look: Option<&mut super::sparse:: if measure_selection { visited += 1; } - if !e.cpu_backed { - continue; + if measure_selection && e.cpu_backed && !e.pending && !e.gpu_written { + dirty_tests += 1; } - let selected = if e.pending { - true - } else if e.gpu_written { - false - } else { - if measure_selection { - dirty_tests += 1; - } - let i = dirty.partition_point(|&p| p + page as usize <= e.src); - dirty.get(i).is_some_and(|&p| p < e.src + e.tex.size() as usize) - }; + let selected = texture_needs_upload( + e.cpu_backed, + e.pending, + e.gpu_written, + || { + let i = dirty.partition_point(|&p| p + page as usize <= e.src); + dirty.get(i).is_some_and(|&p| p < e.src + e.tex.size() as usize) + }, + ); if !selected { continue; } @@ -1129,6 +1146,102 @@ unsafe fn refresh_once(queue: *mut c_void, mut look: Option<&mut super::sparse:: super::sparse::PreparationOutcome::Ready } +#[cfg(feature = "texture-selection-bench")] +#[derive(Clone, Copy)] +pub enum TextureSelectionScenario { + Empty, + SparsePending, + AllPending, + AllGpuWritten, + OneDirty, + SparseDirty, + DenseDirty, + MixedAliases, +} + +#[cfg(feature = "texture-selection-bench")] +struct TextureSelectionBenchEntry { + src: usize, + size: usize, + cpu_backed: bool, + pending: bool, + gpu_written: bool, +} + +/// CPU-only model of the exact selection predicate and table lock. It deliberately excludes COM +/// retention and uploads: the benchmark separates scan/convoy cost from those Windows operations. +#[cfg(feature = "texture-selection-bench")] +pub struct TextureSelectionBench { + entries: Mutex>, + dirty: Vec, + pending: std::sync::atomic::AtomicUsize, +} + +#[cfg(feature = "texture-selection-bench")] +impl TextureSelectionBench { + const PAGE: usize = 0x1000; + + pub fn new(count: usize, scenario: TextureSelectionScenario) -> Self { + let base = 0x1000_0000usize; + let mut entries = BTreeMap::new(); + let mut pending = 0; + for i in 0..count { + let aliased = matches!(scenario, TextureSelectionScenario::MixedAliases) && i % 16 == 1; + let alias_group = i - aliased as usize; + let src = base + alias_group * 2 * Self::PAGE; + let cpu_backed = !matches!(scenario, TextureSelectionScenario::MixedAliases) || i % 8 != 4; + let is_pending = matches!(scenario, TextureSelectionScenario::AllPending) + || matches!(scenario, TextureSelectionScenario::SparsePending) && i % 64 == 0 + || matches!(scenario, TextureSelectionScenario::MixedAliases) && i % 127 == 0; + let gpu_written = matches!(scenario, TextureSelectionScenario::AllGpuWritten) + || matches!(scenario, TextureSelectionScenario::MixedAliases) && i % 11 == 0; + pending += is_pending as usize; + entries.insert(i + 1, TextureSelectionBenchEntry { + src, + size: Self::PAGE + (alias_group % 3) * Self::PAGE, + cpu_backed, + pending: is_pending, + gpu_written, + }); + } + let pages = match scenario { + TextureSelectionScenario::Empty | TextureSelectionScenario::SparsePending | TextureSelectionScenario::AllPending => 0, + TextureSelectionScenario::OneDirty => 1, + TextureSelectionScenario::AllGpuWritten | TextureSelectionScenario::SparseDirty | TextureSelectionScenario::MixedAliases => 64, + TextureSelectionScenario::DenseDirty => 4096, + }; + let stride = if matches!(scenario, TextureSelectionScenario::DenseDirty) { 1 } else { 31 }; + let mut dirty: Vec<_> = (0..pages).map(|i| base + i * stride * Self::PAGE).collect(); + dirty.sort_unstable(); + Self { entries: Mutex::new(entries), dirty, pending: std::sync::atomic::AtomicUsize::new(pending) } + } + + #[inline(never)] + pub fn scan(&self) -> (usize, usize) { + if self.dirty.is_empty() && self.pending.load(Ordering::Relaxed) == 0 { + return (0, 0); + } + let entries = self.entries.lock().unwrap(); + let mut matches = 0usize; + let mut checksum = 0usize; + for (&resource, entry) in entries.iter() { + if texture_needs_upload( + entry.cpu_backed, + entry.pending, + entry.gpu_written, + || { + let i = self.dirty.partition_point(|&p| p + Self::PAGE <= entry.src); + self.dirty.get(i).is_some_and(|&p| p < entry.src + entry.size) + }, + ) { + matches += 1; + checksum ^= resource; + } + } + (matches, checksum) + } +} + /// texture → `content_hash` of its memory at its last upload static LAST_CONTENT: Mutex> = Mutex::new(BTreeMap::new()); static UNCHANGED: AtomicU64 = AtomicU64::new(0); @@ -3817,6 +3930,73 @@ mod tests { assert_eq!(watched_texture_span(usize::MAX - 0x800, 1), None); } + #[test] + fn texture_upload_selection_preserves_flag_and_page_boundaries() { + const PAGE: usize = 0x1000; + let dirty = [0x1000usize, 0x4000]; + for cpu_backed in [false, true] { + for pending in [false, true] { + for gpu_written in [false, true] { + let selected = texture_needs_upload(cpu_backed, pending, gpu_written, || { + let i = dirty.partition_point(|&p| p + PAGE <= 0x1800); + dirty.get(i).is_some_and(|&p| p < 0x1800 + PAGE) + }); + assert_eq!(selected, cpu_backed && (pending || !gpu_written), "cpu {cpu_backed}, pending {pending}, GPU-written {gpu_written}"); + } + } + } + let overlap = |src: usize, dirty: &[usize]| { + let i = dirty.partition_point(|&p| p + PAGE <= src); + dirty.get(i).is_some_and(|&p| p < src + PAGE) + }; + assert!(!texture_needs_upload(true, false, false, || overlap(0x2000, &[0x1000])), "page end equals texture start"); + assert!(!texture_needs_upload(true, false, false, || overlap(0x1000, &[0x2000])), "page start equals texture end"); + assert!(texture_needs_upload(true, false, false, || overlap(0x1800, &[0x1000])), "half-open ranges overlap"); + } + + #[test] + fn texture_upload_selection_reads_dirty_overlap_only_when_needed() { + let calls = std::cell::Cell::new(0); + let selected = |cpu_backed, pending, gpu_written| { + texture_needs_upload(cpu_backed, pending, gpu_written, || { + calls.set(calls.get() + 1); + true + }) + }; + assert!(!selected(false, true, false)); + assert!(selected(true, true, true)); + assert!(!selected(true, false, true)); + assert_eq!(calls.get(), 0, "an excluded or pending entry evaluated its dirty range"); + assert!(selected(true, false, false)); + assert_eq!(calls.get(), 1); + } + + #[cfg(feature = "texture-selection-bench")] + #[test] + fn texture_selection_fixture_keeps_empty_gate_and_exact_aliases() { + assert_eq!(TextureSelectionBench::new(128, TextureSelectionScenario::Empty).scan(), (0, 0)); + assert_eq!(TextureSelectionBench::new(128, TextureSelectionScenario::AllPending).scan().0, 128); + assert_eq!(TextureSelectionBench::new(128, TextureSelectionScenario::AllGpuWritten).scan().0, 0); + let mixed = TextureSelectionBench::new(32, TextureSelectionScenario::MixedAliases); + { + let entries = mixed.entries.lock().unwrap(); + assert_eq!((entries[&1].src, entries[&1].size), (entries[&2].src, entries[&2].size), "alias fixture ranges differ"); + } + let entry = |src| TextureSelectionBenchEntry { + src, + size: TextureSelectionBench::PAGE, + cpu_backed: true, + pending: false, + gpu_written: false, + }; + let aliases = TextureSelectionBench { + entries: Mutex::new(BTreeMap::from([(1, entry(0x1000)), (2, entry(0x1000))])), + dirty: vec![0x1000], + pending: std::sync::atomic::AtomicUsize::new(0), + }; + assert_eq!(aliases.scan(), (2, 3), "exact-range aliases remain distinct candidates"); + } + #[test] fn budgeted_watch_ranges_cover_every_wanted_byte() { let regions = [(20, 20), (80, 30), (45, 10)];