Scope bindless refreshes to their heap and bound speculative staging

This commit is contained in:
codec_d committed 2026-11-25 15:50:47 -08:00
1 parent 3bcc96f9bb
commit 933b382635
5 files changed
+126 -51

No files matched your search

+55 -41
View File
@@ -157,7 +157,7 @@ thread_local! {
/// While `State::early_textures` runs the texture pass: the
/// milliseconds and the bytes that run may take, in place of the
/// pass's own per frame.
static PASS_BUDGET: std::cell::Cell<Option<(u64, u64)>> = const { std::cell::Cell::new(None) };
static PASS_BUDGET: std::cell::Cell<Option<(u64, u64, bool)>> = const { std::cell::Cell::new(None) };
}
/// The milliseconds of every frame the pass that uploads textures picked
@@ -600,6 +600,20 @@ fn heap_spread() -> bool {
#[derive(Default)]
struct HeapRanges<T>(Vec<Option<(T, u64, u64)>>);
/// Descriptor-derived references belong to the heap that produced them.
pub(crate) struct BufferSweep {
passes: u64,
ranges: HeapRanges<std::sync::Arc<crate::shadow::Shadow>>,
// Last refresh frame and covered interval for throttled, unwatched copies.
unwatched: std::collections::HashMap<u64, (u64, u64, u64)>,
}
impl Default for BufferSweep {
fn default() -> Self {
Self { passes: 0, ranges: HeapRanges(Vec::new()), unwatched: Default::default() }
}
}
impl<T> HeapRanges<T> {
fn set(&mut self, slot: u32, range: Option<(T, u64, u64)>) {
let slot = slot as usize;
@@ -839,14 +853,8 @@ fn heap_buffer_pass(heap: &crate::descriptor::Heap, host: &crate::descriptor::ho
// at best); all of them every 16 passes, because a view of a
// released resource goes stale without a write, and a slot written
// before its memory had a host copy is only made then.
type Ranges = HeapRanges<std::sync::Arc<crate::shadow::Shadow>>;
static RANGES: std::sync::Mutex<Option<std::collections::HashMap<usize, (u64, Ranges)>>> =
std::sync::Mutex::new(None);
let mut kept = RANGES.lock().unwrap_or_else(|e| e.into_inner());
let (passes, ranges) = kept
.get_or_insert_with(Default::default)
.entry(std::ptr::from_ref(host) as usize)
.or_insert_with(|| (0, HeapRanges(Vec::new())));
let mut kept = host.buffer_sweep.lock().unwrap_or_else(|e| e.into_inner());
let BufferSweep { passes, ranges, unwatched } = &mut *kept;
let changed = host.buffer_changes();
let all = *passes == 0 || (*passes % HEAP_ROUND == 0 && !heap_spread());
let slots = if all {
@@ -942,7 +950,6 @@ fn heap_buffer_pass(heap: &crate::descriptor::Heap, host: &crate::descriptor::ho
span.1 = span.1.min(*from);
span.2 = span.2.max(*to);
}
drop(kept);
let bytes: u64 = spans.values().map(|s| s.2 - s.1).sum();
// Memory that is not write-watched is copied whole by every refresh.
// It was once refreshed only every 120 frames (unmapped pages read
@@ -951,32 +958,27 @@ fn heap_buffer_pass(heap: &crate::descriptor::Heap, host: &crate::descriptor::ho
// the garbage ended runs (the card stopped answering 14-90 frames
// into the opening cutscene; with every frame it ran on). The pass
// costs some 30 ms a frame this way.
static WHOLE: std::sync::Mutex<Option<std::collections::HashMap<usize, u64>>> = std::sync::Mutex::new(None);
let mut whole = WHOLE.lock().unwrap_or_else(|e| e.into_inner());
let whole = whole.get_or_insert_with(Default::default);
static EVERY: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
let every = *EVERY.get_or_init(|| {
std::env::var("VICTORY_UNWATCHED_FRAMES").ok().and_then(|v| v.parse().ok()).unwrap_or(1)
});
let mut found = Vec::with_capacity(spans.len());
let mut next_unwatched = std::collections::HashMap::new();
for (shadow, from, to) in spans.values() {
if shadow.unwatched() {
// By the copy's number: a copy made where one let go was
// (`shadow::let_go_idle`) has never been refreshed.
let key = shadow.id() as usize;
// `VICTORY_UNWATCHED_FRAMES` sets the interval (1: every frame).
static EVERY: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
let every = *EVERY.get_or_init(|| {
std::env::var("VICTORY_UNWATCHED_FRAMES")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(1)
});
match whole.get(&key) {
Some(&last) if frame < last + every => continue,
_ => {
whole.insert(key, frame);
}
if every > 1 && shadow.unwatched() {
let id = shadow.id();
if let Some(&(last, low, high)) = unwatched.get(&id)
&& frame >= last && frame - last < every && low <= *from && *to <= high
{
next_unwatched.insert(id, (last, low, high));
continue;
}
next_unwatched.insert(id, (frame, *from, *to));
}
found.push((shadow.clone(), *from, (*to).min(*from + shadow.left(*from))));
}
*unwatched = next_unwatched;
drop(kept);
#[cfg(feature = "diagnostics")]
let refreshed = std::time::Instant::now();
crate::watch::look_at_holes();
@@ -2012,9 +2014,9 @@ impl State {
.find(|h| !h.samplers())?;
let host = heap.host()?;
let gpu = host.strides_gpu()?;
// One key per list, as textures have: no title resource is there.
const ONCE: usize = usize::MAX - 0x5e;
if !list.first_use(ONCE) {
// Stride buffers have aligned GPU addresses. Tag the list key so it
// is distinct from aligned guest resource pointers.
if !list.first_use(gpu as usize | 1) {
return Some(gpu);
}
// The list may outlive the title's heap object, which owns the table.
@@ -2026,9 +2028,8 @@ impl State {
// frame (2026-11-24, run 11).
// shortcut: what the title writes into such buffers between lists
// of one frame is seen a frame late.
static FRAME: AtomicU64 = AtomicU64::new(u64::MAX);
let frame = crate::frame::current();
if FRAME.swap(frame, Ordering::Relaxed) == frame {
if host.buffer_frame.swap(frame, Ordering::Relaxed) == frame {
return Some(gpu);
}
let started = std::time::Instant::now();
@@ -2036,7 +2037,11 @@ impl State {
if crate::host::aside::on() {
let at = std::ptr::from_ref(heap) as usize;
list.refreshes_coming(move || {
crate::descriptor::with_heap(at, |heap| heap.host().map(|host| heap_buffer_pass(heap, host, frame)))
crate::descriptor::with_heap(at, |heap| {
// The list retains the original stride buffer. A newly
// allocated heap at the same CPU address has another one.
heap.host().filter(|h| h.strides_gpu() == Some(gpu)).map(|h| heap_buffer_pass(heap, h, frame))
})
.flatten()
.unwrap_or_default()
});
@@ -2121,7 +2126,7 @@ impl State {
);
}
let began = std::time::Instant::now();
PASS_BUDGET.set(Some(budget));
PASS_BUDGET.set(Some((budget.0, budget.1, bake)));
crate::texture::BAKING.set(bake && bake_exact());
self.heap_textures(list, heap, host);
crate::texture::BAKING.set(false);
@@ -2241,12 +2246,11 @@ impl State {
Some(mb) => mb << 20,
None => {
let base = staging_budget_per_frame(memory.map(|[budget, ..]| budget));
let capped = if crate::residency::limited_host_ram() {
if crate::residency::limited_host_ram() {
base.min(128 << 20)
} else {
base
};
crate::residency::prefetch_limit(capped, crate::residency::pressured())
}
}
};
static FRAME: AtomicU64 = AtomicU64::new(u64::MAX);
@@ -2351,11 +2355,21 @@ impl State {
// own: its bytes on top of what the frame has spent so far.
// (Not `most`: that is the longest wait between looks, used below.)
let (span, bytes_at_most) = match PASS_BUDGET.get() {
Some((ms, bytes)) => {
Some((ms, bytes, _)) => {
(std::time::Duration::from_millis(ms), SPENT.load(Ordering::Relaxed).saturating_add(bytes))
}
None => (span, per_frame),
};
// Apply the shared frame ceiling after early-pass and catch-up budgets.
// A bake samples its inputs immediately and cannot retry that draw.
let required = PASS_BUDGET.get().is_some_and(|(_, _, bake)| bake);
let bytes_at_most = if required || asked.is_some() {
bytes_at_most
} else {
crate::residency::prefetch_limit(bytes_at_most, crate::residency::pressured(),
crate::residency::limited_host_ram(), crate::texture::live_staging_bytes())
};
let began = std::time::Instant::now();
BAKE_SERVED.store(0, Ordering::Relaxed);
let mut served = true;