Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions crates/victory-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>) {
#[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);
Expand Down Expand Up @@ -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) };
Expand Down Expand Up @@ -3378,7 +3388,19 @@ impl State {
arguments: u64,
count: Option<u64>,
) -> 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");
Expand Down
2 changes: 2 additions & 0 deletions crates/victory-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
81 changes: 81 additions & 0 deletions crates/victory-gpu/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,73 @@ 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::Release);
}

fn counts(&self) -> [u64; 4] {
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]
}
}

/// 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<Vec<(String, DispatchTrace)>> = 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) {
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"))]
Expand Down Expand Up @@ -989,6 +1056,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};

Expand Down
4 changes: 4 additions & 0 deletions crates/victory-gpu/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,10 @@ extern "system" fn present(
release_held(forces());
let frame = FRAMES.fetch_add(1, Ordering::Relaxed) + 1;
#[cfg(all(windows, feature = "diagnostics"))]
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()
&& planes > 0
Expand Down