2 * Mesa 3-D graphics library
4 * Copyright (C) 2014 Intel Corporation All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
26 #include "format_utils.h"
27 #include "glformats.h"
28 #include "format_pack.h"
29 #include "format_unpack.h"
31 const mesa_array_format RGBA32_FLOAT
=
32 MESA_ARRAY_FORMAT(MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS
,
33 4, 1, 1, 1, 4, 0, 1, 2, 3);
35 const mesa_array_format RGBA8_UBYTE
=
36 MESA_ARRAY_FORMAT(MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS
,
37 1, 0, 0, 1, 4, 0, 1, 2, 3);
39 const mesa_array_format BGRA8_UBYTE
=
40 MESA_ARRAY_FORMAT(MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS
,
41 1, 0, 0, 1, 4, 2, 1, 0, 3);
43 const mesa_array_format RGBA32_UINT
=
44 MESA_ARRAY_FORMAT(MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS
,
45 4, 0, 0, 0, 4, 0, 1, 2, 3);
47 const mesa_array_format RGBA32_INT
=
48 MESA_ARRAY_FORMAT(MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS
,
49 4, 1, 0, 0, 4, 0, 1, 2, 3);
52 invert_swizzle(uint8_t dst
[4], const uint8_t src
[4])
56 dst
[0] = MESA_FORMAT_SWIZZLE_NONE
;
57 dst
[1] = MESA_FORMAT_SWIZZLE_NONE
;
58 dst
[2] = MESA_FORMAT_SWIZZLE_NONE
;
59 dst
[3] = MESA_FORMAT_SWIZZLE_NONE
;
61 for (i
= 0; i
< 4; ++i
)
62 for (j
= 0; j
< 4; ++j
)
63 if (src
[j
] == i
&& dst
[i
] == MESA_FORMAT_SWIZZLE_NONE
)
67 /* Takes a src to RGBA swizzle and applies a rebase swizzle to it. This
68 * is used when we need to rebase a format to match a different
69 * base internal format.
71 * The rebase swizzle can be NULL, which means that no rebase is necessary,
72 * in which case the src to RGBA swizzle is copied to the output without
75 * The resulting rebased swizzle and well as the input swizzles are
76 * all 4-element swizzles, but the rebase swizzle can be NULL if no rebase
80 compute_rebased_rgba_component_mapping(uint8_t *src2rgba
,
81 uint8_t *rebase_swizzle
,
82 uint8_t *rebased_src2rgba
)
87 for (i
= 0; i
< 4; i
++) {
88 if (rebase_swizzle
[i
] > MESA_FORMAT_SWIZZLE_W
)
89 rebased_src2rgba
[i
] = rebase_swizzle
[i
];
91 rebased_src2rgba
[i
] = src2rgba
[rebase_swizzle
[i
]];
94 /* No rebase needed, so src2rgba is all that we need */
95 memcpy(rebased_src2rgba
, src2rgba
, 4 * sizeof(uint8_t));
99 /* Computes the final swizzle transform to apply from src to dst in a
100 * conversion that might involve a rebase swizzle.
102 * This is used to compute the swizzle transform to apply in conversions
103 * between array formats where we have a src2rgba swizzle, a rgba2dst swizzle
104 * and possibly, a rebase swizzle.
106 * The final swizzle transform to apply (src2dst) when a rebase swizzle is
107 * involved is: src -> rgba -> base -> rgba -> dst
110 compute_src2dst_component_mapping(uint8_t *src2rgba
, uint8_t *rgba2dst
,
111 uint8_t *rebase_swizzle
, uint8_t *src2dst
)
115 if (!rebase_swizzle
) {
116 for (i
= 0; i
< 4; i
++) {
117 if (rgba2dst
[i
] > MESA_FORMAT_SWIZZLE_W
) {
118 src2dst
[i
] = rgba2dst
[i
];
120 src2dst
[i
] = src2rgba
[rgba2dst
[i
]];
124 for (i
= 0; i
< 4; i
++) {
125 if (rgba2dst
[i
] > MESA_FORMAT_SWIZZLE_W
) {
126 src2dst
[i
] = rgba2dst
[i
];
127 } else if (rebase_swizzle
[rgba2dst
[i
]] > MESA_FORMAT_SWIZZLE_W
) {
128 src2dst
[i
] = rebase_swizzle
[rgba2dst
[i
]];
130 src2dst
[i
] = src2rgba
[rebase_swizzle
[rgba2dst
[i
]]];
137 * This function is used by clients of _mesa_format_convert to obtain
138 * the rebase swizzle to use in a format conversion based on the base
141 * \param baseFormat the base internal format involved in the conversion.
142 * \param map the rebase swizzle to consider
144 * This function computes 'map' as rgba -> baseformat -> rgba and returns true
145 * if the resulting swizzle transform is not the identity transform (thus, a
146 * rebase is needed). If the function returns false then a rebase swizzle
147 * is not necessary and the value of 'map' is undefined. In this situation
148 * clients of _mesa_format_convert should pass NULL in the 'rebase_swizzle'
152 _mesa_compute_rgba2base2rgba_component_mapping(GLenum baseFormat
, uint8_t *map
)
154 uint8_t rgba2base
[6], base2rgba
[6];
157 switch (baseFormat
) {
170 case GL_LUMINANCE_ALPHA
:
172 bool needRebase
= false;
173 _mesa_compute_component_mapping(GL_RGBA
, baseFormat
, rgba2base
);
174 _mesa_compute_component_mapping(baseFormat
, GL_RGBA
, base2rgba
);
175 for (i
= 0; i
< 4; i
++) {
176 if (base2rgba
[i
] > MESA_FORMAT_SWIZZLE_W
) {
177 map
[i
] = base2rgba
[i
];
179 map
[i
] = rgba2base
[base2rgba
[i
]];
187 unreachable("Unexpected base format");
193 * Special case conversion function to swap r/b channels from the source
194 * image to the dest image.
197 convert_ubyte_rgba_to_bgra(size_t width
, size_t height
,
198 const uint8_t *src
, size_t src_stride
,
199 uint8_t *dst
, size_t dst_stride
)
203 if (sizeof(void *) == 8 &&
204 src_stride
% 8 == 0 &&
205 dst_stride
% 8 == 0 &&
206 (GLsizeiptr
) src
% 8 == 0 &&
207 (GLsizeiptr
) dst
% 8 == 0) {
208 /* use 64-bit word to swizzle two 32-bit pixels. We need 8-byte
209 * alignment for src/dst addresses and strides.
211 for (row
= 0; row
< height
; row
++) {
212 const GLuint64
*s
= (const GLuint64
*) src
;
213 GLuint64
*d
= (GLuint64
*) dst
;
215 for (i
= 0; i
< width
/2; i
++) {
216 d
[i
] = ( (s
[i
] & 0xff00ff00ff00ff00) |
217 ((s
[i
] & 0xff000000ff) << 16) |
218 ((s
[i
] & 0xff000000ff0000) >> 16));
221 /* handle the case of odd widths */
222 const GLuint s
= ((const GLuint
*) src
)[width
- 1];
223 GLuint
*d
= (GLuint
*) dst
+ width
- 1;
224 *d
= ( (s
& 0xff00ff00) |
226 ((s
& 0xff0000) >> 16));
232 for (row
= 0; row
< height
; row
++) {
233 const GLuint
*s
= (const GLuint
*) src
;
234 GLuint
*d
= (GLuint
*) dst
;
236 for (i
= 0; i
< width
; i
++) {
237 d
[i
] = ( (s
[i
] & 0xff00ff00) |
238 ((s
[i
] & 0xff) << 16) |
239 ((s
[i
] & 0xff0000) >> 16));
249 * This can be used to convert between most color formats.
252 * - This function doesn't handle GL_COLOR_INDEX or YCBCR formats.
253 * - This function doesn't handle byte-swapping or transferOps, these should
254 * be handled by the caller.
256 * \param void_dst The address where converted color data will be stored.
257 * The caller must ensure that the buffer is large enough
258 * to hold the converted pixel data.
259 * \param dst_format The destination color format. It can be a mesa_format
260 * or a mesa_array_format represented as an uint32_t.
261 * \param dst_stride The stride of the destination format in bytes.
262 * \param void_src The address of the source color data to convert.
263 * \param src_format The source color format. It can be a mesa_format
264 * or a mesa_array_format represented as an uint32_t.
265 * \param src_stride The stride of the source format in bytes.
266 * \param width The width, in pixels, of the source image to convert.
267 * \param height The height, in pixels, of the source image to convert.
268 * \param rebase_swizzle A swizzle transform to apply during the conversion,
269 * typically used to match a different internal base
270 * format involved. NULL if no rebase transform is needed
271 * (i.e. the internal base format and the base format of
272 * the dst or the src -depending on whether we are doing
273 * an upload or a download respectively- are the same).
276 _mesa_format_convert(void *void_dst
, uint32_t dst_format
, size_t dst_stride
,
277 void *void_src
, uint32_t src_format
, size_t src_stride
,
278 size_t width
, size_t height
, uint8_t *rebase_swizzle
)
280 uint8_t *dst
= (uint8_t *)void_dst
;
281 uint8_t *src
= (uint8_t *)void_src
;
282 mesa_array_format src_array_format
, dst_array_format
;
283 bool src_format_is_mesa_array_format
, dst_format_is_mesa_array_format
;
284 uint8_t src2dst
[4], src2rgba
[4], rgba2dst
[4], dst2rgba
[4];
285 uint8_t rebased_src2rgba
[4];
286 enum mesa_array_format_datatype src_type
= 0, dst_type
= 0, common_type
;
287 bool normalized
, dst_integer
, src_integer
, is_signed
;
288 int src_num_channels
= 0, dst_num_channels
= 0;
289 uint8_t (*tmp_ubyte
)[4];
290 float (*tmp_float
)[4];
291 uint32_t (*tmp_uint
)[4];
295 if (_mesa_format_is_mesa_array_format(src_format
)) {
296 src_format_is_mesa_array_format
= true;
297 src_array_format
= src_format
;
299 assert(_mesa_is_format_color_format(src_format
));
300 src_format_is_mesa_array_format
= false;
301 src_array_format
= _mesa_format_to_array_format(src_format
);
304 if (_mesa_format_is_mesa_array_format(dst_format
)) {
305 dst_format_is_mesa_array_format
= true;
306 dst_array_format
= dst_format
;
308 assert(_mesa_is_format_color_format(dst_format
));
309 dst_format_is_mesa_array_format
= false;
310 dst_array_format
= _mesa_format_to_array_format(dst_format
);
313 /* First we see if we can implement the conversion with a direct pack
316 * In this case we want to be careful when we need to apply a swizzle to
317 * match an internal base format, since in these cases a simple pack/unpack
318 * to the dst format from the src format may not match the requirements
319 * of the internal base format. For now we decide to be safe and
320 * avoid this path in these scenarios but in the future we may want to
321 * enable it for specific combinations that are known to work.
323 if (!rebase_swizzle
) {
324 /* Do a direct memcpy where possible */
325 if ((dst_format_is_mesa_array_format
&&
326 src_format_is_mesa_array_format
&&
327 src_array_format
== dst_array_format
) ||
328 src_format
== dst_format
) {
329 int format_size
= _mesa_get_format_bytes(src_format
);
330 for (row
= 0; row
< height
; row
++) {
331 memcpy(dst
, src
, width
* format_size
);
338 /* Handle the cases where we can directly unpack */
339 if (!src_format_is_mesa_array_format
) {
340 if (dst_array_format
== RGBA32_FLOAT
) {
341 for (row
= 0; row
< height
; ++row
) {
342 _mesa_unpack_rgba_row(src_format
, width
,
343 src
, (float (*)[4])dst
);
348 } else if (dst_array_format
== RGBA8_UBYTE
) {
349 assert(!_mesa_is_format_integer_color(src_format
));
350 for (row
= 0; row
< height
; ++row
) {
351 _mesa_unpack_ubyte_rgba_row(src_format
, width
,
352 src
, (uint8_t (*)[4])dst
);
357 } else if (dst_array_format
== BGRA8_UBYTE
&&
358 src_format
== MESA_FORMAT_R8G8B8A8_UNORM
) {
359 convert_ubyte_rgba_to_bgra(width
, height
, src
, src_stride
,
362 } else if (dst_array_format
== RGBA32_UINT
&&
363 _mesa_is_format_unsigned(src_format
)) {
364 assert(_mesa_is_format_integer_color(src_format
));
365 for (row
= 0; row
< height
; ++row
) {
366 _mesa_unpack_uint_rgba_row(src_format
, width
,
367 src
, (uint32_t (*)[4])dst
);
375 /* Handle the cases where we can directly pack */
376 if (!dst_format_is_mesa_array_format
) {
377 if (src_array_format
== RGBA32_FLOAT
) {
378 for (row
= 0; row
< height
; ++row
) {
379 _mesa_pack_float_rgba_row(dst_format
, width
,
380 (const float (*)[4])src
, dst
);
385 } else if (src_array_format
== RGBA8_UBYTE
) {
386 assert(!_mesa_is_format_integer_color(dst_format
));
388 if (dst_format
== MESA_FORMAT_B8G8R8A8_UNORM
) {
389 convert_ubyte_rgba_to_bgra(width
, height
, src
, src_stride
,
393 for (row
= 0; row
< height
; ++row
) {
394 _mesa_pack_ubyte_rgba_row(dst_format
, width
,
395 (const uint8_t (*)[4])src
, dst
);
401 } else if (src_array_format
== RGBA32_UINT
&&
402 _mesa_is_format_unsigned(dst_format
)) {
403 assert(_mesa_is_format_integer_color(dst_format
));
404 for (row
= 0; row
< height
; ++row
) {
405 _mesa_pack_uint_rgba_row(dst_format
, width
,
406 (const uint32_t (*)[4])src
, dst
);
415 /* Handle conversions between array formats */
417 if (src_array_format
) {
418 src_type
= _mesa_array_format_get_datatype(src_array_format
);
420 src_num_channels
= _mesa_array_format_get_num_channels(src_array_format
);
422 _mesa_array_format_get_swizzle(src_array_format
, src2rgba
);
424 normalized
= _mesa_array_format_is_normalized(src_array_format
);
427 if (dst_array_format
) {
428 dst_type
= _mesa_array_format_get_datatype(dst_array_format
);
430 dst_num_channels
= _mesa_array_format_get_num_channels(dst_array_format
);
432 _mesa_array_format_get_swizzle(dst_array_format
, dst2rgba
);
433 invert_swizzle(rgba2dst
, dst2rgba
);
435 normalized
|= _mesa_array_format_is_normalized(dst_array_format
);
438 if (src_array_format
&& dst_array_format
) {
439 assert(_mesa_array_format_is_normalized(src_array_format
) ==
440 _mesa_array_format_is_normalized(dst_array_format
));
442 compute_src2dst_component_mapping(src2rgba
, rgba2dst
, rebase_swizzle
,
445 for (row
= 0; row
< height
; ++row
) {
446 _mesa_swizzle_and_convert(dst
, dst_type
, dst_num_channels
,
447 src
, src_type
, src_num_channels
,
448 src2dst
, normalized
, width
);
455 /* At this point, we're fresh out of fast-paths and we need to convert
456 * to float, uint32, or, if we're lucky, uint8.
461 if (src_array_format
) {
462 if (!_mesa_array_format_is_float(src_array_format
) &&
463 !_mesa_array_format_is_normalized(src_array_format
))
466 switch (_mesa_get_format_datatype(src_format
)) {
467 case GL_UNSIGNED_INT
:
474 /* If the destination format is signed but the source is unsigned, then we
475 * don't loose any data by converting to a signed intermediate format above
476 * and beyond the precision that we loose in the conversion itself. If the
477 * destination is unsigned then, by using an unsigned intermediate format,
478 * we make the conversion function that converts from the source to the
479 * intermediate format take care of truncating at zero. The exception here
480 * is if the intermediate format is float, in which case the first
481 * conversion will leave it signed and the second conversion will truncate
485 if (dst_array_format
) {
486 if (!_mesa_array_format_is_float(dst_array_format
) &&
487 !_mesa_array_format_is_normalized(dst_array_format
))
489 is_signed
= _mesa_array_format_is_signed(dst_array_format
);
490 bits
= 8 * _mesa_array_format_get_type_size(dst_array_format
);
492 switch (_mesa_get_format_datatype(dst_format
)) {
493 case GL_UNSIGNED_NORMALIZED
:
496 case GL_SIGNED_NORMALIZED
:
502 case GL_UNSIGNED_INT
:
511 bits
= _mesa_get_format_max_bits(dst_format
);
514 assert(src_integer
== dst_integer
);
516 if (src_integer
&& dst_integer
) {
517 tmp_uint
= malloc(width
* height
* sizeof(*tmp_uint
));
519 /* The [un]packing functions for unsigned datatypes treat the 32-bit
520 * integer array as signed for signed formats and as unsigned for
521 * unsigned formats. This is a bit of a problem if we ever convert from
522 * a signed to an unsigned format because the unsigned packing function
523 * doesn't know that the input is signed and will treat it as unsigned
524 * and not do the trunctation. The thing that saves us here is that all
525 * of the packed formats are unsigned, so we can just always use
526 * _mesa_swizzle_and_convert for signed formats, which is aware of the
527 * truncation problem.
529 common_type
= is_signed
? MESA_ARRAY_FORMAT_TYPE_INT
:
530 MESA_ARRAY_FORMAT_TYPE_UINT
;
531 if (src_array_format
) {
532 compute_rebased_rgba_component_mapping(src2rgba
, rebase_swizzle
,
534 for (row
= 0; row
< height
; ++row
) {
535 _mesa_swizzle_and_convert(tmp_uint
+ row
* width
, common_type
, 4,
536 src
, src_type
, src_num_channels
,
537 rebased_src2rgba
, normalized
, width
);
541 for (row
= 0; row
< height
; ++row
) {
542 _mesa_unpack_uint_rgba_row(src_format
, width
,
543 src
, tmp_uint
+ row
* width
);
545 _mesa_swizzle_and_convert(tmp_uint
+ row
* width
, common_type
, 4,
546 tmp_uint
+ row
* width
, common_type
, 4,
547 rebase_swizzle
, false, width
);
552 /* At this point, we have already done the truncation if the source is
553 * signed but the destination is unsigned, so no need to force the
554 * _mesa_swizzle_and_convert path.
556 if (dst_format_is_mesa_array_format
) {
557 for (row
= 0; row
< height
; ++row
) {
558 _mesa_swizzle_and_convert(dst
, dst_type
, dst_num_channels
,
559 tmp_uint
+ row
* width
, common_type
, 4,
560 rgba2dst
, normalized
, width
);
564 for (row
= 0; row
< height
; ++row
) {
565 _mesa_pack_uint_rgba_row(dst_format
, width
,
566 (const uint32_t (*)[4])tmp_uint
+ row
* width
, dst
);
572 } else if (is_signed
|| bits
> 8) {
573 tmp_float
= malloc(width
* height
* sizeof(*tmp_float
));
575 if (src_format_is_mesa_array_format
) {
576 compute_rebased_rgba_component_mapping(src2rgba
, rebase_swizzle
,
578 for (row
= 0; row
< height
; ++row
) {
579 _mesa_swizzle_and_convert(tmp_float
+ row
* width
,
580 MESA_ARRAY_FORMAT_TYPE_FLOAT
, 4,
581 src
, src_type
, src_num_channels
,
582 rebased_src2rgba
, normalized
, width
);
586 for (row
= 0; row
< height
; ++row
) {
587 _mesa_unpack_rgba_row(src_format
, width
,
588 src
, tmp_float
+ row
* width
);
590 _mesa_swizzle_and_convert(tmp_float
+ row
* width
,
591 MESA_ARRAY_FORMAT_TYPE_FLOAT
, 4,
592 tmp_float
+ row
* width
,
593 MESA_ARRAY_FORMAT_TYPE_FLOAT
, 4,
594 rebase_swizzle
, normalized
, width
);
599 if (dst_format_is_mesa_array_format
) {
600 for (row
= 0; row
< height
; ++row
) {
601 _mesa_swizzle_and_convert(dst
, dst_type
, dst_num_channels
,
602 tmp_float
+ row
* width
,
603 MESA_ARRAY_FORMAT_TYPE_FLOAT
, 4,
604 rgba2dst
, normalized
, width
);
608 for (row
= 0; row
< height
; ++row
) {
609 _mesa_pack_float_rgba_row(dst_format
, width
,
610 (const float (*)[4])tmp_float
+ row
* width
, dst
);
617 tmp_ubyte
= malloc(width
* height
* sizeof(*tmp_ubyte
));
619 if (src_format_is_mesa_array_format
) {
620 compute_rebased_rgba_component_mapping(src2rgba
, rebase_swizzle
,
622 for (row
= 0; row
< height
; ++row
) {
623 _mesa_swizzle_and_convert(tmp_ubyte
+ row
* width
,
624 MESA_ARRAY_FORMAT_TYPE_UBYTE
, 4,
625 src
, src_type
, src_num_channels
,
626 rebased_src2rgba
, normalized
, width
);
630 for (row
= 0; row
< height
; ++row
) {
631 _mesa_unpack_ubyte_rgba_row(src_format
, width
,
632 src
, tmp_ubyte
+ row
* width
);
634 _mesa_swizzle_and_convert(tmp_ubyte
+ row
* width
,
635 MESA_ARRAY_FORMAT_TYPE_UBYTE
, 4,
636 tmp_ubyte
+ row
* width
,
637 MESA_ARRAY_FORMAT_TYPE_UBYTE
, 4,
638 rebase_swizzle
, normalized
, width
);
643 if (dst_format_is_mesa_array_format
) {
644 for (row
= 0; row
< height
; ++row
) {
645 _mesa_swizzle_and_convert(dst
, dst_type
, dst_num_channels
,
646 tmp_ubyte
+ row
* width
,
647 MESA_ARRAY_FORMAT_TYPE_UBYTE
, 4,
648 rgba2dst
, normalized
, width
);
652 for (row
= 0; row
< height
; ++row
) {
653 _mesa_pack_ubyte_rgba_row(dst_format
, width
,
654 (const uint8_t (*)[4])tmp_ubyte
+ row
* width
, dst
);
663 static const uint8_t map_identity
[7] = { 0, 1, 2, 3, 4, 5, 6 };
664 #if UTIL_ARCH_BIG_ENDIAN
665 static const uint8_t map_3210
[7] = { 3, 2, 1, 0, 4, 5, 6 };
666 static const uint8_t map_1032
[7] = { 1, 0, 3, 2, 4, 5, 6 };
670 * Describes a format as an array format, if possible
672 * A helper function for figuring out if a (possibly packed) format is
673 * actually an array format and, if so, what the array parameters are.
675 * \param[in] format the mesa format
676 * \param[out] type the GL type of the array (GL_BYTE, etc.)
677 * \param[out] num_components the number of components in the array
678 * \param[out] swizzle a swizzle describing how to get from the
679 * given format to RGBA
680 * \param[out] normalized for integer formats, this represents whether
681 * the format is a normalized integer or a
683 * \return true if this format is an array format, false otherwise
686 _mesa_format_to_array(mesa_format format
, GLenum
*type
, int *num_components
,
687 uint8_t swizzle
[4], bool *normalized
)
690 GLuint format_components
;
691 uint8_t packed_swizzle
[4];
692 const uint8_t *endian
;
694 if (_mesa_is_format_compressed(format
))
697 *normalized
= !_mesa_is_format_integer(format
);
699 _mesa_uncompressed_format_to_type_and_comps(format
, type
, &format_components
);
701 switch (_mesa_get_format_layout(format
)) {
702 case MESA_FORMAT_LAYOUT_ARRAY
:
703 *num_components
= format_components
;
704 _mesa_get_format_swizzle(format
, swizzle
);
706 case MESA_FORMAT_LAYOUT_PACKED
:
708 case GL_UNSIGNED_BYTE
:
710 if (_mesa_get_format_max_bits(format
) != 8)
712 *num_components
= _mesa_get_format_bytes(format
);
713 switch (*num_components
) {
715 endian
= map_identity
;
718 #if UTIL_ARCH_LITTLE_ENDIAN
719 endian
= map_identity
;
725 #if UTIL_ARCH_LITTLE_ENDIAN
726 endian
= map_identity
;
732 endian
= map_identity
;
733 assert(!"Invalid number of components");
736 case GL_UNSIGNED_SHORT
:
739 if (_mesa_get_format_max_bits(format
) != 16)
741 *num_components
= _mesa_get_format_bytes(format
) / 2;
742 switch (*num_components
) {
744 endian
= map_identity
;
747 #if UTIL_ARCH_LITTLE_ENDIAN
748 endian
= map_identity
;
754 endian
= map_identity
;
755 assert(!"Invalid number of components");
758 case GL_UNSIGNED_INT
:
761 /* This isn't packed. At least not really. */
762 assert(format_components
== 1);
763 if (_mesa_get_format_max_bits(format
) != 32)
765 *num_components
= format_components
;
766 endian
= map_identity
;
772 _mesa_get_format_swizzle(format
, packed_swizzle
);
774 for (i
= 0; i
< 4; ++i
)
775 swizzle
[i
] = endian
[packed_swizzle
[i
]];
778 case MESA_FORMAT_LAYOUT_OTHER
:
785 * Attempts to perform the given swizzle-and-convert operation with memcpy
787 * This function determines if the given swizzle-and-convert operation can
788 * be done with a simple memcpy and, if so, does the memcpy. If not, it
789 * returns false and we fall back to the standard version below.
791 * The arguments are exactly the same as for _mesa_swizzle_and_convert
793 * \return true if it successfully performed the swizzle-and-convert
794 * operation with memcpy, false otherwise
797 swizzle_convert_try_memcpy(void *dst
,
798 enum mesa_array_format_datatype dst_type
,
799 int num_dst_channels
,
801 enum mesa_array_format_datatype src_type
,
802 int num_src_channels
,
803 const uint8_t swizzle
[4], bool normalized
, int count
)
807 if (src_type
!= dst_type
)
809 if (num_src_channels
!= num_dst_channels
)
812 for (i
= 0; i
< num_dst_channels
; ++i
)
813 if (swizzle
[i
] != i
&& swizzle
[i
] != MESA_FORMAT_SWIZZLE_NONE
)
816 memcpy(dst
, src
, count
* num_src_channels
*
817 _mesa_array_format_datatype_get_size(src_type
));
823 * Represents a single instance of the standard swizzle-and-convert loop
825 * Any swizzle-and-convert operation simply loops through the pixels and
826 * performs the transformation operation one pixel at a time. This macro
827 * embodies one instance of the conversion loop. This way we can do all
828 * control flow outside of the loop and allow the compiler to unroll
829 * everything inside the loop.
831 * Note: This loop is carefully crafted for performance. Be careful when
832 * changing it and run some benchmarks to ensure no performance regressions
835 * \param DST_TYPE the C datatype of the destination
836 * \param DST_CHANS the number of destination channels
837 * \param SRC_TYPE the C datatype of the source
838 * \param SRC_CHANS the number of source channels
839 * \param CONV an expression for converting from the source data,
840 * storred in the variable "src", to the destination
843 #define SWIZZLE_CONVERT_LOOP(DST_TYPE, DST_CHANS, SRC_TYPE, SRC_CHANS, CONV) \
846 for (s = 0; s < count; ++s) { \
847 for (j = 0; j < SRC_CHANS; ++j) { \
848 SRC_TYPE src = typed_src[j]; \
852 typed_dst[0] = tmp[swizzle_x]; \
853 if (DST_CHANS > 1) { \
854 typed_dst[1] = tmp[swizzle_y]; \
855 if (DST_CHANS > 2) { \
856 typed_dst[2] = tmp[swizzle_z]; \
857 if (DST_CHANS > 3) { \
858 typed_dst[3] = tmp[swizzle_w]; \
862 typed_src += SRC_CHANS; \
863 typed_dst += DST_CHANS; \
868 * Represents a single swizzle-and-convert operation
870 * This macro represents everything done in a single swizzle-and-convert
871 * operation. The actual work is done by the SWIZZLE_CONVERT_LOOP macro.
872 * This macro acts as a wrapper that uses a nested switch to ensure that
873 * all looping parameters get unrolled.
875 * This macro makes assumptions about variables etc. in the calling
876 * function. Changes to _mesa_swizzle_and_convert may require changes to
879 * \param DST_TYPE the C datatype of the destination
880 * \param SRC_TYPE the C datatype of the source
881 * \param CONV an expression for converting from the source data,
882 * storred in the variable "src", to the destination
885 #define SWIZZLE_CONVERT(DST_TYPE, SRC_TYPE, CONV) \
887 const uint8_t swizzle_x = swizzle[0]; \
888 const uint8_t swizzle_y = swizzle[1]; \
889 const uint8_t swizzle_z = swizzle[2]; \
890 const uint8_t swizzle_w = swizzle[3]; \
891 const SRC_TYPE *typed_src = void_src; \
892 DST_TYPE *typed_dst = void_dst; \
896 switch (num_dst_channels) { \
898 switch (num_src_channels) { \
900 SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 1, CONV); \
903 SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 2, CONV); \
906 SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 3, CONV); \
909 SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 4, CONV); \
914 switch (num_src_channels) { \
916 SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 1, CONV); \
919 SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 2, CONV); \
922 SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 3, CONV); \
925 SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 4, CONV); \
930 switch (num_src_channels) { \
932 SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 1, CONV); \
935 SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 2, CONV); \
938 SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 3, CONV); \
941 SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 4, CONV); \
946 switch (num_src_channels) { \
948 SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 1, CONV); \
951 SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 2, CONV); \
954 SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 3, CONV); \
957 SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 4, CONV); \
966 convert_float(void *void_dst
, int num_dst_channels
,
967 const void *void_src
, GLenum src_type
, int num_src_channels
,
968 const uint8_t swizzle
[4], bool normalized
, int count
)
970 const float one
= 1.0f
;
973 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
974 SWIZZLE_CONVERT(float, float, src
);
976 case MESA_ARRAY_FORMAT_TYPE_HALF
:
977 SWIZZLE_CONVERT(float, uint16_t, _mesa_half_to_float(src
));
979 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
981 SWIZZLE_CONVERT(float, uint8_t, _mesa_unorm_to_float(src
, 8));
983 SWIZZLE_CONVERT(float, uint8_t, src
);
986 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
988 SWIZZLE_CONVERT(float, int8_t, _mesa_snorm_to_float(src
, 8));
990 SWIZZLE_CONVERT(float, int8_t, src
);
993 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
995 SWIZZLE_CONVERT(float, uint16_t, _mesa_unorm_to_float(src
, 16));
997 SWIZZLE_CONVERT(float, uint16_t, src
);
1000 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1002 SWIZZLE_CONVERT(float, int16_t, _mesa_snorm_to_float(src
, 16));
1004 SWIZZLE_CONVERT(float, int16_t, src
);
1007 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1009 SWIZZLE_CONVERT(float, uint32_t, _mesa_unorm_to_float(src
, 32));
1011 SWIZZLE_CONVERT(float, uint32_t, src
);
1014 case MESA_ARRAY_FORMAT_TYPE_INT
:
1016 SWIZZLE_CONVERT(float, int32_t, _mesa_snorm_to_float(src
, 32));
1018 SWIZZLE_CONVERT(float, int32_t, src
);
1022 assert(!"Invalid channel type combination");
1028 convert_half_float(void *void_dst
, int num_dst_channels
,
1029 const void *void_src
, GLenum src_type
, int num_src_channels
,
1030 const uint8_t swizzle
[4], bool normalized
, int count
)
1032 const uint16_t one
= _mesa_float_to_half(1.0f
);
1035 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1036 SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_half(src
));
1038 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1039 SWIZZLE_CONVERT(uint16_t, uint16_t, src
);
1041 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1043 SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_unorm_to_half(src
, 8));
1045 SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_float_to_half(src
));
1048 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1050 SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_snorm_to_half(src
, 8));
1052 SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_float_to_half(src
));
1055 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1057 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_unorm_to_half(src
, 16));
1059 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_float_to_half(src
));
1062 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1064 SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_snorm_to_half(src
, 16));
1066 SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_float_to_half(src
));
1069 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1071 SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unorm_to_half(src
, 32));
1073 SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_float_to_half(src
));
1076 case MESA_ARRAY_FORMAT_TYPE_INT
:
1078 SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_snorm_to_half(src
, 32));
1080 SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_float_to_half(src
));
1084 assert(!"Invalid channel type combination");
1089 convert_ubyte(void *void_dst
, int num_dst_channels
,
1090 const void *void_src
, GLenum src_type
, int num_src_channels
,
1091 const uint8_t swizzle
[4], bool normalized
, int count
)
1093 const uint8_t one
= normalized
? UINT8_MAX
: 1;
1096 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1098 SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_unorm(src
, 8));
1100 SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_unsigned(src
, 8));
1103 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1105 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_unorm(src
, 8));
1107 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_unsigned(src
, 8));
1110 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1111 SWIZZLE_CONVERT(uint8_t, uint8_t, src
);
1113 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1115 SWIZZLE_CONVERT(uint8_t, int8_t, _mesa_snorm_to_unorm(src
, 8, 8));
1117 SWIZZLE_CONVERT(uint8_t, int8_t, _mesa_signed_to_unsigned(src
, 8));
1120 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1122 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_unorm_to_unorm(src
, 16, 8));
1124 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_unsigned_to_unsigned(src
, 8));
1127 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1129 SWIZZLE_CONVERT(uint8_t, int16_t, _mesa_snorm_to_unorm(src
, 16, 8));
1131 SWIZZLE_CONVERT(uint8_t, int16_t, _mesa_signed_to_unsigned(src
, 8));
1134 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1136 SWIZZLE_CONVERT(uint8_t, uint32_t, _mesa_unorm_to_unorm(src
, 32, 8));
1138 SWIZZLE_CONVERT(uint8_t, uint32_t, _mesa_unsigned_to_unsigned(src
, 8));
1141 case MESA_ARRAY_FORMAT_TYPE_INT
:
1143 SWIZZLE_CONVERT(uint8_t, int32_t, _mesa_snorm_to_unorm(src
, 32, 8));
1145 SWIZZLE_CONVERT(uint8_t, int32_t, _mesa_signed_to_unsigned(src
, 8));
1149 assert(!"Invalid channel type combination");
1155 convert_byte(void *void_dst
, int num_dst_channels
,
1156 const void *void_src
, GLenum src_type
, int num_src_channels
,
1157 const uint8_t swizzle
[4], bool normalized
, int count
)
1159 const int8_t one
= normalized
? INT8_MAX
: 1;
1162 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1164 SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_snorm(src
, 8));
1166 SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_signed(src
, 8));
1169 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1171 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_snorm(src
, 8));
1173 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_signed(src
, 8));
1176 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1178 SWIZZLE_CONVERT(int8_t, uint8_t, _mesa_unorm_to_snorm(src
, 8, 8));
1180 SWIZZLE_CONVERT(int8_t, uint8_t, _mesa_unsigned_to_signed(src
, 8));
1183 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1184 SWIZZLE_CONVERT(int8_t, int8_t, src
);
1186 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1188 SWIZZLE_CONVERT(int8_t, uint16_t, _mesa_unorm_to_snorm(src
, 16, 8));
1190 SWIZZLE_CONVERT(int8_t, uint16_t, _mesa_unsigned_to_signed(src
, 8));
1193 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1195 SWIZZLE_CONVERT(int8_t, int16_t, _mesa_snorm_to_snorm(src
, 16, 8));
1197 SWIZZLE_CONVERT(int8_t, int16_t, _mesa_signed_to_signed(src
, 8));
1200 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1202 SWIZZLE_CONVERT(int8_t, uint32_t, _mesa_unorm_to_snorm(src
, 32, 8));
1204 SWIZZLE_CONVERT(int8_t, uint32_t, _mesa_unsigned_to_signed(src
, 8));
1207 case MESA_ARRAY_FORMAT_TYPE_INT
:
1209 SWIZZLE_CONVERT(int8_t, int32_t, _mesa_snorm_to_snorm(src
, 32, 8));
1211 SWIZZLE_CONVERT(int8_t, int32_t, _mesa_signed_to_signed(src
, 8));
1215 assert(!"Invalid channel type combination");
1221 convert_ushort(void *void_dst
, int num_dst_channels
,
1222 const void *void_src
, GLenum src_type
, int num_src_channels
,
1223 const uint8_t swizzle
[4], bool normalized
, int count
)
1225 const uint16_t one
= normalized
? UINT16_MAX
: 1;
1228 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1230 SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_unorm(src
, 16));
1232 SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_unsigned(src
, 16));
1235 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1237 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_unorm(src
, 16));
1239 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_unsigned(src
, 16));
1242 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1244 SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_unorm_to_unorm(src
, 8, 16));
1246 SWIZZLE_CONVERT(uint16_t, uint8_t, src
);
1249 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1251 SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_snorm_to_unorm(src
, 8, 16));
1253 SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_signed_to_unsigned(src
, 16));
1256 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1257 SWIZZLE_CONVERT(uint16_t, uint16_t, src
);
1259 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1261 SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_snorm_to_unorm(src
, 16, 16));
1263 SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_signed_to_unsigned(src
, 16));
1266 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1268 SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unorm_to_unorm(src
, 32, 16));
1270 SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unsigned_to_unsigned(src
, 16));
1273 case MESA_ARRAY_FORMAT_TYPE_INT
:
1275 SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_snorm_to_unorm(src
, 32, 16));
1277 SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_signed_to_unsigned(src
, 16));
1281 assert(!"Invalid channel type combination");
1287 convert_short(void *void_dst
, int num_dst_channels
,
1288 const void *void_src
, GLenum src_type
, int num_src_channels
,
1289 const uint8_t swizzle
[4], bool normalized
, int count
)
1291 const int16_t one
= normalized
? INT16_MAX
: 1;
1294 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1296 SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_snorm(src
, 16));
1298 SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_signed(src
, 16));
1301 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1303 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_snorm(src
, 16));
1305 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_signed(src
, 16));
1308 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1310 SWIZZLE_CONVERT(int16_t, uint8_t, _mesa_unorm_to_snorm(src
, 8, 16));
1312 SWIZZLE_CONVERT(int16_t, uint8_t, src
);
1315 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1317 SWIZZLE_CONVERT(int16_t, int8_t, _mesa_snorm_to_snorm(src
, 8, 16));
1319 SWIZZLE_CONVERT(int16_t, int8_t, src
);
1322 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1324 SWIZZLE_CONVERT(int16_t, uint16_t, _mesa_unorm_to_snorm(src
, 16, 16));
1326 SWIZZLE_CONVERT(int16_t, uint16_t, _mesa_unsigned_to_signed(src
, 16));
1329 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1330 SWIZZLE_CONVERT(int16_t, int16_t, src
);
1332 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1334 SWIZZLE_CONVERT(int16_t, uint32_t, _mesa_unorm_to_snorm(src
, 32, 16));
1336 SWIZZLE_CONVERT(int16_t, uint32_t, _mesa_unsigned_to_signed(src
, 16));
1339 case MESA_ARRAY_FORMAT_TYPE_INT
:
1341 SWIZZLE_CONVERT(int16_t, int32_t, _mesa_snorm_to_snorm(src
, 32, 16));
1343 SWIZZLE_CONVERT(int16_t, int32_t, _mesa_signed_to_signed(src
, 16));
1347 assert(!"Invalid channel type combination");
1352 convert_uint(void *void_dst
, int num_dst_channels
,
1353 const void *void_src
, GLenum src_type
, int num_src_channels
,
1354 const uint8_t swizzle
[4], bool normalized
, int count
)
1356 const uint32_t one
= normalized
? UINT32_MAX
: 1;
1359 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1361 SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_unorm(src
, 32));
1363 SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_unsigned(src
, 32));
1366 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1368 SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_unorm(src
, 32));
1370 SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_unsigned(src
, 32));
1373 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1375 SWIZZLE_CONVERT(uint32_t, uint8_t, _mesa_unorm_to_unorm(src
, 8, 32));
1377 SWIZZLE_CONVERT(uint32_t, uint8_t, src
);
1380 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1382 SWIZZLE_CONVERT(uint32_t, int8_t, _mesa_snorm_to_unorm(src
, 8, 32));
1384 SWIZZLE_CONVERT(uint32_t, int8_t, _mesa_signed_to_unsigned(src
, 32));
1387 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1389 SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_unorm_to_unorm(src
, 16, 32));
1391 SWIZZLE_CONVERT(uint32_t, uint16_t, src
);
1394 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1396 SWIZZLE_CONVERT(uint32_t, int16_t, _mesa_snorm_to_unorm(src
, 16, 32));
1398 SWIZZLE_CONVERT(uint32_t, int16_t, _mesa_signed_to_unsigned(src
, 32));
1401 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1402 SWIZZLE_CONVERT(uint32_t, uint32_t, src
);
1404 case MESA_ARRAY_FORMAT_TYPE_INT
:
1406 SWIZZLE_CONVERT(uint32_t, int32_t, _mesa_snorm_to_unorm(src
, 32, 32));
1408 SWIZZLE_CONVERT(uint32_t, int32_t, _mesa_signed_to_unsigned(src
, 32));
1412 assert(!"Invalid channel type combination");
1418 convert_int(void *void_dst
, int num_dst_channels
,
1419 const void *void_src
, GLenum src_type
, int num_src_channels
,
1420 const uint8_t swizzle
[4], bool normalized
, int count
)
1422 const int32_t one
= normalized
? INT32_MAX
: 1;
1425 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1427 SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_snorm(src
, 32));
1429 SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_signed(src
, 32));
1432 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1434 SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_snorm(src
, 32));
1436 SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_signed(src
, 32));
1439 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1441 SWIZZLE_CONVERT(int32_t, uint8_t, _mesa_unorm_to_snorm(src
, 8, 32));
1443 SWIZZLE_CONVERT(int32_t, uint8_t, src
);
1446 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1448 SWIZZLE_CONVERT(int32_t, int8_t, _mesa_snorm_to_snorm(src
, 8, 32));
1450 SWIZZLE_CONVERT(int32_t, int8_t, src
);
1453 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1455 SWIZZLE_CONVERT(int32_t, uint16_t, _mesa_unorm_to_snorm(src
, 16, 32));
1457 SWIZZLE_CONVERT(int32_t, uint16_t, src
);
1460 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1462 SWIZZLE_CONVERT(int32_t, int16_t, _mesa_snorm_to_snorm(src
, 16, 32));
1464 SWIZZLE_CONVERT(int32_t, int16_t, src
);
1467 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1469 SWIZZLE_CONVERT(int32_t, uint32_t, _mesa_unorm_to_snorm(src
, 32, 32));
1471 SWIZZLE_CONVERT(int32_t, uint32_t, _mesa_unsigned_to_signed(src
, 32));
1474 case MESA_ARRAY_FORMAT_TYPE_INT
:
1475 SWIZZLE_CONVERT(int32_t, int32_t, src
);
1478 assert(!"Invalid channel type combination");
1484 * Convert between array-based color formats.
1486 * Most format conversion operations required by GL can be performed by
1487 * converting one channel at a time, shuffling the channels around, and
1488 * optionally filling missing channels with zeros and ones. This function
1489 * does just that in a general, yet efficient, way.
1491 * The swizzle parameter is an array of 4 numbers (see
1492 * _mesa_get_format_swizzle) that describes where each channel in the
1493 * destination should come from in the source. If swizzle[i] < 4 then it
1494 * means that dst[i] = CONVERT(src[swizzle[i]]). If swizzle[i] is
1495 * MESA_FORMAT_SWIZZLE_ZERO or MESA_FORMAT_SWIZZLE_ONE, the corresponding
1496 * dst[i] will be filled with the appropreate representation of zero or one
1499 * Under most circumstances, the source and destination images must be
1500 * different as no care is taken not to clobber one with the other.
1501 * However, if they have the same number of bits per pixel, it is safe to
1502 * do an in-place conversion.
1504 * \param[out] dst pointer to where the converted data should
1507 * \param[in] dst_type the destination GL type of the converted
1508 * data (GL_BYTE, etc.)
1510 * \param[in] num_dst_channels the number of channels in the converted
1513 * \param[in] src pointer to the source data
1515 * \param[in] src_type the GL type of the source data (GL_BYTE,
1518 * \param[in] num_src_channels the number of channels in the source data
1519 * (the number of channels total, not just
1522 * \param[in] swizzle describes how to get the destination data
1523 * from the source data.
1525 * \param[in] normalized for integer types, this indicates whether
1526 * the data should be considered as integers
1527 * or as normalized integers;
1529 * \param[in] count the number of pixels to convert
1532 _mesa_swizzle_and_convert(void *void_dst
, enum mesa_array_format_datatype dst_type
, int num_dst_channels
,
1533 const void *void_src
, enum mesa_array_format_datatype src_type
, int num_src_channels
,
1534 const uint8_t swizzle
[4], bool normalized
, int count
)
1536 if (swizzle_convert_try_memcpy(void_dst
, dst_type
, num_dst_channels
,
1537 void_src
, src_type
, num_src_channels
,
1538 swizzle
, normalized
, count
))
1542 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1543 convert_float(void_dst
, num_dst_channels
, void_src
, src_type
,
1544 num_src_channels
, swizzle
, normalized
, count
);
1546 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1547 convert_half_float(void_dst
, num_dst_channels
, void_src
, src_type
,
1548 num_src_channels
, swizzle
, normalized
, count
);
1550 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1551 convert_ubyte(void_dst
, num_dst_channels
, void_src
, src_type
,
1552 num_src_channels
, swizzle
, normalized
, count
);
1554 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1555 convert_byte(void_dst
, num_dst_channels
, void_src
, src_type
,
1556 num_src_channels
, swizzle
, normalized
, count
);
1558 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1559 convert_ushort(void_dst
, num_dst_channels
, void_src
, src_type
,
1560 num_src_channels
, swizzle
, normalized
, count
);
1562 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1563 convert_short(void_dst
, num_dst_channels
, void_src
, src_type
,
1564 num_src_channels
, swizzle
, normalized
, count
);
1566 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1567 convert_uint(void_dst
, num_dst_channels
, void_src
, src_type
,
1568 num_src_channels
, swizzle
, normalized
, count
);
1570 case MESA_ARRAY_FORMAT_TYPE_INT
:
1571 convert_int(void_dst
, num_dst_channels
, void_src
, src_type
,
1572 num_src_channels
, swizzle
, normalized
, count
);
1575 assert(!"Invalid channel type");