<div dir="ltr"><div>I may have brought this up before (I'm starting to not remember things) but shouldn't we also be clamping float-to-integer as well?<br></div>--Jason<br><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 9, 2014 at 4:06 AM, Iago Toral Quiroga <span dir="ltr"><<a href="mailto:itoral@igalia.com" target="_blank">itoral@igalia.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Samuel Iglesias Gonsalvez <<a href="mailto:siglesias@igalia.com">siglesias@igalia.com</a>><br>
<br>
Fix various conversion paths that involved integer data types of different<br>
sizes (uint16_t to uint8_t, int16_t to uint8_t, etc) that were not<br>
being clamped properly.<br>
<br>
Also, one of the paths was incorrectly assigning the value 12, instead of 1,<br>
to the constant "one".<br>
<br>
v2:<br>
- Create auxiliary clamping functions and use them in all paths that<br>
required clamp because of different source and destination sizes<br>
and signed-unsigned conversions.<br>
<br>
v3:<br>
- Create MIN_INT macro and use it.<br>
<br>
Signed-off-by: Samuel Iglesias Gonsalvez <<a href="mailto:siglesias@igalia.com">siglesias@igalia.com</a>><br>
---<br>
src/mesa/main/format_utils.c | 47 ++++++++++++++++++++++----------------------<br>
src/mesa/main/format_utils.h | 26 ++++++++++++++++++++++++<br>
2 files changed, 49 insertions(+), 24 deletions(-)<br>
<br>
diff --git a/src/mesa/main/format_utils.c b/src/mesa/main/format_utils.c<br>
index 41fd043..dc63e1f 100644<br>
--- a/src/mesa/main/format_utils.c<br>
+++ b/src/mesa/main/format_utils.c<br>
@@ -449,7 +449,6 @@ convert_half_float(void *void_dst, int num_dst_channels,<br>
}<br>
}<br>
<br>
-<br>
static void<br>
convert_ubyte(void *void_dst, int num_dst_channels,<br>
const void *void_src, GLenum src_type, int num_src_channels,<br>
@@ -469,7 +468,7 @@ convert_ubyte(void *void_dst, int num_dst_channels,<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_unorm(src, 8));<br>
} else {<br>
- SWIZZLE_CONVERT(uint8_t, uint16_t, half_to_uint(src));<br>
+ SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_unsigned_to_unsigned(half_to_uint(src), 8));<br>
}<br>
break;<br>
case GL_UNSIGNED_BYTE:<br>
@@ -479,35 +478,35 @@ convert_ubyte(void *void_dst, int num_dst_channels,<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint8_t, int8_t, _mesa_snorm_to_unorm(src, 8, 8));<br>
} else {<br>
- SWIZZLE_CONVERT(uint8_t, int8_t, (src < 0) ? 0 : src);<br>
+ SWIZZLE_CONVERT(uint8_t, int8_t, _mesa_signed_to_unsigned(src, 8));<br>
}<br>
break;<br>
case GL_UNSIGNED_SHORT:<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_unorm_to_unorm(src, 16, 8));<br>
} else {<br>
- SWIZZLE_CONVERT(uint8_t, uint16_t, src);<br>
+ SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_unsigned_to_unsigned(src, 8));<br>
}<br>
break;<br>
case GL_SHORT:<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint8_t, int16_t, _mesa_snorm_to_unorm(src, 16, 8));<br>
} else {<br>
- SWIZZLE_CONVERT(uint8_t, int16_t, (src < 0) ? 0 : src);<br>
+ SWIZZLE_CONVERT(uint8_t, int16_t, _mesa_signed_to_unsigned(src, 8));<br>
}<br>
break;<br>
case GL_UNSIGNED_INT:<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint8_t, uint32_t, _mesa_unorm_to_unorm(src, 32, 8));<br>
} else {<br>
- SWIZZLE_CONVERT(uint8_t, uint32_t, src);<br>
+ SWIZZLE_CONVERT(uint8_t, uint32_t, _mesa_unsigned_to_unsigned(src, 8));<br>
}<br>
break;<br>
case GL_INT:<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint8_t, int32_t, _mesa_snorm_to_unorm(src, 32, 8));<br>
} else {<br>
- SWIZZLE_CONVERT(uint8_t, int32_t, (src < 0) ? 0 : src);<br>
+ SWIZZLE_CONVERT(uint8_t, int32_t, _mesa_signed_to_unsigned(src, 8));<br>
}<br>
break;<br>
default:<br>
@@ -542,7 +541,7 @@ convert_byte(void *void_dst, int num_dst_channels,<br>
if (normalized) {<br>
SWIZZLE_CONVERT(int8_t, uint8_t, _mesa_unorm_to_snorm(src, 8, 8));<br>
} else {<br>
- SWIZZLE_CONVERT(int8_t, uint8_t, src);<br>
+ SWIZZLE_CONVERT(int8_t, uint8_t, _mesa_unsigned_to_signed(src, 8));<br>
}<br>
break;<br>
case GL_BYTE:<br>
@@ -552,28 +551,28 @@ convert_byte(void *void_dst, int num_dst_channels,<br>
if (normalized) {<br>
SWIZZLE_CONVERT(int8_t, uint16_t, _mesa_unorm_to_snorm(src, 16, 8));<br>
} else {<br>
- SWIZZLE_CONVERT(int8_t, uint16_t, src);<br>
+ SWIZZLE_CONVERT(int8_t, uint16_t, _mesa_unsigned_to_signed(src, 8));<br>
}<br>
break;<br>
case GL_SHORT:<br>
if (normalized) {<br>
SWIZZLE_CONVERT(int8_t, int16_t, _mesa_snorm_to_snorm(src, 16, 8));<br>
} else {<br>
- SWIZZLE_CONVERT(int8_t, int16_t, src);<br>
+ SWIZZLE_CONVERT(int8_t, int16_t, _mesa_signed_to_signed(src, 8));<br>
}<br>
break;<br>
case GL_UNSIGNED_INT:<br>
if (normalized) {<br>
SWIZZLE_CONVERT(int8_t, uint32_t, _mesa_unorm_to_snorm(src, 32, 8));<br>
} else {<br>
- SWIZZLE_CONVERT(int8_t, uint32_t, src);<br>
+ SWIZZLE_CONVERT(int8_t, uint32_t, _mesa_unsigned_to_signed(src, 8));<br>
}<br>
break;<br>
case GL_INT:<br>
if (normalized) {<br>
SWIZZLE_CONVERT(int8_t, int32_t, _mesa_snorm_to_snorm(src, 32, 8));<br>
} else {<br>
- SWIZZLE_CONVERT(int8_t, int32_t, src);<br>
+ SWIZZLE_CONVERT(int8_t, int32_t, _mesa_signed_to_signed(src, 8));<br>
}<br>
break;<br>
default:<br>
@@ -615,7 +614,7 @@ convert_ushort(void *void_dst, int num_dst_channels,<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_snorm_to_unorm(src, 8, 16));<br>
} else {<br>
- SWIZZLE_CONVERT(uint16_t, int8_t, (src < 0) ? 0 : src);<br>
+ SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_signed_to_unsigned(src, 16));<br>
}<br>
break;<br>
case GL_UNSIGNED_SHORT:<br>
@@ -625,21 +624,21 @@ convert_ushort(void *void_dst, int num_dst_channels,<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_snorm_to_unorm(src, 16, 16));<br>
} else {<br>
- SWIZZLE_CONVERT(uint16_t, int16_t, (src < 0) ? 0 : src);<br>
+ SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_signed_to_unsigned(src, 16));<br>
}<br>
break;<br>
case GL_UNSIGNED_INT:<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unorm_to_unorm(src, 32, 16));<br>
} else {<br>
- SWIZZLE_CONVERT(uint16_t, uint32_t, src);<br>
+ SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unsigned_to_unsigned(src, 16));<br>
}<br>
break;<br>
case GL_INT:<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_snorm_to_unorm(src, 32, 16));<br>
} else {<br>
- SWIZZLE_CONVERT(uint16_t, int32_t, (src < 0) ? 0 : src);<br>
+ SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_signed_to_unsigned(src, 16));<br>
}<br>
break;<br>
default:<br>
@@ -688,7 +687,7 @@ convert_short(void *void_dst, int num_dst_channels,<br>
if (normalized) {<br>
SWIZZLE_CONVERT(int16_t, uint16_t, _mesa_unorm_to_snorm(src, 16, 16));<br>
} else {<br>
- SWIZZLE_CONVERT(int16_t, uint16_t, src);<br>
+ SWIZZLE_CONVERT(int16_t, uint16_t, _mesa_unsigned_to_signed(src, 16));<br>
}<br>
break;<br>
case GL_SHORT:<br>
@@ -698,14 +697,14 @@ convert_short(void *void_dst, int num_dst_channels,<br>
if (normalized) {<br>
SWIZZLE_CONVERT(int16_t, uint32_t, _mesa_unorm_to_snorm(src, 32, 16));<br>
} else {<br>
- SWIZZLE_CONVERT(int16_t, uint32_t, src);<br>
+ SWIZZLE_CONVERT(int16_t, uint32_t, _mesa_unsigned_to_signed(src, 16));<br>
}<br>
break;<br>
case GL_INT:<br>
if (normalized) {<br>
SWIZZLE_CONVERT(int16_t, int32_t, _mesa_snorm_to_snorm(src, 32, 16));<br>
} else {<br>
- SWIZZLE_CONVERT(int16_t, int32_t, src);<br>
+ SWIZZLE_CONVERT(int16_t, int32_t, _mesa_signed_to_signed(src, 16));<br>
}<br>
break;<br>
default:<br>
@@ -746,7 +745,7 @@ convert_uint(void *void_dst, int num_dst_channels,<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint32_t, int8_t, _mesa_snorm_to_unorm(src, 8, 32));<br>
} else {<br>
- SWIZZLE_CONVERT(uint32_t, int8_t, (src < 0) ? 0 : src);<br>
+ SWIZZLE_CONVERT(uint32_t, int8_t, _mesa_signed_to_unsigned(src, 32));<br>
}<br>
break;<br>
case GL_UNSIGNED_SHORT:<br>
@@ -760,7 +759,7 @@ convert_uint(void *void_dst, int num_dst_channels,<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint32_t, int16_t, _mesa_snorm_to_unorm(src, 16, 32));<br>
} else {<br>
- SWIZZLE_CONVERT(uint32_t, int16_t, (src < 0) ? 0 : src);<br>
+ SWIZZLE_CONVERT(uint32_t, int16_t, _mesa_signed_to_unsigned(src, 32));<br>
}<br>
break;<br>
case GL_UNSIGNED_INT:<br>
@@ -770,7 +769,7 @@ convert_uint(void *void_dst, int num_dst_channels,<br>
if (normalized) {<br>
SWIZZLE_CONVERT(uint32_t, int32_t, _mesa_snorm_to_unorm(src, 32, 32));<br>
} else {<br>
- SWIZZLE_CONVERT(uint32_t, int32_t, (src < 0) ? 0 : src);<br>
+ SWIZZLE_CONVERT(uint32_t, int32_t, _mesa_signed_to_unsigned(src, 32));<br>
}<br>
break;<br>
default:<br>
@@ -784,7 +783,7 @@ convert_int(void *void_dst, int num_dst_channels,<br>
const void *void_src, GLenum src_type, int num_src_channels,<br>
const uint8_t swizzle[4], bool normalized, int count)<br>
{<br>
- const int32_t one = normalized ? INT32_MAX : 12;<br>
+ const int32_t one = normalized ? INT32_MAX : 1;<br>
<br>
switch (src_type) {<br>
case GL_FLOAT:<br>
@@ -833,7 +832,7 @@ convert_int(void *void_dst, int num_dst_channels,<br>
if (normalized) {<br>
SWIZZLE_CONVERT(int32_t, uint32_t, _mesa_unorm_to_snorm(src, 32, 32));<br>
} else {<br>
- SWIZZLE_CONVERT(int32_t, uint32_t, src);<br>
+ SWIZZLE_CONVERT(int32_t, uint32_t, _mesa_unsigned_to_signed(src, 32));<br>
}<br>
break;<br>
case GL_INT:<br>
diff --git a/src/mesa/main/format_utils.h b/src/mesa/main/format_utils.h<br>
index df53edf..fb00f55 100644<br>
--- a/src/mesa/main/format_utils.h<br>
+++ b/src/mesa/main/format_utils.h<br>
@@ -32,10 +32,12 @@<br>
#define FORMAT_UTILS_H<br>
<br>
#include "imports.h"<br>
+#include "macros.h"<br>
<br>
/* Only guaranteed to work for BITS <= 32 */<br>
#define MAX_UINT(BITS) ((BITS) == 32 ? UINT32_MAX : ((1u << (BITS)) - 1))<br>
#define MAX_INT(BITS) ((int)MAX_UINT((BITS) - 1))<br>
+#define MIN_INT(BITS) ((BITS) == 32 ? INT32_MIN : (-(1 << (BITS - 1))))<br>
<br>
/* Extends an integer of size SRC_BITS to one of size DST_BITS linearly */<br>
#define EXTEND_NORMALIZED_INT(X, SRC_BITS, DST_BITS) \<br>
@@ -138,6 +140,30 @@ _mesa_snorm_to_snorm(int x, unsigned src_bits, unsigned dst_bits)<br>
return x >> (src_bits - dst_bits);<br>
}<br>
<br>
+static inline unsigned<br>
+_mesa_unsigned_to_unsigned(unsigned src, unsigned dst_size)<br>
+{<br>
+ return MIN2(src, MAX_UINT(dst_size));<br>
+}<br>
+<br>
+static inline int<br>
+_mesa_unsigned_to_signed(unsigned src, unsigned dst_size)<br>
+{<br>
+ return MIN2(src, MAX_INT(dst_size));<br>
+}<br>
+<br>
+static inline int<br>
+_mesa_signed_to_signed(int src, unsigned dst_size)<br>
+{<br>
+ return CLAMP(src, MIN_INT(dst_size), MAX_INT(dst_size));<br>
+}<br>
+<br>
+static inline unsigned<br>
+_mesa_signed_to_unsigned(int src, unsigned dst_size)<br>
+{<br>
+ return CLAMP(src, 0, MAX_UINT(dst_size));<br>
+}<br>
+<br>
bool<br>
_mesa_format_to_array(mesa_format, GLenum *type, int *num_components,<br>
uint8_t swizzle[4], bool *normalized);<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.9.1<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div></div></div>