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:
MarvisandClaude Fable 5.2 committed 2026-11-23 23:10:01 +01:00
1 parent bc3070991a
commit 2849477415
107 files changed
+42526 -868

No files matched your search

+159 -31
View File
@@ -1,12 +1,12 @@
//! Root signatures.
//!
//! Serializing goes to the host's `D3D12SerializeRootSignature`: the
//! description is laid out as on PC, and the blob it returns works as the
//! console's, whose two methods sit in the same slots. The console's own
//! flags (bit 24 and up) are dropped first.
//! Serializing goes to the host's `D3D12SerializeRootSignature` (or its
//! versioned form): the description is laid out as on PC, and the blob it
//! returns works as the console's, whose two methods sit in the same slots.
//! The console's own flags (bit 24 and up) are dropped first.
#![allow(clippy::not_unsafe_ptr_arg_deref)]
use std::ffi::c_void;
use std::ffi::{CStr, c_void};
use std::sync::OnceLock;
use crate::object::{
@@ -37,23 +37,32 @@ pub struct Desc {
flags: u32,
}
/// Every version's description starts with the fields of [`Desc`].
#[repr(C)]
#[derive(Clone, Copy)]
pub struct VersionedDesc {
version: u32,
desc: Desc,
}
type Serialize =
unsafe extern "system" fn(*const Desc, u32, *mut *mut c_void, *mut *mut c_void) -> HRESULT;
type SerializeVersioned =
unsafe extern "system" fn(*const VersionedDesc, *mut *mut c_void, *mut *mut c_void) -> HRESULT;
/// The host's serializer, from `d3d12.dll` in System32.
fn host_serializer() -> Option<Serialize> {
static FUNCTION: OnceLock<Option<usize>> = OnceLock::new();
let address = *FUNCTION.get_or_init(|| {
/// An export of the host's `d3d12.dll` in System32, looked up once.
fn host_export(cache: &OnceLock<Option<usize>>, name: &CStr) -> Option<usize> {
*cache.get_or_init(|| {
#[cfg(windows)]
{
use windows_sys::Win32::System::LibraryLoader::{
GetProcAddress, LOAD_LIBRARY_SEARCH_SYSTEM32, LoadLibraryExW,
};
let name: Vec<u16> = "d3d12.dll".encode_utf16().chain([0]).collect();
let dll: Vec<u16> = "d3d12.dll".encode_utf16().chain([0]).collect();
// SAFETY: a NUL-terminated name; System32 only.
let module = unsafe {
LoadLibraryExW(
name.as_ptr(),
dll.as_ptr(),
std::ptr::null_mut(),
LOAD_LIBRARY_SEARCH_SYSTEM32,
)
@@ -62,14 +71,24 @@ fn host_serializer() -> Option<Serialize> {
return None;
}
// SAFETY: a loaded module and a NUL-terminated name.
unsafe { GetProcAddress(module, c"D3D12SerializeRootSignature".as_ptr().cast()) }
.map(|f| f as usize)
unsafe { GetProcAddress(module, name.as_ptr().cast()) }.map(|f| f as usize)
}
#[cfg(not(windows))]
None
});
// SAFETY: the export has this signature.
address.map(|a| unsafe { std::mem::transmute::<usize, Serialize>(a) })
})
}
fn without_console_flags(desc: Desc) -> Desc {
if desc.flags & CONSOLE_FLAGS != 0 {
tracing::debug!(
flags = format_args!("{:#x}", desc.flags),
"console root signature flags dropped"
);
}
Desc {
flags: desc.flags & !CONSOLE_FLAGS,
..desc
}
}
/// `d3d12_xs!D3D12SerializeRootSignature`.
@@ -83,22 +102,68 @@ pub extern "system" fn serialize(
let Some(&desc) = (unsafe { desc.as_ref() }) else {
return E_INVALIDARG;
};
if desc.flags & CONSOLE_FLAGS != 0 {
tracing::debug!(
flags = format_args!("{:#x}", desc.flags),
"console root signature flags dropped"
);
}
let desc = Desc {
flags: desc.flags & !CONSOLE_FLAGS,
..desc
};
let Some(serialize) = host_serializer() else {
static FUNCTION: OnceLock<Option<usize>> = OnceLock::new();
let Some(address) = host_export(&FUNCTION, c"D3D12SerializeRootSignature") else {
tracing::warn!("no host d3d12.dll to serialize root signatures");
return E_NOTIMPL;
};
// SAFETY: the host function takes the same arguments the title passed.
unsafe { serialize(&desc, version, blob, error) }
// SAFETY: the export has this signature; it takes what the title passed.
unsafe {
std::mem::transmute::<usize, Serialize>(address)(
&without_console_flags(desc),
version,
blob,
error,
)
}
}
/// `d3d12_xs!D3D12SerializeVersionedRootSignature`.
pub extern "system" fn serialize_versioned(
desc: *const VersionedDesc,
blob: *mut *mut c_void,
error: *mut *mut c_void,
) -> HRESULT {
// SAFETY: the title passes a description (null is tolerated).
let Some(&desc) = (unsafe { desc.as_ref() }) else {
return E_INVALIDARG;
};
static FUNCTION: OnceLock<Option<usize>> = OnceLock::new();
let Some(address) = host_export(&FUNCTION, c"D3D12SerializeVersionedRootSignature") else {
tracing::warn!("no host d3d12.dll to serialize root signatures");
return E_NOTIMPL;
};
let desc = VersionedDesc {
desc: without_console_flags(desc.desc),
..desc
};
// SAFETY: the export has this signature; it takes what the title passed.
unsafe { std::mem::transmute::<usize, SerializeVersioned>(address)(&desc, blob, error) }
}
type Deserialize =
unsafe extern "system" fn(*const u8, usize, *const Iid, *mut *mut c_void) -> HRESULT;
/// `d3d12_xs!D3D12CreateRootSignatureDeserializer`: our blobs are the
/// host's serialization, so the host's deserializer reads them, and its
/// object (IUnknown, then the description getter) is laid out as the
/// console's. Grand Theft Auto VI's upscaler asks with the PC interface id.
pub extern "system" fn create_deserializer(
blob: *const u8,
size: usize,
iid: *const Iid,
out: *mut *mut c_void,
) -> HRESULT {
if blob.is_null() || out.is_null() {
return E_INVALIDARG;
}
static FUNCTION: OnceLock<Option<usize>> = OnceLock::new();
let Some(address) = host_export(&FUNCTION, c"D3D12CreateRootSignatureDeserializer") else {
tracing::warn!("no host d3d12.dll to deserialize root signatures");
return E_NOTIMPL;
};
// SAFETY: the export has this signature; it takes what the title passed.
unsafe { std::mem::transmute::<usize, Deserialize>(address)(blob, size, iid, out) }
}
#[repr(C)]
@@ -139,8 +204,8 @@ extern "system" fn release(this: This) -> u32 {
}
/// IUnknown 3, object 4, device child 1.
static FUNCTIONS: Table<8> = {
let mut f = object::d3d12_object(object::missing_table::<ROOT_SIGNATURE, 8>());
static FUNCTIONS: Table<24> = {
let mut f = object::d3d12_object(object::missing_table::<ROOT_SIGNATURE, 24>());
f[0] = query as *const ();
f[2] = release as *const ();
f[7] = object::parent_device as *const (); // 0x38
@@ -193,4 +258,67 @@ mod tests {
assert_eq!(slot::<extern "system" fn(This) -> u32>(root, 0x10)(root), 0);
slot::<extern "system" fn(This) -> u32>(blob, 0x10)(blob);
}
/// The host's deserializer reads our blobs back (the upscaler's call).
#[cfg(windows)]
#[test]
fn our_blobs_deserialize_on_the_host() {
use windows::Win32::Graphics::Direct3D12::ID3D12RootSignatureDeserializer;
use windows::core::Interface;
let desc = VersionedDesc {
version: 1,
desc: Desc {
parameter_count: 0,
parameters: std::ptr::null(),
sampler_count: 0,
samplers: std::ptr::null(),
flags: 1,
},
};
let (mut blob, mut error) = (std::ptr::null_mut(), std::ptr::null_mut());
assert_eq!(serialize_versioned(&desc, &mut blob, &mut error), S_OK);
let blob = blob.cast_const().cast::<Object>();
let data = slot::<extern "system" fn(This) -> *const u8>(blob, 0x18)(blob);
let size = slot::<extern "system" fn(This) -> usize>(blob, 0x20)(blob);
let iid: Iid = ID3D12RootSignatureDeserializer::IID.to_u128().to_be_bytes();
let iid = guid(
u32::from_be_bytes(iid[0..4].try_into().unwrap()),
u16::from_be_bytes(iid[4..6].try_into().unwrap()),
u16::from_be_bytes(iid[6..8].try_into().unwrap()),
iid[8..16].try_into().unwrap(),
);
let mut out = std::ptr::null_mut();
assert_eq!(create_deserializer(data, size, &iid, &mut out), S_OK);
// SAFETY: the host handed out this interface.
let deserializer = unsafe { ID3D12RootSignatureDeserializer::from_raw(out) };
// SAFETY: valid while the deserializer lives.
let read = unsafe { &*deserializer.GetRootSignatureDesc() };
assert_eq!((read.NumParameters, read.Flags.0), (0, 1));
slot::<extern "system" fn(This) -> u32>(blob, 0x10)(blob);
}
/// Version 1.0 through the versioned entry point, with a console flag.
#[cfg(windows)]
#[test]
fn versioned_descriptions_serialize_on_the_host() {
let desc = VersionedDesc {
version: 1,
desc: Desc {
parameter_count: 0,
parameters: std::ptr::null(),
sampler_count: 0,
samplers: std::ptr::null(),
flags: 0x0200_0001,
},
};
let (mut blob, mut error) = (std::ptr::null_mut(), std::ptr::null_mut());
assert_eq!(serialize_versioned(&desc, &mut blob, &mut error), S_OK);
let blob = blob.cast_const().cast::<Object>();
assert!(slot::<extern "system" fn(This) -> usize>(blob, 0x20)(blob) > 0);
slot::<extern "system" fn(This) -> u32>(blob, 0x10)(blob);
assert_eq!(
serialize_versioned(std::ptr::null(), &mut blob.cast_mut().cast(), &mut error),
E_INVALIDARG
);
}
}