mesa: enable enums for OES_texture_storage_multisample_2d_array
[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 #include "macros.h"
36 #include "util/rounding.h"
37
38 extern const mesa_array_format RGBA32_FLOAT;
39 extern const mesa_array_format RGBA8_UBYTE;
40 extern const mesa_array_format RGBA32_UINT;
41 extern const mesa_array_format RGBA32_INT;
42
43 /* Only guaranteed to work for BITS <= 32 */
44 #define MAX_UINT(BITS) ((BITS) == 32 ? UINT32_MAX : ((1u << (BITS)) - 1))
45 #define MAX_INT(BITS) ((int)MAX_UINT((BITS) - 1))
46 #define MIN_INT(BITS) ((BITS) == 32 ? INT32_MIN : (-(1 << (BITS - 1))))
47
48 /* Extends an integer of size SRC_BITS to one of size DST_BITS linearly */
49 #define EXTEND_NORMALIZED_INT(X, SRC_BITS, DST_BITS) \
50 (((X) * (int)(MAX_UINT(DST_BITS) / MAX_UINT(SRC_BITS))) + \
51 ((DST_BITS % SRC_BITS) ? ((X) >> (SRC_BITS - DST_BITS % SRC_BITS)) : 0))
52
53 static inline float
54 _mesa_unorm_to_float(unsigned x, unsigned src_bits)
55 {
56 return x * (1.0f / (float)MAX_UINT(src_bits));
57 }
58
59 static inline float
60 _mesa_snorm_to_float(int x, unsigned src_bits)
61 {
62 if (x <= -MAX_INT(src_bits))
63 return -1.0f;
64 else
65 return x * (1.0f / (float)MAX_INT(src_bits));
66 }
67
68 static inline uint16_t
69 _mesa_unorm_to_half(unsigned x, unsigned src_bits)
70 {
71 return _mesa_float_to_half(_mesa_unorm_to_float(x, src_bits));
72 }
73
74 static inline uint16_t
75 _mesa_snorm_to_half(int x, unsigned src_bits)
76 {
77 return _mesa_float_to_half(_mesa_snorm_to_float(x, src_bits));
78 }
79
80 static inline unsigned
81 _mesa_float_to_unorm(float x, unsigned dst_bits)
82 {
83 if (x < 0.0f)
84 return 0;
85 else if (x > 1.0f)
86 return MAX_UINT(dst_bits);
87 else
88 return _mesa_lroundevenf(x * MAX_UINT(dst_bits));
89 }
90
91 static inline unsigned
92 _mesa_half_to_unorm(uint16_t x, unsigned dst_bits)
93 {
94 return _mesa_float_to_unorm(_mesa_half_to_float(x), dst_bits);
95 }
96
97 static inline unsigned
98 _mesa_unorm_to_unorm(unsigned x, unsigned src_bits, unsigned dst_bits)
99 {
100 if (src_bits < dst_bits) {
101 return EXTEND_NORMALIZED_INT(x, src_bits, dst_bits);
102 } else if (src_bits > dst_bits) {
103 unsigned src_half = (1 << (src_bits - 1)) - 1;
104
105 if (src_bits + dst_bits > sizeof(x) * 8) {
106 assert(src_bits + dst_bits <= sizeof(uint64_t) * 8);
107 return (((uint64_t) x * MAX_UINT(dst_bits) + src_half) /
108 MAX_UINT(src_bits));
109 } else {
110 return (x * MAX_UINT(dst_bits) + src_half) / MAX_UINT(src_bits);
111 }
112 } else {
113 return x;
114 }
115 }
116
117 static inline unsigned
118 _mesa_snorm_to_unorm(int x, unsigned src_bits, unsigned dst_bits)
119 {
120 if (x < 0)
121 return 0;
122 else
123 return _mesa_unorm_to_unorm(x, src_bits - 1, dst_bits);
124 }
125
126 static inline int
127 _mesa_float_to_snorm(float x, unsigned dst_bits)
128 {
129 if (x < -1.0f)
130 return -MAX_INT(dst_bits);
131 else if (x > 1.0f)
132 return MAX_INT(dst_bits);
133 else
134 return _mesa_lroundevenf(x * MAX_INT(dst_bits));
135 }
136
137 static inline int
138 _mesa_half_to_snorm(uint16_t x, unsigned dst_bits)
139 {
140 return _mesa_float_to_snorm(_mesa_half_to_float(x), dst_bits);
141 }
142
143 static inline int
144 _mesa_unorm_to_snorm(unsigned x, unsigned src_bits, unsigned dst_bits)
145 {
146 return _mesa_unorm_to_unorm(x, src_bits, dst_bits - 1);
147 }
148
149 static inline int
150 _mesa_snorm_to_snorm(int x, unsigned src_bits, unsigned dst_bits)
151 {
152 if (x < -MAX_INT(src_bits))
153 return -MAX_INT(dst_bits);
154 else if (src_bits < dst_bits)
155 return EXTEND_NORMALIZED_INT(x, src_bits - 1, dst_bits - 1);
156 else
157 return x >> (src_bits - dst_bits);
158 }
159
160 static inline unsigned
161 _mesa_unsigned_to_unsigned(unsigned src, unsigned dst_size)
162 {
163 return MIN2(src, MAX_UINT(dst_size));
164 }
165
166 static inline int
167 _mesa_unsigned_to_signed(unsigned src, unsigned dst_size)
168 {
169 return MIN2(src, (unsigned)MAX_INT(dst_size));
170 }
171
172 static inline int
173 _mesa_signed_to_signed(int src, unsigned dst_size)
174 {
175 return CLAMP(src, MIN_INT(dst_size), MAX_INT(dst_size));
176 }
177
178 static inline unsigned
179 _mesa_signed_to_unsigned(int src, unsigned dst_size)
180 {
181 return CLAMP(src, 0, MAX_UINT(dst_size));
182 }
183
184 static inline unsigned
185 _mesa_float_to_unsigned(float src, unsigned dst_bits)
186 {
187 if (src < 0.0f)
188 return 0;
189 if (src > (float)MAX_UINT(dst_bits))
190 return MAX_UINT(dst_bits);
191 return _mesa_signed_to_unsigned(src, dst_bits);
192 }
193
194 static inline unsigned
195 _mesa_float_to_signed(float src, unsigned dst_bits)
196 {
197 if (src < (float)(-MAX_INT(dst_bits)))
198 return -MAX_INT(dst_bits);
199 if (src > (float)MAX_INT(dst_bits))
200 return MAX_INT(dst_bits);
201 return _mesa_signed_to_signed(src, dst_bits);
202 }
203
204 static inline unsigned
205 _mesa_half_to_unsigned(uint16_t src, unsigned dst_bits)
206 {
207 if (_mesa_half_is_negative(src))
208 return 0;
209 return _mesa_unsigned_to_unsigned(_mesa_float_to_half(src), dst_bits);
210 }
211
212 static inline unsigned
213 _mesa_half_to_signed(uint16_t src, unsigned dst_bits)
214 {
215 return _mesa_float_to_signed(_mesa_half_to_float(src), dst_bits);
216 }
217
218 bool
219 _mesa_format_to_array(mesa_format, GLenum *type, int *num_components,
220 uint8_t swizzle[4], bool *normalized);
221
222 void
223 _mesa_swizzle_and_convert(void *dst,
224 enum mesa_array_format_datatype dst_type,
225 int num_dst_channels,
226 const void *src,
227 enum mesa_array_format_datatype src_type,
228 int num_src_channels,
229 const uint8_t swizzle[4], bool normalized, int count);
230
231 bool
232 _mesa_compute_rgba2base2rgba_component_mapping(GLenum baseFormat, uint8_t *map);
233
234 void
235 _mesa_format_convert(void *void_dst, uint32_t dst_format, size_t dst_stride,
236 void *void_src, uint32_t src_format, size_t src_stride,
237 size_t width, size_t height, uint8_t *rebase_swizzle);
238
239 #endif