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