Merge local/next d4772b3

The check of fetched pieces (VICTORY_FETCH_CHECK) with the landing pool:
pieces are gathered and ordered as there; with the check whole pieces come
down and are hashed by the job that lands them. Off, the fetch is the
landing's own.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 22:36:00 -06:00
commit c11b92e9ac
31 files changed
+5562 -538

No files matched your search

+381 -95
View File
@@ -767,6 +767,10 @@ mod tests {
}
}
/// The batches of clamped records in one scratch buffer
/// ([`HostList::batch_place`]).
const BATCHES: u32 = 8;
/// A host command list and its allocator, always used together.
pub(crate) struct HostList {
allocator: ID3D12CommandAllocator,
@@ -802,6 +806,9 @@ pub(crate) struct HostList {
/// Device-local scratch buffer for clamped indirect arguments (`indirect`),
/// grown to the largest indirect execution recorded on the list.
indirect_args: Mutex<Option<(ID3D12Resource, u64)>>,
/// Scratch buffers of the batched clamps (`crate::indirect::batch`), and
/// how many batches of this recording have a place in them.
batch_args: Mutex<(Vec<ID3D12Resource>, u32)>,
/// Counts the list in [`ALIVE`] while it lives.
_alive: Counted,
/// The texts of the markers `note_op` set, in order (for device-removed
@@ -1646,7 +1653,8 @@ static CARD_PASSES: [std::sync::atomic::AtomicU64; 3] = [const { std::sync::atom
/// settles: about 630 pieces and 40 MB a frame (run 104), each compared
/// with the copy's CPU side byte by byte and put into title memory through
/// the kernel. A piece is its own bytes of its copy and of title memory,
/// so eight threads take an eighth of them each.
/// so eight threads take an eighth of them each. (Not with [`relay`], whose
/// landings do not come here: `crate::land` has threads of its own.)
fn wide_merge() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_WIDE_MERGE").is_some_and(|v| v == "1"))
@@ -1687,10 +1695,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.
@@ -1980,13 +1987,16 @@ fn landable(line: &[Option<u64>], completed: u64) -> usize {
/// For one plan of copies in the card's memory, with [`relay`]: `flags`
/// holds, for every piece of every range of the plan, the copy, the
/// piece's number in the copy and whether the compare flagged it there (or
/// did not run). The pieces to fetch, under whichever range: all of them
/// flagged under any. Two ranges of a plan may hold parts of the same
/// did not run). The pieces to fetch, under whichever range, in order: all
/// of them flagged under any. Two ranges of a plan may hold parts of the same
/// 64 KiB piece; the compare hashes the whole piece for each and stores
/// the hash, so the second of them may find it stored already and not
/// flag it.
fn flagged_pieces(flags: &[(usize, u32, bool)]) -> std::collections::HashSet<(usize, u32)> {
flags.iter().filter(|f| f.2).map(|f| (f.0, f.1)).collect()
fn flagged_pieces(flags: &[(usize, u32, bool)]) -> Vec<(usize, u32)> {
let mut pieces: Vec<(usize, u32)> = flags.iter().filter(|f| f.2).map(|f| (f.0, f.1)).collect();
pieces.sort_unstable();
pieces.dedup();
pieces
}
#[cfg(test)]
@@ -2301,13 +2311,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 {
@@ -2352,7 +2372,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: ",
},
);
}
}
@@ -2426,7 +2450,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).
@@ -2447,11 +2471,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
@@ -2672,6 +2700,8 @@ struct RelayFetch {
buffer: Option<(ID3D12Resource, usize, u64)>,
/// Pieces caught while a later flight was writing them.
suspects: torn::Suspects,
/// What earlier fetches brought of flights not landed yet.
had: crate::land::Had,
}
/// The thread that lands flights ([`relay`]): as soon as the card has run
@@ -2750,8 +2780,16 @@ fn relay_worker() {
/// in the PC's memory goes into title memory. As [`hand_back`], each byte
/// once, the pieces that differ found on several threads; but the merge is
/// `Shadow::relay_merge`, which does not write the copy, and the ranges
/// are not done with here (each flight's are, afterwards).
fn relay_hand_back(back: &[Declared]) {
/// are not done with here (each flight's are, afterwards). In two steps:
/// what is to be looked through and what of it differs ([`RelayBack::look`],
/// which needs nothing that the fetch from the card's memory brings), and
/// the merge.
struct RelayBack {
work: Vec<(std::sync::Arc<crate::shadow::Shadow>, usize, usize)>,
found: Option<Vec<Vec<(usize, usize)>>>,
}
fn relay_hand_back(back: &[Declared]) -> RelayBack {
let mut work: Vec<(std::sync::Arc<crate::shadow::Shadow>, usize, usize)> = Vec::new();
for (shadow, declared) in back {
if shadow.carded() {
@@ -2764,27 +2802,71 @@ fn relay_hand_back(back: &[Declared]) {
}
work.extend(spans.into_iter().map(|(from, to)| (shadow.clone(), from, to)));
}
if work.is_empty() {
return;
}
#[cfg(feature = "diagnostics")]
let scanning = std::time::Instant::now();
// Looked through without a lock: a piece that changes meanwhile (a
// later flight writing, the title's bytes going in) is found now or by
// that flight's landing, and the merge looks again under the lock.
let found = crate::shadow::find_all(&work);
#[cfg(feature = "diagnostics")]
crate::copystat::add(crate::copystat::C::BackScanUs, scanning.elapsed().as_micros() as u64);
let (mut now, mut title) = (Vec::new(), Vec::new());
for ((shadow, from, to), found) in work.iter().zip(found) {
let mut written = 0;
for (at, n) in found {
written += shadow.relay_merge(at, n, &mut now, &mut title);
RelayBack { work, found: None }
}
impl RelayBack {
/// Looks the ranges through, once; its time counts as `stat`.
fn look(&mut self, stat: usize) {
if self.work.is_empty() || self.found.is_some() {
return;
}
COPY_BACK[1].fetch_add(written as u64, Ordering::Relaxed);
let scanning = std::time::Instant::now();
// Looked through without a lock: a piece that changes meanwhile (a
// later flight writing, the title's bytes going in) is found now or
// by that flight's landing, and the merge looks again under the
// lock.
self.found = Some(crate::shadow::find_all(&self.work));
crate::land::spent(stat, scanning);
#[cfg(feature = "diagnostics")]
crate::copystat::compared(*from, to - from, written);
let _ = (from, to);
crate::copystat::add(crate::copystat::C::BackScanUs, scanning.elapsed().as_micros() as u64);
}
/// What differs goes into title memory.
fn merge(mut self) {
use crate::land;
self.look(land::stat::SCAN);
let Some(found) = self.found else {
return;
};
let merging = std::time::Instant::now();
let mut written: Vec<u64> = Vec::new();
if land::on() {
// Pieces in the same block of title memory in one job, in order.
let pieces: Vec<(usize, usize, usize)> =
found.iter().enumerate().flat_map(|(i, found)| found.iter().map(move |&(at, n)| (i, at, n))).collect();
let start = |i: usize| self.work[i].0.span().0 as usize;
let mut order: Vec<usize> = (0..pieces.len()).collect();
order.sort_by_key(|&p| (start(pieces[p].0) + pieces[p].1) / crate::land::BLOCK);
let cut: Vec<(usize, usize)> = order.iter().map(|&p| (start(pieces[p].0) + pieces[p].1, pieces[p].2)).collect();
let jobs = land::jobs(&cut);
let put: Vec<std::sync::atomic::AtomicU64> = self.work.iter().map(|_| Default::default()).collect();
land::each(jobs.len(), &|j| {
let working = std::time::Instant::now();
let mut notes = land::Notes::default();
for &p in &order[jobs[j].clone()] {
let (i, at, n) = pieces[p];
put[i].fetch_add(self.work[i].0.land_back(at, n, &mut notes), Ordering::Relaxed);
}
notes.flush();
land::spent(land::stat::BACK_ALL, working);
});
land::count(land::stat::JOBS, jobs.len() as u64);
written.extend(put.iter().map(|p| p.load(Ordering::Relaxed)));
} else {
let (mut now, mut title) = (Vec::new(), Vec::new());
for ((shadow, ..), found) in self.work.iter().zip(found) {
let merge = |(at, n)| shadow.relay_merge(at, n, &mut now, &mut title) as u64;
written.push(found.into_iter().map(merge).sum());
}
}
land::spent(land::stat::BACK, merging);
for ((_, from, to), written) in self.work.iter().zip(written) {
COPY_BACK[1].fetch_add(written, Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
crate::copystat::compared(*from, to - from, written as usize);
let _ = (from, to);
}
}
}
@@ -2862,6 +2944,7 @@ fn late_line(frames: f64) {
);
}
}
crate::land::log(frames);
card_steps::log(frames);
#[cfg(feature = "diagnostics")]
log_blame(frames);
@@ -3244,6 +3327,7 @@ impl Host {
budget: OnceLock::new(),
card_budget: OnceLock::new(),
indirect_args: Mutex::default(),
batch_args: Mutex::default(),
_alive: Counted::new(),
#[cfg(feature = "diagnostics")]
notes: Mutex::default(),
@@ -3673,6 +3757,7 @@ impl Host {
sent: 0,
buffer: None,
suspects: torn::Suspects::default(),
had: crate::land::Had::default(),
})
}
}
@@ -3686,33 +3771,103 @@ impl Host {
/// fetched under all ([`flagged_pieces`]); the fetch runs behind
/// whatever was sent meanwhile, so it may bring a later flight's
/// bytes, which that flight's landing then finds merged already; and
/// nothing is sent back up.
fn relay_card_back(&self, plans: &[&CardPlan], fetch: &mut Option<RelayFetch>) {
/// nothing is sent back up. `plans` come with the value signalled behind
/// their flight. `meanwhile` is called while the fetch is on its way, if
/// there is one.
fn relay_card_back(&self, plans: &[(&CardPlan, u64)], fetch: &mut Option<RelayFetch>, meanwhile: &mut dyn FnMut()) {
use crate::land;
let gathering = std::time::Instant::now();
let differs = {
let work = CARD_WORK.lock().unwrap_or_else(|e| e.into_inner());
work.as_ref().map(|work| (work.differs.1, (work.differs.2 / 4) as usize))
};
let mut pieces: Vec<(std::sync::Arc<crate::shadow::Shadow>, usize, usize, u64)> = Vec::new();
let mut total = 0u64;
if let (Some(fetch), Some(first)) = (fetch.as_mut(), plans.iter().map(|p| p.1).min()) {
fetch.had.landing(first);
}
// SAFETY: the mapping of the results. The compares of these plans
// have run, and their dwords are given to no other plan before these
// are done with (`card_done`).
let differs = differs.map(|(differs, dwords)| unsafe { std::slice::from_raw_parts(differs as *const u32, dwords) });
if let Some(differs) = differs {
// Pieces caught while a later flight was at them (`torn`).
let suspects = fetch.as_ref().map(|f| &f.suspects);
let mut seen = std::collections::HashSet::new();
for &(plan, behind) in plans {
let flags: Vec<(usize, u32, bool)> = plan
.ranges
.iter()
.flat_map(|r| {
(0..r.span.pieces).map(move |p| {
let flagged = !r.compared || differs.get((r.at + p) as usize).is_none_or(|&d| torn::flagged(d));
let wanted = suspects.map_or(flagged, |s| s.wanted((r.shadow.id(), r.span.first + p), flagged));
(std::sync::Arc::as_ptr(&r.shadow) as usize, r.span.first + p, wanted)
})
})
.collect();
let fetched = flagged_pieces(&flags);
for r in &plan.ranges {
let copy = std::sync::Arc::as_ptr(&r.shadow) as usize;
if upcheck::on() {
upcheck::declared(r.shadow.id(), r.span.from as usize, r.span.to as usize);
}
for p in 0..r.span.pieces {
if fetched.binary_search(&(copy, r.span.first + p)).is_err() {
continue;
}
let (lo, hi) = r.shadow.card_piece(&r.span, p);
if lo < hi && !seen.contains(&(copy, lo, hi)) {
let key = (r.shadow.id(), r.shadow.span().0 as usize + lo, hi - lo);
if fetch.as_ref().is_some_and(|f| f.had.holds(behind, key)) {
land::count(land::stat::HAD, 1);
land::count(land::stat::HAD_BYTES, (hi - lo) as u64);
if land::skip_on() {
continue;
}
}
if upcheck::on() {
upcheck::fetched(r.shadow.id(), lo);
}
seen.insert((copy, lo, hi));
pieces.push((r.shadow.clone(), lo, hi, total));
total += (hi - lo) as u64;
}
}
crate::shadow::CARD_STATS[2].fetch_add(u64::from(r.span.pieces), Ordering::Relaxed);
}
}
}
if land::on() {
// In the order they are landed, each byte of a copy once.
let keys: Vec<land::Piece> =
pieces.iter().map(|(shadow, lo, hi, _)| (shadow.id(), shadow.span().0 as usize + lo, hi - lo)).collect();
total = 0;
pieces = land::ordered(&keys)
.into_iter()
.map(|(i, title, len)| {
let shadow = &pieces[i].0;
let lo = title - shadow.span().0 as usize;
let at = total;
total += len as u64;
(shadow.clone(), lo, lo + len, at)
})
.collect();
}
land::spent(land::stat::GATHER, gathering);
if fetch.is_none() {
*fetch = self.make_relay_fetch();
}
// On the copy queue a later flight may be at a piece while it is
// fetched (`torn`).
let mut suspects = fetch.as_mut().filter(|f| f.queue.is_some() && torn::on()).map(|f| std::mem::take(&mut f.suspects));
let mut pieces = Vec::new();
if let Some((differs, dwords)) = differs {
// SAFETY: the mapping of the results. The compares of these
// plans have run, and their dwords are given to no other plan
// before these are done with (`card_done`).
let differs = unsafe { std::slice::from_raw_parts(differs as *const u32, dwords) };
pieces = torn::fetches(plans, differs, suspects.as_ref());
for r in plans.iter().flat_map(|plan| &plan.ranges) {
crate::shadow::CARD_STATS[2].fetch_add(u64::from(r.span.pieces), Ordering::Relaxed);
}
// fetched: the pieces come down whole, to be hashed.
let check = (torn::on() && fetch.as_ref().is_some_and(|f| f.queue.is_some()))
.then(|| torn::Check::new(plans.iter().map(|p| p.0), differs.unwrap_or_default(), &mut pieces));
if let Some(check) = &check {
total = check.total;
}
let total = pieces.last().map_or(0, |p| p.at + (p.to - p.from) as u64);
// The sends these flights ran behind, all a fetch on the copy queue
// is known to have run behind.
let behind = plans.iter().map(|p| p.sent_as).max().unwrap_or(0);
let behind = plans.iter().map(|p| p.0.sent_as).max().unwrap_or(0);
if pieces.is_empty() {
// Nothing to fetch: every send so far is older than the next
// fetch, if that goes to the queue the sends went to.
@@ -3721,9 +3876,6 @@ impl Host {
false => *QUEUE_ORDER.lock().unwrap_or_else(|e| e.into_inner()),
};
crate::shadow::relay_fetched(after);
if let (Some(f), Some(suspects)) = (fetch.as_mut(), suspects) {
f.suspects = suspects;
}
return;
}
let Some(f) = fetch.as_mut() else {
@@ -3740,6 +3892,12 @@ impl Host {
let ((allocator, list), fence, event, sent) = (f.list.clone(), f.fence.clone(), f.event, f.sent);
let copy_queue = f.queue.clone();
let waiting = std::time::Instant::now();
// The flights the fetch runs behind: those in the line by now have
// been given to the queue. On the copy queue, those landed here.
let ahead = match copy_queue {
Some(_) => 0,
None => line().target,
};
// SAFETY: the list's last run was waited for by this thread; live
// buffers (the copies are held by the plans); the fence and the
// event are this thread's.
@@ -3747,14 +3905,15 @@ impl Host {
(|| -> windows::core::Result<u64> {
allocator.Reset()?;
list.Reset(&allocator, None)?;
for p in &pieces {
list.CopyBufferRegion(&buffer, p.at, &p.shadow.buffer, p.from as u64, (p.to - p.from) as u64);
for (shadow, lo, hi, at) in check.as_ref().map_or(&pieces, |c| &c.copies) {
list.CopyBufferRegion(&buffer, *at, &shadow.buffer, *lo as u64, (*hi - *lo) as u64);
}
list.Close()?;
let lists = [Some(list.cast::<ID3D12CommandList>()?)];
if let Some(queue) = &copy_queue {
queue.ExecuteCommandLists(&lists);
queue.Signal(&fence, sent)?;
meanwhile();
fence.SetEventOnCompletion(sent, event)?;
WaitForSingleObject(event, INFINITE);
return Ok(behind);
@@ -3767,6 +3926,7 @@ impl Host {
drop(order);
self.card_sent();
self.queue.Signal(&fence, sent)?;
meanwhile();
fence.SetEventOnCompletion(sent, event)?;
WaitForSingleObject(event, INFINITE);
Ok(after)
@@ -3785,26 +3945,61 @@ impl Host {
CARD_STATS[5].fetch_add(1, Ordering::Relaxed);
CARD_STATS[6].fetch_add(us, Ordering::Relaxed);
}
let mut title = Vec::new();
let merging = std::time::Instant::now();
let mut written = 0;
for p in &pieces {
// SAFETY: the mapping of the fetched bytes, `total` of them;
// the copy into it has run.
let gpu = unsafe { std::slice::from_raw_parts((data + p.at as usize) as *const u8, p.to - p.from) };
if let (Some((piece, Some(hash))), Some(suspects)) = (p.check, &mut suspects) {
suspects.fetched((p.shadow.id(), piece), torn::hash(gpu) == hash);
}
for &(lo, hi) in &p.merged {
let gpu = &gpu[lo - p.from..hi - p.from];
let merged = p.shadow.card_merge_relay(lo, gpu, after, &mut title);
p.shadow.walls_merged(gpu.len(), merged);
written += merged;
if land::on() {
let titles: Vec<(usize, usize)> =
pieces.iter().map(|(shadow, lo, hi, _)| (shadow.span().0 as usize + lo, hi - lo)).collect();
let jobs = land::jobs(&titles);
let put = std::sync::atomic::AtomicU64::new(0);
land::each(jobs.len(), &|j| {
let working = std::time::Instant::now();
if let Some(check) = &check {
for i in jobs[j].clone() {
// SAFETY: as below.
unsafe { check.look(i, data) };
}
}
let mut notes = land::Notes::default();
for run in pieces[jobs[j].clone()].chunk_by(|a, b| std::sync::Arc::ptr_eq(&a.0, &b.0)) {
let addressed: Vec<(usize, usize, usize)> =
run.iter().map(|(_, lo, hi, at)| (*lo, *hi, data + *at as usize)).collect();
// SAFETY: the mapping of the fetched bytes, `total` of
// them; the copy into it has run, and the next waits
// for this call.
put.fetch_add(unsafe { run[0].0.land_card(&addressed, after, &mut notes) }, Ordering::Relaxed);
}
notes.flush();
land::spent(land::stat::CARD_ALL, working);
});
land::count(land::stat::JOBS, jobs.len() as u64);
written = put.into_inner();
} else {
let mut title = Vec::new();
for (i, (shadow, lo, hi, at)) in pieces.iter().enumerate() {
if let Some(check) = &check {
// SAFETY: as below.
unsafe { check.look(i, data) };
}
// SAFETY: the mapping of the fetched bytes, `total` of them;
// the copy into it has run.
let gpu = unsafe { std::slice::from_raw_parts((data + *at as usize) as *const u8, hi - lo) };
let merged = shadow.card_merge_relay(*lo, gpu, after, &mut title);
shadow.walls_merged(gpu.len(), merged);
written += merged as u64;
}
}
if let (Some(f), Some(suspects)) = (fetch.as_mut(), suspects) {
f.suspects = suspects;
land::spent(land::stat::CARD, merging);
COPY_BACK[1].fetch_add(written, Ordering::Relaxed);
if let Some(f) = fetch.as_mut() {
if let Some(check) = &check {
check.done(&mut f.suspects);
}
f.had.fetched(
ahead,
pieces.iter().map(|(shadow, lo, hi, _)| (shadow.id(), shadow.span().0 as usize + lo, hi - lo)),
);
}
COPY_BACK[1].fetch_add(written as u64, Ordering::Relaxed);
// Sends up to `after` are of no use to any later merge.
crate::shadow::relay_fetched(after);
}
@@ -3839,24 +4034,25 @@ impl Host {
last_notes(&runs)
));
}
let plans: Vec<&CardPlan> = flights.iter().filter_map(|f| f.card.as_ref()).collect();
self.relay_card_back(&plans, fetch);
let plans: Vec<(&CardPlan, u64)> = flights.iter().filter_map(|f| Some((f.card.as_ref()?, f.fence))).collect();
// (Each byte once: the ranges are joined.)
let mut back: Vec<Declared> = Vec::new();
for flight in &flights {
for (shadow, ranges) in &flight.back {
for (shadow, ranges) in flight.back.iter().filter(|(shadow, _)| !shadow.carded()) {
match back.iter_mut().find(|(s, _)| std::sync::Arc::ptr_eq(s, shadow)) {
Some((_, all)) => {
for range in ranges {
if !all.contains(range) {
all.push(*range);
}
}
}
Some((_, all)) => all.extend_from_slice(ranges),
None => back.push((shadow.clone(), ranges.clone())),
}
}
}
relay_hand_back(&back);
let mut hand_back = relay_hand_back(&back);
self.relay_card_back(&plans, fetch, &mut || {
if crate::land::on() {
hand_back.look(crate::land::stat::SCAN_BESIDE);
}
});
hand_back.merge();
let finishing = std::time::Instant::now();
// Only now, and flight by flight: a range another flight
// in the air declared too stays closed to refreshes.
for flight in &flights {
@@ -3864,6 +4060,7 @@ impl Host {
shadow.copy_back_done(ranges);
}
}
crate::land::spent(crate::land::stat::DONE, finishing);
COPY_BACK[0].fetch_add(landing.elapsed().as_micros() as u64, Ordering::Relaxed);
RELAY_STATS[0].fetch_add(1, Ordering::Relaxed);
RELAY_STATS[1].fetch_add(flights.len() as u64, Ordering::Relaxed);
@@ -3872,8 +4069,9 @@ impl Host {
// In the order given: what tells the title the card is done.
let mut last = 0;
let mut landed = Vec::new();
let mut telling = std::time::Instant::now();
for item in items {
match item {
telling = match item {
Late::Run(flight) => {
last = flight.number;
for &i in &flight.order {
@@ -3886,16 +4084,24 @@ impl Host {
own.landed();
}
landed.push((flight.lists, flight.order));
crate::land::spent(crate::land::stat::READ_BACKS, telling)
}
Late::Write(at, what, bytes) => what.write(at as *mut u8, bytes),
Late::Signal(fence, value) => crate::fence::set(fence as crate::object::This, value),
}
Late::Write(at, what, bytes) => {
what.write(at as *mut u8, bytes);
crate::land::spent(crate::land::stat::TOLD, telling)
}
Late::Signal(fence, value) => {
crate::fence::set(fence as crate::object::This, value);
crate::land::spent(crate::land::stat::TOLD, telling)
}
};
}
if last > 0 {
FINISHED.store(last, Ordering::Release);
bury(last);
}
self.log_messages();
crate::land::spent(crate::land::stat::REST, telling);
RELAY_STATS[4].fetch_add(landing.elapsed().as_micros() as u64, Ordering::Relaxed);
{
let mut line = line();
@@ -4318,6 +4524,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
@@ -4428,13 +4668,13 @@ impl Host {
if behind && upcheck::on() {
upcheck::reads(runs);
}
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();
@@ -4905,6 +5145,7 @@ impl HostList {
if let Some((scratch, _)) = scratch {
self.indirect_common(&scratch);
}
self.batches_common();
self.card_budget_out();
#[cfg(feature = "diagnostics")]
if let Some((heap, back)) = self.stamps() {
@@ -5387,7 +5628,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
@@ -5439,7 +5687,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))
@@ -5626,6 +5875,43 @@ impl HostList {
Some(buffer)
}
/// Where the next batch of clamped records goes: a device-local buffer
/// the list keeps from recording to recording, the batch's place in it
/// and the state the buffer is in. Every batch of a recording has a place
/// of its own: the executions before it may still read theirs.
pub(crate) fn batch_place(&self) -> Option<(ID3D12Resource, u32, D3D12_RESOURCE_STATES)> {
use crate::indirect::batch::{JOBS, SLOT};
let mut args = self.batch_args.lock().unwrap_or_else(|e| e.into_inner());
let (buffer, i) = ((args.1 / BATCHES) as usize, args.1 % BATCHES);
if buffer == args.0.len() {
let size = u64::from(BATCHES * JOBS * SLOT);
let made = host()?.uav_buffer(size)?;
name(&made, || format!("indirect batches {size:#x}"));
args.0.push(made);
}
args.1 += 1;
// (Its first batch finds it as the recording before left it:
// `batches_common`.)
let state = if i == 0 { D3D12_RESOURCE_STATE_COMMON } else { D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT };
Some((args.0[buffer].clone(), i * JOBS * SLOT, state))
}
/// Before the list closes: the scratch buffers of the batched clamps go
/// back to the common state, where the next recording expects them.
fn batches_common(&self) {
let mut args = self.batch_args.lock().unwrap_or_else(|e| e.into_inner());
let used = std::mem::take(&mut args.1).div_ceil(BATCHES) as usize;
let barriers: Vec<D3D12_RESOURCE_BARRIER> = args.0[..used]
.iter()
.map(|b| transition(b, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT, D3D12_RESOURCE_STATE_COMMON))
.collect();
if !barriers.is_empty() {
// SAFETY: an open list and live buffers this recording left in
// `INDIRECT_ARGUMENT`.
unsafe { self.list.ResourceBarrier(&barriers) };
}
}
/// Moves `scratch` back to the common state if a clamp left it readable
/// as indirect arguments ([`HostList::indirect_readable`]): where the
/// list's first clamp expects it. Done before the list closes and before