Merge agent/belo: frame rate switch ported from Belobug/VIctory-Belobug-AMD 49cb83b, 08b88b8 (VICTORY_FRAME_INTERVAL_MS, the title's own 30 fps setting patched to match; off by default)
Only run so far (y-fr60-a) ran from a cold shader-cache path: not yet measured fairly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
commit
fa08ad6808
6 files changed
+536
-5
No files matched your search
@@ -244,6 +244,29 @@ pub fn run(
|
||||
// SAFETY: the image stays mapped, and nothing of it has run yet.
|
||||
unsafe { victory_loader::write_sites::apply_asked(&image_name, image.base(), size) };
|
||||
}
|
||||
// The frame rate (VICTORY_FRAME_INTERVAL_MS, off without it): our frame
|
||||
// clock ticks at that interval (victory-gpu), and the title is given
|
||||
// the matching rate in place of its setting's 30 (VICTORY_TITLE_FPS
|
||||
// overrides it; `off` leaves the title alone).
|
||||
#[cfg(windows)]
|
||||
if execute && let Some(ms) = victory_loader::frame_rate::asked_interval_ms() {
|
||||
use victory_loader::frame_rate::{Patched, SITES, apply, title_interval_us, title_rate};
|
||||
let asked = std::env::var("VICTORY_TITLE_FPS").ok();
|
||||
match title_rate(ms, asked.as_deref()) {
|
||||
Some(rate) => {
|
||||
let size = image.info().size_of_image as usize;
|
||||
// SAFETY: the image stays mapped and nothing of it has run yet.
|
||||
let done = unsafe { apply(image.base(), size, rate, &SITES) };
|
||||
let patched = done.iter().filter(|d| **d == Patched::Done).count();
|
||||
println!(
|
||||
"frame rate: our clock {ms} ms; the title's rate {rate} ({patched} of {} reads of its setting patched; it asks for {} us at 60 Hz)",
|
||||
SITES.len(),
|
||||
title_interval_us(rate, false)
|
||||
);
|
||||
}
|
||||
None => println!("frame rate: our clock {ms} ms; the title's own rate (30) left as it is"),
|
||||
}
|
||||
}
|
||||
if !execute {
|
||||
println!("not executing (pass --execute to start the title)");
|
||||
return Ok(());
|
||||
@@ -281,6 +304,7 @@ fn diagnostics(image: &victory_loader::LoadedImage, cfg: &victory_core::Config)
|
||||
victory_loader::profile_on_schedule(victory_hle::frame_phase);
|
||||
victory_hle::modules::audio::capture::start();
|
||||
watch(image.base(), image.info().size_of_image as usize);
|
||||
timers(image.base(), image.info().size_of_image as usize);
|
||||
poke(image.base(), image.info().size_of_image as usize);
|
||||
// Debugging aid: `VICTORY_CRASH_SAVE=<rva>:<len>,...` (hex) writes those
|
||||
// title ranges to `crash-<rva>.bin` in the cache dir on a crash.
|
||||
@@ -329,6 +353,46 @@ fn watch(base: usize, size: usize) {
|
||||
});
|
||||
}
|
||||
|
||||
/// Debugging aid: `VICTORY_TIMER_LOG=1` logs the title's timers once a
|
||||
/// second (`victory_loader::frame_rate`): its frame time and limiter flag,
|
||||
/// and each timer's two times, step in seconds and in milliseconds. Whether
|
||||
/// game time keeps to the wall clock at any frame rate is read from these.
|
||||
#[cfg(all(windows, feature = "diagnostics"))]
|
||||
fn timers(base: usize, size: usize) {
|
||||
use victory_loader::frame_rate::{FRAME_US, LIMITER_ON, TIMERS};
|
||||
if std::env::var("VICTORY_TIMER_LOG").ok().as_deref() != Some("1") {
|
||||
return;
|
||||
}
|
||||
if TIMERS.iter().chain([&FRAME_US, &LIMITER_ON]).any(|&rva| rva + 0x14 > size) {
|
||||
return;
|
||||
}
|
||||
std::thread::spawn(move || {
|
||||
let start = std::time::Instant::now();
|
||||
let u32_at = |rva: usize| {
|
||||
// SAFETY: inside the mapped image (checked above), title data.
|
||||
unsafe { ((base + rva) as *const u32).read_volatile() }
|
||||
};
|
||||
loop {
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
let timers: Vec<String> = TIMERS
|
||||
.iter()
|
||||
.map(|&t| {
|
||||
let step = f32::from_bits(u32_at(t + 0xc));
|
||||
format!("{} {} {step:.4} {}", u32_at(t), u32_at(t + 4), u32_at(t + 0x10))
|
||||
})
|
||||
.collect();
|
||||
eprintln!(
|
||||
"VIctory: the title's timers at {} ms: frame_us {} limiter {} frames {}; timers (time ms, frames, step s, step ms): {}",
|
||||
start.elapsed().as_millis(),
|
||||
u32_at(FRAME_US),
|
||||
u32_at(LIMITER_ON) & 0xff,
|
||||
victory_hle::frames_begun(),
|
||||
timers.join(" | ")
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Debugging aid: `VICTORY_POKE=<rva>:<byte>,...` (hex) writes the byte at
|
||||
/// each title+rva twice a second. A title that ends itself when its main
|
||||
/// loop has not ticked for a while (Grand Theft Auto VI: 30 s) runs on
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -113,6 +113,11 @@ pub fn switched(switch: &str) -> Option<bool> {
|
||||
pub fn frame_phase() -> u32 {
|
||||
copystat::phase()
|
||||
}
|
||||
|
||||
/// Frames the title has begun (its frame waits that ended), for logs.
|
||||
pub fn frames_begun() -> u64 {
|
||||
frame::current()
|
||||
}
|
||||
#[cfg(windows)]
|
||||
pub use inplace::{title_memory_freeing, title_memory_section, title_memory_section_gone, title_memory_view};
|
||||
#[cfg(windows)]
|
||||
|
||||
@@ -15,6 +15,7 @@ mod host;
|
||||
mod image_watch;
|
||||
#[cfg(all(windows, feature = "diagnostics"))]
|
||||
pub use victory_gpu::frame_phase;
|
||||
pub use victory_gpu::frames_begun;
|
||||
|
||||
pub mod modules;
|
||||
mod registry;
|
||||
|
||||
@@ -0,0 +1,336 @@
|
||||
//! The title's frame rate (`VICTORY_FRAME_INTERVAL_MS`, off without it): the
|
||||
//! rate Grand Theft Auto VI asks for is a setting of its own, and two
|
||||
//! places in its code are changed at load to read another.
|
||||
//!
|
||||
//! What the title does (game_scarlett_master.exe 1.0.2.1, image offsets):
|
||||
//!
|
||||
//! - Its graphics settings hold a frame rate (+0x210 of the settings object,
|
||||
//! 30 on Series X). Two functions that apply the settings, +0x1e0fb30 and
|
||||
//! +0x22434a0, turn it into a frame time: `frame_us = 1e6 /
|
||||
//! clamp(rate)`, 0 and 1000 or more being 1000 (the code's "no limit"),
|
||||
//! stored at +0x98f84ac.
|
||||
//! - +0x247ab10 sets the frame interval from it: the display's rate is 60,
|
||||
//! or 120 if its mode list (the output's `GetDisplayModeList`) has a
|
||||
//! 120 Hz mode (+0x2481ca0); the divisor is `min(display / rate, 4)`, or
|
||||
//! 1 for a rate above the display's; the interval is a table at
|
||||
//! +0x61ece60 (`[16667, 16667, 33333, 50000, 66667]` at 60 Hz, `[8333,
|
||||
//! 8333, 16667, 25000, 33333]` at 120 Hz) and goes to
|
||||
//! `SetFrameIntervalX` (device +0x270), with `ScheduleFrameEventX`
|
||||
//! (+0x278) after it. At 30 and 60 Hz: 33,333 us, the flat 33.3 ms of the
|
||||
//! menus and videos.
|
||||
//! - +0x120530, once a flag (+0x7453ec9, set by +0x21b77c0) is up, is a
|
||||
//! limiter of its own on `QueryPerformanceCounter`: it waits until
|
||||
//! `1000 / trunc(1e6 / frame_us)` ms have passed since the last frame.
|
||||
//! - +0x3eaf5d0 hands `1e6 / frame_us` to scripts, and +0x234490 scales
|
||||
//! per-frame amounts by it (1.0 below 60 except 0.7 at 40, 0.5 from 60).
|
||||
//!
|
||||
//! - The simulation: +0x22264d0 hands each of its timers the real time
|
||||
//! since the last frame (`QueryPerformanceCounter`); +0x2226040 makes the
|
||||
//! step of it, at most 1/15 s and at least `1 / rate` (+0x6849844, made
|
||||
//! at +0x22435de and +0x2241190 from +0x2241fd0, which reads the same
|
||||
//! setting). So at 30 every frame shorter than 33.3 ms still moves game
|
||||
//! time 33.3 ms: a fixed 30 Hz step for fast frames, delta time for slow
|
||||
//! ones.
|
||||
//!
|
||||
//! So reporting a 120 Hz display alone changes nothing at 30 (divisor 4,
|
||||
//! 33,333 us). Here the two loads of the setting (`mov eax, [reg+210h]`)
|
||||
//! become `mov eax, rate ; nop`, so the title's frame time, interval and
|
||||
//! own limiter follow the rate asked for, and +0x2241fd0 returns the rate
|
||||
//! too (1000 for no limit), so the shortest step follows; the setting
|
||||
//! itself is left as it is. Each site's bytes, from the load to the store of the frame time, are
|
||||
//! compared before anything is written; a site whose bytes differ is left
|
||||
//! alone and said so (another build of the title).
|
||||
|
||||
/// The switch: the interval in milliseconds our frame clock ticks at
|
||||
/// (`victory-gpu`'s `frame`), 0 for no wait at all. `None` if unset or
|
||||
/// malformed (said why).
|
||||
pub fn asked_interval_ms() -> Option<f64> {
|
||||
let spec = std::env::var("VICTORY_FRAME_INTERVAL_MS").ok()?;
|
||||
let ms = parse_interval_ms(&spec);
|
||||
if ms.is_none() {
|
||||
eprintln!("VIctory: VICTORY_FRAME_INTERVAL_MS={spec}: expected milliseconds from 0 (no wait) to 1000");
|
||||
}
|
||||
ms
|
||||
}
|
||||
|
||||
/// `16.667` → 16.667; 0 is allowed (no wait), more than a second is not.
|
||||
pub fn parse_interval_ms(spec: &str) -> Option<f64> {
|
||||
spec.trim().parse::<f64>().ok().filter(|ms| ms.is_finite() && (0.0..=1000.0).contains(ms))
|
||||
}
|
||||
|
||||
/// The frame rate the title is given for an interval: `1000 / ms` rounded
|
||||
/// (16.667 → 60, 8.333 → 120, 33.333 → 30), 0 for no wait (the title's own
|
||||
/// "no limit"). `VICTORY_TITLE_FPS=<rate>` gives it outright, `off` leaves the
|
||||
/// title as it is (only our clock changes).
|
||||
pub fn title_rate(interval_ms: f64, title_fps: Option<&str>) -> Option<u32> {
|
||||
match title_fps.map(str::trim) {
|
||||
Some("off") => None,
|
||||
Some(rate) => rate.parse::<u32>().ok().filter(|r| *r <= 1000),
|
||||
None if interval_ms <= 0.0 => Some(0),
|
||||
None => Some(((1000.0 / interval_ms).round() as u32).clamp(1, 1000)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The frame time the title derives from a rate, in microseconds, as its
|
||||
/// code computes it (single-precision division, truncated).
|
||||
pub fn title_frame_us(rate: u32) -> u32 {
|
||||
let clamped = if rate == 0 || rate >= 1000 { 1000 } else { rate };
|
||||
(1_000_000.0_f32 / clamped as f32) as u32
|
||||
}
|
||||
|
||||
/// The interval the title then asks for (`SetFrameIntervalX`), in
|
||||
/// microseconds, on a display of 60 Hz (or 120 Hz).
|
||||
pub fn title_interval_us(rate: u32, display_120: bool) -> u32 {
|
||||
const TABLE: [[u32; 5]; 2] = [[16667, 16667, 33333, 50000, 66667], [8333, 8333, 16667, 25000, 33333]];
|
||||
let fps = (1_000_000.0_f32 / title_frame_us(rate) as f32) as u32 & 0xffff;
|
||||
let display = if display_120 { 120 } else { 60 };
|
||||
let index = if fps <= display { (display / fps.max(1)).min(4) } else { 1 };
|
||||
TABLE[usize::from(display_120)][index as usize]
|
||||
}
|
||||
|
||||
/// A read of the setting to change: its offset in the image, what it is,
|
||||
/// and the bytes there (compared first; the replacement goes at the start).
|
||||
pub struct Site {
|
||||
pub at: usize,
|
||||
pub kind: Kind,
|
||||
pub bytes: &'static [u8],
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Kind {
|
||||
/// `mov eax, [reg+210h]` before the frame time is made of it: becomes
|
||||
/// `mov eax, rate ; nop` (6 bytes).
|
||||
Load,
|
||||
/// +0x2241fd0, the rate the timers' shortest step is made of (`1 /
|
||||
/// rate`, +0x6849844): `cmp dword [rax+210h], 0` becomes `mov eax, rate
|
||||
/// ; jmp` to its `vcvtsi2ss` (7 bytes). Left as it was, the timers step at
|
||||
/// least 1/30 s however short the frame: at 60 frames a second the game
|
||||
/// ran at twice its speed (run fps-1, menus). For no limit, 1000.
|
||||
SimRate,
|
||||
}
|
||||
|
||||
/// The three reads (game_scarlett_master.exe 1.0.2.1, image offsets).
|
||||
pub const SITES: [Site; 3] = [
|
||||
// +0x1e0fb30: mov eax, [rdi+210h] ... mov [+0x98f84ac], eax
|
||||
Site {
|
||||
at: 0x1e0fc62,
|
||||
kind: Kind::Load,
|
||||
bytes: &[
|
||||
0x8b, 0x87, 0x10, 0x02, 0x00, 0x00, 0xba, 0xe8, 0x03, 0x00, 0x00, 0xc5, 0xfa, 0x10, 0x0d, 0x9f, 0xa1, 0x34, 0x04, 0xb9, 0xe8,
|
||||
0x03, 0x00, 0x00, 0x3d, 0xe8, 0x03, 0x00, 0x00, 0x0f, 0x42, 0xd0, 0x85, 0xc0, 0x0f, 0x44, 0xd1, 0xc5, 0xea, 0x2a, 0xc2, 0xc5,
|
||||
0xf2, 0x5e, 0xc0, 0xc4, 0xe1, 0xfa, 0x2c, 0xc0, 0x89, 0x05, 0x12, 0x88, 0xae, 0x07,
|
||||
],
|
||||
},
|
||||
// +0x22434a0: mov eax, [rsi+210h] ... mov [+0x98f84ac], eax
|
||||
Site {
|
||||
at: 0x224359a,
|
||||
kind: Kind::Load,
|
||||
bytes: &[
|
||||
0x8b, 0x86, 0x10, 0x02, 0x00, 0x00, 0xba, 0xe8, 0x03, 0x00, 0x00, 0xc5, 0xfa, 0x10, 0x0d, 0x67, 0x68, 0xf1, 0x03, 0xb9, 0xe8,
|
||||
0x03, 0x00, 0x00, 0x3d, 0xe8, 0x03, 0x00, 0x00, 0x0f, 0x42, 0xd0, 0x85, 0xc0, 0x0f, 0x44, 0xd1, 0xb1, 0xff, 0xc5, 0xea, 0x2a,
|
||||
0xc2, 0xc5, 0xf2, 0x5e, 0xc0, 0xc4, 0xe1, 0xfa, 0x2c, 0xc0, 0x89, 0x05, 0xd8, 0x4e, 0x6b, 0x07,
|
||||
],
|
||||
},
|
||||
// +0x2241fd0: cmp dword [rax+210h], 0 ; je ; call ; mov eax, [rax+210h] ; vcvtsi2ss xmm0, xmm0, rax
|
||||
Site {
|
||||
at: 0x2241fdb,
|
||||
kind: Kind::SimRate,
|
||||
bytes: &[
|
||||
0x83, 0xb8, 0x10, 0x02, 0x00, 0x00, 0x00, 0x74, 0x12, 0xe8, 0xc7, 0xa1, 0xe0, 0x02, 0x8b, 0x80, 0x10, 0x02, 0x00, 0x00, 0xc4,
|
||||
0xe1, 0xfa, 0x2a, 0xc0,
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
/// Title data worth seeing while it runs (`VICTORY_TIMER_LOG=1`): the frame
|
||||
/// time (+0x98f84ac) and the limiter's flag (+0x7453ec9).
|
||||
pub const FRAME_US: usize = 0x98f84ac;
|
||||
pub const LIMITER_ON: usize = 0x7453ec9;
|
||||
/// The title's timers (each set up at +0x22261a9 with a step of 0.05 s and
|
||||
/// 50 ms): +0 and +4 times, +0xc the step in seconds, +0x10 in milliseconds.
|
||||
pub const TIMERS: [usize; 7] = [0xa943788, 0x856c6b4, 0xabbef40, 0xa958358, 0xa958380, 0x74fa558, 0xa943768];
|
||||
|
||||
/// What goes at a site of `kind` for `rate`.
|
||||
pub fn replacement(kind: Kind, rate: u32) -> Vec<u8> {
|
||||
match kind {
|
||||
// mov eax, rate ; nop
|
||||
Kind::Load => {
|
||||
let r = rate.to_le_bytes();
|
||||
vec![0xb8, r[0], r[1], r[2], r[3], 0x90]
|
||||
}
|
||||
// mov eax, rate ; jmp +13 (to the vcvtsi2ss, 20 bytes on)
|
||||
Kind::SimRate => {
|
||||
let r = (if rate == 0 { 1000 } else { rate }).to_le_bytes();
|
||||
vec![0xb8, r[0], r[1], r[2], r[3], 0xeb, 0x0d]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What became of a site.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Patched {
|
||||
Done,
|
||||
/// Its bytes are not the ones expected: left alone.
|
||||
Differs,
|
||||
Failed(&'static str),
|
||||
}
|
||||
|
||||
/// Makes each site of `sites` in the image of `size` bytes at `base` load
|
||||
/// `rate`. What became of each, said in the log too.
|
||||
///
|
||||
/// # Safety
|
||||
/// The image stays mapped and no thread runs its code yet.
|
||||
#[cfg(windows)]
|
||||
pub unsafe fn apply(base: usize, size: usize, rate: u32, sites: &[Site]) -> Vec<Patched> {
|
||||
use windows_sys::Win32::System::Diagnostics::Debug::FlushInstructionCache;
|
||||
use windows_sys::Win32::System::Memory::{PAGE_EXECUTE_READWRITE, VirtualProtect};
|
||||
use windows_sys::Win32::System::Threading::GetCurrentProcess;
|
||||
sites
|
||||
.iter()
|
||||
.map(|site| {
|
||||
let code = replacement(site.kind, rate);
|
||||
let len = code.len();
|
||||
let done = (|| {
|
||||
if site.bytes.len() < len || site.at.checked_add(site.bytes.len()).is_none_or(|end| end > size) {
|
||||
return Patched::Failed("outside the image");
|
||||
}
|
||||
let at = base + site.at;
|
||||
// SAFETY: inside the mapped image (checked above).
|
||||
let there = unsafe { std::slice::from_raw_parts(at as *const u8, site.bytes.len()) };
|
||||
if there != site.bytes {
|
||||
return Patched::Differs;
|
||||
}
|
||||
let mut old = 0;
|
||||
// SAFETY: the load's bytes, made writable for this and put
|
||||
// back; nothing runs them yet.
|
||||
unsafe {
|
||||
if VirtualProtect(at as *const _, len, PAGE_EXECUTE_READWRITE, &mut old) == 0 {
|
||||
return Patched::Failed("Windows would not make it writable");
|
||||
}
|
||||
std::ptr::copy_nonoverlapping(code.as_ptr(), at as *mut u8, len);
|
||||
VirtualProtect(at as *const _, len, old, &mut old);
|
||||
FlushInstructionCache(GetCurrentProcess(), at as *const _, len);
|
||||
}
|
||||
Patched::Done
|
||||
})();
|
||||
let offset = format_args!("{:#x}", site.at);
|
||||
match done {
|
||||
Patched::Done => tracing::info!(site = %offset, kind = ?site.kind, rate, "frame rate: the title's read of its frame rate setting patched (VICTORY_FRAME_INTERVAL_MS)"),
|
||||
Patched::Differs => tracing::warn!(site = %offset, "frame rate: not patched, the title's bytes there are not the ones expected (another build?)"),
|
||||
Patched::Failed(why) => tracing::warn!(site = %offset, why, "frame rate: not patched"),
|
||||
}
|
||||
done
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn intervals_and_rates() {
|
||||
assert_eq!(parse_interval_ms("16.667"), Some(16.667));
|
||||
assert_eq!(parse_interval_ms(" 0 "), Some(0.0));
|
||||
assert_eq!(parse_interval_ms("-1"), None);
|
||||
assert_eq!(parse_interval_ms("fast"), None);
|
||||
assert_eq!(parse_interval_ms("1001"), None);
|
||||
assert_eq!(title_rate(16.667, None), Some(60));
|
||||
assert_eq!(title_rate(8.333, None), Some(120));
|
||||
assert_eq!(title_rate(33.333, None), Some(30));
|
||||
assert_eq!(title_rate(0.0, None), Some(0));
|
||||
assert_eq!(title_rate(0.5, None), Some(1000));
|
||||
assert_eq!(title_rate(16.667, Some("off")), None);
|
||||
assert_eq!(title_rate(0.0, Some("90")), Some(90));
|
||||
assert_eq!(title_rate(0.0, Some("lots")), None);
|
||||
}
|
||||
|
||||
/// The title's own arithmetic: its 30 is the 33,333 us the log shows,
|
||||
/// on a 60 Hz display and on a 120 Hz one.
|
||||
#[test]
|
||||
fn the_titles_intervals() {
|
||||
assert_eq!(title_frame_us(30), 33333);
|
||||
assert_eq!(title_frame_us(60), 16666);
|
||||
assert_eq!(title_frame_us(0), 1000);
|
||||
assert_eq!(title_frame_us(5000), 1000);
|
||||
assert_eq!(title_interval_us(30, false), 33333);
|
||||
assert_eq!(title_interval_us(30, true), 33333);
|
||||
assert_eq!(title_interval_us(40, true), 25000);
|
||||
assert_eq!(title_interval_us(60, false), 16667);
|
||||
assert_eq!(title_interval_us(60, true), 16667);
|
||||
assert_eq!(title_interval_us(120, true), 8333);
|
||||
assert_eq!(title_interval_us(120, false), 16667);
|
||||
assert_eq!(title_interval_us(0, false), 16667);
|
||||
assert_eq!(title_interval_us(0, true), 8333);
|
||||
assert_eq!(title_interval_us(20, false), 50000);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn the_sites_are_reads_of_the_setting() {
|
||||
for site in SITES.iter().filter(|s| s.kind == Kind::Load) {
|
||||
// mov eax, [rdi/rsi+210h]
|
||||
assert_eq!(site.bytes[0], 0x8b);
|
||||
assert!(matches!(site.bytes[1], 0x86 | 0x87));
|
||||
assert_eq!(&site.bytes[2..6], &[0x10, 0x02, 0x00, 0x00]);
|
||||
// mov edx, 1000 follows; the window ends with the store of eax.
|
||||
assert_eq!(&site.bytes[6..11], &[0xba, 0xe8, 0x03, 0x00, 0x00]);
|
||||
assert_eq!(&site.bytes[site.bytes.len() - 6..site.bytes.len() - 4], &[0x89, 0x05]);
|
||||
// RIP-relative: the store is of +0x98f84ac.
|
||||
let disp = i32::from_le_bytes(site.bytes[site.bytes.len() - 4..].try_into().unwrap());
|
||||
let end = site.at + site.bytes.len();
|
||||
assert_eq!(end as i64 + i64::from(disp), FRAME_US as i64);
|
||||
}
|
||||
let sim = SITES.iter().find(|s| s.kind == Kind::SimRate).unwrap();
|
||||
// cmp dword [rax+210h], 0 ; ... ; mov eax, [rax+210h] at 14 ; vcvtsi2ss at 20
|
||||
assert_eq!(&sim.bytes[..7], &[0x83, 0xb8, 0x10, 0x02, 0x00, 0x00, 0x00]);
|
||||
assert_eq!(&sim.bytes[14..20], &[0x8b, 0x80, 0x10, 0x02, 0x00, 0x00]);
|
||||
assert_eq!(&sim.bytes[20..], &[0xc4, 0xe1, 0xfa, 0x2a, 0xc0]);
|
||||
let code = replacement(Kind::SimRate, 60);
|
||||
assert_eq!(code.len() + usize::from(code[6]), 20, "the jump lands on the vcvtsi2ss");
|
||||
assert_eq!(replacement(Kind::Load, 60), [0xb8, 60, 0, 0, 0, 0x90]);
|
||||
assert_eq!(replacement(Kind::SimRate, 0)[1..5], 1000u32.to_le_bytes());
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn sites_take_the_rate_and_others_are_left_alone() {
|
||||
use windows_sys::Win32::System::Memory::{MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_EXECUTE_READWRITE, VirtualAlloc, VirtualFree};
|
||||
// SAFETY: fresh memory of our own, written and run inside it.
|
||||
unsafe {
|
||||
let place = VirtualAlloc(std::ptr::null(), 0x10000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE) as *mut u8;
|
||||
assert!(!place.is_null());
|
||||
let bytes = SITES[0].bytes;
|
||||
std::ptr::copy_nonoverlapping(bytes.as_ptr(), place.add(0x100), bytes.len());
|
||||
let mut other = SITES[1].bytes.to_vec();
|
||||
other[2] = 0x14; // [rsi+214h]
|
||||
std::ptr::copy_nonoverlapping(other.as_ptr(), place.add(0x200), other.len());
|
||||
// The simulation's rate as a function: rax = settings (+0x210 = 30),
|
||||
// the window, ret.
|
||||
let settings = [30u32; 0x100];
|
||||
let sim = SITES[2].bytes;
|
||||
let mut f = vec![0x48, 0xb8];
|
||||
f.extend((settings.as_ptr() as u64).to_le_bytes());
|
||||
let window_at = f.len();
|
||||
f.extend(sim);
|
||||
f.push(0xc3);
|
||||
std::ptr::copy_nonoverlapping(f.as_ptr(), place.add(0x300), f.len());
|
||||
let mine = [
|
||||
Site { at: 0x100, kind: Kind::Load, bytes },
|
||||
Site { at: 0x200, kind: Kind::Load, bytes: SITES[1].bytes },
|
||||
Site { at: 0x300 + window_at, kind: Kind::SimRate, bytes: sim },
|
||||
Site { at: 0xfff0, kind: Kind::Load, bytes },
|
||||
];
|
||||
let done = apply(place as usize, 0x10000, 60, &mine);
|
||||
assert_eq!(done[..3], [Patched::Done, Patched::Differs, Patched::Done]);
|
||||
assert!(matches!(done[3], Patched::Failed(_)));
|
||||
let after = std::slice::from_raw_parts(place.add(0x100), bytes.len());
|
||||
assert_eq!(after[..6], replacement(Kind::Load, 60)[..]);
|
||||
assert_eq!(after[6..], bytes[6..]);
|
||||
assert_eq!(std::slice::from_raw_parts(place.add(0x200), other.len()), &other[..]);
|
||||
let rate: extern "system" fn() -> f32 = std::mem::transmute(place.add(0x300));
|
||||
assert_eq!(rate(), 60.0);
|
||||
VirtualFree(place.cast(), 0, MEM_RELEASE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@
|
||||
#[cfg(windows)]
|
||||
mod console_insn;
|
||||
mod crash;
|
||||
pub mod frame_rate;
|
||||
pub mod prepare;
|
||||
pub(crate) mod publish;
|
||||
pub mod resolve;
|
||||
|
||||
Reference in new issue
Block a user