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 */
63mesa_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      RETURN_IF_SUPPORTED(MESA_FORMAT_A1B5G5R5_UNORM);
92      break;
93   case GL_RGBA2:
94      RETURN_IF_SUPPORTED(MESA_FORMAT_A4R4G4B4_UNORM); /* just to test another format*/
95      RETURN_IF_SUPPORTED(MESA_FORMAT_B4G4R4A4_UNORM);
96      break;
97   case GL_RGBA4:
98      RETURN_IF_SUPPORTED(MESA_FORMAT_B4G4R4A4_UNORM);
99      break;
100
101   /* deep RGBA formats */
102   case GL_RGB10_A2:
103      RETURN_IF_SUPPORTED(MESA_FORMAT_R10G10B10A2_UNORM);
104      RETURN_IF_SUPPORTED(MESA_FORMAT_B10G10R10A2_UNORM);
105      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
106      break;
107   case GL_RGBA12:
108   case GL_RGBA16:
109      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UNORM16);
110      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UNORM16);
111      RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_UNORM);
112      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
113      break;
114
115   /* shallow RGB formats */
116   case 3:
117   case GL_RGB:
118      if (type == GL_UNSIGNED_INT_2_10_10_10_REV) {
119         RETURN_IF_SUPPORTED(MESA_FORMAT_B10G10R10A2_UNORM);
120      }
121      if (type == GL_UNSIGNED_SHORT_5_6_5) {
122         RETURN_IF_SUPPORTED(MESA_FORMAT_B5G6R5_UNORM);
123      }
124      FALLTHROUGH;
125   case GL_RGB8:
126      RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_UNORM8);
127      RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8X8_UNORM);
128      RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_UNORM);
129
130      RETURN_IF_SUPPORTED(MESA_FORMAT_BGR_UNORM8);
131      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8X8_UNORM);
132      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
133      break;
134   case GL_R3_G3_B2:
135      RETURN_IF_SUPPORTED(MESA_FORMAT_B2G3R3_UNORM);
136      RETURN_IF_SUPPORTED(MESA_FORMAT_B5G6R5_UNORM);
137      RETURN_IF_SUPPORTED(MESA_FORMAT_R5G6B5_UNORM);
138      RETURN_IF_SUPPORTED(MESA_FORMAT_BGR_UNORM8);
139      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8X8_UNORM);
140      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
141      break;
142   case GL_RGB4:
143      RETURN_IF_SUPPORTED(MESA_FORMAT_R5G6B5_UNORM); /* just to test another format */
144      RETURN_IF_SUPPORTED(MESA_FORMAT_B5G6R5_UNORM);
145      break;
146   case GL_RGB5:
147      RETURN_IF_SUPPORTED(MESA_FORMAT_B5G6R5_UNORM);
148      break;
149
150   /* deep RGB formats */
151   case GL_RGB10:
152   case GL_RGB12:
153   case GL_RGB16:
154      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_UNORM16);
155      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UNORM16);
156      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8X8_UNORM);
157      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
158      break;
159
160   /* Alpha formats */
161   case GL_ALPHA:
162   case GL_ALPHA4:
163   case GL_ALPHA8:
164      RETURN_IF_SUPPORTED(MESA_FORMAT_A_UNORM8);
165      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UNORM8);
166      break;
167
168   case GL_ALPHA12:
169   case GL_ALPHA16:
170      RETURN_IF_SUPPORTED(MESA_FORMAT_A_UNORM16);
171      RETURN_IF_SUPPORTED(MESA_FORMAT_A_UNORM8);
172      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UNORM8);
173      break;
174
175   /* Luminance formats */
176   case 1:
177   case GL_LUMINANCE:
178   case GL_LUMINANCE4:
179   case GL_LUMINANCE8:
180      RETURN_IF_SUPPORTED(MESA_FORMAT_L_UNORM8);
181      break;
182
183   case GL_LUMINANCE12:
184   case GL_LUMINANCE16:
185      RETURN_IF_SUPPORTED(MESA_FORMAT_L_UNORM16);
186      RETURN_IF_SUPPORTED(MESA_FORMAT_L_UNORM8);
187      break;
188
189      /* Luminance/Alpha formats */
190   case GL_LUMINANCE4_ALPHA4:
191      RETURN_IF_SUPPORTED(MESA_FORMAT_L4A4_UNORM);
192      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UNORM8);
193      break;
194
195   case 2:
196   case GL_LUMINANCE_ALPHA:
197   case GL_LUMINANCE6_ALPHA2:
198   case GL_LUMINANCE8_ALPHA8:
199      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UNORM8);
200      break;
201
202   case GL_LUMINANCE12_ALPHA4:
203   case GL_LUMINANCE12_ALPHA12:
204   case GL_LUMINANCE16_ALPHA16:
205      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UNORM16);
206      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UNORM8);
207      break;
208
209   case GL_INTENSITY:
210   case GL_INTENSITY4:
211   case GL_INTENSITY8:
212      RETURN_IF_SUPPORTED(MESA_FORMAT_I_UNORM8);
213      break;
214
215   case GL_INTENSITY12:
216   case GL_INTENSITY16:
217      RETURN_IF_SUPPORTED(MESA_FORMAT_I_UNORM16);
218      RETURN_IF_SUPPORTED(MESA_FORMAT_I_UNORM8);
219      break;
220
221   case GL_DEPTH_COMPONENT:
222   case GL_DEPTH_COMPONENT24:
223   case GL_DEPTH_COMPONENT32:
224      RETURN_IF_SUPPORTED(MESA_FORMAT_Z_UNORM32);
225      RETURN_IF_SUPPORTED(MESA_FORMAT_Z24_UNORM_X8_UINT);
226      RETURN_IF_SUPPORTED(MESA_FORMAT_Z24_UNORM_S8_UINT);
227      break;
228   case GL_DEPTH_COMPONENT16:
229      RETURN_IF_SUPPORTED(MESA_FORMAT_Z_UNORM16);
230      RETURN_IF_SUPPORTED(MESA_FORMAT_Z24_UNORM_X8_UINT);
231      RETURN_IF_SUPPORTED(MESA_FORMAT_Z24_UNORM_S8_UINT);
232      break;
233
234   case GL_COMPRESSED_ALPHA_ARB:
235      RETURN_IF_SUPPORTED(MESA_FORMAT_A_UNORM8);
236      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UNORM8);
237      break;
238   case GL_COMPRESSED_LUMINANCE_ARB:
239      RETURN_IF_SUPPORTED(MESA_FORMAT_L_UNORM8);
240      break;
241   case GL_COMPRESSED_LUMINANCE_ALPHA_ARB:
242      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UNORM8);
243      break;
244   case GL_COMPRESSED_INTENSITY_ARB:
245      RETURN_IF_SUPPORTED(MESA_FORMAT_I_UNORM8);
246      break;
247   case GL_COMPRESSED_RGB_ARB:
248      /* We don't use texture compression for 1D and 1D array textures.
249       * For 1D textures, compressions doesn't buy us much.
250       * For 1D ARRAY textures, there's complicated issues with updating
251       * sub-regions on non-block boundaries with glCopyTexSubImage, among
252       * other issues.  FWIW, the GL_EXT_texture_array extension prohibits
253       * 1D ARRAY textures in S3TC format.
254       */
255      if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY) {
256         RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_DXT1);
257      }
258      RETURN_IF_SUPPORTED(MESA_FORMAT_BGR_UNORM8);
259      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8X8_UNORM);
260      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
261      break;
262   case GL_COMPRESSED_RGBA_ARB:
263      /* We don't use texture compression for 1D and 1D array textures. */
264      if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY) {
265         RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_DXT3); /* Not rgba_dxt1, see spec */
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_RG_SNORM8);
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_LA_SNORM8);
413      RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
414      RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
415      break;
416   case GL_INTENSITY_SNORM:
417   case GL_INTENSITY8_SNORM:
418      RETURN_IF_SUPPORTED(MESA_FORMAT_I_SNORM8);
419      RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
420      RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
421      break;
422   case GL_R16_SNORM:
423      RETURN_IF_SUPPORTED(MESA_FORMAT_R_SNORM16);
424      break;
425   case GL_RG16_SNORM:
426      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_SNORM16);
427      break;
428   case GL_RGB16_SNORM:
429      RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_SNORM16);
430      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_SNORM16);
431      FALLTHROUGH;
432   case GL_RGBA16_SNORM:
433      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SNORM16);
434      RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
435      RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
436      break;
437   case GL_ALPHA16_SNORM:
438      RETURN_IF_SUPPORTED(MESA_FORMAT_A_SNORM16);
439      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SNORM16);
440      RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
441      RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
442      break;
443   case GL_LUMINANCE16_SNORM:
444      RETURN_IF_SUPPORTED(MESA_FORMAT_L_SNORM16);
445      FALLTHROUGH;
446   case GL_LUMINANCE16_ALPHA16_SNORM:
447      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_SNORM16);
448      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SNORM16);
449      RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
450      RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
451      break;
452   case GL_INTENSITY16_SNORM:
453      RETURN_IF_SUPPORTED(MESA_FORMAT_I_SNORM16);
454      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SNORM16);
455      RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SNORM);
456      RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SNORM);
457      break;
458
459   case GL_SRGB_EXT:
460   case GL_SRGB8_EXT:
461      /* there is no MESA_FORMAT_RGB_SRGB8 */
462      RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8X8_SRGB);
463      RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SRGB);
464
465      RETURN_IF_SUPPORTED(MESA_FORMAT_BGR_SRGB8);
466      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
467
468      RETURN_IF_SUPPORTED(MESA_FORMAT_X8B8G8R8_SRGB);
469      RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
470      break;
471   case GL_SRGB_ALPHA_EXT:
472   case GL_SRGB8_ALPHA8_EXT:
473      RETURN_IF_SUPPORTED(MESA_FORMAT_R8G8B8A8_SRGB);
474
475      RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SRGB);
476      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
477      RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
478      break;
479   case GL_SR8_EXT:
480      RETURN_IF_SUPPORTED(MESA_FORMAT_R_SRGB8);
481      break;
482   case GL_SRG8_EXT:
483      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_SRGB8);
484      break;
485   case GL_SLUMINANCE_EXT:
486   case GL_SLUMINANCE8_EXT:
487      RETURN_IF_SUPPORTED(MESA_FORMAT_L_SRGB8);
488      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
489      RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
490      break;
491   case GL_SLUMINANCE_ALPHA_EXT:
492   case GL_SLUMINANCE8_ALPHA8_EXT:
493      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_SRGB8);
494      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
495      RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
496      break;
497   case GL_COMPRESSED_SLUMINANCE_EXT:
498      RETURN_IF_SUPPORTED(MESA_FORMAT_L_SRGB8);
499      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
500      RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
501      break;
502   case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
503      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_SRGB8);
504      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
505      RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
506      break;
507   case GL_COMPRESSED_SRGB_EXT:
508      RETURN_IF_SUPPORTED(MESA_FORMAT_SRGB_DXT1);
509      RETURN_IF_SUPPORTED(MESA_FORMAT_BGR_SRGB8);
510      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
511      RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
512      break;
513   case GL_COMPRESSED_SRGB_ALPHA_EXT:
514      RETURN_IF_SUPPORTED(MESA_FORMAT_SRGBA_DXT3); /* Not srgba_dxt1, see spec */
515      RETURN_IF_SUPPORTED(MESA_FORMAT_A8B8G8R8_SRGB);
516      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_SRGB);
517      RETURN_IF_SUPPORTED(MESA_FORMAT_A8R8G8B8_SRGB);
518      break;
519
520   case GL_ALPHA8UI_EXT:
521      RETURN_IF_SUPPORTED(MESA_FORMAT_A_UINT8);
522      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT8);
523      break;
524   case GL_ALPHA16UI_EXT:
525      RETURN_IF_SUPPORTED(MESA_FORMAT_A_UINT16);
526      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT16);
527      break;
528   case GL_ALPHA32UI_EXT:
529      RETURN_IF_SUPPORTED(MESA_FORMAT_A_UINT32);
530      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT32);
531      break;
532   case GL_ALPHA8I_EXT:
533      RETURN_IF_SUPPORTED(MESA_FORMAT_A_SINT8);
534      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT8);
535      break;
536   case GL_ALPHA16I_EXT:
537      RETURN_IF_SUPPORTED(MESA_FORMAT_A_SINT16);
538      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT16);
539      break;
540   case GL_ALPHA32I_EXT:
541      RETURN_IF_SUPPORTED(MESA_FORMAT_A_SINT32);
542      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT32);
543      break;
544   case GL_LUMINANCE8UI_EXT:
545      RETURN_IF_SUPPORTED(MESA_FORMAT_L_UINT8);
546      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT8);
547      break;
548   case GL_LUMINANCE16UI_EXT:
549      RETURN_IF_SUPPORTED(MESA_FORMAT_L_UINT16);
550      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT16);
551      break;
552   case GL_LUMINANCE32UI_EXT:
553      RETURN_IF_SUPPORTED(MESA_FORMAT_L_UINT32);
554      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT32);
555      break;
556   case GL_LUMINANCE8I_EXT:
557      RETURN_IF_SUPPORTED(MESA_FORMAT_L_SINT8);
558      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT8);
559      break;
560   case GL_LUMINANCE16I_EXT:
561      RETURN_IF_SUPPORTED(MESA_FORMAT_L_SINT16);
562      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT16);
563      break;
564   case GL_LUMINANCE32I_EXT:
565      RETURN_IF_SUPPORTED(MESA_FORMAT_L_SINT32);
566      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT32);
567      break;
568   case GL_LUMINANCE_ALPHA8UI_EXT:
569      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UINT8);
570      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT8);
571      break;
572   case GL_LUMINANCE_ALPHA16UI_EXT:
573      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UINT16);
574      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT16);
575      break;
576   case GL_LUMINANCE_ALPHA32UI_EXT:
577      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_UINT32);
578      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT32);
579      break;
580   case GL_LUMINANCE_ALPHA8I_EXT:
581      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_SINT8);
582      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT8);
583      break;
584   case GL_LUMINANCE_ALPHA16I_EXT:
585      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_SINT16);
586      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT16);
587      break;
588   case GL_LUMINANCE_ALPHA32I_EXT:
589      RETURN_IF_SUPPORTED(MESA_FORMAT_LA_SINT32);
590      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT32);
591      break;
592   case GL_INTENSITY8UI_EXT:
593      RETURN_IF_SUPPORTED(MESA_FORMAT_I_UINT8);
594      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT8);
595      break;
596   case GL_INTENSITY16UI_EXT:
597      RETURN_IF_SUPPORTED(MESA_FORMAT_I_UINT16);
598      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT16);
599      break;
600   case GL_INTENSITY32UI_EXT:
601      RETURN_IF_SUPPORTED(MESA_FORMAT_I_UINT32);
602      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT32);
603      break;
604   case GL_INTENSITY8I_EXT:
605      RETURN_IF_SUPPORTED(MESA_FORMAT_I_SINT8);
606      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT8);
607      break;
608   case GL_INTENSITY16I_EXT:
609      RETURN_IF_SUPPORTED(MESA_FORMAT_I_SINT16);
610      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT16);
611      break;
612   case GL_INTENSITY32I_EXT:
613      RETURN_IF_SUPPORTED(MESA_FORMAT_I_SINT32);
614      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT32);
615      break;
616
617   case GL_RGB8UI_EXT:
618      RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_UINT8);
619      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_UINT8);
620      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT8);
621      break;
622   case GL_RGB16UI_EXT:
623      RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_UINT16);
624      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_UINT16);
625      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT16);
626      break;
627   case GL_RGB32UI_EXT:
628      RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_UINT32);
629      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_UINT32);
630      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT32);
631      break;
632   case GL_RGB8I_EXT:
633      RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_SINT8);
634      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_SINT8);
635      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT8);
636      break;
637   case GL_RGB16I_EXT:
638      RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_SINT16);
639      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_SINT16);
640      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT16);
641      break;
642   case GL_RGB32I_EXT:
643      RETURN_IF_SUPPORTED(MESA_FORMAT_RGB_SINT32);
644      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBX_SINT32);
645      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT32);
646      break;
647   case GL_RGBA8UI_EXT:
648      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT8);
649      break;
650   case GL_RGBA16UI_EXT:
651      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT16);
652      break;
653   case GL_RGBA32UI_EXT:
654      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_UINT32);
655      break;
656   case GL_RGBA8I_EXT:
657      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT8);
658      break;
659   case GL_RGBA16I_EXT:
660      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT16);
661      break;
662   case GL_RGBA32I_EXT:
663      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_SINT32);
664      break;
665
666   case GL_R8:
667   case GL_RED:
668      RETURN_IF_SUPPORTED(MESA_FORMAT_R_UNORM8);
669      break;
670
671   case GL_COMPRESSED_RED:
672      if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY)
673         RETURN_IF_SUPPORTED(MESA_FORMAT_R_RGTC1_UNORM);
674      RETURN_IF_SUPPORTED(MESA_FORMAT_R_UNORM8);
675      break;
676
677   case GL_R16:
678      RETURN_IF_SUPPORTED(MESA_FORMAT_R_UNORM16);
679      break;
680
681   case GL_RG:
682   case GL_RG8:
683      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_UNORM8);
684      break;
685
686   case GL_COMPRESSED_RG:
687      if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY)
688         RETURN_IF_SUPPORTED(MESA_FORMAT_RG_RGTC2_UNORM);
689      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_UNORM8);
690      break;
691
692   case GL_RG16:
693      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_UNORM16);
694      break;
695
696   case GL_R16F:
697      RETURN_IF_SUPPORTED(MESA_FORMAT_R_FLOAT16);
698      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT16);
699      RETURN_IF_SUPPORTED(MESA_FORMAT_R_FLOAT32);
700      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT32);
701      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
702      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
703      break;
704   case GL_R32F:
705      RETURN_IF_SUPPORTED(MESA_FORMAT_R_FLOAT32);
706      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT32);
707      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
708      RETURN_IF_SUPPORTED(MESA_FORMAT_R_FLOAT16);
709      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT16);
710      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
711      break;
712   case GL_RG16F:
713      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT16);
714      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT32);
715      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
716      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
717      break;
718   case GL_RG32F:
719      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT32);
720      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT32);
721      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_FLOAT16);
722      RETURN_IF_SUPPORTED(MESA_FORMAT_RGBA_FLOAT16);
723      break;
724
725   case GL_R8UI:
726      RETURN_IF_SUPPORTED(MESA_FORMAT_R_UINT8);
727      break;
728   case GL_RG8UI:
729      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_UINT8);
730      break;
731   case GL_R16UI:
732      RETURN_IF_SUPPORTED(MESA_FORMAT_R_UINT16);
733      break;
734   case GL_RG16UI:
735      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_UINT16);
736      break;
737   case GL_R32UI:
738      RETURN_IF_SUPPORTED(MESA_FORMAT_R_UINT32);
739      break;
740   case GL_RG32UI:
741      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_UINT32);
742      break;
743   case GL_R8I:
744      RETURN_IF_SUPPORTED(MESA_FORMAT_R_SINT8);
745      break;
746   case GL_RG8I:
747      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_SINT8);
748      break;
749   case GL_R16I:
750      RETURN_IF_SUPPORTED(MESA_FORMAT_R_SINT16);
751      break;
752   case GL_RG16I:
753      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_SINT16);
754      break;
755   case GL_R32I:
756      RETURN_IF_SUPPORTED(MESA_FORMAT_R_SINT32);
757      break;
758   case GL_RG32I:
759      RETURN_IF_SUPPORTED(MESA_FORMAT_RG_SINT32);
760      break;
761
762   case GL_RGB10_A2UI:
763      RETURN_IF_SUPPORTED(MESA_FORMAT_B10G10R10A2_UINT);
764      RETURN_IF_SUPPORTED(MESA_FORMAT_R10G10B10A2_UINT);
765      break;
766
767   case GL_BGRA:
768      RETURN_IF_SUPPORTED(MESA_FORMAT_B8G8R8A8_UNORM);
769      break;
770
771   case GL_STENCIL_INDEX:
772   case GL_STENCIL_INDEX8:
773      RETURN_IF_SUPPORTED(MESA_FORMAT_S_UINT8);
774      break;
775
776   default:
777      /* For non-generic compressed format we assert two things:
778       *
779       * 1. The format has already been validated against the set of available
780       *    extensions.
781       *
782       * 2. The driver only enables the extension if it supports all of the
783       *    formats that are part of that extension.
784       */
785      if (_mesa_is_compressed_format(ctx, internalFormat))
786         return _mesa_glenum_to_compressed_format(internalFormat);
787   }
788
789   _mesa_problem(ctx, "unexpected format %s in _mesa_choose_tex_format()",
790                 _mesa_enum_to_string(internalFormat));
791   return MESA_FORMAT_NONE;
792}
793