|
@@ -6151,6 +6151,123 @@ impl<'a> Lower<'a> {
|
|
|
Ok(phi)
|
|
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
|
|
/// 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
|
|
/// 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
|
|
/// `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) {
|
|
if cmp && (*a16 || dmask.count_ones() != 1) {
|
|
|
return Err("a comparison sample of 16-bit coordinates or several channels");
|
|
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,
|
|
// Texel offsets (`_o`) come first: x in bits 0-5, y in 8-13,
|
|
|
// signed (programmable offsets: gathers only).
|
|
// signed (programmable offsets: gathers only).
|
|
|
let offset_word = name.ends_with("_o");
|
|
let offset_word = name.ends_with("_o");
|
|
@@ -9239,6 +9372,60 @@ impl<'a> Lower<'a> {
|
|
|
} else {
|
|
} else {
|
|
|
fu
|
|
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, ®) 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 {
|
|
match op {
|
|
|
// Outside a pixel shader there are no screen derivatives:
|
|
// Outside a pixel shader there are no screen derivatives:
|
|
|
// the validator refuses `sample` there ("features
|
|
// the validator refuses `sample` there ("features
|
|
@@ -9252,14 +9439,7 @@ impl<'a> Lower<'a> {
|
|
|
60 if !self.s.pixel => {
|
|
60 if !self.s.pixel => {
|
|
|
args.extend(&os);
|
|
args.extend(&os);
|
|
|
args.push(self.s.m.f32(0));
|
|
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 => {
|
|
60 => {
|
|
|
if offset_word {
|
|
if offset_word {
|
|
@@ -9285,8 +9465,7 @@ impl<'a> Lower<'a> {
|
|
|
}
|
|
}
|
|
|
args.extend(&os);
|
|
args.extend(&os);
|
|
|
args.push(clamp);
|
|
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 => {
|
|
61 => {
|
|
|
// With a16 the bias is an f16 in the low half of its
|
|
// With a16 the bias is an f16 in the low half of its
|
|
@@ -9302,25 +9481,11 @@ impl<'a> Lower<'a> {
|
|
|
args.extend(&os);
|
|
args.extend(&os);
|
|
|
if self.s.pixel {
|
|
if self.s.pixel {
|
|
|
args.extend([bias, clamp]);
|
|
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 {
|
|
} else {
|
|
|
// As above: the bias is the level.
|
|
// As above: the bias is the level.
|
|
|
args.push(bias);
|
|
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 => {
|
|
63 if kind == 5 => {
|
|
@@ -9359,14 +9524,7 @@ impl<'a> Lower<'a> {
|
|
|
let lod = self.s.m.bin(c, Bin::Mul, ln, per_ln2);
|
|
let lod = self.s.m.bin(c, Bin::Mul, ln, per_ln2);
|
|
|
args.extend(&os);
|
|
args.extend(&os);
|
|
|
args.push(lod);
|
|
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 => {
|
|
63 => {
|
|
|
let mut g = vec![fu; 6];
|
|
let mut g = vec![fu; 6];
|
|
@@ -9379,14 +9537,7 @@ impl<'a> Lower<'a> {
|
|
|
args.extend(&os);
|
|
args.extend(&os);
|
|
|
args.extend(g);
|
|
args.extend(g);
|
|
|
args.push(clamp);
|
|
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 => {
|
|
64 | 65 => {
|
|
|
let reference = addr_u32(self, first - 1)?;
|
|
let reference = addr_u32(self, first - 1)?;
|
|
@@ -9399,8 +9550,7 @@ impl<'a> Lower<'a> {
|
|
|
} else {
|
|
} else {
|
|
|
"sampleCmpLevelZero.f32"
|
|
"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 => {
|
|
62 => {
|
|
|
let lod = if name == "image_sample_l" {
|
|
let lod = if name == "image_sample_l" {
|
|
@@ -9411,14 +9561,7 @@ impl<'a> Lower<'a> {
|
|
|
};
|
|
};
|
|
|
args.extend(&os);
|
|
args.extend(&os);
|
|
|
args.push(lod);
|
|
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" {
|
|
if name == "image_gather4" {
|
|
@@ -9500,14 +9643,7 @@ impl<'a> Lower<'a> {
|
|
|
} else {
|
|
} else {
|
|
|
args.extend(&os[..2]);
|
|
args.extend(&os[..2]);
|
|
|
args.push(self.s.m.i32(dmask.trailing_zeros()));
|
|
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)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|