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