Skip to content
Open
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
110 changes: 109 additions & 1 deletion d3d12/crates/leonida-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8398,6 +8398,10 @@ impl<'a> Lower<'a> {
/// to four of them (`bitcode::opt::WIDE` of `LEONIDA_SHADER_OPT`), not
/// by a call each.
fn wide_loads() -> bool {
#[cfg(test)]
if let Some(on) = TEST_WIDE_LOADS.get() {
return on;
}
super::shader_opt() & super::bitcode::opt::WIDE != 0
}

Expand Down Expand Up @@ -8593,7 +8597,7 @@ impl<'a> Lower<'a> {
}
None => self.handle(site, src, false, (11, 0))?,
};
if Self::wide_loads() {
if Self::wide_loads() && !(size && root) {
for (k, x) in self.raw_dwords(h, addr, count).into_iter().enumerate() {
self.write(inst, Reg::S(code + k as u8), x, None);
}
Expand Down Expand Up @@ -15706,6 +15710,12 @@ fn interp_mode(kind: Barycentric) -> Option<u8> {
})
}

#[cfg(test)]
thread_local! {
/// A test's choice for [`Lower::wide_loads`], for its thread only.
static TEST_WIDE_LOADS: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) };
}

#[cfg(test)]
mod tests {
use super::{Lowered, Numeric, Parts, buffer_format, half_shift, lower};
Expand Down Expand Up @@ -16911,4 +16921,102 @@ mod tests {
};
assert_eq!(what, "the primitive id");
}

/// With `bitcode::opt::WIDE` enabled, a 1-dword root SRV header size load
/// (offset 4 on a spilled builder descriptor) must still emit the
/// `builder_bounds` sanity clamp (`x < (1 << 20) ? x : 0`), matching the
/// scalar path and differing from an unclamped load at offset 8.
#[test]
fn wide_loads_keeps_builder_root_header_size_clamp() {
let lower_root_load = |offset: u32, wide: bool| -> (super::V, Vec<u8>) {
// Spilled compute shader:
// s_and_b32 s3, s1, 0xff
// s_mov_b32 s2, s0
// s_load_dwordx2 s[4:5], s[2:3], 0
// s_mov_b32 s6, 0xFFFF_FFFF
// s_mov_b32 s7, 0x0002_0000
// s_buffer_load_dword s8, s[4:7], <offset>
// s_endpgm
let words: [u32; 12] = [
0x8703_FF01, 0x0000_00FF, // s_and_b32 s3, s1, 0xff
0xBE82_0300, // s_mov_b32 s2, s0
0xF404_0101, 0xFA00_0000, // s_load_dwordx2 s[4:5], s[2:3], 0
0xBE86_03FF, 0xFFFF_FFFF, // s_mov_b32 s6, 0xffff_ffff
0xBE87_03FF, 0x0002_0000, // s_mov_b32 s7, 0x0002_0000
0xF420_0202, 0xFA00_0000 | offset, // s_buffer_load_dword s8, s[4:7], offset
S_ENDPGM,
];
let native = NativeProgram {
stage: HwStage::Cs,
wave64: false,
vgprs: 8,
layout: vec![root(RootType::Descriptor, 16, 0)],
used: vec![UsedResource {
kind: ResourceType::Srv,
root: RootType::Descriptor,
table_offset_dwords: 0,
lower_bound: 0,
stride: 0,
dwords: 4,
user_data: 16,
slot: 0,
}],
io: StageIo::Cs(ComputeIo {
rsrc2: 2 << 1,
threads: [1, 1, 1],
}),
code: words.iter().flat_map(|w| w.to_le_bytes()).collect(),
};
assert!(crate::spill::is_spilled(&native));
let p = crate::spill::build(&native).unwrap();
let st = structure::structure(&p).unwrap();
let res = resource::track(&p, &native.layout, native.stage, Some(&st)).unwrap();
let io = io::raise(&p, &native).unwrap();
let parts = Parts {
native: &native,
p: &p,
st: &st,
io: &io,
res: &res,
inputs: vec![],
outputs: vec![],
prims: vec![],
mesh_stride: 0,
};
let (idx, inst) = p.blocks[0]
.insts
.iter()
.enumerate()
.find(|(_, inst)| inst.name() == "s_buffer_load_dword")
.expect("s_buffer_load_dword in IR");
let dst = inst.write(crate::ir::Reg::S(8)).expect("s8 write");
let bad = std::collections::HashMap::new();
let mut l = super::Lower::new(&parts, &bad, false);
let prev = super::TEST_WIDE_LOADS.replace(Some(wide));
let r = l.smem(inst, (0, idx));
super::TEST_WIDE_LOADS.set(prev);
r.expect("smem lowering succeeds");
let v = l.i32_of(dst).expect("s8 i32 value");
l.s.m.terminate(l.cur, super::Term::Ret);
(v, l.s.m.write())
};

let (v_off4_scalar, bc_off4_scalar) = lower_root_load(4, false);
let (v_off4_wide, bc_off4_wide) = lower_root_load(4, true);
let (v_off8_wide, _) = lower_root_load(8, true);

assert_eq!(
v_off4_wide, v_off4_scalar,
"wide_loads must emit the same builder_bounds size clamp for offset 4"
);
assert_eq!(
bc_off4_wide, bc_off4_scalar,
"wide_loads bitcode for offset 4 must match the clamped scalar path"
);
// Offset 4 emits cmp(IULT, x, 1 << 20) + select(sane, x, 0) beyond offset 8.
let (super::V::Inst(i4), super::V::Inst(i8)) = (v_off4_wide, v_off8_wide) else {
panic!("expected lowered instruction values");
};
assert_eq!(i4, i8 + 2, "offset 4 must emit the 2-instruction sanity clamp");
}
}