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