summaryrefslogtreecommitdiffstats
path: root/src/mesa/main
diff options
context:
space:
mode:
authorSamuel Iglesias Gonsalvez <[email protected]>2014-10-27 16:15:36 +0100
committerIago Toral Quiroga <[email protected]>2015-01-12 11:20:27 +0100
commitfea1be8d0bd53f817f62d9b4fa82cfcb667fd3c3 (patch)
treedb9fd82be67f8fbf78d1d38e3dcff990a15bbc6c /src/mesa/main
parent483b04348848e193d7c71c2f40ce58c447d51755 (diff)
mesa: Fix _mesa_swizzle_and_convert integer conversions to clamp properly
Fix various conversion paths that involved integer data types of different sizes (uint16_t to uint8_t, int16_t to uint8_t, etc) that were not being clamped properly. Also, one of the paths was incorrectly assigning the value 12, instead of 1, to the constant "one". v2: - Create auxiliary clamping functions and use them in all paths that required clamp because of different source and destination sizes and signed-unsigned conversions. v3: - Create MIN_INT macro and use it. v4: - Add _mesa_float_to_[un]signed() and mesa_half_to_[un]signed() auxiliary functions. - Add clamp for float-to-integer conversions in _mesa_swizzle_and_convert() Signed-off-by: Samuel Iglesias Gonsalvez <[email protected]> Reviewed-by: Jason Ekstrand <[email protected]>
Diffstat (limited to 'src/mesa/main')
-rw-r--r--src/mesa/main/format_utils.c87
-rw-r--r--src/mesa/main/format_utils.h60
2 files changed, 94 insertions, 53 deletions
diff --git a/src/mesa/main/format_utils.c b/src/mesa/main/format_utils.c
index 41fd0433d7a..a6b88834453 100644
--- a/src/mesa/main/format_utils.c
+++ b/src/mesa/main/format_utils.c
@@ -132,24 +132,6 @@ _mesa_format_to_array(mesa_format format, GLenum *type, int *num_components,
}
}
-static inline unsigned
-float_to_uint(float x)
-{
- if (x < 0.0f)
- return 0;
- else
- return x;
-}
-
-static inline unsigned
-half_to_uint(uint16_t x)
-{
- if (_mesa_half_is_negative(x))
- return 0;
- else
- return _mesa_float_to_half(x);
-}
-
/**
* Attempts to perform the given swizzle-and-convert operation with memcpy
*
@@ -449,7 +431,6 @@ convert_half_float(void *void_dst, int num_dst_channels,
}
}
-
static void
convert_ubyte(void *void_dst, int num_dst_channels,
const void *void_src, GLenum src_type, int num_src_channels,
@@ -462,14 +443,14 @@ convert_ubyte(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_unorm(src, 8));
} else {
- SWIZZLE_CONVERT(uint8_t, float, (src < 0) ? 0 : src);
+ SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_unsigned(src, 8));
}
break;
case GL_HALF_FLOAT:
if (normalized) {
SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_unorm(src, 8));
} else {
- SWIZZLE_CONVERT(uint8_t, uint16_t, half_to_uint(src));
+ SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_unsigned(src, 8));
}
break;
case GL_UNSIGNED_BYTE:
@@ -479,35 +460,35 @@ convert_ubyte(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(uint8_t, int8_t, _mesa_snorm_to_unorm(src, 8, 8));
} else {
- SWIZZLE_CONVERT(uint8_t, int8_t, (src < 0) ? 0 : src);
+ SWIZZLE_CONVERT(uint8_t, int8_t, _mesa_signed_to_unsigned(src, 8));
}
break;
case GL_UNSIGNED_SHORT:
if (normalized) {
SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_unorm_to_unorm(src, 16, 8));
} else {
- SWIZZLE_CONVERT(uint8_t, uint16_t, src);
+ SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_unsigned_to_unsigned(src, 8));
}
break;
case GL_SHORT:
if (normalized) {
SWIZZLE_CONVERT(uint8_t, int16_t, _mesa_snorm_to_unorm(src, 16, 8));
} else {
- SWIZZLE_CONVERT(uint8_t, int16_t, (src < 0) ? 0 : src);
+ SWIZZLE_CONVERT(uint8_t, int16_t, _mesa_signed_to_unsigned(src, 8));
}
break;
case GL_UNSIGNED_INT:
if (normalized) {
SWIZZLE_CONVERT(uint8_t, uint32_t, _mesa_unorm_to_unorm(src, 32, 8));
} else {
- SWIZZLE_CONVERT(uint8_t, uint32_t, src);
+ SWIZZLE_CONVERT(uint8_t, uint32_t, _mesa_unsigned_to_unsigned(src, 8));
}
break;
case GL_INT:
if (normalized) {
SWIZZLE_CONVERT(uint8_t, int32_t, _mesa_snorm_to_unorm(src, 32, 8));
} else {
- SWIZZLE_CONVERT(uint8_t, int32_t, (src < 0) ? 0 : src);
+ SWIZZLE_CONVERT(uint8_t, int32_t, _mesa_signed_to_unsigned(src, 8));
}
break;
default:
@@ -528,21 +509,21 @@ convert_byte(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_snorm(src, 8));
} else {
- SWIZZLE_CONVERT(uint8_t, float, src);
+ SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_signed(src, 8));
}
break;
case GL_HALF_FLOAT:
if (normalized) {
SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_snorm(src, 8));
} else {
- SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_float(src));
+ SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_signed(src, 8));
}
break;
case GL_UNSIGNED_BYTE:
if (normalized) {
SWIZZLE_CONVERT(int8_t, uint8_t, _mesa_unorm_to_snorm(src, 8, 8));
} else {
- SWIZZLE_CONVERT(int8_t, uint8_t, src);
+ SWIZZLE_CONVERT(int8_t, uint8_t, _mesa_unsigned_to_signed(src, 8));
}
break;
case GL_BYTE:
@@ -552,28 +533,28 @@ convert_byte(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(int8_t, uint16_t, _mesa_unorm_to_snorm(src, 16, 8));
} else {
- SWIZZLE_CONVERT(int8_t, uint16_t, src);
+ SWIZZLE_CONVERT(int8_t, uint16_t, _mesa_unsigned_to_signed(src, 8));
}
break;
case GL_SHORT:
if (normalized) {
SWIZZLE_CONVERT(int8_t, int16_t, _mesa_snorm_to_snorm(src, 16, 8));
} else {
- SWIZZLE_CONVERT(int8_t, int16_t, src);
+ SWIZZLE_CONVERT(int8_t, int16_t, _mesa_signed_to_signed(src, 8));
}
break;
case GL_UNSIGNED_INT:
if (normalized) {
SWIZZLE_CONVERT(int8_t, uint32_t, _mesa_unorm_to_snorm(src, 32, 8));
} else {
- SWIZZLE_CONVERT(int8_t, uint32_t, src);
+ SWIZZLE_CONVERT(int8_t, uint32_t, _mesa_unsigned_to_signed(src, 8));
}
break;
case GL_INT:
if (normalized) {
SWIZZLE_CONVERT(int8_t, int32_t, _mesa_snorm_to_snorm(src, 32, 8));
} else {
- SWIZZLE_CONVERT(int8_t, int32_t, src);
+ SWIZZLE_CONVERT(int8_t, int32_t, _mesa_signed_to_signed(src, 8));
}
break;
default:
@@ -594,14 +575,14 @@ convert_ushort(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_unorm(src, 16));
} else {
- SWIZZLE_CONVERT(uint16_t, float, (src < 0) ? 0 : src);
+ SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_unsigned(src, 16));
}
break;
case GL_HALF_FLOAT:
if (normalized) {
SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_unorm(src, 16));
} else {
- SWIZZLE_CONVERT(uint16_t, uint16_t, half_to_uint(src));
+ SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_unsigned(src, 16));
}
break;
case GL_UNSIGNED_BYTE:
@@ -615,7 +596,7 @@ convert_ushort(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_snorm_to_unorm(src, 8, 16));
} else {
- SWIZZLE_CONVERT(uint16_t, int8_t, (src < 0) ? 0 : src);
+ SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_signed_to_unsigned(src, 16));
}
break;
case GL_UNSIGNED_SHORT:
@@ -625,21 +606,21 @@ convert_ushort(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_snorm_to_unorm(src, 16, 16));
} else {
- SWIZZLE_CONVERT(uint16_t, int16_t, (src < 0) ? 0 : src);
+ SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_signed_to_unsigned(src, 16));
}
break;
case GL_UNSIGNED_INT:
if (normalized) {
SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unorm_to_unorm(src, 32, 16));
} else {
- SWIZZLE_CONVERT(uint16_t, uint32_t, src);
+ SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unsigned_to_unsigned(src, 16));
}
break;
case GL_INT:
if (normalized) {
SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_snorm_to_unorm(src, 32, 16));
} else {
- SWIZZLE_CONVERT(uint16_t, int32_t, (src < 0) ? 0 : src);
+ SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_signed_to_unsigned(src, 16));
}
break;
default:
@@ -660,14 +641,14 @@ convert_short(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_snorm(src, 16));
} else {
- SWIZZLE_CONVERT(uint16_t, float, src);
+ SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_signed(src, 16));
}
break;
case GL_HALF_FLOAT:
if (normalized) {
SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_snorm(src, 16));
} else {
- SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_float(src));
+ SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_signed(src, 16));
}
break;
case GL_UNSIGNED_BYTE:
@@ -688,7 +669,7 @@ convert_short(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(int16_t, uint16_t, _mesa_unorm_to_snorm(src, 16, 16));
} else {
- SWIZZLE_CONVERT(int16_t, uint16_t, src);
+ SWIZZLE_CONVERT(int16_t, uint16_t, _mesa_unsigned_to_signed(src, 16));
}
break;
case GL_SHORT:
@@ -698,14 +679,14 @@ convert_short(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(int16_t, uint32_t, _mesa_unorm_to_snorm(src, 32, 16));
} else {
- SWIZZLE_CONVERT(int16_t, uint32_t, src);
+ SWIZZLE_CONVERT(int16_t, uint32_t, _mesa_unsigned_to_signed(src, 16));
}
break;
case GL_INT:
if (normalized) {
SWIZZLE_CONVERT(int16_t, int32_t, _mesa_snorm_to_snorm(src, 32, 16));
} else {
- SWIZZLE_CONVERT(int16_t, int32_t, src);
+ SWIZZLE_CONVERT(int16_t, int32_t, _mesa_signed_to_signed(src, 16));
}
break;
default:
@@ -725,14 +706,14 @@ convert_uint(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_unorm(src, 32));
} else {
- SWIZZLE_CONVERT(uint32_t, float, (src < 0) ? 0 : src);
+ SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_unsigned(src, 32));
}
break;
case GL_HALF_FLOAT:
if (normalized) {
SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_unorm(src, 32));
} else {
- SWIZZLE_CONVERT(uint32_t, uint16_t, half_to_uint(src));
+ SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_unsigned(src, 32));
}
break;
case GL_UNSIGNED_BYTE:
@@ -746,7 +727,7 @@ convert_uint(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(uint32_t, int8_t, _mesa_snorm_to_unorm(src, 8, 32));
} else {
- SWIZZLE_CONVERT(uint32_t, int8_t, (src < 0) ? 0 : src);
+ SWIZZLE_CONVERT(uint32_t, int8_t, _mesa_signed_to_unsigned(src, 32));
}
break;
case GL_UNSIGNED_SHORT:
@@ -760,7 +741,7 @@ convert_uint(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(uint32_t, int16_t, _mesa_snorm_to_unorm(src, 16, 32));
} else {
- SWIZZLE_CONVERT(uint32_t, int16_t, (src < 0) ? 0 : src);
+ SWIZZLE_CONVERT(uint32_t, int16_t, _mesa_signed_to_unsigned(src, 32));
}
break;
case GL_UNSIGNED_INT:
@@ -770,7 +751,7 @@ convert_uint(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(uint32_t, int32_t, _mesa_snorm_to_unorm(src, 32, 32));
} else {
- SWIZZLE_CONVERT(uint32_t, int32_t, (src < 0) ? 0 : src);
+ SWIZZLE_CONVERT(uint32_t, int32_t, _mesa_signed_to_unsigned(src, 32));
}
break;
default:
@@ -784,21 +765,21 @@ convert_int(void *void_dst, int num_dst_channels,
const void *void_src, GLenum src_type, int num_src_channels,
const uint8_t swizzle[4], bool normalized, int count)
{
- const int32_t one = normalized ? INT32_MAX : 12;
+ const int32_t one = normalized ? INT32_MAX : 1;
switch (src_type) {
case GL_FLOAT:
if (normalized) {
SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_snorm(src, 32));
} else {
- SWIZZLE_CONVERT(uint32_t, float, src);
+ SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_signed(src, 32));
}
break;
case GL_HALF_FLOAT:
if (normalized) {
SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_snorm(src, 32));
} else {
- SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_float(src));
+ SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_signed(src, 32));
}
break;
case GL_UNSIGNED_BYTE:
@@ -833,7 +814,7 @@ convert_int(void *void_dst, int num_dst_channels,
if (normalized) {
SWIZZLE_CONVERT(int32_t, uint32_t, _mesa_unorm_to_snorm(src, 32, 32));
} else {
- SWIZZLE_CONVERT(int32_t, uint32_t, src);
+ SWIZZLE_CONVERT(int32_t, uint32_t, _mesa_unsigned_to_signed(src, 32));
}
break;
case GL_INT:
diff --git a/src/mesa/main/format_utils.h b/src/mesa/main/format_utils.h
index df53edf4f81..4e63200da69 100644
--- a/src/mesa/main/format_utils.h
+++ b/src/mesa/main/format_utils.h
@@ -32,10 +32,12 @@
#define FORMAT_UTILS_H
#include "imports.h"
+#include "macros.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))
+#define MIN_INT(BITS) ((BITS) == 32 ? INT32_MIN : (-(1 << (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) \
@@ -138,6 +140,64 @@ _mesa_snorm_to_snorm(int x, unsigned src_bits, unsigned dst_bits)
return x >> (src_bits - dst_bits);
}
+static inline unsigned
+_mesa_unsigned_to_unsigned(unsigned src, unsigned dst_size)
+{
+ return MIN2(src, MAX_UINT(dst_size));
+}
+
+static inline int
+_mesa_unsigned_to_signed(unsigned src, unsigned dst_size)
+{
+ return MIN2(src, MAX_INT(dst_size));
+}
+
+static inline int
+_mesa_signed_to_signed(int src, unsigned dst_size)
+{
+ return CLAMP(src, MIN_INT(dst_size), MAX_INT(dst_size));
+}
+
+static inline unsigned
+_mesa_signed_to_unsigned(int src, unsigned dst_size)
+{
+ return CLAMP(src, 0, MAX_UINT(dst_size));
+}
+
+static inline unsigned
+_mesa_float_to_unsigned(float src, unsigned dst_bits)
+{
+ if (src < 0.0f)
+ return 0;
+ if (src > (float)MAX_UINT(dst_bits))
+ return MAX_UINT(dst_bits);
+ return _mesa_signed_to_unsigned(src, dst_bits);
+}
+
+static inline unsigned
+_mesa_float_to_signed(float src, unsigned dst_bits)
+{
+ if (src < (float)(-MAX_INT(dst_bits)))
+ return -MAX_INT(dst_bits);
+ if (src > (float)MAX_INT(dst_bits))
+ return MAX_INT(dst_bits);
+ return _mesa_signed_to_signed(src, dst_bits);
+}
+
+static inline unsigned
+_mesa_half_to_unsigned(uint16_t src, unsigned dst_bits)
+{
+ if (_mesa_half_is_negative(src))
+ return 0;
+ return _mesa_unsigned_to_unsigned(_mesa_float_to_half(src), dst_bits);
+}
+
+static inline unsigned
+_mesa_half_to_signed(uint16_t src, unsigned dst_bits)
+{
+ return _mesa_float_to_signed(_mesa_half_to_float(src), dst_bits);
+}
+
bool
_mesa_format_to_array(mesa_format, GLenum *type, int *num_components,
uint8_t swizzle[4], bool *normalized);