Run Grand Theft Auto VI from first boot into the world
The title now boots through every first-boot and menu screen at its own 30 fps, drawing with translated shaders, with input, sound, saves and loading served; it reaches the world, where the host GPU path still fails (see "Not working yet"). Loader and runtime - Crash reports unwind properly, name C++ throws and host GPU failures, carry the title's last debug lines and are appended to crashes.log. - Thread notices on our own threads, reentrant late DLL loading, exit in the console's order, relaunch on a launch-new-game request. - Offline guard: the title cannot reach the network (sockets, name lookups, WinHTTP, the media source resolver); by-name lookups and loads follow the import rules. - Every runtime area the title calls is served (networking hint, packages, store, saves, display, UI, speech stubs, user, async pool). Saves persist; an update is one rename and survives a kill at any step. - DirectStorage reads for real: shared worker pool, priorities, zlib and BCPack. BCPack is our own decoder; the format's constant tables ship in crates/victory-formats/src/bcpack-tables.bin (the asset policy's one exception, see docs/asset-policy.md). - GameInput from XInput pads and the keyboard, with a keyboard stand-in pad; console timers (1 ms) for the whole process. - Audio: XMA2 decoding on the emulated contexts, Opus on the decoder queues, the DSP interface as a stub; title memory placed below 2^40. - Console texture layouts (sizes, tiling, detiling) and the layout library's imports. GPU - Draws, mesh dispatches and compute dispatches run on the host with shaders translated at first use and cached on disk; descriptor heaps, views, root signatures, barriers and host texture states; rectangle lists; comparison samplers. - Title memory reaches the host through persistent mirrors that copy only pages written since the last copy; shader-written buffers are copied back. - Presentation with frame pacing per event kind; framerate in the window caption. Shaders - DXIL emission for vertex, pixel, compute and mesh programs from the console's native code, with a differential test suite against the PC compile. World set: mesh 8,191 of 8,506, vertex/pixel 10,617 of 12,346, compute 1,643 of 2,539; utility set complete. Build - Debugging aids (debug logs, frame capture, stack dumps, scripted keys, the host debug layer, the software adapter) exist only with the `diagnostics` feature; `[profile.diag]` keeps that build apart. Not working yet - In the world the host device is removed within seconds (descriptor handling fixes are in, unproven by a surviving run) and frames present black. - 64-bit image atomics, merged geometry stages and the BVH builder's own shaders do not translate; reverb is silent. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
1 parent
bc3070991a
commit
2849477415
107 files changed
+42526
-868
No files matched your search
+126
-39
@@ -1,25 +1,35 @@
|
||||
//! Frame pacing: the title sets a frame interval and waits for each frame
|
||||
//! to begin. Ours is a clock started by the first interval set, ticking
|
||||
//! once per interval; the token is the frame number.
|
||||
//! once per interval; the token is the frame number. Each event kind
|
||||
//! (origin, vblank, flip) happens once per tick, so two waits for the same
|
||||
//! kind never end on the same tick: Grand Theft Auto VI polls the origin
|
||||
//! with "token on time-out" (starting its next frame at once) and is paced
|
||||
//! by a blocking wait for the flip before each present.
|
||||
//!
|
||||
//! shortcut: event offsets and display events are not told apart (all
|
||||
//! waits end on the next tick); tie the clock to presentation once frames
|
||||
//! reach the screen.
|
||||
//! shortcut: event offsets are ignored (every event is at the tick); tie
|
||||
//! the clock to presentation if a title paces by them.
|
||||
#![allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crate::object::{HRESULT, S_OK, This};
|
||||
use crate::object::{HRESULT, S_FALSE, S_OK, This};
|
||||
|
||||
/// `HRESULT_FROM_WIN32(WAIT_TIMEOUT)`.
|
||||
const TIMEOUT: HRESULT = 0x8007_0102_u32 as i32;
|
||||
/// What a wait returns when its time is up first.
|
||||
const TIMEOUT: HRESULT = S_FALSE;
|
||||
/// The frame-origin event: the one that starts frames and makes tokens.
|
||||
const ORIGIN: u32 = 0;
|
||||
/// Make the frame's token even if the wait times out (the title starts the
|
||||
/// frame early).
|
||||
const TOKEN_ON_TIMEOUT: u32 = 2;
|
||||
const SIXTIETH: Duration = Duration::from_micros(16_667);
|
||||
|
||||
struct Clock {
|
||||
start: Instant,
|
||||
interval: Duration,
|
||||
/// The tick number at `start` (ticks go on across interval changes).
|
||||
first: u64,
|
||||
}
|
||||
|
||||
static CLOCK: Mutex<Option<Clock>> = Mutex::new(None);
|
||||
@@ -29,29 +39,32 @@ fn clock() -> std::sync::MutexGuard<'static, Option<Clock>> {
|
||||
CLOCK.lock().unwrap_or_else(|e| e.into_inner())
|
||||
}
|
||||
|
||||
/// Device method: `periods` intervals of `length` microseconds per frame.
|
||||
/// Device method: a frame every `length` microseconds. The period is how
|
||||
/// many intervals a frame may take to reach the display (pipeline depth),
|
||||
/// not part of the rate.
|
||||
pub(crate) extern "system" fn set_interval(
|
||||
_: This,
|
||||
_output: This,
|
||||
length: u32,
|
||||
periods: u32,
|
||||
_period: u32,
|
||||
_flags: u32,
|
||||
) -> HRESULT {
|
||||
let interval = match u64::from(length) * u64::from(periods.max(1)) {
|
||||
let interval = match length {
|
||||
0 => SIXTIETH,
|
||||
us => Duration::from_micros(us),
|
||||
us => Duration::from_micros(u64::from(us)),
|
||||
};
|
||||
tracing::info!(?interval, "frame interval");
|
||||
let mut clock = clock();
|
||||
match clock.as_mut() {
|
||||
Some(c) => c.interval = interval,
|
||||
None => {
|
||||
*clock = Some(Clock {
|
||||
start: Instant::now(),
|
||||
interval,
|
||||
})
|
||||
}
|
||||
}
|
||||
// A new interval starts from now, after the last tick handed out.
|
||||
let first = LAST
|
||||
.iter()
|
||||
.map(|t| t.load(Ordering::Relaxed))
|
||||
.max()
|
||||
.unwrap_or(0);
|
||||
*clock() = Some(Clock {
|
||||
start: Instant::now(),
|
||||
interval,
|
||||
first,
|
||||
});
|
||||
S_OK
|
||||
}
|
||||
|
||||
@@ -66,42 +79,84 @@ pub(crate) extern "system" fn schedule(
|
||||
S_OK
|
||||
}
|
||||
|
||||
/// How long until the next tick.
|
||||
fn until_next_tick() -> Duration {
|
||||
/// Frames begun so far (frame waits that ended).
|
||||
pub(crate) fn current() -> u64 {
|
||||
FRAME.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Per event kind (origin, vblank, flip and later kinds): the last tick a
|
||||
/// wait for it ended on.
|
||||
static LAST: [AtomicU64; 3] = [const { AtomicU64::new(0) }; 3];
|
||||
|
||||
fn last(kind: u32) -> &'static AtomicU64 {
|
||||
&LAST[(kind as usize).min(LAST.len() - 1)]
|
||||
}
|
||||
|
||||
/// The next tick after now and after the last one a wait for `kind` ended
|
||||
/// on (an event happens once per tick: a wait woken a little early must not
|
||||
/// let the next one end on the same tick), and how long until it.
|
||||
fn next_tick(kind: u32) -> (u64, Duration) {
|
||||
let mut clock = clock();
|
||||
let c = clock.get_or_insert_with(|| Clock {
|
||||
start: Instant::now(),
|
||||
interval: SIXTIETH,
|
||||
first: 0,
|
||||
});
|
||||
let elapsed = c.start.elapsed().as_nanos();
|
||||
let interval = c.interval.as_nanos().max(1);
|
||||
let next = (elapsed / interval + 1) * interval;
|
||||
Duration::from_nanos((next - elapsed) as u64)
|
||||
let tick =
|
||||
(c.first + (elapsed / interval + 1) as u64).max(last(kind).load(Ordering::Relaxed) + 1);
|
||||
let at = u128::from(tick - c.first) * interval;
|
||||
(
|
||||
tick,
|
||||
Duration::from_nanos(at.saturating_sub(elapsed) as u64),
|
||||
)
|
||||
}
|
||||
|
||||
/// Device method: blocks until the next tick, then hands out the frame's
|
||||
/// token.
|
||||
/// Device method: blocks until the next tick (or the time-out); an origin
|
||||
/// wait then hands out the new frame's token. Other events keep the token
|
||||
/// the title passes. Ticks are absolute (start plus whole intervals), so
|
||||
/// lateness never accumulates.
|
||||
pub(crate) extern "system" fn wait(
|
||||
_: This,
|
||||
_kind: u32,
|
||||
kind: u32,
|
||||
timeout_ms: u32,
|
||||
_list: *const u8,
|
||||
_flags: u32,
|
||||
flags: u32,
|
||||
token: *mut u64,
|
||||
) -> HRESULT {
|
||||
let wait = until_next_tick();
|
||||
let origin = kind == ORIGIN;
|
||||
let (tick, wait) = next_tick(kind);
|
||||
let limit = Duration::from_millis(u64::from(timeout_ms));
|
||||
if wait > limit {
|
||||
let hr = if wait > limit {
|
||||
std::thread::sleep(limit);
|
||||
return TIMEOUT;
|
||||
TIMEOUT
|
||||
} else {
|
||||
std::thread::sleep(wait);
|
||||
last(kind).fetch_max(tick, Ordering::Relaxed);
|
||||
S_OK
|
||||
};
|
||||
// A frame begun on a time-out takes no tick: its flip still does.
|
||||
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;
|
||||
if !token.is_null() {
|
||||
// SAFETY: checked non-null; the title passes its token.
|
||||
unsafe { token.write(frame) };
|
||||
}
|
||||
}
|
||||
std::thread::sleep(wait);
|
||||
let frame = FRAME.fetch_add(1, Ordering::Relaxed) + 1;
|
||||
if !token.is_null() {
|
||||
// SAFETY: checked non-null; the title passes its token.
|
||||
unsafe { token.write(frame) };
|
||||
}
|
||||
S_OK
|
||||
#[cfg(feature = "diagnostics")]
|
||||
tracing::debug!(
|
||||
kind,
|
||||
timeout_ms,
|
||||
flags,
|
||||
hr,
|
||||
tick,
|
||||
wait_us = wait.as_micros() as u64,
|
||||
frame = current(),
|
||||
"frame wait"
|
||||
);
|
||||
hr
|
||||
}
|
||||
|
||||
/// Device method: no statistics are kept, so none are returned.
|
||||
@@ -135,5 +190,37 @@ mod tests {
|
||||
assert!(second > first);
|
||||
assert!(before.elapsed() <= Duration::from_millis(50));
|
||||
assert_eq!(wait(null, 0, 0, std::ptr::null(), 0, &mut second), TIMEOUT);
|
||||
// Tokens count frames, a frame started early on a time-out too.
|
||||
let mut early = 0;
|
||||
let flags = TOKEN_ON_TIMEOUT;
|
||||
assert_eq!(
|
||||
wait(null, 0, 0, std::ptr::null(), flags, &mut early),
|
||||
TIMEOUT
|
||||
);
|
||||
let mut next = 0;
|
||||
assert_eq!(wait(null, 0, 1_000, std::ptr::null(), 0, &mut next), S_OK);
|
||||
assert!(early == second + 1 && next == early + 1);
|
||||
assert!(LAST[0].load(Ordering::Relaxed) >= 2);
|
||||
|
||||
// The title's loop (the clock is global, so in the same test): an
|
||||
// origin poll that times out (and starts the frame), then a
|
||||
// blocking flip wait. Each flip takes its own tick.
|
||||
const FLIP: u32 = 2;
|
||||
assert_eq!(set_interval(null, null, 5_000, 3, 0), S_OK);
|
||||
let mut token = 0;
|
||||
let flips: Vec<u64> = (0..4)
|
||||
.map(|_| {
|
||||
let _ = wait(null, 0, 0, std::ptr::null(), TOKEN_ON_TIMEOUT, &mut token);
|
||||
assert_eq!(
|
||||
wait(null, FLIP, u32::MAX, std::ptr::null(), 0, &mut token),
|
||||
S_OK
|
||||
);
|
||||
LAST[2].load(Ordering::Relaxed)
|
||||
})
|
||||
.collect();
|
||||
assert!(flips.windows(2).all(|w| w[1] == w[0] + 1), "{flips:?}");
|
||||
// Even when a flip wait begins on the tick another just ended on.
|
||||
let (tick, _) = next_tick(FLIP);
|
||||
assert!(tick > flips[3]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user