Title bytes under overlapping shader writes once; textures on several threads; cheaper walks of a submission's steps
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
c6e74e52ff
commit
63d27fb04f
2 files changed
+104
-17
No files matched your search
@@ -2438,14 +2438,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
|
// Per copy, the span of what is refreshed (a flush brings that much
|
||||||
// up to date at most).
|
// up to date at most).
|
||||||
let mut spans: Vec<(Arc<crate::shadow::Shadow>, usize, usize)> = Vec::new();
|
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 list in runs {
|
||||||
for step in &list.deferred().copies {
|
for step in &list.deferred().copies {
|
||||||
if let Step::Refresh(shadow, address, len) = step
|
if let Step::Refresh(shadow, address, len) = step {
|
||||||
&& shadow.writes_pending()
|
let place = places.entry(Arc::as_ptr(shadow) as usize).or_insert_with(|| {
|
||||||
{
|
shadow.writes_pending().then(|| {
|
||||||
match spans.iter_mut().find(|s| Arc::ptr_eq(&s.0, shadow)) {
|
spans.push((shadow.clone(), *address, *address + *len));
|
||||||
Some(s) => (s.1, s.2) = (s.1.min(*address), s.2.max(*address + *len)),
|
spans.len() - 1
|
||||||
None => spans.push((shadow.clone(), *address, *address + *len)),
|
})
|
||||||
|
});
|
||||||
|
if let Some(i) = *place {
|
||||||
|
let s = &mut spans[i];
|
||||||
|
(s.1, s.2) = (s.1.min(*address), s.2.max(*address + *len));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3029,18 +3036,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.
|
/// One list's lock at a time: a submission may name a list twice.
|
||||||
fn declared_writes(runs: &[&HostList]) -> Vec<Declared> {
|
fn declared_writes(runs: &[&HostList]) -> Vec<Declared> {
|
||||||
let mut back: Vec<Declared> = Vec::new();
|
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 list in runs {
|
||||||
for step in &list.deferred().copies {
|
for step in &list.deferred().copies {
|
||||||
if let Step::GpuWrite(shadow, address, len) = step {
|
if let Step::GpuWrite(shadow, address, len) = step {
|
||||||
let range = (*address, *address + *len);
|
let key = std::sync::Arc::as_ptr(shadow) as usize;
|
||||||
match back.iter_mut().find(|(s, _)| std::sync::Arc::ptr_eq(s, shadow)) {
|
if !seen.insert((key, *address, *len)) {
|
||||||
Some((_, ranges)) => {
|
continue;
|
||||||
if !ranges.contains(&range) {
|
|
||||||
ranges.push(range);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => back.push((shadow.clone(), vec![range])),
|
|
||||||
}
|
}
|
||||||
|
let i = *places.entry(key).or_insert_with(|| {
|
||||||
|
back.push((shadow.clone(), Vec::new()));
|
||||||
|
back.len() - 1
|
||||||
|
});
|
||||||
|
back[i].1.push((*address, *address + *len));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5977,7 +5988,10 @@ impl HostList {
|
|||||||
}
|
}
|
||||||
Step::Texture(staging) => {
|
Step::Texture(staging) => {
|
||||||
if batch.texture_due(staging) {
|
if batch.texture_due(staging) {
|
||||||
staging.refresh();
|
batch.due.push(staging.clone());
|
||||||
|
if !wide_textures() {
|
||||||
|
batch.refresh_textures();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -6168,8 +6182,29 @@ struct Batch {
|
|||||||
/// for every refresh of a submission was tens of millions of
|
/// for every refresh of a submission was tens of millions of
|
||||||
/// comparisons a frame.
|
/// comparisons a frame.
|
||||||
index: ByCopy<usize>,
|
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>,
|
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 {
|
impl Batch {
|
||||||
@@ -6232,7 +6267,23 @@ impl Batch {
|
|||||||
self.textures.clear();
|
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) {
|
fn flush(&mut self) {
|
||||||
|
self.refresh_textures();
|
||||||
self.index.clear();
|
self.index.clear();
|
||||||
if self.refreshes.is_empty() {
|
if self.refreshes.is_empty() {
|
||||||
return;
|
return;
|
||||||
@@ -7730,7 +7781,7 @@ mod refresh_tests {
|
|||||||
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
||||||
assert!(crate::safecopy::on());
|
assert!(crate::safecopy::on());
|
||||||
const LEN: usize = 24 * PAGE;
|
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 region = title(LEN, true);
|
||||||
let dword = |at: usize| unsafe { &*((region + at) as *const AtomicU32) };
|
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)];
|
let carded = [Carded::new(region, LEN, 3), Carded::new(region + 4 * PAGE + 1000, LEN - 6 * PAGE - 4, 5)];
|
||||||
@@ -7896,6 +7947,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
|
/// A range nothing wrote is known to be as it was, at its ends too, and
|
||||||
/// is so no more once something wrote it.
|
/// is so no more once something wrote it.
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -2579,6 +2579,11 @@ impl Shadow {
|
|||||||
note_stale(0);
|
note_stale(0);
|
||||||
return true;
|
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 base = start / PAGE;
|
||||||
let mut title = vec![0u8; PAGE];
|
let mut title = vec![0u8; PAGE];
|
||||||
for (a, e) in busy {
|
for (a, e) in busy {
|
||||||
|
|||||||
Reference in new issue
Block a user