Skip to content
Open
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
26 changes: 19 additions & 7 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand All @@ -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};");
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 33 additions & 1 deletion crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ impl VsInterface {
pub fn input_vgpr(&self, register: u32) -> Option<u64> {
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<usize> {
vs_input_slot(self, register, chan)
}
}

/// The ISG1 registers the console's fetch shader loads, in the order of their VGPRs (four
Expand Down Expand Up @@ -419,6 +424,31 @@ pub fn vs_param_register(vs: &VsInterface, n: usize) -> Option<u32> {
}
}

/// 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<String> {
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<usize> {
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).
Expand Down Expand Up @@ -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::<Vec<_>>(), [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;
Expand Down
53 changes: 53 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading