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

+679
View File
@@ -0,0 +1,679 @@
//! Whole-wave sections (decision 0003): code between `s_or_saveexec -1`
//! (or `s_nand_saveexec 0`) and the restore of the saved mask runs in every lane of the console's
//! wave, the inactive ones included, which is how the compiler expands
//! HLSL wave reductions and prefix sums (an identity put into the inactive
//! lanes with `v_cndmask`, then DPP, `v_permlanex16` and `v_readlane`
//! steps). A host lane that is elsewhere cannot run that code, so each
//! section is simulated on the console's lanes with symbolic values, and a
//! value it leaves is lowered by what it turned out to be: the reduction or
//! prefix of one operation over the lanes' (masked) inputs, which DXIL's
//! wave operations compute over the host's active lanes. No instruction
//! sequence is matched; any step the simulation does not model leaves the
//! values it writes unknown.
use std::collections::HashMap;
use crate::gfx10::{Dpp, Fields, NULL, Operand};
use crate::ir::{EXEC, Inst, Program, Reg, Value};
/// An associative operation and its identity.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Op {
AddU,
AddF,
MinU,
MaxU,
MinI,
MaxI,
MinF,
MaxF,
Or,
And,
Xor,
}
impl Op {
fn of(name: &str) -> Option<Op> {
Some(match name {
"v_add_nc_u32" => Op::AddU,
"v_add_f32" => Op::AddF,
"v_min_u32" => Op::MinU,
"v_max_u32" => Op::MaxU,
"v_min_i32" => Op::MinI,
"v_max_i32" => Op::MaxI,
"v_min_f32" => Op::MinF,
"v_max_f32" => Op::MaxF,
"v_or_b32" => Op::Or,
"v_and_b32" => Op::And,
"v_xor_b32" => Op::Xor,
_ => return None,
})
}
/// Whether `k` leaves the other operand unchanged.
fn identity(self, k: u32) -> bool {
match self {
Op::AddU | Op::Or | Op::Xor | Op::MaxU => k == 0,
Op::AddF => k == 0 || k == 0x8000_0000,
Op::MinU | Op::And => k == u32::MAX,
Op::MinI => k == 0x7FFF_FFFF,
Op::MaxI => k == 0x8000_0000,
Op::MinF => k == 0x7F80_0000,
Op::MaxF => k == 0xFF80_0000,
}
}
/// Counting a lane twice changes the result.
fn counts(self) -> bool {
matches!(self, Op::AddU | Op::AddF | Op::Xor)
}
}
/// A lane's value inside a section.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Sym {
Unknown,
K(u32),
/// The lane's own value of a VGPR value from before the section.
Own(Value),
/// A uniform value from before the section.
Uni(Value),
/// `op` over `lanes` of `mask ? src : ident` (`op` none: one lane).
Red {
op: Option<Op>,
src: Value,
mask: Value,
ident: u32,
lanes: u64,
},
}
/// What a value a section leaves holds in every lane.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Shape {
K(u32),
Value(Value),
/// The lane's own `mask ? src : ident`.
Masked(Value, Value, u32),
/// `op` over every lane.
Active(Op, Value, Value, u32),
/// `op` over the lanes below (`inclusive`: and including) each lane.
Prefix(Op, Value, Value, u32, bool),
}
/// The section starting at instruction `start` of a block: its restore
/// index and the shapes of the values it writes.
pub(crate) struct Section {
pub end: usize,
pub shapes: HashMap<Value, Shape>,
}
/// Every whole-wave section of `block`, by start index.
pub(crate) fn sections(p: &Program, block: usize) -> HashMap<usize, Section> {
let insts = &p.blocks[block].insts;
let wave: u32 = if p.wave64 { 64 } else { 32 };
let mut out = HashMap::new();
let mut i = 0;
while i < insts.len() {
let inst = &insts[i];
// EXEC becomes every lane: `s_or_saveexec -1` or `s_nand_saveexec 0`.
let all = match inst.name() {
"s_or_saveexec_b64" | "s_or_saveexec_b32" => Some(Operand::Const(193)),
"s_nand_saveexec_b64" | "s_nand_saveexec_b32" => Some(Operand::Const(128)),
_ => None,
};
let start = all.is_some_and(
|k| matches!(&inst.fields, Fields::Sop { src, .. } if src.first() == Some(&k)),
);
let saved = match &inst.fields {
Fields::Sop {
dst: Some(Operand::Scalar { code, .. }),
..
} if start => inst.write(Reg::S(*code)),
_ => None,
};
let Some(saved) = saved else {
i += 1;
continue;
};
let end = (i + 1..insts.len()).find(|&j| {
insts[j].write(Reg::S(EXEC)).is_some()
&& insts[j].read(Reg::S(saved_code(inst))) == Some(saved)
});
let Some(end) = end else {
break;
};
let shapes = simulate(&insts[i + 1..end], wave, saved);
out.insert(i, Section { end, shapes });
i = end + 1;
}
out
}
fn saved_code(inst: &Inst) -> u8 {
match &inst.fields {
Fields::Sop {
dst: Some(Operand::Scalar { code, .. }),
..
} => *code,
_ => NULL,
}
}
/// The lane `l` reads with DPP, whether it reads 0 instead, and whether it
/// writes; `None` for patterns not modelled.
fn dpp_lane(d: Dpp, l: u32) -> Option<(u32, bool, bool)> {
match d {
Dpp::Lanes { sel, .. } => Some(((l & !7) | (sel >> (3 * (l & 7)) & 7), false, true)),
Dpp::Row {
ctrl,
row_mask,
bank_mask,
bound_ctrl,
..
} => {
let (row, pos, n) = (l & !15, l & 15, u32::from(ctrl & 15));
let enabled = row_mask >> (l >> 4 & 3) & 1 != 0 && bank_mask >> (l >> 2 & 3) & 1 != 0;
let (src, inside) = match ctrl {
0..=0xFF => ((l & !3) | (u32::from(ctrl) >> (2 * (l & 3)) & 3), true),
0x101..=0x10F => (l + n, pos + n < 16),
0x111..=0x11F => (l.wrapping_sub(n), pos >= n),
0x121..=0x12F => (row | (pos.wrapping_sub(n) & 15), true),
0x140 => (l ^ 15, true),
0x141 => (l ^ 7, true),
0x150..=0x15F => (row | n, true),
0x160..=0x16F => (l ^ n, true),
_ => return None,
};
match (inside, bound_ctrl) {
(true, _) => Some((src, false, enabled)),
(false, true) => Some((l, true, enabled)),
(false, false) => Some((l, false, false)),
}
}
}
}
fn combine(op: Op, a: Sym, b: Sym) -> Sym {
match (a, b) {
(Sym::K(k), x) | (x, Sym::K(k)) if op.identity(k) => x,
(
Sym::Red {
op: oa,
src,
mask,
ident,
lanes: la,
},
Sym::Red {
op: ob,
src: sb,
mask: mb,
ident: ib,
lanes: lb,
},
) if (src, mask, ident) == (sb, mb, ib)
&& op.identity(ident)
&& oa.is_none_or(|o| o == op)
&& ob.is_none_or(|o| o == op)
&& (!op.counts() || la & lb == 0) =>
{
Sym::Red {
op: Some(op),
src,
mask,
ident,
lanes: la | lb,
}
}
_ => Sym::Unknown,
}
}
fn simulate(insts: &[Inst], wave: u32, saved: Value) -> HashMap<Value, Shape> {
let all = if wave == 64 {
u64::MAX
} else {
u64::from(u32::MAX)
};
let mut exec = all;
let mut vec: HashMap<Value, Vec<Sym>> = HashMap::new();
let mut uni: HashMap<Value, Sym> = HashMap::new();
// Masks the section derives from the saved EXEC (`s_and` with it).
let mut masked = vec![saved];
for inst in insts {
let name = inst.name();
if inst.write(Reg::S(EXEC)).is_some() {
// Constant lane sets (prefix sums); anything else ends what
// the simulation can follow.
let lo = name
.starts_with("s_mov_b")
.then(|| constant(inst))
.flatten();
let Some(k) = lo else {
return shapes(vec, uni, wave);
};
exec = if name == "s_mov_b64" {
k
} else {
exec & !0xFFFF_FFFF | k & 0xFFFF_FFFF
};
continue;
}
if inst.write(Reg::S(EXEC + 1)).is_some() {
let Some(k) = name
.starts_with("s_mov_b")
.then(|| constant(inst))
.flatten()
else {
return shapes(vec, uni, wave);
};
exec = exec & 0xFFFF_FFFF | k << 32;
continue;
}
if let Fields::Sop { src, .. } = &inst.fields {
if name.starts_with("s_and_b")
&& src.iter().any(|o| matches!(*o, Operand::Scalar { code, .. } if inst.read(Reg::S(code)).is_some_and(|v| masked.contains(&v))))
&& let Some(&(_, w)) = inst.writes.iter().find(|w| matches!(w.0, Reg::S(_)))
{
masked.push(w);
}
for &(_, w) in &inst.writes {
uni.entry(w).or_insert(Sym::Unknown);
}
continue;
}
let Fields::Valu(v) = &inst.fields else {
for &(_, w) in &inst.writes {
vec.insert(w, vec![Sym::Unknown; wave as usize]);
}
continue;
};
let lanes = |op: &Operand,
vec: &HashMap<Value, Vec<Sym>>,
uni: &HashMap<Value, Sym>|
-> Vec<Sym> {
let one = |s: Sym| vec![s; wave as usize];
match *op {
Operand::Vector { reg, .. } => match inst.read(Reg::V(reg)) {
Some(x) => vec
.get(&x)
.cloned()
.unwrap_or_else(|| (0..wave).map(|_| Sym::Own(x)).collect()),
None => one(Sym::Unknown),
},
Operand::Scalar { code, .. } if code != NULL => match inst.read(Reg::S(code)) {
Some(x) => one(uni.get(&x).copied().unwrap_or(Sym::Uni(x))),
None => one(Sym::Unknown),
},
Operand::Const(c) => one(crate::resource::inline(c).map_or(Sym::Unknown, Sym::K)),
Operand::Literal(x) => one(Sym::K(x)),
_ => one(Sym::Unknown),
}
};
let modifiers = v.abs != 0 || v.neg != 0 || v.clamp || v.omod != 0 || v.sdwa.is_some();
if name == "v_readlane_b32" {
let (Some(Operand::Scalar { code, .. }), Some(src), Some(lane)) =
(v.dst, v.src.first(), v.src.get(1))
else {
continue;
};
let l = match *lane {
Operand::Const(c) => crate::resource::inline(c),
Operand::Literal(x) => Some(x),
_ => None,
};
let x = lanes(src, &vec, &uni);
if let Some(w) = inst.write(Reg::S(code)) {
let s = l
.and_then(|l| x.get(l as usize).copied())
.unwrap_or(Sym::Unknown);
uni.insert(w, s);
}
continue;
}
let Some(Operand::Vector { reg: dreg, .. }) = v.dst else {
for &(_, w) in &inst.writes {
uni.insert(w, Sym::Unknown);
}
continue;
};
let Some(dst) = inst.write(Reg::V(dreg)) else {
continue;
};
let old = lanes(
&Operand::Vector {
reg: dreg,
dwords: 1,
},
&vec,
&uni,
);
// src0 as each lane fetches it (DPP), with the lanes that write.
let src0 = v.src.first().map(|o| lanes(o, &vec, &uni));
let mut writes = exec;
let fetched: Option<Vec<Sym>> = match (v.dpp, &src0) {
(Some(d), Some(s)) => {
if exec != all {
vec.insert(dst, vec![Sym::Unknown; wave as usize]);
continue;
}
let mut f = Vec::with_capacity(wave as usize);
let mut ok = true;
for l in 0..wave {
match dpp_lane(d, l) {
Some((src, zero, on)) if src < wave => {
f.push(if zero { Sym::K(0) } else { s[src as usize] });
if !on {
writes &= !(1u64 << l);
}
}
_ => ok = false,
}
}
ok.then_some(f)
}
(None, Some(s)) => Some(s.clone()),
_ => None,
};
let src = |k: usize| v.src.get(k).map(|o| lanes(o, &vec, &uni));
let result: Vec<Sym> = match (name, fetched) {
_ if modifiers => vec![Sym::Unknown; wave as usize],
("v_mov_b32", Some(a)) => a,
("v_bfrev_b32", Some(a)) => a
.into_iter()
.map(|s| match s {
Sym::K(k) => Sym::K(k.reverse_bits()),
_ => Sym::Unknown,
})
.collect(),
("v_cndmask_b32", Some(a)) => {
let mask = match v.src.get(2) {
Some(&Operand::Scalar { code, .. }) => inst.read(Reg::S(code)),
_ => None,
};
let b = src(1).unwrap_or_default();
(0..wave as usize)
.map(|l| match (a[l], b.get(l).copied(), mask) {
(Sym::K(ident), Some(Sym::Own(x)), Some(m)) if masked.contains(&m) => {
Sym::Red {
op: None,
src: x,
mask: m,
ident,
lanes: 1 << l,
}
}
_ => Sym::Unknown,
})
.collect()
}
("v_permlanex16_b32", Some(a)) => {
// Lanes of each row read the other row of their half; lane
// i's pick is nibble i of {src2, src1}.
let sel = |k: usize| match v.src.get(k) {
Some(&Operand::Const(c)) => crate::resource::inline(c),
Some(&Operand::Literal(x)) => Some(x),
_ => None,
};
match (sel(1), sel(2)) {
(Some(s1), Some(s2)) => {
let s = u64::from(s1) | u64::from(s2) << 32;
(0..wave)
.map(|l| {
let other = (l & !31) | ((l & 16) ^ 16);
let pick = (s >> (4 * (l & 15)) & 15) as u32;
a[(other | pick) as usize]
})
.collect()
}
_ => vec![Sym::Unknown; wave as usize],
}
}
("v_sub_nc_u32", Some(a)) => {
// A prefix sum less the lane's own term: the exclusive one.
let b = src(1).unwrap_or_default();
(0..wave as usize)
.map(|l| match (a[l], b.get(l).copied()) {
(
Sym::Red {
op: None | Some(Op::AddU),
src,
mask,
ident: 0,
lanes,
},
Some(Sym::Red {
op: None | Some(Op::AddU),
src: s2,
mask: m2,
ident: 0,
lanes: l2,
}),
) if (src, mask) == (s2, m2) && lanes & l2 == l2 => {
let rest = lanes & !l2;
if rest == 0 {
Sym::K(0)
} else {
Sym::Red {
op: Some(Op::AddU),
src,
mask,
ident: 0,
lanes: rest,
}
}
}
_ => Sym::Unknown,
})
.collect()
}
(n, Some(a)) => match (Op::of(n), src(1)) {
(Some(op), Some(b)) => (0..wave as usize)
.map(|l| combine(op, a[l], b[l]))
.collect(),
_ => vec![Sym::Unknown; wave as usize],
},
_ => vec![Sym::Unknown; wave as usize],
};
let merged = (0..wave as usize)
.map(|l| {
if writes >> l & 1 != 0 {
result[l]
} else {
old[l]
}
})
.collect();
vec.insert(dst, merged);
}
shapes(vec, uni, wave)
}
fn constant(inst: &Inst) -> Option<u64> {
let Fields::Sop { src, .. } = &inst.fields else {
return None;
};
match *src.first()? {
Operand::Const(c) => {
let x = crate::resource::inline(c)?;
Some(if (x as i32) < 0 {
u64::MAX
} else {
u64::from(x)
})
}
Operand::Literal(x) => Some(u64::from(x)),
_ => None,
}
}
/// The shape of each value, where every lane agrees on one.
fn shapes(
vec: HashMap<Value, Vec<Sym>>,
uni: HashMap<Value, Sym>,
wave: u32,
) -> HashMap<Value, Shape> {
let all = if wave == 64 {
u64::MAX
} else {
u64::from(u32::MAX)
};
let mut out = HashMap::new();
let single = |s: Sym| match s {
Sym::K(k) => Some(Shape::K(k)),
Sym::Own(x) | Sym::Uni(x) => Some(Shape::Value(x)),
Sym::Red {
op: Some(op),
src,
mask,
ident,
lanes,
} if lanes == all => Some(Shape::Active(op, src, mask, ident)),
_ => None,
};
for (v, s) in uni {
if let Some(sh) = single(s) {
out.insert(v, sh);
}
}
for (v, lanes) in vec {
if let Some(sh) = per_lane(&lanes, all) {
out.insert(v, sh);
}
}
out
}
fn per_lane(lanes: &[Sym], all: u64) -> Option<Shape> {
let first = lanes[0];
let own = |l: usize, s: Sym| match s {
Sym::Red {
op: None,
src,
mask,
ident,
lanes,
} if lanes == 1 << l => Some((src, mask, ident)),
_ => None,
};
if let Some(m) = own(0, first)
&& lanes.iter().enumerate().all(|(l, &s)| own(l, s) == Some(m))
{
return Some(Shape::Masked(m.0, m.1, m.2));
}
if lanes.iter().all(|&s| s == first) {
return match first {
Sym::K(k) => Some(Shape::K(k)),
Sym::Uni(x) | Sym::Own(x) => Some(Shape::Value(x)),
Sym::Red {
op: Some(op),
src,
mask,
ident,
lanes,
} if lanes == all => Some(Shape::Active(op, src, mask, ident)),
_ => None,
};
}
// Prefixes: lane l holds lanes 0..l (exclusive) or 0..=l (inclusive).
let mut found: Option<(Op, Value, Value, u32, bool)> = None;
for (l, &s) in lanes.iter().enumerate() {
let below = (1u64 << l) - 1;
let (op, src, mask, ident, set) = match s {
Sym::Red {
op,
src,
mask,
ident,
lanes,
} => (op, src, mask, ident, lanes),
// Lane 0 of an exclusive prefix holds the identity.
Sym::K(_) if l == 0 => continue,
_ => return None,
};
let inclusive = set == below | 1 << l;
if !inclusive && set != below {
return None;
}
let op = match (op, found) {
(Some(o), _) => o,
(None, Some(f)) => f.0,
(None, None) => continue,
};
let me = (op, src, mask, ident, inclusive);
match found {
None => found = Some(me),
Some(f) if f == me => {}
_ => return None,
}
}
let (op, src, mask, ident, inclusive) = found?;
if let Sym::K(k) = lanes[0]
&& (inclusive || k != ident)
{
return None;
}
Some(Shape::Prefix(op, src, mask, ident, inclusive))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dpp_lanes_follow_the_isa() {
// row_shr:1 with bound_ctrl: lane 16 reads 0, lane 17 lane 16.
let shr = Dpp::Row {
ctrl: 0x111,
row_mask: 0xF,
bank_mask: 0xF,
bound_ctrl: true,
fi: false,
};
assert_eq!(dpp_lane(shr, 16), Some((16, true, true)));
assert_eq!(dpp_lane(shr, 17), Some((16, false, true)));
// row_mirror: lane 3 reads lane 12.
let mirror = Dpp::Row {
ctrl: 0x140,
row_mask: 0xF,
bank_mask: 0xF,
bound_ctrl: true,
fi: false,
};
assert_eq!(dpp_lane(mirror, 3), Some((12, false, true)));
// dpp8 [1,0,3,2,5,4,7,6] swaps neighbours.
let swap = Dpp::Lanes {
sel: 1 | 3 << 6 | 2 << 9 | 5 << 12 | 4 << 15 | 7 << 18 | 6 << 21,
fi: false,
};
assert_eq!(dpp_lane(swap, 8), Some((9, false, true)));
}
#[test]
fn prefixes_are_recognised() {
let (x, m) = (Value(1), Value(2));
let red = |lanes: u64| Sym::Red {
op: Some(Op::AddU),
src: x,
mask: m,
ident: 0,
lanes,
};
let inclusive: Vec<Sym> = (0..64).map(|l| red(u64::MAX >> (63 - l))).collect();
assert_eq!(
per_lane(&inclusive, u64::MAX),
Some(Shape::Prefix(Op::AddU, x, m, 0, true))
);
let mut exclusive: Vec<Sym> = (0..64).map(|l| red((1u64 << l) - 1)).collect();
exclusive[0] = Sym::K(0);
assert_eq!(
per_lane(&exclusive, u64::MAX),
Some(Shape::Prefix(Op::AddU, x, m, 0, false))
);
assert_eq!(
per_lane(&vec![red(u64::MAX); 64], u64::MAX),
Some(Shape::Active(Op::AddU, x, m, 0))
);
}
}