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