Merge agent/texqueue-ports: texture queue lock work (marks, batched tells, split lines; on), VICTORY_TABLED_REREAD and VICTORY_QUEUE_HOST_TEXTURES (off); ports of Belobug/VIctory-Belobug-AMD c278f0d, 4cd2d13+91018a5, 5d6bd84 (VICTORY_POOL_SMALL, VICTORY_SHOW_NEW_LATER, VICTORY_PRELUDE_UPLOADS; off)
Chain z1 (from the main path): A (new defaults) gameplay 33.9 ms p95 34.7, cutscene 38.8 ms p95 55; B (VICTORY_QUEUE_MARKS=0 VICTORY_QUEUE_BATCH=0, the old behaviour) gameplay 39.5 ms p95 54, cutscene 40.4 ms; C (TABLED_REREAD=1) 37.2 / 40.2 ms; D (QUEUE_HOST_TEXTURES=0) 36.4 / 41.8 ms. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
commit
2ff3e4a3a0
8 files changed
+1390
-125
No files matched your search
+204
-66
@@ -968,6 +968,116 @@ 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<bool> = 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
|
||||
/// shown after their upload (`texture_slot_show_after`), and of slots
|
||||
/// flipped (`texture_slot_flip`, or made at once as they show an earlier
|
||||
/// view of the same texture): [ns made, ns after, ns flipped, made, after,
|
||||
/// flipped].
|
||||
pub(crate) static HEAP_TEXTURE_SHOWN: [AtomicU64; 6] = [const { AtomicU64::new(0) }; 6];
|
||||
|
||||
/// `f`'s result, its time added to [`HEAP_TEXTURE_REST`]' place 4 and to
|
||||
/// [`HEAP_TEXTURE_SHOWN`]' place `which`.
|
||||
fn shown<T>(which: usize, f: impl FnOnce() -> T) -> T {
|
||||
let started = std::time::Instant::now();
|
||||
let out = f();
|
||||
let ns = started.elapsed().as_nanos() as u64;
|
||||
HEAP_TEXTURE_REST[4].fetch_add(ns, Ordering::Relaxed);
|
||||
HEAP_TEXTURE_SHOWN[which].fetch_add(ns, Ordering::Relaxed);
|
||||
HEAP_TEXTURE_SHOWN[3 + which].fetch_add(1, Ordering::Relaxed);
|
||||
out
|
||||
}
|
||||
|
||||
/// Of the slots the pass found nothing to bring up to date for
|
||||
/// ([`slot_outcome::NOTHING`]), since the last 5-second line: views of a
|
||||
/// texture object the host renders to or writes (the queue held them for
|
||||
/// nothing: a host texture is never uploaded); slots with no texture view
|
||||
/// or no texture the host can make; slots whose host texture is the host's
|
||||
/// own (a target, depth or unordered access texture placed in title
|
||||
/// memory); and of all these, the ones the pass made at once.
|
||||
pub(crate) static HEAP_TEXTURE_NOTHING: [AtomicU64; 4] = [const { AtomicU64::new(0) }; 4];
|
||||
|
||||
/// Of [`HEAP_TEXTURE_REST`]' place 7 (the passes' work before their loops),
|
||||
/// since the last 5-second line, in nanoseconds: slots made due (room on the
|
||||
/// card again, memory mapped, unmapped or written, levels waiting); views
|
||||
/// made again and slots flipped after earlier uploads; the slots due taken
|
||||
/// from the queue and ordered. (The rest of it: budgets, counts, the card
|
||||
/// accounting line.)
|
||||
pub(crate) static HEAP_TEXTURE_BEFORE: [AtomicU64; 3] = [const { AtomicU64::new(0) }; 3];
|
||||
|
||||
/// `f`'s result, its time added to [`HEAP_TEXTURE_BEFORE`]' place `slot`.
|
||||
fn before<T>(slot: usize, f: impl FnOnce() -> T) -> T {
|
||||
let started = std::time::Instant::now();
|
||||
let out = f();
|
||||
HEAP_TEXTURE_BEFORE[slot].fetch_add(started.elapsed().as_nanos() as u64, Ordering::Relaxed);
|
||||
out
|
||||
}
|
||||
|
||||
/// Passes since the last 5-second line by kind (the frame's, from
|
||||
/// [`State::heap_buffers`]; the early one at a frame's first draw; before a
|
||||
/// bake draw), their loops' nanoseconds by kind, and the passes that stopped
|
||||
@@ -2983,40 +3093,53 @@ impl State {
|
||||
once!("textures picked by index: the card has no room for more; they wait");
|
||||
}
|
||||
let mut refused = 0;
|
||||
// Room again: the slots held back while the card was full are due
|
||||
// now (`full_card_pass`).
|
||||
if room {
|
||||
host.texture_slots_room(frame);
|
||||
}
|
||||
// Slots whose texture's memory the runtime mapped, unmapped or
|
||||
// wrote since the last pass are due again.
|
||||
host.texture_slots_remapped(frame);
|
||||
let baking = crate::texture::BAKING.get();
|
||||
if baking {
|
||||
host.texture_slots_bake_written(frame);
|
||||
}
|
||||
host.texture_slots_short(frame);
|
||||
// The slots of textures whose first level with memory (levels on
|
||||
// demand), or whose host copy for one with more levels, changed since
|
||||
// the last pass are made again from it; those whose new view's upload
|
||||
// has run are made from that.
|
||||
host.texture_views_remade();
|
||||
host.texture_slots_flip();
|
||||
before(0, || {
|
||||
// Room again: the slots held back while the card was full are
|
||||
// due now (`full_card_pass`).
|
||||
if room {
|
||||
host.texture_slots_room(frame);
|
||||
}
|
||||
// Slots whose texture's memory the runtime mapped, unmapped or
|
||||
// wrote since the last pass are due again.
|
||||
host.texture_slots_remapped(frame);
|
||||
if baking {
|
||||
host.texture_slots_bake_written(frame);
|
||||
}
|
||||
host.texture_slots_short(frame);
|
||||
});
|
||||
before(1, || {
|
||||
// The slots of textures whose first level with memory (levels
|
||||
// on demand), or whose host copy for one with more levels,
|
||||
// changed since the last pass are made again from it; those
|
||||
// whose new view's upload has run are made from that.
|
||||
host.texture_views_remade();
|
||||
host.texture_slots_flip();
|
||||
});
|
||||
// And with room, the textures whose upload left a held level
|
||||
// without the card's memory are uploaded again.
|
||||
if room {
|
||||
host.texture_slots_waiting(frame);
|
||||
before(0, || host.texture_slots_waiting(frame));
|
||||
}
|
||||
let required = PASS_BUDGET.get().is_some_and(|(_, _, bake)| bake);
|
||||
let (mut due, census) = 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 {
|
||||
let known: std::collections::HashSet<u32> = 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)));
|
||||
before(2, || {
|
||||
let taken: std::collections::HashSet<u32> = due.iter().map(|&(index, _)| index).collect();
|
||||
static AHEAD: AtomicU64 = AtomicU64::new(u64::MAX);
|
||||
let again = AHEAD.swap(frame, Ordering::Relaxed) == frame;
|
||||
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);
|
||||
for (count, now) in HEAP_TEXTURES_QUEUE.iter().zip(census) {
|
||||
@@ -3097,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);
|
||||
@@ -3130,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;
|
||||
}
|
||||
@@ -3181,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));
|
||||
@@ -3193,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) {
|
||||
@@ -3211,7 +3339,8 @@ impl State {
|
||||
&& !crate::resource::title_fills(object as This)
|
||||
{
|
||||
outcome(slot_outcome::NOTHING);
|
||||
rest(0, || host.texture_slot_done(index, write, None));
|
||||
HEAP_TEXTURE_NOTHING[0].fetch_add(1, Ordering::Relaxed);
|
||||
tells.tell(index, write, Told::Done(None));
|
||||
continue;
|
||||
}
|
||||
// From here on a host texture is made: not without room. The
|
||||
@@ -3237,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;
|
||||
}
|
||||
@@ -3283,16 +3412,19 @@ impl State {
|
||||
Some((view, key)) => (Some(view), Some(key)),
|
||||
None => (None, None),
|
||||
}
|
||||
} else if shows_other || late {
|
||||
// The view read at the start of this slot's turn (read
|
||||
// again, the views' table was asked twice a slot).
|
||||
(held.clone(), None)
|
||||
} else {
|
||||
let view = heap.handle(index).and_then(|handle| {
|
||||
if shows_other || late { crate::descriptor::view(handle) } else { host.mirror(index, handle, true) }
|
||||
});
|
||||
(view, None)
|
||||
(heap.handle(index).and_then(|handle| host.mirror(index, handle, true)), None)
|
||||
}
|
||||
});
|
||||
let at_once = || {
|
||||
// (`nothing`: the slot has nothing to bring up to date.)
|
||||
let at_once = |nothing: bool| {
|
||||
if (late || bake_key.is_some()) && let Some(handle) = heap.handle(index) {
|
||||
rest(4, || {
|
||||
HEAP_TEXTURE_NOTHING[3].fetch_add(u64::from(nothing), Ordering::Relaxed);
|
||||
shown(0, || {
|
||||
let _ = host.mirror(index, handle, true);
|
||||
});
|
||||
}
|
||||
@@ -3305,9 +3437,10 @@ impl State {
|
||||
let resource = match placed.as_ref().or(view.as_ref()) {
|
||||
Some(&View::Shader(resource, _)) => resource as This,
|
||||
_ => {
|
||||
at_once();
|
||||
at_once(true);
|
||||
outcome(slot_outcome::NOTHING);
|
||||
rest(0, || host.texture_slot_done(index, write, None));
|
||||
HEAP_TEXTURE_NOTHING[1].fetch_add(1, Ordering::Relaxed);
|
||||
tells.tell(index, write, Told::Done(None));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
@@ -3318,10 +3451,11 @@ impl State {
|
||||
}
|
||||
let title_fills = rest(6, || texture.is_some_and(crate::texture::title_written));
|
||||
if !title_fills {
|
||||
at_once();
|
||||
at_once(true);
|
||||
// a render target, a depth or unordered access texture: the host's own
|
||||
outcome(slot_outcome::NOTHING);
|
||||
rest(0, || host.texture_slot_done(index, write, None));
|
||||
HEAP_TEXTURE_NOTHING[2].fetch_add(1, Ordering::Relaxed);
|
||||
tells.tell(index, write, Told::Done(None));
|
||||
continue;
|
||||
}
|
||||
// A texture that has a staging buffer now is one draws read
|
||||
@@ -3344,17 +3478,17 @@ impl State {
|
||||
sync.publish.push((index, handle, key));
|
||||
} else {
|
||||
if shows_other && let Some(handle) = heap.handle(index) {
|
||||
rest(4, || {
|
||||
shown(2, || {
|
||||
let _ = host.mirror(index, handle, true);
|
||||
});
|
||||
}
|
||||
// Its bytes are in, or on the way there in a list before.
|
||||
if late && texture.is_some_and(crate::pool::fresh) && !crate::texture::ahead::there(resource) {
|
||||
if let Some(handle) = heap.handle(index) {
|
||||
rest(4, || host.texture_slot_show_after(list, index, handle));
|
||||
shown(1, || host.texture_slot_show_after(list, index, handle));
|
||||
}
|
||||
} else {
|
||||
at_once();
|
||||
at_once(false);
|
||||
}
|
||||
}
|
||||
let again = partly.map(|mapped| (frame + 1, mapped));
|
||||
@@ -3362,11 +3496,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;
|
||||
}
|
||||
@@ -3422,10 +3554,10 @@ impl State {
|
||||
sync.publish.push((index, handle, key));
|
||||
}
|
||||
if shows_other && let Some(handle) = heap.handle(index) {
|
||||
rest(4, || host.texture_slot_flip(list, index, handle));
|
||||
shown(2, || host.texture_slot_flip(list, index, handle));
|
||||
}
|
||||
if late && let Some(handle) = heap.handle(index) {
|
||||
rest(4, || host.texture_slot_show_after(list, index, handle));
|
||||
shown(1, || host.texture_slot_show_after(list, index, handle));
|
||||
}
|
||||
#[cfg(feature = "diagnostics")]
|
||||
if crate::book::on()
|
||||
@@ -3474,7 +3606,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;
|
||||
@@ -3496,14 +3628,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);
|
||||
}
|
||||
@@ -4286,9 +4418,15 @@ impl State {
|
||||
}
|
||||
}
|
||||
}
|
||||
// (Told once per record and recording; with
|
||||
// `VICTORY_TABLED_REREAD=1` also of a slot the queue
|
||||
// holds again since: written after its first read.)
|
||||
if let Some(title) = title_first
|
||||
&& listed.texture
|
||||
&& listed.tabled != Some((title + k, kind.is_some()))
|
||||
&& (listed.tabled != Some((title + k, kind.is_some()))
|
||||
|| (crate::descriptor::host::tabled_reread()
|
||||
&& kind.is_some()
|
||||
&& host.texture_slot_queued(title + k)))
|
||||
{
|
||||
listed.tabled = Some((title + k, kind.is_some()));
|
||||
host.texture_slot_tabled(title + k, kind.is_some());
|
||||
|
||||
Reference in new issue
Block a user