Merge branch 'local/next' into local/copies

This commit is contained in:
local committed 2026-11-24 23:24:47 -06:00
commit 0b3af7939c
15 files changed
+2212 -35

No files matched your search

+150 -22
View File
@@ -1515,7 +1515,7 @@ struct CardWork {
fetch: Prelude,
root: ID3D12RootSignature,
pso: ID3D12PipelineState,
/// One dword per piece compared, set where the piece differs: PC
/// One dword per piece compared, odd where the piece differs: PC
/// memory, mapped (buffer, mapping, bytes).
differs: (ID3D12Resource, usize, u64),
/// Where the pieces that differ are fetched to: PC memory, mapped.
@@ -1536,7 +1536,7 @@ static CARD_WORK: Mutex<Option<CardWork>> = Mutex::new(None);
/// The compare of a declared range of a copy in the card's memory with
/// its bytes from before, one group per 64 KiB piece (cut at multiples of
/// 64 KiB of title memory, as `Shadow::card_piece` cuts them): `differs`
/// gets a dword per piece, 1 where a byte differs. The offsets are widened
/// gets a dword per piece, odd where a byte differs. The offsets are widened
/// to 16 bytes; buffers are multiples of 64 KiB, and a piece flagged for a
/// byte just outside the range costs a fetch that finds nothing.
///
@@ -1586,7 +1586,9 @@ void main(uint3 id : SV_GroupID, uint thread : SV_GroupIndex) {
h = h.yzwx ^ (h >> 15);
}
if (compare != 0u) {
differs.Store(out_at + id.x * 4u, any(h != hashes.Load4(piece * 16u)) ? 1u : 0u);
// The flag, and above it 31 bits of the hash (`torn`).
uint folded = h.x ^ (h.y * 3u) ^ (h.z * 5u) ^ (h.w * 7u);
differs.Store(out_at + id.x * 4u, (folded << 1u) | (any(h != hashes.Load4(piece * 16u)) ? 1u : 0u));
// With the copy-back on its own thread: the next compare of the
// piece finds what is written from here on.
if (compare == 2u) hashes.Store4(piece * 16u, h);
@@ -1596,6 +1598,12 @@ void main(uint3 id : SV_GroupID, uint thread : SV_GroupIndex) {
}
"#;
/// The hash [`CARD_COMPARE`] gives of a piece with its flag.
#[cfg(test)]
pub(crate) fn piece_hash(bytes: &[u8]) -> u32 {
torn::hash(bytes)
}
/// One pass of [`CARD_COMPARE`] over a span's pieces, into `differs` from
/// dword `at` on (`mode` as the shader's `compare`): a group per piece, in
/// parts a dispatch can hold.
@@ -1662,6 +1670,22 @@ struct CardRange {
at: u32,
/// Whether the card compares it; else every piece is fetched.
compared: bool,
/// Looked at by `crate::picked`, and not declared: what is found changed
/// there was written through a view nobody declared. Not landed.
picked: bool,
}
impl CardPlan {
/// `crate::picked` is told where what it looked at changed, by the
/// compare's results.
fn picked_found(&self, differs: &[u32]) {
for r in self.ranges.iter().filter(|r| r.picked) {
let changed = (0..r.span.pieces).find(|p| differs.get((r.at + p) as usize).is_some_and(|&d| torn::flagged(d)));
if let Some(p) = changed {
crate::picked::found(&r.shadow, r.shadow.card_piece(&r.span, p).0);
}
}
}
}
/// What a submission does for copies in the card's memory.
@@ -1678,6 +1702,8 @@ struct CardPlan {
/// With [`relay`]: the number of the send to copies in the card's memory
/// that the submission ran behind ([`QUEUE_ORDER`]).
sent_as: u64,
/// With [`upcheck`]: the look at what it sent up.
looks: Option<upcheck::Looks>,
}
/// One piece of submission-time work, in recorded order.
@@ -2695,6 +2721,8 @@ struct RelayFetch {
sent: u64,
/// Buffer, mapping, bytes.
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,
}
@@ -2929,9 +2957,19 @@ fn late_line(frames: f64) {
r[3] / 1000.0,
r[4] / 1000.0,
);
let t: Vec<f64> = torn::STATS.iter().map(|r| r.swap(0, Ordering::Relaxed) as f64 / frames).collect();
if t[0] > 0.0 {
tracing::info!(
"fetched pieces checked per frame (VICTORY_FETCH_CHECK): {:.1}, of which {:.2} were not what their flight ended with (caught while a later flight was at them, or title bytes went up meanwhile); {:.2} were found whole when fetched again",
t[0],
t[1],
t[2],
);
}
}
crate::land::log(frames);
card_steps::log(frames);
crate::picked::log(frames);
#[cfg(feature = "diagnostics")]
log_blame(frames);
if late_exact() {
@@ -2945,6 +2983,8 @@ fn late_line(frames: f64) {
}
mod card_steps;
mod torn;
pub(crate) mod upcheck;
#[cfg(test)]
mod late_tests {
@@ -3451,7 +3491,9 @@ impl Host {
/// `flown`: the submission is sent ahead with [`relay`]. Its compare
/// then stores the hashes it makes (see [`flagged_pieces`] and
/// `relay`).
fn card_before(&self, runs: &[&HostList], flown: bool) -> Option<CardPlan> {
/// `picked`: ranges that may be written too, and ranges to look at
/// (`crate::picked`).
fn card_before(&self, runs: &[&HostList], picked: &crate::picked::Ranges, flown: bool) -> Option<CardPlan> {
use std::sync::Arc;
if !crate::shadow::cards() {
return None;
@@ -3475,6 +3517,10 @@ impl Host {
}
}
}
let declared = crate::picked::with(declared, &picked.written);
// (Of a copy with a declared range here, that says it.)
let looked: Vec<&crate::picked::Range> =
picked.looked.iter().filter(|l| !declared.iter().any(|(s, _)| Arc::ptr_eq(s, &l.0))).collect();
// With the relay: the order of the queue is held from here until
// what is taken is on the queue, and the taking is numbered.
let mut order = relay().then(|| QUEUE_ORDER.lock().unwrap_or_else(|e| e.into_inner()));
@@ -3486,7 +3532,7 @@ impl Host {
None => 0,
};
let mut dirty = crate::shadow::card_dirty(sent_as);
if declared.is_empty() && dirty.is_empty() {
if declared.is_empty() && dirty.is_empty() && looked.is_empty() {
return None;
}
let mut work = CARD_WORK.lock().unwrap_or_else(|e| e.into_inner());
@@ -3494,7 +3540,7 @@ impl Host {
// A copy whose shader writes are declared for the first time gets
// its bytes from before, whole.
let mut whole: Vec<Arc<crate::shadow::Shadow>> = Vec::new();
for (shadow, _) in &declared {
for shadow in declared.iter().map(|d| &d.0).chain(looked.iter().map(|l| &l.0)) {
if shadow.card_make_before(|size| {
let before = self.uav_buffer(crate::shadow::card_hash_bytes(size))?;
name(&before, || "title buffer on the card, hashes of its pieces".into());
@@ -3507,6 +3553,7 @@ impl Host {
}
}
let mut used: Vec<(bool, Prelude)> = Vec::new();
let mut looks = None;
if !dirty.is_empty() {
let Some(sync) = work.sync.pop().or_else(|| self.make_prelude()) else {
fail(format_args!("no host list to bring host copies in the card's memory up to date"));
@@ -3519,6 +3566,9 @@ impl Host {
(|| -> windows::core::Result<()> {
allocator.Reset()?;
list.Reset(allocator, None)?;
if upcheck::on() {
looks = upcheck::record(self, list, &dirty);
}
// Every copy first, then one barrier call, then every
// pass: a wait between each copy and its pass had the
// card do them one after the other.
@@ -3588,6 +3638,7 @@ impl Host {
_synced: dirty.into_iter().map(|(shadow, _)| shadow).collect(),
lists: used,
sent_as,
looks,
};
// Each byte once, as the copy-back joins the declared ranges. The
// results go behind those of plans still in the air.
@@ -3614,12 +3665,30 @@ impl Host {
span,
at,
compared,
picked: false,
});
if compared {
at += span.pieces;
}
}
}
for (shadow, from, len) in looked {
let twin = shadow.card_sides().is_some_and(|(_, before, _)| before.is_some());
if let Some(span) = shadow.card_span(*from, from + len)
&& twin
&& u64::from(at + span.pieces) * 4 <= work.differs.2
{
plan.ranges.push(CardRange {
shadow: shadow.clone(),
declared: (*from, from + len),
span,
at,
compared: true,
picked: true,
});
at += span.pieces;
}
}
work.taken = at;
if plan.ranges.iter().any(|r| r.compared) {
let Some(check) = work.check.pop().or_else(|| self.make_prelude()) else {
@@ -3652,7 +3721,9 @@ impl Host {
};
list.SetComputeRootShaderResourceView(1, r.shadow.buffer.GetGPUVirtualAddress());
list.SetComputeRootUnorderedAccessView(2, before.GetGPUVirtualAddress());
card_pass(list, &r.span, size as u32, r.at, if flown { 2 } else { 1 });
// (What is only looked at keeps its hashes: what
// changed there is the next declared range's to land.)
card_pass(list, &r.span, size as u32, r.at, if flown && !r.picked { 2 } else { 1 });
CARD_PASSES[2].fetch_add(1, Ordering::Relaxed);
}
list.Close()?;
@@ -3685,6 +3756,9 @@ impl Host {
/// when nothing of ours is on the card, so the compare's results start
/// from the beginning again.
fn card_done(&self, plan: CardPlan) {
if let Some(looks) = plan.looks {
looks.read();
}
let mut work = CARD_WORK.lock().unwrap_or_else(|e| e.into_inner());
let Some(work) = work.as_mut() else {
return;
@@ -3736,6 +3810,7 @@ impl Host {
event,
sent: 0,
buffer: None,
suspects: torn::Suspects::default(),
had: crate::land::Had::default(),
})
}
@@ -3765,26 +3840,34 @@ impl Host {
if let (Some(fetch), Some(first)) = (fetch.as_mut(), plans.iter().map(|p| p.1).min()) {
fetch.had.landing(first);
}
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) };
// 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 {
plan.picked_found(differs);
let flags: Vec<(usize, u32, bool)> = plan
.ranges
.iter()
.filter(|r| !r.picked)
.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| d != 0);
(std::sync::Arc::as_ptr(&r.shadow) as usize, r.span.first + p, flagged)
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 {
for r in plan.ranges.iter().filter(|r| !r.picked) {
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;
@@ -3799,6 +3882,9 @@ impl Host {
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;
@@ -3828,6 +3914,13 @@ impl Host {
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: 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;
}
// 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.0.sent_as).max().unwrap_or(0);
@@ -3868,7 +3961,7 @@ impl Host {
(|| -> windows::core::Result<u64> {
allocator.Reset()?;
list.Reset(&allocator, None)?;
for (shadow, lo, hi, at) in &pieces {
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()?;
@@ -3917,6 +4010,12 @@ impl Host {
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)> =
@@ -3933,7 +4032,11 @@ impl Host {
written = put.into_inner();
} else {
let mut title = Vec::new();
for (shadow, lo, hi, at) in &pieces {
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) };
@@ -3945,6 +4048,9 @@ impl Host {
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)),
@@ -4116,9 +4222,10 @@ impl Host {
// where the bytes come down.
let mut pieces: Vec<(usize, usize, usize, u64)> = Vec::new();
let mut total = 0u64;
for (i, r) in plan.ranges.iter().enumerate() {
plan.picked_found(differs);
for (i, r) in plan.ranges.iter().enumerate().filter(|(_, r)| !r.picked) {
for p in 0..r.span.pieces {
if r.compared && differs[(r.at + p) as usize] == 0 {
if r.compared && !torn::flagged(differs[(r.at + p) as usize]) {
continue;
}
let (lo, hi) = r.shadow.card_piece(&r.span, p);
@@ -4558,12 +4665,18 @@ impl Host {
// one at every present).
static SWEPT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
if !behind {
if upcheck::on() {
upcheck::at_rest(self);
}
sweep();
SWEPT.store(now_ms(), Ordering::Relaxed);
} else if late_free_present() && now_ms() > SWEPT.load(Ordering::Relaxed) + 1500 {
// With the present sent ahead nothing else brings a moment
// with nothing in the air: one is made for the sweep.
self.settle(&mut submitted, WHY_SYNC);
if upcheck::on() {
upcheck::at_rest(self);
}
sweep();
SWEPT.store(now_ms(), Ordering::Relaxed);
}
@@ -4615,6 +4728,9 @@ 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 && upcheck::on() {
upcheck::reads(runs);
}
let stale = if behind { stale_under_flights(runs) } else { Vec::new() };
if !stale.is_empty() {
let settling = std::time::Instant::now();
@@ -4661,7 +4777,13 @@ impl Host {
self.settle(&mut submitted, WHY_MOST);
settled += settling.elapsed();
}
let card = self.card_before(runs, flown);
// What shaders may write through views they pick: last, so that no
// refresh of this submission leaves it alone.
let picked = crate::picked::declare(runs);
for (shadow, at, len) in &picked.written {
shadow.writing(*at, *len);
}
let card = self.card_before(runs, &picked, flown);
if let Some(check) = card.as_ref().and_then(|c| c.check.clone()) {
lists.push(Some(check));
}
@@ -4681,7 +4803,7 @@ impl Host {
}
#[cfg(feature = "diagnostics")]
split.part(5);
let back = declared_writes(runs);
let back = crate::picked::with(declared_writes(runs), &picked.written);
let writes = runs.iter().flat_map(|l| l.deferred().writes.clone()).collect();
SUBMISSION_US[0].fetch_add((prepared - started).saturating_sub(settled).as_micros() as u64, Ordering::Relaxed);
submitting(0, 0);
@@ -4801,7 +4923,7 @@ impl Host {
if let Some(card) = &card {
self.card_back(card, &mut submitted, &mut std::collections::HashSet::new());
}
let back = declared_writes(runs);
let back = crate::picked::with(declared_writes(runs), &picked.written);
hand_back(&back);
if let Some(card) = card {
self.card_done(card);
@@ -5926,6 +6048,12 @@ impl HostList {
self.deferred().textures.insert(key)
}
/// Whether `key` was asked about ([`HostList::first_use`]) since the
/// reset.
pub(crate) fn used(&self, key: usize) -> bool {
self.deferred().textures.contains(&key)
}
/// Runs `work` when the list is submitted, in order with its copies.
/// `work` may write title memory (refreshes after it copy again).
pub(crate) fn defer(&self, work: impl Fn() + Send + 'static) {