[Mesa-dev] [PATCH 08/22] nir/format_convert: Add [us]norm conversion helpers

Kenneth Graunke kenneth at whitecape.org
Mon Aug 20 07:44:46 UTC 2018


On Friday, August 17, 2018 1:06:14 PM PDT Jason Ekstrand wrote:
> ---
>  src/compiler/nir/nir_format_convert.h | 56 +++++++++++++++++++++++++++
>  1 file changed, 56 insertions(+)
> 
> diff --git a/src/compiler/nir/nir_format_convert.h b/src/compiler/nir/nir_format_convert.h
> index f2eafcbf5b4..75793733ea6 100644
> --- a/src/compiler/nir/nir_format_convert.h
> +++ b/src/compiler/nir/nir_format_convert.h
> @@ -201,6 +201,62 @@ nir_format_bitcast_uvec_unmasked(nir_builder *b, nir_ssa_def *src,
>     return nir_vec(b, dst_chan, dst_components);
>  }
>  
> +static inline nir_ssa_def *
> +_nir_format_norm_factor(nir_builder *b, unsigned *bits,
> +                        unsigned num_components,
> +                        bool is_signed)
> +{
> +   nir_const_value factor;
> +   for (unsigned i = 0; i < num_components; i++) {
> +      assert(bits[i] < 32);
> +      factor.f32[i] = (1ul << (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_ssa_def *factor =
> +      _nir_format_norm_factor(b, bits, u->num_components, false);
> +
> +   return nir_fdiv(b, nir_u2f32(b, u), factor);
> +}
> +
> +static inline nir_ssa_def *
> +nir_format_snorm_to_float(nir_builder *b, nir_ssa_def *s, unsigned *bits)
> +{
> +   nir_ssa_def *factor =
> +      _nir_format_norm_factor(b, bits, s->num_components, true);
> +
> +   return nir_fmax(b, nir_fdiv(b, nir_i2f32(b, s), factor),
> +                      nir_imm_float(b, -1.0f));
> +}
> +
> +static inline nir_ssa_def *
> +nir_format_float_to_unorm(nir_builder *b, nir_ssa_def *f, unsigned *bits)
> +{
> +   nir_ssa_def *factor =
> +      _nir_format_norm_factor(b, bits, f->num_components, false);
> +
> +   /* Clamp to the range [0, 1] */
> +   f = nir_fsat(b, f);
> +
> +   return nir_f2u32(b, nir_fround_even(b, nir_fmul(b, f, factor)));
> +}
> +
> +static inline nir_ssa_def *
> +nir_format_float_to_snorm(nir_builder *b, nir_ssa_def *f, unsigned *bits)
> +{
> +   nir_ssa_def *factor =
> +      _nir_format_norm_factor(b, bits, f->num_components, true);
> +
> +   /* Clamp to the range [0, 1] */

Should be [-1, 1] here.

Could always move nir_fclamp from nir_builtin_builder.h to nir_builder.h
and then use that here, too.  Would be ever so slightly simpler.

With the comment fixed,
Reviewed-by: Kenneth Graunke <kenneth at whitecape.org>

> +   f = nir_fmin(b, nir_fmax(b, f, nir_imm_float(b, -1)), nir_imm_float(b, 1));
> +
> +   return nir_f2i32(b, nir_fround_even(b, nir_fmul(b, f, factor)));
> +}
> +
>  static inline nir_ssa_def *
>  nir_format_linear_to_srgb(nir_builder *b, nir_ssa_def *c)
>  {
> 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20180820/0037cc3d/attachment-0001.sig>


More information about the mesa-dev mailing list