Merge local/copies (wide textures, joined writes)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 22:52:24 -06:00
commit 827ec2d5d0
3 files changed
+140 -17

No files matched your search

+99 -17
View File
@@ -2445,14 +2445,21 @@ fn stale_under_flights(runs: &[&HostList]) -> Vec<(std::sync::Arc<crate::shadow:
// Per copy, the span of what is refreshed (a flush brings that much
// up to date at most).
let mut spans: Vec<(Arc<crate::shadow::Shadow>, usize, usize)> = Vec::new();
// Where each copy is in it, if shaders may be writing it (none begins to
// while this runs): a frame has 20,000 refreshes of some 4,500 copies.
let mut places: ByCopy<Option<usize>> = ByCopy::default();
for list in runs {
for step in &list.deferred().copies {
if let Step::Refresh(shadow, address, len) = step
&& shadow.writes_pending()
{
match spans.iter_mut().find(|s| Arc::ptr_eq(&s.0, shadow)) {
Some(s) => (s.1, s.2) = (s.1.min(*address), s.2.max(*address + *len)),
None => spans.push((shadow.clone(), *address, *address + *len)),
if let Step::Refresh(shadow, address, len) = step {
let place = places.entry(Arc::as_ptr(shadow) as usize).or_insert_with(|| {
shadow.writes_pending().then(|| {
spans.push((shadow.clone(), *address, *address + *len));
spans.len() - 1
})
});
if let Some(i) = *place {
let s = &mut spans[i];
(s.1, s.2) = (s.1.min(*address), s.2.max(*address + *len));
}
}
}
@@ -3036,18 +3043,22 @@ fn continues(last: (usize, usize, usize), next: (usize, usize, usize)) -> bool {
/// One list's lock at a time: a submission may name a list twice.
fn declared_writes(runs: &[&HostList]) -> Vec<Declared> {
let mut back: Vec<Declared> = Vec::new();
// Where each copy is in it, and the ranges seen: dispatches declare the
// same ranges thousands of times a frame.
let mut places: ByCopy<usize> = ByCopy::default();
let mut seen = std::collections::HashSet::<(usize, usize, usize), std::hash::BuildHasherDefault<AddressHash>>::default();
for list in runs {
for step in &list.deferred().copies {
if let Step::GpuWrite(shadow, address, len) = step {
let range = (*address, *address + *len);
match back.iter_mut().find(|(s, _)| std::sync::Arc::ptr_eq(s, shadow)) {
Some((_, ranges)) => {
if !ranges.contains(&range) {
ranges.push(range);
}
}
None => back.push((shadow.clone(), vec![range])),
let key = std::sync::Arc::as_ptr(shadow) as usize;
if !seen.insert((key, *address, *len)) {
continue;
}
let i = *places.entry(key).or_insert_with(|| {
back.push((shadow.clone(), Vec::new()));
back.len() - 1
});
back[i].1.push((*address, *address + *len));
}
}
}
@@ -6023,7 +6034,10 @@ impl HostList {
}
Step::Texture(staging) => {
if batch.texture_due(staging) {
staging.refresh();
batch.due.push(staging.clone());
if !wide_textures() {
batch.refresh_textures();
}
}
continue;
}
@@ -6214,8 +6228,29 @@ struct Batch {
/// for every refresh of a submission was tens of millions of
/// comparisons a frame.
index: ByCopy<usize>,
/// Staging buffers (by address) refreshed since the last title write.
/// Staging buffers (by address) refreshed since the last title write,
/// or due to be.
textures: std::collections::HashSet<usize>,
/// Those due ([`Batch::refresh_textures`]).
due: Vec<std::sync::Arc<crate::texture::Staging>>,
}
/// Whether a submission's textures are brought up to date on several
/// threads (`VICTORY_WIDE_TEXTURES=0`: one after the other, where the lists
/// name them, as before).
///
/// They are read, hashed and, if they changed, detiled into their staging
/// buffers: 5 ms a frame of the submitting thread, for some 16 MB. Each has
/// its own buffer, notes and lock, and is refreshed by the title's own
/// threads as it is. They are done before the next step that writes title
/// memory, as the copies' refreshes are, and before the lists go to the
/// card. What is new: the note of a list that its upload takes what the
/// buffer then holds (`texture.rs`, `dirty`) may come ahead of the refresh
/// an earlier list of the submission asked for, not behind it. The texture
/// is then uploaded by a later list too, once more than needed.
fn wide_textures() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_WIDE_TEXTURES").is_none_or(|v| v != "0") && wide_refresh())
}
impl Batch {
@@ -6278,7 +6313,23 @@ impl Batch {
self.textures.clear();
}
/// The textures due are brought up to date.
fn refresh_textures(&mut self) {
if self.due.len() < 2 || !wide_textures() {
self.due.drain(..).for_each(|staging| staging.refresh());
return;
}
// The time counted is the time it took, not the threads' sum.
let started = std::time::Instant::now();
let before = PREPARING[2].load(Ordering::Relaxed);
let due = std::mem::take(&mut self.due);
crate::shadow::wide_each(due.len(), move |i| due[i].refresh());
let summed = PREPARING[2].load(Ordering::Relaxed).saturating_sub(before);
PREPARING[2].fetch_sub(summed.saturating_sub(started.elapsed().as_micros() as u64), Ordering::Relaxed);
}
fn flush(&mut self) {
self.refresh_textures();
self.index.clear();
if self.refreshes.is_empty() {
return;
@@ -7776,7 +7827,7 @@ mod refresh_tests {
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
assert!(crate::safecopy::on());
const LEN: usize = 24 * PAGE;
let rounds: usize = std::env::var("VICTORY_TEST_ROUNDS").ok().and_then(|v| v.parse().ok()).unwrap_or(400);
let rounds: usize = std::env::var("VICTORY_TEST_ROUNDS").ok().and_then(|v| v.parse().ok()).unwrap_or(120);
let region = title(LEN, true);
let dword = |at: usize| unsafe { &*((region + at) as *const AtomicU32) };
let carded = [Carded::new(region, LEN, 3), Carded::new(region + 4 * PAGE + 1000, LEN - 6 * PAGE - 4, 5)];
@@ -7942,6 +7993,37 @@ mod refresh_tests {
});
}
/// The title's bytes put in under shader writes, for a copy of 174 MB
/// whose shader writes were declared as 40 ranges that overlap, with 12
/// pages written since. Run with `--ignored --nocapture`, and with
/// `VICTORY_LIGHT_REFRESH=0` for the way it was.
#[test]
#[ignore]
fn bytes_under_overlapping_ranges() {
const BIG: usize = 174 << 20;
let big = title(BIG, true);
// SAFETY (both): the test's own memory.
unsafe { std::ptr::write_bytes(big as *mut u8, 1, BIG) };
let copy = Shadow::unbacked(big as u64, memory(BIG), BIG as u64, true);
copy.update(big, BIG);
copy.take_dirty();
for i in 0..40 {
copy.writing(big + i * 5920, BIG - i * 5920);
}
let mut best = f64::MAX;
for round in 0..30 {
for i in 0..12 {
unsafe { ((big + (i * 211 + round) % 2700 * PAGE + 64) as *mut u8).write_volatile(round as u8) };
}
let started = std::time::Instant::now();
let mut runs = usize::MAX;
assert!(!copy.merge_under_writes(big, BIG, &mut runs));
best = best.min(started.elapsed().as_secs_f64() * 1e3);
copy.take_dirty();
}
println!("VICTORY_LIGHT_REFRESH {}: best {best:.3} ms", crate::watch::light());
}
/// A range nothing wrote is known to be as it was, at its ends too, and
/// is so no more once something wrote it.
#[test]
+5
View File
@@ -2579,6 +2579,11 @@ impl Shadow {
note_stale(0);
return true;
}
// The declared ranges overlap (views of the title's whole heap from
// dozens of offsets): as one range each page is asked about and
// compared once. Every byte of it is in a declared range, and of a
// copy in the PC's memory in the snapshot.
let busy = if crate::watch::light() { joined(busy) } else { busy };
let base = start / PAGE;
let mut title = vec![0u8; PAGE];
for (a, e) in busy {
+36
View File
@@ -1704,6 +1704,42 @@ mod many_askers_tests {
// of these writes.)
}
/// A block's bit goes with everything that makes it the title's to write,
/// or forgets it.
#[test]
fn bits_go_with_the_guard() {
// SAFETY: a view of a section of our own, which stays.
let at = unsafe {
let section =
CreateFileMappingW(-1isize as _, std::ptr::null(), PAGE_READWRITE, 0, (2 * PAGE) as u32, std::ptr::null());
MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, 2 * PAGE).Value as usize
};
let end = at + 2 * PAGE;
let takes: [&dyn Fn(); 4] = [
&|| super::title_memory_remapped(at, PAGE),
// SAFETY: the view.
&|| unsafe { ((at + 8) as *mut u8).write_volatile(1) },
&|| super::writing(at, 1),
// (Told again, as of a view mapped there next: it is read-only.)
&|| {
super::title_memory_unmapping(at, PAGE);
assert!(!super::published(at, end), "forgotten");
super::title_memory_remapped(at, PAGE);
let mut old = 0;
// SAFETY: the view.
unsafe { VirtualProtect(at as *const _, PAGE, PAGE_READWRITE, &mut old) };
},
];
for (i, take) in takes.iter().enumerate() {
writes(at, end, true).expect("a view");
assert!(super::published(at, end), "{i}");
take();
assert!(!super::published(at, end), "{i}");
assert!(super::published(at + PAGE, end), "{i}: the other block");
}
super::title_memory_unmapping(at, 2 * PAGE);
}
/// A range's bits are all of its blocks', whatever lies beside them.
#[test]
fn bits_of_ranges() {