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
111 changes: 84 additions & 27 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8596,18 +8596,29 @@ impl<'a> Lower<'a> {
Some("format_xyz") => (true, 3),
Some("format_xyzw") => (true, 4),
Some("ubyte" | "sbyte" | "ushort" | "sshort") if !store => (false, 1),
Some(
"ubyte_d16" | "ubyte_d16_hi" | "sbyte_d16" | "sbyte_d16_hi" | "short_d16"
| "short_d16_hi",
) if !store => (false, 1),
Some("byte" | "short") if store => (false, 1),
_ => return Err(name),
};
// Loads or stores of a byte or a half: its width and whether signed.
// Loads or stores of a byte or a half: its width, whether signed, and
// for `_d16` / `_d16_hi` which half of the destination VGPR receives it.
let sub = match plain
.strip_prefix("buffer_load_")
.or(plain.strip_prefix("buffer_store_"))
{
Some("ubyte" | "byte") => Some((8, false)),
Some("sbyte") => Some((8, true)),
Some("ushort" | "short") => Some((16, false)),
Some("sshort") => Some((16, true)),
Some("ubyte" | "byte") => Some((8, false, None)),
Some("sbyte") => Some((8, true, None)),
Some("ushort" | "short") => Some((16, false, None)),
Some("sshort") => Some((16, true, None)),
Some("ubyte_d16") => Some((8, false, Some(false))),
Some("ubyte_d16_hi") => Some((8, false, Some(true))),
Some("sbyte_d16") => Some((8, true, Some(false))),
Some("sbyte_d16_hi") => Some((8, true, Some(true))),
Some("short_d16") => Some((16, false, Some(false))),
Some("short_d16_hi") => Some((16, false, Some(true))),
_ => None,
};
let Operand::Vector { reg: areg, .. } = *addr else {
Expand Down Expand Up @@ -8661,6 +8672,9 @@ impl<'a> Lower<'a> {
}
(_, ix) => ix,
};
if sub.is_some() && !store && used.is_some_and(|u| u.kind == ResourceType::Cbv) {
return Err("a sub-dword read of a constant buffer");
}
if used.is_some_and(|u| u.kind == ResourceType::Cbv) && !typed {
// A constant buffer read per lane (a dynamically indexed array).
// shortcut: the console's CBV descriptor is taken to have a 16-byte
Expand Down Expand Up @@ -8772,7 +8786,7 @@ impl<'a> Lower<'a> {
}
return Ok(());
}
if let (true, Some((width, _))) = (store, sub) {
if let (true, Some((width, _, _))) = (store, sub) {
// The field of the dword holding it, by two atomics on that dword
// (clear, then set): other lanes' fields in it are left alone.
// shortcut: the field is taken not to cross a dword.
Expand Down Expand Up @@ -8885,39 +8899,82 @@ impl<'a> Lower<'a> {
}
return Ok(());
}
if let Some((width, signed)) = sub {
// The dword holding the field, as `global_load_ubyte` and kin.
// shortcut: the field is taken not to cross a dword.
if let Some((width, signed, half)) = sub {
// The dword holding the field, as `global_load_ubyte` and kin; a
// halfword at byte 3 of a dword reads the next dword too, only for
// lanes that cross. `_d16`/`_d16_hi` keep the other half of the
// destination register.
if index.is_some() {
return Err("a byte or half buffer load by index");
}
let Some(Operand::Vector { reg, .. }) = *data else {
return Err("a buffer result");
};
let word = |l: &mut Self, byte_off: V, active: Option<V>| -> Result<V, Why> {
let (h, at) = l.raw_view(site, src, byte_off, 4, (11 | u, 0), false, active)?;
let c = l.cur;
let (mask, align) = (l.s.m.i8(1), l.s.m.i32(4));
let r = l.s.op(
c,
"rawBufferLoad.f32",
Attr::ReadOnly,
l.s.t.res_ret,
139,
&[h, at, iu, mask, align],
);
let w = l.s.m.extract(c, r, 0);
Ok(l.as_int(w))
};
let (not3, three) = (self.s.m.i32(!3), self.s.m.i32(3));
let at = self.s.m.bin(c, Bin::And, off, not3);
let (h, at) = self.raw_view(site, src, at, 4, (11 | u, 0), false, guard)?;
let aligned = self.s.m.bin(c, Bin::And, off, not3);
let w = word(self, aligned, guard)?;
let c = self.cur;
let (mask, align) = (self.s.m.i8(1), self.s.m.i32(4));
let r = self.s.op(
c,
"rawBufferLoad.f32",
Attr::ReadOnly,
self.s.t.res_ret,
139,
&[h, at, iu, mask, align],
);
let w = self.s.m.extract(c, r, 0);
let w = self.as_int(w);
let byte = self.s.m.bin(c, Bin::And, off, three);
let sh = self.s.m.bin(c, Bin::Shl, byte, three);
let x = self.s.m.bin(c, Bin::LShr, w, sh);
let mut x = self.s.m.bin(c, Bin::LShr, w, sh);
if width == 16 {
let mut crosses = self.s.m.cmp(c, pred::EQ, byte, three);
if let Some(g) = guard {
crosses = self.s.m.bin(c, Bin::And, crosses, g);
}
let br = self.begin_guarded(Some(crosses));
let four = self.s.m.i32(4);
let next = self.s.m.bin(self.cur, Bin::Add, aligned, four);
let hi = word(self, next, Some(crosses))?;
let cur = self.cur;
let eight = self.s.m.i32(8);
let hi = self.s.m.bin(cur, Bin::Shl, hi, eight);
let both = self.s.m.bin(cur, Bin::Or, x, hi);
x = self.end_guarded(br, &[(both, x)])[0];
}
let c = self.cur;
let left = self.s.m.i32(32 - width);
let x = self.s.m.bin(c, Bin::Shl, x, left);
let x = self
let mut x = self
.s
.m
.bin(c, if signed { Bin::AShr } else { Bin::LShr }, x, left);
let Some(Operand::Vector { reg, .. }) = *data else {
return Err("a buffer result");
};
if let Some(high) = half {
let old = match inst.read(Reg::V(reg)) {
Some(v) => self.i32_of(v)?,
None => self.s.m.i32(0),
};
let low = self.s.m.i32(0xFFFF);
let (keep, field) = if high {
let k16 = self.s.m.i32(16);
(
self.s.m.bin(c, Bin::And, old, low),
self.s.m.bin(c, Bin::Shl, x, k16),
)
} else {
let up = self.s.m.i32(0xFFFF_0000);
(
self.s.m.bin(c, Bin::And, old, up),
self.s.m.bin(c, Bin::And, x, low),
)
};
x = self.s.m.bin(c, Bin::Or, keep, field);
}
self.write(inst, Reg::V(reg), x, guard);
return Ok(());
}
Expand Down
62 changes: 61 additions & 1 deletion crates/victory-shader/src/emit/valu_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ fn run_cases(
&pso,
&Dispatch {
roots: vec![(0, Root::Constants(ud))],
views: &[View::RawUav(1)],
views: &[View::RawUav(4)],
samplers: &[],
groups: [1, 1, 1],
},
Expand Down Expand Up @@ -1743,3 +1743,63 @@ fn relative_moves_reach_only_what_their_clamped_index_can() {
refused.err()
);
}

/// MUBUF `buffer_load_* vdata, vaddr, s[srsrc:srsrc+3], 0 offen offset:byte_off`.
const fn buffer_load_offen(op: u8, vdata: u8, vaddr: u8, srsrc: u8, byte_off: u16) -> [u32; 2] {
[
0x38 << 26 | (op as u32) << 18 | 1 << 12 | (byte_off as u32 & 0xFFF),
0x80 << 24 | ((srsrc as u32 >> 2) & 0x1F) << 16 | (vdata as u32) << 8 | vaddr as u32,
]
}

/// RDNA2 ISA, MUBUF: `buffer_load_{ubyte,sbyte,short}_d16` and `_hi` put the loaded byte or
/// halfword in one half of the destination VGPR and keep the other half (reading the next dword
/// when a halfword starts at byte 3 mod 4).
#[test]
fn d16_sub_dword_buffer_loads_preserve_the_other_half() {
// (opcode, width, signed, high half)
let loads: [(u8, u32, bool, bool); 6] = [
(0x20, 8, false, false),
(0x21, 8, false, true),
(0x22, 8, true, false),
(0x23, 8, true, true),
(0x24, 16, false, false),
(0x25, 16, false, true),
];
let (d1, d2, old) = (0x84F2_81C3u32, 0x7A9B_6D5Eu32, 0xA5B6_C7D8u32);
let bytes: Vec<u8> = [0, d1, d2].iter().flat_map(|d| d.to_le_bytes()).collect();
for (op, width, signed, high) in loads {
// v1 = byte address, v2 and v3 the dwords stored at bytes 4 and 8, v4 the old v0.
let mut body = vec![vop1(0x001, 0, vgpr(4))];
body.extend_from_slice(&buffer_store_dword(2, 8, 4));
body.extend_from_slice(&buffer_store_dword(3, 8, 8));
body.extend_from_slice(&buffer_load_offen(op, 0, 1, 8, 0));
let shader = build_valu_cs(&body);
// Every byte of dwords 1 and 2; a halfword at 7 crosses them.
let addresses: Vec<u32> = (4..=if width == 8 { 11 } else { 10 }).collect();
let cases: Vec<[u32; 4]> = addresses.iter().map(|&a| [a, d1, d2, old]).collect();
run_cases(&shader, &cases, |backend, i, _, got| {
let at = addresses[i] as usize;
let raw = if width == 8 {
u32::from(bytes[at])
} else {
u32::from(bytes[at]) | u32::from(bytes[at + 1]) << 8
};
let shift = 32 - width;
let field = if signed {
((raw << shift) as i32 >> shift) as u32
} else {
raw
};
let want = if high {
old & 0xFFFF | field << 16
} else {
old & 0xFFFF_0000 | field & 0xFFFF
};
assert_eq!(
got, want,
"{backend}: op {op:#x} at byte {at}: {got:#010x}, want {want:#010x}"
);
});
}
}