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
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ Tested on one machine: Windows 11, Intel i7-14700KF, NVIDIA RTX 4070 12 GB,

**Update, 2026-11-25 afternoon: what a frame is made of, measured, and two
changes that hold.** First rooms, about 3,850 draws a frame: 104 ms a frame
(9.6 frames a second) with the set below, from about 120 ms in the
morning. Still far from the console's 33 ms.
(9.6 frames a second) with the first two changes below, 97 ms (10.3)
with all of them, from about 120 ms in the morning. Still far from the console's 33 ms.
- How the title paces itself (new meters in `fence.rs` and
`console_insn.rs`): it never asks for an event. Each frame begins with
one small submission and a signal, and a title thread then waits for
Expand Down Expand Up @@ -83,6 +83,23 @@ morning. Still far from the console's 33 ms.
windows on (960 are compute dispatches; about 1,000 are mesh records
the title keeps scattered through a 100 MB argument buffer, which a
window of records at one step cannot serve).
- `VICTORY_INDIRECT_GATHER=1` (off by default): a single indirect record
that misses its window gets a slot in a scratch buffer of its list, and
the list's prelude clamps all such records in one pass by a table of
their offsets (the clamp shader with a gather bit), where each draw had
a compute pass of the layer's in front of it. Proved by clamping every
gathered record again at its draw and comparing on the card
(`VICTORY_INDIRECT_WINDOW_CHECK=1`): 7.9 million records, none differed.
About 600 of the ~1,600 draws that missed a window are served; the
others come after a dispatch or a storing draw in their list. With
`VICTORY_CARD_REHASH_RUNS=1`: 104 -> 97 ms a frame (10.3 frames a
second at 3,800 draws).
- What the card's time is now, added up: of ~87 ms busy in a 97 ms
frame, the layer's own lists are ~19 ms and its clamp passes 10-20 ms;
about 50 ms are the title's own ~30 compute passes and its geometry at
2560x1440 as translated. With every pass of the layer's gone, this
scene would still be near 20 frames a second on this card: the
translated shaders have to get cheaper too.
- Measured and found to give nothing (switches left in, off):
`VICTORY_REFRESH_ONCE`, `VICTORY_ASYNC_PRESENT`, `VICTORY_VIEW_KNOWN`,
`VICTORY_SPREAD`, `VICTORY_QUEUE2=3`, `VICTORY_WINDOW_JUMP`. Each has an
Expand Down
129 changes: 121 additions & 8 deletions crates/victory-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,45 @@ fn jump_windows() -> bool {
}
}

/// Whether a single record that misses its window is gathered: given a
/// slot that the list's prelude fills in one pass with the list's other
/// such records, in place of a pass of ours in front of its draw
/// (`crate::indirect::gather`; `VICTORY_INDIRECT_GATHER=1`; `alt`: twenty
/// seconds with, twenty without, in turn, each change said in the log).
/// Off by default. Needs [`windows()`].
fn gathers() -> bool {
/// 0 off, 1 on, 2 in turn.
static MODE: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
match *MODE.get_or_init(
|| match std::env::var("VICTORY_INDIRECT_GATHER").as_deref() {
Ok("1") => 1,
Ok("alt") => 2,
_ => 0,
},
) {
0 => false,
1 => true,
_ => {
static EPOCH: std::sync::OnceLock<std::time::Instant> = std::sync::OnceLock::new();
let on = (EPOCH
.get_or_init(std::time::Instant::now)
.elapsed()
.as_secs()
/ 20)
% 2
== 1;
static LAST: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(2);
if LAST.swap(u8::from(on), std::sync::atomic::Ordering::Relaxed) != u8::from(on) {
tracing::info!(
"single indirect records are gathered for their list's prelude from now (VICTORY_INDIRECT_GATHER=alt): {}",
if on { "ON" } else { "off, as usual" }
);
}
on
}
}
}

/// The records of a window opened at a remembered step ([`jump_windows`]):
/// what the record after a lone one opens.
const WINDOW_JUMP: u32 = 16;
Expand Down Expand Up @@ -288,10 +327,13 @@ fn jump_window(
/// ended); its record does not continue the window's walk; it is past the
/// window's end (the next window: as meant). And the records those passes
/// clamped in all; the passes that opened a window at a remembered step
/// ([`jump_windows`]), and those of them no draw read from.
/// ([`jump_windows`]), and those of them no draw read from; the commands
/// that read a gathered record ([`gathers`]: no pass of ours either), and
/// those that asked for one and got none (the list no longer gathers, or
/// its set is full).
#[cfg(feature = "diagnostics")]
pub(crate) static WINDOW_WHY: [std::sync::atomic::AtomicU64; 12] =
[const { std::sync::atomic::AtomicU64::new(0) }; 12];
pub(crate) static WINDOW_WHY: [std::sync::atomic::AtomicU64; 14] =
[const { std::sync::atomic::AtomicU64::new(0) }; 14];

/// The line of [`WINDOW_WHY`], per frame over `frames` frames.
#[cfg(feature = "diagnostics")]
Expand All @@ -300,10 +342,15 @@ pub(crate) fn window_line(frames: f64) {
.iter()
.map(|n| n.swap(0, std::sync::atomic::Ordering::Relaxed) as f64 / frames.max(1.0))
.collect();
let g: Vec<f64> = crate::indirect::GATHER_NONE
.iter()
.map(|n| n.swap(0, std::sync::atomic::Ordering::Relaxed) as f64 / frames.max(1.0))
.collect();
tracing::info!(
"indirect commands per frame by their clamp: {:.0} read from a window (no pass of ours); a pass of ours for {:.0}: {:.0} dispatches, {:.0} of more than one record, {:.0} with a count \
buffer, {:.0} whose shaders can store to buffers, {:.0} first on their list, {:.0} with no window on their argument buffer, {:.0} off the window's walk, {:.0} past the window's end; \
{:.0} records clamped by those passes; {:.0} of the passes opened a window at a remembered step (VICTORY_WINDOW_JUMP=1), {:.0} of those windows were read by no draw",
{:.0} records clamped by those passes; {:.0} of the passes opened a window at a remembered step (VICTORY_WINDOW_JUMP=1), {:.0} of those windows were read by no draw; \
{:.0} read a record gathered for their list's prelude (VICTORY_INDIRECT_GATHER=1), {:.0} asked for one and got none (no fit for the clamp {:.0}, the list had stopped gathering {:.0}, too many buffers {:.0}, the set full {:.0}, no buffer {:.0})",
n[0],
n[1..9].iter().sum::<f64>(),
n[1],
Expand All @@ -317,6 +364,13 @@ pub(crate) fn window_line(frames: f64) {
n[9],
n[10],
n[11],
n[12],
n[13],
g[0],
g[1],
g[2],
g[3],
g[4],
);
}

Expand Down Expand Up @@ -2307,6 +2361,16 @@ impl State {
});
}

/// Whether the shaders of the pipeline bound now can store to buffers
/// (or the pipeline is not known): a draw with them may write the
/// arguments of draws after it, which ends a list's gathering
/// (`crate::indirect::gather`). The same test keeps such a draw out of
/// a window ([`windows()`]).
fn may_store(&self) -> bool {
crate::pipeline::host_pipeline(self.pipeline as This)
.is_none_or(|p| p.stages.iter().any(|stage| stage.translated().stores))
}

fn bind_emulated_cs(
&mut self,
list: &HostList,
Expand Down Expand Up @@ -2389,6 +2453,7 @@ impl State {
}
// It may write the arguments a window holds clamped.
self.window = None;
list.gather_close();
#[cfg(feature = "diagnostics")]
if crate::pipeline::is_skipped(self.pipeline as This) {
DROPPED.fetch_add(1, Ordering::Relaxed);
Expand Down Expand Up @@ -2769,6 +2834,9 @@ impl State {
/// programs are one-dimensional).
pub(crate) fn dispatch_mesh(&mut self, list: &HostList, x: u32) {
self.window = None;
if self.may_store() {
list.gather_close();
}
#[cfg(feature = "diagnostics")]
self.trace("mesh", x, 0);
if let Some(p) = crate::pipeline::host_pipeline(self.pipeline as This)
Expand Down Expand Up @@ -3425,6 +3493,9 @@ impl State {
first_instance: u32,
) {
self.window = None;
if self.may_store() {
list.gather_close();
}
#[cfg(feature = "diagnostics")]
self.trace("draw", vertices, first);
let ready = self.prepare(list, false);
Expand Down Expand Up @@ -3531,6 +3602,9 @@ impl State {
first_instance: u32,
) {
self.window = None;
if self.may_store() {
list.gather_close();
}
#[cfg(feature = "diagnostics")]
self.trace("indexed", indices, first);
// Indexed rectangles are drawn by the rectangle geometry stage
Expand Down Expand Up @@ -3691,6 +3765,11 @@ impl State {
if !windowed || first_on_list {
self.window = None;
}
if !windowed {
// A dispatch, or shaders that store to buffers: arguments may
// be written from here on.
list.gather_close();
}
Comment on lines +3768 to +3772

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Stale indirect arguments after mesh emulation

A mesh emulation pass that writes later draw arguments can leave gather_close uncalled. Indirect emulation bypasses the cutoff, and direct emulation checks only graphics stages. Later draws then read the arguments from before the write.

Devin left this for you to triage: Interaction of their opt-in gather with the mesh-emulation dispatch: only reachable with VICTORY_INDIRECT_GATHER on. If it ships as-is, an emulated mesh pass writing later draw args can leave stale indirect arguments; recommend a follow-up wiring gather_close into the emulation paths rather than a speculative patch in the integration PR.

Learn more

Gathered arguments are read by the prelude before the main list executes, so writes within that list must stop further gathering. execute_indirect returns through the emulation branch before it reaches the cutoff. That branch runs both an emulated compute dispatch and a graphics draw. dispatch_mesh calls may_store, but the emulated pipeline keeps its compute stage separate from the graphics stages that may_store checks.

Example: An emulated mesh pass writes a later indexed draw’s instance count from zero to ten. If the indexed draw gathers, the prelude captures zero and the draw runs empty.

Recommended fix: Stop gathering before either emulated mesh path runs, or inspect both the emulated compute stage and the graphics stages before deciding to leave gathering open. Cover an emulated producer followed by a native indirect consumer in the same list.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

// Diagnostics: why this command gets a pass of ours, if it does.
#[cfg(feature = "diagnostics")]
let why_not = if !windows() {
Expand Down Expand Up @@ -3723,8 +3802,20 @@ impl State {
&& (offset - w.base) / u64::from(w.step) < u64::from(w.records)
})
.map(|w| (w.scratch.clone(), offset - w.base, w.step));
let (bounded, at) = match hit {
Some((scratch, at, step)) => {
// A record that misses its window: a slot the list's prelude fills,
// if the list still gathers ([`gathers`]).
let gathered = if hit.is_none() && windowed && gathers() {
let slot =
crate::indirect::gather(list, (&args.buffer, args.id()), offset, (kind, stride));
#[cfg(feature = "diagnostics")]
WINDOW_WHY[if slot.is_some() { 12 } else { 13 }]
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
slot
} else {
None
};
let (bounded, at) = match (hit, gathered) {
(Some((scratch, at, step)), _) => {
// Diagnostics of windows: the record clamped again and
// compared (`crate::indirect::window_check`).
if crate::indirect::window_check()
Expand All @@ -3749,7 +3840,27 @@ impl State {
}
(scratch, at)
}
None => {
(None, Some((scratch, at))) => {
// The record's bytes are up to date when the list is
// submitted, as for a record clamped in the list.
args.refresh(list, arguments, len);
// Diagnostics of windows, for gathered records too: the
// record clamped again here and compared.
if crate::indirect::window_check()
&& crate::indirect::check_hit(
list,
(&args.buffer, offset),
(kind, stride),
&scratch,
at,
)
{
self.host_pipeline = 0;
self.host_roots[1] = 0;
}
(scratch, at)
}
(None, None) => {
// A window opened at the remembered step that no draw read
// from: the title does not walk here by that step.
if self.window_jumped && !self.window_read {
Expand Down Expand Up @@ -3850,7 +3961,9 @@ impl State {
if (6..=8).contains(&why_not) {
static N: std::sync::atomic::AtomicU64 =
std::sync::atomic::AtomicU64::new(0);
if N.fetch_add(1, std::sync::atomic::Ordering::Relaxed).is_multiple_of(256) {
if N.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
.is_multiple_of(256)
{
let (last_copy, last_offset) = self.last_indirect;
tracing::info!(
"a window miss (one in 256): cause {why_not}, kind {kind}, stride {stride}, buffer {} offset {offset:#x} ({} left); the command before it on the list: buffer {last_copy} offset \
Expand Down
Loading
Loading