//! Command allocators and command lists. //! //! The title's inlined code writes packets into a list only while its put //! pointer is below a limit, and calls the list's method otherwise. Our //! lists keep the pointer and every limit at zero, so every command reaches //! the table as a call. //! //! Each list records onto a host command list as its methods are called //! (decision 0007); draw state lives in `crate::draw`. Methods not //! written yet log once with their caller, so a run lists what to do next. #![allow(clippy::not_unsafe_ptr_arg_deref)] use std::mem::offset_of; use crate::object::{ self, ALLOCATOR, E_INVALIDARG, HRESULT, Iid, LIST, Object, S_OK, Table, This, guid, }; use crate::queue::Written; use crate::resource::PAGEABLE_IIDS; const ALLOCATOR_IIDS: [Iid; 4] = [ guid( 0x6102dee4, 0xaf59, 0x4b09, [0xb9, 0x99, 0xb4, 0x4d, 0x73, 0xf0, 0x9b, 0x24], ), PAGEABLE_IIDS[0], PAGEABLE_IIDS[1], PAGEABLE_IIDS[2], ]; /// The list interfaces up to `ID3D12GraphicsCommandList6` (the table has /// all their slots): those whose ids are the same as on PC, the console's /// own List5 and List6 (the title asks for List6), then the /// device child's. const LIST_IIDS: [Iid; 10] = [ guid( 0x7116d91c, 0xe7e4, 0x47ce, [0xb8, 0xc6, 0xec, 0x81, 0x68, 0xf4, 0x37, 0xe5], ), guid( 0x5b160d0f, 0xac1b, 0x4185, [0x8b, 0xa8, 0xb3, 0xae, 0x42, 0xa5, 0xa4, 0x55], ), guid( 0x553103fb, 0x1fe7, 0x4557, [0xbb, 0x38, 0x94, 0x6d, 0x7d, 0x0e, 0x7c, 0xa7], ), guid( 0x38c3e585, 0xff17, 0x412c, [0x91, 0x50, 0x4f, 0xc6, 0xf9, 0xd7, 0x2a, 0x28], ), guid( 0x6fda83a7, 0xb84c, 0x4e38, [0x9a, 0xc8, 0xc7, 0xbd, 0x22, 0x01, 0x6b, 0x3d], ), guid( 0x8754318e, 0xd3a9, 0x4541, [0x98, 0xcf, 0x64, 0x5b, 0x50, 0xdc, 0x48, 0x74], ), guid( 0x55050859, 0x4024, 0x474c, [0x98, 0xcf, 0x64, 0x5b, 0x51, 0xdc, 0x48, 0x74], ), guid( 0xc3827890, 0x4024, 0x474c, [0x98, 0xcf, 0x64, 0x5b, 0x51, 0xdc, 0x48, 0x74], ), PAGEABLE_IIDS[1], PAGEABLE_IIDS[2], ]; #[repr(C)] struct Allocator { object: Object, kind: u32, } /// The layout the title's inlined code reads. #[repr(C)] struct List { object: Object, kind: u32, /// The put pointer, then the plain, draw, API and unbiased API limits: /// all zero, so every inline check fails. put: [u64; 5], reserved: [u32; 64], /// State the inline code keeps for building packets; unused by us. graphics_root_headers: [u32; 128], compute_root_headers: [u32; 128], pipeline_shadow: [u32; 8], /// Newer titles' lists are larger: the title's state flush /// (title+0x32786) copies a pipeline's 32 bytes to +0x544..+0x564 and /// tests +0x548, so its layout runs past 0x560. Its size is unknown; /// this keeps our fields clear of whatever the title writes there. newer: [u32; 256], /// Ours, after the console layout: memory handed out by /// [`allocate_upload`], kept until the list is reset; the host list the /// calls record into (decision 0007), and the draw state the title set. uploads: std::sync::Mutex>>, #[cfg(windows)] host: Option, #[cfg(windows)] state: std::sync::Mutex, /// The parts of the recording before `host`, in order, each closed: the /// list is cut at its waits for memory values ([`cut`]). #[cfg(windows)] earlier: Vec, /// The waits recorded before the first work of `host`: the queue stands /// at them before it runs `host`. #[cfg(windows)] gate: Vec, } /// One part of a title list cut at its waits: the waits the queue stands /// at, then the host list that runs once they are met. #[cfg(windows)] struct Part { waits: Vec, host: crate::host::HostList, } const _: () = assert!(offset_of!(List, put) == 0x18); const _: () = assert!(offset_of!(List, pipeline_shadow) + 32 == 0x560); /// Runs `f` with the host list and draw state of a title list, if there /// is a host GPU. #[cfg(windows)] fn with_state(this: This, f: impl FnOnce(&crate::host::HostList, &mut crate::draw::State)) { let Some(list) = host_list(this) else { return }; #[cfg(feature = "diagnostics")] let started = std::time::Instant::now(); // SAFETY: the title passes one of our lists. let state = unsafe { &(*this.cast::()).state }; f(list, &mut state.lock().unwrap_or_else(|e| e.into_inner())); #[cfg(feature = "diagnostics")] crate::copystat::recorded(started); } /// The submission of `lists` has run: each gives its descriptor tables' /// slots back ([`crate::draw::State::tables_done`]). A list cut at waits /// keeps them: its state goes with its last part. #[cfg(windows)] pub(crate) fn tables_done(lists: &[This]) { for &this in lists { if this.is_null() { continue; } // SAFETY: the title passes our lists. let list = unsafe { &*this.cast::() }; if list.earlier.is_empty() { list.state.lock().unwrap_or_else(|e| e.into_inner()).tables_done(); } } } /// The host list behind a title list, if there is a host GPU. #[cfg(windows)] pub(crate) fn host_list<'a>(this: This) -> Option<&'a crate::host::HostList> { if this.is_null() { return None; } // SAFETY: the title passes one of our lists. unsafe { (*this.cast::()).host.as_ref() } } /// The host side of a submitted list, taken from it: a queue that waits /// for a fence runs it later, and the title may reset the list meanwhile. /// With it go the upload memory and the dynamic descriptor slots its /// commands name, kept until it has run. #[cfg(windows)] pub(crate) struct Detached { host: crate::host::HostList, _uploads: Vec>, _state: crate::draw::State, } #[cfg(windows)] impl Detached { pub(crate) fn host(&self) -> &crate::host::HostList { &self.host } } /// A host list that has run and waits to be used again, and the bytes of /// upload memory it keeps. #[cfg(windows)] struct Idle(crate::host::HostList, u64); // SAFETY: D3D12 objects are free-threaded, and an idle list is touched by // nobody until it is taken out again. #[cfg(windows)] unsafe impl Send for Idle {} /// Host lists that have run, reset and ready to record: the host side of /// a list a queue held, or of a part of a list cut at a wait, is taken /// from the title's list at its submission, and the title's list needs /// another. Each was made new and thrown away after its run: a hundred a /// frame in gameplay, each two command allocators and two lists, and for /// four in ten a budget and an argument buffer (run 61: "104 host lists /// made 21.2 ms" a frame, and the graphics kernel's time to free them). #[cfg(windows)] static IDLE: std::sync::Mutex<(Vec, u64)> = std::sync::Mutex::new((Vec::new(), 0)); /// The most host lists kept idle, and the most upload memory they may keep /// between them (a list keeps scratch chunks over a reset only while lists /// do not share them, `VICTORY_SCRATCH_POOL=0`), scaled from CPU cores and /// total system RAM. #[cfg(windows)] fn idle_most(total_ram_bytes: Option, cores: usize) -> (usize, u64) { const GIB: u64 = 1 << 30; let lists = cores.saturating_mul(16).clamp(64, 256); let bytes = match total_ram_bytes { Some(ram) if ram <= 16 * GIB => 128 << 20, // 32 GB Windows systems typically report ~31.7–31.9 GiB in // `ullTotalPhys` after firmware/hardware reservations. Some(ram) if ram >= 30 * GIB => 512 << 20, _ => 256 << 20, }; (lists, bytes) } #[cfg(windows)] fn idle_limits() -> (usize, u64) { static LIMITS: std::sync::OnceLock<(usize, u64)> = std::sync::OnceLock::new(); *LIMITS.get_or_init(|| { let cores = std::thread::available_parallelism().map_or(8, usize::from); idle_most(crate::total_system_ram_bytes(), cores) }) } /// Whether host lists that have run are used again (on by default; /// `VICTORY_LIST_POOL=0`: each is made new and thrown away, as before). #[cfg(windows)] fn pools() -> bool { static ON: std::sync::OnceLock = std::sync::OnceLock::new(); *ON.get_or_init(|| std::env::var_os("VICTORY_LIST_POOL").is_none_or(|v| v != "0")) } /// A host list open for recording: one that has run, as a title's list is /// after its reset, or a new one. #[cfg(windows)] fn fresh_list() -> Option { let idle = { let mut idle = IDLE.lock().unwrap_or_else(|e| e.into_inner()); let taken = idle.0.pop(); if let Some(Idle(_, bytes)) = &taken { idle.1 -= bytes; } taken }; match idle { Some(Idle(list, _)) => Some(list), None => crate::host::host().and_then(|h| h.new_list().ok()), } } /// The host sides of lists whose submission has run (submissions are /// waited for; `ran` names the ones it ran, by their place in `lists`): /// what they held is let go, as before, and the host lists are reset and /// kept for [`fresh_list`]. #[cfg(windows)] pub(crate) fn recycle(lists: Vec, ran: &[usize]) { if !pools() { return; } let (max_lists, max_bytes) = idle_limits(); for (i, Detached { host, _uploads, _state }) in lists.into_iter().enumerate() { // The descriptor slots and the upload memory its commands named. drop(_state); drop(_uploads); if !ran.contains(&i) || IDLE.lock().unwrap_or_else(|e| e.into_inner()).0.len() >= max_lists { continue; } // As the title's reset does: what the list kept for its commands // goes, its allocators stay. host.reset(); let bytes = host.scratch_kept(); let mut idle = IDLE.lock().unwrap_or_else(|e| e.into_inner()); if idle.0.len() < max_lists && idle.1 + bytes <= max_bytes { idle.0.push(Idle(host, bytes)); idle.1 += bytes; } } } /// A wait for a memory value recorded into list `this`, with waits cut /// into lists (`crate::queue::cuts`). On the console the queue runs the /// list up to the wait, stands there until the value is in memory, and /// runs on; the list's bottom-of-pipe writes before the wait are done by /// then, and other queues may be waiting for exactly those. So the /// recording is cut here: the host list so far is closed and kept as a /// part of its own ([`Part`]), the title's calls record into a new host /// list from here on, and the wait stands before that one. A submission /// then gives the queue the parts in order ([`detach_parts`]). /// /// A wait before any work (the first call after a reset, or a wait right /// after another) cuts nothing: it joins the waits before the current /// host list. /// shortcut: a bottom-of-pipe write is done when its part has run, not at /// its place in the part: a queue waiting for it starts later than on the /// console, never earlier, and nothing can wait for itself that way (a /// part has no wait after its first work). #[cfg(windows)] fn cut(this: This, wait: ValueWait) { if this.is_null() { return; } // SAFETY: the title passes one of our lists; a list is recorded by one // thread at a time and is not submitted while it records. let list = unsafe { &mut *this.cast_mut().cast::() }; let Some(current) = list.host.as_ref() else { return; }; if current.untouched() { list.gate.push(wait); return; } let Some(fresh) = fresh_list() else { // shortcut: without a new host list the wait stands before the part // it was recorded in. static ONCE: std::sync::Once = std::sync::Once::new(); ONCE.call_once(|| tracing::warn!("no host list for the part after a wait: the wait moves to the part's start")); list.gate.push(wait); return; }; current.close(); static CUTS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); if CUTS.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 { tracing::info!(parts = list.earlier.len() + 2, "a list is cut at a wait for a memory value"); } if let Some(done) = list.host.replace(fresh) { let waits = std::mem::replace(&mut list.gate, vec![wait]); list.earlier.push(Part { waits, host: done }); } // What the title set so far, on the new host list. if let Some(host) = list.host.as_ref() { list.state .lock() .unwrap_or_else(|e| e.into_inner()) .replay(host); } } /// Whether list `this` recorded a wait for a memory value (with waits cut /// into lists). #[cfg(windows)] pub(crate) fn cut_list(this: This) -> bool { if this.is_null() { return false; } // SAFETY: one of our lists, closed and submitted: nothing records // into it. let list = unsafe { &*this.cast::() }; !list.earlier.is_empty() || !list.gate.is_empty() } /// What a queue is given for submitted lists that were cut at their waits: /// the waits it stands at, the host lists it runs once they are met, and /// the order those run in. #[cfg(windows)] pub(crate) type Parts = Vec<(Vec, Vec, Vec)>; /// Takes the host sides of the submitted `lists`, part by part ([`cut`]), /// and leaves each list a new, closed host list for its next reset, as /// [`detach`] does. What comes back is in the order the queue runs it: a /// new entry starts at every part that has waits; host lists without /// waits join the entry before them. The list's upload memory and draw /// state (its dynamic descriptor slots) go with its last part, which runs /// last. /// shortcut: a list the submission names a second time runs again only if /// it has one part and that part is in the entry being filled; otherwise /// the second naming is dropped (logged once). #[cfg(windows)] pub(crate) fn detach_parts(lists: &[This]) -> Parts { let mut parts: Parts = vec![(Vec::new(), Vec::new(), Vec::new())]; // The lists taken: where its one host list is (entry, place), or none // for a list of several parts. let mut seen: Vec<(This, Option<(usize, usize)>)> = Vec::new(); for &this in lists { if this.is_null() { continue; } if let Some(&(_, at)) = seen.iter().find(|(t, _)| *t == this) { match at { Some((entry, place)) if entry + 1 == parts.len() => parts[entry].2.push(place), _ => { static ONCE: std::sync::Once = std::sync::Once::new(); ONCE.call_once(|| { tracing::warn!("a list named twice by a submission that waits for memory values runs once") }); } } continue; } let Some(fresh) = fresh_list() else { continue; }; // A new host list is open; the title's next call on its list is a // reset, which wants it closed. fresh.close(); // SAFETY: one of our lists, closed and submitted: nothing records // into it until the title resets it. let list = unsafe { &mut *this.cast_mut().cast::() }; let Some(host) = list.host.replace(fresh) else { continue; }; let earlier = std::mem::take(&mut list.earlier); let gate = std::mem::take(&mut list.gate); let uploads = std::mem::take(&mut *list.uploads.lock().unwrap_or_else(|e| e.into_inner())); let state = std::mem::take(&mut *list.state.lock().unwrap_or_else(|e| e.into_inner())); let single = earlier.is_empty() && gate.is_empty(); let mut pieces: Vec<(Vec, Detached)> = earlier .into_iter() .map(|part| { ( part.waits, Detached { host: part.host, _uploads: Vec::new(), _state: Default::default(), }, ) }) .collect(); pieces.push(( gate, Detached { host, _uploads: uploads, _state: state, }, )); let mut last = None; for (waits, detached) in pieces { if !waits.is_empty() { parts.push((waits, Vec::new(), Vec::new())); } let entry = parts.len() - 1; let (_, taken, order) = &mut parts[entry]; let place = taken.len(); order.push(place); taken.push(detached); last = Some((entry, place)); } seen.push((this, if single { last } else { None })); } // (The first entry is empty if the first list begins with a wait.) parts.retain(|(_, taken, _)| !taken.is_empty()); parts } /// Takes the host sides of submitted lists (each once, however often the /// submission names it) and leaves each list a new, closed host list for /// its next reset: what was taken, and which of them run in what order. #[cfg(windows)] pub(crate) fn detach(lists: &[This]) -> (Vec, Vec) { let mut taken: Vec<(This, Detached)> = Vec::new(); let mut order = Vec::new(); for &this in lists { if this.is_null() { continue; } if let Some(i) = taken.iter().position(|(t, _)| *t == this) { order.push(i); continue; } let Some(fresh) = fresh_list() else { continue; }; // A new host list is open; the title's next call on its list is a // reset, which wants it closed. fresh.close(); let list = this.cast_mut().cast::(); // SAFETY: one of our lists, closed and submitted: nothing records // into it until the title resets it. let (host, uploads, state) = unsafe { let Some(host) = (*list).host.replace(fresh) else { continue; }; let uploads = std::mem::take(&mut *(*list).uploads.lock().unwrap_or_else(|e| e.into_inner())); let state = std::mem::take(&mut *(*list).state.lock().unwrap_or_else(|e| e.into_inner())); (host, uploads, state) }; order.push(taken.len()); taken.push(( this, Detached { host, _uploads: uploads, _state: state, }, )); } (taken.into_iter().map(|(_, d)| d).collect(), order) } pub(crate) extern "system" fn create_allocator( _: This, kind: u32, iid: *const Iid, out: *mut This, ) -> HRESULT { let allocator = Box::new(Allocator { object: Object::new(&ALLOCATOR_FUNCTIONS), kind, }); object::hand_out(ALLOCATOR, allocator, iid, &ALLOCATOR_IIDS, out) } /// Lists start open, as in D3D12. pub(crate) extern "system" fn create_list( _: This, _nodes: u32, kind: u32, allocator: This, pipeline: This, iid: *const Iid, out: *mut This, ) -> HRESULT { new_list(kind, allocator, pipeline, iid, out) } /// The console's list creation: type, flags, node mask and barrier batch /// size in a description. #[repr(C)] #[derive(Debug)] pub(crate) struct ListDesc { kind: u32, flags: u32, nodes: u32, barrier_batch: u32, } pub(crate) extern "system" fn create_list_x( _: This, desc: *const ListDesc, allocator: This, pipeline: This, iid: *const Iid, out: *mut This, ) -> HRESULT { // SAFETY: the title passes a description (null is tolerated). let Some(desc) = (unsafe { desc.as_ref() }) else { return E_INVALIDARG; }; tracing::debug!(?desc, "console command list"); new_list(desc.kind, allocator, pipeline, iid, out) } /// `pipeline` is the list's initial pipeline (null: none), bound until the /// title sets another, as after a reset. fn new_list( kind: u32, allocator: This, pipeline: This, iid: *const Iid, out: *mut This, ) -> HRESULT { if allocator.is_null() { return E_INVALIDARG; } tracing::debug!(kind, "command list"); let list = Box::new(List { object: if crate::device::newer() { Object::new(&NEWER_LIST_FUNCTIONS) } else { Object::new(&LIST_FUNCTIONS) }, kind, put: [0; 5], reserved: [0; 64], graphics_root_headers: [0; 128], compute_root_headers: [0; 128], pipeline_shadow: [0; 8], newer: [0; 256], uploads: Default::default(), #[cfg(windows)] host: crate::host::host().and_then(|h| match h.new_list() { Ok(list) => Some(list), Err(e) => { tracing::warn!(error = %e, "no host command list"); None } }), #[cfg(windows)] state: { let mut state = crate::draw::State::default(); state.set_pipeline(pipeline); std::sync::Mutex::new(state) }, #[cfg(windows)] earlier: Vec::new(), #[cfg(windows)] gate: Vec::new(), }); #[cfg(not(windows))] let _ = pipeline; object::hand_out(LIST, list, iid, &LIST_IIDS, out) } extern "system" fn allocator_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT { let found = object::is(iid, &ALLOCATOR_IIDS).then_some(this); object::answer(ALLOCATOR, iid, found, out) } extern "system" fn allocator_release(this: This) -> u32 { object::release_boxed::(this) } /// Nothing is recorded, so there is nothing to free. extern "system" fn allocator_reset(_: This) -> HRESULT { S_OK } extern "system" fn list_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT { object::answer(LIST, iid, object::is(iid, &LIST_IIDS).then_some(this), out) } extern "system" fn list_release(this: This) -> u32 { object::release_boxed::(this) } extern "system" fn list_type(this: This) -> u32 { // SAFETY: `this` is one of our lists. unsafe { (*this.cast::()).kind } } extern "system" fn close(this: This) -> HRESULT { #[cfg(windows)] if let Some(host) = host_list(this) { host.close(); } S_OK } extern "system" fn reset(this: This, allocator: This, pipeline: This) -> HRESULT { if allocator.is_null() { return E_INVALIDARG; } // A wait recorded and never submitted goes with the recording. VALUE_WAITS .lock() .unwrap_or_else(|e| e.into_inner()) .retain(|&(list, _)| list != this as usize); // Every submission of the list has run (submissions are waited for). // SAFETY: the title passes one of our lists. let uploads = unsafe { &(*this.cast::()).uploads }; uploads.lock().unwrap_or_else(|e| e.into_inner()).clear(); // The parts of a recording that was cut and never submitted go with it // (a submission takes the parts it runs). #[cfg(windows)] { // SAFETY: the title passes one of our lists, and neither records // into it nor submits it during its reset. let list = unsafe { &mut *this.cast_mut().cast::() }; list.earlier.clear(); list.gate.clear(); } #[cfg(windows)] with_state(this, |host, state| { host.reset(); state.reset(pipeline); }); #[cfg(not(windows))] let _ = (this, pipeline); S_OK } /// A resource barrier as titles pass it: type, flags, then a resource and /// either a subresource and two states (transition) or a second resource /// (aliasing). The same layout as the host's. #[repr(C)] #[derive(Clone, Copy)] pub(crate) struct Barrier { kind: u32, flags: u32, first: This, second: u64, last: u64, } /// The barrier on host resources, or `None` if it names a resource the host /// GPU does not have (buffers so far). #[cfg(windows)] fn host_barrier(list: &crate::host::HostList, b: &Barrier) -> Option { use windows::core::Interface; let host = |r: This| crate::resource::host_resource(r).map(|h| h.as_raw().cast_const().cast()); match b.kind { 0 => { // A split barrier's first half: its second half transitions. const BEGIN_ONLY: u32 = 1; if b.flags & BEGIN_ONLY != 0 { return None; } // shortcut: whole resources, from the state the list tracked // (`HostList::transition`); subresources are not told apart. let wanted = crate::host::texture_state(b.last as u32); let (texture, before) = list.transition(b.first, wanted)?; Some(Barrier { kind: 0, flags: 0, first: texture.as_raw().cast_const().cast(), second: 0xffff_ffff | (u64::from(before) << 32), last: u64::from(crate::host::legal_state(texture, wanted)), }) } 1 => Some(Barrier { first: if b.first.is_null() { b.first } else { host(b.first)? }, second: if b.second == 0 { 0 } else { host(b.second as This)? as u64 }, ..*b }), 2 => Some(Barrier { first: if b.first.is_null() { b.first } else { // A depth texture's unordered access is its colour twin's // (the host gives the depth texture none): the barrier is // on the texture shaders write. match crate::resource::twin_resource(b.first) { Some(twin) => twin.as_raw().cast_const().cast(), None => host(b.first)?, } }, ..*b }), _ => None, } } /// An unordered access barrier on every resource: earlier shader writes /// finish before later work reads or writes. const UAV_BARRIER: Barrier = Barrier { kind: 2, flags: 0, first: std::ptr::null(), second: 0, last: 0, }; /// Returns the console's barrier "actions" (none). extern "system" fn barrier(this: This, count: u32, barriers: *const Barrier) -> u32 { #[cfg(windows)] if let Some(list) = host_list(this) && !barriers.is_null() { // SAFETY: the title passes `count` barriers. let barriers = unsafe { std::slice::from_raw_parts(barriers, count as usize) }; let mut host: Vec = barriers .iter() .filter_map(|b| host_barrier(list, b)) .collect(); // Buffers have host copies, not resources the barrier can name: // a barrier on one waits for every shader write before it. if barriers.iter().any(|b| { b.kind != 1 && !b.first.is_null() && crate::resource::buffer_bytes(b.first).is_some() }) { host.push(UAV_BARRIER); #[cfg(feature = "diagnostics")] crate::copystat::add(crate::copystat::C::WaitsForBuffers, 1); } if !host.is_empty() { use windows::Win32::Graphics::Direct3D12::D3D12_RESOURCE_BARRIER; const _: () = assert!(size_of::() == size_of::()); // SAFETY: the same layout; borrowed resource pointers, never // released through this slice. let host = unsafe { std::slice::from_raw_parts( host.as_ptr().cast::(), host.len(), ) }; // SAFETY: an open list and live resources. unsafe { list.list.ResourceBarrier(host) }; } } 0 } /// Moves a title texture's host copy to `state` in the list, if needed. #[cfg(windows)] pub(crate) fn ensure_state(list: &crate::host::HostList, resource: This, state: u32) { use windows::Win32::Graphics::Direct3D12::D3D12_RESOURCE_STATES as State; if let Some((texture, before)) = list.transition(resource, state) { let state = crate::host::legal_state(texture, state); let barrier = crate::host::transition(texture, State(before as i32), State(state as i32)); // SAFETY: an open list and a live texture. unsafe { list.list.ResourceBarrier(&[barrier]) }; } } #[cfg(windows)] const COPY_DEST: u32 = 0x400; #[cfg(windows)] const COPY_SOURCE: u32 = 0x800; /// `len` bytes at `offset` of a title buffer, if they are inside it. #[cfg(windows)] fn buffer_range(buffer: This, offset: u64, len: u64) -> Option<*mut u8> { let (data, size) = crate::resource::buffer_bytes(buffer)?; let end = offset.checked_add(len)?; if end > size { tracing::warn!(offset, len, size, "copy outside its buffer"); return None; } // SAFETY: inside the buffer, checked above. Some(unsafe { data.add(offset as usize) }) } /// Buffer to buffer: both are host memory, copied at submission. extern "system" fn copy_buffer_region( this: This, dst: This, dst_offset: u64, src: This, src_offset: u64, len: u64, ) { #[cfg(windows)] if let Some(list) = host_list(this) && let Some(to) = buffer_range(dst, dst_offset, len) && let Some(from) = buffer_range(src, src_offset, len) { list.defer_copy(to, from, len as usize); } #[cfg(not(windows))] let _ = (this, dst, dst_offset, src, src_offset, len); } /// Whether one of two resources is a target in a stand-in format and the /// other is not ([`crate::resource::target_format`]): their texels differ /// in size, so the host cannot copy between them (logged once). #[cfg(windows)] fn unlike(dst: This, src: This) -> bool { let unlike = crate::resource::stands_in(dst) != crate::resource::stands_in(src); if unlike { static ONCE: std::sync::Once = std::sync::Once::new(); ONCE.call_once(|| { tracing::warn!(?dst, ?src, "copy between a stand-in target and title-format texels skipped") }); } unlike } extern "system" fn copy_resource(this: This, dst: This, src: This) { #[cfg(windows)] { let Some(list) = host_list(this) else { return; }; if let (Some(to), Some(from)) = ( crate::resource::host_resource(dst), crate::resource::host_resource(src), ) { if unlike(dst, src) { return; } // A source the title fills itself is uploaded first (upstream // PR #671): a copy read whatever the host texture held. if crate::texture::title_written(from) { crate::texture::upload(list, src, from); } // Depth textures shaders also write: what they wrote, first. list.twin_use(dst, false, true); list.twin_use(src, false, false); ensure_state(list, dst, COPY_DEST); ensure_state(list, src, COPY_SOURCE); crate::texture::host_written(dst); #[cfg(feature = "diagnostics")] list.note_op(|| format!("copy resource {src:?} to {dst:?}")); // SAFETY: an open list and live textures. unsafe { list.list.CopyResource(to, from) }; } else if let (Some((to, a)), Some((from, b))) = ( crate::resource::buffer_bytes(dst), crate::resource::buffer_bytes(src), ) { list.defer_copy(to, from, a.min(b) as usize); } } #[cfg(not(windows))] let _ = (this, dst, src); } extern "system" fn copy_resource_x(this: This, dst: This, src: This, _flags: u32) { copy_resource(this, dst, src); } /// A copy location as titles pass it, laid out as the host's: a resource, /// then a subresource index (type 0) or a placed footprint (type 1). #[repr(C)] #[derive(Clone, Copy)] pub(crate) struct CopyLocation { resource: This, kind: u32, footprint: crate::resource::PlacedFootprint, } /// Texture to texture on the GPU; buffer to texture through an upload /// buffer filled at submission. Texture to buffer (read back) is not done /// yet. 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], ) { #[cfg(windows)] { use windows::Win32::Graphics::Direct3D12::{D3D12_BOX, D3D12_TEXTURE_COPY_LOCATION}; use windows::core::Interface; const FOOTPRINT: u32 = 1; // SAFETY: the title passes both locations (null is tolerated). let (Some(list), Some(&dst), Some(&src)) = (host_list(this), unsafe { dst.as_ref() }, unsafe { src.as_ref() }) else { return; }; let Some(to) = crate::resource::host_resource(dst.resource) else { // Texture to buffer: a read back. The list copies the texels // into a host buffer of ours; once it has run they go to the // title's buffer (`HostList::defer_read_back`). // The title reads two tiny float targets back every frame (5x1 // and 1x1 RGBA32F, 80 and 16 bytes: run 90), and read a buffer // nobody wrote. `VICTORY_READ_BACK=0` leaves them unmade, as // before. static COUNT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); let n = COUNT.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; let made = read_back(list, dst, x, y, z, src, source_box); if n <= 16 || n.is_multiple_of(4096) || made.is_err() { static REFUSED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); if made.is_err() && REFUSED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) >= 16 { return; } let source = crate::resource::host_resource(src.resource).map(|r| { // SAFETY: a live resource. let d = unsafe { r.GetDesc() }; format!("{}x{} f{} mips {}", d.Width, d.Height, d.Format.0, d.MipLevels) }); tracing::info!( copies = n, made = ?made, source = ?source, source_kind = src.kind, destination_kind = dst.kind, destination_offset = dst.footprint.offset, destination_bytes = ?dst.footprint.bytes(), "a texture to buffer copy (a read back)" ); } return; }; if unlike(dst.resource, src.resource) { return; } let mut from = src; let _upload; // keeps the source alive while this call records it #[cfg(feature = "diagnostics")] let mut read = None; if src.kind == FOOTPRINT { let (Some(host), Some(bytes)) = (crate::host::host(), src.footprint.bytes()) else { return; }; let Some(data) = buffer_range(src.resource, src.footprint.offset, bytes) else { return; }; let Some((buffer, staging)) = list.upload(host, bytes) else { return; }; // The list owns this mapped D3D12 upload buffer. Wine can report // its address as MEM_FREE, so use the host-upload path. list.defer_fill(staging, data, bytes as usize); from.resource = buffer.as_raw().cast_const().cast(); from.footprint.offset = 0; #[cfg(feature = "diagnostics")] { // SAFETY: a live buffer. read = Some((unsafe { buffer.GetGPUVirtualAddress() }, bytes)); } _upload = buffer; } else { let Some(texture) = crate::resource::host_resource(src.resource) else { return; }; list.twin_use(src.resource, false, false); if crate::texture::title_written(texture) { crate::texture::upload(list, src.resource, texture); } ensure_state(list, src.resource, COPY_SOURCE); from.resource = texture.as_raw().cast_const().cast(); } // A depth texture shaders also write: what they wrote, first (the // source above too). list.twin_use(dst.resource, false, true); ensure_state(list, dst.resource, COPY_DEST); crate::texture::host_written(dst.resource); let mut to_location = dst; to_location.resource = to.as_raw().cast_const().cast(); let host_box = (!source_box.is_null()).then(|| source_box.cast::()); #[cfg(feature = "diagnostics")] { let text = || { // SAFETY: a live texture; the box is the title's or null. let (d, b) = unsafe { (to.GetDesc(), host_box.map(|b| *b)) }; format!( "copy to {:?} {}x{} f{} sub/kind {} at {x},{y},{z} from {:?} kind {} footprint {:?} box {b:?}", dst.resource, d.Width, d.Height, d.Format.0, dst.kind, src.resource, src.kind, (src.kind == FOOTPRINT).then_some(src.footprint) ) }; match read { Some(range) => list.note_read(text, range), None => list.note_op(text), } } const _: () = assert!(size_of::() == size_of::()); // SAFETY: an open list; both locations in the host's layout, naming // live resources; the box is the title's (same layout). unsafe { list.list.CopyTextureRegion( (&raw const to_location).cast(), x, y, z, (&raw const from).cast(), host_box, ) }; } #[cfg(not(windows))] let _ = (this, dst, x, y, z, src, source_box); } /// Records a texture to buffer copy (a read back) of [`copy_texture_region`] /// into `list`, or says why not. #[cfg(windows)] #[allow(clippy::too_many_arguments)] // the method's own arguments fn read_back( list: &crate::host::HostList, dst: CopyLocation, x: u32, y: u32, z: u32, src: CopyLocation, source_box: *const [u32; 6], ) -> Result<(), &'static str> { use windows::Win32::Graphics::Direct3D12::D3D12_BOX; use windows::core::Interface; const FOOTPRINT: u32 = 1; static ON: std::sync::OnceLock = std::sync::OnceLock::new(); if !*ON.get_or_init(|| std::env::var_os("VICTORY_READ_BACK").is_none_or(|v| v != "0")) { return Err("switched off"); } if dst.kind != FOOTPRINT || src.kind == FOOTPRINT { return Err("not a texture subresource into a placed footprint"); } if (x, y, z) != (0, 0, 0) { return Err("a destination corner other than the footprint's"); } let host = crate::host::host().ok_or("no host")?; let (footprint, [size, row, rows, pitch]) = dst.footprint.host_rows().ok_or("an unknown format")?; // The title's buffer holds every row at its own pitch. let bytes = pitch * (rows - 1) + row; let data = buffer_range(dst.resource, dst.footprint.offset, bytes).ok_or("a destination outside its buffer")?; let texture = crate::resource::host_resource(src.resource).ok_or("no host texture")?; let buffer = host.read_back_buffer(size).ok_or("no read-back buffer")?; // As a texture source of any copy: what shaders wrote to a depth twin // and what the title wrote itself first, then the state. list.twin_use(src.resource, false, false); if crate::texture::title_written(texture) { crate::texture::upload(list, src.resource, texture); } ensure_state(list, src.resource, COPY_SOURCE); let mut from = src; from.resource = texture.as_raw().cast_const().cast(); let mut to = dst; to.resource = buffer.as_raw().cast_const().cast(); to.footprint = footprint; let host_box = (!source_box.is_null()).then(|| source_box.cast::()); #[cfg(feature = "diagnostics")] list.note_op(|| { format!( "read back of {:?} sub {} into buffer {:?}+{:#x}: {rows} rows of {row} bytes", src.resource, src.footprint.offset as u32, dst.resource, dst.footprint.offset ) }); // SAFETY: an open list; both locations in the host's layout (asserted // in `copy_texture_region`), naming a live texture and our buffer, which // the list keeps until it is reset; the box is the title's. unsafe { list.list .CopyTextureRegion((&raw const to).cast(), 0, 0, 0, (&raw const from).cast(), host_box) }; list.defer_read_back(buffer, data, [row as usize, rows as usize, pitch as usize]); Ok(()) } #[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); } extern "system" fn resolve( this: This, dst: This, dst_sub: u32, src: This, src_sub: u32, format: u32, ) { #[cfg(windows)] if let Some(list) = host_list(this) && let (Some(to), Some(from)) = ( crate::resource::host_resource(dst), crate::resource::host_resource(src), ) { use windows::Win32::Foundation::RECT; use windows::Win32::Graphics::Direct3D12::{ D3D12_RESOLVE_MODE_AVERAGE, ID3D12GraphicsCommandList1, }; use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT; use windows::core::Interface; const RESOLVE_DEST: u32 = 0x1000; const RESOLVE_SOURCE: u32 = 0x2000; if unlike(dst, src) { return; } // Depth textures shaders also write: what they wrote, first. list.twin_use(dst, false, true); list.twin_use(src, false, false); ensure_state(list, dst, RESOLVE_DEST); ensure_state(list, src, RESOLVE_SOURCE); crate::texture::host_written(dst); #[cfg(feature = "diagnostics")] list.note_op(|| format!("resolve {src:?} to {dst:?} format {format}")); // SAFETY: an open list and live textures; the region is inside both. unsafe { let (a, b) = (to.GetDesc(), from.GetDesc()); let format = if crate::resource::stands_in(dst) { a.Format } else { DXGI_FORMAT(format as i32) }; if (a.Width, a.Height) == (b.Width, b.Height) { list.list .ResolveSubresource(to, dst_sub, from, src_sub, format); } else if let Ok(list1) = list.list.cast::() { // The console clips a larger source to the destination. let rect = RECT { left: 0, top: 0, right: a.Width.min(b.Width) as i32, bottom: a.Height.min(b.Height) as i32, }; list1.ResolveSubresourceRegion( to, dst_sub, 0, 0, from, src_sub, Some(&rect), format, D3D12_RESOLVE_MODE_AVERAGE, ); } } } #[cfg(not(windows))] let _ = (this, dst, dst_sub, src, src_sub, format); } #[repr(C)] pub(crate) struct Rect { left: i32, top: i32, right: i32, bottom: i32, } extern "system" fn clear_rtv( this: This, handle: u64, color: *const [f32; 4], count: u32, rects: *const Rect, ) { #[cfg(windows)] { let (Some(list), Some(record)) = (host_list(this), crate::descriptor::rtv_record(handle)) else { return; }; let (Some(texture), Some(host)) = ( crate::resource::host_resource(record.resource), crate::host::host(), ) else { return; }; // SAFETY: the title passes four floats (null is tolerated). let Some(color) = (unsafe { color.as_ref() }) else { return; }; let rects = (count > 0 && !rects.is_null()).then(|| { // SAFETY: the title passes `count` rectangles, laid out as the host's. unsafe { std::slice::from_raw_parts(rects.cast(), count as usize) } }); const RENDER_TARGET: u32 = 0x4; let rtv = host.rtv(Some(texture), record.desc().as_ref()); // SAFETY: an open list, a fresh view of a live texture. unsafe { if let Some((_, before)) = list.transition(record.resource, RENDER_TARGET) { use windows::Win32::Graphics::Direct3D12::D3D12_RESOURCE_STATES as State; list.list.ResourceBarrier(&[crate::host::transition( texture, State(before as i32), State(RENDER_TARGET as i32), )]); } list.list.ClearRenderTargetView(rtv, color, rects); } } #[cfg(not(windows))] let _ = (this, handle, color, count, rects); } /// `ClearDepthStencilView(handle, flags, depth, stencil, count, rects)`; /// flags: 1 depth, 2 stencil. extern "system" fn clear_dsv( this: This, handle: u64, flags: u32, depth: f32, stencil: u8, count: u32, rects: *const Rect, ) { #[cfg(windows)] { use windows::Win32::Graphics::Direct3D12::D3D12_CLEAR_FLAGS; let (Some(list), Some(record)) = (host_list(this), crate::descriptor::dsv_record(handle)) else { return; }; let (Some(texture), Some(host)) = ( crate::resource::host_resource(record.resource), crate::host::host(), ) else { return; }; let rects = (count > 0 && !rects.is_null()).then(|| { // SAFETY: the title passes `count` rectangles, laid out as the host's. unsafe { std::slice::from_raw_parts(rects.cast(), count as usize) } }); const DEPTH_WRITE: u32 = 0x10; // A depth texture shaders also write: what they wrote, first (the // clear may be of one slice or of some rectangles). list.twin_use(record.resource, false, true); ensure_state(list, record.resource, DEPTH_WRITE); if crate::resource::depth_and_unordered(record.resource) { crate::resource::DEPTH_UNORDERED[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed); } let dsv = host.dsv(texture, record.depth_desc().as_ref()); // SAFETY: an open list, a fresh view of a live texture. unsafe { list.list .ClearDepthStencilView(dsv, D3D12_CLEAR_FLAGS(flags as i32 & 3), depth, stencil, rects) }; } #[cfg(not(windows))] let _ = (this, handle, flags, depth, stencil, count, rects); } /// `CopyMemoryToMemoryX(destination, source, size, flags)`: GPU addresses /// are title memory, copied at submission like buffer copies (if mapped). extern "system" fn copy_memory(this: This, dst: u64, src: u64, len: u64, _flags: u32) { #[cfg(windows)] if let Some(list) = host_list(this) && dst != 0 && src != 0 { let len = len as usize; list.defer(move || { if crate::texture::committed(src as *const u8, len) && crate::texture::committed(dst as *const u8, len) { // SAFETY: both ranges are committed title memory. unsafe { std::ptr::copy(src as *const u8, dst as *mut u8, len) }; } }); } #[cfg(not(windows))] let _ = (this, dst, src, len); } /// `DriverHintX(feature, value)`: no hints for the host. extern "system" fn driver_hint(_: This, _feature: u64, _value: u64) -> u64 { 0 } /// `DispatchX(x, y, z, flags)`: a dispatch with console flags, which the /// host has no use for (the ray-tracing builder dispatches this way). extern "system" fn dispatch_x(this: This, x: u32, y: u32, z: u32, _flags: u32) { #[cfg(windows)] state::dispatch(this, x, y, z); #[cfg(not(windows))] { let _ = (this, x, y, z); count_dispatch(); } } /// `BeginQuery`/`EndQuery(heap, type, index)`: queries are answered when /// resolved. extern "system" fn query_mark(_: This, _heap: This, _kind: u32, _index: u32) {} /// `ResolveQueryData(heap, type, first, count, buffer, offset)`: writes /// each query's result into the buffer once the list has run. Timestamps /// (type 2) are the GPU clock then; occlusion counts (0) say a million /// samples passed and binary occlusion (1) visible, so nothing is culled /// by a query. /// shortcut: no query runs on the host GPU; results are plausible values, /// not measured ones (time spans come out as the submission's length). extern "system" fn resolve_queries( this: This, _heap: This, kind: u32, _first: u32, count: u32, buffer: This, offset: u64, ) { const OCCLUSION: u32 = 0; const BINARY: u32 = 1; const TIMESTAMP: u32 = 2; const PIPELINE_STATISTICS: u32 = 3; let Some((data, size)) = crate::resource::buffer_bytes(buffer) else { return; }; let (what, bytes) = match kind { OCCLUSION => (Written::Value(1 << 20), 8), BINARY => (Written::Value(1), 8), TIMESTAMP => (Written::GpuTimestamp, 8), PIPELINE_STATISTICS => (Written::Value(0), 88), _ => (Written::Value(0), 16), }; if offset + u64::from(count) * bytes > size { return; } for i in 0..u64::from(count) { // Wider results are written as 8-byte pieces. for piece in 0..bytes / 8 { // SAFETY: inside the buffer, checked above. let at = unsafe { data.add((offset + i * bytes + piece * 8) as usize) }; let what = if piece == 0 { what } else { Written::Value(0) }; defer_write(this, at as u64, what, 8); } } } /// IUnknown 3, object 4, device child 1, allocator 2. static ALLOCATOR_FUNCTIONS: Table<24> = { let mut f = object::d3d12_object(object::missing_table::()); f[0] = allocator_query as *const (); f[2] = allocator_release as *const (); f[7] = object::parent_device as *const (); // 0x38 f[8] = allocator_reset as *const (); // 0x40 f[9] = object::ignore as *const (); // 0x48 allocation callback Table(f) }; extern "system" fn set_pipeline(this: This, pipeline: This) { crate::pipeline::bound(pipeline); #[cfg(windows)] with_state(this, |_, state| state.set_pipeline(pipeline)); #[cfg(not(windows))] let _ = this; } /// Pipeline syncs: waits for earlier work and cache flushes, at the top or /// bottom of the pipe `(flags, control, address, size)`. Host lists order /// their own work and every submission is waited for, so there is nothing /// to do. extern "system" fn sync(_: This, _flags: u32, _control: u32, _address: u64, _size: u64) {} /// Reset with console flags `(allocator, pipeline, flags)`; the flags only /// tune the console's command memory. extern "system" fn reset_x(this: This, allocator: This, pipeline: This, _flags: u32) -> HRESULT { reset(this, allocator, pipeline) } /// Newer lists' 0x4a0 `(size, alignment) -> pointer`: memory the list /// owns until it is reset, which the CPU writes and the GPU reads at the /// same address; null on failure. The title's BVH builder puts /// its instance descriptions there (and wrote through our old /// `E_NOTIMPL` as a pointer). 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 mut block = vec![0u8; len]; let start = block.as_mut_ptr(); // SAFETY: the offset is below `align`, so `size` bytes fit after it. let at = unsafe { start.add(start.align_offset(align)) }; // SAFETY: the title passes one of our lists. let uploads = unsafe { &(*this.cast::()).uploads }; // Moving the vector keeps its heap block where it is. uploads.lock().unwrap_or_else(|e| e.into_inner()).push(block); at } /// Draw state the host has no use for (root signatures: ours come with /// the pipeline; depth order and shading rate hints): any arguments, /// nothing returned. extern "system" fn ignored_state(_: This) {} /// The title's draw state (decision 0007, step 3), kept for its draws or /// set on the host list at once. Without a host GPU these do nothing. #[cfg(windows)] mod state { use super::{This, with_state}; use crate::draw::{IndexView, VertexView}; use windows::Win32::Foundation::RECT; use windows::Win32::Graphics::Direct3D12::D3D12_VIEWPORT; /// `count` items at `items`, or none. fn items<'a, T>(items: *const T, count: u32) -> &'a [T] { if items.is_null() || count == 0 { return &[]; } // SAFETY: the title passes `count` items. unsafe { std::slice::from_raw_parts(items, count as usize) } } pub(super) extern "system" fn topology(this: This, topology: u32) { with_state(this, |list, s| s.set_topology(list, topology)); } pub(super) extern "system" fn viewports( this: This, count: u32, viewports: *const D3D12_VIEWPORT, ) { // (Through the draw state, which keeps them for the host list // that continues this one after a cut: `State::replay`.) with_state(this, |list, s| s.set_viewports(list, items(viewports, count))); } pub(super) extern "system" fn scissors(this: This, count: u32, rects: *const RECT) { with_state(this, |list, s| s.set_scissors(list, items(rects, count))); } pub(super) extern "system" fn blend_factor(this: This, factor: *const [f32; 4]) { with_state(this, |list, s| { // SAFETY: the title passes four floats or null. s.set_blend_factor(list, unsafe { factor.as_ref() }) }); } pub(super) extern "system" fn stencil_ref(this: This, reference: u32) { with_state(this, |list, s| s.set_stencil_ref(list, reference)); } pub(super) extern "system" fn depth_bounds(this: This, min: f32, max: f32) { with_state(this, |list, s| s.set_depth_bounds(list, min, max)); } pub(super) extern "system" fn heaps(this: This, count: u32, heaps: *const This) { with_state(this, |_, s| s.set_heaps(items(heaps, count))); } pub(super) extern "system" fn compute_table(this: This, slot: u32, handle: u64) { with_state(this, |_, s| s.set_table(true, slot, handle)); } pub(super) extern "system" fn graphics_table(this: This, slot: u32, handle: u64) { with_state(this, |_, s| s.set_table(false, slot, handle)); } pub(super) extern "system" fn compute_constant(this: This, slot: u32, value: u32, offset: u32) { with_state(this, |_, s| s.set_constants(true, slot, &[value], offset)); } pub(super) extern "system" fn graphics_constant( this: This, slot: u32, value: u32, offset: u32, ) { with_state(this, |_, s| s.set_constants(false, slot, &[value], offset)); } pub(super) extern "system" fn compute_constants( this: This, slot: u32, count: u32, values: *const u32, offset: u32, ) { with_state(this, |_, s| { s.set_constants(true, slot, items(values, count), offset) }); } pub(super) extern "system" fn graphics_constants( this: This, slot: u32, count: u32, values: *const u32, offset: u32, ) { with_state(this, |_, s| { s.set_constants(false, slot, items(values, count), offset) }); } /// Root constant buffer, shader resource and unordered access views. pub(super) extern "system" fn compute_view(this: This, slot: u32, address: u64) { with_state(this, |_, s| s.set_view(true, slot, address)); } pub(super) extern "system" fn graphics_view(this: This, slot: u32, address: u64) { with_state(this, |_, s| s.set_view(false, slot, address)); } pub(super) extern "system" fn index_buffer(this: This, view: *const IndexView) { // SAFETY: the title passes a view or null. with_state(this, |_, s| s.set_index_buffer(unsafe { view.as_ref() })); } pub(super) extern "system" fn vertex_buffers( this: This, start: u32, count: u32, views: *const VertexView, ) { with_state(this, |_, s| { if views.is_null() { s.clear_vertex_buffers(start, count); } else { s.set_vertex_buffers(start, items(views, count)); } }); } /// `(count, handles, one range, depth)`: with one range, the handles /// follow the first at the render target view size (128 bytes). pub(super) extern "system" fn targets( this: This, count: u32, handles: *const u64, one_range: i32, depth: *const u64, ) { let handles: Vec = if one_range != 0 && !handles.is_null() { // SAFETY: the title passes the first handle. let first = unsafe { *handles }; (0..u64::from(count)).map(|i| first + 128 * i).collect() } else { items(handles, count).to_vec() }; // SAFETY: the title passes a handle or null. let depth = unsafe { depth.as_ref() }.copied().unwrap_or(0); with_state(this, |list, s| s.set_targets(list, &handles, depth)); } pub(super) extern "system" fn draw( this: This, vertices: u32, instances: u32, first: u32, first_instance: u32, ) { super::count_draw(); with_state(this, |list, s| { s.draw(list, vertices, instances, first, first_instance) }); } pub(super) extern "system" fn dispatch(this: This, x: u32, y: u32, z: u32) { super::count_dispatch(); with_state(this, |list, s| s.dispatch(list, x, y, z)); } /// `DispatchMesh(x, y, z)` (the PC method of List6); the title's mesh /// programs take `x` groups only. pub(super) extern "system" fn dispatch_mesh(this: This, x: u32, y: u32, z: u32) { super::count_draw(); if y * z != 1 { static ONCE: std::sync::Once = std::sync::Once::new(); ONCE.call_once(|| tracing::warn!(x, y, z, "a mesh dispatch with y or z groups")); } with_state(this, |list, s| s.dispatch_mesh(list, x)); } /// `DispatchMeshX(x, flags, indirect arguments, payload)`. /// shortcut: dispatches whose group count is in GPU memory (indirect) /// are dropped; make them ExecuteIndirect when the title uses them. pub(super) extern "system" fn dispatch_mesh_x( this: This, x: u32, _flags: u64, indirect: u64, _payload: u64, ) { super::count_draw(); if indirect != 0 { static ONCE: std::sync::Once = std::sync::Once::new(); ONCE.call_once(|| tracing::warn!("indirect mesh dispatches dropped")); return; } with_state(this, |list, s| s.dispatch_mesh(list, x)); } pub(super) extern "system" fn draw_indexed( this: This, indices: u32, instances: u32, first: u32, base: i32, first_instance: u32, ) { super::count_draw(); with_state(this, |list, s| { s.draw_indexed(list, indices, instances, first, base, first_instance) }); } } /// Without a host GPU every draw state call is ignored. #[cfg(not(windows))] mod state { pub(super) use super::dispatch; pub(super) use super::draw; pub(super) use super::draw as draw_indexed; pub(super) use super::draw as dispatch_mesh; pub(super) use super::draw as dispatch_mesh_x; pub(super) use super::ignored_state as blend_factor; pub(super) use super::ignored_state as compute_constant; pub(super) use super::ignored_state as compute_constants; pub(super) use super::ignored_state as compute_table; pub(super) use super::ignored_state as compute_view; pub(super) use super::ignored_state as graphics_constant; pub(super) use super::ignored_state as graphics_constants; pub(super) use super::ignored_state as graphics_table; pub(super) use super::ignored_state as graphics_view; pub(super) use super::ignored_state as heaps; pub(super) use super::ignored_state as index_buffer; pub(super) use super::ignored_state as scissors; pub(super) use super::ignored_state as stencil_ref; pub(super) use super::ignored_state as depth_bounds; pub(super) use super::ignored_state as targets; pub(super) use super::ignored_state as topology; pub(super) use super::ignored_state as vertex_buffers; pub(super) use super::ignored_state as viewports; } fn count_draw() { DRAWS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); } /// Draws of the kinds the host does not run yet (indirect, mesh), and /// dispatches, counted per frame for the log. /// shortcut: dropped until their shaders translate (decision 0007). #[cfg(not(windows))] extern "system" fn draw(_: This) { count_draw(); } /// `ExecuteIndirect(signature, max count, arguments, offset, count buffer, /// offset)`. The title's signatures (title+0x2479466, 2026-11-24) /// hold one argument each: draw (stride 16), indexed draw (20), dispatch /// (12), mesh dispatch (12), and the console's single-pass dispatch, type /// 105 (stride 12: the three group counts; its flags live in the /// signature, the title passes none), run as a plain dispatch. They run /// through the host's indirect execution (`State::execute_indirect`). /// shortcut: signatures that change root arguments are dropped (logged /// once per signature). extern "system" fn execute_indirect( this: This, signature: This, max: u32, arguments: This, offset: u64, count: This, count_offset: u64, ) { count_draw(); let Some((stride, args)) = crate::signature::layout(signature) else { return; }; let address = crate::resource::buffer_bytes(arguments).map(|(p, _)| p as u64 + offset); let counter = crate::resource::buffer_bytes(count).map(|(p, _)| p as u64 + count_offset); let mut ran = false; #[cfg(windows)] if let ([[kind, ..]], Some(address)) = (args, address) && (count.is_null() || counter.is_some()) { const DISPATCH: u32 = 2; const DISPATCH_SINGLE: u32 = 105; let kind = if *kind == DISPATCH_SINGLE { DISPATCH } else { *kind }; with_state(this, |list, s| { ran = s.execute_indirect(list, (kind, stride, max), address, counter); }); } else { #[cfg(all(windows, feature = "diagnostics"))] crate::pipeline::note_why(format!( "indirect signature {args:?}: arguments in a buffer {}, count in a buffer {}", address.is_some(), count.is_null() || counter.is_some() )); } if ran { return; } INDIRECT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); static SEEN: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); let mut seen = SEEN.lock().unwrap_or_else(|e| e.into_inner()); if !seen.contains(&(signature as usize)) { seen.push(signature as usize); tracing::info!( stride, ?args, max, address = ?address.map(|a| format!("{a:#x}")), counted = !count.is_null(), "indirect execution dropped (first time for this signature)" ); } } /// Indirect executions dropped since the last call. pub(crate) fn take_indirect() -> u64 { INDIRECT.swap(0, std::sync::atomic::Ordering::Relaxed) } static INDIRECT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); fn count_dispatch() { DISPATCHES.fetch_add(1, std::sync::atomic::Ordering::Relaxed); } #[cfg(not(windows))] extern "system" fn dispatch(_: This) { count_dispatch(); } static DRAWS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); static DISPATCHES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); /// Draws (any kind, mesh and indirect included) and compute dispatches /// recorded since the last call. pub(crate) fn take_counts() -> (u64, u64) { use std::sync::atomic::Ordering::Relaxed; (DRAWS.swap(0, Relaxed), DISPATCHES.swap(0, Relaxed)) } /// A GPU wait for a memory value: the list's commands after it run when /// the 32 or 64 bits at `address` compare as asked with `reference`. #[derive(Clone, Copy, Debug)] pub(crate) struct ValueWait { address: u64, comparison: u32, reference: u64, wide: bool, } impl ValueWait { /// Whether the memory holds what is waited for now. /// shortcut: the comparison codes are taken to be the comparison /// functions' (1 never .. 8 always, value against reference); one not /// known counts as met, and so does memory that is not mapped. /// /// The value is read through the kernel, which reports memory that is /// not there instead of faulting (as the copies of title memory are /// read, `crate::host::copy_mapped`). Asking Windows first whether the /// memory is committed (`VirtualQuery`) walks the page tables to the /// end of the region: the title keeps these values in a 1.1 GB /// allocation (0xd00000000), where one question took about 8 ms, and a /// queue asks at every submission and every signal, for each queue that /// stands at a wait (the title has five queues). That was /// 1.0-2.2 s of every cutscene frame on the thread that submits (runs /// 36-49: "between its submissions ... kernel"; 47 of 110 looks at /// that thread in run 49 found it in this question). /// `VICTORY_WAIT_ASK=1` asks first, as before. pub(crate) fn met(&self) -> bool { #[cfg(all(windows, feature = "diagnostics"))] let started = std::time::Instant::now(); let met = self.holds(); #[cfg(all(windows, feature = "diagnostics"))] { use crate::copystat::{C, add}; add(C::WaitChecks, 1); add(C::WaitCheckNs, started.elapsed().as_nanos() as u64); } met } fn holds(&self) -> bool { let bytes = if self.wide { 8 } else { 4 }; if self.address == 0 { return true; } static ASK: std::sync::OnceLock = std::sync::OnceLock::new(); if *ASK.get_or_init(|| std::env::var_os("VICTORY_WAIT_ASK").is_some_and(|v| v == "1")) && !crate::texture::committed(self.address as *const u8, bytes) { return true; } // The low `bytes` bytes; the rest stays zero. let mut raw = [0u8; 8]; if !crate::host::read_into(self.address as *const u8, &mut raw[..bytes]) { return true; } self.holds_for(u64::from_le_bytes(raw)) } /// Where the value waited for is: address and bytes. #[cfg(windows)] pub(crate) fn place(&self) -> (usize, usize) { (self.address as usize, if self.wide { 8 } else { 4 }) } /// Whether the wait is met with `value` at its address (its low half /// for a 32-bit wait). pub(crate) fn holds_for(&self, value: u64) -> bool { let (value, reference) = if self.wide { (value, self.reference) } else { (value & 0xFFFF_FFFF, self.reference & 0xFFFF_FFFF) }; match self.comparison { 1 => false, 2 => value < reference, 3 => value == reference, 4 => value <= reference, 5 => value > reference, 6 => value != reference, 7 => value >= reference, _ => true, } } } /// The waits lists recorded since their last reset, by list. static VALUE_WAITS: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); /// The waits of the submitted `lists` that are not met yet (taken from /// them): what the console's GPU would stop at. The title orders work /// between its queues this way (the title: the compute queue's /// lighting passes wait for a value the graphics queue's depth pass writes /// at the bottom of the pipe; run at once, they read a cleared depth /// buffer and nothing was lit). /// shortcut: a wait holds its whole list (and submission) back, not only /// the commands after it. pub(crate) fn unmet_waits(lists: &[This]) -> Vec { let mut waits = VALUE_WAITS.lock().unwrap_or_else(|e| e.into_inner()); let mut unmet = Vec::new(); waits.retain(|&(list, wait)| { if !lists.iter().any(|&l| l as usize == list) { return true; } if !wait.met() { unmet.push(wait); } false }); unmet } /// The waits each of the submitted `lists` recorded (taken from them), /// met or not, at the list's first place in the submission. #[cfg(windows)] pub(crate) fn waits_by_list(lists: &[This]) -> Vec> { let mut waits = VALUE_WAITS.lock().unwrap_or_else(|e| e.into_inner()); let mut by = vec![Vec::new(); lists.len()]; waits.retain(|&(list, wait)| { match lists.iter().position(|&l| l as usize == list) { Some(i) => { by[i].push(wait); false } None => true, } }); by } fn wait_for(this: This, address: u64, comparison: u32, reference: u64, wide: bool) { let wait = ValueWait { address, comparison, reference, wide, }; static SEEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); if SEEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 12 { tracing::info!( address = format_args!("{address:#x}"), comparison, reference, wide, met = wait.met(), "a list waits for a memory value" ); } #[cfg(all(windows, feature = "diagnostics"))] if let Some(host) = host_list(this) { crate::capture::op(std::ptr::from_ref(host) as usize, || { format!( "WAIT VALUE at {address:#x} comparison {comparison} reference {reference:#x} wide {wide} met {}", wait.met() ) }); } // The list is cut here, and the wait stands before what follows. #[cfg(windows)] if crate::queue::cuts() { cut(this, wait); return; } VALUE_WAITS .lock() .unwrap_or_else(|e| e.into_inner()) .push((this as usize, wait)); } extern "system" fn wait_value(this: This, address: u64, comparison: u32, reference: u64) { wait_for(this, address, comparison, reference, false); } extern "system" fn wait_value64(this: This, address: u64, comparison: u32, reference: u64) { wait_for(this, address, comparison, reference, true); } /// The enhanced barriers `(group count, groups)`: buffer, texture and /// global barriers with layouts instead of states. Every host use of a /// title texture (clears, copies) moves its host copy to the state it needs /// first, so only their ordering matters: one barrier on every shader /// write (the title's buffer barriers order compute passes). extern "system" fn barrier_groups(this: This, count: u32, _groups: *const u8) { // Diagnostics: counted, and left out under `VICTORY_BARRIER_TEST=groups`. #[cfg(all(windows, feature = "diagnostics"))] if count > 0 { crate::copystat::add(crate::copystat::C::WaitsForGroups, 1); if crate::copystat::barrier_test() & 1 != 0 { return; } } #[cfg(windows)] if count > 0 && let Some(list) = host_list(this) { use windows::Win32::Graphics::Direct3D12::D3D12_RESOURCE_BARRIER; // SAFETY: the same layout; no resource named. let barrier = unsafe { std::mem::transmute::(UAV_BARRIER) }; // SAFETY: an open list. unsafe { list.list.ResourceBarrier(&[barrier]) }; } #[cfg(not(windows))] let _ = (this, count); } /// The bottom-of-pipe value writes `(address, value, flush, flags)`, 32 and /// 64 bits: done when the list has run, as the GPU would. Flag 1 writes the /// GPU timestamp instead (64-bit form), flag 2 the system timestamp. extern "system" fn write32(this: This, address: u64, value: u32, _flush: u32, _flags: u32) { defer_write(this, address, Written::Value(u64::from(value)), 4); value_written(this, address, u64::from(value)); } extern "system" fn write64(this: This, address: u64, value: u64, _flush: u32, flags: u32) { const GPU_TIMESTAMP: u32 = 1; const SYSTEM_TIMESTAMP: u32 = 2; let what = match flags { GPU_TIMESTAMP => Written::GpuTimestamp, SYSTEM_TIMESTAMP => Written::SystemTimestamp, _ => Written::Value(value), }; defer_write(this, address, what, 8); if let Written::Value(value) = what { value_written(this, address, value); } } /// The title wrote a value at the bottom of the pipe (not a timestamp, not /// a query result): listed (diagnostics), and with `VICTORY_CUT_WRITES=1` /// the host list is cut right behind it, so that the value is stored when /// the card has run the list up to there, as on the console, and not when /// the whole list has run. Lists of other queues wait for these values /// (the title: the compute list that builds the depth pyramid /// waits for one, and the meshlet culling of the same frame reads that /// pyramid). Off by default: every such write then ends a host list, and /// each part is a submission of its own. fn value_written(this: This, address: u64, value: u64) { #[cfg(windows)] { if address == 0 || this.is_null() { return; } #[cfg(not(feature = "diagnostics"))] let _ = value; #[cfg(feature = "diagnostics")] if let Some(host) = host_list(this) { crate::capture::op(std::ptr::from_ref(host) as usize, || { format!("WRITE VALUE at {address:#x} value {value:#x}") }); } static ON: std::sync::OnceLock = std::sync::OnceLock::new(); if !*ON.get_or_init(|| std::env::var_os("VICTORY_CUT_WRITES").is_some_and(|v| v == "1")) || !crate::queue::cuts() { return; } // SAFETY: the title passes one of our lists; a list is recorded by // one thread at a time and is not submitted while it records. let list = unsafe { &mut *this.cast_mut().cast::() }; let Some(current) = list.host.as_ref() else { return; }; let Some(fresh) = fresh_list() else { return; }; current.close(); if let Some(done) = list.host.replace(fresh) { // The part so far keeps its waits; nothing stands before the // next one but this part itself. let waits = std::mem::take(&mut list.gate); list.earlier.push(Part { waits, host: done }); } if let Some(host) = list.host.as_ref() { list.state .lock() .unwrap_or_else(|e| e.into_inner()) .replay(host); } } #[cfg(not(windows))] let _ = (this, address, value); } fn defer_write(this: This, address: u64, what: Written, bytes: usize) { if address == 0 { return; } #[cfg(windows)] if let Some(list) = host_list(this) { list.defer_write(address as *mut u8, what, bytes); return; } let _ = this; // Without a host GPU nothing runs later: write now. what.write(address as *mut u8, bytes); } /// The list methods by their slot in older titles' lists (the 2020-era /// layout): IUnknown 3, object 4, device child 1, list 1, graphics list 96 /// (59 as on PC, then console ones), then versions 1-6 (6 + 1 + 1 + 11 + 3 /// + 13): 140 slots. const METHODS: [(usize, *const ()); 52] = [ (0, list_query as *const ()), (2, list_release as *const ()), (7, object::parent_device as *const ()), // 0x38 (8, list_type as *const ()), // 0x40 (9, close as *const ()), // 0x48 (10, reset as *const ()), // 0x50 (12, state::draw as *const ()), // 0x60 instanced (13, state::draw_indexed as *const ()), // 0x68 indexed (14, state::dispatch as *const ()), // 0x70 (15, copy_buffer_region as *const ()), // 0x78 (16, copy_texture_region as *const ()), // 0x80 (17, copy_resource as *const ()), // 0x88 (19, resolve as *const ()), // 0x98 (20, state::topology as *const ()), // 0xa0 (21, state::viewports as *const ()), // 0xa8 (22, state::scissors as *const ()), // 0xb0 (23, state::blend_factor as *const ()), // 0xb8 (24, state::stencil_ref as *const ()), // 0xc0 (25, set_pipeline as *const ()), // 0xc8 (26, barrier as *const ()), // 0xd0 (32, state::heaps as *const ()), // 0x100 (33, ignored_state as *const ()), // 0x108 compute root signature (34, ignored_state as *const ()), // 0x110 graphics root signature (35, state::compute_table as *const ()), // 0x118 (36, state::graphics_table as *const ()), // 0x120 (37, state::compute_constant as *const ()), // 0x128 (38, state::graphics_constant as *const ()), // 0x130 (39, state::compute_constants as *const ()), // 0x138 (40, state::graphics_constants as *const ()), // 0x140 (41, state::compute_view as *const ()), // 0x148 CBV (42, state::graphics_view as *const ()), // 0x150 CBV (43, state::compute_view as *const ()), // 0x158 SRV (44, state::graphics_view as *const ()), // 0x160 SRV (45, state::compute_view as *const ()), // 0x168 UAV (46, state::graphics_view as *const ()), // 0x170 UAV (47, state::index_buffer as *const ()), // 0x178 (48, state::vertex_buffers as *const ()), // 0x180 (50, state::targets as *const ()), // 0x190 (51, clear_dsv as *const ()), // 0x198 (52, clear_rtv as *const ()), // 0x1a0 (59, execute_indirect as *const ()), // 0x1d8 indirect (60, copy_memory as *const ()), // 0x1e0 (68, state::depth_bounds as *const ()), // 0x220 depth bounds (69, write32 as *const ()), // 0x228 (70, write64 as *const ()), // 0x230 (71, wait_value as *const ()), // 0x238, 32 bits (72, wait_value64 as *const ()), // 0x240, 64 bits (73, ignored_state as *const ()), // 0x248 pixel shader depth order (83, copy_texture_region_x as *const ()), // 0x298 (84, copy_resource_x as *const ()), // 0x2a0 (124, ignored_state as *const ()), // 0x3e0 shading rate (127, state::dispatch_mesh as *const ()), // 0x3f8 mesh dispatch ]; /// The rest of [`METHODS`], kept apart only to stay readable. const MORE_METHODS: [(usize, *const ()); 9] = [ (28, query_mark as *const ()), // 0xe0 BeginQuery (29, query_mark as *const ()), // 0xe8 EndQuery (30, resolve_queries as *const ()), // 0xf0 (78, dispatch_x as *const ()), // 0x270 (98, state::dispatch_mesh_x as *const ()), // 0x310 (99, driver_hint as *const ()), // 0x318 // Pipeline syncs at the top and bottom of the pipe, 0x408 and 0x410. (129, sync as *const ()), (130, sync as *const ()), (137, reset_x as *const ()), // 0x448 ]; /// Newer titles' lists have two more methods in the console part of the /// base list, after the clears (0x198/0x1a0 as before) and before /// ExecuteIndirect, which moved from 0x1d8 to 0x1e8: every slot from 0x1d8 /// on is two later (taken here as starting exactly at 0x1d8). Inferred from /// The title's call shapes: ExecuteIndirect 0x1e8, bottom-of-pipe /// writes 0x238/0x240, waits 0x248/0x250, depth bounds 0x230, shading rate /// 0x3f0/0x400, syncs 0x418/0x420, ResetX 0x458 (asserts on failure). const fn newer_slot(slot: usize) -> usize { if slot >= 59 { slot + 2 } else { slot } } /// Methods only newer titles' lists have. 0x490: the enhanced barrier /// `(group count, groups)`, with 16-byte groups `(type, count, barriers)` /// and 40-byte buffer barriers (title+0x95e0 builds them). 0x470 `(u64)`, /// called from the title's state flush with its result unused. /// shortcut: 0x470's purpose is unknown; ignored until a title shows it /// matters. const NEWER_METHODS: [(usize, *const ()); 3] = [ (142, ignored_state as *const ()), (146, barrier_groups as *const ()), (148, allocate_upload as *const ()), ]; const fn list_table(newer: bool) -> [*const (); 160] { let mut f = object::d3d12_object(object::missing_table::()); let mut i = 0; while i < METHODS.len() + MORE_METHODS.len() { let (slot, method) = if i < METHODS.len() { METHODS[i] } else { MORE_METHODS[i - METHODS.len()] }; f[if newer { newer_slot(slot) } else { slot }] = method; i += 1; } let mut i = 0; while newer && i < NEWER_METHODS.len() { f[NEWER_METHODS[i].0] = NEWER_METHODS[i].1; i += 1; } f } /// Every slot up to the limit exists and logs, for the methods newer /// titles add. static LIST_FUNCTIONS: Table<160> = Table(list_table(false)); static NEWER_LIST_FUNCTIONS: Table<160> = Table(list_table(true)); #[cfg(test)] mod tests { use super::*; use crate::object::slot; #[test] fn every_inline_command_falls_back_to_the_table() { let null = std::ptr::null(); let mut allocator = null; assert_eq!( create_allocator(null, 0, &ALLOCATOR_IIDS[0], &mut allocator), S_OK ); let mut list = null; let hr = create_list(null, 0, 0, allocator, null, &LIST_IIDS[1], &mut list); assert_eq!(hr, S_OK); // What the title's inline draw checks: put < draw limit. // SAFETY: the list is 0x560 bytes. let (put, draw_limit, api_limit) = unsafe { let at = |o: usize| list.cast::().add(o).cast::().read(); (at(0x18), at(0x28), at(0x30)) }; assert!(put >= draw_limit && put >= api_limit); // The console's ID3D12GraphicsCommandList6, as the title asks. let mut same = null; assert_eq!(list_query(list, &LIST_IIDS[7], &mut same), S_OK); assert_eq!(same, list); assert_eq!(slot:: u32>(list, 0x10)(list), 1); assert_eq!(slot:: u32>(list, 0x40)(list), 0); assert_eq!( slot:: HRESULT>(list, 0x48)(list), S_OK ); // List-owned upload memory: aligned, writable, gone at reset. let p = allocate_upload(list, 40, 0x100); assert!(!p.is_null() && (p as usize).is_multiple_of(0x100)); // SAFETY: 40 bytes were handed out. unsafe { p.write_bytes(0xab, 40) }; assert!(allocate_upload(list, 8, 0x30).is_null(), "not a power of two"); assert_eq!(reset(list, allocator, null), S_OK); // SAFETY: the list is ours. assert!(unsafe { &(*list.cast::()).uploads }.lock().unwrap().is_empty()); let release = |o: This| slot:: u32>(o, 0x10)(o); assert_eq!(release(list), 0); assert_eq!(release(allocator), 0); } #[cfg(windows)] #[test] fn idle_most_scales_with_ram_and_cores() { const GIB: u64 = 1 << 30; const MIB: u64 = 1 << 20; assert_eq!(idle_most(None, 8), (128, 256 * MIB)); assert_eq!(idle_most(Some(8 * GIB), 4), (64, 128 * MIB)); assert_eq!(idle_most(Some(16 * GIB), 8), (128, 128 * MIB)); assert_eq!(idle_most(Some(24 * GIB), 12), (192, 256 * MIB)); assert_eq!(idle_most(Some(31 * GIB), 16), (256, 512 * MIB)); assert_eq!(idle_most(Some(32 * GIB), 16), (256, 512 * MIB)); assert_eq!(idle_most(Some(64 * GIB), 32), (256, 512 * MIB)); } }