glformats.c revision af69d88d
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
31
32/**
33 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
34 */
35GLboolean
36_mesa_type_is_packed(GLenum type)
37{
38   switch (type) {
39   case GL_UNSIGNED_BYTE_3_3_2:
40   case GL_UNSIGNED_BYTE_2_3_3_REV:
41   case MESA_UNSIGNED_BYTE_4_4:
42   case GL_UNSIGNED_SHORT_5_6_5:
43   case GL_UNSIGNED_SHORT_5_6_5_REV:
44   case GL_UNSIGNED_SHORT_4_4_4_4:
45   case GL_UNSIGNED_SHORT_4_4_4_4_REV:
46   case GL_UNSIGNED_SHORT_5_5_5_1:
47   case GL_UNSIGNED_SHORT_1_5_5_5_REV:
48   case GL_UNSIGNED_INT_8_8_8_8:
49   case GL_UNSIGNED_INT_8_8_8_8_REV:
50   case GL_UNSIGNED_INT_10_10_10_2:
51   case GL_UNSIGNED_INT_2_10_10_10_REV:
52   case GL_UNSIGNED_SHORT_8_8_MESA:
53   case GL_UNSIGNED_SHORT_8_8_REV_MESA:
54   case GL_UNSIGNED_INT_24_8_EXT:
55   case GL_UNSIGNED_INT_5_9_9_9_REV:
56   case GL_UNSIGNED_INT_10F_11F_11F_REV:
57   case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
58      return GL_TRUE;
59   }
60
61   return GL_FALSE;
62}
63
64
65/**
66 * Get the size of a GL data type.
67 *
68 * \param type GL data type.
69 *
70 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
71 * if an invalid type enum.
72 */
73GLint
74_mesa_sizeof_type(GLenum type)
75{
76   switch (type) {
77   case GL_BITMAP:
78      return 0;
79   case GL_UNSIGNED_BYTE:
80      return sizeof(GLubyte);
81   case GL_BYTE:
82      return sizeof(GLbyte);
83   case GL_UNSIGNED_SHORT:
84      return sizeof(GLushort);
85   case GL_SHORT:
86      return sizeof(GLshort);
87   case GL_UNSIGNED_INT:
88      return sizeof(GLuint);
89   case GL_INT:
90      return sizeof(GLint);
91   case GL_FLOAT:
92      return sizeof(GLfloat);
93   case GL_DOUBLE:
94      return sizeof(GLdouble);
95   case GL_HALF_FLOAT_ARB:
96      return sizeof(GLhalfARB);
97   case GL_FIXED:
98      return sizeof(GLfixed);
99   default:
100      return -1;
101   }
102}
103
104
105/**
106 * Same as _mesa_sizeof_type() but also accepting the packed pixel
107 * format data types.
108 */
109GLint
110_mesa_sizeof_packed_type(GLenum type)
111{
112   switch (type) {
113   case GL_BITMAP:
114      return 0;
115   case GL_UNSIGNED_BYTE:
116      return sizeof(GLubyte);
117   case GL_BYTE:
118      return sizeof(GLbyte);
119   case GL_UNSIGNED_SHORT:
120      return sizeof(GLushort);
121   case GL_SHORT:
122      return sizeof(GLshort);
123   case GL_UNSIGNED_INT:
124      return sizeof(GLuint);
125   case GL_INT:
126      return sizeof(GLint);
127   case GL_HALF_FLOAT_ARB:
128      return sizeof(GLhalfARB);
129   case GL_FLOAT:
130      return sizeof(GLfloat);
131   case GL_UNSIGNED_BYTE_3_3_2:
132   case GL_UNSIGNED_BYTE_2_3_3_REV:
133   case MESA_UNSIGNED_BYTE_4_4:
134      return sizeof(GLubyte);
135   case GL_UNSIGNED_SHORT_5_6_5:
136   case GL_UNSIGNED_SHORT_5_6_5_REV:
137   case GL_UNSIGNED_SHORT_4_4_4_4:
138   case GL_UNSIGNED_SHORT_4_4_4_4_REV:
139   case GL_UNSIGNED_SHORT_5_5_5_1:
140   case GL_UNSIGNED_SHORT_1_5_5_5_REV:
141   case GL_UNSIGNED_SHORT_8_8_MESA:
142   case GL_UNSIGNED_SHORT_8_8_REV_MESA:
143      return sizeof(GLushort);
144   case GL_UNSIGNED_INT_8_8_8_8:
145   case GL_UNSIGNED_INT_8_8_8_8_REV:
146   case GL_UNSIGNED_INT_10_10_10_2:
147   case GL_UNSIGNED_INT_2_10_10_10_REV:
148   case GL_UNSIGNED_INT_24_8_EXT:
149   case GL_UNSIGNED_INT_5_9_9_9_REV:
150   case GL_UNSIGNED_INT_10F_11F_11F_REV:
151      return sizeof(GLuint);
152   case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
153      return 8;
154   default:
155      return -1;
156   }
157}
158
159
160/**
161 * Get the number of components in a pixel format.
162 *
163 * \param format pixel format.
164 *
165 * \return the number of components in the given format, or -1 if a bad format.
166 */
167GLint
168_mesa_components_in_format(GLenum format)
169{
170   switch (format) {
171   case GL_COLOR_INDEX:
172   case GL_STENCIL_INDEX:
173   case GL_DEPTH_COMPONENT:
174   case GL_RED:
175   case GL_RED_INTEGER_EXT:
176   case GL_GREEN:
177   case GL_GREEN_INTEGER_EXT:
178   case GL_BLUE:
179   case GL_BLUE_INTEGER_EXT:
180   case GL_ALPHA:
181   case GL_ALPHA_INTEGER_EXT:
182   case GL_LUMINANCE:
183   case GL_LUMINANCE_INTEGER_EXT:
184   case GL_INTENSITY:
185      return 1;
186
187   case GL_LUMINANCE_ALPHA:
188   case GL_LUMINANCE_ALPHA_INTEGER_EXT:
189   case GL_RG:
190   case GL_YCBCR_MESA:
191   case GL_DEPTH_STENCIL_EXT:
192   case GL_RG_INTEGER:
193      return 2;
194
195   case GL_RGB:
196   case GL_BGR:
197   case GL_RGB_INTEGER_EXT:
198   case GL_BGR_INTEGER_EXT:
199      return 3;
200
201   case GL_RGBA:
202   case GL_BGRA:
203   case GL_ABGR_EXT:
204   case GL_RGBA_INTEGER_EXT:
205   case GL_BGRA_INTEGER_EXT:
206      return 4;
207
208   default:
209      return -1;
210   }
211}
212
213
214/**
215 * Get the bytes per pixel of pixel format type pair.
216 *
217 * \param format pixel format.
218 * \param type pixel type.
219 *
220 * \return bytes per pixel, or -1 if a bad format or type was given.
221 */
222GLint
223_mesa_bytes_per_pixel(GLenum format, GLenum type)
224{
225   GLint comps = _mesa_components_in_format(format);
226   if (comps < 0)
227      return -1;
228
229   switch (type) {
230   case GL_BITMAP:
231      return 0;  /* special case */
232   case GL_BYTE:
233   case GL_UNSIGNED_BYTE:
234      return comps * sizeof(GLubyte);
235   case GL_SHORT:
236   case GL_UNSIGNED_SHORT:
237      return comps * sizeof(GLshort);
238   case GL_INT:
239   case GL_UNSIGNED_INT:
240      return comps * sizeof(GLint);
241   case GL_FLOAT:
242      return comps * sizeof(GLfloat);
243   case GL_HALF_FLOAT_ARB:
244      return comps * sizeof(GLhalfARB);
245   case GL_UNSIGNED_BYTE_3_3_2:
246   case GL_UNSIGNED_BYTE_2_3_3_REV:
247      if (format == GL_RGB || format == GL_BGR ||
248          format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT)
249         return sizeof(GLubyte);
250      else
251         return -1;  /* error */
252   case GL_UNSIGNED_SHORT_5_6_5:
253   case GL_UNSIGNED_SHORT_5_6_5_REV:
254      if (format == GL_RGB || format == GL_BGR ||
255          format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT)
256         return sizeof(GLushort);
257      else
258         return -1;  /* error */
259   case GL_UNSIGNED_SHORT_4_4_4_4:
260   case GL_UNSIGNED_SHORT_4_4_4_4_REV:
261   case GL_UNSIGNED_SHORT_5_5_5_1:
262   case GL_UNSIGNED_SHORT_1_5_5_5_REV:
263      if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT ||
264          format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT)
265         return sizeof(GLushort);
266      else
267         return -1;
268   case GL_UNSIGNED_INT_8_8_8_8:
269   case GL_UNSIGNED_INT_8_8_8_8_REV:
270   case GL_UNSIGNED_INT_10_10_10_2:
271   case GL_UNSIGNED_INT_2_10_10_10_REV:
272      if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT ||
273          format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT ||
274          format == GL_RGB)
275         return sizeof(GLuint);
276      else
277         return -1;
278   case GL_UNSIGNED_SHORT_8_8_MESA:
279   case GL_UNSIGNED_SHORT_8_8_REV_MESA:
280      if (format == GL_YCBCR_MESA)
281         return sizeof(GLushort);
282      else
283         return -1;
284   case GL_UNSIGNED_INT_24_8_EXT:
285      if (format == GL_DEPTH_STENCIL_EXT)
286         return sizeof(GLuint);
287      else
288         return -1;
289   case GL_UNSIGNED_INT_5_9_9_9_REV:
290      if (format == GL_RGB)
291         return sizeof(GLuint);
292      else
293         return -1;
294   case GL_UNSIGNED_INT_10F_11F_11F_REV:
295      if (format == GL_RGB)
296         return sizeof(GLuint);
297      else
298         return -1;
299   case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
300      if (format == GL_DEPTH_STENCIL)
301         return 8;
302      else
303         return -1;
304   default:
305      return -1;
306   }
307}
308
309
310/**
311 * Get the number of bytes for a vertex attrib with the given number of
312 * components and type.
313 *
314 * \param comps number of components.
315 * \param type data type.
316 *
317 * \return bytes per attribute, or -1 if a bad comps/type combination was given.
318 */
319GLint
320_mesa_bytes_per_vertex_attrib(GLint comps, GLenum type)
321{
322   switch (type) {
323   case GL_BYTE:
324   case GL_UNSIGNED_BYTE:
325      return comps * sizeof(GLubyte);
326   case GL_SHORT:
327   case GL_UNSIGNED_SHORT:
328      return comps * sizeof(GLshort);
329   case GL_INT:
330   case GL_UNSIGNED_INT:
331      return comps * sizeof(GLint);
332   case GL_FLOAT:
333      return comps * sizeof(GLfloat);
334   case GL_HALF_FLOAT_ARB:
335      return comps * sizeof(GLhalfARB);
336   case GL_DOUBLE:
337      return comps * sizeof(GLdouble);
338   case GL_FIXED:
339      return comps * sizeof(GLfixed);
340   case GL_INT_2_10_10_10_REV:
341   case GL_UNSIGNED_INT_2_10_10_10_REV:
342      if (comps == 4)
343         return sizeof(GLuint);
344      else
345         return -1;
346   case GL_UNSIGNED_INT_10F_11F_11F_REV:
347      if (comps == 3)
348         return sizeof(GLuint);
349      else
350         return -1;
351   default:
352      return -1;
353   }
354}
355
356/**
357 * Test if the given format is unsized.
358 */
359GLboolean
360_mesa_is_enum_format_unsized(GLenum format)
361{
362   switch (format) {
363   case GL_RGBA:
364   case GL_BGRA:
365   case GL_ABGR_EXT:
366   case GL_RGB:
367   case GL_BGR:
368   case GL_RG:
369   case GL_RED:
370   case GL_GREEN:
371   case GL_BLUE:
372   case GL_ALPHA:
373   case GL_INTENSITY:
374   case GL_LUMINANCE:
375   case GL_LUMINANCE_ALPHA:
376
377   case GL_SRGB:
378   case GL_SRGB_ALPHA:
379   case GL_SLUMINANCE:
380   case GL_SLUMINANCE_ALPHA:
381
382   case GL_RGBA_SNORM:
383   case GL_RGB_SNORM:
384   case GL_RG_SNORM:
385   case GL_RED_SNORM:
386   case GL_ALPHA_SNORM:
387   case GL_INTENSITY_SNORM:
388   case GL_LUMINANCE_SNORM:
389   case GL_LUMINANCE_ALPHA_SNORM:
390
391   case GL_RED_INTEGER:
392   case GL_GREEN_INTEGER:
393   case GL_BLUE_INTEGER:
394   case GL_ALPHA_INTEGER:
395   case GL_RGB_INTEGER:
396   case GL_RGBA_INTEGER:
397   case GL_BGR_INTEGER:
398   case GL_BGRA_INTEGER:
399   case GL_RG_INTEGER:
400   case GL_LUMINANCE_INTEGER_EXT:
401   case GL_LUMINANCE_ALPHA_INTEGER_EXT:
402
403   case GL_DEPTH_COMPONENT:
404   case GL_DEPTH_STENCIL:
405   case GL_STENCIL_INDEX:
406      return GL_TRUE;
407   default:
408      return GL_FALSE;
409   }
410}
411
412/**
413 * Test if the given format is a UNORM (unsigned-normalized) format.
414 */
415GLboolean
416_mesa_is_enum_format_unorm(GLenum format)
417{
418      switch(format) {
419      case GL_RED:
420      case GL_GREEN:
421      case GL_BLUE:
422      case GL_ALPHA:
423      case GL_ALPHA4:
424      case GL_ALPHA8:
425      case GL_ALPHA12:
426      case GL_ALPHA16:
427      case 1:
428      case GL_LUMINANCE:
429      case GL_SLUMINANCE:
430      case GL_LUMINANCE4:
431      case GL_LUMINANCE8:
432      case GL_LUMINANCE12:
433      case GL_LUMINANCE16:
434      case 2:
435      case GL_LUMINANCE_ALPHA:
436      case GL_SLUMINANCE_ALPHA:
437      case GL_LUMINANCE4_ALPHA4:
438      case GL_LUMINANCE6_ALPHA2:
439      case GL_LUMINANCE8_ALPHA8:
440      case GL_LUMINANCE12_ALPHA4:
441      case GL_LUMINANCE12_ALPHA12:
442      case GL_LUMINANCE16_ALPHA16:
443      case GL_INTENSITY:
444      case GL_INTENSITY4:
445      case GL_INTENSITY8:
446      case GL_INTENSITY12:
447      case GL_INTENSITY16:
448      case GL_R8:
449      case GL_R16:
450      case GL_RG:
451      case GL_RG8:
452      case GL_RG16:
453      case 3:
454      case GL_RGB:
455      case GL_BGR:
456      case GL_SRGB:
457      case GL_R3_G3_B2:
458      case GL_RGB4:
459      case GL_RGB5:
460      case GL_RGB565:
461      case GL_RGB8:
462      case GL_RGB10:
463      case GL_RGB12:
464      case GL_RGB16:
465      case 4:
466      case GL_ABGR_EXT:
467      case GL_RGBA:
468      case GL_BGRA:
469      case GL_SRGB_ALPHA:
470      case GL_RGBA2:
471      case GL_RGBA4:
472      case GL_RGB5_A1:
473      case GL_RGBA8:
474      case GL_RGB10_A2:
475      case GL_RGBA12:
476      case GL_RGBA16:
477         return GL_TRUE;
478      default:
479         return GL_FALSE;
480   }
481}
482
483/**
484 * Test if the given format is a SNORM (signed-normalized) format.
485 */
486GLboolean
487_mesa_is_enum_format_snorm(GLenum format)
488{
489   switch (format) {
490   /* signed, normalized texture formats */
491   case GL_RED_SNORM:
492   case GL_R8_SNORM:
493   case GL_R16_SNORM:
494   case GL_RG_SNORM:
495   case GL_RG8_SNORM:
496   case GL_RG16_SNORM:
497   case GL_RGB_SNORM:
498   case GL_RGB8_SNORM:
499   case GL_RGB16_SNORM:
500   case GL_RGBA_SNORM:
501   case GL_RGBA8_SNORM:
502   case GL_RGBA16_SNORM:
503   case GL_ALPHA_SNORM:
504   case GL_ALPHA8_SNORM:
505   case GL_ALPHA16_SNORM:
506   case GL_LUMINANCE_SNORM:
507   case GL_LUMINANCE8_SNORM:
508   case GL_LUMINANCE16_SNORM:
509   case GL_LUMINANCE_ALPHA_SNORM:
510   case GL_LUMINANCE8_ALPHA8_SNORM:
511   case GL_LUMINANCE16_ALPHA16_SNORM:
512   case GL_INTENSITY_SNORM:
513   case GL_INTENSITY8_SNORM:
514   case GL_INTENSITY16_SNORM:
515      return GL_TRUE;
516   default:
517      return GL_FALSE;
518   }
519}
520
521/**
522 * Test if the given format is an integer (non-normalized) format.
523 */
524GLboolean
525_mesa_is_enum_format_unsigned_int(GLenum format)
526{
527   switch (format) {
528   /* specific integer formats */
529   case GL_RGBA32UI_EXT:
530   case GL_RGB32UI_EXT:
531   case GL_RG32UI:
532   case GL_R32UI:
533   case GL_ALPHA32UI_EXT:
534   case GL_INTENSITY32UI_EXT:
535   case GL_LUMINANCE32UI_EXT:
536   case GL_LUMINANCE_ALPHA32UI_EXT:
537   case GL_RGBA16UI_EXT:
538   case GL_RGB16UI_EXT:
539   case GL_RG16UI:
540   case GL_R16UI:
541   case GL_ALPHA16UI_EXT:
542   case GL_INTENSITY16UI_EXT:
543   case GL_LUMINANCE16UI_EXT:
544   case GL_LUMINANCE_ALPHA16UI_EXT:
545   case GL_RGBA8UI_EXT:
546   case GL_RGB8UI_EXT:
547   case GL_RG8UI:
548   case GL_R8UI:
549   case GL_ALPHA8UI_EXT:
550   case GL_INTENSITY8UI_EXT:
551   case GL_LUMINANCE8UI_EXT:
552   case GL_LUMINANCE_ALPHA8UI_EXT:
553   case GL_RGB10_A2UI:
554      return GL_TRUE;
555   default:
556      return GL_FALSE;
557   }
558}
559
560
561/**
562 * Test if the given format is an integer (non-normalized) format.
563 */
564GLboolean
565_mesa_is_enum_format_signed_int(GLenum format)
566{
567   switch (format) {
568   /* generic integer formats */
569   case GL_RED_INTEGER_EXT:
570   case GL_GREEN_INTEGER_EXT:
571   case GL_BLUE_INTEGER_EXT:
572   case GL_ALPHA_INTEGER_EXT:
573   case GL_RGB_INTEGER_EXT:
574   case GL_RGBA_INTEGER_EXT:
575   case GL_BGR_INTEGER_EXT:
576   case GL_BGRA_INTEGER_EXT:
577   case GL_LUMINANCE_INTEGER_EXT:
578   case GL_LUMINANCE_ALPHA_INTEGER_EXT:
579   case GL_RG_INTEGER:
580   /* specific integer formats */
581   case GL_RGBA32I_EXT:
582   case GL_RGB32I_EXT:
583   case GL_RG32I:
584   case GL_R32I:
585   case GL_ALPHA32I_EXT:
586   case GL_INTENSITY32I_EXT:
587   case GL_LUMINANCE32I_EXT:
588   case GL_LUMINANCE_ALPHA32I_EXT:
589   case GL_RGBA16I_EXT:
590   case GL_RGB16I_EXT:
591   case GL_RG16I:
592   case GL_R16I:
593   case GL_ALPHA16I_EXT:
594   case GL_INTENSITY16I_EXT:
595   case GL_LUMINANCE16I_EXT:
596   case GL_LUMINANCE_ALPHA16I_EXT:
597   case GL_RGBA8I_EXT:
598   case GL_RGB8I_EXT:
599   case GL_RG8I:
600   case GL_R8I:
601   case GL_ALPHA8I_EXT:
602   case GL_INTENSITY8I_EXT:
603   case GL_LUMINANCE8I_EXT:
604   case GL_LUMINANCE_ALPHA8I_EXT:
605      return GL_TRUE;
606   default:
607      return GL_FALSE;
608   }
609}
610
611
612/**
613 * Test if the given format is an integer (non-normalized) format.
614 */
615GLboolean
616_mesa_is_enum_format_integer(GLenum format)
617{
618   return _mesa_is_enum_format_unsigned_int(format) ||
619          _mesa_is_enum_format_signed_int(format);
620}
621
622
623GLboolean
624_mesa_is_type_unsigned(GLenum type)
625{
626   switch (type) {
627   case GL_UNSIGNED_INT:
628   case GL_UNSIGNED_INT_8_8_8_8:
629   case GL_UNSIGNED_INT_8_8_8_8_REV:
630   case GL_UNSIGNED_INT_10_10_10_2:
631   case GL_UNSIGNED_INT_2_10_10_10_REV:
632
633   case GL_UNSIGNED_SHORT:
634   case GL_UNSIGNED_SHORT_4_4_4_4:
635   case GL_UNSIGNED_SHORT_5_5_5_1:
636   case GL_UNSIGNED_SHORT_5_6_5:
637   case GL_UNSIGNED_SHORT_5_6_5_REV:
638   case GL_UNSIGNED_SHORT_4_4_4_4_REV:
639   case GL_UNSIGNED_SHORT_1_5_5_5_REV:
640   case GL_UNSIGNED_SHORT_8_8_MESA:
641   case GL_UNSIGNED_SHORT_8_8_REV_MESA:
642
643   case GL_UNSIGNED_BYTE:
644   case GL_UNSIGNED_BYTE_3_3_2:
645   case GL_UNSIGNED_BYTE_2_3_3_REV:
646      return GL_TRUE;
647
648   default:
649      return GL_FALSE;
650   }
651}
652
653
654/**
655 * Test if the given image format is a color/RGBA format (i.e., not color
656 * index, depth, stencil, etc).
657 * \param format  the image format value (may by an internal texture format)
658 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
659 */
660GLboolean
661_mesa_is_color_format(GLenum format)
662{
663   switch (format) {
664      case GL_RED:
665      case GL_GREEN:
666      case GL_BLUE:
667      case GL_ALPHA:
668      case GL_ALPHA4:
669      case GL_ALPHA8:
670      case GL_ALPHA12:
671      case GL_ALPHA16:
672      case 1:
673      case GL_LUMINANCE:
674      case GL_LUMINANCE4:
675      case GL_LUMINANCE8:
676      case GL_LUMINANCE12:
677      case GL_LUMINANCE16:
678      case 2:
679      case GL_LUMINANCE_ALPHA:
680      case GL_LUMINANCE4_ALPHA4:
681      case GL_LUMINANCE6_ALPHA2:
682      case GL_LUMINANCE8_ALPHA8:
683      case GL_LUMINANCE12_ALPHA4:
684      case GL_LUMINANCE12_ALPHA12:
685      case GL_LUMINANCE16_ALPHA16:
686      case GL_INTENSITY:
687      case GL_INTENSITY4:
688      case GL_INTENSITY8:
689      case GL_INTENSITY12:
690      case GL_INTENSITY16:
691      case GL_R8:
692      case GL_R16:
693      case GL_RG:
694      case GL_RG8:
695      case GL_RG16:
696      case 3:
697      case GL_RGB:
698      case GL_BGR:
699      case GL_R3_G3_B2:
700      case GL_RGB4:
701      case GL_RGB5:
702      case GL_RGB565:
703      case GL_RGB8:
704      case GL_RGB10:
705      case GL_RGB12:
706      case GL_RGB16:
707      case 4:
708      case GL_ABGR_EXT:
709      case GL_RGBA:
710      case GL_BGRA:
711      case GL_RGBA2:
712      case GL_RGBA4:
713      case GL_RGB5_A1:
714      case GL_RGBA8:
715      case GL_RGB10_A2:
716      case GL_RGBA12:
717      case GL_RGBA16:
718      /* float texture formats */
719      case GL_ALPHA16F_ARB:
720      case GL_ALPHA32F_ARB:
721      case GL_LUMINANCE16F_ARB:
722      case GL_LUMINANCE32F_ARB:
723      case GL_LUMINANCE_ALPHA16F_ARB:
724      case GL_LUMINANCE_ALPHA32F_ARB:
725      case GL_INTENSITY16F_ARB:
726      case GL_INTENSITY32F_ARB:
727      case GL_R16F:
728      case GL_R32F:
729      case GL_RG16F:
730      case GL_RG32F:
731      case GL_RGB16F_ARB:
732      case GL_RGB32F_ARB:
733      case GL_RGBA16F_ARB:
734      case GL_RGBA32F_ARB:
735      /* compressed formats */
736      case GL_COMPRESSED_ALPHA:
737      case GL_COMPRESSED_LUMINANCE:
738      case GL_COMPRESSED_LUMINANCE_ALPHA:
739      case GL_COMPRESSED_INTENSITY:
740      case GL_COMPRESSED_RED:
741      case GL_COMPRESSED_RG:
742      case GL_COMPRESSED_RGB:
743      case GL_COMPRESSED_RGBA:
744      case GL_RGB_S3TC:
745      case GL_RGB4_S3TC:
746      case GL_RGBA_S3TC:
747      case GL_RGBA4_S3TC:
748      case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
749      case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
750      case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
751      case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
752      case GL_COMPRESSED_RGB_FXT1_3DFX:
753      case GL_COMPRESSED_RGBA_FXT1_3DFX:
754      case GL_SRGB_EXT:
755      case GL_SRGB8_EXT:
756      case GL_SRGB_ALPHA_EXT:
757      case GL_SRGB8_ALPHA8_EXT:
758      case GL_SLUMINANCE_ALPHA_EXT:
759      case GL_SLUMINANCE8_ALPHA8_EXT:
760      case GL_SLUMINANCE_EXT:
761      case GL_SLUMINANCE8_EXT:
762      case GL_COMPRESSED_SRGB_EXT:
763      case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
764      case GL_COMPRESSED_SRGB_ALPHA_EXT:
765      case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
766      case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
767      case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
768      case GL_COMPRESSED_SLUMINANCE_EXT:
769      case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
770      case GL_COMPRESSED_RED_RGTC1:
771      case GL_COMPRESSED_SIGNED_RED_RGTC1:
772      case GL_COMPRESSED_RG_RGTC2:
773      case GL_COMPRESSED_SIGNED_RG_RGTC2:
774      case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
775      case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
776      case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
777      case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
778      case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
779      case GL_ETC1_RGB8_OES:
780      case GL_COMPRESSED_RGB8_ETC2:
781      case GL_COMPRESSED_SRGB8_ETC2:
782      case GL_COMPRESSED_RGBA8_ETC2_EAC:
783      case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
784      case GL_COMPRESSED_R11_EAC:
785      case GL_COMPRESSED_RG11_EAC:
786      case GL_COMPRESSED_SIGNED_R11_EAC:
787      case GL_COMPRESSED_SIGNED_RG11_EAC:
788      case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
789      case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
790      case GL_COMPRESSED_RGBA_BPTC_UNORM:
791      case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
792      case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT:
793      case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
794      /* generic integer formats */
795      case GL_RED_INTEGER_EXT:
796      case GL_GREEN_INTEGER_EXT:
797      case GL_BLUE_INTEGER_EXT:
798      case GL_ALPHA_INTEGER_EXT:
799      case GL_RGB_INTEGER_EXT:
800      case GL_RGBA_INTEGER_EXT:
801      case GL_BGR_INTEGER_EXT:
802      case GL_BGRA_INTEGER_EXT:
803      case GL_RG_INTEGER:
804      case GL_LUMINANCE_INTEGER_EXT:
805      case GL_LUMINANCE_ALPHA_INTEGER_EXT:
806      /* sized integer formats */
807      case GL_RGBA32UI_EXT:
808      case GL_RGB32UI_EXT:
809      case GL_RG32UI:
810      case GL_R32UI:
811      case GL_ALPHA32UI_EXT:
812      case GL_INTENSITY32UI_EXT:
813      case GL_LUMINANCE32UI_EXT:
814      case GL_LUMINANCE_ALPHA32UI_EXT:
815      case GL_RGBA16UI_EXT:
816      case GL_RGB16UI_EXT:
817      case GL_RG16UI:
818      case GL_R16UI:
819      case GL_ALPHA16UI_EXT:
820      case GL_INTENSITY16UI_EXT:
821      case GL_LUMINANCE16UI_EXT:
822      case GL_LUMINANCE_ALPHA16UI_EXT:
823      case GL_RGBA8UI_EXT:
824      case GL_RGB8UI_EXT:
825      case GL_RG8UI:
826      case GL_R8UI:
827      case GL_ALPHA8UI_EXT:
828      case GL_INTENSITY8UI_EXT:
829      case GL_LUMINANCE8UI_EXT:
830      case GL_LUMINANCE_ALPHA8UI_EXT:
831      case GL_RGBA32I_EXT:
832      case GL_RGB32I_EXT:
833      case GL_RG32I:
834      case GL_R32I:
835      case GL_ALPHA32I_EXT:
836      case GL_INTENSITY32I_EXT:
837      case GL_LUMINANCE32I_EXT:
838      case GL_LUMINANCE_ALPHA32I_EXT:
839      case GL_RGBA16I_EXT:
840      case GL_RGB16I_EXT:
841      case GL_RG16I:
842      case GL_R16I:
843      case GL_ALPHA16I_EXT:
844      case GL_INTENSITY16I_EXT:
845      case GL_LUMINANCE16I_EXT:
846      case GL_LUMINANCE_ALPHA16I_EXT:
847      case GL_RGBA8I_EXT:
848      case GL_RGB8I_EXT:
849      case GL_RG8I:
850      case GL_R8I:
851      case GL_ALPHA8I_EXT:
852      case GL_INTENSITY8I_EXT:
853      case GL_LUMINANCE8I_EXT:
854      case GL_LUMINANCE_ALPHA8I_EXT:
855      /* signed, normalized texture formats */
856      case GL_RED_SNORM:
857      case GL_R8_SNORM:
858      case GL_R16_SNORM:
859      case GL_RG_SNORM:
860      case GL_RG8_SNORM:
861      case GL_RG16_SNORM:
862      case GL_RGB_SNORM:
863      case GL_RGB8_SNORM:
864      case GL_RGB16_SNORM:
865      case GL_RGBA_SNORM:
866      case GL_RGBA8_SNORM:
867      case GL_RGBA16_SNORM:
868      case GL_ALPHA_SNORM:
869      case GL_ALPHA8_SNORM:
870      case GL_ALPHA16_SNORM:
871      case GL_LUMINANCE_SNORM:
872      case GL_LUMINANCE8_SNORM:
873      case GL_LUMINANCE16_SNORM:
874      case GL_LUMINANCE_ALPHA_SNORM:
875      case GL_LUMINANCE8_ALPHA8_SNORM:
876      case GL_LUMINANCE16_ALPHA16_SNORM:
877      case GL_INTENSITY_SNORM:
878      case GL_INTENSITY8_SNORM:
879      case GL_INTENSITY16_SNORM:
880      case GL_RGB9_E5:
881      case GL_R11F_G11F_B10F:
882      case GL_RGB10_A2UI:
883         return GL_TRUE;
884      case GL_YCBCR_MESA:  /* not considered to be RGB */
885         /* fall-through */
886      default:
887         return GL_FALSE;
888   }
889}
890
891
892/**
893 * Test if the given image format is a depth component format.
894 */
895GLboolean
896_mesa_is_depth_format(GLenum format)
897{
898   switch (format) {
899      case GL_DEPTH_COMPONENT:
900      case GL_DEPTH_COMPONENT16:
901      case GL_DEPTH_COMPONENT24:
902      case GL_DEPTH_COMPONENT32:
903      case GL_DEPTH_COMPONENT32F:
904         return GL_TRUE;
905      default:
906         return GL_FALSE;
907   }
908}
909
910
911/**
912 * Test if the given image format is a stencil format.
913 */
914GLboolean
915_mesa_is_stencil_format(GLenum format)
916{
917   switch (format) {
918      case GL_STENCIL_INDEX:
919         return GL_TRUE;
920      default:
921         return GL_FALSE;
922   }
923}
924
925
926/**
927 * Test if the given image format is a YCbCr format.
928 */
929GLboolean
930_mesa_is_ycbcr_format(GLenum format)
931{
932   switch (format) {
933      case GL_YCBCR_MESA:
934         return GL_TRUE;
935      default:
936         return GL_FALSE;
937   }
938}
939
940
941/**
942 * Test if the given image format is a depth+stencil format.
943 */
944GLboolean
945_mesa_is_depthstencil_format(GLenum format)
946{
947   switch (format) {
948      case GL_DEPTH24_STENCIL8_EXT:
949      case GL_DEPTH_STENCIL_EXT:
950      case GL_DEPTH32F_STENCIL8:
951         return GL_TRUE;
952      default:
953         return GL_FALSE;
954   }
955}
956
957
958/**
959 * Test if the given image format is a depth or stencil format.
960 */
961GLboolean
962_mesa_is_depth_or_stencil_format(GLenum format)
963{
964   switch (format) {
965      case GL_DEPTH_COMPONENT:
966      case GL_DEPTH_COMPONENT16:
967      case GL_DEPTH_COMPONENT24:
968      case GL_DEPTH_COMPONENT32:
969      case GL_STENCIL_INDEX:
970      case GL_STENCIL_INDEX1_EXT:
971      case GL_STENCIL_INDEX4_EXT:
972      case GL_STENCIL_INDEX8_EXT:
973      case GL_STENCIL_INDEX16_EXT:
974      case GL_DEPTH_STENCIL_EXT:
975      case GL_DEPTH24_STENCIL8_EXT:
976      case GL_DEPTH_COMPONENT32F:
977      case GL_DEPTH32F_STENCIL8:
978         return GL_TRUE;
979      default:
980         return GL_FALSE;
981   }
982}
983
984
985/**
986 * Test if an image format is a supported compressed format.
987 * \param format the internal format token provided by the user.
988 * \return GL_TRUE if compressed, GL_FALSE if uncompressed
989 */
990GLboolean
991_mesa_is_compressed_format(struct gl_context *ctx, GLenum format)
992{
993   switch (format) {
994   case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
995   case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
996   case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
997   case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
998      /* Assume that the ANGLE flag will always be set if the EXT flag is set.
999       */
1000      return ctx->Extensions.ANGLE_texture_compression_dxt;
1001   case GL_RGB_S3TC:
1002   case GL_RGB4_S3TC:
1003   case GL_RGBA_S3TC:
1004   case GL_RGBA4_S3TC:
1005      return _mesa_is_desktop_gl(ctx) &&
1006         ctx->Extensions.ANGLE_texture_compression_dxt;
1007   case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
1008   case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
1009   case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
1010   case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
1011      return _mesa_is_desktop_gl(ctx)
1012         && ctx->Extensions.EXT_texture_sRGB
1013         && ctx->Extensions.EXT_texture_compression_s3tc;
1014   case GL_COMPRESSED_RGB_FXT1_3DFX:
1015   case GL_COMPRESSED_RGBA_FXT1_3DFX:
1016      return _mesa_is_desktop_gl(ctx)
1017         && ctx->Extensions.TDFX_texture_compression_FXT1;
1018   case GL_COMPRESSED_RED_RGTC1:
1019   case GL_COMPRESSED_SIGNED_RED_RGTC1:
1020   case GL_COMPRESSED_RG_RGTC2:
1021   case GL_COMPRESSED_SIGNED_RG_RGTC2:
1022      return _mesa_is_desktop_gl(ctx)
1023         && ctx->Extensions.ARB_texture_compression_rgtc;
1024   case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
1025   case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
1026   case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
1027   case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
1028      return ctx->API == API_OPENGL_COMPAT
1029         && ctx->Extensions.EXT_texture_compression_latc;
1030   case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
1031      return ctx->API == API_OPENGL_COMPAT
1032         && ctx->Extensions.ATI_texture_compression_3dc;
1033   case GL_ETC1_RGB8_OES:
1034      return _mesa_is_gles(ctx)
1035         && ctx->Extensions.OES_compressed_ETC1_RGB8_texture;
1036   case GL_COMPRESSED_RGB8_ETC2:
1037   case GL_COMPRESSED_SRGB8_ETC2:
1038   case GL_COMPRESSED_RGBA8_ETC2_EAC:
1039   case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
1040   case GL_COMPRESSED_R11_EAC:
1041   case GL_COMPRESSED_RG11_EAC:
1042   case GL_COMPRESSED_SIGNED_R11_EAC:
1043   case GL_COMPRESSED_SIGNED_RG11_EAC:
1044   case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
1045   case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
1046      return _mesa_is_gles3(ctx) || ctx->Extensions.ARB_ES3_compatibility;
1047   case GL_COMPRESSED_RGBA_BPTC_UNORM:
1048   case GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM:
1049   case GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT:
1050   case GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT:
1051      return _mesa_is_desktop_gl(ctx) &&
1052         ctx->Extensions.ARB_texture_compression_bptc;
1053   case GL_PALETTE4_RGB8_OES:
1054   case GL_PALETTE4_RGBA8_OES:
1055   case GL_PALETTE4_R5_G6_B5_OES:
1056   case GL_PALETTE4_RGBA4_OES:
1057   case GL_PALETTE4_RGB5_A1_OES:
1058   case GL_PALETTE8_RGB8_OES:
1059   case GL_PALETTE8_RGBA8_OES:
1060   case GL_PALETTE8_R5_G6_B5_OES:
1061   case GL_PALETTE8_RGBA4_OES:
1062   case GL_PALETTE8_RGB5_A1_OES:
1063      return ctx->API == API_OPENGLES;
1064   default:
1065      return GL_FALSE;
1066   }
1067}
1068
1069
1070/**
1071 * Convert various base formats to the cooresponding integer format.
1072 */
1073GLenum
1074_mesa_base_format_to_integer_format(GLenum format)
1075{
1076   switch(format) {
1077   case GL_RED:
1078      return GL_RED_INTEGER;
1079   case GL_GREEN:
1080      return GL_GREEN_INTEGER;
1081   case GL_BLUE:
1082      return GL_BLUE_INTEGER;
1083   case GL_RG:
1084      return GL_RG_INTEGER;
1085   case GL_RGB:
1086      return GL_RGB_INTEGER;
1087   case GL_RGBA:
1088      return GL_RGBA_INTEGER;
1089   case GL_BGR:
1090      return GL_BGR_INTEGER;
1091   case GL_BGRA:
1092      return GL_BGRA_INTEGER;
1093   case GL_ALPHA:
1094      return GL_ALPHA_INTEGER;
1095   case GL_LUMINANCE:
1096      return GL_LUMINANCE_INTEGER_EXT;
1097   case GL_LUMINANCE_ALPHA:
1098      return GL_LUMINANCE_ALPHA_INTEGER_EXT;
1099   }
1100
1101   return format;
1102}
1103
1104
1105/**
1106 * Does the given base texture/renderbuffer format have the channel
1107 * named by 'pname'?
1108 */
1109GLboolean
1110_mesa_base_format_has_channel(GLenum base_format, GLenum pname)
1111{
1112   switch (pname) {
1113   case GL_TEXTURE_RED_SIZE:
1114   case GL_TEXTURE_RED_TYPE:
1115   case GL_RENDERBUFFER_RED_SIZE_EXT:
1116   case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1117      if (base_format == GL_RED ||
1118	  base_format == GL_RG ||
1119	  base_format == GL_RGB ||
1120	  base_format == GL_RGBA) {
1121	 return GL_TRUE;
1122      }
1123      return GL_FALSE;
1124   case GL_TEXTURE_GREEN_SIZE:
1125   case GL_TEXTURE_GREEN_TYPE:
1126   case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1127   case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1128      if (base_format == GL_RG ||
1129	  base_format == GL_RGB ||
1130	  base_format == GL_RGBA) {
1131	 return GL_TRUE;
1132      }
1133      return GL_FALSE;
1134   case GL_TEXTURE_BLUE_SIZE:
1135   case GL_TEXTURE_BLUE_TYPE:
1136   case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1137   case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1138      if (base_format == GL_RGB ||
1139	  base_format == GL_RGBA) {
1140	 return GL_TRUE;
1141      }
1142      return GL_FALSE;
1143   case GL_TEXTURE_ALPHA_SIZE:
1144   case GL_TEXTURE_ALPHA_TYPE:
1145   case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1146   case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1147      if (base_format == GL_RGBA ||
1148	  base_format == GL_ALPHA ||
1149	  base_format == GL_LUMINANCE_ALPHA) {
1150	 return GL_TRUE;
1151      }
1152      return GL_FALSE;
1153   case GL_TEXTURE_LUMINANCE_SIZE:
1154   case GL_TEXTURE_LUMINANCE_TYPE:
1155      if (base_format == GL_LUMINANCE ||
1156	  base_format == GL_LUMINANCE_ALPHA) {
1157	 return GL_TRUE;
1158      }
1159      return GL_FALSE;
1160   case GL_TEXTURE_INTENSITY_SIZE:
1161   case GL_TEXTURE_INTENSITY_TYPE:
1162      if (base_format == GL_INTENSITY) {
1163	 return GL_TRUE;
1164      }
1165      return GL_FALSE;
1166   case GL_TEXTURE_DEPTH_SIZE:
1167   case GL_TEXTURE_DEPTH_TYPE:
1168   case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1169   case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1170      if (base_format == GL_DEPTH_STENCIL ||
1171	  base_format == GL_DEPTH_COMPONENT) {
1172	 return GL_TRUE;
1173      }
1174      return GL_FALSE;
1175   case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1176   case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1177      if (base_format == GL_DEPTH_STENCIL ||
1178	  base_format == GL_STENCIL_INDEX) {
1179	 return GL_TRUE;
1180      }
1181      return GL_FALSE;
1182   default:
1183      _mesa_warning(NULL, "%s: Unexpected channel token 0x%x\n",
1184		    __FUNCTION__, pname);
1185      return GL_FALSE;
1186   }
1187
1188   return GL_FALSE;
1189}
1190
1191
1192/**
1193 * Returns the number of channels/components for a base format.
1194 */
1195GLint
1196_mesa_base_format_component_count(GLenum base_format)
1197{
1198   switch (base_format) {
1199   case GL_RED:
1200   case GL_ALPHA:
1201   case GL_INTENSITY:
1202   case GL_DEPTH_COMPONENT:
1203      return 1;
1204   case GL_RG:
1205   case GL_LUMINANCE_ALPHA:
1206   case GL_DEPTH_STENCIL:
1207      return 2;
1208   case GL_RGB:
1209      return 3;
1210   case GL_RGBA:
1211      return 4;
1212   default:
1213      return -1;
1214   }
1215}
1216
1217
1218/**
1219 * If format is a generic compressed format, return the corresponding
1220 * non-compressed format.  For other formats, return the format as-is.
1221 */
1222GLenum
1223_mesa_generic_compressed_format_to_uncompressed_format(GLenum format)
1224{
1225   switch (format) {
1226   case GL_COMPRESSED_RED:
1227      return GL_RED;
1228   case GL_COMPRESSED_RG:
1229      return GL_RG;
1230   case GL_COMPRESSED_RGB:
1231      return GL_RGB;
1232   case GL_COMPRESSED_RGBA:
1233      return GL_RGBA;
1234   case GL_COMPRESSED_ALPHA:
1235      return GL_ALPHA;
1236   case GL_COMPRESSED_LUMINANCE:
1237      return GL_LUMINANCE;
1238   case GL_COMPRESSED_LUMINANCE_ALPHA:
1239      return GL_LUMINANCE_ALPHA;
1240   case GL_COMPRESSED_INTENSITY:
1241      return GL_INTENSITY;
1242   /* sRGB formats */
1243   case GL_COMPRESSED_SRGB:
1244      return GL_SRGB;
1245   case GL_COMPRESSED_SRGB_ALPHA:
1246      return GL_SRGB_ALPHA;
1247   case GL_COMPRESSED_SLUMINANCE:
1248      return GL_SLUMINANCE;
1249   case GL_COMPRESSED_SLUMINANCE_ALPHA:
1250      return GL_SLUMINANCE_ALPHA;
1251   default:
1252      return format;
1253   }
1254}
1255
1256
1257/**
1258 * Return the equivalent non-generic internal format.
1259 * This is useful for comparing whether two internal formats are equivalent.
1260 */
1261GLenum
1262_mesa_get_nongeneric_internalformat(GLenum format)
1263{
1264   switch (format) {
1265   /* GL 1.1 formats. */
1266   case 4:
1267   case GL_RGBA:
1268      return GL_RGBA8;
1269   case 3:
1270   case GL_RGB:
1271      return GL_RGB8;
1272   case 2:
1273   case GL_LUMINANCE_ALPHA:
1274      return GL_LUMINANCE8_ALPHA8;
1275   case 1:
1276   case GL_LUMINANCE:
1277      return GL_LUMINANCE8;
1278   case GL_ALPHA:
1279      return GL_ALPHA8;
1280   case GL_INTENSITY:
1281      return GL_INTENSITY8;
1282
1283   /* GL_ARB_texture_rg */
1284   case GL_RED:
1285      return GL_R8;
1286   case GL_RG:
1287      return GL_RG8;
1288
1289   /* GL_EXT_texture_sRGB */
1290   case GL_SRGB:
1291      return GL_SRGB8;
1292   case GL_SRGB_ALPHA:
1293      return GL_SRGB8_ALPHA8;
1294   case GL_SLUMINANCE:
1295      return GL_SLUMINANCE8;
1296   case GL_SLUMINANCE_ALPHA:
1297      return GL_SLUMINANCE8_ALPHA8;
1298
1299   /* GL_EXT_texture_snorm */
1300   case GL_RGBA_SNORM:
1301      return GL_RGBA8_SNORM;
1302   case GL_RGB_SNORM:
1303      return GL_RGB8_SNORM;
1304   case GL_RG_SNORM:
1305      return GL_RG8_SNORM;
1306   case GL_RED_SNORM:
1307      return GL_R8_SNORM;
1308   case GL_LUMINANCE_ALPHA_SNORM:
1309      return GL_LUMINANCE8_ALPHA8_SNORM;
1310   case GL_LUMINANCE_SNORM:
1311      return GL_LUMINANCE8_SNORM;
1312   case GL_ALPHA_SNORM:
1313      return GL_ALPHA8_SNORM;
1314   case GL_INTENSITY_SNORM:
1315      return GL_INTENSITY8_SNORM;
1316
1317   default:
1318      return format;
1319   }
1320}
1321
1322
1323/**
1324 * Convert an sRGB internal format to linear.
1325 */
1326GLenum
1327_mesa_get_linear_internalformat(GLenum format)
1328{
1329   switch (format) {
1330   case GL_SRGB:
1331      return GL_RGB;
1332   case GL_SRGB_ALPHA:
1333      return GL_RGBA;
1334   case GL_SRGB8:
1335      return GL_RGB8;
1336   case GL_SRGB8_ALPHA8:
1337      return GL_RGBA8;
1338   case GL_SLUMINANCE8:
1339      return GL_LUMINANCE8;
1340   case GL_SLUMINANCE:
1341      return GL_LUMINANCE;
1342   case GL_SLUMINANCE_ALPHA:
1343      return GL_LUMINANCE_ALPHA;
1344   case GL_SLUMINANCE8_ALPHA8:
1345      return GL_LUMINANCE8_ALPHA8;
1346   default:
1347      return format;
1348   }
1349}
1350
1351
1352/**
1353 * Do error checking of format/type combinations for glReadPixels,
1354 * glDrawPixels and glTex[Sub]Image.  Note that depending on the format
1355 * and type values, we may either generate GL_INVALID_OPERATION or
1356 * GL_INVALID_ENUM.
1357 *
1358 * \param format pixel format.
1359 * \param type pixel type.
1360 *
1361 * \return GL_INVALID_ENUM, GL_INVALID_OPERATION or GL_NO_ERROR
1362 */
1363GLenum
1364_mesa_error_check_format_and_type(const struct gl_context *ctx,
1365                                  GLenum format, GLenum type)
1366{
1367   /* From OpenGL 3.3 spec, page 220:
1368    *    "If the format is DEPTH_STENCIL, then values are taken from
1369    *    both the depth buffer and the stencil buffer. If there is no
1370    *    depth buffer or if there is no stencil buffer, then the error
1371    *    INVALID_OPERATION occurs. If the type parameter is not
1372    *    UNSIGNED_INT_24_8 or FLOAT_32_UNSIGNED_INT_24_8_REV, then the
1373    *    error INVALID_ENUM occurs."
1374    *
1375    * OpenGL ES still generates GL_INVALID_OPERATION because glReadPixels
1376    * cannot be used to read depth or stencil in that API.
1377    */
1378   if (_mesa_is_desktop_gl(ctx) && format == GL_DEPTH_STENCIL
1379       && type != GL_UNSIGNED_INT_24_8
1380       && type != GL_FLOAT_32_UNSIGNED_INT_24_8_REV)
1381      return GL_INVALID_ENUM;
1382
1383   /* special type-based checks (see glReadPixels, glDrawPixels error lists) */
1384   switch (type) {
1385   case GL_BITMAP:
1386      if (format != GL_COLOR_INDEX && format != GL_STENCIL_INDEX) {
1387         return GL_INVALID_ENUM;
1388      }
1389      break;
1390
1391   case GL_UNSIGNED_BYTE_3_3_2:
1392   case GL_UNSIGNED_BYTE_2_3_3_REV:
1393   case GL_UNSIGNED_SHORT_5_6_5:
1394   case GL_UNSIGNED_SHORT_5_6_5_REV:
1395      if (format == GL_RGB) {
1396         break; /* OK */
1397      }
1398      if (format == GL_RGB_INTEGER_EXT &&
1399          ctx->Extensions.ARB_texture_rgb10_a2ui) {
1400         break; /* OK */
1401      }
1402      return GL_INVALID_OPERATION;
1403
1404   case GL_UNSIGNED_SHORT_4_4_4_4:
1405   case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1406   case GL_UNSIGNED_SHORT_5_5_5_1:
1407   case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1408   case GL_UNSIGNED_INT_8_8_8_8:
1409   case GL_UNSIGNED_INT_8_8_8_8_REV:
1410   case GL_UNSIGNED_INT_10_10_10_2:
1411   case GL_UNSIGNED_INT_2_10_10_10_REV:
1412      if (format == GL_RGBA ||
1413          format == GL_BGRA ||
1414          format == GL_ABGR_EXT) {
1415         break; /* OK */
1416      }
1417      if ((format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT) &&
1418          ctx->Extensions.ARB_texture_rgb10_a2ui) {
1419         break; /* OK */
1420      }
1421      if (type == GL_UNSIGNED_INT_2_10_10_10_REV && format == GL_RGB &&
1422          ctx->API == API_OPENGLES2) {
1423         break; /* OK by GL_EXT_texture_type_2_10_10_10_REV */
1424      }
1425      return GL_INVALID_OPERATION;
1426
1427   case GL_UNSIGNED_INT_24_8:
1428      if (format != GL_DEPTH_STENCIL) {
1429         return GL_INVALID_OPERATION;
1430      }
1431      return GL_NO_ERROR;
1432
1433   case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1434      if (!ctx->Extensions.ARB_depth_buffer_float) {
1435         return GL_INVALID_ENUM;
1436      }
1437      if (format != GL_DEPTH_STENCIL) {
1438         return GL_INVALID_OPERATION;
1439      }
1440      return GL_NO_ERROR;
1441
1442   case GL_UNSIGNED_INT_10F_11F_11F_REV:
1443      if (!ctx->Extensions.EXT_packed_float) {
1444         return GL_INVALID_ENUM;
1445      }
1446      if (format != GL_RGB) {
1447         return GL_INVALID_OPERATION;
1448      }
1449      return GL_NO_ERROR;
1450
1451   default:
1452      ; /* fall-through */
1453   }
1454
1455   /* now, for each format, check the type for compatibility */
1456   switch (format) {
1457      case GL_COLOR_INDEX:
1458      case GL_STENCIL_INDEX:
1459         switch (type) {
1460            case GL_BITMAP:
1461            case GL_BYTE:
1462            case GL_UNSIGNED_BYTE:
1463            case GL_SHORT:
1464            case GL_UNSIGNED_SHORT:
1465            case GL_INT:
1466            case GL_UNSIGNED_INT:
1467            case GL_FLOAT:
1468            case GL_HALF_FLOAT:
1469               return GL_NO_ERROR;
1470            default:
1471               return GL_INVALID_ENUM;
1472         }
1473
1474      case GL_RED:
1475      case GL_GREEN:
1476      case GL_BLUE:
1477      case GL_ALPHA:
1478#if 0 /* not legal!  see table 3.6 of the 1.5 spec */
1479      case GL_INTENSITY:
1480#endif
1481      case GL_LUMINANCE:
1482      case GL_LUMINANCE_ALPHA:
1483      case GL_DEPTH_COMPONENT:
1484         switch (type) {
1485            case GL_BYTE:
1486            case GL_UNSIGNED_BYTE:
1487            case GL_SHORT:
1488            case GL_UNSIGNED_SHORT:
1489            case GL_INT:
1490            case GL_UNSIGNED_INT:
1491            case GL_FLOAT:
1492            case GL_HALF_FLOAT:
1493               return GL_NO_ERROR;
1494            default:
1495               return GL_INVALID_ENUM;
1496         }
1497
1498      case GL_RG:
1499	 if (!ctx->Extensions.ARB_texture_rg)
1500	    return GL_INVALID_ENUM;
1501         switch (type) {
1502            case GL_BYTE:
1503            case GL_UNSIGNED_BYTE:
1504            case GL_SHORT:
1505            case GL_UNSIGNED_SHORT:
1506            case GL_INT:
1507            case GL_UNSIGNED_INT:
1508            case GL_FLOAT:
1509            case GL_HALF_FLOAT:
1510               return GL_NO_ERROR;
1511            default:
1512               return GL_INVALID_ENUM;
1513         }
1514
1515      case GL_RGB:
1516         switch (type) {
1517            case GL_BYTE:
1518            case GL_UNSIGNED_BYTE:
1519            case GL_SHORT:
1520            case GL_UNSIGNED_SHORT:
1521            case GL_INT:
1522            case GL_UNSIGNED_INT:
1523            case GL_FLOAT:
1524            case GL_UNSIGNED_BYTE_3_3_2:
1525            case GL_UNSIGNED_BYTE_2_3_3_REV:
1526            case GL_UNSIGNED_SHORT_5_6_5:
1527            case GL_UNSIGNED_SHORT_5_6_5_REV:
1528            case GL_HALF_FLOAT:
1529               return GL_NO_ERROR;
1530            case GL_UNSIGNED_INT_2_10_10_10_REV:
1531               /* OK by GL_EXT_texture_type_2_10_10_10_REV */
1532               return (ctx->API == API_OPENGLES2)
1533                  ? GL_NO_ERROR : GL_INVALID_ENUM;
1534            case GL_UNSIGNED_INT_5_9_9_9_REV:
1535               return ctx->Extensions.EXT_texture_shared_exponent
1536                  ? GL_NO_ERROR : GL_INVALID_ENUM;
1537            case GL_UNSIGNED_INT_10F_11F_11F_REV:
1538               return ctx->Extensions.EXT_packed_float
1539                  ? GL_NO_ERROR : GL_INVALID_ENUM;
1540            default:
1541               return GL_INVALID_ENUM;
1542         }
1543
1544      case GL_BGR:
1545         switch (type) {
1546            /* NOTE: no packed types are supported with BGR.  That's
1547             * intentional, according to the GL spec.
1548             */
1549            case GL_BYTE:
1550            case GL_UNSIGNED_BYTE:
1551            case GL_SHORT:
1552            case GL_UNSIGNED_SHORT:
1553            case GL_INT:
1554            case GL_UNSIGNED_INT:
1555            case GL_FLOAT:
1556            case GL_HALF_FLOAT:
1557               return GL_NO_ERROR;
1558            default:
1559               return GL_INVALID_ENUM;
1560         }
1561
1562      case GL_RGBA:
1563      case GL_BGRA:
1564      case GL_ABGR_EXT:
1565         switch (type) {
1566            case GL_BYTE:
1567            case GL_UNSIGNED_BYTE:
1568            case GL_SHORT:
1569            case GL_UNSIGNED_SHORT:
1570            case GL_INT:
1571            case GL_UNSIGNED_INT:
1572            case GL_FLOAT:
1573            case GL_UNSIGNED_SHORT_4_4_4_4:
1574            case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1575            case GL_UNSIGNED_SHORT_5_5_5_1:
1576            case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1577            case GL_UNSIGNED_INT_8_8_8_8:
1578            case GL_UNSIGNED_INT_8_8_8_8_REV:
1579            case GL_UNSIGNED_INT_10_10_10_2:
1580            case GL_UNSIGNED_INT_2_10_10_10_REV:
1581            case GL_HALF_FLOAT:
1582               return GL_NO_ERROR;
1583            default:
1584               return GL_INVALID_ENUM;
1585         }
1586
1587      case GL_YCBCR_MESA:
1588         if (!ctx->Extensions.MESA_ycbcr_texture)
1589            return GL_INVALID_ENUM;
1590         if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
1591             type == GL_UNSIGNED_SHORT_8_8_REV_MESA)
1592            return GL_NO_ERROR;
1593         else
1594            return GL_INVALID_OPERATION;
1595
1596      case GL_DEPTH_STENCIL:
1597         if (type == GL_UNSIGNED_INT_24_8)
1598            return GL_NO_ERROR;
1599         else if (ctx->Extensions.ARB_depth_buffer_float &&
1600             type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV)
1601            return GL_NO_ERROR;
1602         else
1603            return GL_INVALID_ENUM;
1604
1605      /* integer-valued formats */
1606      case GL_RED_INTEGER_EXT:
1607      case GL_GREEN_INTEGER_EXT:
1608      case GL_BLUE_INTEGER_EXT:
1609      case GL_ALPHA_INTEGER_EXT:
1610      case GL_RG_INTEGER:
1611         switch (type) {
1612            case GL_BYTE:
1613            case GL_UNSIGNED_BYTE:
1614            case GL_SHORT:
1615            case GL_UNSIGNED_SHORT:
1616            case GL_INT:
1617            case GL_UNSIGNED_INT:
1618               return (ctx->Version >= 30 ||
1619                       ctx->Extensions.EXT_texture_integer)
1620                  ? GL_NO_ERROR : GL_INVALID_ENUM;
1621            default:
1622               return GL_INVALID_ENUM;
1623         }
1624
1625      case GL_RGB_INTEGER_EXT:
1626         switch (type) {
1627            case GL_BYTE:
1628            case GL_UNSIGNED_BYTE:
1629            case GL_SHORT:
1630            case GL_UNSIGNED_SHORT:
1631            case GL_INT:
1632            case GL_UNSIGNED_INT:
1633               return (ctx->Version >= 30 ||
1634                       ctx->Extensions.EXT_texture_integer)
1635                  ? GL_NO_ERROR : GL_INVALID_ENUM;
1636            case GL_UNSIGNED_BYTE_3_3_2:
1637            case GL_UNSIGNED_BYTE_2_3_3_REV:
1638            case GL_UNSIGNED_SHORT_5_6_5:
1639            case GL_UNSIGNED_SHORT_5_6_5_REV:
1640               return ctx->Extensions.ARB_texture_rgb10_a2ui
1641                  ? GL_NO_ERROR : GL_INVALID_ENUM;
1642            default:
1643               return GL_INVALID_ENUM;
1644         }
1645
1646      case GL_BGR_INTEGER_EXT:
1647         switch (type) {
1648            case GL_BYTE:
1649            case GL_UNSIGNED_BYTE:
1650            case GL_SHORT:
1651            case GL_UNSIGNED_SHORT:
1652            case GL_INT:
1653            case GL_UNSIGNED_INT:
1654            /* NOTE: no packed formats w/ BGR format */
1655               return (ctx->Version >= 30 ||
1656                       ctx->Extensions.EXT_texture_integer)
1657                  ? GL_NO_ERROR : GL_INVALID_ENUM;
1658            default:
1659               return GL_INVALID_ENUM;
1660         }
1661
1662      case GL_RGBA_INTEGER_EXT:
1663      case GL_BGRA_INTEGER_EXT:
1664         switch (type) {
1665            case GL_BYTE:
1666            case GL_UNSIGNED_BYTE:
1667            case GL_SHORT:
1668            case GL_UNSIGNED_SHORT:
1669            case GL_INT:
1670            case GL_UNSIGNED_INT:
1671               return (ctx->Version >= 30 ||
1672                       ctx->Extensions.EXT_texture_integer)
1673                  ? GL_NO_ERROR : GL_INVALID_ENUM;
1674            case GL_UNSIGNED_SHORT_4_4_4_4:
1675            case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1676            case GL_UNSIGNED_SHORT_5_5_5_1:
1677            case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1678            case GL_UNSIGNED_INT_8_8_8_8:
1679            case GL_UNSIGNED_INT_8_8_8_8_REV:
1680            case GL_UNSIGNED_INT_10_10_10_2:
1681            case GL_UNSIGNED_INT_2_10_10_10_REV:
1682               return ctx->Extensions.ARB_texture_rgb10_a2ui
1683                  ? GL_NO_ERROR : GL_INVALID_ENUM;
1684            default:
1685               return GL_INVALID_ENUM;
1686         }
1687
1688      case GL_LUMINANCE_INTEGER_EXT:
1689      case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1690         switch (type) {
1691            case GL_BYTE:
1692            case GL_UNSIGNED_BYTE:
1693            case GL_SHORT:
1694            case GL_UNSIGNED_SHORT:
1695            case GL_INT:
1696            case GL_UNSIGNED_INT:
1697               return ctx->Extensions.EXT_texture_integer
1698                  ? GL_NO_ERROR : GL_INVALID_ENUM;
1699            default:
1700               return GL_INVALID_ENUM;
1701         }
1702
1703      default:
1704         return GL_INVALID_ENUM;
1705   }
1706   return GL_NO_ERROR;
1707}
1708
1709
1710/**
1711 * Do error checking of format/type combinations for OpenGL ES glReadPixels
1712 * and glTex[Sub]Image.
1713 * \return error code, or GL_NO_ERROR.
1714 */
1715GLenum
1716_mesa_es_error_check_format_and_type(GLenum format, GLenum type,
1717                                     unsigned dimensions)
1718{
1719   GLboolean type_valid = GL_TRUE;
1720
1721   switch (format) {
1722   case GL_ALPHA:
1723   case GL_LUMINANCE:
1724   case GL_LUMINANCE_ALPHA:
1725      type_valid = (type == GL_UNSIGNED_BYTE
1726                    || type == GL_FLOAT
1727                    || type == GL_HALF_FLOAT_OES);
1728      break;
1729
1730   case GL_RGB:
1731      type_valid = (type == GL_UNSIGNED_BYTE
1732                    || type == GL_UNSIGNED_SHORT_5_6_5
1733                    || type == GL_FLOAT
1734                    || type == GL_HALF_FLOAT_OES);
1735      break;
1736
1737   case GL_RGBA:
1738      type_valid = (type == GL_UNSIGNED_BYTE
1739                    || type == GL_UNSIGNED_SHORT_4_4_4_4
1740                    || type == GL_UNSIGNED_SHORT_5_5_5_1
1741                    || type == GL_FLOAT
1742                    || type == GL_HALF_FLOAT_OES
1743                    || type == GL_UNSIGNED_INT_2_10_10_10_REV);
1744      break;
1745
1746   case GL_DEPTH_COMPONENT:
1747      /* This format is filtered against invalid dimensionalities elsewhere.
1748       */
1749      type_valid = (type == GL_UNSIGNED_SHORT
1750                    || type == GL_UNSIGNED_INT);
1751      break;
1752
1753   case GL_DEPTH_STENCIL:
1754      /* This format is filtered against invalid dimensionalities elsewhere.
1755       */
1756      type_valid = (type == GL_UNSIGNED_INT_24_8);
1757      break;
1758
1759   case GL_BGRA_EXT:
1760      type_valid = (type == GL_UNSIGNED_BYTE);
1761
1762      /* This feels like a bug in the EXT_texture_format_BGRA8888 spec, but
1763       * the format does not appear to be allowed for 3D textures in OpenGL
1764       * ES.
1765       */
1766      if (dimensions != 2)
1767         return GL_INVALID_VALUE;
1768
1769      break;
1770
1771   default:
1772      return GL_INVALID_VALUE;
1773   }
1774
1775   return type_valid ? GL_NO_ERROR : GL_INVALID_OPERATION;
1776}
1777
1778
1779/**
1780 * Do error checking of format/type combinations for OpenGL ES 3
1781 * glTex[Sub]Image.
1782 * \return error code, or GL_NO_ERROR.
1783 */
1784GLenum
1785_mesa_es3_error_check_format_and_type(GLenum format, GLenum type,
1786                                      GLenum internalFormat)
1787{
1788   switch (format) {
1789   case GL_RGBA:
1790      switch (type) {
1791      case GL_UNSIGNED_BYTE:
1792         switch (internalFormat) {
1793         case GL_RGBA:
1794         case GL_RGBA8:
1795         case GL_RGB5_A1:
1796         case GL_RGBA4:
1797         case GL_SRGB8_ALPHA8_EXT:
1798            break;
1799         default:
1800            return GL_INVALID_OPERATION;
1801         }
1802         break;
1803
1804      case GL_BYTE:
1805         if (internalFormat != GL_RGBA8_SNORM)
1806            return GL_INVALID_OPERATION;
1807         break;
1808
1809      case GL_UNSIGNED_SHORT_4_4_4_4:
1810         switch (internalFormat) {
1811         case GL_RGBA:
1812         case GL_RGBA4:
1813            break;
1814         default:
1815            return GL_INVALID_OPERATION;
1816         }
1817         break;
1818
1819      case GL_UNSIGNED_SHORT_5_5_5_1:
1820         switch (internalFormat) {
1821         case GL_RGBA:
1822         case GL_RGB5_A1:
1823            break;
1824         default:
1825            return GL_INVALID_OPERATION;
1826         }
1827         break;
1828
1829      case GL_UNSIGNED_INT_2_10_10_10_REV:
1830         switch (internalFormat) {
1831         case GL_RGBA: /* GL_EXT_texture_type_2_10_10_10_REV */
1832         case GL_RGB10_A2:
1833         case GL_RGB5_A1:
1834            break;
1835         default:
1836            return GL_INVALID_OPERATION;
1837         }
1838         break;
1839
1840      case GL_HALF_FLOAT:
1841         if (internalFormat != GL_RGBA16F)
1842            return GL_INVALID_OPERATION;
1843         break;
1844
1845      case GL_FLOAT:
1846         switch (internalFormat) {
1847         case GL_RGBA16F:
1848         case GL_RGBA32F:
1849            break;
1850         default:
1851            return GL_INVALID_OPERATION;
1852         }
1853         break;
1854
1855      default:
1856         return GL_INVALID_OPERATION;
1857      }
1858      break;
1859
1860   case GL_RGBA_INTEGER:
1861      switch (type) {
1862      case GL_UNSIGNED_BYTE:
1863         if (internalFormat != GL_RGBA8UI)
1864            return GL_INVALID_OPERATION;
1865         break;
1866
1867      case GL_BYTE:
1868         if (internalFormat != GL_RGBA8I)
1869            return GL_INVALID_OPERATION;
1870         break;
1871
1872      case GL_UNSIGNED_SHORT:
1873         if (internalFormat != GL_RGBA16UI)
1874            return GL_INVALID_OPERATION;
1875         break;
1876
1877      case GL_SHORT:
1878         if (internalFormat != GL_RGBA16I)
1879            return GL_INVALID_OPERATION;
1880         break;
1881
1882      case GL_UNSIGNED_INT:
1883         if (internalFormat != GL_RGBA32UI)
1884            return GL_INVALID_OPERATION;
1885         break;
1886
1887      case GL_INT:
1888         if (internalFormat != GL_RGBA32I)
1889            return GL_INVALID_OPERATION;
1890         break;
1891
1892      case GL_UNSIGNED_INT_2_10_10_10_REV:
1893         if (internalFormat != GL_RGB10_A2UI)
1894            return GL_INVALID_OPERATION;
1895         break;
1896
1897      default:
1898         return GL_INVALID_OPERATION;
1899      }
1900      break;
1901
1902   case GL_RGB:
1903      switch (type) {
1904      case GL_UNSIGNED_BYTE:
1905         switch (internalFormat) {
1906         case GL_RGB:
1907         case GL_RGB8:
1908         case GL_RGB565:
1909         case GL_SRGB8:
1910            break;
1911         default:
1912            return GL_INVALID_OPERATION;
1913         }
1914         break;
1915
1916      case GL_BYTE:
1917         if (internalFormat != GL_RGB8_SNORM)
1918            return GL_INVALID_OPERATION;
1919         break;
1920
1921      case GL_UNSIGNED_SHORT_5_6_5:
1922         switch (internalFormat) {
1923         case GL_RGB:
1924         case GL_RGB565:
1925            break;
1926         default:
1927            return GL_INVALID_OPERATION;
1928         }
1929         break;
1930
1931      case GL_UNSIGNED_INT_10F_11F_11F_REV:
1932         if (internalFormat != GL_R11F_G11F_B10F)
1933            return GL_INVALID_OPERATION;
1934         break;
1935
1936      case GL_UNSIGNED_INT_5_9_9_9_REV:
1937         if (internalFormat != GL_RGB9_E5)
1938            return GL_INVALID_OPERATION;
1939         break;
1940
1941      case GL_HALF_FLOAT:
1942         switch (internalFormat) {
1943         case GL_RGB16F:
1944         case GL_R11F_G11F_B10F:
1945         case GL_RGB9_E5:
1946            break;
1947         default:
1948            return GL_INVALID_OPERATION;
1949         }
1950         break;
1951
1952      case GL_FLOAT:
1953         switch (internalFormat) {
1954         case GL_RGB16F:
1955         case GL_RGB32F:
1956         case GL_R11F_G11F_B10F:
1957         case GL_RGB9_E5:
1958            break;
1959         default:
1960            return GL_INVALID_OPERATION;
1961         }
1962         break;
1963
1964      case GL_UNSIGNED_INT_2_10_10_10_REV:
1965         switch (internalFormat) {
1966         case GL_RGB: /* GL_EXT_texture_type_2_10_10_10_REV */
1967            break;
1968         default:
1969            return GL_INVALID_OPERATION;
1970         }
1971         break;
1972
1973      default:
1974         return GL_INVALID_OPERATION;
1975      }
1976      break;
1977
1978   case GL_RGB_INTEGER:
1979      switch (type) {
1980      case GL_UNSIGNED_BYTE:
1981         if (internalFormat != GL_RGB8UI)
1982            return GL_INVALID_OPERATION;
1983         break;
1984
1985      case GL_BYTE:
1986         if (internalFormat != GL_RGB8I)
1987            return GL_INVALID_OPERATION;
1988         break;
1989
1990      case GL_UNSIGNED_SHORT:
1991         if (internalFormat != GL_RGB16UI)
1992            return GL_INVALID_OPERATION;
1993         break;
1994
1995      case GL_SHORT:
1996         if (internalFormat != GL_RGB16I)
1997            return GL_INVALID_OPERATION;
1998         break;
1999
2000      case GL_UNSIGNED_INT:
2001         if (internalFormat != GL_RGB32UI)
2002            return GL_INVALID_OPERATION;
2003         break;
2004
2005      case GL_INT:
2006         if (internalFormat != GL_RGB32I)
2007            return GL_INVALID_OPERATION;
2008         break;
2009
2010      default:
2011         return GL_INVALID_OPERATION;
2012      }
2013      break;
2014
2015   case GL_RG:
2016      switch (type) {
2017      case GL_UNSIGNED_BYTE:
2018         if (internalFormat != GL_RG8)
2019            return GL_INVALID_OPERATION;
2020         break;
2021
2022      case GL_BYTE:
2023         if (internalFormat != GL_RG8_SNORM)
2024            return GL_INVALID_OPERATION;
2025         break;
2026
2027      case GL_HALF_FLOAT:
2028         if (internalFormat != GL_RG16F)
2029            return GL_INVALID_OPERATION;
2030         break;
2031
2032      case GL_FLOAT:
2033         switch (internalFormat) {
2034         case GL_RG16F:
2035         case GL_RG32F:
2036            break;
2037         default:
2038            return GL_INVALID_OPERATION;
2039         }
2040         break;
2041
2042      default:
2043         return GL_INVALID_OPERATION;
2044      }
2045      break;
2046
2047   case GL_RG_INTEGER:
2048      switch (type) {
2049      case GL_UNSIGNED_BYTE:
2050         if (internalFormat != GL_RG8UI)
2051            return GL_INVALID_OPERATION;
2052         break;
2053
2054      case GL_BYTE:
2055         if (internalFormat != GL_RG8I)
2056            return GL_INVALID_OPERATION;
2057         break;
2058
2059      case GL_UNSIGNED_SHORT:
2060         if (internalFormat != GL_RG16UI)
2061            return GL_INVALID_OPERATION;
2062         break;
2063
2064      case GL_SHORT:
2065         if (internalFormat != GL_RG16I)
2066            return GL_INVALID_OPERATION;
2067         break;
2068
2069      case GL_UNSIGNED_INT:
2070         if (internalFormat != GL_RG32UI)
2071            return GL_INVALID_OPERATION;
2072         break;
2073
2074      case GL_INT:
2075         if (internalFormat != GL_RG32I)
2076            return GL_INVALID_OPERATION;
2077         break;
2078
2079      default:
2080         return GL_INVALID_OPERATION;
2081      }
2082      break;
2083
2084   case GL_RED:
2085      switch (type) {
2086      case GL_UNSIGNED_BYTE:
2087         if (internalFormat != GL_R8)
2088            return GL_INVALID_OPERATION;
2089         break;
2090
2091      case GL_BYTE:
2092         if (internalFormat != GL_R8_SNORM)
2093            return GL_INVALID_OPERATION;
2094         break;
2095
2096      case GL_HALF_FLOAT:
2097         if (internalFormat != GL_R16F)
2098            return GL_INVALID_OPERATION;
2099         break;
2100
2101      case GL_FLOAT:
2102         switch (internalFormat) {
2103         case GL_R16F:
2104         case GL_R32F:
2105            break;
2106         default:
2107            return GL_INVALID_OPERATION;
2108         }
2109         break;
2110
2111      default:
2112         return GL_INVALID_OPERATION;
2113      }
2114      break;
2115
2116   case GL_RED_INTEGER:
2117      switch (type) {
2118      case GL_UNSIGNED_BYTE:
2119         if (internalFormat != GL_R8UI)
2120            return GL_INVALID_OPERATION;
2121         break;
2122
2123      case GL_BYTE:
2124         if (internalFormat != GL_R8I)
2125            return GL_INVALID_OPERATION;
2126         break;
2127
2128      case GL_UNSIGNED_SHORT:
2129         if (internalFormat != GL_R16UI)
2130            return GL_INVALID_OPERATION;
2131         break;
2132
2133      case GL_SHORT:
2134         if (internalFormat != GL_R16I)
2135            return GL_INVALID_OPERATION;
2136         break;
2137
2138      case GL_UNSIGNED_INT:
2139         if (internalFormat != GL_R32UI)
2140            return GL_INVALID_OPERATION;
2141         break;
2142
2143      case GL_INT:
2144         if (internalFormat != GL_R32I)
2145            return GL_INVALID_OPERATION;
2146         break;
2147
2148      default:
2149         return GL_INVALID_OPERATION;
2150      }
2151      break;
2152
2153   case GL_DEPTH_COMPONENT:
2154      switch (type) {
2155      case GL_UNSIGNED_SHORT:
2156         if (internalFormat != GL_DEPTH_COMPONENT
2157             && internalFormat != GL_DEPTH_COMPONENT16)
2158            return GL_INVALID_OPERATION;
2159         break;
2160
2161      case GL_UNSIGNED_INT:
2162         switch (internalFormat) {
2163         case GL_DEPTH_COMPONENT:
2164         case GL_DEPTH_COMPONENT16:
2165         case GL_DEPTH_COMPONENT24:
2166            break;
2167         default:
2168            return GL_INVALID_OPERATION;
2169         }
2170         break;
2171
2172      case GL_FLOAT:
2173         if (internalFormat != GL_DEPTH_COMPONENT32F)
2174            return GL_INVALID_OPERATION;
2175         break;
2176
2177      default:
2178         return GL_INVALID_OPERATION;
2179      }
2180      break;
2181
2182   case GL_DEPTH_STENCIL:
2183      switch (type) {
2184      case GL_UNSIGNED_INT_24_8:
2185         if (internalFormat != GL_DEPTH_STENCIL
2186             && internalFormat != GL_DEPTH24_STENCIL8)
2187            return GL_INVALID_OPERATION;
2188         break;
2189
2190      case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
2191         if (internalFormat != GL_DEPTH32F_STENCIL8)
2192            return GL_INVALID_OPERATION;
2193         break;
2194
2195      default:
2196         return GL_INVALID_OPERATION;
2197      }
2198      break;
2199
2200   case GL_ALPHA:
2201   case GL_LUMINANCE:
2202   case GL_LUMINANCE_ALPHA:
2203      if (type != GL_UNSIGNED_BYTE || format != internalFormat)
2204         return GL_INVALID_OPERATION;
2205      break;
2206   }
2207
2208   return GL_NO_ERROR;
2209}
2210