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
380 changes: 379 additions & 1 deletion crates/xcl/src/gpu/dxil/core_tests.rs

Large diffs are not rendered by default.

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 @@ -1322,7 +1322,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, vok, pok, outputs: ms.outputs.clone() });
lw.mesh = Some(super::lower::MeshCtx { rec, vtx, prm, vok, pok, 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 @@ -78,6 +78,8 @@ pub struct MeshCtx {
pub vok: Value,
pub pok: 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
82 changes: 70 additions & 12 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::{CastOp, 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_layout, 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,9 @@ 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;
// a float16 input (packed or one half per dword) has the mode its reads give it -
// flat where `v_interp_mov` reads it; integers are flat
let float = matches!(e.component_type, 3 | 6);
let interp = if !float {
Interp::Constant
} else {
Expand Down Expand Up @@ -326,15 +328,24 @@ 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 {
// (a float16 param's layout checked against the dwords the export writes)
let dwords = (0..4).filter(|&c| enabled(c)).fold(0u32, |m, c| m | 1 << c);
let (at, layout) = out_param_layout(&vs.outputs, vs.params.as_deref(), k, dwords);
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);
// float16: component c is the float of its half (`ParamLayout::place`)
let (dw, half) = layout.place(first, c);
if dw < 4 && enabled(dw) {
let mut v = comp(self, dw);
if let Some(h) = half {
let h = self.lshrc(v, 16 * h);
let x = self.s.f16_to_f32(h);
v = self.bits(x);
}
self.set_out(vars[c], v);
}
}
Expand Down Expand Up @@ -401,11 +412,35 @@ 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") };
let load = |lw: &mut Lw| -> (Value, u32) {
// 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) => {
let high = d.enc.name != "ENC_VINTRP" && f("SRC0") >> 8 & 1 == 1;
if name == "v_interp_p2_f16" {
crate::gpu::recomp::note_ps_f16_read(&ps, attr as usize, chan as usize, high);
}
ps_f16_slot(&ps, &regs, chan as usize, high)
}
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"),
},
};
// `v_interp_mov_f32` of a float16 attribute, whose halves reach the PS as their floats
// (`recomp::out_param_layout`): the half of channel `chan`'s float in the low bits, as the
// title's `v_cvt_f32_f16` takes it - both halves of the dword where the record names a
// second register (packed)
let f16_mov = match ps_attribute_f16(&ps, attr as usize).filter(|_| name == "v_interp_mov_f32") {
Some(regs) if ps.attrs.as_ref().and_then(|t| t.get(attr as usize)).is_some_and(|a| a.pair.is_some()) => Some([ps_f16_slot(&ps, &regs, chan as usize, false), ps_f16_slot(&ps, &regs, chan as usize, true)]),
Some(_) => Some([place, None]),
None => None,
};
let load_at = |lw: &mut Lw, at: Option<(usize, usize)>| -> (Value, u32) {
let Some((slot, comp)) = at else { return (lw.fc(0.0), 3) };
let g = lw.gfx.as_mut().expect("gfx");
let (sig, ct) = g.ps_in[slot];
(g.io.load_input(&mut lw.s, sig, 0, chan as u8), ct)
(g.io.load_input(&mut lw.s, sig, 0, comp as u8), ct)
};
let as_f32 = |lw: &mut Lw, x: Value, ct: u32| -> Value {
let f32 = lw.s.m.f32();
Expand All @@ -415,6 +450,7 @@ impl Lw {
_ => x,
}
};
let load = |lw: &mut Lw| load_at(lw, place);
let dst = super::lower::Dst::V(vdst);
match name {
"v_interp_p1_f32" | "v_interp_p1ll_f16" | "v_interp_p1lv_f16" => {}
Expand All @@ -428,8 +464,30 @@ impl Lw {
if ij != 2 {
return err(d, "v_interp_mov p10/p20");
}
let (x, _) = load(self);
let v = self.bits(x);
let v = match f16_mov {
None => {
let (x, _) = load(self);
self.bits(x)
}
Some([lo, hi]) => {
let half = |lw: &mut Lw, at: Option<(usize, usize)>| -> Value {
if at.is_none() {
return lw.c(0);
}
let (x, ct) = load_at(lw, at);
let xf = as_f32(lw, x, ct);
lw.to_f16(xf)
};
let l = half(self, lo);
if hi.is_some() {
let h = half(self, hi);
let hs = self.shlc(h, 16);
self.or(l, hs)
} else {
l
}
}
};
self.write32(&dst, v);
}
"v_interp_p2_f16" => {
Expand Down
22 changes: 17 additions & 5 deletions crates/xcl/src/gpu/dxil/lower_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2697,13 +2697,25 @@ 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 {
// (a float16 param's layout checked against the dwords the export writes)
let dwords = (0..4).filter(|&c| enabled(c)).fold(0u32, |m, c| m | 1 << c);
let (at, layout) = {
let m = self.mesh.as_ref().expect("mesh");
crate::gpu::recomp::out_param_layout(&m.outputs, m.params.as_deref(), k, dwords)
};
for (j, first) in at {
for c in 0..4 {
if enabled(c) {
// float16: component c is the float of its half (`ParamLayout::place`);
// the record keeps it as a float
let (dw, half) = layout.place(first, 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 let Some(h) = half {
let h = self.lshrc(v, 16 * h);
let x = self.s.f16_to_f32(h);
v = self.bits(x);
}
st.push((a, v));
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/dxil/robustness_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn a_group_barrier_of_several_hardware_waves_uses_safe_barrier_directly() {
// (the kernel form of the tests is no thread group of a title's: its barrier stays)
kernel::translate(&code, o(true, true)).expect("kernel");
// a mesh program's compute pass: two console waves a group, as a retail title's
let ms = |threads| crate::gpu::recomp::MsInterface { outputs: vec![el("SV_Position", 0, 1, 3, 0, 0xF)], threads, max_vertices: 64, max_primitives: 64 };
let ms = |threads| crate::gpu::recomp::MsInterface { outputs: vec![el("SV_Position", 0, 1, 3, 0, 0xF)], threads, max_vertices: 64, max_primitives: 64, params: None };
for split in [true, false] {
let k = kernel::translate_mesh(&code, o(true, split), &ms(128))
.unwrap_or_else(|e| panic!("mesh pass failed (split {split}): {e}"));
Expand Down
78 changes: 63 additions & 15 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::{out_param_layout, ps_attribute_f16, ps_attribute_slot, ps_f16_slot, ps_input_vgpr, ps_slot_attribute, ranked, 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 @@ -2353,6 +2353,8 @@ impl Em {
let done = rdna2::field_value(d.enc, &d.words, "DONE") == Some(1);
let vm = rdna2::field_value(d.enc, &d.words, "VM") == Some(1);
let enabled = |c: usize| if compr { en >> (c / 2 * 2) & 3 != 0 } else { en >> c & 1 == 1 };
// (the dwords the export writes: a float16 param's layout is checked against them)
let dwords = (0..4).filter(|&c| enabled(c)).fold(0u32, |m, c| m | 1 << c);
let mut st = Vec::new();
match (&self.opt.mode, tgt) {
(Mode::Vertex(_), 12) => {
Expand Down Expand Up @@ -2380,18 +2382,26 @@ impl Em {
// param N is a register: channel c goes to component c of every element there
let k = (tgt - 32) as usize;
let params = ranked(&vs.outputs, true);
let at = vs_param_register(vs, k).map(|r| elements_at(&vs.outputs, true, r)).unwrap_or_default();
let (at, layout) = out_param_layout(&vs.outputs, vs.params.as_deref(), k, dwords);
if at.is_empty() {
self.line("/* param without an OSG1 element (dummy) */");
return Ok(());
}
for j in at {
for (j, first) in at {
let e = params[j];
let n = out_comps(e);
for c in 0..n {
if enabled(c) {
let v = Self::exp_comp(srcs, compr, c);
let v = if e.component_type == 3 { format!("asfloat({v})") } else { format!("{v}") };
// float16: component c is the float of its half (`ParamLayout::place`)
let (dw, half) = layout.place(first, c);
if dw < 4 && enabled(dw) {
let v = Self::exp_comp(srcs, compr, dw);
// (else the dword's bits: assigned to a float output the uint was
// converted as a number - PR 2513's thread, 2026-11-29 13:44Z)
let v = match half {
Some(h) => format!("f16tof32({v} >> {}u)", 16 * h),
None if matches!(e.component_type, 1 | 2) => v,
None => format!("asfloat({v})"),
};
let comp = if n == 1 { String::new() } else { format!("[{c}]") };
st.push(format!("xo.p{j}{comp} = {v};"));
}
Expand Down Expand Up @@ -2442,15 +2452,22 @@ impl Em {
// param N is a register: its record slot is that of every element there (the
// pass-through reads an element's slot as the whole register, as for the GS)
let k = (tgt - 32) as usize;
let at = param_registers(&ms.outputs, true).get(k).map(|&r| elements_at(&ms.outputs, true, r)).unwrap_or_default();
let (at, layout) = out_param_layout(&ms.outputs, ms.params.as_deref(), k, dwords);
if at.is_empty() {
self.line("/* param without an OSG1 element (dummy) */");
return Ok(());
}
for j in at {
for (j, first) in at {
for c in 0..4 {
if enabled(c) {
st.push(format!("if (xcl_vok) res.Store((xcl_vtx + {}u) * 4u, {});", 4 * (j + 1) + c, Self::exp_comp(srcs, compr, c)));
// float16 (as in the VS): the record keeps each half as a float
let (dw, half) = layout.place(first, c);
if dw < 4 && enabled(dw) {
let v = Self::exp_comp(srcs, compr, dw);
let v = match half {
Some(h) => format!("asuint(f16tof32({v} >> {}u))", 16 * h),
None => v,
};
st.push(format!("if (xcl_vok) res.Store((xcl_vtx + {}u) * 4u, {v});", 4 * (j + 1) + c));
}
}
}
Expand Down Expand Up @@ -2491,10 +2508,38 @@ impl Em {
fn interp(&mut self, d: &Decoded, name: &str) -> Result<(), &'static str> {
let Mode::Pixel(ps) = &self.opt.mode else { return Err("interpolation outside PS") };
let (vdst, ij, attr, chan) = Self::interp_fields(d);
let Some(slot) = ps_attribute_slot(ps, attr as usize, chan as usize) else {
return Err("attribute outside ISG1");
// a float16-packed attribute (two registers, two components per dword): the PC input
// is the component itself, interpolated as a float – SRC0 bit 8 picks the high half
let high = d.enc.name != "ENC_VINTRP" && rdna2::field_value(d.enc, &d.words, "SRC0").unwrap_or(0) >> 8 & 1 == 1;
let src = match ps_attribute_f16(ps, attr as usize).filter(|_| name.ends_with("_f16")) {
Some(regs) => {
if name == "v_interp_p2_f16" {
super::note_ps_f16_read(ps, attr as usize, chan as usize, high);
}
match ps_f16_slot(ps, &regs, chan as usize, high) {
Some((slot, comp)) => format!("xi.a{slot}[{comp}]"),
None => "0.0f".to_string(),
}
}
None => {
let Some(slot) = ps_attribute_slot(ps, attr as usize, chan as usize) else {
return Err("attribute outside ISG1");
};
format!("xi.a{slot}[{chan}]")
}
};
// `v_interp_mov_f32` of a float16 attribute, whose halves reach the PS as their floats
// (`out_param_layout`): the half of channel `chan`'s float in the low bits, as the title's
// `v_cvt_f32_f16` takes it - both halves of the dword where the record names a second
// register (packed)
let f16_word = match ps_attribute_f16(ps, attr as usize).filter(|_| name == "v_interp_mov_f32") {
Some(regs) if ps.attrs.as_ref().and_then(|t| t.get(attr as usize)).is_some_and(|a| a.pair.is_some()) => {
let half = |high: bool| ps_f16_slot(ps, &regs, chan as usize, high).map(|(s, c)| to_f16_expr(self.dynamic_f16_rm, &format!("(float)xi.a{s}[{c}]")));
Some(format!("({} | ({} << 16))", half(false).unwrap_or_else(|| "0u".into()), half(true).unwrap_or_else(|| "0u".into())))
}
Some(_) => Some(to_f16_expr(self.dynamic_f16_rm, &format!("(float){src}"))),
None => None,
};
let src = format!("xi.a{slot}[{chan}]");
let act = self.act();
match name {
"v_interp_p1_f32" | "v_interp_p1ll_f16" | "v_interp_p1lv_f16" => {
Expand All @@ -2511,7 +2556,8 @@ impl Em {
}
let v = self.vreg(vdst);
// P0 loads the parameter word, including integer selector bits (#249).
self.line(&format!("{v} = select({act}, asuint({src}), {v});"));
let word = f16_word.unwrap_or_else(|| format!("asuint({src})"));
self.line(&format!("{v} = select({act}, {word}, {v});"));
}
"v_interp_p2_f16" => {
let v = self.vreg(vdst);
Expand Down Expand Up @@ -5577,7 +5623,9 @@ static uint xcl_cap = 1u << 16;
// the mode of the attribute this input is read through (none: never read)
let attr = ps_slot_attribute(ps, k);
let mut m = attr.and_then(|a| self.ps_modes.get(&(a as u32))).cloned().unwrap_or_default();
if e.component_type != 3 {
// a float16 input (packed or one half per dword) has the mode its reads give
// it - flat where `v_interp_mov` reads it; integers are flat
if !matches!(e.component_type, 3 | 6) {
m = "nointerpolation".into();
}
let _ = writeln!(h, " {m} {}4 a{k} : {};", hlsl_type(e), e.semantic());
Expand Down
Loading
Loading