glformats: add _mesa_base_format_component_count
[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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #include "context.h"
28 #include "glformats.h"
29
30
31 /**
32 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
33 */
34 GLboolean
35 _mesa_type_is_packed(GLenum type)
36 {
37 switch (type) {
38 case GL_UNSIGNED_BYTE_3_3_2:
39 case GL_UNSIGNED_BYTE_2_3_3_REV:
40 case MESA_UNSIGNED_BYTE_4_4:
41 case GL_UNSIGNED_SHORT_5_6_5:
42 case GL_UNSIGNED_SHORT_5_6_5_REV:
43 case GL_UNSIGNED_SHORT_4_4_4_4:
44 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
45 case GL_UNSIGNED_SHORT_5_5_5_1:
46 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
47 case GL_UNSIGNED_INT_8_8_8_8:
48 case GL_UNSIGNED_INT_8_8_8_8_REV:
49 case GL_UNSIGNED_INT_10_10_10_2:
50 case GL_UNSIGNED_INT_2_10_10_10_REV:
51 case GL_UNSIGNED_SHORT_8_8_MESA:
52 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
53 case GL_UNSIGNED_INT_24_8_EXT:
54 case GL_UNSIGNED_INT_5_9_9_9_REV:
55 case GL_UNSIGNED_INT_10F_11F_11F_REV:
56 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
57 return GL_TRUE;
58 }
59
60 return GL_FALSE;
61 }
62
63
64 /**
65 * Get the size of a GL data type.
66 *
67 * \param type GL data type.
68 *
69 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
70 * if an invalid type enum.
71 */
72 GLint
73 _mesa_sizeof_type(GLenum type)
74 {
75 switch (type) {
76 case GL_BITMAP:
77 return 0;
78 case GL_UNSIGNED_BYTE:
79 return sizeof(GLubyte);
80 case GL_BYTE:
81 return sizeof(GLbyte);
82 case GL_UNSIGNED_SHORT:
83 return sizeof(GLushort);
84 case GL_SHORT:
85 return sizeof(GLshort);
86 case GL_UNSIGNED_INT:
87 return sizeof(GLuint);
88 case GL_INT:
89 return sizeof(GLint);
90 case GL_FLOAT:
91 return sizeof(GLfloat);
92 case GL_DOUBLE:
93 return sizeof(GLdouble);
94 case GL_HALF_FLOAT_ARB:
95 return sizeof(GLhalfARB);
96 case GL_FIXED:
97 return sizeof(GLfixed);
98 default:
99 return -1;
100 }
101 }
102
103
104 /**
105 * Same as _mesa_sizeof_type() but also accepting the packed pixel
106 * format data types.
107 */
108 GLint
109 _mesa_sizeof_packed_type(GLenum type)
110 {
111 switch (type) {
112 case GL_BITMAP:
113 return 0;
114 case GL_UNSIGNED_BYTE:
115 return sizeof(GLubyte);
116 case GL_BYTE:
117 return sizeof(GLbyte);
118 case GL_UNSIGNED_SHORT:
119 return sizeof(GLushort);
120 case GL_SHORT:
121 return sizeof(GLshort);
122 case GL_UNSIGNED_INT:
123 return sizeof(GLuint);
124 case GL_INT:
125 return sizeof(GLint);
126 case GL_HALF_FLOAT_ARB:
127 return sizeof(GLhalfARB);
128 case GL_FLOAT:
129 return sizeof(GLfloat);
130 case GL_UNSIGNED_BYTE_3_3_2:
131 case GL_UNSIGNED_BYTE_2_3_3_REV:
132 case MESA_UNSIGNED_BYTE_4_4:
133 return sizeof(GLubyte);
134 case GL_UNSIGNED_SHORT_5_6_5:
135 case GL_UNSIGNED_SHORT_5_6_5_REV:
136 case GL_UNSIGNED_SHORT_4_4_4_4:
137 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
138 case GL_UNSIGNED_SHORT_5_5_5_1:
139 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
140 case GL_UNSIGNED_SHORT_8_8_MESA:
141 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
142 return sizeof(GLushort);
143 case GL_UNSIGNED_INT_8_8_8_8:
144 case GL_UNSIGNED_INT_8_8_8_8_REV:
145 case GL_UNSIGNED_INT_10_10_10_2:
146 case GL_UNSIGNED_INT_2_10_10_10_REV:
147 case GL_UNSIGNED_INT_24_8_EXT:
148 case GL_UNSIGNED_INT_5_9_9_9_REV:
149 case GL_UNSIGNED_INT_10F_11F_11F_REV:
150 return sizeof(GLuint);
151 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
152 return 8;
153 default:
154 return -1;
155 }
156 }
157
158
159 /**
160 * Get the number of components in a pixel format.
161 *
162 * \param format pixel format.
163 *
164 * \return the number of components in the given format, or -1 if a bad format.
165 */
166 GLint
167 _mesa_components_in_format(GLenum format)
168 {
169 switch (format) {
170 case GL_COLOR_INDEX:
171 case GL_STENCIL_INDEX:
172 case GL_DEPTH_COMPONENT:
173 case GL_RED:
174 case GL_RED_INTEGER_EXT:
175 case GL_GREEN:
176 case GL_GREEN_INTEGER_EXT:
177 case GL_BLUE:
178 case GL_BLUE_INTEGER_EXT:
179 case GL_ALPHA:
180 case GL_ALPHA_INTEGER_EXT:
181 case GL_LUMINANCE:
182 case GL_LUMINANCE_INTEGER_EXT:
183 case GL_INTENSITY:
184 return 1;
185
186 case GL_LUMINANCE_ALPHA:
187 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
188 case GL_RG:
189 case GL_YCBCR_MESA:
190 case GL_DEPTH_STENCIL_EXT:
191 case GL_DUDV_ATI:
192 case GL_DU8DV8_ATI:
193 case GL_RG_INTEGER:
194 return 2;
195
196 case GL_RGB:
197 case GL_BGR:
198 case GL_RGB_INTEGER_EXT:
199 case GL_BGR_INTEGER_EXT:
200 return 3;
201
202 case GL_RGBA:
203 case GL_BGRA:
204 case GL_ABGR_EXT:
205 case GL_RGBA_INTEGER_EXT:
206 case GL_BGRA_INTEGER_EXT:
207 return 4;
208
209 default:
210 return -1;
211 }
212 }
213
214
215 /**
216 * Get the bytes per pixel of pixel format type pair.
217 *
218 * \param format pixel format.
219 * \param type pixel type.
220 *
221 * \return bytes per pixel, or -1 if a bad format or type was given.
222 */
223 GLint
224 _mesa_bytes_per_pixel(GLenum format, GLenum type)
225 {
226 GLint comps = _mesa_components_in_format(format);
227 if (comps < 0)
228 return -1;
229
230 switch (type) {
231 case GL_BITMAP:
232 return 0; /* special case */
233 case GL_BYTE:
234 case GL_UNSIGNED_BYTE:
235 return comps * sizeof(GLubyte);
236 case GL_SHORT:
237 case GL_UNSIGNED_SHORT:
238 return comps * sizeof(GLshort);
239 case GL_INT:
240 case GL_UNSIGNED_INT:
241 return comps * sizeof(GLint);
242 case GL_FLOAT:
243 return comps * sizeof(GLfloat);
244 case GL_HALF_FLOAT_ARB:
245 return comps * sizeof(GLhalfARB);
246 case GL_UNSIGNED_BYTE_3_3_2:
247 case GL_UNSIGNED_BYTE_2_3_3_REV:
248 if (format == GL_RGB || format == GL_BGR ||
249 format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT)
250 return sizeof(GLubyte);
251 else
252 return -1; /* error */
253 case GL_UNSIGNED_SHORT_5_6_5:
254 case GL_UNSIGNED_SHORT_5_6_5_REV:
255 if (format == GL_RGB || format == GL_BGR ||
256 format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT)
257 return sizeof(GLushort);
258 else
259 return -1; /* error */
260 case GL_UNSIGNED_SHORT_4_4_4_4:
261 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
262 case GL_UNSIGNED_SHORT_5_5_5_1:
263 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
264 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT ||
265 format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT)
266 return sizeof(GLushort);
267 else
268 return -1;
269 case GL_UNSIGNED_INT_8_8_8_8:
270 case GL_UNSIGNED_INT_8_8_8_8_REV:
271 case GL_UNSIGNED_INT_10_10_10_2:
272 case GL_UNSIGNED_INT_2_10_10_10_REV:
273 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT ||
274 format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT)
275 return sizeof(GLuint);
276 else
277 return -1;
278 case GL_UNSIGNED_SHORT_8_8_MESA:
279 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
280 if (format == GL_YCBCR_MESA)
281 return sizeof(GLushort);
282 else
283 return -1;
284 case GL_UNSIGNED_INT_24_8_EXT:
285 if (format == GL_DEPTH_STENCIL_EXT)
286 return sizeof(GLuint);
287 else
288 return -1;
289 case GL_UNSIGNED_INT_5_9_9_9_REV:
290 if (format == GL_RGB)
291 return sizeof(GLuint);
292 else
293 return -1;
294 case GL_UNSIGNED_INT_10F_11F_11F_REV:
295 if (format == GL_RGB)
296 return sizeof(GLuint);
297 else
298 return -1;
299 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
300 if (format == GL_DEPTH_STENCIL)
301 return 8;
302 else
303 return -1;
304 default:
305 return -1;
306 }
307 }
308
309
310 /**
311 * Get the number of bytes for a vertex attrib with the given number of
312 * components and type.
313 *
314 * \param comps number of components.
315 * \param type data type.
316 *
317 * \return bytes per attribute, or -1 if a bad comps/type combination was given.
318 */
319 GLint
320 _mesa_bytes_per_vertex_attrib(GLint comps, GLenum type)
321 {
322 switch (type) {
323 case GL_BYTE:
324 case GL_UNSIGNED_BYTE:
325 return comps * sizeof(GLubyte);
326 case GL_SHORT:
327 case GL_UNSIGNED_SHORT:
328 return comps * sizeof(GLshort);
329 case GL_INT:
330 case GL_UNSIGNED_INT:
331 return comps * sizeof(GLint);
332 case GL_FLOAT:
333 return comps * sizeof(GLfloat);
334 case GL_HALF_FLOAT_ARB:
335 return comps * sizeof(GLhalfARB);
336 case GL_DOUBLE:
337 return comps * sizeof(GLdouble);
338 case GL_FIXED:
339 return comps * sizeof(GLfixed);
340 case GL_INT_2_10_10_10_REV:
341 case GL_UNSIGNED_INT_2_10_10_10_REV:
342 if (comps == 4)
343 return sizeof(GLuint);
344 else
345 return -1;
346 default:
347 return -1;
348 }
349 }
350
351
352 /**
353 * Test if the given format is an integer (non-normalized) format.
354 */
355 GLboolean
356 _mesa_is_enum_format_unsigned_int(GLenum format)
357 {
358 switch (format) {
359 /* specific integer formats */
360 case GL_RGBA32UI_EXT:
361 case GL_RGB32UI_EXT:
362 case GL_RG32UI:
363 case GL_R32UI:
364 case GL_ALPHA32UI_EXT:
365 case GL_INTENSITY32UI_EXT:
366 case GL_LUMINANCE32UI_EXT:
367 case GL_LUMINANCE_ALPHA32UI_EXT:
368 case GL_RGBA16UI_EXT:
369 case GL_RGB16UI_EXT:
370 case GL_RG16UI:
371 case GL_R16UI:
372 case GL_ALPHA16UI_EXT:
373 case GL_INTENSITY16UI_EXT:
374 case GL_LUMINANCE16UI_EXT:
375 case GL_LUMINANCE_ALPHA16UI_EXT:
376 case GL_RGBA8UI_EXT:
377 case GL_RGB8UI_EXT:
378 case GL_RG8UI:
379 case GL_R8UI:
380 case GL_ALPHA8UI_EXT:
381 case GL_INTENSITY8UI_EXT:
382 case GL_LUMINANCE8UI_EXT:
383 case GL_LUMINANCE_ALPHA8UI_EXT:
384 case GL_RGB10_A2UI:
385 return GL_TRUE;
386 default:
387 return GL_FALSE;
388 }
389 }
390
391
392 /**
393 * Test if the given format is an integer (non-normalized) format.
394 */
395 GLboolean
396 _mesa_is_enum_format_signed_int(GLenum format)
397 {
398 switch (format) {
399 /* generic integer formats */
400 case GL_RED_INTEGER_EXT:
401 case GL_GREEN_INTEGER_EXT:
402 case GL_BLUE_INTEGER_EXT:
403 case GL_ALPHA_INTEGER_EXT:
404 case GL_RGB_INTEGER_EXT:
405 case GL_RGBA_INTEGER_EXT:
406 case GL_BGR_INTEGER_EXT:
407 case GL_BGRA_INTEGER_EXT:
408 case GL_LUMINANCE_INTEGER_EXT:
409 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
410 case GL_RG_INTEGER:
411 /* specific integer formats */
412 case GL_RGBA32I_EXT:
413 case GL_RGB32I_EXT:
414 case GL_RG32I:
415 case GL_R32I:
416 case GL_ALPHA32I_EXT:
417 case GL_INTENSITY32I_EXT:
418 case GL_LUMINANCE32I_EXT:
419 case GL_LUMINANCE_ALPHA32I_EXT:
420 case GL_RGBA16I_EXT:
421 case GL_RGB16I_EXT:
422 case GL_RG16I:
423 case GL_R16I:
424 case GL_ALPHA16I_EXT:
425 case GL_INTENSITY16I_EXT:
426 case GL_LUMINANCE16I_EXT:
427 case GL_LUMINANCE_ALPHA16I_EXT:
428 case GL_RGBA8I_EXT:
429 case GL_RGB8I_EXT:
430 case GL_RG8I:
431 case GL_R8I:
432 case GL_ALPHA8I_EXT:
433 case GL_INTENSITY8I_EXT:
434 case GL_LUMINANCE8I_EXT:
435 case GL_LUMINANCE_ALPHA8I_EXT:
436 return GL_TRUE;
437 default:
438 return GL_FALSE;
439 }
440 }
441
442
443 /**
444 * Test if the given format is an integer (non-normalized) format.
445 */
446 GLboolean
447 _mesa_is_enum_format_integer(GLenum format)
448 {
449 return _mesa_is_enum_format_unsigned_int(format) ||
450 _mesa_is_enum_format_signed_int(format);
451 }
452
453
454 /**
455 * Test if the given type is an integer (non-normalized) format.
456 */
457 GLboolean
458 _mesa_is_type_integer(GLenum type)
459 {
460 switch (type) {
461 case GL_INT:
462 case GL_UNSIGNED_INT:
463 case GL_SHORT:
464 case GL_UNSIGNED_SHORT:
465 case GL_BYTE:
466 case GL_UNSIGNED_BYTE:
467 return GL_TRUE;
468 default:
469 return GL_FALSE;
470 }
471 }
472
473
474 /**
475 * Test if the given format or type is an integer (non-normalized) format.
476 */
477 extern GLboolean
478 _mesa_is_enum_format_or_type_integer(GLenum format, GLenum type)
479 {
480 return _mesa_is_enum_format_integer(format) || _mesa_is_type_integer(type);
481 }
482
483
484 GLboolean
485 _mesa_is_type_unsigned(GLenum type)
486 {
487 switch (type) {
488 case GL_UNSIGNED_INT:
489 case GL_UNSIGNED_INT_8_8_8_8:
490 case GL_UNSIGNED_INT_8_8_8_8_REV:
491 case GL_UNSIGNED_INT_10_10_10_2:
492 case GL_UNSIGNED_INT_2_10_10_10_REV:
493
494 case GL_UNSIGNED_SHORT:
495 case GL_UNSIGNED_SHORT_4_4_4_4:
496 case GL_UNSIGNED_SHORT_5_5_5_1:
497 case GL_UNSIGNED_SHORT_5_6_5:
498 case GL_UNSIGNED_SHORT_5_6_5_REV:
499 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
500 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
501 case GL_UNSIGNED_SHORT_8_8_MESA:
502 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
503
504 case GL_UNSIGNED_BYTE:
505 case GL_UNSIGNED_BYTE_3_3_2:
506 case GL_UNSIGNED_BYTE_2_3_3_REV:
507 return GL_TRUE;
508
509 default:
510 return GL_FALSE;
511 }
512 }
513
514
515 /**
516 * Test if the given image format is a color/RGBA format (i.e., not color
517 * index, depth, stencil, etc).
518 * \param format the image format value (may by an internal texture format)
519 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
520 */
521 GLboolean
522 _mesa_is_color_format(GLenum format)
523 {
524 switch (format) {
525 case GL_RED:
526 case GL_GREEN:
527 case GL_BLUE:
528 case GL_ALPHA:
529 case GL_ALPHA4:
530 case GL_ALPHA8:
531 case GL_ALPHA12:
532 case GL_ALPHA16:
533 case 1:
534 case GL_LUMINANCE:
535 case GL_LUMINANCE4:
536 case GL_LUMINANCE8:
537 case GL_LUMINANCE12:
538 case GL_LUMINANCE16:
539 case 2:
540 case GL_LUMINANCE_ALPHA:
541 case GL_LUMINANCE4_ALPHA4:
542 case GL_LUMINANCE6_ALPHA2:
543 case GL_LUMINANCE8_ALPHA8:
544 case GL_LUMINANCE12_ALPHA4:
545 case GL_LUMINANCE12_ALPHA12:
546 case GL_LUMINANCE16_ALPHA16:
547 case GL_INTENSITY:
548 case GL_INTENSITY4:
549 case GL_INTENSITY8:
550 case GL_INTENSITY12:
551 case GL_INTENSITY16:
552 case GL_R8:
553 case GL_R16:
554 case GL_RG:
555 case GL_RG8:
556 case GL_RG16:
557 case 3:
558 case GL_RGB:
559 case GL_BGR:
560 case GL_R3_G3_B2:
561 case GL_RGB4:
562 case GL_RGB5:
563 case GL_RGB565:
564 case GL_RGB8:
565 case GL_RGB10:
566 case GL_RGB12:
567 case GL_RGB16:
568 case 4:
569 case GL_ABGR_EXT:
570 case GL_RGBA:
571 case GL_BGRA:
572 case GL_RGBA2:
573 case GL_RGBA4:
574 case GL_RGB5_A1:
575 case GL_RGBA8:
576 case GL_RGB10_A2:
577 case GL_RGBA12:
578 case GL_RGBA16:
579 /* float texture formats */
580 case GL_ALPHA16F_ARB:
581 case GL_ALPHA32F_ARB:
582 case GL_LUMINANCE16F_ARB:
583 case GL_LUMINANCE32F_ARB:
584 case GL_LUMINANCE_ALPHA16F_ARB:
585 case GL_LUMINANCE_ALPHA32F_ARB:
586 case GL_INTENSITY16F_ARB:
587 case GL_INTENSITY32F_ARB:
588 case GL_R16F:
589 case GL_R32F:
590 case GL_RG16F:
591 case GL_RG32F:
592 case GL_RGB16F_ARB:
593 case GL_RGB32F_ARB:
594 case GL_RGBA16F_ARB:
595 case GL_RGBA32F_ARB:
596 /* compressed formats */
597 case GL_COMPRESSED_ALPHA:
598 case GL_COMPRESSED_LUMINANCE:
599 case GL_COMPRESSED_LUMINANCE_ALPHA:
600 case GL_COMPRESSED_INTENSITY:
601 case GL_COMPRESSED_RED:
602 case GL_COMPRESSED_RG:
603 case GL_COMPRESSED_RGB:
604 case GL_COMPRESSED_RGBA:
605 case GL_RGB_S3TC:
606 case GL_RGB4_S3TC:
607 case GL_RGBA_S3TC:
608 case GL_RGBA4_S3TC:
609 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
610 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
611 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
612 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
613 case GL_COMPRESSED_RGB_FXT1_3DFX:
614 case GL_COMPRESSED_RGBA_FXT1_3DFX:
615 case GL_SRGB_EXT:
616 case GL_SRGB8_EXT:
617 case GL_SRGB_ALPHA_EXT:
618 case GL_SRGB8_ALPHA8_EXT:
619 case GL_SLUMINANCE_ALPHA_EXT:
620 case GL_SLUMINANCE8_ALPHA8_EXT:
621 case GL_SLUMINANCE_EXT:
622 case GL_SLUMINANCE8_EXT:
623 case GL_COMPRESSED_SRGB_EXT:
624 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
625 case GL_COMPRESSED_SRGB_ALPHA_EXT:
626 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
627 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
628 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
629 case GL_COMPRESSED_SLUMINANCE_EXT:
630 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
631 case GL_COMPRESSED_RED_RGTC1:
632 case GL_COMPRESSED_SIGNED_RED_RGTC1:
633 case GL_COMPRESSED_RG_RGTC2:
634 case GL_COMPRESSED_SIGNED_RG_RGTC2:
635 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
636 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
637 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
638 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
639 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
640 case GL_ETC1_RGB8_OES:
641 case GL_COMPRESSED_RGB8_ETC2:
642 case GL_COMPRESSED_SRGB8_ETC2:
643 case GL_COMPRESSED_RGBA8_ETC2_EAC:
644 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
645 case GL_COMPRESSED_R11_EAC:
646 case GL_COMPRESSED_RG11_EAC:
647 case GL_COMPRESSED_SIGNED_R11_EAC:
648 case GL_COMPRESSED_SIGNED_RG11_EAC:
649 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
650 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
651 /* generic integer formats */
652 case GL_RED_INTEGER_EXT:
653 case GL_GREEN_INTEGER_EXT:
654 case GL_BLUE_INTEGER_EXT:
655 case GL_ALPHA_INTEGER_EXT:
656 case GL_RGB_INTEGER_EXT:
657 case GL_RGBA_INTEGER_EXT:
658 case GL_BGR_INTEGER_EXT:
659 case GL_BGRA_INTEGER_EXT:
660 case GL_RG_INTEGER:
661 case GL_LUMINANCE_INTEGER_EXT:
662 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
663 /* sized integer formats */
664 case GL_RGBA32UI_EXT:
665 case GL_RGB32UI_EXT:
666 case GL_RG32UI:
667 case GL_R32UI:
668 case GL_ALPHA32UI_EXT:
669 case GL_INTENSITY32UI_EXT:
670 case GL_LUMINANCE32UI_EXT:
671 case GL_LUMINANCE_ALPHA32UI_EXT:
672 case GL_RGBA16UI_EXT:
673 case GL_RGB16UI_EXT:
674 case GL_RG16UI:
675 case GL_R16UI:
676 case GL_ALPHA16UI_EXT:
677 case GL_INTENSITY16UI_EXT:
678 case GL_LUMINANCE16UI_EXT:
679 case GL_LUMINANCE_ALPHA16UI_EXT:
680 case GL_RGBA8UI_EXT:
681 case GL_RGB8UI_EXT:
682 case GL_RG8UI:
683 case GL_R8UI:
684 case GL_ALPHA8UI_EXT:
685 case GL_INTENSITY8UI_EXT:
686 case GL_LUMINANCE8UI_EXT:
687 case GL_LUMINANCE_ALPHA8UI_EXT:
688 case GL_RGBA32I_EXT:
689 case GL_RGB32I_EXT:
690 case GL_RG32I:
691 case GL_R32I:
692 case GL_ALPHA32I_EXT:
693 case GL_INTENSITY32I_EXT:
694 case GL_LUMINANCE32I_EXT:
695 case GL_LUMINANCE_ALPHA32I_EXT:
696 case GL_RGBA16I_EXT:
697 case GL_RGB16I_EXT:
698 case GL_RG16I:
699 case GL_R16I:
700 case GL_ALPHA16I_EXT:
701 case GL_INTENSITY16I_EXT:
702 case GL_LUMINANCE16I_EXT:
703 case GL_LUMINANCE_ALPHA16I_EXT:
704 case GL_RGBA8I_EXT:
705 case GL_RGB8I_EXT:
706 case GL_RG8I:
707 case GL_R8I:
708 case GL_ALPHA8I_EXT:
709 case GL_INTENSITY8I_EXT:
710 case GL_LUMINANCE8I_EXT:
711 case GL_LUMINANCE_ALPHA8I_EXT:
712 /* signed, normalized texture formats */
713 case GL_RED_SNORM:
714 case GL_R8_SNORM:
715 case GL_R16_SNORM:
716 case GL_RG_SNORM:
717 case GL_RG8_SNORM:
718 case GL_RG16_SNORM:
719 case GL_RGB_SNORM:
720 case GL_RGB8_SNORM:
721 case GL_RGB16_SNORM:
722 case GL_RGBA_SNORM:
723 case GL_RGBA8_SNORM:
724 case GL_RGBA16_SNORM:
725 case GL_ALPHA_SNORM:
726 case GL_ALPHA8_SNORM:
727 case GL_ALPHA16_SNORM:
728 case GL_LUMINANCE_SNORM:
729 case GL_LUMINANCE8_SNORM:
730 case GL_LUMINANCE16_SNORM:
731 case GL_LUMINANCE_ALPHA_SNORM:
732 case GL_LUMINANCE8_ALPHA8_SNORM:
733 case GL_LUMINANCE16_ALPHA16_SNORM:
734 case GL_INTENSITY_SNORM:
735 case GL_INTENSITY8_SNORM:
736 case GL_INTENSITY16_SNORM:
737 case GL_RGB9_E5:
738 case GL_R11F_G11F_B10F:
739 case GL_RGB10_A2UI:
740 return GL_TRUE;
741 case GL_YCBCR_MESA: /* not considered to be RGB */
742 /* fall-through */
743 default:
744 return GL_FALSE;
745 }
746 }
747
748
749 /**
750 * Test if the given image format is a depth component format.
751 */
752 GLboolean
753 _mesa_is_depth_format(GLenum format)
754 {
755 switch (format) {
756 case GL_DEPTH_COMPONENT:
757 case GL_DEPTH_COMPONENT16:
758 case GL_DEPTH_COMPONENT24:
759 case GL_DEPTH_COMPONENT32:
760 case GL_DEPTH_COMPONENT32F:
761 return GL_TRUE;
762 default:
763 return GL_FALSE;
764 }
765 }
766
767
768 /**
769 * Test if the given image format is a stencil format.
770 */
771 GLboolean
772 _mesa_is_stencil_format(GLenum format)
773 {
774 switch (format) {
775 case GL_STENCIL_INDEX:
776 return GL_TRUE;
777 default:
778 return GL_FALSE;
779 }
780 }
781
782
783 /**
784 * Test if the given image format is a YCbCr format.
785 */
786 GLboolean
787 _mesa_is_ycbcr_format(GLenum format)
788 {
789 switch (format) {
790 case GL_YCBCR_MESA:
791 return GL_TRUE;
792 default:
793 return GL_FALSE;
794 }
795 }
796
797
798 /**
799 * Test if the given image format is a depth+stencil format.
800 */
801 GLboolean
802 _mesa_is_depthstencil_format(GLenum format)
803 {
804 switch (format) {
805 case GL_DEPTH24_STENCIL8_EXT:
806 case GL_DEPTH_STENCIL_EXT:
807 case GL_DEPTH32F_STENCIL8:
808 return GL_TRUE;
809 default:
810 return GL_FALSE;
811 }
812 }
813
814
815 /**
816 * Test if the given image format is a depth or stencil format.
817 */
818 GLboolean
819 _mesa_is_depth_or_stencil_format(GLenum format)
820 {
821 switch (format) {
822 case GL_DEPTH_COMPONENT:
823 case GL_DEPTH_COMPONENT16:
824 case GL_DEPTH_COMPONENT24:
825 case GL_DEPTH_COMPONENT32:
826 case GL_STENCIL_INDEX:
827 case GL_STENCIL_INDEX1_EXT:
828 case GL_STENCIL_INDEX4_EXT:
829 case GL_STENCIL_INDEX8_EXT:
830 case GL_STENCIL_INDEX16_EXT:
831 case GL_DEPTH_STENCIL_EXT:
832 case GL_DEPTH24_STENCIL8_EXT:
833 case GL_DEPTH_COMPONENT32F:
834 case GL_DEPTH32F_STENCIL8:
835 return GL_TRUE;
836 default:
837 return GL_FALSE;
838 }
839 }
840
841
842 /**
843 * Test if the given image format is a dudv format.
844 */
845 GLboolean
846 _mesa_is_dudv_format(GLenum format)
847 {
848 switch (format) {
849 case GL_DUDV_ATI:
850 case GL_DU8DV8_ATI:
851 return GL_TRUE;
852 default:
853 return GL_FALSE;
854 }
855 }
856
857
858 /**
859 * Test if an image format is a supported compressed format.
860 * \param format the internal format token provided by the user.
861 * \return GL_TRUE if compressed, GL_FALSE if uncompressed
862 */
863 GLboolean
864 _mesa_is_compressed_format(struct gl_context *ctx, GLenum format)
865 {
866 switch (format) {
867 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
868 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
869 return ctx->Extensions.EXT_texture_compression_s3tc;
870 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
871 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
872 return (_mesa_is_desktop_gl(ctx) &&
873 ctx->Extensions.EXT_texture_compression_s3tc) ||
874 (ctx->API == API_OPENGLES2 &&
875 ctx->Extensions.ANGLE_texture_compression_dxt);
876 case GL_RGB_S3TC:
877 case GL_RGB4_S3TC:
878 case GL_RGBA_S3TC:
879 case GL_RGBA4_S3TC:
880 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.S3_s3tc;
881 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
882 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
883 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
884 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
885 return _mesa_is_desktop_gl(ctx)
886 && ctx->Extensions.EXT_texture_sRGB
887 && ctx->Extensions.EXT_texture_compression_s3tc;
888 case GL_COMPRESSED_RGB_FXT1_3DFX:
889 case GL_COMPRESSED_RGBA_FXT1_3DFX:
890 return _mesa_is_desktop_gl(ctx)
891 && ctx->Extensions.TDFX_texture_compression_FXT1;
892 case GL_COMPRESSED_RED_RGTC1:
893 case GL_COMPRESSED_SIGNED_RED_RGTC1:
894 case GL_COMPRESSED_RG_RGTC2:
895 case GL_COMPRESSED_SIGNED_RG_RGTC2:
896 return _mesa_is_desktop_gl(ctx)
897 && ctx->Extensions.ARB_texture_compression_rgtc;
898 case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
899 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
900 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
901 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
902 return ctx->API == API_OPENGL_COMPAT
903 && ctx->Extensions.EXT_texture_compression_latc;
904 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI:
905 return ctx->API == API_OPENGL_COMPAT
906 && ctx->Extensions.ATI_texture_compression_3dc;
907 case GL_ETC1_RGB8_OES:
908 return _mesa_is_gles(ctx)
909 && ctx->Extensions.OES_compressed_ETC1_RGB8_texture;
910 case GL_COMPRESSED_RGB8_ETC2:
911 case GL_COMPRESSED_SRGB8_ETC2:
912 case GL_COMPRESSED_RGBA8_ETC2_EAC:
913 case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
914 case GL_COMPRESSED_R11_EAC:
915 case GL_COMPRESSED_RG11_EAC:
916 case GL_COMPRESSED_SIGNED_R11_EAC:
917 case GL_COMPRESSED_SIGNED_RG11_EAC:
918 case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
919 case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
920 return _mesa_is_gles3(ctx);
921 case GL_PALETTE4_RGB8_OES:
922 case GL_PALETTE4_RGBA8_OES:
923 case GL_PALETTE4_R5_G6_B5_OES:
924 case GL_PALETTE4_RGBA4_OES:
925 case GL_PALETTE4_RGB5_A1_OES:
926 case GL_PALETTE8_RGB8_OES:
927 case GL_PALETTE8_RGBA8_OES:
928 case GL_PALETTE8_R5_G6_B5_OES:
929 case GL_PALETTE8_RGBA4_OES:
930 case GL_PALETTE8_RGB5_A1_OES:
931 return ctx->API == API_OPENGLES;
932 default:
933 return GL_FALSE;
934 }
935 }
936
937
938 /**
939 * Convert various base formats to the cooresponding integer format.
940 */
941 GLenum
942 _mesa_base_format_to_integer_format(GLenum format)
943 {
944 switch(format) {
945 case GL_RED:
946 return GL_RED_INTEGER;
947 case GL_GREEN:
948 return GL_GREEN_INTEGER;
949 case GL_BLUE:
950 return GL_BLUE_INTEGER;
951 case GL_RG:
952 return GL_RG_INTEGER;
953 case GL_RGB:
954 return GL_RGB_INTEGER;
955 case GL_RGBA:
956 return GL_RGBA_INTEGER;
957 case GL_BGR:
958 return GL_BGR_INTEGER;
959 case GL_BGRA:
960 return GL_BGRA_INTEGER;
961 case GL_ALPHA:
962 return GL_ALPHA_INTEGER;
963 case GL_LUMINANCE:
964 return GL_LUMINANCE_INTEGER_EXT;
965 case GL_LUMINANCE_ALPHA:
966 return GL_LUMINANCE_ALPHA_INTEGER_EXT;
967 }
968
969 return format;
970 }
971
972
973 /**
974 * Does the given base texture/renderbuffer format have the channel
975 * named by 'pname'?
976 */
977 GLboolean
978 _mesa_base_format_has_channel(GLenum base_format, GLenum pname)
979 {
980 switch (pname) {
981 case GL_TEXTURE_RED_SIZE:
982 case GL_TEXTURE_RED_TYPE:
983 case GL_RENDERBUFFER_RED_SIZE_EXT:
984 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
985 if (base_format == GL_RED ||
986 base_format == GL_RG ||
987 base_format == GL_RGB ||
988 base_format == GL_RGBA) {
989 return GL_TRUE;
990 }
991 return GL_FALSE;
992 case GL_TEXTURE_GREEN_SIZE:
993 case GL_TEXTURE_GREEN_TYPE:
994 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
995 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
996 if (base_format == GL_RG ||
997 base_format == GL_RGB ||
998 base_format == GL_RGBA) {
999 return GL_TRUE;
1000 }
1001 return GL_FALSE;
1002 case GL_TEXTURE_BLUE_SIZE:
1003 case GL_TEXTURE_BLUE_TYPE:
1004 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1005 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1006 if (base_format == GL_RGB ||
1007 base_format == GL_RGBA) {
1008 return GL_TRUE;
1009 }
1010 return GL_FALSE;
1011 case GL_TEXTURE_ALPHA_SIZE:
1012 case GL_TEXTURE_ALPHA_TYPE:
1013 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1014 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1015 if (base_format == GL_RGBA ||
1016 base_format == GL_ALPHA ||
1017 base_format == GL_LUMINANCE_ALPHA) {
1018 return GL_TRUE;
1019 }
1020 return GL_FALSE;
1021 case GL_TEXTURE_LUMINANCE_SIZE:
1022 case GL_TEXTURE_LUMINANCE_TYPE:
1023 if (base_format == GL_LUMINANCE ||
1024 base_format == GL_LUMINANCE_ALPHA) {
1025 return GL_TRUE;
1026 }
1027 return GL_FALSE;
1028 case GL_TEXTURE_INTENSITY_SIZE:
1029 case GL_TEXTURE_INTENSITY_TYPE:
1030 if (base_format == GL_INTENSITY) {
1031 return GL_TRUE;
1032 }
1033 return GL_FALSE;
1034 case GL_TEXTURE_DEPTH_SIZE:
1035 case GL_TEXTURE_DEPTH_TYPE:
1036 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1037 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1038 if (base_format == GL_DEPTH_STENCIL ||
1039 base_format == GL_DEPTH_COMPONENT) {
1040 return GL_TRUE;
1041 }
1042 return GL_FALSE;
1043 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1044 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1045 if (base_format == GL_DEPTH_STENCIL ||
1046 base_format == GL_STENCIL_INDEX) {
1047 return GL_TRUE;
1048 }
1049 return GL_FALSE;
1050 default:
1051 _mesa_warning(NULL, "%s: Unexpected channel token 0x%x\n",
1052 __FUNCTION__, pname);
1053 return GL_FALSE;
1054 }
1055
1056 return GL_FALSE;
1057 }
1058
1059
1060 /**
1061 * Returns the number of channels/components for a base format.
1062 */
1063 GLint
1064 _mesa_base_format_component_count(GLenum base_format)
1065 {
1066 switch (base_format) {
1067 case GL_RED:
1068 case GL_ALPHA:
1069 case GL_INTENSITY:
1070 case GL_DEPTH_COMPONENT:
1071 return 1;
1072 case GL_RG:
1073 case GL_LUMINANCE_ALPHA:
1074 case GL_DEPTH_STENCIL:
1075 return 2;
1076 case GL_RGB:
1077 return 3;
1078 case GL_RGBA:
1079 return 4;
1080 default:
1081 return -1;
1082 }
1083 }
1084
1085
1086 /**
1087 * If format is a generic compressed format, return the corresponding
1088 * non-compressed format. For other formats, return the format as-is.
1089 */
1090 GLenum
1091 _mesa_generic_compressed_format_to_uncompressed_format(GLenum format)
1092 {
1093 switch (format) {
1094 case GL_COMPRESSED_RED:
1095 return GL_RED;
1096 case GL_COMPRESSED_RG:
1097 return GL_RG;
1098 case GL_COMPRESSED_RGB:
1099 return GL_RGB;
1100 case GL_COMPRESSED_RGBA:
1101 return GL_RGBA;
1102 case GL_COMPRESSED_ALPHA:
1103 return GL_ALPHA;
1104 case GL_COMPRESSED_LUMINANCE:
1105 return GL_LUMINANCE;
1106 case GL_COMPRESSED_LUMINANCE_ALPHA:
1107 return GL_LUMINANCE_ALPHA;
1108 case GL_COMPRESSED_INTENSITY:
1109 return GL_INTENSITY;
1110 /* sRGB formats */
1111 case GL_COMPRESSED_SRGB:
1112 return GL_SRGB;
1113 case GL_COMPRESSED_SRGB_ALPHA:
1114 return GL_SRGB_ALPHA;
1115 case GL_COMPRESSED_SLUMINANCE:
1116 return GL_SLUMINANCE;
1117 case GL_COMPRESSED_SLUMINANCE_ALPHA:
1118 return GL_SLUMINANCE_ALPHA;
1119 default:
1120 return format;
1121 }
1122 }
1123
1124
1125 /**
1126 * Return the equivalent non-generic internal format.
1127 * This is useful for comparing whether two internal formats are equivalent.
1128 */
1129 GLenum
1130 _mesa_get_nongeneric_internalformat(GLenum format)
1131 {
1132 switch (format) {
1133 /* GL 1.1 formats. */
1134 case 4:
1135 case GL_RGBA:
1136 return GL_RGBA8;
1137
1138 case 3:
1139 case GL_RGB:
1140 return GL_RGB8;
1141
1142 case 2:
1143 case GL_LUMINANCE_ALPHA:
1144 return GL_LUMINANCE8_ALPHA8;
1145
1146 case 1:
1147 case GL_LUMINANCE:
1148 return GL_LUMINANCE8;
1149
1150 case GL_ALPHA:
1151 return GL_ALPHA8;
1152
1153 case GL_INTENSITY:
1154 return GL_INTENSITY8;
1155
1156 /* GL_ARB_texture_rg */
1157 case GL_RED:
1158 return GL_R8;
1159
1160 case GL_RG:
1161 return GL_RG8;
1162
1163 /* GL_EXT_texture_sRGB */
1164 case GL_SRGB:
1165 return GL_SRGB8;
1166
1167 case GL_SRGB_ALPHA:
1168 return GL_SRGB8_ALPHA8;
1169
1170 case GL_SLUMINANCE:
1171 return GL_SLUMINANCE8;
1172
1173 case GL_SLUMINANCE_ALPHA:
1174 return GL_SLUMINANCE8_ALPHA8;
1175
1176 /* GL_EXT_texture_snorm */
1177 case GL_RGBA_SNORM:
1178 return GL_RGBA8_SNORM;
1179
1180 case GL_RGB_SNORM:
1181 return GL_RGB8_SNORM;
1182
1183 case GL_RG_SNORM:
1184 return GL_RG8_SNORM;
1185
1186 case GL_RED_SNORM:
1187 return GL_R8_SNORM;
1188
1189 case GL_LUMINANCE_ALPHA_SNORM:
1190 return GL_LUMINANCE8_ALPHA8_SNORM;
1191
1192 case GL_LUMINANCE_SNORM:
1193 return GL_LUMINANCE8_SNORM;
1194
1195 case GL_ALPHA_SNORM:
1196 return GL_ALPHA8_SNORM;
1197
1198 case GL_INTENSITY_SNORM:
1199 return GL_INTENSITY8_SNORM;
1200
1201 default:
1202 return format;
1203 }
1204 }
1205
1206
1207 /**
1208 * Convert an sRGB internal format to linear.
1209 */
1210 GLenum
1211 _mesa_get_linear_internalformat(GLenum format)
1212 {
1213 switch (format) {
1214 case GL_SRGB:
1215 return GL_RGB;
1216
1217 case GL_SRGB_ALPHA:
1218 return GL_RGBA;
1219
1220 case GL_SRGB8:
1221 return GL_RGB8;
1222
1223 case GL_SRGB8_ALPHA8:
1224 return GL_RGBA8;
1225
1226 case GL_SLUMINANCE:
1227 return GL_LUMINANCE8;
1228
1229 case GL_SLUMINANCE_ALPHA:
1230 return GL_LUMINANCE8_ALPHA8;
1231
1232 default:
1233 return format;
1234 }
1235 }
1236
1237
1238 /**
1239 * Do error checking of format/type combinations for glReadPixels,
1240 * glDrawPixels and glTex[Sub]Image. Note that depending on the format
1241 * and type values, we may either generate GL_INVALID_OPERATION or
1242 * GL_INVALID_ENUM.
1243 *
1244 * \param format pixel format.
1245 * \param type pixel type.
1246 *
1247 * \return GL_INVALID_ENUM, GL_INVALID_OPERATION or GL_NO_ERROR
1248 */
1249 GLenum
1250 _mesa_error_check_format_and_type(const struct gl_context *ctx,
1251 GLenum format, GLenum type)
1252 {
1253 /* special type-based checks (see glReadPixels, glDrawPixels error lists) */
1254 switch (type) {
1255 case GL_BITMAP:
1256 if (format != GL_COLOR_INDEX && format != GL_STENCIL_INDEX) {
1257 return GL_INVALID_ENUM;
1258 }
1259 break;
1260
1261 case GL_UNSIGNED_BYTE_3_3_2:
1262 case GL_UNSIGNED_BYTE_2_3_3_REV:
1263 case GL_UNSIGNED_SHORT_5_6_5:
1264 case GL_UNSIGNED_SHORT_5_6_5_REV:
1265 if (format == GL_RGB) {
1266 break; /* OK */
1267 }
1268 if (format == GL_RGB_INTEGER_EXT &&
1269 ctx->Extensions.ARB_texture_rgb10_a2ui) {
1270 break; /* OK */
1271 }
1272 return GL_INVALID_OPERATION;
1273
1274 case GL_UNSIGNED_SHORT_4_4_4_4:
1275 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1276 case GL_UNSIGNED_SHORT_5_5_5_1:
1277 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1278 case GL_UNSIGNED_INT_8_8_8_8:
1279 case GL_UNSIGNED_INT_8_8_8_8_REV:
1280 case GL_UNSIGNED_INT_10_10_10_2:
1281 case GL_UNSIGNED_INT_2_10_10_10_REV:
1282 if (format == GL_RGBA ||
1283 format == GL_BGRA ||
1284 format == GL_ABGR_EXT) {
1285 break; /* OK */
1286 }
1287 if ((format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT) &&
1288 ctx->Extensions.ARB_texture_rgb10_a2ui) {
1289 break; /* OK */
1290 }
1291 return GL_INVALID_OPERATION;
1292
1293 case GL_UNSIGNED_INT_24_8:
1294 if (!ctx->Extensions.EXT_packed_depth_stencil) {
1295 return GL_INVALID_ENUM;
1296 }
1297 if (format != GL_DEPTH_STENCIL) {
1298 return GL_INVALID_OPERATION;
1299 }
1300 return GL_NO_ERROR;
1301
1302 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1303 if (!ctx->Extensions.ARB_depth_buffer_float) {
1304 return GL_INVALID_ENUM;
1305 }
1306 if (format != GL_DEPTH_STENCIL) {
1307 return GL_INVALID_OPERATION;
1308 }
1309 return GL_NO_ERROR;
1310
1311 case GL_UNSIGNED_INT_10F_11F_11F_REV:
1312 if (!ctx->Extensions.EXT_packed_float) {
1313 return GL_INVALID_ENUM;
1314 }
1315 if (format != GL_RGB) {
1316 return GL_INVALID_OPERATION;
1317 }
1318 return GL_NO_ERROR;
1319
1320 default:
1321 ; /* fall-through */
1322 }
1323
1324 /* now, for each format, check the type for compatibility */
1325 switch (format) {
1326 case GL_COLOR_INDEX:
1327 case GL_STENCIL_INDEX:
1328 switch (type) {
1329 case GL_BITMAP:
1330 case GL_BYTE:
1331 case GL_UNSIGNED_BYTE:
1332 case GL_SHORT:
1333 case GL_UNSIGNED_SHORT:
1334 case GL_INT:
1335 case GL_UNSIGNED_INT:
1336 case GL_FLOAT:
1337 return GL_NO_ERROR;
1338 case GL_HALF_FLOAT:
1339 return ctx->Extensions.ARB_half_float_pixel
1340 ? GL_NO_ERROR : GL_INVALID_ENUM;
1341 default:
1342 return GL_INVALID_ENUM;
1343 }
1344
1345 case GL_RED:
1346 case GL_GREEN:
1347 case GL_BLUE:
1348 case GL_ALPHA:
1349 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
1350 case GL_INTENSITY:
1351 #endif
1352 case GL_LUMINANCE:
1353 case GL_LUMINANCE_ALPHA:
1354 case GL_DEPTH_COMPONENT:
1355 switch (type) {
1356 case GL_BYTE:
1357 case GL_UNSIGNED_BYTE:
1358 case GL_SHORT:
1359 case GL_UNSIGNED_SHORT:
1360 case GL_INT:
1361 case GL_UNSIGNED_INT:
1362 case GL_FLOAT:
1363 return GL_NO_ERROR;
1364 case GL_HALF_FLOAT:
1365 return ctx->Extensions.ARB_half_float_pixel
1366 ? GL_NO_ERROR : GL_INVALID_ENUM;
1367 default:
1368 return GL_INVALID_ENUM;
1369 }
1370
1371 case GL_RG:
1372 if (!ctx->Extensions.ARB_texture_rg)
1373 return GL_INVALID_ENUM;
1374 switch (type) {
1375 case GL_BYTE:
1376 case GL_UNSIGNED_BYTE:
1377 case GL_SHORT:
1378 case GL_UNSIGNED_SHORT:
1379 case GL_INT:
1380 case GL_UNSIGNED_INT:
1381 case GL_FLOAT:
1382 return GL_NO_ERROR;
1383 case GL_HALF_FLOAT:
1384 return ctx->Extensions.ARB_half_float_pixel
1385 ? GL_NO_ERROR : GL_INVALID_ENUM;
1386 default:
1387 return GL_INVALID_ENUM;
1388 }
1389
1390 case GL_RGB:
1391 switch (type) {
1392 case GL_BYTE:
1393 case GL_UNSIGNED_BYTE:
1394 case GL_SHORT:
1395 case GL_UNSIGNED_SHORT:
1396 case GL_INT:
1397 case GL_UNSIGNED_INT:
1398 case GL_FLOAT:
1399 case GL_UNSIGNED_BYTE_3_3_2:
1400 case GL_UNSIGNED_BYTE_2_3_3_REV:
1401 case GL_UNSIGNED_SHORT_5_6_5:
1402 case GL_UNSIGNED_SHORT_5_6_5_REV:
1403 return GL_NO_ERROR;
1404 case GL_HALF_FLOAT:
1405 return ctx->Extensions.ARB_half_float_pixel
1406 ? GL_NO_ERROR : GL_INVALID_ENUM;
1407 case GL_UNSIGNED_INT_5_9_9_9_REV:
1408 return ctx->Extensions.EXT_texture_shared_exponent
1409 ? GL_NO_ERROR : GL_INVALID_ENUM;
1410 case GL_UNSIGNED_INT_10F_11F_11F_REV:
1411 return ctx->Extensions.EXT_packed_float
1412 ? GL_NO_ERROR : GL_INVALID_ENUM;
1413 default:
1414 return GL_INVALID_ENUM;
1415 }
1416
1417 case GL_BGR:
1418 switch (type) {
1419 /* NOTE: no packed types are supported with BGR. That's
1420 * intentional, according to the GL spec.
1421 */
1422 case GL_BYTE:
1423 case GL_UNSIGNED_BYTE:
1424 case GL_SHORT:
1425 case GL_UNSIGNED_SHORT:
1426 case GL_INT:
1427 case GL_UNSIGNED_INT:
1428 case GL_FLOAT:
1429 return GL_NO_ERROR;
1430 case GL_HALF_FLOAT:
1431 return ctx->Extensions.ARB_half_float_pixel
1432 ? GL_NO_ERROR : GL_INVALID_ENUM;
1433 default:
1434 return GL_INVALID_ENUM;
1435 }
1436
1437 case GL_RGBA:
1438 case GL_BGRA:
1439 case GL_ABGR_EXT:
1440 switch (type) {
1441 case GL_BYTE:
1442 case GL_UNSIGNED_BYTE:
1443 case GL_SHORT:
1444 case GL_UNSIGNED_SHORT:
1445 case GL_INT:
1446 case GL_UNSIGNED_INT:
1447 case GL_FLOAT:
1448 case GL_UNSIGNED_SHORT_4_4_4_4:
1449 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1450 case GL_UNSIGNED_SHORT_5_5_5_1:
1451 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1452 case GL_UNSIGNED_INT_8_8_8_8:
1453 case GL_UNSIGNED_INT_8_8_8_8_REV:
1454 case GL_UNSIGNED_INT_10_10_10_2:
1455 case GL_UNSIGNED_INT_2_10_10_10_REV:
1456 return GL_NO_ERROR;
1457 case GL_HALF_FLOAT:
1458 return ctx->Extensions.ARB_half_float_pixel
1459 ? GL_NO_ERROR : GL_INVALID_ENUM;
1460 default:
1461 return GL_INVALID_ENUM;
1462 }
1463
1464 case GL_YCBCR_MESA:
1465 if (!ctx->Extensions.MESA_ycbcr_texture)
1466 return GL_INVALID_ENUM;
1467 if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
1468 type == GL_UNSIGNED_SHORT_8_8_REV_MESA)
1469 return GL_NO_ERROR;
1470 else
1471 return GL_INVALID_OPERATION;
1472
1473 case GL_DEPTH_STENCIL_EXT:
1474 if (ctx->Extensions.EXT_packed_depth_stencil &&
1475 type == GL_UNSIGNED_INT_24_8)
1476 return GL_NO_ERROR;
1477 else if (ctx->Extensions.ARB_depth_buffer_float &&
1478 type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV)
1479 return GL_NO_ERROR;
1480 else
1481 return GL_INVALID_ENUM;
1482
1483 case GL_DUDV_ATI:
1484 case GL_DU8DV8_ATI:
1485 if (!ctx->Extensions.ATI_envmap_bumpmap)
1486 return GL_INVALID_ENUM;
1487 switch (type) {
1488 case GL_BYTE:
1489 case GL_UNSIGNED_BYTE:
1490 case GL_SHORT:
1491 case GL_UNSIGNED_SHORT:
1492 case GL_INT:
1493 case GL_UNSIGNED_INT:
1494 case GL_FLOAT:
1495 return GL_NO_ERROR;
1496 default:
1497 return GL_INVALID_ENUM;
1498 }
1499
1500 /* integer-valued formats */
1501 case GL_RED_INTEGER_EXT:
1502 case GL_GREEN_INTEGER_EXT:
1503 case GL_BLUE_INTEGER_EXT:
1504 case GL_ALPHA_INTEGER_EXT:
1505 case GL_RG_INTEGER:
1506 switch (type) {
1507 case GL_BYTE:
1508 case GL_UNSIGNED_BYTE:
1509 case GL_SHORT:
1510 case GL_UNSIGNED_SHORT:
1511 case GL_INT:
1512 case GL_UNSIGNED_INT:
1513 return (ctx->Version >= 30 ||
1514 ctx->Extensions.EXT_texture_integer)
1515 ? GL_NO_ERROR : GL_INVALID_ENUM;
1516 default:
1517 return GL_INVALID_ENUM;
1518 }
1519
1520 case GL_RGB_INTEGER_EXT:
1521 switch (type) {
1522 case GL_BYTE:
1523 case GL_UNSIGNED_BYTE:
1524 case GL_SHORT:
1525 case GL_UNSIGNED_SHORT:
1526 case GL_INT:
1527 case GL_UNSIGNED_INT:
1528 return (ctx->Version >= 30 ||
1529 ctx->Extensions.EXT_texture_integer)
1530 ? GL_NO_ERROR : GL_INVALID_ENUM;
1531 case GL_UNSIGNED_BYTE_3_3_2:
1532 case GL_UNSIGNED_BYTE_2_3_3_REV:
1533 case GL_UNSIGNED_SHORT_5_6_5:
1534 case GL_UNSIGNED_SHORT_5_6_5_REV:
1535 return ctx->Extensions.ARB_texture_rgb10_a2ui
1536 ? GL_NO_ERROR : GL_INVALID_ENUM;
1537 default:
1538 return GL_INVALID_ENUM;
1539 }
1540
1541 case GL_BGR_INTEGER_EXT:
1542 switch (type) {
1543 case GL_BYTE:
1544 case GL_UNSIGNED_BYTE:
1545 case GL_SHORT:
1546 case GL_UNSIGNED_SHORT:
1547 case GL_INT:
1548 case GL_UNSIGNED_INT:
1549 /* NOTE: no packed formats w/ BGR format */
1550 return (ctx->Version >= 30 ||
1551 ctx->Extensions.EXT_texture_integer)
1552 ? GL_NO_ERROR : GL_INVALID_ENUM;
1553 default:
1554 return GL_INVALID_ENUM;
1555 }
1556
1557 case GL_RGBA_INTEGER_EXT:
1558 case GL_BGRA_INTEGER_EXT:
1559 switch (type) {
1560 case GL_BYTE:
1561 case GL_UNSIGNED_BYTE:
1562 case GL_SHORT:
1563 case GL_UNSIGNED_SHORT:
1564 case GL_INT:
1565 case GL_UNSIGNED_INT:
1566 return (ctx->Version >= 30 ||
1567 ctx->Extensions.EXT_texture_integer)
1568 ? GL_NO_ERROR : GL_INVALID_ENUM;
1569 case GL_UNSIGNED_SHORT_4_4_4_4:
1570 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1571 case GL_UNSIGNED_SHORT_5_5_5_1:
1572 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1573 case GL_UNSIGNED_INT_8_8_8_8:
1574 case GL_UNSIGNED_INT_8_8_8_8_REV:
1575 case GL_UNSIGNED_INT_10_10_10_2:
1576 case GL_UNSIGNED_INT_2_10_10_10_REV:
1577 return ctx->Extensions.ARB_texture_rgb10_a2ui
1578 ? GL_NO_ERROR : GL_INVALID_ENUM;
1579 default:
1580 return GL_INVALID_ENUM;
1581 }
1582
1583 case GL_LUMINANCE_INTEGER_EXT:
1584 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1585 switch (type) {
1586 case GL_BYTE:
1587 case GL_UNSIGNED_BYTE:
1588 case GL_SHORT:
1589 case GL_UNSIGNED_SHORT:
1590 case GL_INT:
1591 case GL_UNSIGNED_INT:
1592 return ctx->Extensions.EXT_texture_integer
1593 ? GL_NO_ERROR : GL_INVALID_ENUM;
1594 default:
1595 return GL_INVALID_ENUM;
1596 }
1597
1598 default:
1599 return GL_INVALID_ENUM;
1600 }
1601 return GL_NO_ERROR;
1602 }
1603
1604
1605 /**
1606 * Do error checking of format/type combinations for OpenGL ES glReadPixels
1607 * and glTex[Sub]Image.
1608 * \return error code, or GL_NO_ERROR.
1609 */
1610 GLenum
1611 _mesa_es_error_check_format_and_type(GLenum format, GLenum type,
1612 unsigned dimensions)
1613 {
1614 GLboolean type_valid = GL_TRUE;
1615
1616 switch (format) {
1617 case GL_ALPHA:
1618 case GL_LUMINANCE:
1619 case GL_LUMINANCE_ALPHA:
1620 type_valid = (type == GL_UNSIGNED_BYTE
1621 || type == GL_FLOAT
1622 || type == GL_HALF_FLOAT_OES);
1623 break;
1624
1625 case GL_RGB:
1626 type_valid = (type == GL_UNSIGNED_BYTE
1627 || type == GL_UNSIGNED_SHORT_5_6_5
1628 || type == GL_FLOAT
1629 || type == GL_HALF_FLOAT_OES);
1630 break;
1631
1632 case GL_RGBA:
1633 type_valid = (type == GL_UNSIGNED_BYTE
1634 || type == GL_UNSIGNED_SHORT_4_4_4_4
1635 || type == GL_UNSIGNED_SHORT_5_5_5_1
1636 || type == GL_FLOAT
1637 || type == GL_HALF_FLOAT_OES
1638 || type == GL_UNSIGNED_INT_2_10_10_10_REV);
1639 break;
1640
1641 case GL_DEPTH_COMPONENT:
1642 /* This format is filtered against invalid dimensionalities elsewhere.
1643 */
1644 type_valid = (type == GL_UNSIGNED_SHORT
1645 || type == GL_UNSIGNED_INT);
1646 break;
1647
1648 case GL_DEPTH_STENCIL:
1649 /* This format is filtered against invalid dimensionalities elsewhere.
1650 */
1651 type_valid = (type == GL_UNSIGNED_INT_24_8);
1652 break;
1653
1654 case GL_BGRA_EXT:
1655 type_valid = (type == GL_UNSIGNED_BYTE);
1656
1657 /* This feels like a bug in the EXT_texture_format_BGRA8888 spec, but
1658 * the format does not appear to be allowed for 3D textures in OpenGL
1659 * ES.
1660 */
1661 if (dimensions != 2)
1662 return GL_INVALID_VALUE;
1663
1664 break;
1665
1666 default:
1667 return GL_INVALID_VALUE;
1668 }
1669
1670 return type_valid ? GL_NO_ERROR : GL_INVALID_OPERATION;
1671 }