[Mesa-dev] [PATCH 08/20] mesa/format_utils: Prefix and expose the conversion helper functions
Iago Toral Quiroga
itoral at igalia.com
Tue Nov 18 00:43:56 PST 2014
From: Jason Ekstrand <jason.ekstrand at intel.com>
Signed-off-by: Jason Ekstrand <jason.ekstrand at intel.com>
v2 by Samuel Iglesias <siglesias at igalia.com>:
- Fix compilation errors
Signed-off-by: Samuel Iglesias Gonsalvez <siglesias at igalia.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 8040173..fcbbba4 100644
--- a/src/mesa/main/format_utils.c
+++ b/src/mesa/main/format_utils.c
@@ -133,113 +133,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)
{
@@ -450,42 +343,42 @@ convert_float(void *void_dst, 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);
}
@@ -512,42 +405,42 @@ convert_half_float(void *void_dst, 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));
}
@@ -568,14 +461,14 @@ convert_ubyte(void *void_dst, 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));
}
@@ -585,35 +478,35 @@ convert_ubyte(void *void_dst, 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, MIN2(src, 0xff));
}
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, CLAMP(src, 0, 0xff));
}
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, MIN2(src, 0xff));
}
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, CLAMP(src, 0, 0xff));
}
@@ -634,21 +527,21 @@ convert_byte(void *void_dst, 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, MIN2(src, 0x7f));
}
@@ -658,28 +551,28 @@ convert_byte(void *void_dst, 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, MIN2(src, 0x7f));
}
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, CLAMP(src, -0x80, 0x7f));
}
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, MIN2(src, 0x7f));
}
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, CLAMP(src, -0x80, 0x7f));
}
@@ -700,28 +593,28 @@ convert_ushort(void *void_dst, 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);
}
@@ -731,21 +624,21 @@ convert_ushort(void *void_dst, 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, MIN2(src, 0xffff));
}
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, CLAMP(src, 0, 0xffff));
}
@@ -766,35 +659,35 @@ convert_short(void *void_dst, 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 < 0) ? 0 : src);
}
@@ -804,14 +697,14 @@ convert_short(void *void_dst, 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, MIN2(src, 0x7fff));
}
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, CLAMP(src, -0x8000, 0x7fff));
}
@@ -831,42 +724,42 @@ convert_uint(void *void_dst, int num_dst_channels,
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);
}
@@ -876,7 +769,7 @@ convert_uint(void *void_dst, 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);
}
@@ -897,49 +790,49 @@ convert_int(void *void_dst, 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, MIN2(src, 0x7fffffff));
}
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);
--
1.9.1
More information about the mesa-dev
mailing list