Skip to content
Closed
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
91 changes: 56 additions & 35 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3174,39 +3174,55 @@ impl<'a> Lower<'a> {
// formats divide, which the AMD driver does 1 ULP off the hardware's
// correctly rounded conversion for some values; correct it with an
// integer check if a title compares converted values exactly.
fn format_load(&mut self, h: V, off: V, format: u8, channels: u32) -> Result<Vec<V>, Why> {
fn format_load(
&mut self,
h: V,
off: V,
format: u8,
channels: u32,
root_size: Option<V>,
) -> Result<Vec<V>, Why> {
let (bits, numeric) = buffer_format(format).ok_or("a buffer format")?;
let c = self.cur;
let total: u32 = bits.iter().sum();
let (u, align) = (self.s.m.undef(self.s.t.i32), self.s.m.i32(4));
let load = |l: &mut Self, at: V, n: u32| -> Vec<V> {
let mask = l.s.m.i8((1u8 << n) - 1);
let r = l.s.op(
c,
"rawBufferLoad.f32",
Attr::ReadOnly,
l.s.t.res_ret,
139,
&[h, at, u, mask, align],
);
(0..n)
.map(|k| {
let x = l.s.m.extract(c, r, k);
l.as_int(x)
})
.collect()
};
let words = if total < 32 {
let sub_dword = total < 32;
let n = (total / 32).max(1);
let at = if sub_dword {
// A byte or half inside the dword that holds it.
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 w = load(self, at, 1)[0];
let byte = self.s.m.bin(c, Bin::And, off, three);
let sh = self.s.m.bin(c, Bin::Shl, byte, three);
vec![self.s.m.bin(c, Bin::LShr, w, sh)]
let not3 = self.s.m.i32(!3);
self.s.m.bin(c, Bin::And, off, not3)
} else {
load(self, off, total / 32)
off
};
let (at, root_ok) = match root_size {
Some(size) => {
let (ok, at) = self.in_root_view(size, at, 4 * n);
(at, Some(ok))
}
None => (at, None),
};
let mask = self.s.m.i8((1u8 << n) - 1);
let r = self.s.op(
c,
"rawBufferLoad.f32",
Attr::ReadOnly,
self.s.t.res_ret,
139,
&[h, at, u, mask, align],
);
let mut words: Vec<_> = (0..n)
.map(|k| {
let x = self.s.m.extract(c, r, k);
self.as_int(x)
})
.collect();
if sub_dword {
let three = self.s.m.i32(3);
let byte = self.s.m.bin(c, Bin::And, off, three);
let sh = self.s.m.bin(c, Bin::Shl, byte, three);
words[0] = self.s.m.bin(c, Bin::LShr, words[0], sh);
}
let float = |l: &mut Self, f: V| l.as_int(f);
let mut out = Vec::new();
let mut at = 0;
Expand Down Expand Up @@ -3276,7 +3292,10 @@ impl<'a> Lower<'a> {
out.push(self.s.m.i32(if k == 3 { one } else { 0 }));
}
out.truncate(channels as usize);
Ok(out)
Ok(out
.into_iter()
.map(|x| self.zero_unless(root_ok, x))
.collect())
}

/// The byte offset a buffer descriptor built around a root descriptor adds
Expand Down Expand Up @@ -6035,12 +6054,11 @@ impl<'a> Lower<'a> {
let uav = store || atomic.is_some() || used.is_some_and(|u| u.kind == ResourceType::Uav);
let u = if uav { 0x1000 } else { 0 };
let iu = self.s.m.undef(self.s.t.i32);
// Root views: the bytes accessed checked against the view's size
// (raw accesses, and the raw loads formatted ones make).
let (off, root_ok) = match self.root_view_size(src) {
Some(size) if !typed || format.is_some() => {
let bytes = if format.is_some() { 16 } else { 4 * count };
let (ok, at) = self.in_root_view(size, off, bytes);
// Formatted loads handle bounds at their actual raw load address.
let root_size = self.root_view_size(src);
let (off, root_ok) = match root_size {
Some(size) if !typed && format.is_none() => {
let (ok, at) = self.in_root_view(size, off, 4 * count);
(at, Some(ok))
}
_ => (off, None),
Expand Down Expand Up @@ -6169,8 +6187,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 x = self.zero_unless(root_ok, x);
for (k, x) in self
.format_load(h, off, f, count, root_size)?
.into_iter()
.enumerate()
{
self.write(inst, Reg::V(reg + k as u8), x, guard);
}
return Ok(());
Expand Down
56 changes: 42 additions & 14 deletions crates/victory-shader/src/emit/valu_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,16 +697,46 @@ fn root_view_reads_stop_at_the_bound_size() {
words.extend_from_slice(&buffer_load_dword_offen(0, 1, 8));
words.extend_from_slice(&buffer_store_dword(0, 12, 0));
words.push(S_ENDPGM);
let code: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
let root = |root, sgprs, user_data, slot| RootParam {
root,
let cases = &[
(0, 64, 0x100),
(60, 64, 0x10F),
(64, 64, 0),
(128, 64, 0),
(u32::MAX - 3, 64, 0),
];
root_view_loads(words, cases);
}

#[test]
fn formatted_root_view_reads_use_the_access_width() {
// MTBUF tbuffer_load_format_x with BUF_FMT_32_UINT or BUF_FMT_8_UINT.
// The latter reads byte 61 from the aligned dword at byte 60.
for (insn, offset, expected) in [(0xE8A01000, 60, 0x10F), (0xE8281000, 61, 1)] {
let mut words = vec![s_mov_b32(5, 0x80)];
words.extend_from_slice(&s_load_dwordx4(12, 4));
words.push(s_mov_b32(10, 0xC1));
words.push(s_mov_b32(11, 0x80));
words.push(vop1(0x001, 1, sgpr(0)));
words.extend_from_slice(&[insn, 0x80020001]);
words.extend_from_slice(&buffer_store_dword(0, 12, 0));
words.push(S_ENDPGM);
root_view_loads(words, &[(offset, 64, expected), (offset, 63, 0)]);
}
}

fn root(kind: RootType, sgprs: u32, user_data: u8, slot: u8) -> RootParam {
RootParam {
root: kind,
table_offset_dwords: 0,
sgprs,
base_register: 0,
user_data,
slot,
};
let used = |kind, root, user_data, slot| UsedResource {
}
}

fn used(kind: ResourceType, root: RootType, user_data: u8, slot: u8) -> UsedResource {
UsedResource {
kind,
root,
table_offset_dwords: 0,
Expand All @@ -715,7 +745,11 @@ fn root_view_reads_stop_at_the_bound_size() {
dwords: 4,
user_data,
slot,
};
}
}

fn root_view_loads(words: Vec<u32>, cases: &[(u32, u32, u32)]) {
let code: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
let native = NativeProgram {
stage: HwStage::Cs,
wave64: false,
Expand Down Expand Up @@ -782,18 +816,12 @@ fn root_view_reads_stop_at_the_bound_size() {
continue;
};
let (root, pso) = host.compute_pipeline(&shader).expect("pipeline");
for (offset, want) in [
(0, 0x100),
(60, 0x10F),
(64, 0),
(128, 0),
(u32::MAX - 3, 0),
] {
for &(offset, size, want) in cases {
let mut ud = vec![0u32; shader.user_data_dwords().max(1) as usize];
for u in &shader.user_data {
ud[u.index as usize] = match u.root {
RootType::Constants => offset,
RootType::Descriptor => 64,
RootType::Descriptor => size,
_ => 0,
};
}
Expand Down