| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912 |
- //! 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<bool> = 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<bool> = 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<Arc<Recording>>,
- words: Vec<u64>,
- 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<Vec<Vec<u64>>> = Mutex::new(Vec::new());
- /// Recordings with words to translate.
- static READY: Mutex<VecDeque<Arc<Recording>>> = Mutex::new(VecDeque::new());
- static WORK: Condvar = Condvar::new();
- /// Lists of ours nobody uses, closed.
- static BACKS: Mutex<Vec<usize>> = 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<T>(m: &Mutex<T>) -> 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<Inner>,
- /// Translated to its end.
- done: AtomicBool,
- moved: Condvar,
- abandoned: AtomicBool,
- }
- #[derive(Default)]
- struct Inner {
- chunks: VecDeque<Vec<u64>>,
- /// Streams translated (commands may name places in them until the end).
- kept: Vec<Vec<u64>>,
- /// 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<Vec<u64>>) {
- 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_many_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<Recording>]> {
- 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::<List>()).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<f64> = 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::<List>()).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::<T>()) }
- }
- /// Where the things are in a stream (null for none).
- fn at<T>(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<T>(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::<RtvRecord>())
- };
- }
- words
- }
- const RECORD: usize = size_of::<RtvRecord>().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::<List>()).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::<List>()).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::<u64>::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::<CopyLocation>().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<usize> = 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!(<i32 as Word>::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::<u32>(&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]);
- }
- }
|