diff options
author | Samuel Iglesias Gonsalvez <[email protected]> | 2014-10-27 16:15:36 +0100 |
---|---|---|
committer | Iago Toral Quiroga <[email protected]> | 2015-01-12 11:20:27 +0100 |
commit | fea1be8d0bd53f817f62d9b4fa82cfcb667fd3c3 (patch) | |
tree | db9fd82be67f8fbf78d1d38e3dcff990a15bbc6c /src/mesa/main/format_utils.h | |
parent | 483b04348848e193d7c71c2f40ce58c447d51755 (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/format_utils.h')
-rw-r--r-- | src/mesa/main/format_utils.h | 60 |
1 files changed, 60 insertions, 0 deletions
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); |