Back off refused GPU pool growth and defer uploads under low commit

This commit is contained in:
codec_d committed 2026-11-25 16:07:37 -08:00
1 parent 933b382635
commit efeef1d65b
6 files changed
+110 -5

No files matched your search

+26 -2
View File
@@ -2297,6 +2297,11 @@ impl State {
if room {
host.texture_slots_waiting(frame);
}
let required = PASS_BUDGET.get().is_some_and(|(_, _, bake)| bake);
if !crate::residency::admit_texture_pass(required) {
BAKE_SERVED.store(0, Ordering::Relaxed);
return;
}
let baking = crate::texture::BAKING.get();
let (mut due, census) = host.texture_slots_due(frame, if baking { 1 << 16 } else { 4096 });
// (Those that are due later after all that are due.)
@@ -2362,7 +2367,6 @@ impl State {
};
// 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 {
@@ -2374,7 +2378,9 @@ impl State {
BAKE_SERVED.store(0, Ordering::Relaxed);
let mut served = true;
for (index, write) in due {
if SPENT.load(Ordering::Relaxed) >= bytes_at_most || began.elapsed() > span {
if SPENT.load(Ordering::Relaxed) >= bytes_at_most || began.elapsed() > span
|| !crate::residency::admit_texture_pass(required)
{
served = false;
break;
}
@@ -2539,6 +2545,10 @@ impl State {
}
};
let placed = view.as_ref().and_then(crate::descriptor::host::placed_texture);
if crate::residency::upload_refused() {
served = false;
break;
}
let resource = match placed.as_ref().or(view.as_ref()) {
Some(&View::Shader(resource, _)) => resource as This,
_ => {
@@ -2548,6 +2558,10 @@ impl State {
}
};
let texture = crate::resource::host_resource(resource);
if crate::residency::upload_refused() {
served = false;
break;
}
let title_fills = texture.is_some_and(crate::texture::title_written);
if !title_fills {
at_once();
@@ -2606,6 +2620,10 @@ impl State {
crate::texture::ahead::send(job);
}
}
if ahead.is_none() && crate::residency::upload_refused() {
served = false;
break;
}
let late = late && ahead.is_none();
if let Some(view) = &view
&& ahead.is_none()
@@ -2616,6 +2634,12 @@ impl State {
readable(list, sampled.kind, view);
crate::texture::PICKING.set(None);
}
// A failed allocation leaves this slot due. Publishing it or
// marking it served would lose the only retry for some materials.
if ahead.is_none() && crate::residency::upload_refused() {
served = false;
break;
}
if shows_other && let Some(handle) = heap.handle(index) {
host.texture_slot_flip(list, index, handle);
}
+1
View File
@@ -8597,6 +8597,7 @@ impl Host {
match made {
Ok(()) => Some((buffer?, data.cast())),
Err(e) => {
crate::residency::allocation_error(&e);
tracing::warn!(error = %e, size, "no upload buffer");
None
}
+31 -3
View File
@@ -191,11 +191,12 @@ struct Pool<B> {
/// The empty blocks it was last told to keep, and the most it keeps.
spare: usize,
most: usize,
retry_growth_at: u64,
}
impl<B: Clone> Pool<B> {
const fn new(most: usize) -> Pool<B> {
Pool { blocks: Vec::new(), resting: VecDeque::new(), spare: 0, most }
Pool { blocks: Vec::new(), resting: VecDeque::new(), spare: 0, most, retry_growth_at: 0 }
}
/// A piece of `len` bytes and the memory it lies in, and whether that
@@ -237,10 +238,13 @@ impl<B: Clone> Pool<B> {
unripe.sort_unstable();
found = unripe.into_iter().find_map(|(_, i)| Some((i, self.blocks[i].as_mut()?.free.take(len, align)?)));
}
// A failed 64 MiB block must not trigger another driver call for each
// small texture. Existing free ranges remain usable during backoff.
let (i, start) = match found {
Some(found) => found,
None => {
let i = self.add(make()?, ripe.started);
let memory = self.grow(now, &mut make)?;
let i = self.add(memory, ripe.started);
(i, self.blocks[i].as_mut()?.free.take(len, align)?)
}
};
@@ -253,7 +257,7 @@ impl<B: Clone> Pool<B> {
}
let empty = self.blocks.iter().flatten().filter(|b| b.free.whole(BLOCK)).count();
if empty < ripe.ahead
&& let Some(memory) = make()
&& let Some(memory) = self.grow(now, &mut make)
{
self.add(memory, ripe.started);
}
@@ -264,6 +268,17 @@ impl<B: Clone> Pool<B> {
Some((Piece { block: i, start, len }, block.memory.clone(), again))
}
fn grow(&mut self, now: u64, make: &mut impl FnMut() -> Option<B>) -> Option<B> {
if now < self.retry_growth_at {
return None;
}
let block = make();
if block.is_none() {
self.retry_growth_at = now.saturating_add(500);
}
block
}
fn add(&mut self, memory: B, made: u64) -> usize {
let i = match self.blocks.iter().position(Option::is_none) {
Some(i) => i,
@@ -503,6 +518,7 @@ pub(crate) unsafe fn texture(
let mut heap: Option<ID3D12Heap> = None;
// SAFETY: plain creation.
if let Err(e) = unsafe { device.CreateHeap(&desc, &mut heap) } {
crate::residency::allocation_error(&e);
tracing::warn!(error = %e, "no block of texture memory");
}
if let Some(h) = &heap
@@ -575,6 +591,18 @@ mod tests {
assert_eq!(spares(None, true), SPARE);
}
#[test]
fn a_failed_block_is_not_retried_for_each_small_upload() {
let mut pool: Pool<u32> = Pool::new(2);
let calls = std::cell::Cell::new(0);
let refuse = || { calls.set(calls.get() + 1); None };
for size in [704008, 179720, 179720, 24080, 48648, 524288, 89616, 351760] {
assert!(pool.take(size, 512, 12, refuse).is_none());
}
assert_eq!(calls.get(), 1);
assert!(pool.take(24080, 512, 1012, || Some(1)).is_some());
}
#[test]
fn budget_tick_reclaims_idle_blocks_without_reusing_live_or_resting_pieces() {
let mut pool = Pool::new(STAGING_KEPT);
+47
View File
@@ -14,6 +14,43 @@ fn memory_status() -> Option<MEMORYSTATUSEX> {
(unsafe { GlobalMemoryStatusEx(&mut status) } != 0).then_some(status)
}
// The texture sweep may run for seconds inside a single submitted frame.
static NEXT_COMMIT_SAMPLE: AtomicU64 = AtomicU64::new(0);
static AVAILABLE_COMMIT: AtomicU64 = AtomicU64::new(u64::MAX);
static RETRY_UPLOAD_AT: AtomicU64 = AtomicU64::new(0);
pub(crate) fn allocation_error(error: &windows::core::Error) {
if error.code() == windows::Win32::Foundation::E_OUTOFMEMORY {
RETRY_UPLOAD_AT.fetch_max(crate::host::coarse_ms().saturating_add(500), Ordering::Relaxed);
}
}
pub(crate) fn upload_refused() -> bool {
crate::host::coarse_ms() < RETRY_UPLOAD_AT.load(Ordering::Relaxed)
}
fn has_upload_reserve(commit: u64, bake: bool) -> bool {
commit >= if bake { 2 * GIB } else { 4 * GIB }
}
/// Leave commit for game allocations and submissions already in flight. Direct
/// texture consumers bypass this gate. Bake prefetch gets priority over lookahead.
pub(crate) fn admit_texture_pass(bake: bool) -> bool {
let now = crate::host::coarse_ms();
if now < RETRY_UPLOAD_AT.load(Ordering::Relaxed) {
return false;
}
let next = NEXT_COMMIT_SAMPLE.load(Ordering::Relaxed);
if now >= next && NEXT_COMMIT_SAMPLE.compare_exchange(
next, now.saturating_add(32), Ordering::Relaxed, Ordering::Relaxed,
).is_ok() {
if let Some(status) = memory_status() {
AVAILABLE_COMMIT.store(status.ullAvailPageFile, Ordering::Relaxed);
}
}
has_upload_reserve(AVAILABLE_COMMIT.load(Ordering::Relaxed), bake)
}
pub(crate) fn limited_host_ram() -> bool {
static LIMITED: OnceLock<bool> = OnceLock::new();
*LIMITED.get_or_init(|| memory_status().is_some_and(|s| s.ullTotalPhys <= 32 * GIB))
@@ -79,6 +116,16 @@ pub(crate) fn prefetch_limit(bytes: u64, pressure: bool, limited: bool, staging:
mod tests {
use super::*;
#[test]
fn bake_and_lookahead_leave_room_for_live_game_allocations() {
for (commit, bake, lookahead) in [
(277 << 20, false, false), (2 * GIB, true, false), (4 * GIB, true, true),
] {
assert_eq!(has_upload_reserve(commit, true), bake);
assert_eq!(has_upload_reserve(commit, false), lookahead);
}
}
#[test]
fn either_constraint_enters_pressure_and_both_must_recover() {
assert!(next_pressure(false, 12 * GIB, 6 * GIB));
+1
View File
@@ -2533,6 +2533,7 @@ fn host_texture_as(
}
};
if let Err(e) = &made {
crate::residency::allocation_error(&e);
tracing::warn!(error = %e, desc = ?d, reserved, "no host texture");
}
if !reserved