• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5  * Copyright (c) 2008-2009  VMware, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 
27 #include "imports.h"
28 #include "formats.h"
29 #include "macros.h"
30 #include "glformats.h"
31 #include "c11/threads.h"
32 #include "util/hash_table.h"
33 
34 /**
35  * Information about texture formats.
36  */
37 struct gl_format_info
38 {
39    mesa_format Name;
40 
41    /** text name for debugging */
42    const char *StrName;
43 
44    enum mesa_format_layout Layout;
45 
46    /**
47     * Base format is one of GL_RED, GL_RG, GL_RGB, GL_RGBA, GL_ALPHA,
48     * GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_INTENSITY, GL_YCBCR_MESA,
49     * GL_DEPTH_COMPONENT, GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
50     */
51    GLenum BaseFormat;
52 
53    /**
54     * Logical data type: one of  GL_UNSIGNED_NORMALIZED, GL_SIGNED_NORMALIZED,
55     * GL_UNSIGNED_INT, GL_INT, GL_FLOAT.
56     */
57    GLenum DataType;
58 
59    GLubyte RedBits;
60    GLubyte GreenBits;
61    GLubyte BlueBits;
62    GLubyte AlphaBits;
63    GLubyte LuminanceBits;
64    GLubyte IntensityBits;
65    GLubyte DepthBits;
66    GLubyte StencilBits;
67 
68    bool IsSRGBFormat;
69 
70    /**
71     * To describe compressed formats.  If not compressed, Width=Height=Depth=1.
72     */
73    GLubyte BlockWidth, BlockHeight, BlockDepth;
74    GLubyte BytesPerBlock;
75 
76    uint8_t Swizzle[4];
77    mesa_array_format ArrayFormat;
78 };
79 
80 #include "format_info.h"
81 
82 static const struct gl_format_info *
_mesa_get_format_info(mesa_format format)83 _mesa_get_format_info(mesa_format format)
84 {
85    const struct gl_format_info *info = &format_info[format];
86    STATIC_ASSERT(ARRAY_SIZE(format_info) == MESA_FORMAT_COUNT);
87    assert(info->Name == format);
88    return info;
89 }
90 
91 
92 /** Return string name of format (for debugging) */
93 const char *
_mesa_get_format_name(mesa_format format)94 _mesa_get_format_name(mesa_format format)
95 {
96    const struct gl_format_info *info = _mesa_get_format_info(format);
97    return info->StrName;
98 }
99 
100 
101 
102 /**
103  * Return bytes needed to store a block of pixels in the given format.
104  * Normally, a block is 1x1 (a single pixel).  But for compressed formats
105  * a block may be 4x4 or 8x4, etc.
106  *
107  * Note: not GLuint, so as not to coerce math to unsigned. cf. fdo #37351
108  */
109 GLint
_mesa_get_format_bytes(mesa_format format)110 _mesa_get_format_bytes(mesa_format format)
111 {
112    const struct gl_format_info *info = _mesa_get_format_info(format);
113    assert(info->BytesPerBlock);
114    assert(info->BytesPerBlock <= MAX_PIXEL_BYTES ||
115           _mesa_is_format_compressed(format));
116    return info->BytesPerBlock;
117 }
118 
119 
120 /**
121  * Return bits per component for the given format.
122  * \param format  one of MESA_FORMAT_x
123  * \param pname  the component, such as GL_RED_BITS, GL_TEXTURE_BLUE_BITS, etc.
124  */
125 GLint
_mesa_get_format_bits(mesa_format format,GLenum pname)126 _mesa_get_format_bits(mesa_format format, GLenum pname)
127 {
128    const struct gl_format_info *info = _mesa_get_format_info(format);
129 
130    switch (pname) {
131    case GL_RED_BITS:
132    case GL_TEXTURE_RED_SIZE:
133    case GL_RENDERBUFFER_RED_SIZE_EXT:
134    case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
135    case GL_INTERNALFORMAT_RED_SIZE:
136       return info->RedBits;
137    case GL_GREEN_BITS:
138    case GL_TEXTURE_GREEN_SIZE:
139    case GL_RENDERBUFFER_GREEN_SIZE_EXT:
140    case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
141    case GL_INTERNALFORMAT_GREEN_SIZE:
142       return info->GreenBits;
143    case GL_BLUE_BITS:
144    case GL_TEXTURE_BLUE_SIZE:
145    case GL_RENDERBUFFER_BLUE_SIZE_EXT:
146    case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
147    case GL_INTERNALFORMAT_BLUE_SIZE:
148       return info->BlueBits;
149    case GL_ALPHA_BITS:
150    case GL_TEXTURE_ALPHA_SIZE:
151    case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
152    case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
153    case GL_INTERNALFORMAT_ALPHA_SIZE:
154       return info->AlphaBits;
155    case GL_TEXTURE_INTENSITY_SIZE:
156       return info->IntensityBits;
157    case GL_TEXTURE_LUMINANCE_SIZE:
158       return info->LuminanceBits;
159    case GL_INDEX_BITS:
160       return 0;
161    case GL_DEPTH_BITS:
162    case GL_TEXTURE_DEPTH_SIZE_ARB:
163    case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
164    case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
165    case GL_INTERNALFORMAT_DEPTH_SIZE:
166       return info->DepthBits;
167    case GL_STENCIL_BITS:
168    case GL_TEXTURE_STENCIL_SIZE_EXT:
169    case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
170    case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
171    case GL_INTERNALFORMAT_STENCIL_SIZE:
172       return info->StencilBits;
173    default:
174       _mesa_problem(NULL, "bad pname in _mesa_get_format_bits()");
175       return 0;
176    }
177 }
178 
179 
180 GLuint
_mesa_get_format_max_bits(mesa_format format)181 _mesa_get_format_max_bits(mesa_format format)
182 {
183    const struct gl_format_info *info = _mesa_get_format_info(format);
184    GLuint max = MAX2(info->RedBits, info->GreenBits);
185    max = MAX2(max, info->BlueBits);
186    max = MAX2(max, info->AlphaBits);
187    max = MAX2(max, info->LuminanceBits);
188    max = MAX2(max, info->IntensityBits);
189    max = MAX2(max, info->DepthBits);
190    max = MAX2(max, info->StencilBits);
191    return max;
192 }
193 
194 
195 /**
196  * Return the layout type of the given format.
197  */
198 extern enum mesa_format_layout
_mesa_get_format_layout(mesa_format format)199 _mesa_get_format_layout(mesa_format format)
200 {
201    const struct gl_format_info *info = _mesa_get_format_info(format);
202    return info->Layout;
203 }
204 
205 
206 /**
207  * Return the data type (or more specifically, the data representation)
208  * for the given format.
209  * The return value will be one of:
210  *    GL_UNSIGNED_NORMALIZED = unsigned int representing [0,1]
211  *    GL_SIGNED_NORMALIZED = signed int representing [-1, 1]
212  *    GL_UNSIGNED_INT = an ordinary unsigned integer
213  *    GL_INT = an ordinary signed integer
214  *    GL_FLOAT = an ordinary float
215  */
216 GLenum
_mesa_get_format_datatype(mesa_format format)217 _mesa_get_format_datatype(mesa_format format)
218 {
219    const struct gl_format_info *info = _mesa_get_format_info(format);
220    return info->DataType;
221 }
222 
223 static GLenum
get_base_format_for_array_format(mesa_array_format format)224 get_base_format_for_array_format(mesa_array_format format)
225 {
226    uint8_t swizzle[4];
227    int num_channels;
228 
229    _mesa_array_format_get_swizzle(format, swizzle);
230    num_channels = _mesa_array_format_get_num_channels(format);
231 
232    switch (num_channels) {
233    case 4:
234       /* FIXME: RGBX formats have 4 channels, but their base format is GL_RGB.
235        * This is not really a problem for now because we only create array
236        * formats from GL format/type combinations, and these cannot specify
237        * RGBX formats.
238        */
239       return GL_RGBA;
240    case 3:
241       return GL_RGB;
242    case 2:
243       if (swizzle[0] == 0 &&
244           swizzle[1] == 0 &&
245           swizzle[2] == 0 &&
246           swizzle[3] == 1)
247          return GL_LUMINANCE_ALPHA;
248       if (swizzle[0] == 1 &&
249           swizzle[1] == 1 &&
250           swizzle[2] == 1 &&
251           swizzle[3] == 0)
252          return GL_LUMINANCE_ALPHA;
253       if (swizzle[0] == 0 &&
254           swizzle[1] == 1 &&
255           swizzle[2] == 4 &&
256           swizzle[3] == 5)
257          return GL_RG;
258       if (swizzle[0] == 1 &&
259           swizzle[1] == 0 &&
260           swizzle[2] == 4 &&
261           swizzle[3] == 5)
262          return GL_RG;
263       break;
264    case 1:
265       if (swizzle[0] == 0 &&
266           swizzle[1] == 0 &&
267           swizzle[2] == 0 &&
268           swizzle[3] == 5)
269          return GL_LUMINANCE;
270       if (swizzle[0] == 0 &&
271           swizzle[1] == 0 &&
272           swizzle[2] == 0 &&
273           swizzle[3] == 0)
274          return GL_INTENSITY;
275       if (swizzle[0] <= MESA_FORMAT_SWIZZLE_W)
276          return GL_RED;
277       if (swizzle[1] <= MESA_FORMAT_SWIZZLE_W)
278          return GL_GREEN;
279       if (swizzle[2] <= MESA_FORMAT_SWIZZLE_W)
280          return GL_BLUE;
281       if (swizzle[3] <= MESA_FORMAT_SWIZZLE_W)
282          return GL_ALPHA;
283       break;
284    }
285 
286    unreachable("Unsupported format");
287 }
288 
289 /**
290  * Return the basic format for the given type.  The result will be one of
291  * GL_RGB, GL_RGBA, GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_INTENSITY,
292  * GL_YCBCR_MESA, GL_DEPTH_COMPONENT, GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
293  * This functions accepts a mesa_format or a mesa_array_format.
294  */
295 GLenum
_mesa_get_format_base_format(uint32_t format)296 _mesa_get_format_base_format(uint32_t format)
297 {
298    if (!_mesa_format_is_mesa_array_format(format)) {
299       const struct gl_format_info *info = _mesa_get_format_info(format);
300       return info->BaseFormat;
301    } else {
302       return get_base_format_for_array_format(format);
303    }
304 }
305 
306 
307 /**
308  * Return the block size (in pixels) for the given format.  Normally
309  * the block size is 1x1.  But compressed formats will have block sizes
310  * of 4x4 or 8x4 pixels, etc.
311  * \param bw  returns block width in pixels
312  * \param bh  returns block height in pixels
313  */
314 void
_mesa_get_format_block_size(mesa_format format,GLuint * bw,GLuint * bh)315 _mesa_get_format_block_size(mesa_format format, GLuint *bw, GLuint *bh)
316 {
317    const struct gl_format_info *info = _mesa_get_format_info(format);
318    /* Use _mesa_get_format_block_size_3d() for 3D blocks. */
319    assert(info->BlockDepth == 1);
320 
321    *bw = info->BlockWidth;
322    *bh = info->BlockHeight;
323 }
324 
325 
326 /**
327  * Return the block size (in pixels) for the given format. Normally
328  * the block size is 1x1x1. But compressed formats will have block
329  * sizes of 4x4x4, 3x3x3 pixels, etc.
330  * \param bw  returns block width in pixels
331  * \param bh  returns block height in pixels
332  * \param bd  returns block depth in pixels
333  */
334 void
_mesa_get_format_block_size_3d(mesa_format format,GLuint * bw,GLuint * bh,GLuint * bd)335 _mesa_get_format_block_size_3d(mesa_format format,
336                                GLuint *bw,
337                                GLuint *bh,
338                                GLuint *bd)
339 {
340    const struct gl_format_info *info = _mesa_get_format_info(format);
341    *bw = info->BlockWidth;
342    *bh = info->BlockHeight;
343    *bd = info->BlockDepth;
344 }
345 
346 
347 /**
348  * Returns the an array of four numbers representing the transformation
349  * from the RGBA or SZ colorspace to the given format.  For array formats,
350  * the i'th RGBA component is given by:
351  *
352  * if (swizzle[i] <= MESA_FORMAT_SWIZZLE_W)
353  *    comp = data[swizzle[i]];
354  * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_ZERO)
355  *    comp = 0;
356  * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_ONE)
357  *    comp = 1;
358  * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_NONE)
359  *    // data does not contain a channel of this format
360  *
361  * For packed formats, the swizzle gives the number of components left of
362  * the least significant bit.
363  *
364  * Compressed formats have no swizzle.
365  */
366 void
_mesa_get_format_swizzle(mesa_format format,uint8_t swizzle_out[4])367 _mesa_get_format_swizzle(mesa_format format, uint8_t swizzle_out[4])
368 {
369    const struct gl_format_info *info = _mesa_get_format_info(format);
370    memcpy(swizzle_out, info->Swizzle, sizeof(info->Swizzle));
371 }
372 
373 mesa_array_format
_mesa_array_format_flip_channels(mesa_array_format format)374 _mesa_array_format_flip_channels(mesa_array_format format)
375 {
376    int num_channels;
377    uint8_t swizzle[4];
378 
379    num_channels = _mesa_array_format_get_num_channels(format);
380    _mesa_array_format_get_swizzle(format, swizzle);
381 
382    if (num_channels == 1)
383       return format;
384 
385    if (num_channels == 2) {
386       /* Assert that the swizzle makes sense for 2 channels */
387       for (unsigned i = 0; i < 4; i++)
388          assert(swizzle[i] != 2 && swizzle[i] != 3);
389 
390       static const uint8_t flip_xy[6] = { 1, 0, 2, 3, 4, 5 };
391       _mesa_array_format_set_swizzle(&format,
392                                      flip_xy[swizzle[0]], flip_xy[swizzle[1]],
393                                      flip_xy[swizzle[2]], flip_xy[swizzle[3]]);
394       return format;
395    }
396 
397    if (num_channels == 4) {
398       static const uint8_t flip[6] = { 3, 2, 1, 0, 4, 5 };
399       _mesa_array_format_set_swizzle(&format,
400                                      flip[swizzle[0]], flip[swizzle[1]],
401                                      flip[swizzle[2]], flip[swizzle[3]]);
402       return format;
403    }
404 
405    unreachable("Invalid array format");
406 }
407 
408 uint32_t
_mesa_format_to_array_format(mesa_format format)409 _mesa_format_to_array_format(mesa_format format)
410 {
411    const struct gl_format_info *info = _mesa_get_format_info(format);
412    if (info->ArrayFormat && !_mesa_little_endian() &&
413        info->Layout == MESA_FORMAT_LAYOUT_PACKED)
414       return _mesa_array_format_flip_channels(info->ArrayFormat);
415    else
416       return info->ArrayFormat;
417 }
418 
419 static struct hash_table *format_array_format_table;
420 static once_flag format_array_format_table_exists = ONCE_FLAG_INIT;
421 
422 static bool
array_formats_equal(const void * a,const void * b)423 array_formats_equal(const void *a, const void *b)
424 {
425    return (intptr_t)a == (intptr_t)b;
426 }
427 
428 static void
format_array_format_table_init(void)429 format_array_format_table_init(void)
430 {
431    const struct gl_format_info *info;
432    mesa_array_format array_format;
433    unsigned f;
434 
435    format_array_format_table = _mesa_hash_table_create(NULL, NULL,
436                                                        array_formats_equal);
437 
438    if (!format_array_format_table) {
439       _mesa_error_no_memory(__func__);
440       return;
441    }
442 
443    for (f = 1; f < MESA_FORMAT_COUNT; ++f) {
444       info = _mesa_get_format_info(f);
445       if (!info->ArrayFormat)
446          continue;
447 
448       if (_mesa_little_endian()) {
449          array_format = info->ArrayFormat;
450       } else {
451          array_format = _mesa_array_format_flip_channels(info->ArrayFormat);
452       }
453 
454       /* This can happen and does for some of the BGR formats.  Let's take
455        * the first one in the list.
456        */
457       if (_mesa_hash_table_search_pre_hashed(format_array_format_table,
458                                              array_format,
459                                              (void *)(intptr_t)array_format))
460          continue;
461 
462       _mesa_hash_table_insert_pre_hashed(format_array_format_table,
463                                          array_format,
464                                          (void *)(intptr_t)array_format,
465                                          (void *)(intptr_t)f);
466    }
467 }
468 
469 mesa_format
_mesa_format_from_array_format(uint32_t array_format)470 _mesa_format_from_array_format(uint32_t array_format)
471 {
472    struct hash_entry *entry;
473 
474    assert(_mesa_format_is_mesa_array_format(array_format));
475 
476    call_once(&format_array_format_table_exists, format_array_format_table_init);
477 
478    if (!format_array_format_table) {
479       static const once_flag once_flag_init = ONCE_FLAG_INIT;
480       format_array_format_table_exists = once_flag_init;
481       return MESA_FORMAT_NONE;
482    }
483 
484    entry = _mesa_hash_table_search_pre_hashed(format_array_format_table,
485                                               array_format,
486                                               (void *)(intptr_t)array_format);
487    if (entry)
488       return (intptr_t)entry->data;
489    else
490       return MESA_FORMAT_NONE;
491 }
492 
493 /** Is the given format a compressed format? */
494 GLboolean
_mesa_is_format_compressed(mesa_format format)495 _mesa_is_format_compressed(mesa_format format)
496 {
497    const struct gl_format_info *info = _mesa_get_format_info(format);
498    return info->BlockWidth > 1 || info->BlockHeight > 1;
499 }
500 
501 
502 /**
503  * Determine if the given format represents a packed depth/stencil buffer.
504  */
505 GLboolean
_mesa_is_format_packed_depth_stencil(mesa_format format)506 _mesa_is_format_packed_depth_stencil(mesa_format format)
507 {
508    const struct gl_format_info *info = _mesa_get_format_info(format);
509 
510    return info->BaseFormat == GL_DEPTH_STENCIL;
511 }
512 
513 
514 /**
515  * Is the given format a signed/unsigned integer color format?
516  */
517 GLboolean
_mesa_is_format_integer_color(mesa_format format)518 _mesa_is_format_integer_color(mesa_format format)
519 {
520    const struct gl_format_info *info = _mesa_get_format_info(format);
521    return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT) &&
522       info->BaseFormat != GL_DEPTH_COMPONENT &&
523       info->BaseFormat != GL_DEPTH_STENCIL &&
524       info->BaseFormat != GL_STENCIL_INDEX;
525 }
526 
527 
528 /**
529  * Is the given format an unsigned integer format?
530  */
531 GLboolean
_mesa_is_format_unsigned(mesa_format format)532 _mesa_is_format_unsigned(mesa_format format)
533 {
534    const struct gl_format_info *info = _mesa_get_format_info(format);
535    return _mesa_is_type_unsigned(info->DataType);
536 }
537 
538 
539 /**
540  * Does the given format store signed values?
541  */
542 GLboolean
_mesa_is_format_signed(mesa_format format)543 _mesa_is_format_signed(mesa_format format)
544 {
545    if (format == MESA_FORMAT_R11G11B10_FLOAT ||
546        format == MESA_FORMAT_R9G9B9E5_FLOAT) {
547       /* these packed float formats only store unsigned values */
548       return GL_FALSE;
549    }
550    else {
551       const struct gl_format_info *info = _mesa_get_format_info(format);
552       return (info->DataType == GL_SIGNED_NORMALIZED ||
553               info->DataType == GL_INT ||
554               info->DataType == GL_FLOAT);
555    }
556 }
557 
558 /**
559  * Is the given format an integer format?
560  */
561 GLboolean
_mesa_is_format_integer(mesa_format format)562 _mesa_is_format_integer(mesa_format format)
563 {
564    const struct gl_format_info *info = _mesa_get_format_info(format);
565    return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT);
566 }
567 
568 
569 /**
570  * Return true if the given format is a color format.
571  */
572 GLenum
_mesa_is_format_color_format(mesa_format format)573 _mesa_is_format_color_format(mesa_format format)
574 {
575    const struct gl_format_info *info = _mesa_get_format_info(format);
576    switch (info->BaseFormat) {
577    case GL_DEPTH_COMPONENT:
578    case GL_STENCIL_INDEX:
579    case GL_DEPTH_STENCIL:
580       return false;
581    default:
582       return true;
583    }
584 }
585 
586 
587 /**
588  * Return color encoding for given format.
589  * \return GL_LINEAR or GL_SRGB
590  */
591 GLenum
_mesa_get_format_color_encoding(mesa_format format)592 _mesa_get_format_color_encoding(mesa_format format)
593 {
594    const struct gl_format_info *info = _mesa_get_format_info(format);
595    return info->IsSRGBFormat ? GL_SRGB : GL_LINEAR;
596 }
597 
598 
599 /**
600  * Return TRUE if format is an ETC2 compressed format specified
601  * by GL_ARB_ES3_compatibility.
602  */
603 bool
_mesa_is_format_etc2(mesa_format format)604 _mesa_is_format_etc2(mesa_format format)
605 {
606    switch (format) {
607    case MESA_FORMAT_ETC2_RGB8:
608    case MESA_FORMAT_ETC2_SRGB8:
609    case MESA_FORMAT_ETC2_RGBA8_EAC:
610    case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
611    case MESA_FORMAT_ETC2_R11_EAC:
612    case MESA_FORMAT_ETC2_RG11_EAC:
613    case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
614    case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
615    case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
616    case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
617       return GL_TRUE;
618    default:
619       return GL_FALSE;
620    }
621 }
622 
623 
624 /**
625  * For an sRGB format, return the corresponding linear color space format.
626  * For non-sRGB formats, return the format as-is.
627  */
628 mesa_format
_mesa_get_srgb_format_linear(mesa_format format)629 _mesa_get_srgb_format_linear(mesa_format format)
630 {
631    switch (format) {
632    case MESA_FORMAT_BGR_SRGB8:
633       format = MESA_FORMAT_BGR_UNORM8;
634       break;
635    case MESA_FORMAT_A8B8G8R8_SRGB:
636       format = MESA_FORMAT_A8B8G8R8_UNORM;
637       break;
638    case MESA_FORMAT_B8G8R8A8_SRGB:
639       format = MESA_FORMAT_B8G8R8A8_UNORM;
640       break;
641    case MESA_FORMAT_A8R8G8B8_SRGB:
642       format = MESA_FORMAT_A8R8G8B8_UNORM;
643       break;
644    case MESA_FORMAT_R8G8B8A8_SRGB:
645       format = MESA_FORMAT_R8G8B8A8_UNORM;
646       break;
647    case MESA_FORMAT_L_SRGB8:
648       format = MESA_FORMAT_L_UNORM8;
649       break;
650    case MESA_FORMAT_L8A8_SRGB:
651       format = MESA_FORMAT_L8A8_UNORM;
652       break;
653    case MESA_FORMAT_A8L8_SRGB:
654       format = MESA_FORMAT_A8L8_UNORM;
655       break;
656    case MESA_FORMAT_SRGB_DXT1:
657       format = MESA_FORMAT_RGB_DXT1;
658       break;
659    case MESA_FORMAT_SRGBA_DXT1:
660       format = MESA_FORMAT_RGBA_DXT1;
661       break;
662    case MESA_FORMAT_SRGBA_DXT3:
663       format = MESA_FORMAT_RGBA_DXT3;
664       break;
665    case MESA_FORMAT_SRGBA_DXT5:
666       format = MESA_FORMAT_RGBA_DXT5;
667       break;
668    case MESA_FORMAT_R8G8B8X8_SRGB:
669       format = MESA_FORMAT_R8G8B8X8_UNORM;
670       break;
671    case MESA_FORMAT_X8B8G8R8_SRGB:
672       format = MESA_FORMAT_X8B8G8R8_UNORM;
673       break;
674    case MESA_FORMAT_ETC2_SRGB8:
675       format = MESA_FORMAT_ETC2_RGB8;
676       break;
677    case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
678       format = MESA_FORMAT_ETC2_RGBA8_EAC;
679       break;
680    case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
681       format = MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1;
682       break;
683    case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
684       format = MESA_FORMAT_BPTC_RGBA_UNORM;
685       break;
686    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4:
687       format = MESA_FORMAT_RGBA_ASTC_4x4;
688       break;
689    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4:
690       format = MESA_FORMAT_RGBA_ASTC_5x4;
691       break;
692    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5:
693       format = MESA_FORMAT_RGBA_ASTC_5x5;
694       break;
695    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5:
696       format = MESA_FORMAT_RGBA_ASTC_6x5;
697       break;
698    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6:
699       format = MESA_FORMAT_RGBA_ASTC_6x6;
700       break;
701    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5:
702       format = MESA_FORMAT_RGBA_ASTC_8x5;
703       break;
704    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6:
705       format = MESA_FORMAT_RGBA_ASTC_8x6;
706       break;
707    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8:
708       format = MESA_FORMAT_RGBA_ASTC_8x8;
709       break;
710    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5:
711       format = MESA_FORMAT_RGBA_ASTC_10x5;
712       break;
713    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6:
714       format = MESA_FORMAT_RGBA_ASTC_10x6;
715       break;
716    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8:
717       format = MESA_FORMAT_RGBA_ASTC_10x8;
718       break;
719    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10:
720       format = MESA_FORMAT_RGBA_ASTC_10x10;
721       break;
722    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10:
723       format = MESA_FORMAT_RGBA_ASTC_12x10;
724       break;
725    case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12:
726       format = MESA_FORMAT_RGBA_ASTC_12x12;
727       break;
728    case MESA_FORMAT_B8G8R8X8_SRGB:
729       format = MESA_FORMAT_B8G8R8X8_UNORM;
730       break;
731    case MESA_FORMAT_X8R8G8B8_SRGB:
732       format = MESA_FORMAT_X8R8G8B8_UNORM;
733       break;
734    default:
735       break;
736    }
737    return format;
738 }
739 
740 
741 /**
742  * If the given format is a compressed format, return a corresponding
743  * uncompressed format.
744  */
745 mesa_format
_mesa_get_uncompressed_format(mesa_format format)746 _mesa_get_uncompressed_format(mesa_format format)
747 {
748    switch (format) {
749    case MESA_FORMAT_RGB_FXT1:
750       return MESA_FORMAT_BGR_UNORM8;
751    case MESA_FORMAT_RGBA_FXT1:
752       return MESA_FORMAT_A8B8G8R8_UNORM;
753    case MESA_FORMAT_RGB_DXT1:
754    case MESA_FORMAT_SRGB_DXT1:
755       return MESA_FORMAT_BGR_UNORM8;
756    case MESA_FORMAT_RGBA_DXT1:
757    case MESA_FORMAT_SRGBA_DXT1:
758       return MESA_FORMAT_A8B8G8R8_UNORM;
759    case MESA_FORMAT_RGBA_DXT3:
760    case MESA_FORMAT_SRGBA_DXT3:
761       return MESA_FORMAT_A8B8G8R8_UNORM;
762    case MESA_FORMAT_RGBA_DXT5:
763    case MESA_FORMAT_SRGBA_DXT5:
764       return MESA_FORMAT_A8B8G8R8_UNORM;
765    case MESA_FORMAT_R_RGTC1_UNORM:
766       return MESA_FORMAT_R_UNORM8;
767    case MESA_FORMAT_R_RGTC1_SNORM:
768       return MESA_FORMAT_R_SNORM8;
769    case MESA_FORMAT_RG_RGTC2_UNORM:
770       return MESA_FORMAT_R8G8_UNORM;
771    case MESA_FORMAT_RG_RGTC2_SNORM:
772       return MESA_FORMAT_R8G8_SNORM;
773    case MESA_FORMAT_L_LATC1_UNORM:
774       return MESA_FORMAT_L_UNORM8;
775    case MESA_FORMAT_L_LATC1_SNORM:
776       return MESA_FORMAT_L_SNORM8;
777    case MESA_FORMAT_LA_LATC2_UNORM:
778       return MESA_FORMAT_L8A8_UNORM;
779    case MESA_FORMAT_LA_LATC2_SNORM:
780       return MESA_FORMAT_L8A8_SNORM;
781    case MESA_FORMAT_ETC1_RGB8:
782    case MESA_FORMAT_ETC2_RGB8:
783    case MESA_FORMAT_ETC2_SRGB8:
784       return MESA_FORMAT_BGR_UNORM8;
785    case MESA_FORMAT_ETC2_RGBA8_EAC:
786    case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
787    case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
788    case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
789       return MESA_FORMAT_A8B8G8R8_UNORM;
790    case MESA_FORMAT_ETC2_R11_EAC:
791    case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
792       return MESA_FORMAT_R_UNORM16;
793    case MESA_FORMAT_ETC2_RG11_EAC:
794    case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
795       return MESA_FORMAT_R16G16_UNORM;
796    case MESA_FORMAT_BPTC_RGBA_UNORM:
797    case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
798       return MESA_FORMAT_A8B8G8R8_UNORM;
799    case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
800    case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
801       return MESA_FORMAT_RGB_FLOAT32;
802    default:
803 #ifdef DEBUG
804       assert(!_mesa_is_format_compressed(format));
805 #endif
806       return format;
807    }
808 }
809 
810 
811 GLuint
_mesa_format_num_components(mesa_format format)812 _mesa_format_num_components(mesa_format format)
813 {
814    const struct gl_format_info *info = _mesa_get_format_info(format);
815    return ((info->RedBits > 0) +
816            (info->GreenBits > 0) +
817            (info->BlueBits > 0) +
818            (info->AlphaBits > 0) +
819            (info->LuminanceBits > 0) +
820            (info->IntensityBits > 0) +
821            (info->DepthBits > 0) +
822            (info->StencilBits > 0));
823 }
824 
825 
826 /**
827  * Returns true if a color format has data stored in the R/G/B/A channels,
828  * given an index from 0 to 3.
829  */
830 bool
_mesa_format_has_color_component(mesa_format format,int component)831 _mesa_format_has_color_component(mesa_format format, int component)
832 {
833    const struct gl_format_info *info = _mesa_get_format_info(format);
834 
835    assert(info->BaseFormat != GL_DEPTH_COMPONENT &&
836           info->BaseFormat != GL_DEPTH_STENCIL &&
837           info->BaseFormat != GL_STENCIL_INDEX);
838 
839    switch (component) {
840    case 0:
841       return (info->RedBits + info->IntensityBits + info->LuminanceBits) > 0;
842    case 1:
843       return (info->GreenBits + info->IntensityBits + info->LuminanceBits) > 0;
844    case 2:
845       return (info->BlueBits + info->IntensityBits + info->LuminanceBits) > 0;
846    case 3:
847       return (info->AlphaBits + info->IntensityBits) > 0;
848    default:
849       assert(!"Invalid color component: must be 0..3");
850       return false;
851    }
852 }
853 
854 
855 /**
856  * Return number of bytes needed to store an image of the given size
857  * in the given format.
858  */
859 GLuint
_mesa_format_image_size(mesa_format format,GLsizei width,GLsizei height,GLsizei depth)860 _mesa_format_image_size(mesa_format format, GLsizei width,
861                         GLsizei height, GLsizei depth)
862 {
863    const struct gl_format_info *info = _mesa_get_format_info(format);
864    GLuint sz;
865    /* Strictly speaking, a conditional isn't needed here */
866    if (info->BlockWidth > 1 || info->BlockHeight > 1 || info->BlockDepth > 1) {
867       /* compressed format (2D only for now) */
868       const GLuint bw = info->BlockWidth;
869       const GLuint bh = info->BlockHeight;
870       const GLuint bd = info->BlockDepth;
871       const GLuint wblocks = (width + bw - 1) / bw;
872       const GLuint hblocks = (height + bh - 1) / bh;
873       const GLuint dblocks = (depth + bd - 1) / bd;
874       sz = wblocks * hblocks * dblocks * info->BytesPerBlock;
875    } else
876       /* non-compressed */
877       sz = width * height * depth * info->BytesPerBlock;
878 
879    return sz;
880 }
881 
882 
883 /**
884  * Same as _mesa_format_image_size() but returns a 64-bit value to
885  * accommodate very large textures.
886  */
887 uint64_t
_mesa_format_image_size64(mesa_format format,GLsizei width,GLsizei height,GLsizei depth)888 _mesa_format_image_size64(mesa_format format, GLsizei width,
889                           GLsizei height, GLsizei depth)
890 {
891    const struct gl_format_info *info = _mesa_get_format_info(format);
892    uint64_t sz;
893    /* Strictly speaking, a conditional isn't needed here */
894    if (info->BlockWidth > 1 || info->BlockHeight > 1 || info->BlockDepth > 1) {
895       /* compressed format (2D only for now) */
896       const uint64_t bw = info->BlockWidth;
897       const uint64_t bh = info->BlockHeight;
898       const uint64_t bd = info->BlockDepth;
899       const uint64_t wblocks = (width + bw - 1) / bw;
900       const uint64_t hblocks = (height + bh - 1) / bh;
901       const uint64_t dblocks = (depth + bd - 1) / bd;
902       sz = wblocks * hblocks * dblocks * info->BytesPerBlock;
903    } else
904       /* non-compressed */
905       sz = ((uint64_t) width * (uint64_t) height *
906             (uint64_t) depth * info->BytesPerBlock);
907 
908    return sz;
909 }
910 
911 
912 
913 GLint
_mesa_format_row_stride(mesa_format format,GLsizei width)914 _mesa_format_row_stride(mesa_format format, GLsizei width)
915 {
916    const struct gl_format_info *info = _mesa_get_format_info(format);
917    /* Strictly speaking, a conditional isn't needed here */
918    if (info->BlockWidth > 1 || info->BlockHeight > 1) {
919       /* compressed format */
920       const GLuint bw = info->BlockWidth;
921       const GLuint wblocks = (width + bw - 1) / bw;
922       const GLint stride = wblocks * info->BytesPerBlock;
923       return stride;
924    }
925    else {
926       const GLint stride = width * info->BytesPerBlock;
927       return stride;
928    }
929 }
930 
931 
932 
933 /**
934  * Return datatype and number of components per texel for the given
935  * uncompressed mesa_format. Only used for mipmap generation code.
936  */
937 void
_mesa_uncompressed_format_to_type_and_comps(mesa_format format,GLenum * datatype,GLuint * comps)938 _mesa_uncompressed_format_to_type_and_comps(mesa_format format,
939                                GLenum *datatype, GLuint *comps)
940 {
941    switch (format) {
942    case MESA_FORMAT_A8B8G8R8_UNORM:
943    case MESA_FORMAT_R8G8B8A8_UNORM:
944    case MESA_FORMAT_B8G8R8A8_UNORM:
945    case MESA_FORMAT_A8R8G8B8_UNORM:
946    case MESA_FORMAT_X8B8G8R8_UNORM:
947    case MESA_FORMAT_R8G8B8X8_UNORM:
948    case MESA_FORMAT_B8G8R8X8_UNORM:
949    case MESA_FORMAT_X8R8G8B8_UNORM:
950    case MESA_FORMAT_A8B8G8R8_UINT:
951    case MESA_FORMAT_R8G8B8A8_UINT:
952    case MESA_FORMAT_B8G8R8A8_UINT:
953    case MESA_FORMAT_A8R8G8B8_UINT:
954       *datatype = GL_UNSIGNED_BYTE;
955       *comps = 4;
956       return;
957    case MESA_FORMAT_BGR_UNORM8:
958    case MESA_FORMAT_RGB_UNORM8:
959       *datatype = GL_UNSIGNED_BYTE;
960       *comps = 3;
961       return;
962    case MESA_FORMAT_B5G6R5_UNORM:
963    case MESA_FORMAT_R5G6B5_UNORM:
964    case MESA_FORMAT_B5G6R5_UINT:
965    case MESA_FORMAT_R5G6B5_UINT:
966       *datatype = GL_UNSIGNED_SHORT_5_6_5;
967       *comps = 3;
968       return;
969 
970    case MESA_FORMAT_B4G4R4A4_UNORM:
971    case MESA_FORMAT_A4R4G4B4_UNORM:
972    case MESA_FORMAT_B4G4R4X4_UNORM:
973    case MESA_FORMAT_B4G4R4A4_UINT:
974    case MESA_FORMAT_A4R4G4B4_UINT:
975       *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
976       *comps = 4;
977       return;
978 
979    case MESA_FORMAT_B5G5R5A1_UNORM:
980    case MESA_FORMAT_A1R5G5B5_UNORM:
981    case MESA_FORMAT_B5G5R5X1_UNORM:
982    case MESA_FORMAT_B5G5R5A1_UINT:
983    case MESA_FORMAT_A1R5G5B5_UINT:
984       *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
985       *comps = 4;
986       return;
987 
988    case MESA_FORMAT_B10G10R10A2_UNORM:
989       *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
990       *comps = 4;
991       return;
992 
993    case MESA_FORMAT_A1B5G5R5_UNORM:
994    case MESA_FORMAT_A1B5G5R5_UINT:
995       *datatype = GL_UNSIGNED_SHORT_5_5_5_1;
996       *comps = 4;
997       return;
998 
999    case MESA_FORMAT_L4A4_UNORM:
1000       *datatype = MESA_UNSIGNED_BYTE_4_4;
1001       *comps = 2;
1002       return;
1003 
1004    case MESA_FORMAT_L8A8_UNORM:
1005    case MESA_FORMAT_A8L8_UNORM:
1006    case MESA_FORMAT_R8G8_UNORM:
1007    case MESA_FORMAT_G8R8_UNORM:
1008       *datatype = GL_UNSIGNED_BYTE;
1009       *comps = 2;
1010       return;
1011 
1012    case MESA_FORMAT_L16A16_UNORM:
1013    case MESA_FORMAT_A16L16_UNORM:
1014    case MESA_FORMAT_R16G16_UNORM:
1015    case MESA_FORMAT_G16R16_UNORM:
1016       *datatype = GL_UNSIGNED_SHORT;
1017       *comps = 2;
1018       return;
1019 
1020    case MESA_FORMAT_R_UNORM16:
1021    case MESA_FORMAT_A_UNORM16:
1022    case MESA_FORMAT_L_UNORM16:
1023    case MESA_FORMAT_I_UNORM16:
1024       *datatype = GL_UNSIGNED_SHORT;
1025       *comps = 1;
1026       return;
1027 
1028    case MESA_FORMAT_R3G3B2_UNORM:
1029    case MESA_FORMAT_R3G3B2_UINT:
1030       *datatype = GL_UNSIGNED_BYTE_2_3_3_REV;
1031       *comps = 3;
1032       return;
1033    case MESA_FORMAT_A4B4G4R4_UNORM:
1034    case MESA_FORMAT_A4B4G4R4_UINT:
1035       *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
1036       *comps = 4;
1037       return;
1038 
1039    case MESA_FORMAT_R4G4B4A4_UNORM:
1040    case MESA_FORMAT_R4G4B4A4_UINT:
1041       *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
1042       *comps = 4;
1043       return;
1044    case MESA_FORMAT_R5G5B5A1_UNORM:
1045    case MESA_FORMAT_R5G5B5A1_UINT:
1046       *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1047       *comps = 4;
1048       return;
1049    case MESA_FORMAT_A2B10G10R10_UNORM:
1050    case MESA_FORMAT_A2B10G10R10_UINT:
1051       *datatype = GL_UNSIGNED_INT_10_10_10_2;
1052       *comps = 4;
1053       return;
1054    case MESA_FORMAT_A2R10G10B10_UNORM:
1055    case MESA_FORMAT_A2R10G10B10_UINT:
1056       *datatype = GL_UNSIGNED_INT_10_10_10_2;
1057       *comps = 4;
1058       return;
1059 
1060    case MESA_FORMAT_B2G3R3_UNORM:
1061    case MESA_FORMAT_B2G3R3_UINT:
1062       *datatype = GL_UNSIGNED_BYTE_3_3_2;
1063       *comps = 3;
1064       return;
1065 
1066    case MESA_FORMAT_A_UNORM8:
1067    case MESA_FORMAT_L_UNORM8:
1068    case MESA_FORMAT_I_UNORM8:
1069    case MESA_FORMAT_R_UNORM8:
1070    case MESA_FORMAT_S_UINT8:
1071       *datatype = GL_UNSIGNED_BYTE;
1072       *comps = 1;
1073       return;
1074 
1075    case MESA_FORMAT_YCBCR:
1076    case MESA_FORMAT_YCBCR_REV:
1077       *datatype = GL_UNSIGNED_SHORT;
1078       *comps = 2;
1079       return;
1080 
1081    case MESA_FORMAT_S8_UINT_Z24_UNORM:
1082       *datatype = GL_UNSIGNED_INT_24_8_MESA;
1083       *comps = 2;
1084       return;
1085 
1086    case MESA_FORMAT_Z24_UNORM_S8_UINT:
1087       *datatype = GL_UNSIGNED_INT_8_24_REV_MESA;
1088       *comps = 2;
1089       return;
1090 
1091    case MESA_FORMAT_Z_UNORM16:
1092       *datatype = GL_UNSIGNED_SHORT;
1093       *comps = 1;
1094       return;
1095 
1096    case MESA_FORMAT_Z24_UNORM_X8_UINT:
1097       *datatype = GL_UNSIGNED_INT;
1098       *comps = 1;
1099       return;
1100 
1101    case MESA_FORMAT_X8_UINT_Z24_UNORM:
1102       *datatype = GL_UNSIGNED_INT;
1103       *comps = 1;
1104       return;
1105 
1106    case MESA_FORMAT_Z_UNORM32:
1107       *datatype = GL_UNSIGNED_INT;
1108       *comps = 1;
1109       return;
1110 
1111    case MESA_FORMAT_Z_FLOAT32:
1112       *datatype = GL_FLOAT;
1113       *comps = 1;
1114       return;
1115 
1116    case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1117       *datatype = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
1118       *comps = 1;
1119       return;
1120 
1121    case MESA_FORMAT_R_SNORM8:
1122    case MESA_FORMAT_A_SNORM8:
1123    case MESA_FORMAT_L_SNORM8:
1124    case MESA_FORMAT_I_SNORM8:
1125       *datatype = GL_BYTE;
1126       *comps = 1;
1127       return;
1128    case MESA_FORMAT_R8G8_SNORM:
1129    case MESA_FORMAT_L8A8_SNORM:
1130    case MESA_FORMAT_A8L8_SNORM:
1131       *datatype = GL_BYTE;
1132       *comps = 2;
1133       return;
1134    case MESA_FORMAT_A8B8G8R8_SNORM:
1135    case MESA_FORMAT_R8G8B8A8_SNORM:
1136    case MESA_FORMAT_X8B8G8R8_SNORM:
1137       *datatype = GL_BYTE;
1138       *comps = 4;
1139       return;
1140 
1141    case MESA_FORMAT_RGBA_UNORM16:
1142       *datatype = GL_UNSIGNED_SHORT;
1143       *comps = 4;
1144       return;
1145 
1146    case MESA_FORMAT_R_SNORM16:
1147    case MESA_FORMAT_A_SNORM16:
1148    case MESA_FORMAT_L_SNORM16:
1149    case MESA_FORMAT_I_SNORM16:
1150       *datatype = GL_SHORT;
1151       *comps = 1;
1152       return;
1153    case MESA_FORMAT_R16G16_SNORM:
1154    case MESA_FORMAT_LA_SNORM16:
1155       *datatype = GL_SHORT;
1156       *comps = 2;
1157       return;
1158    case MESA_FORMAT_RGB_SNORM16:
1159       *datatype = GL_SHORT;
1160       *comps = 3;
1161       return;
1162    case MESA_FORMAT_RGBA_SNORM16:
1163       *datatype = GL_SHORT;
1164       *comps = 4;
1165       return;
1166 
1167    case MESA_FORMAT_BGR_SRGB8:
1168       *datatype = GL_UNSIGNED_BYTE;
1169       *comps = 3;
1170       return;
1171    case MESA_FORMAT_A8B8G8R8_SRGB:
1172    case MESA_FORMAT_B8G8R8A8_SRGB:
1173    case MESA_FORMAT_A8R8G8B8_SRGB:
1174    case MESA_FORMAT_R8G8B8A8_SRGB:
1175       *datatype = GL_UNSIGNED_BYTE;
1176       *comps = 4;
1177       return;
1178    case MESA_FORMAT_L_SRGB8:
1179       *datatype = GL_UNSIGNED_BYTE;
1180       *comps = 1;
1181       return;
1182    case MESA_FORMAT_L8A8_SRGB:
1183    case MESA_FORMAT_A8L8_SRGB:
1184       *datatype = GL_UNSIGNED_BYTE;
1185       *comps = 2;
1186       return;
1187 
1188    case MESA_FORMAT_RGBA_FLOAT32:
1189       *datatype = GL_FLOAT;
1190       *comps = 4;
1191       return;
1192    case MESA_FORMAT_RGBA_FLOAT16:
1193       *datatype = GL_HALF_FLOAT_ARB;
1194       *comps = 4;
1195       return;
1196    case MESA_FORMAT_RGB_FLOAT32:
1197       *datatype = GL_FLOAT;
1198       *comps = 3;
1199       return;
1200    case MESA_FORMAT_RGB_FLOAT16:
1201       *datatype = GL_HALF_FLOAT_ARB;
1202       *comps = 3;
1203       return;
1204    case MESA_FORMAT_LA_FLOAT32:
1205    case MESA_FORMAT_RG_FLOAT32:
1206       *datatype = GL_FLOAT;
1207       *comps = 2;
1208       return;
1209    case MESA_FORMAT_LA_FLOAT16:
1210    case MESA_FORMAT_RG_FLOAT16:
1211       *datatype = GL_HALF_FLOAT_ARB;
1212       *comps = 2;
1213       return;
1214    case MESA_FORMAT_A_FLOAT32:
1215    case MESA_FORMAT_L_FLOAT32:
1216    case MESA_FORMAT_I_FLOAT32:
1217    case MESA_FORMAT_R_FLOAT32:
1218       *datatype = GL_FLOAT;
1219       *comps = 1;
1220       return;
1221    case MESA_FORMAT_A_FLOAT16:
1222    case MESA_FORMAT_L_FLOAT16:
1223    case MESA_FORMAT_I_FLOAT16:
1224    case MESA_FORMAT_R_FLOAT16:
1225       *datatype = GL_HALF_FLOAT_ARB;
1226       *comps = 1;
1227       return;
1228 
1229    case MESA_FORMAT_A_UINT8:
1230    case MESA_FORMAT_L_UINT8:
1231    case MESA_FORMAT_I_UINT8:
1232       *datatype = GL_UNSIGNED_BYTE;
1233       *comps = 1;
1234       return;
1235    case MESA_FORMAT_LA_UINT8:
1236       *datatype = GL_UNSIGNED_BYTE;
1237       *comps = 2;
1238       return;
1239 
1240    case MESA_FORMAT_A_UINT16:
1241    case MESA_FORMAT_L_UINT16:
1242    case MESA_FORMAT_I_UINT16:
1243       *datatype = GL_UNSIGNED_SHORT;
1244       *comps = 1;
1245       return;
1246    case MESA_FORMAT_LA_UINT16:
1247       *datatype = GL_UNSIGNED_SHORT;
1248       *comps = 2;
1249       return;
1250    case MESA_FORMAT_A_UINT32:
1251    case MESA_FORMAT_L_UINT32:
1252    case MESA_FORMAT_I_UINT32:
1253       *datatype = GL_UNSIGNED_INT;
1254       *comps = 1;
1255       return;
1256    case MESA_FORMAT_LA_UINT32:
1257       *datatype = GL_UNSIGNED_INT;
1258       *comps = 2;
1259       return;
1260    case MESA_FORMAT_A_SINT8:
1261    case MESA_FORMAT_L_SINT8:
1262    case MESA_FORMAT_I_SINT8:
1263       *datatype = GL_BYTE;
1264       *comps = 1;
1265       return;
1266    case MESA_FORMAT_LA_SINT8:
1267       *datatype = GL_BYTE;
1268       *comps = 2;
1269       return;
1270 
1271    case MESA_FORMAT_A_SINT16:
1272    case MESA_FORMAT_L_SINT16:
1273    case MESA_FORMAT_I_SINT16:
1274       *datatype = GL_SHORT;
1275       *comps = 1;
1276       return;
1277    case MESA_FORMAT_LA_SINT16:
1278       *datatype = GL_SHORT;
1279       *comps = 2;
1280       return;
1281 
1282    case MESA_FORMAT_A_SINT32:
1283    case MESA_FORMAT_L_SINT32:
1284    case MESA_FORMAT_I_SINT32:
1285       *datatype = GL_INT;
1286       *comps = 1;
1287       return;
1288    case MESA_FORMAT_LA_SINT32:
1289       *datatype = GL_INT;
1290       *comps = 2;
1291       return;
1292 
1293    case MESA_FORMAT_R_SINT8:
1294       *datatype = GL_BYTE;
1295       *comps = 1;
1296       return;
1297    case MESA_FORMAT_RG_SINT8:
1298       *datatype = GL_BYTE;
1299       *comps = 2;
1300       return;
1301    case MESA_FORMAT_RGB_SINT8:
1302       *datatype = GL_BYTE;
1303       *comps = 3;
1304       return;
1305    case MESA_FORMAT_RGBA_SINT8:
1306       *datatype = GL_BYTE;
1307       *comps = 4;
1308       return;
1309    case MESA_FORMAT_R_SINT16:
1310       *datatype = GL_SHORT;
1311       *comps = 1;
1312       return;
1313    case MESA_FORMAT_RG_SINT16:
1314       *datatype = GL_SHORT;
1315       *comps = 2;
1316       return;
1317    case MESA_FORMAT_RGB_SINT16:
1318       *datatype = GL_SHORT;
1319       *comps = 3;
1320       return;
1321    case MESA_FORMAT_RGBA_SINT16:
1322       *datatype = GL_SHORT;
1323       *comps = 4;
1324       return;
1325    case MESA_FORMAT_R_SINT32:
1326       *datatype = GL_INT;
1327       *comps = 1;
1328       return;
1329    case MESA_FORMAT_RG_SINT32:
1330       *datatype = GL_INT;
1331       *comps = 2;
1332       return;
1333    case MESA_FORMAT_RGB_SINT32:
1334       *datatype = GL_INT;
1335       *comps = 3;
1336       return;
1337    case MESA_FORMAT_RGBA_SINT32:
1338       *datatype = GL_INT;
1339       *comps = 4;
1340       return;
1341 
1342    /**
1343     * \name Non-normalized unsigned integer formats.
1344     */
1345    case MESA_FORMAT_R_UINT8:
1346       *datatype = GL_UNSIGNED_BYTE;
1347       *comps = 1;
1348       return;
1349    case MESA_FORMAT_RG_UINT8:
1350       *datatype = GL_UNSIGNED_BYTE;
1351       *comps = 2;
1352       return;
1353    case MESA_FORMAT_RGB_UINT8:
1354       *datatype = GL_UNSIGNED_BYTE;
1355       *comps = 3;
1356       return;
1357    case MESA_FORMAT_RGBA_UINT8:
1358       *datatype = GL_UNSIGNED_BYTE;
1359       *comps = 4;
1360       return;
1361    case MESA_FORMAT_R_UINT16:
1362       *datatype = GL_UNSIGNED_SHORT;
1363       *comps = 1;
1364       return;
1365    case MESA_FORMAT_RG_UINT16:
1366       *datatype = GL_UNSIGNED_SHORT;
1367       *comps = 2;
1368       return;
1369    case MESA_FORMAT_RGB_UINT16:
1370       *datatype = GL_UNSIGNED_SHORT;
1371       *comps = 3;
1372       return;
1373    case MESA_FORMAT_RGBA_UINT16:
1374       *datatype = GL_UNSIGNED_SHORT;
1375       *comps = 4;
1376       return;
1377    case MESA_FORMAT_R_UINT32:
1378       *datatype = GL_UNSIGNED_INT;
1379       *comps = 1;
1380       return;
1381    case MESA_FORMAT_RG_UINT32:
1382       *datatype = GL_UNSIGNED_INT;
1383       *comps = 2;
1384       return;
1385    case MESA_FORMAT_RGB_UINT32:
1386       *datatype = GL_UNSIGNED_INT;
1387       *comps = 3;
1388       return;
1389    case MESA_FORMAT_RGBA_UINT32:
1390       *datatype = GL_UNSIGNED_INT;
1391       *comps = 4;
1392       return;
1393 
1394    case MESA_FORMAT_R9G9B9E5_FLOAT:
1395       *datatype = GL_UNSIGNED_INT_5_9_9_9_REV;
1396       *comps = 3;
1397       return;
1398 
1399    case MESA_FORMAT_R11G11B10_FLOAT:
1400       *datatype = GL_UNSIGNED_INT_10F_11F_11F_REV;
1401       *comps = 3;
1402       return;
1403 
1404    case MESA_FORMAT_B10G10R10A2_UINT:
1405    case MESA_FORMAT_R10G10B10A2_UINT:
1406       *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1407       *comps = 4;
1408       return;
1409 
1410    case MESA_FORMAT_R8G8B8X8_SRGB:
1411    case MESA_FORMAT_X8B8G8R8_SRGB:
1412    case MESA_FORMAT_RGBX_UINT8:
1413       *datatype = GL_UNSIGNED_BYTE;
1414       *comps = 4;
1415       return;
1416 
1417    case MESA_FORMAT_R8G8B8X8_SNORM:
1418    case MESA_FORMAT_RGBX_SINT8:
1419       *datatype = GL_BYTE;
1420       *comps = 4;
1421       return;
1422 
1423    case MESA_FORMAT_B10G10R10X2_UNORM:
1424    case MESA_FORMAT_R10G10B10X2_UNORM:
1425       *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1426       *comps = 4;
1427       return;
1428 
1429    case MESA_FORMAT_RGBX_UNORM16:
1430    case MESA_FORMAT_RGBX_UINT16:
1431       *datatype = GL_UNSIGNED_SHORT;
1432       *comps = 4;
1433       return;
1434 
1435    case MESA_FORMAT_RGBX_SNORM16:
1436    case MESA_FORMAT_RGBX_SINT16:
1437       *datatype = GL_SHORT;
1438       *comps = 4;
1439       return;
1440 
1441    case MESA_FORMAT_RGBX_FLOAT16:
1442       *datatype = GL_HALF_FLOAT;
1443       *comps = 4;
1444       return;
1445 
1446    case MESA_FORMAT_RGBX_FLOAT32:
1447       *datatype = GL_FLOAT;
1448       *comps = 4;
1449       return;
1450 
1451    case MESA_FORMAT_RGBX_UINT32:
1452       *datatype = GL_UNSIGNED_INT;
1453       *comps = 4;
1454       return;
1455 
1456    case MESA_FORMAT_RGBX_SINT32:
1457       *datatype = GL_INT;
1458       *comps = 4;
1459       return;
1460 
1461    case MESA_FORMAT_R10G10B10A2_UNORM:
1462       *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1463       *comps = 4;
1464       return;
1465 
1466    case MESA_FORMAT_G8R8_SNORM:
1467       *datatype = GL_BYTE;
1468       *comps = 2;
1469       return;
1470 
1471    case MESA_FORMAT_G16R16_SNORM:
1472       *datatype = GL_SHORT;
1473       *comps = 2;
1474       return;
1475 
1476    case MESA_FORMAT_B8G8R8X8_SRGB:
1477    case MESA_FORMAT_X8R8G8B8_SRGB:
1478       *datatype = GL_UNSIGNED_BYTE;
1479       *comps = 4;
1480       return;
1481 
1482    case MESA_FORMAT_COUNT:
1483       assert(0);
1484       return;
1485    default:
1486       /* Warn if any formats are not handled */
1487       _mesa_problem(NULL, "bad format %s in _mesa_uncompressed_format_to_type_and_comps",
1488                     _mesa_get_format_name(format));
1489       assert(format == MESA_FORMAT_NONE ||
1490              _mesa_is_format_compressed(format));
1491       *datatype = 0;
1492       *comps = 1;
1493    }
1494 }
1495 
1496 /**
1497  * Check if a mesa_format exactly matches a GL format/type combination
1498  * such that we can use memcpy() from one to the other.
1499  * \param mesa_format  a MESA_FORMAT_x value
1500  * \param format  the user-specified image format
1501  * \param type  the user-specified image datatype
1502  * \param swapBytes  typically the current pixel pack/unpack byteswap state
1503  * \param[out] error GL_NO_ERROR if format is an expected input.
1504  *                   GL_INVALID_ENUM if format is an unexpected input.
1505  * \return GL_TRUE if the formats match, GL_FALSE otherwise.
1506  */
1507 GLboolean
_mesa_format_matches_format_and_type(mesa_format mesa_format,GLenum format,GLenum type,GLboolean swapBytes,GLenum * error)1508 _mesa_format_matches_format_and_type(mesa_format mesa_format,
1509 				     GLenum format, GLenum type,
1510 				     GLboolean swapBytes, GLenum *error)
1511 {
1512    const GLboolean littleEndian = _mesa_little_endian();
1513    if (error)
1514       *error = GL_NO_ERROR;
1515 
1516    /* Note: When reading a GL format/type combination, the format lists channel
1517     * assignments from most significant channel in the type to least
1518     * significant.  A type with _REV indicates that the assignments are
1519     * swapped, so they are listed from least significant to most significant.
1520     *
1521     * Compressed formats will fall through and return GL_FALSE.
1522     *
1523     * For sanity, please keep this switch statement ordered the same as the
1524     * enums in formats.h.
1525     */
1526 
1527    switch (mesa_format) {
1528 
1529    case MESA_FORMAT_NONE:
1530    case MESA_FORMAT_COUNT:
1531       return GL_FALSE;
1532 
1533    case MESA_FORMAT_A8B8G8R8_UNORM:
1534    case MESA_FORMAT_A8B8G8R8_SRGB:
1535       if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
1536          return GL_TRUE;
1537 
1538       if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8_REV && swapBytes)
1539          return GL_TRUE;
1540 
1541       if (format == GL_RGBA && type == GL_UNSIGNED_BYTE && !littleEndian)
1542          return GL_TRUE;
1543 
1544       if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8_REV
1545           && !swapBytes)
1546          return GL_TRUE;
1547 
1548       if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8
1549           && swapBytes)
1550          return GL_TRUE;
1551 
1552       if (format == GL_ABGR_EXT && type == GL_UNSIGNED_BYTE && littleEndian)
1553          return GL_TRUE;
1554 
1555       return GL_FALSE;
1556 
1557    case MESA_FORMAT_R8G8B8A8_UNORM:
1558    case MESA_FORMAT_R8G8B8A8_SRGB:
1559       if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1560           !swapBytes)
1561          return GL_TRUE;
1562 
1563       if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1564          return GL_TRUE;
1565 
1566       if (format == GL_RGBA && type == GL_UNSIGNED_BYTE && littleEndian)
1567          return GL_TRUE;
1568 
1569       if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8 &&
1570           !swapBytes)
1571          return GL_TRUE;
1572 
1573       if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1574           swapBytes)
1575          return GL_TRUE;
1576 
1577       if (format == GL_ABGR_EXT && type == GL_UNSIGNED_BYTE && !littleEndian)
1578          return GL_TRUE;
1579 
1580       return GL_FALSE;
1581 
1582    case MESA_FORMAT_B8G8R8A8_UNORM:
1583    case MESA_FORMAT_B8G8R8A8_SRGB:
1584       if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1585           !swapBytes)
1586          return GL_TRUE;
1587 
1588       if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1589          return GL_TRUE;
1590 
1591       if (format == GL_BGRA && type == GL_UNSIGNED_BYTE && littleEndian)
1592          return GL_TRUE;
1593 
1594       return GL_FALSE;
1595 
1596    case MESA_FORMAT_A8R8G8B8_UNORM:
1597    case MESA_FORMAT_A8R8G8B8_SRGB:
1598       if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
1599          return GL_TRUE;
1600 
1601       if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1602           swapBytes)
1603          return GL_TRUE;
1604 
1605       if (format == GL_BGRA && type == GL_UNSIGNED_BYTE && !littleEndian)
1606          return GL_TRUE;
1607 
1608       return GL_FALSE;
1609 
1610    case MESA_FORMAT_X8B8G8R8_UNORM:
1611    case MESA_FORMAT_R8G8B8X8_UNORM:
1612       return GL_FALSE;
1613 
1614    case MESA_FORMAT_B8G8R8X8_UNORM:
1615    case MESA_FORMAT_X8R8G8B8_UNORM:
1616       return GL_FALSE;
1617 
1618    case MESA_FORMAT_BGR_UNORM8:
1619    case MESA_FORMAT_BGR_SRGB8:
1620       return format == GL_BGR && type == GL_UNSIGNED_BYTE && littleEndian;
1621 
1622    case MESA_FORMAT_RGB_UNORM8:
1623       return format == GL_RGB && type == GL_UNSIGNED_BYTE && littleEndian;
1624 
1625    case MESA_FORMAT_B5G6R5_UNORM:
1626       return ((format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5) ||
1627               (format == GL_BGR && type == GL_UNSIGNED_SHORT_5_6_5_REV)) &&
1628               !swapBytes;
1629 
1630    case MESA_FORMAT_R5G6B5_UNORM:
1631       return ((format == GL_BGR && type == GL_UNSIGNED_SHORT_5_6_5) ||
1632               (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5_REV)) &&
1633               !swapBytes;
1634 
1635    case MESA_FORMAT_B4G4R4A4_UNORM:
1636       return format == GL_BGRA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV &&
1637          !swapBytes;
1638 
1639    case MESA_FORMAT_A4R4G4B4_UNORM:
1640       return GL_FALSE;
1641 
1642    case MESA_FORMAT_A1B5G5R5_UNORM:
1643       return format == GL_RGBA && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1644          !swapBytes;
1645 
1646    case MESA_FORMAT_B5G5R5A1_UNORM:
1647       return format == GL_BGRA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV &&
1648          !swapBytes;
1649 
1650    case MESA_FORMAT_A1R5G5B5_UNORM:
1651       return format == GL_BGRA && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1652          !swapBytes;
1653 
1654    case MESA_FORMAT_L4A4_UNORM:
1655       return GL_FALSE;
1656    case MESA_FORMAT_L8A8_UNORM:
1657    case MESA_FORMAT_L8A8_SRGB:
1658       return format == GL_LUMINANCE_ALPHA && type == GL_UNSIGNED_BYTE && littleEndian;
1659    case MESA_FORMAT_A8L8_UNORM:
1660    case MESA_FORMAT_A8L8_SRGB:
1661       return GL_FALSE;
1662 
1663    case MESA_FORMAT_L16A16_UNORM:
1664       return format == GL_LUMINANCE_ALPHA && type == GL_UNSIGNED_SHORT && littleEndian && !swapBytes;
1665    case MESA_FORMAT_A16L16_UNORM:
1666       return GL_FALSE;
1667 
1668    case MESA_FORMAT_B2G3R3_UNORM:
1669       return format == GL_RGB && type == GL_UNSIGNED_BYTE_3_3_2;
1670 
1671    case MESA_FORMAT_R3G3B2_UNORM:
1672       return format == GL_RGB && type == GL_UNSIGNED_BYTE_2_3_3_REV;
1673 
1674    case MESA_FORMAT_A4B4G4R4_UNORM:
1675       if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
1676          return GL_TRUE;
1677 
1678       if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && swapBytes)
1679          return GL_TRUE;
1680 
1681       if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !swapBytes)
1682          return GL_TRUE;
1683 
1684       if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4 && swapBytes)
1685          return GL_TRUE;
1686 
1687       return GL_FALSE;
1688 
1689    case MESA_FORMAT_R4G4B4A4_UNORM:
1690       if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
1691          return GL_TRUE;
1692 
1693       if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && swapBytes)
1694          return GL_TRUE;
1695 
1696       if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !swapBytes)
1697          return GL_TRUE;
1698 
1699       if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4 && swapBytes)
1700          return GL_TRUE;
1701 
1702       return GL_FALSE;
1703 
1704    case MESA_FORMAT_R5G5B5A1_UNORM:
1705       return format == GL_RGBA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV;
1706 
1707    case MESA_FORMAT_A2B10G10R10_UNORM:
1708       return format == GL_RGBA && type == GL_UNSIGNED_INT_10_10_10_2;
1709 
1710    case MESA_FORMAT_A2B10G10R10_UINT:
1711       return format == GL_RGBA_INTEGER_EXT && type == GL_UNSIGNED_INT_10_10_10_2;
1712 
1713    case MESA_FORMAT_A2R10G10B10_UNORM:
1714       return format == GL_BGRA && type == GL_UNSIGNED_INT_10_10_10_2;
1715 
1716    case MESA_FORMAT_A2R10G10B10_UINT:
1717       return format == GL_BGRA_INTEGER_EXT && type == GL_UNSIGNED_INT_10_10_10_2;
1718 
1719    case MESA_FORMAT_A_UNORM8:
1720       return format == GL_ALPHA && type == GL_UNSIGNED_BYTE;
1721    case MESA_FORMAT_A_UNORM16:
1722       return format == GL_ALPHA && type == GL_UNSIGNED_SHORT && !swapBytes;
1723    case MESA_FORMAT_L_UNORM8:
1724    case MESA_FORMAT_L_SRGB8:
1725       return format == GL_LUMINANCE && type == GL_UNSIGNED_BYTE;
1726    case MESA_FORMAT_L_UNORM16:
1727       return format == GL_LUMINANCE && type == GL_UNSIGNED_SHORT && !swapBytes;
1728    case MESA_FORMAT_I_UNORM8:
1729       return format == GL_RED && type == GL_UNSIGNED_BYTE;
1730    case MESA_FORMAT_I_UNORM16:
1731       return format == GL_RED && type == GL_UNSIGNED_SHORT && !swapBytes;
1732 
1733    case MESA_FORMAT_YCBCR:
1734       return format == GL_YCBCR_MESA &&
1735              ((type == GL_UNSIGNED_SHORT_8_8_MESA && littleEndian != swapBytes) ||
1736               (type == GL_UNSIGNED_SHORT_8_8_REV_MESA && littleEndian == swapBytes));
1737    case MESA_FORMAT_YCBCR_REV:
1738       return format == GL_YCBCR_MESA &&
1739              ((type == GL_UNSIGNED_SHORT_8_8_MESA && littleEndian == swapBytes) ||
1740               (type == GL_UNSIGNED_SHORT_8_8_REV_MESA && littleEndian != swapBytes));
1741 
1742    case MESA_FORMAT_R_UNORM8:
1743       return format == GL_RED && type == GL_UNSIGNED_BYTE;
1744    case MESA_FORMAT_R8G8_UNORM:
1745       return format == GL_RG && type == GL_UNSIGNED_BYTE && littleEndian;
1746    case MESA_FORMAT_G8R8_UNORM:
1747       return GL_FALSE;
1748 
1749    case MESA_FORMAT_R_UNORM16:
1750       return format == GL_RED && type == GL_UNSIGNED_SHORT &&
1751          !swapBytes;
1752    case MESA_FORMAT_R16G16_UNORM:
1753       return format == GL_RG && type == GL_UNSIGNED_SHORT && littleEndian &&
1754          !swapBytes;
1755    case MESA_FORMAT_G16R16_UNORM:
1756       return GL_FALSE;
1757 
1758    case MESA_FORMAT_B10G10R10A2_UNORM:
1759       return format == GL_BGRA && type == GL_UNSIGNED_INT_2_10_10_10_REV &&
1760          !swapBytes;
1761 
1762    case MESA_FORMAT_S8_UINT_Z24_UNORM:
1763       return format == GL_DEPTH_STENCIL && type == GL_UNSIGNED_INT_24_8 &&
1764          !swapBytes;
1765    case MESA_FORMAT_X8_UINT_Z24_UNORM:
1766    case MESA_FORMAT_Z24_UNORM_S8_UINT:
1767       return GL_FALSE;
1768 
1769    case MESA_FORMAT_Z_UNORM16:
1770       return format == GL_DEPTH_COMPONENT && type == GL_UNSIGNED_SHORT &&
1771          !swapBytes;
1772 
1773    case MESA_FORMAT_Z24_UNORM_X8_UINT:
1774       return GL_FALSE;
1775 
1776    case MESA_FORMAT_Z_UNORM32:
1777       return format == GL_DEPTH_COMPONENT && type == GL_UNSIGNED_INT &&
1778          !swapBytes;
1779 
1780    case MESA_FORMAT_S_UINT8:
1781       return format == GL_STENCIL_INDEX && type == GL_UNSIGNED_BYTE;
1782 
1783    case MESA_FORMAT_RGBA_FLOAT32:
1784       return format == GL_RGBA && type == GL_FLOAT && !swapBytes;
1785    case MESA_FORMAT_RGBA_FLOAT16:
1786       return format == GL_RGBA && type == GL_HALF_FLOAT && !swapBytes;
1787 
1788    case MESA_FORMAT_RGB_FLOAT32:
1789       return format == GL_RGB && type == GL_FLOAT && !swapBytes;
1790    case MESA_FORMAT_RGB_FLOAT16:
1791       return format == GL_RGB && type == GL_HALF_FLOAT && !swapBytes;
1792 
1793    case MESA_FORMAT_A_FLOAT32:
1794       return format == GL_ALPHA && type == GL_FLOAT && !swapBytes;
1795    case MESA_FORMAT_A_FLOAT16:
1796       return format == GL_ALPHA && type == GL_HALF_FLOAT && !swapBytes;
1797 
1798    case MESA_FORMAT_L_FLOAT32:
1799       return format == GL_LUMINANCE && type == GL_FLOAT && !swapBytes;
1800    case MESA_FORMAT_L_FLOAT16:
1801       return format == GL_LUMINANCE && type == GL_HALF_FLOAT && !swapBytes;
1802 
1803    case MESA_FORMAT_LA_FLOAT32:
1804       return format == GL_LUMINANCE_ALPHA && type == GL_FLOAT && !swapBytes;
1805    case MESA_FORMAT_LA_FLOAT16:
1806       return format == GL_LUMINANCE_ALPHA && type == GL_HALF_FLOAT && !swapBytes;
1807 
1808    case MESA_FORMAT_I_FLOAT32:
1809       return format == GL_RED && type == GL_FLOAT && !swapBytes;
1810    case MESA_FORMAT_I_FLOAT16:
1811       return format == GL_RED && type == GL_HALF_FLOAT && !swapBytes;
1812 
1813    case MESA_FORMAT_R_FLOAT32:
1814       return format == GL_RED && type == GL_FLOAT && !swapBytes;
1815    case MESA_FORMAT_R_FLOAT16:
1816       return format == GL_RED && type == GL_HALF_FLOAT && !swapBytes;
1817 
1818    case MESA_FORMAT_RG_FLOAT32:
1819       return format == GL_RG && type == GL_FLOAT && !swapBytes;
1820    case MESA_FORMAT_RG_FLOAT16:
1821       return format == GL_RG && type == GL_HALF_FLOAT && !swapBytes;
1822 
1823    case MESA_FORMAT_A_UINT8:
1824       return format == GL_ALPHA_INTEGER && type == GL_UNSIGNED_BYTE;
1825    case MESA_FORMAT_A_UINT16:
1826       return format == GL_ALPHA_INTEGER && type == GL_UNSIGNED_SHORT &&
1827              !swapBytes;
1828    case MESA_FORMAT_A_UINT32:
1829       return format == GL_ALPHA_INTEGER && type == GL_UNSIGNED_INT &&
1830              !swapBytes;
1831    case MESA_FORMAT_A_SINT8:
1832       return format == GL_ALPHA_INTEGER && type == GL_BYTE;
1833    case MESA_FORMAT_A_SINT16:
1834       return format == GL_ALPHA_INTEGER && type == GL_SHORT && !swapBytes;
1835    case MESA_FORMAT_A_SINT32:
1836       return format == GL_ALPHA_INTEGER && type == GL_INT && !swapBytes;
1837 
1838    case MESA_FORMAT_I_UINT8:
1839       return format == GL_RED_INTEGER && type == GL_UNSIGNED_BYTE;
1840    case MESA_FORMAT_I_UINT16:
1841       return format == GL_RED_INTEGER && type == GL_UNSIGNED_SHORT && !swapBytes;
1842    case MESA_FORMAT_I_UINT32:
1843       return format == GL_RED_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1844    case MESA_FORMAT_I_SINT8:
1845       return format == GL_RED_INTEGER && type == GL_BYTE;
1846    case MESA_FORMAT_I_SINT16:
1847       return format == GL_RED_INTEGER && type == GL_SHORT && !swapBytes;
1848    case MESA_FORMAT_I_SINT32:
1849       return format == GL_RED_INTEGER && type == GL_INT && !swapBytes;
1850 
1851    case MESA_FORMAT_L_UINT8:
1852       return format == GL_LUMINANCE_INTEGER_EXT && type == GL_UNSIGNED_BYTE;
1853    case MESA_FORMAT_L_UINT16:
1854       return format == GL_LUMINANCE_INTEGER_EXT && type == GL_UNSIGNED_SHORT &&
1855              !swapBytes;
1856    case MESA_FORMAT_L_UINT32:
1857       return format == GL_LUMINANCE_INTEGER_EXT && type == GL_UNSIGNED_INT &&
1858              !swapBytes;
1859    case MESA_FORMAT_L_SINT8:
1860       return format == GL_LUMINANCE_INTEGER_EXT && type == GL_BYTE;
1861    case MESA_FORMAT_L_SINT16:
1862       return format == GL_LUMINANCE_INTEGER_EXT && type == GL_SHORT &&
1863              !swapBytes;
1864    case MESA_FORMAT_L_SINT32:
1865       return format == GL_LUMINANCE_INTEGER_EXT && type == GL_INT && !swapBytes;
1866 
1867    case MESA_FORMAT_LA_UINT8:
1868       return format == GL_LUMINANCE_ALPHA_INTEGER_EXT &&
1869              type == GL_UNSIGNED_BYTE && !swapBytes;
1870    case MESA_FORMAT_LA_UINT16:
1871       return format == GL_LUMINANCE_ALPHA_INTEGER_EXT &&
1872              type == GL_UNSIGNED_SHORT && !swapBytes;
1873    case MESA_FORMAT_LA_UINT32:
1874       return format == GL_LUMINANCE_ALPHA_INTEGER_EXT &&
1875              type == GL_UNSIGNED_INT && !swapBytes;
1876    case MESA_FORMAT_LA_SINT8:
1877       return format == GL_LUMINANCE_ALPHA_INTEGER_EXT && type == GL_BYTE &&
1878              !swapBytes;
1879    case MESA_FORMAT_LA_SINT16:
1880       return format == GL_LUMINANCE_ALPHA_INTEGER_EXT && type == GL_SHORT &&
1881              !swapBytes;
1882    case MESA_FORMAT_LA_SINT32:
1883       return format == GL_LUMINANCE_ALPHA_INTEGER_EXT && type == GL_INT &&
1884              !swapBytes;
1885 
1886    case MESA_FORMAT_R_SINT8:
1887       return format == GL_RED_INTEGER && type == GL_BYTE;
1888    case MESA_FORMAT_RG_SINT8:
1889       return format == GL_RG_INTEGER && type == GL_BYTE && !swapBytes;
1890    case MESA_FORMAT_RGB_SINT8:
1891       return format == GL_RGB_INTEGER && type == GL_BYTE && !swapBytes;
1892    case MESA_FORMAT_RGBA_SINT8:
1893       return format == GL_RGBA_INTEGER && type == GL_BYTE && !swapBytes;
1894    case MESA_FORMAT_R_SINT16:
1895       return format == GL_RED_INTEGER && type == GL_SHORT && !swapBytes;
1896    case MESA_FORMAT_RG_SINT16:
1897       return format == GL_RG_INTEGER && type == GL_SHORT && !swapBytes;
1898    case MESA_FORMAT_RGB_SINT16:
1899       return format == GL_RGB_INTEGER && type == GL_SHORT && !swapBytes;
1900    case MESA_FORMAT_RGBA_SINT16:
1901       return format == GL_RGBA_INTEGER && type == GL_SHORT && !swapBytes;
1902    case MESA_FORMAT_R_SINT32:
1903       return format == GL_RED_INTEGER && type == GL_INT && !swapBytes;
1904    case MESA_FORMAT_RG_SINT32:
1905       return format == GL_RG_INTEGER && type == GL_INT && !swapBytes;
1906    case MESA_FORMAT_RGB_SINT32:
1907       return format == GL_RGB_INTEGER && type == GL_INT && !swapBytes;
1908    case MESA_FORMAT_RGBA_SINT32:
1909       return format == GL_RGBA_INTEGER && type == GL_INT && !swapBytes;
1910 
1911    case MESA_FORMAT_R_UINT8:
1912       return format == GL_RED_INTEGER && type == GL_UNSIGNED_BYTE;
1913    case MESA_FORMAT_RG_UINT8:
1914       return format == GL_RG_INTEGER && type == GL_UNSIGNED_BYTE && !swapBytes;
1915    case MESA_FORMAT_RGB_UINT8:
1916       return format == GL_RGB_INTEGER && type == GL_UNSIGNED_BYTE && !swapBytes;
1917    case MESA_FORMAT_RGBA_UINT8:
1918       return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_BYTE &&
1919              !swapBytes;
1920    case MESA_FORMAT_R_UINT16:
1921       return format == GL_RED_INTEGER && type == GL_UNSIGNED_SHORT &&
1922              !swapBytes;
1923    case MESA_FORMAT_RG_UINT16:
1924       return format == GL_RG_INTEGER && type == GL_UNSIGNED_SHORT && !swapBytes;
1925    case MESA_FORMAT_RGB_UINT16:
1926       return format == GL_RGB_INTEGER && type == GL_UNSIGNED_SHORT &&
1927              !swapBytes;
1928    case MESA_FORMAT_RGBA_UINT16:
1929       return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT &&
1930              !swapBytes;
1931    case MESA_FORMAT_R_UINT32:
1932       return format == GL_RED_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1933    case MESA_FORMAT_RG_UINT32:
1934       return format == GL_RG_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1935    case MESA_FORMAT_RGB_UINT32:
1936       return format == GL_RGB_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1937    case MESA_FORMAT_RGBA_UINT32:
1938       return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1939 
1940    case MESA_FORMAT_R_SNORM8:
1941       return format == GL_RED && type == GL_BYTE;
1942    case MESA_FORMAT_R8G8_SNORM:
1943       return format == GL_RG && type == GL_BYTE && littleEndian &&
1944              !swapBytes;
1945    case MESA_FORMAT_X8B8G8R8_SNORM:
1946       return GL_FALSE;
1947 
1948    case MESA_FORMAT_A8B8G8R8_SNORM:
1949       if (format == GL_RGBA && type == GL_BYTE && !littleEndian)
1950          return GL_TRUE;
1951 
1952       if (format == GL_ABGR_EXT && type == GL_BYTE && littleEndian)
1953          return GL_TRUE;
1954 
1955       return GL_FALSE;
1956 
1957    case MESA_FORMAT_R8G8B8A8_SNORM:
1958       if (format == GL_RGBA && type == GL_BYTE && littleEndian)
1959          return GL_TRUE;
1960 
1961       if (format == GL_ABGR_EXT && type == GL_BYTE && !littleEndian)
1962          return GL_TRUE;
1963 
1964       return GL_FALSE;
1965 
1966    case MESA_FORMAT_R_SNORM16:
1967       return format == GL_RED && type == GL_SHORT &&
1968              !swapBytes;
1969    case MESA_FORMAT_R16G16_SNORM:
1970       return format == GL_RG && type == GL_SHORT && littleEndian && !swapBytes;
1971    case MESA_FORMAT_RGB_SNORM16:
1972       return format == GL_RGB && type == GL_SHORT && !swapBytes;
1973    case MESA_FORMAT_RGBA_SNORM16:
1974       return format == GL_RGBA && type == GL_SHORT && !swapBytes;
1975    case MESA_FORMAT_RGBA_UNORM16:
1976       return format == GL_RGBA && type == GL_UNSIGNED_SHORT &&
1977              !swapBytes;
1978 
1979    case MESA_FORMAT_A_SNORM8:
1980       return format == GL_ALPHA && type == GL_BYTE;
1981    case MESA_FORMAT_L_SNORM8:
1982       return format == GL_LUMINANCE && type == GL_BYTE;
1983    case MESA_FORMAT_L8A8_SNORM:
1984       return format == GL_LUMINANCE_ALPHA && type == GL_BYTE &&
1985              littleEndian && !swapBytes;
1986    case MESA_FORMAT_A8L8_SNORM:
1987       return format == GL_LUMINANCE_ALPHA && type == GL_BYTE &&
1988              !littleEndian && !swapBytes;
1989    case MESA_FORMAT_I_SNORM8:
1990       return format == GL_RED && type == GL_BYTE;
1991    case MESA_FORMAT_A_SNORM16:
1992       return format == GL_ALPHA && type == GL_SHORT && !swapBytes;
1993    case MESA_FORMAT_L_SNORM16:
1994       return format == GL_LUMINANCE && type == GL_SHORT && !swapBytes;
1995    case MESA_FORMAT_LA_SNORM16:
1996       return format == GL_LUMINANCE_ALPHA && type == GL_SHORT &&
1997              littleEndian && !swapBytes;
1998    case MESA_FORMAT_I_SNORM16:
1999       return format == GL_RED && type == GL_SHORT && littleEndian &&
2000              !swapBytes;
2001 
2002    case MESA_FORMAT_B10G10R10A2_UINT:
2003       return (format == GL_BGRA_INTEGER_EXT &&
2004               type == GL_UNSIGNED_INT_2_10_10_10_REV &&
2005               !swapBytes);
2006 
2007    case MESA_FORMAT_R10G10B10A2_UINT:
2008       return (format == GL_RGBA_INTEGER_EXT &&
2009               type == GL_UNSIGNED_INT_2_10_10_10_REV &&
2010               !swapBytes);
2011 
2012    case MESA_FORMAT_B5G6R5_UINT:
2013       return format == GL_RGB_INTEGER && type == GL_UNSIGNED_SHORT_5_6_5;
2014 
2015    case MESA_FORMAT_R5G6B5_UINT:
2016       return format == GL_RGB_INTEGER && type == GL_UNSIGNED_SHORT_5_6_5_REV;
2017 
2018    case MESA_FORMAT_B2G3R3_UINT:
2019       return format == GL_RGB_INTEGER && type == GL_UNSIGNED_BYTE_3_3_2;
2020 
2021    case MESA_FORMAT_R3G3B2_UINT:
2022       return format == GL_RGB_INTEGER && type == GL_UNSIGNED_BYTE_2_3_3_REV;
2023 
2024    case MESA_FORMAT_A4B4G4R4_UINT:
2025       if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
2026          return GL_TRUE;
2027 
2028       if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && swapBytes)
2029          return GL_TRUE;
2030       return GL_FALSE;
2031 
2032    case MESA_FORMAT_R4G4B4A4_UINT:
2033       if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !swapBytes)
2034          return GL_TRUE;
2035 
2036       if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4 && swapBytes)
2037          return GL_TRUE;
2038 
2039       return GL_FALSE;
2040 
2041    case MESA_FORMAT_B4G4R4A4_UINT:
2042       return format == GL_BGRA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4_REV &&
2043          !swapBytes;
2044 
2045    case MESA_FORMAT_A4R4G4B4_UINT:
2046       return GL_FALSE;
2047 
2048    case MESA_FORMAT_A1B5G5R5_UINT:
2049       return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
2050          !swapBytes;
2051 
2052    case MESA_FORMAT_B5G5R5A1_UINT:
2053       return format == GL_BGRA_INTEGER && type == GL_UNSIGNED_SHORT_1_5_5_5_REV &&
2054          !swapBytes;
2055 
2056    case MESA_FORMAT_A1R5G5B5_UINT:
2057       return format == GL_BGRA_INTEGER && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
2058          !swapBytes;
2059 
2060    case MESA_FORMAT_R5G5B5A1_UINT:
2061       return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_1_5_5_5_REV;
2062 
2063    case MESA_FORMAT_A8B8G8R8_UINT:
2064       if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
2065          return GL_TRUE;
2066 
2067       if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8_REV && swapBytes)
2068          return GL_TRUE;
2069       return GL_FALSE;
2070 
2071    case MESA_FORMAT_A8R8G8B8_UINT:
2072       if (format == GL_BGRA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8 &&
2073           !swapBytes)
2074          return GL_TRUE;
2075 
2076       if (format == GL_BGRA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
2077           swapBytes)
2078          return GL_TRUE;
2079 
2080       return GL_FALSE;
2081 
2082    case MESA_FORMAT_R8G8B8A8_UINT:
2083       if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
2084           !swapBytes)
2085          return GL_TRUE;
2086 
2087       if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
2088          return GL_TRUE;
2089 
2090       return GL_FALSE;
2091 
2092    case MESA_FORMAT_B8G8R8A8_UINT:
2093       if (format == GL_BGRA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
2094           !swapBytes)
2095          return GL_TRUE;
2096 
2097       if (format == GL_BGRA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
2098          return GL_TRUE;
2099 
2100       return GL_FALSE;
2101 
2102    case MESA_FORMAT_R9G9B9E5_FLOAT:
2103       return format == GL_RGB && type == GL_UNSIGNED_INT_5_9_9_9_REV &&
2104          !swapBytes;
2105 
2106    case MESA_FORMAT_R11G11B10_FLOAT:
2107       return format == GL_RGB && type == GL_UNSIGNED_INT_10F_11F_11F_REV &&
2108          !swapBytes;
2109 
2110    case MESA_FORMAT_Z_FLOAT32:
2111       return format == GL_DEPTH_COMPONENT && type == GL_FLOAT && !swapBytes;
2112 
2113    case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
2114       return format == GL_DEPTH_STENCIL &&
2115              type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV && !swapBytes;
2116 
2117    case MESA_FORMAT_B4G4R4X4_UNORM:
2118    case MESA_FORMAT_B5G5R5X1_UNORM:
2119    case MESA_FORMAT_R8G8B8X8_SNORM:
2120    case MESA_FORMAT_R8G8B8X8_SRGB:
2121    case MESA_FORMAT_X8B8G8R8_SRGB:
2122    case MESA_FORMAT_RGBX_UINT8:
2123    case MESA_FORMAT_RGBX_SINT8:
2124    case MESA_FORMAT_B10G10R10X2_UNORM:
2125    case MESA_FORMAT_R10G10B10X2_UNORM:
2126    case MESA_FORMAT_RGBX_UNORM16:
2127    case MESA_FORMAT_RGBX_SNORM16:
2128    case MESA_FORMAT_RGBX_FLOAT16:
2129    case MESA_FORMAT_RGBX_UINT16:
2130    case MESA_FORMAT_RGBX_SINT16:
2131    case MESA_FORMAT_RGBX_FLOAT32:
2132    case MESA_FORMAT_RGBX_UINT32:
2133    case MESA_FORMAT_RGBX_SINT32:
2134       return GL_FALSE;
2135 
2136    case MESA_FORMAT_R10G10B10A2_UNORM:
2137       return format == GL_RGBA && type == GL_UNSIGNED_INT_2_10_10_10_REV &&
2138          !swapBytes;
2139 
2140    case MESA_FORMAT_G8R8_SNORM:
2141       return format == GL_RG && type == GL_BYTE && !littleEndian &&
2142          !swapBytes;
2143 
2144    case MESA_FORMAT_G16R16_SNORM:
2145       return format == GL_RG && type == GL_SHORT && !littleEndian &&
2146          !swapBytes;
2147 
2148    case MESA_FORMAT_B8G8R8X8_SRGB:
2149    case MESA_FORMAT_X8R8G8B8_SRGB:
2150       return GL_FALSE;
2151    default:
2152       assert(_mesa_is_format_compressed(mesa_format));
2153       if (error)
2154          *error = GL_INVALID_ENUM;
2155    }
2156    return GL_FALSE;
2157 }
2158 
2159