Frame rate switch: VICTORY_FRAME_INTERVAL_MS (our clock) and the title's own 30 fps setting patched to match (off by default)

The title asks for 33,333 us (SetFrameIntervalX) because its graphics
settings hold a frame rate of 30 (settings +0x210): +0x1e0fb30 and
+0x22434a0 turn it into a frame time (+0x98f84ac), +0x247ab10 picks the
interval from it and the display's 60/120 Hz (a 120 Hz mode alone changes
nothing at 30), and +0x120530 is a QueryPerformanceCounter limiter of its
own at the same rate.

VICTORY_FRAME_INTERVAL_MS=<ms> (16.667, 8.333, 0 = no wait): frame.rs ticks
at that interval whatever the title sets; the loader makes the two loads of
the setting "mov eax, rate; nop" (rate = 1000/ms rounded, 0 = the title's
own "no limit"), with the original bytes checked. VICTORY_TITLE_FPS=<n>|off
overrides the rate or leaves the title alone. Diagnostics:
VICTORY_TIMER_LOG=1 logs the title's timers once a second and frame pacing
bands every 5 s.

Ported from Belobug/VIctory-Belobug-AMD 49cb83b (author: local); the descriptor-site module of renderer port 2 left out. Our VICTORY_FRAME_INTERVAL_US (our clock alone) still works; VICTORY_FRAME_INTERVAL_MS is read first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 authored and readmet3xt-lab committed 2026-11-27 01:08:53 +05:30
1 parent a83bc910a7
commit bacb402091
6 files changed
+468 -5

No files matched your search

+129 -5
View File
@@ -8,6 +8,12 @@
//!
//! shortcut: event offsets are ignored (every event is at the tick); tie
//! the clock to presentation if a title paces by them.
//!
//! `VICTORY_FRAME_INTERVAL_MS=<ms>` ticks at that interval whatever the
//! title sets (16.667: 60 a second; 0: no wait at all, every wait ends at
//! once); unset, the title's interval as before. The loader gives the title
//! the matching frame rate too (`victory_loader::frame_rate`), as the title
//! would otherwise keep to its 30 with a limiter of its own.
#![allow(clippy::not_unsafe_ptr_arg_deref)]
use std::sync::Mutex;
@@ -25,6 +31,28 @@ const ORIGIN: u32 = 0;
const TOKEN_ON_TIMEOUT: u32 = 2;
const SIXTIETH: Duration = Duration::from_micros(16_667);
/// `VICTORY_FRAME_INTERVAL_MS`: the interval asked for in place of the
/// title's (zero: no wait). Read once.
fn asked() -> Option<Duration> {
static ASKED: std::sync::OnceLock<Option<Duration>> = std::sync::OnceLock::new();
*ASKED.get_or_init(|| {
let spec = std::env::var("VICTORY_FRAME_INTERVAL_MS").ok()?;
let ms = parse_ms(&spec);
match ms {
Some(d) => tracing::info!(interval = ?d, "frame interval of ours in place of the title's (VICTORY_FRAME_INTERVAL_MS)"),
None => tracing::warn!(spec, "VICTORY_FRAME_INTERVAL_MS: expected milliseconds from 0 to 1000; the title's interval kept"),
}
ms
})
}
/// `16.667` → 16,667 us; 0 → zero; negative, more than a second or not a
/// number → `None`.
fn parse_ms(spec: &str) -> Option<Duration> {
let ms = spec.trim().parse::<f64>().ok().filter(|ms| ms.is_finite() && (0.0..=1000.0).contains(ms))?;
Some(Duration::from_nanos((ms * 1e6).round() as u64))
}
struct Clock {
start: Instant,
interval: Duration,
@@ -68,12 +96,14 @@ pub(crate) extern "system" fn set_interval(
period: u32,
flags: u32,
) -> HRESULT {
let asked = match length {
let asked_by_title = match length {
0 => SIXTIETH,
us => Duration::from_micros(u64::from(us)),
};
let interval = interval_override().unwrap_or(asked);
tracing::info!(?asked, ?interval, period, flags, "frame interval");
// VICTORY_FRAME_INTERVAL_MS (with the title's own rate patched to match)
// first, then VICTORY_FRAME_INTERVAL_US (our clock alone).
let interval = asked().or_else(interval_override).unwrap_or(asked_by_title);
tracing::info!(?interval, title = ?asked_by_title, period, flags, "frame interval");
// A new interval starts from now, after the last tick handed out.
let first = LAST
.iter()
@@ -125,9 +155,14 @@ fn next_tick(kind: u32) -> (u64, Duration) {
let mut clock = clock();
let c = clock.get_or_insert_with(|| Clock {
start: Instant::now(),
interval: interval_override().unwrap_or(SIXTIETH),
interval: asked().or_else(interval_override).unwrap_or(SIXTIETH),
first: 0,
});
if c.interval.is_zero() {
// No wait: every wait ends at once, on a tick of its own.
let tick = last(kind).load(Ordering::Relaxed) + 1;
return (tick, Duration::ZERO);
}
let elapsed = c.start.elapsed().as_nanos();
let interval = c.interval.as_nanos().max(1);
let tick =
@@ -173,7 +208,9 @@ pub(crate) extern "system" fn wait(
std::thread::sleep(limit);
TIMEOUT
} else {
std::thread::sleep(wait);
if !wait.is_zero() {
std::thread::sleep(wait);
}
last(kind).fetch_max(tick, Ordering::Relaxed);
S_OK
};
@@ -187,6 +224,7 @@ pub(crate) extern "system" fn wait(
if origin && (hr == S_OK || flags & TOKEN_ON_TIMEOUT != 0) {
// Tokens count frames (consecutive), not ticks.
let frame = FRAME.fetch_add(1, Ordering::Relaxed) + 1;
pace::begun();
if !token.is_null() {
// SAFETY: checked non-null; the title passes its token.
unsafe { token.write(frame) };
@@ -206,6 +244,81 @@ pub(crate) extern "system" fn wait(
hr
}
/// How even the frames are (`VICTORY_TIMER_LOG=1`): the times between frame
/// beginnings, logged every 5 s by bands, with the pairs of a short frame
/// next to a long one (a 16/33 alternation shows as many of them).
mod pace {
use std::sync::Mutex;
use std::time::{Duration, Instant};
/// Upper edges of the bands, in milliseconds (the last band is open).
const BANDS: [f64; 6] = [12.0, 20.0, 28.0, 38.0, 55.0, 70.0];
struct Stats {
since: Instant,
last: Option<Instant>,
last_ms: f64,
bands: [u32; 7],
sum_ms: f64,
max_ms: f64,
n: u32,
alternations: u32,
}
static STATS: Mutex<Option<Stats>> = Mutex::new(None);
fn on() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var("VICTORY_TIMER_LOG").is_ok_and(|v| v.trim() == "1"))
}
pub(super) fn begun() {
if !on() {
return;
}
let now = Instant::now();
let mut stats = STATS.lock().unwrap_or_else(|e| e.into_inner());
let s = stats.get_or_insert_with(|| Stats {
since: now,
last: None,
last_ms: 0.0,
bands: [0; 7],
sum_ms: 0.0,
max_ms: 0.0,
n: 0,
alternations: 0,
});
if let Some(last) = s.last.replace(now) {
let ms = (now - last).as_secs_f64() * 1e3;
let band = BANDS.iter().position(|&edge| ms < edge).unwrap_or(BANDS.len());
s.bands[band] += 1;
s.sum_ms += ms;
s.max_ms = s.max_ms.max(ms);
if s.n > 0 && (ms < 20.0 && s.last_ms > 28.0 || ms > 28.0 && s.last_ms < 20.0) {
s.alternations += 1;
}
s.last_ms = ms;
s.n += 1;
}
if now - s.since >= Duration::from_secs(5) && s.n > 0 {
tracing::info!(
frames = s.n,
mean_ms = format_args!("{:.2}", s.sum_ms / f64::from(s.n)),
max_ms = format_args!("{:.1}", s.max_ms),
bands = ?s.bands,
alternations = s.alternations,
"frame pacing: times between frame beginnings (bands <12, <20, <28, <38, <55, <70, more ms)"
);
s.since = now;
s.bands = [0; 7];
s.sum_ms = 0.0;
s.max_ms = 0.0;
s.n = 0;
s.alternations = 0;
}
}
}
/// Device method: no statistics are kept, so none are returned.
pub(crate) extern "system" fn statistics(
_: This,
@@ -225,6 +338,17 @@ pub(crate) extern "system" fn statistics(
mod tests {
use super::*;
#[test]
fn the_switch_reads_milliseconds() {
assert_eq!(parse_ms("16.667"), Some(Duration::from_micros(16_667)));
assert_eq!(parse_ms(" 8.333 "), Some(Duration::from_micros(8_333)));
assert_eq!(parse_ms("0"), Some(Duration::ZERO));
assert_eq!(parse_ms("-1"), None);
assert_eq!(parse_ms("1001"), None);
assert_eq!(parse_ms("NaN"), None);
assert_eq!(parse_ms("fast"), None);
}
#[test]
fn waits_end_on_ticks_and_count_frames() {
let null = std::ptr::null();