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