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  *
      7  * Permission is hereby granted, free of charge, to any person obtaining a
      8  * copy of this software and associated documentation files (the "Software"),
      9  * to deal in the Software without restriction, including without limitation
     10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     11  * and/or sell copies of the Software, and to permit persons to whom the
     12  * Software is furnished to do so, subject to the following conditions:
     13  *
     14  * The above copyright notice and this permission notice shall be included
     15  * in all copies or substantial portions of the Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     23  * OTHER DEALINGS IN THE SOFTWARE.
     24  */
     25 
     26 
     27 /**
     28  * \file texformat.c
     29  * Texture formats.
     30  *
     31  * \author Gareth Hughes
     32  * \author Brian Paul
     33  */
     34 
     35 
     36 #include "context.h"
     37 #include "enums.h"
     38 #include "mtypes.h"
     39 #include "texcompress.h"
     40 #include "texformat.h"
     41 #include "glformats.h"
     42 
     43 #define RETURN_IF_SUPPORTED(f) do {		\
     44    if (ctx->TextureFormatSupported[f])		\
     45       return f;					\
     46 } while (0)
     47 
     48 /**
     49  * Choose an appropriate texture format given the format, type and
     50  * internalFormat parameters passed to glTexImage().
     51  *
     52  * \param ctx  the GL context.
     53  * \param target  a texture target (GL_TEXTURE_x)
     54  * \param internalFormat  user's prefered internal texture format.
     55  * \param format  incoming image pixel format.
     56  * \param type  incoming image data type.
     57  *
     58  * \return the closest mesa_format for the given format/type arguments
     59  *
     60  * This is called via dd_function_table::ChooseTextureFormat.  Hardware
     61  * drivers may override this function with a specialized version.
     62  */
     63 mesa_format
     64 _mesa_choose_tex_format(struct gl_context *ctx, GLenum target,
     65                         GLint internalFormat, GLenum format, GLenum type)
     66 {
     67    (void) format;
     68 
     69    switch (internalFormat) {
     70    /* shallow RGBA formats */
     71    case 4:
     72    case GL_RGBA:
     73       if (type == GL_UNSIGNED_SHORT_4_4_4_4_REV) {
     74          RETURN_IF_SUPPORTED(MESA_FORMAT_B4G4R4A4_UNORM);
     75       } else if (type == GL_UNSIGNED_SHORT_1_5_5_5_REV) {
     76          RETURN_IF_SUPPORTED(MESA_FORMAT_B5G5R5A1_UNORM);
     77       } else if (type == GL_UNSIGNED_SHORT_5_5_5_1) {
     78          RETURN_IF_SUPPORTED(MESA_FORMAT_B5G5R5A1_UNORM);
     79       } else if (type == GL_UNSIGNED_INT_2_10_10_10_REV) {
     80          RETURN_IF_SUPPORTED(MESA_FORMAT_B10G10R10A2_UNORM);
     81       }
     82       /* fallthrough */
     83 
     84    case GL_RGBA8:
     85       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_UNORM);
     86       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_UNORM);
     87       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
     88       break;
     89    case GL_RGB5_A1:
     90       RETURN_IF_SUPPORTED(MESA_FORMAT_B5G5R5A1_UNORM);
     91       break;
     92    case GL_RGBA2:
     93       RETURN_IF_SUPPORTED(MESA_FORMAT_A4R4G4B4_UNORM); /* just to test another format*/
     94       RETURN_IF_SUPPORTED(MESA_FORMAT_B4G4R4A4_UNORM);
     95       break;
     96    case GL_RGBA4:
     97       RETURN_IF_SUPPORTED(MESA_FORMAT_B4G4R4A4_UNORM);
     98       break;
     99 
    100    /* deep RGBA formats */
    101    case GL_RGB10_A2:
    102       RETURN_IF_SUPPORTED(MESA_FORMAT_R10G10B10A2_UNORM);
    103       RETURN_IF_SUPPORTED(MESA_FORMAT_B10G10R10A2_UNORM);
    104       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
    105       break;
    106    case GL_RGBA12:
    107    case GL_RGBA16:
    108       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UNORM16);
    109       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UNORM16);
    110       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_UNORM);
    111       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
    112       break;
    113 
    114    /* shallow RGB formats */
    115    case 3:
    116    case GL_RGB:
    117       if (type == GL_UNSIGNED_INT_2_10_10_10_REV) {
    118          RETURN_IF_SUPPORTED(MESA_FORMAT_B10G10R10A2_UNORM);
    119       }
    120       /* fallthrough */
    121    case GL_RGB8:
    122       RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_UNORM8);
    123       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8X8_UNORM);
    124       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_UNORM);
    125 
    126       RETURN_IF_SUPPORTED(MESA_FORMAT_BGR_UNORM8);
    127       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8X8_UNORM);
    128       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
    129       break;
    130    case GL_R3_G3_B2:
    131       RETURN_IF_SUPPORTED(MESA_FORMAT_B2G3R3_UNORM);
    132       RETURN_IF_SUPPORTED(MESA_FORMAT_B5G6R5_UNORM);
    133       RETURN_IF_SUPPORTED(MESA_FORMAT_R5G6B5_UNORM);
    134       RETURN_IF_SUPPORTED(MESA_FORMAT_BGR_UNORM8);
    135       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8X8_UNORM);
    136       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
    137       break;
    138    case GL_RGB4:
    139       RETURN_IF_SUPPORTED(MESA_FORMAT_R5G6B5_UNORM); /* just to test another format */
    140       RETURN_IF_SUPPORTED(MESA_FORMAT_B5G6R5_UNORM);
    141       break;
    142    case GL_RGB5:
    143       RETURN_IF_SUPPORTED(MESA_FORMAT_B5G6R5_UNORM);
    144       break;
    145 
    146    /* deep RGB formats */
    147    case GL_RGB10:
    148    case GL_RGB12:
    149    case GL_RGB16:
    150       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_UNORM16);
    151       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UNORM16);
    152       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8X8_UNORM);
    153       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
    154       break;
    155 
    156    /* Alpha formats */
    157    case GL_ALPHA:
    158    case GL_ALPHA4:
    159    case GL_ALPHA8:
    160       RETURN_IF_SUPPORTED(MESA_FORMAT_A_UNORM8);
    161       RETURN_IF_SUPPORTED(MESA_FORMAT_L8A8_UNORM);
    162       break;
    163 
    164    case GL_ALPHA12:
    165    case GL_ALPHA16:
    166       RETURN_IF_SUPPORTED(MESA_FORMAT_A_UNORM16);
    167       RETURN_IF_SUPPORTED(MESA_FORMAT_A_UNORM8);
    168       RETURN_IF_SUPPORTED(MESA_FORMAT_L8A8_UNORM);
    169       break;
    170 
    171    /* Luminance formats */
    172    case 1:
    173    case GL_LUMINANCE:
    174    case GL_LUMINANCE4:
    175    case GL_LUMINANCE8:
    176       RETURN_IF_SUPPORTED(MESA_FORMAT_L_UNORM8);
    177       break;
    178 
    179    case GL_LUMINANCE12:
    180    case GL_LUMINANCE16:
    181       RETURN_IF_SUPPORTED(MESA_FORMAT_L_UNORM16);
    182       RETURN_IF_SUPPORTED(MESA_FORMAT_L_UNORM8);
    183       break;
    184 
    185       /* Luminance/Alpha formats */
    186    case GL_LUMINANCE4_ALPHA4:
    187       RETURN_IF_SUPPORTED(MESA_FORMAT_L4A4_UNORM);
    188       RETURN_IF_SUPPORTED(MESA_FORMAT_L8A8_UNORM);
    189       break;
    190 
    191    case 2:
    192    case GL_LUMINANCE_ALPHA:
    193    case GL_LUMINANCE6_ALPHA2:
    194    case GL_LUMINANCE8_ALPHA8:
    195       RETURN_IF_SUPPORTED(MESA_FORMAT_L8A8_UNORM);
    196       break;
    197 
    198    case GL_LUMINANCE12_ALPHA4:
    199    case GL_LUMINANCE12_ALPHA12:
    200    case GL_LUMINANCE16_ALPHA16:
    201       RETURN_IF_SUPPORTED(MESA_FORMAT_L16A16_UNORM);
    202       RETURN_IF_SUPPORTED(MESA_FORMAT_L8A8_UNORM);
    203       break;
    204 
    205    case GL_INTENSITY:
    206    case GL_INTENSITY4:
    207    case GL_INTENSITY8:
    208       RETURN_IF_SUPPORTED(MESA_FORMAT_I_UNORM8);
    209       break;
    210 
    211    case GL_INTENSITY12:
    212    case GL_INTENSITY16:
    213       RETURN_IF_SUPPORTED(MESA_FORMAT_I_UNORM16);
    214       RETURN_IF_SUPPORTED(MESA_FORMAT_I_UNORM8);
    215       break;
    216 
    217    case GL_DEPTH_COMPONENT:
    218    case GL_DEPTH_COMPONENT24:
    219    case GL_DEPTH_COMPONENT32:
    220       RETURN_IF_SUPPORTED(MESA_FORMAT_Z_UNORM32);
    221       RETURN_IF_SUPPORTED(MESA_FORMAT_Z24_UNORM_X8_UINT);
    222       RETURN_IF_SUPPORTED(MESA_FORMAT_Z24_UNORM_S8_UINT);
    223       break;
    224    case GL_DEPTH_COMPONENT16:
    225       RETURN_IF_SUPPORTED(MESA_FORMAT_Z_UNORM16);
    226       RETURN_IF_SUPPORTED(MESA_FORMAT_Z24_UNORM_X8_UINT);
    227       RETURN_IF_SUPPORTED(MESA_FORMAT_Z24_UNORM_S8_UINT);
    228       break;
    229 
    230    case GL_COMPRESSED_ALPHA_ARB:
    231       RETURN_IF_SUPPORTED(MESA_FORMAT_A_UNORM8);
    232       RETURN_IF_SUPPORTED(MESA_FORMAT_L8A8_UNORM);
    233       break;
    234    case GL_COMPRESSED_LUMINANCE_ARB:
    235       RETURN_IF_SUPPORTED(MESA_FORMAT_L_UNORM8);
    236       break;
    237    case GL_COMPRESSED_LUMINANCE_ALPHA_ARB:
    238       RETURN_IF_SUPPORTED(MESA_FORMAT_L8A8_UNORM);
    239       break;
    240    case GL_COMPRESSED_INTENSITY_ARB:
    241       RETURN_IF_SUPPORTED(MESA_FORMAT_I_UNORM8);
    242       break;
    243    case GL_COMPRESSED_RGB_ARB:
    244       /* We don't use texture compression for 1D and 1D array textures.
    245        * For 1D textures, compressions doesn't buy us much.
    246        * For 1D ARRAY textures, there's complicated issues with updating
    247        * sub-regions on non-block boundaries with glCopyTexSubImage, among
    248        * other issues.  FWIW, the GL_EXT_texture_array extension prohibits
    249        * 1D ARRAY textures in S3TC format.
    250        */
    251       if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY) {
    252          if (ctx->Mesa_DXTn)
    253             RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_DXT1);
    254          RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_FXT1);
    255       }
    256       RETURN_IF_SUPPORTED(MESA_FORMAT_BGR_UNORM8);
    257       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8X8_UNORM);
    258       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
    259       break;
    260    case GL_COMPRESSED_RGBA_ARB:
    261       /* We don't use texture compression for 1D and 1D array textures. */
    262       if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY) {
    263          if (ctx->Mesa_DXTn)
    264             RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_DXT3); /* Not rgba_dxt1, see spec */
    265          RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FXT1);
    266       }
    267       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_UNORM);
    268       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
    269       break;
    270 
    271    case GL_RGB565:
    272       RETURN_IF_SUPPORTED(MESA_FORMAT_B5G6R5_UNORM);
    273       break;
    274 
    275    case GL_YCBCR_MESA:
    276       if (type == GL_UNSIGNED_SHORT_8_8_MESA)
    277          RETURN_IF_SUPPORTED(MESA_FORMAT_YCBCR);
    278       else
    279          RETURN_IF_SUPPORTED(MESA_FORMAT_YCBCR_REV);
    280       break;
    281 
    282    case GL_ALPHA16F_ARB:
    283       RETURN_IF_SUPPORTED(MESA_FORMAT_A_FLOAT16);
    284       RETURN_IF_SUPPORTED(MESA_FORMAT_A_FLOAT32);
    285       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    286       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    287       break;
    288    case GL_ALPHA32F_ARB:
    289       RETURN_IF_SUPPORTED(MESA_FORMAT_A_FLOAT32);
    290       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    291       RETURN_IF_SUPPORTED(MESA_FORMAT_A_FLOAT16);
    292       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    293       break;
    294    case GL_LUMINANCE16F_ARB:
    295       RETURN_IF_SUPPORTED(MESA_FORMAT_L_FLOAT16);
    296       RETURN_IF_SUPPORTED(MESA_FORMAT_L_FLOAT32);
    297       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    298       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    299       break;
    300    case GL_LUMINANCE32F_ARB:
    301       RETURN_IF_SUPPORTED(MESA_FORMAT_L_FLOAT32);
    302       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    303       RETURN_IF_SUPPORTED(MESA_FORMAT_L_FLOAT16);
    304       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    305       break;
    306    case GL_LUMINANCE_ALPHA16F_ARB:
    307       RETURN_IF_SUPPORTED(MESA_FORMAT_LA_FLOAT16);
    308       RETURN_IF_SUPPORTED(MESA_FORMAT_LA_FLOAT32);
    309       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    310       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    311       break;
    312    case GL_LUMINANCE_ALPHA32F_ARB:
    313       RETURN_IF_SUPPORTED(MESA_FORMAT_LA_FLOAT32);
    314       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    315       RETURN_IF_SUPPORTED(MESA_FORMAT_LA_FLOAT16);
    316       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    317       break;
    318    case GL_INTENSITY16F_ARB:
    319       RETURN_IF_SUPPORTED(MESA_FORMAT_I_FLOAT16);
    320       RETURN_IF_SUPPORTED(MESA_FORMAT_I_FLOAT32);
    321       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    322       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    323       break;
    324    case GL_INTENSITY32F_ARB:
    325       RETURN_IF_SUPPORTED(MESA_FORMAT_I_FLOAT32);
    326       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    327       RETURN_IF_SUPPORTED(MESA_FORMAT_I_FLOAT16);
    328       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    329       break;
    330    case GL_RGB16F_ARB:
    331       RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_FLOAT16);
    332       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_FLOAT16);
    333       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    334       RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_FLOAT32);
    335       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    336       break;
    337    case GL_RGB32F_ARB:
    338       RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_FLOAT32);
    339       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_FLOAT32);
    340       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    341       RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_FLOAT16);
    342       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    343       break;
    344    case GL_RGBA16F_ARB:
    345       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    346       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    347       break;
    348    case GL_RGBA32F_ARB:
    349       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    350       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    351       break;
    352 
    353    case GL_RGB9_E5:
    354       /* GL_EXT_texture_shared_exponent -- just one format to support */
    355       assert(ctx->TextureFormatSupported[MESA_FORMAT_R9G9B9E5_FLOAT]);
    356       return MESA_FORMAT_R9G9B9E5_FLOAT;
    357 
    358    case GL_R11F_G11F_B10F:
    359       /* GL_EXT_texture_packed_float -- just one format to support */
    360       assert(ctx->TextureFormatSupported[MESA_FORMAT_R11G11B10_FLOAT]);
    361       return MESA_FORMAT_R11G11B10_FLOAT;
    362 
    363    case GL_DEPTH_STENCIL_EXT:
    364    case GL_DEPTH24_STENCIL8_EXT:
    365       RETURN_IF_SUPPORTED(MESA_FORMAT_S8_UINT_Z24_UNORM);
    366       RETURN_IF_SUPPORTED(MESA_FORMAT_Z24_UNORM_S8_UINT);
    367       break;
    368 
    369    case GL_DEPTH_COMPONENT32F:
    370       assert(ctx->TextureFormatSupported[MESA_FORMAT_Z_FLOAT32]);
    371       return MESA_FORMAT_Z_FLOAT32;
    372    case GL_DEPTH32F_STENCIL8:
    373       assert(ctx->TextureFormatSupported[MESA_FORMAT_Z32_FLOAT_S8X24_UINT]);
    374       return MESA_FORMAT_Z32_FLOAT_S8X24_UINT;
    375 
    376    case GL_RED_SNORM:
    377    case GL_R8_SNORM:
    378       RETURN_IF_SUPPORTED(MESA_FORMAT_R_SNORM8);
    379       break;
    380    case GL_RG_SNORM:
    381    case GL_RG8_SNORM:
    382       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8_SNORM);
    383       break;
    384    case GL_RGB_SNORM:
    385    case GL_RGB8_SNORM:
    386       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8X8_SNORM);
    387       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
    388       RETURN_IF_SUPPORTED(MESA_FORMAT_X8B8G8R8_SNORM);
    389       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
    390       break;
    391    case GL_RGBA_SNORM:
    392    case GL_RGBA8_SNORM:
    393       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
    394       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
    395       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
    396       break;
    397    case GL_ALPHA_SNORM:
    398    case GL_ALPHA8_SNORM:
    399       RETURN_IF_SUPPORTED(MESA_FORMAT_A_SNORM8);
    400       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
    401       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
    402       break;
    403    case GL_LUMINANCE_SNORM:
    404    case GL_LUMINANCE8_SNORM:
    405       RETURN_IF_SUPPORTED(MESA_FORMAT_L_SNORM8);
    406       RETURN_IF_SUPPORTED(MESA_FORMAT_X8B8G8R8_SNORM);
    407       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
    408       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
    409       break;
    410    case GL_LUMINANCE_ALPHA_SNORM:
    411    case GL_LUMINANCE8_ALPHA8_SNORM:
    412       RETURN_IF_SUPPORTED(MESA_FORMAT_L8A8_SNORM);
    413       RETURN_IF_SUPPORTED(MESA_FORMAT_A8L8_SNORM);
    414       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
    415       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
    416       break;
    417    case GL_INTENSITY_SNORM:
    418    case GL_INTENSITY8_SNORM:
    419       RETURN_IF_SUPPORTED(MESA_FORMAT_I_SNORM8);
    420       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
    421       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
    422       break;
    423    case GL_R16_SNORM:
    424       RETURN_IF_SUPPORTED(MESA_FORMAT_R_SNORM16);
    425       break;
    426    case GL_RG16_SNORM:
    427       RETURN_IF_SUPPORTED(MESA_FORMAT_R16G16_SNORM);
    428       break;
    429    case GL_RGB16_SNORM:
    430       RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_SNORM16);
    431       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_SNORM16);
    432       /* FALLTHROUGH */
    433    case GL_RGBA16_SNORM:
    434       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SNORM16);
    435       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
    436       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
    437       break;
    438    case GL_ALPHA16_SNORM:
    439       RETURN_IF_SUPPORTED(MESA_FORMAT_A_SNORM16);
    440       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SNORM16);
    441       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
    442       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
    443       break;
    444    case GL_LUMINANCE16_SNORM:
    445       RETURN_IF_SUPPORTED(MESA_FORMAT_L_SNORM16);
    446       /* FALLTHROUGH */
    447    case GL_LUMINANCE16_ALPHA16_SNORM:
    448       RETURN_IF_SUPPORTED(MESA_FORMAT_LA_SNORM16);
    449       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SNORM16);
    450       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
    451       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
    452       break;
    453    case GL_INTENSITY16_SNORM:
    454       RETURN_IF_SUPPORTED(MESA_FORMAT_I_SNORM16);
    455       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SNORM16);
    456       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
    457       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
    458       break;
    459 
    460    case GL_SRGB_EXT:
    461    case GL_SRGB8_EXT:
    462       /* there is no MESA_FORMAT_RGB_SRGB8 */
    463       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8X8_SRGB);
    464       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SRGB);
    465 
    466       RETURN_IF_SUPPORTED(MESA_FORMAT_BGR_SRGB8);
    467       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
    468 
    469       RETURN_IF_SUPPORTED(MESA_FORMAT_X8B8G8R8_SRGB);
    470       RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
    471       break;
    472    case GL_SRGB_ALPHA_EXT:
    473    case GL_SRGB8_ALPHA8_EXT:
    474       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SRGB);
    475 
    476       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SRGB);
    477       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
    478       RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
    479       break;
    480    case GL_SLUMINANCE_EXT:
    481    case GL_SLUMINANCE8_EXT:
    482       RETURN_IF_SUPPORTED(MESA_FORMAT_L_SRGB8);
    483       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
    484       RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
    485       break;
    486    case GL_SLUMINANCE_ALPHA_EXT:
    487    case GL_SLUMINANCE8_ALPHA8_EXT:
    488       RETURN_IF_SUPPORTED(MESA_FORMAT_L8A8_SRGB);
    489       RETURN_IF_SUPPORTED(MESA_FORMAT_A8L8_SRGB);
    490       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
    491       RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
    492       break;
    493    case GL_COMPRESSED_SLUMINANCE_EXT:
    494       RETURN_IF_SUPPORTED(MESA_FORMAT_L_SRGB8);
    495       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
    496       RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
    497       break;
    498    case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
    499       RETURN_IF_SUPPORTED(MESA_FORMAT_L8A8_SRGB);
    500       RETURN_IF_SUPPORTED(MESA_FORMAT_A8L8_SRGB);
    501       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
    502       RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
    503       break;
    504    case GL_COMPRESSED_SRGB_EXT:
    505       if (ctx->Mesa_DXTn)
    506          RETURN_IF_SUPPORTED(MESA_FORMAT_SRGB_DXT1);
    507       RETURN_IF_SUPPORTED(MESA_FORMAT_BGR_SRGB8);
    508       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
    509       RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
    510       break;
    511    case GL_COMPRESSED_SRGB_ALPHA_EXT:
    512       if (ctx->Mesa_DXTn)
    513          RETURN_IF_SUPPORTED(MESA_FORMAT_SRGBA_DXT3); /* Not srgba_dxt1, see spec */
    514       RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SRGB);
    515       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
    516       RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
    517       break;
    518 
    519    case GL_ALPHA8UI_EXT:
    520       RETURN_IF_SUPPORTED(MESA_FORMAT_A_UINT8);
    521       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT8);
    522       break;
    523    case GL_ALPHA16UI_EXT:
    524       RETURN_IF_SUPPORTED(MESA_FORMAT_A_UINT16);
    525       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT16);
    526       break;
    527    case GL_ALPHA32UI_EXT:
    528       RETURN_IF_SUPPORTED(MESA_FORMAT_A_UINT32);
    529       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT32);
    530       break;
    531    case GL_ALPHA8I_EXT:
    532       RETURN_IF_SUPPORTED(MESA_FORMAT_A_SINT8);
    533       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT8);
    534       break;
    535    case GL_ALPHA16I_EXT:
    536       RETURN_IF_SUPPORTED(MESA_FORMAT_A_SINT16);
    537       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT16);
    538       break;
    539    case GL_ALPHA32I_EXT:
    540       RETURN_IF_SUPPORTED(MESA_FORMAT_A_SINT32);
    541       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT32);
    542       break;
    543    case GL_LUMINANCE8UI_EXT:
    544       RETURN_IF_SUPPORTED(MESA_FORMAT_L_UINT8);
    545       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT8);
    546       break;
    547    case GL_LUMINANCE16UI_EXT:
    548       RETURN_IF_SUPPORTED(MESA_FORMAT_L_UINT16);
    549       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT16);
    550       break;
    551    case GL_LUMINANCE32UI_EXT:
    552       RETURN_IF_SUPPORTED(MESA_FORMAT_L_UINT32);
    553       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT32);
    554       break;
    555    case GL_LUMINANCE8I_EXT:
    556       RETURN_IF_SUPPORTED(MESA_FORMAT_L_SINT8);
    557       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT8);
    558       break;
    559    case GL_LUMINANCE16I_EXT:
    560       RETURN_IF_SUPPORTED(MESA_FORMAT_L_SINT16);
    561       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT16);
    562       break;
    563    case GL_LUMINANCE32I_EXT:
    564       RETURN_IF_SUPPORTED(MESA_FORMAT_L_SINT32);
    565       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT32);
    566       break;
    567    case GL_LUMINANCE_ALPHA8UI_EXT:
    568       RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UINT8);
    569       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT8);
    570       break;
    571    case GL_LUMINANCE_ALPHA16UI_EXT:
    572       RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UINT16);
    573       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT16);
    574       break;
    575    case GL_LUMINANCE_ALPHA32UI_EXT:
    576       RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UINT32);
    577       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT32);
    578       break;
    579    case GL_LUMINANCE_ALPHA8I_EXT:
    580       RETURN_IF_SUPPORTED(MESA_FORMAT_LA_SINT8);
    581       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT8);
    582       break;
    583    case GL_LUMINANCE_ALPHA16I_EXT:
    584       RETURN_IF_SUPPORTED(MESA_FORMAT_LA_SINT16);
    585       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT16);
    586       break;
    587    case GL_LUMINANCE_ALPHA32I_EXT:
    588       RETURN_IF_SUPPORTED(MESA_FORMAT_LA_SINT32);
    589       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT32);
    590       break;
    591    case GL_INTENSITY8UI_EXT:
    592       RETURN_IF_SUPPORTED(MESA_FORMAT_I_UINT8);
    593       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT8);
    594       break;
    595    case GL_INTENSITY16UI_EXT:
    596       RETURN_IF_SUPPORTED(MESA_FORMAT_I_UINT16);
    597       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT16);
    598       break;
    599    case GL_INTENSITY32UI_EXT:
    600       RETURN_IF_SUPPORTED(MESA_FORMAT_I_UINT32);
    601       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT32);
    602       break;
    603    case GL_INTENSITY8I_EXT:
    604       RETURN_IF_SUPPORTED(MESA_FORMAT_I_SINT8);
    605       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT8);
    606       break;
    607    case GL_INTENSITY16I_EXT:
    608       RETURN_IF_SUPPORTED(MESA_FORMAT_I_SINT16);
    609       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT16);
    610       break;
    611    case GL_INTENSITY32I_EXT:
    612       RETURN_IF_SUPPORTED(MESA_FORMAT_I_SINT32);
    613       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT32);
    614       break;
    615 
    616    case GL_RGB8UI_EXT:
    617       RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_UINT8);
    618       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_UINT8);
    619       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT8);
    620       break;
    621    case GL_RGB16UI_EXT:
    622       RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_UINT16);
    623       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_UINT16);
    624       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT16);
    625       break;
    626    case GL_RGB32UI_EXT:
    627       RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_UINT32);
    628       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_UINT32);
    629       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT32);
    630       break;
    631    case GL_RGB8I_EXT:
    632       RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_SINT8);
    633       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_SINT8);
    634       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT8);
    635       break;
    636    case GL_RGB16I_EXT:
    637       RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_SINT16);
    638       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_SINT16);
    639       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT16);
    640       break;
    641    case GL_RGB32I_EXT:
    642       RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_SINT32);
    643       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_SINT32);
    644       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT32);
    645       break;
    646    case GL_RGBA8UI_EXT:
    647       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT8);
    648       break;
    649    case GL_RGBA16UI_EXT:
    650       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT16);
    651       break;
    652    case GL_RGBA32UI_EXT:
    653       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT32);
    654       break;
    655    case GL_RGBA8I_EXT:
    656       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT8);
    657       break;
    658    case GL_RGBA16I_EXT:
    659       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT16);
    660       break;
    661    case GL_RGBA32I_EXT:
    662       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT32);
    663       break;
    664 
    665    case GL_R8:
    666    case GL_RED:
    667       RETURN_IF_SUPPORTED(MESA_FORMAT_R_UNORM8);
    668       break;
    669 
    670    case GL_COMPRESSED_RED:
    671       if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY)
    672          RETURN_IF_SUPPORTED(MESA_FORMAT_R_RGTC1_UNORM);
    673       RETURN_IF_SUPPORTED(MESA_FORMAT_R_UNORM8);
    674       break;
    675 
    676    case GL_R16:
    677       RETURN_IF_SUPPORTED(MESA_FORMAT_R_UNORM16);
    678       break;
    679 
    680    case GL_RG:
    681    case GL_RG8:
    682       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8_UNORM);
    683       break;
    684 
    685    case GL_COMPRESSED_RG:
    686       if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY)
    687          RETURN_IF_SUPPORTED(MESA_FORMAT_RG_RGTC2_UNORM);
    688       RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8_UNORM);
    689       break;
    690 
    691    case GL_RG16:
    692       RETURN_IF_SUPPORTED(MESA_FORMAT_R16G16_UNORM);
    693       break;
    694 
    695    case GL_R16F:
    696       RETURN_IF_SUPPORTED(MESA_FORMAT_R_FLOAT16);
    697       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT16);
    698       RETURN_IF_SUPPORTED(MESA_FORMAT_R_FLOAT32);
    699       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT32);
    700       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    701       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    702       break;
    703    case GL_R32F:
    704       RETURN_IF_SUPPORTED(MESA_FORMAT_R_FLOAT32);
    705       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT32);
    706       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    707       RETURN_IF_SUPPORTED(MESA_FORMAT_R_FLOAT16);
    708       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT16);
    709       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    710       break;
    711    case GL_RG16F:
    712       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT16);
    713       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT32);
    714       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    715       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    716       break;
    717    case GL_RG32F:
    718       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT32);
    719       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
    720       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT16);
    721       RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
    722       break;
    723 
    724    case GL_R8UI:
    725       RETURN_IF_SUPPORTED(MESA_FORMAT_R_UINT8);
    726       break;
    727    case GL_RG8UI:
    728       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_UINT8);
    729       break;
    730    case GL_R16UI:
    731       RETURN_IF_SUPPORTED(MESA_FORMAT_R_UINT16);
    732       break;
    733    case GL_RG16UI:
    734       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_UINT16);
    735       break;
    736    case GL_R32UI:
    737       RETURN_IF_SUPPORTED(MESA_FORMAT_R_UINT32);
    738       break;
    739    case GL_RG32UI:
    740       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_UINT32);
    741       break;
    742    case GL_R8I:
    743       RETURN_IF_SUPPORTED(MESA_FORMAT_R_SINT8);
    744       break;
    745    case GL_RG8I:
    746       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_SINT8);
    747       break;
    748    case GL_R16I:
    749       RETURN_IF_SUPPORTED(MESA_FORMAT_R_SINT16);
    750       break;
    751    case GL_RG16I:
    752       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_SINT16);
    753       break;
    754    case GL_R32I:
    755       RETURN_IF_SUPPORTED(MESA_FORMAT_R_SINT32);
    756       break;
    757    case GL_RG32I:
    758       RETURN_IF_SUPPORTED(MESA_FORMAT_RG_SINT32);
    759       break;
    760 
    761    case GL_RGB10_A2UI:
    762       RETURN_IF_SUPPORTED(MESA_FORMAT_B10G10R10A2_UINT);
    763       RETURN_IF_SUPPORTED(MESA_FORMAT_R10G10B10A2_UINT);
    764       break;
    765 
    766    case GL_BGRA:
    767       RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
    768       break;
    769 
    770    case GL_STENCIL_INDEX:
    771    case GL_STENCIL_INDEX8:
    772       RETURN_IF_SUPPORTED(MESA_FORMAT_S_UINT8);
    773       break;
    774 
    775    default:
    776       /* For non-generic compressed format we assert two things:
    777        *
    778        * 1. The format has already been validated against the set of available
    779        *    extensions.
    780        *
    781        * 2. The driver only enables the extension if it supports all of the
    782        *    formats that are part of that extension.
    783        */
    784       if (_mesa_is_compressed_format(ctx, internalFormat))
    785          return _mesa_glenum_to_compressed_format(internalFormat);
    786    }
    787 
    788    _mesa_problem(ctx, "unexpected format %s in _mesa_choose_tex_format()",
    789                  _mesa_enum_to_string(internalFormat));
    790    return MESA_FORMAT_NONE;
    791 }
    792