Merge pull request #563 from ratatrampa007/fix/720p-targets-settle

fix(gpu): settle targets_room_kept() at 720p (Series S)
This commit is contained in:
goldfishdev
2026-12-05 15:08:07 -06:00
committed by GitHub

View File

@ -1830,35 +1830,113 @@ 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;
#[cfg(any(windows, test))]
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))]
struct TargetsRoomTracker {
last_targets: u64,
grew_at: u64,
sample_frames: u64,
sample_drawn: u64,
world_since: Option<u64>,
non_world_since: Option<u64>,
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};
let most = 3221225472;
let settle = 20000;
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 `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;
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);
if settle > 0 && targets >= most / 2 && now.saturating_sub(since) >= settle {
0
} else {
most.saturating_sub(targets)
}
static TRACKER: std::sync::Mutex<TargetsRoomTracker> =
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
@ -4195,6 +4273,89 @@ mod tests {
assert_eq!(family(66), 66);
}
#[test]
fn targets_room_settles_at_720p_but_not_pre_world() {
const MIB: u64 = 1 << 20;
// 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!(
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
);
// 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!(
s720.kept(1_260 * MIB, frames, drawn, 10_000),
TARGETS_ROOM_MOST - 1_260 * MIB
);
// 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;
type Map = extern "system" fn(This, u32, *const c_void, *mut *mut u8) -> HRESULT;
type Address = extern "system" fn(This) -> u64;