From abf1020d25e46da6974f173bff08c89c990cfc49 Mon Sep 17 00:00:00 2001 From: readmet3xt-lab <238408149+readmet3xt-lab@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:11:34 +0530 Subject: [PATCH] Texture pass: tells the queue a batch at a time, takes what it knows of a slot with the slots due The pass over textures picked by index took the queue's lock once or twice for every slot it took: to ask what is known of the slot (a texture uploaded whole, waiting for a look again) and to tell what came of it. In 5 s of a cutscene it took 90,000-250,000 slots, and its "telling the queue" was 143-400 ms, about all of it waiting for the lock (152-355 ms by the pass; coolfox-20261126-233017 18:02:18-18:02:23). - What it found of a slot (done, uploaded in part, uploaded whole and looked at again, nothing to upload now, held for room) is told 16 slots at a time under one take of the lock (`HostHeap::texture_slots_told`), the rest when the pass ends, on every way out. A slot's entry is told of a few slots' work later, as it would be if the pass's thread were held up for that long; what is told of a write the queue no longer holds is dropped as before (`TextureQueue::same`). - What the queue knows of a slot waiting for a look again is read while the slots due are taken, in the same take of the lock (`HostHeap::texture_slots_due_known`), and used if the queue's marks (b5b7806) say it still holds the write handed out; else it is asked as before. Behaviour-neutral: the queue ends as it would with each slot told at its turn (test), only some microseconds later. On by default; `VICTORY_QUEUE_BATCH=0` tells and asks each slot at its turn, as before. The lock line (18ff932) counts the lock taken by the pass apart. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/descriptor/host.rs | 173 ++++++++++++++++++++-- crates/victory-gpu/src/draw.rs | 136 +++++++++++++---- 2 files changed, 268 insertions(+), 41 deletions(-) diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index edc167f..bc04362 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -1106,32 +1106,65 @@ impl TextureQueue { if !again { c[4] = c[4].max(frame.saturating_sub(w.written)); } - let class = if w.looks > 0 || w.mapped > 0 { &mut due.again } else { &mut due.fresh }; - class.push((w.written, index, w.write)); + if w.looks > 0 || w.mapped > 0 { + // (Only these can wait for a look again: `recheck` + // leaves a look, `partly` some bytes mapped.) + due.again.push((w.written, index, w.write, Some(w.checked).filter(|c| c.2 != 0))); + } else { + due.fresh.push((w.written, index, w.write)); + } } } (due, c) } } +/// What [`TextureQueue::recheck`] noted of a slot uploaded whole: where its +/// memory is (address, bytes), its fingerprint, the looks that found it +/// unchanged, the frame of the upload. +pub(crate) type Checked = (u64, u64, u64, u8, u64); + +/// What the pass over textures picked by index found of a slot it took, as +/// told to the queue ([`HostHeap::texture_slots_told`]). +#[derive(Clone, Copy, Debug)] +pub(crate) enum Told { + /// Up to date ([`HostHeap::texture_slot_done`]): due again from frame + /// `.0` with `.1` bytes of its memory there, or not until written. + Done(Option<(u64, u64)>), + /// Uploaded whole, looked at again ([`HostHeap::texture_slot_recheck`]): + /// from frame, memory (address, bytes), fingerprint, looks, upload frame. + Recheck(u64, (u64, u64), u64, u8, u64), + /// Nothing to upload now ([`HostHeap::texture_slot_later`]): frame, most. + Later(u64, u64), + /// No room on the card ([`HostHeap::texture_slot_held`]): until frame. + Held(u64), +} + /// The slots due, taken from the queue ([`TextureQueue::due_and_census`]): -/// (frame written, slot, write number) of those looked at before and of the -/// others. +/// (frame written, slot, write number) of those looked at before, with what +/// [`TextureQueue::checked`] says of them, and of the others. #[derive(Default)] struct Due { - again: Vec<(u64, u32, u64)>, + again: Vec<(u64, u32, u64, Option)>, fresh: Vec<(u64, u32, u64)>, } impl Due { /// Up to `max` of them, in the order [`TextureQueue::due`] serves them. + #[cfg(test)] fn order(self, max: usize) -> Vec<(u32, u64)> { + self.ordered(max).0 + } + + /// [`Due::order`], and beside it what [`TextureQueue::checked`] said of + /// each when they were taken. + fn ordered(self, max: usize) -> (Vec<(u32, u64)>, Vec>) { let Due { mut again, mut fresh } = self; // By the number of the write, counted over the run. - let newest_first = |a: &(u64, u32, u64), b: &(u64, u32, u64)| b.2.cmp(&a.2); - again.sort_unstable_by(newest_first); - fresh.sort_unstable_by(newest_first); - let mut order: Vec<(u32, u64)> = again.iter().map(|&(_, index, write)| (index, write)).collect(); + again.sort_unstable_by(|a, b| b.2.cmp(&a.2)); + fresh.sort_unstable_by(|a, b| b.2.cmp(&a.2)); + let mut order: Vec<(u32, u64)> = again.iter().map(|&(_, index, write, _)| (index, write)).collect(); + let mut known: Vec> = again.iter().map(|a| a.3).collect(); let (mut new, mut old) = (0, fresh.len()); while new < old && order.len() < max { order.push((fresh[new].1, fresh[new].2)); @@ -1142,7 +1175,8 @@ impl Due { } } order.truncate(max); - order + known.resize(order.len(), None); + (order, known) } } @@ -2039,11 +2073,61 @@ impl HostHeap { /// with its write number, in the order to serve them /// ([`TextureQueue::due`]), and the queue's counts /// ([`TextureQueue::census`]). + #[cfg(test)] pub(crate) fn texture_slots_due(&self, frame: u64, max: usize) -> (Vec<(u32, u64)>, [u64; 6]) { let (due, census) = self.queue(super::queue_site::DUE).due_and_census(frame); (if max == 0 { Vec::new() } else { due.order(max) }, census) } + /// Up to `max` title slots whose texture view is due at `frame`, each + /// with its write number, in the order to serve them + /// ([`TextureQueue::due`]); beside each slot what + /// [`TextureQueue::checked`] said of it then, read in the same take of + /// the lock (the pass asked it slot by slot, under the lock each time); + /// and the queue's counts ([`TextureQueue::census`]). + pub(crate) fn texture_slots_due_known(&self, frame: u64, max: usize) -> (Vec<(u32, u64)>, Vec>, [u64; 6]) { + let (due, census) = self.queue(super::queue_site::DUE).due_and_census(frame); + let (order, known) = if max == 0 { (Vec::new(), Vec::new()) } else { due.ordered(max) }; + (order, known, census) + } + + /// The write of the entry the queue holds for title slot `index`, by its + /// marks (0: none; [`QueueMarks`]); `None`: not known so (ask the queue). + pub(crate) fn texture_slot_write(&self, index: u32) -> Option { + if !queue_marks() { + return None; + } + self.marks.get(index) + } + + /// What the pass found of slots it took, each with the write it was + /// handed and the frame it was found in, told under one take of the + /// lock, as [`HostHeap::texture_slot_done`], `_recheck`, `_later` and + /// `_held` tell it one at a time. Leaves `told` empty. + pub(crate) fn texture_slots_told(&self, told: &mut Vec<(u32, u64, u64, Told)>) { + if told.is_empty() { + return; + } + let mut textures = self.queue(super::queue_site::TELL); + for (index, write, frame, what) in told.drain(..) { + match what { + Told::Done(again) => { + textures.note_served(index, write, frame); + match again { + Some((due, mapped)) => textures.partly(index, write, due, mapped), + None => textures.done(index, write), + } + } + Told::Recheck(due, at, print, looks, uploaded) => { + textures.note_served(index, write, frame); + textures.recheck(index, write, due, at, print, looks, uploaded); + } + Told::Later(at, most) => textures.later(index, write, at, most), + Told::Held(due) => textures.hold(index, write, due), + } + } + } + /// A number that changes when a slot is written or made due anew, and /// whether memory was noted since a pass last took the notes. pub(crate) fn texture_news(&self) -> (u64, bool) { @@ -5978,6 +6062,75 @@ mod tests { assert_eq!(marks(&heap), held(&[])); } + /// The slots due come in their order with what the queue knew of each + /// (a slot uploaded whole and waiting for a look again), as asked slot + /// by slot. + #[test] + fn the_slots_due_come_with_what_is_known_of_them() { + let mut q = TextureQueue::default(); + for i in 0..4u32 { + q.written(30 + i, 1, 0, false); + } + let write = |q: &TextureQueue, index: u32| q.due(u64::MAX, 100).iter().find(|d| d.0 == index).unwrap().1; + let w31 = write(&q, 31); + q.recheck(31, w31, 5, (0x2000, 4096), 9, 0, 2); + let w32 = write(&q, 32); + q.later(32, w32, 2, 4); + let (order, known) = q.due_and_census(u64::MAX).0.ordered(100); + assert_eq!(order, q.due(u64::MAX, 100)); + assert_eq!(known.len(), order.len()); + for (&(index, _), knew) in order.iter().zip(&known) { + assert_eq!(*knew, q.checked(index)); + } + assert_eq!(known.iter().filter(|k| k.is_some()).count(), 1); + let (order, known) = q.due_and_census(u64::MAX).0.ordered(1); + assert_eq!((order.len(), known.len()), (1, 1)); + } + + /// What the pass tells of several slots under one take of the lock ends + /// as told slot by slot. + #[test] + fn slots_told_together_end_as_told_one_at_a_time() { + let (Some(a), Some(b)) = (HostHeap::new(false, 8), HostHeap::new(false, 8)) else { + return; // no host GPU + }; + for heap in [&a, &b] { + for i in 0..6 { + heap.queue_texture(i, true, false, 10); + } + } + let due = |heap: &HostHeap, at: u64| heap.texture_slots_due(at, 8).0; + let handed = due(&a, u64::MAX); + assert_eq!(handed, due(&b, u64::MAX)); + let w = |i: u32| handed.iter().find(|d| d.0 == i).unwrap().1; + let what = [ + (0, Told::Done(None)), + (1, Told::Done(Some((12, 4096)))), + (2, Told::Recheck(14, (0x1000, 64), 7, 0, 10)), + (3, Told::Later(10, 4)), + (4, Told::Held(30)), + // A write the queue does not hold any more: dropped. + (5, Told::Done(None)), + ]; + let mut told: Vec<_> = what.iter().map(|&(i, t)| (i, if i == 5 { 0 } else { w(i) }, 10, t)).collect(); + a.texture_slots_told(&mut told); + assert!(told.is_empty()); + b.texture_slot_done(0, w(0), None); + b.texture_slot_done(1, w(1), Some((12, 4096))); + b.texture_slot_recheck(2, w(2), 14, (0x1000, 64), 7, 0, 10); + b.texture_slot_later(3, w(3), 10, 4); + b.texture_slot_held(4, w(4), 30); + b.texture_slot_done(5, 0, None); + for at in [10, 11, 12, 14, 30, u64::MAX] { + assert_eq!(due(&a, at), due(&b, at), "due at {at}"); + } + for i in 0..6 { + assert_eq!(a.texture_slot_checked(i), b.texture_slot_checked(i)); + assert_eq!(a.texture_slot_mapped(i), b.texture_slot_mapped(i)); + assert_eq!(a.texture_slot_write(i).map(|w| w != 0), b.texture_slot_write(i).map(|w| w != 0)); + } + } + /// The queue's marks say which slots it holds, by the write of each /// entry: set by writes and memory made due, kept by looks, uploads in /// part and makes of a slot it holds, cleared when the slot leaves. diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index 821c6a7..4f6ac35 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -968,6 +968,70 @@ fn outcome(which: usize) { HEAP_TEXTURE_SLOTS[which].fetch_add(1, Ordering::Relaxed); } +/// Whether the pass over textures picked by index tells the queue what came +/// of its slots a batch at a time ([`Tells`]), and takes what the queue +/// knows of a slot looked at again with the slots due +/// (`HostHeap::texture_slots_due_known`; `VICTORY_QUEUE_BATCH=0`: each told +/// and asked at its turn, a take of the queue's lock each, as before). +fn queue_batch() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("VICTORY_QUEUE_BATCH").is_none_or(|v| v != "0")) +} + +/// What the pass over textures picked by index found of its slots, told to +/// the queue [`Tells::BATCH`] slots at a time and when the pass ends +/// (`HostHeap::texture_slots_told`): a take of the queue's lock for a batch, +/// where each slot took it once or twice (90,000-250,000 slots in 5 s of a +/// cutscene, coolfox-20261126-233017, while the recording threads took it for +/// every record they made). A slot's entry is told of after a few more slots' +/// work, as it would be if the pass's thread had been held up for that long; +/// the queue drops what is told of a write that is not the one it holds +/// (`TextureQueue::same`), as before. With [`queue_batch`] off, each at once. +struct Tells<'a> { + host: &'a crate::descriptor::host::HostHeap, + told: Vec<(u32, u64, u64, crate::descriptor::host::Told)>, +} + +impl Tells<'_> { + /// Few slots' work (some microseconds each), so that a pass that takes + /// the slots due meanwhile on another thread finds few of these told + /// late. + const BATCH: usize = 16; + + fn tell(&mut self, index: u32, write: u64, what: crate::descriptor::host::Told) { + use crate::descriptor::host::Told; + let host = self.host; + if !queue_batch() { + rest(0, || match what { + Told::Done(again) => host.texture_slot_done(index, write, again), + Told::Recheck(due, at, print, looks, uploaded) => { + host.texture_slot_recheck(index, write, due, at, print, looks, uploaded); + } + Told::Later(frame, most) => host.texture_slot_later(index, write, frame, most), + Told::Held(due) => host.texture_slot_held(index, write, due), + }); + return; + } + self.told.push((index, write, crate::frame::current(), what)); + if self.told.len() >= Self::BATCH { + self.flush(); + } + } + + fn flush(&mut self) { + if !self.told.is_empty() { + let host = self.host; + rest(0, || host.texture_slots_told(&mut self.told)); + } + } +} + +impl Drop for Tells<'_> { + fn drop(&mut self) { + self.flush(); + } +} + /// Of [`HEAP_TEXTURE_REST`]' place 4 (slots shown after their upload, /// flipped or made at once), since the last 5-second line: nanoseconds and /// calls of slots made at once (`HostHeap::mirror` by the pass), of slots @@ -3058,16 +3122,23 @@ impl State { before(0, || host.texture_slots_waiting(frame)); } let required = PASS_BUDGET.get().is_some_and(|(_, _, bake)| bake); - let (mut due, census) = before(2, || host.texture_slots_due(frame, if baking { 1 << 16 } else { 4096 })); + // (And what the queue knew of each, `known`: see `Tells`.) + let (mut due, mut known, census) = + before(2, || host.texture_slots_due_known(frame, if baking { 1 << 16 } else { 4096 })); // (Those that are due later after all that are due.) let on_time = due.len(); if baking { before(2, || { - let known: std::collections::HashSet = due.iter().map(|&(index, _)| index).collect(); + let taken: std::collections::HashSet = due.iter().map(|&(index, _)| index).collect(); static AHEAD: AtomicU64 = AtomicU64::new(u64::MAX); let again = AHEAD.swap(frame, Ordering::Relaxed) == frame; - let (ahead, _) = host.texture_slots_due(frame + BAKE_AHEAD[usize::from(again)], 1 << 16); - due.extend(ahead.into_iter().filter(|(index, _)| !known.contains(index))); + let (ahead, ahead_known, _) = host.texture_slots_due_known(frame + BAKE_AHEAD[usize::from(again)], 1 << 16); + for (slot, knew) in ahead.into_iter().zip(ahead_known) { + if !taken.contains(&slot.0) { + due.push(slot); + known.push(knew); + } + } }); } HEAP_TEXTURES_WAITING.store(census[0], Ordering::Relaxed); @@ -3149,7 +3220,9 @@ impl State { BAKE_SERVED.store(0, Ordering::Relaxed); } let mut served = true; - for (index, write) in due { + let mut tells = Tells { host, told: Vec::with_capacity(Tells::BATCH) }; + use crate::descriptor::host::Told; + for ((index, write), knew) in due.into_iter().zip(known) { let at_time = began.elapsed() > span; if at_time || SPENT.load(Ordering::Relaxed) >= bytes_at_most { HEAP_TEXTURE_PASSES[if at_time { 6 } else { 7 }].fetch_add(1, Ordering::Relaxed); @@ -3182,16 +3255,21 @@ impl State { // (all their blocks are shown at other addresses too), and each // was shown four frames late, over and over. let mut again_in = RECHECK_FRAMES[0]; - if let Some((at, len, print, looks, uploaded)) = rest(0, || host.texture_slot_checked(index)) { + // What the queue knew of it when the slots due were taken, while + // its marks say it holds the write handed out still; else (or with + // `VICTORY_QUEUE_BATCH=0`) asked now. + let checked = match knew { + knew if queue_batch() && host.texture_slot_write(index) == Some(write) => knew, + _ => rest(0, || host.texture_slot_checked(index)), + }; + if let Some((at, len, print, looks, uploaded)) = checked { let looked = timed(0, || crate::texture::fingerprint(at, len)); if looked == print { HEAP_TEXTURES_RECHECKED[0].fetch_add(1, Ordering::Relaxed); outcome(slot_outcome::UNCHANGED); - rest(0, || match RECHECK_FRAMES.get(usize::from(looks) + 1) { - Some(wait) => { - host.texture_slot_recheck(index, write, frame + wait, (at, len), print, looks + 1, uploaded) - } - None => host.texture_slot_done(index, write, None), + tells.tell(index, write, match RECHECK_FRAMES.get(usize::from(looks) + 1) { + Some(wait) => Told::Recheck(frame + wait, (at, len), print, looks + 1, uploaded), + None => Told::Done(None), }); continue; } @@ -3233,10 +3311,8 @@ impl State { HEAP_TEXTURES_AWAY.fetch_add(1, Ordering::Relaxed); outcome(slot_outcome::AWAY); // Streamed out: what the slot showed is let go. - rest(0, || { - host.texture_slot_gone(index); - host.texture_slot_later(index, write, frame, most); - }); + rest(0, || host.texture_slot_gone(index)); + tells.tell(index, write, Told::Later(frame, most)); continue; } rest(0, || host.texture_slot_here(index)); @@ -3245,7 +3321,7 @@ impl State { if mapped <= rest(0, || host.texture_slot_mapped(index)) { HEAP_TEXTURES_PARTLY[0].fetch_add(1, Ordering::Relaxed); outcome(slot_outcome::NO_MORE); - rest(0, || host.texture_slot_later(index, write, frame, most)); + tells.tell(index, write, Told::Later(frame, most)); continue; } if !crate::texture::committed(address as *const u8, 1) { @@ -3264,7 +3340,7 @@ impl State { { outcome(slot_outcome::NOTHING); HEAP_TEXTURE_NOTHING[0].fetch_add(1, Ordering::Relaxed); - rest(0, || host.texture_slot_done(index, write, None)); + tells.tell(index, write, Told::Done(None)); continue; } // From here on a host texture is made: not without room. The @@ -3290,7 +3366,7 @@ impl State { // Held back for the frames of about four seconds // (`most`), or until the card has room; the pass // goes on, within a count that bounds one pass. - rest(0, || host.texture_slot_held(index, write, frame + most)); + tells.tell(index, write, Told::Held(frame + most)); if refused > 1024 { break; } @@ -3363,7 +3439,7 @@ impl State { at_once(true); outcome(slot_outcome::NOTHING); HEAP_TEXTURE_NOTHING[1].fetch_add(1, Ordering::Relaxed); - rest(0, || host.texture_slot_done(index, write, None)); + tells.tell(index, write, Told::Done(None)); continue; } }; @@ -3378,7 +3454,7 @@ impl State { // a render target, a depth or unordered access texture: the host's own outcome(slot_outcome::NOTHING); HEAP_TEXTURE_NOTHING[2].fetch_add(1, Ordering::Relaxed); - rest(0, || host.texture_slot_done(index, write, None)); + tells.tell(index, write, Told::Done(None)); continue; } // A texture that has a staging buffer now is one draws read @@ -3419,11 +3495,9 @@ impl State { let memory = if again.is_none() && heap_recheck() { crate::texture::title_span(resource, first) } else { None }; memory.map(|(at, len)| (at, len, crate::texture::fingerprint(at, len))) }); - rest(0, || match memory { - Some((at, len, print)) => { - host.texture_slot_recheck(index, write, frame + again_in, (at, len), print, 0, frame); - } - None => host.texture_slot_done(index, write, again), + tells.tell(index, write, match memory { + Some((at, len, print)) => Told::Recheck(frame + again_in, (at, len), print, 0, frame), + None => Told::Done(again), }); continue; } @@ -3531,7 +3605,7 @@ impl State { // detile and upload all of it once more. HEAP_TEXTURES_TABLED.fetch_add(1, Ordering::Relaxed); outcome(slot_outcome::TABLED); - rest(0, || host.texture_slot_done(index, write, again)); + tells.tell(index, write, Told::Done(again)); continue; } // Uploaded: the list holds the staging buffer until it has run; @@ -3553,14 +3627,14 @@ impl State { } HEAP_TEXTURE_BYTES.fetch_add(bytes, Ordering::Relaxed); HEAP_TEXTURES.fetch_add(1, Ordering::Relaxed); - rest(0, || match memory { + tells.tell(index, write, match memory { // Whole and uploaded: looked at again (see above). - Some((at, len, print)) => { - host.texture_slot_recheck(index, write, frame + again_in, (at, len), print, 0, frame); - } - None => host.texture_slot_done(index, write, again), + Some((at, len, print)) => Told::Recheck(frame + again_in, (at, len), print, 0, frame), + None => Told::Done(again), }); } + // Told before anything after the loop looks at the queue. + tells.flush(); if sync.is_none() && baking && served { BAKE_SERVED.store(host.texture_news().0 + 1, Ordering::Relaxed); }