diff --git a/crates/victory-gpu/src/list.rs b/crates/victory-gpu/src/list.rs index ade005b..8a17ddb 100644 --- a/crates/victory-gpu/src/list.rs +++ b/crates/victory-gpu/src/list.rs @@ -18,6 +18,9 @@ use crate::object::{ use crate::queue::Written; use crate::resource::PAGEABLE_IIDS; +#[cfg(windows)] +pub(crate) mod thin; + const ALLOCATOR_IIDS: [Iid; 4] = [ guid( 0x6102dee4, @@ -127,6 +130,10 @@ struct List { /// at them before it runs `host`. #[cfg(windows)] gate: Vec, + /// With `VICTORY_THIN=1`, of a list the title has: what it wrote down + /// ([`thin`]; then there is no `host`). + #[cfg(windows)] + thin: thin::Front, } /// One part of a title list cut at its waits: the waits the queue stands @@ -560,11 +567,29 @@ fn new_list( return E_INVALIDARG; } tracing::debug!(kind, "command list"); + #[cfg(windows)] + if thin::on() { + let mut list = boxed(kind, pipeline, true); + list.thin.begin(kind, pipeline); + return object::hand_out(LIST, list, iid, &LIST_IIDS, out); + } + object::hand_out(LIST, boxed(kind, pipeline, false), iid, &LIST_IIDS, out) +} + +/// A list; with `thin` one whose calls are written down (it has no host list). +fn boxed(kind: u32, pipeline: This, thin: bool) -> Box { + #[cfg(not(windows))] + let _ = thin; let list = Box::new(List { - object: if crate::device::newer() { - Object::new(&NEWER_LIST_FUNCTIONS) - } else { - Object::new(&LIST_FUNCTIONS) + object: match (crate::device::newer(), thin) { + (true, false) => Object::new(&NEWER_LIST_FUNCTIONS), + (false, false) => Object::new(&LIST_FUNCTIONS), + #[cfg(windows)] + (true, true) => Object::new(&NEWER_THIN_FUNCTIONS), + #[cfg(windows)] + (false, true) => Object::new(&THIN_FUNCTIONS), + #[cfg(not(windows))] + _ => Object::new(&LIST_FUNCTIONS), }, kind, put: [0; 5], @@ -575,7 +600,7 @@ fn new_list( newer: [0; 256], uploads: Default::default(), #[cfg(windows)] - host: crate::host::host().and_then(|h| match h.new_list() { + host: crate::host::host().filter(|_| !thin).and_then(|h| match h.new_list() { Ok(list) => Some(list), Err(e) => { tracing::warn!(error = %e, "no host command list"); @@ -592,10 +617,12 @@ fn new_list( earlier: Vec::new(), #[cfg(windows)] gate: Vec::new(), + #[cfg(windows)] + thin: Default::default(), }); #[cfg(not(windows))] let _ = pipeline; - object::hand_out(LIST, list, iid, &LIST_IIDS, out) + list } extern "system" fn allocator_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT { @@ -2339,6 +2366,11 @@ const fn list_table(newer: bool) -> [*const (); 160] { /// titles add. static LIST_FUNCTIONS: Table<160> = Table(list_table(false)); static NEWER_LIST_FUNCTIONS: Table<160> = Table(list_table(true)); +/// Of lists whose calls are written down. +#[cfg(windows)] +static THIN_FUNCTIONS: Table<160> = Table(thin::table(false)); +#[cfg(windows)] +static NEWER_THIN_FUNCTIONS: Table<160> = Table(thin::table(true)); #[cfg(test)] mod tests { diff --git a/crates/victory-gpu/src/list/thin.rs b/crates/victory-gpu/src/list/thin.rs new file mode 100644 index 0000000..1991a33 --- /dev/null +++ b/crates/victory-gpu/src/list/thin.rs @@ -0,0 +1,912 @@ +//! List calls that only write down their arguments (`VICTORY_THIN=1`); what +//! they mean is worked out by threads of ours, beside the title's. +//! +//! Why: the title makes 45,000 to 60,000 list calls a frame. Each was +//! translated inside the call, 3.5 us of it, 160 to 190 ms of processor time a +//! frame on the title's five recording threads; its own code between the calls +//! is a tenth of that. Nothing else of the title's frame could go on +//! meanwhile: recording took 55 of 65 ms, and behind it the card stood idle +//! for half the frame (timeline waits-3). On the console such a call writes a +//! few words into the list's memory and the card reads them when the list +//! runs. So it is here now: a call appends its arguments (and copies of what +//! they point at) to a stream of words; threads of ours run the stream, call +//! by call and in order, through the list methods there were before, on a list +//! of ours ([`Recording::back`]) that the title never sees. They begin while +//! the title still records (every [`CHUNK`] words are handed over), several +//! lists at a time. A submission goes into the line of submissions +//! (`crate::queue::behind`) as it was made, by the recordings' names; the +//! thread that serves the line goes on with it when they are translated. +//! +//! What may be looked at later than before, and why that is all right: all +//! that the card of the console looks at when the list runs (descriptor heaps, +//! constants, upload memory, indirect arguments): it must stay as it is until +//! the list has run, and no fence behind the list is set before. What D3D12 +//! reads at the call is copied at the call: the arrays and structures passed, +//! and the render target and depth views, whose slots may be written again +//! right after. Objects named (pipelines, resources) must live until the list +//! has run; a recording the title resets or releases without submitting it is +//! not run any further. + +use super::*; +use std::collections::VecDeque; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering::*}; +use std::sync::{Arc, Condvar, Mutex, OnceLock}; + +use crate::descriptor::RtvRecord; +use crate::draw::{IndexView, VertexView}; +use windows::Win32::Foundation::RECT; +use windows::Win32::Graphics::Direct3D12::D3D12_VIEWPORT; + +/// Words after which a stream is handed to the translating threads. +const CHUNK: usize = 1024; + +/// Whether list calls are only written down (`VICTORY_THIN=1`; needs the line +/// of submissions, which it turns on). +pub(crate) fn on() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| { + wanted() && crate::host::host().is_some() && crate::queue::line_on() && { + start(); + true + } + }) +} + +/// Whether it is asked for. +pub(crate) fn wanted() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("VICTORY_THIN").is_some_and(|v| v == "1")) +} + +/// What a title list keeps: the recording being written and the words of it +/// not handed over yet. +#[derive(Default)] +pub(super) struct Front { + recording: Option>, + words: Vec, + closed: bool, + submitted: bool, +} + +impl Drop for Front { + fn drop(&mut self) { + self.leave(); + } +} + +impl Front { + /// Lets go of the recording there is: one never submitted is not run any + /// further. + fn leave(&mut self) { + if let Some(recording) = self.recording.take() + && !self.submitted + { + recording.abandoned.store(true, Release); + } + self.words.clear(); + } + + /// Begins a recording, with `pipeline` bound. + pub(super) fn begin(&mut self, kind: u32, pipeline: This) { + self.leave(); + self.recording = Some(Arc::new(Recording::new(kind))); + self.closed = false; + self.submitted = false; + self.put(RESET, &[pipeline as u64]); + } + + #[inline] + fn put(&mut self, op: u64, args: &[u64]) { + if self.closed || self.recording.is_none() { + return; + } + self.words.push(op | (args.len() as u64) << 16); + self.words.extend_from_slice(args); + if self.words.len() >= CHUNK { + self.hand_over(); + } + } + + /// [`Self::put`] of some words and then the bytes of some things. + fn put_with(&mut self, op: u64, args: &[u64], blobs: &[&[u8]]) { + if self.closed || self.recording.is_none() { + return; + } + let at = self.words.len(); + self.words.push(0); + self.words.extend_from_slice(args); + for blob in blobs { + let from = self.words.len(); + self.words.resize(from + blob.len().div_ceil(8), 0); + // SAFETY: the words just made hold the bytes; plain data. + unsafe { + std::ptr::copy_nonoverlapping(blob.as_ptr(), self.words.as_mut_ptr().add(from).cast(), blob.len()) + }; + } + let len = (self.words.len() - at - 1) as u64; + self.words[at] = op | len << 16; + if self.words.len() >= CHUNK { + self.hand_over(); + } + } + + fn hand_over(&mut self) { + let Some(recording) = &self.recording else { return }; + if self.words.is_empty() { + return; + } + let fresh = held(&WORDS).pop().unwrap_or_else(|| Vec::with_capacity(CHUNK + 256)); + let words = std::mem::replace(&mut self.words, fresh); + CHUNKS.fetch_add(1, Relaxed); + let schedule = { + let mut inner = held(&recording.inner); + inner.chunks.push_back(words); + !std::mem::replace(&mut inner.scheduled, true) + }; + if schedule { + held(&READY).push_back(recording.clone()); + WORK.notify_one(); + } + } + + fn close(&mut self) { + if self.closed { + return; + } + self.put(CLOSE, &[]); + self.closed = true; + self.hand_over(); + } +} + +/// Streams' memory to use again. +static WORDS: Mutex>> = Mutex::new(Vec::new()); +/// Recordings with words to translate. +static READY: Mutex>> = Mutex::new(VecDeque::new()); +static WORK: Condvar = Condvar::new(); +/// Lists of ours nobody uses, closed. +static BACKS: Mutex> = Mutex::new(Vec::new()); +/// Since the last line of the log: streams handed over; calls translated and +/// the microseconds that took (all threads), recordings translated, and +/// recordings let go of in the middle. +static CHUNKS: AtomicU64 = AtomicU64::new(0); +static STATS: [AtomicU64; 4] = [const { AtomicU64::new(0) }; 4]; + +fn held(m: &Mutex) -> std::sync::MutexGuard<'_, T> { + m.lock().unwrap_or_else(|e| e.into_inner()) +} + +/// One recording of a title list: what was written of it and not translated +/// yet, and the list of ours it goes to. +pub(crate) struct Recording { + kind: u32, + inner: Mutex, + /// Translated to its end. + done: AtomicBool, + moved: Condvar, + abandoned: AtomicBool, +} + +#[derive(Default)] +struct Inner { + chunks: VecDeque>, + /// Streams translated (commands may name places in them until the end). + kept: Vec>, + /// It is among [`READY`] or being translated. + scheduled: bool, + /// The list of ours (0: none yet). + back: usize, + /// That list was closed by the stream. + closed: bool, +} + +impl Recording { + fn new(kind: u32) -> Self { + Recording { + kind, + inner: Mutex::default(), + done: AtomicBool::new(false), + moved: Condvar::new(), + abandoned: AtomicBool::new(false), + } + } + + pub(crate) fn done(&self) -> bool { + self.done.load(Acquire) + } + + /// Waits until it is translated. + pub(crate) fn wait(&self) { + let mut inner = held(&self.inner); + while !self.done() { + inner = self.moved.wait(inner).unwrap_or_else(|e| e.into_inner()); + } + } + + /// The list of ours it went to (null: it was never begun). + pub(crate) fn back(&self) -> This { + held(&self.inner).back as This + } + + /// Translates what there is. + fn run(&self) { + loop { + let (chunk, back) = { + let mut inner = held(&self.inner); + let Some(chunk) = inner.chunks.pop_front() else { + inner.scheduled = false; + return; + }; + if inner.back == 0 { + inner.back = back_list(self.kind) as usize; + } + (chunk, inner.back as This) + }; + let began = std::time::Instant::now(); + let (calls, closed) = translate(back, &chunk, || self.abandoned.load(Acquire)); + STATS[0].fetch_add(calls, Relaxed); + STATS[1].fetch_add(began.elapsed().as_micros() as u64, Relaxed); + let mut inner = held(&self.inner); + inner.kept.push(chunk); + if closed { + inner.closed = true; + let kept = std::mem::take(&mut inner.kept); + self.done.store(true, Release); + self.moved.notify_all(); + drop(inner); + give_back(kept); + STATS[2].fetch_add(1, Relaxed); + crate::queue::line_moved(); + } + } + } +} + +impl Drop for Recording { + fn drop(&mut self) { + let inner = self.inner.get_mut().unwrap_or_else(|e| e.into_inner()); + give_back(inner.chunks.drain(..).chain(std::mem::take(&mut inner.kept)).collect()); + if inner.back != 0 { + if !inner.closed { + // Left in the middle. + STATS[3].fetch_add(1, Relaxed); + super::close(inner.back as This); + } + held(&BACKS).push(inner.back); + } + } +} + +fn give_back(streams: Vec>) { + let mut words = held(&WORDS); + for mut s in streams { + if words.len() < 1024 && s.capacity() <= 16 * CHUNK { + s.clear(); + words.push(s); + } + } +} + +/// A list of ours, closed. +fn back_list(kind: u32) -> This { + if let Some(back) = held(&BACKS).pop() { + // SAFETY: a list of ours nobody uses. + unsafe { (*(back as *mut List)).kind = kind }; + return back as This; + } + let list: This = Box::into_raw(super::boxed(kind, std::ptr::null(), false)).cast_const().cast(); + super::close(list); + list +} + +fn start() { + let threads = std::env::var("VICTORY_THIN_THREADS").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(8usize).clamp(1, 32); + for i in 0..threads { + let worker = move || { + crate::cores::keep_helper_on_fast_cores("victory-list"); + crate::queue::no_drain_here(); + loop { + let recording = { + let mut ready = held(&READY); + loop { + match ready.pop_front() { + Some(r) => break r, + None => ready = WORK.wait(ready).unwrap_or_else(|e| e.into_inner()), + } + } + }; + if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| recording.run())).is_err() { + crate::host::fail(format_args!("a thread that translates lists panicked")); + } + } + }; + if let Err(e) = std::thread::Builder::new().name(format!("victory-list-{i}")).spawn(worker) { + tracing::warn!(error = %e, "a thread less to translate lists"); + } + } + tracing::info!(threads, "list calls are written down and translated by threads of ours (VICTORY_THIN=1)"); +} + +/// The recordings of submitted lists, in order (one that was not begun is left +/// out). +pub(crate) fn submitted(lists: &[This]) -> Arc<[Arc]> { + lists + .iter() + .filter(|l| !l.is_null()) + .filter_map(|&l| { + // SAFETY: the title passes its lists, which nobody records into now. + let front = unsafe { &mut (*l.cast_mut().cast::()).thin }; + front.close(); + front.submitted = true; + front.recording.clone() + }) + .collect() +} + +/// For the frame-rate line. +pub(crate) fn line(frames: f64) { + if !wanted() || !on() { + return; + } + let s: Vec = STATS.iter().map(|s| s.swap(0, Relaxed) as f64 / frames).collect(); + tracing::info!( + "list calls translated by threads of ours per frame (VICTORY_THIN): {:.0} in {:.1} ms (all threads), {:.1} lists, {:.1} pieces; left in the middle {:.2}", + s[0], + s[1] / 1000.0, + s[2], + CHUNKS.swap(0, Relaxed) as f64 / frames, + s[3] + ); +} + +#[inline] +fn front<'a>(this: This) -> &'a mut Front { + // SAFETY: the title passes one of our lists; a list is recorded by one + // thread at a time. + unsafe { &mut (*this.cast_mut().cast::()).thin } +} + +/// An argument as a word. +trait Word: Copy { + fn word(self) -> u64; + fn unword(w: u64) -> Self; +} +macro_rules! words { + ($($t:ty),*) => { $(impl Word for $t { + #[inline] + fn word(self) -> u64 { self as u64 } + #[inline] + fn unword(w: u64) -> Self { w as $t } + })* }; +} +words!(u8, u32, i32, u64, This); +impl Word for f32 { + #[inline] + fn word(self) -> u64 { + u64::from(self.to_bits()) + } + #[inline] + fn unword(w: u64) -> Self { + f32::from_bits(w as u32) + } +} + +/// The methods whose arguments are all plain values: the one the title calls, +/// and the call made of it later. +macro_rules! plain { + ($($op:ident $name:ident = $was:path, ($($a:ident: $t:ty),*);)*) => { + #[allow(clippy::upper_case_acronyms, non_camel_case_types)] + #[repr(u64)] + enum Plain { $($op),* , END } + $(extern "system" fn $name(this: This $(, $a: $t)*) { + front(this).put(Plain::$op as u64, &[$(Word::word($a)),*]); + })* + /// Whether `op` was one of them; done on `list`. + #[inline] + fn run_plain(op: u64, list: This, w: &[u64]) -> bool { + match op { + $(n if n == Plain::$op as u64 => { + let mut words = w.iter(); + $(let $a: $t = Word::unword(words.next().copied().unwrap_or(0));)* + let _ = words; + $was(list $(, $a)*); + })* + _ => return false, + } + true + } + }; +} + +plain! { + TOPOLOGY topology = state::topology, (v: u32); + STENCIL_REF stencil_ref = state::stencil_ref, (v: u32); + DEPTH_BOUNDS depth_bounds = state::depth_bounds, (min: f32, max: f32); + COMPUTE_TABLE compute_table = state::compute_table, (slot: u32, handle: u64); + GRAPHICS_TABLE graphics_table = state::graphics_table, (slot: u32, handle: u64); + COMPUTE_CONSTANT compute_constant = state::compute_constant, (slot: u32, value: u32, offset: u32); + GRAPHICS_CONSTANT graphics_constant = state::graphics_constant, (slot: u32, value: u32, offset: u32); + COMPUTE_VIEW compute_view = state::compute_view, (slot: u32, address: u64); + GRAPHICS_VIEW graphics_view = state::graphics_view, (slot: u32, address: u64); + SET_PIPELINE set_pipeline = super::set_pipeline, (pipeline: This); + DRAW draw = state::draw, (vertices: u32, instances: u32, first: u32, first_instance: u32); + DRAW_INDEXED draw_indexed = state::draw_indexed, (indices: u32, instances: u32, first: u32, base: i32, first_instance: u32); + DISPATCH dispatch = state::dispatch, (x: u32, y: u32, z: u32); + DISPATCH_X dispatch_x = super::dispatch_x, (x: u32, y: u32, z: u32, flags: u32); + DISPATCH_MESH dispatch_mesh = state::dispatch_mesh, (x: u32, y: u32, z: u32); + DISPATCH_MESH_X dispatch_mesh_x = state::dispatch_mesh_x, (x: u32, flags: u64, indirect: u64, payload: u64); + COPY_BUFFER copy_buffer_region = super::copy_buffer_region, (dst: This, dst_offset: u64, src: This, src_offset: u64, len: u64); + COPY_RESOURCE copy_resource = super::copy_resource, (dst: This, src: This); + COPY_RESOURCE_X copy_resource_x = super::copy_resource_x, (dst: This, src: This, flags: u32); + COPY_MEMORY copy_memory = super::copy_memory, (dst: u64, src: u64, len: u64, flags: u32); + RESOLVE resolve = super::resolve, (dst: This, dst_sub: u32, src: This, src_sub: u32, format: u32); + RESOLVE_QUERIES resolve_queries = super::resolve_queries, (heap: This, kind: u32, first: u32, count: u32, buffer: This, offset: u64); + EXECUTE_INDIRECT execute_indirect = super::execute_indirect, (signature: This, max: u32, arguments: This, offset: u64, count: This, count_offset: u64); + WRITE32 write32 = super::write32, (address: u64, value: u32, flush: u32, flags: u32); + WRITE64 write64 = super::write64, (address: u64, value: u64, flush: u32, flags: u32); + WAIT_VALUE wait_value = super::wait_value, (address: u64, comparison: u32, reference: u64); + WAIT_VALUE64 wait_value64 = super::wait_value64, (address: u64, comparison: u32, reference: u64); + SYNC sync = super::sync, (flags: u32, control: u32, address: u64, size: u64); +} + +const fn after(n: u64) -> u64 { + Plain::END as u64 + n +} +const RESET: u64 = after(0); +const CLOSE: u64 = after(1); +const UPLOAD: u64 = after(2); +const BARRIER: u64 = after(3); +const BARRIER_GROUPS: u64 = after(4); +const VIEWPORTS: u64 = after(5); +const SCISSORS: u64 = after(6); +const BLEND_FACTOR: u64 = after(7); +const HEAPS: u64 = after(8); +const COMPUTE_CONSTANTS: u64 = after(9); +const GRAPHICS_CONSTANTS: u64 = after(10); +const INDEX_BUFFER: u64 = after(11); +const VERTEX_BUFFERS: u64 = after(12); +const TARGETS: u64 = after(13); +const CLEAR_RTV: u64 = after(14); +const CLEAR_DSV: u64 = after(15); +const COPY_TEXTURE: u64 = after(16); + +/// The bytes of `count` things at `items` (none if null). +fn bytes_of<'a, T>(items: *const T, count: usize) -> &'a [u8] { + if items.is_null() || count == 0 { + return &[]; + } + // SAFETY: the title passes `count` items. + unsafe { std::slice::from_raw_parts(items.cast(), count * size_of::()) } +} + +/// Where the things are in a stream (null for none). +fn at(w: &[u64], from: usize, count: usize) -> *const T { + if count == 0 || from >= w.len() { std::ptr::null() } else { w[from..].as_ptr().cast() } +} + +/// [`at`] where an empty array is not null. +fn at_or(w: &[u64], from: usize) -> *const T { + // SAFETY: at most one past the end. + unsafe { w.as_ptr().add(from.min(w.len())).cast() } +} + +/// A view's record as it is now (zeros for no view), so that its slot may be +/// written again: the copy stands for the handle. +fn record(handle: u64) -> [u64; RECORD] { + let mut words = [0u64; RECORD]; + if handle != 0 { + // SAFETY: a descriptor slot the title passes (they are larger). + unsafe { + std::ptr::copy_nonoverlapping(handle as *const u8, words.as_mut_ptr().cast(), size_of::()) + }; + } + words +} +const RECORD: usize = size_of::().div_ceil(8); + +extern "system" fn list_close(this: This) -> HRESULT { + front(this).close(); + S_OK +} +extern "system" fn list_reset(this: This, allocator: This, pipeline: This) -> HRESULT { + if allocator.is_null() { + return E_INVALIDARG; + } + // SAFETY: the title passes one of our lists. + let kind = unsafe { (*this.cast::()).kind }; + front(this).begin(kind, pipeline); + S_OK +} +extern "system" fn list_reset_x(this: This, allocator: This, pipeline: This, _flags: u32) -> HRESULT { + list_reset(this, allocator, pipeline) +} +extern "system" fn allocate_upload(this: This, size: u64, alignment: u64) -> *mut u8 { + let align = usize::try_from(alignment).unwrap_or(0).max(16); + let Some(len) = usize::try_from(size).ok().and_then(|s| s.checked_add(align)) else { + return std::ptr::null_mut(); + }; + if this.is_null() || !align.is_power_of_two() { + return std::ptr::null_mut(); + } + let front = front(this); + if front.closed || front.recording.is_none() { + return std::ptr::null_mut(); + } + let Some(mut block) = Upload::new(len) else { + return std::ptr::null_mut(); + }; + let start = block.start(); + // SAFETY: the offset is below `align`, so `size` bytes fit after it. + let place = unsafe { start.add(start.align_offset(align)) }; + front.put(UPLOAD, &[Box::into_raw(Box::new(block)) as u64]); + place +} +extern "system" fn barrier(this: This, count: u32, barriers: *const Barrier) -> u32 { + if !barriers.is_null() { + front(this).put_with(BARRIER, &[u64::from(count)], &[bytes_of(barriers, count as usize)]); + } + 0 +} +extern "system" fn barrier_groups(this: This, count: u32, _groups: *const u8) { + front(this).put(BARRIER_GROUPS, &[u64::from(count)]); +} +extern "system" fn viewports(this: This, count: u32, items: *const D3D12_VIEWPORT) { + let count = if items.is_null() { 0 } else { count }; + front(this).put_with(VIEWPORTS, &[u64::from(count)], &[bytes_of(items, count as usize)]); +} +extern "system" fn scissors(this: This, count: u32, items: *const RECT) { + let count = if items.is_null() { 0 } else { count }; + front(this).put_with(SCISSORS, &[u64::from(count)], &[bytes_of(items, count as usize)]); +} +extern "system" fn blend_factor(this: This, factor: *const [f32; 4]) { + let given = !factor.is_null(); + front(this).put_with(BLEND_FACTOR, &[u64::from(given)], &[bytes_of(factor, usize::from(given))]); +} +extern "system" fn heaps(this: This, count: u32, items: *const This) { + let count = if items.is_null() { 0 } else { count }; + front(this).put_with(HEAPS, &[u64::from(count)], &[bytes_of(items, count as usize)]); +} +fn constants(this: This, op: u64, slot: u32, count: u32, values: *const u32, offset: u32) { + let count = if values.is_null() { 0 } else { count }; + front(this).put_with( + op, + &[u64::from(slot) | u64::from(offset) << 32, u64::from(count)], + &[bytes_of(values, count as usize)], + ); +} +extern "system" fn compute_constants(this: This, slot: u32, count: u32, values: *const u32, offset: u32) { + constants(this, COMPUTE_CONSTANTS, slot, count, values, offset); +} +extern "system" fn graphics_constants(this: This, slot: u32, count: u32, values: *const u32, offset: u32) { + constants(this, GRAPHICS_CONSTANTS, slot, count, values, offset); +} +extern "system" fn index_buffer(this: This, view: *const IndexView) { + let given = !view.is_null(); + front(this).put_with(INDEX_BUFFER, &[u64::from(given)], &[bytes_of(view, usize::from(given))]); +} +extern "system" fn vertex_buffers(this: This, start: u32, count: u32, views: *const VertexView) { + let given = !views.is_null(); + front(this).put_with( + VERTEX_BUFFERS, + &[u64::from(start) | u64::from(count) << 32, u64::from(given)], + &[bytes_of(views, count as usize)], + ); +} +extern "system" fn targets(this: This, count: u32, handles: *const u64, one_range: i32, depth: *const u64) { + let count = if handles.is_null() { 0 } else { (count as usize).min(8) }; + // SAFETY: the title passes a handle or null. + let depth = unsafe { depth.as_ref() }.copied().unwrap_or(0); + let mut records = [0u64; RECORD * 9]; + let mut present = 0u64; + for i in 0..count { + // SAFETY: the title passes `count` handles, or the first of a range + // of views of 128 bytes. + let handle = unsafe { if one_range != 0 { *handles + 128 * i as u64 } else { *handles.add(i) } }; + present |= u64::from(handle != 0) << i; + records[i * RECORD..(i + 1) * RECORD].copy_from_slice(&record(handle)); + } + records[count * RECORD..(count + 1) * RECORD].copy_from_slice(&record(depth)); + let records = &records[..(count + 1) * RECORD]; + front(this).put_with( + TARGETS, + &[count as u64, present, u64::from(depth != 0)], + &[bytes_of(records.as_ptr(), records.len())], + ); +} +extern "system" fn clear_rtv(this: This, handle: u64, color: *const [f32; 4], count: u32, rects: *const Rect) { + if handle == 0 || color.is_null() { + return; + } + let count = if rects.is_null() { 0 } else { count }; + let view = record(handle); + front(this).put_with( + CLEAR_RTV, + &[u64::from(count)], + &[bytes_of(view.as_ptr(), RECORD), bytes_of(color, 1), bytes_of(rects, count as usize)], + ); +} +extern "system" fn clear_dsv( + this: This, + handle: u64, + flags: u32, + depth: f32, + stencil: u8, + count: u32, + rects: *const Rect, +) { + if handle == 0 { + return; + } + let count = if rects.is_null() { 0 } else { count }; + let view = record(handle); + front(this).put_with( + CLEAR_DSV, + &[u64::from(count), u64::from(flags) | u64::from(stencil) << 32, Word::word(depth)], + &[bytes_of(view.as_ptr(), RECORD), bytes_of(rects, count as usize)], + ); +} +extern "system" fn copy_texture_region( + this: This, + dst: *const CopyLocation, + x: u32, + y: u32, + z: u32, + src: *const CopyLocation, + source_box: *const [u32; 6], +) { + if dst.is_null() || src.is_null() { + return; + } + let boxed = !source_box.is_null(); + front(this).put_with( + COPY_TEXTURE, + &[u64::from(x) | u64::from(y) << 32, u64::from(z) | u64::from(boxed) << 32], + &[bytes_of(dst, 1), bytes_of(src, 1), bytes_of(source_box, usize::from(boxed))], + ); +} +#[allow(clippy::too_many_arguments)] // the method's own arguments +extern "system" fn copy_texture_region_x( + this: This, + dst: *const CopyLocation, + x: u32, + y: u32, + z: u32, + src: *const CopyLocation, + source_box: *const [u32; 6], + _flags: u32, +) { + copy_texture_region(this, dst, x, y, z, src, source_box); +} + +/// Makes the calls of a stream on `list`: how many, and whether the last one +/// closed it. Once `left` says so only what must not be lost is done. +fn translate(list: This, stream: &[u64], left: impl Fn() -> bool) -> (u64, bool) { + let (mut i, mut calls, mut closed) = (0, 0, false); + let mut skip = left(); + while i < stream.len() { + let (op, len) = (stream[i] & 0xffff, (stream[i] >> 16) as usize); + let w = &stream[i + 1..i + 1 + len]; + i += 1 + len; + calls += 1; + if calls % 64 == 0 { + skip = left(); + } + if op == UPLOAD { + // SAFETY: made by `allocate_upload`, named once. + let block = *unsafe { Box::from_raw(w[0] as *mut Upload) }; + // SAFETY: a list of ours. + let uploads = unsafe { &(*list.cast::()).uploads }; + held(uploads).push(block); + continue; + } + if skip && op != CLOSE && op != RESET { + continue; + } + if run_plain(op, list, w) { + continue; + } + let n = w.first().map_or(0, |&n| n as usize); + match op { + RESET => { + // (Only whether there is an allocator is looked at.) + let allocator: This = std::ptr::NonNull::::dangling().as_ptr().cast_const().cast(); + super::reset(list, allocator, w[0] as This); + } + CLOSE => { + super::close(list); + closed = true; + } + BARRIER => { + super::barrier(list, n as u32, at_or(w, 1)); + } + BARRIER_GROUPS => super::barrier_groups(list, n as u32, std::ptr::null()), + VIEWPORTS => state::viewports(list, n as u32, at(w, 1, n)), + SCISSORS => state::scissors(list, n as u32, at(w, 1, n)), + BLEND_FACTOR => state::blend_factor(list, at(w, 1, n)), + HEAPS => state::heaps(list, n as u32, at(w, 1, n)), + COMPUTE_CONSTANTS => { + state::compute_constants(list, w[0] as u32, w[1] as u32, at(w, 2, w[1] as usize), (w[0] >> 32) as u32) + } + GRAPHICS_CONSTANTS => { + state::graphics_constants(list, w[0] as u32, w[1] as u32, at(w, 2, w[1] as usize), (w[0] >> 32) as u32) + } + INDEX_BUFFER => state::index_buffer(list, at(w, 1, n)), + VERTEX_BUFFERS => { + let views = if w[1] != 0 { at_or(w, 2) } else { std::ptr::null() }; + state::vertex_buffers(list, w[0] as u32, (w[0] >> 32) as u32, views); + } + TARGETS => { + let mut handles = [0u64; 8]; + let place = |k: usize| w[3 + k * RECORD..].as_ptr() as u64; + for (k, h) in handles.iter_mut().enumerate().take(n) { + if w[1] >> k & 1 != 0 { + *h = place(k); + } + } + let depth = if w[2] != 0 { place(n) } else { 0 }; + state::targets(list, n as u32, handles.as_ptr(), 0, &depth); + } + CLEAR_RTV => { + super::clear_rtv( + list, + w[1..].as_ptr() as u64, + at_or(w, 1 + RECORD), + n as u32, + at(w, 1 + RECORD + 2, n), + ); + } + CLEAR_DSV => { + super::clear_dsv( + list, + w[3..].as_ptr() as u64, + w[1] as u32, + Word::unword(w[2]), + (w[1] >> 32) as u8, + n as u32, + at(w, 3 + RECORD, n), + ); + } + COPY_TEXTURE => { + let step = size_of::().div_ceil(8); + let boxed = (w[1] >> 32) as usize; + super::copy_texture_region( + list, + at_or(w, 2), + w[0] as u32, + (w[0] >> 32) as u32, + w[1] as u32, + at_or(w, 2 + step), + at(w, 2 + 2 * step, boxed), + ); + } + _ => {} + } + } + (calls, closed) +} + +/// The table of a list whose calls are written down: that of the others with +/// the methods that record replaced (slots as in [`super::METHODS`]). +pub(super) const fn table(newer: bool) -> [*const (); 160] { + let mut f = super::list_table(newer); + let mut i = 0; + while i < MINE.len() { + let (slot, method) = MINE[i]; + f[if newer { newer_slot(slot) } else { slot }] = method; + i += 1; + } + if newer { + f[146] = barrier_groups as *const (); + f[148] = allocate_upload as *const (); + } + f +} + +const MINE: [(usize, *const ()); 50] = [ + (9, list_close as *const ()), + (10, list_reset as *const ()), + (12, draw as *const ()), + (13, draw_indexed as *const ()), + (14, dispatch as *const ()), + (15, copy_buffer_region as *const ()), + (16, copy_texture_region as *const ()), + (17, copy_resource as *const ()), + (19, resolve as *const ()), + (20, topology as *const ()), + (21, viewports as *const ()), + (22, scissors as *const ()), + (23, blend_factor as *const ()), + (24, stencil_ref as *const ()), + (25, set_pipeline as *const ()), + (26, barrier as *const ()), + (30, resolve_queries as *const ()), + (32, heaps as *const ()), + (35, compute_table as *const ()), + (36, graphics_table as *const ()), + (37, compute_constant as *const ()), + (38, graphics_constant as *const ()), + (39, compute_constants as *const ()), + (40, graphics_constants as *const ()), + (41, compute_view as *const ()), + (42, graphics_view as *const ()), + (43, compute_view as *const ()), + (44, graphics_view as *const ()), + (45, compute_view as *const ()), + (46, graphics_view as *const ()), + (47, index_buffer as *const ()), + (48, vertex_buffers as *const ()), + (50, targets as *const ()), + (51, clear_dsv as *const ()), + (52, clear_rtv as *const ()), + (59, execute_indirect as *const ()), + (60, copy_memory as *const ()), + (68, depth_bounds as *const ()), + (69, write32 as *const ()), + (70, write64 as *const ()), + (71, wait_value as *const ()), + (72, wait_value64 as *const ()), + (78, dispatch_x as *const ()), + (83, copy_texture_region_x as *const ()), + (84, copy_resource_x as *const ()), + (98, dispatch_mesh_x as *const ()), + (127, dispatch_mesh as *const ()), + (129, sync as *const ()), + (130, sync as *const ()), + (137, list_reset_x as *const ()), +]; + +#[cfg(test)] +mod tests { + use super::*; + + /// Every method of the tables that records is written down too: what is + /// left in both tables alike records nothing. + #[test] + fn every_recording_method_is_written_down() { + const NOTHING: [usize; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 28, 29, 33, 34, 73, 99, 124]; + let mine: Vec = MINE.iter().map(|m| m.0).collect(); + for (slot, _) in METHODS.iter().chain(MORE_METHODS.iter()) { + assert!(mine.contains(slot) != NOTHING.contains(slot), "slot {slot}"); + } + for &slot in &mine { + assert!( + METHODS.iter().chain(MORE_METHODS.iter()).any(|m| m.0 == slot), + "slot {slot} is written down and not there" + ); + } + } + + /// Arguments come out of a stream as they went in. + #[test] + fn a_stream_gives_back_what_was_written() { + let mut f = Front::default(); + f.recording = Some(Arc::new(Recording::new(0))); + f.put(Plain::DRAW_INDEXED as u64, &[3, 1, 0, Word::word(-7i32), 5]); + let viewport = [1.0f32, 2.0, 3.0, 4.0, 0.0, 1.0]; + f.put_with(VIEWPORTS, &[1], &[bytes_of(viewport.as_ptr(), 6)]); + f.put_with(COMPUTE_CONSTANTS, &[4 | 2 << 32, 3], &[bytes_of([7u32, 8, 9].as_ptr(), 3)]); + let s = std::mem::take(&mut f.words); + assert_eq!((s[0] & 0xffff, s[0] >> 16), (Plain::DRAW_INDEXED as u64, 5)); + assert_eq!(::unword(s[4]), -7); + let w = &s[6..]; + assert_eq!((w[0] & 0xffff, w[0] >> 16, w[1]), (VIEWPORTS, 4, 1)); + // SAFETY: what was written just now. + assert_eq!(unsafe { *at::<[f32; 6]>(&w[1..], 1, 1) }, viewport); + let w = &w[5..]; + assert_eq!((w[0] & 0xffff, w[0] >> 16), (COMPUTE_CONSTANTS, 4)); + // SAFETY: as above. + assert_eq!(unsafe { *at::<[u32; 3]>(&w[1..], 2, 3) }, [7, 8, 9]); + assert!(at::(&w[1..], 2, 0).is_null()); + } + + /// A view's record is the one at the call, also if its slot is written + /// again right after. + #[test] + fn a_target_is_the_one_at_the_call() { + let mut slot = [0x5256_5476u64, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; + let kept = record(slot.as_ptr() as u64); + slot[1] = 99; + assert_eq!(kept[1], 2); + assert_eq!(record(0), [0; RECORD]); + } +} diff --git a/crates/victory-gpu/src/queue.rs b/crates/victory-gpu/src/queue.rs index 90ec3d8..b4cf177 100644 --- a/crates/victory-gpu/src/queue.rs +++ b/crates/victory-gpu/src/queue.rs @@ -442,6 +442,24 @@ pub(crate) fn before_sync() { } } +/// Whether submissions go into the line of a thread of ours ([`behind`]). +#[cfg(windows)] +pub(crate) fn line_on() -> bool { + behind::on() +} + +/// Lists in the line may have been translated ([`crate::list::thin`]). +#[cfg(windows)] +pub(crate) fn line_moved() { + behind::wake(); +} + +/// This thread does work the line waits for: it does not wait for the line. +#[cfg(windows)] +pub(crate) fn no_drain_here() { + behind::no_drain_here(); +} + /// Submissions the title made that the host has not begun ([`behind`]). #[cfg(windows)] pub(crate) fn queued() -> u64 { @@ -452,6 +470,7 @@ pub(crate) fn queued() -> u64 { #[cfg(windows)] pub(crate) fn behind_line(frames: f64) { if behind::on() { + crate::list::thin::line(frames); behind::line(frames); } } @@ -921,6 +940,9 @@ extern "system" fn present( crate::resource::host_copy(resource).map(|copy| { let state = crate::resource::host_transition(resource, COPY_SOURCE).map_or(COPY_SOURCE, |(_, before)| before); crate::resource::host_transition(resource, state); + // (Lists not translated yet leave it in no state yet: the + // one it is in when its turn in the line comes.) + let state = if crate::list::thin::wanted() { u32::MAX } else { state }; (resource as usize, copy, state) }) } else { diff --git a/crates/victory-gpu/src/queue/behind.rs b/crates/victory-gpu/src/queue/behind.rs index b2f3de7..b7152de 100644 --- a/crates/victory-gpu/src/queue/behind.rs +++ b/crates/victory-gpu/src/queue/behind.rs @@ -61,6 +61,19 @@ pub(super) trait Work: Send { fn shows(&self) -> bool; /// Whether it is a submission. fn runs(&self) -> bool; + /// Whether it stands for other work, which takes its place in the line + /// once it is free to go ([`Work::unfold`]). + fn folded(&self) -> bool { + false + } + fn unfold(self) -> Vec + where + Self: Sized, + { + vec![self] + } + /// Waits until a folded one is free to go. + fn settle(_: &Self::Ask) {} /// Does it. `forced`: its wait was not met. fn go(self, forced: bool); } @@ -166,6 +179,9 @@ impl Lane { /// Waits until everything in the line that is free to run has been done /// (what comes in after this call may not be). pub(super) fn drain(&self) { + let folded: Vec = + self.lock().items.iter().filter(|e| e.what.folded()).filter_map(|e| e.what.ask()).collect(); + folded.iter().for_each(W::settle); let mut state = self.lock(); if state.items.is_empty() && state.running.is_none() { return; @@ -234,11 +250,14 @@ impl Lane { fn step(&self, limit: u64) -> bool { // Each queue's first entry, oldest first. let mut heads: Vec<(u64, usize, Option, bool)> = Vec::new(); + // Whether one of them stands for others not known yet. + let mut folded = false; { let state = self.lock(); for e in state.items.iter().take_while(|e| e.number < limit) { if !heads.iter().any(|h| h.1 == e.queue) { heads.push((e.number, e.queue, e.what.ask(), e.what.shows())); + folded |= e.what.folded(); } } } @@ -255,7 +274,11 @@ impl Lane { None => { // Nothing is free to run. With a present in the line the // oldest goes on regardless: a wait is not kept across - // frames (`super::present`). + // frames (`super::present`). (Not while there is work whose + // parts are not known: it may be what is waited for.) + if folded { + return false; + } let state = self.lock(); if !super::forces() || !state.items.iter().any(|e| e.number < limit && e.what.shows()) { return false; @@ -275,6 +298,24 @@ impl Lane { entry }; let (shows, runs) = (entry.what.shows(), entry.what.runs()); + if entry.what.folded() { + // Its parts take its place: behind everything before it, before + // everything after it (they share its number; the first of them + // is found first). + let (number, queue) = (entry.number, entry.queue); + let parts = entry.what.unfold(); + let mut state = self.lock(); + state.running = None; + state.runs -= usize::from(runs); + state.runs += parts.iter().filter(|p| p.runs()).count(); + let place = state.items.iter().position(|e| e.number > number).unwrap_or(state.items.len()); + for (i, what) in parts.into_iter().enumerate() { + state.items.insert(place + i, Entry { number, queue, what }); + } + drop(state); + self.moved.notify_all(); + return true; + } entry.what.go(forced); { let mut state = self.lock(); @@ -287,6 +328,8 @@ impl Lane { } } +type Recordings = std::sync::Arc<[std::sync::Arc]>; + /// What the title's queues put in the line. enum Job { /// The queue waits for the fence (its address) to reach the value. @@ -296,6 +339,9 @@ enum Job { /// A submission, or a part of one: the lists' host sides and the order /// they run in. Run(usize, Vec, Vec), + /// A submission whose lists are being translated ([`crate::list::thin`]): + /// the parts of it are known when they are. + Lists(usize, Recordings), /// A signal of the queue's. Signal(usize, u64), /// A frame is shown: the texture and the state its lists leave it in. @@ -310,6 +356,7 @@ unsafe impl Send for Job {} enum Ask { Wait(usize, u64), Value(crate::list::ValueWait), + Lists(Recordings), } impl Work for Job { @@ -319,14 +366,40 @@ impl Work for Job { match self { &Job::Wait(fence, value) => Some(Ask::Wait(fence, value)), Job::Value(wait) => Some(Ask::Value(*wait)), + Job::Lists(_, lists) => Some(Ask::Lists(lists.clone())), _ => None, } } + fn folded(&self) -> bool { + matches!(self, Job::Lists(..)) + } + + fn unfold(self) -> Vec { + let Job::Lists(queue, recordings) = self else { + return vec![self]; + }; + let lists: Vec = recordings.iter().map(|r| r.back()).collect(); + let jobs = jobs_of(queue as This, &lists); + // It stood for one submission not begun. + let runs = jobs.iter().filter(|j| j.runs()).count() as u64; + QUEUED.fetch_add(runs, Relaxed); + QUEUED.fetch_sub(1, Relaxed); + STATS[RUNS].fetch_add(runs, Relaxed); + jobs + } + + fn settle(ask: &Ask) { + if let Ask::Lists(recordings) = ask { + recordings.iter().for_each(|r| r.wait()); + } + } + fn met(ask: &Ask) -> bool { match ask { &Ask::Wait(fence, value) => crate::fence::completed(fence as This) >= value, Ask::Value(wait) => super::value_met(wait), + Ask::Lists(recordings) => recordings.iter().all(|r| r.done()), } } @@ -335,7 +408,7 @@ impl Work for Job { } fn runs(&self) -> bool { - matches!(self, Job::Run(..)) + matches!(self, Job::Run(..) | Job::Lists(..)) } fn go(self, forced: bool) { @@ -354,7 +427,8 @@ impl Work for Job { ONCE.call_once(|| tracing::warn!("a queue still stood at a wait at a present: it was not met")); } match self { - Job::Wait(..) | Job::Value(..) => {} + // (A folded one is unfolded, never done.) + Job::Wait(..) | Job::Value(..) | Job::Lists(..) => {} Job::Run(queue, lists, order) => { let _ = queue; #[cfg(feature = "diagnostics")] @@ -372,6 +446,9 @@ impl Work for Job { } Job::Present(texture, shown) => { if let Some((texture, state)) = texture { + // (Where its lists were not translated when the title + // presented: the state they left it in.) + let state = if state == u32::MAX { texture.state.load(Relaxed) } else { state }; super::show(&texture.texture, state, shown); } QUEUED.fetch_sub(1, Relaxed); @@ -438,7 +515,9 @@ pub(super) fn on() -> bool { return on && started(); } static ON: OnceLock = OnceLock::new(); - *ON.get_or_init(|| std::env::var_os("VICTORY_ASYNC_SUBMIT").is_some_and(|v| v == "1") && started()) + *ON.get_or_init(|| { + (std::env::var_os("VICTORY_ASYNC_SUBMIT").is_some_and(|v| v == "1") || crate::list::thin::wanted()) && started() + }) } /// The line is about to be left for the way without it: everything in it is @@ -498,6 +577,11 @@ pub(super) fn wake() { LANE.wake(); } +/// This thread does work the line waits for. +pub(super) fn no_drain_here() { + OURS.set(true); +} + /// Before something that has to see the submissions made so far sent: they /// are, as far as they are free to run. Not on our own thread, which is /// inside one of them. @@ -513,6 +597,41 @@ pub(super) fn drain() { /// A submission of `lists` to queue `this`. pub(super) fn submit(this: This, lists: &[This]) { + let queue = this as usize; + if crate::list::thin::on() { + let recordings = crate::list::thin::submitted(lists); + if recordings.is_empty() { + return; + } + let before = QUEUED.fetch_add(1, Relaxed); + STATS[DEPTH].fetch_add(before, Relaxed); + STATS[DEEPEST].fetch_max(before + 1, Relaxed); + LANE.push(queue, [Job::Lists(queue, recordings)]); + room(before + 1); + return; + } + let jobs = jobs_of(this, lists); + let runs = jobs.iter().filter(|j| j.runs()).count() as u64; + let before = QUEUED.fetch_add(runs, Relaxed); + STATS[RUNS].fetch_add(runs, Relaxed); + STATS[DEPTH].fetch_add(before, Relaxed); + STATS[DEEPEST].fetch_max(before + runs, Relaxed); + LANE.push(queue, jobs); + room(before + runs); +} + +/// Waits for room in the line, with `queued` submissions in it. +fn room(queued: u64) { + if queued > runs_most() as u64 { + let began = Instant::now(); + let _waiting = crate::timeline::span("a submission waits for room in the line"); + LANE.wait_runs(runs_most()); + STATS[ROOM].fetch_add(began.elapsed().as_micros() as u64, Relaxed); + } +} + +/// What a submission of `lists` is in the line. +fn jobs_of(this: This, lists: &[This]) -> Vec { let queue = this as usize; // In one piece up to the first list that waits for a memory value, as // `super::submit_cut` runs them; the rest part by part. @@ -542,18 +661,7 @@ pub(super) fn submit(this: This, lists: &[This]) { jobs.push(Job::Run(queue, taken, order)); } } - let runs = jobs.iter().filter(|j| j.runs()).count() as u64; - let before = QUEUED.fetch_add(runs, Relaxed); - STATS[RUNS].fetch_add(runs, Relaxed); - STATS[DEPTH].fetch_add(before, Relaxed); - STATS[DEEPEST].fetch_max(before + runs, Relaxed); - LANE.push(queue, jobs); - if before + runs > runs_most() as u64 { - let began = Instant::now(); - let _waiting = crate::timeline::span("a submission waits for room in the line"); - LANE.wait_runs(runs_most()); - STATS[ROOM].fetch_add(began.elapsed().as_micros() as u64, Relaxed); - } + jobs } /// A signal of queue `this`: behind what it has in the line; if that is