diff --git a/crates/victory-gpu/src/pipeline/host.rs b/crates/victory-gpu/src/pipeline/host.rs index d5343c84b..639893bc3 100644 --- a/crates/victory-gpu/src/pipeline/host.rs +++ b/crates/victory-gpu/src/pipeline/host.rs @@ -621,6 +621,7 @@ impl HostPipeline { }; let name = super::library::name("compute", &root_blob, &[&code_bytes(&code(0))], ""); let made = super::library::pipeline( + super::library::Stage::Compute, &name, // SAFETY: the description points at live data. |library, name| unsafe { library.LoadComputePipeline(name, &desc) }, @@ -770,6 +771,7 @@ impl HostPipeline { return graphics_depth_bounds_pipeline(&host.device, &root, desc, g.depth1(), name); } super::library::pipeline( + super::library::Stage::Graphics, name, // SAFETY: the description points at live data. |library, name| unsafe { library.LoadGraphicsPipeline(name, desc) }, @@ -855,6 +857,7 @@ struct Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE, T); fn stream_pipeline( device: &ID3D12Device, stream: &mut S, + stage: super::library::Stage, name: &str, ) -> windows::core::Result { use windows::core::Interface; @@ -864,6 +867,7 @@ fn stream_pipeline( }; let device = device.cast::()?; super::library::pipeline( + stage, name, // SAFETY: the stream holds whole subobjects pointing at live data. |library, name| unsafe { library.LoadPipeline(name, &desc) }, @@ -925,13 +929,13 @@ fn graphics_depth_bounds_pipeline( }; if d.GS.pShaderBytecode.is_null() { let mut stream = base; - stream_pipeline(device, &mut stream, name) + stream_pipeline(device, &mut stream, super::library::Stage::Graphics, name) } else { let mut stream = StreamGs { base, gs: Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_GS, d.GS), }; - stream_pipeline(device, &mut stream, name) + stream_pipeline(device, &mut stream, super::library::Stage::Graphics, name) } } @@ -987,7 +991,7 @@ fn mesh_pipeline( D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, ), }; - stream_pipeline(device, &mut stream, name) + stream_pipeline(device, &mut stream, super::library::Stage::Mesh, name) }; if depth_bounds { let mut stream = Stream { @@ -1006,7 +1010,7 @@ fn mesh_pipeline( D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, ), }; - stream_pipeline(device, &mut stream, name) + stream_pipeline(device, &mut stream, super::library::Stage::Mesh, name) } else { build(Sub(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL, g.depth)) } diff --git a/crates/victory-gpu/src/pipeline/library.rs b/crates/victory-gpu/src/pipeline/library.rs index 06db5a1e9..81e1bdc0a 100644 --- a/crates/victory-gpu/src/pipeline/library.rs +++ b/crates/victory-gpu/src/pipeline/library.rs @@ -580,66 +580,93 @@ pub(crate) fn name(kind: &str, root: &[u8], code: &[&[u8]], state: &str) -> Stri h.finalize().to_hex()[..32].to_owned() } +mod outcomes; +pub(crate) use outcomes::Stage; + +static TOTALS: Mutex<[outcomes::Totals; 3]> = Mutex::new([const { outcomes::Totals { + bypass: 0, loaded: 0, load_refused: 0, created: 0, create_refused: 0, stored: 0, + store_refused: 0, load_ns: 0, create_ns: 0, store_ns: 0, +} }; 3]); + +fn report_outcomes(now: bool) { + static LAST: Mutex> = Mutex::new(None); + let mut last = LAST.lock().unwrap_or_else(|e| e.into_inner()); + if !now && last.is_some_and(|t| t.elapsed().as_secs() < 20) { return; } + *last = Some(std::time::Instant::now()); + let totals = *TOTALS.lock().unwrap_or_else(|e| e.into_inner()); + drop(last); + for stage in [Stage::Graphics, Stage::Compute, Stage::Mesh] { + let t = totals[stage as usize]; + let attempts = t.bypass + t.loaded + t.load_refused; + if attempts > 0 { + tracing::info!(?stage, attempts, bypass = t.bypass, loaded = t.loaded, load_refused = t.load_refused, + created = t.created, create_refused = t.create_refused, stored = t.stored, store_refused = t.store_refused, + load_ns = t.load_ns, create_ns = t.create_ns, store_ns = t.store_ns, "completed host PSO requests (cumulative)"); + } + } +} + /// The pipeline named `name`: loaded from the library by `load` if it is /// there with the same description, else made by `create` and stored. pub(crate) fn pipeline( + stage: Stage, name: &str, load: impl FnOnce(&ID3D12PipelineLibrary1, PCWSTR) -> windows::core::Result, create: impl FnOnce() -> windows::core::Result, ) -> windows::core::Result { let wide = HSTRING::from(name); - let Some(library) = library().filter(|_| !STATIC_SAMPLERS.get()) else { - MADE.fetch_add(1, Ordering::Relaxed); - return create(); - }; + let library = library().filter(|_| !STATIC_SAMPLERS.get()); let began = std::time::Instant::now(); - let loaded = { - let _all = one_call(); - let _one = same_name(name); - let _call = call("load", name); - load(&library.library, PCWSTR(wide.as_ptr())) - }; - // The load read the library's file (see `trim`). - LOADED_AT.store(crate::host::now_ms(), Ordering::Relaxed); - TRIMMED.store(false, Ordering::Relaxed); - let refused = match loaded { - Ok(pso) => { - LOADED.fetch_add(1, Ordering::Relaxed); + let (result, counts) = outcomes::resolve( + library, + |library| { + let loaded = { + let _all = one_call(); + let _one = same_name(name); + let _call = call("load", name); + load(&library.library, PCWSTR(wide.as_ptr())) + }; + LOADED_AT.store(crate::host::now_ms(), Ordering::Relaxed); + TRIMMED.store(false, Ordering::Relaxed); + if loaded.is_ok() { + crate::host::trail(|| { + let label = LABEL.with(|l| l.borrow().clone()); + format!("pipeline library: loaded {label} in {:.1} ms", began.elapsed().as_secs_f64() * 1e3) + }); + } + loaded + }, + |refused| { + let Some(refused) = refused else { return create() }; + // No library lock is held during compilation. + let pso = { let _call = call("make", name); create()? }; crate::host::trail(|| { let label = LABEL.with(|l| l.borrow().clone()); - format!("pipeline library: loaded {label} in {:.1} ms", began.elapsed().as_secs_f64() * 1e3) + format!("pipeline library: made {label} in {:.1} ms (the library did not load it: {refused})", + began.elapsed().as_secs_f64() * 1e3) }); - return Ok(pso); - } - Err(e) => e, - }; - // Compiled outside any lock: compiles take up to seconds. - let pso = { - let _call = call("make", name); - create()? - }; - MADE.fetch_add(1, Ordering::Relaxed); - crate::host::trail(|| { - let label = LABEL.with(|l| l.borrow().clone()); - format!( - "pipeline library: made {label} in {:.1} ms (the library did not load it: {refused})", - began.elapsed().as_secs_f64() * 1e3 - ) - }); - let _all = one_call(); - let _stores = library.saving.read().unwrap_or_else(|e| e.into_inner()); - let _call = call("store", name); - // SAFETY: a live pipeline; a name already stored (a description that - // differs) is refused, which leaves the library as it was. - if unsafe { library.library.StorePipeline(PCWSTR(wide.as_ptr()), &pso) }.is_ok() { - library.added.fetch_add(1, Ordering::Relaxed); - } - Ok(pso) + Ok(pso) + }, + |library, pso| { + let _all = one_call(); + let _stores = library.saving.read().unwrap_or_else(|e| e.into_inner()); + let _call = call("store", name); + // SAFETY: the name and the pipeline live through the call. + let stored = unsafe { library.library.StorePipeline(PCWSTR(wide.as_ptr()), pso) }.is_ok(); + if stored { library.added.fetch_add(1, Ordering::Relaxed); } + stored + }, + ); + MADE.fetch_add(counts.created, Ordering::Relaxed); + LOADED.fetch_add(counts.loaded, Ordering::Relaxed); + TOTALS.lock().unwrap_or_else(|e| e.into_inner())[stage as usize].add(counts); + result } /// Writes the library and the early list if pipelines were added since the /// last save and `SAVE_EVERY` has passed (or `now`). pub(crate) fn save(now: bool) { + report_outcomes(now); static LAST: Mutex> = Mutex::new(None); const SAVE_EVERY: std::time::Duration = std::time::Duration::from_secs(20); let (Some(dir), Some(library)) = (DIR.get(), library()) else { return }; @@ -872,6 +899,7 @@ mod tests { { let _guard = with_static_samplers(true); let made = pipeline( + Stage::Graphics, "with static samplers", |_, _| { looked.set(true); diff --git a/crates/victory-gpu/src/pipeline/library/outcomes.rs b/crates/victory-gpu/src/pipeline/library/outcomes.rs new file mode 100644 index 000000000..554d98d4f --- /dev/null +++ b/crates/victory-gpu/src/pipeline/library/outcomes.rs @@ -0,0 +1,147 @@ +//! One record per completed host pipeline request, including bypasses and refusals. +use std::time::Instant; + +#[derive(Copy, Clone, Debug)] +pub(crate) enum Stage { + Graphics, + Compute, + Mesh, +} + +#[derive(Copy, Clone, Default, Debug)] +pub(super) struct Totals { + pub bypass: u64, + pub loaded: u64, + pub load_refused: u64, + pub created: u64, + pub create_refused: u64, + pub stored: u64, + pub store_refused: u64, + pub load_ns: u64, + pub create_ns: u64, + pub store_ns: u64, +} + +impl Totals { + pub fn add(&mut self, r: Self) { + self.bypass += r.bypass; + self.loaded += r.loaded; + self.load_refused += r.load_refused; + self.created += r.created; + self.create_refused += r.create_refused; + self.stored += r.stored; + self.store_refused += r.store_refused; + self.load_ns += r.load_ns; + self.create_ns += r.create_ns; + self.store_ns += r.store_ns; + } +} + +pub(super) fn resolve( + library: Option<&L>, + load: impl FnOnce(&L) -> Result, + create: impl FnOnce(Option<&E>) -> Result, + store: impl FnOnce(&L, &T) -> bool, +) -> (Result, Totals) { + let mut counts = Totals::default(); + let miss = if let Some(library) = library { + let start = Instant::now(); + let result = load(library); + counts.load_ns = start.elapsed().as_nanos() as u64; + match result { + Ok(pso) => { + counts.loaded = 1; + return (Ok(pso), counts); + } + Err(error) => { + counts.load_refused = 1; + Some(error) + } + } + } else { + counts.bypass = 1; + None + }; + let start = Instant::now(); + let result = create(miss.as_ref()); + counts.create_ns = start.elapsed().as_nanos() as u64; + match &result { + Ok(pso) => { + counts.created = 1; + if let Some(library) = library { + let start = Instant::now(); + if store(library, pso) { + counts.stored = 1; + } else { + counts.store_refused = 1; + } + counts.store_ns = start.elapsed().as_nanos() as u64; + } + } + Err(_) => counts.create_refused = 1, + } + (result, counts) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn every_route_preserves_the_result_and_accounts_for_each_call() { + let mut total = Totals::default(); + for named in [false, true] { + for hit in [false, true] { + for accepted in [false, true] { + for stored in [false, true] { + let (pso, r) = resolve( + named.then_some(&()), + |_| { + assert!(named); + if hit { Ok(11) } else { Err("load") } + }, + |miss| { + assert!(!named || !hit); + assert_eq!(miss.copied(), named.then_some("load")); + if accepted { Ok(22) } else { Err("create") } + }, + |_, p| { + assert!(named && !hit && accepted); + assert_eq!(*p, 22); + stored + }, + ); + assert_eq!( + pso, + if named && hit { + Ok(11) + } else if accepted { + Ok(22) + } else { + Err("create") + } + ); + assert_eq!(r.bypass + r.load_refused + r.loaded, 1); + assert_eq!(r.bypass + r.load_refused, r.created + r.create_refused); + assert_eq!( + r.stored + r.store_refused, + u64::from(named && !hit && accepted) + ); + total.add(r); + } + } + } + } + assert_eq!( + ( + total.bypass, + total.loaded, + total.load_refused, + total.created, + total.create_refused, + total.stored, + total.store_refused + ), + (8, 4, 4, 6, 6, 1, 1) + ); + } +}