[Mesa-dev] [PATCH 1/9] gallium/u_math: add ushort_to_float/float_to_ushort

Roland Scheidegger sroland at vmware.com
Sat Mar 16 03:11:53 UTC 2019


Am 16.03.19 um 02:28 schrieb Qiang Yu:
> Signed-off-by: Qiang Yu <yuq825 at gmail.com>
> ---
>  src/util/u_math.h | 31 +++++++++++++++++++++++++++++++
>  1 file changed, 31 insertions(+)
> 
> diff --git a/src/util/u_math.h b/src/util/u_math.h
> index e7dbbe5ca22..ffadfb47282 100644
> --- a/src/util/u_math.h
> +++ b/src/util/u_math.h
> @@ -389,6 +389,37 @@ float_to_ubyte(float f)
>     }
>  }
>  
> +/**
> + * Convert ushort to float in [0, 1].
> + */
> +static inline float
> +ushort_to_float(ushort us)
> +{
> +   return (float) us * (1.0f / 65535.0f);
> +}
> +
> +
> +/**
> + * Convert float in [0,1] to ushort in [0,65535] with clamping.
> + */
> +static inline ushort
> +float_to_ushort(float f)
> +{
> +   union fi tmp;
> +
> +   tmp.f = f;
> +   if (tmp.i < 0) {
> +      return (ushort) 0;
> +   }
> +   else if (tmp.i >= 0x3f800000 /* 1.0f */) {
> +      return (ushort) 65535;
> +   }
This will convert NaNs to either 0 or 65535, depending on their sign.
I think generally it's better to convert this consistently to 0 (gl
usually doesn't require this, however d3d10 does).

Roland


> +   else {
> +      tmp.f = tmp.f * (65535.0f/65536.0f) + 128.0f;
> +      return (ushort) tmp.i;
> +   }
> +}
> +
>  static inline float
>  byte_to_float_tex(int8_t b)
>  {
> 



More information about the mesa-dev mailing list