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