victory-gta6: this fork's changes on top of VIctory main (ffe310e)

World rendering, lighting, frame-rate and memory work for Grand Theft Auto VI
(Xbox Series X profile) on one Windows PC with an NVIDIA RTX 4070. The README
describes the state, the changes and the credits. Based on VIctory by Marvisak
and contributors (GPL-3.0-or-later); XCL by ivall and contributors was used as
a reference. No game files are included.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
fxcelessandClaude Fable 5.2 committed 2026-11-24 13:02:18 -05:00
1 parent ffe310eb20
commit 578d459c34
61 files changed
+32259 -1274

No files matched your search

+329 -7
View File
@@ -3,7 +3,8 @@
//!
//! Covers RDNA2 hardware semantics that differ from naive DXIL lowering:
//! `v_med3_f32`/`v_med3_f16` with NaN operands, `v_fract_f32`/`v_fract_f16`
//! near `1.0`, and turn-range reduction for `v_sin_*`/`v_cos_*`.
//! near `1.0`, turn-range reduction for `v_sin_*`/`v_cos_*`, and the LDS
//! float atomics `ds_min_f32`/`ds_max_f32`.
use super::host::{Dispatch, Host, Root, View};
use super::{HostShader, lower, validator};
@@ -43,9 +44,33 @@ const fn vop1(op: u16, vdst: u8, src0: u16) -> u32 {
/// VOP3 `op vdst, src0, src1, src2` (no modifiers).
const fn vop3(op: u16, vdst: u8, src0: u16, src1: u16, src2: u16) -> [u32; 2] {
vop3_mod(op, vdst, src0, src1, src2, 0, 0, 0, false)
}
/// VOP3 `op vdst, src0, src1, src2` with explicit `abs`, `neg`, `omod`, `clamp`.
#[allow(clippy::too_many_arguments)]
const fn vop3_mod(
op: u16,
vdst: u8,
src0: u16,
src1: u16,
src2: u16,
abs: u8,
neg: u8,
omod: u8,
clamp: bool,
) -> [u32; 2] {
[
0x35 << 26 | (op as u32 & 0x3FF) << 16 | vdst as u32,
(src0 as u32 & 0x1FF) | (src1 as u32 & 0x1FF) << 9 | (src2 as u32 & 0x1FF) << 18,
0x35 << 26
| (op as u32 & 0x3FF) << 16
| (clamp as u32) << 15
| (abs as u32 & 7) << 8
| vdst as u32,
(neg as u32 & 7) << 29
| (omod as u32 & 3) << 27
| (src0 as u32 & 0x1FF)
| (src1 as u32 & 0x1FF) << 9
| (src2 as u32 & 0x1FF) << 18,
]
}
@@ -69,6 +94,12 @@ const fn vgpr(n: u8) -> u16 {
/// 4 dwords of root constants in `s0..s3`, runs `body` (leaving its result in
/// `v0`), and stores `v0` to dword 0 of the raw UAV at heap index 0.
fn build_valu_cs(body: &[u32]) -> HostShader {
build_cs(body, 1, 0)
}
/// [`build_valu_cs`] with `threads` threads in x (thread index in `v0`
/// on entry, before `body`) and `lds_bytes` of LDS (a multiple of 512).
fn build_cs(body: &[u32], threads: u32, lds_bytes: u32) -> HostShader {
let mut words = Vec::new();
// s[4:5] = descriptor table address (heap index 0 -> byte offset 0).
words.push(s_mov_b32(5, 0x80)); // inline constant 0
@@ -116,8 +147,8 @@ fn build_valu_cs(body: &[u32]) -> HostShader {
slot: 1,
}],
io: StageIo::Cs(ComputeIo {
rsrc2: 5 << 1, // 5 user SGPRs: s0..s4
threads: [1, 1, 1],
rsrc2: (5 << 1) | ((lds_bytes / 512) << 15), // 5 user SGPRs: s0..s4
threads: [threads, 1, 1],
}),
code,
};
@@ -148,6 +179,8 @@ fn build_valu_cs(body: &[u32]) -> HostShader {
static_samplers: Vec::new(),
root_cbvs: lowered.root_cbvs,
root_srvs: lowered.root_srvs,
root_uavs: lowered.root_uavs,
root_indexed_views: res.root_indexed_views,
};
if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
@@ -172,11 +205,20 @@ pub(super) static ONE_AT_A_TIME: std::sync::Mutex<()> = std::sync::Mutex::new(()
fn run_cases(
shader: &HostShader,
cases: &[[u32; 4]],
check: impl FnMut(&str, usize, [u32; 4], u32),
) {
run_cases_on(shader, cases, &[("hardware", false), ("warp", true)], check);
}
fn run_cases_on(
shader: &HostShader,
cases: &[[u32; 4]],
devices: &[(&str, bool)],
mut check: impl FnMut(&str, usize, [u32; 4], u32),
) {
let _one = ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner());
let mut ran = 0;
for (label, warp) in [("hardware", false), ("warp", true)] {
for &(label, warp) in devices {
let Ok(host) = Host::new(warp) else {
eprintln!("no SM 6.6 {label} device; skipped");
continue;
@@ -209,7 +251,7 @@ fn run_cases(
}
ran += 1;
}
assert!(ran > 0, "neither hardware nor WARP SM 6.6 device available");
assert!(ran > 0, "no SM 6.6 device available ({devices:?})");
}
fn is_f16_nan(bits: u16) -> bool {
@@ -868,6 +910,286 @@ fn bfe_signed_and_unsigned_match_rdna2() {
});
}
#[test]
fn f16_omod_applies_before_clamp() {
// VOP3 v_add_f16 (0x132 = 0x100 + VOP2 0x32): v0 = (v1 + v2) * omod [clamp]
let mul2 = build_valu_cs(&vop3_mod(0x132, 0, vgpr(1), vgpr(2), 0, 0, 0, 1, false));
let mul4 = build_valu_cs(&vop3_mod(0x132, 0, vgpr(1), vgpr(2), 0, 0, 0, 2, false));
let div2 = build_valu_cs(&vop3_mod(0x132, 0, vgpr(1), vgpr(2), 0, 0, 0, 3, false));
let div2_clamp = build_valu_cs(&vop3_mod(0x132, 0, vgpr(1), vgpr(2), 0, 0, 0, 3, true));
// f16 constants: 0.25=0x3400, 0.5=0x3800, 0.75=0x3A00, 1.0=0x3C00, 1.5=0x3E00, 2.0=0x4000, -0.5=0xB800
run_cases(
&mul2,
&[[0x3400, 0x3800, 0, 0]], // (0.25 + 0.5) * 2 = 1.5 (0x3E00)
|backend, _, _, got| {
assert_eq!(got as u16, 0x3E00, "{backend}: v_add_f16 mul:2");
},
);
run_cases(
&mul4,
&[[0x3400, 0x3400, 0, 0]], // (0.25 + 0.25) * 4 = 2.0 (0x4000)
|backend, _, _, got| {
assert_eq!(got as u16, 0x4000, "{backend}: v_add_f16 mul:4");
},
);
run_cases(
&div2,
&[[0x3E00, 0x3E00, 0, 0]], // (1.5 + 1.5) / 2 = 1.5 (0x3E00)
|backend, _, _, got| {
assert_eq!(got as u16, 0x3E00, "{backend}: v_add_f16 div:2");
},
);
// Ordering check: (0.75 + 0.75) = 1.5; div:2 before clamp gives 0.75 (0x3A00),
// whereas clamp before div:2 would give 0.5 (0x3800).
let clamp_cases: &[(u16, u16, u16)] = &[
(0x3A00, 0x3A00, 0x3A00), // (0.75 + 0.75) / 2 = 0.75 -> 0.75
(0x3E00, 0x3E00, 0x3C00), // (1.5 + 1.5) / 2 = 1.5 -> clamped to 1.0
(0xB800, 0xB800, 0x0000), // (-0.5 + -0.5) / 2 = -0.5 -> clamped to 0.0
];
let inputs: Vec<[u32; 4]> = clamp_cases
.iter()
.map(|&(a, b, _)| [u32::from(a), u32::from(b), 0, 0])
.collect();
run_cases(&div2_clamp, &inputs, |backend, i, _, got| {
let want = clamp_cases[i].2;
assert_eq!(
got as u16, want,
"{backend}: v_add_f16 div:2 clamp case {i}: got {:#06x}, want {want:#06x}",
got as u16,
);
});
}
#[test]
fn cvt_16bit_int_from_f16_saturates_and_honors_modifiers() {
// VOP3 v_cvt_u16_f16 = 0x1D2 (0x180 + VOP1 0x52)
// VOP3 v_cvt_i16_f16 = 0x1D3 (0x180 + VOP1 0x53)
let cvt_u16 = build_valu_cs(&vop3_mod(0x1D2, 0, vgpr(1), 0, 0, 0, 0, 0, false));
let cvt_u16_abs = build_valu_cs(&vop3_mod(0x1D2, 0, vgpr(1), 0, 0, 1, 0, 0, false));
let cvt_i16 = build_valu_cs(&vop3_mod(0x1D3, 0, vgpr(1), 0, 0, 0, 0, 0, false));
let cvt_i16_neg = build_valu_cs(&vop3_mod(0x1D3, 0, vgpr(1), 0, 0, 0, 1, 0, false));
let u16_cases: &[(u16, u16)] = &[
(0xBC00, 0), // -1.0 -> 0
(0xFC00, 0), // -inf -> 0
(0x7E00, 0), // NaN -> 0
(0x57BC, 123), // 123.75 -> 123
(0x7BFF, 65504), // 65504.0 (max finite f16) -> 65504
(0x7C00, 0xFFFF), // +inf -> 65535 (saturated)
];
let u16_in: Vec<[u32; 4]> = u16_cases.iter().map(|&(x, _)| [u32::from(x), 0, 0, 0]).collect();
run_cases(&cvt_u16, &u16_in, |backend, i, inp, got| {
let want = u16_cases[i].1;
assert_eq!(
got as u16, want,
"{backend}: v_cvt_u16_f16({:#06x}) = {:#06x}, want {want:#06x}",
inp[0], got as u16,
);
});
run_cases(
&cvt_u16_abs,
&[[0xD7BC, 0, 0, 0]], // |-123.75| -> 123
|backend, _, _, got| {
assert_eq!(got as u16, 123, "{backend}: v_cvt_u16_f16 |src0|");
},
);
let i16_cases: &[(u16, u16)] = &[
(0xFC00, 0x8000), // -inf -> -32768
(0xF8E2, 0x8000), // -40000.0 -> -32768 (saturated, not wrapped)
(0xD7BC, (-123i16) as u16), // -123.75 -> -123
(0x7E00, 0), // NaN -> 0
(0x57BC, 123), // 123.75 -> 123
(0x78E2, 0x7FFF), // +40000.0 -> 32767 (saturated, not wrapped)
(0x7C00, 0x7FFF), // +inf -> 32767
];
let i16_in: Vec<[u32; 4]> = i16_cases.iter().map(|&(x, _)| [u32::from(x), 0, 0, 0]).collect();
run_cases(&cvt_i16, &i16_in, |backend, i, inp, got| {
let want = i16_cases[i].1;
assert_eq!(
got as u16, want,
"{backend}: v_cvt_i16_f16({:#06x}) = {:#06x}, want {want:#06x}",
inp[0], got as u16,
);
});
run_cases(
&cvt_i16_neg,
&[[0x57BC, 0, 0, 0], [0x78E2, 0, 0, 0]], // -(123.75) -> -123; -(40000.0) -> -32768
|backend, i, _, got| {
let want = [(-123i16) as u16, 0x8000][i];
assert_eq!(got as u16, want, "{backend}: v_cvt_i16_f16 -src0 case {i}");
},
);
}
const S_BARRIER: u32 = 0xBF8A_0000;
/// VOP2 `op vdst, src0, vsrc1`.
const fn vop2(op: u8, vdst: u8, src0: u16, vsrc1: u8) -> u32 {
(op as u32 & 0x3F) << 25 | (vdst as u32) << 17 | (vsrc1 as u32) << 9 | (src0 as u32 & 0x1FF)
}
/// DS `op vdst, addr, data0` at offset 0.
const fn ds(op: u8, vdst: u8, addr: u8, data0: u8) -> [u32; 2] {
[
0x36 << 26 | (op as u32) << 18,
(vdst as u32) << 24 | (data0 as u32) << 8 | addr as u32,
]
}
const DS_WRITE_B32: u8 = 0x0D;
const DS_MIN_F32: u8 = 0x12;
const DS_MAX_F32: u8 = 0x13;
const DS_MIN_RTN_F32: u8 = 0x32;
const DS_MAX_RTN_F32: u8 = 0x33;
const DS_READ_B32: u8 = 0x36;
/// What RDNA2 writes for `ds_max_f32` / `ds_min_f32` over a dword holding
/// `old` with `data` (bits), as measured on gfx1036 with f32 denormals kept:
/// an sNaN, data first, then memory, is written quieted; otherwise a NaN
/// loses to a number (two quiet NaNs give the data); numbers compare as
/// floats with -0 below +0.
fn lds_min_max_ref(old: u32, data: u32, max: bool) -> u32 {
let nan = |x: u32| x & 0x7FFF_FFFF > 0x7F80_0000;
let snan = |x: u32| nan(x) && x & 0x40_0000 == 0;
let key = |x: u32| {
let b = x as i32;
if b < 0 { b ^ 0x7FFF_FFFF } else { b }
};
if snan(data) {
return data | 0x40_0000;
}
if snan(old) {
return old | 0x40_0000;
}
match (nan(data), nan(old)) {
(true, true) | (false, true) => return data,
(true, false) => return old,
(false, false) => {}
}
let take = if max {
key(data) > key(old)
} else {
key(data) < key(old)
};
if take { data } else { old }
}
#[test]
fn ds_min_max_f32_order_floats_and_return_the_old_value() {
let values = [
0.0f32,
-0.0,
1.0,
-1.0,
2.5,
-2.5,
1e-30,
-1e-30,
f32::from_bits(1), // denormal
f32::from_bits(0x8000_0001),
f32::INFINITY,
f32::NEG_INFINITY,
f32::MAX,
f32::MIN,
];
// Quiet and signalling NaNs of both signs, in memory and as data.
let nans = [
0x7FC0_0000u32,
0xFFC0_0000,
0x7F80_0001,
0xFFA0_0000,
0x7FBF_FFFF,
];
let bits: Vec<u32> = values.iter().map(|v| v.to_bits()).chain(nans).collect();
let mut cases: Vec<[u32; 4]> = Vec::new();
for &old in &bits {
for &data in &bits {
cases.push([old, data, 0, 0]);
}
}
for (rtn, plain, max, what) in [
(DS_MAX_RTN_F32, DS_MAX_F32, true, "ds_max"),
(DS_MIN_RTN_F32, DS_MIN_F32, false, "ds_min"),
] {
// v5 = 0; LDS[v5] = v1; the atomic with v2; v0 = the old value
// (rtn) or the dword afterwards (read back).
let mut returns_old = vec![vop1(0x001, 5, 0x80)];
returns_old.extend(ds(DS_WRITE_B32, 0, 5, 1));
returns_old.extend(ds(rtn, 0, 5, 2));
let mut returns_new = vec![vop1(0x001, 5, 0x80)];
returns_new.extend(ds(DS_WRITE_B32, 0, 5, 1));
returns_new.extend(ds(plain, 0, 5, 2));
returns_new.extend(ds(DS_READ_B32, 0, 5, 0));
for (body, old_result) in [(returns_old, true), (returns_new, false)] {
// One thread with LDS declares WaveSize(32), which WARP's
// pipeline creation refuses: hardware only (the 64-thread test
// below runs on WARP too).
let shader = build_cs(&body, 1, 512);
run_cases_on(
&shader,
&cases,
&[("hardware", false)],
|backend, _, inp, got| {
let (old, data) = (f32::from_bits(inp[0]), f32::from_bits(inp[1]));
let want = if old_result {
inp[0]
} else {
lds_min_max_ref(inp[0], inp[1], max)
};
assert_eq!(
got,
want,
"{backend}: {what}_f32 on {old:e} [{:#010x}] with {data:e} [{:#010x}] gave {got:#010x}, want {want:#010x} ({})",
inp[0],
inp[1],
if old_result {
"old value"
} else {
"dword after"
},
);
},
);
}
}
}
#[test]
fn ds_min_max_f32_are_atomic_across_waves() {
// 64 threads (two host waves of 32 or one of 64): LDS[0] = v1, then
// every thread applies (thread - 32) as a float; v0 = LDS[0].
for (op, max, init, want) in [
(DS_MAX_F32, true, f32::NEG_INFINITY, 31.0f32),
(DS_MAX_F32, true, 100.0, 100.0),
(DS_MIN_F32, false, f32::INFINITY, -32.0),
(DS_MIN_F32, false, -0.0, -32.0),
] {
let mut body = vec![vop1(0x001, 5, 0x80)];
body.extend(ds(DS_WRITE_B32, 0, 5, 1));
body.push(S_BARRIER);
body.push(vop2(0x26, 6, vgpr(0), 3)); // v_sub_nc_u32 v6, v0, v3
body.push(vop1(0x005, 6, vgpr(6))); // v_cvt_f32_i32
body.extend(ds(op, 0, 5, 6));
body.push(S_BARRIER);
body.extend(ds(DS_READ_B32, 0, 5, 0));
let shader = build_cs(&body, 64, 512);
run_cases(
&shader,
&[[init.to_bits(), 0, 32, 0]],
|backend, _, _, got| {
assert_eq!(
f32::from_bits(got),
want,
"{backend}: {} from {init} over 64 threads",
if max { "max" } else { "min" },
);
},
);
}
}
#[test]
fn v_cmp_class_f16_matches_rdna2() {
// v_cmp_class_f16_e64 s6, v1, v2 (VOP3 opcode 0x08f); v_cndmask_b32_e64 v0, 0, 1, s6