}
static inline nir_ssa_def *
-nir_format_mask_uvec(nir_builder *b, nir_ssa_def *src,
- const unsigned *bits)
+nir_format_mask_uvec(nir_builder *b, nir_ssa_def *src, const unsigned *bits)
{
- nir_const_value mask;
+ nir_const_value mask[NIR_MAX_VEC_COMPONENTS];
+ memset(mask, 0, sizeof(mask));
for (unsigned i = 0; i < src->num_components; i++) {
assert(bits[i] < 32);
- mask.u32[i] = (1u << bits[i]) - 1;
+ mask[i].u32 = (1u << bits[i]) - 1;
}
return nir_iand(b, src, nir_build_imm(b, src->num_components, 32, mask));
}
return packed;
}
+ unsigned next_chan = 0;
unsigned offset = 0;
for (unsigned i = 0; i < num_components; i++) {
assert(bits[i] < bit_size);
assert(offset + bits[i] <= bit_size);
+ nir_ssa_def *chan = nir_channel(b, packed, next_chan);
nir_ssa_def *lshift = nir_imm_int(b, bit_size - (offset + bits[i]));
nir_ssa_def *rshift = nir_imm_int(b, bit_size - bits[i]);
if (sign_extend)
- comps[i] = nir_ishr(b, nir_ishl(b, packed, lshift), rshift);
+ comps[i] = nir_ishr(b, nir_ishl(b, chan, lshift), rshift);
else
- comps[i] = nir_ushr(b, nir_ishl(b, packed, lshift), rshift);
+ comps[i] = nir_ushr(b, nir_ishl(b, chan, lshift), rshift);
offset += bits[i];
+ if (offset >= bit_size) {
+ next_chan++;
+ offset -= bit_size;
+ }
}
- assert(offset <= bit_size);
return nir_vec(b, comps, num_components);
}
}
static inline nir_ssa_def *
-_nir_format_norm_factor(nir_builder *b, unsigned *bits,
+_nir_format_norm_factor(nir_builder *b, const unsigned *bits,
unsigned num_components,
bool is_signed)
{
- nir_const_value factor;
+ nir_const_value factor[NIR_MAX_VEC_COMPONENTS];
+ memset(factor, 0, sizeof(factor));
for (unsigned i = 0; i < num_components; i++) {
- assert(bits[i] < 32);
- factor.f32[i] = (1ul << (bits[i] - is_signed)) - 1;
+ assert(bits[i] <= 32);
+ factor[i].f32 = (1ull << (bits[i] - is_signed)) - 1;
}
return nir_build_imm(b, num_components, 32, factor);
}
static inline nir_ssa_def *
-nir_format_unorm_to_float(nir_builder *b, nir_ssa_def *u, unsigned *bits)
+nir_format_unorm_to_float(nir_builder *b, nir_ssa_def *u, const unsigned *bits)
{
nir_ssa_def *factor =
_nir_format_norm_factor(b, bits, u->num_components, false);
}
static inline nir_ssa_def *
-nir_format_snorm_to_float(nir_builder *b, nir_ssa_def *s, unsigned *bits)
+nir_format_snorm_to_float(nir_builder *b, nir_ssa_def *s, const unsigned *bits)
{
nir_ssa_def *factor =
_nir_format_norm_factor(b, bits, s->num_components, true);
}
static inline nir_ssa_def *
-nir_format_float_to_unorm(nir_builder *b, nir_ssa_def *f, unsigned *bits)
+nir_format_float_to_unorm(nir_builder *b, nir_ssa_def *f, const unsigned *bits)
{
nir_ssa_def *factor =
_nir_format_norm_factor(b, bits, f->num_components, false);
}
static inline nir_ssa_def *
-nir_format_float_to_snorm(nir_builder *b, nir_ssa_def *f, unsigned *bits)
+nir_format_float_to_snorm(nir_builder *b, nir_ssa_def *f, const unsigned *bits)
{
nir_ssa_def *factor =
_nir_format_norm_factor(b, bits, f->num_components, true);
return nir_f2i32(b, nir_fround_even(b, nir_fmul(b, f, factor)));
}
+/* Converts a vector of floats to a vector of half-floats packed in the low 16
+ * bits.
+ */
+static inline nir_ssa_def *
+nir_format_float_to_half(nir_builder *b, nir_ssa_def *f)
+{
+ nir_ssa_def *zero = nir_imm_float(b, 0);
+ nir_ssa_def *f16comps[4];
+ for (unsigned i = 0; i < f->num_components; i++)
+ f16comps[i] = nir_pack_half_2x16_split(b, nir_channel(b, f, i), zero);
+ return nir_vec(b, f16comps, f->num_components);
+}
+
static inline nir_ssa_def *
nir_format_linear_to_srgb(nir_builder *b, nir_ssa_def *c)
{
linear, curved));
}
+/* Clamps a vector of uints so they don't extend beyond the given number of
+ * bits per channel.
+ */
+static inline nir_ssa_def *
+nir_format_clamp_uint(nir_builder *b, nir_ssa_def *f, const unsigned *bits)
+{
+ if (bits[0] == 32)
+ return f;
+
+ nir_const_value max[NIR_MAX_VEC_COMPONENTS];
+ memset(max, 0, sizeof(max));
+ for (unsigned i = 0; i < f->num_components; i++) {
+ assert(bits[i] < 32);
+ max[i].u32 = (1 << bits[i]) - 1;
+ }
+ return nir_umin(b, f, nir_build_imm(b, f->num_components, 32, max));
+}
+
+/* Clamps a vector of sints so they don't extend beyond the given number of
+ * bits per channel.
+ */
+static inline nir_ssa_def *
+nir_format_clamp_sint(nir_builder *b, nir_ssa_def *f, const unsigned *bits)
+{
+ if (bits[0] == 32)
+ return f;
+
+ nir_const_value min[NIR_MAX_VEC_COMPONENTS], max[NIR_MAX_VEC_COMPONENTS];
+ memset(min, 0, sizeof(min));
+ memset(max, 0, sizeof(max));
+ for (unsigned i = 0; i < f->num_components; i++) {
+ assert(bits[i] < 32);
+ max[i].i32 = (1 << (bits[i] - 1)) - 1;
+ min[i].i32 = -(1 << (bits[i] - 1));
+ }
+ f = nir_imin(b, f, nir_build_imm(b, f->num_components, 32, max));
+ f = nir_imax(b, f, nir_build_imm(b, f->num_components, 32, min));
+
+ return f;
+}
+
static inline nir_ssa_def *
nir_format_unpack_11f11f10f(nir_builder *b, nir_ssa_def *packed)
{
nir_ssa_def *chans[3];
chans[0] = nir_mask_shift(b, packed, 0x000007ff, 4);
- chans[1] = nir_mask_shift(b, packed, 0x003ff100, -7);
+ chans[1] = nir_mask_shift(b, packed, 0x003ff800, -7);
chans[2] = nir_mask_shift(b, packed, 0xffc00000, -17);
for (unsigned i = 0; i < 3; i++)