diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index e6b203b..c01e08b 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -1279,6 +1279,10 @@ pub(crate) struct HostList { /// The upscaling (`crate::streamline`) recorded in the list, noted for /// the present after its submission; 0 for none. upscale: std::sync::atomic::AtomicU64, + /// Left open and empty, as made or reset, for the title's next reset + /// ([`HostList::leave_open`]): that reset finds nothing to undo. The + /// list's title calls and epoch then, each plus one (0: not left open). + left_open: [std::sync::atomic::AtomicU64; 2], /// Counts the list in [`ALIVE`] while it lives. _alive: Counted, /// The texts of the markers `note_op` set, in order (for device-removed @@ -1294,6 +1298,31 @@ pub(crate) struct HostList { /// A list's prelude: an allocator and a closed list. type Prelude = (ID3D12CommandAllocator, ID3D12GraphicsCommandList); +/// Whether the new host list each of the title's lists gets when it is +/// submitted is left open for the title's next reset ([`HostList::leave_open`]; +/// `VICTORY_FRESH_OPEN=0`: closed at once and reset again, as before). What +/// runs on the card is the same: those lists are empty either way. +fn fresh_open() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("VICTORY_FRESH_OPEN").is_none_or(|v| v != "0")) +} + +/// Since the last frame line: host lists left open ([`fresh_open`]), the +/// title's resets that found one so and did nothing to it, and those closed +/// after all (submitted again without a reset, or recorded into before it). +static FRESH_OPEN: [std::sync::atomic::AtomicU64; 3] = [const { std::sync::atomic::AtomicU64::new(0) }; 3]; + +/// [`FRESH_OPEN`], for the frame-rate line. +fn fresh_open_line(frames: f64) { + let [open, kept, closed] = [0, 1, 2].map(|i| FRESH_OPEN[i].swap(0, Ordering::Relaxed) as f64 / frames); + if open + kept + closed > 0.0 { + tracing::info!( + "new host lists for the title's lists left open per frame (VICTORY_FRESH_OPEN=0: closed at once): {open:.1}, \ + {kept:.1} resets of the title found them so and had nothing to undo, {closed:.2} closed after all" + ); + } +} + /// Whether a host list's prelude is made when the list first needs one. A /// prelude runs the texture transitions and twin copies a list needs before /// it; lists that name no texture never have one, and every host list was @@ -4224,6 +4253,7 @@ impl Host { indirect_run: Mutex::default(), holding: std::sync::atomic::AtomicBool::new(false), upscale: std::sync::atomic::AtomicU64::new(0), + left_open: [const { std::sync::atomic::AtomicU64::new(0) }; 2], _alive: Counted::new(), #[cfg(feature = "diagnostics")] notes, @@ -6387,6 +6417,7 @@ impl HostList { pub(crate) fn close(&self) { let _closing = crate::timeline::span("closing a host list"); + self.left_open[0].store(0, Ordering::Release); self.flush_indirect(); self.twins_to_depth(); self.textures_back(); @@ -6436,9 +6467,61 @@ impl HostList { .splice(at..at, found.into_iter().map(|(shadow, from, to)| Step::Refresh(shadow, from as usize, (to - from) as usize))); } + /// A new host list for one of the title's lists (`crate::list::detach`), + /// open and empty as it was made or reset: left so, for the title's next + /// call on its list, a reset, which then has nothing to undo + /// ([`fresh_open`]); or closed now, as before. + /// + /// Each of the title's lists got a new host list at every submission, + /// closed at once on the submitting thread (an empty list's close, some + /// 5 us in the driver, 200 a frame) and reset again by the title's reset + /// (allocator and list) after the reset that made it ready for reuse. + pub(crate) fn leave_open(&self) { + if !fresh_open() { + self.close(); + return; + } + // (The marks first: a reset that sees the list left open sees them.) + self.left_open[1].store(self.epoch() + 1, Ordering::Relaxed); + self.left_open[0].store(self.title_calls() + 1, Ordering::Release); + FRESH_OPEN[0].fetch_add(1, Ordering::Relaxed); + } + + /// A host list taken from one of the title's lists to run + /// (`crate::list::detach`): closed first if it was left open + /// ([`HostList::leave_open`]), as it is when the title submits its list + /// again without a reset. + pub(crate) fn close_if_left_open(&self) { + if self.left_open[0].load(Ordering::Acquire) != 0 { + FRESH_OPEN[2].fetch_add(1, Ordering::Relaxed); + self.close(); + } + } + + /// Whether the list is as [`HostList::leave_open`] left it: nothing was + /// recorded into it since, as far as its title calls and its epoch say. + /// It is no longer marked so; one left open that something was recorded + /// into is closed here, for the reset that follows. + fn still_left_open(&self) -> bool { + let calls = self.left_open[0].swap(0, Ordering::AcqRel); + let epoch = self.left_open[1].swap(0, Ordering::Relaxed); + if calls == 0 { + return false; + } + if calls == self.title_calls() + 1 && epoch == self.epoch() + 1 { + return true; + } + FRESH_OPEN[2].fetch_add(1, Ordering::Relaxed); + // SAFETY: our list, open; what went into it goes with the reset. + let _ = unsafe { self.list.Close() }; + false + } + /// Only after the list's last submission finished (every one is waited /// for). pub(crate) fn reset(&self) { + // Before the epoch moves on below. + let left_open = self.still_left_open(); self.changed_something(); self.holding.store(false, Ordering::Relaxed); *self.indirect_run.lock().unwrap_or_else(|e| e.into_inner()) = None; @@ -6465,14 +6548,20 @@ impl HostList { give_chunks(std::mem::take(&mut scratch.chunks)); } } - // SAFETY: the list is not executing. - let reset = unsafe { - self.allocator - .Reset() - .and_then(|()| self.list.Reset(&self.allocator, None)) - }; - if let Err(e) = reset { - fail(format_args!("a host command list did not reset ({e})")); + // Open and empty since it was made or last reset: the allocator holds + // nothing, the list nothing recorded (`HostList::leave_open`). + if left_open { + FRESH_OPEN[1].fetch_add(1, Ordering::Relaxed); + } else { + // SAFETY: the list is not executing. + let reset = unsafe { + self.allocator + .Reset() + .and_then(|()| self.list.Reset(&self.allocator, None)) + }; + if let Err(e) = reset { + fail(format_args!("a host command list did not reset ({e})")); + } } #[cfg(feature = "diagnostics")] crate::copystat::add(crate::copystat::C::ResetNs, started.elapsed().as_nanos() as u64); @@ -8974,6 +9063,7 @@ impl Meter { spans::line(frames); crate::shadow::back_line(frames); submit_split_line(frames); + fresh_open_line(frames); #[cfg(feature = "diagnostics")] split_line(frames); { diff --git a/crates/victory-gpu/src/list.rs b/crates/victory-gpu/src/list.rs index 698b8b7..1d3f364 100644 --- a/crates/victory-gpu/src/list.rs +++ b/crates/victory-gpu/src/list.rs @@ -476,14 +476,16 @@ pub(crate) fn detach_parts(lists: &[This]) -> Parts { continue; }; // A new host list is open; the title's next call on its list is a - // reset, which wants it closed. - fresh.close(); + // reset, which finds it so (or wants it closed). + fresh.leave_open(); // 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; }; + // (Submitted again without a reset: it runs closed, empty.) + host.close_if_left_open(); 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())); @@ -548,8 +550,8 @@ pub(crate) fn detach(lists: &[This]) -> (Vec, Vec) { continue; }; // A new host list is open; the title's next call on its list is a - // reset, which wants it closed. - fresh.close(); + // reset, which finds it so (or wants it closed). + fresh.leave_open(); let list = this.cast_mut().cast::(); // SAFETY: one of our lists, closed and submitted: nothing records // into it until the title resets it. @@ -557,6 +559,8 @@ pub(crate) fn detach(lists: &[This]) -> (Vec, Vec) { let Some(host) = (*list).host.replace(fresh) else { continue; }; + // (Submitted again without a reset: it runs closed, empty.) + host.close_if_left_open(); let uploads = std::mem::take(&mut *(*list).uploads.lock().unwrap_or_else(|e| e.into_inner())); let state =