diff --git a/crates/victory-gpu/src/pool.rs b/crates/victory-gpu/src/pool.rs index f94df0483..0af4a1e02 100644 --- a/crates/victory-gpu/src/pool.rs +++ b/crates/victory-gpu/src/pool.rs @@ -44,12 +44,13 @@ static SPARES: AtomicUsize = AtomicUsize::new(SPARE); pub(crate) fn room(memory: Option<[u64; 4]>) { let pressure = crate::residency::pressured(); let limit = spares(memory, crate::host::keep_freed() && !pressure); - SPARES.store(limit, Ordering::Relaxed); + let texture_limit = texture_spares(memory, limit, spare_scale().then(ahead)); + SPARES.store(texture_limit, Ordering::Relaxed); let tick = now(); // A full pool can become idle while another subsystem needs the VRAM. // A busy allocator observes the new limit on its next allocation or tick. if let Ok(mut pool) = TEXTURES.try_lock() { - pool.reclaim(tick, limit, true); + pool.reclaim(tick, texture_limit, true); } if let Ok(mut pool) = STAGING.try_lock() { let staging_limit = if crate::residency::critical_commit() { @@ -69,6 +70,26 @@ pub(crate) fn room(memory: Option<[u64; 4]>) { } } +/// `VICTORY_SPARE_SCALE=1` caps empty texture blocks by the DXGI budget. +fn spare_scale() -> bool { + static SCALE: std::sync::OnceLock = std::sync::OnceLock::new(); + *SCALE.get_or_init(|| std::env::var_os("VICTORY_SPARE_SCALE").is_some_and(|v| v == "1")) +} + +fn texture_spares(memory: Option<[u64; 4]>, limit: usize, ripe_ahead: Option) -> usize { + match (memory, ripe_ahead) { + (Some([budget, ..]), Some(ripe_ahead)) => { + // Both Ripe::now and the staging pool's rested() read SPARES. + let scaled = (budget / 32 / BLOCK) + .max(ripe_ahead as u64) + .max(STAGING_KEPT as u64) + .min(SPARE_KEPT as u64); + limit.min(scaled as usize) + } + _ => limit, + } +} + fn spares(memory: Option<[u64; 4]>, keep: bool) -> usize { match memory { Some([budget, used, ..]) if keep && u128::from(used) * 100 < u128::from(budget) * 85 => SPARE_KEPT, @@ -90,6 +111,8 @@ pub(crate) fn on() -> bool { *ON.get_or_init(|| std::env::var_os("VICTORY_TEXTURE_POOL").is_none_or(|v| v != "0")) } +const DEFAULT_RIPE_BLOCKS: usize = 12; + /// How many blocks of textures are made ahead, so that one has been there /// for a while when its first piece is taken (`VICTORY_RIPE_BLOCKS`, 12; /// 0: none, and new blocks are used at once, as before). @@ -104,7 +127,12 @@ pub(crate) fn on() -> bool { /// no such lists: pieces are cut from those first. fn ahead() -> usize { static AHEAD: std::sync::OnceLock = std::sync::OnceLock::new(); - *AHEAD.get_or_init(|| std::env::var("VICTORY_RIPE_BLOCKS").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(12)) + *AHEAD.get_or_init(|| { + std::env::var("VICTORY_RIPE_BLOCKS") + .ok() + .and_then(|v| v.trim().parse().ok()) + .unwrap_or(DEFAULT_RIPE_BLOCKS) + }) } /// Submissions begun and finished ([`crate::host::submissions`]), and how @@ -931,6 +959,48 @@ mod tests { assert_eq!(spares(Some([100, 85, 0, 0]), true), SPARE); assert_eq!(spares(Some([100, 10, 0, 0]), false), SPARE); assert_eq!(spares(None, true), SPARE); + + let budgets = [ + (1u64, 8, 12), + (8, 8, 12), + (12, 8, 12), + (16, 8, 12), + (24, 12, 12), + (256, SPARE_KEPT, SPARE_KEPT), + ]; + for (gib, without_ripe, with_ripe) in budgets { + let budget = gib << 30; + for (percent, legacy) in [(84, SPARE_KEPT), (85, SPARE), (86, SPARE)] { + let memory = Some([budget, (budget * percent).div_ceil(100), 0, 0]); + let limit = spares(memory, true); + for (ripe_ahead, expected) in [ + (None, legacy), + (Some(0), legacy.min(without_ripe)), + (Some(DEFAULT_RIPE_BLOCKS), legacy.min(with_ripe)), + (Some(16), legacy.min(without_ripe.max(16))), + ] { + assert_eq!( + texture_spares(memory, limit, ripe_ahead), + expected, + "{gib} GiB at {percent}%, scaled ripe floor={ripe_ahead:?}" + ); + } + } + } + + let memory = Some([8 << 30, 1 << 30, 0, 0]); + assert_eq!( + texture_spares(memory, spares(memory, false), Some(DEFAULT_RIPE_BLOCKS)), + SPARE + ); + assert_eq!( + texture_spares(None, spares(None, true), Some(DEFAULT_RIPE_BLOCKS)), + SPARE + ); + assert_eq!( + texture_spares(memory, SPARE_KEPT, Some(SPARE_KEPT + 1)), + SPARE_KEPT + ); } #[test] diff --git a/docs/README-foxhound.md b/docs/README-foxhound.md index 72926e7dd..8bf2f7ce8 100644 --- a/docs/README-foxhound.md +++ b/docs/README-foxhound.md @@ -140,6 +140,7 @@ Every `VICTORY_...` switch is described in a comment where the code reads it. Th | `VICTORY_PRELUDE_UPLOADS`, `VICTORY_SHOW_NEW_LATER`, `VICTORY_TABLED_REREAD` | on | texture upload order and slot changes (flicker, device losses) | | `VICTORY_SYNC_IN_PLACE`, `VICTORY_SPANS_QUICK`, `VICTORY_HEAP_SYNC_AT_SUBMIT` | on | cheaper descriptor-heap bookkeeping | | `VICTORY_PORT_CONSTANTS` | off | the title's constants heap read in place: less CPU, but on a 12 GB card the GPU then waits (slower overall) | +| `VICTORY_SPARE_SCALE` | off | `--set VICTORY_SPARE_SCALE=1`: below 85% use, the cap is `min(existing_limit, 64, max(budget_bytes / 32 / BLOCK, VICTORY_RIPE_BLOCKS, 8))` with `BLOCK = 64 MiB`. With the default of 12 ripe blocks, 8, 12, 16, and 24 GiB budgets each keep up to 12 blocks (768 MiB). The floor preserves the ripe-block device-loss safeguard and the staging pool's existing limit. | | `VICTORY_FRAME_INTERVAL_MS` | off | experimental: the title's own 30 fps setting patched to a higher rate | | `VICTORY_BVH_BUILDER` | off | the title's ray tracing (slow, broken) |