//! Host-memory admission for speculative texture work and reusable upload storage. use std::sync::OnceLock; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX}; const GIB: u64 = 1 << 30; static PRESSURED: AtomicBool = AtomicBool::new(false); static HEADROOM: AtomicU64 = AtomicU64::new(u64::MAX); fn memory_status() -> Option { // SAFETY: the API initializes the fields of a correctly sized structure. let mut status: MEMORYSTATUSEX = unsafe { std::mem::zeroed() }; status.dwLength = std::mem::size_of::() as u32; (unsafe { GlobalMemoryStatusEx(&mut status) } != 0).then_some(status) } pub(crate) fn limited_host_ram() -> bool { static LIMITED: OnceLock = OnceLock::new(); *LIMITED.get_or_init(|| memory_status().is_some_and(|s| s.ullTotalPhys <= 32 * GIB)) } fn next_pressure(was: bool, physical: u64, commit: u64) -> bool { if was { physical < 6 * GIB || commit < 8 * GIB } else { physical <= 4 * GIB || commit <= 6 * GIB } } /// Called by the once-per-second submission sweep. A failed sample keeps the last good state. pub(crate) fn refresh() { let Some(status) = memory_status() else { return }; let (physical, commit) = (status.ullAvailPhys, status.ullAvailPageFile); HEADROOM.store(physical.min(commit), Ordering::Relaxed); let was = pressured(); let pressure = next_pressure(was, physical, commit); if pressure != was { PRESSURED.store(pressure, Ordering::Relaxed); if pressure { tracing::warn!( pid = std::process::id(), frame = crate::frame::current(), physical_free_mib = physical >> 20, available_commit_mib = commit >> 20, buffer_mirror_span_mib = crate::shadow::live_span_bytes() >> 20, texture_staging_mib = crate::texture::live_staging_bytes() >> 20, spare_scratch_mib = crate::host::ALIVE[4].load(Ordering::Relaxed) >> 20, game_filled_gpu_texture_mib = crate::resource::KINDS[1].load(Ordering::Relaxed) >> 20, "host memory pressure: limiting texture prefetch and idle upload storage" ); } else { tracing::info!(physical_free_mib = physical >> 20, available_commit_mib = commit >> 20, "host memory pressure cleared"); } } } pub(crate) fn headroom() -> Option { let bytes = HEADROOM.load(Ordering::Relaxed); (bytes != u64::MAX).then_some(bytes) } pub(crate) fn pressured() -> bool { PRESSURED.load(Ordering::Relaxed) } /// A soft per-frame admission limit. A single texture can cross it. The /// queue retains unserved slots so they can be retried on later frames. pub(crate) fn prefetch_limit(bytes: u64, pressure: bool) -> u64 { if pressure { bytes.min(16 << 20) } else { bytes } } #[cfg(test)] mod tests { use super::*; #[test] fn either_constraint_enters_pressure_and_both_must_recover() { assert!(next_pressure(false, 12 * GIB, 6 * GIB)); assert!(next_pressure(false, 4 * GIB, 16 * GIB)); assert!(next_pressure(true, 5 * GIB, 9 * GIB)); assert!(next_pressure(true, 7 * GIB, 7 * GIB)); assert!(!next_pressure(true, 6 * GIB, 8 * GIB)); assert!(!next_pressure(false, 5 * GIB, 7 * GIB)); } #[test] fn prefetch_respects_existing_budgets_and_resumes_after_recovery() { assert_eq!(prefetch_limit(0, true), 0); assert_eq!(prefetch_limit(8 << 20, true), 8 << 20); assert_eq!(prefetch_limit(512 << 20, true), 16 << 20); assert_eq!(prefetch_limit(192 << 20, false), 192 << 20); } }