From 868e466247463f7292036d94e367f13104d63f44 Mon Sep 17 00:00:00 2001 From: Foniass <107039406+Foniass@users.noreply.github.com> Date: Tue, 1 Dec 2026 18:55:33 +0100 Subject: [PATCH] Recompiler: keep clamped VOP3 pkrtz when patching 32-bit PS exports patch_ps_32bit_exports turns a compressed `exp mrtK` fed by v_cvt_pkrtz_f16_f32 into an uncompressed export of the conversion's raw f32 sources, and must decline the patch when the VOP3 form carries an input or output modifier. It looked the clamp bit up as "CLMP", but the encoding table names the ENC_VOP3 field "CLAMP"; field_value returns None for an unknown name, which the lookup turns into 0. So a clamped conversion was never declined and its clamp was dropped: a render target with a 32-bit format received the unclamped f32 value instead of the value the title clamped to [0, 1]. The new test exports a VOP3 pkrtz of a large float to a 32-bit MRT: it is patched without CLAMP and left alone with CLAMP, in both the HLSL and the direct DXIL backends. It fails on main. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/core_tests.rs | 40 +++++++++++++++++++++++++++ crates/xcl/src/gpu/recomp/mod.rs | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index e1421c69bc..08b7b762ab 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -245,6 +245,46 @@ fn ps_32bit_rt_export_declines_patch_when_aliased_pkrtz_dst_is_live_after_exp() assert!(!dis.contains("storeOutput.i32(i32 105, i32 1, i32 0, i8 0, i32 2015180568)"), "direct DXIL must not store raw literal 0x781d3718 to RT1 when patch is declined:\n{dis}"); } +#[test] +fn ps_32bit_rt_export_declines_patch_for_clamped_vop3_pkrtz() { + use crate::gpu::dxbc::SigElement as S; + use crate::gpu::recomp::{translate, Mode, Options, PsInterface}; + let el = |name: &str, index: u32, sv: u32, ct: u32, reg: u32, mask: u8| S { + name: name.into(), index, system_value: sv, component_type: ct, register: reg, mask, rw_mask: mask, stream: 0, + }; + const V: u64 = 256; + // MRT1 exports a VOP3 v_cvt_pkrtz_f16_f32 of v6 (a large float) and v7; with CLAMP set, the + // raw 32-bit v6 is not what the title exports, so the patch must leave the conversion alone. + let code = |clamp: u64| { + prog(&[ + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 6), ("SRC0", 255)], Some(0x781d_3718)), + ("V_MOV_B32", "ENC_VOP1", &[("VDST", 7), ("SRC0", 128)], None), + ("V_CVT_PKRTZ_F16_F32", "ENC_VOP3", &[("VDST", 8), ("SRC0", V + 6), ("SRC1", V + 7), ("CLAMP", clamp)], None), + ("EXP", "ENC_EXP", &[("TGT", 1), ("EN", 0x3), ("COMPR", 1), ("DONE", 1), ("VM", 1), ("VSRC0", 8), ("VSRC1", 0), ("VSRC2", 0), ("VSRC3", 0)], None), + ]) + }; + let psi = PsInterface { + inputs: vec![el("SV_Position", 0, 1, 3, 0, 0xF)], + outputs: vec![el("SV_Target", 1, 64, 1, 1, 0x1)], + input_ena: 0x2, + attrs: None, + }; + let hlsl = |clamp: u64| translate(&code(clamp), &Options { mode: Mode::Pixel(psi.clone()), wave64: false, ps_rt32_mask: 1 << 1, ..Default::default() }).unwrap().hlsl; + + let unclamped = hlsl(0); + assert!(unclamped.contains("xo.t1[0] = v6;"), "an unclamped VOP3 pkrtz is patched to export raw v6:\n{unclamped}"); + let clamped = hlsl(1); + assert!(!clamped.contains("xo.t1[0] = v6;"), "a clamped VOP3 pkrtz must not be patched away:\n{clamped}"); + assert!(clamped.contains("xo.t1[0] = (v8 & 0xFFFFu);"), "RT1 must keep exporting the clamped conversion:\n{clamped}"); + + let ko = KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 }; + let k = super::kernel::translate_pixel_rt32(&code(1), ko, &psi, 1 << 1) + .unwrap_or_else(|e| panic!("direct DXIL PS rt32 clamped pkrtz failed: {}", &e[..e.len().min(800)])); + super::validate::validate(&k.dxil).expect("valid direct DXIL when patch is declined"); + let dis = super::validate::disassemble(&k.dxil).expect("disassemble direct DXIL"); + assert!(!dis.contains("i32 2015180568)"), "direct DXIL must not store raw literal 0x781d3718 to RT1 for a clamped pkrtz:\n{dis}"); +} + #[test] fn straight_line_and_loop_kernels_validate() { let code = prog(&[ diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 40e5550799..0194a6a57c 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -208,7 +208,7 @@ pub(crate) fn patch_ps_32bit_exports(insts: &mut [Decoded], rt32_mask: u8) { ((256..512).contains(&s0) && s1 < 256).then(|| (s0 - 256, s1)) } "ENC_VOP3" => { - if f("ABS") != 0 || f("NEG") != 0 || f("CLMP") != 0 || f("OMOD") != 0 { + if f("ABS") != 0 || f("NEG") != 0 || f("CLAMP") != 0 || f("OMOD") != 0 { return None; } let s0 = f("SRC0");