diff --git a/crates/victory-gpu/src/residency.rs b/crates/victory-gpu/src/residency.rs index 2a9bb46..49a66a8 100644 --- a/crates/victory-gpu/src/residency.rs +++ b/crates/victory-gpu/src/residency.rs @@ -6,6 +6,10 @@ use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORY const MIB: u64 = 1 << 20; const GIB: u64 = 1 << 30; static PRESSURED: AtomicBool = AtomicBool::new(false); +/// The card's share of the PC's memory (DXGI's non-local budget, which Windows +/// caps at half the RAM) is nearly used up: [`SHARED`]'s levels. +static SHARED_PRESSURED: AtomicBool = AtomicBool::new(false); +static SHARED_CRITICAL: AtomicBool = AtomicBool::new(false); static HEADROOM: AtomicU64 = AtomicU64::new(u64::MAX); fn memory_status() -> Option { @@ -110,6 +114,7 @@ fn next_pressure(was: bool, physical: u64, commit: u64) -> bool { /// Called by the once-per-second submission sweep. A failed sample keeps the last good state. pub(crate) fn refresh() { + refresh_shared(); let Some(status) = memory_status() else { return }; let (physical, commit) = (status.ullAvailPhys, status.ullAvailPageFile); HEADROOM.store(physical.min(commit), Ordering::Relaxed); @@ -141,14 +146,65 @@ pub(crate) fn headroom() -> Option { } pub(crate) fn pressured() -> bool { - PRESSURED.load(Ordering::Relaxed) + PRESSURED.load(Ordering::Relaxed) || SHARED_PRESSURED.load(Ordering::Relaxed) +} + +/// Whether the shared memory's use asks for pressure, and for the critical +/// level, from its use and budget (`was`: pressure was on). On at 85 % of the +/// budget, off again under 75 %; critical from 95 %. +/// +/// Every buffer the CPU maps and the card reads (upload, shared, readback) +/// counts against that budget, not against free RAM: on a 16 or 24 GB PC it +/// ran out with gigabytes of RAM still free, and from then on every such +/// buffer was refused, down to 256 bytes, until a host command list could not +/// close (0x8007000E; #156, #206). The same limit and ending as VIctory-NVIDIA's +/// issue 1520, whose fix scaled its L0 caches down on PCs under 24 GiB. +fn shared_levels(was: bool, usage: u64, budget: u64) -> (bool, bool) { + if budget == 0 { + return (false, false); + } + let percent = u128::from(usage) * 100 / u128::from(budget); + let pressure = if was { percent >= 75 } else { percent >= 85 }; + (pressure, percent >= 95) +} + +/// Samples the shared memory's use (once a second, by [`refresh`]). +/// `VICTORY_SHARED_PRESSURE=0`: not looked at, as before. +fn refresh_shared() { + static ON: OnceLock = OnceLock::new(); + if !*ON.get_or_init(|| std::env::var_os("VICTORY_SHARED_PRESSURE").is_none_or(|v| v != "0")) { + return; + } + let Some([_, [budget, usage, ..]]) = crate::host::host().and_then(|h| h.video_memory_groups()) else { + return; + }; + let was = SHARED_PRESSURED.load(Ordering::Relaxed); + let (pressure, critical) = shared_levels(was, usage, budget); + SHARED_CRITICAL.store(critical, Ordering::Relaxed); + if pressure != was { + SHARED_PRESSURED.store(pressure, Ordering::Relaxed); + if pressure { + tracing::warn!( + frame = crate::frame::current(), + shared_used_mib = usage >> 20, + shared_budget_mib = budget >> 20, + buffer_mirror_span_mib = crate::shadow::live_span_bytes() >> 20, + texture_staging_mib = crate::texture::live_staging_bytes() >> 20, + "the card's share of the PC's memory is nearly used: limiting texture prefetch and idle upload storage (VICTORY_SHARED_PRESSURE=0: off)" + ); + } else { + tracing::info!(shared_used_mib = usage >> 20, shared_budget_mib = budget >> 20, "shared memory pressure cleared"); + } + } } /// True when OS commit headroom is critically low (< 1.5 GiB) or an allocation /// just failed with `E_OUTOFMEMORY`, requiring emergency throttling. pub(crate) fn critical_commit() -> bool { let now = crate::host::coarse_ms(); - now < RETRY_UPLOAD_AT.load(Ordering::Relaxed) || sample_commit(now) < 1536 * MIB + now < RETRY_UPLOAD_AT.load(Ordering::Relaxed) + || SHARED_CRITICAL.load(Ordering::Relaxed) + || sample_commit(now) < 1536 * MIB } /// A soft per-frame admission limit. A single texture can cross it. The @@ -167,6 +223,17 @@ pub(crate) fn prefetch_limit(bytes: u64, pressure: bool, critical: bool, limited mod tests { use super::*; + #[test] + fn shared_memory_pressure_has_a_gap_and_a_critical_level() { + let budget = 12 * GIB; + assert_eq!(shared_levels(false, budget / 100 * 80, budget), (false, false)); + assert_eq!(shared_levels(false, budget / 100 * 86, budget), (true, false)); + assert_eq!(shared_levels(true, budget / 100 * 80, budget), (true, false)); + assert_eq!(shared_levels(true, budget / 100 * 70, budget), (false, false)); + assert_eq!(shared_levels(false, budget / 100 * 96, budget), (true, true)); + assert_eq!(shared_levels(false, 5, 0), (false, false)); + } + #[test] fn bake_and_lookahead_leave_room_for_live_game_allocations() { for (commit, bake, lookahead, growth) in [ diff --git a/docs/README-fxceless.md b/docs/README-fxceless.md index 7178283..80983ae 100644 --- a/docs/README-fxceless.md +++ b/docs/README-fxceless.md @@ -316,6 +316,7 @@ The on/off switches below (`...=1`) are off when unset or set to `0`. | `VICTORY_KEYS=30:Enter,35:X` | presses keys at those seconds, focus or not | | `VICTORY_KEYS_UNTIL=` | no more of those keys once the file is there (a script makes it when the world draws: a run with nobody at the keyboard) | | `VICTORY_SAMPLER_ORDER=0` | static samplers in the order each process meets them; by default they are made in the order of `pipelines/static-samplers.txt`, so the driver's disk cache finds pipelines again across runs (from VIctory-NVIDIA) | +| `VICTORY_SHARED_PRESSURE=0` | the card's share of the PC's memory (Task Manager's "Shared GPU memory", half the RAM) is not watched; by default memory pressure is on from 85 % of it (off under 75 %) and critical from 95 %, so spares, staging and texture prefetch shrink before every shared buffer is refused (#156, #206) | | `VICTORY_CRASH_SAVE=:,...` | saves title memory to the cache folder on a crash | | `VICTORY_CAPTURE=` | saves presented frames (powers of two, or `VICTORY_CAPTURE_FRAMES=`) | | `VICTORY_DUMP_TARGETS=1` | with the two above, saves every render target of those frames |