Shaders: lower immediate samplers chosen by uniform control flow

This commit is contained in:
Noname0202 committed 2026-11-24 21:23:58 +01:00
1 parent 16849898f3
commit cb80bf0f9c
2 files changed
+381 -61

No files matched your search

+197 -61
View File
@@ -6151,6 +6151,123 @@ impl<'a> Lower<'a> {
Ok(phi)
}
/// Registers an immediate sampler constant and returns its slot index.
fn register_sampler(&mut self, words: &[u32], cmp: bool) -> Result<u32, Why> {
let words: [u32; 4] = words
.get(..4)
.and_then(|w| w.try_into().ok())
.ok_or("a sampler constant")?;
let n = match self.samplers.iter().position(|w| *w == words) {
Some(n) => n,
None => {
self.samplers.push(words);
self.samplers.len() - 1
}
};
if cmp && n < 64 {
self.s.cmp_samplers |= 1 << n;
}
Ok(n as u32)
}
/// Resolves a control-flow phi of immediate sampler constants into either a
/// single static sampler register (`One`) or a uniform `i32` phi index over
/// `regs` (`Many`) for branch dispatch at the sample site.
fn sampler_phi_choice(
&mut self,
block: usize,
args: &[Source],
cmp: bool,
depth: u32,
) -> Result<(Option<V>, Vec<u32>), Why> {
const WHY: Why = "a descriptor chosen by control flow";
let mut distinct: Vec<[u32; 4]> = Vec::new();
self.collect_constant_samplers(block, args, depth, &mut distinct)?;
if distinct.is_empty() {
return Err(WHY);
}
let mut regs = Vec::with_capacity(distinct.len());
for w in &distinct {
regs.push(self.register_sampler(w, cmp)?);
}
if regs.len() == 1 {
return Ok((None, regs));
}
let idx = self.sampler_index_phi(block, args, 0, &distinct, depth)?;
Ok((Some(idx), regs))
}
fn collect_constant_samplers(
&self,
block: usize,
args: &[Source],
depth: u32,
distinct: &mut Vec<[u32; 4]>,
) -> Result<(), Why> {
const WHY: Why = "a descriptor chosen by control flow";
let p = self.x.p;
for (&q, arg) in p.blocks[block].preds.iter().zip(args) {
if self.block[q].is_none() {
continue;
}
match arg {
Source::Constant(w) if w.len() >= 4 => {
let k: [u32; 4] = w[..4].try_into().unwrap();
if !distinct.contains(&k) {
distinct.push(k);
}
}
Source::Phi {
block: inner,
args: inner_args,
} if depth > 0 => {
self.collect_constant_samplers(*inner, inner_args, depth - 1, distinct)?;
}
_ => return Err(WHY),
}
}
Ok(())
}
fn sampler_index_phi(
&mut self,
block: usize,
args: &[Source],
lower: u32,
distinct: &[[u32; 4]],
depth: u32,
) -> Result<V, Why> {
const WHY: Why = "a descriptor chosen by control flow";
let p = self.x.p;
let mut incoming = Vec::new();
for (&q, arg) in p.blocks[block].preds.iter().zip(args) {
if self.block[q].is_none() {
continue;
}
let tail = self.tail[q].ok_or(WHY)?;
let at = self.edges.get(&(q, block)).copied().unwrap_or(tail);
let idx = match arg {
Source::Constant(w) if w.len() >= 4 => {
let k: [u32; 4] = w[..4].try_into().unwrap();
let pos = distinct.iter().position(|d| *d == k).ok_or(WHY)? as u32;
self.s.m.i32(lower + pos)
}
Source::Phi {
block: inner,
args: inner_args,
} if depth > 0 => {
self.sampler_index_phi(*inner, inner_args, lower, distinct, depth - 1)?
}
_ => return Err(WHY),
};
incoming.push((idx, at));
}
let join = self.block[block].ok_or(WHY)?;
let phi = self.s.m.phi(join, self.s.t.i32);
self.s.m.set_phi(phi, incoming);
Ok(phi)
}
/// The bytes per element of the buffer view `src` names, if the program
/// picks it from the view heap at an index it computes (root type
/// `ResourceHeap`): read from the host's table by heap slot in the
@@ -9133,7 +9250,23 @@ impl<'a> Lower<'a> {
if cmp && (*a16 || dmask.count_ones() != 1) {
return Err("a comparison sample of 16-bit coordinates or several channels");
}
let sh = self.handle(site, ss, true, (14 | if cmp { 0x8000 } else { 0 }, 0))?;
let mut sampler_choice: Option<(V, Vec<u32>)> = None;
let sh = match ss {
Source::Phi { block, args } => {
match self.sampler_phi_choice(*block, args, cmp, 8) {
Ok((None, regs)) => self.s.sampler_handle(self.cur, regs[0]),
Ok((Some(idx), regs)) => {
let first = self.s.sampler_handle(self.cur, regs[0]);
sampler_choice = Some((idx, regs));
first
}
Err(_) => {
self.handle(site, ss, true, (14 | if cmp { 0x8000 } else { 0 }, 0))?
}
}
}
_ => self.handle(site, ss, true, (14 | if cmp { 0x8000 } else { 0 }, 0))?,
};
// Texel offsets (`_o`) come first: x in bits 0-5, y in 8-13,
// signed (programmable offsets: gathers only).
let offset_word = name.ends_with("_o");
@@ -9239,6 +9372,60 @@ impl<'a> Lower<'a> {
} else {
fu
};
let call_sample = |l: &mut Self,
intrinsic: &str,
opcode: u32,
args: &[V],
gathered: &mut Option<[V; 4]>|
-> V {
let Some((idx, ref regs)) = sampler_choice else {
return l
.s
.op(c, intrinsic, Attr::ReadOnly, l.s.t.res_ret, opcode, args);
};
let merge = l.s.m.new_block();
let mut cur_test = c;
let mut per_ch: [Vec<(V, usize)>; 4] = Default::default();
let mut last_r = args[0];
for (i, &reg) in regs.iter().enumerate() {
let arm = if i + 1 == regs.len() {
cur_test
} else {
let arm = l.s.m.new_block();
let next = l.s.m.new_block();
let k = l.s.m.i32(i as u32);
let cond = l.s.m.cmp(cur_test, pred::EQ, idx, k);
l.s.m.terminate(cur_test, Term::CondBr(cond, arm, next));
cur_test = next;
arm
};
let mut arm_args = args.to_vec();
arm_args[1] = l.s.sampler_handle(arm, reg);
let r_i = l.s.op(
arm,
intrinsic,
Attr::ReadOnly,
l.s.t.res_ret,
opcode,
&arm_args,
);
for (ch, slot) in per_ch.iter_mut().enumerate() {
let v = l.s.m.extract(arm, r_i, ch as u32);
slot.push((v, arm));
}
l.s.m.terminate(arm, Term::Br(merge));
last_r = r_i;
}
let mut phis = [fu; 4];
for (ch, incoming) in per_ch.into_iter().enumerate() {
let phi = l.s.m.phi(merge, l.s.t.f32);
l.s.m.set_phi(phi, incoming);
phis[ch] = phi;
}
*gathered = Some(phis);
l.cur = merge;
last_r
};
match op {
// Outside a pixel shader there are no screen derivatives:
// the validator refuses `sample` there ("features
@@ -9252,14 +9439,7 @@ impl<'a> Lower<'a> {
60 if !self.s.pixel => {
args.extend(&os);
args.push(self.s.m.f32(0));
self.s.op(
c,
"sampleLevel.f32",
Attr::ReadOnly,
self.s.t.res_ret,
62,
&args,
)
call_sample(self, "sampleLevel.f32", 62, &args, &mut gathered)
}
60 => {
if offset_word {
@@ -9285,8 +9465,7 @@ impl<'a> Lower<'a> {
}
args.extend(&os);
args.push(clamp);
self.s
.op(c, "sample.f32", Attr::ReadOnly, self.s.t.res_ret, 60, &args)
call_sample(self, "sample.f32", 60, &args, &mut gathered)
}
61 => {
// With a16 the bias is an f16 in the low half of its
@@ -9302,25 +9481,11 @@ impl<'a> Lower<'a> {
args.extend(&os);
if self.s.pixel {
args.extend([bias, clamp]);
self.s.op(
c,
"sampleBias.f32",
Attr::ReadOnly,
self.s.t.res_ret,
61,
&args,
)
call_sample(self, "sampleBias.f32", 61, &args, &mut gathered)
} else {
// As above: the bias is the level.
args.push(bias);
self.s.op(
c,
"sampleLevel.f32",
Attr::ReadOnly,
self.s.t.res_ret,
62,
&args,
)
call_sample(self, "sampleLevel.f32", 62, &args, &mut gathered)
}
}
63 if kind == 5 => {
@@ -9359,14 +9524,7 @@ impl<'a> Lower<'a> {
let lod = self.s.m.bin(c, Bin::Mul, ln, per_ln2);
args.extend(&os);
args.push(lod);
self.s.op(
c,
"sampleLevel.f32",
Attr::ReadOnly,
self.s.t.res_ret,
62,
&args,
)
call_sample(self, "sampleLevel.f32", 62, &args, &mut gathered)
}
63 => {
let mut g = vec![fu; 6];
@@ -9379,14 +9537,7 @@ impl<'a> Lower<'a> {
args.extend(&os);
args.extend(g);
args.push(clamp);
self.s.op(
c,
"sampleGrad.f32",
Attr::ReadOnly,
self.s.t.res_ret,
63,
&args,
)
call_sample(self, "sampleGrad.f32", 63, &args, &mut gathered)
}
64 | 65 => {
let reference = addr_u32(self, first - 1)?;
@@ -9399,8 +9550,7 @@ impl<'a> Lower<'a> {
} else {
"sampleCmpLevelZero.f32"
};
self.s
.op(c, name, Attr::ReadOnly, self.s.t.res_ret, op, &args)
call_sample(self, name, op, &args, &mut gathered)
}
62 => {
let lod = if name == "image_sample_l" {
@@ -9411,14 +9561,7 @@ impl<'a> Lower<'a> {
};
args.extend(&os);
args.push(lod);
self.s.op(
c,
"sampleLevel.f32",
Attr::ReadOnly,
self.s.t.res_ret,
62,
&args,
)
call_sample(self, "sampleLevel.f32", 62, &args, &mut gathered)
}
_ => {
if name == "image_gather4" {
@@ -9500,14 +9643,7 @@ impl<'a> Lower<'a> {
} else {
args.extend(&os[..2]);
args.push(self.s.m.i32(dmask.trailing_zeros()));
self.s.op(
c,
"textureGather.f32",
Attr::ReadOnly,
self.s.t.res_ret,
73,
&args,
)
call_sample(self, "textureGather.f32", 73, &args, &mut gathered)
}
}
}
@@ -249,3 +249,187 @@ fn lanes_sample_their_own_texture_in_a_waterfall_narrowed_by_v_cmpx() {
}
}
}
#[test]
fn immediate_sampler_chosen_by_control_flow() {
let s_mov_lit = |dst: u8, lit: u32| [0xBE80_03FF | (dst as u32) << 16, lit];
let s_mov_zero = |dst: u8| 0xBE80_0380 | (dst as u32) << 16;
let s_load_x8 = |dst: u8, base: u8| [0x3D << 26 | 0x03 << 18 | (dst as u32) << 6 | (base as u32 >> 1), 125 << 25];
let s_load_x4_off = |dst: u8, base: u8, off: u32| [0x3D << 26 | 0x02 << 18 | (dst as u32) << 6 | (base as u32 >> 1), 125 << 25 | off];
// image_sample_lz v4, v[2:3], s[8:15], s[16:19] dmask:0x1 dim:SQ_RSRC_IMG_2D
let image_sample_lz: [u32; 2] = [
0x3C << 26 | 0x21 << 18 | 1 << 8 | 1 << 3, // dmask=1, dim=2D(1)
(16u32 >> 2) << 21 | (8u32 >> 2) << 16 | 4 << 8 | 2,
];
// buffer_store_dword v4, off, s[20:23], 0
let buf_store: [u32; 2] = [0x38 << 26 | 0x1C << 18, 0x80 << 24 | (20u32 >> 2) << 16 | 4 << 8];
// Two distinct valid immediate sampler descriptors (different wrap/filter bits).
let samp_a: [u32; 4] = [0x0000_0092, 0x00C0_0000, 0x1850_0000, 0];
let samp_b: [u32; 4] = [0x0000_0092, 0x00C0_0000, 0x1850_0001, 0];
for (case_name, alt) in [("same_sampler", samp_a), ("different_samplers", samp_b)] {
let mut words = Vec::new();
// s[2:3] = view table base (s0, 0).
words.push(0xBE80_0300 | (2 << 16)); // s_mov_b32 s2, s0
words.push(s_mov_zero(3));
words.extend_from_slice(&s_load_x8(8, 2)); // s[8:15] = texture SRV (offset 0)
words.extend_from_slice(&s_load_x4_off(20, 2, 64)); // s[20:23] = raw UAV (offset 16 dwords = 64B)
// v2 = 0.5, v3 = 0.5 (sample coordinates).
words.push(0x7E00_02F0 | (2 << 17)); // v_mov_b32 v2, 0.5
words.push(0x7E00_02F0 | (3 << 17)); // v_mov_b32 v3, 0.5
// s_cmp_eq_u32 s1, 0; s_cbranch_scc0 else_arm
words.push(0xBF00_8001); // s_cmp_eq_u32 s1, 0 (const 128)
let br_else = words.len();
words.push(0); // placeholder s_cbranch_scc0
// then arm: load samp_a into s[16:19]
for (i, &w) in samp_a.iter().enumerate() {
words.extend_from_slice(&s_mov_lit(16 + i as u8, w));
}
let br_join = words.len();
words.push(0); // placeholder s_branch
let else_pc = words.len();
// else arm: load alt into s[16:19]
for (i, &w) in alt.iter().enumerate() {
words.extend_from_slice(&s_mov_lit(16 + i as u8, w));
}
let join_pc = words.len();
words[br_else] = 0xBF84_0000 | ((else_pc - br_else - 1) as u32 & 0xFFFF);
words[br_join] = 0xBF82_0000 | ((join_pc - br_join - 1) as u32 & 0xFFFF);
words.extend_from_slice(&image_sample_lz);
words.extend_from_slice(&buf_store);
words.push(0xBF81_0000); // s_endpgm
let native = NativeProgram {
stage: HwStage::Cs,
wave64: false,
vgprs: 8,
layout: vec![
RootParam {
root: RootType::Table,
table_offset_dwords: 0,
sgprs: 1,
base_register: 0,
user_data: 0,
slot: 0,
},
RootParam {
root: RootType::Constants,
table_offset_dwords: 0,
sgprs: 1,
base_register: 0,
user_data: 1,
slot: 1,
},
],
used: vec![
UsedResource {
kind: ResourceType::Srv,
root: RootType::Table,
table_offset_dwords: 0,
lower_bound: 0,
stride: 0,
dwords: 8,
user_data: 0,
slot: 0,
},
UsedResource {
kind: ResourceType::Uav,
root: RootType::Table,
table_offset_dwords: 16,
lower_bound: 0,
stride: 0,
dwords: 4,
user_data: 0,
slot: 0,
},
],
io: StageIo::Cs(ComputeIo {
rsrc2: 2 << 1,
threads: [1, 1, 1],
}),
code: words.iter().flat_map(|w| w.to_le_bytes()).collect(),
};
let p = ir::build(&native.code, false, native.vgprs).expect("ir::build");
let st = structure::structure(&p).expect("structure");
let res = resource::track(&p, &native.layout, native.stage, Some(&st)).expect("track");
let io = io::raise(&p, &native).expect("io::raise");
let lowered = lower::lower(&lower::Parts {
native: &native,
p: &p,
st: &st,
io: &io,
res: &res,
inputs: Vec::new(),
outputs: Vec::new(),
prims: Vec::new(),
mesh_stride: 0,
})
.unwrap_or_else(|e| panic!("{case_name}: lower failed: {e}"));
let static_samplers = lowered
.samplers
.iter()
.map(|&w| super::StaticSampler::from_words(w))
.collect();
let shader = HostShader {
stage: HwStage::Cs,
dxil: lowered.dxil,
user_data: lowered.user_data,
samplers: lowered.samplers,
static_samplers,
root_cbvs: lowered.root_cbvs,
root_srvs: lowered.root_srvs,
root_uavs: lowered.root_uavs,
root_indexed_views: Vec::new(),
};
if let Ok(mut v) =
validator::Validator::load(validator::Validator::default_path().as_deref())
{
let mut copy = shader.dxil.clone();
v.validate(&mut copy).expect("DXIL validation failed");
}
let _one = super::valu_tests::ONE_AT_A_TIME
.lock()
.unwrap_or_else(|e| e.into_inner());
for (label, warp) in [("hardware", false), ("warp", true)] {
let Ok(host) = Host::new(warp) else {
continue;
};
let (root, pso) = host
.compute_pipeline(&shader)
.unwrap_or_else(|e| panic!("{case_name}/{label}: compute_pipeline failed: {e}"));
for branch in [0u32, 1u32] {
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, u.slot) {
(RootType::Table, 0) => 0,
(RootType::Constants, 1) => branch,
other => panic!("unexpected user data {other:?}"),
};
}
let texels = [[1.0f32, 0.0, 0.0, 1.0]];
let out = host
.dispatch(
&root,
&pso,
&Dispatch {
roots: vec![(0, Root::Constants(ud))],
views: &[
View::Texture(1, 1, &texels),
View::Texture(1, 1, &texels),
View::RawUav(1),
],
samplers: &[],
groups: [1, 1, 1],
},
)
.unwrap_or_else(|e| panic!("{case_name}/{label}: dispatch failed: {e}"));
let got = f32::from_bits(out[0][0]);
assert_eq!(got, 1.0, "{case_name}/{label} branch={branch}");
}
}
}
}