From aaf6df3ce33215cf99def8dc8d4ddd305c5ae2c9 Mon Sep 17 00:00:00 2001 From: Saurabh Suresh Date: Sat, 28 Nov 2026 08:09:31 +0400 Subject: [PATCH 1/2] perf(gpu): clamp indirect arguments in group passes (VICTORY_CLAMP_GROUPS=1) #453 hoists an indirect execution's clamp into its list's prelude only when nothing earlier in the list wrote its arguments. #570 starts a new host list for a clamp the list cannot hoist, so the clamps after it, up to the next write, are hoisted into that list's prelude. A world frame in the prison cell has ~4,000 such clamps (the title's culling passes write the arguments in the same list); inline, each is a compute dispatch and three barrier calls between two draws, which D3DMetal records as a compute encoder and a new render encoder (Metal System Trace, M5 Pro: 6,917 encoders a frame). With VICTORY_CLAMP_GROUPS=1 (off by default) a held-out clamp goes into a group pass instead, recorded where its inline clamp would be, in the same host list: one dispatch reading a table of jobs the CPU goes on filling while the list records. It clamps every execution after it until a command writes what the next one reads (the list's tracker of written ranges, also kept per pass), an inline clamp is recorded, its 512 jobs are taken, or a record is in none of the eight buffers it binds. Each execution has its own job and slot, charged to the list's budget in recorded order: the records are word for word what inline clamps write. One record without a count (all of the title's) is grouped. A pass costs two barrier calls; its output stays readable as arguments until the list closes. The prelude's batch puts runs of such clamps in group passes too, one barrier per pass instead of one per clamp. The decision is: hoist, else group, else a cut (#570, where it pays), else inline. A cut is then made only for what no group takes (a count, several records, a record outside the bound buffers, no pipeline). The frame line keeps #570's reasons for inline clamps and the cuts, and also counts clamps in prelude group passes, executions in group passes and the passes, why clamps stayed out of the prelude, the clamps' barrier calls and barriers, and draws recorded after compute in one host list (render passes split). A hook, indirect::clamped_in_place, clamps arguments read in place in title memory inline (never hoisted, grouped or cut for) for in-place section views. With VICTORY_CLAMP_SYNC=1 (#588) every clamp path records its enhanced global barriers, group passes included, and their outputs stay common. The barrier before a group pass waits for every shader stage, not only compute as an inline clamp's does: an execution that joins the pass may read a record written before it whose title barrier comes after it. In the prelude the budget's barrier between dispatches is one per group run. A culling dispatch that writes the records again after a pass ends its group: tests run culling dispatches and indirect draws in one list, and the culling write through a table's view (as a title dispatch binds it, with VICTORY_TABLE_MEMO on or off); with the check of writes since the pass taken out, the draws after the second culling draw the first one's records. On Windows the group pass did not build (fxc), so the group tests found no seat; Wine's compiler took it. The pass now walks the eight tiles a table holds whatever its count (fxc refuses barriers in a loop up to a count read from memory, as indirect-clamp.hlsl notes), its constant buffer is no longer named Pass (an effects keyword), and argument_clamp_builds shows the compiler's messages if it fails. The design follows avatarmanz's VIctory-7900XTX 0375962 (clamps of many executions in one pass from a job table filled while the list records) and cc11ccc (clamped records left in INDIRECT_ARGUMENT), GPL-3.0. On macOS, halfwave50's VICTORY_BATCH_CLAMPS (main-mac, from 64bitMango 18872f48 and 131721b7, after Belobug ccab973) batches a run's clamps in one dispatch walking a table in draw order, recorded in a host list cut before the drawing one. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/draw.rs | 126 +- crates/victory-gpu/src/host.rs | 362 +++++- crates/victory-gpu/src/indirect.rs | 1724 +++++++++++++++++++++++++++- 3 files changed, 2170 insertions(+), 42 deletions(-) diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index 5d49aa204..45ddcf08e 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -1654,6 +1654,7 @@ impl State { #[cfg(feature = "diagnostics")] self.probe_written(list); DISPATCHED.fetch_add(1, Ordering::Relaxed); + list.note_work(true); self.ambient(list); } @@ -1908,6 +1909,7 @@ impl State { unsafe { l6.DispatchMesh(x, 1, 1) }; self.note_hdr_draw(); DRAWN.fetch_add(1, Ordering::Relaxed); + list.note_work(false); true } Err(_) => false, @@ -2529,6 +2531,7 @@ impl State { }; self.note_hdr_draw(); DRAWN.fetch_add(1, Ordering::Relaxed); + list.note_work(false); } if !ran { DROPPED.fetch_add(1, Ordering::Relaxed); @@ -2625,6 +2628,7 @@ impl State { }; self.note_hdr_draw(); DRAWN.fetch_add(1, Ordering::Relaxed); + list.note_work(false); } if !ran { DROPPED.fetch_add(1, Ordering::Relaxed); @@ -2765,25 +2769,54 @@ impl State { // and before anything of this call is recorded, so a cut leaves // nothing of it behind. let blocker = if crate::indirect::batch_clamps_enabled() { - list.clamp_blocker(arguments, len, count) + crate::indirect::blocker(list, (arguments, len), count) } else { None }; let hoist = crate::indirect::batch_clamps_enabled() && blocker.is_none(); - if !hoist + let in_place = blocker == Some(crate::host::ClampBlock::InPlace); + // Held out of the prelude: clamped in a group pass if one can take + // it (`crate::indirect::groups_enabled`), a pass recorded here if the + // group open in the list cannot; else in a new host list if a cut + // pays; else inline. A seat records nothing when there is none. + let seat = (!hoist + && blocker.is_some() + && !in_place + && crate::indirect::groups_enabled() + && crate::indirect::groupable(kind, stride, max, count.is_some())) + .then(|| { + crate::indirect::note_source(&args); + list.clamp_group_seat( + (&args.buffer, args_gpu), + (arguments, len), + crate::indirect::recent_sources, + ) + }) + .flatten(); + if seat.is_none() + && !hoist && may_cut && blocker.is_some() + && !in_place && crate::indirect::clamp_cuts_enabled() && list.clamp_cut_pays() { return None; } + if !hoist { + crate::indirect::note_held_out(blocker); + } + if seat.as_ref().is_some_and(|&(_, opened)| opened) { + // The pass binds a compute pipeline of its own. + self.host_pipeline = 0; + self.host_roots[1] = 0; + } args.refresh(list, arguments, len); if let Some((s, at)) = &counter { s.refresh(list, *at, 4); } let counted = counter.as_ref().map(|(s, at)| s.gpu(*at)); - let inline_clamped = if hoist { + let inline_clamped = if hoist || seat.is_some() { None } else { // Bounded first: the inline clamp binds a compute pipeline of its @@ -2833,11 +2866,21 @@ impl State { } return Some(false); } - let (clamped, dst_offset, needs_done) = match inline_clamped { - Some((clamped, dst_offset)) => (clamped, dst_offset, true), - None => { + let (clamped, dst_offset, needs_done) = match (inline_clamped, seat) { + (Some((clamped, dst_offset)), _) => (clamped, dst_offset, true), + (None, Some((seat, _))) => { + let Some((clamped, dst_offset)) = + list.clamp_group_join(&seat, args_gpu, (kind, stride)) + else { + why("not bounded (no place in its clamp group)"); + return Some(false); + }; + (clamped, dst_offset, false) + } + (None, None) => { + let record = (Some(&args.buffer), args_gpu); let Some((clamped, dst_offset)) = - crate::indirect::enqueue(list, args_gpu, (kind, stride, max), counted) + crate::indirect::enqueue_from(list, record, (kind, stride, max), counted) else { why("not bounded (no hoisted argument buffer)"); return Some(false); @@ -2895,10 +2938,12 @@ impl State { if kind == DISPATCH { self.probe_hdr_compute(list); DISPATCHED.fetch_add(1, Ordering::Relaxed); + list.note_work(true); self.ambient(list); } else { self.note_hdr_draw(); DRAWN.fetch_add(1, Ordering::Relaxed); + list.note_work(false); } Some(true) } @@ -6612,6 +6657,73 @@ void main() { assert_eq!(release(heap_raw, 0x10)(heap_raw), 0); } + /// A culling dispatch writes the records of the indirect executions after + /// it through a table's unordered view, then again before the next + /// ones: the second write ends the clamp group the first executions + /// opened (`VICTORY_CLAMP_GROUPS`), as its preparation declares it, with + /// the table memo off and on (`VICTORY_TABLE_MEMO=1`: it remembers no + /// buffer's unordered view, so every such write is declared again). + #[test] + fn a_culling_write_through_a_table_ends_the_clamp_group_memo_or_not() { + let Some(host) = crate::host::host() else { + return; + }; + assert!( + crate::indirect::group_available(), + "the group pass compiles" + ); + let p = crate::watch::tests::pool(1); + let buf = buffer_at_address(p as u64, crate::watch::PAGE as u64); + let (heap_raw, title_heap, _heap_host) = view_heap(8); + // A raw view of 64 words at 4 KiB into the buffer: the records. + let uav_desc: [u32; 10] = [39, 1, 1024, 0, 64, 0, 0, 0, 1, 0]; + crate::descriptor::create_uav( + std::ptr::null(), + buf, + std::ptr::null(), + &uav_desc, + title_heap.start, + ); + let record = p as u64 + 4096; + let shadow = crate::shadow::find(record, 16).expect("the buffer's copy"); + let u_uav = used(ResourceType::Uav); + for memo in [0, 1] { + let _guard = TableMemoGuard::set(memo); + let list = host.new_list().unwrap(); + let mut state = State::default(); + state.set_heaps(&[heap_raw]); + state.set_table(true, 0, title_heap.start); + let seat = |list: &HostList| { + let at = shadow.gpu(record); + let (seat, opened) = list + .clamp_group_seat((&shadow.buffer, at), (record, 16), Vec::new) + .expect("a seat"); + list.clamp_group_join(&seat, at, (0, 16)).expect("a place"); + opened + }; + // The culling dispatch, then two draws. + state.table_entry(&list, &u_uav, true, true); + assert!( + list.clamp_blocker(record, 16, None).is_some(), + "memo {memo}: out of the prelude" + ); + assert!(seat(&list), "memo {memo}: the first draw opens a pass"); + assert!(!seat(&list), "memo {memo}: the next joins it"); + // The culling dispatch again over the same records, then a draw. + state.table_entry(&list, &u_uav, true, true); + assert!( + seat(&list), + "memo {memo}: a draw after the second write gets a pass of its own" + ); + list.close(); + } + crate::shadow::unregister(buf as usize, p as u64); + let release = crate::object::slot:: u32>; + assert_eq!(release(buf, 0x10)(buf), 0); + assert_eq!(release(heap_raw, 0x10)(heap_raw), 0); + assert_eq!(host.log_messages(), None); + } + #[test] fn table_memo_invalidates_on_descriptor_change_epoch_bump_stores_and_reset() { let Some(host) = crate::host::host() else { return }; diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index d35468c7d..e5f00c316 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -269,6 +269,16 @@ static LOST: [std::sync::atomic::AtomicU64; 2] = [ /// (`crate::shadow::take_gave_up`). static GAVE_UP: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); +/// Draws recorded after compute work in the same host list since the last +/// frame-rate line ([`HostList::note_work`]): the render passes compute +/// splits. +static WORK_SWITCHES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + +/// [`WORK_SWITCHES`] since the last call. +pub(crate) fn take_work_switches() -> u64 { + WORK_SWITCHES.swap(0, Ordering::Relaxed) +} + /// Time spent in submissions since the last frame-rate line, in /// microseconds: preparing them on the CPU (title memory copies, texture /// detiling), then waiting for the GPU. @@ -825,6 +835,10 @@ pub(crate) mod tests { /// Every transition barrier built on this thread (resource, before, /// after), in order. pub(crate) static ISSUED: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; + + /// Every global barrier recorded on this thread (sync before, sync + /// after), in order ([`super::global_barrier`]). + pub(crate) static GLOBAL_ISSUED: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; } /// The barriers built on this thread since the last call. @@ -832,6 +846,11 @@ pub(crate) mod tests { ISSUED.with(|l| std::mem::take(&mut *l.borrow_mut())) } + /// The global barriers recorded on this thread since the last call. + pub(crate) fn global_issued() -> Vec<(i32, i32)> { + GLOBAL_ISSUED.with(|l| std::mem::take(&mut *l.borrow_mut())) + } + pub(crate) fn start_native_call_trace() { NATIVE_CALLS.with(|trace| { assert!(trace.borrow_mut().replace(NativeCallTrace::default()).is_none()); @@ -1805,6 +1824,9 @@ pub(crate) struct HostList { /// The work its indirect executions may still do (`indirect`): a /// shader-writable word, zeroed when the list is submitted. budget: OnceLock>, + /// What the last draw or dispatch recorded was ([`HostList::note_work`]): + /// 0 nothing yet, 1 a draw, 2 compute. + work: std::sync::atomic::AtomicU8, /// Clamped indirect arguments (`crate::indirect`), reused by each /// inline execution in turn in legacy sync mode; replaced by a bigger one /// when too small. @@ -2098,8 +2120,12 @@ pub(crate) enum ClampBlock { /// An earlier command of the list writes the arguments or the count. Overlap, /// An earlier clamp of the list ran inline (the budget is charged in - /// recorded order). + /// recorded order), or a group pass of the list was recorded. AfterInline, + /// The GPU reads the arguments or the count in place in title memory: + /// clamped inline, not hoisted, grouped or cut for + /// ([`crate::indirect::clamped_in_place`]). + InPlace, } #[derive(Default)] @@ -2288,12 +2314,78 @@ struct HoistedClamps { /// The list was started for a clamp that could not run in the prelude /// of the one before it (`crate::list::cut_for_clamp`). cut: bool, + /// The clamp group open in the list, if any (`crate::indirect` group + /// passes): the last pass recorded, which may still take executions. + group: Option, + /// The group passes' output buffers, kept from recording to recording, + /// the one in use and the bytes of it the passes of this recording took. + group_chunks: Vec, + group_current: usize, + group_used: u64, + /// Group passes recorded since the list was made. + group_passes: u64, +} + +/// The bytes of one group pass's output: a slot for each of its jobs. +const GROUP_OUT: u64 = crate::indirect::GROUP_JOBS as u64 * crate::indirect::GROUP_SLOT as u64; + +/// The bytes of a buffer of group passes' outputs: eight passes. +const GROUP_CHUNK: u64 = 8 * GROUP_OUT; + +/// A buffer of group passes' outputs, and whether a pass of this recording +/// left it readable as arguments (else it is common). +struct GroupChunk { + buffer: ID3D12Resource, + readable: bool, +} + +/// A group pass recorded in a list, which executions recorded after it +/// join until it cannot take the next one ([`HostList::clamp_group_seat`]). +struct ClampGroup { + /// Which pass of the list it is ([`HostList::clamp_group_join`]). + pass: u64, + /// Its table of jobs, in the list's upload memory. + table: usize, + jobs: u32, + /// The host addresses of the buffers it reads records from. + sources: [u64; crate::indirect::GROUP_SOURCES], + /// Where its output is: a buffer of [`HoistedClamps::group_chunks`], the + /// byte its first slot is at. + chunk: usize, + base: u64, + /// What commands recorded after the pass wrote: an execution reading + /// any of it cannot join. + since: WriteHazard, +} + +/// A place in a list's clamp group for an indirect execution about to be +/// recorded ([`HostList::clamp_group_seat`]): the pass, and which of its +/// sources the record is read from. +pub(crate) struct GroupSeat { + pass: u64, + source: usize, } impl HoistedClamps { fn touched_buffers(&self) -> Vec { self.arena.touched_buffers() } + + /// A command of the list writes `[start, start + len)` of a title buffer. + fn note_write(&mut self, start: u64, len: u64) { + self.hazard.note_write(start, len); + if let Some(group) = &mut self.group { + group.since.note_write(start, len); + } + } + + /// A command of the list may write any title buffer. + fn note_unknown(&mut self) { + self.hazard.note_unknown(); + if let Some(group) = &mut self.group { + group.since.note_unknown(); + } + } } struct ScratchChunk { @@ -2568,6 +2660,7 @@ impl HostGpu for Host { pipelines: Mutex::default(), id, budget: OnceLock::new(), + work: 0.into(), arguments: Mutex::new(None), inline_arguments: Mutex::default(), clamps: Mutex::default(), @@ -3891,6 +3984,7 @@ impl HostList { self.reset_hoisted_clamps(); self.reset_inline_arguments(); self.marked.store(false, Ordering::Relaxed); + self.work.store(0, Ordering::Relaxed); // SAFETY: the list is not executing. let reset = unsafe { self.allocator @@ -4417,6 +4511,7 @@ impl HostList { clamp_budget, &clamp_buffers, &pending_clamps, + &mut |len| self.scratch(len), ); } list.Close() @@ -4674,7 +4769,6 @@ impl HostList { self.clamps .lock() .unwrap_or_else(|e| e.into_inner()) - .hazard .note_write(address as u64, len as u64); } @@ -4690,18 +4784,16 @@ impl HostList { self.clamps .lock() .unwrap_or_else(|e| e.into_inner()) - .hazard .note_unknown(); } /// Notes that an inline indirect clamp was recorded in this list so later - /// clamps stay inline and charge the list budget in recorded order. + /// clamps stay inline and charge the list budget in recorded order (and + /// none joins a clamp group recorded before it). pub(crate) fn note_inline_clamp(&self) { - self.clamps - .lock() - .unwrap_or_else(|e| e.into_inner()) - .hazard - .note_inline(); + let mut clamps = self.clamps.lock().unwrap_or_else(|e| e.into_inner()); + clamps.hazard.note_inline(); + clamps.group = None; } /// Whether an indirect execution reading `[arguments, arguments + len)` @@ -4799,17 +4891,23 @@ impl HostList { /// Transitions hoisted indirect argument buffers back to `COMMON` at the /// end of `list.list` so the list can be re-submitted or reset cleanly. - /// No-op when narrow clamp sync is active (buffers stay in `COMMON`). + /// Under narrow clamp sync the hoisted ones stay in `COMMON`, as do + /// group passes' outputs ([`crate::indirect::record_group`]). fn hoisted_clamps_close(&self) { - if self.narrow_clamp_sync() { - return; - } + let narrow = self.narrow_clamp_sync(); let buffers = { - let clamps = self.clamps.lock().unwrap_or_else(|e| e.into_inner()); - if clamps.pending.is_empty() { - return; + let mut clamps = self.clamps.lock().unwrap_or_else(|e| e.into_inner()); + clamps.group = None; + // The group passes' outputs a pass left readable (none narrow). + let mut buffers: Vec = clamps + .group_chunks + .iter_mut() + .filter_map(|c| std::mem::take(&mut c.readable).then(|| c.buffer.clone())) + .collect(); + if !narrow && !clamps.pending.is_empty() { + buffers.extend(clamps.touched_buffers()); } - clamps.touched_buffers() + buffers }; if buffers.is_empty() { return; @@ -4825,10 +4923,11 @@ impl HostList { }) .collect(); // SAFETY: an open list and live argument buffers left in - // `INDIRECT_ARGUMENT` by `prelude`. + // `INDIRECT_ARGUMENT` by `prelude` or by group passes. unsafe { self.list.ResourceBarrier(&barriers); } + crate::indirect::note_barriers(barriers.len()); } /// Resets hoisted clamp tracking after the list finishes running, keeping @@ -4840,6 +4939,23 @@ impl HostList { clamps.budget_gpu = 0; clamps.hazard.clear(); clamps.cut = false; + // A list reset unclosed ran none of its passes: all common. + clamps.group = None; + clamps + .group_chunks + .iter_mut() + .for_each(|c| c.readable = false); + let kept = (clamps.group_current + 2).min(clamps.group_chunks.len()); + let spent_groups: Vec = clamps + .group_chunks + .split_off(kept) + .into_iter() + .map(|c| c.buffer) + .collect(); + if !spent_groups.is_empty() { + release_later(Box::new(spent_groups)); + } + (clamps.group_current, clamps.group_used) = (0, 0); clamps.arena.reset() }; if !spent_chunks.is_empty() { @@ -5202,6 +5318,186 @@ impl HostList { Some(unsafe { (*data as *const u32).read_volatile() }) } + /// A draw (`compute` false) or compute work was recorded: a draw after + /// compute in one host list is counted ([`take_work_switches`]), as the + /// host ends a render pass for the compute and starts another after it + /// (D3DMetal does, one Metal encoder each). + pub(crate) fn note_work(&self, compute: bool) { + let now = if compute { 2 } else { 1 }; + if self.work.swap(now, Ordering::Relaxed) == 2 && !compute { + WORK_SWITCHES.fetch_add(1, Ordering::Relaxed); + } + } + + /// For an indirect execution about to be recorded whose clamp the + /// prelude cannot take, reading `[arguments, arguments + len)` of a title + /// buffer whose host copy is in `source` (at host address `record`): a + /// seat in the list's open clamp group (`crate::indirect` group passes) + /// if its pass reads `source` and nothing recorded since the pass writes + /// what the execution reads, else in a group whose pass is recorded now, + /// reading `source` and the first of `recent` (the buffers other + /// executions read lately), and whether it was. `None` if no pass can be + /// recorded or the record is not where one can read it. + /// + /// Only the group recorded last takes executions, and none after an + /// inline clamp ([`HostList::note_inline_clamp`]) or once a pass is + /// recorded hoists ([`WriteHazard::note_inline`]): every clamp of the + /// list charges the budget in recorded order. The execution's job goes + /// in once it has been recorded ([`HostList::clamp_group_join`]). + pub(crate) fn clamp_group_seat( + &self, + (source, record): (&ID3D12Resource, u64), + (arguments, len): (u64, u64), + recent: impl FnOnce() -> Vec, + ) -> Option<(GroupSeat, bool)> { + use crate::indirect::{GROUP_JOBS, GROUP_SOURCES, GROUP_TABLE}; + // SAFETY: live buffers. + let gpu = |b: &ID3D12Resource| unsafe { b.GetGPUVirtualAddress() }; + let at = gpu(source); + // The pass reads the record as a word of the buffer's (32-bit index). + // SAFETY: a plain query on a live buffer. + let end = at.saturating_add(unsafe { source.GetDesc() }.Width); + let inside = record >= at + && record.saturating_add(len) <= end + && (record - at).is_multiple_of(4) + && (record - at) / 4 <= u64::from(u32::MAX); + if !inside { + return None; + } + { + let clamps = self.clamps.lock().unwrap_or_else(|e| e.into_inner()); + if let Some(g) = &clamps.group + && g.jobs < GROUP_JOBS + && g.since.blocker(arguments, len, None).is_none() + && let Some(source) = g.sources.iter().position(|&s| s == at) + { + return Some(( + GroupSeat { + pass: g.pass, + source, + }, + false, + )); + } + } + if !at.is_multiple_of(4) || !crate::indirect::group_available() { + return None; + } + let budget = self.budget()?; + let (table_gpu, table) = self.scratch(GROUP_TABLE)?; + // SAFETY: the list's upload memory, until it is reset; no job yet. + unsafe { table.cast::().write_volatile(0) }; + let mut sources = [at; GROUP_SOURCES]; + let mut bound = 1; + self.keep(source); + for buffer in recent() { + let other = gpu(&buffer); + if bound == GROUP_SOURCES { + break; + } + if other.is_multiple_of(4) && !sources[..bound].contains(&other) { + // Read only if a job names it; kept as the copies draws read. + self.keep(&buffer); + sources[bound] = other; + bound += 1; + } + } + let host = host()?; + let mut clamps = self.clamps.lock().unwrap_or_else(|e| e.into_inner()); + if clamps.group_current < clamps.group_chunks.len() + && clamps.group_used + GROUP_OUT > GROUP_CHUNK + { + (clamps.group_current, clamps.group_used) = (clamps.group_current + 1, 0); + } + if clamps.group_current == clamps.group_chunks.len() { + let (buffer, _) = host.shared_buffer(GROUP_CHUNK)?; + name(&buffer, || { + format!("indirect clamp groups {GROUP_CHUNK:#x}") + }); + clamps.group_chunks.push(GroupChunk { + buffer, + readable: false, + }); + clamps.group_used = 0; + } + let chunk = clamps.group_current; + let out = &mut clamps.group_chunks[chunk]; + let before = if out.readable { + D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT + } else { + D3D12_RESOURCE_STATE_COMMON + }; + let pass = (&out.buffer, before); + let cmd7 = self.narrow_clamp_sync_list(); + if !crate::indirect::record_group(&self.list, cmd7, pass, budget, table_gpu, &sources) { + return None; + } + // Narrow clamp sync leaves it common. + out.readable |= cmd7.is_none(); + let base = clamps.group_used; + clamps.group_used += GROUP_OUT; + clamps.group_passes += 1; + let pass = clamps.group_passes; + // Later clamps stay out of the prelude, where they would be charged + // before this pass's. + clamps.hazard.note_inline(); + clamps.group = Some(ClampGroup { + pass, + table: table as usize, + jobs: 0, + sources, + chunk, + base, + since: WriteHazard::default(), + }); + drop(clamps); + self.note_work(true); + Some((GroupSeat { pass, source: 0 }, true)) + } + + /// The execution `seat` was for has been recorded, reading a record of + /// `kind` and `stride` bytes at host address `record`: its job goes into + /// the group's table, and the place of its clamped record (a buffer + /// readable as arguments, the offset in it). `None` if the group is gone + /// or full, or the record is not where the pass can read it. + pub(crate) fn clamp_group_join( + &self, + seat: &GroupSeat, + record: u64, + (kind, stride): (u32, u32), + ) -> Option<(ID3D12Resource, u64)> { + use crate::indirect::{GROUP_JOBS, GROUP_SLOT}; + let mut clamps = self.clamps.lock().unwrap_or_else(|e| e.into_inner()); + let group = clamps + .group + .as_mut() + .filter(|g| g.pass == seat.pass && g.jobs < GROUP_JOBS)?; + let from = record + .checked_sub(group.sources[seat.source]) + .filter(|b| b.is_multiple_of(4))? + / 4; + let from = u32::try_from(from).ok()?; + let i = group.jobs; + let to = group.base + u64::from(i * GROUP_SLOT); + let job = crate::indirect::group_job(seat.source as u32, kind, stride, from, to as u32); + // SAFETY: the table's own words ([`HostList::clamp_group_seat`]): + // fewer than `GROUP_JOBS` jobs before this one; the list has not run. + unsafe { + let table = group.table as *mut u32; + let at = table.add(4 + 4 * i as usize); + for (k, word) in job.into_iter().enumerate() { + at.add(k).write_volatile(word); + } + table.write_volatile(i + 1); + } + group.jobs += 1; + let chunk = group.chunk; + let out = clamps.group_chunks[chunk].buffer.clone(); + drop(clamps); + crate::indirect::note_grouped(); + Some((out, to)) + } + /// The list's argument buffer, at least `bytes` long, common between /// its uses (`crate::indirect`). pub(crate) fn arguments(&self, bytes: u64) -> Option { @@ -6590,9 +6886,22 @@ impl Meter { crate::indirect::take_records().map(|n| n as f64 / report_frames); let [hoisted, inline] = crate::indirect::take_clamps().map(|n| n as f64 / report_frames); - let [anywhere, theirs, after_inline, unbatched] = + let [anywhere, theirs, after_inline, unbatched, inline_in_place] = crate::indirect::take_inline_why().map(|n| n as f64 / report_frames); let clamp_cuts = crate::indirect::take_cuts() as f64 / report_frames; + let [ + passes, + grouped, + after_clamp, + after_unknown, + over_written, + in_place, + prelude_passes, + prelude_grouped, + ] = crate::indirect::take_groups().map(|n| n as f64 / report_frames); + let [barrier_calls, barriers] = + crate::indirect::take_barriers().map(|n| n as f64 / report_frames); + let switches = take_work_switches() as f64 / report_frames; tracing::info!( "{fps:.1} fps, {ms:.1} ms/frame ({cpu:.1} preparing: copies {copy_ms:.1} ms \ {copy_mb:.1} MB, textures {texture_ms:.1} ms {texture_mb:.1} MB; {gpu:.1} on the \ @@ -6600,10 +6909,15 @@ impl Meter { dropped {dropped:.0} without a host pipeline ({making} being made) and \ {indirect:.0} indirect; {root_views:.0} root-view slots/frame; indirect \ max records {records:.0}/frame ({counted:.0} under a count; clamps \ - {hoisted:.0} hoisted, {inline:.0} inline: {anywhere:.0} after a write \ - anywhere, {theirs:.0} after a write of theirs, {after_inline:.0} after an \ - inline one, {unbatched:.0} unbatched; {clamp_cuts:.1} host lists started \ - for clamps)", + {hoisted:.0} hoisted ({prelude_grouped:.0} in {prelude_passes:.0} prelude \ + group passes), {inline:.0} inline: {anywhere:.0} after a write anywhere, \ + {theirs:.0} after a write of theirs, {after_inline:.0} after an inline one, \ + {unbatched:.0} unbatched, {inline_in_place:.0} read in place; \ + {clamp_cuts:.1} host lists started for clamps; {grouped:.0} in {passes:.0} \ + group passes; out of the prelude {after_clamp:.0} after a clamp of the list \ + not in it, {after_unknown:.0} after a write anywhere, {over_written:.0} over \ + written bytes, {in_place:.0} read in place; {barrier_calls:.0} barrier calls \ + of {barriers:.0} barriers); {switches:.0} draws after compute/frame", making = crate::pipeline::making() ); crate::ray_skip::log(report_frames); @@ -7550,6 +7864,8 @@ pub(crate) unsafe fn global_barrier( access_before: D3D12_BARRIER_ACCESS, access_after: D3D12_BARRIER_ACCESS, ) { + #[cfg(test)] + tests::GLOBAL_ISSUED.with(|l| l.borrow_mut().push((sync_before.0, sync_after.0))); let barrier = D3D12_GLOBAL_BARRIER { SyncBefore: sync_before, SyncAfter: sync_after, diff --git a/crates/victory-gpu/src/indirect.rs b/crates/victory-gpu/src/indirect.rs index 8f095f122..75625d066 100644 --- a/crates/victory-gpu/src/indirect.rs +++ b/crates/victory-gpu/src/indirect.rs @@ -122,6 +122,52 @@ pub(crate) fn with_clamp_cuts(enabled: bool, f: impl FnOnce() -> R) -> R { f() } +/// Whether indirect executions whose clamps their host list's prelude cannot +/// take (an earlier command of the list wrote their arguments, may have, or +/// an earlier clamp of the list is not in the prelude) are clamped in group +/// passes (`VICTORY_CLAMP_GROUPS=1`; off by default): one pass, recorded +/// where the first of them would have clamped inline, clamps every +/// execution recorded after it until a command writes what the next one +/// reads ([`GROUP_SOURCE`], `HostList::clamp_group_seat`). Inline, each +/// clamp is a compute dispatch between two draws: the host ends the render +/// pass for it and starts another after it, two Metal encoders on D3DMetal +/// for each of ~4,000 executions a world frame (a Metal System Trace of the +/// prison cell on an M5 Pro: 6,917 encoders a frame, compute and render in +/// turn). Needs the batched clamps (`VICTORY_BATCH_CLAMPS`, on by default). +/// +/// After avatarmanz's batches (VIctory-7900XTX 0375962, GPL-3.0: a pass +/// reading a table of jobs the CPU fills while the list records, and the +/// clamped records left readable as arguments, cc11ccc), with the list's +/// tracker of written ranges deciding where a group ends and every +/// execution its own job, charged in recorded order: what a group pass +/// writes is word for word what the executions' inline clamps write. +pub(crate) fn groups_enabled() -> bool { + #[cfg(test)] + if let Some(enabled) = TEST_CLAMP_GROUPS.with(std::cell::Cell::get) { + return enabled; + } + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| crate::switch::on("VICTORY_CLAMP_GROUPS")) +} + +#[cfg(test)] +std::thread_local! { + static TEST_CLAMP_GROUPS: std::cell::Cell> = const { std::cell::Cell::new(None) }; +} + +#[cfg(test)] +pub(crate) fn with_clamp_groups(enabled: bool, f: impl FnOnce() -> R) -> R { + struct Restore(Option); + impl Drop for Restore { + fn drop(&mut self) { + TEST_CLAMP_GROUPS.with(|c| c.set(self.0)); + } + } + let previous = TEST_CLAMP_GROUPS.with(|c| c.replace(Some(enabled))); + let _restore = Restore(previous); + f() +} + /// Whether a clamp a title list's host list cannot hoist (a shader of the /// list may have written its arguments) starts a new host list for the rest /// of the title list, whose prelude then takes it and the clamps after it, @@ -143,6 +189,485 @@ pub(crate) fn clamp_cuts_enabled() -> bool { *ON.get_or_init(|| crate::switch::on_by_default("VICTORY_CLAMP_CUTS")) } +/// Whether an indirect execution's records `[arguments, arguments + len)` +/// or its count word (title addresses) are read by the GPU in place in title +/// memory, which commands the list's tracker does not see may write while +/// it runs: such an execution is clamped inline, right before it runs, not +/// hoisted into the prelude nor grouped (avatarmanz 631400f: hoisted, trees +/// flickered). The hook for in-place section views +/// (`VICTORY_INPLACE_SECTIONS`, `crate::alias::clamp_in_place_inline`); +/// none are read in place yet. +pub(crate) fn clamped_in_place(arguments: u64, len: u64, count: Option) -> bool { + let _ = (arguments, len, count); + false +} + +/// What keeps an indirect execution's clamp out of its list's prelude: +/// memory read in place ([`clamped_in_place`]), else what `list` recorded +/// before ([`HostList::clamp_blocker`]). +pub(crate) fn blocker( + list: &HostList, + (arguments, len): (u64, u64), + count: Option, +) -> Option { + if clamped_in_place(arguments, len, count) { + return Some(crate::host::ClampBlock::InPlace); + } + list.clamp_blocker(arguments, len, count) +} + +/// An indirect execution's clamp was kept out of its list's prelude by +/// `blocker` (it was grouped or clamped inline). +pub(crate) fn note_held_out(blocker: Option) { + use crate::host::ClampBlock; + let at = match blocker { + None => return, + Some(ClampBlock::AfterInline) => 2, + Some(ClampBlock::Unknown) => 3, + Some(ClampBlock::Overlap) => 4, + Some(ClampBlock::InPlace) => 5, + }; + GROUPS[at].fetch_add(1, Ordering::Relaxed); +} + +/// The clamps recorded a `ResourceBarrier` call of `barriers` barriers, or +/// a `Barrier` call of one global barrier under narrow clamp sync (inline, +/// in a prelude's batch, in a group pass, or moving their buffers back to +/// common): each a wait D3DMetal records and submits at a cost. +pub(crate) fn note_barriers(barriers: usize) { + BARRIERS[0].fetch_add(1, Ordering::Relaxed); + BARRIERS[1].fetch_add(barriers as u64, Ordering::Relaxed); +} + +/// Since the last call: the clamps' `ResourceBarrier` calls and barriers. +pub(crate) fn take_barriers() -> [u64; 2] { + BARRIERS.each_ref().map(|n| n.swap(0, Ordering::Relaxed)) +} +static BARRIERS: [AtomicU64; 2] = [const { AtomicU64::new(0) }; 2]; + +/// Since the last call: group passes in lists, executions clamped in them, +/// clamps kept out of their list's prelude after an earlier clamp of the +/// list that is not in it, after a command that may write any title buffer, +/// over bytes an earlier command of the list wrote, and read in place; and +/// group passes in preludes and the hoisted clamps in them. +pub(crate) fn take_groups() -> [u64; 8] { + GROUPS.each_ref().map(|n| n.swap(0, Ordering::Relaxed)) +} +static GROUPS: [AtomicU64; 8] = [const { AtomicU64::new(0) }; 8]; + +/// The jobs of one group pass: a group of more goes on in another. The +/// trace's long runs of clamped draws hold ~480 executions each (5 runs, +/// 2,409 of a frame's 3,258 clamp/draw pairs). [`GROUP_SOURCE`] caps its +/// count at this. +pub(crate) const GROUP_JOBS: u32 = 512; +/// The bytes of a group pass's table of jobs in upload memory: the count +/// (and 12 bytes unused), then per job four words ([`GROUP_SOURCE`]). +pub(crate) const GROUP_TABLE: u64 = 16 + 16 * GROUP_JOBS as u64; +/// The bytes of one clamped record in a group's output: the longest record +/// a group takes. +pub(crate) const GROUP_SLOT: u32 = 32; +/// The title buffers (host copies) one group pass reads records from. +pub(crate) const GROUP_SOURCES: usize = 8; + +/// The group pass: the clamp of [`WORDS_SOURCE`] over the jobs of a table +/// instead of consecutive records, each with its own source buffer (one of +/// eight, `src0`..`src7`), record (a word index) and place in `dest`. Jobs +/// are charged in their order, a tile of 64 at a time, as records are. In +/// the words form, for Wine's HLSL compiler: no `Load` on read-write +/// buffers and no stores to indexed group-shared memory (scratch slots in +/// `budget` bytes `64..320`). +const GROUP_SOURCE: &str = r#" +cbuffer Params : register(b0) { uint flags; uint widest; }; +StructuredBuffer jobs : register(t0); +RWByteAddressBuffer dest : register(u0); +RWByteAddressBuffer budget : register(u1); +RWStructuredBuffer budgetWords : register(u2); +StructuredBuffer src0 : register(t1); +StructuredBuffer src1 : register(t2); +StructuredBuffer src2 : register(t3); +StructuredBuffer src3 : register(t4); +StructuredBuffer src4 : register(t5); +StructuredBuffer src5 : register(t6); +StructuredBuffer src6 : register(t7); +StructuredBuffer src7 : register(t8); +static const uint BUDGET = 1u << 27; +static const uint MOST_DRAW = 1u << 23; +static const uint GROUPS = 1u << 20; +static const uint PAST = 0xffffffffu; +uint word(uint s, uint at) { + if (s == 0u) { return src0[at]; } + if (s == 1u) { return src1[at]; } + if (s == 2u) { return src2[at]; } + if (s == 3u) { return src3[at]; } + if (s == 4u) { return src4[at]; } + if (s == 5u) { return src5[at]; } + if (s == 6u) { return src6[at]; } + return src7[at]; +} +// The record of `kind` and `stride` bytes at word `from` of source `s` +// clamped to byte `to` of `dest`, as `WORDS_SOURCE` clamps one; its cost. +uint clamped(uint s, uint kind, uint stride, uint from, uint to) { + uint cost; + uint base_bytes; + if (kind <= 1u) { + uint raw_count = word(s, from); + uint raw_instances = word(s, from + 1u); + uint count = raw_count; + uint instances = 0u; + if (raw_count >= MOST_DRAW) { + count = 0u; + } else { + instances = min(raw_instances, (1u << 26) / max(count, 1u)); + } + cost = count * instances; + dest.Store2(to, uint2(count, instances)); + if (kind == 1u) { + dest.Store3(to + 8u, uint3(word(s, from + 2u), word(s, from + 3u), word(s, from + 4u))); + base_bytes = 20u; + } else { + dest.Store2(to + 8u, uint2(word(s, from + 2u), word(s, from + 3u))); + base_bytes = 16u; + } + } else if (kind == 2u) { + uint3 g = uint3(word(s, from), word(s, from + 1u), word(s, from + 2u)); + g = uint3(g.x <= widest ? g.x : 65535u, min(g.y, 65535u), min(g.z, 65535u)); + if ((g.y != 0u && g.x > GROUPS / g.y) || (g.z != 0u && g.x * g.y > GROUPS / g.z)) { + g = uint3(0u, 0u, 0u); + } + cost = g.x * g.y * g.z * 64u; + dest.Store3(to, g); + base_bytes = 12u; + } else { + uint3 raw_g = uint3(word(s, from), word(s, from + 1u), word(s, from + 2u)); + uint3 g = uint3(raw_g.x, min(raw_g.y, 1u), min(raw_g.z, 1u)); + if (raw_g.x > 65535u) { + g = uint3(0u, 0u, 0u); + } + cost = g.x * g.y * g.z * 128u; + dest.Store3(to, g); + base_bytes = 12u; + } + for (uint extra = base_bytes; extra < stride; extra += 4u) { + dest.Store(to + extra, word(s, from + extra / 4u)); + } + return cost; +} +// A job: source (bits 0-3), kind (4-7) and stride in bytes (8-15); the +// record's first word in the source; its byte in `dest`; unused. +[numthreads(64, 1, 1)] +void main(uint3 id : SV_DispatchThreadID) { + uint lane = id.x; + uint left = 0u; + if (lane == 0u) { + left = BUDGET - min(budgetWords[0], BUDGET); + } + uint count = min(jobs[0], 512u); + uint take_rest = flags & 2u; + // Every tile a table holds: the count is read from memory, which the + // compiler cannot prove is the same in every lane, and barriers need + // that (fxc refuses them in a loop up to it). Tiles past the count + // only wait at them. + uint tiles = 8u; + for (uint tile = 0u; tile < tiles; tile++) { + uint j = tile * 64u + lane; + uint head = 0u; + uint to = 0u; + uint cost = 0u; + if (j < count) { + head = jobs[4u + j * 4u]; + to = jobs[6u + j * 4u]; + uint from = jobs[5u + j * 4u]; + cost = clamped(head & 15u, (head >> 4) & 15u, (head >> 8) & 255u, from, to); + } + budget.Store(64u + lane * 4u, cost); + AllMemoryBarrierWithGroupSync(); + if (lane == 0u && tile * 64u < count) { + for (uint i = 0u; i < 64u; i++) { + uint c = budgetWords[16u + i]; + if (c > left) { + budget.Store(64u + i * 4u, PAST); + if (take_rest != 0u) { + left = 0u; + } + } else { + left -= c; + } + } + } + AllMemoryBarrierWithGroupSync(); + if (j < count && budgetWords[16u + lane] == PAST) { + if (((head >> 4) & 15u) <= 1u) { + dest.Store2(to, uint2(0u, 0u)); + } else { + dest.Store3(to, uint3(0u, 0u, 0u)); + } + } + if (tile + 1u < tiles) { + AllMemoryBarrierWithGroupSync(); + } + } + if (lane == 0u) { + budget.Store(0u, BUDGET - left); + } +} +"#; + +/// The group pass's root signature and pipeline ([`GROUP_SOURCE`]): root +/// constants (flags, widest), the table, `dest`, the budget as bytes and as +/// words, the eight sources. +struct GroupClamp { + root: ID3D12RootSignature, + pso: ID3D12PipelineState, +} + +// SAFETY: D3D12 objects are free-threaded. +unsafe impl Send for GroupClamp {} +// SAFETY: as above. +unsafe impl Sync for GroupClamp {} + +fn group_clamp() -> Option<&'static GroupClamp> { + static CLAMP: std::sync::OnceLock> = std::sync::OnceLock::new(); + CLAMP + .get_or_init(|| { + use crate::compute::Param; + let mut params = vec![ + Param::Constants { + register: 0, + count: 2, + }, + Param::Srv { + register: 0, + space: 0, + }, + Param::Uav { register: 0 }, + Param::Uav { register: 1 }, + Param::Uav { register: 2 }, + ]; + let sources = + (1..=GROUP_SOURCES as u32).map(|register| Param::Srv { register, space: 0 }); + params.extend(sources); + match crate::compute::pipeline(GROUP_SOURCE, s!("clamp groups"), 0, ¶ms) { + Ok((root, pso)) => Some(GroupClamp { root, pso }), + Err(e) => { + tracing::warn!( + error = %e, + "no group clamp: indirect arguments are clamped one execution at a time" + ); + None + } + } + }) + .as_ref() +} + +/// The group pass's job for a record of `kind` and `stride` bytes at word +/// `from` of source `source`, clamped to byte `to` of the group's output. +pub(crate) fn group_job(source: u32, kind: u32, stride: u32, from: u32, to: u32) -> [u32; 4] { + [source | kind << 4 | stride << 8, from, to, 0] +} + +/// Whether an execution of `max` records of `kind`, `stride` bytes apart, +/// with a count or not, can be clamped in a group pass: one record of the +/// kinds the host runs, no longer than [`GROUP_SLOT`], no count (all the +/// title's indirect executions). +pub(crate) fn groupable(kind: u32, stride: u32, max: u32, counted: bool) -> bool { + matches!(kind, 0 | 1 | 2 | 10) + && max == 1 + && !counted + && stride.is_multiple_of(4) + && (12..=GROUP_SLOT).contains(&stride) +} + +/// Records, in `l`, a group pass over the table at host address `table` +/// into `out` (in state `before`, left readable as arguments), charging the +/// budget at `budget`, reading the eight `sources` (host addresses of title +/// buffers' copies, the first repeated where there are fewer). Replaces the +/// list's compute root signature and pipeline. False if the pass is not +/// available. +/// +/// With `cmd7` (narrow clamp sync, [`clamp_sync_enabled`]) the barriers are +/// global ones and `out` stays common (`before` is not read). The one +/// before the pass waits for every shader stage, not only compute as an +/// inline clamp's does: a job's record may have been written before the +/// pass and ordered for its execution only by a barrier of the title's +/// recorded after the pass, which the pass must not overtake. The title's +/// barriers are global UAV barriers (`crate::list`), which wait for all +/// shading; so does the legacy one here. +pub(crate) fn record_group( + l: &ID3D12GraphicsCommandList, + cmd7: Option<&ID3D12GraphicsCommandList7>, + (out, before): (&ID3D12Resource, D3D12_RESOURCE_STATES), + budget: u64, + table: u64, + sources: &[u64; GROUP_SOURCES], +) -> bool { + if group_clamp().is_none() { + return false; + } + // SAFETY: an open list; our buffer. + unsafe { + // Earlier shader writes (the title's argument passes) first. + if let Some(cmd7) = cmd7 { + global_barrier( + cmd7, + D3D12_BARRIER_SYNC_ALL_SHADING, + D3D12_BARRIER_SYNC_COMPUTE_SHADING, + D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, + D3D12_BARRIER_ACCESS_SHADER_RESOURCE | D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, + ); + note_barriers(1); + } else { + let mut barriers = vec![uav_barrier()]; + if before != D3D12_RESOURCE_STATE_UNORDERED_ACCESS { + barriers.push(transition( + out, + before, + D3D12_RESOURCE_STATE_UNORDERED_ACCESS, + )); + } + l.ResourceBarrier(&barriers); + note_barriers(barriers.len()); + } + dispatch_group(l, out.GetGPUVirtualAddress(), budget, table, sources); + GROUPS[0].fetch_add(1, Ordering::Relaxed); + if let Some(cmd7) = cmd7 { + global_barrier( + cmd7, + D3D12_BARRIER_SYNC_COMPUTE_SHADING, + D3D12_BARRIER_SYNC_EXECUTE_INDIRECT, + D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, + D3D12_BARRIER_ACCESS_INDIRECT_ARGUMENT, + ); + } else { + l.ResourceBarrier(&[transition( + out, + D3D12_RESOURCE_STATE_UNORDERED_ACCESS, + D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, + )]); + } + note_barriers(1); + } + true +} + +/// The group pass over the table at host address `table` into the buffer at +/// `out` (unordered access), with no barrier: [`record_group`]'s, or a +/// prelude's batch ([`record_batch`]). +fn dispatch_group( + l: &ID3D12GraphicsCommandList, + out: u64, + budget: u64, + table: u64, + sources: &[u64; GROUP_SOURCES], +) { + let Some(c) = group_clamp() else { + return; + }; + let constants = [u32::from(indirect_rest()) << 1, widest()]; + // SAFETY: an open list; our root signature, pipeline and buffers; title + // buffers' copies the list keeps, read in the common state (promoted). + unsafe { + l.SetComputeRootSignature(&c.root); + l.SetPipelineState(&c.pso); + l.SetComputeRoot32BitConstants(0, constants.len() as u32, constants.as_ptr().cast(), 0); + l.SetComputeRootShaderResourceView(1, table); + l.SetComputeRootUnorderedAccessView(2, out); + l.SetComputeRootUnorderedAccessView(3, budget); + l.SetComputeRootUnorderedAccessView(4, budget); + for (i, &source) in sources.iter().enumerate() { + l.SetComputeRootShaderResourceView(5 + i as u32, source); + } + l.Dispatch(1, 1, 1); + } +} + +/// What [`prelude_run`] finds: clamps taken, output buffer, sources, jobs. +type PreludeRun = (usize, u64, [u64; GROUP_SOURCES], Vec<[u32; 4]>); + +/// A run of a prelude's clamps ([`record_batch`]) in one group pass: how +/// many of `clamps` it takes (those of no record are skipped, as alone), +/// the buffer of `outs` (host address, bytes) their records go to, the +/// sources it binds and its jobs. `None` if the first cannot be grouped +/// ([`PendingClamp::src_base`]). +fn prelude_run(clamps: &[PendingClamp], outs: &[(u64, u64)]) -> Option { + let first = clamps.first().filter(|c| c.src_base != 0)?; + let (out, out_len) = *outs + .iter() + .find(|&&(at, len)| first.dst_gpu >= at && first.dst_gpu - at < len)?; + let mut sources = [first.src_base; GROUP_SOURCES]; + let mut bound = 1; + let mut jobs = Vec::new(); + let mut taken = 0; + for c in clamps { + if c.records == 0 { + taken += 1; + continue; + } + if c.src_base == 0 || jobs.len() == GROUP_JOBS as usize { + break; + } + let to = c.dst_gpu - out; + if c.dst_gpu < out || to + u64::from(c.stride) > out_len || to > u64::from(u32::MAX) { + break; + } + let source = match sources[..bound].iter().position(|&s| s == c.src_base) { + Some(s) => s, + None if bound < GROUP_SOURCES => { + sources[bound] = c.src_base; + bound += 1; + bound - 1 + } + None => break, + }; + let from = ((c.src_gpu - c.src_base) / 4) as u32; + jobs.push(group_job(source as u32, c.kind, c.stride, from, to as u32)); + taken += 1; + } + (!jobs.is_empty()).then_some((taken, out, sources, jobs)) +} + +/// An execution of one record was clamped in a group pass. +pub(crate) fn note_grouped() { + GROUPS[1].fetch_add(1, Ordering::Relaxed); + RECORDS[0].fetch_add(1, Ordering::Relaxed); +} + +/// Whether the group pass's pipeline is available on this host. +pub(crate) fn group_available() -> bool { + group_clamp().is_some() +} + +/// The host copies the last grouped executions read their records from, +/// newest first: what a group pass binds besides its first execution's +/// ([`recent_sources`]). +static RECENT: std::sync::Mutex>> = + std::sync::Mutex::new(Vec::new()); + +/// `source` was read by a grouped execution: first of [`RECENT`]. +pub(crate) fn note_source(source: &std::sync::Arc) { + let mut recent = RECENT.lock().unwrap_or_else(|e| e.into_inner()); + let this = std::sync::Arc::as_ptr(source); + if recent + .first() + .is_some_and(|r| std::ptr::eq(r.as_ptr(), this)) + { + return; + } + recent.retain(|r| r.strong_count() > 0 && !std::ptr::eq(r.as_ptr(), this)); + recent.insert(0, std::sync::Arc::downgrade(source)); + recent.truncate(2 * GROUP_SOURCES); +} + +/// The buffers of the host copies of [`RECENT`] that live, newest first. +pub(crate) fn recent_sources() -> Vec { + let recent = RECENT.lock().unwrap_or_else(|e| e.into_inner()); + recent + .iter() + .filter_map(std::sync::Weak::upgrade) + .map(|s| s.buffer.clone()) + .collect() +} + /// Whether the argument clamp compute pipeline is available on this host. pub(crate) fn available() -> bool { clamp().is_some() @@ -157,6 +682,10 @@ pub(crate) struct PendingClamp { pub(crate) kind: u32, pub(crate) stride: u32, pub(crate) records: u32, + /// The host address of the buffer `src_gpu` is in, if a group pass can + /// clamp it ([`groupable`], the record a word of it a 32-bit index + /// reaches); 0 otherwise. + pub(crate) src_base: u64, } const SOURCE: &str = include_str!("indirect-clamp.hlsl"); @@ -630,12 +1159,14 @@ fn bound_with( D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, D3D12_BARRIER_ACCESS_SHADER_RESOURCE | D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, ); + note_barriers(1); } else { // Earlier shader writes (the title's argument passes) first. l.ResourceBarrier(&[ uav_barrier(), transition(&out, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), ]); + note_barriers(2); } l.SetComputeRootSignature(&c.root); l.SetPipelineState(&c.pso); @@ -658,13 +1189,16 @@ fn bound_with( D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, D3D12_BARRIER_ACCESS_INDIRECT_ARGUMENT, ); + note_barriers(1); } else { l.ResourceBarrier(&[ uav_barrier(), transition(&out, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT), ]); + note_barriers(2); } } + list.note_work(true); Some((out, dst_offset)) } @@ -673,9 +1207,21 @@ fn bound_with( /// argument arena. Returns the arena buffer and byte offset ready for /// `ExecuteIndirect` (the prelude transitions the buffer into /// `INDIRECT_ARGUMENT`, and `list.close()` transitions it back to `COMMON`). +#[cfg(test)] pub(crate) fn enqueue( list: &HostList, gpu: u64, + spec: (u32, u32, u32), + count: Option, +) -> Option<(ID3D12Resource, u64)> { + enqueue_from(list, (None, gpu), spec, count) +} + +/// [`enqueue`] of a record in the buffer `source` if it is known: with clamp +/// groups on, the prelude may clamp it in a group pass ([`record_batch`]). +pub(crate) fn enqueue_from( + list: &HostList, + (source, gpu): (Option<&ID3D12Resource>, u64), (kind, stride, records): (u32, u32, u32), count: Option, ) -> Option<(ID3D12Resource, u64)> { @@ -685,6 +1231,21 @@ pub(crate) fn enqueue( } let count_gpu = count.filter(|at| at.is_multiple_of(4)); let bytes = u64::from(stride) * u64::from(records.max(1)); + let src_base = source + .filter(|_| groups_enabled() && groupable(kind, stride, records, count_gpu.is_some())) + .map(|buffer| { + // SAFETY: plain queries on a live buffer. + let (at, len) = unsafe { (buffer.GetGPUVirtualAddress(), buffer.GetDesc().Width) }; + let inside = gpu >= at + && gpu + bytes <= at.saturating_add(len) + && (gpu - at) / 4 <= u64::from(u32::MAX); + if inside && at.is_multiple_of(4) { + at + } else { + 0 + } + }) + .unwrap_or(0); let out = list.enqueue_hoisted_clamp(bytes, |dst_gpu| PendingClamp { src_gpu: gpu, dst_gpu, @@ -692,6 +1253,7 @@ pub(crate) fn enqueue( kind, stride, records, + src_base, })?; RECORDS[0].fetch_add(u64::from(records), Ordering::Relaxed); if count_gpu.is_some() { @@ -707,12 +1269,19 @@ pub(crate) fn enqueue( /// UAV barrier between consecutive dispatches so the shared list budget is /// charged in exact recorded order), and one post-batch transition into /// `INDIRECT_ARGUMENT`. +/// +/// With clamp groups on ([`groups_enabled`]), runs of clamps a group pass +/// can take are one dispatch each instead, over a table of jobs in memory +/// from `table` (bytes: host address, CPU pointer, the list's upload +/// memory): the barriers between dispatches, one per clamp, become one per +/// pass. pub(crate) fn record_batch( cmd: &ID3D12GraphicsCommandList, cmd7: Option<&ID3D12GraphicsCommandList7>, budget: u64, buffers: &[ID3D12Resource], clamps: &[PendingClamp], + table: &mut dyn FnMut(u64) -> Option<(u64, *mut u8)>, ) { let Some(c) = clamp() else { return; @@ -745,6 +1314,17 @@ pub(crate) fn record_batch( }; let widest = widest(); let rest = indirect_rest(); + let grouped = groups_enabled() && group_available(); + // The buffers' host addresses and bytes, where group passes write. + let outs: Vec<(u64, u64)> = if grouped { + buffers + .iter() + // SAFETY: plain queries on live buffers. + .map(|b| unsafe { (b.GetGPUVirtualAddress(), b.GetDesc().Width) }) + .collect() + } else { + Vec::new() + }; // SAFETY: `cmd` is the host list's open prelude command list; our clamp // pipeline, budget buffer, hoisted argument buffers, and title shadow // copies are kept alive by the host list until reset. @@ -757,18 +1337,20 @@ pub(crate) fn record_batch( D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, D3D12_BARRIER_ACCESS_SHADER_RESOURCE | D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, ); + note_barriers(1); } else { cmd.ResourceBarrier(&pre); + note_barriers(pre.len()); } - cmd.SetComputeRootSignature(&c.root); - cmd.SetPipelineState(&c.pso); - cmd.SetComputeRootUnorderedAccessView(2, budget); - if c.words { - cmd.SetComputeRootUnorderedAccessView(5, budget); - } + // Whether the one-execution clamp's root signature and pipeline are + // bound (a group pass binds its own). + let mut single = false; let mut dispatched = false; - for clamp in clamps { + let mut at = 0; + while at < clamps.len() { + let clamp = &clamps[at]; if clamp.records == 0 { + at += 1; continue; } if dispatched { @@ -785,6 +1367,35 @@ pub(crate) fn record_batch( } else { cmd.ResourceBarrier(&[uav_barrier()]); } + note_barriers(1); + } + dispatched = true; + let run = grouped.then(|| prelude_run(&clamps[at..], &outs)).flatten(); + if let Some((taken, out, sources, jobs)) = run + && let Some((table_gpu, words)) = table(GROUP_TABLE) + { + let words = words.cast::(); + words.write_volatile(jobs.len() as u32); + for (i, job) in jobs.iter().enumerate() { + for (k, &word) in job.iter().enumerate() { + words.add(4 + 4 * i + k).write_volatile(word); + } + } + dispatch_group(cmd, out, budget, table_gpu, &sources); + GROUPS[6].fetch_add(1, Ordering::Relaxed); + GROUPS[7].fetch_add(jobs.len() as u64, Ordering::Relaxed); + single = false; + at += taken; + continue; + } + if !single { + cmd.SetComputeRootSignature(&c.root); + cmd.SetPipelineState(&c.pso); + cmd.SetComputeRootUnorderedAccessView(2, budget); + if c.words { + cmd.SetComputeRootUnorderedAccessView(5, budget); + } + single = true; } let constants = [ clamp.kind, @@ -803,7 +1414,7 @@ pub(crate) fn record_batch( cmd.SetComputeRootUnorderedAccessView(3, clamp.dst_gpu); cmd.SetComputeRootShaderResourceView(4, clamp.count_gpu.unwrap_or(clamp.src_gpu)); cmd.Dispatch(1, 1, 1); - dispatched = true; + at += 1; } if let Some(cmd7) = cmd7 { global_barrier( @@ -813,8 +1424,10 @@ pub(crate) fn record_batch( D3D12_BARRIER_ACCESS_UNORDERED_ACCESS, D3D12_BARRIER_ACCESS_INDIRECT_ARGUMENT, ); + note_barriers(1); } else { cmd.ResourceBarrier(&post); + note_barriers(post.len()); } } } @@ -842,17 +1455,18 @@ pub(crate) fn note_inline(why: Option) { Some(ClampBlock::Overlap) => 1, Some(ClampBlock::AfterInline) => 2, None => 3, + Some(ClampBlock::InPlace) => 4, }; INLINE_WHY[at].fetch_add(1, Ordering::Relaxed); } /// Inline clamps since the last call by what kept them out of the /// prelude: `[a write anywhere, a write of their arguments, an inline clamp -/// before them, batching off]`. -pub(crate) fn take_inline_why() -> [u64; 4] { +/// before them, batching off, read in place]`. +pub(crate) fn take_inline_why() -> [u64; 5] { INLINE_WHY.each_ref().map(|n| n.swap(0, Ordering::Relaxed)) } -static INLINE_WHY: [AtomicU64; 4] = [const { AtomicU64::new(0) }; 4]; +static INLINE_WHY: [AtomicU64; 5] = [const { AtomicU64::new(0) }; 5]; /// A title list went on in a new host list for a clamp /// ([`clamp_cuts_enabled`]). @@ -880,6 +1494,7 @@ pub(crate) fn done(list: &HostList, arguments: &ID3D12Resource) { D3D12_RESOURCE_STATE_COMMON, )]) }; + note_barriers(1); } #[cfg(test)] @@ -887,7 +1502,9 @@ mod tests { use crate::backend::{HostGpu, HostMemory}; /// The clamp compiles with the system's compiler (including both 64-lane - /// and 1-lane words forms) and the host takes it. + /// and 1-lane words forms) and the host takes it, and so does the group + /// pass, whose compiler messages a failure shows (fxc on Windows refused + /// what Wine's compiler takes). #[test] fn argument_clamp_builds() { if super::host().is_none() { @@ -896,6 +1513,47 @@ mod tests { assert!(super::clamp().is_some()); assert!(super::make_words().is_ok()); assert!(super::make_words_1().is_ok()); + assert!( + super::group_available(), + "the group pass compiles and the host takes it: {}", + compiler_messages(super::GROUP_SOURCE) + ); + } + + /// What the system's compiler says about `source` (`main`, `cs_5_1`). + fn compiler_messages(source: &str) -> String { + use super::{D3DCompile, ID3DBlob, s}; + let (mut code, mut errors): (Option, Option) = (None, None); + // SAFETY: the source and names live through the call; out pointers ours. + let compiled = unsafe { + D3DCompile( + source.as_ptr().cast(), + source.len(), + s!("clamp groups"), + None, + None, + s!("main"), + s!("cs_5_1"), + 0, + 0, + &mut code, + Some(&mut errors), + ) + }; + let text = errors.map(|e| { + // SAFETY: the blob's own bytes. + let bytes = unsafe { + std::slice::from_raw_parts(e.GetBufferPointer().cast::(), e.GetBufferSize()) + }; + String::from_utf8_lossy(bytes).into_owned() + }); + match compiled { + Ok(()) => format!( + "compiles, so the host refused it ({})", + text.unwrap_or_default() + ), + Err(e) => format!("{e}: {}", text.unwrap_or_default()), + } } /// Every word the clamp `c` writes for `records` records of `kind` made @@ -1466,6 +2124,20 @@ void main(uint3 g : SV_GroupID) { assert!(why[0] >= 1 && why[2] >= 1 && why[3] >= 1, "{why:?}"); } + /// Clamp groups are a switch (`VICTORY_CLAMP_GROUPS=1`), off unless set; + /// tests turn them on and off per thread. + #[test] + fn clamp_groups_switch_toggles_per_thread() { + super::with_clamp_groups(true, || { + assert!(super::groups_enabled()); + super::with_clamp_groups(false, || assert!(!super::groups_enabled())); + assert!(super::groups_enabled()); + }); + if std::env::var_os("VICTORY_CLAMP_GROUPS").is_none() { + assert!(!super::groups_enabled(), "off by default"); + } + } + #[test] fn write_hazard_tracks_overlaps_coalescing_and_order() { let mut h = crate::host::WriteHazard::default(); @@ -2056,6 +2728,1034 @@ void main() { assert_eq!(host.log_messages(), None); } + /// Group passes take executions of one record of 12 to 32 bytes with no + /// count, at most a table's jobs a pass. + #[test] + fn groups_take_one_record_of_12_to_32_bytes_with_no_count() { + use super::groupable; + assert!( + groupable(10, 12, 1, false) && groupable(0, 16, 1, false) && groupable(1, 20, 1, false) + ); + assert!(groupable(2, 12, 1, false) && groupable(0, 32, 1, false)); + assert!(!groupable(0, 16, 2, false), "one record"); + assert!(!groupable(0, 16, 0, false), "one record"); + assert!(!groupable(0, 16, 1, true), "no count"); + assert!(!groupable(0, 36, 1, false), "no longer than a slot"); + assert!(!groupable(0, 18, 1, false), "whole words"); + assert!(!groupable(3, 12, 1, false), "the kinds the host runs"); + let cap = format!("min(jobs[0], {}u)", super::GROUP_JOBS); + assert!( + super::GROUP_SOURCE.contains(&cap), + "the pass walks at most a table's jobs" + ); + let tiles = format!("uint tiles = {}u;", super::GROUP_JOBS.div_ceil(64)); + assert!( + super::GROUP_SOURCE.contains(&tiles), + "every tile a table holds, a constant" + ); + } + + /// The executions the world frame clamps inline come after the compute + /// passes that write their arguments. In one list: a dispatch writes + /// every record over stale words, an inline clamp spends half the + /// budget, then executions of every kind, some from other buffers than + /// the first (bound by the pass from the recent ones, or past the eight + /// it binds: another pass), one clamped inline among them (its count: + /// the group after it is another), and records the budget no longer + /// holds. Their group passes write word for word what inline clamps + /// after the same producer write, reading what it wrote, with the + /// clamps' barriers of either kind (`VICTORY_CLAMP_SYNC`): narrow ones + /// record no transition, and a pass waits for every shader stage. + #[test] + fn group_passes_clamp_as_inline_clamps_do() { + use super::*; + use crate::compute::Param; + const PRODUCE: &str = r#" +StructuredBuffer src : register(t0); +RWByteAddressBuffer dst : register(u0); +cbuffer Count : register(b0) { uint n; }; +[numthreads(64, 1, 1)] +void main(uint3 id : SV_DispatchThreadID) { + for (uint i = id.x; i < n; i += 64u) { + dst.Store(i * 4u, src[i]); + } +} +"#; + let Some(host) = host() else { + return; // no host GPU here + }; + let (root, pso) = crate::compute::pipeline( + PRODUCE, + s!("produce"), + 0, + &[ + Param::Srv { + register: 0, + space: 0, + }, + Param::Uav { register: 0 }, + Param::Constants { + register: 0, + count: 1, + }, + ], + ) + .unwrap(); + // (buffer, kind, stride, words of the record, a count word) + type Case = (usize, u32, u32, Vec, Option); + let cases: Vec = vec![ + (0, 0, 16, vec![12, 2, 7, 9], None), + (0, 1, 20, vec![100, 3, 7, 8, 9], None), + (1, 2, 12, vec![widest() + 5, 1, 1], None), + (0, 10, 12, vec![100, 5, 3], None), + (1, 10, 32, vec![70_000, 1, 1, 4, 5, 6, 7, 8], None), + (0, 0, 32, vec![1 << 25, 10, 1, 2, 11, 12, 13, 14], None), + (2, 2, 12, vec![65_535, 17, 1], None), + // Clamped inline (a count): the next execution opens a pass. + (0, 0, 16, vec![5, 1, 3, 4], Some(1)), + (0, 0, 16, vec![6, 2, 3, 4], None), + // Nine more buffers: past the eight one pass binds. + (3, 10, 12, vec![9, 1, 1], None), + (4, 10, 12, vec![10, 1, 1], None), + (5, 10, 12, vec![11, 1, 1], None), + (6, 10, 12, vec![12, 1, 1], None), + (7, 10, 12, vec![13, 1, 1], None), + (8, 10, 12, vec![14, 1, 1], None), + (9, 10, 12, vec![15, 1, 1], None), + (10, 10, 12, vec![16, 1, 1], None), + (11, 10, 12, vec![17, 1, 1], None), + // Half the budget each: after the half spent and the rest, the + // first no longer fits; then small ones still do. + (0, 0, 16, vec![1 << 22, 16, 0, 0], None), + (1, 0, 16, vec![1 << 22, 16, 0, 0], None), + (0, 0, 16, vec![3, 1, 0, 0], None), + (1, 1, 20, vec![3, 1, 0, 0, 0], None), + ]; + const STALE: u32 = 0x5a5a_5a5a; + const BUFFERS: usize = 12; + let at_of = |i: usize| -> u64 { 64 * i as u64 + 16 }; + // Per buffer: the title's (stale until produced, at 16 bytes in) and + // what the producer writes there. + let buffers: Vec<_> = (0..BUFFERS) + .map(|b| { + let (title, title_ptr) = host.shared_buffer(64 * cases.len() as u64 + 64).unwrap(); + let (produced, produced_ptr) = + host.shared_buffer(64 * cases.len() as u64 + 64).unwrap(); + for (i, (buffer, _, _, words, _)) in cases.iter().enumerate() { + if *buffer == b { + // SAFETY: our mapped buffers, written before any list runs. + unsafe { + for (w, &word) in words.iter().enumerate() { + produced_ptr + .cast::() + .add(at_of(i) as usize) + .cast::() + .add(w) + .write_volatile(word); + } + } + } + } + (title, title_ptr, produced) + }) + .collect(); + let (counter, counter_ptr) = host.shared_buffer(4).unwrap(); + let (half, half_ptr) = host.shared_buffer(16).unwrap(); + // SAFETY: as above. + unsafe { + counter_ptr.cast::().write_volatile(1); + half_ptr + .cast::<[u32; 4]>() + .write_volatile([1 << 22, 16, 0, 0]); + } + // SAFETY: live buffers. + let gpu = |b: &ID3D12Resource| unsafe { b.GetGPUVirtualAddress() }; + let words = (64 * cases.len() + 64) / 4; + let stale = || { + for (_, ptr, _) in &buffers { + // SAFETY: our mapped buffers; no list is running. + unsafe { (0..words).for_each(|i| ptr.cast::().add(i).write_volatile(STALE)) }; + } + }; + // The producer, then an inline clamp spending half the budget. + let produce = |list: &HostList| { + let l = &list.list; + let n = words as u32; + // SAFETY: an open list; our pipeline and live buffers. + unsafe { + l.SetComputeRootSignature(&root); + l.SetPipelineState(&pso); + for (title, _, produced) in &buffers { + l.SetComputeRootShaderResourceView(0, gpu(produced)); + l.SetComputeRootUnorderedAccessView(1, gpu(title)); + l.SetComputeRoot32BitConstants(2, 1, (&raw const n).cast(), 0); + l.Dispatch(1, 1, 1); + } + l.ResourceBarrier(&[uav_barrier()]); + } + list.note_unknown_gpu_write(); + let (spent, _) = bound(list, gpu(&half), (0, 16, 1), None).unwrap(); + list.note_inline_clamp(); + done(list, &spent); + }; + let read = |buffer: &ID3D12Resource, offset: u64, n: usize| -> Vec { + let mut p = std::ptr::null_mut(); + // SAFETY: a mapped buffer of ours; the lists have run. + unsafe { + buffer.Map(0, None, Some(&mut p)).unwrap(); + let base = p.cast::().add(offset as usize).cast::(); + let out = (0..n).map(|i| base.add(i).read_volatile()).collect(); + buffer.Unmap(0, None); + out + } + }; + // Where an inline clamp's record can be read once the list ran: a + // copy taken before the next clamp writes the list's one argument + // buffer, or, under narrow clamp sync, its own slice. + let copy_out = |list: &HostList, (out, offset): (ID3D12Resource, u64), bytes: u64| { + if list.narrow_clamp_sync() { + done(list, &out); + return (out, offset); + } + let (copy, _) = host.shared_buffer(bytes).unwrap(); + // SAFETY: an open list; live buffers. + unsafe { + list.list.ResourceBarrier(&[transition( + &out, + D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, + D3D12_RESOURCE_STATE_COPY_SOURCE, + )]); + list.list.CopyBufferRegion(©, 0, &out, offset, bytes); + list.list.ResourceBarrier(&[transition( + &out, + D3D12_RESOURCE_STATE_COPY_SOURCE, + D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, + )]); + } + done(list, &out); + (copy, 0) + }; + + // Inline: the producer and every clamp in one list C, with legacy + // barriers. + let copies: Vec<_> = with_clamp_sync(false, || { + stale(); + let c = host.new_list().unwrap(); + produce(&c); + let copies: Vec<_> = cases + .iter() + .enumerate() + .map(|(i, (b, kind, stride, _, count))| { + let (title, ..) = &buffers[*b]; + let counted = count.map(|_| gpu(&counter)); + let out = + bound(&c, gpu(title) + at_of(i), (*kind, *stride, 1), counted).unwrap(); + c.note_inline_clamp(); + copy_out(&c, out, u64::from(*stride)) + }) + .collect(); + c.close(); + host.execute(&[&c]); + copies + }); + let inline: Vec> = cases + .iter() + .zip(&copies) + .map(|(case, (copy, offset))| read(copy, *offset, case.2 as usize / 4)) + .collect(); + for (i, words) in inline.iter().enumerate() { + assert!( + !words.contains(&STALE), + "execution {i} read the producer's words" + ); + } + + // Grouped: the producer and the executions in one list A, with + // legacy and with narrow clamp barriers. + // The buffers other executions read lately, newest first. + let recent: Vec = [1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 3] + .iter() + .map(|&b| buffers[b].0.clone()) + .collect(); + for narrow in [false, true] { + with_clamp_sync(narrow, || { + stale(); + let a = host.new_list().unwrap(); + assert_eq!( + a.narrow_clamp_sync(), + narrow, + "the host has enhanced barriers" + ); + produce(&a); + let _ = ( + crate::host::tests::issued(), + crate::host::tests::global_issued(), + ); + let mut opened = Vec::new(); + let places: Vec<(ID3D12Resource, u64)> = cases + .iter() + .enumerate() + .map(|(i, (b, kind, stride, _, count))| { + let (title, ..) = &buffers[*b]; + let record = gpu(title) + at_of(i); + if count.is_some() { + let out = bound(&a, record, (*kind, *stride, 1), Some(gpu(&counter))) + .unwrap(); + a.note_inline_clamp(); + opened.push(false); + return copy_out(&a, out, u64::from(*stride)); + } + assert!(groupable(*kind, *stride, 1, false)); + assert!(group_available(), "the group pass compiles"); + let (seat, open) = a + .clamp_group_seat((title, record), (record, u64::from(*stride)), || { + recent.clone() + }) + .expect("a seat"); + opened.push(open); + a.clamp_group_join(&seat, record, (*kind, *stride)) + .expect("a place") + }) + .collect(); + a.close(); + // A pass opens at the first execution (binding buffers 0, 1, + // 2 and 4 to 8), after the inline one, and at the first + // execution of each buffer the pass before does not bind. + let passes: Vec = opened + .iter() + .enumerate() + .filter(|&(_, &o)| o) + .map(|(i, _)| i) + .collect(); + assert_eq!(passes, [0, 8, 9, 15, 16, 17, 18], "narrow {narrow}"); + let (moved, global) = ( + crate::host::tests::issued(), + crate::host::tests::global_issued(), + ); + if narrow { + assert!(moved.is_empty(), "narrow passes move no buffer: {moved:?}"); + let all = ( + D3D12_BARRIER_SYNC_ALL_SHADING.0, + D3D12_BARRIER_SYNC_COMPUTE_SHADING.0, + ); + let waits = global.iter().filter(|&&b| b == all).count(); + assert_eq!( + waits, + passes.len(), + "each pass waits for every shader stage: {global:?}" + ); + } else { + assert!(global.is_empty() && !moved.is_empty()); + } + host.execute(&[&a]); + for (i, (case, (buffer, offset))) in cases.iter().zip(&places).enumerate() { + let grouped = read(buffer, *offset, case.2 as usize / 4); + assert_eq!( + grouped, inline[i], + "narrow {narrow}: execution {i} kind {}", + case.1 + ); + } + }); + } + let n = cases.len(); + assert_eq!( + inline[n - 4][0], + 0, + "the budget left no room for the first half" + ); + assert_eq!(inline[n - 2][..2], [3, 1], "small ones fit"); + assert_eq!(host.log_messages(), None); + } + + /// With clamp groups on, a prelude's clamps go in group passes where + /// they can (one record, no count; up to eight buffers a pass) and one + /// at a time where not (several records, a count, none), in their + /// order: word for word what inline clamps write, across kinds and + /// strides, ten buffers, and records the budget no longer holds. With + /// legacy and with narrow clamp barriers (`VICTORY_CLAMP_SYNC`), where + /// the budget's barrier between dispatches is one a pass. + #[test] + fn prelude_group_passes_clamp_as_inline_clamps_do() { + use super::*; + let Some(host) = host() else { + return; // no host GPU here + }; + // (buffer, kind, stride, records, words, a count word) + type Case = (usize, u32, u32, u32, Vec, Option); + let mut cases: Vec = vec![ + (0, 0, 16, 1, vec![12, 2, 7, 9], None), + (1, 1, 20, 1, vec![100, 3, 7, 8, 9], None), + (2, 2, 12, 1, vec![widest() + 5, 1, 1], None), + (0, 10, 32, 1, vec![70_000, 1, 1, 4, 5, 6, 7, 8], None), + (1, 0, 16, 3, [5, 1, 0, 0].repeat(3), None), + (2, 10, 12, 1, vec![100, 5, 3], None), + (0, 0, 16, 2, [6, 2, 0, 0].repeat(2), Some(1)), + (1, 2, 12, 0, vec![9, 1, 1], None), + (0, 0, 32, 1, vec![1 << 25, 10, 1, 2, 11, 12, 13, 14], None), + ]; + cases.extend((3..10).map(|b| (b, 10, 12, 1, vec![b as u32, 1, 1], None))); + cases.extend([ + (0, 0, 16, 1, vec![1 << 22, 16, 0, 0], None), + (4, 0, 16, 1, vec![1 << 22, 16, 0, 0], None), + (5, 1, 20, 1, vec![1 << 22, 16, 0, 0, 0], None), + (6, 0, 16, 1, vec![3, 1, 0, 0], None), + ]); + let at_of = |i: usize| -> u64 { 128 * i as u64 + 32 }; + let bytes = 128 * cases.len() as u64 + 128; + let buffers: Vec<_> = (0..10) + .map(|b| { + let (buffer, ptr) = host.shared_buffer(bytes).unwrap(); + for (i, (from, _, _, _, words, _)) in cases.iter().enumerate() { + if *from == b { + // SAFETY: our mapped buffer, written before any list runs. + unsafe { + let to = ptr.cast::().add(at_of(i) as usize).cast::(); + words + .iter() + .enumerate() + .for_each(|(w, &word)| to.add(w).write_volatile(word)); + } + } + } + buffer + }) + .collect(); + let (counter, counter_ptr) = host.shared_buffer(4).unwrap(); + // SAFETY: as above. + unsafe { counter_ptr.cast::().write_volatile(1) }; + // SAFETY: live buffers. + let gpu = |b: &ID3D12Resource| unsafe { b.GetGPUVirtualAddress() }; + let walked = |(_, _, stride, records, _, count): &Case| { + count.map_or(*records, |c| c.min(*records)) as usize * (*stride as usize / 4) + }; + let read = |buffer: &ID3D12Resource, offset: u64, n: usize| -> Vec { + let mut p = std::ptr::null_mut(); + // SAFETY: a mapped buffer of ours; the list has run. + unsafe { + buffer.Map(0, None, Some(&mut p)).unwrap(); + let base = p.cast::().add(offset as usize).cast::(); + let out = (0..n).map(|i| base.add(i).read_volatile()).collect(); + buffer.Unmap(0, None); + out + } + }; + + let copies: Vec<_> = with_clamp_sync(false, || { + let c = host.new_list().unwrap(); + let copies: Vec<_> = cases + .iter() + .enumerate() + .map(|(i, (b, kind, stride, records, _, count))| { + let counted = count.map(|_| gpu(&counter)); + let at = gpu(&buffers[*b]) + at_of(i); + let (out, offset) = bound(&c, at, (*kind, *stride, *records), counted).unwrap(); + c.note_inline_clamp(); + let bytes = u64::from(*stride) * u64::from((*records).max(1)); + let (copy, _) = host.shared_buffer(bytes).unwrap(); + // SAFETY: an open list; live buffers. + unsafe { + c.list.ResourceBarrier(&[transition( + &out, + D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, + D3D12_RESOURCE_STATE_COPY_SOURCE, + )]); + c.list.CopyBufferRegion(©, 0, &out, offset, bytes); + c.list.ResourceBarrier(&[transition( + &out, + D3D12_RESOURCE_STATE_COPY_SOURCE, + D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, + )]); + } + done(&c, &out); + copy + }) + .collect(); + c.close(); + host.execute(&[&c]); + copies + }); + let inline: Vec> = cases + .iter() + .zip(&copies) + .map(|(case, copy)| read(copy, 0, walked(case))) + .collect(); + let n = cases.len(); + assert_eq!( + (inline[n - 4][0], inline[n - 3][0], inline[n - 2][0]), + (1 << 22, 0, 0) + ); + assert_eq!(inline[n - 1][..2], [3, 1]); + + assert!(group_available(), "the group pass compiles"); + for narrow in [false, true] { + let _ = crate::host::tests::global_issued(); + let p = host.new_list().unwrap(); + assert_eq!( + with_clamp_sync(narrow, || p.narrow_clamp_sync()), + narrow, + "the host has enhanced barriers" + ); + let places: Vec<_> = with_clamp_sync(narrow, || { + super::with_clamp_groups(true, || { + cases + .iter() + .enumerate() + .map(|(i, (b, kind, stride, records, _, count))| { + let record = (Some(&buffers[*b]), gpu(&buffers[*b]) + at_of(i)); + let counted = count.map(|_| gpu(&counter)); + enqueue_from(&p, record, (*kind, *stride, *records), counted).unwrap() + }) + .collect() + }) + }); + with_clamp_sync(narrow, || { + p.close(); + super::with_clamp_groups(true, || host.execute(&[&p])); + }); + if narrow { + // Before and after the batch, and between its five dispatches + // (three group passes, a clamp of three records and a counted + // one) instead of between its nineteen clamps. + let global = crate::host::tests::global_issued(); + assert_eq!(global.len(), 1 + 4 + 1, "{global:?}"); + } + for (i, (case, (b, at))) in cases.iter().zip(&places).enumerate() { + assert_eq!( + read(b, *at, walked(case)), + inline[i], + "narrow {narrow}: clamp {i} kind {}", + case.1 + ); + } + } + assert_eq!(host.log_messages(), None); + } + + /// A producer, executions, the producer again over the same records, + /// executions again, in one list: the executions after the second + /// write cannot join the pass before it and get one of their own after + /// it, so every execution runs the groups written last before it (those + /// after an unrelated write still join). Twice through the same list, + /// reset between: its passes' outputs start common again; and twice + /// more with narrow clamp barriers (`VICTORY_CLAMP_SYNC`). + #[test] + fn a_write_ends_the_group_of_what_it_writes() { + use super::*; + use crate::compute::Param; + const MARK: &str = r#" +RWByteAddressBuffer marks : register(u0); +cbuffer Offset : register(b0) { uint base; }; +[numthreads(1, 1, 1)] +void main(uint3 g : SV_GroupID) { + marks.Store((base + g.x) * 4, 1); +} +"#; + const PRODUCE: &str = r#" +RWByteAddressBuffer records : register(u0); +cbuffer First : register(b0) { uint first; }; +[numthreads(4, 1, 1)] +void main(uint3 id : SV_DispatchThreadID) { + records.Store3(id.x * 12u, uint3(first + id.x, 1u, 1u)); +} +"#; + let Some(host) = host() else { + return; // no host GPU here + }; + let (mark_root, mark_pso) = crate::compute::pipeline( + MARK, + s!("group_marks"), + 0, + &[ + Param::Uav { register: 0 }, + Param::Constants { + register: 0, + count: 1, + }, + ], + ) + .unwrap(); + let (produce_root, produce_pso) = crate::compute::pipeline( + PRODUCE, + s!("group_produce"), + 0, + &[ + Param::Uav { register: 0 }, + Param::Constants { + register: 0, + count: 1, + }, + ], + ) + .unwrap(); + let argument = D3D12_INDIRECT_ARGUMENT_DESC { + Type: D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH, + ..Default::default() + }; + let desc = D3D12_COMMAND_SIGNATURE_DESC { + ByteStride: 12, + NumArgumentDescs: 1, + pArgumentDescs: &argument, + NodeMask: 0, + }; + let mut signature: Option = None; + // SAFETY: plain command signature creation. + unsafe { + host.device + .CreateCommandSignature(&desc, None::<&ID3D12RootSignature>, &mut signature) + .unwrap() + }; + let signature = signature.unwrap(); + let (marks, marks_ptr) = host.shared_buffer(256 * 4).unwrap(); + // The title's buffer of records (a registered host copy, stale: one + // group each) and one elsewhere. + let backing = vec![0u8; 4096]; + let at = backing.as_ptr() as u64; + crate::shadow::register(at as usize, at, 4096); + let records = crate::shadow::find(at, 48).expect("registered shadow"); + let list = host.new_list().unwrap(); + assert!(group_available(), "the group pass compiles"); + for round in 0..4 { + let narrow = round >= 2; + with_clamp_sync(narrow, || { + assert_eq!( + list.narrow_clamp_sync(), + narrow, + "the host has enhanced barriers" + ); + // SAFETY: our mapped buffers, written before the list runs. + unsafe { + std::ptr::write_bytes(marks_ptr, 0, 256 * 4); + let mut p = std::ptr::null_mut(); + records.buffer.Map(0, None, Some(&mut p)).unwrap(); + let stale = p + .cast::() + .add(records.offset(at) as usize) + .cast::<[u32; 3]>(); + (0..4).for_each(|i| stale.add(i).write_volatile([1, 1, 1])); + records.buffer.Unmap(0, None); + } + let produce = |first: u32| { + records.gpu_writes(&list, at, 48, false); + // SAFETY: an open list; our pipeline and live buffers. + unsafe { + let l = &list.list; + l.SetComputeRootSignature(&produce_root); + l.SetPipelineState(&produce_pso); + l.SetComputeRootUnorderedAccessView(0, records.gpu(at)); + l.SetComputeRoot32BitConstants(1, 1, (&raw const first).cast(), 0); + l.Dispatch(1, 1, 1); + l.ResourceBarrier(&[uav_barrier()]); + } + }; + let mut opened = Vec::new(); + let mut execute = |i: u64, base: u32| { + let record = at + 12 * (i % 4); + assert!( + list.clamp_blocker(record, 12, None).is_some(), + "out of the prelude" + ); + let (seat, open) = list + .clamp_group_seat( + (&records.buffer, records.gpu(record)), + (record, 12), + Vec::new, + ) + .expect("a seat"); + opened.push(open); + let (clamped, offset) = list + .clamp_group_join(&seat, records.gpu(record), (2, 12)) + .unwrap(); + // SAFETY: an open list; our pipeline, signature and live buffers. + unsafe { + let l = &list.list; + l.SetComputeRootSignature(&mark_root); + l.SetPipelineState(&mark_pso); + l.SetComputeRootUnorderedAccessView(0, marks.GetGPUVirtualAddress()); + l.SetComputeRoot32BitConstants(1, 1, (&raw const base).cast(), 0); + l.ExecuteIndirect(&signature, 1, &clamped, offset, None, 0); + } + }; + produce(3); + (0..4).for_each(|i| execute(i, 16 * i as u32)); + // A write elsewhere: the group goes on. + list.defer_gpu_write(&records, at as usize + 1024, 16, false); + execute(1, 64); + produce(7); + (4..8).for_each(|i| execute(i, 16 * i as u32 + 64)); + assert_eq!( + opened, + [true, false, false, false, false, true, false, false, false], + "round {round}" + ); + list.close(); + host.execute(&[&list]); + let ran = |base: usize| { + // SAFETY: mapped; the list has run. + (0..16).filter(|&k| unsafe { marks_ptr.cast::().add(base + k).read_volatile() } != 0).count() + }; + let first: Vec = (0..4).map(|i| ran(16 * i)).collect(); + assert_eq!( + first, + [3, 4, 5, 6], + "round {round}: the first write's groups" + ); + assert_eq!(ran(64), 4, "round {round}: after the write elsewhere"); + let second: Vec = (4..8).map(|i| ran(16 * i + 64)).collect(); + assert_eq!( + second, + [7, 8, 9, 10], + "round {round}: the second write's groups" + ); + list.reset(); + }); + } + // SAFETY: a live buffer. + let past = unsafe { marks.GetGPUVirtualAddress() } + (1 << 30); + assert!( + list.clamp_group_seat((&marks, past), (past, 12), Vec::new) + .is_none(), + "a record outside its buffer is clamped inline" + ); + crate::shadow::unregister(at as usize, at); + drop(backing); + assert_eq!(host.log_messages(), None); + } + + /// A culling dispatch writes the draw records right before the indirect + /// draws that read them, then writes them again before the next draws + /// (shadow cascades over one argument buffer), in one list: each draw + /// draws what the culling dispatch before it wrote, not what the one + /// before that did. Each draw's record asks for one small triangle a + /// unit into a row of its own; the rows' lit pixels are the counts the + /// draws ran. With legacy and narrow clamp barriers. + #[test] + fn draws_after_a_culling_pass_draw_what_it_wrote_last() { + use super::*; + use crate::compute::Param; + use windows::Win32::Graphics::Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; + use windows::Win32::Graphics::Dxgi::Common::{ + DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_SAMPLE_DESC, + }; + const CULL: &str = r#" +RWByteAddressBuffer records : register(u0); +cbuffer First : register(b0) { uint first; }; +[numthreads(4, 1, 1)] +void main(uint3 id : SV_DispatchThreadID) { + records.Store4(id.x * 16u, uint4(3u * (first + id.x), 1u, 0u, 0u)); +} +"#; + // Triangle t of a draw covers the centre of pixel (t, row) of a 16x16 + // target and no other. + const DRAW: &str = r#" +cbuffer Row : register(b0) { uint row; }; +float4 vs(uint id : SV_VertexID) : SV_Position { + float x = float(id / 3u); + uint corner = id % 3u; + float2 at = float2(x + (corner == 1u ? 1.75 : -0.25), float(row) + (corner == 2u ? 1.75 : -0.25)); + return float4(at.x / 8.0 - 1.0, 1.0 - at.y / 8.0, 0.0, 1.0); +} +float4 ps() : SV_Target { return float4(1.0, 1.0, 1.0, 1.0); } +"#; + let Some(host) = host() else { + return; // no host GPU here + }; + assert!(group_available(), "the group pass compiles"); + let (cull_root, cull_pso) = crate::compute::pipeline( + CULL, + s!("group_cull"), + 0, + &[ + Param::Uav { register: 0 }, + Param::Constants { + register: 0, + count: 1, + }, + ], + ) + .unwrap(); + let compile = |entry: windows::core::PCSTR, target: windows::core::PCSTR| { + let mut blob: Option = None; + // SAFETY: a static source and names that outlive the call. + unsafe { + D3DCompile( + DRAW.as_ptr().cast(), + DRAW.len(), + s!("group_draw"), + None, + None, + entry, + target, + 0, + 0, + &mut blob, + None, + ) + .unwrap(); + } + blob.unwrap() + }; + let (vs, ps) = ( + compile(s!("vs"), s!("vs_5_1")), + compile(s!("ps"), s!("ps_5_1")), + ); + let draw_root = crate::compute::root_signature( + &host.device, + &[Param::Constants { + register: 0, + count: 1, + }], + ) + .unwrap(); + let target_desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: 16, + Height: 16, + DepthOrArraySize: 1, + MipLevels: 1, + Format: DXGI_FORMAT_R8G8B8A8_UNORM, + SampleDesc: DXGI_SAMPLE_DESC { + Count: 1, + Quality: 0, + }, + Flags: D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, + ..Default::default() + }; + // SAFETY: compiled shaders, a live root signature; plain creation. + let (draw_pso, target, rtv_heap, rtv, back, footprint) = unsafe { + let mut desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC { + pRootSignature: std::mem::transmute_copy(&draw_root), + VS: D3D12_SHADER_BYTECODE { + pShaderBytecode: vs.GetBufferPointer(), + BytecodeLength: vs.GetBufferSize(), + }, + PS: D3D12_SHADER_BYTECODE { + pShaderBytecode: ps.GetBufferPointer(), + BytecodeLength: ps.GetBufferSize(), + }, + SampleMask: u32::MAX, + PrimitiveTopologyType: D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, + NumRenderTargets: 1, + SampleDesc: DXGI_SAMPLE_DESC { + Count: 1, + Quality: 0, + }, + ..Default::default() + }; + desc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM; + desc.BlendState.RenderTarget[0].RenderTargetWriteMask = 0xf; + desc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID; + desc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE; + let pso: ID3D12PipelineState = host.device.CreateGraphicsPipelineState(&desc).unwrap(); + let mut target: Option = None; + host.device + .CreateCommittedResource( + &D3D12_HEAP_PROPERTIES { + Type: D3D12_HEAP_TYPE_DEFAULT, + ..Default::default() + }, + D3D12_HEAP_FLAG_NONE, + &target_desc, + D3D12_RESOURCE_STATE_RENDER_TARGET, + None, + &mut target, + ) + .unwrap(); + let target = target.unwrap(); + let rtv_heap: ID3D12DescriptorHeap = host + .device + .CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { + Type: D3D12_DESCRIPTOR_HEAP_TYPE_RTV, + NumDescriptors: 1, + ..Default::default() + }) + .unwrap(); + let rtv = rtv_heap.GetCPUDescriptorHandleForHeapStart(); + host.device.CreateRenderTargetView(&target, None, rtv); + let mut footprint = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default(); + let mut total = 0u64; + host.device.GetCopyableFootprints( + &target_desc, + 0, + 1, + 0, + Some(&mut footprint), + None, + None, + Some(&mut total), + ); + let (back, _) = host.shared_buffer(total).unwrap(); + (pso, target, rtv_heap, rtv, back, footprint) + }; + let argument = D3D12_INDIRECT_ARGUMENT_DESC { + Type: D3D12_INDIRECT_ARGUMENT_TYPE_DRAW, + ..Default::default() + }; + let desc = D3D12_COMMAND_SIGNATURE_DESC { + ByteStride: 16, + NumArgumentDescs: 1, + pArgumentDescs: &argument, + NodeMask: 0, + }; + let mut signature: Option = None; + // SAFETY: plain command signature creation. + unsafe { + host.device + .CreateCommandSignature(&desc, None::<&ID3D12RootSignature>, &mut signature) + .unwrap() + }; + let signature = signature.unwrap(); + // The title's argument buffer (a registered host copy, stale: one + // triangle each). + let backing = vec![0u8; 4096]; + let at = backing.as_ptr() as u64; + crate::shadow::register(at as usize, at, 4096); + let records = crate::shadow::find(at, 64).expect("registered shadow"); + for narrow in [false, true] { + with_clamp_sync(narrow, || { + let list = host.new_list().unwrap(); + assert_eq!( + list.narrow_clamp_sync(), + narrow, + "the host has enhanced barriers" + ); + // SAFETY: our mapped buffer, written before the list runs. + unsafe { + let mut p = std::ptr::null_mut(); + records.buffer.Map(0, None, Some(&mut p)).unwrap(); + let stale = p + .cast::() + .add(records.offset(at) as usize) + .cast::<[u32; 4]>(); + (0..4).for_each(|i| stale.add(i).write_volatile([3, 1, 0, 0])); + records.buffer.Unmap(0, None); + } + let l = &list.list; + let cull = |first: u32| { + // As a title dispatch binding the records' UAV declares it. + records.gpu_writes(&list, at, 64, false); + // SAFETY: an open list; our pipeline and live buffers. + unsafe { + l.SetComputeRootSignature(&cull_root); + l.SetPipelineState(&cull_pso); + l.SetComputeRootUnorderedAccessView(0, records.gpu(at)); + l.SetComputeRoot32BitConstants(1, 1, (&raw const first).cast(), 0); + l.Dispatch(1, 1, 1); + // The title's barrier before its draws. + l.ResourceBarrier(&[uav_barrier()]); + } + }; + let mut opened = Vec::new(); + let mut draw = |i: u64, row: u32| { + let record = at + 16 * i; + assert!( + list.clamp_blocker(record, 16, None).is_some(), + "out of the prelude" + ); + let (seat, open) = list + .clamp_group_seat( + (&records.buffer, records.gpu(record)), + (record, 16), + Vec::new, + ) + .expect("a seat"); + opened.push(open); + let (clamped, offset) = list + .clamp_group_join(&seat, records.gpu(record), (0, 16)) + .unwrap(); + let viewport = D3D12_VIEWPORT { + Width: 16.0, + Height: 16.0, + MaxDepth: 1.0, + ..Default::default() + }; + let scissor = windows::Win32::Foundation::RECT { + left: 0, + top: 0, + right: 16, + bottom: 16, + }; + // SAFETY: an open list; our pipeline, signature and live buffers. + unsafe { + l.OMSetRenderTargets(1, Some(&rtv), false, None); + l.RSSetViewports(&[viewport]); + l.RSSetScissorRects(&[scissor]); + l.SetGraphicsRootSignature(&draw_root); + l.SetPipelineState(&draw_pso); + l.SetGraphicsRoot32BitConstants(0, 1, (&raw const row).cast(), 0); + l.IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + l.ExecuteIndirect(&signature, 1, &clamped, offset, None, 0); + } + }; + // SAFETY: an open list and a live view. + unsafe { l.ClearRenderTargetView(rtv, &[0.0; 4], None) }; + cull(3); + (0..4).for_each(|i| draw(i, i as u32)); + cull(7); + (0..4).for_each(|i| draw(i, 4 + i as u32)); + // SAFETY: an open list; live resources in the states named. + unsafe { + l.ResourceBarrier(&[transition( + &target, + D3D12_RESOURCE_STATE_RENDER_TARGET, + D3D12_RESOURCE_STATE_COPY_SOURCE, + )]); + let to = D3D12_TEXTURE_COPY_LOCATION { + pResource: std::mem::transmute_copy(&back), + Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { + PlacedFootprint: footprint, + }, + }; + let from = D3D12_TEXTURE_COPY_LOCATION { + pResource: std::mem::transmute_copy(&target), + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { + SubresourceIndex: 0, + }, + }; + l.CopyTextureRegion(&to, 0, 0, 0, &from, None); + l.ResourceBarrier(&[transition( + &target, + D3D12_RESOURCE_STATE_COPY_SOURCE, + D3D12_RESOURCE_STATE_RENDER_TARGET, + )]); + } + list.close(); + host.execute(&[&list]); + let mut p = std::ptr::null_mut(); + // SAFETY: a mapped readback of ours; the list has run. + let lit: Vec = unsafe { + back.Map(0, None, Some(&mut p)).unwrap(); + let pitch = footprint.Footprint.RowPitch as usize; + let lit = (0..8) + .map(|y| { + let row = + std::slice::from_raw_parts(p.cast::().add(y * pitch), 16 * 4); + row.chunks(4).filter(|px| px[0] != 0).count() + }) + .collect(); + back.Unmap(0, None); + lit + }; + assert_eq!( + lit, + [3, 4, 5, 6, 7, 8, 9, 10], + "narrow {narrow}: each draw ran its last culled record" + ); + assert_eq!( + opened, + [true, false, false, false, true, false, false, false], + "narrow {narrow}" + ); + }); + } + drop(rtv_heap); + crate::shadow::unregister(at as usize, at); + drop(backing); + assert_eq!(host.log_messages(), None); + } + /// A list that mixes a hoisted prefix (in prelude) and an inline suffix /// (in the main list) shares the same list budget sequentially across the /// prelude -> main list boundary. From 5518b816f42a53691826a346b57bfc42b7c6de9a Mon Sep 17 00:00:00 2001 From: Saurabh Suresh Date: Sat, 28 Nov 2026 08:09:31 +0400 Subject: [PATCH 2/2] diag(gpu): count host lists past half of their indirect budget Every clamp charges a host list's indirect executions to one budget of 2^27 (vertex instances, threads), and a record past what is left runs empty. One record costs at most half of it (2^26 instances or 2^20 groups of 64), so a list that spent half or less emptied nothing for the budget. With #570's cuts each new host list has a budget of its own; clamp groups keep a title list in one host list and one budget, as inline clamps do. At a list's reset the word it spent is read (and cleared), and the clamp part of the frame line now ends with how many host lists a frame spent more than half of their budget, and the most one spent. A run with and without VICTORY_CLAMP_GROUPS=1 shows whether groups empty records there that cuts did not. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/host.rs | 35 ++++++++++++++++- crates/victory-gpu/src/indirect.rs | 62 ++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index e5f00c316..b19f80a41 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -279,6 +279,20 @@ pub(crate) fn take_work_switches() -> u64 { WORK_SWITCHES.swap(0, Ordering::Relaxed) } +/// Host lists whose indirect executions spent more than half of their +/// budget ([`crate::indirect::BUDGET`]) since the last frame-rate line, and +/// the most one spent. No record costs more than half, so a list that spent +/// half or less emptied none for the budget; one past half may have. +static BUDGET_SPENT: [std::sync::atomic::AtomicU64; 2] = + [const { std::sync::atomic::AtomicU64::new(0) }; 2]; + +/// [`BUDGET_SPENT`] since the last call: lists past half, the most spent. +pub(crate) fn take_budget_spent() -> [u64; 2] { + BUDGET_SPENT + .each_ref() + .map(|n| n.swap(0, Ordering::Relaxed)) +} + /// Time spent in submissions since the last frame-rate line, in /// microseconds: preparing them on the CPU (title memory copies, texture /// detiling), then waiting for the GPU. @@ -4933,6 +4947,20 @@ impl HostList { /// Resets hoisted clamp tracking after the list finishes running, keeping /// the chunks needed by the last cycle (plus one) for zero-allocation reuse. fn reset_hoisted_clamps(&self) { + if let Some(Some((_, data))) = self.budget.get() { + // SAFETY: the list's own mapped budget word; the list is not + // running. Cleared so a list reset unsubmitted counts nothing. + let spent = unsafe { + let word = *data as *mut u32; + let spent = word.read_volatile(); + word.write_volatile(0); + u64::from(spent) + }; + if spent > u64::from(crate::indirect::BUDGET / 2) { + BUDGET_SPENT[0].fetch_add(1, Ordering::Relaxed); + } + BUDGET_SPENT[1].fetch_max(spent, Ordering::Relaxed); + } let (spent_chunks, spent_oversized) = { let mut clamps = self.clamps.lock().unwrap_or_else(|e| e.into_inner()); clamps.pending.clear(); @@ -6902,6 +6930,9 @@ impl Meter { let [barrier_calls, barriers] = crate::indirect::take_barriers().map(|n| n as f64 / report_frames); let switches = take_work_switches() as f64 / report_frames; + let [past_half, most_spent] = take_budget_spent(); + let past_half = past_half as f64 / report_frames; + let most_spent = most_spent as f64 * 100.0 / f64::from(crate::indirect::BUDGET); tracing::info!( "{fps:.1} fps, {ms:.1} ms/frame ({cpu:.1} preparing: copies {copy_ms:.1} ms \ {copy_mb:.1} MB, textures {texture_ms:.1} ms {texture_mb:.1} MB; {gpu:.1} on the \ @@ -6917,7 +6948,9 @@ impl Meter { group passes; out of the prelude {after_clamp:.0} after a clamp of the list \ not in it, {after_unknown:.0} after a write anywhere, {over_written:.0} over \ written bytes, {in_place:.0} read in place; {barrier_calls:.0} barrier calls \ - of {barriers:.0} barriers); {switches:.0} draws after compute/frame", + of {barriers:.0} barriers; {past_half:.1} host lists past half of their \ + budget, the most {most_spent:.0}% of it); {switches:.0} draws after \ + compute/frame", making = crate::pipeline::making() ); crate::ray_skip::log(report_frames); diff --git a/crates/victory-gpu/src/indirect.rs b/crates/victory-gpu/src/indirect.rs index 75625d066..e7744282e 100644 --- a/crates/victory-gpu/src/indirect.rs +++ b/crates/victory-gpu/src/indirect.rs @@ -255,6 +255,11 @@ pub(crate) fn take_groups() -> [u64; 8] { } static GROUPS: [AtomicU64; 8] = [const { AtomicU64::new(0) }; 8]; +/// The work a host list's indirect executions may do (vertex instances, +/// threads), as every clamp shader has it (`BUDGET`): past it, records run +/// empty. One record costs at most half of it. +pub(crate) const BUDGET: u32 = 1 << 27; + /// The jobs of one group pass: a group of more goes on in another. The /// trace's long runs of clamped draws hold ~480 executions each (5 runs, /// 2,409 of a frame's 3,258 clamp/draw pairs). [`GROUP_SOURCE`] caps its @@ -2728,6 +2733,63 @@ void main() { assert_eq!(host.log_messages(), None); } + /// Every clamp shader has the budget the frame line's figures are of + /// (`crate::host::take_budget_spent`), and caps one record's cost at + /// half of it: a draw's instances at 2^26 over its count, a dispatch's + /// groups at 2^20 of 64 threads. + #[test] + fn every_clamp_charges_the_budget_half_a_record_at_most() { + let budget = format!( + "static const uint BUDGET = 1u << {};", + super::BUDGET.trailing_zeros() + ); + for source in [ + super::SOURCE, + super::WORDS_SOURCE, + super::WORDS_1_SOURCE, + super::GROUP_SOURCE, + ] { + assert!(source.contains(&budget), "{budget}"); + assert!(source.contains("(1u << 26) / max(count, 1u)")); + assert!(source.contains("GROUPS = 1u << 20")); + } + // A draw: count times instances; a dispatch: groups of 64 threads. + assert_eq!(1u32 << 26, super::BUDGET / 2); + assert_eq!((1u32 << 20) * 64, super::BUDGET / 2); + } + + /// A host list whose executions spent more than half of its budget is + /// counted when it is reset, with the most a list spent: two dispatches + /// of 2^20 groups spend all of it. + #[test] + fn a_list_past_half_of_its_budget_is_counted() { + use super::*; + let Some(host) = host() else { + return; // no host GPU here + }; + let (record, ptr) = host.shared_buffer(12).unwrap(); + // SAFETY: our mapped buffer, written before the list runs; live. + let gpu = unsafe { + ptr.cast::<[u32; 3]>().write_volatile([1024, 1024, 1]); + record.GetGPUVirtualAddress() + }; + let list = host.new_list().unwrap(); + for _ in 0..2 { + let (out, _) = bound(&list, gpu, (2, 12, 1), None).unwrap(); + list.note_inline_clamp(); + done(&list, &out); + } + list.close(); + host.execute(&[&list]); + assert_eq!(list.budget_used(), Some(BUDGET)); + list.reset(); + let [past_half, most] = crate::host::take_budget_spent(); + assert!(past_half >= 1, "{past_half}"); + assert_eq!(most, u64::from(BUDGET)); + assert_eq!(list.budget_used(), Some(0), "cleared at the reset"); + assert_eq!(host.log_messages(), None); + } + /// Group passes take executions of one record of 12 to 32 bytes with no /// count, at most a table's jobs a pass. #[test]