Skip to content
Merged
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
77 changes: 77 additions & 0 deletions crates/xcl/src/gpu/dxil/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,80 @@ fn graphics_stages_with_a_branch_before_their_exports() {
assert_eq!(draw(&dvs, &dps, 0), cleared, "branch not taken");
assert_eq!(draw(&dvs, &dps, 1), kept, "branch taken");
}

/// Exhaustive bit-exact check of the IEEE-754 bit-manipulation formula used by
/// `lower_mem::uf_small_bits` for all 10-bit (mb=5) and 11-bit (mb=6) float inputs.
#[test]
fn uf_small_ieee_bit_math_matches_exact_formula() {
for mb in [5u32, 6u32] {
let p = 1u32 << mb;
let mmask = p - 1;
let mshift = 23 - mb;
let den_scale = f32::from_bits(if mb == 6 { 0x3580_0000 } else { 0x3600_0000 });
for v in 0..(1u32 << (mb + 5)) {
let e = v >> mb;
let m = v & mmask;
// Reference formula (matching HLSL `uf_small`)
let fm = (m as f32) / (p as f32);
let ref_f = if e == 31 {
f32::from_bits(if m != 0 { 0x7FC0_0000 } else { 0x7F80_0000 })
} else if e == 0 {
fm * (-14.0f32).exp2()
} else {
((e as f32) - 15.0).exp2() * (1.0 + fm)
};
// Exact IEEE bit math
let got_bits = if e == 31 {
if m != 0 { 0x7FC0_0000 } else { 0x7F80_0000 }
} else if e == 0 {
((m as f32) * den_scale).to_bits()
} else {
((e + 112) << 23) | (m << mshift)
};
assert_eq!(got_bits, ref_f.to_bits(), "mb={mb} v={v:#x} (e={e} m={m})");
}
}
}

/// Multi-window switch dispatch, vector rawBufferLoad/Store, hoisted FormatLoad
/// descriptors, single/multi-component FormatLoad, and hoisted BVH loads all pass
/// DXIL validation across 1..=4 memory windows and wave configurations.
#[test]
fn multi_window_mem_format_and_bvh_validate() {
let code = prog(&[
// Load descriptor s[12:15] once, then reuse across repeated BUFFER_LOAD_FORMAT_X
("S_LOAD_DWORDX4", "ENC_SMEM", &[("SDATA", 12), ("SBASE", 0), ("OFFSET", 0x40), ("SOFFSET", 125)], None),
("S_LOAD_DWORDX8", "ENC_SMEM", &[("SDATA", 16), ("SBASE", 0), ("OFFSET", 0x80), ("SOFFSET", 125)], None),
("S_BUFFER_LOAD_DWORDX4", "ENC_SMEM", &[("SDATA", 24), ("SBASE", 12), ("OFFSET", 0x10), ("SOFFSET", 125)], None),
// Divergent EXEC so lane guards are active (non-constant `act`)
("V_CMPX_LT_U32", "ENC_VOPC", &[("SRC0", 160), ("VSRC1", 0)], None),
// Repeated single-component format loads sharing s[12:15] (exercises hoisted FmtComp1 + Load2)
("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", &[("VDATA", 4), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("IDXEN", 1)], None),
("BUFFER_LOAD_FORMAT_X", "ENC_MUBUF", &[("VDATA", 5), ("VADDR", 1), ("SRSRC", 3), ("SOFFSET", 128), ("IDXEN", 1)], None),
// Sub-4 and full-4 component format loads + D16 + static MTBUF + small byte load
("BUFFER_LOAD_FORMAT_XY", "ENC_MUBUF", &[("VDATA", 6), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("IDXEN", 1)], None),
("BUFFER_LOAD_FORMAT_XYZW", "ENC_MUBUF", &[("VDATA", 8), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("IDXEN", 1)], None),
("BUFFER_LOAD_FORMAT_D16_X", "ENC_MUBUF", &[("VDATA", 12), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("IDXEN", 1)], None),
("BUFFER_LOAD_FORMAT_D16_HI_X", "ENC_MUBUF", &[("VDATA", 12), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("IDXEN", 1)], None),
("TBUFFER_LOAD_FORMAT_X", "ENC_MTBUF", &[("VDATA", 13), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("FORMAT", 28), ("IDXEN", 1)], None),
("BUFFER_LOAD_UBYTE", "ENC_MUBUF", &[("VDATA", 14), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("IDXEN", 1)], None),
// Vector MUBUF & FLAT loads/stores
("BUFFER_LOAD_DWORDX4", "ENC_MUBUF", &[("VDATA", 16), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("IDXEN", 1)], None),
("BUFFER_STORE_DWORDX4", "ENC_MUBUF", &[("VDATA", 16), ("VADDR", 0), ("SRSRC", 3), ("SOFFSET", 128), ("IDXEN", 1)], None),
("GLOBAL_LOAD_DWORDX4", "ENC_FLAT_GLBL", &[("VDST", 20), ("ADDR", 0), ("SADDR", 0x7D), ("OFFSET", 0)], None),
("GLOBAL_STORE_DWORDX4", "ENC_FLAT_GLBL", &[("DATA", 20), ("ADDR", 0), ("SADDR", 0x7D), ("OFFSET", 16)], None),
("GLOBAL_LOAD_USHORT", "ENC_FLAT_GLBL", &[("VDST", 24), ("ADDR", 0), ("SADDR", 0x7D), ("OFFSET", 4)], None),
// BVH node intersection (box32 / box16 / tri hoisted vector loads)
("IMAGE_BVH_INTERSECT_RAY", "ENC_MIMG", &[("VDATA", 28), ("VADDR", 0), ("SRSRC", 3), ("DMASK", 0xF)], None),
]);
for windows in [1u32, 2, 3, 4] {
for (wave64, split) in [(true, false), (false, false)] {
translate(&code, KernelOptions { wave64, split, mem_windows: windows, sampler_base: 0 })
.unwrap_or_else(|e| panic!("w={windows} wave64={wave64}: {}", &e[..e.len().min(800)]));
if windows > 1 {
translate(&code, KernelOptions { wave64, split, mem_windows: windows | crate::gpu::recomp::HEAP_WINDOWS, sampler_base: 0 })
.unwrap_or_else(|e| panic!("heap w={windows} wave64={wave64}: {}", &e[..e.len().min(800)]));
}
}
}
}
5 changes: 4 additions & 1 deletion crates/xcl/src/gpu/dxil/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct KernelOptions {
/// Version of the direct translator's output, part of the on-disk translation cache's
/// identity (`recomp::pso`): bump it on any change that alters the emitted containers
/// (the `gpu/dxil` sources are not in build.rs's translator source hash).
pub const TRANSLATOR_VERSION: u32 = 5;
pub const TRANSLATOR_VERSION: u32 = 6;

/// `XCL_DIRECT_DXIL=1`: console compute shaders (and the kernel tests) go through this
/// translator, with the HLSL path as the fallback for what it cannot translate yet.
Expand Down Expand Up @@ -169,6 +169,9 @@ fn translate_on_this_thread(code: &[u8], o: KernelOptions, entry: Entry) -> Resu
heap_mem,
res,
fmt_table: None,
fmt_desc_cache: HashMap::new(),
fmt_comp1_cache: HashMap::new(),
uma_base_val: None,
ntmp: 0,
scratch: None,
static_samplers: Default::default(),
Expand Down
6 changes: 6 additions & 0 deletions crates/xcl/src/gpu/dxil/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ pub struct Lw {
pub res: Option<RawUav>,
/// the buffer format table (`FMT_INFO`), created on first use
pub fmt_table: Option<Value>,
/// cached decoded format descriptors hoisted to `d3`'s defining block
pub fmt_desc_cache: HashMap<(Value, Option<u32>), super::lower_mem::FmtDesc>,
/// cached single-component format decode parameters hoisted to `d3`'s defining block
pub fmt_comp1_cache: HashMap<(Value, Option<u32>), super::lower_mem::FmtComp1>,
/// cached `IO_UMA_BASE_HI` loaded once in the entry block for multi-window address math
pub uma_base_val: Option<Value>,
/// temporaries handed out (`lower_mem::VAR_TMP`)
pub ntmp: u32,
/// the lane's scratch array (an alloca in the entry block)
Expand Down
71 changes: 39 additions & 32 deletions crates/xcl/src/gpu/dxil/lower_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ impl Lw {
let f32 = self.s.m.f32();
self.s.op(55, "dot3", Some(f32), f32, &[a[0], a[1], a[2], b[0], b[1], b[2]], super::module::FnAttrs::ReadNone)
}
fn load3f(&mut self, at: Value) -> [Value; 3] {
let mut r = [at; 3];
for (k, slot) in r.iter_mut().enumerate() {
let a = self.addc(at, 4 * k as u32);
let v = self.mem_ld(a);
*slot = self.f(v);
}
r
}

/// `bvh_intersect`: one BVH node → 4 result dwords (written to temporaries).
#[allow(clippy::too_many_arguments)]
Expand All @@ -52,27 +43,34 @@ impl Lw {
for box32 in [true, false] {
let is = self.cmpc(Pred::Eq, ty, if box32 { 5 } else { 4 });
self.guarded(is, &mut |lw| {
let mut child = [ones; 4];
for (k, c) in child.iter_mut().enumerate() {
let a = lw.addc(at, 4 * k as u32);
*c = lw.mem_ld(a);
}
// Box32 is 28 contiguous dwords (4 children + 4×6 box floats);
// Box16 is 16 contiguous dwords (4 children + 4×3 packed f16 pairs).
// Load the whole node in one window switch using 4-dword rawBufferLoads.
let at_al = lw.andc(at, !3);
let chunks = if box32 { 7usize } else { 4usize };
let words = lw.with_mem_i32s(None, chunks * 4, &mut |lw, m| {
let mut out = Vec::with_capacity(chunks * 4);
for j in 0..chunks {
let a = lw.addc(at_al, 16 * j as u32);
out.extend(lw.s.raw_load(m, a, 0xF));
}
out
});
let child = [words[0], words[1], words[2], words[3]];
let ninf = lw.s.m.f32c(0xFF80_0000);
let pinf = lw.s.m.f32c(0x7F80_0000);
let mut tn = [pinf; 4];
let mut id = [ones; 4];
for k in 0..4u32 {
for k in 0..4usize {
let (mn, mx) = if box32 {
let a = lw.addc(at, 16 + 24 * k);
let b = lw.addc(at, 28 + 24 * k);
(lw.load3f(a), lw.load3f(b))
let b = 4 + 6 * k;
(
[lw.f(words[b]), lw.f(words[b + 1]), lw.f(words[b + 2])],
[lw.f(words[b + 3]), lw.f(words[b + 4]), lw.f(words[b + 5])],
)
} else {
let a = lw.addc(at, 16 + 12 * k);
let mut h = [ones; 3];
for (j, slot) in h.iter_mut().enumerate() {
let x = lw.addc(a, 4 * j as u32);
*slot = lw.mem_ld(x);
}
let b = 4 + 3 * k;
let h = [words[b], words[b + 1], words[b + 2]];
let hx16 = lw.lshrc(h[0], 16);
let hy16 = lw.lshrc(h[1], 16);
let hz16 = lw.lshrc(h[2], 16);
Expand Down Expand Up @@ -102,7 +100,7 @@ impl Lw {
let a = lw.fmax(a, lo[2]);
let b = lw.fmin(hi[0], hi[1]);
let b = lw.fmin(b, hi[2]);
let cv = lw.cmpc(Pred::Ne, child[k as usize], u32::MAX);
let cv = lw.cmpc(Pred::Ne, child[k], u32::MAX);
let mut all = cv;
for c in 0..3 {
let le = lw.fcmp(Pred::FOLe, mn[c], mx[c]);
Expand All @@ -115,8 +113,8 @@ impl Lw {
let hit = lw.land(all, ab);
let x = lw.land(b0, at_);
let hit = lw.land(hit, x);
tn[k as usize] = lw.sel(hit, a, pinf);
id[k as usize] = lw.sel(hit, child[k as usize], ones);
tn[k] = lw.sel(hit, a, pinf);
id[k] = lw.sel(hit, child[k], ones);
}
for (i, j) in [(0usize, 1usize), (2, 3), (0, 2), (1, 3), (1, 2)] {
let lt = lw.fcmp(Pred::FOLt, tn[j], tn[i]);
Expand Down Expand Up @@ -145,9 +143,20 @@ impl Lw {
let p0 = lw.add(at, a0);
let p1 = lw.add(at, a1);
let p2 = lw.addc(at, 24);
let v0 = lw.load3f(p0);
let v1 = lw.load3f(p1);
let v2 = lw.load3f(p2);
let p60 = lw.addc(at, 60);
let (p0, p1, p2, p60) = (lw.andc(p0, !3), lw.andc(p1, !3), lw.andc(p2, !3), lw.andc(p60, !3));
let words = lw.with_mem_i32s(None, 10, &mut |lw, m| {
let mut out = Vec::with_capacity(10);
out.extend(lw.s.raw_load(m, p0, 0x7));
out.extend(lw.s.raw_load(m, p1, 0x7));
out.extend(lw.s.raw_load(m, p2, 0x7));
out.push(lw.s.raw_load(m, p60, 0x1)[0]);
out
});
let v0 = [lw.f(words[0]), lw.f(words[1]), lw.f(words[2])];
let v1 = [lw.f(words[3]), lw.f(words[4]), lw.f(words[5])];
let v2 = [lw.f(words[6]), lw.f(words[7]), lw.f(words[8])];
let tw = words[9];
let e1 = lw.f3(v1, v0, Lw::fsub);
let e2 = lw.f3(v2, v0, Lw::fsub);
let p = lw.cross(dir, e2);
Expand Down Expand Up @@ -176,8 +185,6 @@ impl Lw {
let di = lw.fsub(det, i);
let b0 = lw.fsub(di, j);
let bc = [b0, i, j, z];
let p60 = lw.addc(at, 60);
let tw = lw.mem_ld(p60);
let sh = lw.shlc(ty, 3);
let tid = lw.shr_masked(tw, sh);
let s0 = lw.andc(tid, 3);
Expand Down
Loading
Loading