|
|
@@ -6594,10 +6594,7 @@ impl<'a> Lower<'a> {
|
|
|
/// typed loads do (channels past the format's read 0, 0, 0, 1).
|
|
|
// shortcut: the descriptor's channel swizzle is taken as the identity
|
|
|
// (it is for raw views and the descriptors code builds), elements of 4
|
|
|
- // bytes or more as dword-aligned. Norm 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.
|
|
|
+ // bytes or more as dword-aligned.
|
|
|
fn format_load(&mut self, h: V, off: V, format: u8, channels: u32) -> Result<Vec<V>, Why> {
|
|
|
let (bits, numeric) = buffer_format(format).ok_or("a buffer format")?;
|
|
|
let c = self.cur;
|
|
|
@@ -6660,10 +6657,15 @@ impl<'a> Lower<'a> {
|
|
|
// 4 loaded 16_16_UNORM's 65535 as the half 0x3BFF, not 0x3C00.
|
|
|
let x = match numeric {
|
|
|
Numeric::Unorm => {
|
|
|
- let f = self.s.m.cast(c, Cast::UiToFp, field, f32t);
|
|
|
let top = (1u64 << w) - 1;
|
|
|
- let max = self.s.m.f32((top as f32).to_bits());
|
|
|
- let q = self.s.m.bin(c, Bin::SDiv, f, max);
|
|
|
+ let q = if w <= 16 {
|
|
|
+ let q = self.norm_quotient(field, w);
|
|
|
+ self.as_float(q)
|
|
|
+ } else {
|
|
|
+ let f = self.s.m.cast(c, Cast::UiToFp, field, f32t);
|
|
|
+ let max = self.s.m.f32((top as f32).to_bits());
|
|
|
+ self.s.m.bin(c, Bin::SDiv, f, max)
|
|
|
+ };
|
|
|
let (high, one) = (self.s.m.i32(top as u32), self.s.m.f32(1.0f32.to_bits()));
|
|
|
let at_top = self.s.m.cmp(c, pred::EQ, field, high);
|
|
|
let q = self.s.m.select(c, at_top, one, q);
|
|
|
@@ -6671,10 +6673,23 @@ impl<'a> Lower<'a> {
|
|
|
}
|
|
|
Numeric::Snorm => {
|
|
|
let s = signed(self);
|
|
|
- let f = self.s.m.cast(c, Cast::SiToFp, s, f32t);
|
|
|
let top = (1u32 << (w - 1)) - 1;
|
|
|
- let max = self.s.m.f32((top as f32).to_bits());
|
|
|
- let q = self.s.m.bin(c, Bin::SDiv, f, max);
|
|
|
+ let q = if w <= 17 {
|
|
|
+ // The magnitude's quotient, signed (codes at and
|
|
|
+ // below -top are -1.0 below).
|
|
|
+ let (zero, sign) = (self.s.m.i32(0), self.s.m.i32(0x8000_0000));
|
|
|
+ let neg = self.s.m.cmp(c, pred::SLT, s, zero);
|
|
|
+ let minus = self.s.m.bin(c, Bin::Sub, zero, s);
|
|
|
+ let m = self.s.m.select(c, neg, minus, s);
|
|
|
+ let q = self.norm_quotient(m, w - 1);
|
|
|
+ let sign = self.s.m.select(c, neg, sign, zero);
|
|
|
+ let q = self.s.m.bin(c, Bin::Or, q, sign);
|
|
|
+ self.as_float(q)
|
|
|
+ } else {
|
|
|
+ let f = self.s.m.cast(c, Cast::SiToFp, s, f32t);
|
|
|
+ let max = self.s.m.f32((top as f32).to_bits());
|
|
|
+ self.s.m.bin(c, Bin::SDiv, f, max)
|
|
|
+ };
|
|
|
// The lowest value is -1.0 too, as is the one above it.
|
|
|
let (high, one) = (self.s.m.i32(top), self.s.m.f32(1.0f32.to_bits()));
|
|
|
let at_top = self.s.m.cmp(c, pred::SGE, s, high);
|
|
|
@@ -6724,6 +6739,144 @@ impl<'a> Lower<'a> {
|
|
|
Ok(out)
|
|
|
}
|
|
|
|
|
|
+ /// The bits of the float nearest to `a / d`, `d = 2^w - 1`, for codes
|
|
|
+ /// `0 <= a < d` and `w <= 16`: what the console's buffer unit gives for
|
|
|
+ /// a normalized code. The host's division is not exact (the RX 6700 XT
|
|
|
+ /// returns 8-bit codes one ulp high and some 16-bit codes one ulp low),
|
|
|
+ /// and titles truncate `x * d` back to the code (a material index in a
|
|
|
+ /// vertex colour's alpha), which a quotient one ulp low turns into the
|
|
|
+ /// code below.
|
|
|
+ ///
|
|
|
+ /// `Q = floor(a * 2^32 / d) = a * K + floor(a * r / d)` with `K =
|
|
|
+ /// floor(2^32 / d)` and `r = 2^32 mod d` (no division when `r` is 1:
|
|
|
+ /// widths dividing 32). `a / d` is never exact for `0 < a < d` (`d`
|
|
|
+ /// odd, `r` coprime to it), so `Q` rounds to 24 bits up exactly when
|
|
|
+ /// its highest dropped bit is set, and the result is that integer (at
|
|
|
+ /// most 2^24, so its conversion is exact on any host) scaled by a power
|
|
|
+ /// of two; `Q` itself when it fits, the quotient's further bits then
|
|
|
+ /// lying below half an ulp. That holds for every code of every width but
|
|
|
+ /// 11 and 12 (checked for all codes of widths 2 to 16), which take
|
|
|
+ /// [`Self::exact_norm`]. The host's conversion of a larger integer is
|
|
|
+ /// not relied on: WARP rounds those above 2^31 otherwise.
|
|
|
+ fn norm_quotient(&mut self, a: V, w: u32) -> V {
|
|
|
+ if matches!(w, 11 | 12) {
|
|
|
+ return self.exact_norm(a, w);
|
|
|
+ }
|
|
|
+ let c = self.cur;
|
|
|
+ let d = (1u64 << w) - 1;
|
|
|
+ let (kk, r) = ((1u64 << 32) / d, (1u64 << 32) % d);
|
|
|
+ let k = |l: &mut Self, v: u64| l.s.m.i32(v as u32);
|
|
|
+ let (one, kv) = (k(self, 1), k(self, kk));
|
|
|
+ let mut q = self.s.m.bin(c, Bin::Mul, a, kv);
|
|
|
+ if r != 1 {
|
|
|
+ let (rv, dv) = (k(self, r), k(self, d));
|
|
|
+ let t = self.s.m.bin(c, Bin::Mul, a, rv);
|
|
|
+ let more = self.s.m.bin(c, Bin::UDiv, t, dv);
|
|
|
+ q = self.s.m.bin(c, Bin::Add, q, more);
|
|
|
+ }
|
|
|
+ // The bits to drop (1 to 8) from the exponent of `q >> 8`, which
|
|
|
+ // converts exactly (fewer than 25 bits); none when `q` fits.
|
|
|
+ let k8 = k(self, 8);
|
|
|
+ let top = self.s.m.bin(c, Bin::LShr, q, k8);
|
|
|
+ let top = self.s.m.cast(c, Cast::UiToFp, top, self.s.t.f32);
|
|
|
+ let top = self.as_int(top);
|
|
|
+ let k23 = k(self, 23);
|
|
|
+ let e = self.s.m.bin(c, Bin::LShr, top, k23);
|
|
|
+ let bias = k(self, 127 + 15);
|
|
|
+ let shift = self.s.m.bin(c, Bin::Sub, e, bias);
|
|
|
+ let (zero, b24) = (k(self, 0), k(self, 1 << 24));
|
|
|
+ let fits = self.s.m.cmp(c, pred::IULT, q, b24);
|
|
|
+ let shift = self.s.m.select(c, fits, zero, shift);
|
|
|
+ // Rounded up when the highest dropped bit is set: `a / d` is never
|
|
|
+ // exact, so a dropped half is more than half, never a tie.
|
|
|
+ let up = self.s.m.bin(c, Bin::Shl, q, one);
|
|
|
+ let up = self.s.m.bin(c, Bin::LShr, up, shift);
|
|
|
+ let up = self.s.m.bin(c, Bin::And, up, one);
|
|
|
+ let m = self.s.m.bin(c, Bin::LShr, q, shift);
|
|
|
+ let m = self.s.m.bin(c, Bin::Add, m, up);
|
|
|
+ // At most 2^24: exact as a float, and so its scale by 2^(shift - 32).
|
|
|
+ let f = self.s.m.cast(c, Cast::UiToFp, m, self.s.t.f32);
|
|
|
+ let b95 = k(self, 127 - 32);
|
|
|
+ let scale = self.s.m.bin(c, Bin::Add, shift, b95);
|
|
|
+ let scale = self.s.m.bin(c, Bin::Shl, scale, k23);
|
|
|
+ let scale = self.as_float(scale);
|
|
|
+ let f = self.s.m.bin(c, Bin::Mul, f, scale);
|
|
|
+ self.as_int(f)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// [`Self::norm_quotient`] in integers only, for any width up to 16: `Q`
|
|
|
+ /// as there, 8 more bits when that leaves fewer than 25 (codes of wide
|
|
|
+ /// formats near 0), then rounded to 24 bits to nearest even with the
|
|
|
+ /// remainder as sticky bit, and assembled as float bits.
|
|
|
+ fn exact_norm(&mut self, a: V, w: u32) -> V {
|
|
|
+ let c = self.cur;
|
|
|
+ let d = (1u64 << w) - 1;
|
|
|
+ let (kk, r) = ((1u64 << 32) / d, (1u64 << 32) % d);
|
|
|
+ let k = |l: &mut Self, v: u64| l.s.m.i32(v as u32);
|
|
|
+ let (one, zero, dv, k8) = (k(self, 1), k(self, 0), k(self, d), k(self, 8));
|
|
|
+ let kv = k(self, kk);
|
|
|
+ let mut q = self.s.m.bin(c, Bin::Mul, a, kv);
|
|
|
+ let mut rem = a;
|
|
|
+ if r != 1 {
|
|
|
+ let rv = k(self, r);
|
|
|
+ let t = self.s.m.bin(c, Bin::Mul, a, rv);
|
|
|
+ let more = self.s.m.bin(c, Bin::UDiv, t, dv);
|
|
|
+ q = self.s.m.bin(c, Bin::Add, q, more);
|
|
|
+ rem = self.s.m.bin(c, Bin::URem, t, dv);
|
|
|
+ }
|
|
|
+ // The exponent's bias less the 32 (or 40) fraction bits of `q`; the
|
|
|
+ // shift below is added.
|
|
|
+ let mut bias = k(self, 127 + 23 - 32);
|
|
|
+ if kk < 1 << 24 {
|
|
|
+ // Fewer than 25 bits for the smallest codes: 8 more.
|
|
|
+ let t = self.s.m.bin(c, Bin::Shl, rem, k8);
|
|
|
+ let more = self.s.m.bin(c, Bin::UDiv, t, dv);
|
|
|
+ let rest = self.s.m.bin(c, Bin::URem, t, dv);
|
|
|
+ let wide = self.s.m.bin(c, Bin::Shl, q, k8);
|
|
|
+ let wide = self.s.m.bin(c, Bin::Or, wide, more);
|
|
|
+ let limit = k(self, 1 << 24);
|
|
|
+ let small = self.s.m.cmp(c, pred::IULT, q, limit);
|
|
|
+ q = self.s.m.select(c, small, wide, q);
|
|
|
+ rem = self.s.m.select(c, small, rest, rem);
|
|
|
+ let b40 = k(self, 127 + 23 - 40);
|
|
|
+ bias = self.s.m.select(c, small, b40, bias);
|
|
|
+ }
|
|
|
+ // Bits to drop: the bit length less 24, 1 to 8.
|
|
|
+ let mut shift = one;
|
|
|
+ for bit in 25..32 {
|
|
|
+ let at = k(self, 1 << bit);
|
|
|
+ let ge = self.s.m.cmp(c, pred::IUGE, q, at);
|
|
|
+ let add = self.s.m.select(c, ge, one, zero);
|
|
|
+ shift = self.s.m.bin(c, Bin::Add, shift, add);
|
|
|
+ }
|
|
|
+ let mant = self.s.m.bin(c, Bin::LShr, q, shift);
|
|
|
+ let below = self.s.m.bin(c, Bin::Sub, shift, one);
|
|
|
+ let half = self.s.m.bin(c, Bin::LShr, q, below);
|
|
|
+ let half = self.s.m.bin(c, Bin::And, half, one);
|
|
|
+ let low = self.s.m.bin(c, Bin::Shl, one, below);
|
|
|
+ let low = self.s.m.bin(c, Bin::Sub, low, one);
|
|
|
+ let low = self.s.m.bin(c, Bin::And, q, low);
|
|
|
+ let rest = self.s.m.bin(c, Bin::Or, low, rem);
|
|
|
+ let sticky = self.s.m.cmp(c, pred::NE, rest, zero);
|
|
|
+ let odd = self.s.m.bin(c, Bin::And, mant, one);
|
|
|
+ let tie = self.s.m.select(c, sticky, one, odd);
|
|
|
+ let up = self.s.m.bin(c, Bin::And, half, tie);
|
|
|
+ let mant = self.s.m.bin(c, Bin::Add, mant, up);
|
|
|
+ // Rounding up to 2^24 moves to the next exponent.
|
|
|
+ let k24 = k(self, 24);
|
|
|
+ let carry = self.s.m.bin(c, Bin::LShr, mant, k24);
|
|
|
+ let mant = self.s.m.bin(c, Bin::LShr, mant, carry);
|
|
|
+ let e = self.s.m.bin(c, Bin::Add, bias, shift);
|
|
|
+ let e = self.s.m.bin(c, Bin::Add, e, carry);
|
|
|
+ let k23 = k(self, 23);
|
|
|
+ let e = self.s.m.bin(c, Bin::Shl, e, k23);
|
|
|
+ let fraction = k(self, 0x7F_FFFF);
|
|
|
+ let m = self.s.m.bin(c, Bin::And, mant, fraction);
|
|
|
+ let bits = self.s.m.bin(c, Bin::Or, e, m);
|
|
|
+ let is_zero = self.s.m.cmp(c, pred::EQ, a, zero);
|
|
|
+ self.s.m.select(c, is_zero, zero, bits)
|
|
|
+ }
|
|
|
+
|
|
|
/// Channels of buffer format `format`, as [`Self::format_load`] gives
|
|
|
/// them, as d16 data: 16 bits each, two per dword, low half first (an
|
|
|
/// odd last high half 0). Integer formats keep their low 16 bits; the
|
|
|
@@ -7152,6 +7305,33 @@ impl<'a> Lower<'a> {
|
|
|
/// `ResourceHeap`): read from the host's table by heap slot in the
|
|
|
/// current block.
|
|
|
fn slot_stride(&mut self, src: &Source) -> Result<Option<V>, Why> {
|
|
|
+ let word = self.slot_word(src)?;
|
|
|
+ Ok(word.map(|word| {
|
|
|
+ let bits = self.s.m.i32(super::shader::VIEW_STRIDE_BITS);
|
|
|
+ self.s.m.bin(self.cur, Bin::And, word, bits)
|
|
|
+ }))
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Whether the typed buffer view `src` names is one of an integer format,
|
|
|
+ /// and whether a signed one, as [`Self::slot_stride`] (`None`: a view the
|
|
|
+ /// program does not pick from the heap).
|
|
|
+ fn integer_view(&mut self, src: &Source) -> Result<Option<(V, V)>, Why> {
|
|
|
+ let Some(word) = self.slot_word(src)? else {
|
|
|
+ return Ok(None);
|
|
|
+ };
|
|
|
+ let c = self.cur;
|
|
|
+ let zero = self.s.m.i32(0);
|
|
|
+ let [int, signed] = [super::shader::VIEW_INTEGER, super::shader::VIEW_SIGNED].map(|bit| {
|
|
|
+ let bit = self.s.m.i32(bit);
|
|
|
+ let x = self.s.m.bin(c, Bin::And, word, bit);
|
|
|
+ self.s.m.cmp(c, pred::NE, x, zero)
|
|
|
+ });
|
|
|
+ Ok(Some((int, signed)))
|
|
|
+ }
|
|
|
+
|
|
|
+ /// The host's word for the buffer view `src` names: its stride and
|
|
|
+ /// format kind ([`super::shader::SLOT_STRIDES`]).
|
|
|
+ fn slot_word(&mut self, src: &Source) -> Result<Option<V>, Why> {
|
|
|
let Source::Loaded { load, first } = src else {
|
|
|
return Ok(None);
|
|
|
};
|
|
|
@@ -10938,11 +11118,14 @@ impl<'a> Lower<'a> {
|
|
|
let width = self.s.m.extract(c, d, 0);
|
|
|
let width = self.s.m.cast(c, Cast::UiToFp, width, self.s.t.f32);
|
|
|
let texels = self.s.m.bin(c, Bin::Mul, longer, width);
|
|
|
- // log2 = natural logarithm / ln 2; a length of 0 gives
|
|
|
+ // DXIL's logarithm is the one to base 2 (it was taken for
|
|
|
+ // the natural one: every level came out 1.44 times too
|
|
|
+ // high; DexNom/VIctory 921d8d3b). A length of 0 gives
|
|
|
// minus infinity, which the sampler clamps to level 0.
|
|
|
- let ln = self.unary(23, texels);
|
|
|
- let per_ln2 = self.s.m.f32(std::f32::consts::LOG2_E.to_bits());
|
|
|
- let lod = self.s.m.bin(c, Bin::Mul, ln, per_ln2);
|
|
|
+ // SampleLevel takes no clamp: the instruction's least
|
|
|
+ // level is applied here.
|
|
|
+ let lod = self.unary(23, texels);
|
|
|
+ let lod = if has_cl { self.binary(35, lod, clamp) } else { lod };
|
|
|
args.extend(&os);
|
|
|
args.push(lod);
|
|
|
call_sample(self, "sampleLevel.f32", 62, &args, &mut gathered)
|
|
|
@@ -11509,8 +11692,14 @@ impl<'a> Lower<'a> {
|
|
|
let mut vals =
|
|
|
self.vgprs(inst, data.as_ref().ok_or("buffer store data")?, regs as u8)?;
|
|
|
if d16 {
|
|
|
- // shortcut: d16 data is taken as f16 (integer formats would
|
|
|
- // keep 16 bits), widened for the 32-bit float store.
|
|
|
+ // d16 data is taken as f16, widened for the 32-bit float
|
|
|
+ // store; through a view of an integer format it is a 16-bit
|
|
|
+ // integer, zero- or sign-extended (VIctory 4243a96a: the face
|
|
|
+ // network keeps its halves in UINT views; its output was
|
|
|
+ // noise). shortcut: that is known of the views a program
|
|
|
+ // picks from the heap, not of those in its tables; and the
|
|
|
+ // handle stays a float one, whose bits the drivers pass.
|
|
|
+ let integer = if typed { self.integer_view(src)? } else { None };
|
|
|
let mut wide = Vec::new();
|
|
|
for k in 0..count {
|
|
|
let mut x = vals[(k / 2) as usize];
|
|
|
@@ -11522,7 +11711,16 @@ impl<'a> Lower<'a> {
|
|
|
let f = self
|
|
|
.s
|
|
|
.op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[x]);
|
|
|
- wide.push(self.as_int(f));
|
|
|
+ let mut w = self.as_int(f);
|
|
|
+ if let Some((int, signed)) = integer {
|
|
|
+ let (k16, low) = (self.s.m.i32(16), self.s.m.i32(0xFFFF));
|
|
|
+ let zero_ext = self.s.m.bin(c, Bin::And, x, low);
|
|
|
+ let up = self.s.m.bin(c, Bin::Shl, x, k16);
|
|
|
+ let sign_ext = self.s.m.bin(c, Bin::AShr, up, k16);
|
|
|
+ let n = self.s.m.select(c, signed, sign_ext, zero_ext);
|
|
|
+ w = self.s.m.select(c, int, n, w);
|
|
|
+ }
|
|
|
+ wide.push(w);
|
|
|
}
|
|
|
vals = wide;
|
|
|
}
|
|
|
@@ -11657,16 +11855,24 @@ impl<'a> Lower<'a> {
|
|
|
return Err("a buffer result");
|
|
|
};
|
|
|
if d16 {
|
|
|
- // Two f16 channels per VGPR (an odd last high half 0).
|
|
|
+ // Two f16 channels per VGPR (an odd last high half 0); the low
|
|
|
+ // 16 bits of an integer view's (see the stores above).
|
|
|
+ let integer = if typed { self.integer_view(src)? } else { None };
|
|
|
let k16 = self.s.m.i32(16);
|
|
|
let i32t = self.s.t.i32;
|
|
|
for k in 0..count.div_ceil(2) {
|
|
|
let mut x = None;
|
|
|
for ch in (2 * k..(2 * k + 2).min(count)).rev() {
|
|
|
let f = self.s.m.extract(c, r, ch);
|
|
|
- let h = self
|
|
|
+ let mut h = self
|
|
|
.s
|
|
|
.op(c, "legacyF32ToF16", Attr::ReadNone, i32t, 130, &[f]);
|
|
|
+ if let Some((int, _)) = integer {
|
|
|
+ let bits = self.as_int(f);
|
|
|
+ let low = self.s.m.i32(0xFFFF);
|
|
|
+ let low = self.s.m.bin(c, Bin::And, bits, low);
|
|
|
+ h = self.s.m.select(c, int, low, h);
|
|
|
+ }
|
|
|
x = Some(match x {
|
|
|
Some(hi) => {
|
|
|
let hi = self.s.m.bin(c, Bin::Shl, hi, k16);
|