use std::sync::atomic::{AtomicU64, Ordering::Relaxed}; use windows::Win32::Graphics::Direct3D12::*; #[inline] pub(crate) fn on() -> bool { static ON: std::sync::OnceLock = std::sync::OnceLock::new(); *ON.get_or_init(|| crate::switch::on("EONACAT_XS_BARRIER_STATS")) } const CALLS: usize = 0; const GLOBAL_UAV: usize = 1; const RES_UAV: usize = 2; const TRANSITION: usize = 3; const ALIASING: usize = 4; const ENH_CALLS: usize = 5; const ENH_GLOBAL: usize = 6; const ENH_BUFFER: usize = 7; const ENH_TEXTURE: usize = 8; const DISPATCH_DIRECT: usize = 9; const DISPATCH_INDIRECT: usize = 10; const TITLE_WAITS: usize = 11; const REDUNDANT: usize = 12; const NEEDED: usize = 13; const BACK_TO_BACK: usize = 14; const UNKNOWN_BINDING: usize = 15; const UNKNOWN_BETWEEN: usize = 16; const UNKNOWN_END: usize = 17; const COUNTERS: usize = 18; const BUCKETS: usize = 7; const RUNS: usize = COUNTERS; const RUNS_IF_DROPPED: usize = COUNTERS + BUCKETS; const SLOTS: usize = COUNTERS + 2 * BUCKETS; static COUNT: [AtomicU64; SLOTS] = [const { AtomicU64::new(0) }; SLOTS]; fn add(slot: usize, n: u64) { COUNT[slot].fetch_add(n, Relaxed); } fn bucket(run: u32) -> usize { match run { 0..=1 => 0, 2 => 1, 3..=4 => 2, 5..=8 => 3, 9..=16 => 4, 17..=32 => 5, _ => 6, } } pub(crate) unsafe fn rb(list: &ID3D12GraphicsCommandList, barriers: &[D3D12_RESOURCE_BARRIER]) { if on() { add(CALLS, 1); for b in barriers { match b.Type { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION => add(TRANSITION, 1), D3D12_RESOURCE_BARRIER_TYPE_ALIASING => add(ALIASING, 1), D3D12_RESOURCE_BARRIER_TYPE_UAV => { if unsafe { &b.Anonymous.UAV }.pResource.is_none() { add(GLOBAL_UAV, 1); } else { add(RES_UAV, 1); } } _ => {} } } } unsafe { list.ResourceBarrier(barriers) }; } pub(crate) fn enhanced(globals: usize, buffers: usize, textures: usize) { if on() { add(ENH_CALLS, 1); add(ENH_GLOBAL, globals as u64); add(ENH_BUFFER, buffers as u64); add(ENH_TEXTURE, textures as u64); } } #[derive(Clone, Copy, PartialEq)] pub(crate) enum Key { Range(u64, u64), Texture(usize), } impl Key { fn overlaps(self, other: Key) -> bool { match (self, other) { (Key::Range(a, b), Key::Range(c, d)) => a < d && c < b, (Key::Texture(a), Key::Texture(b)) => a == b, _ => false, } } } #[derive(Default)] pub(crate) struct Access(pub(crate) Vec<(Key, bool)>); impl Access { pub(crate) fn range(&mut self, at: u64, len: u64, write: bool) { self.0.push((Key::Range(at, at.saturating_add(len.max(1))), write)); } } #[derive(Clone, Copy)] enum Why { Binding, Between, End, } pub(crate) struct Track { pending: Vec<(Key, bool)>, unknown: bool, waiting: u32, waiting_global: bool, run: u32, run_if_dropped: u32, } impl Default for Track { fn default() -> Self { Track { pending: Vec::new(), unknown: true, waiting: 0, waiting_global: false, run: 0, run_if_dropped: 0 } } } impl Track { pub(crate) fn wait(&mut self, global: bool) { add(TITLE_WAITS, 1); if self.run > 0 { add(RUNS + bucket(self.run), 1); self.run = 0; } self.waiting += 1; self.waiting_global |= global; } pub(crate) fn dispatch(&mut self, access: Option, indirect: bool) { add(if indirect { DISPATCH_INDIRECT } else { DISPATCH_DIRECT }, 1); if self.waiting > 0 { match &access { None => self.settle(Some(Why::Binding)), Some(_) if self.unknown => self.settle(Some(Why::Between)), Some(a) => { let conflict = a.0.iter().any(|&(k, w)| self.pending.iter().any(|&(p, pw)| (w || pw) && k.overlaps(p))); if conflict { self.settle(None); } else { add(REDUNDANT, u64::from(self.waiting)); self.waiting = 0; } } } } self.run += 1; self.run_if_dropped += 1; match access { Some(a) => { for (k, w) in a.0 { match self.pending.iter_mut().find(|(p, _)| *p == k) { Some(p) => p.1 |= w, None => self.pending.push((k, w)), } } if self.pending.len() > 512 { self.pending.clear(); self.unknown = true; } } None => self.unknown = true, } } pub(crate) fn other_work(&mut self) { if self.waiting > 0 { self.settle(Some(Why::Between)); } self.unknown = true; } fn settle(&mut self, why: Option) { let n = u64::from(self.waiting); match why { None => add(NEEDED, 1), Some(Why::Binding) => add(UNKNOWN_BINDING, 1), Some(Why::Between) => add(UNKNOWN_BETWEEN, 1), Some(Why::End) => add(UNKNOWN_END, 1), } add(BACK_TO_BACK, n - 1); if self.waiting_global { self.pending.clear(); self.unknown = false; } if self.run_if_dropped > 0 { add(RUNS_IF_DROPPED + bucket(self.run_if_dropped), 1); self.run_if_dropped = 0; } self.waiting = 0; self.waiting_global = false; } } impl Drop for Track { fn drop(&mut self) { if !on() { return; } if self.waiting > 0 { self.settle(Some(Why::End)); } if self.run > 0 { add(RUNS + bucket(self.run), 1); } if self.run_if_dropped > 0 { add(RUNS_IF_DROPPED + bucket(self.run_if_dropped), 1); } } } pub(crate) fn log(frames: f64) { if !on() { return; } let c: Vec = COUNT.iter().map(|c| c.swap(0, Relaxed) as f64 / frames).collect(); let hist = |at: usize| { const NAMES: [&str; BUCKETS] = ["1", "2", "3-4", "5-8", "9-16", "17-32", "33+"]; (0..BUCKETS).map(|i| format!("{}:{:.1}", NAMES[i], c[at + i])).collect::>().join(" ") }; tracing::info!( "barrier stats per frame (EONACAT_XS_BARRIER_STATS=1): ResourceBarrier calls {:.0} with barriers: global UAV {:.0}, \ per-resource UAV {:.0}, transition {:.0}, aliasing {:.0}; enhanced Barrier calls {:.0} with global {:.0}, buffer {:.0}, \ texture {:.0}; dispatches {:.0} direct + {:.0} indirect; title UAV-type waits (barrier, barrier group, pipeline sync) \ {:.0}: redundant {:.0} (the next dispatch touches nothing the dispatches since the last needed wait touched, unless \ both only read it; hazards counted: read after write, write after write, write after read), needed {:.0} (it does), \ unknown {:.0} bindings + {:.0} draw/copy/list start between + {:.0} at list end (counted as needed), back-to-back \ repeats of a needed wait {:.0}; dispatches per run with no title wait between: [{}]; if redundant waits were \ dropped: [{}]; unknown = by-address, dynamically indexed or placed views; host-made waits (clamp passes, copies) \ are in the counts but not judged", c[CALLS], c[GLOBAL_UAV], c[RES_UAV], c[TRANSITION], c[ALIASING], c[ENH_CALLS], c[ENH_GLOBAL], c[ENH_BUFFER], c[ENH_TEXTURE], c[DISPATCH_DIRECT], c[DISPATCH_INDIRECT], c[TITLE_WAITS], c[REDUNDANT], c[NEEDED], c[UNKNOWN_BINDING], c[UNKNOWN_BETWEEN], c[UNKNOWN_END], c[BACK_TO_BACK], hist(RUNS), hist(RUNS_IF_DROPPED), ); }