Merge local/waits (second version)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 22:16:01 -06:00
commit e1702e2204
4 files changed
+539 -151

No files matched your search

+76 -17
View File
@@ -1678,10 +1678,9 @@ enum Step {
/// A copy into title memory (destination, source, bytes): the title's
/// buffer copies, which later refreshes must see.
Copy(usize, usize, usize),
/// A copy of title memory into the list's own upload memory; if a
/// descriptor slot shows that memory and nothing else does (a constant
/// buffer of a table made for the draw), the title heap and the slot.
Fill(usize, usize, usize, Option<(usize, u32)>),
/// A copy of title memory into the list's own upload memory, and how
/// the card could bring the bytes instead.
Fill(usize, usize, usize, Option<card_steps::FillTo>),
/// Brings a range (address, bytes) of a buffer's host copy up to date:
/// once per submission, whichever lists ask, until a `Copy` or `Run`
/// intervenes.
@@ -2295,13 +2294,23 @@ fn refused(why: &'static str) {
*BLAMED.lock().unwrap_or_else(|e| e.into_inner()) = (None, why);
}
/// The wait that comes next is for these ranges ([`stale_under_flights`]).
#[cfg(feature = "diagnostics")]
fn blame_stale(stale: &[(std::sync::Arc<crate::shadow::Shadow>, usize, usize)]) {
if let Some((shadow, from, _)) = stale.first() {
let (start, len) = shadow.span();
let key = ("title memory changed under shader writes", crate::shadow::stale_why(), 0, start as usize, len as usize);
BLAMED.lock().unwrap_or_else(|e| e.into_inner()).0 = Some((key, *from));
}
}
/// The wait for what [`blame`] last named, or a wait of another reason than
/// title memory read, took `us`.
#[cfg(feature = "diagnostics")]
fn blamed(why: usize, us: u64) {
let (named, refused) = std::mem::replace(&mut *BLAMED.lock().unwrap_or_else(|e| e.into_inner()), (None, ""));
let (key, at) = match (why, named) {
(WHY_READ, Some(named)) => named,
(WHY_READ | WHY_STALE, Some(named)) => named,
(WHY_READ, None) => (("a host copy of what a copy done on the card wrote", refused, 0, 0, 0), 0),
_ => {
let kind = match why {
@@ -2346,7 +2355,11 @@ fn log_blame(frames: f64) {
us as f64 / 1e3 / frames,
n as f64 / frames,
passes.join(", "),
if why.is_empty() { "" } else { "; not done on the card: " },
match (why.is_empty(), kind) {
(true, _) => "",
(_, "title memory changed under shader writes") => "; not put in under them: ",
_ => "; not done on the card: ",
},
);
}
}
@@ -2420,7 +2433,7 @@ fn blocked_by(written: &[(usize, usize)], runs: &[&HostList], count: bool) -> Op
/// may write and that the title wrote since it was last copied. The
/// refresh leaves ranges shaders are writing alone (`Shadow::update`), so
/// the title's bytes there would reach the card a submission late.
fn stale_under_flights(runs: &[&HostList]) -> bool {
fn stale_under_flights(runs: &[&HostList]) -> Vec<(std::sync::Arc<crate::shadow::Shadow>, usize, usize)> {
use std::sync::Arc;
// Per copy, the span of what is refreshed (a flush brings that much
// up to date at most).
@@ -2441,11 +2454,15 @@ fn stale_under_flights(runs: &[&HostList]) -> bool {
// The title's bytes are put in beside the flights' writes; a
// settle is still due where that could not be done. At most this
// many changed runs go up to copies in the card's memory, each a
// copy of its own there.
let mut runs_left = 512;
return spans.iter().any(|(shadow, from, to)| shadow.merge_under_writes(*from, *to - *from, &mut runs_left));
// copy of its own there. With [`card_steps`] the pages of the
// title's ring of constants come here too, which were waited for
// before: hundreds of short runs each, at about 1.5 us a run.
let mut runs_left = if card_steps::stage() > 0 { 4096 } else { 512 };
spans.retain(|(shadow, from, to)| shadow.merge_under_writes(*from, *to - *from, &mut runs_left));
return spans;
}
spans.iter().any(|(shadow, from, to)| shadow.stale_under_writes(*from, *to - *from))
spans.retain(|(shadow, from, to)| shadow.stale_under_writes(*from, *to - *from));
spans
}
/// A fence signal on a queue ([`late`]): goes behind the flights in the
@@ -4438,6 +4455,40 @@ impl Host {
working(was_working);
}
/// For a submission of `runs` that refreshes the ranges `stale` of host
/// copies, which flights may be writing and the title wrote too
/// ([`stale_under_flights`]): the flights land first. `ranges` are
/// those of the submission's one asking, which is made again.
///
/// With [`card_steps`] only the flights that declared them: a range of
/// a copy nobody declared any more is refreshed like any other. What
/// they landed is title memory written, so everything is asked and
/// looked at again; what is left after three times waits as before.
fn settle_stale(
&self,
submitted: &mut u64,
runs: &[&HostList],
mut stale: Vec<(std::sync::Arc<crate::shadow::Shadow>, usize, usize)>,
ranges: &[(usize, usize)],
) {
if relay_exact() && card_steps::stage() > 0 {
for _ in 0..3 {
#[cfg(feature = "diagnostics")]
blame_stale(&stale);
relay_wait(WHY_STALE, |_| stale.iter().all(|(shadow, from, to)| !shadow.writes_pending_in(*from, *to)));
crate::watch::ask_once(ranges);
stale = stale_under_flights(runs);
if stale.is_empty() {
return;
}
}
}
#[cfg(feature = "diagnostics")]
blame_stale(&stale);
self.settle(submitted, WHY_STALE);
crate::watch::ask_once(ranges);
}
/// [`Host::execute`], or with `late` the first half of
/// [`Host::execute_late`]: the lists are prepared and sent, and what is
/// to be done once they have run comes back with the submission lock
@@ -4539,13 +4590,13 @@ impl Host {
// may write too, and this submission refreshes it. The flights
// are settled, and the asking made again: the copy-back wrote
// title memory, which other copies of it must see.
if behind && stale_under_flights(runs) {
let stale = if behind { stale_under_flights(runs) } else { Vec::new() };
if !stale.is_empty() {
let settling = std::time::Instant::now();
self.settle(&mut submitted, WHY_STALE);
self.settle_stale(&mut submitted, runs, stale, &ranges);
settled += settling.elapsed();
crate::watch::ask_once(&ranges);
}
} else if behind && stale_under_flights(runs) {
} else if behind && !stale_under_flights(runs).is_empty() {
let settling = std::time::Instant::now();
self.settle(&mut submitted, WHY_STALE);
settled += settling.elapsed();
@@ -5498,7 +5549,14 @@ impl HostList {
pub(crate) fn defer_constants(&self, dst: *mut u8, src: *const u8, len: usize, slot: (usize, u32)) {
self.deferred()
.copies
.push(Step::Fill(dst as usize, src as usize, len, Some(slot)));
.push(Step::Fill(dst as usize, src as usize, len, Some(card_steps::FillTo::Slot(slot.0, slot.1))));
}
/// As [`HostList::defer_fill`], for the beginning of a buffer
/// [`HostList::upload`] made.
pub(crate) fn defer_fill_buffer(&self, buffer: &ID3D12Resource, dst: *mut u8, src: *const u8, len: usize) {
let to = (card_steps::stage() > 1).then(|| card_steps::FillTo::Buffer(buffer.clone()));
self.deferred().copies.push(Step::Fill(dst as usize, src as usize, len, to));
}
/// Brings `[address, address + len)` of a buffer's host copy up to date
@@ -5550,7 +5608,8 @@ impl HostList {
///
/// shortcut: one buffer per copy; pool them once uploads are frequent.
pub(crate) fn upload(&self, host: &Host, size: u64) -> Option<(ID3D12Resource, *mut u8)> {
let (buffer, data) = host.upload_buffer(size)?;
// With [`card_steps`] the card may be the one that fills it.
let (buffer, data) = if card_steps::stage() > 1 { host.shared_buffer(size)? } else { host.upload_buffer(size)? };
name(&buffer, || format!("list upload {size:#x}"));
self.deferred().hold_upload(buffer.clone(), size);
Some((buffer, data))