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