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
66 changes: 59 additions & 7 deletions crates/viport-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5575,20 +5575,34 @@ impl<'a> Lower<'a> {
l.s.m.bin(c, Bin::AShr, t, k)
};
let f32t = self.s.t.f32;
// A normalized format's ends are exactly 1.0 and -1.0. The host
// may divide through a reciprocal and land just below 1.0: RDNA
// 4 loaded 16_16_UNORM's 65535 as the half 0x3BFF, not 0x3C00.
let x = match numeric {
Numeric::Unorm => {
let f = self.s.m.cast(c, Cast::UiToFp, field, f32t);
let max = self.s.m.f32((((1u64 << w) - 1) as f32).to_bits());
let top = (1u64 << w) - 1;
let max = self.s.m.f32((top as f32).to_bits());
let q = self.s.m.bin(c, Bin::SDiv, f, max);
let (high, one) = (self.s.m.i32(top as u32), self.s.m.f32(1.0f32.to_bits()));
let at_top = self.s.m.cmp(c, pred::EQ, field, high);
let q = self.s.m.select(c, at_top, one, q);
float(self, q)
}
Numeric::Snorm => {
let s = signed(self);
let f = self.s.m.cast(c, Cast::SiToFp, s, f32t);
let max = self.s.m.f32((((1u64 << (w - 1)) - 1) as f32).to_bits());
let top = (1u32 << (w - 1)) - 1;
let max = self.s.m.f32((top as f32).to_bits());
let q = self.s.m.bin(c, Bin::SDiv, f, max);
let lo = self.s.m.f32((-1.0f32).to_bits());
let q = self.binary(35, q, lo);
// The lowest value is -1.0 too, as is the one above it.
let (high, one) = (self.s.m.i32(top), self.s.m.f32(1.0f32.to_bits()));
let at_top = self.s.m.cmp(c, pred::SGE, s, high);
let q = self.s.m.select(c, at_top, one, q);
let low = self.s.m.i32(top.wrapping_neg());
let minus_one = self.s.m.f32((-1.0f32).to_bits());
let at_bottom = self.s.m.cmp(c, pred::SLE, s, low);
let q = self.s.m.select(c, at_bottom, minus_one, q);
float(self, q)
}
Numeric::Uscaled => {
Expand Down Expand Up @@ -5643,6 +5657,39 @@ impl<'a> Lower<'a> {
Ok(out)
}

/// Channels of buffer format `format`, as [`Self::format_load`] gives
/// them, as d16 data: 16 bits each, two per dword, low half first (an
/// odd last high half 0). Integer formats keep their low 16 bits; the
/// others become f16.
fn d16_channels(&mut self, format: u8, channels: Vec<V>) -> Result<Vec<V>, Why> {
let (_, numeric) = buffer_format(format).ok_or("a buffer format")?;
let c = self.cur;
let mut halves = Vec::new();
for x in channels {
halves.push(if matches!(numeric, Numeric::Uint | Numeric::Sint) {
let m = self.s.m.i32(0xFFFF);
self.s.m.bin(c, Bin::And, x, m)
} else {
let (f, i32t) = (self.as_float(x), self.s.t.i32);
self.s
.op(c, "legacyF32ToF16", Attr::ReadNone, i32t, 130, &[f])
});
}
let mut out = Vec::new();
for pair in halves.chunks(2) {
out.push(match *pair {
[lo, hi] => {
let k16 = self.s.m.i32(16);
let hi = self.s.m.bin(c, Bin::Shl, hi, k16);
self.s.m.bin(c, Bin::Or, lo, hi)
}
[lo] => lo,
_ => unreachable!("chunks of one or two"),
});
}
Ok(out)
}

/// The byte offset a buffer descriptor built around a root descriptor adds
/// (constant and computed parts); none for other descriptors.
fn built_offset(&mut self, src: &Source) -> Result<(u32, Option<V>), Why> {
Expand Down Expand Up @@ -9946,8 +9993,9 @@ impl<'a> Lower<'a> {
Some("cmpswap") => Some(u32::MAX),
Some(_) => return Err(name),
};
// d16: two 16-bit channels per VGPR, low half first.
let d16 = name.starts_with("buffer_") && name.contains("_format_d16_");
// d16: two 16-bit channels per VGPR, low half first (buffer and
// tbuffer formats alike).
let d16 = name.contains("_format_d16_");
let plain = name.replace("_format_d16_", "_format_");
let (typed, count) = match plain
.strip_prefix("buffer_load_")
Expand Down Expand Up @@ -10246,7 +10294,11 @@ impl<'a> Lower<'a> {
let Some(Operand::Vector { reg, .. }) = *data else {
return Err("a buffer result");
};
for (k, x) in self.format_load(h, off, f, count)?.into_iter().enumerate() {
let mut xs = self.format_load(h, off, f, count)?;
if d16 {
xs = self.d16_channels(f, xs)?;
}
for (k, x) in xs.into_iter().enumerate() {
self.write(inst, Reg::V(reg + k as u8), x, guard);
}
return Ok(());
Expand Down
72 changes: 72 additions & 0 deletions crates/viport-shader/src/emit/valu_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,78 @@ fn f16_to_f32(bits: u16) -> f32 {
f32::from_bits(f32_bits)
}

/// MTBUF `op vdata, off, s[srsrc:srsrc+3], 0 format:format` (at byte 0).
const fn mtbuf(op: u8, vdata: u8, srsrc: u8, format: u8) -> [u32; 2] {
[
0x3A << 26 | (format as u32 & 0x7F) << 19 | (op as u32 & 7) << 16,
0x80 << 24
| (op as u32 >> 3 & 1) << 21
| (srsrc as u32 >> 2 & 0x1F) << 16
| (vdata as u32) << 8,
]
}

/// Stores `v1` to the buffer, then reads it back into `v0` with the tbuffer
/// load `op` in buffer format `format`.
fn tbuffer_load_of_v1(op: u8, format: u8) -> HostShader {
let mut body = buffer_store_dword(1, 8, 0).to_vec();
body.extend(mtbuf(op, 0, 8, format));
build_valu_cs(&body)
}

#[test]
fn tbuffer_d16_loads_pack_two_channels_per_dword() {
// tbuffer_load_format_d16_x and tbuffer_load_format_d16_xy.
let (x, xy) = (8, 9);
// (opcode, format, dword stored, dword loaded)
let cases: &[(u8, u8, u32, u32)] = &[
// 16_16_FLOAT: the f16 pair (1.0, 2.0) as it was stored.
(xy, 29, 0x4000_3C00, 0x4000_3C00),
// 16_16_UINT: the 16-bit integers as they were stored.
(xy, 27, 0xBEEF_1234, 0xBEEF_1234),
// 16_16_UNORM: 0.0 and 1.0, as f16.
(xy, 23, 0xFFFF_0000, 0x3C00_0000),
// 16_FLOAT, one channel: the high half is 0.
(x, 13, 0x4000_3C00, 0x0000_3C00),
];
for &(op, format, stored, want) in cases {
let shader = tbuffer_load_of_v1(op, format);
run_cases(&shader, &[[stored, 0, 0, 0]], |backend, _, _, got| {
assert_eq!(
got, want,
"{backend}: d16 opcode {op} format {format} of {stored:#010x} = {got:#010x}, want {want:#010x}",
);
});
}
}

#[test]
fn normalized_buffer_formats_load_their_ends_exactly() {
// tbuffer_load_format_x.
let x = 0;
// (format, dword stored, value loaded)
let cases: &[(u8, u32, f32)] = &[
(1, 0xFF, 1.0), // 8_UNORM
(7, 0xFFFF, 1.0), // 16_UNORM
(2, 0x7F, 1.0), // 8_SNORM: the top, the one above the lowest, the lowest
(2, 0x81, -1.0),
(2, 0x80, -1.0),
(8, 0x7FFF, 1.0), // 16_SNORM
(8, 0x8001, -1.0),
(8, 0x8000, -1.0),
];
for &(format, stored, want) in cases {
let shader = tbuffer_load_of_v1(x, format);
run_cases(&shader, &[[stored, 0, 0, 0]], |backend, _, _, got| {
assert_eq!(
got,
want.to_bits(),
"{backend}: format {format} of {stored:#x} = {got:#010x}, want {want}",
);
});
}
}

#[test]
fn packed_add_selects_halves_of_inline_constants() {
// Add the inline constant 1/(2*pi) to v1 (zero) to read back its two halves.
Expand Down
Loading