Skip to content
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ A diagnostics build also logs one bounded line per submission that refreshes tit
| `VICTORY_STACKS_FILE=<path>` | Writes those dumps to a file without taking locks, for freezes. |
| `VICTORY_SKIP_TITLE_INT3=<rva>,...` | Steps over the game's own `int3` asserts at those hex RVAs and logs each one. Every other assert still stops the run. |
| `VICTORY_GPU_TIMING=1` | Submits and times each GPU list on its own. |
| `VICTORY_GPU_TIMING_CUTS=sweep` | GPU time per pipeline on a Mac, where GPU timestamps read 0: a few pipelines at a time get host lists of their own, timed by the clock, round all of them in a few minutes. `=<hash>,...` times the pipelines of those shaders only. Diagnostics builds. |

### Every variable

Expand All @@ -184,6 +185,7 @@ The on/off switches below (`...=1`) are off when unset or set to `0`.
| `VICTORY_D3D12_DRED=1` | on a GPU failure, the allocation hit and the commands in flight |
| `VICTORY_SKIP_PIPELINES=<hash>,...` | skip host pipeline creation and omit draws and dispatches, including indirect ones, when the pipeline contains a listed 16-digit shader hash |
| `VICTORY_GPU_TIMING=1` | with the above, each list of a submission run and timed on its own; slow lists logged with their slowest commands; a device removal names the list it happened in and lists all its commands |
| `VICTORY_GPU_TIMING_CUTS=sweep` | GPU time per pipeline without GPU timestamps (D3DMetal reads them all as 0): turns `VICTORY_GPU_TIMING` on, and a title list goes on in a fresh host list around the draws and dispatches of a few pipelines at a time, so each such list runs one pipeline and its wall time is that pipeline's; everything else stays together and the frame keeps its shape. `sweep` (or `sweep:<n>`, 16 by default) takes `n` at a time of the pipelines the title is drawing with, in the order it first drew with them, and moves on after every frame-rate line, so standing still for a few minutes times every pipeline of the scene; `<hash>,...` (16-digit shader hashes) takes the pipelines with a listed shader, for as long as the run lasts. Every frame-rate line is followed by `GPU wall time by pipeline`: lists and ms a frame, the lists' floor (tenth percentile, what an empty list costs), how far the sweep is, then the pipelines, each per frame it had lists in: ms, ms above the floor, lists, commands, and those frames |
| `VICTORY_TRACE_SHADERS=<hash>,...` | with `VICTORY_D3D12_DRED`, every draw and dispatch of a listed shader logged with what it binds (user data, heap slots, root views and their first dwords; the first 500, and all of them in listed capture frames); the records of its indirect executions are read back and logged in every frame (records with work, the first four, the frame) |
| `VICTORY_D3D12_WARP=1` | host device on the software adapter (slow; cannot reset the driver) |
| `VICTORY_PIPELINE_LOG=<file>` | a line per title pipeline created (key, shaders, state), first used, and made or loaded (library name); `translate --cache --pipelines` writes the last kind too |
Expand Down
6 changes: 6 additions & 0 deletions crates/victory-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,12 @@ impl State {
}
}

/// The title pipeline the next draw or dispatch runs (null: none set).
#[cfg(feature = "diagnostics")]
pub(crate) fn title_pipeline(&self) -> usize {
self.pipeline
}

/// Flushes any pending RTV probes recorded during `VICTORY_HDR_PROBE`.
pub(crate) fn flush_hdr_probe(&mut self, list: &HostList) {
let Some((targets, shaders)) = self.hdr_rtvs.take() else {
Expand Down
171 changes: 170 additions & 1 deletion crates/victory-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,10 @@ pub(crate) struct HostList {
/// and at the end, and where they are read back.
#[cfg(feature = "diagnostics")]
stamps: OnceLock<Option<(ID3D12QueryHeap, ID3D12Resource)>>,
/// With `VICTORY_GPU_TIMING_CUTS`: the one kind and pipeline of the
/// list's draws or dispatches, once it has any (`crate::timing_cuts`).
#[cfg(feature = "diagnostics")]
timed: Mutex<Option<crate::timing_cuts::Timed>>,
}

// SAFETY: D3D12 objects are free-threaded; a host list is recorded by the
Expand Down Expand Up @@ -1603,7 +1607,109 @@ pub(crate) fn dred_on() -> bool {
#[cfg(feature = "diagnostics")]
fn timing() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| crate::switch::on("VICTORY_GPU_TIMING"))
*ON.get_or_init(|| crate::switch::on("VICTORY_GPU_TIMING") || timing_cuts_on())
}

/// Whether a few pipelines at a time get host lists of their own, so that
/// a timed list of theirs runs one pipeline and its wall time is that
/// pipeline's (`VICTORY_GPU_TIMING_CUTS`, diagnostics builds; turns the
/// timing on): GPU time per pipeline where timestamps do not advance, as
/// under D3DMetal. See `crate::timing_cuts`.
#[cfg(feature = "diagnostics")]
pub(crate) fn timing_cuts_on() -> bool {
*timing_cuts() != crate::timing_cuts::Mode::Off
}

#[cfg(feature = "diagnostics")]
fn timing_cuts() -> &'static crate::timing_cuts::Mode {
static MODE: OnceLock<crate::timing_cuts::Mode> = OnceLock::new();
MODE.get_or_init(|| {
let mode = crate::timing_cuts::mode_of(std::env::var("VICTORY_GPU_TIMING_CUTS").ok().as_deref());
if mode != crate::timing_cuts::Mode::Off {
tracing::info!(
?mode,
"VICTORY_GPU_TIMING_CUTS: host lists of their own for a few pipelines at a time, each list \
run and timed on its own"
);
}
mode
})
}

/// A sweep's pipelines and where it is (`VICTORY_GPU_TIMING_CUTS=sweep`).
#[cfg(feature = "diagnostics")]
static SWEEP: Mutex<Option<crate::timing_cuts::Sweep>> = Mutex::new(None);

/// Whether the title pipeline `this` gets host lists of its own now: it
/// has a listed shader (remembered per pipeline, which the title makes
/// once and keeps), or the sweep is at it.
#[cfg(feature = "diagnostics")]
pub(crate) fn timing_cut_own(this: usize) -> bool {
use crate::timing_cuts::Mode;
if this == 0 {
return false;
}
match timing_cuts() {
Mode::Off => false,
Mode::Sweep(_) => SWEEP
.lock()
.unwrap_or_else(|e| e.into_inner())
.get_or_insert_with(Default::default)
.times(this),
Mode::Listed(listed) => {
static SEEN: Mutex<Option<std::collections::HashMap<usize, bool>>> = Mutex::new(None);
let mut seen = SEEN.lock().unwrap_or_else(|e| e.into_inner());
*seen
.get_or_insert_with(Default::default)
.entry(this)
.or_insert_with(|| {
crate::timing_cuts::lists(
listed,
&crate::pipeline::shaders_of(this as crate::object::This),
)
})
}
}
}

/// The timed lists' wall time by pipeline since the last frame-rate line
/// (`VICTORY_GPU_TIMING_CUTS`).
#[cfg(feature = "diagnostics")]
static GPU_WALLS: Mutex<Option<crate::timing_cuts::Walls>> = Mutex::new(None);

/// Logs [`GPU_WALLS`] over `frames` frames: the 40 pipelines that cost most
/// above the lists' floor, each per frame it had lists in.
#[cfg(feature = "diagnostics")]
fn log_gpu_walls(frames: f64) {
let Some(walls) = GPU_WALLS.lock().unwrap_or_else(|e| e.into_inner()).take() else {
return;
};
let (lists, ms, floor, rows) = walls.report(frames);
let sweep = match timing_cuts() {
crate::timing_cuts::Mode::Sweep(n) => {
let mut sweep = SWEEP.lock().unwrap_or_else(|e| e.into_inner());
let (now, live) = sweep.get_or_insert_with(Default::default).advance(*n);
format!("; the sweep goes on with {now} of the {live} pipelines drawn with")
}
_ => String::new(),
};
tracing::info!(
"GPU wall time by pipeline: {lists:.1} timed lists/frame, {ms:.1} ms/frame; a list's floor {floor:.3} ms \
(tenth percentile); {} lines{sweep}",
rows.len()
);
for r in rows.iter().take(40) {
tracing::info!(
" GPU wall {:.2} ms ({:.2} above the floor) in {:.1} lists, {:.1} commands a frame, \
in {} frames: {}",
r.ms,
r.above,
r.lists,
r.commands,
r.frames,
r.label
);
}
}

/// On D3DMetal, a separate native queue call before a deferred UAV-to-SRV
Expand Down Expand Up @@ -2123,6 +2229,8 @@ impl HostGpu for Host {
targets: Mutex::default(),
#[cfg(feature = "diagnostics")]
stamps: OnceLock::new(),
#[cfg(feature = "diagnostics")]
timed: Mutex::default(),
})
}
}
Expand Down Expand Up @@ -2303,6 +2411,19 @@ impl Host {
);
}
#[cfg(feature = "diagnostics")]
if gpu_timing && timing_cuts_on() {
let timed = runs[i].timed.lock().unwrap_or_else(|e| e.into_inner());
GPU_WALLS
.lock()
.unwrap_or_else(|e| e.into_inner())
.get_or_insert_with(Default::default)
.add(
started.elapsed().as_secs_f64() * 1e3,
timed.as_ref(),
crate::queue::presented(),
);
}
#[cfg(feature = "diagnostics")]
if groups.len() > 1 && started.elapsed() > std::time::Duration::from_millis(50) {
// SAFETY: a plain query.
let frequency = unsafe { queue.GetTimestampFrequency() }.unwrap_or(1);
Expand Down Expand Up @@ -3335,6 +3456,8 @@ impl HostList {
self.notes.lock().unwrap_or_else(|e| e.into_inner()).clear();
#[cfg(feature = "diagnostics")]
self.targets.lock().unwrap_or_else(|e| e.into_inner()).clear();
#[cfg(feature = "diagnostics")]
self.timed.lock().unwrap_or_else(|e| e.into_inner()).take();
self.trim_scratch();
self.reset_hoisted_clamps();
self.marked.store(false, Ordering::Relaxed);
Expand Down Expand Up @@ -3909,6 +4032,51 @@ impl HostList {
}
}

/// With `VICTORY_GPU_TIMING_CUTS`: whether a draw or dispatch of `key`
/// (`crate::timing_cuts::key`) belongs in a host list after this one,
/// which runs another kind or pipeline.
#[cfg(feature = "diagnostics")]
pub(crate) fn timed_cuts(&self, (key, compute): (usize, bool)) -> bool {
let timed = self.timed.lock().unwrap_or_else(|e| e.into_inner());
crate::timing_cuts::cuts(timed.as_ref(), key, compute)
}

/// Counts a draw or dispatch of `key` about to be recorded into the
/// list, whose kind and pipeline it gives the list if it is the first
/// (`name`: the pipeline's shaders).
#[cfg(feature = "diagnostics")]
pub(crate) fn timed_command(
&self,
(key, compute): (usize, bool),
name: impl FnOnce() -> String,
) {
let mut timed = self.timed.lock().unwrap_or_else(|e| e.into_inner());
match timed.as_mut() {
Some(t) => t.commands += 1,
None => {
*timed = Some(crate::timing_cuts::Timed {
key,
compute,
commands: 1,
name: name(),
})
}
}
}

/// Takes back the last [`HostList::timed_command`]: its command went
/// into the next host list after all (a cut for an indirect clamp).
#[cfg(feature = "diagnostics")]
pub(crate) fn timed_command_moved(&self) {
let mut timed = self.timed.lock().unwrap_or_else(|e| e.into_inner());
if let Some(t) = timed.as_mut() {
t.commands = t.commands.saturating_sub(1);
if t.commands == 0 {
*timed = None;
}
}
}

/// Notes a texture the list renders to or writes, in a listed frame.
#[cfg(feature = "diagnostics")]
pub(crate) fn note_target(&self, resource: usize, by: &str) {
Expand Down Expand Up @@ -5918,6 +6086,7 @@ impl Meter {
let drops = crate::pipeline::take_drops();
crate::frame::log();
log_gpu_times(report_frames);
log_gpu_walls(report_frames);
crate::copystat::log(report_frames);
crate::bindless::log(report_frames);
crate::heap_worker::log(report_frames);
Expand Down
3 changes: 3 additions & 0 deletions crates/victory-gpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ mod tdr;
mod texback;
#[cfg(windows)]
mod texture;
// Plain std, so its tests run everywhere.
#[cfg(any(windows, test))]
mod timing_cuts;
#[cfg(windows)]
mod twin;
#[cfg(windows)]
Expand Down
74 changes: 74 additions & 0 deletions crates/victory-gpu/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,71 @@ fn cut_for_clamp(this: This) -> bool {
true
}

/// With `VICTORY_GPU_TIMING_CUTS` (diagnostics builds), before a draw or
/// dispatch (`compute`) is recorded: the title list goes on in a new host
/// list where a pipeline that gets lists of its own starts or ends, as
/// [`cut_for_clamp`] moves it on, so that such a timed host list runs one
/// pipeline and its wall time is that pipeline's (`crate::timing_cuts`).
/// The command is counted in the list it goes into. Without a new host
/// list it stays in the current one, under that list's first pipeline.
#[cfg(all(windows, feature = "diagnostics"))]
fn cut_for_timing(this: This, compute: bool) {
if !crate::host::timing_cuts_on() {
return;
}
let Some(current) = host_list(this) else {
return;
};
// SAFETY: the title passes one of our lists.
let list = unsafe { &*this.cast::<List>() };
let pipeline = list
.state
.lock()
.unwrap_or_else(|e| e.into_inner())
.title_pipeline();
let key = crate::timing_cuts::key(pipeline, compute, crate::host::timing_cut_own(pipeline));
let mut into = current;
if current.timed_cuts(key)
&& let Some(next) = next_segment(this)
{
let mut state = list.state.lock().unwrap_or_else(|e| e.into_inner());
state.flush_hdr_probe(current);
current.close();
state.cut(next);
into = next;
}
into.timed_command(key, || crate::pipeline::shaders_of(pipeline as This));
}

#[cfg(all(windows, not(feature = "diagnostics")))]
fn cut_for_timing(_: This, _: bool) {}

/// After [`cut_for_clamp`] moved an indirect execution that
/// [`cut_for_timing`] had counted in `previous` on to a new host list: it
/// is counted there instead.
#[cfg(all(windows, feature = "diagnostics"))]
fn retime_after_clamp_cut(this: This, previous: Option<&crate::host::HostList>, compute: bool) {
let (Some(previous), Some(current)) = (previous, host_list(this)) else {
return;
};
if !crate::host::timing_cuts_on() || std::ptr::eq(previous, current) {
return;
}
// SAFETY: the title passes one of our lists.
let list = unsafe { &*this.cast::<List>() };
let pipeline = list
.state
.lock()
.unwrap_or_else(|e| e.into_inner())
.title_pipeline();
previous.timed_command_moved();
let key = crate::timing_cuts::key(pipeline, compute, crate::host::timing_cut_own(pipeline));
current.timed_command(key, || crate::pipeline::shaders_of(pipeline as This));
}

#[cfg(all(windows, not(feature = "diagnostics")))]
fn retime_after_clamp_cut(_: This, _: Option<&crate::host::HostList>, _: bool) {}

/// Moves a title list's recording on to its next host list (made, or one
/// it held since an earlier recording, reset then): that host list.
#[cfg(windows)]
Expand Down Expand Up @@ -1747,17 +1812,20 @@ mod state {
first_instance: u32,
) {
super::count_draw();
super::cut_for_timing(this, false);
with_state(this, |list, s| {
s.draw(list, vertices, instances, first, first_instance)
});
}
pub(super) extern "system" fn dispatch(this: This, x: u32, y: u32, z: u32) {
super::count_dispatch();
super::cut_for_timing(this, true);
with_state(this, |list, s| s.dispatch(list, x, y, z));
}
/// `DispatchX` with its console flags (`State::dispatch_x`).
pub(super) fn dispatch_x(this: This, x: u32, y: u32, z: u32, flags: u32) {
super::count_dispatch();
super::cut_for_timing(this, true);
with_state(this, |list, s| s.dispatch_x(list, x, y, z, flags));
}
/// `DispatchMesh(x, y, z)` (the PC method of List6); the title's mesh
Expand All @@ -1768,6 +1836,7 @@ mod state {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| tracing::warn!(x, y, z, "a mesh dispatch with y or z groups"));
}
super::cut_for_timing(this, false);
with_state(this, |list, s| s.dispatch_mesh(list, x));
}
/// `DispatchMeshX(x, flags, indirect arguments, payload)`.
Expand All @@ -1786,6 +1855,7 @@ mod state {
ONCE.call_once(|| tracing::warn!("indirect mesh dispatches dropped"));
return;
}
super::cut_for_timing(this, false);
with_state(this, |list, s| s.dispatch_mesh(list, x));
}
pub(super) extern "system" fn draw_indexed(
Expand All @@ -1797,6 +1867,7 @@ mod state {
first_instance: u32,
) {
super::count_draw();
super::cut_for_timing(this, false);
with_state(this, |list, s| {
s.draw_indexed(list, indices, instances, first, base, first_instance)
});
Expand Down Expand Up @@ -1882,12 +1953,15 @@ extern "system" fn execute_indirect(
};
use crate::draw::Indirect;
let mut outcome = Indirect::NotRun;
cut_for_timing(this, kind == DISPATCH);
with_state(this, |list, s| {
outcome = s.execute_indirect(list, (kind, stride, max), address, counter, true);
});
if outcome == Indirect::CutFirst {
// Clamped inline in the same host list if no new one can be had.
let previous = host_list(this);
cut_for_clamp(this);
retime_after_clamp_cut(this, previous, kind == DISPATCH);
with_state(this, |list, s| {
outcome = s.execute_indirect(list, (kind, stride, max), address, counter, false);
});
Expand Down
Loading