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