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 RGBA32_UINT
=
40 MESA_ARRAY_FORMAT(MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS
,
41 4, 0, 0, 0, 4, 0, 1, 2, 3);
43 const mesa_array_format RGBA32_INT
=
44 MESA_ARRAY_FORMAT(MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS
,
45 4, 1, 0, 0, 4, 0, 1, 2, 3);
48 invert_swizzle(uint8_t dst
[4], const uint8_t src
[4])
52 dst
[0] = MESA_FORMAT_SWIZZLE_NONE
;
53 dst
[1] = MESA_FORMAT_SWIZZLE_NONE
;
54 dst
[2] = MESA_FORMAT_SWIZZLE_NONE
;
55 dst
[3] = MESA_FORMAT_SWIZZLE_NONE
;
57 for (i
= 0; i
< 4; ++i
)
58 for (j
= 0; j
< 4; ++j
)
59 if (src
[j
] == i
&& dst
[i
] == MESA_FORMAT_SWIZZLE_NONE
)
63 /* Takes a src to RGBA swizzle and applies a rebase swizzle to it. This
64 * is used when we need to rebase a format to match a different
65 * base internal format.
67 * The rebase swizzle can be NULL, which means that no rebase is necessary,
68 * in which case the src to RGBA swizzle is copied to the output without
71 * The resulting rebased swizzle and well as the input swizzles are
72 * all 4-element swizzles, but the rebase swizzle can be NULL if no rebase
76 compute_rebased_rgba_component_mapping(uint8_t *src2rgba
,
77 uint8_t *rebase_swizzle
,
78 uint8_t *rebased_src2rgba
)
83 for (i
= 0; i
< 4; i
++) {
84 if (rebase_swizzle
[i
] > MESA_FORMAT_SWIZZLE_W
)
85 rebased_src2rgba
[i
] = rebase_swizzle
[i
];
87 rebased_src2rgba
[i
] = src2rgba
[rebase_swizzle
[i
]];
90 /* No rebase needed, so src2rgba is all that we need */
91 memcpy(rebased_src2rgba
, src2rgba
, 4 * sizeof(uint8_t));
95 /* Computes the final swizzle transform to apply from src to dst in a
96 * conversion that might involve a rebase swizzle.
98 * This is used to compute the swizzle transform to apply in conversions
99 * between array formats where we have a src2rgba swizzle, a rgba2dst swizzle
100 * and possibly, a rebase swizzle.
102 * The final swizzle transform to apply (src2dst) when a rebase swizzle is
103 * involved is: src -> rgba -> base -> rgba -> dst
106 compute_src2dst_component_mapping(uint8_t *src2rgba
, uint8_t *rgba2dst
,
107 uint8_t *rebase_swizzle
, uint8_t *src2dst
)
111 if (!rebase_swizzle
) {
112 for (i
= 0; i
< 4; i
++) {
113 if (rgba2dst
[i
] > MESA_FORMAT_SWIZZLE_W
) {
114 src2dst
[i
] = rgba2dst
[i
];
116 src2dst
[i
] = src2rgba
[rgba2dst
[i
]];
120 for (i
= 0; i
< 4; i
++) {
121 if (rgba2dst
[i
] > MESA_FORMAT_SWIZZLE_W
) {
122 src2dst
[i
] = rgba2dst
[i
];
123 } else if (rebase_swizzle
[rgba2dst
[i
]] > MESA_FORMAT_SWIZZLE_W
) {
124 src2dst
[i
] = rebase_swizzle
[rgba2dst
[i
]];
126 src2dst
[i
] = src2rgba
[rebase_swizzle
[rgba2dst
[i
]]];
133 * This function is used by clients of _mesa_format_convert to obtain
134 * the rebase swizzle to use in a format conversion based on the base
137 * \param baseFormat the base internal format involved in the conversion.
138 * \param map the rebase swizzle to consider
140 * This function computes 'map' as rgba -> baseformat -> rgba and returns true
141 * if the resulting swizzle transform is not the identity transform (thus, a
142 * rebase is needed). If the function returns false then a rebase swizzle
143 * is not necessary and the value of 'map' is undefined. In this situation
144 * clients of _mesa_format_convert should pass NULL in the 'rebase_swizzle'
148 _mesa_compute_rgba2base2rgba_component_mapping(GLenum baseFormat
, uint8_t *map
)
150 uint8_t rgba2base
[6], base2rgba
[6];
153 switch (baseFormat
) {
166 case GL_LUMINANCE_ALPHA
:
168 bool needRebase
= false;
169 _mesa_compute_component_mapping(GL_RGBA
, baseFormat
, rgba2base
);
170 _mesa_compute_component_mapping(baseFormat
, GL_RGBA
, base2rgba
);
171 for (i
= 0; i
< 4; i
++) {
172 if (base2rgba
[i
] > MESA_FORMAT_SWIZZLE_W
) {
173 map
[i
] = base2rgba
[i
];
175 map
[i
] = rgba2base
[base2rgba
[i
]];
183 unreachable("Unexpected base format");
189 * Special case conversion function to swap r/b channels from the source
190 * image to the dest image.
193 convert_ubyte_rgba_to_bgra(size_t width
, size_t height
,
194 const uint8_t *src
, size_t src_stride
,
195 uint8_t *dst
, size_t dst_stride
)
199 if (sizeof(void *) == 8 &&
200 src_stride
% 8 == 0 &&
201 dst_stride
% 8 == 0 &&
202 (GLsizeiptr
) src
% 8 == 0 &&
203 (GLsizeiptr
) dst
% 8 == 0) {
204 /* use 64-bit word to swizzle two 32-bit pixels. We need 8-byte
205 * alignment for src/dst addresses and strides.
207 for (row
= 0; row
< height
; row
++) {
208 const GLuint64
*s
= (const GLuint64
*) src
;
209 GLuint64
*d
= (GLuint64
*) dst
;
211 for (i
= 0; i
< width
/2; i
++) {
212 d
[i
] = ( (s
[i
] & 0xff00ff00ff00ff00) |
213 ((s
[i
] & 0xff000000ff) << 16) |
214 ((s
[i
] & 0xff000000ff0000) >> 16));
217 /* handle the case of odd widths */
218 const GLuint s
= ((const GLuint
*) src
)[width
- 1];
219 GLuint
*d
= (GLuint
*) dst
+ width
- 1;
220 *d
= ( (s
& 0xff00ff00) |
222 ((s
& 0xff0000) >> 16));
228 for (row
= 0; row
< height
; row
++) {
229 const GLuint
*s
= (const GLuint
*) src
;
230 GLuint
*d
= (GLuint
*) dst
;
232 for (i
= 0; i
< width
; i
++) {
233 d
[i
] = ( (s
[i
] & 0xff00ff00) |
234 ((s
[i
] & 0xff) << 16) |
235 ((s
[i
] & 0xff0000) >> 16));
245 * This can be used to convert between most color formats.
248 * - This function doesn't handle GL_COLOR_INDEX or YCBCR formats.
249 * - This function doesn't handle byte-swapping or transferOps, these should
250 * be handled by the caller.
252 * \param void_dst The address where converted color data will be stored.
253 * The caller must ensure that the buffer is large enough
254 * to hold the converted pixel data.
255 * \param dst_format The destination color format. It can be a mesa_format
256 * or a mesa_array_format represented as an uint32_t.
257 * \param dst_stride The stride of the destination format in bytes.
258 * \param void_src The address of the source color data to convert.
259 * \param src_format The source color format. It can be a mesa_format
260 * or a mesa_array_format represented as an uint32_t.
261 * \param src_stride The stride of the source format in bytes.
262 * \param width The width, in pixels, of the source image to convert.
263 * \param height The height, in pixels, of the source image to convert.
264 * \param rebase_swizzle A swizzle transform to apply during the conversion,
265 * typically used to match a different internal base
266 * format involved. NULL if no rebase transform is needed
267 * (i.e. the internal base format and the base format of
268 * the dst or the src -depending on whether we are doing
269 * an upload or a download respectively- are the same).
272 _mesa_format_convert(void *void_dst
, uint32_t dst_format
, size_t dst_stride
,
273 void *void_src
, uint32_t src_format
, size_t src_stride
,
274 size_t width
, size_t height
, uint8_t *rebase_swizzle
)
276 uint8_t *dst
= (uint8_t *)void_dst
;
277 uint8_t *src
= (uint8_t *)void_src
;
278 mesa_array_format src_array_format
, dst_array_format
;
279 bool src_format_is_mesa_array_format
, dst_format_is_mesa_array_format
;
280 uint8_t src2dst
[4], src2rgba
[4], rgba2dst
[4], dst2rgba
[4];
281 uint8_t rebased_src2rgba
[4];
282 enum mesa_array_format_datatype src_type
= 0, dst_type
= 0, common_type
;
283 bool normalized
, dst_integer
, src_integer
, is_signed
;
284 int src_num_channels
= 0, dst_num_channels
= 0;
285 uint8_t (*tmp_ubyte
)[4];
286 float (*tmp_float
)[4];
287 uint32_t (*tmp_uint
)[4];
291 if (_mesa_format_is_mesa_array_format(src_format
)) {
292 src_format_is_mesa_array_format
= true;
293 src_array_format
= src_format
;
295 assert(_mesa_is_format_color_format(src_format
));
296 src_format_is_mesa_array_format
= false;
297 src_array_format
= _mesa_format_to_array_format(src_format
);
300 if (_mesa_format_is_mesa_array_format(dst_format
)) {
301 dst_format_is_mesa_array_format
= true;
302 dst_array_format
= dst_format
;
304 assert(_mesa_is_format_color_format(dst_format
));
305 dst_format_is_mesa_array_format
= false;
306 dst_array_format
= _mesa_format_to_array_format(dst_format
);
309 /* First we see if we can implement the conversion with a direct pack
312 * In this case we want to be careful when we need to apply a swizzle to
313 * match an internal base format, since in these cases a simple pack/unpack
314 * to the dst format from the src format may not match the requirements
315 * of the internal base format. For now we decide to be safe and
316 * avoid this path in these scenarios but in the future we may want to
317 * enable it for specific combinations that are known to work.
319 if (!rebase_swizzle
) {
320 /* Do a direct memcpy where possible */
321 if ((dst_format_is_mesa_array_format
&&
322 src_format_is_mesa_array_format
&&
323 src_array_format
== dst_array_format
) ||
324 src_format
== dst_format
) {
325 int format_size
= _mesa_get_format_bytes(src_format
);
326 for (row
= 0; row
< height
; row
++) {
327 memcpy(dst
, src
, width
* format_size
);
334 /* Handle the cases where we can directly unpack */
335 if (!src_format_is_mesa_array_format
) {
336 if (dst_array_format
== RGBA32_FLOAT
) {
337 for (row
= 0; row
< height
; ++row
) {
338 _mesa_unpack_rgba_row(src_format
, width
,
339 src
, (float (*)[4])dst
);
344 } else if (dst_array_format
== RGBA8_UBYTE
) {
345 assert(!_mesa_is_format_integer_color(src_format
));
346 for (row
= 0; row
< height
; ++row
) {
347 _mesa_unpack_ubyte_rgba_row(src_format
, width
,
348 src
, (uint8_t (*)[4])dst
);
353 } else if (dst_array_format
== RGBA32_UINT
&&
354 _mesa_is_format_unsigned(src_format
)) {
355 assert(_mesa_is_format_integer_color(src_format
));
356 for (row
= 0; row
< height
; ++row
) {
357 _mesa_unpack_uint_rgba_row(src_format
, width
,
358 src
, (uint32_t (*)[4])dst
);
366 /* Handle the cases where we can directly pack */
367 if (!dst_format_is_mesa_array_format
) {
368 if (src_array_format
== RGBA32_FLOAT
) {
369 for (row
= 0; row
< height
; ++row
) {
370 _mesa_pack_float_rgba_row(dst_format
, width
,
371 (const float (*)[4])src
, dst
);
376 } else if (src_array_format
== RGBA8_UBYTE
) {
377 assert(!_mesa_is_format_integer_color(dst_format
));
379 if (dst_format
== MESA_FORMAT_B8G8R8A8_UNORM
) {
380 convert_ubyte_rgba_to_bgra(width
, height
, src
, src_stride
,
384 for (row
= 0; row
< height
; ++row
) {
385 _mesa_pack_ubyte_rgba_row(dst_format
, width
,
386 (const uint8_t (*)[4])src
, dst
);
392 } else if (src_array_format
== RGBA32_UINT
&&
393 _mesa_is_format_unsigned(dst_format
)) {
394 assert(_mesa_is_format_integer_color(dst_format
));
395 for (row
= 0; row
< height
; ++row
) {
396 _mesa_pack_uint_rgba_row(dst_format
, width
,
397 (const uint32_t (*)[4])src
, dst
);
406 /* Handle conversions between array formats */
408 if (src_array_format
) {
409 src_type
= _mesa_array_format_get_datatype(src_array_format
);
411 src_num_channels
= _mesa_array_format_get_num_channels(src_array_format
);
413 _mesa_array_format_get_swizzle(src_array_format
, src2rgba
);
415 normalized
= _mesa_array_format_is_normalized(src_array_format
);
418 if (dst_array_format
) {
419 dst_type
= _mesa_array_format_get_datatype(dst_array_format
);
421 dst_num_channels
= _mesa_array_format_get_num_channels(dst_array_format
);
423 _mesa_array_format_get_swizzle(dst_array_format
, dst2rgba
);
424 invert_swizzle(rgba2dst
, dst2rgba
);
426 normalized
|= _mesa_array_format_is_normalized(dst_array_format
);
429 if (src_array_format
&& dst_array_format
) {
430 assert(_mesa_array_format_is_normalized(src_array_format
) ==
431 _mesa_array_format_is_normalized(dst_array_format
));
433 compute_src2dst_component_mapping(src2rgba
, rgba2dst
, rebase_swizzle
,
436 for (row
= 0; row
< height
; ++row
) {
437 _mesa_swizzle_and_convert(dst
, dst_type
, dst_num_channels
,
438 src
, src_type
, src_num_channels
,
439 src2dst
, normalized
, width
);
446 /* At this point, we're fresh out of fast-paths and we need to convert
447 * to float, uint32, or, if we're lucky, uint8.
452 if (src_array_format
) {
453 if (!_mesa_array_format_is_float(src_array_format
) &&
454 !_mesa_array_format_is_normalized(src_array_format
))
457 switch (_mesa_get_format_datatype(src_format
)) {
458 case GL_UNSIGNED_INT
:
465 /* If the destination format is signed but the source is unsigned, then we
466 * don't loose any data by converting to a signed intermediate format above
467 * and beyond the precision that we loose in the conversion itself. If the
468 * destination is unsigned then, by using an unsigned intermediate format,
469 * we make the conversion function that converts from the source to the
470 * intermediate format take care of truncating at zero. The exception here
471 * is if the intermediate format is float, in which case the first
472 * conversion will leave it signed and the second conversion will truncate
476 if (dst_array_format
) {
477 if (!_mesa_array_format_is_float(dst_array_format
) &&
478 !_mesa_array_format_is_normalized(dst_array_format
))
480 is_signed
= _mesa_array_format_is_signed(dst_array_format
);
481 bits
= 8 * _mesa_array_format_get_type_size(dst_array_format
);
483 switch (_mesa_get_format_datatype(dst_format
)) {
484 case GL_UNSIGNED_NORMALIZED
:
487 case GL_SIGNED_NORMALIZED
:
493 case GL_UNSIGNED_INT
:
502 bits
= _mesa_get_format_max_bits(dst_format
);
505 assert(src_integer
== dst_integer
);
507 if (src_integer
&& dst_integer
) {
508 tmp_uint
= malloc(width
* height
* sizeof(*tmp_uint
));
510 /* The [un]packing functions for unsigned datatypes treat the 32-bit
511 * integer array as signed for signed formats and as unsigned for
512 * unsigned formats. This is a bit of a problem if we ever convert from
513 * a signed to an unsigned format because the unsigned packing function
514 * doesn't know that the input is signed and will treat it as unsigned
515 * and not do the trunctation. The thing that saves us here is that all
516 * of the packed formats are unsigned, so we can just always use
517 * _mesa_swizzle_and_convert for signed formats, which is aware of the
518 * truncation problem.
520 common_type
= is_signed
? MESA_ARRAY_FORMAT_TYPE_INT
:
521 MESA_ARRAY_FORMAT_TYPE_UINT
;
522 if (src_array_format
) {
523 compute_rebased_rgba_component_mapping(src2rgba
, rebase_swizzle
,
525 for (row
= 0; row
< height
; ++row
) {
526 _mesa_swizzle_and_convert(tmp_uint
+ row
* width
, common_type
, 4,
527 src
, src_type
, src_num_channels
,
528 rebased_src2rgba
, normalized
, width
);
532 for (row
= 0; row
< height
; ++row
) {
533 _mesa_unpack_uint_rgba_row(src_format
, width
,
534 src
, tmp_uint
+ row
* width
);
536 _mesa_swizzle_and_convert(tmp_uint
+ row
* width
, common_type
, 4,
537 tmp_uint
+ row
* width
, common_type
, 4,
538 rebase_swizzle
, false, width
);
543 /* At this point, we have already done the truncation if the source is
544 * signed but the destination is unsigned, so no need to force the
545 * _mesa_swizzle_and_convert path.
547 if (dst_format_is_mesa_array_format
) {
548 for (row
= 0; row
< height
; ++row
) {
549 _mesa_swizzle_and_convert(dst
, dst_type
, dst_num_channels
,
550 tmp_uint
+ row
* width
, common_type
, 4,
551 rgba2dst
, normalized
, width
);
555 for (row
= 0; row
< height
; ++row
) {
556 _mesa_pack_uint_rgba_row(dst_format
, width
,
557 (const uint32_t (*)[4])tmp_uint
+ row
* width
, dst
);
563 } else if (is_signed
|| bits
> 8) {
564 tmp_float
= malloc(width
* height
* sizeof(*tmp_float
));
566 if (src_format_is_mesa_array_format
) {
567 compute_rebased_rgba_component_mapping(src2rgba
, rebase_swizzle
,
569 for (row
= 0; row
< height
; ++row
) {
570 _mesa_swizzle_and_convert(tmp_float
+ row
* width
,
571 MESA_ARRAY_FORMAT_TYPE_FLOAT
, 4,
572 src
, src_type
, src_num_channels
,
573 rebased_src2rgba
, normalized
, width
);
577 for (row
= 0; row
< height
; ++row
) {
578 _mesa_unpack_rgba_row(src_format
, width
,
579 src
, tmp_float
+ row
* width
);
581 _mesa_swizzle_and_convert(tmp_float
+ row
* width
,
582 MESA_ARRAY_FORMAT_TYPE_FLOAT
, 4,
583 tmp_float
+ row
* width
,
584 MESA_ARRAY_FORMAT_TYPE_FLOAT
, 4,
585 rebase_swizzle
, normalized
, width
);
590 if (dst_format_is_mesa_array_format
) {
591 for (row
= 0; row
< height
; ++row
) {
592 _mesa_swizzle_and_convert(dst
, dst_type
, dst_num_channels
,
593 tmp_float
+ row
* width
,
594 MESA_ARRAY_FORMAT_TYPE_FLOAT
, 4,
595 rgba2dst
, normalized
, width
);
599 for (row
= 0; row
< height
; ++row
) {
600 _mesa_pack_float_rgba_row(dst_format
, width
,
601 (const float (*)[4])tmp_float
+ row
* width
, dst
);
608 tmp_ubyte
= malloc(width
* height
* sizeof(*tmp_ubyte
));
610 if (src_format_is_mesa_array_format
) {
611 compute_rebased_rgba_component_mapping(src2rgba
, rebase_swizzle
,
613 for (row
= 0; row
< height
; ++row
) {
614 _mesa_swizzle_and_convert(tmp_ubyte
+ row
* width
,
615 MESA_ARRAY_FORMAT_TYPE_UBYTE
, 4,
616 src
, src_type
, src_num_channels
,
617 rebased_src2rgba
, normalized
, width
);
621 for (row
= 0; row
< height
; ++row
) {
622 _mesa_unpack_ubyte_rgba_row(src_format
, width
,
623 src
, tmp_ubyte
+ row
* width
);
625 _mesa_swizzle_and_convert(tmp_ubyte
+ row
* width
,
626 MESA_ARRAY_FORMAT_TYPE_UBYTE
, 4,
627 tmp_ubyte
+ row
* width
,
628 MESA_ARRAY_FORMAT_TYPE_UBYTE
, 4,
629 rebase_swizzle
, normalized
, width
);
634 if (dst_format_is_mesa_array_format
) {
635 for (row
= 0; row
< height
; ++row
) {
636 _mesa_swizzle_and_convert(dst
, dst_type
, dst_num_channels
,
637 tmp_ubyte
+ row
* width
,
638 MESA_ARRAY_FORMAT_TYPE_UBYTE
, 4,
639 rgba2dst
, normalized
, width
);
643 for (row
= 0; row
< height
; ++row
) {
644 _mesa_pack_ubyte_rgba_row(dst_format
, width
,
645 (const uint8_t (*)[4])tmp_ubyte
+ row
* width
, dst
);
654 static const uint8_t map_identity
[7] = { 0, 1, 2, 3, 4, 5, 6 };
655 static const uint8_t map_3210
[7] = { 3, 2, 1, 0, 4, 5, 6 };
656 static const uint8_t map_1032
[7] = { 1, 0, 3, 2, 4, 5, 6 };
659 * Describes a format as an array format, if possible
661 * A helper function for figuring out if a (possibly packed) format is
662 * actually an array format and, if so, what the array parameters are.
664 * \param[in] format the mesa format
665 * \param[out] type the GL type of the array (GL_BYTE, etc.)
666 * \param[out] num_components the number of components in the array
667 * \param[out] swizzle a swizzle describing how to get from the
668 * given format to RGBA
669 * \param[out] normalized for integer formats, this represents whether
670 * the format is a normalized integer or a
672 * \return true if this format is an array format, false otherwise
675 _mesa_format_to_array(mesa_format format
, GLenum
*type
, int *num_components
,
676 uint8_t swizzle
[4], bool *normalized
)
679 GLuint format_components
;
680 uint8_t packed_swizzle
[4];
681 const uint8_t *endian
;
683 if (_mesa_is_format_compressed(format
))
686 *normalized
= !_mesa_is_format_integer(format
);
688 _mesa_uncompressed_format_to_type_and_comps(format
, type
, &format_components
);
690 switch (_mesa_get_format_layout(format
)) {
691 case MESA_FORMAT_LAYOUT_ARRAY
:
692 *num_components
= format_components
;
693 _mesa_get_format_swizzle(format
, swizzle
);
695 case MESA_FORMAT_LAYOUT_PACKED
:
697 case GL_UNSIGNED_BYTE
:
699 if (_mesa_get_format_max_bits(format
) != 8)
701 *num_components
= _mesa_get_format_bytes(format
);
702 switch (*num_components
) {
704 endian
= map_identity
;
707 endian
= _mesa_little_endian() ? map_identity
: map_1032
;
710 endian
= _mesa_little_endian() ? map_identity
: map_3210
;
713 endian
= map_identity
;
714 assert(!"Invalid number of components");
717 case GL_UNSIGNED_SHORT
:
720 if (_mesa_get_format_max_bits(format
) != 16)
722 *num_components
= _mesa_get_format_bytes(format
) / 2;
723 switch (*num_components
) {
725 endian
= map_identity
;
728 endian
= _mesa_little_endian() ? map_identity
: map_1032
;
731 endian
= map_identity
;
732 assert(!"Invalid number of components");
735 case GL_UNSIGNED_INT
:
738 /* This isn't packed. At least not really. */
739 assert(format_components
== 1);
740 if (_mesa_get_format_max_bits(format
) != 32)
742 *num_components
= format_components
;
743 endian
= map_identity
;
749 _mesa_get_format_swizzle(format
, packed_swizzle
);
751 for (i
= 0; i
< 4; ++i
)
752 swizzle
[i
] = endian
[packed_swizzle
[i
]];
755 case MESA_FORMAT_LAYOUT_OTHER
:
762 * Attempts to perform the given swizzle-and-convert operation with memcpy
764 * This function determines if the given swizzle-and-convert operation can
765 * be done with a simple memcpy and, if so, does the memcpy. If not, it
766 * returns false and we fall back to the standard version below.
768 * The arguments are exactly the same as for _mesa_swizzle_and_convert
770 * \return true if it successfully performed the swizzle-and-convert
771 * operation with memcpy, false otherwise
774 swizzle_convert_try_memcpy(void *dst
,
775 enum mesa_array_format_datatype dst_type
,
776 int num_dst_channels
,
778 enum mesa_array_format_datatype src_type
,
779 int num_src_channels
,
780 const uint8_t swizzle
[4], bool normalized
, int count
)
784 if (src_type
!= dst_type
)
786 if (num_src_channels
!= num_dst_channels
)
789 for (i
= 0; i
< num_dst_channels
; ++i
)
790 if (swizzle
[i
] != i
&& swizzle
[i
] != MESA_FORMAT_SWIZZLE_NONE
)
793 memcpy(dst
, src
, count
* num_src_channels
*
794 _mesa_array_format_datatype_get_size(src_type
));
800 * Represents a single instance of the standard swizzle-and-convert loop
802 * Any swizzle-and-convert operation simply loops through the pixels and
803 * performs the transformation operation one pixel at a time. This macro
804 * embodies one instance of the conversion loop. This way we can do all
805 * control flow outside of the loop and allow the compiler to unroll
806 * everything inside the loop.
808 * Note: This loop is carefully crafted for performance. Be careful when
809 * changing it and run some benchmarks to ensure no performance regressions
812 * \param DST_TYPE the C datatype of the destination
813 * \param DST_CHANS the number of destination channels
814 * \param SRC_TYPE the C datatype of the source
815 * \param SRC_CHANS the number of source channels
816 * \param CONV an expression for converting from the source data,
817 * storred in the variable "src", to the destination
820 #define SWIZZLE_CONVERT_LOOP(DST_TYPE, DST_CHANS, SRC_TYPE, SRC_CHANS, CONV) \
823 for (s = 0; s < count; ++s) { \
824 for (j = 0; j < SRC_CHANS; ++j) { \
825 SRC_TYPE src = typed_src[j]; \
829 typed_dst[0] = tmp[swizzle_x]; \
830 if (DST_CHANS > 1) { \
831 typed_dst[1] = tmp[swizzle_y]; \
832 if (DST_CHANS > 2) { \
833 typed_dst[2] = tmp[swizzle_z]; \
834 if (DST_CHANS > 3) { \
835 typed_dst[3] = tmp[swizzle_w]; \
839 typed_src += SRC_CHANS; \
840 typed_dst += DST_CHANS; \
845 * Represents a single swizzle-and-convert operation
847 * This macro represents everything done in a single swizzle-and-convert
848 * operation. The actual work is done by the SWIZZLE_CONVERT_LOOP macro.
849 * This macro acts as a wrapper that uses a nested switch to ensure that
850 * all looping parameters get unrolled.
852 * This macro makes assumptions about variables etc. in the calling
853 * function. Changes to _mesa_swizzle_and_convert may require changes to
856 * \param DST_TYPE the C datatype of the destination
857 * \param SRC_TYPE the C datatype of the source
858 * \param CONV an expression for converting from the source data,
859 * storred in the variable "src", to the destination
862 #define SWIZZLE_CONVERT(DST_TYPE, SRC_TYPE, CONV) \
864 const uint8_t swizzle_x = swizzle[0]; \
865 const uint8_t swizzle_y = swizzle[1]; \
866 const uint8_t swizzle_z = swizzle[2]; \
867 const uint8_t swizzle_w = swizzle[3]; \
868 const SRC_TYPE *typed_src = void_src; \
869 DST_TYPE *typed_dst = void_dst; \
873 switch (num_dst_channels) { \
875 switch (num_src_channels) { \
877 SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 1, CONV); \
880 SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 2, CONV); \
883 SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 3, CONV); \
886 SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 4, CONV); \
891 switch (num_src_channels) { \
893 SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 1, CONV); \
896 SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 2, CONV); \
899 SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 3, CONV); \
902 SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 4, CONV); \
907 switch (num_src_channels) { \
909 SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 1, CONV); \
912 SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 2, CONV); \
915 SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 3, CONV); \
918 SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 4, CONV); \
923 switch (num_src_channels) { \
925 SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 1, CONV); \
928 SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 2, CONV); \
931 SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 3, CONV); \
934 SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 4, CONV); \
943 convert_float(void *void_dst
, int num_dst_channels
,
944 const void *void_src
, GLenum src_type
, int num_src_channels
,
945 const uint8_t swizzle
[4], bool normalized
, int count
)
947 const float one
= 1.0f
;
950 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
951 SWIZZLE_CONVERT(float, float, src
);
953 case MESA_ARRAY_FORMAT_TYPE_HALF
:
954 SWIZZLE_CONVERT(float, uint16_t, _mesa_half_to_float(src
));
956 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
958 SWIZZLE_CONVERT(float, uint8_t, _mesa_unorm_to_float(src
, 8));
960 SWIZZLE_CONVERT(float, uint8_t, src
);
963 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
965 SWIZZLE_CONVERT(float, int8_t, _mesa_snorm_to_float(src
, 8));
967 SWIZZLE_CONVERT(float, int8_t, src
);
970 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
972 SWIZZLE_CONVERT(float, uint16_t, _mesa_unorm_to_float(src
, 16));
974 SWIZZLE_CONVERT(float, uint16_t, src
);
977 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
979 SWIZZLE_CONVERT(float, int16_t, _mesa_snorm_to_float(src
, 16));
981 SWIZZLE_CONVERT(float, int16_t, src
);
984 case MESA_ARRAY_FORMAT_TYPE_UINT
:
986 SWIZZLE_CONVERT(float, uint32_t, _mesa_unorm_to_float(src
, 32));
988 SWIZZLE_CONVERT(float, uint32_t, src
);
991 case MESA_ARRAY_FORMAT_TYPE_INT
:
993 SWIZZLE_CONVERT(float, int32_t, _mesa_snorm_to_float(src
, 32));
995 SWIZZLE_CONVERT(float, int32_t, src
);
999 assert(!"Invalid channel type combination");
1005 convert_half_float(void *void_dst
, int num_dst_channels
,
1006 const void *void_src
, GLenum src_type
, int num_src_channels
,
1007 const uint8_t swizzle
[4], bool normalized
, int count
)
1009 const uint16_t one
= _mesa_float_to_half(1.0f
);
1012 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1013 SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_half(src
));
1015 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1016 SWIZZLE_CONVERT(uint16_t, uint16_t, src
);
1018 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1020 SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_unorm_to_half(src
, 8));
1022 SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_float_to_half(src
));
1025 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1027 SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_snorm_to_half(src
, 8));
1029 SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_float_to_half(src
));
1032 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1034 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_unorm_to_half(src
, 16));
1036 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_float_to_half(src
));
1039 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1041 SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_snorm_to_half(src
, 16));
1043 SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_float_to_half(src
));
1046 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1048 SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unorm_to_half(src
, 32));
1050 SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_float_to_half(src
));
1053 case MESA_ARRAY_FORMAT_TYPE_INT
:
1055 SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_snorm_to_half(src
, 32));
1057 SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_float_to_half(src
));
1061 assert(!"Invalid channel type combination");
1066 convert_ubyte(void *void_dst
, int num_dst_channels
,
1067 const void *void_src
, GLenum src_type
, int num_src_channels
,
1068 const uint8_t swizzle
[4], bool normalized
, int count
)
1070 const uint8_t one
= normalized
? UINT8_MAX
: 1;
1073 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1075 SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_unorm(src
, 8));
1077 SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_unsigned(src
, 8));
1080 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1082 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_unorm(src
, 8));
1084 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_unsigned(src
, 8));
1087 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1088 SWIZZLE_CONVERT(uint8_t, uint8_t, src
);
1090 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1092 SWIZZLE_CONVERT(uint8_t, int8_t, _mesa_snorm_to_unorm(src
, 8, 8));
1094 SWIZZLE_CONVERT(uint8_t, int8_t, _mesa_signed_to_unsigned(src
, 8));
1097 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1099 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_unorm_to_unorm(src
, 16, 8));
1101 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_unsigned_to_unsigned(src
, 8));
1104 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1106 SWIZZLE_CONVERT(uint8_t, int16_t, _mesa_snorm_to_unorm(src
, 16, 8));
1108 SWIZZLE_CONVERT(uint8_t, int16_t, _mesa_signed_to_unsigned(src
, 8));
1111 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1113 SWIZZLE_CONVERT(uint8_t, uint32_t, _mesa_unorm_to_unorm(src
, 32, 8));
1115 SWIZZLE_CONVERT(uint8_t, uint32_t, _mesa_unsigned_to_unsigned(src
, 8));
1118 case MESA_ARRAY_FORMAT_TYPE_INT
:
1120 SWIZZLE_CONVERT(uint8_t, int32_t, _mesa_snorm_to_unorm(src
, 32, 8));
1122 SWIZZLE_CONVERT(uint8_t, int32_t, _mesa_signed_to_unsigned(src
, 8));
1126 assert(!"Invalid channel type combination");
1132 convert_byte(void *void_dst
, int num_dst_channels
,
1133 const void *void_src
, GLenum src_type
, int num_src_channels
,
1134 const uint8_t swizzle
[4], bool normalized
, int count
)
1136 const int8_t one
= normalized
? INT8_MAX
: 1;
1139 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1141 SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_snorm(src
, 8));
1143 SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_signed(src
, 8));
1146 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1148 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_snorm(src
, 8));
1150 SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_signed(src
, 8));
1153 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1155 SWIZZLE_CONVERT(int8_t, uint8_t, _mesa_unorm_to_snorm(src
, 8, 8));
1157 SWIZZLE_CONVERT(int8_t, uint8_t, _mesa_unsigned_to_signed(src
, 8));
1160 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1161 SWIZZLE_CONVERT(int8_t, int8_t, src
);
1163 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1165 SWIZZLE_CONVERT(int8_t, uint16_t, _mesa_unorm_to_snorm(src
, 16, 8));
1167 SWIZZLE_CONVERT(int8_t, uint16_t, _mesa_unsigned_to_signed(src
, 8));
1170 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1172 SWIZZLE_CONVERT(int8_t, int16_t, _mesa_snorm_to_snorm(src
, 16, 8));
1174 SWIZZLE_CONVERT(int8_t, int16_t, _mesa_signed_to_signed(src
, 8));
1177 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1179 SWIZZLE_CONVERT(int8_t, uint32_t, _mesa_unorm_to_snorm(src
, 32, 8));
1181 SWIZZLE_CONVERT(int8_t, uint32_t, _mesa_unsigned_to_signed(src
, 8));
1184 case MESA_ARRAY_FORMAT_TYPE_INT
:
1186 SWIZZLE_CONVERT(int8_t, int32_t, _mesa_snorm_to_snorm(src
, 32, 8));
1188 SWIZZLE_CONVERT(int8_t, int32_t, _mesa_signed_to_signed(src
, 8));
1192 assert(!"Invalid channel type combination");
1198 convert_ushort(void *void_dst
, int num_dst_channels
,
1199 const void *void_src
, GLenum src_type
, int num_src_channels
,
1200 const uint8_t swizzle
[4], bool normalized
, int count
)
1202 const uint16_t one
= normalized
? UINT16_MAX
: 1;
1205 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1207 SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_unorm(src
, 16));
1209 SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_unsigned(src
, 16));
1212 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1214 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_unorm(src
, 16));
1216 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_unsigned(src
, 16));
1219 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1221 SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_unorm_to_unorm(src
, 8, 16));
1223 SWIZZLE_CONVERT(uint16_t, uint8_t, src
);
1226 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1228 SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_snorm_to_unorm(src
, 8, 16));
1230 SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_signed_to_unsigned(src
, 16));
1233 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1234 SWIZZLE_CONVERT(uint16_t, uint16_t, src
);
1236 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1238 SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_snorm_to_unorm(src
, 16, 16));
1240 SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_signed_to_unsigned(src
, 16));
1243 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1245 SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unorm_to_unorm(src
, 32, 16));
1247 SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unsigned_to_unsigned(src
, 16));
1250 case MESA_ARRAY_FORMAT_TYPE_INT
:
1252 SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_snorm_to_unorm(src
, 32, 16));
1254 SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_signed_to_unsigned(src
, 16));
1258 assert(!"Invalid channel type combination");
1264 convert_short(void *void_dst
, int num_dst_channels
,
1265 const void *void_src
, GLenum src_type
, int num_src_channels
,
1266 const uint8_t swizzle
[4], bool normalized
, int count
)
1268 const int16_t one
= normalized
? INT16_MAX
: 1;
1271 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1273 SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_snorm(src
, 16));
1275 SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_signed(src
, 16));
1278 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1280 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_snorm(src
, 16));
1282 SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_signed(src
, 16));
1285 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1287 SWIZZLE_CONVERT(int16_t, uint8_t, _mesa_unorm_to_snorm(src
, 8, 16));
1289 SWIZZLE_CONVERT(int16_t, uint8_t, src
);
1292 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1294 SWIZZLE_CONVERT(int16_t, int8_t, _mesa_snorm_to_snorm(src
, 8, 16));
1296 SWIZZLE_CONVERT(int16_t, int8_t, src
);
1299 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1301 SWIZZLE_CONVERT(int16_t, uint16_t, _mesa_unorm_to_snorm(src
, 16, 16));
1303 SWIZZLE_CONVERT(int16_t, uint16_t, _mesa_unsigned_to_signed(src
, 16));
1306 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1307 SWIZZLE_CONVERT(int16_t, int16_t, src
);
1309 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1311 SWIZZLE_CONVERT(int16_t, uint32_t, _mesa_unorm_to_snorm(src
, 32, 16));
1313 SWIZZLE_CONVERT(int16_t, uint32_t, _mesa_unsigned_to_signed(src
, 16));
1316 case MESA_ARRAY_FORMAT_TYPE_INT
:
1318 SWIZZLE_CONVERT(int16_t, int32_t, _mesa_snorm_to_snorm(src
, 32, 16));
1320 SWIZZLE_CONVERT(int16_t, int32_t, _mesa_signed_to_signed(src
, 16));
1324 assert(!"Invalid channel type combination");
1329 convert_uint(void *void_dst
, int num_dst_channels
,
1330 const void *void_src
, GLenum src_type
, int num_src_channels
,
1331 const uint8_t swizzle
[4], bool normalized
, int count
)
1333 const uint32_t one
= normalized
? UINT32_MAX
: 1;
1336 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1338 SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_unorm(src
, 32));
1340 SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_unsigned(src
, 32));
1343 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1345 SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_unorm(src
, 32));
1347 SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_unsigned(src
, 32));
1350 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1352 SWIZZLE_CONVERT(uint32_t, uint8_t, _mesa_unorm_to_unorm(src
, 8, 32));
1354 SWIZZLE_CONVERT(uint32_t, uint8_t, src
);
1357 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1359 SWIZZLE_CONVERT(uint32_t, int8_t, _mesa_snorm_to_unorm(src
, 8, 32));
1361 SWIZZLE_CONVERT(uint32_t, int8_t, _mesa_signed_to_unsigned(src
, 32));
1364 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1366 SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_unorm_to_unorm(src
, 16, 32));
1368 SWIZZLE_CONVERT(uint32_t, uint16_t, src
);
1371 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1373 SWIZZLE_CONVERT(uint32_t, int16_t, _mesa_snorm_to_unorm(src
, 16, 32));
1375 SWIZZLE_CONVERT(uint32_t, int16_t, _mesa_signed_to_unsigned(src
, 32));
1378 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1379 SWIZZLE_CONVERT(uint32_t, uint32_t, src
);
1381 case MESA_ARRAY_FORMAT_TYPE_INT
:
1383 SWIZZLE_CONVERT(uint32_t, int32_t, _mesa_snorm_to_unorm(src
, 32, 32));
1385 SWIZZLE_CONVERT(uint32_t, int32_t, _mesa_signed_to_unsigned(src
, 32));
1389 assert(!"Invalid channel type combination");
1395 convert_int(void *void_dst
, int num_dst_channels
,
1396 const void *void_src
, GLenum src_type
, int num_src_channels
,
1397 const uint8_t swizzle
[4], bool normalized
, int count
)
1399 const int32_t one
= normalized
? INT32_MAX
: 1;
1402 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1404 SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_snorm(src
, 32));
1406 SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_signed(src
, 32));
1409 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1411 SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_snorm(src
, 32));
1413 SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_signed(src
, 32));
1416 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1418 SWIZZLE_CONVERT(int32_t, uint8_t, _mesa_unorm_to_snorm(src
, 8, 32));
1420 SWIZZLE_CONVERT(int32_t, uint8_t, src
);
1423 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1425 SWIZZLE_CONVERT(int32_t, int8_t, _mesa_snorm_to_snorm(src
, 8, 32));
1427 SWIZZLE_CONVERT(int32_t, int8_t, src
);
1430 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1432 SWIZZLE_CONVERT(int32_t, uint16_t, _mesa_unorm_to_snorm(src
, 16, 32));
1434 SWIZZLE_CONVERT(int32_t, uint16_t, src
);
1437 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1439 SWIZZLE_CONVERT(int32_t, int16_t, _mesa_snorm_to_snorm(src
, 16, 32));
1441 SWIZZLE_CONVERT(int32_t, int16_t, src
);
1444 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1446 SWIZZLE_CONVERT(int32_t, uint32_t, _mesa_unorm_to_snorm(src
, 32, 32));
1448 SWIZZLE_CONVERT(int32_t, uint32_t, _mesa_unsigned_to_signed(src
, 32));
1451 case MESA_ARRAY_FORMAT_TYPE_INT
:
1452 SWIZZLE_CONVERT(int32_t, int32_t, src
);
1455 assert(!"Invalid channel type combination");
1461 * Convert between array-based color formats.
1463 * Most format conversion operations required by GL can be performed by
1464 * converting one channel at a time, shuffling the channels around, and
1465 * optionally filling missing channels with zeros and ones. This function
1466 * does just that in a general, yet efficient, way.
1468 * The swizzle parameter is an array of 4 numbers (see
1469 * _mesa_get_format_swizzle) that describes where each channel in the
1470 * destination should come from in the source. If swizzle[i] < 4 then it
1471 * means that dst[i] = CONVERT(src[swizzle[i]]). If swizzle[i] is
1472 * MESA_FORMAT_SWIZZLE_ZERO or MESA_FORMAT_SWIZZLE_ONE, the corresponding
1473 * dst[i] will be filled with the appropreate representation of zero or one
1476 * Under most circumstances, the source and destination images must be
1477 * different as no care is taken not to clobber one with the other.
1478 * However, if they have the same number of bits per pixel, it is safe to
1479 * do an in-place conversion.
1481 * \param[out] dst pointer to where the converted data should
1484 * \param[in] dst_type the destination GL type of the converted
1485 * data (GL_BYTE, etc.)
1487 * \param[in] num_dst_channels the number of channels in the converted
1490 * \param[in] src pointer to the source data
1492 * \param[in] src_type the GL type of the source data (GL_BYTE,
1495 * \param[in] num_src_channels the number of channels in the source data
1496 * (the number of channels total, not just
1499 * \param[in] swizzle describes how to get the destination data
1500 * from the source data.
1502 * \param[in] normalized for integer types, this indicates whether
1503 * the data should be considered as integers
1504 * or as normalized integers;
1506 * \param[in] count the number of pixels to convert
1509 _mesa_swizzle_and_convert(void *void_dst
, enum mesa_array_format_datatype dst_type
, int num_dst_channels
,
1510 const void *void_src
, enum mesa_array_format_datatype src_type
, int num_src_channels
,
1511 const uint8_t swizzle
[4], bool normalized
, int count
)
1513 if (swizzle_convert_try_memcpy(void_dst
, dst_type
, num_dst_channels
,
1514 void_src
, src_type
, num_src_channels
,
1515 swizzle
, normalized
, count
))
1519 case MESA_ARRAY_FORMAT_TYPE_FLOAT
:
1520 convert_float(void_dst
, num_dst_channels
, void_src
, src_type
,
1521 num_src_channels
, swizzle
, normalized
, count
);
1523 case MESA_ARRAY_FORMAT_TYPE_HALF
:
1524 convert_half_float(void_dst
, num_dst_channels
, void_src
, src_type
,
1525 num_src_channels
, swizzle
, normalized
, count
);
1527 case MESA_ARRAY_FORMAT_TYPE_UBYTE
:
1528 convert_ubyte(void_dst
, num_dst_channels
, void_src
, src_type
,
1529 num_src_channels
, swizzle
, normalized
, count
);
1531 case MESA_ARRAY_FORMAT_TYPE_BYTE
:
1532 convert_byte(void_dst
, num_dst_channels
, void_src
, src_type
,
1533 num_src_channels
, swizzle
, normalized
, count
);
1535 case MESA_ARRAY_FORMAT_TYPE_USHORT
:
1536 convert_ushort(void_dst
, num_dst_channels
, void_src
, src_type
,
1537 num_src_channels
, swizzle
, normalized
, count
);
1539 case MESA_ARRAY_FORMAT_TYPE_SHORT
:
1540 convert_short(void_dst
, num_dst_channels
, void_src
, src_type
,
1541 num_src_channels
, swizzle
, normalized
, count
);
1543 case MESA_ARRAY_FORMAT_TYPE_UINT
:
1544 convert_uint(void_dst
, num_dst_channels
, void_src
, src_type
,
1545 num_src_channels
, swizzle
, normalized
, count
);
1547 case MESA_ARRAY_FORMAT_TYPE_INT
:
1548 convert_int(void_dst
, num_dst_channels
, void_src
, src_type
,
1549 num_src_channels
, swizzle
, normalized
, count
);
1552 assert(!"Invalid channel type");