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
57 changes: 49 additions & 8 deletions d3d12/crates/leonida-shader/src/emit/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ pub(crate) struct Shader {
pub samplers: u32,
/// Bit n: immediate sampler n is a comparison sampler.
pub cmp_samplers: u64,
/// Cached immediate sampler handles emitted in `self.entry`, keyed by `(slot, cmp)`.
sampler_handles: Vec<(u32, bool, V)>,
/// Root CBVs read, by root parameter index, with their handles.
pub root_cbvs: Vec<(u8, V)>,
/// Root SRVs (raw buffers) read, likewise.
Expand Down Expand Up @@ -446,6 +448,7 @@ impl Shader {
user_data: 0,
samplers: 0,
cmp_samplers: 0,
sampler_handles: Vec::new(),
root_cbvs: Vec::new(),
root_srvs: Vec::new(),
root_uavs: Vec::new(),
Expand Down Expand Up @@ -597,8 +600,16 @@ impl Shader {
}

/// The handle of immediate sampler `n` (a comparison sampler when its
/// bit in `cmp_samplers` is set).
pub fn sampler_handle(&mut self, block: usize, n: u32) -> V {
/// bit in `cmp_samplers` is set), emitted once in the entry block.
pub fn sampler_handle(&mut self, _block: usize, n: u32) -> V {
let is_cmp = n < u64::BITS && self.cmp_samplers >> n & 1 != 0;
if let Some(&(_, _, h)) = self
.sampler_handles
.iter()
.find(|&&(slot, c, _)| slot == n && c == is_cmp)
{
return h;
}
let bind = [
self.m.i32(n),
self.m.i32(n),
Expand All @@ -607,6 +618,7 @@ impl Shader {
];
let bind = self.m.aggregate(self.t.bind, &bind);
let (i, f) = (self.m.i32(n), self.m.bool(false));
let block = self.entry;
let h = self.op(
block,
"createHandleFromBinding",
Expand All @@ -615,12 +627,10 @@ impl Shader {
217,
&[bind, i, f],
);
let cmp = if self.cmp_samplers >> n & 1 != 0 {
0x8000
} else {
0
};
self.annotate(block, h, 14 | cmp, 0)
let cmp = if is_cmp { 0x8000 } else { 0 };
let h = self.annotate(block, h, 14 | cmp, 0);
self.sampler_handles.push((n, is_cmp, h));
h
}

pub fn annotate(&mut self, block: usize, h: V, props0: u32, props1: u32) -> V {
Expand Down Expand Up @@ -1182,3 +1192,34 @@ impl Shader {
Some(self.m.md_node(list))
}
}

#[cfg(test)]
mod tests {
use super::super::bitcode::Term;
use super::*;

#[test]
fn sampler_handle_is_emitted_once_in_entry_and_cached() {
let mut s = Shader::new(true);
let b1 = s.m.new_block();
let b2 = s.m.new_block();
s.samplers = 2;
s.cmp_samplers = 1 << 1;

let h0_a = s.sampler_handle(b1, 0);
let h0_b = s.sampler_handle(b2, 0);
let h1_a = s.sampler_handle(b1, 1);
let h1_b = s.sampler_handle(b2, 1);
assert_eq!(h0_a, h0_b, "same sampler slot must reuse cached entry handle");
assert_eq!(h1_a, h1_b, "comparison sampler slot must reuse cached entry handle");
assert_ne!(h0_a, h1_a);

// `b1` and `b2` are sibling branches dominated only by `s.entry`.
let cond = s.m.bool(true);
s.m.terminate(s.entry, Term::CondBr(cond, b1, b2));
s.m.terminate(b1, Term::Ret);
s.m.terminate(b2, Term::Ret);
let container = s.finish();
assert!(!container.is_empty());
}
}