From 2024577102548b3e26a44ad3e1ab300cc486c226 Mon Sep 17 00:00:00 2001 From: Zhelezniak Artem Date: Thu, 26 Nov 2026 10:09:55 +0300 Subject: [PATCH 1/2] GPU: count selected direct and indirect compute dispatch recordings --- crates/victory-gpu/src/draw.rs | 22 ++++++++ crates/victory-gpu/src/pipeline.rs | 80 ++++++++++++++++++++++++++++++ crates/victory-gpu/src/queue.rs | 2 + 3 files changed, 104 insertions(+) diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index 2900faa5d2..197ffb1222 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -2345,6 +2345,12 @@ impl State { /// Runs a dispatch on the host. pub(crate) fn dispatch(&mut self, list: &HostList, x: u32, y: u32, z: u32, flags: Option) { + #[cfg(feature = "diagnostics")] + let trace = crate::pipeline::dispatch_trace(self.pipeline as This); + #[cfg(feature = "diagnostics")] + if let Some(trace) = trace { + trace.attempt(false); + } // It may write the arguments a window holds clamped. self.window = None; let builder = crate::pipeline::is_builder(self.pipeline as This); @@ -2450,6 +2456,10 @@ impl State { unsafe { list.list.Dispatch(x, y, z) }; } } + #[cfg(feature = "diagnostics")] + if let Some(trace) = trace { + trace.record(false); + } if let Some((param, word, _)) = tail { // Later ordinary or indirect dispatches use all lanes. unsafe { list.list.SetComputeRoot32BitConstant(param, 0, word) }; @@ -3378,7 +3388,19 @@ impl State { arguments: u64, count: Option, ) -> bool { + #[cfg(feature = "diagnostics")] + let trace = (kind == 2 && max != 0) + .then(|| crate::pipeline::dispatch_trace(self.pipeline as This)) + .flatten(); + #[cfg(feature = "diagnostics")] + if let Some(trace) = trace { + trace.attempt(true); + } let ran = self.run_indirect(list, (kind, stride, max), arguments, count); + #[cfg(feature = "diagnostics")] + if ran && let Some(trace) = trace { + trace.record(true); + } if kind == 2 && max != 0 && crate::pipeline::is_builder(self.pipeline as This) { if !ran { crate::pipeline::builder_off("an indirect builder dispatch could not run"); diff --git a/crates/victory-gpu/src/pipeline.rs b/crates/victory-gpu/src/pipeline.rs index 63b3c16a36..e1c5cdcaa9 100644 --- a/crates/victory-gpu/src/pipeline.rs +++ b/crates/victory-gpu/src/pipeline.rs @@ -500,6 +500,72 @@ fn skipped_shader(p: &Pipeline) -> Option<&str> { .map(|shader| shader.hash.as_str()) } +/// Command recording counts for a selected compute shader. An indirect +/// recording can still read a count of zero when it runs on the GPU. +#[cfg(all(windows, feature = "diagnostics"))] +#[derive(Default)] +pub(crate) struct DispatchTrace { + direct_attempted: std::sync::atomic::AtomicU64, + direct_recorded: std::sync::atomic::AtomicU64, + indirect_attempted: std::sync::atomic::AtomicU64, + indirect_recorded: std::sync::atomic::AtomicU64, +} + +#[cfg(all(windows, feature = "diagnostics"))] +impl DispatchTrace { + pub(crate) fn attempt(&self, indirect: bool) { + let counter = if indirect { &self.indirect_attempted } else { &self.direct_attempted }; + counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } + + pub(crate) fn record(&self, indirect: bool) { + let counter = if indirect { &self.indirect_recorded } else { &self.direct_recorded }; + counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } + + fn counts(&self) -> [u64; 4] { + [&self.direct_attempted, &self.direct_recorded, + &self.indirect_attempted, &self.indirect_recorded] + .map(|counter| counter.load(std::sync::atomic::Ordering::Relaxed)) + } +} + +/// VICTORY_TRACE_DISPATCHES takes a comma-separated list of compute hashes. +/// The normal path has no counters or log output when the list is empty. +#[cfg(all(windows, feature = "diagnostics"))] +fn dispatch_traces() -> &'static [(String, DispatchTrace)] { + static TRACES: OnceLock> = OnceLock::new(); + TRACES.get_or_init(|| { + let value = std::env::var("VICTORY_TRACE_DISPATCHES").unwrap_or_default(); + let mut hashes = parse_shader_hashes(&value, "VICTORY_TRACE_DISPATCHES"); + hashes.sort_unstable(); + hashes.dedup(); + hashes.into_iter().map(|hash| (hash, DispatchTrace::default())).collect() + }) +} + +#[cfg(all(windows, feature = "diagnostics"))] +pub(crate) fn dispatch_trace(this: This) -> Option<&'static DispatchTrace> { + let traces = dispatch_traces(); + if traces.is_empty() || this.is_null() { + return None; + } + let shader = pipeline(this).shaders.first().filter(|s| s.stage == "compute")?; + let at = traces.binary_search_by(|(hash, _)| hash.cmp(&shader.hash)).ok()?; + Some(&traces[at].1) +} + +#[cfg(all(windows, feature = "diagnostics"))] +pub(crate) fn report_dispatch_traces(frame: u64) { + if frame == 1 || frame.is_multiple_of(64) { + for (hash, trace) in dispatch_traces() { + let [direct_attempted, direct_recorded, indirect_attempted, indirect_recorded] = trace.counts(); + tracing::info!(%hash, frame, direct_attempted, direct_recorded, + indirect_attempted, indirect_recorded, "selected compute dispatch recording totals"); + } + } +} + /// Recording-time trace, independent of the present and heavy-frame capture. /// The budget is per shader so that a frequent stage cannot silence a later stage. #[cfg(all(windows, feature = "diagnostics"))] @@ -989,6 +1055,20 @@ mod tests { ); } + #[cfg(all(windows, feature = "diagnostics"))] + #[test] + fn dispatch_trace_distinguishes_attempts_from_recordings() { + let trace = super::DispatchTrace::default(); + assert_eq!(trace.counts(), [0, 0, 0, 0]); + trace.attempt(false); + trace.attempt(false); + trace.record(false); + trace.attempt(true); + assert_eq!(trace.counts(), [2, 1, 1, 0]); + trace.record(true); + assert_eq!(trace.counts(), [2, 1, 1, 1]); + } + use super::*; use crate::object::{S_OK, slot}; diff --git a/crates/victory-gpu/src/queue.rs b/crates/victory-gpu/src/queue.rs index 32a8387a33..3a853bd549 100644 --- a/crates/victory-gpu/src/queue.rs +++ b/crates/victory-gpu/src/queue.rs @@ -780,6 +780,8 @@ extern "system" fn present( release_held(forces()); let frame = FRAMES.fetch_add(1, Ordering::Relaxed) + 1; #[cfg(all(windows, feature = "diagnostics"))] + crate::pipeline::report_dispatch_traces(frame); + #[cfg(all(windows, feature = "diagnostics"))] if frame.is_power_of_two() && std::env::var_os("VICTORY_TRACE_PRESENT_PLANES").is_some() && planes > 0 From 5e107818b48fe94edba883e7abc8fcbd9c42d42c Mon Sep 17 00:00:00 2001 From: Zhelezniak Artem Date: Thu, 26 Nov 2026 10:17:21 +0300 Subject: [PATCH 2/2] GPU: report dispatch totals before a fatal host failure --- crates/victory-gpu/src/host.rs | 2 ++ crates/victory-gpu/src/pipeline.rs | 21 +++++++++++---------- crates/victory-gpu/src/queue.rs | 4 +++- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 4b672dfaca..b02a55a5e6 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -521,6 +521,8 @@ pub fn on_failure(report: fn(&str) -> !) { /// the debug layer's first error, if any (`VICTORY_D3D12_DEBUG=1` in a /// diagnostics build names the command). pub(crate) fn fail(what: std::fmt::Arguments) -> ! { + #[cfg(feature = "diagnostics")] + crate::pipeline::report_dispatch_traces(crate::queue::presented()); let mut text = what.to_string(); if let Some(host) = host() { // SAFETY: a plain query. diff --git a/crates/victory-gpu/src/pipeline.rs b/crates/victory-gpu/src/pipeline.rs index e1c5cdcaa9..24269d35d5 100644 --- a/crates/victory-gpu/src/pipeline.rs +++ b/crates/victory-gpu/src/pipeline.rs @@ -520,13 +520,16 @@ impl DispatchTrace { pub(crate) fn record(&self, indirect: bool) { let counter = if indirect { &self.indirect_recorded } else { &self.direct_recorded }; - counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + counter.fetch_add(1, std::sync::atomic::Ordering::Release); } fn counts(&self) -> [u64; 4] { - [&self.direct_attempted, &self.direct_recorded, - &self.indirect_attempted, &self.indirect_recorded] - .map(|counter| counter.load(std::sync::atomic::Ordering::Relaxed)) + use std::sync::atomic::Ordering::{Acquire, Relaxed}; + // Observe a recorded command before reading the attempt count. + let direct_recorded = self.direct_recorded.load(Acquire); + let indirect_recorded = self.indirect_recorded.load(Acquire); + [self.direct_attempted.load(Relaxed), direct_recorded, + self.indirect_attempted.load(Relaxed), indirect_recorded] } } @@ -557,12 +560,10 @@ pub(crate) fn dispatch_trace(this: This) -> Option<&'static DispatchTrace> { #[cfg(all(windows, feature = "diagnostics"))] pub(crate) fn report_dispatch_traces(frame: u64) { - if frame == 1 || frame.is_multiple_of(64) { - for (hash, trace) in dispatch_traces() { - let [direct_attempted, direct_recorded, indirect_attempted, indirect_recorded] = trace.counts(); - tracing::info!(%hash, frame, direct_attempted, direct_recorded, - indirect_attempted, indirect_recorded, "selected compute dispatch recording totals"); - } + for (hash, trace) in dispatch_traces() { + let [direct_attempted, direct_recorded, indirect_attempted, indirect_recorded] = trace.counts(); + tracing::info!(%hash, frame, direct_attempted, direct_recorded, + indirect_attempted, indirect_recorded, "selected compute dispatch recording totals"); } } diff --git a/crates/victory-gpu/src/queue.rs b/crates/victory-gpu/src/queue.rs index 3a853bd549..21368d053d 100644 --- a/crates/victory-gpu/src/queue.rs +++ b/crates/victory-gpu/src/queue.rs @@ -780,7 +780,9 @@ extern "system" fn present( release_held(forces()); let frame = FRAMES.fetch_add(1, Ordering::Relaxed) + 1; #[cfg(all(windows, feature = "diagnostics"))] - crate::pipeline::report_dispatch_traces(frame); + if frame == 1 || frame.is_multiple_of(64) { + crate::pipeline::report_dispatch_traces(frame); + } #[cfg(all(windows, feature = "diagnostics"))] if frame.is_power_of_two() && std::env::var_os("VICTORY_TRACE_PRESENT_PLANES").is_some()