diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 9f04e85..fc01f1b 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -3233,6 +3233,38 @@ pub(crate) fn late_met_known(wait: &crate::list::ValueWait) -> Option { None } +/// The number of the flight in the air whose bottom-of-pipe write meets +/// `wait` ([`late_met_known`]), for `two_queues`: the part behind the wait +/// goes to the card behind that flight, on the one queue; on a queue of its +/// own it would have to wait for it. `None`: memory meets it now, the +/// flight is being landed (the card has run it), or no flight's write does. +pub(crate) fn late_met_by(wait: &crate::list::ValueWait) -> Option { + if !late() || wait.met() { + return None; + } + let (address, bytes) = wait.place(); + let line = line(); + let mut flight = None; + for item in &line.line { + match item { + Late::Run(f) => flight = Some(f.number), + Late::Write(at, crate::queue::Written::Value(v), n) if *at == address && *n >= bytes && wait.holds_for(*v) => { + return flight; + } + _ => {} + } + } + None +} + +/// Tiles were mapped on `queue`: counted for `two_queues` if it is the +/// host's direct queue. +pub(crate) fn tiles_mapped_on(queue: &ID3D12CommandQueue) { + if two_queues::on() && host().is_some_and(|h| h.queue.as_raw() == queue.as_raw()) { + two_queues::tiles_mapped(); + } +} + /// Settles the flights in the air, if any ([`Host::settle`]), for reason /// `why` (one of the `WHY_` places). Not from inside a submission. pub(crate) fn settle_now(why: usize) { @@ -3703,6 +3735,7 @@ pub(crate) mod spans; mod torn; #[cfg(feature = "diagnostics")] mod traffic; +pub(crate) mod two_queues; pub(crate) mod upcheck; #[cfg(feature = "diagnostics")] mod stamps; @@ -5536,6 +5569,9 @@ impl Host { None => runs, }; let parts: Vec = parts.iter().map(|&n| n + usize::from(own.is_some())).collect(); + // The plan for two queues: the textures' states before the preludes + // move them. + let probe = two_queues::on().then(|| two_queues::probe(runs)); stage.next("submit: the sweep"); let behind = late && flying(); // Before this submission counts as started: what the sweep lets go @@ -5682,6 +5718,10 @@ impl Host { } let declared = declared_writes(runs); let card = self.card_before(&declared, &picked, flown); + if let Some(probe) = probe { + let finished = FINISHED.load(Ordering::Acquire); + two_queues::place(probe, this_submission, card.is_some(), own.is_some(), runs.len(), finished); + } // The sends are taken: from here on the references hold what the // copies in the lists put. for (shadow, at, bytes) in batch.placed.drain(..) { @@ -5732,7 +5772,7 @@ impl Host { }; awake::sent(&self.device); #[cfg(feature = "diagnostics")] - crate::timeline::sent(sending, *submitted, stamp); + crate::timeline::sent(sending, *submitted, stamp, two_queues::title_kind() == Some(two_queues::COMPUTE)); self.card_sent(); stage.next("submit: noting what is in the air"); if let Err(e) = signalled { @@ -5787,7 +5827,7 @@ impl Host { .Signal(&self.fence, *submitted) .and_then(|()| self.fence.SetEventOnCompletion(*submitted, self.event)); #[cfg(feature = "diagnostics")] - crate::timeline::sent(sending, *submitted, stamp); + crate::timeline::sent(sending, *submitted, stamp, two_queues::title_kind() == Some(two_queues::COMPUTE)); match waited { Ok(()) => { working(false); @@ -8759,6 +8799,7 @@ impl Meter { late_line(frames); crate::queue::behind_line(frames); card_clock_line(frames, ms); + two_queues::line(frames); #[cfg(feature = "diagnostics")] spans::line(frames); crate::shadow::back_line(frames); diff --git a/crates/victory-gpu/src/host/two_queues.rs b/crates/victory-gpu/src/host/two_queues.rs new file mode 100644 index 0000000..1a3f78b --- /dev/null +++ b/crates/victory-gpu/src/host/two_queues.rs @@ -0,0 +1,631 @@ +//! Where each host submission would go if the title's compute queues had a +//! host queue of their own, and what it would have to wait for there +//! (`VICTORY_ASYNC_QUEUE=plan`). Only a plan: everything still runs on the one +//! direct queue, as before; the frame line says how the plan came out. +//! +//! Why a plan and not the queue: a list runs only on a queue of its own type +//! (D3D12 refuses a compute list on a direct queue and a direct list on a +//! compute queue), and the type is chosen when the list is made, before +//! anything is recorded into it. Every host list is a direct list now, and +//! what the layer records into the title's compute lists is not all +//! something a compute list may hold: a texture a shader reads is put in both +//! read states (0x40 | 0x80, `crate::draw`), and the pixel one is a drawing +//! state; a depth texture with a colour twin is copied through a drawn pass. +//! A submission cannot be sent to the direct queue instead once its lists +//! are compute lists, so whatever a compute list cannot hold has to be known, +//! or cut out into a direct part, while it is recorded. This module is the +//! half that can be had without that: the order between two queues, and the +//! counts of what would stand in the way, so that a run shows whether the +//! rest is worth doing. +//! +//! What the plan keeps between the queues ([`Plan::place`]), each as a wait of +//! one queue for a value the other signals behind a submission (a wait for a +//! value the queue waits behind already is left out, and so is one for a +//! submission that has finished): +//! +//! - copies in the card's memory: the sends of title bytes and the compares +//! behind the lists are numbered in the order they are sent (`relay`'s +//! merge leaves out bytes sent after a compare's number, and each compare +//! flags what changed since the one before it); so a submission with them +//! waits for the last one of the other queue that had them; +//! - steps done on the card behind the flights (`card_steps`): they read what +//! every submission sent before wrote, so they wait for the other queue's +//! last submission; +//! - textures: their states are moved for a whole texture, in the order of +//! the submissions, and only one queue may write a resource at a time; a +//! submission that names a texture the other queue's last one named waits +//! for that one; +//! - a wait for a memory value passed because a flight in the air will write +//! it (`late_met_known`): behind that flight, if it is on the other queue; +//! - tile mappings made while a submission is prepared (they go to its +//! queue): behind the other queue's last submission; +//! - a texture the title's compute queue names that is in a drawing state +//! now: its state is moved by a list on the direct queue first, and the +//! compute queue waits for that; +//! - submissions of ours (the present's copy): behind everything. +//! +//! What it does not keep: the title's own waits for fences and for memory +//! values that are in memory already need nothing, because what they wait +//! for has landed before the waiting part is sent at all (`crate::queue`'s +//! line). Two queues writing different bytes of one buffer (the title's heap +//! is one host copy) at the same time is outside D3D12's rules ("only one +//! queue can write to a resource at a time"), as it is on the console's +//! side; the plan does not order that. + +use std::collections::{HashMap, VecDeque}; +use std::sync::atomic::{AtomicU64, Ordering::Relaxed}; +use std::sync::{Mutex, OnceLock}; + +/// The list type of a title queue whose work would go to the compute queue. +pub(crate) const COMPUTE: u32 = 2; + +/// A host queue. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(crate) enum Engine { + Direct = 0, + Compute = 1, +} + +/// What a submission of the title's compute queue holds that a compute list +/// cannot hold, as the layer records it now (bits of [`Ask::holds`]). +/// A texture read in both read states (0x40 | 0x80): on a compute list it +/// would be the first alone. Counted; the plan takes it as done. +pub(crate) const READ: u8 = 1; +/// A list begins or ends with a texture in a drawing state (render target, +/// depth, resolve, index, the pixel shader's read, ...) other than that read. +/// The submission stays on the direct queue. +pub(crate) const STATE: u8 = 2; +/// A depth texture with a colour twin: copied through a drawn pass. The +/// submission stays on the direct queue. +pub(crate) const TWINS: u8 = 4; +/// A texture it names is in a drawing state now: the move out of it is +/// made on the direct queue first ([`Why::Handoff`]). +pub(crate) const NOW: u8 = 8; + +/// The resource state bits a compute list may name: vertex and constant +/// buffer, unordered access, non-pixel shader resource, indirect argument, +/// copy destination and source, acceleration structure. +pub(crate) const COMPUTE_STATES: u32 = 0x1 | 0x8 | 0x40 | 0x200 | 0x400 | 0x800 | 0x40_0000; +/// The pixel shader's read state. +const PIXEL_READ: u32 = 0x80; + +/// What [`READ`] and [`STATE`] say of a texture a list begins with `first` +/// and ends with `last`. +pub(crate) fn holds_of(first: u32, last: u32) -> u8 { + let drawing = (first | last) & !COMPUTE_STATES; + if drawing == 0 { + 0 + } else if drawing == PIXEL_READ { + READ + } else { + STATE + } +} + +/// Why a submission waits for the other queue. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(crate) enum Why { + Card = 0, + Steps, + Texture, + Value, + Tiles, + Handoff, + Ours, +} +const WHYS: usize = 7; + +/// What is known of a submission when it is sent. +pub(crate) struct Ask<'a> { + /// Its number among the submissions started. + pub(crate) number: u64, + /// The list type of the title queue it was given to (`None`: ours). + pub(crate) title: Option, + /// [`READ`], [`STATE`], [`TWINS`], [`NOW`]. + pub(crate) holds: u8, + /// It has sends to copies in the card's memory or a compare behind it. + pub(crate) card: bool, + /// Steps of it are done on the card behind the flights. + pub(crate) steps: bool, + /// Tiles were mapped while it was prepared. + pub(crate) tiles: bool, + /// The host textures its lists name. + pub(crate) textures: &'a [usize], + /// The number of the submission whose bottom-of-pipe write met a wait + /// it passed (0: none). + pub(crate) after: u64, +} + +/// Where a submission would go: the queue, the value signalled behind it +/// there, and the wait put before it, if one is needed (the other queue's +/// value, and why). +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(crate) struct Placed { + pub(crate) engine: Engine, + pub(crate) value: u64, + pub(crate) wait: Option<(u64, Why)>, +} + +/// A submission as the plan remembers it: number, queue, value. +type Sent = (u64, Engine, u64); + +/// How many submissions are remembered for the waits for memory values +/// (a frame has about thirty). +const REMEMBERED: usize = 4096; + +/// The two queues' order ([`Plan::place`]). +#[derive(Default)] +pub(crate) struct Plan { + /// The value signalled behind the last submission on each queue. + sent: [u64; 2], + /// The other queue's value each queue waits behind already. + waited: [u64; 2], + /// Each queue's last submission, and its last with copies in the + /// card's memory. + last: [Option; 2], + card: [Option; 2], + /// The last submission that named each texture. + textures: HashMap, + /// The submissions lately, in order. + recent: VecDeque, +} + +impl Plan { + /// Places the submission `ask` sent now; `finished`: the submissions up to + /// this number have finished (nothing waits for them). + pub(crate) fn place(&mut self, ask: &Ask, finished: u64) -> Placed { + let engine = match ask.title { + Some(COMPUTE) if ask.holds & (STATE | TWINS) == 0 => Engine::Compute, + _ => Engine::Direct, + }; + let (q, o) = (engine as usize, 1 - engine as usize); + // What it has to come behind, on the other queue: the highest value. + let mut need: Option<(u64, Why)> = None; + let mut behind = |dep: Option, why: Why| { + if let Some((number, on, value)) = dep + && on != engine + && number > finished + && need.is_none_or(|(v, _)| value > v) + { + need = Some((value, why)); + } + }; + if ask.title.is_none() { + behind(self.last[o], Why::Ours); + } + if ask.steps { + behind(self.last[o], Why::Steps); + } + if ask.tiles { + behind(self.last[o], Why::Tiles); + } + if ask.card { + behind(self.card[o], Why::Card); + } + for t in ask.textures { + behind(self.textures.get(t).copied(), Why::Texture); + } + if ask.after > finished { + let at = self.recent.partition_point(|s| s.0 < ask.after); + behind(self.recent.get(at).copied().filter(|s| s.0 == ask.after), Why::Value); + } + if engine == Engine::Compute && ask.holds & NOW != 0 { + // A list of the direct queue's, after everything sent there, + // with a signal of its own. + self.sent[Engine::Direct as usize] += 1; + let handoff = (u64::MAX, Engine::Direct, self.sent[Engine::Direct as usize]); + self.last[Engine::Direct as usize] = Some(handoff); + behind(Some(handoff), Why::Handoff); + } + let wait = need.filter(|&(value, _)| value > self.waited[q]); + if let Some((value, _)) = wait { + self.waited[q] = value; + } + self.sent[q] += 1; + let sent = (ask.number, engine, self.sent[q]); + self.last[q] = Some(sent); + if ask.card { + self.card[q] = Some(sent); + } + for &t in ask.textures { + self.textures.insert(t, sent); + } + if self.textures.len() > 4 * REMEMBERED { + self.textures.retain(|_, s| s.0 > finished); + } + self.recent.push_back(sent); + if self.recent.len() > REMEMBERED { + self.recent.pop_front(); + } + Placed { + engine, + value: self.sent[q], + wait, + } + } +} + +/// Whether submissions are planned for two queues +/// (`VICTORY_ASYNC_QUEUE=plan`; off by default). +/// +/// The gameplay frame is about 45 ms, and the card is busy for most of it +/// running about 11,000 small commands at 130-140 W of its 220: it waits on +/// itself more than it works. A frame timeline (tl.190) had the submission +/// the title waited for stand 7.2 ms sent and not begun, behind other work +/// on the one queue. The title runs part of its work on compute queues of +/// its own, beside its drawing, and on the console that work overlaps. +/// Whether giving it a host queue of its own is worth the work it takes is +/// what this plan is for: how many submissions would go there, how many +/// would stay for what they hold, and how many waits between the queues +/// it would take. `VICTORY_ASYNC_QUEUE=1` (the queue itself) is not built: +/// it plans and says so. +pub(crate) fn on() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| match std::env::var("VICTORY_ASYNC_QUEUE").as_deref() { + Ok("plan") => { + tracing::info!("submissions are planned for a compute queue beside the direct queue (VICTORY_ASYNC_QUEUE=plan); everything runs as before"); + true + } + Ok("1") => { + tracing::warn!( + "VICTORY_ASYNC_QUEUE=1: the title's compute work on a host compute queue is not built (its lists are recorded as \ + direct lists); planned only, as with VICTORY_ASYNC_QUEUE=plan, and everything runs on the direct queue" + ); + true + } + _ => false, + }) +} + +thread_local! { + /// The title queue whose submission this thread sends now: its list + /// type, and the submission a wait it passed relies on ([`for_title`]). + static TITLE: std::cell::Cell> = const { std::cell::Cell::new(None) }; +} + +/// Runs `send`, which sends a submission of a title queue whose lists are of +/// type `kind`; `after` as in [`Ask::after`]. +pub(crate) fn for_title(kind: Option, after: u64, send: impl FnOnce() -> R) -> R { + let was = TITLE.replace(kind.map(|k| (k, after))); + let sent = send(); + TITLE.set(was); + sent +} + +/// The list type of the title queue whose submission this thread sends. +#[cfg(feature = "diagnostics")] +pub(crate) fn title_kind() -> Option { + TITLE.get().map(|t| t.0) +} + +/// Waits for memory values passed because a flight will write them, by the +/// title queue that passed them (its address) and the flight's number. +static AFTER: Mutex> = Mutex::new(Vec::new()); + +/// Queue `queue` passed `wait`: if a flight in the air meets it, its next +/// submission comes behind that flight. +pub(crate) fn value_passed(queue: usize, wait: &crate::list::ValueWait) { + if !on() { + return; + } + let Some(by) = super::late_met_by(wait) else { + return; + }; + let mut after = AFTER.lock().unwrap_or_else(|e| e.into_inner()); + match after.iter_mut().find(|a| a.0 == queue) { + Some(a) => a.1 = a.1.max(by), + None => after.push((queue, by)), + } +} + +/// What queue `queue`'s next submission comes behind ([`value_passed`]; 0: +/// nothing), taken. +pub(crate) fn take_after(queue: usize) -> u64 { + if !on() { + return 0; + } + let mut after = AFTER.lock().unwrap_or_else(|e| e.into_inner()); + match after.iter().position(|a| a.0 == queue) { + Some(i) => after.swap_remove(i).1, + None => 0, + } +} + +/// Tile mappings put on the direct queue so far ([`Ask::tiles`]). +static TILES: AtomicU64 = AtomicU64::new(0); + +/// Tiles were mapped on the direct queue. +pub(crate) fn tiles_mapped() { + TILES.fetch_add(1, Relaxed); +} + +/// What [`Host::submit`](super::Host) finds out about a submission before +/// its lists' preludes move the textures' states. +pub(crate) struct Probe { + title: Option<(u32, u64)>, + holds: u8, + textures: Vec, + tiles: u64, +} + +/// Looks at the lists of a submission about to be prepared. +pub(crate) fn probe(runs: &[&super::HostList]) -> Probe { + let mut holds = 0; + let mut textures = Vec::new(); + for list in runs { + let states = list.states.lock().unwrap_or_else(|e| e.into_inner()); + for (key, (first, last, copy)) in states.iter() { + textures.push(*key); + holds |= holds_of(*first, *last); + if copy.state.load(Relaxed) & !COMPUTE_STATES != 0 { + holds |= NOW; + } + } + drop(states); + if !list.twins.lock().unwrap_or_else(|e| e.into_inner()).is_empty() { + holds |= TWINS; + } + } + textures.sort_unstable(); + textures.dedup(); + Probe { + title: TITLE.get(), + holds, + textures, + tiles: TILES.load(Relaxed), + } +} + +static PLAN: Mutex> = Mutex::new(None); + +/// Since the last frame line: submissions by the title queue's type (direct, +/// compute, copy, ours or not known); lists of the compute queues'; of +/// those submissions, the ones with [`READ`], [`STATE`], [`TWINS`], [`NOW`], +/// the ones placed on the compute queue, and those of them without a wait; +/// waits by [`Why`]; waits of the compute queue for the direct one. +static STATS: [AtomicU64; 12 + WHYS] = [const { AtomicU64::new(0) }; 12 + WHYS]; +const LISTS: usize = 4; +const HELD: usize = 5; +const PLACED: usize = 9; +const FREE: usize = 10; +const COMPUTE_WAITS: usize = 11; +const WAITS: usize = 12; + +/// Places a submission ([`Plan::place`]): `number` as in [`Ask::number`], +/// `card` and `steps` as there, `lists` its host lists, `finished` as there. +pub(crate) fn place(probe: Probe, number: u64, card: bool, steps: bool, lists: usize, finished: u64) { + let title = probe.title.map(|t| t.0); + let ask = Ask { + number, + title, + holds: probe.holds, + card, + steps, + tiles: TILES.load(Relaxed) != probe.tiles, + textures: &probe.textures, + after: probe.title.map_or(0, |t| t.1), + }; + let placed = { + let mut plan = PLAN.lock().unwrap_or_else(|e| e.into_inner()); + plan.get_or_insert_with(Plan::default).place(&ask, finished) + }; + let by = match title { + Some(0) => 0, + Some(COMPUTE) => 1, + Some(3) => 2, + _ => 3, + }; + STATS[by].fetch_add(1, Relaxed); + if title == Some(COMPUTE) { + STATS[LISTS].fetch_add(lists as u64, Relaxed); + for (i, bit) in [READ, STATE, TWINS, NOW].into_iter().enumerate() { + if ask.holds & bit != 0 { + STATS[HELD + i].fetch_add(1, Relaxed); + } + } + } + if placed.engine == Engine::Compute { + STATS[PLACED].fetch_add(1, Relaxed); + if placed.wait.is_none() { + STATS[FREE].fetch_add(1, Relaxed); + } + } + if let Some((_, why)) = placed.wait { + STATS[WAITS + why as usize].fetch_add(1, Relaxed); + if placed.engine == Engine::Compute { + STATS[COMPUTE_WAITS].fetch_add(1, Relaxed); + } + } +} + +/// For the frame-rate line. +pub(crate) fn line(frames: f64) { + if !on() { + return; + } + let s: Vec = STATS.iter().map(|s| s.swap(0, Relaxed) as f64 / frames).collect(); + let waits: f64 = s[WAITS..].iter().sum(); + tracing::info!( + "two host queues, planned per frame (VICTORY_ASYNC_QUEUE; everything runs on the direct queue): submissions of the \ + title's direct queue {:.1}, of its compute queues {:.1} ({:.1} lists), of its copy queues {:.1}, ours or not known {:.1}; \ + of the compute queues' submissions, {:.1} read a texture in both read states (a compute list would name the first \ + alone), {:.1} begin or end a texture in a drawing state and {:.1} copy depth twins (both stay on the direct queue), \ + {:.1} name a texture in a drawing state now (moved on the direct queue first); {:.1} would go to a compute queue, \ + {:.1} of them without a wait; waits between the queues {waits:.1}, {:.1} of them by the compute queue: for copies in \ + the card's memory {:.1}, steps on the card {:.1}, textures {:.1}, memory values a flight of the other queue writes \ + {:.1}, tile mappings {:.1}, a texture's state moved first {:.1}, ours {:.1}", + s[0], + s[1], + s[LISTS], + s[2], + s[3], + s[HELD], + s[HELD + 1], + s[HELD + 2], + s[HELD + 3], + s[PLACED], + s[FREE], + s[COMPUTE_WAITS], + s[WAITS + Why::Card as usize], + s[WAITS + Why::Steps as usize], + s[WAITS + Why::Texture as usize], + s[WAITS + Why::Value as usize], + s[WAITS + Why::Tiles as usize], + s[WAITS + Why::Handoff as usize], + s[WAITS + Why::Ours as usize], + ); +} + +#[cfg(test)] +mod tests { + use super::*; + + fn ask(number: u64, title: Option) -> Ask<'static> { + Ask { + number, + title, + holds: 0, + card: false, + steps: false, + tiles: false, + textures: &[], + after: 0, + } + } + + const DIRECT: Option = Some(0); + const COMPUTE_QUEUE: Option = Some(COMPUTE); + + /// The title's direct and copy queues stay on the direct queue, its + /// compute queues go to the compute queue; each queue counts its own + /// values, and nothing waits where nothing is shared. + #[test] + fn queues_are_kept_apart_and_count_their_own_values() { + let mut plan = Plan::default(); + let a = plan.place(&ask(1, DIRECT), 0); + let b = plan.place(&ask(2, COMPUTE_QUEUE), 0); + let c = plan.place(&ask(3, Some(3)), 0); + let d = plan.place(&ask(4, COMPUTE_QUEUE), 0); + assert_eq!((a.engine, a.value, a.wait), (Engine::Direct, 1, None)); + assert_eq!((b.engine, b.value, b.wait), (Engine::Compute, 1, None)); + assert_eq!((c.engine, c.value), (Engine::Direct, 2)); + assert_eq!((d.engine, d.value, d.wait), (Engine::Compute, 2, None)); + } + + /// A texture named on both queues: the second comes behind the first, + /// on the other queue; on the same queue the order is the queue's. + #[test] + fn a_texture_shared_between_the_queues_is_waited_for() { + let mut plan = Plan::default(); + let t = [0x1000usize]; + plan.place(&Ask { textures: &t, ..ask(1, DIRECT) }, 0); + plan.place(&Ask { textures: &t, ..ask(2, DIRECT) }, 0); + let c = plan.place(&Ask { textures: &t, ..ask(3, COMPUTE_QUEUE) }, 0); + assert_eq!(c.wait, Some((2, Why::Texture))); + let d = plan.place(&Ask { textures: &t, ..ask(4, DIRECT) }, 0); + assert_eq!((d.engine, d.wait), (Engine::Direct, Some((1, Why::Texture)))); + let e = plan.place(&Ask { textures: &t, ..ask(5, DIRECT) }, 0); + assert_eq!(e.wait, None, "named last on the same queue"); + } + + /// A wait for a value the queue waits behind already is left out, and so + /// is one for a submission that has finished. + #[test] + fn waits_are_only_put_where_they_are_new() { + let mut plan = Plan::default(); + let (t, u) = ([1usize], [2usize]); + plan.place(&Ask { textures: &t, ..ask(1, DIRECT) }, 0); + plan.place(&Ask { textures: &u, ..ask(2, DIRECT) }, 0); + let c = plan.place(&Ask { textures: &u, ..ask(3, COMPUTE_QUEUE) }, 0); + assert_eq!(c.wait, Some((2, Why::Texture))); + // Texture 1 was last named by the direct queue's 1: behind it already. + let d = plan.place(&Ask { textures: &t, ..ask(4, COMPUTE_QUEUE) }, 0); + assert_eq!(d.wait, None); + plan.place(&Ask { textures: &t, ..ask(5, DIRECT) }, 0); + // Submission 5 has finished: nothing to wait for. + let f = plan.place(&Ask { textures: &t, ..ask(6, COMPUTE_QUEUE) }, 5); + assert_eq!(f.wait, None); + } + + /// Copies in the card's memory chain the queues: a submission with them + /// comes behind the other queue's last one with them, not behind the rest. + #[test] + fn copies_in_the_cards_memory_are_sent_in_one_order() { + let mut plan = Plan::default(); + plan.place(&Ask { card: true, ..ask(1, DIRECT) }, 0); + plan.place(&ask(2, DIRECT), 0); + let c = plan.place(&Ask { card: true, ..ask(3, COMPUTE_QUEUE) }, 0); + assert_eq!(c.wait, Some((1, Why::Card))); + let d = plan.place(&ask(4, COMPUTE_QUEUE), 0); + assert_eq!(d.wait, None); + let e = plan.place(&Ask { card: true, ..ask(5, DIRECT) }, 0); + assert_eq!(e.wait, Some((1, Why::Card)), "behind the compute queue's first, its only one with copies"); + } + + /// Steps on the card, tile mappings and submissions of ours come behind + /// everything the other queue was given. + #[test] + fn steps_tiles_and_ours_wait_for_everything_before() { + let mut plan = Plan::default(); + plan.place(&ask(1, COMPUTE_QUEUE), 0); + plan.place(&ask(2, COMPUTE_QUEUE), 0); + let s = plan.place(&Ask { steps: true, ..ask(3, DIRECT) }, 0); + assert_eq!(s.wait, Some((2, Why::Steps))); + plan.place(&ask(4, COMPUTE_QUEUE), 0); + let o = plan.place(&ask(5, None), 0); + assert_eq!((o.engine, o.wait), (Engine::Direct, Some((3, Why::Ours)))); + plan.place(&ask(6, DIRECT), 0); + let t = plan.place(&Ask { tiles: true, ..ask(7, COMPUTE_QUEUE) }, 0); + assert_eq!(t.wait, Some((3, Why::Tiles))); + } + + /// A wait for a memory value passed because a flight will write it: behind + /// that flight if it is on the other queue, nothing if it is on the same + /// one or has finished. + #[test] + fn a_value_a_flight_writes_is_waited_for_on_the_other_queue() { + let mut plan = Plan::default(); + plan.place(&ask(1, DIRECT), 0); + plan.place(&ask(2, COMPUTE_QUEUE), 0); + plan.place(&ask(3, DIRECT), 0); + let a = plan.place(&Ask { after: 1, ..ask(4, COMPUTE_QUEUE) }, 0); + assert_eq!(a.wait, Some((1, Why::Value))); + let b = plan.place(&Ask { after: 2, ..ask(5, COMPUTE_QUEUE) }, 0); + assert_eq!(b.wait, None, "the same queue"); + let c = plan.place(&Ask { after: 4, ..ask(6, DIRECT) }, 4); + assert_eq!(c.wait, None, "finished"); + let d = plan.place(&Ask { after: 5, ..ask(7, DIRECT) }, 4); + assert_eq!(d.wait, Some((3, Why::Value))); + } + + /// A compute submission whose lists hold what a compute list cannot stays + /// on the direct queue; one that names a texture in a drawing state has + /// that state moved on the direct queue first, behind everything there. + #[test] + fn what_a_compute_list_cannot_hold_keeps_it_on_the_direct_queue() { + let mut plan = Plan::default(); + plan.place(&ask(1, DIRECT), 0); + for holds in [STATE, TWINS, STATE | READ] { + let p = plan.place(&Ask { holds, ..ask(2, COMPUTE_QUEUE) }, 0); + assert_eq!(p.engine, Engine::Direct); + } + let r = plan.place(&Ask { holds: READ, ..ask(3, COMPUTE_QUEUE) }, 0); + assert_eq!((r.engine, r.wait), (Engine::Compute, None)); + let n = plan.place(&Ask { holds: NOW, ..ask(4, COMPUTE_QUEUE) }, 0); + // Direct: 1, then the three kept, then the handoff's own signal. + assert_eq!((n.engine, n.wait), (Engine::Compute, Some((5, Why::Handoff)))); + let after = plan.place(&ask(5, DIRECT), 0); + assert_eq!(after.value, 6); + } + + /// The states a list begins and ends a texture with: the compute ones + /// pass, the pixel shader's read alone is a read, anything else drawing. + #[test] + fn states_are_sorted_into_what_a_compute_list_may_name() { + assert_eq!(holds_of(0x8, 0x40), 0); + assert_eq!(holds_of(0x40 | 0x80, 0x8), READ); + assert_eq!(holds_of(0x4, 0x40), STATE); + assert_eq!(holds_of(0x20 | 0x40 | 0x80, 0x40), STATE); + assert_eq!(holds_of(0x0, 0x800), 0); + } +} diff --git a/crates/victory-gpu/src/queue.rs b/crates/victory-gpu/src/queue.rs index 2a908ec..3ae12d2 100644 --- a/crates/victory-gpu/src/queue.rs +++ b/crates/victory-gpu/src/queue.rs @@ -162,7 +162,7 @@ fn submit_cut(this: This, lists: &[This]) { // from the title's lists (which it may reset before they have run) // and come back, with their tables' slots, when they are settled. let (taken, order) = crate::list::detach(&lists[..first]); - host.execute_late(taken, order); + crate::host::two_queues::for_title(Some(queue(this).kind), 0, || host.execute_late(taken, order)); } else if first > 0 { let now: Vec<_> = lists[..first] .iter() @@ -383,7 +383,7 @@ pub(crate) fn release_held(all: bool) { { // Not waited for: the host keeps the lists // until they are settled. - host.execute_late(lists, order); + crate::host::two_queues::for_title(kind_of(q), 0, || host.execute_late(lists, order)); } else { let ran = { let runs: Vec<&crate::host::HostList> = @@ -499,6 +499,8 @@ pub(crate) extern "system" fn create( return E_INVALIDARG; }; tracing::debug!(?desc, "command queue"); + #[cfg(windows)] + let kind = desc.kind; let queue = Box::new(Queue { object: if crate::device::newer() { Object::new(&NEWER_FUNCTIONS) @@ -510,7 +512,29 @@ pub(crate) extern "system" fn create( #[cfg(windows)] held: Default::default(), }); - object::hand_out(QUEUE, queue, iid, &QUEUE_IIDS, out) + let made = object::hand_out(QUEUE, queue, iid, &QUEUE_IIDS, out); + #[cfg(windows)] + if made == S_OK + // SAFETY: the out pointer was written. + && let Some(&queue_at) = unsafe { out.as_ref() } + { + let mut kinds = lock(&KINDS); + kinds.retain(|k| k.0 != queue_at as usize); + kinds.push((queue_at as usize, kind)); + } + made +} + +/// The title's queues (their addresses) and their list types, for what is +/// done with a queue's submission later, on another thread, when the +/// queue may have been let go ([`kind_of`]). +#[cfg(windows)] +static KINDS: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + +/// The list type of the title's queue at `queue`, if it is one. +#[cfg(windows)] +pub(crate) fn kind_of(queue: usize) -> Option { + lock(&KINDS).iter().find(|k| k.0 == queue).map(|k| k.1) } /// The console's queue description: an engine or pipe and a queue on it @@ -559,6 +583,7 @@ extern "system" fn release(this: This) -> u32 { #[cfg(windows)] if left == 0 { lock(&WAITING).retain(|&q| q != this as usize); + lock(&KINDS).retain(|k| k.0 != this as usize); } left } diff --git a/crates/victory-gpu/src/queue/behind.rs b/crates/victory-gpu/src/queue/behind.rs index 813155c..c9869ed 100644 --- a/crates/victory-gpu/src/queue/behind.rs +++ b/crates/victory-gpu/src/queue/behind.rs @@ -378,8 +378,9 @@ type Recordings = std::sync::Arc<[std::sync::Arc]> enum Job { /// The queue waits for the fence (its address) to reach the value. Wait(usize, u64), - /// A part of a submission waits for a memory value. - Value(crate::list::ValueWait), + /// A part of a submission to the queue (its address) waits for a memory + /// value. + Value(usize, crate::list::ValueWait), /// A submission, or a part of one: the lists' host sides and the order /// they run in. Run(usize, Vec, Vec), @@ -416,7 +417,7 @@ impl Work for Job { fn ask(&self) -> Option { match self { &Job::Wait(fence, value) => Some(Ask::Wait(fence, value)), - Job::Value(wait) => Some(Ask::Value(*wait)), + Job::Value(_, wait) => Some(Ask::Value(*wait)), Job::Lists(_, lists) => Some(Ask::Lists(lists.clone())), _ => None, } @@ -468,20 +469,24 @@ impl Work for Job { return Err(next); } // Nothing but waits and signals that a submission may follow. - let (count, lists, signals) = match self { - Job::Run(_, lists, order) => (1, (&*lists, &*order), &[][..]), - Job::Joined(_, lists, order, parts, signals) => (1 + parts.len(), (&*lists, &*order), &signals[..]), + let (queue, count, lists, signals) = match self { + Job::Run(queue, lists, order) => (*queue, 1, (&*lists, &*order), &[][..]), + Job::Joined(queue, lists, order, parts, signals) => (*queue, 1 + parts.len(), (&*lists, &*order), &signals[..]), _ => return Err(next), }; let hosts = || lists.1.iter().map(|&i| lists.0[i].host()); match &next { - Job::Value(wait) => { - let met = wait.met() - || crate::host::late_met_known(wait) == Some(true) - || hosts().any(|list| crate::host::will_meet(list, wait)); - if !met { + Job::Value(_, wait) => { + let met = wait.met(); + let ahead = !met && crate::host::late_met_known(wait) == Some(true); + if !met && !ahead && !hosts().any(|list| crate::host::will_meet(list, wait)) { return Err(next); } + // Met by a flight in the air: on a queue of its own the + // submission would wait for it. + if ahead && crate::host::two_queues::on() && !hosts().any(|list| crate::host::will_meet(list, wait)) { + crate::host::two_queues::value_passed(queue, wait); + } } &Job::Wait(fence, value) => { if crate::fence::completed(fence as This) < value && !signals.iter().any(|&(f, v)| f == fence && v >= value) { @@ -539,27 +544,32 @@ impl Work for Job { } match self { // (A folded one is unfolded, never done.) - Job::Wait(..) | Job::Value(..) | Job::Lists(..) => {} + Job::Wait(..) | Job::Lists(..) => {} + Job::Value(queue, wait) => { + if !forced { + crate::host::two_queues::value_passed(queue, &wait); + } + } Job::Run(queue, lists, order) => { - let _ = queue; #[cfg(feature = "diagnostics")] crate::capture::op(queue, || format!("RUN submission of {} lists (its thread's)", order.len())); - match crate::host::host() { + let after = crate::host::two_queues::take_after(queue); + crate::host::two_queues::for_title(super::kind_of(queue), after, || match crate::host::host() { Some(host) => host.execute_late(lists, order), None => drop(lists), - } + }); QUEUED.fetch_sub(1, Relaxed); } Job::Joined(queue, lists, order, parts, signals) => { - let _ = queue; #[cfg(feature = "diagnostics")] crate::capture::op(queue, || { format!("RUN {} parts of submissions in one, {} lists (its thread's)", 1 + parts.len(), order.len()) }); - match crate::host::host() { + let after = crate::host::two_queues::take_after(queue); + crate::host::two_queues::for_title(super::kind_of(queue), after, || match crate::host::host() { Some(host) => host.execute_parts(lists, order, &parts), None => drop(lists), - } + }); // (A part that had no list is one all the same.) QUEUED.fetch_sub(1 + parts.len() as u64, Relaxed); for (fence, value) in signals { @@ -792,7 +802,7 @@ fn jobs_of(this: This, lists: &[This]) -> Vec { } if first < lists.len() { for (waits, taken, order) in crate::list::detach_parts(&lists[first..]) { - jobs.extend(waits.into_iter().map(Job::Value)); + jobs.extend(waits.into_iter().map(|wait| Job::Value(queue, wait))); jobs.push(Job::Run(queue, taken, order)); } } diff --git a/crates/victory-gpu/src/tiles.rs b/crates/victory-gpu/src/tiles.rs index 6d3c3d8..b8ca6a8 100644 --- a/crates/victory-gpu/src/tiles.rs +++ b/crates/victory-gpu/src/tiles.rs @@ -569,6 +569,7 @@ impl Tiled { } STATS[6].fetch_add(1, Ordering::Relaxed); STATS[7].fetch_add(began.elapsed().as_micros() as u64, Ordering::Relaxed); + crate::host::tiles_mapped_on(queue); match level { Some(level) => given.levels[level] = true, None => given.packed = true, diff --git a/crates/victory-gpu/src/timeline/record.rs b/crates/victory-gpu/src/timeline/record.rs index fb3b517..89f256e 100644 --- a/crates/victory-gpu/src/timeline/record.rs +++ b/crates/victory-gpu/src/timeline/record.rs @@ -291,13 +291,19 @@ pub(super) fn kernel(name: &'static str, bytes: u64, t0: u64) { } /// Lists went to the card between `t0` ([`ticks`]) and now, the value -/// `fence` signalled behind them, with the timestamps at `stamp`. -pub(crate) fn sent(t0: u64, fence: u64, stamp: Option) { +/// `fence` signalled behind them, with the timestamps at `stamp`; +/// `compute`: a submission of a compute queue of the title's ([`COMPUTE`]). +pub(crate) fn sent(t0: u64, fence: u64, stamp: Option, compute: bool) { if on() { - put(Kind::Sent, "ExecuteCommandLists", (t0, ticks()), fence, stamp.map_or(0, |s| s as u64 + 1), 0); + let b = stamp.map_or(0, |s| s as u64 + 1) | if compute { COMPUTE } else { 0 }; + put(Kind::Sent, "ExecuteCommandLists", (t0, ticks()), fence, b, 0); } } +/// The bit of a sent event's second word (the timestamps' place plus one) +/// that says it was a submission of a compute queue of the title's. +pub(super) const COMPUTE: u64 = 1 << 63; + /// `value` is being stored at `at`, where the title looks for it. #[inline(always)] pub(crate) fn told(name: &'static str, at: usize, value: u64) { diff --git a/crates/victory-gpu/src/timeline/report.rs b/crates/victory-gpu/src/timeline/report.rs index d07a5e8..2431385 100644 --- a/crates/victory-gpu/src/timeline/report.rs +++ b/crates/victory-gpu/src/timeline/report.rs @@ -4,7 +4,7 @@ use std::collections::{BTreeMap, HashMap}; use std::fmt::Write as _; -use super::record::{COPY_QUEUE, DEPTH, Data, Event, Kind, frequency}; +use super::record::{COMPUTE, COPY_QUEUE, DEPTH, Data, Event, Kind, frequency}; /// The process's threads at a moment. #[derive(Default)] @@ -244,6 +244,8 @@ pub(super) struct Run { pub(super) t1: u64, pub(super) thread: u32, pub(super) on: (u64, u64), + /// A submission of a compute queue of the title's. + pub(super) compute: bool, } /// What was recorded, by thread and in order. @@ -284,13 +286,15 @@ impl Trace { }), Kind::Wake => trace.wakes.entry(e.a).or_default().push((e.t0, e.thread)), Kind::Sent => { - let on = (e.b as usize).checked_sub(1).and_then(|at| stamps.get(at)).copied().unwrap_or((0, 0)); + let at = e.b & !COMPUTE; + let on = (at as usize).checked_sub(1).and_then(|at| stamps.get(at)).copied().unwrap_or((0, 0)); trace.runs.push(Run { fence: e.a, t0: e.t0, t1: e.t1, thread: e.thread, on, + compute: e.b & COMPUTE != 0, }); trace.spans.entry(e.thread).or_default().push(i); } @@ -568,6 +572,17 @@ pub(super) fn summary(trace: &Trace, names: &mut Names, stacks: &[[usize; DEPTH] for (cause, sum) in idle.into_iter().take(30) { let _ = writeln!(text, " {:7.2} ms {cause}", ms(sum) / frames); } + // The most a host queue of their own could run beside the rest + // (`crate::host::two_queues`). + let compute: Vec<&&Run> = runs.iter().filter(|r| r.compute).collect(); + let running: u64 = compute.iter().map(|r| r.on.1.clamp(start, stop).saturating_sub(r.on.0.clamp(start, stop))).sum(); + let _ = writeln!( + text, + "of the card's running, the submissions of the title's compute queues: {:.2} ms in {:.1} (what a host queue of their own \ + could run beside the rest, at most)", + ms(running) / frames, + compute.len() as f64 / frames + ); // Threads. let rate = (after.cycles.saturating_sub(before.cycles)) as f64 / ms(after.ticks.saturating_sub(before.ticks)).max(1e-9);