i965: Change mipmap array_spacing_lod0 to array_layout (enum)
[mesa.git] / src / mesa / main / format_utils.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2014 Intel Corporation All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "format_utils.h"
26 #include "glformats.h"
27
28 static const uint8_t map_identity[7] = { 0, 1, 2, 3, 4, 5, 6 };
29 static const uint8_t map_3210[7] = { 3, 2, 1, 0, 4, 5, 6 };
30 static const uint8_t map_1032[7] = { 1, 0, 3, 2, 4, 5, 6 };
31
32 /**
33 * Describes a format as an array format, if possible
34 *
35 * A helper function for figuring out if a (possibly packed) format is
36 * actually an array format and, if so, what the array parameters are.
37 *
38 * \param[in] format the mesa format
39 * \param[out] type the GL type of the array (GL_BYTE, etc.)
40 * \param[out] num_components the number of components in the array
41 * \param[out] swizzle a swizzle describing how to get from the
42 * given format to RGBA
43 * \param[out] normalized for integer formats, this represents whether
44 * the format is a normalized integer or a
45 * regular integer
46 * \return true if this format is an array format, false otherwise
47 */
48 bool
49 _mesa_format_to_array(mesa_format format, GLenum *type, int *num_components,
50 uint8_t swizzle[4], bool *normalized)
51 {
52 int i;
53 GLuint format_components;
54 uint8_t packed_swizzle[4];
55 const uint8_t *endian;
56
57 if (_mesa_is_format_compressed(format))
58 return false;
59
60 *normalized = !_mesa_is_format_integer(format);
61
62 _mesa_format_to_type_and_comps(format, type, &format_components);
63
64 switch (_mesa_get_format_layout(format)) {
65 case MESA_FORMAT_LAYOUT_ARRAY:
66 *num_components = format_components;
67 _mesa_get_format_swizzle(format, swizzle);
68 return true;
69 case MESA_FORMAT_LAYOUT_PACKED:
70 switch (*type) {
71 case GL_UNSIGNED_BYTE:
72 case GL_BYTE:
73 if (_mesa_get_format_max_bits(format) != 8)
74 return false;
75 *num_components = _mesa_get_format_bytes(format);
76 switch (*num_components) {
77 case 1:
78 endian = map_identity;
79 break;
80 case 2:
81 endian = _mesa_little_endian() ? map_identity : map_1032;
82 break;
83 case 4:
84 endian = _mesa_little_endian() ? map_identity : map_3210;
85 break;
86 default:
87 endian = map_identity;
88 assert(!"Invalid number of components");
89 }
90 break;
91 case GL_UNSIGNED_SHORT:
92 case GL_SHORT:
93 case GL_HALF_FLOAT:
94 if (_mesa_get_format_max_bits(format) != 16)
95 return false;
96 *num_components = _mesa_get_format_bytes(format) / 2;
97 switch (*num_components) {
98 case 1:
99 endian = map_identity;
100 break;
101 case 2:
102 endian = _mesa_little_endian() ? map_identity : map_1032;
103 break;
104 default:
105 endian = map_identity;
106 assert(!"Invalid number of components");
107 }
108 break;
109 case GL_UNSIGNED_INT:
110 case GL_INT:
111 case GL_FLOAT:
112 /* This isn't packed. At least not really. */
113 assert(format_components == 1);
114 if (_mesa_get_format_max_bits(format) != 32)
115 return false;
116 *num_components = format_components;
117 endian = map_identity;
118 break;
119 default:
120 return false;
121 }
122
123 _mesa_get_format_swizzle(format, packed_swizzle);
124
125 for (i = 0; i < 4; ++i)
126 swizzle[i] = endian[packed_swizzle[i]];
127
128 return true;
129 case MESA_FORMAT_LAYOUT_OTHER:
130 default:
131 return false;
132 }
133 }
134
135 /* A bunch of format conversion macros and helper functions used below */
136
137 /* Only guaranteed to work for BITS <= 32 */
138 #define MAX_UINT(BITS) ((BITS) == 32 ? UINT32_MAX : ((1u << (BITS)) - 1))
139 #define MAX_INT(BITS) ((int)MAX_UINT((BITS) - 1))
140
141 /* Extends an integer of size SRC_BITS to one of size DST_BITS linearly */
142 #define EXTEND_NORMALIZED_INT(X, SRC_BITS, DST_BITS) \
143 (((X) * (int)(MAX_UINT(DST_BITS) / MAX_UINT(SRC_BITS))) + \
144 ((DST_BITS % SRC_BITS) ? ((X) >> (SRC_BITS - DST_BITS % SRC_BITS)) : 0))
145
146 static inline float
147 unorm_to_float(unsigned x, unsigned src_bits)
148 {
149 return x * (1.0f / (float)MAX_UINT(src_bits));
150 }
151
152 static inline float
153 snorm_to_float(int x, unsigned src_bits)
154 {
155 if (x == -MAX_INT(src_bits))
156 return -1.0f;
157 else
158 return x * (1.0f / (float)MAX_INT(src_bits));
159 }
160
161 static inline uint16_t
162 unorm_to_half(unsigned x, unsigned src_bits)
163 {
164 return _mesa_float_to_half(unorm_to_float(x, src_bits));
165 }
166
167 static inline uint16_t
168 snorm_to_half(int x, unsigned src_bits)
169 {
170 return _mesa_float_to_half(snorm_to_float(x, src_bits));
171 }
172
173 static inline unsigned
174 float_to_unorm(float x, unsigned dst_bits)
175 {
176 if (x < 0.0f)
177 return 0;
178 else if (x > 1.0f)
179 return MAX_UINT(dst_bits);
180 else
181 return F_TO_I(x * MAX_UINT(dst_bits));
182 }
183
184 static inline unsigned
185 half_to_unorm(uint16_t x, unsigned dst_bits)
186 {
187 return float_to_unorm(_mesa_half_to_float(x), dst_bits);
188 }
189
190 static inline unsigned
191 unorm_to_unorm(unsigned x, unsigned src_bits, unsigned dst_bits)
192 {
193 if (src_bits < dst_bits)
194 return EXTEND_NORMALIZED_INT(x, src_bits, dst_bits);
195 else
196 return x >> (src_bits - dst_bits);
197 }
198
199 static inline unsigned
200 snorm_to_unorm(int x, unsigned src_bits, unsigned dst_bits)
201 {
202 if (x < 0)
203 return 0;
204 else
205 return unorm_to_unorm(x, src_bits - 1, dst_bits);
206 }
207
208 static inline int
209 float_to_snorm(float x, unsigned dst_bits)
210 {
211 if (x < -1.0f)
212 return -MAX_INT(dst_bits);
213 else if (x > 1.0f)
214 return MAX_INT(dst_bits);
215 else
216 return F_TO_I(x * MAX_INT(dst_bits));
217 }
218
219 static inline int
220 half_to_snorm(uint16_t x, unsigned dst_bits)
221 {
222 return float_to_snorm(_mesa_half_to_float(x), dst_bits);
223 }
224
225 static inline int
226 unorm_to_snorm(unsigned x, unsigned src_bits, unsigned dst_bits)
227 {
228 return unorm_to_unorm(x, src_bits, dst_bits - 1);
229 }
230
231 static inline int
232 snorm_to_snorm(int x, unsigned src_bits, unsigned dst_bits)
233 {
234 if (x < -MAX_INT(src_bits))
235 return -MAX_INT(dst_bits);
236 else if (src_bits < dst_bits)
237 return EXTEND_NORMALIZED_INT(x, src_bits - 1, dst_bits - 1);
238 else
239 return x >> (src_bits - dst_bits);
240 }
241
242 static inline unsigned
243 float_to_uint(float x)
244 {
245 if (x < 0.0f)
246 return 0;
247 else
248 return x;
249 }
250
251 static inline unsigned
252 half_to_uint(uint16_t x)
253 {
254 if (_mesa_half_is_negative(x))
255 return 0;
256 else
257 return _mesa_float_to_half(x);
258 }
259
260 /**
261 * Attempts to perform the given swizzle-and-convert operation with memcpy
262 *
263 * This function determines if the given swizzle-and-convert operation can
264 * be done with a simple memcpy and, if so, does the memcpy. If not, it
265 * returns false and we fall back to the standard version below.
266 *
267 * The arguments are exactly the same as for _mesa_swizzle_and_convert
268 *
269 * \return true if it successfully performed the swizzle-and-convert
270 * operation with memcpy, false otherwise
271 */
272 static bool
273 swizzle_convert_try_memcpy(void *dst, GLenum dst_type, int num_dst_channels,
274 const void *src, GLenum src_type, int num_src_channels,
275 const uint8_t swizzle[4], bool normalized, int count)
276 {
277 int i;
278
279 if (src_type != dst_type)
280 return false;
281 if (num_src_channels != num_dst_channels)
282 return false;
283
284 for (i = 0; i < num_dst_channels; ++i)
285 if (swizzle[i] != i && swizzle[i] != MESA_FORMAT_SWIZZLE_NONE)
286 return false;
287
288 memcpy(dst, src, count * num_src_channels * _mesa_sizeof_type(src_type));
289
290 return true;
291 }
292
293 /**
294 * Represents a single instance of the standard swizzle-and-convert loop
295 *
296 * Any swizzle-and-convert operation simply loops through the pixels and
297 * performs the transformation operation one pixel at a time. This macro
298 * embodies one instance of the conversion loop. This way we can do all
299 * control flow outside of the loop and allow the compiler to unroll
300 * everything inside the loop.
301 *
302 * Note: This loop is carefully crafted for performance. Be careful when
303 * changing it and run some benchmarks to ensure no performance regressions
304 * if you do.
305 *
306 * \param DST_TYPE the C datatype of the destination
307 * \param DST_CHANS the number of destination channels
308 * \param SRC_TYPE the C datatype of the source
309 * \param SRC_CHANS the number of source channels
310 * \param CONV an expression for converting from the source data,
311 * storred in the variable "src", to the destination
312 * format
313 */
314 #define SWIZZLE_CONVERT_LOOP(DST_TYPE, DST_CHANS, SRC_TYPE, SRC_CHANS, CONV) \
315 for (s = 0; s < count; ++s) { \
316 for (j = 0; j < SRC_CHANS; ++j) { \
317 SRC_TYPE src = typed_src[j]; \
318 tmp[j] = CONV; \
319 } \
320 \
321 typed_dst[0] = tmp[swizzle_x]; \
322 if (DST_CHANS > 1) { \
323 typed_dst[1] = tmp[swizzle_y]; \
324 if (DST_CHANS > 2) { \
325 typed_dst[2] = tmp[swizzle_z]; \
326 if (DST_CHANS > 3) { \
327 typed_dst[3] = tmp[swizzle_w]; \
328 } \
329 } \
330 } \
331 typed_src += SRC_CHANS; \
332 typed_dst += DST_CHANS; \
333 } \
334
335 /**
336 * Represents a single swizzle-and-convert operation
337 *
338 * This macro represents everything done in a single swizzle-and-convert
339 * operation. The actual work is done by the SWIZZLE_CONVERT_LOOP macro.
340 * This macro acts as a wrapper that uses a nested switch to ensure that
341 * all looping parameters get unrolled.
342 *
343 * This macro makes assumptions about variables etc. in the calling
344 * function. Changes to _mesa_swizzle_and_convert may require changes to
345 * this macro.
346 *
347 * \param DST_TYPE the C datatype of the destination
348 * \param SRC_TYPE the C datatype of the source
349 * \param CONV an expression for converting from the source data,
350 * storred in the variable "src", to the destination
351 * format
352 */
353 #define SWIZZLE_CONVERT(DST_TYPE, SRC_TYPE, CONV) \
354 do { \
355 const SRC_TYPE *typed_src = void_src; \
356 DST_TYPE *typed_dst = void_dst; \
357 DST_TYPE tmp[7]; \
358 tmp[4] = 0; \
359 tmp[5] = one; \
360 switch (num_dst_channels) { \
361 case 1: \
362 switch (num_src_channels) { \
363 case 1: \
364 SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 1, CONV) \
365 break; \
366 case 2: \
367 SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 2, CONV) \
368 break; \
369 case 3: \
370 SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 3, CONV) \
371 break; \
372 case 4: \
373 SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 4, CONV) \
374 break; \
375 } \
376 break; \
377 case 2: \
378 switch (num_src_channels) { \
379 case 1: \
380 SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 1, CONV) \
381 break; \
382 case 2: \
383 SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 2, CONV) \
384 break; \
385 case 3: \
386 SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 3, CONV) \
387 break; \
388 case 4: \
389 SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 4, CONV) \
390 break; \
391 } \
392 break; \
393 case 3: \
394 switch (num_src_channels) { \
395 case 1: \
396 SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 1, CONV) \
397 break; \
398 case 2: \
399 SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 2, CONV) \
400 break; \
401 case 3: \
402 SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 3, CONV) \
403 break; \
404 case 4: \
405 SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 4, CONV) \
406 break; \
407 } \
408 break; \
409 case 4: \
410 switch (num_src_channels) { \
411 case 1: \
412 SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 1, CONV) \
413 break; \
414 case 2: \
415 SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 2, CONV) \
416 break; \
417 case 3: \
418 SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 3, CONV) \
419 break; \
420 case 4: \
421 SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 4, CONV) \
422 break; \
423 } \
424 break; \
425 } \
426 } while (0);
427
428 /**
429 * Convert between array-based color formats.
430 *
431 * Most format conversion operations required by GL can be performed by
432 * converting one channel at a time, shuffling the channels around, and
433 * optionally filling missing channels with zeros and ones. This function
434 * does just that in a general, yet efficient, way.
435 *
436 * The swizzle parameter is an array of 4 numbers (see
437 * _mesa_get_format_swizzle) that describes where each channel in the
438 * destination should come from in the source. If swizzle[i] < 4 then it
439 * means that dst[i] = CONVERT(src[swizzle[i]]). If swizzle[i] is
440 * MESA_FORMAT_SWIZZLE_ZERO or MESA_FORMAT_SWIZZLE_ONE, the corresponding
441 * dst[i] will be filled with the appropreate representation of zero or one
442 * respectively.
443 *
444 * Under most circumstances, the source and destination images must be
445 * different as no care is taken not to clobber one with the other.
446 * However, if they have the same number of bits per pixel, it is safe to
447 * do an in-place conversion.
448 *
449 * \param[out] dst pointer to where the converted data should
450 * be stored
451 *
452 * \param[in] dst_type the destination GL type of the converted
453 * data (GL_BYTE, etc.)
454 *
455 * \param[in] num_dst_channels the number of channels in the converted
456 * data
457 *
458 * \param[in] src pointer to the source data
459 *
460 * \param[in] src_type the GL type of the source data (GL_BYTE,
461 * etc.)
462 *
463 * \param[in] num_src_channels the number of channels in the source data
464 * (the number of channels total, not just
465 * the number used)
466 *
467 * \param[in] swizzle describes how to get the destination data
468 * from the source data.
469 *
470 * \param[in] normalized for integer types, this indicates whether
471 * the data should be considered as integers
472 * or as normalized integers;
473 *
474 * \param[in] count the number of pixels to convert
475 */
476 void
477 _mesa_swizzle_and_convert(void *void_dst, GLenum dst_type, int num_dst_channels,
478 const void *void_src, GLenum src_type, int num_src_channels,
479 const uint8_t swizzle[4], bool normalized, int count)
480 {
481 int s, j;
482 register uint8_t swizzle_x, swizzle_y, swizzle_z, swizzle_w;
483
484 if (swizzle_convert_try_memcpy(void_dst, dst_type, num_dst_channels,
485 void_src, src_type, num_src_channels,
486 swizzle, normalized, count))
487 return;
488
489 swizzle_x = swizzle[0];
490 swizzle_y = swizzle[1];
491 swizzle_z = swizzle[2];
492 swizzle_w = swizzle[3];
493
494 switch (dst_type) {
495 case GL_FLOAT:
496 {
497 const float one = 1.0f;
498 switch (src_type) {
499 case GL_FLOAT:
500 SWIZZLE_CONVERT(float, float, src)
501 break;
502 case GL_HALF_FLOAT:
503 SWIZZLE_CONVERT(float, uint16_t, _mesa_half_to_float(src))
504 break;
505 case GL_UNSIGNED_BYTE:
506 if (normalized) {
507 SWIZZLE_CONVERT(float, uint8_t, unorm_to_float(src, 8))
508 } else {
509 SWIZZLE_CONVERT(float, uint8_t, src)
510 }
511 break;
512 case GL_BYTE:
513 if (normalized) {
514 SWIZZLE_CONVERT(float, int8_t, snorm_to_float(src, 8))
515 } else {
516 SWIZZLE_CONVERT(float, int8_t, src)
517 }
518 break;
519 case GL_UNSIGNED_SHORT:
520 if (normalized) {
521 SWIZZLE_CONVERT(float, uint16_t, unorm_to_float(src, 16))
522 } else {
523 SWIZZLE_CONVERT(float, uint16_t, src)
524 }
525 break;
526 case GL_SHORT:
527 if (normalized) {
528 SWIZZLE_CONVERT(float, int16_t, snorm_to_float(src, 16))
529 } else {
530 SWIZZLE_CONVERT(float, int16_t, src)
531 }
532 break;
533 case GL_UNSIGNED_INT:
534 if (normalized) {
535 SWIZZLE_CONVERT(float, uint32_t, unorm_to_float(src, 32))
536 } else {
537 SWIZZLE_CONVERT(float, uint32_t, src)
538 }
539 break;
540 case GL_INT:
541 if (normalized) {
542 SWIZZLE_CONVERT(float, int32_t, snorm_to_float(src, 32))
543 } else {
544 SWIZZLE_CONVERT(float, int32_t, src)
545 }
546 break;
547 default:
548 assert(!"Invalid channel type combination");
549 }
550 }
551 break;
552 case GL_HALF_FLOAT:
553 {
554 const uint16_t one = _mesa_float_to_half(1.0f);
555 switch (src_type) {
556 case GL_FLOAT:
557 SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_half(src))
558 break;
559 case GL_HALF_FLOAT:
560 SWIZZLE_CONVERT(uint16_t, uint16_t, src)
561 break;
562 case GL_UNSIGNED_BYTE:
563 if (normalized) {
564 SWIZZLE_CONVERT(uint16_t, uint8_t, unorm_to_half(src, 8))
565 } else {
566 SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_float_to_half(src))
567 }
568 break;
569 case GL_BYTE:
570 if (normalized) {
571 SWIZZLE_CONVERT(uint16_t, int8_t, snorm_to_half(src, 8))
572 } else {
573 SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_float_to_half(src))
574 }
575 break;
576 case GL_UNSIGNED_SHORT:
577 if (normalized) {
578 SWIZZLE_CONVERT(uint16_t, uint16_t, unorm_to_half(src, 16))
579 } else {
580 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_float_to_half(src))
581 }
582 break;
583 case GL_SHORT:
584 if (normalized) {
585 SWIZZLE_CONVERT(uint16_t, int16_t, snorm_to_half(src, 16))
586 } else {
587 SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_float_to_half(src))
588 }
589 break;
590 case GL_UNSIGNED_INT:
591 if (normalized) {
592 SWIZZLE_CONVERT(uint16_t, uint32_t, unorm_to_half(src, 32))
593 } else {
594 SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_float_to_half(src))
595 }
596 break;
597 case GL_INT:
598 if (normalized) {
599 SWIZZLE_CONVERT(uint16_t, int32_t, snorm_to_half(src, 32))
600 } else {
601 SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_float_to_half(src))
602 }
603 break;
604 default:
605 assert(!"Invalid channel type combination");
606 }
607 }
608 break;
609 case GL_UNSIGNED_BYTE:
610 {
611 const uint8_t one = normalized ? UINT8_MAX : 1;
612 switch (src_type) {
613 case GL_FLOAT:
614 if (normalized) {
615 SWIZZLE_CONVERT(uint8_t, float, float_to_unorm(src, 8))
616 } else {
617 SWIZZLE_CONVERT(uint8_t, float, (src < 0) ? 0 : src)
618 }
619 break;
620 case GL_HALF_FLOAT:
621 if (normalized) {
622 SWIZZLE_CONVERT(uint8_t, uint16_t, half_to_unorm(src, 8))
623 } else {
624 SWIZZLE_CONVERT(uint8_t, uint16_t, half_to_uint(src))
625 }
626 break;
627 case GL_UNSIGNED_BYTE:
628 SWIZZLE_CONVERT(uint8_t, uint8_t, src)
629 break;
630 case GL_BYTE:
631 if (normalized) {
632 SWIZZLE_CONVERT(uint8_t, int8_t, snorm_to_unorm(src, 8, 8))
633 } else {
634 SWIZZLE_CONVERT(uint8_t, int8_t, (src < 0) ? 0 : src)
635 }
636 break;
637 case GL_UNSIGNED_SHORT:
638 if (normalized) {
639 SWIZZLE_CONVERT(uint8_t, uint16_t, unorm_to_unorm(src, 16, 8))
640 } else {
641 SWIZZLE_CONVERT(uint8_t, uint16_t, src)
642 }
643 break;
644 case GL_SHORT:
645 if (normalized) {
646 SWIZZLE_CONVERT(uint8_t, int16_t, snorm_to_unorm(src, 16, 8))
647 } else {
648 SWIZZLE_CONVERT(uint8_t, int16_t, (src < 0) ? 0 : src)
649 }
650 break;
651 case GL_UNSIGNED_INT:
652 if (normalized) {
653 SWIZZLE_CONVERT(uint8_t, uint32_t, unorm_to_unorm(src, 32, 8))
654 } else {
655 SWIZZLE_CONVERT(uint8_t, uint32_t, src)
656 }
657 break;
658 case GL_INT:
659 if (normalized) {
660 SWIZZLE_CONVERT(uint8_t, int32_t, snorm_to_unorm(src, 32, 8))
661 } else {
662 SWIZZLE_CONVERT(uint8_t, int32_t, (src < 0) ? 0 : src)
663 }
664 break;
665 default:
666 assert(!"Invalid channel type combination");
667 }
668 }
669 break;
670 case GL_BYTE:
671 {
672 const int8_t one = normalized ? INT8_MAX : 1;
673 switch (src_type) {
674 case GL_FLOAT:
675 if (normalized) {
676 SWIZZLE_CONVERT(uint8_t, float, float_to_snorm(src, 8))
677 } else {
678 SWIZZLE_CONVERT(uint8_t, float, src)
679 }
680 break;
681 case GL_HALF_FLOAT:
682 if (normalized) {
683 SWIZZLE_CONVERT(uint8_t, uint16_t, half_to_snorm(src, 8))
684 } else {
685 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_float(src))
686 }
687 break;
688 case GL_UNSIGNED_BYTE:
689 if (normalized) {
690 SWIZZLE_CONVERT(int8_t, uint8_t, unorm_to_snorm(src, 8, 8))
691 } else {
692 SWIZZLE_CONVERT(int8_t, uint8_t, src)
693 }
694 break;
695 case GL_BYTE:
696 SWIZZLE_CONVERT(int8_t, int8_t, src)
697 break;
698 case GL_UNSIGNED_SHORT:
699 if (normalized) {
700 SWIZZLE_CONVERT(int8_t, uint16_t, unorm_to_snorm(src, 16, 8))
701 } else {
702 SWIZZLE_CONVERT(int8_t, uint16_t, src)
703 }
704 break;
705 case GL_SHORT:
706 if (normalized) {
707 SWIZZLE_CONVERT(int8_t, int16_t, snorm_to_snorm(src, 16, 8))
708 } else {
709 SWIZZLE_CONVERT(int8_t, int16_t, src)
710 }
711 break;
712 case GL_UNSIGNED_INT:
713 if (normalized) {
714 SWIZZLE_CONVERT(int8_t, uint32_t, unorm_to_snorm(src, 32, 8))
715 } else {
716 SWIZZLE_CONVERT(int8_t, uint32_t, src)
717 }
718 break;
719 case GL_INT:
720 if (normalized) {
721 SWIZZLE_CONVERT(int8_t, int32_t, snorm_to_snorm(src, 32, 8))
722 } else {
723 SWIZZLE_CONVERT(int8_t, int32_t, src)
724 }
725 break;
726 default:
727 assert(!"Invalid channel type combination");
728 }
729 }
730 break;
731 case GL_UNSIGNED_SHORT:
732 {
733 const uint16_t one = normalized ? UINT16_MAX : 1;
734 switch (src_type) {
735 case GL_FLOAT:
736 if (normalized) {
737 SWIZZLE_CONVERT(uint16_t, float, float_to_unorm(src, 16))
738 } else {
739 SWIZZLE_CONVERT(uint16_t, float, (src < 0) ? 0 : src)
740 }
741 break;
742 case GL_HALF_FLOAT:
743 if (normalized) {
744 SWIZZLE_CONVERT(uint16_t, uint16_t, half_to_unorm(src, 16))
745 } else {
746 SWIZZLE_CONVERT(uint16_t, uint16_t, half_to_uint(src))
747 }
748 break;
749 case GL_UNSIGNED_BYTE:
750 if (normalized) {
751 SWIZZLE_CONVERT(uint16_t, uint8_t, unorm_to_unorm(src, 8, 16))
752 } else {
753 SWIZZLE_CONVERT(uint16_t, uint8_t, src)
754 }
755 break;
756 case GL_BYTE:
757 if (normalized) {
758 SWIZZLE_CONVERT(uint16_t, int8_t, snorm_to_unorm(src, 8, 16))
759 } else {
760 SWIZZLE_CONVERT(uint16_t, int8_t, (src < 0) ? 0 : src)
761 }
762 break;
763 case GL_UNSIGNED_SHORT:
764 SWIZZLE_CONVERT(uint16_t, uint16_t, src)
765 break;
766 case GL_SHORT:
767 if (normalized) {
768 SWIZZLE_CONVERT(uint16_t, int16_t, snorm_to_unorm(src, 16, 16))
769 } else {
770 SWIZZLE_CONVERT(uint16_t, int16_t, (src < 0) ? 0 : src)
771 }
772 break;
773 case GL_UNSIGNED_INT:
774 if (normalized) {
775 SWIZZLE_CONVERT(uint16_t, uint32_t, unorm_to_unorm(src, 32, 16))
776 } else {
777 SWIZZLE_CONVERT(uint16_t, uint32_t, src)
778 }
779 break;
780 case GL_INT:
781 if (normalized) {
782 SWIZZLE_CONVERT(uint16_t, int32_t, snorm_to_unorm(src, 32, 16))
783 } else {
784 SWIZZLE_CONVERT(uint16_t, int32_t, (src < 0) ? 0 : src)
785 }
786 break;
787 default:
788 assert(!"Invalid channel type combination");
789 }
790 }
791 break;
792 case GL_SHORT:
793 {
794 const int16_t one = normalized ? INT16_MAX : 1;
795 switch (src_type) {
796 case GL_FLOAT:
797 if (normalized) {
798 SWIZZLE_CONVERT(uint16_t, float, float_to_snorm(src, 16))
799 } else {
800 SWIZZLE_CONVERT(uint16_t, float, src)
801 }
802 break;
803 case GL_HALF_FLOAT:
804 if (normalized) {
805 SWIZZLE_CONVERT(uint16_t, uint16_t, half_to_snorm(src, 16))
806 } else {
807 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_float(src))
808 }
809 break;
810 case GL_UNSIGNED_BYTE:
811 if (normalized) {
812 SWIZZLE_CONVERT(int16_t, uint8_t, unorm_to_snorm(src, 8, 16))
813 } else {
814 SWIZZLE_CONVERT(int16_t, uint8_t, src)
815 }
816 break;
817 case GL_BYTE:
818 if (normalized) {
819 SWIZZLE_CONVERT(int16_t, int8_t, snorm_to_snorm(src, 8, 16))
820 } else {
821 SWIZZLE_CONVERT(int16_t, int8_t, src)
822 }
823 break;
824 case GL_UNSIGNED_SHORT:
825 if (normalized) {
826 SWIZZLE_CONVERT(int16_t, uint16_t, unorm_to_snorm(src, 16, 16))
827 } else {
828 SWIZZLE_CONVERT(int16_t, uint16_t, src)
829 }
830 break;
831 case GL_SHORT:
832 SWIZZLE_CONVERT(int16_t, int16_t, src)
833 break;
834 case GL_UNSIGNED_INT:
835 if (normalized) {
836 SWIZZLE_CONVERT(int16_t, uint32_t, unorm_to_snorm(src, 32, 16))
837 } else {
838 SWIZZLE_CONVERT(int16_t, uint32_t, src)
839 }
840 break;
841 case GL_INT:
842 if (normalized) {
843 SWIZZLE_CONVERT(int16_t, int32_t, snorm_to_snorm(src, 32, 16))
844 } else {
845 SWIZZLE_CONVERT(int16_t, int32_t, src)
846 }
847 break;
848 default:
849 assert(!"Invalid channel type combination");
850 }
851 }
852 break;
853 case GL_UNSIGNED_INT:
854 {
855 const uint32_t one = normalized ? UINT32_MAX : 1;
856 switch (src_type) { case GL_FLOAT:
857 if (normalized) {
858 SWIZZLE_CONVERT(uint32_t, float, float_to_unorm(src, 32))
859 } else {
860 SWIZZLE_CONVERT(uint32_t, float, (src < 0) ? 0 : src)
861 }
862 break;
863 case GL_HALF_FLOAT:
864 if (normalized) {
865 SWIZZLE_CONVERT(uint32_t, uint16_t, half_to_unorm(src, 32))
866 } else {
867 SWIZZLE_CONVERT(uint32_t, uint16_t, half_to_uint(src))
868 }
869 break;
870 case GL_UNSIGNED_BYTE:
871 if (normalized) {
872 SWIZZLE_CONVERT(uint32_t, uint8_t, unorm_to_unorm(src, 8, 32))
873 } else {
874 SWIZZLE_CONVERT(uint32_t, uint8_t, src)
875 }
876 break;
877 case GL_BYTE:
878 if (normalized) {
879 SWIZZLE_CONVERT(uint32_t, int8_t, snorm_to_unorm(src, 8, 32))
880 } else {
881 SWIZZLE_CONVERT(uint32_t, int8_t, (src < 0) ? 0 : src)
882 }
883 break;
884 case GL_UNSIGNED_SHORT:
885 if (normalized) {
886 SWIZZLE_CONVERT(uint32_t, uint16_t, unorm_to_unorm(src, 16, 32))
887 } else {
888 SWIZZLE_CONVERT(uint32_t, uint16_t, src)
889 }
890 break;
891 case GL_SHORT:
892 if (normalized) {
893 SWIZZLE_CONVERT(uint32_t, int16_t, snorm_to_unorm(src, 16, 32))
894 } else {
895 SWIZZLE_CONVERT(uint32_t, int16_t, (src < 0) ? 0 : src)
896 }
897 break;
898 case GL_UNSIGNED_INT:
899 SWIZZLE_CONVERT(uint32_t, uint32_t, src)
900 break;
901 case GL_INT:
902 if (normalized) {
903 SWIZZLE_CONVERT(uint32_t, int32_t, snorm_to_unorm(src, 32, 32))
904 } else {
905 SWIZZLE_CONVERT(uint32_t, int32_t, (src < 0) ? 0 : src)
906 }
907 break;
908 default:
909 assert(!"Invalid channel type combination");
910 }
911 }
912 break;
913 case GL_INT:
914 {
915 const int32_t one = normalized ? INT32_MAX : 1;
916 switch (src_type) {
917 case GL_FLOAT:
918 if (normalized) {
919 SWIZZLE_CONVERT(uint32_t, float, float_to_snorm(src, 32))
920 } else {
921 SWIZZLE_CONVERT(uint32_t, float, src)
922 }
923 break;
924 case GL_HALF_FLOAT:
925 if (normalized) {
926 SWIZZLE_CONVERT(uint32_t, uint16_t, half_to_snorm(src, 32))
927 } else {
928 SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_float(src))
929 }
930 break;
931 case GL_UNSIGNED_BYTE:
932 if (normalized) {
933 SWIZZLE_CONVERT(int32_t, uint8_t, unorm_to_snorm(src, 8, 32))
934 } else {
935 SWIZZLE_CONVERT(int32_t, uint8_t, src)
936 }
937 break;
938 case GL_BYTE:
939 if (normalized) {
940 SWIZZLE_CONVERT(int32_t, int8_t, snorm_to_snorm(src, 8, 32))
941 } else {
942 SWIZZLE_CONVERT(int32_t, int8_t, src)
943 }
944 break;
945 case GL_UNSIGNED_SHORT:
946 if (normalized) {
947 SWIZZLE_CONVERT(int32_t, uint16_t, unorm_to_snorm(src, 16, 32))
948 } else {
949 SWIZZLE_CONVERT(int32_t, uint16_t, src)
950 }
951 break;
952 case GL_SHORT:
953 if (normalized) {
954 SWIZZLE_CONVERT(int32_t, int16_t, snorm_to_snorm(src, 16, 32))
955 } else {
956 SWIZZLE_CONVERT(int32_t, int16_t, src)
957 }
958 break;
959 case GL_UNSIGNED_INT:
960 if (normalized) {
961 SWIZZLE_CONVERT(int32_t, uint32_t, unorm_to_snorm(src, 32, 32))
962 } else {
963 SWIZZLE_CONVERT(int32_t, uint32_t, src)
964 }
965 break;
966 case GL_INT:
967 SWIZZLE_CONVERT(int32_t, int32_t, src)
968 break;
969 default:
970 assert(!"Invalid channel type combination");
971 }
972 }
973 break;
974 default:
975 assert(!"Invalid channel type");
976 }
977 }