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