From d2e5c7b6e14d3f37dd547c7421c2f9b175c93888 Mon Sep 17 00:00:00 2001 From: slopgobblercobbler <336610228+slopgobblercobbler@users.noreply.github.com> Date: Mon, 23 Nov 2026 22:47:35 +0100 Subject: [PATCH] Recompiler: deduplicate packed ISG1 vertex input struct fields - Prevent duplicate XclIn field names a{e.register} when multiple packed ISG1 vertex elements share an input register (#105). - Deduplicate struct fields with _{dup} suffixes in vs_input_names and route channel reads to the element declaring the channel via vs_input_slot. - Add unit and HLSL translation regression tests verifying non-colliding fields and valid channel reads for packed vertex inputs. --- crates/xcl/src/gpu/recomp/emit.rs | 26 +++++++++++---- crates/xcl/src/gpu/recomp/mod.rs | 34 ++++++++++++++++++- crates/xcl/src/gpu/recomp/tests.rs | 53 ++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 8 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 216240bd2c..000c0fd12c 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, BTreeSet}; use std::fmt::Write as _; -use super::{elements_at, param_registers, ps_attribute_slot, ps_input_vgpr, ps_slot_attribute, ranked, vs_param_register, Mode, MsInterface, Options, KERNEL_EXPORTS, KERNEL_LANES, KERNEL_SCC_SLOT, KERNEL_SGPRS, KERNEL_SPLIT_MAGIC, KERNEL_SPLIT_SGPRS, KERNEL_VGPRS, MESH_HEADER}; +use super::{elements_at, param_registers, ps_attribute_slot, ps_input_vgpr, ps_slot_attribute, ranked, vs_input_names, vs_param_register, Mode, MsInterface, Options, KERNEL_EXPORTS, KERNEL_LANES, KERNEL_SCC_SLOT, KERNEL_SGPRS, KERNEL_SPLIT_MAGIC, KERNEL_SPLIT_SGPRS, KERNEL_VGPRS, MESH_HEADER}; use super::upper_lanes::Read; use crate::gpu::dxbc::SigElement; use crate::gpu::rdna2::{self, Decoded, Encoding, Operand, Variant}; @@ -4338,9 +4338,11 @@ impl Em { } Mode::Vertex(vs) => { h.push_str("struct XclIn {\n"); - let inputs: Vec<&SigElement> = vs.inputs.iter().filter(|e| e.system_value == 0 && !e.name.starts_with("SV_")).collect(); - for e in &inputs { - let _ = writeln!(h, " {}4 a{} : {};", hlsl_type(e), e.register, e.semantic()); + let inputs = ranked(&vs.inputs, false); + let names = vs_input_names(&vs.inputs); + for (k, e) in inputs.iter().enumerate() { + let name = &names[k]; + let _ = writeln!(h, " {}4 {name} : {};", hlsl_type(e), e.semantic()); } h.push_str(" uint xcl_vid : SV_VertexID;\n uint xcl_iid : SV_InstanceID;\n};\n"); out_struct(&mut h, "XclOut", &vs.outputs); @@ -4363,9 +4365,19 @@ impl Em { if e.system_value == 6 { "xi.xcl_vid" } else { "xi.xcl_iid" }.to_string() } else { // the fetch shader's VGPRs: four per register the program reads (`fetched_registers`) - inputs + super::fetched_registers(&vs.inputs) .iter() - .find_map(|e| vs.input_vgpr(e.register).filter(|&b| v >= b && v < b + 4).map(|b| format!("asuint(xi.a{}[{}])", e.register, v - b))) + .find_map(|&r| { + let b = vs.input_vgpr(r)?; + if v >= b && v < b + 4 { + let chan = (v - b) as usize; + let slot = vs.input_slot(r, chan)?; + let name = &names[slot]; + Some(format!("asuint(xi.{name}[{chan}])")) + } else { + None + } + }) .unwrap_or_else(|| "0u".into()) }; let _ = writeln!(h, " uint v{v} = {init};"); @@ -5345,7 +5357,7 @@ 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 == 0u) return (uint)round(clamp(f, 0.0f, 1.0f) * maxu); if (ty == 1u) return (uint)(int)round((isnan(f) ? 0.0f : clamp(f, -1.0f, 1.0f)) * maxs); if (ty == 2u) return (uint)f; if (ty == 3u) return (uint)(int)f; diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 0f4a9049d2..eba38128f0 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -99,6 +99,11 @@ impl VsInterface { pub fn input_vgpr(&self, register: u32) -> Option { fetched_registers(&self.inputs).iter().position(|&r| r == register).map(|k| self.attr_base + 4 * k as u64) } + + /// The place in `ranked(inputs, false)` to read channel `chan` of ISG1 register `register`. + pub fn input_slot(&self, register: u32, chan: usize) -> Option { + vs_input_slot(self, register, chan) + } } /// The ISG1 registers the console's fetch shader loads, in the order of their VGPRs (four @@ -419,6 +424,31 @@ pub fn vs_param_register(vs: &VsInterface, n: usize) -> Option { } } +/// Field names of the PC vertex input struct (`XclIn`): `a{e.register}` when unique, deduplicated +/// with `_{dup}` suffixes when multiple packed ISG1 vertex elements share a register. +pub fn vs_input_names(inputs: &[SigElement]) -> Vec { + let ranked_inputs = ranked(inputs, false); + let mut counts = BTreeMap::new(); + let mut names = Vec::with_capacity(ranked_inputs.len()); + for e in ranked_inputs { + let n = counts.entry(e.register).or_insert(0usize); + names.push(if *n == 0 { + format!("a{}", e.register) + } else { + format!("a{}_{}", e.register, *n) + }); + *n += 1; + } + names +} + +/// The place in `ranked(inputs, false)` to read channel `chan` of ISG1 register `register` +/// from: an element at that register (the one declaring `chan`, if any). +pub fn vs_input_slot(vs: &VsInterface, register: u32, chan: usize) -> Option { + let all = ranked(&vs.inputs, false); + all.iter().position(|e| e.register == register && e.mask >> chan & 1 == 1).or_else(|| all.iter().position(|e| e.register == register)) +} + /// Hardware order and VGPR sizes of the PS inputs enabled in SPI_PS_INPUT_ENA (bit → size): /// persp sample/center/centroid (2), pull model (3), linear sample/center/centroid (2), /// line stipple, pos x/y/z/w, front face, ancillary, sample coverage, pos fixed (1 each). @@ -1536,8 +1566,10 @@ mod attribute_tests { let ps = PsInterface { inputs: sig.clone(), ..Default::default() }; assert_eq!((ps_attribute_slot(&ps, 0, 0), ps_attribute_slot(&ps, 0, 3), ps_attribute_slot(&ps, 1, 0)), (Some(0), Some(1), Some(2))); assert_eq!((0..4).map(|j| ps_slot_attribute(&ps, j)).collect::>(), [Some(0), Some(0), Some(1), Some(2)]); - let vs = VsInterface { outputs: sig, ..Default::default() }; + let vs = VsInterface { inputs: sig.clone(), outputs: sig, ..Default::default() }; assert_eq!((vs_param_register(&vs, 1), vs_param_register(&vs, 3)), (Some(1), None)); + assert_eq!(vs_input_names(&vs.inputs), ["a0", "a0_1", "a1", "a2"]); + assert_eq!((vs.input_slot(0, 0), vs.input_slot(0, 3), vs.input_slot(1, 0)), (Some(0), Some(1), Some(2))); // the console's table: a Fortnite VS whose SV_Position register is param 1 let mut f = vec![0u8; 0x400]; f[0x10] = 5; diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 5a8d735396..f328a84786 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -4181,6 +4181,59 @@ fn vs_system_values_in_isg1_registers() { assert!(t.hlsl.contains("uint v4 = asuint(xi.a0[0]);"), "{}", t.hlsl); } +/// Multiple packed ISG1 vertex elements sharing a register emit non-colliding +/// struct fields and read their declared channels. +#[test] +fn vs_packed_isg1_vertex_input_dedup() { + use super::VsInterface; + let sig_mask = |name: &str, idx: u32, reg: u32, mask: u8| crate::gpu::dxbc::SigElement { + name: name.into(), + index: idx, + system_value: 0, + component_type: 3, + register: reg, + mask, + rw_mask: mask, + stream: 0, + }; + let vs = VsInterface { + inputs: vec![ + sig_mask("UV", 1, 0, 0x3), + sig_mask("UV", 0, 0, 0xC), + sig_mask("COLOR", 0, 1, 0xF), + ], + outputs: vec![crate::gpu::dxbc::SigElement { + name: "SV_Position".into(), + index: 0, + system_value: 1, + component_type: 3, + register: 0, + mask: 0xF, + rw_mask: 0xF, + stream: 0, + }], + attr_base: 4, + params: None, + }; + let code = prog(&[ + ("V_ADD_F32", "ENC_VOP2", &[("VDST", 20), ("SRC0", V + 4), ("VSRC1", 5)], None), + ("V_ADD_F32", "ENC_VOP2", &[("VDST", 21), ("SRC0", V + 6), ("VSRC1", 7)], None), + ("V_ADD_F32", "ENC_VOP2", &[("VDST", 22), ("SRC0", V + 8), ("VSRC1", 9)], None), + ]); + let t = translate(&code, &Options { mode: Mode::Vertex(vs), wave64: true, ..Default::default() }).unwrap(); + assert!(t.hlsl.contains("float4 a0 : UV1;"), "{}", t.hlsl); + assert!(t.hlsl.contains("float4 a0_1 : UV0;"), "{}", t.hlsl); + assert!(t.hlsl.contains("float4 a1 : COLOR0;"), "{}", t.hlsl); + assert!(t.hlsl.contains("uint v4 = asuint(xi.a0[0]);"), "{}", t.hlsl); + assert!(t.hlsl.contains("uint v5 = asuint(xi.a0[1]);"), "{}", t.hlsl); + assert!(t.hlsl.contains("uint v6 = asuint(xi.a0_1[2]);"), "{}", t.hlsl); + assert!(t.hlsl.contains("uint v7 = asuint(xi.a0_1[3]);"), "{}", t.hlsl); + assert!(t.hlsl.contains("uint v8 = asuint(xi.a1[0]);"), "{}", t.hlsl); + if super::dxc::location().is_ok() { + super::dxc::compile(&t.hlsl, "main", "vs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl)); + } +} + #[test] #[ignore] fn probe_standard_swizzle_support() {