diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index 8485014..f50a96f 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -168,6 +168,20 @@ pub(crate) fn release_dynamic(heap: usize, first: u32, n: u32) { } } +/// Makes dynamic slot `index` of the host heap behind the title heap at +/// `heap`, if that heap still exists, the constant buffer view of `size` +/// bytes at `gpu`. +#[cfg(windows)] +pub(crate) fn constant_slot(heap: usize, index: u32, gpu: u64, size: u32) { + let heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner()); + if heaps.iter().any(|&(_, _, h)| h == heap) + // SAFETY: registered heaps are live (removed when dropped). + && let Some(Some(host)) = unsafe { &*(heap as *const Heap) }.host.get() + { + host.constant_at(index, gpu, size); + } +} + #[cfg(windows)] impl Drop for Heap { fn drop(&mut self) { diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index 7d290a3..6530abd 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -1164,6 +1164,8 @@ impl State { let len = s.left(a).min(limit); s.refresh(list, a, len); if writes { + #[cfg(feature = "diagnostics")] + crate::host::wide_write(a, len, self.pipeline); s.gpu_writes(list, a, len); } }; @@ -2213,7 +2215,8 @@ impl State { host.make_null(index); continue; }; - list.defer_fill(to, address as *const u8, size as usize); + let slot = (std::ptr::from_ref(heap) as usize, index); + list.defer_constants(to, address as *const u8, size as usize, slot); host.constant_at(index, gpu, size); } Some(view) => { @@ -2395,7 +2398,7 @@ impl State { }; let (from, to, stride) = (from as usize, to as usize, stride as usize); let n = rects as usize; - list.defer_host(move || { + list.defer_host_read(from, n * 3 * stride, move || { // The vertices are read in place, at submission: no change // of mapping under the read (see // `crate::watch::title_memory_reading`). @@ -2763,6 +2766,8 @@ fn readable(list: &HostList, kind: ResourceType, view: &View) { // A shader that cannot store to buffers leaves the range as // the title has it: nothing to find afterwards. if STORES.get() || !write_skip() { + #[cfg(feature = "diagnostics")] + crate::host::wide_write(address, len, 0); shadow.gpu_writes(list, address, len); } else { #[cfg(feature = "diagnostics")] diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 4ae7669..b2cbc74 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -1642,16 +1642,19 @@ enum Step { /// A copy into title memory (destination, source, bytes): the title's /// buffer copies, which later refreshes must see. Copy(usize, usize, usize), - /// A copy of title memory into the list's own upload memory. - Fill(usize, usize, usize), + /// A copy of title memory into the list's own upload memory; if a + /// descriptor slot shows that memory and nothing else does (a constant + /// buffer of a table made for the draw), the title heap and the slot. + Fill(usize, usize, usize, Option<(usize, u32)>), /// Brings a range (address, bytes) of a buffer's host copy up to date: /// once per submission, whichever lists ask, until a `Copy` or `Run` /// intervenes. Refresh(std::sync::Arc, usize, usize), /// Work that may write title memory. Run(Box), - /// Work that writes host memory only. - Host(Box), + /// Work that writes host memory only, and the title memory it reads + /// (from, to; empty for none). + Host(Box, (usize, usize)), /// Shaders write a range (address, bytes) of a buffer's host copy. GpuWrite(std::sync::Arc, usize, usize), /// Brings a texture's staging buffer up to date: once per submission, @@ -2019,6 +2022,8 @@ struct Sent { number: u64, card: Option, back: Vec, + /// What was taken off it to be done on the card. + own: Option, /// Its bottom-of-pipe writes. writes: Vec<(usize, crate::queue::Written, usize)>, } @@ -2034,6 +2039,8 @@ struct Flight { order: Vec, card: Option, back: Vec, + /// The list of ours that ran ahead of them ([`card_steps`]). + own: Option, } /// One thing in the line of what waits for the card, in order. @@ -2067,6 +2074,8 @@ struct Line { /// With [`relay`]: the bottom-of-pipe writes of the flights being /// landed (taken out of `line`, not done yet), for [`late_met`]. in_hand: Vec<(usize, crate::queue::Written, usize)>, + /// With [`card_steps`]: what the flights being landed write. + landing: Vec, } // SAFETY: D3D12 objects (free-threaded) and host lists nothing records @@ -2082,6 +2091,7 @@ static LINE: Mutex = Mutex::new(Line { poke: false, landed: Vec::new(), in_hand: Vec::new(), + landing: Vec::new(), }); /// Told when a flight joins [`LINE`] or it is poked. static LINE_MOVED: std::sync::Condvar = std::sync::Condvar::new(); @@ -2152,14 +2162,14 @@ fn flying() -> bool { /// runs steps of ours that may read or write it anywhere; what the /// flights' shaders write is not there yet. fn late_blocked(runs: &[&HostList]) -> Option { - let written = { + let (written, copies) = { let line = line(); if line.written.is_empty() { return None; } - line.written.clone() + (line.written.clone(), card_steps::copies(&line)) }; - blocked_by(&written, runs, true) + blocked_by(&written, runs, true).or_else(|| card_steps::copied_read(&copies, runs).then_some(WHY_READ)) } /// Whether a small fill of upload memory, or copy of a title buffer, goes on without waiting for @@ -2188,48 +2198,131 @@ fn fill_is_titles(src: usize, len: usize) -> bool { on && len <= SMALL && (crate::watch::written_since_taken(src, len) || !crate::watch::landed_lately(src, len, 600)) } -/// Diagnostics: the reads that made a submission wait for flights: what -/// kind, where, how much, and the declared write it lies in; how often and -/// how long the wait was. +/// Diagnostics: the waits for flights, by what waited, why it was not done +/// on the card ([`card_steps`]), its bytes, and the declared write it lies +/// in (start, bytes). #[cfg(feature = "diagnostics")] -type Blame = std::collections::HashMap<(&'static str, usize, usize, usize, usize), (u64, u64)>; +type Blamed = (&'static str, &'static str, usize, usize, usize); +/// How often and how long, and the address the last one read. #[cfg(feature = "diagnostics")] -static BLAME: Mutex> = Mutex::new(None); +static BLAME: Mutex>> = Mutex::new(None); +/// What the wait that comes next is for, and why not on the card. #[cfg(feature = "diagnostics")] -static BLAMED: Mutex> = Mutex::new(None); +static BLAMED: Mutex<(Option<(Blamed, usize)>, &'static str)> = Mutex::new((None, "")); + +/// Diagnostics: shader writes declared over a megabyte or more: the range +/// (from, to) and what declares it ([`wide_write`]): how often, and its +/// shaders. +#[cfg(feature = "diagnostics")] +static WIDE_WRITES: Mutex>> = Mutex::new(None); + +/// Diagnostics: a draw or dispatch declares that it writes `len` bytes at +/// `address`, through a root view of `pipeline`, a view in a table (0) or a +/// raw address (1). +#[cfg(feature = "diagnostics")] +pub(crate) fn wide_write(address: u64, len: u64, pipeline: usize) { + if len < 1 << 20 { + return; + } + let mut all = WIDE_WRITES.lock().unwrap_or_else(|e| e.into_inner()); + let e = all + .get_or_insert_with(Default::default) + .entry((address as usize, (address + len) as usize, pipeline)) + .or_insert_with(|| { + let shaders = match pipeline { + 0 => "a view in a table".into(), + 1 => "a raw address".into(), + p => crate::pipeline::host_pipeline(p as crate::object::This).map_or_else(String::new, |p| { + p.stages.iter().map(|s| s.shader.hash.to_string()).collect::>().join("+") + }), + }; + (0, shaders) + }); + e.0 += 1; +} #[cfg(feature = "diagnostics")] fn blame(kind: &'static str, written: &[(usize, usize)], at: usize, len: usize) { let i = written.partition_point(|r| r.1 <= at); let (from, to) = written.get(i).copied().unwrap_or((0, 0)); - *BLAMED.lock().unwrap_or_else(|e| e.into_inner()) = Some((kind, at, len, from, to - from)); + let mut blamed = BLAMED.lock().unwrap_or_else(|e| e.into_inner()); + let why = blamed.1; + blamed.0 = Some(((kind, why, len, from, to - from), at)); } -/// The wait for what [`blame`] last named took `us`. +/// Why [`card_steps`] left the submission to the wait that comes next. #[cfg(feature = "diagnostics")] -fn blamed(us: u64) { - if let Some(key) = BLAMED.lock().unwrap_or_else(|e| e.into_inner()).take() { - let mut all = BLAME.lock().unwrap_or_else(|e| e.into_inner()); - let e = all.get_or_insert_with(Default::default).entry(key).or_insert((0, 0)); - e.0 += 1; - e.1 += us; - } +fn refused(why: &'static str) { + *BLAMED.lock().unwrap_or_else(|e| e.into_inner()) = (None, why); } +/// The wait for what [`blame`] last named, or a wait of another reason than +/// title memory read, took `us`. +#[cfg(feature = "diagnostics")] +fn blamed(why: usize, us: u64) { + let (named, refused) = std::mem::replace(&mut *BLAMED.lock().unwrap_or_else(|e| e.into_inner()), (None, "")); + let (key, at) = match (why, named) { + (WHY_READ, Some(named)) => named, + (WHY_READ, None) => (("a host copy of what a copy done on the card wrote", refused, 0, 0, 0), 0), + _ => { + let kind = match why { + WHY_SYNC => "a synchronous submission, a present or the sweep", + WHY_VALUE => "a memory value", + WHY_STALE => "title memory changed under shader writes", + WHY_RUN => "a step of ours", + WHY_MOST => "lists in the air", + WHY_FENCE => "a blocking fence wait", + _ => "another reason", + }; + ((kind, "", 0, 0, 0), 0) + } + }; + let mut all = BLAME.lock().unwrap_or_else(|e| e.into_inner()); + let e = all.get_or_insert_with(Default::default).entry(key).or_default(); + *e = (e.0 + 1, e.1 + us, at); +} + +/// For the frame-rate line: the five that waited longest. #[cfg(feature = "diagnostics")] fn log_blame(frames: f64) { let all = BLAME.lock().unwrap_or_else(|e| e.into_inner()).take().unwrap_or_default(); + let wide = WIDE_WRITES.lock().unwrap_or_else(|e| e.into_inner()).take().unwrap_or_default(); let mut all: Vec<_> = all.into_iter().collect(); - all.sort_unstable_by_key(|(_, (_, us))| std::cmp::Reverse(*us)); - for ((kind, at, len, from, size), (n, us)) in all.into_iter().take(8) { + all.sort_unstable_by_key(|(_, (_, us, _))| std::cmp::Reverse(*us)); + for ((kind, why, len, from, size), (n, us, at)) in all.into_iter().take(5) { + // The passes that declare it written, the ones that do so most. + let mut passes: std::collections::BTreeMap<&str, u64> = std::collections::BTreeMap::new(); + for ((a, b, _), (n, shaders)) in &wide { + if *a < at + len && at < *b { + *passes.entry(shaders).or_default() += n; + } + } + let mut passes: Vec<(&str, u64)> = passes.into_iter().collect(); + passes.sort_unstable_by_key(|p| std::cmp::Reverse(p.1)); + let passes: Vec = + passes.into_iter().take(5).map(|(shaders, n)| format!("{shaders} {:.1}x", n as f64 / frames)).collect(); tracing::info!( - "waited for flights, per frame: {:.1} ms in {:.2} waits for {kind} {at:#x}+{len:#x}, in shader writes declared over {from:#x}+{size:#x}", + "waited for flights, per frame: {:.1} ms in {:.2} waits for {kind} +{len:#x} (last at {at:#x}), in shader writes declared \ + over {from:#x}+{size:#x} (declared per frame over a megabyte or more there, by shader: {}){}{why}", us as f64 / 1e3 / frames, - n as f64 / frames + n as f64 / frames, + passes.join(", "), + if why.is_empty() { "" } else { "; not done on the card: " }, ); } } +/// Whether flights that write `written` stop a read of `len` bytes at `src`. +fn read_blocked(written: &[(usize, usize)], src: usize, len: usize) -> bool { + touches(written, src, src + len) && !fill_is_titles(src, len) +} + +/// Whether they stop a title buffer copy, and if so whether by its source. +fn copy_blocked(written: &[(usize, usize)], dst: usize, src: usize, len: usize) -> Option { + let read = read_blocked(written, src, len); + (read || touches(written, dst, dst + len)).then_some(read) +} + /// [`late_blocked`] against `written`, the title memory flights' shaders /// may write (in order, joined); `count` whether the kind of read is /// counted for the frame line. @@ -2241,12 +2334,11 @@ fn blocked_by(written: &[(usize, usize)], runs: &[&HostList], count: bool) -> Op for step in &list.deferred().copies { match step { &Step::Copy(dst, src, len) => { - let read = touches(written, src, src + len) && !fill_is_titles(src, len); - if read || touches(written, dst, dst + len) { + if let Some(_read) = copy_blocked(written, dst, src, len) { if count { LATE_STATS[15].fetch_add(1, Ordering::Relaxed); #[cfg(feature = "diagnostics")] - if read { + if _read { blame("a buffer copy's source", written, src, len); } else { blame("a buffer copy's destination", written, dst, len); @@ -2255,8 +2347,8 @@ fn blocked_by(written: &[(usize, usize)], runs: &[&HostList], count: bool) -> Op return Some(WHY_READ); } } - &Step::Fill(_, src, len) => { - if touches(written, src, src + len) && !fill_is_titles(src, len) { + &Step::Fill(_, src, len, _) => { + if read_blocked(written, src, len) { if count { LATE_STATS[16].fetch_add(1, Ordering::Relaxed); #[cfg(feature = "diagnostics")] @@ -2277,7 +2369,7 @@ fn blocked_by(written: &[(usize, usize)], runs: &[&HostList], count: bool) -> Op } } Step::Run(_) => return Some(WHY_RUN), - Step::Refresh(..) | Step::GpuWrite(..) | Step::Host(_) => {} + Step::Refresh(..) | Step::GpuWrite(..) | Step::Host(..) => {} } } } @@ -2517,7 +2609,7 @@ fn relay_wait(why: usize, done: impl Fn(&Line) -> bool) { LATE_WAITED[why.min(10)].fetch_add(us, Ordering::Relaxed); SUBMISSION_US[1].fetch_add(us, Ordering::Relaxed); #[cfg(feature = "diagnostics")] - blamed(us); + blamed(why, us); } /// What the thread that lands flights fetches card copies' pieces with @@ -2582,6 +2674,9 @@ fn relay_worker() { _ => None, }) .collect(); + if card_steps::stage() > 0 { + line.landing = card_steps::writers(&items); + } break Ok(items); } }; @@ -2711,6 +2806,9 @@ fn late_line(frames: f64) { r[4] / 1000.0, ); } + card_steps::log(frames); + #[cfg(feature = "diagnostics")] + log_blame(frames); if late_exact() { let [pages, runs, bytes] = crate::shadow::merged_stats().map(|n| n as f64 / frames); tracing::info!( @@ -2721,6 +2819,8 @@ fn late_line(frames: f64) { } } +mod card_steps; + #[cfg(test)] mod late_tests { use super::{continues, join_ranges, touches}; @@ -3691,6 +3791,9 @@ impl Host { if let Some(card) = flight.card { self.card_done(card); } + if let Some(own) = flight.own { + own.landed(); + } landed.push((flight.lists, flight.order)); } Late::Write(at, what, bytes) => what.write(at as *mut u8, bytes), @@ -3713,10 +3816,12 @@ impl Host { poke, landed: kept, in_hand, + landing, .. } = &mut *line; kept.extend(landed); in_hand.clear(); + landing.clear(); // What is still in the air, from the flights that are. written.clear(); *lists = 0; @@ -3909,6 +4014,7 @@ impl Host { order, card: sent.card, back: sent.back, + own: sent.own, })); items.extend(sent.writes.into_iter().map(|(at, what, bytes)| Late::Write(at, what, bytes))); *flying >= late_most() @@ -3953,6 +4059,7 @@ impl Host { order: Vec::new(), card: sent.card, back: sent.back, + own: sent.own, })); items.extend(sent.writes.into_iter().map(|(at, what, bytes)| Late::Write(at, what, bytes))); } @@ -4133,17 +4240,32 @@ impl Host { // Flights in the air ([`late`]): a submission that is waited for // settles them first, one sent ahead only if preparing it reads // what they write. + // Or the reads are done on the card, behind the flights. + let own = if late { self.card_steps(runs) } else { None }; if !late { self.settle(&mut submitted, WHY_SYNC); - } else if let Some(why) = late_blocked(runs) { + } else if own.is_none() + && let Some(why) = late_blocked(runs) + { if relay_exact() && why == WHY_READ { // Only until the flights that may write what is read have // landed: their copy-back's end, not the card's other work. - relay_wait(why, |line| blocked_by(&line.written, runs, false).is_none()); + relay_wait(why, |line| { + blocked_by(&line.written, runs, false).is_none() && !card_steps::copied_read(&card_steps::copies(line), runs) + }); } else { self.settle(&mut submitted, why); } } + // That list runs first, and is one of the submission's from here on. + let with: Vec<&HostList>; + let runs = match &own { + Some(own) => { + with = std::iter::once(own.list()).chain(runs.iter().copied()).collect(); + &with[..] + } + None => runs, + }; let behind = late && flying(); // Before this submission counts as started: what the sweep lets go // of is named by no list, and nothing runs now (behind flights the @@ -4214,7 +4336,7 @@ impl Host { settled += settling.elapsed(); } for list in runs { - list.run_deferred(&mut batch); + list.run_deferred(&mut batch, |i| own.as_ref().is_some_and(|own| own.takes(list, i))); firsts.push(lists.len()); if let Some(prelude) = list.prelude() { lists.push(Some(prelude)); @@ -4268,6 +4390,7 @@ impl Host { number: this_submission, card, back, + own, writes, }; working(was_working); @@ -5152,7 +5275,15 @@ impl HostList { pub(crate) fn defer_fill(&self, dst: *mut u8, src: *const u8, len: usize) { self.deferred() .copies - .push(Step::Fill(dst as usize, src as usize, len)); + .push(Step::Fill(dst as usize, src as usize, len, None)); + } + + /// As [`HostList::defer_fill`], for upload memory that only `slot` (a + /// title heap's address, and the slot) shows. + pub(crate) fn defer_constants(&self, dst: *mut u8, src: *const u8, len: usize, slot: (usize, u32)) { + self.deferred() + .copies + .push(Step::Fill(dst as usize, src as usize, len, Some(slot))); } /// Brings `[address, address + len)` of a buffer's host copy up to date @@ -5272,7 +5403,7 @@ impl HostList { // list's last run (mesh dispatches: the largest group count // asked for, the groups asked in all; with the card's copy, as // the list's end copied them here): taken, then zeroed. - deferred.copies.push(Step::Host(Box::new(move || unsafe { + let notes = Box::new(move || unsafe { let words = at as *mut u32; let (largest, all) = (words.add(1).read_volatile(), words.add(2).read_volatile()); MESH_GROUPS[0].fetch_max(u64::from(largest), Ordering::Relaxed); @@ -5280,7 +5411,8 @@ impl HostList { for k in 0..3 { words.add(k).write_volatile(0); } - }))); + }); + deferred.copies.push(Step::Host(notes, (0, 0))); drop(deferred); if let Some(card) = card { self.zero_card_budget(card); @@ -5456,7 +5588,13 @@ impl HostList { /// As [`HostList::defer`], for work that writes only host memory. pub(crate) fn defer_host(&self, work: impl Fn() + Send + 'static) { - self.deferred().copies.push(Step::Host(Box::new(work))); + self.deferred().copies.push(Step::Host(Box::new(work), (0, 0))); + } + + /// As [`HostList::defer_host`], for work that reads `len` bytes of title + /// memory at `from`. + pub(crate) fn defer_host_read(&self, from: usize, len: usize, work: impl Fn() + Send + 'static) { + self.deferred().copies.push(Step::Host(Box::new(work), (from, from + len))); } /// Brings a texture's staging buffer up to date when the list is @@ -5467,11 +5605,16 @@ impl HostList { /// Runs the list's submission-time steps; refreshes go into `batch` /// (the title's big buffers, a 174 MB one among them, are read by most - /// lists of a submission, through many ranges). - fn run_deferred(&self, batch: &mut Batch) { + /// lists of a submission, through many ranges). Copies and fills that + /// `taken` names (by their place) are left out: they are done on the + /// card ([`card_steps`]). + fn run_deferred(&self, batch: &mut Batch, taken: impl Fn(usize) -> bool) { #[cfg(feature = "diagnostics")] use crate::copystat::{C, add}; - for step in &self.deferred().copies { + for (i, step) in self.deferred().copies.iter().enumerate() { + if matches!(step, Step::Copy(..) | Step::Fill(..)) && taken(i) { + continue; + } let started = std::time::Instant::now(); let copied = match step { &Step::Copy(dst, src, len) => { @@ -5486,7 +5629,7 @@ impl HostList { crate::watch::wrote(dst, len); len } - &Step::Fill(dst, src, len) => { + &Step::Fill(dst, src, len, _) => { #[cfg(feature = "diagnostics")] { add(C::Copies, 1); @@ -5504,7 +5647,7 @@ impl HostList { batch.add(shadow, *address, *len); continue; } - Step::Run(work) | Step::Host(work) => { + Step::Run(work) | Step::Host(work, _) => { if matches!(step, Step::Run(_)) { batch.title_written(); } @@ -6254,7 +6397,6 @@ impl Meter { ); #[cfg(feature = "diagnostics")] { - log_blame(frames); let put = [0, 1, 2, 3].map(|i| crate::shadow::PUT[i].swap(0, Ordering::Relaxed) as f64 / frames); tracing::info!( "what shaders wrote put into title memory per frame (VICTORY_EXACT_BACK): {:.0} pieces in {:.0} writes, {:.1} MB; {:.0} pieces joined over gaps", diff --git a/crates/victory-gpu/src/host/card_steps.rs b/crates/victory-gpu/src/host/card_steps.rs new file mode 100644 index 0000000..6c1f622 --- /dev/null +++ b/crates/victory-gpu/src/host/card_steps.rs @@ -0,0 +1,868 @@ +//! Steps of a submission that read title memory flights in the air may +//! still write, done on the card in the flights' order instead of on the +//! CPU after a wait for them (`VICTORY_CARD_STEPS`, see [`stage`]). + +use std::sync::Arc; + +use super::*; +use crate::shadow::Shadow; + +/// The stage (`VICTORY_CARD_STEPS`; off by default): `1` constant buffers +/// of descriptor tables (`Step::Fill` with a slot of its own), `2` the +/// title's buffer copies (`Step::Copy`) too. Only with the relay's second +/// stage (`relay_exact`). +/// +/// Why: a root view has no size, so a pass that writes through one into the +/// title's 174 MB heap is declared to write all that is left of it, and +/// some pass of every flight does. The title's ring of constants lies in +/// the same heap. One constant buffer of 480 bytes and one copy of 20 KiB a +/// frame, at a new place of the ring each time, waited for nearly +/// everything in the air: 25-30 ms of a 105 ms frame, the submitting thread +/// standing (run record-1; with `VICTORY_FILL_TITLE=1` in run relaycopy-1 +/// the copy alone took over the wait). +/// +/// What is done: a list of ours goes to the card in front of the +/// submission's lists, in the same call. It copies the bytes read from the +/// host copy that the flights write into a buffer of ours, and from there +/// to the destination's host copy (a copy), or the fill's descriptor slot +/// is pointed at that buffer (a fill; the slot is the fill's alone, the +/// root signatures say such descriptors are volatile, and the list that +/// names it has not gone to the card). +/// +/// Why that is the value the CPU would have read after the wait. After the +/// wait title memory holds, for every byte read, what the title wrote, or +/// what the last of the flights that wrote it wrote (the landings put it +/// there, in order). The host copy on the card holds the same when our list +/// runs: every flight that may write one of these bytes writes this same +/// host copy ([`holder`]: a write through another host copy, a read-back +/// or a value written behind a flight there, and the old wait is taken), +/// they are all ahead of our list on the queue, and the title's own bytes +/// are brought up to date by a refresh step of our list, which is prepared +/// like any other list's (also under flights' declared writes: +/// `stale_under_flights`, which settles if it cannot). Nothing of this +/// submission writes these bytes earlier than our list: the steps are done +/// in front of all its lists in either case, and [`plan`] refuses when +/// another step of the submission writes what is read. Every draw that +/// reads a buffer view of the heap relies on the same refresh and the same +/// order; what is new is only that a constant buffer and a copy do too. +/// Where a page is not mapped the host copy may hold other bytes than the +/// CPU's copy would have left: refused. +/// +/// For a copy's destination our list declares a shader write, exactly the +/// bytes copied, after a refresh of them: from there on it is what any +/// shader's write is. The relay compares, and puts what changed into title +/// memory before the title is told anything of this flight (its +/// bottom-of-pipe writes and fences come after, in order); CPU steps of +/// later submissions that read it find it declared and wait, or are done +/// here. Refused: another step of the submission reads or writes the +/// destination on the CPU, or a refresh brings another host copy of it up +/// to date ([`plan`], [`elsewhere`]). While such a copy is in the air a +/// submission that refreshes another host copy of its destination, or +/// reads it in a step that is not checked otherwise, waits for it +/// ([`copied_read`]); with the copy done on the CPU it was there at once. +/// +/// What stage 2 does not see: code of ours that reads title memory away +/// from a submission's steps (the pass over textures picked by index, while +/// a list is recorded). With the CPU's copy such a read of the destination +/// found the bytes at once; now it finds them when the flight has landed. +/// The landing tells the guards (`crate::watch::landing`), so a texture +/// made from older bytes is made again. This is why stage 2 is its own. +pub(super) fn stage() -> u32 { + static STAGE: OnceLock = OnceLock::new(); + *STAGE.get_or_init(|| match std::env::var("VICTORY_CARD_STEPS").ok().as_deref() { + Some("1") => 1, + Some("2") => 2, + _ => 0, + }) +} + +/// The bytes of our buffer between reading and writing: what one submission +/// may do this way. +const BETWEEN: u64 = 1 << 20; + +/// Title memory something in the air writes: the host copy it goes through +/// (`None`: none, the bytes reach title memory when the flight lands), from, +/// to, and whether it is the destination of a copy done here. +pub(super) struct Writer { + shadow: Option>, + from: usize, + to: usize, + copy: bool, +} + +/// What `items` of the line write. +pub(super) fn writers(items: &[Late]) -> Vec { + let mut out = Vec::new(); + for item in items { + match item { + Late::Run(flight) => { + for (shadow, ranges) in &flight.back { + out.extend(ranges.iter().map(|&(from, to)| Writer { + shadow: Some(shadow.clone()), + from, + to, + copy: false, + })); + } + for &i in &flight.order { + for &(_, at, [row, rows, pitch]) in &flight.lists[i].host().deferred().backs { + out.push(Writer { + shadow: None, + from: at, + to: at + rows.saturating_sub(1) * pitch + row, + copy: false, + }); + } + } + out.extend(flight.own.iter().flat_map(Taken::copied)); + } + Late::Write(at, _, bytes) => out.push(Writer { + shadow: None, + from: *at, + to: *at + *bytes, + copy: false, + }), + Late::Signal(..) => {} + } + } + out +} + +/// The destinations of copies done here that have not landed. +pub(super) fn copies(line: &Line) -> Vec { + if stage() < 2 { + return Vec::new(); + } + let flying = line.line.iter().filter_map(|l| match l { + Late::Run(flight) => flight.own.as_ref(), + _ => None, + }); + let landing = line.landing.iter().filter(|w| w.copy).map(|w| Writer { + shadow: w.shadow.clone(), + ..*w + }); + flying.flat_map(Taken::copied).chain(landing).collect() +} + +/// Whether `runs` refresh another host copy of a destination in `copies`, +/// or read it in a step that flights are not waited for otherwise +/// (`Step::Host`): that reads title memory, where the copy is not yet. +pub(super) fn copied_read(copies: &[Writer], runs: &[&HostList]) -> bool { + if copies.is_empty() { + return false; + } + runs.iter().any(|list| { + list.deferred().copies.iter().any(|step| match step { + Step::Refresh(shadow, at, len) => copies + .iter() + .any(|w| w.from < *at + *len && *at < w.to && w.shadow.as_ref().is_none_or(|s| !Arc::ptr_eq(s, shadow))), + Step::Host(_, (from, to)) => copies.iter().any(|w| w.from < *to && *from < w.to), + _ => false, + }) + }) +} + +/// What a step of a submission does to title memory, as the plan sees it. +/// `asked`: flights in the air stop it from being done on the CPU now. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Does { + Copy { dst: usize, src: usize, len: usize, asked: bool }, + Fill { src: usize, len: usize, asked: bool, slot: Option<(usize, u32)> }, + /// Another read on the CPU (a texture, vertices expanded). + Read { from: usize, to: usize, asked: bool }, + /// A refresh of a host copy (named by its address). + Refresh { shadow: usize, from: usize, to: usize }, + /// Anything, anywhere. + Run, +} + +fn meet(a: (usize, usize), b: (usize, usize)) -> bool { + a.0 < b.1 && b.0 < a.1 +} + +/// The steps of `does` (by place) to be done on the card: all that are +/// asked for, or why not. Our list does them all in front of the +/// submission's, every read before every write, so that is right only if no +/// step, asked for or not, writes what one of them reads or writes, and no +/// CPU step reads what one of them writes. +fn plan(does: &[Does], stage: u32) -> Result, &'static str> { + let mut asked = Vec::new(); + for (i, d) in does.iter().enumerate() { + match *d { + Does::Run => return Err("a step of ours runs"), + Does::Read { asked: true, .. } => return Err("a texture reads it"), + Does::Fill { asked: true, slot: None, .. } => return Err("a fill without a descriptor slot of its own"), + Does::Copy { asked: true, .. } if stage < 2 => return Err("a buffer copy (VICTORY_CARD_STEPS=2 does it on the card)"), + Does::Copy { asked: true, .. } | Does::Fill { asked: true, .. } => asked.push(i), + _ => {} + } + } + for &i in &asked { + let (src, dst) = match does[i] { + Does::Copy { dst, src, len, .. } => ((src, src + len), Some((dst, dst + len))), + Does::Fill { src, len, .. } => ((src, src + len), None), + _ => continue, + }; + for (k, d) in does.iter().enumerate() { + let (read, written) = match *d { + Does::Copy { dst, src, len, .. } => (Some((src, src + len)), Some((dst, dst + len))), + Does::Fill { src, len, .. } => (Some((src, src + len)), None), + Does::Read { from, to, .. } => (Some((from, to)), None), + Does::Refresh { .. } | Does::Run => (None, None), + }; + if written.is_some_and(|w| meet(w, src)) { + return Err("a copy of the submission writes what it reads"); + } + let Some(dst) = dst else { + continue; + }; + if k != i && written.is_some_and(|w| meet(w, dst)) { + return Err("another copy of the submission writes what it writes"); + } + if read.is_some_and(|r| meet(r, dst)) { + return Err("a step of the submission reads what it writes"); + } + } + } + Ok(asked) +} + +/// Whether a refresh of `does` brings a host copy other than `shadow` up to +/// date in `[from, to)`. +fn elsewhere(does: &[Does], shadow: usize, from: usize, to: usize) -> bool { + does.iter().any(|d| match *d { + Does::Refresh { shadow: s, from: a, to: b } => s != shadow && meet((a, b), (from, to)), + _ => false, + }) +} + +/// The host copy that holds `[from, to)` on the card once what `writers` +/// write is there: the one they all write it through, or without any the +/// one shaders would be given. +fn holder(writers: &[Writer], from: usize, to: usize) -> Result, &'static str> { + let mut found: Option<&Arc> = None; + for w in writers.iter().filter(|w| meet((w.from, w.to), (from, to))) { + match (&w.shadow, found) { + (None, _) => return Err("a read-back or a value behind a flight is written there"), + (Some(s), Some(f)) if !Arc::ptr_eq(s, f) => return Err("flights write it through two host copies"), + (Some(s), _) => found = Some(s), + } + } + let shadow = match found { + Some(s) => s.clone(), + None => crate::shadow::find(from as u64, (to - from) as u64).ok_or("no host copy of it")?, + }; + let (start, len) = shadow.span(); + if shadow.in_place() || (from as u64) < start || to as u64 > start + len { + return Err("no host copy holds all of it"); + } + Ok(shadow) +} + +/// Whether every page of `[from, to)` can be read (through the kernel: +/// asking Windows walks the page tables, milliseconds in the title's heap). +fn mapped(from: usize, to: usize) -> bool { + let mut bytes = vec![0u8; to - from]; + read_into(from as *const u8, &mut bytes) +} + +/// A step done on the card. +struct Op { + from: (Arc, usize), + len: usize, + /// Its place in the buffer between. + at: u64, + to: To, +} + +enum To { + /// A host copy, and the title address. + Copy(Arc, usize), + /// A descriptor slot (title heap, slot). + Slot(usize, u32), +} + +/// Records what [`stage`] describes into `list` (open): `reads` are copied +/// into `between` (source, offset in it, offset in `between`, bytes), then +/// `writes` out of it (destination, offset in it, offset in `between`, +/// bytes). `between` is left readable as constants, the others in the +/// common state, as the submission's lists expect them. +fn record( + list: &ID3D12GraphicsCommandList, + between: &ID3D12Resource, + reads: &[(&ID3D12Resource, u64, u64, u64)], + writes: &[(&ID3D12Resource, u64, u64, u64)], +) { + let common = D3D12_RESOURCE_STATE_COMMON; + let (source, dest) = (D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_COPY_DEST); + let mut sources: Vec<&ID3D12Resource> = Vec::new(); + let mut dests: Vec<&ID3D12Resource> = Vec::new(); + for (all, list) in [(reads, &mut sources), (writes, &mut dests)] { + for &(buffer, ..) in all { + if !list.iter().any(|b| b.as_raw() == buffer.as_raw()) { + list.push(buffer); + } + } + } + let both = |b: &ID3D12Resource| dests.iter().any(|d| d.as_raw() == b.as_raw()); + let mut barriers = vec![transition(between, dest, source | D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER)]; + barriers.extend(sources.iter().map(|b| transition(b, source, if both(b) { dest } else { common }))); + let done: Vec = dests.iter().map(|b| transition(b, dest, common)).collect(); + // SAFETY: an open list; live buffers, and the callers' ranges lie in + // them. The buffers start in the common state (the list is the first of + // its call) and are promoted by their first use. + unsafe { + // What earlier lists' shaders wrote, first. + list.ResourceBarrier(&[D3D12_RESOURCE_BARRIER { + Type: D3D12_RESOURCE_BARRIER_TYPE_UAV, + Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: D3D12_RESOURCE_BARRIER_0 { + UAV: std::mem::ManuallyDrop::new(D3D12_RESOURCE_UAV_BARRIER { + pResource: std::mem::ManuallyDrop::new(None), + }), + }, + }]); + for &(buffer, offset, at, len) in reads { + list.CopyBufferRegion(between, at, buffer, offset, len); + } + list.ResourceBarrier(&barriers); + for &(buffer, offset, at, len) in writes { + list.CopyBufferRegion(buffer, offset, between, at, len); + } + if !done.is_empty() { + list.ResourceBarrier(&done); + } + } +} + +/// The list in front of a submission's, and its buffer ([`BETWEEN`] bytes). +struct Own { + list: HostList, + between: ID3D12Resource, +} + +// SAFETY: D3D12 objects are free-threaded, and the list is touched by one +// thread at a time (the submitting one until it is sent, then the one that +// lands it, then nobody until it is taken out of `POOL`). +unsafe impl Send for Own {} + +/// Those that have landed: closed, their steps dropped. +static POOL: Mutex> = Mutex::new(Vec::new()); + +/// Since the last frame-rate line: fills and copies done on the card, their +/// bytes, and submissions that waited instead, refused by a check. +static DONE: [std::sync::atomic::AtomicU64; 4] = [const { std::sync::atomic::AtomicU64::new(0) }; 4]; + +/// What [`Host::card_steps`] took off a submission: the list that does it, +/// for the submission to run first, and the steps the CPU leaves out (their +/// list by address, their place in it). +pub(super) struct Taken { + own: Own, + steps: std::collections::HashSet<(usize, usize)>, + copied: Vec<(Arc, usize, usize)>, +} + +impl Taken { + pub(super) fn list(&self) -> &HostList { + &self.own.list + } + + /// Whether step `i` of `list` is done by this. + pub(super) fn takes(&self, list: &HostList, i: usize) -> bool { + self.steps.contains(&(std::ptr::from_ref(list) as usize, i)) + } + + fn copied(&self) -> impl Iterator { + self.copied.iter().map(|(shadow, from, to)| Writer { + shadow: Some(shadow.clone()), + from: *from, + to: *to, + copy: true, + }) + } + + /// The submission has run and landed: the list and its buffer are free. + pub(super) fn landed(self) { + // The host copies its steps held go; its commands stay until the + // next use resets it. + drop(std::mem::take(&mut *self.own.list.deferred())); + POOL.lock().unwrap_or_else(|e| e.into_inner()).push(self.own); + } +} + +impl Host { + /// With flights in the air: if what keeps `runs` from being prepared + /// now is reads of title memory that can all be done on the card + /// ([`stage`]), the list that does them. `None` if nothing is in the + /// way or the old wait is due. Under the submission lock. + pub(super) fn card_steps(&self, runs: &[&HostList]) -> Option { + if stage() == 0 || !relay_exact() { + return None; + } + let (written, copies) = { + let line = line(); + if line.written.is_empty() { + return None; + } + (line.written.clone(), copies(&line)) + }; + blocked_by(&written, runs, false)?; + let taken = if copied_read(&copies, runs) { + Err("a copy done on the card is read through another host copy") + } else { + self.take_steps(&written, runs) + }; + match taken { + Ok(taken) => taken, + Err(why) => { + DONE[3].fetch_add(1, Ordering::Relaxed); + #[cfg(feature = "diagnostics")] + refused(why); + let _ = why; + None + } + } + } + + fn take_steps(&self, written: &[(usize, usize)], runs: &[&HostList]) -> Result, &'static str> { + let mut does = Vec::new(); + // Which step each is. One list's lock at a time: a submission may + // name a list twice. + let mut steps = Vec::new(); + for list in runs { + for (i, step) in list.deferred().copies.iter().enumerate() { + does.push(match step { + &Step::Copy(dst, src, len) => Does::Copy { + dst, + src, + len, + asked: copy_blocked(written, dst, src, len).is_some(), + }, + &Step::Fill(_, src, len, slot) => Does::Fill { + src, + len, + asked: read_blocked(written, src, len), + slot, + }, + Step::Texture(staging) => { + let (at, len) = staging.memory(); + Does::Read { + from: at, + to: at + len, + asked: touches(written, at, at + len), + } + } + &Step::Host(_, (from, to)) if from < to => Does::Read { from, to, asked: false }, + Step::Refresh(shadow, at, len) => Does::Refresh { + shadow: Arc::as_ptr(shadow) as usize, + from: *at, + to: *at + *len, + }, + Step::Run(_) => Does::Run, + Step::GpuWrite(..) | Step::Host(..) => continue, + }); + steps.push((std::ptr::from_ref::(list) as usize, i)); + } + } + let chosen = plan(&does, stage())?; + if chosen.is_empty() { + return Ok(None); + } + let writers = { + let line = line(); + let mut all = writers(&line.line); + all.extend(line.landing.iter().map(|w| Writer { + shadow: w.shadow.clone(), + ..*w + })); + all + }; + let mut ops: Vec = Vec::with_capacity(chosen.len()); + let mut at = 0u64; + for &i in &chosen { + let (src, len) = match does[i] { + Does::Copy { src, len, .. } | Does::Fill { src, len, .. } => (src, len), + _ => continue, + }; + if len == 0 { + return Err("nothing to read"); + } + if at + len as u64 > BETWEEN { + return Err("too much at once"); + } + let from = holder(&writers, src, src + len)?; + if !mapped(src, src + len) { + return Err("not all of what it reads is mapped"); + } + let to = match does[i] { + Does::Copy { dst, .. } => { + let to = holder(&writers, dst, dst + len)?; + if elsewhere(&does, Arc::as_ptr(&to) as usize, dst, dst + len) { + return Err("another host copy of what it writes is read"); + } + if !mapped(dst, dst + len) { + return Err("not all of what it writes is mapped"); + } + To::Copy(to, dst) + } + Does::Fill { slot: Some((heap, index)), .. } => To::Slot(heap, index), + _ => continue, + }; + ops.push(Op { + from: (from, src), + len, + at, + to, + }); + at = (at + len as u64).next_multiple_of(256); + } + let Some(own) = self.own_list() else { + return Err("no host list or buffer for it"); + }; + let mut reads = Vec::new(); + let mut writes = Vec::new(); + let mut copied = Vec::new(); + // First every refresh, then the write: the bytes compared with are + // the title's. + for op in &ops { + op.from.0.refresh(&own.list, op.from.1 as u64, op.len as u64); + reads.push((&op.from.0.buffer, op.from.0.offset(op.from.1 as u64), op.at, op.len as u64)); + if let To::Copy(to, dst) = &op.to { + to.refresh(&own.list, *dst as u64, op.len as u64); + writes.push((&to.buffer, to.offset(*dst as u64), op.at, op.len as u64)); + } + } + for op in &ops { + if let To::Copy(to, dst) = &op.to { + to.gpu_writes(&own.list, *dst as u64, op.len as u64); + copied.push((to.clone(), *dst, *dst + op.len)); + } + } + #[cfg(feature = "diagnostics")] + own.list.note_op(|| format!("{} copies of title memory done on the card, {at} bytes (VICTORY_CARD_STEPS)", ops.len())); + record(&own.list.list, &own.between, &reads, &writes); + own.list.close(); + // SAFETY: a live buffer. + let gpu = unsafe { own.between.GetGPUVirtualAddress() }; + for op in &ops { + DONE[2].fetch_add(op.len as u64, Ordering::Relaxed); + match &op.to { + To::Slot(heap, index) => { + DONE[0].fetch_add(1, Ordering::Relaxed); + // (A heap that is gone: nothing can read the slot.) + crate::descriptor::constant_slot(*heap, *index, gpu + op.at, op.len as u32); + } + To::Copy(..) => { + DONE[1].fetch_add(1, Ordering::Relaxed); + } + } + #[cfg(feature = "diagnostics")] + noted(op); + } + Ok(Some(Taken { + own, + steps: chosen.iter().map(|&i| steps[i]).collect(), + copied, + })) + } + + /// A list of ours open for recording, and its buffer. + fn own_list(&self) -> Option { + let pooled = POOL.lock().unwrap_or_else(|e| e.into_inner()).pop(); + if let Some(own) = pooled { + own.list.reset(); + return Some(own); + } + let between = self.uav_buffer(BETWEEN)?; + name(&between, || "copies of title memory on the card".into()); + let list = self.new_list().ok()?; + Some(Own { list, between }) + } +} + +/// Diagnostics: the copies and fills done on the card since the last +/// frame-rate line, by bytes, host copy read and host copy written (start +/// and length; zeros for a fill): how many, and the last title addresses. +#[cfg(feature = "diagnostics")] +type Noted = std::collections::HashMap<(usize, (u64, u64), (u64, u64)), (u64, usize, usize)>; +#[cfg(feature = "diagnostics")] +static NOTED: Mutex> = Mutex::new(None); + +#[cfg(feature = "diagnostics")] +fn noted(op: &Op) { + let (to, dst) = match &op.to { + To::Copy(to, dst) => (to.span(), *dst), + To::Slot(..) => ((0, 0), 0), + }; + let mut noted = NOTED.lock().unwrap_or_else(|e| e.into_inner()); + let e = noted.get_or_insert_with(Default::default).entry((op.len, op.from.0.span(), to)).or_default(); + *e = (e.0 + 1, op.from.1, dst); +} + +/// For the frame-rate line. +pub(super) fn log(frames: f64) { + if stage() == 0 { + return; + } + let d: Vec = DONE.iter().map(|d| d.swap(0, Ordering::Relaxed) as f64 / frames).collect(); + tracing::info!( + "steps done on the card instead of waiting for flights, per frame (VICTORY_CARD_STEPS={}): {:.2} fills, {:.2} buffer \ + copies, {:.1} KB; {:.2} submissions refused and waited", + stage(), + d[0], + d[1], + d[2] / 1024.0, + d[3], + ); + #[cfg(feature = "diagnostics")] + { + let noted = NOTED.lock().unwrap_or_else(|e| e.into_inner()).take().unwrap_or_default(); + let mut noted: Vec<_> = noted.into_iter().collect(); + noted.sort_unstable_by_key(|(_, e)| std::cmp::Reverse(e.0)); + for ((len, from, to), (n, src, dst)) in noted.into_iter().take(5) { + let n = n as f64 / frames; + if to.1 == 0 { + tracing::info!("done on the card, per frame: {n:.2} fills of {len:#x} bytes from the host copy of {:#x}+{:#x} (last {src:#x})", from.0, from.1); + } else { + tracing::info!( + "done on the card, per frame: {n:.2} copies of {len:#x} bytes from the host copy of {:#x}+{:#x} (last {src:#x}) \ + to that of {:#x}+{:#x} (last {dst:#x})", + from.0, + from.1, + to.0, + to.1 + ); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn copy(dst: usize, src: usize, len: usize, asked: bool) -> Does { + Does::Copy { dst, src, len, asked } + } + + fn fill(src: usize, len: usize, asked: bool) -> Does { + Does::Fill { src, len, asked, slot: Some((1, 2)) } + } + + /// Only what is asked for is taken, all of it, and a step that stands + /// in none of their ways changes nothing. + #[test] + fn steps_apart_from_one_another_are_taken() { + let does = [ + copy(1000, 5000, 100, false), + fill(2000, 64, true), + Does::Refresh { shadow: 1, from: 0, to: 9000 }, + copy(3000, 6000, 100, true), + fill(1000, 64, false), + Does::Read { from: 3100, to: 4000, asked: false }, + ]; + assert_eq!(plan(&does, 2), Ok(vec![1, 3])); + // Two fills of the same bytes. + assert_eq!(plan(&[fill(0, 64, true), fill(32, 64, true)], 1), Ok(vec![0, 1])); + assert_eq!(plan(&[fill(0, 64, false)], 1), Ok(vec![])); + } + + /// Everything that could make the card's order another than the CPU's + /// refuses the lot, wherever in the submission it stands. + #[test] + fn steps_in_one_anothers_way_are_refused() { + let taken = copy(1000, 5000, 100, true); + let refused = |other: Does| { + assert!(plan(&[taken, other], 2).is_err(), "{other:?} after"); + assert!(plan(&[other, taken], 2).is_err(), "{other:?} before"); + }; + for asked in [false, true] { + // Writes what it reads. + refused(copy(5099, 9000, 1, asked)); + // Writes what it writes. + refused(copy(999, 9000, 2, asked)); + // Reads what it writes. + refused(copy(7000, 1050, 10, asked)); + refused(fill(1099, 64, asked)); + refused(Does::Read { from: 900, to: 1001, asked }); + } + refused(Does::Run); + // A texture that itself waits for flights; a fill with no slot. + refused(Does::Read { from: 1, to: 2, asked: true }); + refused(Does::Fill { src: 1, len: 1, asked: true, slot: None }); + // Into itself, and the same list twice. + assert!(plan(&[copy(1000, 1050, 100, true)], 2).is_err()); + assert!(plan(&[taken, taken], 2).is_err()); + // A fill of what a copy writes, and copies without stage 2. + assert!(plan(&[fill(1000, 4, true), copy(1000, 5000, 100, false)], 2).is_err()); + assert!(plan(&[taken], 1).is_err()); + // Ends that touch are apart. + assert!(plan(&[taken, copy(900, 9000, 100, false), copy(1100, 9000, 5, false), copy(4900, 9000, 100, false)], 2).is_ok()); + } + + #[test] + fn a_refresh_of_another_host_copy_is_seen() { + let does = [Does::Refresh { shadow: 7, from: 100, to: 200 }]; + assert!(!elsewhere(&does, 7, 100, 200)); + assert!(elsewhere(&does, 8, 199, 300)); + assert!(!elsewhere(&does, 8, 200, 300)); + } + + /// A value written behind a flight, or a read-back, is in no host copy + /// before it lands. + #[test] + fn what_no_host_copy_holds_is_refused() { + let writers = [Writer { shadow: None, from: 100, to: 108, copy: false }]; + assert!(holder(&writers, 104, 120).is_err()); + } + + /// Reads are all before writes, between two buffers and within one, and + /// the buffers are usable in the lists that follow in the same call. + #[test] + fn the_card_copies_what_is_recorded() { + let Some(host) = host() else { + return; // no host GPU + }; + let (a, a_data) = host.shared_buffer(1 << 16).unwrap(); + let (b, b_data) = host.shared_buffer(1 << 16).unwrap(); + // SAFETY: mappings of 64 KiB. + let (a_bytes, b_bytes) = + unsafe { (std::slice::from_raw_parts_mut(a_data, 1 << 16), std::slice::from_raw_parts_mut(b_data, 1 << 16)) }; + for (i, (x, y)) in a_bytes.iter_mut().zip(b_bytes.iter_mut()).enumerate() { + (*x, *y) = (i as u8 ^ (i >> 8) as u8, 0xee); + } + let was = a_bytes.to_vec(); + let own = host.own_list().unwrap(); + // Within `a` (there and back), from `a` to `b`, and a read alone. + record( + &own.list.list, + &own.between, + &[(&a, 33, 0, 100), (&a, 2000, 256, 100), (&a, 5001, 512, 7), (&a, 9, 1024, 4)], + &[(&a, 2000, 0, 100), (&a, 33, 256, 100), (&b, 77, 512, 7)], + ); + own.list.close(); + // A list behind it that uses the buffers in other ways. + let next = host.new_list().unwrap(); + let (out, out_data) = host.shared_buffer(1 << 16).unwrap(); + // SAFETY: an open list; live buffers. + unsafe { + next.list.CopyBufferRegion(&out, 0, &own.between, 1024, 4); + next.list.CopyBufferRegion(&b, 4096, &a, 33, 100); + } + next.close(); + host.execute(&[&own.list, &next]); + // With the debug layer (`VICTORY_D3D12_DEBUG=1`, diagnostics). + assert_eq!(host.log_messages(), None); + assert_eq!(a_bytes[2000..2100], was[33..133]); + assert_eq!(a_bytes[33..133], was[2000..2100]); + assert_eq!(b_bytes[77..84], was[5001..5008]); + assert_eq!(b_bytes[4096..4196], was[2000..2100]); + // SAFETY: a mapping of 64 KiB. + assert_eq!(unsafe { std::slice::from_raw_parts(out_data, 4) }, &was[9..13]); + assert!(b_bytes[..77].iter().all(|&x| x == 0xee)); + a_bytes[2000..2100].copy_from_slice(&was[2000..2100]); + a_bytes[33..133].copy_from_slice(&was[33..133]); + assert_eq!(a_bytes, &was[..]); + // As the landing does: its next use records anew. + POOL.lock().unwrap().push(own); + let own = host.own_list().unwrap(); + own.list.close(); + } + + /// The whole way, in a process of its own with the relay on: + /// `VICTORY_RELAY=2 VICTORY_CARD_STEPS=2 cargo test -p victory-gpu --lib + /// card_steps -- --ignored --test-threads=1`. A flight's list writes + /// part of what the next submission's copy reads, the title wrote the + /// rest; the copy is done on the card behind the flight, and a second + /// one reads what the first wrote. Title memory ends as on the CPU. + #[test] + #[ignore = "wants VICTORY_RELAY=2 VICTORY_CARD_STEPS=2 and a process of its own"] + fn a_copy_behind_a_flight_reads_what_the_flight_wrote() { + use windows_sys::Win32::System::Memory::{MEM_COMMIT, MEM_RESERVE, PAGE_READWRITE, VirtualAlloc}; + const LEN: usize = 4 << 20; + const WRITE_WATCH: u32 = 0x20_0000; + let Some(host) = host() else { + return; + }; + assert!(relay_exact() && stage() == 2, "set VICTORY_RELAY=2 VICTORY_CARD_STEPS=2"); + // Two title buffers: in the first a copy within it, from it into the + // second, and back. + let (title, start) = { + // SAFETY: memory of our own, never freed. + let base = unsafe { VirtualAlloc(std::ptr::null(), LEN, MEM_RESERVE | MEM_COMMIT | WRITE_WATCH, PAGE_READWRITE) }.cast::(); + assert!(!base.is_null()); + // SAFETY: `LEN` bytes just committed. + (unsafe { std::slice::from_raw_parts_mut(base, LEN) }, base as usize) + }; + const HALF: usize = LEN / 2; + crate::shadow::register(0x1111, start as u64, HALF as u64); + crate::shadow::register(0x2222, (start + HALF) as u64, HALF as u64); + let first = crate::shadow::find(start as u64, 64).unwrap(); + let (slow_from, slow_to) = (host.uav_buffer(64 << 20).unwrap(), host.uav_buffer(64 << 20).unwrap()); + let before = DONE[1].load(Ordering::Relaxed); + let mut lists = Vec::new(); + for round in 0..8usize { + // The title writes; then a "shader" of a flight writes 300 bytes + // of what the copy reads, behind some work. + let seed = |i: usize| (i * 7 + round * 13 + i / 251) as u8; + for (i, b) in title[4000..6000].iter_mut().enumerate() { + *b = seed(i); + } + let (upload, data) = host.upload_buffer(4096).unwrap(); + for i in 0..300 { + // SAFETY: inside the mapping. + unsafe { data.add(i).write(0x80 | (round * 31 + i) as u8) }; + } + let a = host.new_list().unwrap(); + first.refresh(&a, start as u64, HALF as u64); + first.gpu_writes(&a, start as u64, HALF as u64); + // SAFETY: an open list; live buffers. + unsafe { + for _ in 0..24 { + a.list.CopyBufferRegion(&slow_to, 0, &slow_from, 0, 64 << 20); + } + a.list.CopyBufferRegion(&first.buffer, 4500, &upload, 0, 300); + a.list + .ResourceBarrier(&[transition(&first.buffer, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COMMON)]); + } + a.close(); + host.execute_own(&a); + // With the flight in the air, beside what it writes. + for (i, b) in title[5300..5400].iter_mut().enumerate() { + *b = seed(1300 + i) ^ 0x5c; + } + // Into the second buffer, and within the first. + let b = host.new_list().unwrap(); + b.defer_copy((start + HALF + 100) as *mut u8, (start + 4001) as *const u8, 1500); + b.defer_copy((start + 900_001) as *mut u8, (start + 4400) as *const u8, 777); + b.close(); + host.execute_own(&b); + // What the first wrote, read by the next submission. + let c = host.new_list().unwrap(); + c.defer_copy((start + 1_200_000) as *mut u8, (start + HALF + 100) as *const u8, 1500); + c.close(); + let number = host.execute_own(&c); + settle_now(WHY_SYNC); + assert!(FINISHED.load(Ordering::Acquire) >= number); + let wanted: Vec = (0..2000) + .map(|i| match i { + 500..800 => 0x80 | (round * 31 + i - 500) as u8, + 1300..1400 => seed(i) ^ 0x5c, + _ => seed(i), + }) + .collect(); + assert!(title[4000..6000] == wanted[..], "what the flight wrote, round {round}"); + assert!(title[HALF + 100..HALF + 1600] == wanted[1..1501], "the copy to the other buffer, round {round}"); + assert!(title[900_001..900_778] == wanted[400..1177], "the copy within the buffer, round {round}"); + assert!(title[1_200_000..1_201_500] == wanted[1..1501], "the copy of the copy, round {round}"); + assert!(title[HALF..HALF + 100].iter().all(|&x| x == 0)); + assert!(title[HALF + 1600..].iter().all(|&x| x == 0)); + a.keep(&upload); + lists.extend([a, b, c]); + } + assert_eq!(host.log_messages(), None); + let done = DONE[1].load(Ordering::Relaxed) - before; + eprintln!("{done} of 24 copies were done on the card, {} submissions waited", DONE[3].load(Ordering::Relaxed)); + assert!(done >= 12, "only {done} copies were done on the card"); + } +} diff --git a/crates/victory-gpu/src/memory.rs b/crates/victory-gpu/src/memory.rs index c256367..67ebe7a 100644 --- a/crates/victory-gpu/src/memory.rs +++ b/crates/victory-gpu/src/memory.rs @@ -304,6 +304,8 @@ pub(crate) fn ready( shadow.refresh(list, start, len); } if writes && (first_write || n >= numbered) { + #[cfg(feature = "diagnostics")] + crate::host::wide_write(start, len, 1); shadow.gpu_writes(list, start, len); } } diff --git a/crates/victory-gpu/src/shadow.rs b/crates/victory-gpu/src/shadow.rs index 68817e0..2d2c6c5 100644 --- a/crates/victory-gpu/src/shadow.rs +++ b/crates/victory-gpu/src/shadow.rs @@ -1831,6 +1831,11 @@ impl Shadow { self.id } + /// Whether it is no copy but title memory in place (`crate::inplace`). + pub(crate) fn in_place(&self) -> bool { + self.place.is_some() + } + /// Offset of a title address from the buffer's start. pub(crate) fn offset(&self, address: u64) -> u64 { address - self.start + self.place.as_ref().map_or(0, |place| place.1)