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