From 40f90f7b09fcad72ebcc4822b77b8f521e3049a7 Mon Sep 17 00:00:00 2001 From: Michael-Glaser Date: Mon, 23 Nov 2026 23:56:29 +0100 Subject: [PATCH] Recompiler: handle packed ISG1 vertex inputs sharing a register (#105) In Mode::Vertex, struct XclIn named fields a{e.register} and VGPR initialisation looked up the first element matching e.register while ignoring e.mask. When two ISG1 vertex inputs pack into one register (e.g. TEXCOORD0 .xy and TEXCOORD1 .zw in register 1), XclIn emitted duplicate field names (failing DXC compilation) and upper channels read from the first element instead of the packed element's components. Name XclIn fields by element index (a{k}, matching Mode::Pixel) and match each VGPR channel against e.mask, indexing the packed element starting from component 0 while keeping the unmasked fallback for single elements that rely on IA defaults. Assisted-by: Antigravity (Gemini 4 Neon) --- crates/xcl/src/gpu/recomp/emit.rs | 29 +++++++++++++++++++++---- crates/xcl/src/gpu/recomp/tests.rs | 34 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 216240bd2..926dfef91 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -4339,8 +4339,8 @@ 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()); + for (k, e) in inputs.iter().enumerate() { + let _ = writeln!(h, " {}4 a{k} : {};", 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); @@ -4362,10 +4362,31 @@ impl Em { } else if let Some(e) = sv_at { 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`) + // the fetch shader's VGPRs: four per register the program reads (`fetched_registers`); + // elements packed into one register share its VGPRs by mask (each semantic loads into + // its own XclIn field starting at component 0) 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))) + .enumerate() + .find_map(|(k, e)| { + let b = vs.input_vgpr(e.register)?; + if v < b || v >= b + 4 { + return None; + } + let chan = (v - b) as u8; + if (e.mask >> chan) & 1 != 0 { + let comp = (e.mask & ((1u8 << chan) - 1)).count_ones(); + Some(format!("asuint(xi.a{k}[{comp}])")) + } else { + None + } + }) + .or_else(|| { + inputs + .iter() + .enumerate() + .find_map(|(k, e)| vs.input_vgpr(e.register).filter(|&b| v >= b && v < b + 4).map(|b| format!("asuint(xi.a{k}[{}])", v - b))) + }) .unwrap_or_else(|| "0u".into()) }; let _ = writeln!(h, " uint v{v} = {init};"); diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 4e032b68d..a003e4bc4 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -4181,6 +4181,40 @@ fn vs_system_values_in_isg1_registers() { assert!(t.hlsl.contains("uint v4 = asuint(xi.a0[0]);"), "{}", t.hlsl); } +#[test] +fn vertex_packed_isg1_inputs_get_distinct_fields_and_components() { + let vs = super::VsInterface { + inputs: vec![ + el("POSITION", 0, 0, 3, 0, 0x7), + el("TEXCOORD", 0, 0, 3, 1, 0x3), + el("TEXCOORD", 1, 0, 3, 1, 0xC), + ], + outputs: vec![el("SV_Position", 0, 1, 3, 0, 0xF)], + attr_base: 4, + params: None, + }; + let code = prog(&[ + ("V_ADD_F32", "ENC_VOP2", &[("VDST", 20), ("SRC0", V + 4), ("VSRC1", 7)], None), // POSITION.x + POSITION.w (IA default) + ("V_ADD_F32", "ENC_VOP2", &[("VDST", 21), ("SRC0", V + 8), ("VSRC1", 9)], None), // TEXCOORD0.x + TEXCOORD0.y + ("V_ADD_F32", "ENC_VOP2", &[("VDST", 22), ("SRC0", V + 10), ("VSRC1", 11)], None), // TEXCOORD1.x + TEXCOORD1.y (packed in reg 1 .zw) + ("EXP", "ENC_EXP", &[("TGT", 12), ("EN", 0xF), ("VSRC0", 20), ("VSRC1", 21), ("VSRC2", 22), ("VSRC3", 7), ("DONE", 1)], None), + ]); + let t = translate(&code, &Options { mode: Mode::Vertex(vs), wave64: true, ..Default::default() }).unwrap(); + assert!(t.complete(), "{:?}\n{}", t.unsupported_why, t.hlsl); + assert!(t.hlsl.contains("float4 a0 : POSITION0;"), "{}", t.hlsl); + assert!(t.hlsl.contains("float4 a1 : TEXCOORD0;"), "{}", t.hlsl); + assert!(t.hlsl.contains("float4 a2 : TEXCOORD1;"), "{}", t.hlsl); + assert!(t.hlsl.contains("uint v4 = asuint(xi.a0[0]);"), "{}", t.hlsl); + assert!(t.hlsl.contains("uint v7 = asuint(xi.a0[3]);"), "{}", t.hlsl); + assert!(t.hlsl.contains("uint v8 = asuint(xi.a1[0]);"), "{}", t.hlsl); + assert!(t.hlsl.contains("uint v9 = asuint(xi.a1[1]);"), "{}", t.hlsl); + assert!(t.hlsl.contains("uint v10 = asuint(xi.a2[0]);"), "{}", t.hlsl); + assert!(t.hlsl.contains("uint v11 = asuint(xi.a2[1]);"), "{}", 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() {