xref: /aosp_15_r20/external/mesa3d/src/mesa/main/format_utils.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 <stdlib.h>
26 
27 #include "errors.h"
28 #include "format_utils.h"
29 #include "glformats.h"
30 #include "format_pack.h"
31 #include "format_unpack.h"
32 
33 const mesa_array_format RGBA32_FLOAT =
34    MESA_ARRAY_FORMAT(MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS,
35                      4, 1, 1, 1, 4, 0, 1, 2, 3);
36 
37 const mesa_array_format RGBA8_UBYTE =
38    MESA_ARRAY_FORMAT(MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS,
39                      1, 0, 0, 1, 4, 0, 1, 2, 3);
40 
41 const mesa_array_format BGRA8_UBYTE =
42    MESA_ARRAY_FORMAT(MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS,
43                      1, 0, 0, 1, 4, 2, 1, 0, 3);
44 
45 const mesa_array_format RGBA32_UINT =
46    MESA_ARRAY_FORMAT(MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS,
47                      4, 0, 0, 0, 4, 0, 1, 2, 3);
48 
49 const mesa_array_format RGBA32_INT =
50    MESA_ARRAY_FORMAT(MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS,
51                      4, 1, 0, 0, 4, 0, 1, 2, 3);
52 
53 static void
invert_swizzle(uint8_t dst[4],const uint8_t src[4])54 invert_swizzle(uint8_t dst[4], const uint8_t src[4])
55 {
56    int i, j;
57 
58    dst[0] = MESA_FORMAT_SWIZZLE_NONE;
59    dst[1] = MESA_FORMAT_SWIZZLE_NONE;
60    dst[2] = MESA_FORMAT_SWIZZLE_NONE;
61    dst[3] = MESA_FORMAT_SWIZZLE_NONE;
62 
63    for (i = 0; i < 4; ++i)
64       for (j = 0; j < 4; ++j)
65          if (src[j] == i && dst[i] == MESA_FORMAT_SWIZZLE_NONE)
66             dst[i] = j;
67 }
68 
69 /* Takes a src to RGBA swizzle and applies a rebase swizzle to it. This
70  * is used when we need to rebase a format to match a different
71  * base internal format.
72  *
73  * The rebase swizzle can be NULL, which means that no rebase is necessary,
74  * in which case the src to RGBA swizzle is copied to the output without
75  * changes.
76  *
77  * The resulting rebased swizzle and well as the input swizzles are
78  * all 4-element swizzles, but the rebase swizzle can be NULL if no rebase
79  * is necessary.
80  */
81 static void
compute_rebased_rgba_component_mapping(uint8_t * src2rgba,uint8_t * rebase_swizzle,uint8_t * rebased_src2rgba)82 compute_rebased_rgba_component_mapping(uint8_t *src2rgba,
83                                        uint8_t *rebase_swizzle,
84                                        uint8_t *rebased_src2rgba)
85 {
86    int i;
87 
88    if (rebase_swizzle) {
89       for (i = 0; i < 4; i++) {
90          if (rebase_swizzle[i] > MESA_FORMAT_SWIZZLE_W)
91             rebased_src2rgba[i] = rebase_swizzle[i];
92          else
93             rebased_src2rgba[i] = src2rgba[rebase_swizzle[i]];
94       }
95    } else {
96       /* No rebase needed, so src2rgba is all that we need */
97       memcpy(rebased_src2rgba, src2rgba, 4 * sizeof(uint8_t));
98    }
99 }
100 
101 /* Computes the final swizzle transform to apply from src to dst in a
102  * conversion that might involve a rebase swizzle.
103  *
104  * This is used to compute the swizzle transform to apply in conversions
105  * between array formats where we have a src2rgba swizzle, a rgba2dst swizzle
106  * and possibly, a rebase swizzle.
107  *
108  * The final swizzle transform to apply (src2dst) when a rebase swizzle is
109  * involved is: src -> rgba -> base -> rgba -> dst
110  */
111 static void
compute_src2dst_component_mapping(uint8_t * src2rgba,uint8_t * rgba2dst,uint8_t * rebase_swizzle,uint8_t * src2dst)112 compute_src2dst_component_mapping(uint8_t *src2rgba, uint8_t *rgba2dst,
113                                   uint8_t *rebase_swizzle, uint8_t *src2dst)
114 {
115    int i;
116 
117    if (!rebase_swizzle) {
118       for (i = 0; i < 4; i++) {
119          if (rgba2dst[i] > MESA_FORMAT_SWIZZLE_W) {
120             src2dst[i] = rgba2dst[i];
121          } else {
122             src2dst[i] = src2rgba[rgba2dst[i]];
123          }
124       }
125    } else {
126       for (i = 0; i < 4; i++) {
127          if (rgba2dst[i] > MESA_FORMAT_SWIZZLE_W) {
128             src2dst[i] = rgba2dst[i];
129          } else if (rebase_swizzle[rgba2dst[i]] > MESA_FORMAT_SWIZZLE_W) {
130             src2dst[i] = rebase_swizzle[rgba2dst[i]];
131          } else {
132             src2dst[i] = src2rgba[rebase_swizzle[rgba2dst[i]]];
133          }
134       }
135    }
136 }
137 
138 /**
139  * This function is used by clients of _mesa_format_convert to obtain
140  * the rebase swizzle to use in a format conversion based on the base
141  * format involved.
142  *
143  * \param baseFormat  the base internal format involved in the conversion.
144  * \param map  the rebase swizzle to consider
145  *
146  * This function computes 'map' as rgba -> baseformat -> rgba and returns true
147  * if the resulting swizzle transform is not the identity transform (thus, a
148  * rebase is needed). If the function returns false then a rebase swizzle
149  * is not necessary and the value of 'map' is undefined. In this situation
150  * clients of _mesa_format_convert should pass NULL in the 'rebase_swizzle'
151  * parameter.
152  */
153 bool
_mesa_compute_rgba2base2rgba_component_mapping(GLenum baseFormat,uint8_t * map)154 _mesa_compute_rgba2base2rgba_component_mapping(GLenum baseFormat, uint8_t *map)
155 {
156    uint8_t rgba2base[6], base2rgba[6];
157    int i;
158 
159    switch (baseFormat) {
160    case GL_ALPHA:
161    case GL_RED:
162    case GL_GREEN:
163    case GL_BLUE:
164    case GL_RG:
165    case GL_RGB:
166    case GL_BGR:
167    case GL_RGBA:
168    case GL_BGRA:
169    case GL_ABGR_EXT:
170    case GL_LUMINANCE:
171    case GL_INTENSITY:
172    case GL_LUMINANCE_ALPHA:
173       {
174          bool needRebase = false;
175          _mesa_compute_component_mapping(GL_RGBA, baseFormat, rgba2base);
176          _mesa_compute_component_mapping(baseFormat, GL_RGBA, base2rgba);
177          for (i = 0; i < 4; i++) {
178             if (base2rgba[i] > MESA_FORMAT_SWIZZLE_W) {
179                map[i] = base2rgba[i];
180             } else {
181                map[i] = rgba2base[base2rgba[i]];
182             }
183             if (map[i] != i)
184                needRebase = true;
185          }
186          return needRebase;
187       }
188    default:
189       unreachable("Unexpected base format");
190    }
191 }
192 
193 
194 /**
195  * Special case conversion function to swap r/b channels from the source
196  * image to the dest image.
197  */
198 static void
convert_ubyte_rgba_to_bgra(size_t width,size_t height,const uint8_t * src,size_t src_stride,uint8_t * dst,size_t dst_stride)199 convert_ubyte_rgba_to_bgra(size_t width, size_t height,
200                            const uint8_t *src, size_t src_stride,
201                            uint8_t *dst, size_t dst_stride)
202 {
203    int row;
204 
205    if (sizeof(void *) == 8 &&
206        src_stride % 8 == 0 &&
207        dst_stride % 8 == 0 &&
208        (GLsizeiptr) src % 8 == 0 &&
209        (GLsizeiptr) dst % 8 == 0) {
210       /* use 64-bit word to swizzle two 32-bit pixels.  We need 8-byte
211        * alignment for src/dst addresses and strides.
212        */
213       for (row = 0; row < height; row++) {
214          const GLuint64 *s = (const GLuint64 *) src;
215          GLuint64 *d = (GLuint64 *) dst;
216          int i;
217          for (i = 0; i < width/2; i++) {
218             d[i] = ( (s[i] & 0xff00ff00ff00ff00) |
219                     ((s[i] &       0xff000000ff) << 16) |
220                     ((s[i] &   0xff000000ff0000) >> 16));
221          }
222          if (width & 1) {
223             /* handle the case of odd widths */
224             const GLuint s = ((const GLuint *) src)[width - 1];
225             GLuint *d = (GLuint *) dst + width - 1;
226             *d = ( (s & 0xff00ff00) |
227                   ((s &       0xff) << 16) |
228                   ((s &   0xff0000) >> 16));
229          }
230          src += src_stride;
231          dst += dst_stride;
232       }
233    } else {
234       for (row = 0; row < height; row++) {
235          const GLuint *s = (const GLuint *) src;
236          GLuint *d = (GLuint *) dst;
237          int i;
238          for (i = 0; i < width; i++) {
239             d[i] = ( (s[i] & 0xff00ff00) |
240                     ((s[i] &       0xff) << 16) |
241                     ((s[i] &   0xff0000) >> 16));
242          }
243          src += src_stride;
244          dst += dst_stride;
245       }
246    }
247 }
248 
249 
250 /**
251  * This can be used to convert between most color formats.
252  *
253  * Limitations:
254  * - This function doesn't handle GL_COLOR_INDEX or YCBCR formats.
255  * - This function doesn't handle byte-swapping or transferOps, these should
256  *   be handled by the caller.
257  *
258  * \param void_dst  The address where converted color data will be stored.
259  *                  The caller must ensure that the buffer is large enough
260  *                  to hold the converted pixel data.
261  * \param dst_format  The destination color format. It can be a mesa_format
262  *                    or a mesa_array_format represented as an uint32_t.
263  * \param dst_stride  The stride of the destination format in bytes.
264  * \param void_src  The address of the source color data to convert.
265  * \param src_format  The source color format. It can be a mesa_format
266  *                    or a mesa_array_format represented as an uint32_t.
267  * \param src_stride  The stride of the source format in bytes.
268  * \param width  The width, in pixels, of the source image to convert.
269  * \param height  The height, in pixels, of the source image to convert.
270  * \param rebase_swizzle  A swizzle transform to apply during the conversion,
271  *                        typically used to match a different internal base
272  *                        format involved. NULL if no rebase transform is needed
273  *                        (i.e. the internal base format and the base format of
274  *                        the dst or the src -depending on whether we are doing
275  *                        an upload or a download respectively- are the same).
276  */
277 void
_mesa_format_convert(void * void_dst,uint32_t dst_format,size_t dst_stride,void * void_src,uint32_t src_format,size_t src_stride,size_t width,size_t height,uint8_t * rebase_swizzle)278 _mesa_format_convert(void *void_dst, uint32_t dst_format, size_t dst_stride,
279                      void *void_src, uint32_t src_format, size_t src_stride,
280                      size_t width, size_t height, uint8_t *rebase_swizzle)
281 {
282    uint8_t *dst = (uint8_t *)void_dst;
283    uint8_t *src = (uint8_t *)void_src;
284    mesa_array_format src_array_format, dst_array_format;
285    bool src_format_is_mesa_array_format, dst_format_is_mesa_array_format;
286    uint8_t src2dst[4], src2rgba[4], rgba2dst[4], dst2rgba[4];
287    uint8_t rebased_src2rgba[4];
288    enum mesa_array_format_datatype src_type = 0, dst_type = 0, common_type;
289    bool normalized, dst_integer, src_integer, is_signed;
290    int src_num_channels = 0, dst_num_channels = 0;
291    uint8_t (*tmp_ubyte)[4];
292    float (*tmp_float)[4];
293    uint32_t (*tmp_uint)[4];
294    int bits;
295    size_t row;
296 
297    if (_mesa_format_is_mesa_array_format(src_format)) {
298       src_format_is_mesa_array_format = true;
299       src_array_format = src_format;
300    } else {
301       assert(_mesa_is_format_color_format(src_format));
302       src_format_is_mesa_array_format = false;
303       src_array_format = _mesa_format_to_array_format(src_format);
304    }
305 
306    if (_mesa_format_is_mesa_array_format(dst_format)) {
307       dst_format_is_mesa_array_format = true;
308       dst_array_format = dst_format;
309    } else {
310       assert(_mesa_is_format_color_format(dst_format));
311       dst_format_is_mesa_array_format = false;
312       dst_array_format = _mesa_format_to_array_format(dst_format);
313    }
314 
315    /* First we see if we can implement the conversion with a direct pack
316     * or unpack.
317     *
318     * In this case we want to be careful when we need to apply a swizzle to
319     * match an internal base format, since in these cases a simple pack/unpack
320     * to the dst format from the src format may not match the requirements
321     * of the internal base format. For now we decide to be safe and
322     * avoid this path in these scenarios but in the future we may want to
323     * enable it for specific combinations that are known to work.
324     */
325    if (!rebase_swizzle) {
326       /* Do a direct memcpy where possible */
327       if ((dst_format_is_mesa_array_format &&
328            src_format_is_mesa_array_format &&
329            src_array_format == dst_array_format) ||
330           src_format == dst_format) {
331          int format_size = _mesa_get_format_bytes(src_format);
332          for (row = 0; row < height; row++) {
333             memcpy(dst, src, width * format_size);
334             src += src_stride;
335             dst += dst_stride;
336          }
337          return;
338       }
339 
340       /* Handle the cases where we can directly unpack */
341       if (!src_format_is_mesa_array_format) {
342          if (dst_array_format == RGBA32_FLOAT) {
343             for (row = 0; row < height; ++row) {
344                _mesa_unpack_rgba_row(src_format, width,
345                                      src, (float (*)[4])dst);
346                src += src_stride;
347                dst += dst_stride;
348             }
349             return;
350          } else if (dst_array_format == RGBA8_UBYTE) {
351             assert(!_mesa_is_format_integer_color(src_format));
352             for (row = 0; row < height; ++row) {
353                _mesa_unpack_ubyte_rgba_row(src_format, width,
354                                            src, (uint8_t (*)[4])dst);
355                src += src_stride;
356                dst += dst_stride;
357             }
358             return;
359 #if UTIL_ARCH_LITTLE_ENDIAN
360          } else if (dst_array_format == BGRA8_UBYTE &&
361                     src_format == MESA_FORMAT_R8G8B8A8_UNORM) {
362              convert_ubyte_rgba_to_bgra(width, height, src, src_stride,
363                                         dst, dst_stride);
364              return;
365 #endif
366          } else if (dst_array_format == RGBA32_UINT &&
367                     _mesa_is_format_unsigned(src_format)) {
368             assert(_mesa_is_format_integer_color(src_format));
369             for (row = 0; row < height; ++row) {
370                _mesa_unpack_uint_rgba_row(src_format, width,
371                                           src, (uint32_t (*)[4])dst);
372                src += src_stride;
373                dst += dst_stride;
374             }
375             return;
376          }
377       }
378 
379       /* Handle the cases where we can directly pack */
380       if (!dst_format_is_mesa_array_format) {
381          if (src_array_format == RGBA32_FLOAT) {
382             for (row = 0; row < height; ++row) {
383                _mesa_pack_float_rgba_row(dst_format, width,
384                                          (const float (*)[4])src, dst);
385                src += src_stride;
386                dst += dst_stride;
387             }
388             return;
389          } else if (src_array_format == RGBA8_UBYTE) {
390             assert(!_mesa_is_format_integer_color(dst_format));
391 
392 #if UTIL_ARCH_LITTLE_ENDIAN
393             if (dst_format == MESA_FORMAT_B8G8R8A8_UNORM) {
394                convert_ubyte_rgba_to_bgra(width, height, src, src_stride,
395                                           dst, dst_stride);
396             }
397             else
398 #endif
399             {
400                for (row = 0; row < height; ++row) {
401                   _mesa_pack_ubyte_rgba_row(dst_format, width, src, dst);
402                   src += src_stride;
403                   dst += dst_stride;
404                }
405             }
406             return;
407          } else if (src_array_format == RGBA32_UINT &&
408                     _mesa_is_format_unsigned(dst_format)) {
409             assert(_mesa_is_format_integer_color(dst_format));
410             for (row = 0; row < height; ++row) {
411                _mesa_pack_uint_rgba_row(dst_format, width,
412                                         (const uint32_t (*)[4])src, dst);
413                src += src_stride;
414                dst += dst_stride;
415             }
416             return;
417          }
418       }
419    }
420 
421    /* Handle conversions between array formats */
422    normalized = false;
423    if (src_array_format) {
424       src_type = _mesa_array_format_get_datatype(src_array_format);
425 
426       src_num_channels = _mesa_array_format_get_num_channels(src_array_format);
427 
428       _mesa_array_format_get_swizzle(src_array_format, src2rgba);
429 
430       normalized = _mesa_array_format_is_normalized(src_array_format);
431    }
432 
433    if (dst_array_format) {
434       dst_type = _mesa_array_format_get_datatype(dst_array_format);
435 
436       dst_num_channels = _mesa_array_format_get_num_channels(dst_array_format);
437 
438       _mesa_array_format_get_swizzle(dst_array_format, dst2rgba);
439       invert_swizzle(rgba2dst, dst2rgba);
440 
441       normalized |= _mesa_array_format_is_normalized(dst_array_format);
442    }
443 
444    if (src_array_format && dst_array_format) {
445       assert(_mesa_array_format_is_normalized(src_array_format) ==
446              _mesa_array_format_is_normalized(dst_array_format));
447 
448       compute_src2dst_component_mapping(src2rgba, rgba2dst, rebase_swizzle,
449                                         src2dst);
450 
451       for (row = 0; row < height; ++row) {
452          _mesa_swizzle_and_convert(dst, dst_type, dst_num_channels,
453                                    src, src_type, src_num_channels,
454                                    src2dst, normalized, width);
455          src += src_stride;
456          dst += dst_stride;
457       }
458       return;
459    }
460 
461    /* At this point, we're fresh out of fast-paths and we need to convert
462     * to float, uint32, or, if we're lucky, uint8.
463     */
464    dst_integer = false;
465    src_integer = false;
466 
467    if (src_array_format) {
468       if (!_mesa_array_format_is_float(src_array_format) &&
469           !_mesa_array_format_is_normalized(src_array_format))
470          src_integer = true;
471    } else {
472       switch (_mesa_get_format_datatype(src_format)) {
473       case GL_UNSIGNED_INT:
474       case GL_INT:
475          src_integer = true;
476          break;
477       }
478    }
479 
480    /* If the destination format is signed but the source is unsigned, then we
481     * don't loose any data by converting to a signed intermediate format above
482     * and beyond the precision that we loose in the conversion itself. If the
483     * destination is unsigned then, by using an unsigned intermediate format,
484     * we make the conversion function that converts from the source to the
485     * intermediate format take care of truncating at zero. The exception here
486     * is if the intermediate format is float, in which case the first
487     * conversion will leave it signed and the second conversion will truncate
488     * at zero.
489     */
490    is_signed = false;
491    if (dst_array_format) {
492       if (!_mesa_array_format_is_float(dst_array_format) &&
493           !_mesa_array_format_is_normalized(dst_array_format))
494          dst_integer = true;
495       is_signed = _mesa_array_format_is_signed(dst_array_format);
496       bits = 8 * _mesa_array_format_get_type_size(dst_array_format);
497    } else {
498       switch (_mesa_get_format_datatype(dst_format)) {
499       case GL_UNSIGNED_NORMALIZED:
500          is_signed = false;
501          break;
502       case GL_SIGNED_NORMALIZED:
503          is_signed = true;
504          break;
505       case GL_FLOAT:
506          is_signed = true;
507          break;
508       case GL_UNSIGNED_INT:
509          is_signed = false;
510          dst_integer = true;
511          break;
512       case GL_INT:
513          is_signed = true;
514          dst_integer = true;
515          break;
516       }
517       bits = _mesa_get_format_max_bits(dst_format);
518    }
519 
520    assert(src_integer == dst_integer);
521 
522    if (src_integer && dst_integer) {
523       tmp_uint = malloc(width * height * sizeof(*tmp_uint));
524 
525       /* The [un]packing functions for unsigned datatypes treat the 32-bit
526        * integer array as signed for signed formats and as unsigned for
527        * unsigned formats. This is a bit of a problem if we ever convert from
528        * a signed to an unsigned format because the unsigned packing function
529        * doesn't know that the input is signed and will treat it as unsigned
530        * and not do the trunctation. The thing that saves us here is that all
531        * of the packed formats are unsigned, so we can just always use
532        * _mesa_swizzle_and_convert for signed formats, which is aware of the
533        * truncation problem.
534        */
535       common_type = is_signed ? MESA_ARRAY_FORMAT_TYPE_INT :
536                                 MESA_ARRAY_FORMAT_TYPE_UINT;
537       if (src_array_format) {
538          compute_rebased_rgba_component_mapping(src2rgba, rebase_swizzle,
539                                                 rebased_src2rgba);
540          for (row = 0; row < height; ++row) {
541             _mesa_swizzle_and_convert(tmp_uint + row * width, common_type, 4,
542                                       src, src_type, src_num_channels,
543                                       rebased_src2rgba, normalized, width);
544             src += src_stride;
545          }
546       } else {
547          for (row = 0; row < height; ++row) {
548             _mesa_unpack_uint_rgba_row(src_format, width,
549                                        src, tmp_uint + row * width);
550             if (rebase_swizzle)
551                _mesa_swizzle_and_convert(tmp_uint + row * width, common_type, 4,
552                                          tmp_uint + row * width, common_type, 4,
553                                          rebase_swizzle, false, width);
554             src += src_stride;
555          }
556       }
557 
558       /* At this point, we have already done the truncation if the source is
559        * signed but the destination is unsigned, so no need to force the
560        * _mesa_swizzle_and_convert path.
561        */
562       if (dst_format_is_mesa_array_format) {
563          for (row = 0; row < height; ++row) {
564             _mesa_swizzle_and_convert(dst, dst_type, dst_num_channels,
565                                       tmp_uint + row * width, common_type, 4,
566                                       rgba2dst, normalized, width);
567             dst += dst_stride;
568          }
569       } else {
570          for (row = 0; row < height; ++row) {
571             _mesa_pack_uint_rgba_row(dst_format, width,
572                                      (const uint32_t (*)[4])tmp_uint + row * width, dst);
573             dst += dst_stride;
574          }
575       }
576 
577       free(tmp_uint);
578    } else if (is_signed || bits > 8) {
579       tmp_float = malloc(width * height * sizeof(*tmp_float));
580 
581       if (src_format_is_mesa_array_format) {
582          compute_rebased_rgba_component_mapping(src2rgba, rebase_swizzle,
583                                                 rebased_src2rgba);
584          for (row = 0; row < height; ++row) {
585             _mesa_swizzle_and_convert(tmp_float + row * width,
586                                       MESA_ARRAY_FORMAT_TYPE_FLOAT, 4,
587                                       src, src_type, src_num_channels,
588                                       rebased_src2rgba, normalized, width);
589             src += src_stride;
590          }
591       } else {
592          for (row = 0; row < height; ++row) {
593             _mesa_unpack_rgba_row(src_format, width,
594                                   src, tmp_float + row * width);
595             if (rebase_swizzle)
596                _mesa_swizzle_and_convert(tmp_float + row * width,
597                                          MESA_ARRAY_FORMAT_TYPE_FLOAT, 4,
598                                          tmp_float + row * width,
599                                          MESA_ARRAY_FORMAT_TYPE_FLOAT, 4,
600                                          rebase_swizzle, normalized, width);
601             src += src_stride;
602          }
603       }
604 
605       if (dst_format_is_mesa_array_format) {
606          for (row = 0; row < height; ++row) {
607             _mesa_swizzle_and_convert(dst, dst_type, dst_num_channels,
608                                       tmp_float + row * width,
609                                       MESA_ARRAY_FORMAT_TYPE_FLOAT, 4,
610                                       rgba2dst, normalized, width);
611             dst += dst_stride;
612          }
613       } else {
614          for (row = 0; row < height; ++row) {
615             _mesa_pack_float_rgba_row(dst_format, width,
616                                       (const float (*)[4])tmp_float + row * width, dst);
617             dst += dst_stride;
618          }
619       }
620 
621       free(tmp_float);
622    } else {
623       tmp_ubyte = malloc(width * height * sizeof(*tmp_ubyte));
624 
625       if (src_format_is_mesa_array_format) {
626          compute_rebased_rgba_component_mapping(src2rgba, rebase_swizzle,
627                                                 rebased_src2rgba);
628          for (row = 0; row < height; ++row) {
629             _mesa_swizzle_and_convert(tmp_ubyte + row * width,
630                                       MESA_ARRAY_FORMAT_TYPE_UBYTE, 4,
631                                       src, src_type, src_num_channels,
632                                       rebased_src2rgba, normalized, width);
633             src += src_stride;
634          }
635       } else {
636          for (row = 0; row < height; ++row) {
637             _mesa_unpack_ubyte_rgba_row(src_format, width,
638                                         src, tmp_ubyte + row * width);
639             if (rebase_swizzle)
640                _mesa_swizzle_and_convert(tmp_ubyte + row * width,
641                                          MESA_ARRAY_FORMAT_TYPE_UBYTE, 4,
642                                          tmp_ubyte + row * width,
643                                          MESA_ARRAY_FORMAT_TYPE_UBYTE, 4,
644                                          rebase_swizzle, normalized, width);
645             src += src_stride;
646          }
647       }
648 
649       if (dst_format_is_mesa_array_format) {
650          for (row = 0; row < height; ++row) {
651             _mesa_swizzle_and_convert(dst, dst_type, dst_num_channels,
652                                       tmp_ubyte + row * width,
653                                       MESA_ARRAY_FORMAT_TYPE_UBYTE, 4,
654                                       rgba2dst, normalized, width);
655             dst += dst_stride;
656          }
657       } else {
658          for (row = 0; row < height; ++row) {
659             _mesa_pack_ubyte_rgba_row(dst_format, width,
660                                       (const uint8_t *)(tmp_ubyte + row * width), dst);
661             dst += dst_stride;
662          }
663       }
664 
665       free(tmp_ubyte);
666    }
667 }
668 
669 /**
670  * Attempts to perform the given swizzle-and-convert operation with memcpy
671  *
672  * This function determines if the given swizzle-and-convert operation can
673  * be done with a simple memcpy and, if so, does the memcpy.  If not, it
674  * returns false and we fall back to the standard version below.
675  *
676  * The arguments are exactly the same as for _mesa_swizzle_and_convert
677  *
678  * \return  true if it successfully performed the swizzle-and-convert
679  *          operation with memcpy, false otherwise
680  */
681 static bool
swizzle_convert_try_memcpy(void * dst,enum mesa_array_format_datatype dst_type,int num_dst_channels,const void * src,enum mesa_array_format_datatype src_type,int num_src_channels,const uint8_t swizzle[4],bool normalized,int count)682 swizzle_convert_try_memcpy(void *dst,
683                            enum mesa_array_format_datatype dst_type,
684                            int num_dst_channels,
685                            const void *src,
686                            enum mesa_array_format_datatype src_type,
687                            int num_src_channels,
688                            const uint8_t swizzle[4], bool normalized, int count)
689 {
690    int i;
691 
692    if (src_type != dst_type)
693       return false;
694    if (num_src_channels != num_dst_channels)
695       return false;
696 
697    for (i = 0; i < num_dst_channels; ++i)
698       if (swizzle[i] != i && swizzle[i] != MESA_FORMAT_SWIZZLE_NONE)
699          return false;
700 
701    memcpy(dst, src, count * num_src_channels *
702           _mesa_array_format_datatype_get_size(src_type));
703 
704    return true;
705 }
706 
707 /**
708  * Represents a single instance of the standard swizzle-and-convert loop
709  *
710  * Any swizzle-and-convert operation simply loops through the pixels and
711  * performs the transformation operation one pixel at a time.  This macro
712  * embodies one instance of the conversion loop.  This way we can do all
713  * control flow outside of the loop and allow the compiler to unroll
714  * everything inside the loop.
715  *
716  * Note: This loop is carefully crafted for performance.  Be careful when
717  * changing it and run some benchmarks to ensure no performance regressions
718  * if you do.
719  *
720  * \param   DST_TYPE    the C datatype of the destination
721  * \param   DST_CHANS   the number of destination channels
722  * \param   SRC_TYPE    the C datatype of the source
723  * \param   SRC_CHANS   the number of source channels
724  * \param   CONV        an expression for converting from the source data,
725  *                      storred in the variable "src", to the destination
726  *                      format
727  */
728 #define SWIZZLE_CONVERT_LOOP(DST_TYPE, DST_CHANS, SRC_TYPE, SRC_CHANS, CONV) \
729    do {                                           \
730       int s, j;                                   \
731       for (s = 0; s < count; ++s) {               \
732          for (j = 0; j < SRC_CHANS; ++j) {        \
733             SRC_TYPE src = typed_src[j];          \
734             tmp[j] = CONV;                        \
735          }                                        \
736                                                   \
737          typed_dst[0] = tmp[swizzle_x];           \
738          if (DST_CHANS > 1) {                     \
739             typed_dst[1] = tmp[swizzle_y];        \
740             if (DST_CHANS > 2) {                  \
741                typed_dst[2] = tmp[swizzle_z];     \
742                if (DST_CHANS > 3) {               \
743                   typed_dst[3] = tmp[swizzle_w];  \
744                }                                  \
745             }                                     \
746          }                                        \
747          typed_src += SRC_CHANS;                  \
748          typed_dst += DST_CHANS;                  \
749       }                                           \
750    } while (0)
751 
752 /**
753  * Represents a single swizzle-and-convert operation
754  *
755  * This macro represents everything done in a single swizzle-and-convert
756  * operation.  The actual work is done by the SWIZZLE_CONVERT_LOOP macro.
757  * This macro acts as a wrapper that uses a nested switch to ensure that
758  * all looping parameters get unrolled.
759  *
760  * This macro makes assumptions about variables etc. in the calling
761  * function.  Changes to _mesa_swizzle_and_convert may require changes to
762  * this macro.
763  *
764  * \param   DST_TYPE    the C datatype of the destination
765  * \param   SRC_TYPE    the C datatype of the source
766  * \param   CONV        an expression for converting from the source data,
767  *                      storred in the variable "src", to the destination
768  *                      format
769  */
770 #define SWIZZLE_CONVERT(DST_TYPE, SRC_TYPE, CONV)                 \
771    do {                                                           \
772       const uint8_t swizzle_x = swizzle[0];                       \
773       const uint8_t swizzle_y = swizzle[1];                       \
774       const uint8_t swizzle_z = swizzle[2];                       \
775       const uint8_t swizzle_w = swizzle[3];                       \
776       const SRC_TYPE *typed_src = void_src;                       \
777       DST_TYPE *typed_dst = void_dst;                             \
778       DST_TYPE tmp[7];                                            \
779       tmp[4] = 0;                                                 \
780       tmp[5] = one;                                               \
781       switch (num_dst_channels) {                                 \
782       case 1:                                                     \
783          switch (num_src_channels) {                              \
784          case 1:                                                  \
785             SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 1, CONV); \
786             break;                                                \
787          case 2:                                                  \
788             SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 2, CONV); \
789             break;                                                \
790          case 3:                                                  \
791             SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 3, CONV); \
792             break;                                                \
793          case 4:                                                  \
794             SWIZZLE_CONVERT_LOOP(DST_TYPE, 1, SRC_TYPE, 4, CONV); \
795             break;                                                \
796          }                                                        \
797          break;                                                   \
798       case 2:                                                     \
799          switch (num_src_channels) {                              \
800          case 1:                                                  \
801             SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 1, CONV); \
802             break;                                                \
803          case 2:                                                  \
804             SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 2, CONV); \
805             break;                                                \
806          case 3:                                                  \
807             SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 3, CONV); \
808             break;                                                \
809          case 4:                                                  \
810             SWIZZLE_CONVERT_LOOP(DST_TYPE, 2, SRC_TYPE, 4, CONV); \
811             break;                                                \
812          }                                                        \
813          break;                                                   \
814       case 3:                                                     \
815          switch (num_src_channels) {                              \
816          case 1:                                                  \
817             SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 1, CONV); \
818             break;                                                \
819          case 2:                                                  \
820             SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 2, CONV); \
821             break;                                                \
822          case 3:                                                  \
823             SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 3, CONV); \
824             break;                                                \
825          case 4:                                                  \
826             SWIZZLE_CONVERT_LOOP(DST_TYPE, 3, SRC_TYPE, 4, CONV); \
827             break;                                                \
828          }                                                        \
829          break;                                                   \
830       case 4:                                                     \
831          switch (num_src_channels) {                              \
832          case 1:                                                  \
833             SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 1, CONV); \
834             break;                                                \
835          case 2:                                                  \
836             SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 2, CONV); \
837             break;                                                \
838          case 3:                                                  \
839             SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 3, CONV); \
840             break;                                                \
841          case 4:                                                  \
842             SWIZZLE_CONVERT_LOOP(DST_TYPE, 4, SRC_TYPE, 4, CONV); \
843             break;                                                \
844          }                                                        \
845          break;                                                   \
846       }                                                           \
847    } while (0)
848 
849 
850 static void
convert_float(void * void_dst,int num_dst_channels,const void * void_src,GLenum src_type,int num_src_channels,const uint8_t swizzle[4],bool normalized,int count)851 convert_float(void *void_dst, int num_dst_channels,
852               const void *void_src, GLenum src_type, int num_src_channels,
853               const uint8_t swizzle[4], bool normalized, int count)
854 {
855    const float one = 1.0f;
856 
857    switch (src_type) {
858    case MESA_ARRAY_FORMAT_TYPE_FLOAT:
859       SWIZZLE_CONVERT(float, float, src);
860       break;
861    case MESA_ARRAY_FORMAT_TYPE_HALF:
862       SWIZZLE_CONVERT(float, uint16_t, _mesa_half_to_float(src));
863       break;
864    case MESA_ARRAY_FORMAT_TYPE_UBYTE:
865       if (normalized) {
866          SWIZZLE_CONVERT(float, uint8_t, _mesa_unorm_to_float(src, 8));
867       } else {
868          SWIZZLE_CONVERT(float, uint8_t, src);
869       }
870       break;
871    case MESA_ARRAY_FORMAT_TYPE_BYTE:
872       if (normalized) {
873          SWIZZLE_CONVERT(float, int8_t, _mesa_snorm_to_float(src, 8));
874       } else {
875          SWIZZLE_CONVERT(float, int8_t, src);
876       }
877       break;
878    case MESA_ARRAY_FORMAT_TYPE_USHORT:
879       if (normalized) {
880          SWIZZLE_CONVERT(float, uint16_t, _mesa_unorm_to_float(src, 16));
881       } else {
882          SWIZZLE_CONVERT(float, uint16_t, src);
883       }
884       break;
885    case MESA_ARRAY_FORMAT_TYPE_SHORT:
886       if (normalized) {
887          SWIZZLE_CONVERT(float, int16_t, _mesa_snorm_to_float(src, 16));
888       } else {
889          SWIZZLE_CONVERT(float, int16_t, src);
890       }
891       break;
892    case MESA_ARRAY_FORMAT_TYPE_UINT:
893       if (normalized) {
894          SWIZZLE_CONVERT(float, uint32_t, _mesa_unorm_to_float(src, 32));
895       } else {
896          SWIZZLE_CONVERT(float, uint32_t, src);
897       }
898       break;
899    case MESA_ARRAY_FORMAT_TYPE_INT:
900       if (normalized) {
901          SWIZZLE_CONVERT(float, int32_t, _mesa_snorm_to_float(src, 32));
902       } else {
903          SWIZZLE_CONVERT(float, int32_t, src);
904       }
905       break;
906    default:
907       assert(!"Invalid channel type combination");
908    }
909 }
910 
911 
912 static void
convert_half_float(void * void_dst,int num_dst_channels,const void * void_src,GLenum src_type,int num_src_channels,const uint8_t swizzle[4],bool normalized,int count)913 convert_half_float(void *void_dst, int num_dst_channels,
914                    const void *void_src, GLenum src_type, int num_src_channels,
915                    const uint8_t swizzle[4], bool normalized, int count)
916 {
917    const uint16_t one = _mesa_float_to_half(1.0f);
918 
919    switch (src_type) {
920    case MESA_ARRAY_FORMAT_TYPE_FLOAT:
921       SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_half(src));
922       break;
923    case MESA_ARRAY_FORMAT_TYPE_HALF:
924       SWIZZLE_CONVERT(uint16_t, uint16_t, src);
925       break;
926    case MESA_ARRAY_FORMAT_TYPE_UBYTE:
927       if (normalized) {
928          SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_unorm_to_half(src, 8));
929       } else {
930          SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_float_to_half(src));
931       }
932       break;
933    case MESA_ARRAY_FORMAT_TYPE_BYTE:
934       if (normalized) {
935          SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_snorm_to_half(src, 8));
936       } else {
937          SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_float_to_half(src));
938       }
939       break;
940    case MESA_ARRAY_FORMAT_TYPE_USHORT:
941       if (normalized) {
942          SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_unorm_to_half(src, 16));
943       } else {
944          SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_float_to_half(src));
945       }
946       break;
947    case MESA_ARRAY_FORMAT_TYPE_SHORT:
948       if (normalized) {
949          SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_snorm_to_half(src, 16));
950       } else {
951          SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_float_to_half(src));
952       }
953       break;
954    case MESA_ARRAY_FORMAT_TYPE_UINT:
955       if (normalized) {
956          SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unorm_to_half(src, 32));
957       } else {
958          SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_float_to_half(src));
959       }
960       break;
961    case MESA_ARRAY_FORMAT_TYPE_INT:
962       if (normalized) {
963          SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_snorm_to_half(src, 32));
964       } else {
965          SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_float_to_half(src));
966       }
967       break;
968    default:
969       assert(!"Invalid channel type combination");
970    }
971 }
972 
973 static void
convert_ubyte(void * void_dst,int num_dst_channels,const void * void_src,GLenum src_type,int num_src_channels,const uint8_t swizzle[4],bool normalized,int count)974 convert_ubyte(void *void_dst, int num_dst_channels,
975               const void *void_src, GLenum src_type, int num_src_channels,
976               const uint8_t swizzle[4], bool normalized, int count)
977 {
978    const uint8_t one = normalized ? UINT8_MAX : 1;
979 
980    switch (src_type) {
981    case MESA_ARRAY_FORMAT_TYPE_FLOAT:
982       if (normalized) {
983          SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_unorm(src, 8));
984       } else {
985          SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_unsigned(src, 8));
986       }
987       break;
988    case MESA_ARRAY_FORMAT_TYPE_HALF:
989       if (normalized) {
990          SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_unorm(src, 8));
991       } else {
992          SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_unsigned(src, 8));
993       }
994       break;
995    case MESA_ARRAY_FORMAT_TYPE_UBYTE:
996       SWIZZLE_CONVERT(uint8_t, uint8_t, src);
997       break;
998    case MESA_ARRAY_FORMAT_TYPE_BYTE:
999       if (normalized) {
1000          SWIZZLE_CONVERT(uint8_t, int8_t, _mesa_snorm_to_unorm(src, 8, 8));
1001       } else {
1002          SWIZZLE_CONVERT(uint8_t, int8_t, _mesa_signed_to_unsigned(src, 8));
1003       }
1004       break;
1005    case MESA_ARRAY_FORMAT_TYPE_USHORT:
1006       if (normalized) {
1007          SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_unorm_to_unorm(src, 16, 8));
1008       } else {
1009          SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_unsigned_to_unsigned(src, 8));
1010       }
1011       break;
1012    case MESA_ARRAY_FORMAT_TYPE_SHORT:
1013       if (normalized) {
1014          SWIZZLE_CONVERT(uint8_t, int16_t, _mesa_snorm_to_unorm(src, 16, 8));
1015       } else {
1016          SWIZZLE_CONVERT(uint8_t, int16_t, _mesa_signed_to_unsigned(src, 8));
1017       }
1018       break;
1019    case MESA_ARRAY_FORMAT_TYPE_UINT:
1020       if (normalized) {
1021          SWIZZLE_CONVERT(uint8_t, uint32_t, _mesa_unorm_to_unorm(src, 32, 8));
1022       } else {
1023          SWIZZLE_CONVERT(uint8_t, uint32_t, _mesa_unsigned_to_unsigned(src, 8));
1024       }
1025       break;
1026    case MESA_ARRAY_FORMAT_TYPE_INT:
1027       if (normalized) {
1028          SWIZZLE_CONVERT(uint8_t, int32_t, _mesa_snorm_to_unorm(src, 32, 8));
1029       } else {
1030          SWIZZLE_CONVERT(uint8_t, int32_t, _mesa_signed_to_unsigned(src, 8));
1031       }
1032       break;
1033    default:
1034       assert(!"Invalid channel type combination");
1035    }
1036 }
1037 
1038 
1039 static void
convert_byte(void * void_dst,int num_dst_channels,const void * void_src,GLenum src_type,int num_src_channels,const uint8_t swizzle[4],bool normalized,int count)1040 convert_byte(void *void_dst, int num_dst_channels,
1041              const void *void_src, GLenum src_type, int num_src_channels,
1042              const uint8_t swizzle[4], bool normalized, int count)
1043 {
1044    const int8_t one = normalized ? INT8_MAX : 1;
1045 
1046    switch (src_type) {
1047    case MESA_ARRAY_FORMAT_TYPE_FLOAT:
1048       if (normalized) {
1049          SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_snorm(src, 8));
1050       } else {
1051          SWIZZLE_CONVERT(uint8_t, float, _mesa_float_to_signed(src, 8));
1052       }
1053       break;
1054    case MESA_ARRAY_FORMAT_TYPE_HALF:
1055       if (normalized) {
1056          SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_snorm(src, 8));
1057       } else {
1058          SWIZZLE_CONVERT(uint8_t, uint16_t, _mesa_half_to_signed(src, 8));
1059       }
1060       break;
1061    case MESA_ARRAY_FORMAT_TYPE_UBYTE:
1062       if (normalized) {
1063          SWIZZLE_CONVERT(int8_t, uint8_t, _mesa_unorm_to_snorm(src, 8, 8));
1064       } else {
1065          SWIZZLE_CONVERT(int8_t, uint8_t, _mesa_unsigned_to_signed(src, 8));
1066       }
1067       break;
1068    case MESA_ARRAY_FORMAT_TYPE_BYTE:
1069       SWIZZLE_CONVERT(int8_t, int8_t, src);
1070       break;
1071    case MESA_ARRAY_FORMAT_TYPE_USHORT:
1072       if (normalized) {
1073          SWIZZLE_CONVERT(int8_t, uint16_t, _mesa_unorm_to_snorm(src, 16, 8));
1074       } else {
1075          SWIZZLE_CONVERT(int8_t, uint16_t, _mesa_unsigned_to_signed(src, 8));
1076       }
1077       break;
1078    case MESA_ARRAY_FORMAT_TYPE_SHORT:
1079       if (normalized) {
1080          SWIZZLE_CONVERT(int8_t, int16_t, _mesa_snorm_to_snorm(src, 16, 8));
1081       } else {
1082          SWIZZLE_CONVERT(int8_t, int16_t, _mesa_signed_to_signed(src, 8));
1083       }
1084       break;
1085    case MESA_ARRAY_FORMAT_TYPE_UINT:
1086       if (normalized) {
1087          SWIZZLE_CONVERT(int8_t, uint32_t, _mesa_unorm_to_snorm(src, 32, 8));
1088       } else {
1089          SWIZZLE_CONVERT(int8_t, uint32_t, _mesa_unsigned_to_signed(src, 8));
1090       }
1091       break;
1092    case MESA_ARRAY_FORMAT_TYPE_INT:
1093       if (normalized) {
1094          SWIZZLE_CONVERT(int8_t, int32_t, _mesa_snorm_to_snorm(src, 32, 8));
1095       } else {
1096          SWIZZLE_CONVERT(int8_t, int32_t, _mesa_signed_to_signed(src, 8));
1097       }
1098       break;
1099    default:
1100       assert(!"Invalid channel type combination");
1101    }
1102 }
1103 
1104 
1105 static void
convert_ushort(void * void_dst,int num_dst_channels,const void * void_src,GLenum src_type,int num_src_channels,const uint8_t swizzle[4],bool normalized,int count)1106 convert_ushort(void *void_dst, int num_dst_channels,
1107                const void *void_src, GLenum src_type, int num_src_channels,
1108                const uint8_t swizzle[4], bool normalized, int count)
1109 {
1110    const uint16_t one = normalized ? UINT16_MAX : 1;
1111 
1112    switch (src_type) {
1113    case MESA_ARRAY_FORMAT_TYPE_FLOAT:
1114       if (normalized) {
1115          SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_unorm(src, 16));
1116       } else {
1117          SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_unsigned(src, 16));
1118       }
1119       break;
1120    case MESA_ARRAY_FORMAT_TYPE_HALF:
1121       if (normalized) {
1122          SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_unorm(src, 16));
1123       } else {
1124          SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_unsigned(src, 16));
1125       }
1126       break;
1127    case MESA_ARRAY_FORMAT_TYPE_UBYTE:
1128       if (normalized) {
1129          SWIZZLE_CONVERT(uint16_t, uint8_t, _mesa_unorm_to_unorm(src, 8, 16));
1130       } else {
1131          SWIZZLE_CONVERT(uint16_t, uint8_t, src);
1132       }
1133       break;
1134    case MESA_ARRAY_FORMAT_TYPE_BYTE:
1135       if (normalized) {
1136          SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_snorm_to_unorm(src, 8, 16));
1137       } else {
1138          SWIZZLE_CONVERT(uint16_t, int8_t, _mesa_signed_to_unsigned(src, 16));
1139       }
1140       break;
1141    case MESA_ARRAY_FORMAT_TYPE_USHORT:
1142       SWIZZLE_CONVERT(uint16_t, uint16_t, src);
1143       break;
1144    case MESA_ARRAY_FORMAT_TYPE_SHORT:
1145       if (normalized) {
1146          SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_snorm_to_unorm(src, 16, 16));
1147       } else {
1148          SWIZZLE_CONVERT(uint16_t, int16_t, _mesa_signed_to_unsigned(src, 16));
1149       }
1150       break;
1151    case MESA_ARRAY_FORMAT_TYPE_UINT:
1152       if (normalized) {
1153          SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unorm_to_unorm(src, 32, 16));
1154       } else {
1155          SWIZZLE_CONVERT(uint16_t, uint32_t, _mesa_unsigned_to_unsigned(src, 16));
1156       }
1157       break;
1158    case MESA_ARRAY_FORMAT_TYPE_INT:
1159       if (normalized) {
1160          SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_snorm_to_unorm(src, 32, 16));
1161       } else {
1162          SWIZZLE_CONVERT(uint16_t, int32_t, _mesa_signed_to_unsigned(src, 16));
1163       }
1164       break;
1165    default:
1166       assert(!"Invalid channel type combination");
1167    }
1168 }
1169 
1170 
1171 static void
convert_short(void * void_dst,int num_dst_channels,const void * void_src,GLenum src_type,int num_src_channels,const uint8_t swizzle[4],bool normalized,int count)1172 convert_short(void *void_dst, int num_dst_channels,
1173               const void *void_src, GLenum src_type, int num_src_channels,
1174               const uint8_t swizzle[4], bool normalized, int count)
1175 {
1176    const int16_t one = normalized ? INT16_MAX : 1;
1177 
1178    switch (src_type) {
1179    case MESA_ARRAY_FORMAT_TYPE_FLOAT:
1180       if (normalized) {
1181          SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_snorm(src, 16));
1182       } else {
1183          SWIZZLE_CONVERT(uint16_t, float, _mesa_float_to_signed(src, 16));
1184       }
1185       break;
1186    case MESA_ARRAY_FORMAT_TYPE_HALF:
1187       if (normalized) {
1188          SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_snorm(src, 16));
1189       } else {
1190          SWIZZLE_CONVERT(uint16_t, uint16_t, _mesa_half_to_signed(src, 16));
1191       }
1192       break;
1193    case MESA_ARRAY_FORMAT_TYPE_UBYTE:
1194       if (normalized) {
1195          SWIZZLE_CONVERT(int16_t, uint8_t, _mesa_unorm_to_snorm(src, 8, 16));
1196       } else {
1197          SWIZZLE_CONVERT(int16_t, uint8_t, src);
1198       }
1199       break;
1200    case MESA_ARRAY_FORMAT_TYPE_BYTE:
1201       if (normalized) {
1202          SWIZZLE_CONVERT(int16_t, int8_t, _mesa_snorm_to_snorm(src, 8, 16));
1203       } else {
1204          SWIZZLE_CONVERT(int16_t, int8_t, src);
1205       }
1206       break;
1207    case MESA_ARRAY_FORMAT_TYPE_USHORT:
1208       if (normalized) {
1209          SWIZZLE_CONVERT(int16_t, uint16_t, _mesa_unorm_to_snorm(src, 16, 16));
1210       } else {
1211          SWIZZLE_CONVERT(int16_t, uint16_t, _mesa_unsigned_to_signed(src, 16));
1212       }
1213       break;
1214    case MESA_ARRAY_FORMAT_TYPE_SHORT:
1215       SWIZZLE_CONVERT(int16_t, int16_t, src);
1216       break;
1217    case MESA_ARRAY_FORMAT_TYPE_UINT:
1218       if (normalized) {
1219          SWIZZLE_CONVERT(int16_t, uint32_t, _mesa_unorm_to_snorm(src, 32, 16));
1220       } else {
1221          SWIZZLE_CONVERT(int16_t, uint32_t, _mesa_unsigned_to_signed(src, 16));
1222       }
1223       break;
1224    case MESA_ARRAY_FORMAT_TYPE_INT:
1225       if (normalized) {
1226          SWIZZLE_CONVERT(int16_t, int32_t, _mesa_snorm_to_snorm(src, 32, 16));
1227       } else {
1228          SWIZZLE_CONVERT(int16_t, int32_t, _mesa_signed_to_signed(src, 16));
1229       }
1230       break;
1231    default:
1232       assert(!"Invalid channel type combination");
1233    }
1234 }
1235 
1236 static void
convert_uint(void * void_dst,int num_dst_channels,const void * void_src,GLenum src_type,int num_src_channels,const uint8_t swizzle[4],bool normalized,int count)1237 convert_uint(void *void_dst, int num_dst_channels,
1238              const void *void_src, GLenum src_type, int num_src_channels,
1239              const uint8_t swizzle[4], bool normalized, int count)
1240 {
1241    const uint32_t one = normalized ? UINT32_MAX : 1;
1242 
1243    switch (src_type) {
1244    case MESA_ARRAY_FORMAT_TYPE_FLOAT:
1245       if (normalized) {
1246          SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_unorm(src, 32));
1247       } else {
1248          SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_unsigned(src, 32));
1249       }
1250       break;
1251    case MESA_ARRAY_FORMAT_TYPE_HALF:
1252       if (normalized) {
1253          SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_unorm(src, 32));
1254       } else {
1255          SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_unsigned(src, 32));
1256       }
1257       break;
1258    case MESA_ARRAY_FORMAT_TYPE_UBYTE:
1259       if (normalized) {
1260          SWIZZLE_CONVERT(uint32_t, uint8_t, _mesa_unorm_to_unorm(src, 8, 32));
1261       } else {
1262          SWIZZLE_CONVERT(uint32_t, uint8_t, src);
1263       }
1264       break;
1265    case MESA_ARRAY_FORMAT_TYPE_BYTE:
1266       if (normalized) {
1267          SWIZZLE_CONVERT(uint32_t, int8_t, _mesa_snorm_to_unorm(src, 8, 32));
1268       } else {
1269          SWIZZLE_CONVERT(uint32_t, int8_t, _mesa_signed_to_unsigned(src, 32));
1270       }
1271       break;
1272    case MESA_ARRAY_FORMAT_TYPE_USHORT:
1273       if (normalized) {
1274          SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_unorm_to_unorm(src, 16, 32));
1275       } else {
1276          SWIZZLE_CONVERT(uint32_t, uint16_t, src);
1277       }
1278       break;
1279    case MESA_ARRAY_FORMAT_TYPE_SHORT:
1280       if (normalized) {
1281          SWIZZLE_CONVERT(uint32_t, int16_t, _mesa_snorm_to_unorm(src, 16, 32));
1282       } else {
1283          SWIZZLE_CONVERT(uint32_t, int16_t, _mesa_signed_to_unsigned(src, 32));
1284       }
1285       break;
1286    case MESA_ARRAY_FORMAT_TYPE_UINT:
1287       SWIZZLE_CONVERT(uint32_t, uint32_t, src);
1288       break;
1289    case MESA_ARRAY_FORMAT_TYPE_INT:
1290       if (normalized) {
1291          SWIZZLE_CONVERT(uint32_t, int32_t, _mesa_snorm_to_unorm(src, 32, 32));
1292       } else {
1293          SWIZZLE_CONVERT(uint32_t, int32_t, _mesa_signed_to_unsigned(src, 32));
1294       }
1295       break;
1296    default:
1297       assert(!"Invalid channel type combination");
1298    }
1299 }
1300 
1301 
1302 static void
convert_int(void * void_dst,int num_dst_channels,const void * void_src,GLenum src_type,int num_src_channels,const uint8_t swizzle[4],bool normalized,int count)1303 convert_int(void *void_dst, int num_dst_channels,
1304             const void *void_src, GLenum src_type, int num_src_channels,
1305             const uint8_t swizzle[4], bool normalized, int count)
1306 {
1307    const int32_t one = normalized ? INT32_MAX : 1;
1308 
1309    switch (src_type) {
1310    case MESA_ARRAY_FORMAT_TYPE_FLOAT:
1311       if (normalized) {
1312          SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_snorm(src, 32));
1313       } else {
1314          SWIZZLE_CONVERT(uint32_t, float, _mesa_float_to_signed(src, 32));
1315       }
1316       break;
1317    case MESA_ARRAY_FORMAT_TYPE_HALF:
1318       if (normalized) {
1319          SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_snorm(src, 32));
1320       } else {
1321          SWIZZLE_CONVERT(uint32_t, uint16_t, _mesa_half_to_signed(src, 32));
1322       }
1323       break;
1324    case MESA_ARRAY_FORMAT_TYPE_UBYTE:
1325       if (normalized) {
1326          SWIZZLE_CONVERT(int32_t, uint8_t, _mesa_unorm_to_snorm(src, 8, 32));
1327       } else {
1328          SWIZZLE_CONVERT(int32_t, uint8_t, src);
1329       }
1330       break;
1331    case MESA_ARRAY_FORMAT_TYPE_BYTE:
1332       if (normalized) {
1333          SWIZZLE_CONVERT(int32_t, int8_t, _mesa_snorm_to_snorm(src, 8, 32));
1334       } else {
1335          SWIZZLE_CONVERT(int32_t, int8_t, src);
1336       }
1337       break;
1338    case MESA_ARRAY_FORMAT_TYPE_USHORT:
1339       if (normalized) {
1340          SWIZZLE_CONVERT(int32_t, uint16_t, _mesa_unorm_to_snorm(src, 16, 32));
1341       } else {
1342          SWIZZLE_CONVERT(int32_t, uint16_t, src);
1343       }
1344       break;
1345    case MESA_ARRAY_FORMAT_TYPE_SHORT:
1346       if (normalized) {
1347          SWIZZLE_CONVERT(int32_t, int16_t, _mesa_snorm_to_snorm(src, 16, 32));
1348       } else {
1349          SWIZZLE_CONVERT(int32_t, int16_t, src);
1350       }
1351       break;
1352    case MESA_ARRAY_FORMAT_TYPE_UINT:
1353       if (normalized) {
1354          SWIZZLE_CONVERT(int32_t, uint32_t, _mesa_unorm_to_snorm(src, 32, 32));
1355       } else {
1356          SWIZZLE_CONVERT(int32_t, uint32_t, _mesa_unsigned_to_signed(src, 32));
1357       }
1358       break;
1359    case MESA_ARRAY_FORMAT_TYPE_INT:
1360       SWIZZLE_CONVERT(int32_t, int32_t, src);
1361       break;
1362    default:
1363       assert(!"Invalid channel type combination");
1364    }
1365 }
1366 
1367 
1368 /**
1369  * Convert between array-based color formats.
1370  *
1371  * Most format conversion operations required by GL can be performed by
1372  * converting one channel at a time, shuffling the channels around, and
1373  * optionally filling missing channels with zeros and ones.  This function
1374  * does just that in a general, yet efficient, way.
1375  *
1376  * The swizzle parameter is an array of 4 numbers (see
1377  * _mesa_get_format_swizzle) that describes where each channel in the
1378  * destination should come from in the source.  If swizzle[i] < 4 then it
1379  * means that dst[i] = CONVERT(src[swizzle[i]]).  If swizzle[i] is
1380  * MESA_FORMAT_SWIZZLE_ZERO or MESA_FORMAT_SWIZZLE_ONE, the corresponding
1381  * dst[i] will be filled with the appropreate representation of zero or one
1382  * respectively.
1383  *
1384  * Under most circumstances, the source and destination images must be
1385  * different as no care is taken not to clobber one with the other.
1386  * However, if they have the same number of bits per pixel, it is safe to
1387  * do an in-place conversion.
1388  *
1389  * \param[out] dst               pointer to where the converted data should
1390  *                               be stored
1391  *
1392  * \param[in]  dst_type          the destination GL type of the converted
1393  *                               data (GL_BYTE, etc.)
1394  *
1395  * \param[in]  num_dst_channels  the number of channels in the converted
1396  *                               data
1397  *
1398  * \param[in]  src               pointer to the source data
1399  *
1400  * \param[in]  src_type          the GL type of the source data (GL_BYTE,
1401  *                               etc.)
1402  *
1403  * \param[in]  num_src_channels  the number of channels in the source data
1404  *                               (the number of channels total, not just
1405  *                               the number used)
1406  *
1407  * \param[in]  swizzle           describes how to get the destination data
1408  *                               from the source data.
1409  *
1410  * \param[in]  normalized        for integer types, this indicates whether
1411  *                               the data should be considered as integers
1412  *                               or as normalized integers;
1413  *
1414  * \param[in]  count             the number of pixels to convert
1415  */
1416 void
_mesa_swizzle_and_convert(void * void_dst,enum mesa_array_format_datatype dst_type,int num_dst_channels,const void * void_src,enum mesa_array_format_datatype src_type,int num_src_channels,const uint8_t swizzle[4],bool normalized,int count)1417 _mesa_swizzle_and_convert(void *void_dst, enum mesa_array_format_datatype dst_type, int num_dst_channels,
1418                           const void *void_src, enum mesa_array_format_datatype src_type, int num_src_channels,
1419                           const uint8_t swizzle[4], bool normalized, int count)
1420 {
1421    if (swizzle_convert_try_memcpy(void_dst, dst_type, num_dst_channels,
1422                                   void_src, src_type, num_src_channels,
1423                                   swizzle, normalized, count))
1424       return;
1425 
1426    switch (dst_type) {
1427    case MESA_ARRAY_FORMAT_TYPE_FLOAT:
1428       convert_float(void_dst, num_dst_channels, void_src, src_type,
1429                     num_src_channels, swizzle, normalized, count);
1430       break;
1431    case MESA_ARRAY_FORMAT_TYPE_HALF:
1432       convert_half_float(void_dst, num_dst_channels, void_src, src_type,
1433                     num_src_channels, swizzle, normalized, count);
1434       break;
1435    case MESA_ARRAY_FORMAT_TYPE_UBYTE:
1436       convert_ubyte(void_dst, num_dst_channels, void_src, src_type,
1437                     num_src_channels, swizzle, normalized, count);
1438       break;
1439    case MESA_ARRAY_FORMAT_TYPE_BYTE:
1440       convert_byte(void_dst, num_dst_channels, void_src, src_type,
1441                    num_src_channels, swizzle, normalized, count);
1442       break;
1443    case MESA_ARRAY_FORMAT_TYPE_USHORT:
1444       convert_ushort(void_dst, num_dst_channels, void_src, src_type,
1445                      num_src_channels, swizzle, normalized, count);
1446       break;
1447    case MESA_ARRAY_FORMAT_TYPE_SHORT:
1448       convert_short(void_dst, num_dst_channels, void_src, src_type,
1449                     num_src_channels, swizzle, normalized, count);
1450       break;
1451    case MESA_ARRAY_FORMAT_TYPE_UINT:
1452       convert_uint(void_dst, num_dst_channels, void_src, src_type,
1453                    num_src_channels, swizzle, normalized, count);
1454       break;
1455    case MESA_ARRAY_FORMAT_TYPE_INT:
1456       convert_int(void_dst, num_dst_channels, void_src, src_type,
1457                   num_src_channels, swizzle, normalized, count);
1458       break;
1459    default:
1460       assert(!"Invalid channel type");
1461    }
1462 }
1463