Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ next steps:
| **Ray tracing** | ✅ | BLAS / TLAS built on the GPU by the game's own XboxBvhBuilder; RDNA2 BVH node format; BVH intersection instructions emulated in shaders | GPU traversal = brute force reference; the game's builder output checked on the GPU |
| **Upscaling** | ✅ | the game's AMD FSR 4 console DLL (`amd_fidelityfx_upscaler_xbox.dll`): context, the 29 pipelines in the console driver's own container (FSPX) translated, a frame's 20 passes recorded and run on the GPU | end-to-end with the real DLL: a 1280×720 → 1920×1080 frame on an RX 9070, the input's gradient in the output |

The recompiler rounds FP32-to-UNORM/SNORM conversions and typed buffer stores once to nearest
even, including packed colors, avoiding a one-step error from intermediate FP32 rounding.

### Media and tools

| Feature | State | What it does | Verified by |
Expand Down
37 changes: 31 additions & 6 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3322,7 +3322,7 @@ impl Em {
"v_cvt_pk_u16_u32" => format!("min({a}, 0xFFFFu) | (min({b}, 0xFFFFu) << 16)"),
"v_cvt_pk_i16_i32" => format!("((uint)clamp((int){a}, -32768, 32767) & 0xFFFFu) | (((uint)clamp((int){b}, -32768, 32767) & 0xFFFFu) << 16)"),
"v_cvt_pknorm_i16_f32" => format!("(cvt_norm_i16_f32({}) | (cvt_norm_i16_f32({}) << 16))", fs(0), fs(1)),
"v_cvt_pknorm_u16_f32" => format!("(uint)round(saturate({}) * 65535.0f) | ((uint)round(saturate({}) * 65535.0f) << 16)", fs(0), fs(1)),
"v_cvt_pknorm_u16_f32" => format!("(cvt_norm_u16_f32({}) | (cvt_norm_u16_f32({}) << 16))", fs(0), fs(1)),
// packed f16 sources: op_sel[0]/[1] pick each source's half; result fills all 32 bits
"v_cvt_pknorm_i16_f16" | "v_cvt_pknorm_u16_f16" => {
let hb = |i: usize| {
Expand Down Expand Up @@ -5155,10 +5155,15 @@ void put_bits(inout uint4 r, uint off, uint w, uint v) {
}
uint fmt_encode(uint x, uint info, uint c) {
uint ty = (info >> 3) & 7u; uint w = (info >> (8u + 6u * c)) & 63u; float f = asfloat(x);
float maxu = w >= 32u ? 4294967295.0f : (float)((1u << w) - 1u);
float maxs = w >= 32u ? 2147483647.0f : (float)((1u << (w - 1u)) - 1u);
if (ty == 0u) return (uint)round(saturate(f) * maxu);
if (ty == 1u) return (uint)(int)round((isnan(f) ? 0.0f : clamp(f, -1.0f, 1.0f)) * maxs);
uint maxu = w >= 32u ? 0xFFFFFFFFu : (1u << w) - 1u;
uint maxs = w >= 32u ? 0x7FFFFFFFu : (1u << (w - 1u)) - 1u;
// rounded once (norm_f32_mag): round(f * max) in f32 rounds twice
if (ty == 0u) return select(or(isnan(f), f <= 0.0f), 0u, select(f >= 1.0f, maxu, norm_f32_mag(f, maxu)));
if (ty == 1u) {
if (isnan(f)) return 0u;
uint m = abs(f) >= 1.0f ? maxs : norm_f32_mag(f, maxs);
return f < 0.0f ? (uint)(-(int)m) : m;
}
if (ty == 2u) return (uint)f;
if (ty == 3u) return (uint)(int)f;
if (ty == 4u || ty == 5u || w == 32u) return x;
Expand Down Expand Up @@ -5456,8 +5461,28 @@ uint cvt_norm_i16_f16(uint h) {
uint r = cvt_norm_f16(h, 32767u);
return ((h & 0x8000u) != 0u ? (uint)(-(int)r) : r) & 0xFFFFu;
}
// |f| * scale rounded once to nearest even, for |f| < 1, on the f32 bits: round(f * scale) in f32
// rounds twice (2.2888533e-5 * 65535 = 1.4999..., which f32 makes 1.5 and round() then 2, not 1).
uint norm_f32_mag(float f, uint scale) {
uint u = asuint(f) & 0x7FFFFFFFu;
uint e = u >> 23;
uint s = 150u - e; // |f| < 1, so e <= 126 and s >= 24
if (e == 0u || s >= 64u) return 0u; // zero, denormal or too small: below half a step
uint64_t prod = (uint64_t)((u & 0x7FFFFFu) | 0x800000u) * (uint64_t)scale;
uint64_t q = prod >> s;
uint64_t rem = prod & ((1ull << s) - 1ull);
uint64_t half = 1ull << (s - 1u);
return (uint)(q + (uint64_t)(rem > half || (rem == half && (q & 1ull) != 0ull)));
}
uint cvt_norm_u16_f32(float f) {
if (isnan(f) || f <= 0.0f) return 0u;
return f >= 1.0f ? 65535u : norm_f32_mag(f, 65535u);
}
uint cvt_norm_i16_f32(float f) {
return (uint)(int)round((isnan(f) ? 0.0f : clamp(f, -1.0f, 1.0f)) * 32767.0f) & 0xFFFFu;
if (isnan(f)) return 0u;
if (abs(f) >= 1.0f) return f < 0.0f ? 0x8001u : 0x7FFFu;
uint m = norm_f32_mag(f, 32767u);
return (f < 0.0f ? (uint)(-(int)m) : m) & 0xFFFFu;
}
float frexp_mant(float x) { float e; return (isinf(x) || isnan(x)) ? x : frexp(x, e); }
int frexp_exp(float x) { float e; frexp(x, e); return (isinf(x) || isnan(x)) ? 0 : (int)e; }
Expand Down
170 changes: 170 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6981,6 +6981,176 @@ fn gpu_pknorm_i16_f32_nan_and_fmt_encode_snorm_ufloat() {
assert_eq!(r.mem[0x20C / 4], 0x8001_0000, "16_16_SNORM (NaN, -1.0)");
}

/// Typed buffer stores to 16_UNORM / 16_SNORM / 8_UNORM / 8_SNORM round x * max once to nearest
/// even, like the pknorm instructions: inputs from a CPU search where rounding the f32 product
/// first is off by one (for each of the four scales), plus clamps, NaN and zeros. Lane l stores
/// one element at index l (stride 4) of each buffer.
#[test]
fn gpu_buffer_store_norm_formats_round_once() {
if !gpu_ready() {
return;
}
let searched = [
// 65535
0x37c0_00c0u32, 0x3860_00e0, 0x38f0_00f0, 0x3978_00f8, 0x39fc_00fc, 0x3a7e_00fe, 0x3aff_00ff, 0x3c00_6080,
0x3c80_7080, 0x3d00_7880, 0x3d80_7c80, 0x3e00_7e80, 0x3e80_7f80, 0x3f00_8000,
// 32767
0x3840_0180, 0x38e0_01c0, 0x3970_01e0, 0x39f8_01f0, 0x3a7c_01f8, 0x3afe_01fc, 0x3b7f_01fe, 0x3bff_81ff,
0x3d00_3100, 0x3d80_3900, 0x3e00_3d00, 0x3e80_3f00, 0x3f00_4000,
// 255
0x3b00_8081, 0x3c20_a0a1, 0x3cd0_d0d1, 0x3d68_e8e9, 0x3df4_f4f5, 0x3e7a_fafb, 0x3efd_fdfe, 0x3f7e_7e7e,
// 127
0x3c41_8306, 0x3ce1_c387, 0x3e0d_1a34, 0x3e8f_1e3c, 0x3f10_2040,
];
let mut xs: Vec<f32> = searched.iter().map(|&b| f32::from_bits(b)).collect();
xs.extend([0x3c41_8306u32, 0x3ce1_c387, 0x3e0d_1a34, 0x3e8f_1e3c, 0x3f10_2040].map(|b| -f32::from_bits(b)));
xs.extend([0.0f32, -0.0, 1.0, -1.0, 2.0, -2.0, f32::NAN, 0.5]);
assert!(xs.len() <= 64);
// (base, GFX10 buffer format, bits, signed)
let bufs = [(0x100u32, 7u32, 16u32, false), (0x200, 8, 16, true), (0x300, 1, 8, false), (0x400, 2, 8, true)];
let ident = 4 | 5 << 3 | 6 << 6 | 7 << 9;
let mut parts = Vec::new();
let mut st = KernelState::default();
for (i, &(base, fmt, ..)) in bufs.iter().enumerate() {
let s = 12 + 4 * i;
st.sgpr[s..s + 4].copy_from_slice(&[base, 4 << 16, 64, ident | fmt << 12]);
parts.push(ins("BUFFER_STORE_FORMAT_X", "ENC_MUBUF", &[("VDATA", 1), ("VADDR", 0), ("SRSRC", s as u64 / 4), ("SOFFSET", 128), ("IDXEN", 1)], None));
}
for (l, r) in st.vgpr.iter_mut().enumerate() {
r[0] = l as u32;
r[1] = xs[l % xs.len()].to_bits();
}
let r = runner::run_code(&program_of(&parts), &st, &vec![0u32; 0x180]).unwrap();
let mut bad = Vec::new();
for (l, &x) in xs.iter().enumerate() {
for &(base, _, w, signed) in &bufs {
let mask = (1u32 << w) - 1;
let want = if signed {
let m = ((1u32 << (w - 1)) - 1) as f64;
if x.is_nan() { 0 } else { ((x.clamp(-1.0, 1.0) as f64 * m).round_ties_even() as i32 as u32) & mask }
} else if x.is_nan() || x <= 0.0 {
0
} else {
((x.min(1.0) as f64) * mask as f64).round_ties_even() as u32
};
let got = r.mem[(base / 4) as usize + l] & mask;
if got != want {
bad.push(format!("{}{w}({x:e} = {:#010x}) = {got:#x}, want {want:#x}", if signed { "snorm" } else { "unorm" }, x.to_bits()));
}
}
}
assert!(bad.is_empty(), "{}", bad.join("\n"));
}

/// v_cvt_pknorm_u16_f32 / v_cvt_pknorm_i16_f32 round x * 65535 (32767) once to nearest even.
/// Rounding the f32 product first and then the result is off by one for about 32 000 (16 000)
/// f32 inputs; the inputs here are such cases across [0, 1) found by a CPU search (each one's exact
/// product is just below or above a .5 that the f32 product lands on), plus the clamps, NaN and
/// zeros. Both halves get the same input: low from S0, high from S1.
#[test]
fn gpu_pknorm_f32_rounds_once() {
if !gpu_ready() {
return;
}
let u16_cases = [
0x37c0_00c0u32, 0x3860_00e0, 0x38f0_00f0, 0x3978_00f8, 0x39fc_00fc, 0x3a7e_00fe, 0x3aff_00ff, 0x3c00_6080,
0x3c80_7080, 0x3d00_7880, 0x3d80_7c80, 0x3e00_7e80, 0x3e80_7f80, 0x3f00_8000,
];
let i16_cases = [
0x3840_0180u32, 0x38e0_01c0, 0x3970_01e0, 0x39f8_01f0, 0x3a7c_01f8, 0x3afe_01fc, 0x3b7f_01fe, 0x3bff_81ff,
0x3d00_3100, 0x3d80_3900, 0x3e00_3d00, 0x3e80_3f00, 0x3f00_4000,
];
let edges = [0.0f32, -0.0, 1.0, -1.0, 2.0, -2.0, f32::NAN, f32::INFINITY, 0.5, 0.999_999_94, -0.5, f32::from_bits(1)];
let mut xs: Vec<f32> = u16_cases.iter().chain(&i16_cases).map(|&b| f32::from_bits(b)).collect();
xs.extend(i16_cases.iter().map(|&b| -f32::from_bits(b)));
xs.extend(edges);
assert!(xs.len() <= 64);
let code = prog(&[
("V_CVT_PKNORM_U16_F32", "ENC_VOP3", &[("VDST", 10), ("SRC0", V + 1), ("SRC1", V + 1)], None),
("V_CVT_PKNORM_I16_F32", "ENC_VOP3", &[("VDST", 11), ("SRC0", V + 1), ("SRC1", V + 1)], None),
]);
let mut st = KernelState::default();
for (l, r) in st.vgpr.iter_mut().enumerate() {
r[1] = xs[l % xs.len()].to_bits();
}
let r = runner::run_code(&code, &st, &[]).unwrap();
let unorm = |x: f32| if x.is_nan() || x <= 0.0 { 0 } else if x >= 1.0 { 65535 } else { (x as f64 * 65535.0).round_ties_even() as u32 };
let snorm = |x: f32| if x.is_nan() { 0 } else { ((x.clamp(-1.0, 1.0) as f64 * 32767.0).round_ties_even() as i32 as u32) & 0xFFFF };
let mut bad = Vec::new();
for (l, v) in r.vgpr.iter().enumerate().take(xs.len()) {
let x = xs[l];
let (u, s) = (unorm(x), snorm(x));
if v[10] != (u | u << 16) {
bad.push(format!("v_cvt_pknorm_u16_f32({x:e} = {:#010x}) = {:#010x}, want {:#010x}", x.to_bits(), v[10], u | u << 16));
}
if v[11] != (s | s << 16) {
bad.push(format!("v_cvt_pknorm_i16_f32({x:e} = {:#010x}) = {:#010x}, want {:#010x}", x.to_bits(), v[11], s | s << 16));
}
}
assert!(bad.is_empty(), "{}", bad.join("\n"));
}

/// Mixed-width packed formats also round each component once and preserve its bit position.
#[test]
fn gpu_packed_norm_formats_round_once() {
if !gpu_ready() {
return;
}
let mut xs = Vec::new();
for scale in [1u32, 3, 127, 255, 511, 1023, 2047, 32767, 65535] {
let midpoint = (1.5 / scale as f64) as f32;
for bits in [midpoint.to_bits() - 1, midpoint.to_bits(), midpoint.to_bits() + 1] {
xs.extend([f32::from_bits(bits), -f32::from_bits(bits)]);
}
}
xs.extend([0.0, -0.0, 1.0, -1.0, f32::NAN, f32::INFINITY, f32::NEG_INFINITY,
f32::from_bits(1), -f32::from_bits(1), 0.5]);
assert_eq!(xs.len(), 64);
let formats: &[(&str, &[u32], bool)] = &[
("11_11_10_UNORM", &[10, 11, 11], false),
("11_11_10_SNORM", &[10, 11, 11], true),
("10_10_10_2_UNORM", &[2, 10, 10, 10], false),
("10_10_10_2_SNORM", &[2, 10, 10, 10], true),
("8_8_8_8_UNORM", &[8, 8, 8, 8], false),
("8_8_8_8_SNORM", &[8, 8, 8, 8], true),
("16_16_16_16_UNORM", &[16, 16, 16, 16], false),
("16_16_16_16_SNORM", &[16, 16, 16, 16], true),
];
for &(name, widths, signed) in formats {
let fmt = super::emit::BUF_FORMATS.iter().position(|&f| f == name).unwrap() as u32;
let op = if widths.len() == 3 { "BUFFER_STORE_FORMAT_XYZ" } else { "BUFFER_STORE_FORMAT_XYZW" };
let code = prog(&[(op, "ENC_MUBUF", &[("VDATA", 1), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("IDXEN", 1)], None)]);
let mut st = KernelState::default();
let ident = 4 | 5 << 3 | 6 << 6 | 7 << 9;
st.sgpr[12..16].copy_from_slice(&[0x100, 8 << 16, 64, ident | fmt << 12]);
for (l, regs) in st.vgpr.iter_mut().enumerate() {
regs[0] = l as u32;
for c in 0..widths.len() {
regs[c + 1] = xs[(l + c) % xs.len()].to_bits();
}
}
let r = runner::run_code(&code, &st, &vec![0u32; 0xC0]).unwrap();
for l in 0..64 {
let mut expected = 0u64;
let mut shift = 0;
for (c, &width) in widths.iter().enumerate() {
let x = xs[(l + c) % xs.len()];
let mask = (1u32 << width) - 1;
let scale = if signed { mask >> 1 } else { mask };
let value = if x.is_nan() { 0 } else {
(x.clamp(if signed { -1.0 } else { 0.0 }, 1.0) as f64 * scale as f64)
.round_ties_even() as i64 as u32 & mask
};
expected |= (value as u64) << shift;
shift += width;
}
let at = 0x100 / 4 + l * 2;
let actual = r.mem[at] as u64 | (r.mem[at + 1] as u64) << 32;
assert_eq!(actual, expected, "{name} lane {l}");
}
}
}

#[test]
fn gpu_signed_bfe_arithmetic_shift() {
for wave64 in gpu_waves() {
Expand Down
Loading