From bf3ce7b4e0c072fd1482ef134ef5c0e1d399585a Mon Sep 17 00:00:00 2001 From: ratatrampa007 <365104021+ratatrampa007@users.noreply.github.com> Date: Sat, 5 Dec 2026 02:02:01 -0500 Subject: [PATCH 1/2] fix(gpu): settle targets_room_kept() at 720p (Series S) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit targets_room_kept() reserves 3,072 MiB for host-written render targets and releases the unused reserve after 20 s of stability once targets reach the floor. Previously the floor was most / 2 (1,536 MiB). At 1280x720 (and scaled 720p) on Series S, in-world render targets settle around 1.0-1.25 GiB, never reaching 1,536 MiB. As a result, targets_room_kept() permanently reported ~1.8 GiB of phantom VRAM usage for the entire session, forcing room() closed and pinning vram_plan() in urgent 1.2 s eviction mode. Lower the settle floor to 768 MiB — safely above the ~652 MiB pre-world footprint while letting 720p in-world sessions settle cleanly after 20 s. --- crates/victory-gpu/src/resource.rs | 50 +++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index f6c55bdc..95f86304 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -1808,16 +1808,34 @@ pub(crate) fn room(memory: Option<[u64; 4]>) -> bool { /// 238), and the queue was refused from the first world frame on. With /// their room kept, the queue stops that much earlier while the world /// loads and gets it back as the targets are made. +#[cfg(any(windows, test))] +const TARGETS_ROOM_MOST: u64 = 3221225472; +#[cfg(any(windows, test))] +const TARGETS_SETTLE_MS: u64 = 20_000; +/// Minimum host-written render-target footprint before the settle timer may +/// release the unused reserve: above the ~652 MiB pre-world footprint so load +/// time stays protected, and below the ~1.0-1.25 GiB Series S 720p in-world +/// footprint (which never reached `TARGETS_ROOM_MOST / 2` = 1,536 MiB). +#[cfg(any(windows, test))] +const TARGETS_SETTLE_FLOOR: u64 = 768 << 20; + +#[cfg(any(windows, test))] +fn targets_room_for(targets: u64, since: u64, now: u64) -> u64 { + if targets >= TARGETS_SETTLE_FLOOR && now.saturating_sub(since) >= TARGETS_SETTLE_MS { + 0 + } else { + TARGETS_ROOM_MOST.saturating_sub(targets) + } +} + #[cfg(windows)] pub(crate) fn targets_room_kept() -> u64 { use std::sync::atomic::{AtomicU64, Ordering::Relaxed}; - let most = 3221225472; - let settle = 20000; // Every host texture's bytes less those of the copies the title fills. let targets = HOST_TEXTURES[1] .load(Relaxed) .saturating_sub(KINDS[1].load(Relaxed)); - // Once render targets have settled (no >= 64 MiB growth for `settle` ms), + // Once render targets have settled (no >= 64 MiB growth for `TARGETS_SETTLE_MS`), // return the unused reserve back to the texture budget (Vertex / HotScarlett). static GREW: [AtomicU64; 2] = [const { AtomicU64::new(0) }; 2]; const STEP: u64 = 64 << 20; @@ -1832,11 +1850,7 @@ pub(crate) fn targets_room_kept() -> u64 { }; GREW[0].store(last, Relaxed); GREW[1].store(since, Relaxed); - if settle > 0 && targets >= most / 2 && now.saturating_sub(since) >= settle { - 0 - } else { - most.saturating_sub(targets) - } + targets_room_for(targets, since, now) } /// [`room`] as it was at most a tenth of a second ago, for a question @@ -4045,6 +4059,26 @@ mod tests { assert_eq!(family(66), 66); } + #[test] + fn targets_room_settles_at_720p_but_not_pre_world() { + const MIB: u64 = 1 << 20; + // Pre-world targets (~652 MiB) stay below the settle floor even long after boot. + assert_eq!( + targets_room_for(652 * MIB, 0, 60_000), + TARGETS_ROOM_MOST - 652 * MIB + ); + // Series S 720p in-world targets (~1.0-1.23 GiB) keep the reserve while settling, + // then release it once stable for TARGETS_SETTLE_MS. + assert_eq!( + targets_room_for(1_004 * MIB, 10_000, 29_999), + TARGETS_ROOM_MOST - 1_004 * MIB + ); + assert_eq!(targets_room_for(1_004 * MIB, 10_000, 30_000), 0); + assert_eq!(targets_room_for(1_260 * MIB, 10_000, 30_000), 0); + // 1080p/1440p targets behave the same way. + assert_eq!(targets_room_for(2_350 * MIB, 10_000, 30_000), 0); + } + type Release = extern "system" fn(This) -> u32; type Map = extern "system" fn(This, u32, *const c_void, *mut *mut u8) -> HRESULT; type Address = extern "system" fn(This) -> u64; From 8249de448763ee1fab998251bfc7e89695fe6720 Mon Sep 17 00:00:00 2001 From: ratatrampa007 <365104021+ratatrampa007@users.noreply.github.com> Date: Sat, 5 Dec 2026 12:04:15 -0500 Subject: [PATCH 2/2] fix(gpu): gate 768 MiB targets_room_kept() floor on active world draws Address review feedback on #563: high-resolution (1080p/4K) menus or loading screens can hold 800-1,500 MiB of render targets for over 20 s before the first world frame. Keep the legacy `targets >= most / 2` (1,536 MiB) settle path unchanged, and allow the lower 768 MiB floor only after `TargetsRoomTracker` observes sustained active world draws (>= 1,000 draws/frame over >= 10-frame windows for 20 s, above menu ~7-85 and pause/reload ~250-420 draws/frame and below small interiors ~1,050-1,900 draws/frame). Returning below 1,000 draws/frame for 5 s resets `in_world`, restoring the full pre-world reserve across save reloads and menu transitions. Expand `targets_room_settles_at_720p_but_not_pre_world` to cover the `TargetsRoomTracker` state machine end to end (high-res menu/pause protection, 720p settle timing, small-interior continuity, +64 MiB re-arm, 5 s leave-world reset, and the legacy >=1,536 MiB path). --- crates/victory-gpu/src/resource.rs | 197 ++++++++++++++++++++++++----- 1 file changed, 162 insertions(+), 35 deletions(-) diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index 95f86304..1d576f14 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -1812,45 +1812,109 @@ pub(crate) fn room(memory: Option<[u64; 4]>) -> bool { const TARGETS_ROOM_MOST: u64 = 3221225472; #[cfg(any(windows, test))] const TARGETS_SETTLE_MS: u64 = 20_000; -/// Minimum host-written render-target footprint before the settle timer may -/// release the unused reserve: above the ~652 MiB pre-world footprint so load -/// time stays protected, and below the ~1.0-1.25 GiB Series S 720p in-world -/// footprint (which never reached `TARGETS_ROOM_MOST / 2` = 1,536 MiB). #[cfg(any(windows, test))] -const TARGETS_SETTLE_FLOOR: u64 = 768 << 20; +const TARGETS_SETTLE_STEP: u64 = 64 << 20; +/// Lower settle floor once the world is actively drawing: above the ~652 MiB +/// pre-world footprint and below Series S 720p (~1.0-1.25 GiB) and Series X +/// 1080p (~1.45-1.53 GiB) in-world footprints (neither of which reaches +/// `TARGETS_ROOM_MOST / 2` = 1,536 MiB). +#[cfg(any(windows, test))] +const TARGETS_WORLD_FLOOR: u64 = 768 << 20; +/// Average draws per frame that distinguish an active world from menus, +/// pause/map screens, and loading transitions (~7-85 draws/frame in menus, +/// ~250-420 in pause/reload screens, vs 1,050-7,500+ in gameplay). +#[cfg(any(windows, test))] +const WORLD_DRAWS_PER_FRAME: u64 = 1_000; +/// Duration below [`WORLD_DRAWS_PER_FRAME`] before treating the title as +/// having left the world (restoring the full pre-world reserve for the next load). +#[cfg(any(windows, test))] +const WORLD_LEAVE_MS: u64 = 5_000; +/// Minimum frames between draw-rate samples to avoid single-frame jitter. +#[cfg(any(windows, test))] +const WORLD_SAMPLE_FRAMES: u64 = 10; #[cfg(any(windows, test))] -fn targets_room_for(targets: u64, since: u64, now: u64) -> u64 { - if targets >= TARGETS_SETTLE_FLOOR && now.saturating_sub(since) >= TARGETS_SETTLE_MS { - 0 - } else { - TARGETS_ROOM_MOST.saturating_sub(targets) +struct TargetsRoomTracker { + last_targets: u64, + grew_at: u64, + sample_frames: u64, + sample_drawn: u64, + world_since: Option, + non_world_since: Option, + in_world: bool, +} + +#[cfg(any(windows, test))] +impl TargetsRoomTracker { + const fn new() -> Self { + Self { + last_targets: 0, + grew_at: 0, + sample_frames: 0, + sample_drawn: 0, + world_since: None, + non_world_since: None, + in_world: false, + } + } + + fn kept(&mut self, targets: u64, frames: u64, drawn: u64, now: u64) -> u64 { + if targets >= self.last_targets.saturating_add(TARGETS_SETTLE_STEP) { + self.last_targets = targets; + self.grew_at = now; + } else if targets.saturating_add(TARGETS_SETTLE_STEP) <= self.last_targets { + self.last_targets = targets; + } + + let df = frames.saturating_sub(self.sample_frames); + if df >= WORLD_SAMPLE_FRAMES { + let avg_draws = drawn.saturating_sub(self.sample_drawn) / df; + self.sample_frames = frames; + self.sample_drawn = drawn; + if avg_draws >= WORLD_DRAWS_PER_FRAME { + self.non_world_since = None; + let ws = *self.world_since.get_or_insert(now); + if now.saturating_sub(ws) >= TARGETS_SETTLE_MS { + self.in_world = true; + } + } else { + self.world_since = None; + let nws = *self.non_world_since.get_or_insert(now); + if now.saturating_sub(nws) >= WORLD_LEAVE_MS { + self.in_world = false; + } + } + } + + let settled = now.saturating_sub(self.grew_at) >= TARGETS_SETTLE_MS; + let above_legacy = targets >= TARGETS_ROOM_MOST / 2; + let above_world = self.in_world && targets >= TARGETS_WORLD_FLOOR; + if settled && (above_legacy || above_world) { + 0 + } else { + TARGETS_ROOM_MOST.saturating_sub(targets) + } } } #[cfg(windows)] pub(crate) fn targets_room_kept() -> u64 { - use std::sync::atomic::{AtomicU64, Ordering::Relaxed}; + use std::sync::atomic::Ordering::Relaxed; // Every host texture's bytes less those of the copies the title fills. let targets = HOST_TEXTURES[1] .load(Relaxed) .saturating_sub(KINDS[1].load(Relaxed)); // Once render targets have settled (no >= 64 MiB growth for `TARGETS_SETTLE_MS`), // return the unused reserve back to the texture budget (Vertex / HotScarlett). - static GREW: [AtomicU64; 2] = [const { AtomicU64::new(0) }; 2]; - const STEP: u64 = 64 << 20; - let now = crate::host::now_ms(); - let (last, since) = (GREW[0].load(Relaxed), GREW[1].load(Relaxed)); - let (last, since) = if targets >= last.saturating_add(STEP) { - (targets, now) - } else if targets.saturating_add(STEP) <= last { - (targets, since) - } else { - (last, since) - }; - GREW[0].store(last, Relaxed); - GREW[1].store(since, Relaxed); - targets_room_for(targets, since, now) + static TRACKER: std::sync::Mutex = + std::sync::Mutex::new(TargetsRoomTracker::new()); + let mut tracker = TRACKER.lock().unwrap_or_else(|e| e.into_inner()); + tracker.kept( + targets, + crate::frame::current(), + crate::draw::drawn(), + crate::host::now_ms(), + ) } /// [`room`] as it was at most a tenth of a second ago, for a question @@ -4062,21 +4126,84 @@ mod tests { #[test] fn targets_room_settles_at_720p_but_not_pre_world() { const MIB: u64 = 1 << 20; - // Pre-world targets (~652 MiB) stay below the settle floor even long after boot. + + // 1. High-res menu/loading/pause screen holding 1,100 MiB (between 768 and 1,536 MiB) + // with low menu (~80 draws/f) or pause/reload (~400 draws/f) draw counts must NOT + // release the reserve early. + let mut menu = TargetsRoomTracker::new(); assert_eq!( - targets_room_for(652 * MIB, 0, 60_000), + menu.kept(1_100 * MIB, 0, 0, 1_000), + TARGETS_ROOM_MOST - 1_100 * MIB + ); + assert_eq!( + menu.kept(1_100 * MIB, 300, 300 * 80, 31_000), + TARGETS_ROOM_MOST - 1_100 * MIB + ); + assert_eq!( + menu.kept(1_100 * MIB, 600, 300 * 80 + 300 * 400, 61_000), + TARGETS_ROOM_MOST - 1_100 * MIB + ); + + // 2. Series S 720p in-world sequence (~1,260 MiB, ~1,200-3,500 draws/frame): + // stays reserved while settling, releases at 20 s, stays released in smaller + // interiors (~1,200 draws/f), re-arms on +64 MiB growth, and restores full + // reserve after 5 s back in a menu/reload (<1,000 draws/frame). + let mut s720 = TargetsRoomTracker::new(); + // Pre-world menu (652 MiB, 80 draws/f) + assert_eq!( + s720.kept(652 * MIB, 10, 800, 1_000), TARGETS_ROOM_MOST - 652 * MIB ); - // Series S 720p in-world targets (~1.0-1.23 GiB) keep the reserve while settling, - // then release it once stable for TARGETS_SETTLE_MS. + // First world frame jumps to 1,260 MiB and >=1,000 draws/f + let mut frames = 20u64; + let mut drawn = 800 + 10 * 3_500; assert_eq!( - targets_room_for(1_004 * MIB, 10_000, 29_999), - TARGETS_ROOM_MOST - 1_004 * MIB + s720.kept(1_260 * MIB, frames, drawn, 10_000), + TARGETS_ROOM_MOST - 1_260 * MIB ); - assert_eq!(targets_room_for(1_004 * MIB, 10_000, 30_000), 0); - assert_eq!(targets_room_for(1_260 * MIB, 10_000, 30_000), 0); - // 1080p/1440p targets behave the same way. - assert_eq!(targets_room_for(2_350 * MIB, 10_000, 30_000), 0); + // 19.999 s later: still reserved + frames += 300; + drawn += 300 * 3_500; + assert_eq!( + s720.kept(1_260 * MIB, frames, drawn, 29_999), + TARGETS_ROOM_MOST - 1_260 * MIB + ); + // At 20.000 s: both targets and world settled -> released to 0 + frames += 10; + drawn += 10 * 3_500; + assert_eq!(s720.kept(1_260 * MIB, frames, drawn, 30_000), 0); + // Small interior (~1,200 draws/f) and slow <64 MiB creep stay released + frames += 150; + drawn += 150 * 1_200; + assert_eq!(s720.kept(1_320 * MIB, frames, drawn, 36_000), 0); + // Crossing +64 MiB step re-arms reserve for 20 s + frames += 10; + drawn += 10 * 3_500; + assert_eq!( + s720.kept(1_324 * MIB, frames, drawn, 40_000), + TARGETS_ROOM_MOST - 1_324 * MIB + ); + frames += 10; + drawn += 10 * 3_500; + assert_eq!(s720.kept(1_324 * MIB, frames, drawn, 60_000), 0); + // Leaving world to menu/save reload (<1,000 draws/f for 5 s) restores full reserve + frames += 10; + drawn += 10 * 400; + assert_eq!(s720.kept(1_324 * MIB, frames, drawn, 61_000), 0); + frames += 150; + drawn += 150 * 80; + assert_eq!( + s720.kept(1_324 * MIB, frames, drawn, 66_000), + TARGETS_ROOM_MOST - 1_324 * MIB + ); + + // 3. Legacy 1080p/1440p path (>=1,536 MiB) behaves identically to main even without world flag. + let mut hi = TargetsRoomTracker::new(); + assert_eq!( + hi.kept(2_350 * MIB, 0, 0, 10_000), + TARGETS_ROOM_MOST - 2_350 * MIB + ); + assert_eq!(hi.kept(2_350 * MIB, 0, 0, 30_000), 0); } type Release = extern "system" fn(This) -> u32;