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
45 changes: 41 additions & 4 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4181,8 +4181,9 @@ 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 fields = vs_input_fields(&inputs);
for f in &fields {
let _ = writeln!(h, " {}{} {} : {};", hlsl_type(f.elem), f.count, f.name, f.elem.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 @@ -4205,9 +4206,14 @@ 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
// (a component of a register no element covers stays 0)
fields
.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(|f| {
let b = vs.input_vgpr(f.elem.register)?;
let c = v.checked_sub(b).filter(|&c| c < 4)? as u32;
(f.mask & (1u8 << c) != 0).then(|| format!("asuint(xi.{}[{}])", f.name, c - f.first))
})
.unwrap_or_else(|| "0u".into())
};
let _ = writeln!(h, " uint v{v} = {init};");
Expand Down Expand Up @@ -4721,6 +4727,37 @@ pub fn passthrough_elements(ms: &MsInterface, ps_inputs: Option<&[SigElement]>)
}
}

/// One member of a vertex shader's `XclIn`: the ISG1 element `elem`, read as components
/// `first .. first + count` of its register (the fetch shader's VGPRs `input_vgpr + first ..`).
struct VsInputField<'a> {
elem: &'a SigElement,
name: String,
first: u32,
count: u32,
mask: u8,
}

/// The `XclIn` members of the VS inputs: an element alone in its register is `a{register}`,
/// all four components (the input layout fills those it does not provide). Elements packed
/// into one register (TEXCOORD0 in .xy and TEXCOORD1 in .zw of r1: the fetch shader loads
/// TEXCOORD1.x into the register's z) each get their own member, `a{register}_{first}`, with
/// the components of their mask – one `a1` per element would be two members of one name,
/// which DXC rejects.
fn vs_input_fields<'a>(inputs: &[&'a SigElement]) -> Vec<VsInputField<'a>> {
inputs
.iter()
.map(|&e| {
if inputs.iter().filter(|o| o.register == e.register).count() < 2 {
return VsInputField { elem: e, name: format!("a{}", e.register), first: 0, count: 4, mask: 0xF };
}
let mask = u32::from(e.mask & 0xF).max(1);
let first = mask.trailing_zeros();
let count = 32 - mask.leading_zeros() - first;
VsInputField { elem: e, name: format!("a{}_{first}", e.register), first, count, mask: e.mask & 0xF }
})
.collect()
}

fn hlsl_type(e: &SigElement) -> &'static str {
match e.component_type {
1 => "uint",
Expand Down
141 changes: 141 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,49 @@ fn gpu_graphics_vs_ps_draw() {
}
}

/// A console VS whose ISG1 packs TEXCOORD0 (.xy) and TEXCOORD1 (.zw) into r1, drawn with an
/// input layout that gives each its own R32G32 element: param0 = r1 = (TEXCOORD0.xy,
/// TEXCOORD1.xy) reaches every pixel. Before each packed element had its own `XclIn` member
/// the VS did not compile.
#[test]
fn gpu_vs_packed_isg1_inputs() {
if !gpu_ready() {
return;
}
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32_FLOAT;
let (vs_code, vsi) = packed_inputs_vs(0xC);
let pass_ps = prog(&[
("V_INTERP_P1_F32", "ENC_VINTRP", &[("VDST", 4), ("VSRC", 0), ("ATTR", 0), ("ATTRCHAN", 0)], None),
("V_INTERP_P2_F32", "ENC_VINTRP", &[("VDST", 4), ("VSRC", 1), ("ATTR", 0), ("ATTRCHAN", 0)], None),
("V_INTERP_P1_F32", "ENC_VINTRP", &[("VDST", 5), ("VSRC", 0), ("ATTR", 0), ("ATTRCHAN", 1)], None),
("V_INTERP_P2_F32", "ENC_VINTRP", &[("VDST", 5), ("VSRC", 1), ("ATTR", 0), ("ATTRCHAN", 1)], None),
("V_INTERP_P1_F32", "ENC_VINTRP", &[("VDST", 6), ("VSRC", 0), ("ATTR", 0), ("ATTRCHAN", 2)], None),
("V_INTERP_P2_F32", "ENC_VINTRP", &[("VDST", 6), ("VSRC", 1), ("ATTR", 0), ("ATTRCHAN", 2)], None),
("V_INTERP_P1_F32", "ENC_VINTRP", &[("VDST", 7), ("VSRC", 0), ("ATTR", 0), ("ATTRCHAN", 3)], None),
("V_INTERP_P2_F32", "ENC_VINTRP", &[("VDST", 7), ("VSRC", 1), ("ATTR", 0), ("ATTRCHAN", 3)], None),
("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 4), ("VSRC1", 5), ("VSRC2", 6), ("VSRC3", 7), ("DONE", 1), ("VM", 1)], None),
]);
let compile = |code: &[u8], mode: Mode, profile: &str| {
let t = translate(code, &runner::options(mode)).unwrap();
assert!(t.complete(), "{:?}", t.unsupported_why);
super::dxc::compile(&t.hlsl, "main", profile, &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl))
};
let vs = compile(&vs_code, Mode::Vertex(vsi), "vs_6_6");
let ps = compile(&pass_ps, Mode::Pixel(draw_psi()), "ps_6_6");
let (t0, t1) = ([0.25f32, 0.5], [0.75f32, 0.125]);
let verts: Vec<f32> = [[-1.0f32, -1.0], [-1.0, 3.0], [3.0, -1.0]].iter().flat_map(|p| [p[0], p[1], t0[0], t0[1], t1[0], t1[1]]).collect();
let layout = [
runner::VertexElement { semantic: "POSITION", index: 0, format: DXGI_FORMAT_R32G32_FLOAT, offset: 0 },
runner::VertexElement { semantic: "TEXCOORD", index: 0, format: DXGI_FORMAT_R32G32_FLOAT, offset: 8 },
runner::VertexElement { semantic: "TEXCOORD", index: 1, format: DXGI_FORMAT_R32G32_FLOAT, offset: 16 },
];
let px = runner::draw(&vs, &ps, &layout, &verts, 24, &[], &[], 16, [-1.0; 4]).unwrap();
let want = [t0[0], t0[1], t1[0], t1[1]];
for (i, got) in px.iter().enumerate() {
assert!(got.iter().zip(want).all(|(a, b)| (a - b).abs() < 1e-6), "pixel ({},{}) = {got:?}, expected {want:?}", i % 16, i / 16);
}
}

/// `emit::rect_list_gs` refuses integer (flat) outputs and declares the VS's output struct.
#[test]
fn rect_list_gs_outputs() {
Expand Down Expand Up @@ -3959,6 +4002,62 @@ fn vs_system_values_in_isg1_registers() {
assert!(t.hlsl.contains("uint v4 = asuint(xi.a0[0]);"), "{}", t.hlsl);
}

/// The VS of `gpu_vs_packed_isg1_inputs`: POSITION in r0, TEXCOORD0 / TEXCOORD1 packed into
/// r1 (`t1_mask` the components TEXCOORD1 takes); reads v8..v11 (r1) into param0.
fn packed_inputs_vs(t1_mask: u8) -> (Vec<u8>, super::VsInterface) {
let code = prog(&[
("V_MOV_B32", "ENC_VOP1", &[("VDST", 0), ("SRC0", V + 4)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 1), ("SRC0", V + 5)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 2), ("SRC0", 128)], None),
("V_MOV_B32", "ENC_VOP1", &[("VDST", 3), ("SRC0", 242)], None),
("EXP", "ENC_EXP", &[("TGT", 12), ("EN", 0xF), ("VSRC0", 0), ("VSRC1", 1), ("VSRC2", 2), ("VSRC3", 3), ("DONE", 1)], None),
("EXP", "ENC_EXP", &[("TGT", 32), ("EN", 0xF), ("VSRC0", 8), ("VSRC1", 9), ("VSRC2", 10), ("VSRC3", 11)], None),
]);
let vs = super::VsInterface {
inputs: vec![el("POSITION", 0, 0, 3, 0, 3), el("TEXCOORD", 0, 0, 3, 1, 3), el("TEXCOORD", 1, 0, 3, 1, t1_mask)],
outputs: vec![el("SV_Position", 0, 1, 3, 0, 0xF), el("COLOR", 0, 0, 3, 1, 0xF)],
attr_base: 4,
params: None,
};
(code, vs)
}

/// ISG1 elements packed into one register (TEXCOORD0 .xy and TEXCOORD1 .zw of r1) are one
/// `XclIn` member each, with their own components: the fetch shader's v10 / v11 are
/// TEXCOORD1.x / .y. One `a1` per element was a member declared twice, which DXC rejects. An
/// element alone in its register keeps `a{register}` as a 4-vector, and a component no
/// element covers reads 0.
#[test]
fn vs_packed_isg1_inputs_are_members_of_their_own() {
let (code, vs) = packed_inputs_vs(0xC);
let t = translate(&code, &Options { mode: Mode::Vertex(vs), wave64: true, ..Default::default() }).unwrap();
assert!(t.complete(), "{:?}", t.unsupported_why);
for want in [
"float4 a0 : POSITION0;",
"float2 a1_0 : TEXCOORD0;",
"float2 a1_2 : TEXCOORD1;",
"uint v4 = asuint(xi.a0[0]);",
"uint v8 = asuint(xi.a1_0[0]);",
"uint v9 = asuint(xi.a1_0[1]);",
"uint v10 = asuint(xi.a1_2[0]);",
"uint v11 = asuint(xi.a1_2[1]);",
] {
assert!(t.hlsl.contains(want), "{want}:\n{}", t.hlsl);
}
assert!(!t.hlsl.contains(" a1 :"), "{}", t.hlsl);
// TEXCOORD1 in .z alone: w of r1 is no element's
let (code, vs) = packed_inputs_vs(0x4);
let t1 = translate(&code, &Options { mode: Mode::Vertex(vs), wave64: true, ..Default::default() }).unwrap();
for want in ["float1 a1_2 : TEXCOORD1;", "uint v10 = asuint(xi.a1_2[0]);", "uint v11 = 0u;"] {
assert!(t1.hlsl.contains(want), "{want}:\n{}", t1.hlsl);
}
if super::dxc::location().is_ok() {
for h in [&t.hlsl, &t1.hlsl] {
super::dxc::compile(h, "main", "vs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{h}"));
}
}
}

#[test]
#[ignore]
fn probe_standard_swizzle_support() {
Expand Down Expand Up @@ -7141,3 +7240,45 @@ fn fspx_containers_translate_and_compile() {
println!("FSPX: {ok} / {n} translated and compiled");
assert_eq!(ok, n);
}

// Synthetic GPU probe adapted from this PR's packed-input test.
#[test]
fn gpu_vs_interleaved_isg1_input_masks() {
if !gpu_ready() {
return;
}
use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R32G32_FLOAT, DXGI_FORMAT_R32G32B32_FLOAT, DXGI_FORMAT_R32_FLOAT};
let (vs_code, mut vsi) = packed_inputs_vs(0x2);
vsi.inputs[1].mask = 0x5;
vsi.inputs[1].rw_mask = 0x5;
let pass_ps = prog(&[
("V_INTERP_P1_F32", "ENC_VINTRP", &[("VDST", 4), ("VSRC", 0), ("ATTR", 0), ("ATTRCHAN", 0)], None),
("V_INTERP_P2_F32", "ENC_VINTRP", &[("VDST", 4), ("VSRC", 1), ("ATTR", 0), ("ATTRCHAN", 0)], None),
("V_INTERP_P1_F32", "ENC_VINTRP", &[("VDST", 5), ("VSRC", 0), ("ATTR", 0), ("ATTRCHAN", 1)], None),
("V_INTERP_P2_F32", "ENC_VINTRP", &[("VDST", 5), ("VSRC", 1), ("ATTR", 0), ("ATTRCHAN", 1)], None),
("V_INTERP_P1_F32", "ENC_VINTRP", &[("VDST", 6), ("VSRC", 0), ("ATTR", 0), ("ATTRCHAN", 2)], None),
("V_INTERP_P2_F32", "ENC_VINTRP", &[("VDST", 6), ("VSRC", 1), ("ATTR", 0), ("ATTRCHAN", 2)], None),
("V_INTERP_P1_F32", "ENC_VINTRP", &[("VDST", 7), ("VSRC", 0), ("ATTR", 0), ("ATTRCHAN", 3)], None),
("V_INTERP_P2_F32", "ENC_VINTRP", &[("VDST", 7), ("VSRC", 1), ("ATTR", 0), ("ATTRCHAN", 3)], None),
("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 4), ("VSRC1", 5), ("VSRC2", 6), ("VSRC3", 7), ("DONE", 1), ("VM", 1)], None),
]);
let compile = |code: &[u8], mode: Mode, profile: &str| {
let t = translate(code, &runner::options(mode)).unwrap();
assert!(t.complete(), "{:?}", t.unsupported_why);
super::dxc::compile(&t.hlsl, "main", profile, &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl))
};
let vs = compile(&vs_code, Mode::Vertex(vsi), "vs_6_6");
let ps = compile(&pass_ps, Mode::Pixel(draw_psi()), "ps_6_6");
let (t0, t1) = ([0.25f32, -0.4, 0.75], [0.5f32]);
let verts: Vec<f32> = [[-1.0f32, -1.0], [-1.0, 3.0], [3.0, -1.0]].iter().flat_map(|p| [p[0], p[1], t0[0], t0[1], t0[2], t1[0]]).collect();
let layout = [
runner::VertexElement { semantic: "POSITION", index: 0, format: DXGI_FORMAT_R32G32_FLOAT, offset: 0 },
runner::VertexElement { semantic: "TEXCOORD", index: 0, format: DXGI_FORMAT_R32G32B32_FLOAT, offset: 8 },
runner::VertexElement { semantic: "TEXCOORD", index: 1, format: DXGI_FORMAT_R32_FLOAT, offset: 20 },
];
let px = runner::draw(&vs, &ps, &layout, &verts, 24, &[], &[], 16, [-1.0; 4]).unwrap();
let want = [t0[0], t1[0], t0[2], 0.0];
for (i, got) in px.iter().enumerate() {
assert!(got.iter().zip(want).all(|(a, b)| (a - b).abs() < 1e-6), "pixel ({},{}) = {got:?}, expected {want:?}", i % 16, i / 16);
}
}
Loading