summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/format_utils.h
diff options
context:
space:
mode:
authorJason Ekstrand <jason.ekstrand@intel.com>2014-08-21 12:30:10 -0700
committerIago Toral Quiroga <itoral@igalia.com>2015-01-12 11:20:27 +0100
commit483b04348848e193d7c71c2f40ce58c447d51755 (patch)
treea6397e114fbdf599abc60383090717283176f13c /src/mesa/main/format_utils.h
parent3473a84fb2c2cdab0bcecb43fbf519d9b3fc0142 (diff)
downloadexternal_mesa3d-483b04348848e193d7c71c2f40ce58c447d51755.zip
external_mesa3d-483b04348848e193d7c71c2f40ce58c447d51755.tar.gz
external_mesa3d-483b04348848e193d7c71c2f40ce58c447d51755.tar.bz2
mesa/format_utils: Prefix and expose the conversion helper functions
Signed-off-by: Jason Ekstrand <jason.ekstrand@intel.com> v2 by Samuel Iglesias <siglesias@igalia.com>: - Fix compilation errors Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com> Reviewed-by: Ian Romanick <ian.d.romanick@intel.com> Reviewed-by: Jason Ekstrand <jason.ekstrand@intel.com>
Diffstat (limited to 'src/mesa/main/format_utils.h')
-rw-r--r--src/mesa/main/format_utils.h105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/mesa/main/format_utils.h b/src/mesa/main/format_utils.h
index 9f778e3..df53edf 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);