Signed-off-by: Jason Ekstrand <jason.ekstr...@intel.com> --- src/mesa/main/format_utils.c | 215 +++++++++++-------------------------------- src/mesa/main/format_utils.h | 105 +++++++++++++++++++++ 2 files changed, 159 insertions(+), 161 deletions(-)
diff --git a/src/mesa/main/format_utils.c b/src/mesa/main/format_utils.c index 7a00169..5f9328e 100644 --- a/src/mesa/main/format_utils.c +++ b/src/mesa/main/format_utils.c @@ -132,113 +132,6 @@ _mesa_format_to_array(mesa_format format, GLenum *type, int *num_components, } } -/* A bunch of format conversion macros and helper functions used below */ - -/* Only guaranteed to work for BITS <= 32 */ -#define MAX_UINT(BITS) ((BITS) == 32 ? UINT32_MAX : ((1u << (BITS)) - 1)) -#define MAX_INT(BITS) ((int)MAX_UINT((BITS) - 1)) - -/* Extends an integer of size SRC_BITS to one of size DST_BITS linearly */ -#define EXTEND_NORMALIZED_INT(X, SRC_BITS, DST_BITS) \ - (((X) * (int)(MAX_UINT(DST_BITS) / MAX_UINT(SRC_BITS))) + \ - ((DST_BITS % SRC_BITS) ? ((X) >> (SRC_BITS - DST_BITS % SRC_BITS)) : 0)) - -static inline float -unorm_to_float(unsigned x, unsigned src_bits) -{ - return x * (1.0f / (float)MAX_UINT(src_bits)); -} - -static inline float -snorm_to_float(int x, unsigned src_bits) -{ - if (x < -MAX_INT(src_bits)) - return -1.0f; - else - return x * (1.0f / (float)MAX_INT(src_bits)); -} - -static inline uint16_t -unorm_to_half(unsigned x, unsigned src_bits) -{ - return _mesa_float_to_half(unorm_to_float(x, src_bits)); -} - -static inline uint16_t -snorm_to_half(int x, unsigned src_bits) -{ - return _mesa_float_to_half(snorm_to_float(x, src_bits)); -} - -static inline unsigned -float_to_unorm(float x, unsigned dst_bits) -{ - if (x < 0.0f) - return 0; - else if (x > 1.0f) - return MAX_UINT(dst_bits); - else - return F_TO_I(x * MAX_UINT(dst_bits)); -} - -static inline unsigned -half_to_unorm(uint16_t x, unsigned dst_bits) -{ - return float_to_unorm(_mesa_half_to_float(x), dst_bits); -} - -static inline unsigned -unorm_to_unorm(unsigned x, unsigned src_bits, unsigned dst_bits) -{ - if (src_bits < dst_bits) - return EXTEND_NORMALIZED_INT(x, src_bits, dst_bits); - else - return x >> (src_bits - dst_bits); -} - -static inline unsigned -snorm_to_unorm(int x, unsigned src_bits, unsigned dst_bits) -{ - if (x < 0) - return 0; - else - return unorm_to_unorm(x, src_bits - 1, dst_bits); -} - -static inline int -float_to_snorm(float x, unsigned dst_bits) -{ - if (x < -1.0f) - return -MAX_INT(dst_bits); - else if (x > 1.0f) - return MAX_INT(dst_bits); - else - return F_TO_I(x * MAX_INT(dst_bits)); -} - -static inline int -half_to_snorm(uint16_t x, unsigned dst_bits) -{ - return float_to_snorm(_mesa_half_to_float(x), dst_bits); -} - -static inline int -unorm_to_snorm(unsigned x, unsigned src_bits, unsigned dst_bits) -{ - return unorm_to_unorm(x, src_bits, dst_bits - 1); -} - -static inline int -snorm_to_snorm(int x, unsigned src_bits, unsigned dst_bits) -{ - if (x < -MAX_INT(src_bits)) - return -MAX_INT(dst_bits); - else if (src_bits < dst_bits) - return EXTEND_NORMALIZED_INT(x, src_bits - 1, dst_bits - 1); - else - return x >> (src_bits - dst_bits); -} - static inline unsigned float_to_uint(float x) { @@ -504,42 +397,42 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, break; case GL_UNSIGNED_BYTE: if (normalized) { - SWIZZLE_CONVERT(float, uint8_t, unorm_to_float(src, 8)) + SWIZZLE_CONVERT(float, uint8_t, _mesa_unorm_to_float(src, 8)) } else { SWIZZLE_CONVERT(float, uint8_t, src) } break; case GL_BYTE: if (normalized) { - SWIZZLE_CONVERT(float, int8_t, snorm_to_float(src, 8)) + SWIZZLE_CONVERT(float, int8_t, _mesa_snorm_to_float(src, 8)) } else { SWIZZLE_CONVERT(float, int8_t, src) } break; case GL_UNSIGNED_SHORT: if (normalized) { - SWIZZLE_CONVERT(float, uint16_t, unorm_to_float(src, 16)) + SWIZZLE_CONVERT(float, uint16_t, _mesa_unorm_to_float(src, 16)) } else { SWIZZLE_CONVERT(float, uint16_t, src) } break; case GL_SHORT: if (normalized) { - SWIZZLE_CONVERT(float, int16_t, snorm_to_float(src, 16)) + SWIZZLE_CONVERT(float, int16_t, _mesa_snorm_to_float(src, 16)) } else { SWIZZLE_CONVERT(float, int16_t, src) } break; case GL_UNSIGNED_INT: if (normalized) { - SWIZZLE_CONVERT(float, uint32_t, unorm_to_float(src, 32)) + SWIZZLE_CONVERT(float, uint32_t, _mesa_unorm_to_float(src, 32)) } else { SWIZZLE_CONVERT(float, uint32_t, src) } break; case GL_INT: if (normalized) { - SWIZZLE_CONVERT(float, int32_t, snorm_to_float(src, 32)) + SWIZZLE_CONVERT(float, int32_t, _mesa_snorm_to_float(src, 32)) } else { SWIZZLE_CONVERT(float, int32_t, src) } @@ -561,42 +454,42 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, break; case GL_UNSIGNED_BYTE: if (normalized) { - SWIZZLE_CONVERT(uint16_t, uint8_t, unorm_to_half(src, 8)) + SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_unorm_to_half(src, 8)) } else { SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_float_to_half(src)) } break; case GL_BYTE: if (normalized) { - SWIZZLE_CONVERT(uint16_t, int8_t, snorm_to_half(src, 8)) + SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_snorm_to_half(src, 8)) } else { SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_float_to_half(src)) } break; case GL_UNSIGNED_SHORT: if (normalized) { - SWIZZLE_CONVERT(uint16_t, uint16_t, unorm_to_half(src, 16)) + SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_unorm_to_half(src, 16)) } else { SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_float_to_half(src)) } break; case GL_SHORT: if (normalized) { - SWIZZLE_CONVERT(uint16_t, int16_t, snorm_to_half(src, 16)) + SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_snorm_to_half(src, 16)) } else { SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_float_to_half(src)) } break; case GL_UNSIGNED_INT: if (normalized) { - SWIZZLE_CONVERT(uint16_t, uint32_t, unorm_to_half(src, 32)) + SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unorm_to_half(src, 32)) } else { SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_float_to_half(src)) } break; case GL_INT: if (normalized) { - SWIZZLE_CONVERT(uint16_t, int32_t, snorm_to_half(src, 32)) + SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_snorm_to_half(src, 32)) } else { SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_float_to_half(src)) } @@ -612,14 +505,14 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, switch (src_type) { case GL_FLOAT: if (normalized) { - SWIZZLE_CONVERT(uint8_t, float, float_to_unorm(src, 8)) + SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_unorm(src, 8)) } else { SWIZZLE_CONVERT(uint8_t, float, (src < 0) ? 0 : src) } break; case GL_HALF_FLOAT: if (normalized) { - SWIZZLE_CONVERT(uint8_t, uint16_t, half_to_unorm(src, 8)) + SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_unorm(src, 8)) } else { SWIZZLE_CONVERT(uint8_t, uint16_t, half_to_uint(src)) } @@ -629,35 +522,35 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, break; case GL_BYTE: if (normalized) { - SWIZZLE_CONVERT(uint8_t, int8_t, snorm_to_unorm(src, 8, 8)) + SWIZZLE_CONVERT(uint8_t, int8_t, _mesa_snorm_to_unorm(src, 8, 8)) } else { SWIZZLE_CONVERT(uint8_t, int8_t, (src < 0) ? 0 : src) } break; case GL_UNSIGNED_SHORT: if (normalized) { - SWIZZLE_CONVERT(uint8_t, uint16_t, unorm_to_unorm(src, 16, 8)) + SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_unorm_to_unorm(src, 16, 8)) } else { SWIZZLE_CONVERT(uint8_t, uint16_t, src) } break; case GL_SHORT: if (normalized) { - SWIZZLE_CONVERT(uint8_t, int16_t, snorm_to_unorm(src, 16, 8)) + SWIZZLE_CONVERT(uint8_t, int16_t, _mesa_snorm_to_unorm(src, 16, 8)) } else { SWIZZLE_CONVERT(uint8_t, int16_t, (src < 0) ? 0 : src) } break; case GL_UNSIGNED_INT: if (normalized) { - SWIZZLE_CONVERT(uint8_t, uint32_t, unorm_to_unorm(src, 32, 8)) + SWIZZLE_CONVERT(uint8_t, uint32_t, _mesa_unorm_to_unorm(src, 32, 8)) } else { SWIZZLE_CONVERT(uint8_t, uint32_t, src) } break; case GL_INT: if (normalized) { - SWIZZLE_CONVERT(uint8_t, int32_t, snorm_to_unorm(src, 32, 8)) + SWIZZLE_CONVERT(uint8_t, int32_t, _mesa_snorm_to_unorm(src, 32, 8)) } else { SWIZZLE_CONVERT(uint8_t, int32_t, (src < 0) ? 0 : src) } @@ -673,21 +566,21 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, switch (src_type) { case GL_FLOAT: if (normalized) { - SWIZZLE_CONVERT(uint8_t, float, float_to_snorm(src, 8)) + SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_snorm(src, 8)) } else { SWIZZLE_CONVERT(uint8_t, float, src) } break; case GL_HALF_FLOAT: if (normalized) { - SWIZZLE_CONVERT(uint8_t, uint16_t, half_to_snorm(src, 8)) + SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_snorm(src, 8)) } else { SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_float(src)) } break; case GL_UNSIGNED_BYTE: if (normalized) { - SWIZZLE_CONVERT(int8_t, uint8_t, unorm_to_snorm(src, 8, 8)) + SWIZZLE_CONVERT(int8_t, uint8_t, _mesa_unorm_to_snorm(src, 8, 8)) } else { SWIZZLE_CONVERT(int8_t, uint8_t, src) } @@ -697,28 +590,28 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, break; case GL_UNSIGNED_SHORT: if (normalized) { - SWIZZLE_CONVERT(int8_t, uint16_t, unorm_to_snorm(src, 16, 8)) + SWIZZLE_CONVERT(int8_t, uint16_t, _mesa_unorm_to_snorm(src, 16, 8)) } else { SWIZZLE_CONVERT(int8_t, uint16_t, src) } break; case GL_SHORT: if (normalized) { - SWIZZLE_CONVERT(int8_t, int16_t, snorm_to_snorm(src, 16, 8)) + SWIZZLE_CONVERT(int8_t, int16_t, _mesa_snorm_to_snorm(src, 16, 8)) } else { SWIZZLE_CONVERT(int8_t, int16_t, src) } break; case GL_UNSIGNED_INT: if (normalized) { - SWIZZLE_CONVERT(int8_t, uint32_t, unorm_to_snorm(src, 32, 8)) + SWIZZLE_CONVERT(int8_t, uint32_t, _mesa_unorm_to_snorm(src, 32, 8)) } else { SWIZZLE_CONVERT(int8_t, uint32_t, src) } break; case GL_INT: if (normalized) { - SWIZZLE_CONVERT(int8_t, int32_t, snorm_to_snorm(src, 32, 8)) + SWIZZLE_CONVERT(int8_t, int32_t, _mesa_snorm_to_snorm(src, 32, 8)) } else { SWIZZLE_CONVERT(int8_t, int32_t, src) } @@ -734,28 +627,28 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, switch (src_type) { case GL_FLOAT: if (normalized) { - SWIZZLE_CONVERT(uint16_t, float, float_to_unorm(src, 16)) + SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_unorm(src, 16)) } else { SWIZZLE_CONVERT(uint16_t, float, (src < 0) ? 0 : src) } break; case GL_HALF_FLOAT: if (normalized) { - SWIZZLE_CONVERT(uint16_t, uint16_t, half_to_unorm(src, 16)) + SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_unorm(src, 16)) } else { SWIZZLE_CONVERT(uint16_t, uint16_t, half_to_uint(src)) } break; case GL_UNSIGNED_BYTE: if (normalized) { - SWIZZLE_CONVERT(uint16_t, uint8_t, unorm_to_unorm(src, 8, 16)) + SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_unorm_to_unorm(src, 8, 16)) } else { SWIZZLE_CONVERT(uint16_t, uint8_t, src) } break; case GL_BYTE: if (normalized) { - SWIZZLE_CONVERT(uint16_t, int8_t, snorm_to_unorm(src, 8, 16)) + SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_snorm_to_unorm(src, 8, 16)) } else { SWIZZLE_CONVERT(uint16_t, int8_t, (src < 0) ? 0 : src) } @@ -765,21 +658,21 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, break; case GL_SHORT: if (normalized) { - SWIZZLE_CONVERT(uint16_t, int16_t, snorm_to_unorm(src, 16, 16)) + SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_snorm_to_unorm(src, 16, 16)) } else { SWIZZLE_CONVERT(uint16_t, int16_t, (src < 0) ? 0 : src) } break; case GL_UNSIGNED_INT: if (normalized) { - SWIZZLE_CONVERT(uint16_t, uint32_t, unorm_to_unorm(src, 32, 16)) + SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unorm_to_unorm(src, 32, 16)) } else { SWIZZLE_CONVERT(uint16_t, uint32_t, src) } break; case GL_INT: if (normalized) { - SWIZZLE_CONVERT(uint16_t, int32_t, snorm_to_unorm(src, 32, 16)) + SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_snorm_to_unorm(src, 32, 16)) } else { SWIZZLE_CONVERT(uint16_t, int32_t, (src < 0) ? 0 : src) } @@ -795,35 +688,35 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, switch (src_type) { case GL_FLOAT: if (normalized) { - SWIZZLE_CONVERT(uint16_t, float, float_to_snorm(src, 16)) + SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_snorm(src, 16)) } else { SWIZZLE_CONVERT(uint16_t, float, src) } break; case GL_HALF_FLOAT: if (normalized) { - SWIZZLE_CONVERT(uint16_t, uint16_t, half_to_snorm(src, 16)) + SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_snorm(src, 16)) } else { SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_float(src)) } break; case GL_UNSIGNED_BYTE: if (normalized) { - SWIZZLE_CONVERT(int16_t, uint8_t, unorm_to_snorm(src, 8, 16)) + SWIZZLE_CONVERT(int16_t, uint8_t, _mesa_unorm_to_snorm(src, 8, 16)) } else { SWIZZLE_CONVERT(int16_t, uint8_t, src) } break; case GL_BYTE: if (normalized) { - SWIZZLE_CONVERT(int16_t, int8_t, snorm_to_snorm(src, 8, 16)) + SWIZZLE_CONVERT(int16_t, int8_t, _mesa_snorm_to_snorm(src, 8, 16)) } else { SWIZZLE_CONVERT(int16_t, int8_t, src) } break; case GL_UNSIGNED_SHORT: if (normalized) { - SWIZZLE_CONVERT(int16_t, uint16_t, unorm_to_snorm(src, 16, 16)) + SWIZZLE_CONVERT(int16_t, uint16_t, _mesa_unorm_to_snorm(src, 16, 16)) } else { SWIZZLE_CONVERT(int16_t, uint16_t, src) } @@ -833,14 +726,14 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, break; case GL_UNSIGNED_INT: if (normalized) { - SWIZZLE_CONVERT(int16_t, uint32_t, unorm_to_snorm(src, 32, 16)) + SWIZZLE_CONVERT(int16_t, uint32_t, _mesa_unorm_to_snorm(src, 32, 16)) } else { SWIZZLE_CONVERT(int16_t, uint32_t, src) } break; case GL_INT: if (normalized) { - SWIZZLE_CONVERT(int16_t, int32_t, snorm_to_snorm(src, 32, 16)) + SWIZZLE_CONVERT(int16_t, int32_t, _mesa_snorm_to_snorm(src, 32, 16)) } else { SWIZZLE_CONVERT(int16_t, int32_t, src) } @@ -855,42 +748,42 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, const uint32_t one = normalized ? UINT32_MAX : 1; switch (src_type) { case GL_FLOAT: if (normalized) { - SWIZZLE_CONVERT(uint32_t, float, float_to_unorm(src, 32)) + SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_unorm(src, 32)) } else { SWIZZLE_CONVERT(uint32_t, float, (src < 0) ? 0 : src) } break; case GL_HALF_FLOAT: if (normalized) { - SWIZZLE_CONVERT(uint32_t, uint16_t, half_to_unorm(src, 32)) + SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_unorm(src, 32)) } else { SWIZZLE_CONVERT(uint32_t, uint16_t, half_to_uint(src)) } break; case GL_UNSIGNED_BYTE: if (normalized) { - SWIZZLE_CONVERT(uint32_t, uint8_t, unorm_to_unorm(src, 8, 32)) + SWIZZLE_CONVERT(uint32_t, uint8_t, _mesa_unorm_to_unorm(src, 8, 32)) } else { SWIZZLE_CONVERT(uint32_t, uint8_t, src) } break; case GL_BYTE: if (normalized) { - SWIZZLE_CONVERT(uint32_t, int8_t, snorm_to_unorm(src, 8, 32)) + SWIZZLE_CONVERT(uint32_t, int8_t, _mesa_snorm_to_unorm(src, 8, 32)) } else { SWIZZLE_CONVERT(uint32_t, int8_t, (src < 0) ? 0 : src) } break; case GL_UNSIGNED_SHORT: if (normalized) { - SWIZZLE_CONVERT(uint32_t, uint16_t, unorm_to_unorm(src, 16, 32)) + SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_unorm_to_unorm(src, 16, 32)) } else { SWIZZLE_CONVERT(uint32_t, uint16_t, src) } break; case GL_SHORT: if (normalized) { - SWIZZLE_CONVERT(uint32_t, int16_t, snorm_to_unorm(src, 16, 32)) + SWIZZLE_CONVERT(uint32_t, int16_t, _mesa_snorm_to_unorm(src, 16, 32)) } else { SWIZZLE_CONVERT(uint32_t, int16_t, (src < 0) ? 0 : src) } @@ -900,7 +793,7 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, break; case GL_INT: if (normalized) { - SWIZZLE_CONVERT(uint32_t, int32_t, snorm_to_unorm(src, 32, 32)) + SWIZZLE_CONVERT(uint32_t, int32_t, _mesa_snorm_to_unorm(src, 32, 32)) } else { SWIZZLE_CONVERT(uint32_t, int32_t, (src < 0) ? 0 : src) } @@ -916,49 +809,49 @@ _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels, switch (src_type) { case GL_FLOAT: if (normalized) { - SWIZZLE_CONVERT(uint32_t, float, float_to_snorm(src, 32)) + SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_snorm(src, 32)) } else { SWIZZLE_CONVERT(uint32_t, float, src) } break; case GL_HALF_FLOAT: if (normalized) { - SWIZZLE_CONVERT(uint32_t, uint16_t, half_to_snorm(src, 32)) + SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_snorm(src, 32)) } else { SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_float(src)) } break; case GL_UNSIGNED_BYTE: if (normalized) { - SWIZZLE_CONVERT(int32_t, uint8_t, unorm_to_snorm(src, 8, 32)) + SWIZZLE_CONVERT(int32_t, uint8_t, _mesa_unorm_to_snorm(src, 8, 32)) } else { SWIZZLE_CONVERT(int32_t, uint8_t, src) } break; case GL_BYTE: if (normalized) { - SWIZZLE_CONVERT(int32_t, int8_t, snorm_to_snorm(src, 8, 32)) + SWIZZLE_CONVERT(int32_t, int8_t, _mesa_snorm_to_snorm(src, 8, 32)) } else { SWIZZLE_CONVERT(int32_t, int8_t, src) } break; case GL_UNSIGNED_SHORT: if (normalized) { - SWIZZLE_CONVERT(int32_t, uint16_t, unorm_to_snorm(src, 16, 32)) + SWIZZLE_CONVERT(int32_t, uint16_t, _mesa_unorm_to_snorm(src, 16, 32)) } else { SWIZZLE_CONVERT(int32_t, uint16_t, src) } break; case GL_SHORT: if (normalized) { - SWIZZLE_CONVERT(int32_t, int16_t, snorm_to_snorm(src, 16, 32)) + SWIZZLE_CONVERT(int32_t, int16_t, _mesa_snorm_to_snorm(src, 16, 32)) } else { SWIZZLE_CONVERT(int32_t, int16_t, src) } break; case GL_UNSIGNED_INT: if (normalized) { - SWIZZLE_CONVERT(int32_t, uint32_t, unorm_to_snorm(src, 32, 32)) + SWIZZLE_CONVERT(int32_t, uint32_t, _mesa_unorm_to_snorm(src, 32, 32)) } else { SWIZZLE_CONVERT(int32_t, uint32_t, src) } diff --git a/src/mesa/main/format_utils.h b/src/mesa/main/format_utils.h index 9f778e3..010d137 100644 --- a/src/mesa/main/format_utils.h +++ b/src/mesa/main/format_utils.h @@ -33,6 +33,111 @@ #include "imports.h" +/* Only guaranteed to work for BITS <= 32 */ +#define MAX_UINT(BITS) ((BITS) == 32 ? UINT32_MAX : ((1u << (BITS)) - 1)) +#define MAX_INT(BITS) ((int)MAX_UINT((BITS) - 1)) + +/* Extends an integer of size SRC_BITS to one of size DST_BITS linearly */ +#define EXTEND_NORMALIZED_INT(X, SRC_BITS, DST_BITS) \ + (((X) * (int)(MAX_UINT(DST_BITS) / MAX_UINT(SRC_BITS))) + \ + ((DST_BITS % SRC_BITS) ? ((X) >> (SRC_BITS - DST_BITS % SRC_BITS)) : 0)) + +static inline float +_mesa_unorm_to_float(unsigned x, unsigned src_bits) +{ + return x * (1.0f / (float)MAX_UINT(src_bits)); +} + +static inline float +_mesa_snorm_to_float(int x, unsigned src_bits) +{ + if (x < -MAX_INT(src_bits)) + return -1.0f; + else + return x * (1.0f / (float)MAX_INT(src_bits)); +} + +static inline uint16_t +_mesa_unorm_to_half(unsigned x, unsigned src_bits) +{ + return _mesa_float_to_half(_mesa_unorm_to_float(x, src_bits)); +} + +static inline uint16_t +_mesa_snorm_to_half(int x, unsigned src_bits) +{ + return _mesa_float_to_half(_mesa_snorm_to_float(x, src_bits)); +} + +static inline unsigned +_mesa_float_to_unorm(float x, unsigned dst_bits) +{ + if (x < 0.0f) + return 0; + else if (x > 1.0f) + return MAX_UINT(dst_bits); + else + return F_TO_I(x * MAX_UINT(dst_bits)); +} + +static inline unsigned +_mesa_half_to_unorm(uint16_t x, unsigned dst_bits) +{ + return _mesa_float_to_unorm(_mesa_half_to_float(x), dst_bits); +} + +static inline unsigned +_mesa_unorm_to_unorm(unsigned x, unsigned src_bits, unsigned dst_bits) +{ + if (src_bits < dst_bits) + return EXTEND_NORMALIZED_INT(x, src_bits, dst_bits); + else + return x >> (src_bits - dst_bits); +} + +static inline unsigned +_mesa_snorm_to_unorm(int x, unsigned src_bits, unsigned dst_bits) +{ + if (x < 0) + return 0; + else + return _mesa_unorm_to_unorm(x, src_bits - 1, dst_bits); +} + +static inline int +_mesa_float_to_snorm(float x, unsigned dst_bits) +{ + if (x < -1.0f) + return -MAX_INT(dst_bits); + else if (x > 1.0f) + return MAX_INT(dst_bits); + else + return F_TO_I(x * MAX_INT(dst_bits)); +} + +static inline int +_mesa_half_to_snorm(uint16_t x, unsigned dst_bits) +{ + return _mesa_float_to_snorm(_mesa_half_to_float(x), dst_bits); +} + +static inline int +_mesa_unorm_to_snorm(unsigned x, unsigned src_bits, unsigned dst_bits) +{ + return _mesa_unorm_to_unorm(x, src_bits, dst_bits - 1); +} + +static inline int +_mesa_snorm_to_snorm(int x, unsigned src_bits, unsigned dst_bits) +{ + if (x < -MAX_INT(src_bits)) + return -MAX_INT(dst_bits); + else if (src_bits < dst_bits) + return EXTEND_NORMALIZED_INT(x, src_bits - 1, dst_bits - 1); + else + return x >> (src_bits - dst_bits); +} + bool _mesa_format_to_array(mesa_format, GLenum *type, int *num_components, uint8_t swizzle[4], bool *normalized); -- 2.1.0 _______________________________________________ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev