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
32 changes: 15 additions & 17 deletions crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5278,34 +5278,32 @@ void or_at4(inout uint4 r, uint i, uint v) {
r.x |= select(i == 0u, v, 0u); r.y |= select(i == 1u, v, 0u);
r.z |= select(i == 2u, v, 0u); r.w |= select(i == 3u, v, 0u);
}
uint bits_at(uint4 r, uint off, uint w) {
uint i = off >> 5; uint s = off & 31u;
uint lo = pick4(r, i); uint hi = select(i < 3u, pick4(r, min(i + 1u, 3u)), 0u);
uint v = select(s == 0u, lo, (lo >> s) | (hi << ((32u - s) & 31u)));
return select(w >= 32u, v, v & ((1u << (w & 31u)) - 1u));
}
float uf_small(uint v, uint mb) {
uint e = v >> mb; uint m = v & ((1u << mb) - 1u); float fm = (float)m / (float)(1u << mb);
uint e = v >> mb; uint m = v & ((1u << mb) - 1u);
float special = select(m != 0u, asfloat(0x7FC00000u), asfloat(0x7F800000u));
return select(e == 31u, special, select(e == 0u, fm * exp2(-14.0f), exp2((float)e - 15.0f) * (1.0f + fm)));
// Rebase the 5-bit exponent from 15 to 127. Subnormals use 2^(-14 - mb).
float subnormal = (float)m * asfloat((113u - mb) << 23);
float normal = asfloat(((112u + e) << 23) | (m << (23u - mb)));
return select(e == 31u, special, select(e == 0u, subnormal, normal));
}
uint fmt_bytes(uint info) { return (((info >> 8) & 63u) + ((info >> 14) & 63u) + ((info >> 20) & 63u) + ((info >> 26) & 63u)) / 8u; }
uint fmt_comp(uint4 r, uint info, uint c) {
uint n = info & 7u; uint ty = (info >> 3) & 7u;
bool isint = or(ty == 4u, ty == 5u);
uint off = select(c > 0u, (info >> 8) & 63u, 0u) + select(c > 1u, (info >> 14) & 63u, 0u) + select(c > 2u, (info >> 20) & 63u, 0u);
uint w = (info >> ((8u + 6u * c) & 31u)) & 63u;
uint v = bits_at(r, off, w);
// Each BUF_FORMATS component fits in one dword of the aligned ld_raw result.
uint sh = (32u - w) & 31u;
int sv = select(w >= 32u, (int)v, ((int)(v << sh)) >> sh);
float maxu = select(w >= 32u, 4294967295.0f, (float)((1u << (w & 31u)) - 1u));
float maxs = select(w >= 32u, 2147483647.0f, (float)((1u << ((w - 1u) & 31u)) - 1u));
float fv = (float)v; float fsv = (float)sv;
// exact endpoints (the hardware conversion is exact; DXC divides approximately)
uint f_un = asuint(select(v == 0u, 0.0f, select(fv >= maxu, 1.0f, fv / maxu)));
uint f_sn = asuint(select(sv == 0, 0.0f, select(fsv >= maxs, 1.0f, max(fsv / maxs, -1.0f))));
uint v = (pick4(r, (off >> 5) & 3u) >> (off & 31u)) & (0xFFFFFFFFu >> sh);
int sv = ((int)(v << sh)) >> sh;
uint sign = ty & 1u;
bool is_signed = sign != 0u;
float value = select(is_signed, (float)sv, (float)v);
float maxv = (float)(0xFFFFFFFFu >> ((sh + sign) & 31u));
// Share the divide for UNORM and SNORM and keep their endpoints exact.
float norm = select(value == 0.0f, 0.0f, select(value >= maxv, 1.0f, max(value / maxv, -1.0f)));
uint flt = select(w == 32u, v, select(w == 16u, asuint(f16tof32(v)), asuint(uf_small(v, select(w == 11u, 6u, 5u)))));
uint res = select(ty == 0u, f_un, select(ty == 1u, f_sn, select(ty == 2u, asuint(fv), select(ty == 3u, asuint(fsv), select(ty == 4u, v, select(ty == 5u, (uint)sv, flt))))));
uint res = select(ty < 2u, asuint(norm), select(ty < 4u, asuint(value), select(isint, select(is_signed, (uint)sv, v), flt)));
return select(c >= n, select(c == 3u, select(isint, 1u, 0x3F800000u), 0u), res);
}
// image_bvh(64)_intersect_ray: type = node & 7, at = low 32 bits of base + (node & ~7) * 8.
Expand Down
56 changes: 56 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,62 @@ fn buffer_format_table_matches_llvm() {
assert!(checked >= 10, "{checked}");
}

#[test]
fn buffer_format_components_fit_in_one_dword() {
for (format, name) in super::emit::BUF_FORMATS.iter().enumerate() {
let info = super::emit::buf_format_info(format);
let mut offset = 0;
for component in 0..(info & 7) {
let width = (info >> (8 + 6 * component)) & 63;
assert!(width > 0 && (offset & 31) + width <= 32, "{name} component {component}");
offset += width;
}
assert!(offset <= 128, "{name}");
}
}

#[test]
fn gpu_buffer_format_component_decode() {
if !gpu_ready() {
return;
}
let code = prog(&[("BUFFER_LOAD_FORMAT_XYZW", "ENC_MUBUF", &[("VDATA", 10), ("SRSRC", 3), ("SOFFSET", 128)], None)]);
let t = translate(&code, &Options { wave64: false, ..Default::default() }).unwrap();
assert!(t.complete(), "{:?}", t.unsupported_why);
let cases = [
("8_8_8_8_UNORM", [0x00FF00FFu32, 0, 0, 0], [f(1.0), 0, f(1.0), 0]),
("2_10_10_10_SNORM", [1 << 30 | 0x200 << 20 | 0x1FF << 10, 0, 0, 0], [0, f(1.0), f(-1.0), f(1.0)]),
("8_8_USCALED", [0x7FFF, 0, 0, 0], [f(255.0), f(127.0), 0, f(1.0)]),
("8_8_SSCALED", [0x7FFF, 0, 0, 0], [f(-1.0), f(127.0), 0, f(1.0)]),
("16_16_SINT", [0xFF00FFFC, 0, 0, 0], [(-4i32) as u32, (-256i32) as u32, 0, 1]),
("32_32_32_32_UINT", [0, 0x80000000, 0x12345678, u32::MAX], [0, 0x80000000, 0x12345678, u32::MAX]),
("32_32_32_32_FLOAT", [f(-0.0), f(2.5), f(f32::INFINITY), f(-1.0)], [f(-0.0), f(2.5), f(f32::INFINITY), f(-1.0)]),
("16_16_16_16_FLOAT", [0x00003C00, 0x3800C000, 0, 0], [f(1.0), 0, f(-2.0), f(0.5)]),
("10_11_11_FLOAT", [1 | (31 << 6) << 11 | (15 << 5) << 22, 0, 0, 0], [f(2.0f32.powi(-20)), f(f32::INFINITY), f(1.0), f(1.0)]),
("11_11_10_FLOAT", [1 | ((15 << 6) | 32) << 10 | ((31 << 6) | 1) << 21, 0, 0, 0], [f(2.0f32.powi(-19)), f(1.5), 0x7FC00000, f(1.0)]),
("INVALID", [u32::MAX; 4], [0; 4]),
];
let mut state = KernelState::default();
let dst_sel = 4 | 5 << 3 | 6 << 6 | 7 << 9;
for level in ["-O0", "-O3"] {
let dxil = super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[level]).unwrap();
for (name, input, expected) in cases {
let format = super::emit::BUF_FORMATS.iter().position(|&f| f == name).unwrap() as u32;
// The buffer starts at byte 1 to exercise ld_raw's unaligned load.
state.sgpr[12..16].copy_from_slice(&[1, 0, 16, dst_sel | format << 12]);
let mut bytes = [0; 20];
for (dst, word) in bytes[1..17].chunks_exact_mut(4).zip(input) {
dst.copy_from_slice(&word.to_le_bytes());
}
let mem: Vec<u32> = bytes.chunks_exact(4).map(|word| u32::from_le_bytes(word.try_into().unwrap())).collect();
let result = runner::run(&dxil, &state, &mem).unwrap();
for (lane, regs) in result.vgpr[..32].iter().enumerate() {
assert_eq!(regs[10..14], expected, "{name} {level} lane {lane}");
}
}
}
}

#[test]
fn buffer_format_d16_packs_and_unpacks_halves() {
let code = prog(&[
Expand Down
Loading