Home | History | Annotate | Download | only in main
      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  * Copyright (c) 2012 Intel Corporation
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a
      9  * copy of this software and associated documentation files (the "Software"),
     10  * to deal in the Software without restriction, including without limitation
     11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     12  * and/or sell copies of the Software, and to permit persons to whom the
     13  * Software is furnished to do so, subject to the following conditions:
     14  *
     15  * The above copyright notice and this permission notice shall be included
     16  * in all copies or substantial portions of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     24  * OTHER DEALINGS IN THE SOFTWARE.
     25  */
     26 
     27 
     28 #include "context.h"
     29 #include "glformats.h"
     30 #include "formats.h"
     31 #include "texcompress.h"
     32 #include "enums.h"
     33 
     34 enum {
     35    ZERO = 4,
     36    ONE = 5
     37 };
     38 
     39 enum {
     40    IDX_LUMINANCE = 0,
     41    IDX_ALPHA,
     42    IDX_INTENSITY,
     43    IDX_LUMINANCE_ALPHA,
     44    IDX_RGB,
     45    IDX_RGBA,
     46    IDX_RED,
     47    IDX_GREEN,
     48    IDX_BLUE,
     49    IDX_BGR,
     50    IDX_BGRA,
     51    IDX_ABGR,
     52    IDX_RG,
     53    MAX_IDX
     54 };
     55 
     56 #define MAP1(x)       MAP4(x, ZERO, ZERO, ZERO)
     57 #define MAP2(x,y)     MAP4(x, y, ZERO, ZERO)
     58 #define MAP3(x,y,z)   MAP4(x, y, z, ZERO)
     59 #define MAP4(x,y,z,w) { x, y, z, w, ZERO, ONE }
     60 
     61 static const struct {
     62    GLubyte format_idx;
     63    GLubyte to_rgba[6];
     64    GLubyte from_rgba[6];
     65 } mappings[MAX_IDX] =
     66 {
     67    {
     68       IDX_LUMINANCE,
     69       MAP4(0,0,0,ONE),
     70       MAP1(0)
     71    },
     72 
     73    {
     74       IDX_ALPHA,
     75       MAP4(ZERO, ZERO, ZERO, 0),
     76       MAP1(3)
     77    },
     78 
     79    {
     80       IDX_INTENSITY,
     81       MAP4(0, 0, 0, 0),
     82       MAP1(0),
     83    },
     84 
     85    {
     86       IDX_LUMINANCE_ALPHA,
     87       MAP4(0,0,0,1),
     88       MAP2(0,3)
     89    },
     90 
     91    {
     92       IDX_RGB,
     93       MAP4(0,1,2,ONE),
     94       MAP3(0,1,2)
     95    },
     96 
     97    {
     98       IDX_RGBA,
     99       MAP4(0,1,2,3),
    100       MAP4(0,1,2,3),
    101    },
    102 
    103    {
    104       IDX_RED,
    105       MAP4(0, ZERO, ZERO, ONE),
    106       MAP1(0),
    107    },
    108 
    109    {
    110       IDX_GREEN,
    111       MAP4(ZERO, 0, ZERO, ONE),
    112       MAP1(1),
    113    },
    114 
    115    {
    116       IDX_BLUE,
    117       MAP4(ZERO, ZERO, 0, ONE),
    118       MAP1(2),
    119    },
    120 
    121    {
    122       IDX_BGR,
    123       MAP4(2,1,0,ONE),
    124       MAP3(2,1,0)
    125    },
    126 
    127    {
    128       IDX_BGRA,
    129       MAP4(2,1,0,3),
    130       MAP4(2,1,0,3)
    131    },
    132 
    133    {
    134       IDX_ABGR,
    135       MAP4(3,2,1,0),
    136       MAP4(3,2,1,0)
    137    },
    138 
    139    {
    140       IDX_RG,
    141       MAP4(0, 1, ZERO, ONE),
    142       MAP2(0, 1)
    143    },
    144 };
    145 
    146 /**
    147  * Convert a GL image format enum to an IDX_* value (see above).
    148  */
    149 static int
    150 get_map_idx(GLenum value)
    151 {
    152    switch (value) {
    153    case GL_LUMINANCE:
    154    case GL_LUMINANCE_INTEGER_EXT:
    155       return IDX_LUMINANCE;
    156    case GL_ALPHA:
    157    case GL_ALPHA_INTEGER:
    158       return IDX_ALPHA;
    159    case GL_INTENSITY:
    160       return IDX_INTENSITY;
    161    case GL_LUMINANCE_ALPHA:
    162    case GL_LUMINANCE_ALPHA_INTEGER_EXT:
    163       return IDX_LUMINANCE_ALPHA;
    164    case GL_RGB:
    165    case GL_RGB_INTEGER:
    166       return IDX_RGB;
    167    case GL_RGBA:
    168    case GL_RGBA_INTEGER:
    169       return IDX_RGBA;
    170    case GL_RED:
    171    case GL_RED_INTEGER:
    172       return IDX_RED;
    173    case GL_GREEN:
    174       return IDX_GREEN;
    175    case GL_BLUE:
    176       return IDX_BLUE;
    177    case GL_BGR:
    178    case GL_BGR_INTEGER:
    179       return IDX_BGR;
    180    case GL_BGRA:
    181    case GL_BGRA_INTEGER:
    182       return IDX_BGRA;
    183    case GL_ABGR_EXT:
    184       return IDX_ABGR;
    185    case GL_RG:
    186    case GL_RG_INTEGER:
    187       return IDX_RG;
    188    default:
    189       _mesa_problem(NULL, "Unexpected inFormat %s",
    190                     _mesa_enum_to_string(value));
    191       return 0;
    192    }
    193 }
    194 
    195 /**
    196  * When promoting texture formats (see below) we need to compute the
    197  * mapping of dest components back to source components.
    198  * This function does that.
    199  * \param inFormat  the incoming format of the texture
    200  * \param outFormat  the final texture format
    201  * \return map[6]  a full 6-component map
    202  */
    203 void
    204 _mesa_compute_component_mapping(GLenum inFormat, GLenum outFormat, GLubyte *map)
    205 {
    206    const int inFmt = get_map_idx(inFormat);
    207    const int outFmt = get_map_idx(outFormat);
    208    const GLubyte *in2rgba = mappings[inFmt].to_rgba;
    209    const GLubyte *rgba2out = mappings[outFmt].from_rgba;
    210    int i;
    211 
    212    for (i = 0; i < 4; i++)
    213       map[i] = in2rgba[rgba2out[i]];
    214 
    215    map[ZERO] = ZERO;
    216    map[ONE] = ONE;
    217 
    218 #if 0
    219    printf("from %x/%s to %x/%s map %d %d %d %d %d %d\n",
    220 	  inFormat, _mesa_enum_to_string(inFormat),
    221 	  outFormat, _mesa_enum_to_string(outFormat),
    222 	  map[0],
    223 	  map[1],
    224 	  map[2],
    225 	  map[3],
    226 	  map[4],
    227 	  map[5]);
    228 #endif
    229 }
    230 
    231 /**
    232  * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
    233  */
    234 GLboolean
    235 _mesa_type_is_packed(GLenum type)
    236 {
    237    switch (type) {
    238    case GL_UNSIGNED_BYTE_3_3_2:
    239    case GL_UNSIGNED_BYTE_2_3_3_REV:
    240    case MESA_UNSIGNED_BYTE_4_4:
    241    case GL_UNSIGNED_SHORT_5_6_5:
    242    case GL_UNSIGNED_SHORT_5_6_5_REV:
    243    case GL_UNSIGNED_SHORT_4_4_4_4:
    244    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
    245    case GL_UNSIGNED_SHORT_5_5_5_1:
    246    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
    247    case GL_UNSIGNED_INT_8_8_8_8:
    248    case GL_UNSIGNED_INT_8_8_8_8_REV:
    249    case GL_UNSIGNED_INT_10_10_10_2:
    250    case GL_UNSIGNED_INT_2_10_10_10_REV:
    251    case GL_UNSIGNED_SHORT_8_8_MESA:
    252    case GL_UNSIGNED_SHORT_8_8_REV_MESA:
    253    case GL_UNSIGNED_INT_24_8_EXT:
    254    case GL_UNSIGNED_INT_5_9_9_9_REV:
    255    case GL_UNSIGNED_INT_10F_11F_11F_REV:
    256    case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
    257       return GL_TRUE;
    258    }
    259 
    260    return GL_FALSE;
    261 }
    262 
    263 
    264 /**
    265  * Get the size of a GL data type.
    266  *
    267  * \param type GL data type.
    268  *
    269  * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
    270  * if an invalid type enum.
    271  */
    272 GLint
    273 _mesa_sizeof_type(GLenum type)
    274 {
    275    switch (type) {
    276    case GL_BITMAP:
    277       return 0;
    278    case GL_UNSIGNED_BYTE:
    279       return sizeof(GLubyte);
    280    case GL_BYTE:
    281       return sizeof(GLbyte);
    282    case GL_UNSIGNED_SHORT:
    283       return sizeof(GLushort);
    284    case GL_SHORT:
    285       return sizeof(GLshort);
    286    case GL_UNSIGNED_INT:
    287       return sizeof(GLuint);
    288    case GL_INT:
    289       return sizeof(GLint);
    290    case GL_FLOAT:
    291       return sizeof(GLfloat);
    292    case GL_DOUBLE:
    293       return sizeof(GLdouble);
    294    case GL_HALF_FLOAT_ARB:
    295    case GL_HALF_FLOAT_OES:
    296       return sizeof(GLhalfARB);
    297    case GL_FIXED:
    298       return sizeof(GLfixed);
    299    default:
    300       return -1;
    301    }
    302 }
    303 
    304 
    305 /**
    306  * Same as _mesa_sizeof_type() but also accepting the packed pixel
    307  * format data types.
    308  */
    309 GLint
    310 _mesa_sizeof_packed_type(GLenum type)
    311 {
    312    switch (type) {
    313    case GL_BITMAP:
    314       return 0;
    315    case GL_UNSIGNED_BYTE:
    316       return sizeof(GLubyte);
    317    case GL_BYTE:
    318       return sizeof(GLbyte);
    319    case GL_UNSIGNED_SHORT:
    320       return sizeof(GLushort);
    321    case GL_SHORT:
    322       return sizeof(GLshort);
    323    case GL_UNSIGNED_INT:
    324       return sizeof(GLuint);
    325    case GL_INT:
    326       return sizeof(GLint);
    327    case GL_HALF_FLOAT_ARB:
    328    case GL_HALF_FLOAT_OES:
    329       return sizeof(GLhalfARB);
    330    case GL_FLOAT:
    331       return sizeof(GLfloat);
    332    case GL_UNSIGNED_BYTE_3_3_2:
    333    case GL_UNSIGNED_BYTE_2_3_3_REV:
    334    case MESA_UNSIGNED_BYTE_4_4:
    335       return sizeof(GLubyte);
    336    case GL_UNSIGNED_SHORT_5_6_5:
    337    case GL_UNSIGNED_SHORT_5_6_5_REV:
    338    case GL_UNSIGNED_SHORT_4_4_4_4:
    339    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
    340    case GL_UNSIGNED_SHORT_5_5_5_1:
    341    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
    342    case GL_UNSIGNED_SHORT_8_8_MESA:
    343    case GL_UNSIGNED_SHORT_8_8_REV_MESA:
    344       return sizeof(GLushort);
    345    case GL_UNSIGNED_INT_8_8_8_8:
    346    case GL_UNSIGNED_INT_8_8_8_8_REV:
    347    case GL_UNSIGNED_INT_10_10_10_2:
    348    case GL_UNSIGNED_INT_2_10_10_10_REV:
    349    case GL_UNSIGNED_INT_24_8_EXT:
    350    case GL_UNSIGNED_INT_5_9_9_9_REV:
    351    case GL_UNSIGNED_INT_10F_11F_11F_REV:
    352       return sizeof(GLuint);
    353    case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
    354       return 8;
    355    default:
    356       return -1;
    357    }
    358 }
    359 
    360 
    361 /**
    362  * Get the number of components in a pixel format.
    363  *
    364  * \param format pixel format.
    365  *
    366  * \return the number of components in the given format, or -1 if a bad format.
    367  */
    368 GLint
    369 _mesa_components_in_format(GLenum format)
    370 {
    371    switch (format) {
    372    case GL_COLOR_INDEX:
    373    case GL_STENCIL_INDEX:
    374    case GL_DEPTH_COMPONENT:
    375    case GL_RED:
    376    case GL_RED_INTEGER_EXT:
    377    case GL_GREEN:
    378    case GL_GREEN_INTEGER_EXT:
    379    case GL_BLUE:
    380    case GL_BLUE_INTEGER_EXT:
    381    case GL_ALPHA:
    382    case GL_ALPHA_INTEGER_EXT:
    383    case GL_LUMINANCE:
    384    case GL_LUMINANCE_INTEGER_EXT:
    385    case GL_INTENSITY:
    386       return 1;
    387 
    388    case GL_LUMINANCE_ALPHA:
    389    case GL_LUMINANCE_ALPHA_INTEGER_EXT:
    390    case GL_RG:
    391    case GL_YCBCR_MESA:
    392    case GL_DEPTH_STENCIL_EXT:
    393    case GL_RG_INTEGER:
    394       return 2;
    395 
    396    case GL_RGB:
    397    case GL_BGR:
    398    case GL_RGB_INTEGER_EXT:
    399    case GL_BGR_INTEGER_EXT:
    400       return 3;
    401 
    402    case GL_RGBA:
    403    case GL_BGRA:
    404    case GL_ABGR_EXT:
    405    case GL_RGBA_INTEGER_EXT:
    406    case GL_BGRA_INTEGER_EXT:
    407       return 4;
    408 
    409    default:
    410       return -1;
    411    }
    412 }
    413 
    414 
    415 /**
    416  * Get the bytes per pixel of pixel format type pair.
    417  *
    418  * \param format pixel format.
    419  * \param type pixel type.
    420  *
    421  * \return bytes per pixel, or -1 if a bad format or type was given.
    422  */
    423 GLint
    424 _mesa_bytes_per_pixel(GLenum format, GLenum type)
    425 {
    426    GLint comps = _mesa_components_in_format(format);
    427    if (comps < 0)
    428       return -1;
    429 
    430    switch (type) {
    431    case GL_BITMAP:
    432       return 0;  /* special case */
    433    case GL_BYTE:
    434    case GL_UNSIGNED_BYTE:
    435       return comps * sizeof(GLubyte);
    436    case GL_SHORT:
    437    case GL_UNSIGNED_SHORT:
    438       return comps * sizeof(GLshort);
    439    case GL_INT:
    440    case GL_UNSIGNED_INT:
    441       return comps * sizeof(GLint);
    442    case GL_FLOAT:
    443       return comps * sizeof(GLfloat);
    444    case GL_HALF_FLOAT_ARB:
    445    case GL_HALF_FLOAT_OES:
    446       return comps * sizeof(GLhalfARB);
    447    case GL_UNSIGNED_BYTE_3_3_2:
    448    case GL_UNSIGNED_BYTE_2_3_3_REV:
    449       if (format == GL_RGB || format == GL_BGR ||
    450           format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT)
    451          return sizeof(GLubyte);
    452       else
    453          return -1;  /* error */
    454    case GL_UNSIGNED_SHORT_5_6_5:
    455    case GL_UNSIGNED_SHORT_5_6_5_REV:
    456       if (format == GL_RGB || format == GL_BGR ||
    457           format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT)
    458          return sizeof(GLushort);
    459       else
    460          return -1;  /* error */
    461    case GL_UNSIGNED_SHORT_4_4_4_4:
    462    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
    463       if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT ||
    464           format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT)
    465          return sizeof(GLushort);
    466       else
    467          return -1;
    468    case GL_UNSIGNED_SHORT_5_5_5_1:
    469    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
    470       if (format == GL_RGBA || format == GL_BGRA ||
    471           format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT)
    472          return sizeof(GLushort);
    473       else
    474          return -1;
    475    case GL_UNSIGNED_INT_8_8_8_8:
    476    case GL_UNSIGNED_INT_8_8_8_8_REV:
    477       if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT ||
    478           format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT ||
    479           format == GL_RGB)
    480          return sizeof(GLuint);
    481       else
    482          return -1;
    483    case GL_UNSIGNED_INT_10_10_10_2:
    484    case GL_UNSIGNED_INT_2_10_10_10_REV:
    485       if (format == GL_RGBA || format == GL_BGRA ||
    486           format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT ||
    487           format == GL_RGB)
    488          return sizeof(GLuint);
    489       else
    490          return -1;
    491    case GL_UNSIGNED_SHORT_8_8_MESA:
    492    case GL_UNSIGNED_SHORT_8_8_REV_MESA:
    493       if (format == GL_YCBCR_MESA)
    494          return sizeof(GLushort);
    495       else
    496          return -1;
    497    case GL_UNSIGNED_INT_24_8_EXT:
    498       if (format == GL_DEPTH_COMPONENT ||
    499           format == GL_DEPTH_STENCIL_EXT)
    500          return sizeof(GLuint);
    501       else
    502          return -1;
    503    case GL_UNSIGNED_INT_5_9_9_9_REV:
    504       if (format == GL_RGB)
    505          return sizeof(GLuint);
    506       else
    507          return -1;
    508    case GL_UNSIGNED_INT_10F_11F_11F_REV:
    509       if (format == GL_RGB)
    510          return sizeof(GLuint);
    511       else
    512          return -1;
    513    case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
    514       if (format == GL_DEPTH_STENCIL)
    515          return 8;
    516       else
    517          return -1;
    518    default:
    519       return -1;
    520    }
    521 }
    522 
    523 
    524 /**
    525  * Get the number of bytes for a vertex attrib with the given number of
    526  * components and type.
    527  *
    528  * \param comps number of components.
    529  * \param type data type.
    530  *
    531  * \return bytes per attribute, or -1 if a bad comps/type combination was given.
    532  */
    533 GLint
    534 _mesa_bytes_per_vertex_attrib(GLint comps, GLenum type)
    535 {
    536    switch (type) {
    537    case GL_BYTE:
    538    case GL_UNSIGNED_BYTE:
    539       return comps * sizeof(GLubyte);
    540    case GL_SHORT:
    541    case GL_UNSIGNED_SHORT:
    542       return comps * sizeof(GLshort);
    543    case GL_INT:
    544    case GL_UNSIGNED_INT:
    545       return comps * sizeof(GLint);
    546    case GL_FLOAT:
    547       return comps * sizeof(GLfloat);
    548    case GL_HALF_FLOAT_ARB:
    549    case GL_HALF_FLOAT_OES:
    550       return comps * sizeof(GLhalfARB);
    551    case GL_DOUBLE:
    552       return comps * sizeof(GLdouble);
    553    case GL_FIXED:
    554       return comps * sizeof(GLfixed);
    555    case GL_INT_2_10_10_10_REV:
    556    case GL_UNSIGNED_INT_2_10_10_10_REV:
    557       if (comps == 4)
    558          return sizeof(GLuint);
    559       else
    560          return -1;
    561    case GL_UNSIGNED_INT_10F_11F_11F_REV:
    562       if (comps == 3)
    563          return sizeof(GLuint);
    564       else
    565          return -1;
    566    default:
    567       return -1;
    568    }
    569 }
    570 
    571 /**
    572  * Test if the given format is unsized.
    573  */
    574 GLboolean
    575 _mesa_is_enum_format_unsized(GLenum format)
    576 {
    577    switch (format) {
    578    case GL_RGBA:
    579    case GL_BGRA:
    580    case GL_ABGR_EXT:
    581    case GL_RGB:
    582    case GL_BGR:
    583    case GL_RG:
    584    case GL_RED:
    585    case GL_GREEN:
    586    case GL_BLUE:
    587    case GL_ALPHA:
    588    case GL_INTENSITY:
    589    case GL_LUMINANCE:
    590    case GL_LUMINANCE_ALPHA:
    591 
    592    case GL_SRGB:
    593    case GL_SRGB_ALPHA:
    594    case GL_SLUMINANCE:
    595    case GL_SLUMINANCE_ALPHA:
    596 
    597    case GL_RGBA_SNORM:
    598    case GL_RGB_SNORM:
    599    case GL_RG_SNORM:
    600    case GL_RED_SNORM:
    601    case GL_ALPHA_SNORM:
    602    case GL_INTENSITY_SNORM:
    603    case GL_LUMINANCE_SNORM:
    604    case GL_LUMINANCE_ALPHA_SNORM:
    605 
    606    case GL_RED_INTEGER:
    607    case GL_GREEN_INTEGER:
    608    case GL_BLUE_INTEGER:
    609    case GL_ALPHA_INTEGER:
    610    case GL_RGB_INTEGER:
    611    case GL_RGBA_INTEGER:
    612    case GL_BGR_INTEGER:
    613    case GL_BGRA_INTEGER:
    614    case GL_RG_INTEGER:
    615    case GL_LUMINANCE_INTEGER_EXT:
    616    case GL_LUMINANCE_ALPHA_INTEGER_EXT:
    617 
    618    case GL_DEPTH_COMPONENT:
    619    case GL_DEPTH_STENCIL:
    620    case GL_STENCIL_INDEX:
    621       return GL_TRUE;
    622    default:
    623       return GL_FALSE;
    624    }
    625 }
    626 
    627 /**
    628  * Test if the given format is a UNORM (unsigned-normalized) format.
    629  */
    630 GLboolean
    631 _mesa_is_enum_format_unorm(GLenum format)
    632 {
    633       switch(format) {
    634       case GL_RED:
    635       case GL_GREEN:
    636       case GL_BLUE:
    637       case GL_ALPHA:
    638       case GL_ALPHA4:
    639       case GL_ALPHA8:
    640       case GL_ALPHA12:
    641       case GL_ALPHA16:
    642       case 1:
    643       case GL_LUMINANCE:
    644       case GL_SLUMINANCE:
    645       case GL_LUMINANCE4:
    646       case GL_LUMINANCE8:
    647       case GL_LUMINANCE12:
    648       case GL_LUMINANCE16:
    649       case 2:
    650       case GL_LUMINANCE_ALPHA:
    651       case GL_SLUMINANCE_ALPHA:
    652       case GL_LUMINANCE4_ALPHA4:
    653       case GL_LUMINANCE6_ALPHA2:
    654       case GL_LUMINANCE8_ALPHA8:
    655       case GL_LUMINANCE12_ALPHA4:
    656       case GL_LUMINANCE12_ALPHA12:
    657       case GL_LUMINANCE16_ALPHA16:
    658       case GL_INTENSITY:
    659       case GL_INTENSITY4:
    660       case GL_INTENSITY8:
    661       case GL_INTENSITY12:
    662       case GL_INTENSITY16:
    663       case GL_R8:
    664       case GL_R16:
    665       case GL_RG:
    666       case GL_RG8:
    667       case GL_RG16:
    668       case 3:
    669       case GL_RGB:
    670       case GL_BGR:
    671       case GL_SRGB:
    672       case GL_R3_G3_B2:
    673       case GL_RGB4:
    674       case GL_RGB5:
    675       case GL_RGB565:
    676       case GL_RGB8:
    677       case GL_RGB10:
    678       case GL_RGB12:
    679       case GL_RGB16:
    680       case 4:
    681       case GL_ABGR_EXT:
    682       case GL_RGBA:
    683       case GL_BGRA:
    684       case GL_SRGB_ALPHA:
    685       case GL_RGBA2:
    686       case GL_RGBA4:
    687       case GL_RGB5_A1:
    688       case GL_RGBA8:
    689       case GL_RGB10_A2:
    690       case GL_RGBA12:
    691       case GL_RGBA16:
    692          return GL_TRUE;
    693       default:
    694          return GL_FALSE;
    695    }
    696 }
    697 
    698 /**
    699  * Test if the given format is a SNORM (signed-normalized) format.
    700  */
    701 GLboolean
    702 _mesa_is_enum_format_snorm(GLenum format)
    703 {
    704    switch (format) {
    705    /* signed, normalized texture formats */
    706    case GL_RED_SNORM:
    707    case GL_R8_SNORM:
    708    case GL_R16_SNORM:
    709    case GL_RG_SNORM:
    710    case GL_RG8_SNORM:
    711    case GL_RG16_SNORM:
    712    case GL_RGB_SNORM:
    713    case GL_RGB8_SNORM:
    714    case GL_RGB16_SNORM:
    715    case GL_RGBA_SNORM:
    716    case GL_RGBA8_SNORM:
    717    case GL_RGBA16_SNORM:
    718    case GL_ALPHA_SNORM:
    719    case GL_ALPHA8_SNORM:
    720    case GL_ALPHA16_SNORM:
    721    case GL_LUMINANCE_SNORM:
    722    case GL_LUMINANCE8_SNORM:
    723    case GL_LUMINANCE16_SNORM:
    724    case GL_LUMINANCE_ALPHA_SNORM:
    725    case GL_LUMINANCE8_ALPHA8_SNORM:
    726    case GL_LUMINANCE16_ALPHA16_SNORM:
    727    case GL_INTENSITY_SNORM:
    728    case GL_INTENSITY8_SNORM:
    729    case GL_INTENSITY16_SNORM:
    730       return GL_TRUE;
    731    default:
    732       return GL_FALSE;
    733    }
    734 }
    735 
    736 /**
    737  * Test if the given format is an integer (non-normalized) format.
    738  */
    739 GLboolean
    740 _mesa_is_enum_format_unsigned_int(GLenum format)
    741 {
    742    switch (format) {
    743    /* specific integer formats */
    744    case GL_RGBA32UI_EXT:
    745    case GL_RGB32UI_EXT:
    746    case GL_RG32UI:
    747    case GL_R32UI:
    748    case GL_ALPHA32UI_EXT:
    749    case GL_INTENSITY32UI_EXT:
    750    case GL_LUMINANCE32UI_EXT:
    751    case GL_LUMINANCE_ALPHA32UI_EXT:
    752    case GL_RGBA16UI_EXT:
    753    case GL_RGB16UI_EXT:
    754    case GL_RG16UI:
    755    case GL_R16UI:
    756    case GL_ALPHA16UI_EXT:
    757    case GL_INTENSITY16UI_EXT:
    758    case GL_LUMINANCE16UI_EXT:
    759    case GL_LUMINANCE_ALPHA16UI_EXT:
    760    case GL_RGBA8UI_EXT:
    761    case GL_RGB8UI_EXT:
    762    case GL_RG8UI:
    763    case GL_R8UI:
    764    case GL_ALPHA8UI_EXT:
    765    case GL_INTENSITY8UI_EXT:
    766    case GL_LUMINANCE8UI_EXT:
    767    case GL_LUMINANCE_ALPHA8UI_EXT:
    768    case GL_RGB10_A2UI:
    769       return GL_TRUE;
    770    default:
    771       return GL_FALSE;
    772    }
    773 }
    774 
    775 
    776 /**
    777  * Test if the given format is an integer (non-normalized) format.
    778  */
    779 GLboolean
    780 _mesa_is_enum_format_signed_int(GLenum format)
    781 {
    782    switch (format) {
    783    /* generic integer formats */
    784    case GL_RED_INTEGER_EXT:
    785    case GL_GREEN_INTEGER_EXT:
    786    case GL_BLUE_INTEGER_EXT:
    787    case GL_ALPHA_INTEGER_EXT:
    788    case GL_RGB_INTEGER_EXT:
    789    case GL_RGBA_INTEGER_EXT:
    790    case GL_BGR_INTEGER_EXT:
    791    case GL_BGRA_INTEGER_EXT:
    792    case GL_LUMINANCE_INTEGER_EXT:
    793    case GL_LUMINANCE_ALPHA_INTEGER_EXT:
    794    case GL_RG_INTEGER:
    795    /* specific integer formats */
    796    case GL_RGBA32I_EXT:
    797    case GL_RGB32I_EXT:
    798    case GL_RG32I:
    799    case GL_R32I:
    800    case GL_ALPHA32I_EXT:
    801    case GL_INTENSITY32I_EXT:
    802    case GL_LUMINANCE32I_EXT:
    803    case GL_LUMINANCE_ALPHA32I_EXT:
    804    case GL_RGBA16I_EXT:
    805    case GL_RGB16I_EXT:
    806    case GL_RG16I:
    807    case GL_R16I:
    808    case GL_ALPHA16I_EXT:
    809    case GL_INTENSITY16I_EXT:
    810    case GL_LUMINANCE16I_EXT:
    811    case GL_LUMINANCE_ALPHA16I_EXT:
    812    case GL_RGBA8I_EXT:
    813    case GL_RGB8I_EXT:
    814    case GL_RG8I:
    815    case GL_R8I:
    816    case GL_ALPHA8I_EXT:
    817    case GL_INTENSITY8I_EXT:
    818    case GL_LUMINANCE8I_EXT:
    819    case GL_LUMINANCE_ALPHA8I_EXT:
    820       return GL_TRUE;
    821    default:
    822       return GL_FALSE;
    823    }
    824 }
    825 
    826 /**
    827  * Test if the given format is an ASTC 2D format.
    828  */
    829 static bool
    830 is_astc_2d_format(GLenum internalFormat)
    831 {
    832    switch (internalFormat) {
    833    case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
    834    case GL_COMPRESSED_RGBA_ASTC_5x4_KHR:
    835    case GL_COMPRESSED_RGBA_ASTC_5x5_KHR:
    836    case GL_COMPRESSED_RGBA_ASTC_6x5_KHR:
    837    case GL_COMPRESSED_RGBA_ASTC_6x6_KHR:
    838    case GL_COMPRESSED_RGBA_ASTC_8x5_KHR:
    839    case GL_COMPRESSED_RGBA_ASTC_8x6_KHR:
    840    case GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
    841    case GL_COMPRESSED_RGBA_ASTC_10x5_KHR:
    842    case GL_COMPRESSED_RGBA_ASTC_10x6_KHR:
    843    case GL_COMPRESSED_RGBA_ASTC_10x8_KHR:
    844    case GL_COMPRESSED_RGBA_ASTC_10x10_KHR:
    845    case GL_COMPRESSED_RGBA_ASTC_12x10_KHR:
    846    case GL_COMPRESSED_RGBA_ASTC_12x12_KHR:
    847    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
    848    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
    849    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
    850    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
    851    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
    852    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
    853    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
    854    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
    855    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
    856    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
    857    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
    858    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
    859    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
    860    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
    861       return true;
    862    default:
    863       return false;
    864    }
    865 }
    866 
    867 /**
    868  * Test if the given format is an ASTC 3D format.
    869  */
    870 static bool
    871 is_astc_3d_format(GLenum internalFormat)
    872 {
    873    switch (internalFormat) {
    874    case GL_COMPRESSED_RGBA_ASTC_3x3x3_OES:
    875    case GL_COMPRESSED_RGBA_ASTC_4x3x3_OES:
    876    case GL_COMPRESSED_RGBA_ASTC_4x4x3_OES:
    877    case GL_COMPRESSED_RGBA_ASTC_4x4x4_OES:
    878    case GL_COMPRESSED_RGBA_ASTC_5x4x4_OES:
    879    case GL_COMPRESSED_RGBA_ASTC_5x5x4_OES:
    880    case GL_COMPRESSED_RGBA_ASTC_5x5x5_OES:
    881    case GL_COMPRESSED_RGBA_ASTC_6x5x5_OES:
    882    case GL_COMPRESSED_RGBA_ASTC_6x6x5_OES:
    883    case GL_COMPRESSED_RGBA_ASTC_6x6x6_OES:
    884    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES:
    885    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES:
    886    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES:
    887    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES:
    888    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES:
    889    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES:
    890    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES:
    891    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES:
    892    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES:
    893    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES:
    894       return true;
    895    default:
    896       return false;
    897    }
    898 }
    899 
    900 /**
    901  * Test if the given format is an ASTC format.
    902  */
    903 GLboolean
    904 _mesa_is_astc_format(GLenum internalFormat)
    905 {
    906    return is_astc_2d_format(internalFormat) ||
    907           is_astc_3d_format(internalFormat);
    908 }
    909 
    910 /**
    911  * Test if the given format is an ETC2 format.
    912  */
    913 GLboolean
    914 _mesa_is_etc2_format(GLenum internalFormat)
    915 {
    916    switch (internalFormat) {
    917    case GL_COMPRESSED_RGB8_ETC2:
    918    case GL_COMPRESSED_SRGB8_ETC2:
    919    case GL_COMPRESSED_RGBA8_ETC2_EAC:
    920    case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
    921    case GL_COMPRESSED_R11_EAC:
    922    case GL_COMPRESSED_RG11_EAC:
    923    case GL_COMPRESSED_SIGNED_R11_EAC:
    924    case GL_COMPRESSED_SIGNED_RG11_EAC:
    925    case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
    926    case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
    927       return true;
    928    default:
    929       return false;
    930    }
    931 }
    932 
    933 /**
    934  * Test if the given format is an integer (non-normalized) format.
    935  */
    936 GLboolean
    937 _mesa_is_enum_format_integer(GLenum format)
    938 {
    939    return _mesa_is_enum_format_unsigned_int(format) ||
    940           _mesa_is_enum_format_signed_int(format);
    941 }
    942 
    943 
    944 GLboolean
    945 _mesa_is_type_unsigned(GLenum type)
    946 {
    947    switch (type) {
    948    case GL_UNSIGNED_INT:
    949    case GL_UNSIGNED_INT_8_8_8_8:
    950    case GL_UNSIGNED_INT_8_8_8_8_REV:
    951    case GL_UNSIGNED_INT_10_10_10_2:
    952    case GL_UNSIGNED_INT_2_10_10_10_REV:
    953 
    954    case GL_UNSIGNED_SHORT:
    955    case GL_UNSIGNED_SHORT_4_4_4_4:
    956    case GL_UNSIGNED_SHORT_5_5_5_1:
    957    case GL_UNSIGNED_SHORT_5_6_5:
    958    case GL_UNSIGNED_SHORT_5_6_5_REV:
    959    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
    960    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
    961    case GL_UNSIGNED_SHORT_8_8_MESA:
    962    case GL_UNSIGNED_SHORT_8_8_REV_MESA:
    963 
    964    case GL_UNSIGNED_BYTE:
    965    case GL_UNSIGNED_BYTE_3_3_2:
    966    case GL_UNSIGNED_BYTE_2_3_3_REV:
    967       return GL_TRUE;
    968 
    969    default:
    970       return GL_FALSE;
    971    }
    972 }
    973 
    974 
    975 /**
    976  * Test if the given image format is a color/RGBA format (i.e., not color
    977  * index, depth, stencil, etc).
    978  * \param format  the image format value (may by an internal texture format)
    979  * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
    980  */
    981 GLboolean
    982 _mesa_is_color_format(GLenum format)
    983 {
    984    switch (format) {
    985       case GL_RED:
    986       case GL_GREEN:
    987       case GL_BLUE:
    988       case GL_ALPHA:
    989       case GL_ALPHA4:
    990       case GL_ALPHA8:
    991       case GL_ALPHA12:
    992       case GL_ALPHA16:
    993       case 1:
    994       case GL_LUMINANCE:
    995       case GL_LUMINANCE4:
    996       case GL_LUMINANCE8:
    997       case GL_LUMINANCE12:
    998       case GL_LUMINANCE16:
    999       case 2:
   1000       case GL_LUMINANCE_ALPHA:
   1001       case GL_LUMINANCE4_ALPHA4:
   1002       case GL_LUMINANCE6_ALPHA2:
   1003       case GL_LUMINANCE8_ALPHA8:
   1004       case GL_LUMINANCE12_ALPHA4:
   1005       case GL_LUMINANCE12_ALPHA12:
   1006       case GL_LUMINANCE16_ALPHA16:
   1007       case GL_INTENSITY:
   1008       case GL_INTENSITY4:
   1009       case GL_INTENSITY8:
   1010       case GL_INTENSITY12:
   1011       case GL_INTENSITY16:
   1012       case GL_R8:
   1013       case GL_R16:
   1014       case GL_RG:
   1015       case GL_RG8:
   1016       case GL_RG16:
   1017       case 3:
   1018       case GL_RGB:
   1019       case GL_BGR:
   1020       case GL_R3_G3_B2:
   1021       case GL_RGB4:
   1022       case GL_RGB5:
   1023       case GL_RGB565:
   1024       case GL_RGB8:
   1025       case GL_RGB10:
   1026       case GL_RGB12:
   1027       case GL_RGB16:
   1028       case 4:
   1029       case GL_ABGR_EXT:
   1030       case GL_RGBA:
   1031       case GL_BGRA:
   1032       case GL_RGBA2:
   1033       case GL_RGBA4:
   1034       case GL_RGB5_A1:
   1035       case GL_RGBA8:
   1036       case GL_RGB10_A2:
   1037       case GL_RGBA12:
   1038       case GL_RGBA16:
   1039       /* float texture formats */
   1040       case GL_ALPHA16F_ARB:
   1041       case GL_ALPHA32F_ARB:
   1042       case GL_LUMINANCE16F_ARB:
   1043       case GL_LUMINANCE32F_ARB:
   1044       case GL_LUMINANCE_ALPHA16F_ARB:
   1045       case GL_LUMINANCE_ALPHA32F_ARB:
   1046       case GL_INTENSITY16F_ARB:
   1047       case GL_INTENSITY32F_ARB:
   1048       case GL_R16F:
   1049       case GL_R32F:
   1050       case GL_RG16F:
   1051       case GL_RG32F:
   1052       case GL_RGB16F_ARB:
   1053       case GL_RGB32F_ARB:
   1054       case GL_RGBA16F_ARB:
   1055       case GL_RGBA32F_ARB:
   1056       /* compressed formats */
   1057       case GL_COMPRESSED_ALPHA:
   1058       case GL_COMPRESSED_LUMINANCE:
   1059       case GL_COMPRESSED_LUMINANCE_ALPHA:
   1060       case GL_COMPRESSED_INTENSITY:
   1061       case GL_COMPRESSED_RED:
   1062       case GL_COMPRESSED_RG:
   1063       case GL_COMPRESSED_RGB:
   1064       case GL_COMPRESSED_RGBA:
   1065       case GL_RGB_S3TC:
   1066       case GL_RGB4_S3TC:
   1067       case GL_RGBA_S3TC:
   1068       case GL_RGBA4_S3TC:
   1069       case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
   1070       case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
   1071       case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
   1072       case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
   1073       case GL_COMPRESSED_RGB_FXT1_3DFX:
   1074       case GL_COMPRESSED_RGBA_FXT1_3DFX:
   1075       case GL_SRGB_EXT:
   1076       case GL_SRGB8_EXT:
   1077       case GL_SRGB_ALPHA_EXT:
   1078       case GL_SRGB8_ALPHA8_EXT:
   1079       case GL_SLUMINANCE_ALPHA_EXT:
   1080       case GL_SLUMINANCE8_ALPHA8_EXT:
   1081       case GL_SLUMINANCE_EXT:
   1082       case GL_SLUMINANCE8_EXT:
   1083       case GL_COMPRESSED_SRGB_EXT:
   1084       case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
   1085       case GL_COMPRESSED_SRGB_ALPHA_EXT:
   1086       case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
   1087       case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
   1088       case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
   1089       case GL_COMPRESSED_SLUMINANCE_EXT:
   1090       case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
   1091       case GL_COMPRESSED_RED_RGTC1:
   1092       case GL_COMPRESSED_SIGNED_RED_RGTC1:
   1093       case GL_COMPRESSED_RG_RGTC2:
   1094       case GL_COMPRESSED_SIGNED_RG_RGTC2:
   1095       case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
   1096       case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
   1097       case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
   1098       case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
   1099       case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
   1100       case GL_ETC1_RGB8_OES:
   1101       case GL_COMPRESSED_RGB8_ETC2:
   1102       case GL_COMPRESSED_SRGB8_ETC2:
   1103       case GL_COMPRESSED_RGBA8_ETC2_EAC:
   1104       case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
   1105       case GL_COMPRESSED_R11_EAC:
   1106       case GL_COMPRESSED_RG11_EAC:
   1107       case GL_COMPRESSED_SIGNED_R11_EAC:
   1108       case GL_COMPRESSED_SIGNED_RG11_EAC:
   1109       case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
   1110       case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
   1111       case GL_COMPRESSED_RGBA_BPTC_UNORM:
   1112       case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
   1113       case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT:
   1114       case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
   1115       case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
   1116       case GL_COMPRESSED_RGBA_ASTC_5x4_KHR:
   1117       case GL_COMPRESSED_RGBA_ASTC_5x5_KHR:
   1118       case GL_COMPRESSED_RGBA_ASTC_6x5_KHR:
   1119       case GL_COMPRESSED_RGBA_ASTC_6x6_KHR:
   1120       case GL_COMPRESSED_RGBA_ASTC_8x5_KHR:
   1121       case GL_COMPRESSED_RGBA_ASTC_8x6_KHR:
   1122       case GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
   1123       case GL_COMPRESSED_RGBA_ASTC_10x5_KHR:
   1124       case GL_COMPRESSED_RGBA_ASTC_10x6_KHR:
   1125       case GL_COMPRESSED_RGBA_ASTC_10x8_KHR:
   1126       case GL_COMPRESSED_RGBA_ASTC_10x10_KHR:
   1127       case GL_COMPRESSED_RGBA_ASTC_12x10_KHR:
   1128       case GL_COMPRESSED_RGBA_ASTC_12x12_KHR:
   1129       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
   1130       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
   1131       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
   1132       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
   1133       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
   1134       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
   1135       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
   1136       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
   1137       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
   1138       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
   1139       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
   1140       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
   1141       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
   1142       case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
   1143       /* generic integer formats */
   1144       case GL_RED_INTEGER_EXT:
   1145       case GL_GREEN_INTEGER_EXT:
   1146       case GL_BLUE_INTEGER_EXT:
   1147       case GL_ALPHA_INTEGER_EXT:
   1148       case GL_RGB_INTEGER_EXT:
   1149       case GL_RGBA_INTEGER_EXT:
   1150       case GL_BGR_INTEGER_EXT:
   1151       case GL_BGRA_INTEGER_EXT:
   1152       case GL_RG_INTEGER:
   1153       case GL_LUMINANCE_INTEGER_EXT:
   1154       case GL_LUMINANCE_ALPHA_INTEGER_EXT:
   1155       /* sized integer formats */
   1156       case GL_RGBA32UI_EXT:
   1157       case GL_RGB32UI_EXT:
   1158       case GL_RG32UI:
   1159       case GL_R32UI:
   1160       case GL_ALPHA32UI_EXT:
   1161       case GL_INTENSITY32UI_EXT:
   1162       case GL_LUMINANCE32UI_EXT:
   1163       case GL_LUMINANCE_ALPHA32UI_EXT:
   1164       case GL_RGBA16UI_EXT:
   1165       case GL_RGB16UI_EXT:
   1166       case GL_RG16UI:
   1167       case GL_R16UI:
   1168       case GL_ALPHA16UI_EXT:
   1169       case GL_INTENSITY16UI_EXT:
   1170       case GL_LUMINANCE16UI_EXT:
   1171       case GL_LUMINANCE_ALPHA16UI_EXT:
   1172       case GL_RGBA8UI_EXT:
   1173       case GL_RGB8UI_EXT:
   1174       case GL_RG8UI:
   1175       case GL_R8UI:
   1176       case GL_ALPHA8UI_EXT:
   1177       case GL_INTENSITY8UI_EXT:
   1178       case GL_LUMINANCE8UI_EXT:
   1179       case GL_LUMINANCE_ALPHA8UI_EXT:
   1180       case GL_RGBA32I_EXT:
   1181       case GL_RGB32I_EXT:
   1182       case GL_RG32I:
   1183       case GL_R32I:
   1184       case GL_ALPHA32I_EXT:
   1185       case GL_INTENSITY32I_EXT:
   1186       case GL_LUMINANCE32I_EXT:
   1187       case GL_LUMINANCE_ALPHA32I_EXT:
   1188       case GL_RGBA16I_EXT:
   1189       case GL_RGB16I_EXT:
   1190       case GL_RG16I:
   1191       case GL_R16I:
   1192       case GL_ALPHA16I_EXT:
   1193       case GL_INTENSITY16I_EXT:
   1194       case GL_LUMINANCE16I_EXT:
   1195       case GL_LUMINANCE_ALPHA16I_EXT:
   1196       case GL_RGBA8I_EXT:
   1197       case GL_RGB8I_EXT:
   1198       case GL_RG8I:
   1199       case GL_R8I:
   1200       case GL_ALPHA8I_EXT:
   1201       case GL_INTENSITY8I_EXT:
   1202       case GL_LUMINANCE8I_EXT:
   1203       case GL_LUMINANCE_ALPHA8I_EXT:
   1204       /* signed, normalized texture formats */
   1205       case GL_RED_SNORM:
   1206       case GL_R8_SNORM:
   1207       case GL_R16_SNORM:
   1208       case GL_RG_SNORM:
   1209       case GL_RG8_SNORM:
   1210       case GL_RG16_SNORM:
   1211       case GL_RGB_SNORM:
   1212       case GL_RGB8_SNORM:
   1213       case GL_RGB16_SNORM:
   1214       case GL_RGBA_SNORM:
   1215       case GL_RGBA8_SNORM:
   1216       case GL_RGBA16_SNORM:
   1217       case GL_ALPHA_SNORM:
   1218       case GL_ALPHA8_SNORM:
   1219       case GL_ALPHA16_SNORM:
   1220       case GL_LUMINANCE_SNORM:
   1221       case GL_LUMINANCE8_SNORM:
   1222       case GL_LUMINANCE16_SNORM:
   1223       case GL_LUMINANCE_ALPHA_SNORM:
   1224       case GL_LUMINANCE8_ALPHA8_SNORM:
   1225       case GL_LUMINANCE16_ALPHA16_SNORM:
   1226       case GL_INTENSITY_SNORM:
   1227       case GL_INTENSITY8_SNORM:
   1228       case GL_INTENSITY16_SNORM:
   1229       case GL_RGB9_E5:
   1230       case GL_R11F_G11F_B10F:
   1231       case GL_RGB10_A2UI:
   1232          return GL_TRUE;
   1233       case GL_YCBCR_MESA:  /* not considered to be RGB */
   1234          /* fall-through */
   1235       default:
   1236          return GL_FALSE;
   1237    }
   1238 }
   1239 
   1240 
   1241 /**
   1242  * Test if the given image format is a depth component format.
   1243  */
   1244 GLboolean
   1245 _mesa_is_depth_format(GLenum format)
   1246 {
   1247    switch (format) {
   1248       case GL_DEPTH_COMPONENT:
   1249       case GL_DEPTH_COMPONENT16:
   1250       case GL_DEPTH_COMPONENT24:
   1251       case GL_DEPTH_COMPONENT32:
   1252       case GL_DEPTH_COMPONENT32F:
   1253          return GL_TRUE;
   1254       default:
   1255          return GL_FALSE;
   1256    }
   1257 }
   1258 
   1259 
   1260 /**
   1261  * Test if the given image format is a stencil format.
   1262  */
   1263 GLboolean
   1264 _mesa_is_stencil_format(GLenum format)
   1265 {
   1266    switch (format) {
   1267       case GL_STENCIL_INDEX:
   1268          return GL_TRUE;
   1269       default:
   1270          return GL_FALSE;
   1271    }
   1272 }
   1273 
   1274 
   1275 /**
   1276  * Test if the given image format is a YCbCr format.
   1277  */
   1278 GLboolean
   1279 _mesa_is_ycbcr_format(GLenum format)
   1280 {
   1281    switch (format) {
   1282       case GL_YCBCR_MESA:
   1283          return GL_TRUE;
   1284       default:
   1285          return GL_FALSE;
   1286    }
   1287 }
   1288 
   1289 
   1290 /**
   1291  * Test if the given image format is a depth+stencil format.
   1292  */
   1293 GLboolean
   1294 _mesa_is_depthstencil_format(GLenum format)
   1295 {
   1296    switch (format) {
   1297       case GL_DEPTH24_STENCIL8_EXT:
   1298       case GL_DEPTH_STENCIL_EXT:
   1299       case GL_DEPTH32F_STENCIL8:
   1300          return GL_TRUE;
   1301       default:
   1302          return GL_FALSE;
   1303    }
   1304 }
   1305 
   1306 
   1307 /**
   1308  * Test if the given image format is a depth or stencil format.
   1309  */
   1310 GLboolean
   1311 _mesa_is_depth_or_stencil_format(GLenum format)
   1312 {
   1313    switch (format) {
   1314       case GL_DEPTH_COMPONENT:
   1315       case GL_DEPTH_COMPONENT16:
   1316       case GL_DEPTH_COMPONENT24:
   1317       case GL_DEPTH_COMPONENT32:
   1318       case GL_STENCIL_INDEX:
   1319       case GL_STENCIL_INDEX1_EXT:
   1320       case GL_STENCIL_INDEX4_EXT:
   1321       case GL_STENCIL_INDEX8_EXT:
   1322       case GL_STENCIL_INDEX16_EXT:
   1323       case GL_DEPTH_STENCIL_EXT:
   1324       case GL_DEPTH24_STENCIL8_EXT:
   1325       case GL_DEPTH_COMPONENT32F:
   1326       case GL_DEPTH32F_STENCIL8:
   1327          return GL_TRUE;
   1328       default:
   1329          return GL_FALSE;
   1330    }
   1331 }
   1332 
   1333 
   1334 /**
   1335  * Test if an image format is a supported compressed format.
   1336  * \param format the internal format token provided by the user.
   1337  * \return GL_TRUE if compressed, GL_FALSE if uncompressed
   1338  */
   1339 GLboolean
   1340 _mesa_is_compressed_format(const struct gl_context *ctx, GLenum format)
   1341 {
   1342    mesa_format m_format = _mesa_glenum_to_compressed_format(format);
   1343 
   1344    /* Some formats in this switch have an equivalent mesa_format_layout
   1345     * to the compressed formats in the layout switch below and thus
   1346     * must be handled first.
   1347     */
   1348    switch (format) {
   1349    case GL_RGB_S3TC:
   1350    case GL_RGB4_S3TC:
   1351    case GL_RGBA_S3TC:
   1352    case GL_RGBA4_S3TC:
   1353       return _mesa_is_desktop_gl(ctx) &&
   1354          ctx->Extensions.ANGLE_texture_compression_dxt;
   1355    case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
   1356       return ctx->API == API_OPENGL_COMPAT
   1357          && ctx->Extensions.ATI_texture_compression_3dc;
   1358    case GL_PALETTE4_RGB8_OES:
   1359    case GL_PALETTE4_RGBA8_OES:
   1360    case GL_PALETTE4_R5_G6_B5_OES:
   1361    case GL_PALETTE4_RGBA4_OES:
   1362    case GL_PALETTE4_RGB5_A1_OES:
   1363    case GL_PALETTE8_RGB8_OES:
   1364    case GL_PALETTE8_RGBA8_OES:
   1365    case GL_PALETTE8_R5_G6_B5_OES:
   1366    case GL_PALETTE8_RGBA4_OES:
   1367    case GL_PALETTE8_RGB5_A1_OES:
   1368       return ctx->API == API_OPENGLES;
   1369    }
   1370 
   1371    switch (_mesa_get_format_layout(m_format)) {
   1372    case MESA_FORMAT_LAYOUT_S3TC:
   1373       if (_mesa_get_format_color_encoding(m_format) == GL_LINEAR) {
   1374          /* Assume that the ANGLE flag will always be set if the
   1375           * EXT flag is set.
   1376           */
   1377          return ctx->Extensions.ANGLE_texture_compression_dxt;
   1378       } else {
   1379          return _mesa_is_desktop_gl(ctx)
   1380             && ctx->Extensions.EXT_texture_sRGB
   1381             && ctx->Extensions.EXT_texture_compression_s3tc;
   1382       }
   1383    case MESA_FORMAT_LAYOUT_FXT1:
   1384       return _mesa_is_desktop_gl(ctx)
   1385          && ctx->Extensions.TDFX_texture_compression_FXT1;
   1386    case MESA_FORMAT_LAYOUT_RGTC:
   1387       return _mesa_is_desktop_gl(ctx)
   1388          && ctx->Extensions.ARB_texture_compression_rgtc;
   1389    case MESA_FORMAT_LAYOUT_LATC:
   1390       return ctx->API == API_OPENGL_COMPAT
   1391          && ctx->Extensions.EXT_texture_compression_latc;
   1392    case MESA_FORMAT_LAYOUT_ETC1:
   1393       return _mesa_is_gles(ctx)
   1394          && ctx->Extensions.OES_compressed_ETC1_RGB8_texture;
   1395    case MESA_FORMAT_LAYOUT_ETC2:
   1396       return _mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility;
   1397    case MESA_FORMAT_LAYOUT_BPTC:
   1398       return _mesa_is_desktop_gl(ctx) &&
   1399          ctx->Extensions.ARB_texture_compression_bptc;
   1400    case MESA_FORMAT_LAYOUT_ASTC:
   1401       return ctx->Extensions.KHR_texture_compression_astc_ldr;
   1402    default:
   1403       return GL_FALSE;
   1404    }
   1405 }
   1406 
   1407 /**
   1408  * Test if the given format represents an sRGB format.
   1409  * \param format the GL format (can be an internal format)
   1410  * \return GL_TRUE if format is sRGB, GL_FALSE otherwise
   1411  */
   1412 GLboolean
   1413 _mesa_is_srgb_format(GLenum format)
   1414 {
   1415    switch (format) {
   1416    case GL_SRGB:
   1417    case GL_SRGB8:
   1418    case GL_SRGB_ALPHA:
   1419    case GL_SRGB8_ALPHA8:
   1420    case GL_COMPRESSED_SRGB:
   1421    case GL_COMPRESSED_SRGB_ALPHA:
   1422    case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
   1423    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
   1424    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
   1425    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
   1426    case GL_COMPRESSED_SRGB8_ETC2:
   1427    case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
   1428    case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
   1429    case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
   1430    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
   1431    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
   1432    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
   1433    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
   1434    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
   1435    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
   1436    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
   1437    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
   1438    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
   1439    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
   1440    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
   1441    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
   1442    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
   1443    case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
   1444       return GL_TRUE;
   1445    default:
   1446       break;
   1447    }
   1448 
   1449    return GL_FALSE;
   1450 }
   1451 
   1452 /**
   1453  * Convert various unpack formats to the corresponding base format.
   1454  */
   1455 GLenum
   1456 _mesa_unpack_format_to_base_format(GLenum format)
   1457 {
   1458    switch(format) {
   1459    case GL_RED_INTEGER:
   1460       return GL_RED;
   1461    case GL_GREEN_INTEGER:
   1462       return GL_GREEN;
   1463    case GL_BLUE_INTEGER:
   1464       return GL_BLUE;
   1465    case GL_ALPHA_INTEGER:
   1466       return GL_ALPHA;
   1467    case GL_RG_INTEGER:
   1468       return GL_RG;
   1469    case GL_RGB_INTEGER:
   1470       return GL_RGB;
   1471    case GL_RGBA_INTEGER:
   1472       return GL_RGBA;
   1473    case GL_BGR_INTEGER:
   1474       return GL_BGR;
   1475    case GL_BGRA_INTEGER:
   1476       return GL_BGRA;
   1477    case GL_LUMINANCE_INTEGER_EXT:
   1478       return GL_LUMINANCE;
   1479    case GL_LUMINANCE_ALPHA_INTEGER_EXT:
   1480       return GL_LUMINANCE_ALPHA;
   1481    case GL_RED:
   1482    case GL_GREEN:
   1483    case GL_BLUE:
   1484    case GL_RG:
   1485    case GL_RGB:
   1486    case GL_RGBA:
   1487    case GL_BGR:
   1488    case GL_BGRA:
   1489    case GL_ALPHA:
   1490    case GL_LUMINANCE:
   1491    case GL_LUMINANCE_ALPHA:
   1492    default:
   1493       return format;
   1494    }
   1495 }
   1496 
   1497 /**
   1498  * Convert various base formats to the corresponding integer format.
   1499  */
   1500 GLenum
   1501 _mesa_base_format_to_integer_format(GLenum format)
   1502 {
   1503    switch(format) {
   1504    case GL_RED:
   1505       return GL_RED_INTEGER;
   1506    case GL_GREEN:
   1507       return GL_GREEN_INTEGER;
   1508    case GL_BLUE:
   1509       return GL_BLUE_INTEGER;
   1510    case GL_RG:
   1511       return GL_RG_INTEGER;
   1512    case GL_RGB:
   1513       return GL_RGB_INTEGER;
   1514    case GL_RGBA:
   1515       return GL_RGBA_INTEGER;
   1516    case GL_BGR:
   1517       return GL_BGR_INTEGER;
   1518    case GL_BGRA:
   1519       return GL_BGRA_INTEGER;
   1520    case GL_ALPHA:
   1521       return GL_ALPHA_INTEGER;
   1522    case GL_LUMINANCE:
   1523       return GL_LUMINANCE_INTEGER_EXT;
   1524    case GL_LUMINANCE_ALPHA:
   1525       return GL_LUMINANCE_ALPHA_INTEGER_EXT;
   1526    }
   1527 
   1528    return format;
   1529 }
   1530 
   1531 
   1532 /**
   1533  * Does the given base texture/renderbuffer format have the channel
   1534  * named by 'pname'?
   1535  */
   1536 GLboolean
   1537 _mesa_base_format_has_channel(GLenum base_format, GLenum pname)
   1538 {
   1539    switch (pname) {
   1540    case GL_TEXTURE_RED_SIZE:
   1541    case GL_TEXTURE_RED_TYPE:
   1542    case GL_RENDERBUFFER_RED_SIZE_EXT:
   1543    case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
   1544    case GL_INTERNALFORMAT_RED_SIZE:
   1545    case GL_INTERNALFORMAT_RED_TYPE:
   1546       if (base_format == GL_RED ||
   1547 	  base_format == GL_RG ||
   1548 	  base_format == GL_RGB ||
   1549 	  base_format == GL_RGBA) {
   1550 	 return GL_TRUE;
   1551       }
   1552       return GL_FALSE;
   1553    case GL_TEXTURE_GREEN_SIZE:
   1554    case GL_TEXTURE_GREEN_TYPE:
   1555    case GL_RENDERBUFFER_GREEN_SIZE_EXT:
   1556    case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
   1557    case GL_INTERNALFORMAT_GREEN_SIZE:
   1558    case GL_INTERNALFORMAT_GREEN_TYPE:
   1559       if (base_format == GL_RG ||
   1560 	  base_format == GL_RGB ||
   1561 	  base_format == GL_RGBA) {
   1562 	 return GL_TRUE;
   1563       }
   1564       return GL_FALSE;
   1565    case GL_TEXTURE_BLUE_SIZE:
   1566    case GL_TEXTURE_BLUE_TYPE:
   1567    case GL_RENDERBUFFER_BLUE_SIZE_EXT:
   1568    case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
   1569    case GL_INTERNALFORMAT_BLUE_SIZE:
   1570    case GL_INTERNALFORMAT_BLUE_TYPE:
   1571       if (base_format == GL_RGB ||
   1572 	  base_format == GL_RGBA) {
   1573 	 return GL_TRUE;
   1574       }
   1575       return GL_FALSE;
   1576    case GL_TEXTURE_ALPHA_SIZE:
   1577    case GL_TEXTURE_ALPHA_TYPE:
   1578    case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
   1579    case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
   1580    case GL_INTERNALFORMAT_ALPHA_SIZE:
   1581    case GL_INTERNALFORMAT_ALPHA_TYPE:
   1582       if (base_format == GL_RGBA ||
   1583 	  base_format == GL_ALPHA ||
   1584 	  base_format == GL_LUMINANCE_ALPHA) {
   1585 	 return GL_TRUE;
   1586       }
   1587       return GL_FALSE;
   1588    case GL_TEXTURE_LUMINANCE_SIZE:
   1589    case GL_TEXTURE_LUMINANCE_TYPE:
   1590       if (base_format == GL_LUMINANCE ||
   1591 	  base_format == GL_LUMINANCE_ALPHA) {
   1592 	 return GL_TRUE;
   1593       }
   1594       return GL_FALSE;
   1595    case GL_TEXTURE_INTENSITY_SIZE:
   1596    case GL_TEXTURE_INTENSITY_TYPE:
   1597       if (base_format == GL_INTENSITY) {
   1598 	 return GL_TRUE;
   1599       }
   1600       return GL_FALSE;
   1601    case GL_TEXTURE_DEPTH_SIZE:
   1602    case GL_TEXTURE_DEPTH_TYPE:
   1603    case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
   1604    case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
   1605    case GL_INTERNALFORMAT_DEPTH_SIZE:
   1606    case GL_INTERNALFORMAT_DEPTH_TYPE:
   1607       if (base_format == GL_DEPTH_STENCIL ||
   1608 	  base_format == GL_DEPTH_COMPONENT) {
   1609 	 return GL_TRUE;
   1610       }
   1611       return GL_FALSE;
   1612    case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
   1613    case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
   1614    case GL_INTERNALFORMAT_STENCIL_SIZE:
   1615    case GL_INTERNALFORMAT_STENCIL_TYPE:
   1616       if (base_format == GL_DEPTH_STENCIL ||
   1617 	  base_format == GL_STENCIL_INDEX) {
   1618 	 return GL_TRUE;
   1619       }
   1620       return GL_FALSE;
   1621    default:
   1622       _mesa_warning(NULL, "%s: Unexpected channel token 0x%x\n",
   1623 		    __func__, pname);
   1624       return GL_FALSE;
   1625    }
   1626 
   1627    return GL_FALSE;
   1628 }
   1629 
   1630 
   1631 /**
   1632  * Returns the number of channels/components for a base format.
   1633  */
   1634 GLint
   1635 _mesa_base_format_component_count(GLenum base_format)
   1636 {
   1637    switch (base_format) {
   1638    case GL_LUMINANCE:
   1639    case GL_RED:
   1640    case GL_ALPHA:
   1641    case GL_INTENSITY:
   1642    case GL_DEPTH_COMPONENT:
   1643       return 1;
   1644    case GL_RG:
   1645    case GL_LUMINANCE_ALPHA:
   1646    case GL_DEPTH_STENCIL:
   1647       return 2;
   1648    case GL_RGB:
   1649       return 3;
   1650    case GL_RGBA:
   1651       return 4;
   1652    default:
   1653       return -1;
   1654    }
   1655 }
   1656 
   1657 
   1658 /**
   1659  * If format is a generic compressed format, return the corresponding
   1660  * non-compressed format.  For other formats, return the format as-is.
   1661  */
   1662 GLenum
   1663 _mesa_generic_compressed_format_to_uncompressed_format(GLenum format)
   1664 {
   1665    switch (format) {
   1666    case GL_COMPRESSED_RED:
   1667       return GL_RED;
   1668    case GL_COMPRESSED_RG:
   1669       return GL_RG;
   1670    case GL_COMPRESSED_RGB:
   1671       return GL_RGB;
   1672    case GL_COMPRESSED_RGBA:
   1673       return GL_RGBA;
   1674    case GL_COMPRESSED_ALPHA:
   1675       return GL_ALPHA;
   1676    case GL_COMPRESSED_LUMINANCE:
   1677       return GL_LUMINANCE;
   1678    case GL_COMPRESSED_LUMINANCE_ALPHA:
   1679       return GL_LUMINANCE_ALPHA;
   1680    case GL_COMPRESSED_INTENSITY:
   1681       return GL_INTENSITY;
   1682    /* sRGB formats */
   1683    case GL_COMPRESSED_SRGB:
   1684       return GL_SRGB;
   1685    case GL_COMPRESSED_SRGB_ALPHA:
   1686       return GL_SRGB_ALPHA;
   1687    case GL_COMPRESSED_SLUMINANCE:
   1688       return GL_SLUMINANCE;
   1689    case GL_COMPRESSED_SLUMINANCE_ALPHA:
   1690       return GL_SLUMINANCE_ALPHA;
   1691    default:
   1692       return format;
   1693    }
   1694 }
   1695 
   1696 
   1697 /**
   1698  * Return the equivalent non-generic internal format.
   1699  * This is useful for comparing whether two internal formats are equivalent.
   1700  */
   1701 GLenum
   1702 _mesa_get_nongeneric_internalformat(GLenum format)
   1703 {
   1704    switch (format) {
   1705    /* GL 1.1 formats. */
   1706    case 4:
   1707    case GL_RGBA:
   1708       return GL_RGBA8;
   1709    case 3:
   1710    case GL_RGB:
   1711       return GL_RGB8;
   1712    case 2:
   1713    case GL_LUMINANCE_ALPHA:
   1714       return GL_LUMINANCE8_ALPHA8;
   1715    case 1:
   1716    case GL_LUMINANCE:
   1717       return GL_LUMINANCE8;
   1718    case GL_ALPHA:
   1719       return GL_ALPHA8;
   1720    case GL_INTENSITY:
   1721       return GL_INTENSITY8;
   1722 
   1723    /* GL_ARB_texture_rg */
   1724    case GL_RED:
   1725       return GL_R8;
   1726    case GL_RG:
   1727       return GL_RG8;
   1728 
   1729    /* GL_EXT_texture_sRGB */
   1730    case GL_SRGB:
   1731       return GL_SRGB8;
   1732    case GL_SRGB_ALPHA:
   1733       return GL_SRGB8_ALPHA8;
   1734    case GL_SLUMINANCE:
   1735       return GL_SLUMINANCE8;
   1736    case GL_SLUMINANCE_ALPHA:
   1737       return GL_SLUMINANCE8_ALPHA8;
   1738 
   1739    /* GL_EXT_texture_snorm */
   1740    case GL_RGBA_SNORM:
   1741       return GL_RGBA8_SNORM;
   1742    case GL_RGB_SNORM:
   1743       return GL_RGB8_SNORM;
   1744    case GL_RG_SNORM:
   1745       return GL_RG8_SNORM;
   1746    case GL_RED_SNORM:
   1747       return GL_R8_SNORM;
   1748    case GL_LUMINANCE_ALPHA_SNORM:
   1749       return GL_LUMINANCE8_ALPHA8_SNORM;
   1750    case GL_LUMINANCE_SNORM:
   1751       return GL_LUMINANCE8_SNORM;
   1752    case GL_ALPHA_SNORM:
   1753       return GL_ALPHA8_SNORM;
   1754    case GL_INTENSITY_SNORM:
   1755       return GL_INTENSITY8_SNORM;
   1756 
   1757    default:
   1758       return format;
   1759    }
   1760 }
   1761 
   1762 
   1763 /**
   1764  * Convert an sRGB internal format to linear.
   1765  */
   1766 GLenum
   1767 _mesa_get_linear_internalformat(GLenum format)
   1768 {
   1769    switch (format) {
   1770    case GL_SRGB:
   1771       return GL_RGB;
   1772    case GL_SRGB_ALPHA:
   1773       return GL_RGBA;
   1774    case GL_SRGB8:
   1775       return GL_RGB8;
   1776    case GL_SRGB8_ALPHA8:
   1777       return GL_RGBA8;
   1778    case GL_SLUMINANCE8:
   1779       return GL_LUMINANCE8;
   1780    case GL_SLUMINANCE:
   1781       return GL_LUMINANCE;
   1782    case GL_SLUMINANCE_ALPHA:
   1783       return GL_LUMINANCE_ALPHA;
   1784    case GL_SLUMINANCE8_ALPHA8:
   1785       return GL_LUMINANCE8_ALPHA8;
   1786    default:
   1787       return format;
   1788    }
   1789 }
   1790 
   1791 
   1792 /**
   1793  * Do error checking of format/type combinations for glReadPixels,
   1794  * glDrawPixels and glTex[Sub]Image.  Note that depending on the format
   1795  * and type values, we may either generate GL_INVALID_OPERATION or
   1796  * GL_INVALID_ENUM.
   1797  *
   1798  * \param format pixel format.
   1799  * \param type pixel type.
   1800  *
   1801  * \return GL_INVALID_ENUM, GL_INVALID_OPERATION or GL_NO_ERROR
   1802  */
   1803 GLenum
   1804 _mesa_error_check_format_and_type(const struct gl_context *ctx,
   1805                                   GLenum format, GLenum type)
   1806 {
   1807    /* From OpenGL 3.3 spec, page 220:
   1808     *    "If the format is DEPTH_STENCIL, then values are taken from
   1809     *    both the depth buffer and the stencil buffer. If there is no
   1810     *    depth buffer or if there is no stencil buffer, then the error
   1811     *    INVALID_OPERATION occurs. If the type parameter is not
   1812     *    UNSIGNED_INT_24_8 or FLOAT_32_UNSIGNED_INT_24_8_REV, then the
   1813     *    error INVALID_ENUM occurs."
   1814     *
   1815     * OpenGL ES still generates GL_INVALID_OPERATION because glReadPixels
   1816     * cannot be used to read depth or stencil in that API.
   1817     */
   1818    if (_mesa_is_desktop_gl(ctx) && format == GL_DEPTH_STENCIL
   1819        && type != GL_UNSIGNED_INT_24_8
   1820        && type != GL_FLOAT_32_UNSIGNED_INT_24_8_REV)
   1821       return GL_INVALID_ENUM;
   1822 
   1823    /* special type-based checks (see glReadPixels, glDrawPixels error lists) */
   1824    switch (type) {
   1825    case GL_BITMAP:
   1826       if (format != GL_COLOR_INDEX && format != GL_STENCIL_INDEX) {
   1827          return GL_INVALID_ENUM;
   1828       }
   1829       break;
   1830 
   1831    case GL_UNSIGNED_BYTE_3_3_2:
   1832    case GL_UNSIGNED_BYTE_2_3_3_REV:
   1833    case GL_UNSIGNED_SHORT_5_6_5:
   1834    case GL_UNSIGNED_SHORT_5_6_5_REV:
   1835       if (format == GL_RGB) {
   1836          break; /* OK */
   1837       }
   1838       if (format == GL_RGB_INTEGER_EXT &&
   1839           ctx->Extensions.ARB_texture_rgb10_a2ui) {
   1840          break; /* OK */
   1841       }
   1842       return GL_INVALID_OPERATION;
   1843 
   1844    case GL_UNSIGNED_SHORT_4_4_4_4:
   1845    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
   1846    case GL_UNSIGNED_INT_8_8_8_8:
   1847    case GL_UNSIGNED_INT_8_8_8_8_REV:
   1848       if (format == GL_RGBA ||
   1849           format == GL_BGRA ||
   1850           format == GL_ABGR_EXT) {
   1851          break; /* OK */
   1852       }
   1853       if ((format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT) &&
   1854           ctx->Extensions.ARB_texture_rgb10_a2ui) {
   1855          break; /* OK */
   1856       }
   1857       return GL_INVALID_OPERATION;
   1858 
   1859    case GL_UNSIGNED_SHORT_5_5_5_1:
   1860    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
   1861    case GL_UNSIGNED_INT_10_10_10_2:
   1862    case GL_UNSIGNED_INT_2_10_10_10_REV:
   1863       if (format == GL_RGBA ||
   1864           format == GL_BGRA) {
   1865          break; /* OK */
   1866       }
   1867       if ((format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT) &&
   1868           ctx->Extensions.ARB_texture_rgb10_a2ui) {
   1869          break; /* OK */
   1870       }
   1871       if (type == GL_UNSIGNED_INT_2_10_10_10_REV && format == GL_RGB &&
   1872           ctx->API == API_OPENGLES2) {
   1873          break; /* OK by GL_EXT_texture_type_2_10_10_10_REV */
   1874       }
   1875       return GL_INVALID_OPERATION;
   1876 
   1877    case GL_UNSIGNED_INT_24_8:
   1878       /* Depth buffer OK to read in OpenGL ES (NV_read_depth). */
   1879       if (ctx->API == API_OPENGLES2 && format == GL_DEPTH_COMPONENT)
   1880          return GL_NO_ERROR;
   1881 
   1882       if (format != GL_DEPTH_STENCIL) {
   1883          return GL_INVALID_OPERATION;
   1884       }
   1885       return GL_NO_ERROR;
   1886 
   1887    case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
   1888       if (!ctx->Extensions.ARB_depth_buffer_float) {
   1889          return GL_INVALID_ENUM;
   1890       }
   1891       if (format != GL_DEPTH_STENCIL) {
   1892          return GL_INVALID_OPERATION;
   1893       }
   1894       return GL_NO_ERROR;
   1895 
   1896    case GL_UNSIGNED_INT_10F_11F_11F_REV:
   1897       if (!ctx->Extensions.EXT_packed_float) {
   1898          return GL_INVALID_ENUM;
   1899       }
   1900       if (format != GL_RGB) {
   1901          return GL_INVALID_OPERATION;
   1902       }
   1903       return GL_NO_ERROR;
   1904 
   1905    case GL_HALF_FLOAT_OES:
   1906       switch (format) {
   1907       case GL_RGBA:
   1908       case GL_RGB:
   1909       case GL_LUMINANCE_ALPHA:
   1910       case GL_LUMINANCE:
   1911       case GL_ALPHA:
   1912          return GL_NO_ERROR;
   1913       case GL_RG:
   1914       case GL_RED:
   1915 	 if (_mesa_is_gles3(ctx) || ctx->Extensions.ARB_texture_rg)
   1916             return GL_NO_ERROR;
   1917       default:
   1918          return GL_INVALID_OPERATION;
   1919       }
   1920 
   1921    default:
   1922       ; /* fall-through */
   1923    }
   1924 
   1925    /* now, for each format, check the type for compatibility */
   1926    switch (format) {
   1927       case GL_COLOR_INDEX:
   1928       case GL_STENCIL_INDEX:
   1929          switch (type) {
   1930             case GL_BITMAP:
   1931             case GL_BYTE:
   1932             case GL_UNSIGNED_BYTE:
   1933             case GL_SHORT:
   1934             case GL_UNSIGNED_SHORT:
   1935             case GL_INT:
   1936             case GL_UNSIGNED_INT:
   1937             case GL_FLOAT:
   1938             case GL_HALF_FLOAT:
   1939                return GL_NO_ERROR;
   1940             default:
   1941                return GL_INVALID_ENUM;
   1942          }
   1943 
   1944       case GL_RED:
   1945       case GL_GREEN:
   1946       case GL_BLUE:
   1947       case GL_ALPHA:
   1948 #if 0 /* not legal!  see table 3.6 of the 1.5 spec */
   1949       case GL_INTENSITY:
   1950 #endif
   1951       case GL_LUMINANCE:
   1952       case GL_LUMINANCE_ALPHA:
   1953       case GL_DEPTH_COMPONENT:
   1954          switch (type) {
   1955             case GL_BYTE:
   1956             case GL_UNSIGNED_BYTE:
   1957             case GL_SHORT:
   1958             case GL_UNSIGNED_SHORT:
   1959             case GL_INT:
   1960             case GL_UNSIGNED_INT:
   1961             case GL_FLOAT:
   1962             case GL_HALF_FLOAT:
   1963                return GL_NO_ERROR;
   1964             default:
   1965                return GL_INVALID_ENUM;
   1966          }
   1967 
   1968       case GL_RG:
   1969 	 if (!ctx->Extensions.ARB_texture_rg)
   1970 	    return GL_INVALID_ENUM;
   1971          switch (type) {
   1972             case GL_BYTE:
   1973             case GL_UNSIGNED_BYTE:
   1974             case GL_SHORT:
   1975             case GL_UNSIGNED_SHORT:
   1976             case GL_INT:
   1977             case GL_UNSIGNED_INT:
   1978             case GL_FLOAT:
   1979             case GL_HALF_FLOAT:
   1980                return GL_NO_ERROR;
   1981             default:
   1982                return GL_INVALID_ENUM;
   1983          }
   1984 
   1985       case GL_RGB:
   1986          switch (type) {
   1987             case GL_BYTE:
   1988             case GL_UNSIGNED_BYTE:
   1989             case GL_SHORT:
   1990             case GL_UNSIGNED_SHORT:
   1991             case GL_INT:
   1992             case GL_UNSIGNED_INT:
   1993             case GL_FLOAT:
   1994             case GL_UNSIGNED_BYTE_3_3_2:
   1995             case GL_UNSIGNED_BYTE_2_3_3_REV:
   1996             case GL_UNSIGNED_SHORT_5_6_5:
   1997             case GL_UNSIGNED_SHORT_5_6_5_REV:
   1998             case GL_HALF_FLOAT:
   1999                return GL_NO_ERROR;
   2000             case GL_UNSIGNED_INT_2_10_10_10_REV:
   2001                /* OK by GL_EXT_texture_type_2_10_10_10_REV */
   2002                return (ctx->API == API_OPENGLES2)
   2003                   ? GL_NO_ERROR : GL_INVALID_ENUM;
   2004             case GL_UNSIGNED_INT_5_9_9_9_REV:
   2005                return ctx->Extensions.EXT_texture_shared_exponent
   2006                   ? GL_NO_ERROR : GL_INVALID_ENUM;
   2007             case GL_UNSIGNED_INT_10F_11F_11F_REV:
   2008                return ctx->Extensions.EXT_packed_float
   2009                   ? GL_NO_ERROR : GL_INVALID_ENUM;
   2010             default:
   2011                return GL_INVALID_ENUM;
   2012          }
   2013 
   2014       case GL_BGR:
   2015          switch (type) {
   2016             /* NOTE: no packed types are supported with BGR.  That's
   2017              * intentional, according to the GL spec.
   2018              */
   2019             case GL_BYTE:
   2020             case GL_UNSIGNED_BYTE:
   2021             case GL_SHORT:
   2022             case GL_UNSIGNED_SHORT:
   2023             case GL_INT:
   2024             case GL_UNSIGNED_INT:
   2025             case GL_FLOAT:
   2026             case GL_HALF_FLOAT:
   2027                return GL_NO_ERROR;
   2028             default:
   2029                return GL_INVALID_ENUM;
   2030          }
   2031 
   2032       case GL_RGBA:
   2033       case GL_BGRA:
   2034          switch (type) {
   2035             case GL_BYTE:
   2036             case GL_UNSIGNED_BYTE:
   2037             case GL_SHORT:
   2038             case GL_UNSIGNED_SHORT:
   2039             case GL_INT:
   2040             case GL_UNSIGNED_INT:
   2041             case GL_FLOAT:
   2042             case GL_UNSIGNED_SHORT_4_4_4_4:
   2043             case GL_UNSIGNED_SHORT_4_4_4_4_REV:
   2044             case GL_UNSIGNED_SHORT_5_5_5_1:
   2045             case GL_UNSIGNED_SHORT_1_5_5_5_REV:
   2046             case GL_UNSIGNED_INT_8_8_8_8:
   2047             case GL_UNSIGNED_INT_8_8_8_8_REV:
   2048             case GL_UNSIGNED_INT_10_10_10_2:
   2049             case GL_UNSIGNED_INT_2_10_10_10_REV:
   2050             case GL_HALF_FLOAT:
   2051                return GL_NO_ERROR;
   2052             default:
   2053                return GL_INVALID_ENUM;
   2054          }
   2055 
   2056       case GL_ABGR_EXT:
   2057          switch (type) {
   2058             case GL_BYTE:
   2059             case GL_UNSIGNED_BYTE:
   2060             case GL_SHORT:
   2061             case GL_UNSIGNED_SHORT:
   2062             case GL_INT:
   2063             case GL_UNSIGNED_INT:
   2064             case GL_FLOAT:
   2065             case GL_UNSIGNED_SHORT_4_4_4_4:
   2066             case GL_UNSIGNED_SHORT_4_4_4_4_REV:
   2067             case GL_UNSIGNED_INT_8_8_8_8:
   2068             case GL_UNSIGNED_INT_8_8_8_8_REV:
   2069             case GL_HALF_FLOAT:
   2070                return GL_NO_ERROR;
   2071             default:
   2072                return GL_INVALID_ENUM;
   2073          }
   2074 
   2075       case GL_YCBCR_MESA:
   2076          if (!ctx->Extensions.MESA_ycbcr_texture)
   2077             return GL_INVALID_ENUM;
   2078          if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
   2079              type == GL_UNSIGNED_SHORT_8_8_REV_MESA)
   2080             return GL_NO_ERROR;
   2081          else
   2082             return GL_INVALID_OPERATION;
   2083 
   2084       case GL_DEPTH_STENCIL:
   2085          if (type == GL_UNSIGNED_INT_24_8)
   2086             return GL_NO_ERROR;
   2087          else if (ctx->Extensions.ARB_depth_buffer_float &&
   2088              type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV)
   2089             return GL_NO_ERROR;
   2090          else
   2091             return GL_INVALID_ENUM;
   2092 
   2093       /* integer-valued formats */
   2094       case GL_RED_INTEGER_EXT:
   2095       case GL_GREEN_INTEGER_EXT:
   2096       case GL_BLUE_INTEGER_EXT:
   2097       case GL_ALPHA_INTEGER_EXT:
   2098       case GL_RG_INTEGER:
   2099          switch (type) {
   2100             case GL_BYTE:
   2101             case GL_UNSIGNED_BYTE:
   2102             case GL_SHORT:
   2103             case GL_UNSIGNED_SHORT:
   2104             case GL_INT:
   2105             case GL_UNSIGNED_INT:
   2106                return (ctx->Version >= 30 ||
   2107                        ctx->Extensions.EXT_texture_integer)
   2108                   ? GL_NO_ERROR : GL_INVALID_ENUM;
   2109             default:
   2110                return GL_INVALID_ENUM;
   2111          }
   2112 
   2113       case GL_RGB_INTEGER_EXT:
   2114          switch (type) {
   2115             case GL_BYTE:
   2116             case GL_UNSIGNED_BYTE:
   2117             case GL_SHORT:
   2118             case GL_UNSIGNED_SHORT:
   2119             case GL_INT:
   2120             case GL_UNSIGNED_INT:
   2121                return (ctx->Version >= 30 ||
   2122                        ctx->Extensions.EXT_texture_integer)
   2123                   ? GL_NO_ERROR : GL_INVALID_ENUM;
   2124             case GL_UNSIGNED_BYTE_3_3_2:
   2125             case GL_UNSIGNED_BYTE_2_3_3_REV:
   2126             case GL_UNSIGNED_SHORT_5_6_5:
   2127             case GL_UNSIGNED_SHORT_5_6_5_REV:
   2128                return ctx->Extensions.ARB_texture_rgb10_a2ui
   2129                   ? GL_NO_ERROR : GL_INVALID_ENUM;
   2130             default:
   2131                return GL_INVALID_ENUM;
   2132          }
   2133 
   2134       case GL_BGR_INTEGER_EXT:
   2135          switch (type) {
   2136             case GL_BYTE:
   2137             case GL_UNSIGNED_BYTE:
   2138             case GL_SHORT:
   2139             case GL_UNSIGNED_SHORT:
   2140             case GL_INT:
   2141             case GL_UNSIGNED_INT:
   2142             /* NOTE: no packed formats w/ BGR format */
   2143                return (ctx->Version >= 30 ||
   2144                        ctx->Extensions.EXT_texture_integer)
   2145                   ? GL_NO_ERROR : GL_INVALID_ENUM;
   2146             default:
   2147                return GL_INVALID_ENUM;
   2148          }
   2149 
   2150       case GL_RGBA_INTEGER_EXT:
   2151       case GL_BGRA_INTEGER_EXT:
   2152          switch (type) {
   2153             case GL_BYTE:
   2154             case GL_UNSIGNED_BYTE:
   2155             case GL_SHORT:
   2156             case GL_UNSIGNED_SHORT:
   2157             case GL_INT:
   2158             case GL_UNSIGNED_INT:
   2159                return (ctx->Version >= 30 ||
   2160                        ctx->Extensions.EXT_texture_integer)
   2161                   ? GL_NO_ERROR : GL_INVALID_ENUM;
   2162             case GL_UNSIGNED_SHORT_4_4_4_4:
   2163             case GL_UNSIGNED_SHORT_4_4_4_4_REV:
   2164             case GL_UNSIGNED_SHORT_5_5_5_1:
   2165             case GL_UNSIGNED_SHORT_1_5_5_5_REV:
   2166             case GL_UNSIGNED_INT_8_8_8_8:
   2167             case GL_UNSIGNED_INT_8_8_8_8_REV:
   2168             case GL_UNSIGNED_INT_10_10_10_2:
   2169             case GL_UNSIGNED_INT_2_10_10_10_REV:
   2170                return ctx->Extensions.ARB_texture_rgb10_a2ui
   2171                   ? GL_NO_ERROR : GL_INVALID_ENUM;
   2172             default:
   2173                return GL_INVALID_ENUM;
   2174          }
   2175 
   2176       case GL_LUMINANCE_INTEGER_EXT:
   2177       case GL_LUMINANCE_ALPHA_INTEGER_EXT:
   2178          switch (type) {
   2179             case GL_BYTE:
   2180             case GL_UNSIGNED_BYTE:
   2181             case GL_SHORT:
   2182             case GL_UNSIGNED_SHORT:
   2183             case GL_INT:
   2184             case GL_UNSIGNED_INT:
   2185                return ctx->Extensions.EXT_texture_integer
   2186                   ? GL_NO_ERROR : GL_INVALID_ENUM;
   2187             default:
   2188                return GL_INVALID_ENUM;
   2189          }
   2190 
   2191       default:
   2192          return GL_INVALID_ENUM;
   2193    }
   2194    return GL_NO_ERROR;
   2195 }
   2196 
   2197 
   2198 /**
   2199  * Do error checking of format/type combinations for OpenGL ES glReadPixels
   2200  * and glTex[Sub]Image.
   2201  * \return error code, or GL_NO_ERROR.
   2202  */
   2203 GLenum
   2204 _mesa_es_error_check_format_and_type(const struct gl_context *ctx,
   2205                                      GLenum format, GLenum type,
   2206                                      unsigned dimensions)
   2207 {
   2208    GLboolean type_valid = GL_TRUE;
   2209 
   2210    switch (format) {
   2211    case GL_RED:
   2212    case GL_RG:
   2213       if (ctx->API == API_OPENGLES || !ctx->Extensions.ARB_texture_rg)
   2214          return GL_INVALID_VALUE;
   2215       /* fallthrough */
   2216    case GL_ALPHA:
   2217    case GL_LUMINANCE:
   2218    case GL_LUMINANCE_ALPHA:
   2219       type_valid = (type == GL_UNSIGNED_BYTE
   2220                     || type == GL_FLOAT
   2221                     || type == GL_HALF_FLOAT_OES);
   2222       break;
   2223 
   2224    case GL_RGB:
   2225       type_valid = (type == GL_UNSIGNED_BYTE
   2226                     || type == GL_UNSIGNED_SHORT_5_6_5
   2227                     || type == GL_FLOAT
   2228                     || type == GL_HALF_FLOAT_OES);
   2229       break;
   2230 
   2231    case GL_RGBA:
   2232       type_valid = (type == GL_UNSIGNED_BYTE
   2233                     || type == GL_UNSIGNED_SHORT_4_4_4_4
   2234                     || type == GL_UNSIGNED_SHORT_5_5_5_1
   2235                     || type == GL_FLOAT
   2236                     || type == GL_HALF_FLOAT_OES
   2237                     || type == GL_UNSIGNED_INT_2_10_10_10_REV);
   2238       break;
   2239 
   2240    case GL_DEPTH_COMPONENT:
   2241       /* This format is filtered against invalid dimensionalities elsewhere.
   2242        */
   2243       type_valid = (type == GL_UNSIGNED_SHORT
   2244                     || type == GL_UNSIGNED_INT);
   2245       break;
   2246 
   2247    case GL_DEPTH_STENCIL:
   2248       /* This format is filtered against invalid dimensionalities elsewhere.
   2249        */
   2250       type_valid = (type == GL_UNSIGNED_INT_24_8);
   2251       break;
   2252 
   2253    case GL_BGRA_EXT:
   2254       type_valid = (type == GL_UNSIGNED_BYTE);
   2255 
   2256       /* This feels like a bug in the EXT_texture_format_BGRA8888 spec, but
   2257        * the format does not appear to be allowed for 3D textures in OpenGL
   2258        * ES.
   2259        */
   2260       if (dimensions != 2)
   2261          return GL_INVALID_VALUE;
   2262 
   2263       break;
   2264 
   2265    default:
   2266       return GL_INVALID_VALUE;
   2267    }
   2268 
   2269    return type_valid ? GL_NO_ERROR : GL_INVALID_OPERATION;
   2270 }
   2271 
   2272 /**
   2273  * Return the simple base format for a given internal texture format.
   2274  * For example, given GL_LUMINANCE12_ALPHA4, return GL_LUMINANCE_ALPHA.
   2275  *
   2276  * \param ctx GL context.
   2277  * \param internalFormat the internal texture format token or 1, 2, 3, or 4.
   2278  *
   2279  * \return the corresponding \u base internal format (GL_ALPHA, GL_LUMINANCE,
   2280  * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA), or -1 if invalid enum.
   2281  *
   2282  * This is the format which is used during texture application (i.e. the
   2283  * texture format and env mode determine the arithmetic used.
   2284  */
   2285 GLint
   2286 _mesa_base_tex_format(const struct gl_context *ctx, GLint internalFormat)
   2287 {
   2288    switch (internalFormat) {
   2289    case GL_ALPHA:
   2290    case GL_ALPHA4:
   2291    case GL_ALPHA8:
   2292    case GL_ALPHA12:
   2293    case GL_ALPHA16:
   2294       return (ctx->API != API_OPENGL_CORE) ? GL_ALPHA : -1;
   2295    case 1:
   2296    case GL_LUMINANCE:
   2297    case GL_LUMINANCE4:
   2298    case GL_LUMINANCE8:
   2299    case GL_LUMINANCE12:
   2300    case GL_LUMINANCE16:
   2301       return (ctx->API != API_OPENGL_CORE) ? GL_LUMINANCE : -1;
   2302    case 2:
   2303    case GL_LUMINANCE_ALPHA:
   2304    case GL_LUMINANCE4_ALPHA4:
   2305    case GL_LUMINANCE6_ALPHA2:
   2306    case GL_LUMINANCE8_ALPHA8:
   2307    case GL_LUMINANCE12_ALPHA4:
   2308    case GL_LUMINANCE12_ALPHA12:
   2309    case GL_LUMINANCE16_ALPHA16:
   2310       return (ctx->API != API_OPENGL_CORE) ? GL_LUMINANCE_ALPHA : -1;
   2311    case GL_INTENSITY:
   2312    case GL_INTENSITY4:
   2313    case GL_INTENSITY8:
   2314    case GL_INTENSITY12:
   2315    case GL_INTENSITY16:
   2316       return (ctx->API != API_OPENGL_CORE) ? GL_INTENSITY : -1;
   2317    case 3:
   2318       return (ctx->API != API_OPENGL_CORE) ? GL_RGB : -1;
   2319    case GL_RGB:
   2320    case GL_R3_G3_B2:
   2321    case GL_RGB4:
   2322    case GL_RGB5:
   2323    case GL_RGB8:
   2324    case GL_RGB10:
   2325    case GL_RGB12:
   2326    case GL_RGB16:
   2327       return GL_RGB;
   2328    case 4:
   2329       return (ctx->API != API_OPENGL_CORE) ? GL_RGBA : -1;
   2330    case GL_RGBA:
   2331    case GL_RGBA2:
   2332    case GL_RGBA4:
   2333    case GL_RGB5_A1:
   2334    case GL_RGBA8:
   2335    case GL_RGB10_A2:
   2336    case GL_RGBA12:
   2337    case GL_RGBA16:
   2338       return GL_RGBA;
   2339    default:
   2340       ; /* fallthrough */
   2341    }
   2342 
   2343    /* GL_BGRA can be an internal format *only* in OpenGL ES (1.x or 2.0).
   2344     */
   2345    if (_mesa_is_gles(ctx)) {
   2346       switch (internalFormat) {
   2347       case GL_BGRA:
   2348          return GL_RGBA;
   2349       default:
   2350          ; /* fallthrough */
   2351       }
   2352    }
   2353 
   2354    if (ctx->Extensions.ARB_ES2_compatibility) {
   2355       switch (internalFormat) {
   2356       case GL_RGB565:
   2357          return GL_RGB;
   2358       default:
   2359          ; /* fallthrough */
   2360       }
   2361    }
   2362 
   2363    if (ctx->Extensions.ARB_depth_texture) {
   2364       switch (internalFormat) {
   2365       case GL_DEPTH_COMPONENT:
   2366       case GL_DEPTH_COMPONENT16:
   2367       case GL_DEPTH_COMPONENT24:
   2368       case GL_DEPTH_COMPONENT32:
   2369          return GL_DEPTH_COMPONENT;
   2370       case GL_DEPTH_STENCIL:
   2371       case GL_DEPTH24_STENCIL8:
   2372          return GL_DEPTH_STENCIL;
   2373       default:
   2374          ; /* fallthrough */
   2375       }
   2376    }
   2377 
   2378    if (ctx->Extensions.ARB_texture_stencil8) {
   2379       switch (internalFormat) {
   2380       case GL_STENCIL_INDEX:
   2381       case GL_STENCIL_INDEX1:
   2382       case GL_STENCIL_INDEX4:
   2383       case GL_STENCIL_INDEX8:
   2384       case GL_STENCIL_INDEX16:
   2385          return GL_STENCIL_INDEX;
   2386       default:
   2387          ; /* fallthrough */
   2388       }
   2389    }
   2390 
   2391    switch (internalFormat) {
   2392    case GL_COMPRESSED_ALPHA:
   2393       return GL_ALPHA;
   2394    case GL_COMPRESSED_LUMINANCE:
   2395       return GL_LUMINANCE;
   2396    case GL_COMPRESSED_LUMINANCE_ALPHA:
   2397       return GL_LUMINANCE_ALPHA;
   2398    case GL_COMPRESSED_INTENSITY:
   2399       return GL_INTENSITY;
   2400    case GL_COMPRESSED_RGB:
   2401       return GL_RGB;
   2402    case GL_COMPRESSED_RGBA:
   2403       return GL_RGBA;
   2404    default:
   2405       ; /* fallthrough */
   2406    }
   2407 
   2408    if (_mesa_is_compressed_format(ctx, internalFormat)) {
   2409       GLenum base_compressed =
   2410          _mesa_gl_compressed_format_base_format(internalFormat);
   2411       if (base_compressed)
   2412             return base_compressed;
   2413    }
   2414 
   2415    if ((ctx->Extensions.KHR_texture_compression_astc_ldr &&
   2416         is_astc_2d_format(internalFormat)) ||
   2417        (ctx->Extensions.OES_texture_compression_astc &&
   2418         is_astc_3d_format(internalFormat)))
   2419         return GL_RGBA;
   2420 
   2421    if (ctx->Extensions.MESA_ycbcr_texture) {
   2422       if (internalFormat == GL_YCBCR_MESA)
   2423          return GL_YCBCR_MESA;
   2424    }
   2425 
   2426    if (ctx->Extensions.ARB_texture_float) {
   2427       switch (internalFormat) {
   2428       case GL_ALPHA16F_ARB:
   2429       case GL_ALPHA32F_ARB:
   2430          return GL_ALPHA;
   2431       case GL_RGBA16F_ARB:
   2432       case GL_RGBA32F_ARB:
   2433          return GL_RGBA;
   2434       case GL_RGB16F_ARB:
   2435       case GL_RGB32F_ARB:
   2436          return GL_RGB;
   2437       case GL_INTENSITY16F_ARB:
   2438       case GL_INTENSITY32F_ARB:
   2439          return GL_INTENSITY;
   2440       case GL_LUMINANCE16F_ARB:
   2441       case GL_LUMINANCE32F_ARB:
   2442          return GL_LUMINANCE;
   2443       case GL_LUMINANCE_ALPHA16F_ARB:
   2444       case GL_LUMINANCE_ALPHA32F_ARB:
   2445          return GL_LUMINANCE_ALPHA;
   2446       default:
   2447          ; /* fallthrough */
   2448       }
   2449    }
   2450 
   2451    if (ctx->Extensions.EXT_texture_snorm) {
   2452       switch (internalFormat) {
   2453       case GL_RED_SNORM:
   2454       case GL_R8_SNORM:
   2455       case GL_R16_SNORM:
   2456          return GL_RED;
   2457       case GL_RG_SNORM:
   2458       case GL_RG8_SNORM:
   2459       case GL_RG16_SNORM:
   2460          return GL_RG;
   2461       case GL_RGB_SNORM:
   2462       case GL_RGB8_SNORM:
   2463       case GL_RGB16_SNORM:
   2464          return GL_RGB;
   2465       case GL_RGBA_SNORM:
   2466       case GL_RGBA8_SNORM:
   2467       case GL_RGBA16_SNORM:
   2468          return GL_RGBA;
   2469       case GL_ALPHA_SNORM:
   2470       case GL_ALPHA8_SNORM:
   2471       case GL_ALPHA16_SNORM:
   2472          return GL_ALPHA;
   2473       case GL_LUMINANCE_SNORM:
   2474       case GL_LUMINANCE8_SNORM:
   2475       case GL_LUMINANCE16_SNORM:
   2476          return GL_LUMINANCE;
   2477       case GL_LUMINANCE_ALPHA_SNORM:
   2478       case GL_LUMINANCE8_ALPHA8_SNORM:
   2479       case GL_LUMINANCE16_ALPHA16_SNORM:
   2480          return GL_LUMINANCE_ALPHA;
   2481       case GL_INTENSITY_SNORM:
   2482       case GL_INTENSITY8_SNORM:
   2483       case GL_INTENSITY16_SNORM:
   2484          return GL_INTENSITY;
   2485       default:
   2486          ; /* fallthrough */
   2487       }
   2488    }
   2489 
   2490    if (ctx->Extensions.EXT_texture_sRGB) {
   2491       switch (internalFormat) {
   2492       case GL_SRGB_EXT:
   2493       case GL_SRGB8_EXT:
   2494       case GL_COMPRESSED_SRGB_EXT:
   2495          return GL_RGB;
   2496       case GL_SRGB_ALPHA_EXT:
   2497       case GL_SRGB8_ALPHA8_EXT:
   2498       case GL_COMPRESSED_SRGB_ALPHA_EXT:
   2499          return GL_RGBA;
   2500       case GL_SLUMINANCE_ALPHA_EXT:
   2501       case GL_SLUMINANCE8_ALPHA8_EXT:
   2502       case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
   2503          return GL_LUMINANCE_ALPHA;
   2504       case GL_SLUMINANCE_EXT:
   2505       case GL_SLUMINANCE8_EXT:
   2506       case GL_COMPRESSED_SLUMINANCE_EXT:
   2507          return GL_LUMINANCE;
   2508       default:
   2509          ; /* fallthrough */
   2510       }
   2511    }
   2512 
   2513    if (ctx->Version >= 30 ||
   2514        ctx->Extensions.EXT_texture_integer) {
   2515       switch (internalFormat) {
   2516       case GL_RGBA8UI_EXT:
   2517       case GL_RGBA16UI_EXT:
   2518       case GL_RGBA32UI_EXT:
   2519       case GL_RGBA8I_EXT:
   2520       case GL_RGBA16I_EXT:
   2521       case GL_RGBA32I_EXT:
   2522          return GL_RGBA;
   2523       case GL_RGB8UI_EXT:
   2524       case GL_RGB16UI_EXT:
   2525       case GL_RGB32UI_EXT:
   2526       case GL_RGB8I_EXT:
   2527       case GL_RGB16I_EXT:
   2528       case GL_RGB32I_EXT:
   2529          return GL_RGB;
   2530       }
   2531    }
   2532 
   2533    if (ctx->Extensions.ARB_texture_rgb10_a2ui) {
   2534       switch (internalFormat) {
   2535       case GL_RGB10_A2UI:
   2536          return GL_RGBA;
   2537       }
   2538    }
   2539 
   2540    if (ctx->Extensions.EXT_texture_integer) {
   2541       switch (internalFormat) {
   2542       case GL_ALPHA8UI_EXT:
   2543       case GL_ALPHA16UI_EXT:
   2544       case GL_ALPHA32UI_EXT:
   2545       case GL_ALPHA8I_EXT:
   2546       case GL_ALPHA16I_EXT:
   2547       case GL_ALPHA32I_EXT:
   2548          return GL_ALPHA;
   2549       case GL_INTENSITY8UI_EXT:
   2550       case GL_INTENSITY16UI_EXT:
   2551       case GL_INTENSITY32UI_EXT:
   2552       case GL_INTENSITY8I_EXT:
   2553       case GL_INTENSITY16I_EXT:
   2554       case GL_INTENSITY32I_EXT:
   2555          return GL_INTENSITY;
   2556       case GL_LUMINANCE8UI_EXT:
   2557       case GL_LUMINANCE16UI_EXT:
   2558       case GL_LUMINANCE32UI_EXT:
   2559       case GL_LUMINANCE8I_EXT:
   2560       case GL_LUMINANCE16I_EXT:
   2561       case GL_LUMINANCE32I_EXT:
   2562          return GL_LUMINANCE;
   2563       case GL_LUMINANCE_ALPHA8UI_EXT:
   2564       case GL_LUMINANCE_ALPHA16UI_EXT:
   2565       case GL_LUMINANCE_ALPHA32UI_EXT:
   2566       case GL_LUMINANCE_ALPHA8I_EXT:
   2567       case GL_LUMINANCE_ALPHA16I_EXT:
   2568       case GL_LUMINANCE_ALPHA32I_EXT:
   2569          return GL_LUMINANCE_ALPHA;
   2570       default:
   2571          ; /* fallthrough */
   2572       }
   2573    }
   2574 
   2575    if (ctx->Extensions.ARB_texture_rg) {
   2576       switch (internalFormat) {
   2577       case GL_R16F:
   2578       case GL_R32F:
   2579 	 if (!ctx->Extensions.ARB_texture_float)
   2580 	    break;
   2581          return GL_RED;
   2582       case GL_R8I:
   2583       case GL_R8UI:
   2584       case GL_R16I:
   2585       case GL_R16UI:
   2586       case GL_R32I:
   2587       case GL_R32UI:
   2588 	 if (ctx->Version < 30 && !ctx->Extensions.EXT_texture_integer)
   2589 	    break;
   2590 	 /* FALLTHROUGH */
   2591       case GL_R8:
   2592       case GL_R16:
   2593       case GL_RED:
   2594       case GL_COMPRESSED_RED:
   2595          return GL_RED;
   2596 
   2597       case GL_RG16F:
   2598       case GL_RG32F:
   2599 	 if (!ctx->Extensions.ARB_texture_float)
   2600 	    break;
   2601          return GL_RG;
   2602       case GL_RG8I:
   2603       case GL_RG8UI:
   2604       case GL_RG16I:
   2605       case GL_RG16UI:
   2606       case GL_RG32I:
   2607       case GL_RG32UI:
   2608 	 if (ctx->Version < 30 && !ctx->Extensions.EXT_texture_integer)
   2609 	    break;
   2610 	 /* FALLTHROUGH */
   2611       case GL_RG:
   2612       case GL_RG8:
   2613       case GL_RG16:
   2614       case GL_COMPRESSED_RG:
   2615          return GL_RG;
   2616       default:
   2617          ; /* fallthrough */
   2618       }
   2619    }
   2620 
   2621    if (ctx->Extensions.EXT_texture_shared_exponent) {
   2622       switch (internalFormat) {
   2623       case GL_RGB9_E5_EXT:
   2624          return GL_RGB;
   2625       default:
   2626          ; /* fallthrough */
   2627       }
   2628    }
   2629 
   2630    if (ctx->Extensions.EXT_packed_float) {
   2631       switch (internalFormat) {
   2632       case GL_R11F_G11F_B10F_EXT:
   2633          return GL_RGB;
   2634       default:
   2635          ; /* fallthrough */
   2636       }
   2637    }
   2638 
   2639    if (ctx->Extensions.ARB_depth_buffer_float) {
   2640       switch (internalFormat) {
   2641       case GL_DEPTH_COMPONENT32F:
   2642          return GL_DEPTH_COMPONENT;
   2643       case GL_DEPTH32F_STENCIL8:
   2644          return GL_DEPTH_STENCIL;
   2645       default:
   2646          ; /* fallthrough */
   2647       }
   2648    }
   2649 
   2650    return -1; /* error */
   2651 }
   2652 
   2653 /**
   2654  * Returns the effective internal format from a texture format and type.
   2655  * This is used by texture image operations internally for validation, when
   2656  * the specified internal format is a base (unsized) format.
   2657  *
   2658  * This method will only return a valid effective internal format if the
   2659  * combination of format, type and internal format in base form, is acceptable.
   2660  *
   2661  * If a single sized internal format is defined in the spec (OpenGL-ES 3.0.4) or
   2662  * in extensions, to unambiguously correspond to the given base format, then
   2663  * that internal format is returned as the effective. Otherwise, if the
   2664  * combination is accepted but a single effective format is not defined, the
   2665  * passed base format will be returned instead.
   2666  *
   2667  * \param format the texture format
   2668  * \param type the texture type
   2669  */
   2670 static GLenum
   2671 _mesa_es3_effective_internal_format_for_format_and_type(GLenum format,
   2672                                                         GLenum type)
   2673 {
   2674    switch (type) {
   2675    case GL_UNSIGNED_BYTE:
   2676       switch (format) {
   2677       case GL_RGBA:
   2678          return GL_RGBA8;
   2679       case GL_RGB:
   2680          return GL_RGB8;
   2681       case GL_RG:
   2682          return GL_RG8;
   2683       case GL_RED:
   2684          return GL_R8;
   2685       /* Although LUMINANCE_ALPHA, LUMINANCE and ALPHA appear in table 3.12,
   2686        * (section 3.8 Texturing, page 128 of the OpenGL-ES 3.0.4) as effective
   2687        * internal formats, they do not correspond to GL constants, so the base
   2688        * format is returned instead.
   2689        */
   2690       case GL_BGRA_EXT:
   2691       case GL_LUMINANCE_ALPHA:
   2692       case GL_LUMINANCE:
   2693       case GL_ALPHA:
   2694          return format;
   2695       }
   2696       break;
   2697 
   2698    case GL_UNSIGNED_SHORT_4_4_4_4:
   2699       if (format == GL_RGBA)
   2700          return GL_RGBA4;
   2701       break;
   2702 
   2703    case GL_UNSIGNED_SHORT_5_5_5_1:
   2704       if (format == GL_RGBA)
   2705          return GL_RGB5_A1;
   2706       break;
   2707 
   2708    case GL_UNSIGNED_SHORT_5_6_5:
   2709       if (format == GL_RGB)
   2710          return GL_RGB565;
   2711       break;
   2712 
   2713    /* OES_packed_depth_stencil */
   2714    case GL_UNSIGNED_INT_24_8:
   2715       if (format == GL_DEPTH_STENCIL)
   2716          return GL_DEPTH24_STENCIL8;
   2717       break;
   2718 
   2719    case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
   2720       if (format == GL_DEPTH_STENCIL)
   2721          return GL_DEPTH32F_STENCIL8;
   2722       break;
   2723 
   2724    case GL_UNSIGNED_SHORT:
   2725       if (format == GL_DEPTH_COMPONENT)
   2726          return GL_DEPTH_COMPONENT16;
   2727       break;
   2728 
   2729    case GL_UNSIGNED_INT:
   2730       /* It can be DEPTH_COMPONENT16 or DEPTH_COMPONENT24, so just return
   2731        * the format.
   2732        */
   2733       if (format == GL_DEPTH_COMPONENT)
   2734          return format;
   2735       break;
   2736 
   2737    /* OES_texture_float and OES_texture_half_float */
   2738    case GL_FLOAT:
   2739       if (format == GL_DEPTH_COMPONENT)
   2740          return GL_DEPTH_COMPONENT32F;
   2741       /* fall through */
   2742    case GL_HALF_FLOAT_OES:
   2743       switch (format) {
   2744       case GL_RGBA:
   2745       case GL_RGB:
   2746       case GL_LUMINANCE_ALPHA:
   2747       case GL_LUMINANCE:
   2748       case GL_ALPHA:
   2749       case GL_RED:
   2750       case GL_RG:
   2751          return format;
   2752       }
   2753       break;
   2754    case GL_HALF_FLOAT:
   2755       switch (format) {
   2756       case GL_RG:
   2757       case GL_RED:
   2758          return format;
   2759       }
   2760       break;
   2761 
   2762    /* GL_EXT_texture_type_2_10_10_10_REV */
   2763    case GL_UNSIGNED_INT_2_10_10_10_REV:
   2764       switch (format) {
   2765       case GL_RGBA:
   2766       case GL_RGB:
   2767          return format;
   2768       }
   2769       break;
   2770 
   2771    default:
   2772       /* fall through and return NONE */
   2773       break;
   2774    }
   2775 
   2776    return GL_NONE;
   2777 }
   2778 
   2779 /**
   2780  * Do error checking of format/type combinations for OpenGL ES 3
   2781  * glTex[Sub]Image.
   2782  * \return error code, or GL_NO_ERROR.
   2783  */
   2784 GLenum
   2785 _mesa_es3_error_check_format_and_type(const struct gl_context *ctx,
   2786                                       GLenum format, GLenum type,
   2787                                       GLenum internalFormat)
   2788 {
   2789    /* If internalFormat is an unsized format, then the effective internal
   2790     * format derived from format and type should be used instead. Page 127,
   2791     * section "3.8 Texturing" of the GLES 3.0.4 spec states:
   2792     *
   2793     *    "if internalformat is a base internal format, the effective
   2794     *     internal format is a sized internal format that is derived
   2795     *     from the format and type for internal use by the GL.
   2796     *     Table 3.12 specifies the mapping of format and type to effective
   2797     *     internal formats. The effective internal format is used by the GL
   2798     *     for purposes such as texture completeness or type checks for
   2799     *     CopyTex* commands. In these cases, the GL is required to operate
   2800     *     as if the effective internal format was used as the internalformat
   2801     *     when specifying the texture data."
   2802     */
   2803    if (_mesa_is_enum_format_unsized(internalFormat)) {
   2804       GLenum effectiveInternalFormat =
   2805          _mesa_es3_effective_internal_format_for_format_and_type(format, type);
   2806 
   2807       if (effectiveInternalFormat == GL_NONE)
   2808          return GL_INVALID_OPERATION;
   2809 
   2810       GLenum baseInternalFormat;
   2811       if (internalFormat == GL_BGRA_EXT) {
   2812          /* Unfortunately, _mesa_base_tex_format returns a base format of
   2813           * GL_RGBA for GL_BGRA_EXT.  This makes perfect sense if you're
   2814           * asking the question, "what channels does this format have?"
   2815           * However, if we're trying to determine if two internal formats
   2816           * match in the ES3 sense, we actually want GL_BGRA.
   2817           */
   2818          baseInternalFormat = GL_BGRA_EXT;
   2819       } else {
   2820          baseInternalFormat =
   2821             _mesa_base_tex_format(ctx, effectiveInternalFormat);
   2822       }
   2823 
   2824       if (internalFormat != baseInternalFormat)
   2825          return GL_INVALID_OPERATION;
   2826 
   2827       internalFormat = effectiveInternalFormat;
   2828    }
   2829 
   2830    switch (format) {
   2831    case GL_BGRA_EXT:
   2832       if (type != GL_UNSIGNED_BYTE || internalFormat != GL_BGRA)
   2833          return GL_INVALID_OPERATION;
   2834       break;
   2835 
   2836    case GL_RGBA:
   2837       switch (type) {
   2838       case GL_UNSIGNED_BYTE:
   2839          switch (internalFormat) {
   2840          case GL_RGBA:
   2841          case GL_RGBA8:
   2842          case GL_RGB5_A1:
   2843          case GL_RGBA4:
   2844          case GL_SRGB8_ALPHA8_EXT:
   2845             break;
   2846          default:
   2847             return GL_INVALID_OPERATION;
   2848          }
   2849          break;
   2850 
   2851       case GL_BYTE:
   2852          if (internalFormat != GL_RGBA8_SNORM)
   2853             return GL_INVALID_OPERATION;
   2854          break;
   2855 
   2856       case GL_UNSIGNED_SHORT_4_4_4_4:
   2857          switch (internalFormat) {
   2858          case GL_RGBA:
   2859          case GL_RGBA4:
   2860             break;
   2861          default:
   2862             return GL_INVALID_OPERATION;
   2863          }
   2864          break;
   2865 
   2866       case GL_UNSIGNED_SHORT_5_5_5_1:
   2867          switch (internalFormat) {
   2868          case GL_RGBA:
   2869          case GL_RGB5_A1:
   2870             break;
   2871          default:
   2872             return GL_INVALID_OPERATION;
   2873          }
   2874          break;
   2875 
   2876       case GL_UNSIGNED_INT_2_10_10_10_REV:
   2877          switch (internalFormat) {
   2878          case GL_RGBA: /* GL_EXT_texture_type_2_10_10_10_REV */
   2879          case GL_RGB10_A2:
   2880          case GL_RGB5_A1:
   2881             break;
   2882          default:
   2883             return GL_INVALID_OPERATION;
   2884          }
   2885          break;
   2886 
   2887       case GL_HALF_FLOAT:
   2888          if (internalFormat != GL_RGBA16F)
   2889             return GL_INVALID_OPERATION;
   2890          break;
   2891 
   2892       case GL_FLOAT:
   2893          switch (internalFormat) {
   2894          case GL_RGBA16F:
   2895          case GL_RGBA32F:
   2896             break;
   2897          case GL_RGBA:
   2898             if (ctx->Extensions.OES_texture_float && internalFormat == format)
   2899                break;
   2900          default:
   2901             return GL_INVALID_OPERATION;
   2902          }
   2903          break;
   2904 
   2905       case GL_HALF_FLOAT_OES:
   2906          if (ctx->Extensions.OES_texture_half_float && internalFormat == format)
   2907             break;
   2908       default:
   2909          return GL_INVALID_OPERATION;
   2910       }
   2911       break;
   2912 
   2913    case GL_RGBA_INTEGER:
   2914       switch (type) {
   2915       case GL_UNSIGNED_BYTE:
   2916          if (internalFormat != GL_RGBA8UI)
   2917             return GL_INVALID_OPERATION;
   2918          break;
   2919 
   2920       case GL_BYTE:
   2921          if (internalFormat != GL_RGBA8I)
   2922             return GL_INVALID_OPERATION;
   2923          break;
   2924 
   2925       case GL_UNSIGNED_SHORT:
   2926          if (internalFormat != GL_RGBA16UI)
   2927             return GL_INVALID_OPERATION;
   2928          break;
   2929 
   2930       case GL_SHORT:
   2931          if (internalFormat != GL_RGBA16I)
   2932             return GL_INVALID_OPERATION;
   2933          break;
   2934 
   2935       case GL_UNSIGNED_INT:
   2936          if (internalFormat != GL_RGBA32UI)
   2937             return GL_INVALID_OPERATION;
   2938          break;
   2939 
   2940       case GL_INT:
   2941          if (internalFormat != GL_RGBA32I)
   2942             return GL_INVALID_OPERATION;
   2943          break;
   2944 
   2945       case GL_UNSIGNED_INT_2_10_10_10_REV:
   2946          if (internalFormat != GL_RGB10_A2UI)
   2947             return GL_INVALID_OPERATION;
   2948          break;
   2949 
   2950       default:
   2951          return GL_INVALID_OPERATION;
   2952       }
   2953       break;
   2954 
   2955    case GL_RGB:
   2956       switch (type) {
   2957       case GL_UNSIGNED_BYTE:
   2958          switch (internalFormat) {
   2959          case GL_RGB:
   2960          case GL_RGB8:
   2961          case GL_RGB565:
   2962          case GL_SRGB8:
   2963             break;
   2964          default:
   2965             return GL_INVALID_OPERATION;
   2966          }
   2967          break;
   2968 
   2969       case GL_BYTE:
   2970          if (internalFormat != GL_RGB8_SNORM)
   2971             return GL_INVALID_OPERATION;
   2972          break;
   2973 
   2974       case GL_UNSIGNED_SHORT_5_6_5:
   2975          switch (internalFormat) {
   2976          case GL_RGB:
   2977          case GL_RGB565:
   2978             break;
   2979          default:
   2980             return GL_INVALID_OPERATION;
   2981          }
   2982          break;
   2983 
   2984       case GL_UNSIGNED_INT_10F_11F_11F_REV:
   2985          if (internalFormat != GL_R11F_G11F_B10F)
   2986             return GL_INVALID_OPERATION;
   2987          break;
   2988 
   2989       case GL_UNSIGNED_INT_5_9_9_9_REV:
   2990          if (internalFormat != GL_RGB9_E5)
   2991             return GL_INVALID_OPERATION;
   2992          break;
   2993 
   2994       case GL_HALF_FLOAT:
   2995          switch (internalFormat) {
   2996          case GL_RGB16F:
   2997          case GL_R11F_G11F_B10F:
   2998          case GL_RGB9_E5:
   2999             break;
   3000          default:
   3001             return GL_INVALID_OPERATION;
   3002          }
   3003          break;
   3004 
   3005       case GL_FLOAT:
   3006          switch (internalFormat) {
   3007          case GL_RGB16F:
   3008          case GL_RGB32F:
   3009          case GL_R11F_G11F_B10F:
   3010          case GL_RGB9_E5:
   3011             break;
   3012          case GL_RGB:
   3013             if (ctx->Extensions.OES_texture_float && internalFormat == format)
   3014                break;
   3015          default:
   3016             return GL_INVALID_OPERATION;
   3017          }
   3018          break;
   3019 
   3020       case GL_HALF_FLOAT_OES:
   3021          if (!ctx->Extensions.OES_texture_half_float || internalFormat != format)
   3022             return GL_INVALID_OPERATION;
   3023          break;
   3024 
   3025       case GL_UNSIGNED_INT_2_10_10_10_REV:
   3026          switch (internalFormat) {
   3027          case GL_RGB: /* GL_EXT_texture_type_2_10_10_10_REV */
   3028             break;
   3029          default:
   3030             return GL_INVALID_OPERATION;
   3031          }
   3032          break;
   3033 
   3034       default:
   3035          return GL_INVALID_OPERATION;
   3036       }
   3037       break;
   3038 
   3039    case GL_RGB_INTEGER:
   3040       switch (type) {
   3041       case GL_UNSIGNED_BYTE:
   3042          if (internalFormat != GL_RGB8UI)
   3043             return GL_INVALID_OPERATION;
   3044          break;
   3045 
   3046       case GL_BYTE:
   3047          if (internalFormat != GL_RGB8I)
   3048             return GL_INVALID_OPERATION;
   3049          break;
   3050 
   3051       case GL_UNSIGNED_SHORT:
   3052          if (internalFormat != GL_RGB16UI)
   3053             return GL_INVALID_OPERATION;
   3054          break;
   3055 
   3056       case GL_SHORT:
   3057          if (internalFormat != GL_RGB16I)
   3058             return GL_INVALID_OPERATION;
   3059          break;
   3060 
   3061       case GL_UNSIGNED_INT:
   3062          if (internalFormat != GL_RGB32UI)
   3063             return GL_INVALID_OPERATION;
   3064          break;
   3065 
   3066       case GL_INT:
   3067          if (internalFormat != GL_RGB32I)
   3068             return GL_INVALID_OPERATION;
   3069          break;
   3070 
   3071       default:
   3072          return GL_INVALID_OPERATION;
   3073       }
   3074       break;
   3075 
   3076    case GL_RG:
   3077       switch (type) {
   3078       case GL_UNSIGNED_BYTE:
   3079          if (internalFormat != GL_RG8)
   3080             return GL_INVALID_OPERATION;
   3081          break;
   3082 
   3083       case GL_BYTE:
   3084          if (internalFormat != GL_RG8_SNORM)
   3085             return GL_INVALID_OPERATION;
   3086          break;
   3087 
   3088       case GL_HALF_FLOAT:
   3089       case GL_HALF_FLOAT_OES:
   3090          switch (internalFormat) {
   3091             case GL_RG16F:
   3092                break;
   3093             case GL_RG:
   3094                if (ctx->Extensions.ARB_texture_rg &&
   3095                    ctx->Extensions.OES_texture_half_float)
   3096                   break;
   3097             /* fallthrough */
   3098             default:
   3099                return GL_INVALID_OPERATION;
   3100          }
   3101          break;
   3102 
   3103       case GL_FLOAT:
   3104          switch (internalFormat) {
   3105          case GL_RG16F:
   3106          case GL_RG32F:
   3107             break;
   3108          case GL_RG:
   3109             if (ctx->Extensions.ARB_texture_rg &&
   3110                 ctx->Extensions.OES_texture_float)
   3111                break;
   3112             /* fallthrough */
   3113          default:
   3114             return GL_INVALID_OPERATION;
   3115          }
   3116          break;
   3117 
   3118       default:
   3119          return GL_INVALID_OPERATION;
   3120       }
   3121       break;
   3122 
   3123    case GL_RG_INTEGER:
   3124       switch (type) {
   3125       case GL_UNSIGNED_BYTE:
   3126          if (internalFormat != GL_RG8UI)
   3127             return GL_INVALID_OPERATION;
   3128          break;
   3129 
   3130       case GL_BYTE:
   3131          if (internalFormat != GL_RG8I)
   3132             return GL_INVALID_OPERATION;
   3133          break;
   3134 
   3135       case GL_UNSIGNED_SHORT:
   3136          if (internalFormat != GL_RG16UI)
   3137             return GL_INVALID_OPERATION;
   3138          break;
   3139 
   3140       case GL_SHORT:
   3141          if (internalFormat != GL_RG16I)
   3142             return GL_INVALID_OPERATION;
   3143          break;
   3144 
   3145       case GL_UNSIGNED_INT:
   3146          if (internalFormat != GL_RG32UI)
   3147             return GL_INVALID_OPERATION;
   3148          break;
   3149 
   3150       case GL_INT:
   3151          if (internalFormat != GL_RG32I)
   3152             return GL_INVALID_OPERATION;
   3153          break;
   3154 
   3155       default:
   3156          return GL_INVALID_OPERATION;
   3157       }
   3158       break;
   3159 
   3160    case GL_RED:
   3161       switch (type) {
   3162       case GL_UNSIGNED_BYTE:
   3163          if (internalFormat != GL_R8)
   3164             return GL_INVALID_OPERATION;
   3165          break;
   3166 
   3167       case GL_BYTE:
   3168          if (internalFormat != GL_R8_SNORM)
   3169             return GL_INVALID_OPERATION;
   3170          break;
   3171 
   3172       case GL_HALF_FLOAT:
   3173       case GL_HALF_FLOAT_OES:
   3174          switch (internalFormat) {
   3175          case GL_R16F:
   3176             break;
   3177          case GL_RG:
   3178          case GL_RED:
   3179             if (ctx->Extensions.ARB_texture_rg &&
   3180                 ctx->Extensions.OES_texture_half_float)
   3181                break;
   3182             /* fallthrough */
   3183          default:
   3184             return GL_INVALID_OPERATION;
   3185          }
   3186          break;
   3187 
   3188       case GL_FLOAT:
   3189          switch (internalFormat) {
   3190          case GL_R16F:
   3191          case GL_R32F:
   3192             break;
   3193          case GL_RED:
   3194             if (ctx->Extensions.ARB_texture_rg &&
   3195                 ctx->Extensions.OES_texture_float)
   3196                break;
   3197             /* fallthrough */
   3198          default:
   3199             return GL_INVALID_OPERATION;
   3200          }
   3201          break;
   3202 
   3203       default:
   3204          return GL_INVALID_OPERATION;
   3205       }
   3206       break;
   3207 
   3208    case GL_RED_INTEGER:
   3209       switch (type) {
   3210       case GL_UNSIGNED_BYTE:
   3211          if (internalFormat != GL_R8UI)
   3212             return GL_INVALID_OPERATION;
   3213          break;
   3214 
   3215       case GL_BYTE:
   3216          if (internalFormat != GL_R8I)
   3217             return GL_INVALID_OPERATION;
   3218          break;
   3219 
   3220       case GL_UNSIGNED_SHORT:
   3221          if (internalFormat != GL_R16UI)
   3222             return GL_INVALID_OPERATION;
   3223          break;
   3224 
   3225       case GL_SHORT:
   3226          if (internalFormat != GL_R16I)
   3227             return GL_INVALID_OPERATION;
   3228          break;
   3229 
   3230       case GL_UNSIGNED_INT:
   3231          if (internalFormat != GL_R32UI)
   3232             return GL_INVALID_OPERATION;
   3233          break;
   3234 
   3235       case GL_INT:
   3236          if (internalFormat != GL_R32I)
   3237             return GL_INVALID_OPERATION;
   3238          break;
   3239 
   3240       default:
   3241          return GL_INVALID_OPERATION;
   3242       }
   3243       break;
   3244 
   3245    case GL_DEPTH_COMPONENT:
   3246       switch (type) {
   3247       case GL_UNSIGNED_SHORT:
   3248          if (internalFormat != GL_DEPTH_COMPONENT
   3249              && internalFormat != GL_DEPTH_COMPONENT16)
   3250             return GL_INVALID_OPERATION;
   3251          break;
   3252 
   3253       case GL_UNSIGNED_INT:
   3254          switch (internalFormat) {
   3255          case GL_DEPTH_COMPONENT:
   3256          case GL_DEPTH_COMPONENT16:
   3257          case GL_DEPTH_COMPONENT24:
   3258             break;
   3259          default:
   3260             return GL_INVALID_OPERATION;
   3261          }
   3262          break;
   3263 
   3264       case GL_FLOAT:
   3265          if (internalFormat != GL_DEPTH_COMPONENT32F)
   3266             return GL_INVALID_OPERATION;
   3267          break;
   3268 
   3269       default:
   3270          return GL_INVALID_OPERATION;
   3271       }
   3272       break;
   3273 
   3274    case GL_DEPTH_STENCIL:
   3275       switch (type) {
   3276       case GL_UNSIGNED_INT_24_8:
   3277          if (internalFormat != GL_DEPTH_STENCIL
   3278              && internalFormat != GL_DEPTH24_STENCIL8)
   3279             return GL_INVALID_OPERATION;
   3280          break;
   3281 
   3282       case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
   3283          if (internalFormat != GL_DEPTH32F_STENCIL8)
   3284             return GL_INVALID_OPERATION;
   3285          break;
   3286 
   3287       default:
   3288          return GL_INVALID_OPERATION;
   3289       }
   3290       break;
   3291 
   3292    case GL_STENCIL_INDEX:
   3293       if (!_mesa_has_OES_texture_stencil8(ctx) ||
   3294           type != GL_UNSIGNED_BYTE ||
   3295           internalFormat != GL_STENCIL_INDEX8) {
   3296          return GL_INVALID_OPERATION;
   3297       }
   3298       break;
   3299 
   3300    case GL_ALPHA:
   3301    case GL_LUMINANCE:
   3302    case GL_LUMINANCE_ALPHA:
   3303       switch (type) {
   3304       case GL_FLOAT:
   3305          if (ctx->Extensions.OES_texture_float && internalFormat == format)
   3306             break;
   3307       case GL_HALF_FLOAT_OES:
   3308          if (ctx->Extensions.OES_texture_half_float && internalFormat == format)
   3309             break;
   3310       default:
   3311          if (type != GL_UNSIGNED_BYTE || format != internalFormat)
   3312             return GL_INVALID_OPERATION;
   3313       }
   3314    }
   3315 
   3316    return GL_NO_ERROR;
   3317 }
   3318 
   3319 static void
   3320 set_swizzle(uint8_t *swizzle, int x, int y, int z, int w)
   3321 {
   3322    swizzle[MESA_FORMAT_SWIZZLE_X] = x;
   3323    swizzle[MESA_FORMAT_SWIZZLE_Y] = y;
   3324    swizzle[MESA_FORMAT_SWIZZLE_Z] = z;
   3325    swizzle[MESA_FORMAT_SWIZZLE_W] = w;
   3326 }
   3327 
   3328 static bool
   3329 get_swizzle_from_gl_format(GLenum format, uint8_t *swizzle)
   3330 {
   3331    switch (format) {
   3332    case GL_RGBA:
   3333    case GL_RGBA_INTEGER_EXT:
   3334       set_swizzle(swizzle, 0, 1, 2, 3);
   3335       return true;
   3336    case GL_BGRA:
   3337    case GL_BGRA_INTEGER_EXT:
   3338       set_swizzle(swizzle, 2, 1, 0, 3);
   3339       return true;
   3340    case GL_ABGR_EXT:
   3341       set_swizzle(swizzle, 3, 2, 1, 0);
   3342       return true;
   3343    case GL_RGB:
   3344    case GL_RGB_INTEGER_EXT:
   3345       set_swizzle(swizzle, 0, 1, 2, 5);
   3346       return true;
   3347    case GL_BGR:
   3348    case GL_BGR_INTEGER_EXT:
   3349       set_swizzle(swizzle, 2, 1, 0, 5);
   3350       return true;
   3351    case GL_LUMINANCE_ALPHA:
   3352    case GL_LUMINANCE_ALPHA_INTEGER_EXT:
   3353       set_swizzle(swizzle, 0, 0, 0, 1);
   3354       return true;
   3355    case GL_RG:
   3356    case GL_RG_INTEGER:
   3357       set_swizzle(swizzle, 0, 1, 4, 5);
   3358       return true;
   3359    case GL_RED:
   3360    case GL_RED_INTEGER_EXT:
   3361       set_swizzle(swizzle, 0, 4, 4, 5);
   3362       return true;
   3363    case GL_GREEN:
   3364    case GL_GREEN_INTEGER_EXT:
   3365       set_swizzle(swizzle, 4, 0, 4, 5);
   3366       return true;
   3367    case GL_BLUE:
   3368    case GL_BLUE_INTEGER_EXT:
   3369       set_swizzle(swizzle, 4, 4, 0, 5);
   3370       return true;
   3371    case GL_ALPHA:
   3372    case GL_ALPHA_INTEGER_EXT:
   3373       set_swizzle(swizzle, 4, 4, 4, 0);
   3374       return true;
   3375    case GL_LUMINANCE:
   3376    case GL_LUMINANCE_INTEGER_EXT:
   3377       set_swizzle(swizzle, 0, 0, 0, 5);
   3378       return true;
   3379    case GL_INTENSITY:
   3380       set_swizzle(swizzle, 0, 0, 0, 0);
   3381       return true;
   3382    default:
   3383       return false;
   3384    }
   3385 }
   3386 
   3387 /**
   3388 * Take an OpenGL format (GL_RGB, GL_RGBA, etc), OpenGL data type (GL_INT,
   3389 * GL_FOAT, etc) and return a matching mesa_array_format or a mesa_format
   3390 * otherwise (for non-array formats).
   3391 *
   3392 * This function will typically be used to compute a mesa format from a GL type
   3393 * so we can then call _mesa_format_convert. This function does
   3394 * not consider byte swapping, so it returns types assuming that no byte
   3395 * swapping is involved. If byte swapping is involved then clients are supposed
   3396 * to handle that on their side before calling _mesa_format_convert.
   3397 *
   3398 * This function returns an uint32_t that can pack a mesa_format or a
   3399 * mesa_array_format. Clients must check the mesa array format bit
   3400 * (MESA_ARRAY_FORMAT_BIT) on the return value to know if the returned
   3401 * format is a mesa_array_format or a mesa_format.
   3402 */
   3403 uint32_t
   3404 _mesa_format_from_format_and_type(GLenum format, GLenum type)
   3405 {
   3406    bool is_array_format = true;
   3407    uint8_t swizzle[4];
   3408    bool normalized = false, is_float = false, is_signed = false;
   3409    int num_channels = 0, type_size = 0;
   3410 
   3411    /* Extract array format type information from the OpenGL data type */
   3412    switch (type) {
   3413    case GL_UNSIGNED_BYTE:
   3414       type_size = 1;
   3415       break;
   3416    case GL_BYTE:
   3417       type_size = 1;
   3418       is_signed = true;
   3419       break;
   3420    case GL_UNSIGNED_SHORT:
   3421       type_size = 2;
   3422       break;
   3423    case GL_SHORT:
   3424       type_size = 2;
   3425       is_signed = true;
   3426       break;
   3427    case GL_UNSIGNED_INT:
   3428       type_size = 4;
   3429       break;
   3430    case GL_INT:
   3431       type_size = 4;
   3432       is_signed = true;
   3433       break;
   3434    case GL_HALF_FLOAT:
   3435    case GL_HALF_FLOAT_OES:
   3436       type_size = 2;
   3437       is_signed = true;
   3438       is_float = true;
   3439       break;
   3440    case GL_FLOAT:
   3441       type_size = 4;
   3442       is_signed = true;
   3443       is_float = true;
   3444       break;
   3445    default:
   3446       is_array_format = false;
   3447       break;
   3448    }
   3449 
   3450    /* Extract array format swizzle information from the OpenGL format */
   3451    if (is_array_format)
   3452       is_array_format = get_swizzle_from_gl_format(format, swizzle);
   3453 
   3454    /* If this is an array format type after checking data type and format,
   3455     * create the array format
   3456     */
   3457    if (is_array_format) {
   3458       normalized = !_mesa_is_enum_format_integer(format);
   3459       num_channels = _mesa_components_in_format(format);
   3460 
   3461       return MESA_ARRAY_FORMAT(type_size, is_signed, is_float,
   3462                                normalized, num_channels,
   3463                                swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
   3464    }
   3465 
   3466    /* Otherwise this is not an array format, so return the mesa_format
   3467     * matching the OpenGL format and data type
   3468     */
   3469    switch (type) {
   3470    case GL_UNSIGNED_SHORT_5_6_5:
   3471      if (format == GL_RGB)
   3472          return MESA_FORMAT_B5G6R5_UNORM;
   3473       else if (format == GL_BGR)
   3474          return MESA_FORMAT_R5G6B5_UNORM;
   3475       else if (format == GL_RGB_INTEGER)
   3476          return MESA_FORMAT_B5G6R5_UINT;
   3477       break;
   3478    case GL_UNSIGNED_SHORT_5_6_5_REV:
   3479       if (format == GL_RGB)
   3480          return MESA_FORMAT_R5G6B5_UNORM;
   3481       else if (format == GL_BGR)
   3482          return MESA_FORMAT_B5G6R5_UNORM;
   3483       else if (format == GL_RGB_INTEGER)
   3484          return MESA_FORMAT_R5G6B5_UINT;
   3485       break;
   3486    case GL_UNSIGNED_SHORT_4_4_4_4:
   3487       if (format == GL_RGBA)
   3488          return MESA_FORMAT_A4B4G4R4_UNORM;
   3489       else if (format == GL_BGRA)
   3490          return MESA_FORMAT_A4R4G4B4_UNORM;
   3491       else if (format == GL_ABGR_EXT)
   3492          return MESA_FORMAT_R4G4B4A4_UNORM;
   3493       else if (format == GL_RGBA_INTEGER)
   3494          return MESA_FORMAT_A4B4G4R4_UINT;
   3495       else if (format == GL_BGRA_INTEGER)
   3496          return MESA_FORMAT_A4R4G4B4_UINT;
   3497       break;
   3498    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
   3499       if (format == GL_RGBA)
   3500          return MESA_FORMAT_R4G4B4A4_UNORM;
   3501       else if (format == GL_BGRA)
   3502          return MESA_FORMAT_B4G4R4A4_UNORM;
   3503       else if (format == GL_ABGR_EXT)
   3504          return MESA_FORMAT_A4B4G4R4_UNORM;
   3505       else if (format == GL_RGBA_INTEGER)
   3506          return MESA_FORMAT_R4G4B4A4_UINT;
   3507       else if (format == GL_BGRA_INTEGER)
   3508          return MESA_FORMAT_B4G4R4A4_UINT;
   3509       break;
   3510    case GL_UNSIGNED_SHORT_5_5_5_1:
   3511       if (format == GL_RGBA)
   3512          return MESA_FORMAT_A1B5G5R5_UNORM;
   3513       else if (format == GL_BGRA)
   3514          return MESA_FORMAT_A1R5G5B5_UNORM;
   3515       else if (format == GL_RGBA_INTEGER)
   3516          return MESA_FORMAT_A1B5G5R5_UINT;
   3517       else if (format == GL_BGRA_INTEGER)
   3518          return MESA_FORMAT_A1R5G5B5_UINT;
   3519       break;
   3520    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
   3521       if (format == GL_RGBA)
   3522          return MESA_FORMAT_R5G5B5A1_UNORM;
   3523       else if (format == GL_BGRA)
   3524          return MESA_FORMAT_B5G5R5A1_UNORM;
   3525       else if (format == GL_RGBA_INTEGER)
   3526          return MESA_FORMAT_R5G5B5A1_UINT;
   3527       else if (format == GL_BGRA_INTEGER)
   3528          return MESA_FORMAT_B5G5R5A1_UINT;
   3529       break;
   3530    case GL_UNSIGNED_BYTE_3_3_2:
   3531       if (format == GL_RGB)
   3532          return MESA_FORMAT_B2G3R3_UNORM;
   3533       else if (format == GL_RGB_INTEGER)
   3534          return MESA_FORMAT_B2G3R3_UINT;
   3535       break;
   3536    case GL_UNSIGNED_BYTE_2_3_3_REV:
   3537       if (format == GL_RGB)
   3538          return MESA_FORMAT_R3G3B2_UNORM;
   3539       else if (format == GL_RGB_INTEGER)
   3540          return MESA_FORMAT_R3G3B2_UINT;
   3541       break;
   3542    case GL_UNSIGNED_INT_5_9_9_9_REV:
   3543       if (format == GL_RGB)
   3544          return MESA_FORMAT_R9G9B9E5_FLOAT;
   3545       break;
   3546    case GL_UNSIGNED_INT_10_10_10_2:
   3547       if (format == GL_RGBA)
   3548          return MESA_FORMAT_A2B10G10R10_UNORM;
   3549       else if (format == GL_RGBA_INTEGER)
   3550          return MESA_FORMAT_A2B10G10R10_UINT;
   3551       else if (format == GL_BGRA)
   3552          return MESA_FORMAT_A2R10G10B10_UNORM;
   3553       else if (format == GL_BGRA_INTEGER)
   3554          return MESA_FORMAT_A2R10G10B10_UINT;
   3555       break;
   3556    case GL_UNSIGNED_INT_2_10_10_10_REV:
   3557       if (format == GL_RGB)
   3558          return MESA_FORMAT_R10G10B10X2_UNORM;
   3559       if (format == GL_RGBA)
   3560          return MESA_FORMAT_R10G10B10A2_UNORM;
   3561       else if (format == GL_RGBA_INTEGER)
   3562          return MESA_FORMAT_R10G10B10A2_UINT;
   3563       else if (format == GL_BGRA)
   3564          return MESA_FORMAT_B10G10R10A2_UNORM;
   3565       else if (format == GL_BGRA_INTEGER)
   3566          return MESA_FORMAT_B10G10R10A2_UINT;
   3567       break;
   3568    case GL_UNSIGNED_INT_8_8_8_8:
   3569       if (format == GL_RGBA)
   3570          return MESA_FORMAT_A8B8G8R8_UNORM;
   3571       else if (format == GL_BGRA)
   3572          return MESA_FORMAT_A8R8G8B8_UNORM;
   3573       else if (format == GL_ABGR_EXT)
   3574          return MESA_FORMAT_R8G8B8A8_UNORM;
   3575       else if (format == GL_RGBA_INTEGER)
   3576          return MESA_FORMAT_A8B8G8R8_UINT;
   3577       else if (format == GL_BGRA_INTEGER)
   3578          return MESA_FORMAT_A8R8G8B8_UINT;
   3579       break;
   3580    case GL_UNSIGNED_INT_8_8_8_8_REV:
   3581       if (format == GL_RGBA)
   3582          return MESA_FORMAT_R8G8B8A8_UNORM;
   3583       else if (format == GL_BGRA)
   3584          return MESA_FORMAT_B8G8R8A8_UNORM;
   3585       else if (format == GL_ABGR_EXT)
   3586          return MESA_FORMAT_A8B8G8R8_UNORM;
   3587       else if (format == GL_RGBA_INTEGER)
   3588          return MESA_FORMAT_R8G8B8A8_UINT;
   3589       else if (format == GL_BGRA_INTEGER)
   3590          return MESA_FORMAT_B8G8R8A8_UINT;
   3591       break;
   3592    case GL_UNSIGNED_SHORT_8_8_MESA:
   3593       if (format == GL_YCBCR_MESA)
   3594          return MESA_FORMAT_YCBCR;
   3595       break;
   3596    case GL_UNSIGNED_SHORT_8_8_REV_MESA:
   3597       if (format == GL_YCBCR_MESA)
   3598          return MESA_FORMAT_YCBCR_REV;
   3599       break;
   3600    case GL_UNSIGNED_INT_10F_11F_11F_REV:
   3601       if (format == GL_RGB)
   3602          return MESA_FORMAT_R11G11B10_FLOAT;
   3603       break;
   3604    case GL_FLOAT:
   3605       if (format == GL_DEPTH_COMPONENT)
   3606          return MESA_FORMAT_Z_FLOAT32;
   3607       break;
   3608    case GL_UNSIGNED_INT:
   3609       if (format == GL_DEPTH_COMPONENT)
   3610          return MESA_FORMAT_Z_UNORM32;
   3611       break;
   3612    case GL_UNSIGNED_SHORT:
   3613       if (format == GL_DEPTH_COMPONENT)
   3614          return MESA_FORMAT_Z_UNORM16;
   3615       break;
   3616    case GL_UNSIGNED_INT_24_8:
   3617       if (format == GL_DEPTH_STENCIL)
   3618          return MESA_FORMAT_Z24_UNORM_S8_UINT;
   3619       break;
   3620    case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
   3621       if (format == GL_DEPTH_STENCIL)
   3622          return MESA_FORMAT_Z32_FLOAT_S8X24_UINT;
   3623       break;
   3624    default:
   3625       break;
   3626    }
   3627 
   3628    /* If we got here it means that we could not find a Mesa format that
   3629     * matches the GL format/type provided. We may need to add a new Mesa
   3630     * format in that case.
   3631     */
   3632    unreachable("Unsupported format");
   3633 }
   3634 
   3635 /**
   3636  * Returns true if \p internal_format is a sized internal format that
   3637  * is marked "Color Renderable" in Table 8.10 of the ES 3.2 specification.
   3638  */
   3639 bool
   3640 _mesa_is_es3_color_renderable(GLenum internal_format)
   3641 {
   3642    switch (internal_format) {
   3643    case GL_R8:
   3644    case GL_RG8:
   3645    case GL_RGB8:
   3646    case GL_RGB565:
   3647    case GL_RGBA4:
   3648    case GL_RGB5_A1:
   3649    case GL_RGBA8:
   3650    case GL_RGB10_A2:
   3651    case GL_RGB10_A2UI:
   3652    case GL_SRGB8_ALPHA8:
   3653    case GL_R16F:
   3654    case GL_RG16F:
   3655    case GL_RGBA16F:
   3656    case GL_R32F:
   3657    case GL_RG32F:
   3658    case GL_RGBA32F:
   3659    case GL_R11F_G11F_B10F:
   3660    case GL_R8I:
   3661    case GL_R8UI:
   3662    case GL_R16I:
   3663    case GL_R16UI:
   3664    case GL_R32I:
   3665    case GL_R32UI:
   3666    case GL_RG8I:
   3667    case GL_RG8UI:
   3668    case GL_RG16I:
   3669    case GL_RG16UI:
   3670    case GL_RG32I:
   3671    case GL_RG32UI:
   3672    case GL_RGBA8I:
   3673    case GL_RGBA8UI:
   3674    case GL_RGBA16I:
   3675    case GL_RGBA16UI:
   3676    case GL_RGBA32I:
   3677    case GL_RGBA32UI:
   3678       return true;
   3679    default:
   3680       return false;
   3681    }
   3682 }
   3683 
   3684 /**
   3685  * Returns true if \p internal_format is a sized internal format that
   3686  * is marked "Texture Filterable" in Table 8.10 of the ES 3.2 specification.
   3687  */
   3688 bool
   3689 _mesa_is_es3_texture_filterable(const struct gl_context *ctx,
   3690                                 GLenum internal_format)
   3691 {
   3692    switch (internal_format) {
   3693    case GL_R8:
   3694    case GL_R8_SNORM:
   3695    case GL_RG8:
   3696    case GL_RG8_SNORM:
   3697    case GL_RGB8:
   3698    case GL_RGB8_SNORM:
   3699    case GL_RGB565:
   3700    case GL_RGBA4:
   3701    case GL_RGB5_A1:
   3702    case GL_RGBA8:
   3703    case GL_RGBA8_SNORM:
   3704    case GL_RGB10_A2:
   3705    case GL_SRGB8:
   3706    case GL_SRGB8_ALPHA8:
   3707    case GL_R16F:
   3708    case GL_RG16F:
   3709    case GL_RGB16F:
   3710    case GL_RGBA16F:
   3711    case GL_R11F_G11F_B10F:
   3712    case GL_RGB9_E5:
   3713       return true;
   3714    case GL_R32F:
   3715    case GL_RG32F:
   3716    case GL_RGB32F:
   3717    case GL_RGBA32F:
   3718       /* The OES_texture_float_linear spec says:
   3719        *
   3720        *    "When implemented against OpenGL ES 3.0 or later versions, sized
   3721        *     32-bit floating-point formats become texture-filterable. This
   3722        *     should be noted by, for example, checking the ``TF'' column of
   3723        *     table 8.13 in the ES 3.1 Specification (``Correspondence of sized
   3724        *     internal formats to base internal formats ... and use cases ...'')
   3725        *     for the R32F, RG32F, RGB32F, and RGBA32F formats."
   3726        */
   3727       return ctx->Extensions.OES_texture_float_linear;
   3728    default:
   3729       return false;
   3730    }
   3731 }
   3732