mesa/format_utils: Prefix and expose the conversion helper functions
[mesa.git] / src / mesa / main / format_utils.h
1 /**
2 * \file format_utils.h
3 * A collection of format conversion utility functions.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 *
9 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
10 * Copyright (C) 2014 Intel Corporation All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31 #ifndef FORMAT_UTILS_H
32 #define FORMAT_UTILS_H
33
34 #include "imports.h"
35
36 /* Only guaranteed to work for BITS <= 32 */
37 #define MAX_UINT(BITS) ((BITS) == 32 ? UINT32_MAX : ((1u << (BITS)) - 1))
38 #define MAX_INT(BITS) ((int)MAX_UINT((BITS) - 1))
39
40 /* Extends an integer of size SRC_BITS to one of size DST_BITS linearly */
41 #define EXTEND_NORMALIZED_INT(X, SRC_BITS, DST_BITS) \
42 (((X) * (int)(MAX_UINT(DST_BITS) / MAX_UINT(SRC_BITS))) + \
43 ((DST_BITS % SRC_BITS) ? ((X) >> (SRC_BITS - DST_BITS % SRC_BITS)) : 0))
44
45 static inline float
46 _mesa_unorm_to_float(unsigned x, unsigned src_bits)
47 {
48 return x * (1.0f / (float)MAX_UINT(src_bits));
49 }
50
51 static inline float
52 _mesa_snorm_to_float(int x, unsigned src_bits)
53 {
54 if (x <= -MAX_INT(src_bits))
55 return -1.0f;
56 else
57 return x * (1.0f / (float)MAX_INT(src_bits));
58 }
59
60 static inline uint16_t
61 _mesa_unorm_to_half(unsigned x, unsigned src_bits)
62 {
63 return _mesa_float_to_half(_mesa_unorm_to_float(x, src_bits));
64 }
65
66 static inline uint16_t
67 _mesa_snorm_to_half(int x, unsigned src_bits)
68 {
69 return _mesa_float_to_half(_mesa_snorm_to_float(x, src_bits));
70 }
71
72 static inline unsigned
73 _mesa_float_to_unorm(float x, unsigned dst_bits)
74 {
75 if (x < 0.0f)
76 return 0;
77 else if (x > 1.0f)
78 return MAX_UINT(dst_bits);
79 else
80 return F_TO_I(x * MAX_UINT(dst_bits));
81 }
82
83 static inline unsigned
84 _mesa_half_to_unorm(uint16_t x, unsigned dst_bits)
85 {
86 return _mesa_float_to_unorm(_mesa_half_to_float(x), dst_bits);
87 }
88
89 static inline unsigned
90 _mesa_unorm_to_unorm(unsigned x, unsigned src_bits, unsigned dst_bits)
91 {
92 if (src_bits < dst_bits)
93 return EXTEND_NORMALIZED_INT(x, src_bits, dst_bits);
94 else
95 return x >> (src_bits - dst_bits);
96 }
97
98 static inline unsigned
99 _mesa_snorm_to_unorm(int x, unsigned src_bits, unsigned dst_bits)
100 {
101 if (x < 0)
102 return 0;
103 else
104 return _mesa_unorm_to_unorm(x, src_bits - 1, dst_bits);
105 }
106
107 static inline int
108 _mesa_float_to_snorm(float x, unsigned dst_bits)
109 {
110 if (x < -1.0f)
111 return -MAX_INT(dst_bits);
112 else if (x > 1.0f)
113 return MAX_INT(dst_bits);
114 else
115 return F_TO_I(x * MAX_INT(dst_bits));
116 }
117
118 static inline int
119 _mesa_half_to_snorm(uint16_t x, unsigned dst_bits)
120 {
121 return _mesa_float_to_snorm(_mesa_half_to_float(x), dst_bits);
122 }
123
124 static inline int
125 _mesa_unorm_to_snorm(unsigned x, unsigned src_bits, unsigned dst_bits)
126 {
127 return _mesa_unorm_to_unorm(x, src_bits, dst_bits - 1);
128 }
129
130 static inline int
131 _mesa_snorm_to_snorm(int x, unsigned src_bits, unsigned dst_bits)
132 {
133 if (x < -MAX_INT(src_bits))
134 return -MAX_INT(dst_bits);
135 else if (src_bits < dst_bits)
136 return EXTEND_NORMALIZED_INT(x, src_bits - 1, dst_bits - 1);
137 else
138 return x >> (src_bits - dst_bits);
139 }
140
141 bool
142 _mesa_format_to_array(mesa_format, GLenum *type, int *num_components,
143 uint8_t swizzle[4], bool *normalized);
144
145 void
146 _mesa_swizzle_and_convert(void *dst, GLenum dst_type, int num_dst_channels,
147 const void *src, GLenum src_type, int num_src_channels,
148 const uint8_t swizzle[4], bool normalized, int count);
149
150 #endif