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
124 changes: 124 additions & 0 deletions crates/xcl/src/gpu/dxil/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,130 @@ fn direct_and_hlsl_graphics_stages_link() {
assert_eq!(draw(&hvs, &dps), want, "HLSL VS + direct PS");
}

/// Float16 interpolants packed two per dword, two registers in one attribute (TEXCOORD1.xyz
/// and TEXCOORD4.xyzw in attribute 0, as a retail title's world shaders have them): the PS
/// reads each half with `v_interp_p2_f16` (SRC0 bit 8: the high half) and gets that component
/// of its own element interpolated across the triangle; the float attribute after the pair
/// keeps its register. The console VS exports the packed dwords as param 0 and unpacks them
/// into the same components. Every combination of the direct and the HLSL stages, and a PC VS.
/// Before: the tables were refused, the PS converted the raw packed dword of a
/// `nointerpolation` input and attribute 1 was read from TEXCOORD4.
#[test]
fn f16_packed_attributes_interpolate_per_half() {
use crate::gpu::dxbc::SigElement as S;
use crate::gpu::recomp::{runner, translate, Mode, Options, OutParam, PsAttr, PsInterface, VsInterface};
use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R32G32B32A32_FLOAT, DXGI_FORMAT_R32G32_FLOAT};
if !super::graphics::tests::draw_ready() {
return;
}
if crate::gpu::recomp::dxc::location().is_err() {
return runner::skip_test("no DXC");
}
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;
// COLOR c in v8..v11 -> halves h0..h3 in v12..v15; param 0 = (h0 | h1 << 16, h2, h3 | h0 << 16,
// h1 | h2 << 16): TEXCOORD1 = (c0, c1, c2), TEXCOORD4 = (c3, c0, c1, c2); param 1 = c reversed
let mut vs_ops: Vec<(&str, &str, Vec<(&str, u64)>, Option<u32>)> = vec![
("V_MOV_B32", "ENC_VOP1", vec![("VDST", 0), ("SRC0", V + 4)], None),
("V_MOV_B32", "ENC_VOP1", vec![("VDST", 1), ("SRC0", V + 5)], None),
("V_MOV_B32", "ENC_VOP1", vec![("VDST", 2), ("SRC0", 128)], None),
("V_MOV_B32", "ENC_VOP1", vec![("VDST", 3), ("SRC0", 242)], None),
("EXP", "ENC_EXP", vec![("TGT", 12), ("EN", 0xF), ("VSRC0", 0), ("VSRC1", 1), ("VSRC2", 2), ("VSRC3", 3), ("DONE", 1)], None),
];
for i in 0..4u64 {
vs_ops.push(("V_CVT_F16_F32", "ENC_VOP1", vec![("VDST", 12 + i), ("SRC0", V + 8 + i)], None));
}
for (dst, lo, hi) in [(16u64, 12u64, 13u64), (18, 15, 12), (19, 13, 14)] {
vs_ops.push(("V_PACK_B32_F16", "ENC_VOP3", vec![("VDST", dst), ("SRC0", V + lo), ("SRC1", V + hi)], None));
}
vs_ops.push(("EXP", "ENC_EXP", vec![("TGT", 32), ("EN", 0xF), ("VSRC0", 16), ("VSRC1", 14), ("VSRC2", 18), ("VSRC3", 19)], None));
vs_ops.push(("EXP", "ENC_EXP", vec![("TGT", 33), ("EN", 0xF), ("VSRC0", 11), ("VSRC1", 10), ("VSRC2", 9), ("VSRC3", 8)], None));
let vs_list: Vec<(&str, &str, &[(&str, u64)], Option<u32>)> = vs_ops.iter().map(|(n, e, f, l)| (*n, *e, f.as_slice(), *l)).collect();
let vs_code = prog(&vs_list);
let user = vec![el("TEXCOORD", 1, 0, 6, 1, 0x7), el("TEXCOORD", 4, 0, 6, 2, 0xF), el("TEXCOORD", 3, 0, 3, 3, 0xF)];
let vsi = VsInterface {
inputs: vec![el("POSITION", 0, 0, 3, 0, 3), el("COLOR", 0, 0, 3, 1, 0xF)],
outputs: std::iter::once(el("SV_Position", 0, 1, 3, 0, 0xF)).chain(user.iter().cloned()).collect(),
attr_base: 4,
params: Some(vec![OutParam { register: 1, f16: true, pair: true }, OutParam { register: 3, f16: false, pair: false }]),
};
// a PC VS handing over each component as a float: COLOR c -> TEXCOORD1 = (c0, c1, c2, 0),
// TEXCOORD4 = (c3, c0, c1, c2), TEXCOORD3 = c reversed
let pc_vs = "struct I { float2 pos : POSITION; float4 c : COLOR0; };\nstruct O { float4 pos : SV_Position; float4 t1 : TEXCOORD1; float4 t4 : TEXCOORD4; float4 t3 : TEXCOORD3; };\nO main(I i) { O o; o.pos = float4(i.pos, 0.0, 1.0); o.t1 = float4(i.c.xyz, 0.0); o.t4 = float4(i.c.w, i.c.xyz); o.t3 = i.c.wzyx; return o; }\n";
let pvs = crate::gpu::recomp::dxc::compile(pc_vs, "main", "vs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{pc_vs}"));
let psi = PsInterface {
inputs: std::iter::once(el("SV_Position", 0, 1, 3, 0, 0xF)).chain(user.iter().cloned()).collect(),
outputs: vec![el("SV_Target", 0, 64, 3, 0, 0xF)],
input_ena: 0x302,
attrs: Some(vec![
PsAttr { kind: 6, register: 1, mask: 3, flat: false, f16: true, pair: Some(2) },
PsAttr { kind: 6, register: 3, mask: 0xF, flat: false, f16: false, pair: None },
]),
};
// a PS returning four values: Some((dword, high)) of attribute 0, None: attr1[k] as f32
let ps_code = |sel: [Option<(u64, u64)>; 4]| {
let mut ops: Vec<(&str, &str, Vec<(&str, u64)>, Option<u32>)> = Vec::new();
for (k, s) in sel.iter().enumerate() {
let k = k as u64;
match *s {
Some((chan, high)) => {
let attr = chan << 6 | high << 8;
ops.push(("V_INTERP_P1LL_F16", "ENC_VOP3", vec![("VDST", 8 + k), ("SRC1", V), ("SRC0", attr)], None));
ops.push(("V_INTERP_P2_F16", "ENC_VOP3", vec![("VDST", 4 + k), ("SRC1", V + 1), ("SRC0", attr), ("SRC2", V + 8 + k)], None));
ops.push(("V_CVT_F32_F16", "ENC_VOP1", vec![("VDST", 4 + k), ("SRC0", V + 4 + k)], None));
}
None => {
ops.push(("V_INTERP_P1_F32", "ENC_VINTRP", vec![("VDST", 4 + k), ("VSRC", 0), ("ATTR", 1), ("ATTRCHAN", k)], None));
ops.push(("V_INTERP_P2_F32", "ENC_VINTRP", vec![("VDST", 4 + k), ("VSRC", 1), ("ATTR", 1), ("ATTRCHAN", k)], None));
}
}
}
ops.push(("EXP", "ENC_EXP", vec![("TGT", 0), ("EN", 0xF), ("VSRC0", 4), ("VSRC1", 5), ("VSRC2", 6), ("VSRC3", 7), ("DONE", 1), ("VM", 1)], None));
let list: Vec<(&str, &str, &[(&str, u64)], Option<u32>)> = ops.iter().map(|(n, e, f, l)| (*n, *e, f.as_slice(), *l)).collect();
prog(&list)
};
let ko = KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 };
let vt = translate(&vs_code, &Options { mode: Mode::Vertex(vsi.clone()), wave64: true, ..Default::default() }).unwrap();
assert!(vt.complete(), "{:?}", vt.unsupported_why);
let hvs = crate::gpu::recomp::dxc::compile(&vt.hlsl, "main", "vs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", vt.hlsl));
let dvs = super::kernel::translate_vertex(&vs_code, ko, &vsi).unwrap().dxil;
// COLOR = (x, y, x / 4 + 1 / 2, y / 2 - 1 / 4) at each vertex: exact in float16, and linear
// in the pixel's position
let color = |x: f32, y: f32| [x, y, 0.25 * x + 0.5, 0.5 * y - 0.25];
let verts: Vec<f32> = [[-1.0f32, -1.0], [-1.0, 3.0], [3.0, -1.0]].iter().flat_map(|p| [p[0], p[1]].into_iter().chain(color(p[0], p[1]))).collect();
let layout = [
runner::VertexElement { semantic: "POSITION", index: 0, format: DXGI_FORMAT_R32G32_FLOAT, offset: 0 },
runner::VertexElement { semantic: "COLOR", index: 0, format: DXGI_FORMAT_R32G32B32A32_FLOAT, offset: 8 },
];
// what each selection reads, as indices into COLOR (4: nothing declared there, 0)
let cases: [([Option<(u64, u64)>; 4], [usize; 4], &str); 4] = [
([Some((0, 0)), Some((0, 1)), Some((1, 0)), Some((2, 0))], [0, 1, 2, 3], "TEXCOORD1.xyz, TEXCOORD4.x"),
([Some((2, 1)), Some((3, 0)), Some((3, 1)), Some((1, 1))], [0, 1, 2, 4], "TEXCOORD4.yzw, TEXCOORD1.w"),
([Some((3, 1)), Some((2, 0)), Some((0, 1)), Some((0, 0))], [2, 3, 1, 0], "both registers, mixed halves"),
([None, None, None, None], [3, 2, 1, 0], "the float attribute after the pair"),
];
for (sel, want, what) in cases {
let code = ps_code(sel);
let pt = translate(&code, &Options { mode: Mode::Pixel(psi.clone()), wave64: true, ..Default::default() }).unwrap();
assert!(pt.complete(), "{what}: {:?}", pt.unsupported_why);
assert!(!pt.hlsl.contains("nointerpolation"), "{what}: the float16 inputs interpolate\n{}", pt.hlsl);
let hps = crate::gpu::recomp::dxc::compile(&pt.hlsl, "main", "ps_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", pt.hlsl));
let dps = super::kernel::translate_pixel(&code, ko, &psi).unwrap().dxil;
let pairs = [("PC VS + HLSL PS", &pvs, &hps), ("PC VS + direct PS", &pvs, &dps), ("HLSL VS + HLSL PS", &hvs, &hps), ("direct VS + direct PS", &dvs, &dps), ("direct VS + HLSL PS", &dvs, &hps), ("HLSL VS + direct PS", &hvs, &dps)];
for (stages, vs, ps) in pairs {
let px = runner::draw(vs, ps, &layout, &verts, 24, &[], &[], 8, [9.0; 4]).unwrap();
for y in 0..8usize {
for x in 0..8usize {
let c = color((x as f32 + 0.5) / 4.0 - 1.0, 1.0 - (y as f32 + 0.5) / 4.0);
let expected = want.map(|k| if k == 4 { 0.0 } else { c[k] });
let got = px[y * 8 + x];
assert!(got.iter().zip(expected).all(|(a, b)| (a - b).abs() < 1e-3), "{what}, {stages}: pixel ({x},{y}) = {got:?}, expected {expected:?}");
}
}
}
}
}

fn sig(name: &str, index: u32, sv: u32, ct: u32, reg: u32, mask: u8) -> crate::gpu::dxbc::SigElement {
crate::gpu::dxbc::SigElement { name: name.into(), index, system_value: sv, component_type: ct, register: reg, mask, rw_mask: mask, stream: 0 }
}
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ pub fn mesh_init(lw: &mut Lw, ms: &MsInterface, tid: Value, io: super::shader::R
let mut vgpr = HashMap::new();
vgpr.insert(5, v5);
lw.init.compute = Some(ComputeInit { sgpr, vgpr, zero });
lw.mesh = Some(super::lower::MeshCtx { rec, vtx, prm, outputs: ms.outputs.clone() });
lw.mesh = Some(super::lower::MeshCtx { rec, vtx, prm, outputs: ms.outputs.clone(), params: ms.params.clone() });
}

/// EXEC at entry: the console wave's threads (static per wave in the split form: no
Expand Down
2 changes: 2 additions & 0 deletions crates/xcl/src/gpu/dxil/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub struct MeshCtx {
pub vtx: Value,
pub prm: Value,
pub outputs: Vec<crate::gpu::dxbc::SigElement>,
/// the console's output table (`MsInterface::params`)
pub params: Option<Vec<crate::gpu::recomp::OutParam>>,
}

/// Initial register values: loaded from `io` in the entry block on first use.
Expand Down
35 changes: 26 additions & 9 deletions crates/xcl/src/gpu/dxil/lower_gfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::lower::{err, LResult, Lw};
use super::module::{Pred, Value};
use crate::gpu::dxbc::SigElement;
use crate::gpu::rdna2::{self, Decoded};
use crate::gpu::recomp::{elements_at, ps_attribute_slot, ps_input_vgpr, ps_slot_attribute, ranked, vs_param_register, PsInterface, VsInterface};
use crate::gpu::recomp::{out_param_elements, ps_attribute_f16, ps_attribute_slot, ps_f16_slot, ps_input_vgpr, ps_slot_attribute, ranked, PsInterface, VsInterface};

/// An output element: its signature handle, components and the temporaries holding them.
pub struct GfxOut {
Expand Down Expand Up @@ -119,7 +119,7 @@ impl Lw {
_ => SemKind::Arbitrary,
};
let s = io.output(GSig::new(&e.name, e.index, kind, gct(e.component_type), n));
outs.push((s, n, e.component_type == 3));
outs.push((s, n, matches!(e.component_type, 3 | 6)));
}
// initial VGPRs: v0 = vertex id, v3 = instance id, the fetch shader's attribute VGPRs
let mut vgpr = HashMap::new();
Expand Down Expand Up @@ -166,7 +166,8 @@ impl Lw {
for (k, e) in ranked(&ps.inputs, false).iter().enumerate() {
let attr = ps_slot_attribute(ps, k);
let m = attr.and_then(|a| modes.get(&(a as u32))).cloned().unwrap_or_default();
let float = e.component_type == 3;
// float16 read through a packed attribute interpolates like a float
let float = e.component_type == 3 || (e.component_type == 6 && attr.is_some_and(|a| ps_attribute_f16(ps, a).is_some()));
let interp = if !float {
Interp::Constant
} else {
Expand Down Expand Up @@ -326,15 +327,22 @@ impl Lw {
32..=63 => {
let vs = self.gfx().vs.clone().expect("vs");
let k = (tgt - 32) as usize;
let at = vs_param_register(&vs, k).map(|r| elements_at(&vs.outputs, true, r)).unwrap_or_default();
for j in at {
let (at, f16) = out_param_elements(&vs.outputs, vs.params.as_deref(), k);
for (j, first) in at {
let (vars, n) = {
let o = &self.gfx().params[j];
(o.vars.clone(), o.comps)
};
for c in 0..n as usize {
if enabled(c) {
let v = comp(self, c);
// packed float16: component c is half c & 1 of dword first + c / 2
let dw = if f16 { first + c / 2 } else { c };
if dw < 4 && enabled(dw) {
let mut v = comp(self, dw);
if f16 {
let h = self.lshrc(v, 16 * (c as u32 & 1));
let x = self.s.f16_to_f32(h);
v = self.bits(x);
}
self.set_out(vars[c], v);
}
}
Expand Down Expand Up @@ -401,11 +409,20 @@ impl Lw {
let s1 = f("SRC1");
(f("VDST"), if s1 >= 256 { s1 - 256 } else { s1 }, f("SRC0") & 0x3F, f("SRC0") >> 6 & 3)
};
let Some(slot) = ps_attribute_slot(&ps, attr as usize, chan as usize) else { return err(d, "attribute outside ISG1") };
// a float16-packed attribute: the PC input is the component itself (SRC0 bit 8: the
// high half of the dword); a half nothing declares reads 0
let place = match ps_attribute_f16(&ps, attr as usize).filter(|_| name.ends_with("_f16")) {
Some(regs) => ps_f16_slot(&ps, &regs, chan as usize, d.enc.name != "ENC_VINTRP" && f("SRC0") >> 8 & 1 == 1),
None => match ps_attribute_slot(&ps, attr as usize, chan as usize) {
Some(slot) => Some((slot, chan as usize)),
None => return err(d, "attribute outside ISG1"),
},
};
let load = |lw: &mut Lw| -> Value {
let Some((slot, comp)) = place else { return lw.c(0) };
let g = lw.gfx.as_mut().expect("gfx");
let (sig, _) = g.ps_in[slot];
let x = g.io.load_input(&mut lw.s, sig, 0, chan as u8);
let x = g.io.load_input(&mut lw.s, sig, 0, comp as u8);
let i32 = lw.s.m.i32();
lw.s.m.bitcast(x, i32)
};
Expand Down
20 changes: 15 additions & 5 deletions crates/xcl/src/gpu/dxil/lower_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2535,13 +2535,23 @@ impl Lw {
13..=15 | 9 => {}
32..=63 => {
let k = (tgt - 32) as usize;
let outputs = self.mesh.as_ref().expect("mesh").outputs.clone();
let at = crate::gpu::recomp::param_registers(&outputs, true).get(k).map(|&r| crate::gpu::recomp::elements_at(&outputs, true, r)).unwrap_or_default();
for j in at {
let (at, f16) = {
let m = self.mesh.as_ref().expect("mesh");
crate::gpu::recomp::out_param_elements(&m.outputs, m.params.as_deref(), k)
};
for (j, first) in at {
for c in 0..4 {
if enabled(c) {
// packed float16: component c is half c & 1 of dword first + c / 2; the
// record keeps it as a float
let dw = if f16 { first + c / 2 } else { c };
if dw < 4 && enabled(dw) {
let a = self.addc(vtx, (4 * (j + 1) + c) as u32);
let v = comp(self, c);
let mut v = comp(self, dw);
if f16 {
let h = self.lshrc(v, 16 * (c as u32 & 1));
let x = self.s.f16_to_f32(h);
v = self.bits(x);
}
st.push((a, v));
}
}
Expand Down
Loading
Loading