diff options
author | Jason Ekstrand <[email protected]> | 2014-08-21 12:30:10 -0700 |
---|---|---|
committer | Iago Toral Quiroga <[email protected]> | 2015-01-12 11:20:27 +0100 |
commit | 483b04348848e193d7c71c2f40ce58c447d51755 (patch) | |
tree | a6397e114fbdf599abc60383090717283176f13c /src/mesa/main/format_utils.h | |
parent | 3473a84fb2c2cdab0bcecb43fbf519d9b3fc0142 (diff) |
mesa/format_utils: Prefix and expose the conversion helper functions
Signed-off-by: Jason Ekstrand <[email protected]>
v2 by Samuel Iglesias <[email protected]>:
- Fix compilation errors
Signed-off-by: Samuel Iglesias Gonsalvez <[email protected]>
Reviewed-by: Ian Romanick <[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 | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/mesa/main/format_utils.h b/src/mesa/main/format_utils.h index 9f778e37734..df53edf4f81 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); |