mesa/formats: add some formats from GL3.3
[mesa.git] / src / mesa / main / formats.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 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #include "imports.h"
28 #include "formats.h"
29 #include "macros.h"
30 #include "glformats.h"
31 #include "c11/threads.h"
32 #include "util/hash_table.h"
33
34 /**
35 * Information about texture formats.
36 */
37 struct gl_format_info
38 {
39 mesa_format Name;
40
41 /** text name for debugging */
42 const char *StrName;
43
44 enum mesa_format_layout Layout;
45
46 /**
47 * Base format is one of GL_RED, GL_RG, GL_RGB, GL_RGBA, GL_ALPHA,
48 * GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_INTENSITY, GL_YCBCR_MESA,
49 * GL_DEPTH_COMPONENT, GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
50 */
51 GLenum BaseFormat;
52
53 /**
54 * Logical data type: one of GL_UNSIGNED_NORMALIZED, GL_SIGNED_NORMALIZED,
55 * GL_UNSIGNED_INT, GL_INT, GL_FLOAT.
56 */
57 GLenum DataType;
58
59 GLubyte RedBits;
60 GLubyte GreenBits;
61 GLubyte BlueBits;
62 GLubyte AlphaBits;
63 GLubyte LuminanceBits;
64 GLubyte IntensityBits;
65 GLubyte DepthBits;
66 GLubyte StencilBits;
67
68 bool IsSRGBFormat;
69
70 /**
71 * To describe compressed formats. If not compressed, Width=Height=1.
72 */
73 GLubyte BlockWidth, BlockHeight;
74 GLubyte BytesPerBlock;
75
76 uint8_t Swizzle[4];
77 mesa_array_format ArrayFormat;
78 };
79
80 #include "format_info.h"
81
82 static const struct gl_format_info *
83 _mesa_get_format_info(mesa_format format)
84 {
85 const struct gl_format_info *info = &format_info[format];
86 STATIC_ASSERT(ARRAY_SIZE(format_info) == MESA_FORMAT_COUNT);
87 assert(info->Name == format);
88 return info;
89 }
90
91
92 /** Return string name of format (for debugging) */
93 const char *
94 _mesa_get_format_name(mesa_format format)
95 {
96 const struct gl_format_info *info = _mesa_get_format_info(format);
97 return info->StrName;
98 }
99
100
101
102 /**
103 * Return bytes needed to store a block of pixels in the given format.
104 * Normally, a block is 1x1 (a single pixel). But for compressed formats
105 * a block may be 4x4 or 8x4, etc.
106 *
107 * Note: not GLuint, so as not to coerce math to unsigned. cf. fdo #37351
108 */
109 GLint
110 _mesa_get_format_bytes(mesa_format format)
111 {
112 const struct gl_format_info *info = _mesa_get_format_info(format);
113 assert(info->BytesPerBlock);
114 assert(info->BytesPerBlock <= MAX_PIXEL_BYTES ||
115 _mesa_is_format_compressed(format));
116 return info->BytesPerBlock;
117 }
118
119
120 /**
121 * Return bits per component for the given format.
122 * \param format one of MESA_FORMAT_x
123 * \param pname the component, such as GL_RED_BITS, GL_TEXTURE_BLUE_BITS, etc.
124 */
125 GLint
126 _mesa_get_format_bits(mesa_format format, GLenum pname)
127 {
128 const struct gl_format_info *info = _mesa_get_format_info(format);
129
130 switch (pname) {
131 case GL_RED_BITS:
132 case GL_TEXTURE_RED_SIZE:
133 case GL_RENDERBUFFER_RED_SIZE_EXT:
134 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
135 return info->RedBits;
136 case GL_GREEN_BITS:
137 case GL_TEXTURE_GREEN_SIZE:
138 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
139 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
140 return info->GreenBits;
141 case GL_BLUE_BITS:
142 case GL_TEXTURE_BLUE_SIZE:
143 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
144 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
145 return info->BlueBits;
146 case GL_ALPHA_BITS:
147 case GL_TEXTURE_ALPHA_SIZE:
148 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
149 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
150 return info->AlphaBits;
151 case GL_TEXTURE_INTENSITY_SIZE:
152 return info->IntensityBits;
153 case GL_TEXTURE_LUMINANCE_SIZE:
154 return info->LuminanceBits;
155 case GL_INDEX_BITS:
156 return 0;
157 case GL_DEPTH_BITS:
158 case GL_TEXTURE_DEPTH_SIZE_ARB:
159 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
160 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
161 return info->DepthBits;
162 case GL_STENCIL_BITS:
163 case GL_TEXTURE_STENCIL_SIZE_EXT:
164 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
165 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
166 return info->StencilBits;
167 default:
168 _mesa_problem(NULL, "bad pname in _mesa_get_format_bits()");
169 return 0;
170 }
171 }
172
173
174 GLuint
175 _mesa_get_format_max_bits(mesa_format format)
176 {
177 const struct gl_format_info *info = _mesa_get_format_info(format);
178 GLuint max = MAX2(info->RedBits, info->GreenBits);
179 max = MAX2(max, info->BlueBits);
180 max = MAX2(max, info->AlphaBits);
181 max = MAX2(max, info->LuminanceBits);
182 max = MAX2(max, info->IntensityBits);
183 max = MAX2(max, info->DepthBits);
184 max = MAX2(max, info->StencilBits);
185 return max;
186 }
187
188
189 /**
190 * Return the layout type of the given format.
191 * The return value will be one of:
192 * MESA_FORMAT_LAYOUT_ARRAY
193 * MESA_FORMAT_LAYOUT_PACKED
194 * MESA_FORMAT_LAYOUT_S3TC
195 * MESA_FORMAT_LAYOUT_RGTC
196 * MESA_FORMAT_LAYOUT_FXT1
197 * MESA_FORMAT_LAYOUT_ETC1
198 * MESA_FORMAT_LAYOUT_ETC2
199 * MESA_FORMAT_LAYOUT_BPTC
200 * MESA_FORMAT_LAYOUT_ASTC
201 * MESA_FORMAT_LAYOUT_OTHER
202 */
203 extern enum mesa_format_layout
204 _mesa_get_format_layout(mesa_format format)
205 {
206 const struct gl_format_info *info = _mesa_get_format_info(format);
207 return info->Layout;
208 }
209
210
211 /**
212 * Return the data type (or more specifically, the data representation)
213 * for the given format.
214 * The return value will be one of:
215 * GL_UNSIGNED_NORMALIZED = unsigned int representing [0,1]
216 * GL_SIGNED_NORMALIZED = signed int representing [-1, 1]
217 * GL_UNSIGNED_INT = an ordinary unsigned integer
218 * GL_INT = an ordinary signed integer
219 * GL_FLOAT = an ordinary float
220 */
221 GLenum
222 _mesa_get_format_datatype(mesa_format format)
223 {
224 const struct gl_format_info *info = _mesa_get_format_info(format);
225 return info->DataType;
226 }
227
228 static GLenum
229 get_base_format_for_array_format(mesa_array_format format)
230 {
231 uint8_t swizzle[4];
232 int num_channels;
233
234 _mesa_array_format_get_swizzle(format, swizzle);
235 num_channels = _mesa_array_format_get_num_channels(format);
236
237 switch (num_channels) {
238 case 4:
239 /* FIXME: RGBX formats have 4 channels, but their base format is GL_RGB.
240 * This is not really a problem for now because we only create array
241 * formats from GL format/type combinations, and these cannot specify
242 * RGBX formats.
243 */
244 return GL_RGBA;
245 case 3:
246 return GL_RGB;
247 case 2:
248 if (swizzle[0] == 0 &&
249 swizzle[1] == 0 &&
250 swizzle[2] == 0 &&
251 swizzle[3] == 1)
252 return GL_LUMINANCE_ALPHA;
253 if (swizzle[0] == 1 &&
254 swizzle[1] == 1 &&
255 swizzle[2] == 1 &&
256 swizzle[3] == 0)
257 return GL_LUMINANCE_ALPHA;
258 if (swizzle[0] == 0 &&
259 swizzle[1] == 1 &&
260 swizzle[2] == 4 &&
261 swizzle[3] == 5)
262 return GL_RG;
263 if (swizzle[0] == 1 &&
264 swizzle[1] == 0 &&
265 swizzle[2] == 4 &&
266 swizzle[3] == 5)
267 return GL_RG;
268 break;
269 case 1:
270 if (swizzle[0] == 0 &&
271 swizzle[1] == 0 &&
272 swizzle[2] == 0 &&
273 swizzle[3] == 5)
274 return GL_LUMINANCE;
275 if (swizzle[0] == 0 &&
276 swizzle[1] == 0 &&
277 swizzle[2] == 0 &&
278 swizzle[3] == 0)
279 return GL_INTENSITY;
280 if (swizzle[0] <= MESA_FORMAT_SWIZZLE_W)
281 return GL_RED;
282 if (swizzle[1] <= MESA_FORMAT_SWIZZLE_W)
283 return GL_GREEN;
284 if (swizzle[2] <= MESA_FORMAT_SWIZZLE_W)
285 return GL_BLUE;
286 if (swizzle[3] <= MESA_FORMAT_SWIZZLE_W)
287 return GL_ALPHA;
288 break;
289 }
290
291 unreachable("Unsupported format");
292 }
293
294 /**
295 * Return the basic format for the given type. The result will be one of
296 * GL_RGB, GL_RGBA, GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_INTENSITY,
297 * GL_YCBCR_MESA, GL_DEPTH_COMPONENT, GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
298 * This functions accepts a mesa_format or a mesa_array_format.
299 */
300 GLenum
301 _mesa_get_format_base_format(uint32_t format)
302 {
303 if (!_mesa_format_is_mesa_array_format(format)) {
304 const struct gl_format_info *info = _mesa_get_format_info(format);
305 return info->BaseFormat;
306 } else {
307 return get_base_format_for_array_format(format);
308 }
309 }
310
311
312 /**
313 * Return the block size (in pixels) for the given format. Normally
314 * the block size is 1x1. But compressed formats will have block sizes
315 * of 4x4 or 8x4 pixels, etc.
316 * \param bw returns block width in pixels
317 * \param bh returns block height in pixels
318 */
319 void
320 _mesa_get_format_block_size(mesa_format format, GLuint *bw, GLuint *bh)
321 {
322 const struct gl_format_info *info = _mesa_get_format_info(format);
323 *bw = info->BlockWidth;
324 *bh = info->BlockHeight;
325 }
326
327
328 /**
329 * Returns the an array of four numbers representing the transformation
330 * from the RGBA or SZ colorspace to the given format. For array formats,
331 * the i'th RGBA component is given by:
332 *
333 * if (swizzle[i] <= MESA_FORMAT_SWIZZLE_W)
334 * comp = data[swizzle[i]];
335 * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_ZERO)
336 * comp = 0;
337 * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_ONE)
338 * comp = 1;
339 * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_NONE)
340 * // data does not contain a channel of this format
341 *
342 * For packed formats, the swizzle gives the number of components left of
343 * the least significant bit.
344 *
345 * Compressed formats have no swizzle.
346 */
347 void
348 _mesa_get_format_swizzle(mesa_format format, uint8_t swizzle_out[4])
349 {
350 const struct gl_format_info *info = _mesa_get_format_info(format);
351 memcpy(swizzle_out, info->Swizzle, sizeof(info->Swizzle));
352 }
353
354 mesa_array_format
355 _mesa_array_format_flip_channels(mesa_array_format format)
356 {
357 int num_channels;
358 uint8_t swizzle[4];
359
360 num_channels = _mesa_array_format_get_num_channels(format);
361 _mesa_array_format_get_swizzle(format, swizzle);
362
363 if (num_channels == 1)
364 return format;
365
366 if (num_channels == 2) {
367 /* Assert that the swizzle makes sense for 2 channels */
368 for (unsigned i = 0; i < 4; i++)
369 assert(swizzle[i] != 2 && swizzle[i] != 3);
370
371 static const uint8_t flip_xy[6] = { 1, 0, 2, 3, 4, 5 };
372 _mesa_array_format_set_swizzle(&format,
373 flip_xy[swizzle[0]], flip_xy[swizzle[1]],
374 flip_xy[swizzle[2]], flip_xy[swizzle[3]]);
375 return format;
376 }
377
378 if (num_channels == 4) {
379 static const uint8_t flip[6] = { 3, 2, 1, 0, 4, 5 };
380 _mesa_array_format_set_swizzle(&format,
381 flip[swizzle[0]], flip[swizzle[1]],
382 flip[swizzle[2]], flip[swizzle[3]]);
383 return format;
384 }
385
386 unreachable("Invalid array format");
387 }
388
389 uint32_t
390 _mesa_format_to_array_format(mesa_format format)
391 {
392 const struct gl_format_info *info = _mesa_get_format_info(format);
393 if (info->ArrayFormat && !_mesa_little_endian() &&
394 info->Layout == MESA_FORMAT_LAYOUT_PACKED)
395 return _mesa_array_format_flip_channels(info->ArrayFormat);
396 else
397 return info->ArrayFormat;
398 }
399
400 static struct hash_table *format_array_format_table;
401 static once_flag format_array_format_table_exists = ONCE_FLAG_INIT;
402
403 static bool
404 array_formats_equal(const void *a, const void *b)
405 {
406 return (intptr_t)a == (intptr_t)b;
407 }
408
409 static void
410 format_array_format_table_init(void)
411 {
412 const struct gl_format_info *info;
413 mesa_array_format array_format;
414 unsigned f;
415
416 format_array_format_table = _mesa_hash_table_create(NULL, NULL,
417 array_formats_equal);
418
419 if (!format_array_format_table) {
420 _mesa_error_no_memory(__func__);
421 return;
422 }
423
424 for (f = 1; f < MESA_FORMAT_COUNT; ++f) {
425 info = _mesa_get_format_info(f);
426 if (!info->ArrayFormat)
427 continue;
428
429 if (_mesa_little_endian()) {
430 array_format = info->ArrayFormat;
431 } else {
432 array_format = _mesa_array_format_flip_channels(info->ArrayFormat);
433 }
434
435 /* This can happen and does for some of the BGR formats. Let's take
436 * the first one in the list.
437 */
438 if (_mesa_hash_table_search_pre_hashed(format_array_format_table,
439 array_format,
440 (void *)(intptr_t)array_format))
441 continue;
442
443 _mesa_hash_table_insert_pre_hashed(format_array_format_table,
444 array_format,
445 (void *)(intptr_t)array_format,
446 (void *)(intptr_t)f);
447 }
448 }
449
450 mesa_format
451 _mesa_format_from_array_format(uint32_t array_format)
452 {
453 struct hash_entry *entry;
454
455 assert(_mesa_format_is_mesa_array_format(array_format));
456
457 call_once(&format_array_format_table_exists, format_array_format_table_init);
458
459 if (!format_array_format_table) {
460 static const once_flag once_flag_init = ONCE_FLAG_INIT;
461 format_array_format_table_exists = once_flag_init;
462 return MESA_FORMAT_NONE;
463 }
464
465 entry = _mesa_hash_table_search_pre_hashed(format_array_format_table,
466 array_format,
467 (void *)(intptr_t)array_format);
468 if (entry)
469 return (intptr_t)entry->data;
470 else
471 return MESA_FORMAT_NONE;
472 }
473
474 /** Is the given format a compressed format? */
475 GLboolean
476 _mesa_is_format_compressed(mesa_format format)
477 {
478 const struct gl_format_info *info = _mesa_get_format_info(format);
479 return info->BlockWidth > 1 || info->BlockHeight > 1;
480 }
481
482
483 /**
484 * Determine if the given format represents a packed depth/stencil buffer.
485 */
486 GLboolean
487 _mesa_is_format_packed_depth_stencil(mesa_format format)
488 {
489 const struct gl_format_info *info = _mesa_get_format_info(format);
490
491 return info->BaseFormat == GL_DEPTH_STENCIL;
492 }
493
494
495 /**
496 * Is the given format a signed/unsigned integer color format?
497 */
498 GLboolean
499 _mesa_is_format_integer_color(mesa_format format)
500 {
501 const struct gl_format_info *info = _mesa_get_format_info(format);
502 return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT) &&
503 info->BaseFormat != GL_DEPTH_COMPONENT &&
504 info->BaseFormat != GL_DEPTH_STENCIL &&
505 info->BaseFormat != GL_STENCIL_INDEX;
506 }
507
508
509 /**
510 * Is the given format an unsigned integer format?
511 */
512 GLboolean
513 _mesa_is_format_unsigned(mesa_format format)
514 {
515 const struct gl_format_info *info = _mesa_get_format_info(format);
516 return _mesa_is_type_unsigned(info->DataType);
517 }
518
519
520 /**
521 * Does the given format store signed values?
522 */
523 GLboolean
524 _mesa_is_format_signed(mesa_format format)
525 {
526 if (format == MESA_FORMAT_R11G11B10_FLOAT ||
527 format == MESA_FORMAT_R9G9B9E5_FLOAT) {
528 /* these packed float formats only store unsigned values */
529 return GL_FALSE;
530 }
531 else {
532 const struct gl_format_info *info = _mesa_get_format_info(format);
533 return (info->DataType == GL_SIGNED_NORMALIZED ||
534 info->DataType == GL_INT ||
535 info->DataType == GL_FLOAT);
536 }
537 }
538
539 /**
540 * Is the given format an integer format?
541 */
542 GLboolean
543 _mesa_is_format_integer(mesa_format format)
544 {
545 const struct gl_format_info *info = _mesa_get_format_info(format);
546 return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT);
547 }
548
549
550 /**
551 * Return true if the given format is a color format.
552 */
553 GLenum
554 _mesa_is_format_color_format(mesa_format format)
555 {
556 const struct gl_format_info *info = _mesa_get_format_info(format);
557 switch (info->BaseFormat) {
558 case GL_DEPTH_COMPONENT:
559 case GL_STENCIL_INDEX:
560 case GL_DEPTH_STENCIL:
561 return false;
562 default:
563 return true;
564 }
565 }
566
567
568 /**
569 * Return color encoding for given format.
570 * \return GL_LINEAR or GL_SRGB
571 */
572 GLenum
573 _mesa_get_format_color_encoding(mesa_format format)
574 {
575 const struct gl_format_info *info = _mesa_get_format_info(format);
576 return info->IsSRGBFormat ? GL_SRGB : GL_LINEAR;
577 }
578
579
580 /**
581 * Return TRUE if format is an ETC2 compressed format specified
582 * by GL_ARB_ES3_compatibility.
583 */
584 bool
585 _mesa_is_format_etc2(mesa_format format)
586 {
587 switch (format) {
588 case MESA_FORMAT_ETC2_RGB8:
589 case MESA_FORMAT_ETC2_SRGB8:
590 case MESA_FORMAT_ETC2_RGBA8_EAC:
591 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
592 case MESA_FORMAT_ETC2_R11_EAC:
593 case MESA_FORMAT_ETC2_RG11_EAC:
594 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
595 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
596 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
597 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
598 return GL_TRUE;
599 default:
600 return GL_FALSE;
601 }
602 }
603
604
605 /**
606 * For an sRGB format, return the corresponding linear color space format.
607 * For non-sRGB formats, return the format as-is.
608 */
609 mesa_format
610 _mesa_get_srgb_format_linear(mesa_format format)
611 {
612 switch (format) {
613 case MESA_FORMAT_BGR_SRGB8:
614 format = MESA_FORMAT_BGR_UNORM8;
615 break;
616 case MESA_FORMAT_A8B8G8R8_SRGB:
617 format = MESA_FORMAT_A8B8G8R8_UNORM;
618 break;
619 case MESA_FORMAT_B8G8R8A8_SRGB:
620 format = MESA_FORMAT_B8G8R8A8_UNORM;
621 break;
622 case MESA_FORMAT_A8R8G8B8_SRGB:
623 format = MESA_FORMAT_A8R8G8B8_UNORM;
624 break;
625 case MESA_FORMAT_R8G8B8A8_SRGB:
626 format = MESA_FORMAT_R8G8B8A8_UNORM;
627 break;
628 case MESA_FORMAT_L_SRGB8:
629 format = MESA_FORMAT_L_UNORM8;
630 break;
631 case MESA_FORMAT_L8A8_SRGB:
632 format = MESA_FORMAT_L8A8_UNORM;
633 break;
634 case MESA_FORMAT_A8L8_SRGB:
635 format = MESA_FORMAT_A8L8_UNORM;
636 break;
637 case MESA_FORMAT_SRGB_DXT1:
638 format = MESA_FORMAT_RGB_DXT1;
639 break;
640 case MESA_FORMAT_SRGBA_DXT1:
641 format = MESA_FORMAT_RGBA_DXT1;
642 break;
643 case MESA_FORMAT_SRGBA_DXT3:
644 format = MESA_FORMAT_RGBA_DXT3;
645 break;
646 case MESA_FORMAT_SRGBA_DXT5:
647 format = MESA_FORMAT_RGBA_DXT5;
648 break;
649 case MESA_FORMAT_R8G8B8X8_SRGB:
650 format = MESA_FORMAT_R8G8B8X8_UNORM;
651 break;
652 case MESA_FORMAT_X8B8G8R8_SRGB:
653 format = MESA_FORMAT_X8B8G8R8_UNORM;
654 break;
655 case MESA_FORMAT_ETC2_SRGB8:
656 format = MESA_FORMAT_ETC2_RGB8;
657 break;
658 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
659 format = MESA_FORMAT_ETC2_RGBA8_EAC;
660 break;
661 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
662 format = MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1;
663 break;
664 case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
665 format = MESA_FORMAT_BPTC_RGBA_UNORM;
666 break;
667 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4:
668 format = MESA_FORMAT_RGBA_ASTC_4x4;
669 break;
670 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4:
671 format = MESA_FORMAT_RGBA_ASTC_5x4;
672 break;
673 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5:
674 format = MESA_FORMAT_RGBA_ASTC_5x5;
675 break;
676 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5:
677 format = MESA_FORMAT_RGBA_ASTC_6x5;
678 break;
679 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6:
680 format = MESA_FORMAT_RGBA_ASTC_6x6;
681 break;
682 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5:
683 format = MESA_FORMAT_RGBA_ASTC_8x5;
684 break;
685 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6:
686 format = MESA_FORMAT_RGBA_ASTC_8x6;
687 break;
688 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8:
689 format = MESA_FORMAT_RGBA_ASTC_8x8;
690 break;
691 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5:
692 format = MESA_FORMAT_RGBA_ASTC_10x5;
693 break;
694 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6:
695 format = MESA_FORMAT_RGBA_ASTC_10x6;
696 break;
697 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8:
698 format = MESA_FORMAT_RGBA_ASTC_10x8;
699 break;
700 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10:
701 format = MESA_FORMAT_RGBA_ASTC_10x10;
702 break;
703 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10:
704 format = MESA_FORMAT_RGBA_ASTC_12x10;
705 break;
706 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12:
707 format = MESA_FORMAT_RGBA_ASTC_12x12;
708 break;
709 case MESA_FORMAT_B8G8R8X8_SRGB:
710 format = MESA_FORMAT_B8G8R8X8_UNORM;
711 break;
712 case MESA_FORMAT_X8R8G8B8_SRGB:
713 format = MESA_FORMAT_X8R8G8B8_UNORM;
714 break;
715 default:
716 break;
717 }
718 return format;
719 }
720
721
722 /**
723 * If the given format is a compressed format, return a corresponding
724 * uncompressed format.
725 */
726 mesa_format
727 _mesa_get_uncompressed_format(mesa_format format)
728 {
729 switch (format) {
730 case MESA_FORMAT_RGB_FXT1:
731 return MESA_FORMAT_BGR_UNORM8;
732 case MESA_FORMAT_RGBA_FXT1:
733 return MESA_FORMAT_A8B8G8R8_UNORM;
734 case MESA_FORMAT_RGB_DXT1:
735 case MESA_FORMAT_SRGB_DXT1:
736 return MESA_FORMAT_BGR_UNORM8;
737 case MESA_FORMAT_RGBA_DXT1:
738 case MESA_FORMAT_SRGBA_DXT1:
739 return MESA_FORMAT_A8B8G8R8_UNORM;
740 case MESA_FORMAT_RGBA_DXT3:
741 case MESA_FORMAT_SRGBA_DXT3:
742 return MESA_FORMAT_A8B8G8R8_UNORM;
743 case MESA_FORMAT_RGBA_DXT5:
744 case MESA_FORMAT_SRGBA_DXT5:
745 return MESA_FORMAT_A8B8G8R8_UNORM;
746 case MESA_FORMAT_R_RGTC1_UNORM:
747 return MESA_FORMAT_R_UNORM8;
748 case MESA_FORMAT_R_RGTC1_SNORM:
749 return MESA_FORMAT_R_SNORM8;
750 case MESA_FORMAT_RG_RGTC2_UNORM:
751 return MESA_FORMAT_R8G8_UNORM;
752 case MESA_FORMAT_RG_RGTC2_SNORM:
753 return MESA_FORMAT_R8G8_SNORM;
754 case MESA_FORMAT_L_LATC1_UNORM:
755 return MESA_FORMAT_L_UNORM8;
756 case MESA_FORMAT_L_LATC1_SNORM:
757 return MESA_FORMAT_L_SNORM8;
758 case MESA_FORMAT_LA_LATC2_UNORM:
759 return MESA_FORMAT_L8A8_UNORM;
760 case MESA_FORMAT_LA_LATC2_SNORM:
761 return MESA_FORMAT_L8A8_SNORM;
762 case MESA_FORMAT_ETC1_RGB8:
763 case MESA_FORMAT_ETC2_RGB8:
764 case MESA_FORMAT_ETC2_SRGB8:
765 return MESA_FORMAT_BGR_UNORM8;
766 case MESA_FORMAT_ETC2_RGBA8_EAC:
767 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
768 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
769 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
770 return MESA_FORMAT_A8B8G8R8_UNORM;
771 case MESA_FORMAT_ETC2_R11_EAC:
772 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
773 return MESA_FORMAT_R_UNORM16;
774 case MESA_FORMAT_ETC2_RG11_EAC:
775 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
776 return MESA_FORMAT_R16G16_UNORM;
777 case MESA_FORMAT_BPTC_RGBA_UNORM:
778 case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
779 return MESA_FORMAT_A8B8G8R8_UNORM;
780 case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
781 case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
782 return MESA_FORMAT_RGB_FLOAT32;
783 default:
784 #ifdef DEBUG
785 assert(!_mesa_is_format_compressed(format));
786 #endif
787 return format;
788 }
789 }
790
791
792 GLuint
793 _mesa_format_num_components(mesa_format format)
794 {
795 const struct gl_format_info *info = _mesa_get_format_info(format);
796 return ((info->RedBits > 0) +
797 (info->GreenBits > 0) +
798 (info->BlueBits > 0) +
799 (info->AlphaBits > 0) +
800 (info->LuminanceBits > 0) +
801 (info->IntensityBits > 0) +
802 (info->DepthBits > 0) +
803 (info->StencilBits > 0));
804 }
805
806
807 /**
808 * Returns true if a color format has data stored in the R/G/B/A channels,
809 * given an index from 0 to 3.
810 */
811 bool
812 _mesa_format_has_color_component(mesa_format format, int component)
813 {
814 const struct gl_format_info *info = _mesa_get_format_info(format);
815
816 assert(info->BaseFormat != GL_DEPTH_COMPONENT &&
817 info->BaseFormat != GL_DEPTH_STENCIL &&
818 info->BaseFormat != GL_STENCIL_INDEX);
819
820 switch (component) {
821 case 0:
822 return (info->RedBits + info->IntensityBits + info->LuminanceBits) > 0;
823 case 1:
824 return (info->GreenBits + info->IntensityBits + info->LuminanceBits) > 0;
825 case 2:
826 return (info->BlueBits + info->IntensityBits + info->LuminanceBits) > 0;
827 case 3:
828 return (info->AlphaBits + info->IntensityBits) > 0;
829 default:
830 assert(!"Invalid color component: must be 0..3");
831 return false;
832 }
833 }
834
835
836 /**
837 * Return number of bytes needed to store an image of the given size
838 * in the given format.
839 */
840 GLuint
841 _mesa_format_image_size(mesa_format format, GLsizei width,
842 GLsizei height, GLsizei depth)
843 {
844 const struct gl_format_info *info = _mesa_get_format_info(format);
845 /* Strictly speaking, a conditional isn't needed here */
846 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
847 /* compressed format (2D only for now) */
848 const GLuint bw = info->BlockWidth, bh = info->BlockHeight;
849 const GLuint wblocks = (width + bw - 1) / bw;
850 const GLuint hblocks = (height + bh - 1) / bh;
851 const GLuint sz = wblocks * hblocks * info->BytesPerBlock;
852 return sz * depth;
853 }
854 else {
855 /* non-compressed */
856 const GLuint sz = width * height * depth * info->BytesPerBlock;
857 return sz;
858 }
859 }
860
861
862 /**
863 * Same as _mesa_format_image_size() but returns a 64-bit value to
864 * accommodate very large textures.
865 */
866 uint64_t
867 _mesa_format_image_size64(mesa_format format, GLsizei width,
868 GLsizei height, GLsizei depth)
869 {
870 const struct gl_format_info *info = _mesa_get_format_info(format);
871 /* Strictly speaking, a conditional isn't needed here */
872 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
873 /* compressed format (2D only for now) */
874 const uint64_t bw = info->BlockWidth, bh = info->BlockHeight;
875 const uint64_t wblocks = (width + bw - 1) / bw;
876 const uint64_t hblocks = (height + bh - 1) / bh;
877 const uint64_t sz = wblocks * hblocks * info->BytesPerBlock;
878 return sz * depth;
879 }
880 else {
881 /* non-compressed */
882 const uint64_t sz = ((uint64_t) width *
883 (uint64_t) height *
884 (uint64_t) depth *
885 info->BytesPerBlock);
886 return sz;
887 }
888 }
889
890
891
892 GLint
893 _mesa_format_row_stride(mesa_format format, GLsizei width)
894 {
895 const struct gl_format_info *info = _mesa_get_format_info(format);
896 /* Strictly speaking, a conditional isn't needed here */
897 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
898 /* compressed format */
899 const GLuint bw = info->BlockWidth;
900 const GLuint wblocks = (width + bw - 1) / bw;
901 const GLint stride = wblocks * info->BytesPerBlock;
902 return stride;
903 }
904 else {
905 const GLint stride = width * info->BytesPerBlock;
906 return stride;
907 }
908 }
909
910
911
912 /**
913 * Return datatype and number of components per texel for the given
914 * uncompressed mesa_format. Only used for mipmap generation code.
915 */
916 void
917 _mesa_uncompressed_format_to_type_and_comps(mesa_format format,
918 GLenum *datatype, GLuint *comps)
919 {
920 switch (format) {
921 case MESA_FORMAT_A8B8G8R8_UNORM:
922 case MESA_FORMAT_R8G8B8A8_UNORM:
923 case MESA_FORMAT_B8G8R8A8_UNORM:
924 case MESA_FORMAT_A8R8G8B8_UNORM:
925 case MESA_FORMAT_X8B8G8R8_UNORM:
926 case MESA_FORMAT_R8G8B8X8_UNORM:
927 case MESA_FORMAT_B8G8R8X8_UNORM:
928 case MESA_FORMAT_X8R8G8B8_UNORM:
929 *datatype = GL_UNSIGNED_BYTE;
930 *comps = 4;
931 return;
932 case MESA_FORMAT_BGR_UNORM8:
933 case MESA_FORMAT_RGB_UNORM8:
934 *datatype = GL_UNSIGNED_BYTE;
935 *comps = 3;
936 return;
937 case MESA_FORMAT_B5G6R5_UNORM:
938 case MESA_FORMAT_R5G6B5_UNORM:
939 case MESA_FORMAT_B5G6R5_UINT:
940 case MESA_FORMAT_R5G6B5_UINT:
941 *datatype = GL_UNSIGNED_SHORT_5_6_5;
942 *comps = 3;
943 return;
944
945 case MESA_FORMAT_B4G4R4A4_UNORM:
946 case MESA_FORMAT_A4R4G4B4_UNORM:
947 case MESA_FORMAT_B4G4R4X4_UNORM:
948 case MESA_FORMAT_B4G4R4A4_UINT:
949 case MESA_FORMAT_A4R4G4B4_UINT:
950 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
951 *comps = 4;
952 return;
953
954 case MESA_FORMAT_B5G5R5A1_UNORM:
955 case MESA_FORMAT_A1R5G5B5_UNORM:
956 case MESA_FORMAT_B5G5R5X1_UNORM:
957 case MESA_FORMAT_B5G5R5A1_UINT:
958 case MESA_FORMAT_A1R5G5B5_UINT:
959 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
960 *comps = 4;
961 return;
962
963 case MESA_FORMAT_B10G10R10A2_UNORM:
964 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
965 *comps = 4;
966 return;
967
968 case MESA_FORMAT_A1B5G5R5_UNORM:
969 case MESA_FORMAT_A1B5G5R5_UINT:
970 *datatype = GL_UNSIGNED_SHORT_5_5_5_1;
971 *comps = 4;
972 return;
973
974 case MESA_FORMAT_L4A4_UNORM:
975 *datatype = MESA_UNSIGNED_BYTE_4_4;
976 *comps = 2;
977 return;
978
979 case MESA_FORMAT_L8A8_UNORM:
980 case MESA_FORMAT_A8L8_UNORM:
981 case MESA_FORMAT_R8G8_UNORM:
982 case MESA_FORMAT_G8R8_UNORM:
983 *datatype = GL_UNSIGNED_BYTE;
984 *comps = 2;
985 return;
986
987 case MESA_FORMAT_L16A16_UNORM:
988 case MESA_FORMAT_A16L16_UNORM:
989 case MESA_FORMAT_R16G16_UNORM:
990 case MESA_FORMAT_G16R16_UNORM:
991 *datatype = GL_UNSIGNED_SHORT;
992 *comps = 2;
993 return;
994
995 case MESA_FORMAT_R_UNORM16:
996 case MESA_FORMAT_A_UNORM16:
997 case MESA_FORMAT_L_UNORM16:
998 case MESA_FORMAT_I_UNORM16:
999 *datatype = GL_UNSIGNED_SHORT;
1000 *comps = 1;
1001 return;
1002
1003 case MESA_FORMAT_R3G3B2_UNORM:
1004 case MESA_FORMAT_R3G3B2_UINT:
1005 *datatype = GL_UNSIGNED_BYTE_2_3_3_REV;
1006 *comps = 3;
1007 return;
1008 case MESA_FORMAT_A4B4G4R4_UNORM:
1009 case MESA_FORMAT_A4B4G4R4_UINT:
1010 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
1011 *comps = 4;
1012 return;
1013
1014 case MESA_FORMAT_R4G4B4A4_UNORM:
1015 case MESA_FORMAT_R4G4B4A4_UINT:
1016 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
1017 *comps = 4;
1018 return;
1019 case MESA_FORMAT_R5G5B5A1_UNORM:
1020 case MESA_FORMAT_R5G5B5A1_UINT:
1021 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1022 *comps = 4;
1023 return;
1024 case MESA_FORMAT_A2B10G10R10_UNORM:
1025 case MESA_FORMAT_A2B10G10R10_UINT:
1026 *datatype = GL_UNSIGNED_INT_10_10_10_2;
1027 *comps = 4;
1028 return;
1029 case MESA_FORMAT_A2R10G10B10_UNORM:
1030 case MESA_FORMAT_A2R10G10B10_UINT:
1031 *datatype = GL_UNSIGNED_INT_10_10_10_2;
1032 *comps = 4;
1033 return;
1034
1035 case MESA_FORMAT_B2G3R3_UNORM:
1036 case MESA_FORMAT_B2G3R3_UINT:
1037 *datatype = GL_UNSIGNED_BYTE_3_3_2;
1038 *comps = 3;
1039 return;
1040
1041 case MESA_FORMAT_A_UNORM8:
1042 case MESA_FORMAT_L_UNORM8:
1043 case MESA_FORMAT_I_UNORM8:
1044 case MESA_FORMAT_R_UNORM8:
1045 case MESA_FORMAT_S_UINT8:
1046 *datatype = GL_UNSIGNED_BYTE;
1047 *comps = 1;
1048 return;
1049
1050 case MESA_FORMAT_YCBCR:
1051 case MESA_FORMAT_YCBCR_REV:
1052 *datatype = GL_UNSIGNED_SHORT;
1053 *comps = 2;
1054 return;
1055
1056 case MESA_FORMAT_S8_UINT_Z24_UNORM:
1057 *datatype = GL_UNSIGNED_INT_24_8_MESA;
1058 *comps = 2;
1059 return;
1060
1061 case MESA_FORMAT_Z24_UNORM_S8_UINT:
1062 *datatype = GL_UNSIGNED_INT_8_24_REV_MESA;
1063 *comps = 2;
1064 return;
1065
1066 case MESA_FORMAT_Z_UNORM16:
1067 *datatype = GL_UNSIGNED_SHORT;
1068 *comps = 1;
1069 return;
1070
1071 case MESA_FORMAT_Z24_UNORM_X8_UINT:
1072 *datatype = GL_UNSIGNED_INT;
1073 *comps = 1;
1074 return;
1075
1076 case MESA_FORMAT_X8_UINT_Z24_UNORM:
1077 *datatype = GL_UNSIGNED_INT;
1078 *comps = 1;
1079 return;
1080
1081 case MESA_FORMAT_Z_UNORM32:
1082 *datatype = GL_UNSIGNED_INT;
1083 *comps = 1;
1084 return;
1085
1086 case MESA_FORMAT_Z_FLOAT32:
1087 *datatype = GL_FLOAT;
1088 *comps = 1;
1089 return;
1090
1091 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1092 *datatype = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
1093 *comps = 1;
1094 return;
1095
1096 case MESA_FORMAT_R_SNORM8:
1097 case MESA_FORMAT_A_SNORM8:
1098 case MESA_FORMAT_L_SNORM8:
1099 case MESA_FORMAT_I_SNORM8:
1100 *datatype = GL_BYTE;
1101 *comps = 1;
1102 return;
1103 case MESA_FORMAT_R8G8_SNORM:
1104 case MESA_FORMAT_L8A8_SNORM:
1105 case MESA_FORMAT_A8L8_SNORM:
1106 *datatype = GL_BYTE;
1107 *comps = 2;
1108 return;
1109 case MESA_FORMAT_A8B8G8R8_SNORM:
1110 case MESA_FORMAT_R8G8B8A8_SNORM:
1111 case MESA_FORMAT_X8B8G8R8_SNORM:
1112 *datatype = GL_BYTE;
1113 *comps = 4;
1114 return;
1115
1116 case MESA_FORMAT_RGBA_UNORM16:
1117 *datatype = GL_UNSIGNED_SHORT;
1118 *comps = 4;
1119 return;
1120
1121 case MESA_FORMAT_R_SNORM16:
1122 case MESA_FORMAT_A_SNORM16:
1123 case MESA_FORMAT_L_SNORM16:
1124 case MESA_FORMAT_I_SNORM16:
1125 *datatype = GL_SHORT;
1126 *comps = 1;
1127 return;
1128 case MESA_FORMAT_R16G16_SNORM:
1129 case MESA_FORMAT_LA_SNORM16:
1130 *datatype = GL_SHORT;
1131 *comps = 2;
1132 return;
1133 case MESA_FORMAT_RGB_SNORM16:
1134 *datatype = GL_SHORT;
1135 *comps = 3;
1136 return;
1137 case MESA_FORMAT_RGBA_SNORM16:
1138 *datatype = GL_SHORT;
1139 *comps = 4;
1140 return;
1141
1142 case MESA_FORMAT_BGR_SRGB8:
1143 *datatype = GL_UNSIGNED_BYTE;
1144 *comps = 3;
1145 return;
1146 case MESA_FORMAT_A8B8G8R8_SRGB:
1147 case MESA_FORMAT_B8G8R8A8_SRGB:
1148 case MESA_FORMAT_A8R8G8B8_SRGB:
1149 case MESA_FORMAT_R8G8B8A8_SRGB:
1150 *datatype = GL_UNSIGNED_BYTE;
1151 *comps = 4;
1152 return;
1153 case MESA_FORMAT_L_SRGB8:
1154 *datatype = GL_UNSIGNED_BYTE;
1155 *comps = 1;
1156 return;
1157 case MESA_FORMAT_L8A8_SRGB:
1158 case MESA_FORMAT_A8L8_SRGB:
1159 *datatype = GL_UNSIGNED_BYTE;
1160 *comps = 2;
1161 return;
1162
1163 case MESA_FORMAT_RGBA_FLOAT32:
1164 *datatype = GL_FLOAT;
1165 *comps = 4;
1166 return;
1167 case MESA_FORMAT_RGBA_FLOAT16:
1168 *datatype = GL_HALF_FLOAT_ARB;
1169 *comps = 4;
1170 return;
1171 case MESA_FORMAT_RGB_FLOAT32:
1172 *datatype = GL_FLOAT;
1173 *comps = 3;
1174 return;
1175 case MESA_FORMAT_RGB_FLOAT16:
1176 *datatype = GL_HALF_FLOAT_ARB;
1177 *comps = 3;
1178 return;
1179 case MESA_FORMAT_LA_FLOAT32:
1180 case MESA_FORMAT_RG_FLOAT32:
1181 *datatype = GL_FLOAT;
1182 *comps = 2;
1183 return;
1184 case MESA_FORMAT_LA_FLOAT16:
1185 case MESA_FORMAT_RG_FLOAT16:
1186 *datatype = GL_HALF_FLOAT_ARB;
1187 *comps = 2;
1188 return;
1189 case MESA_FORMAT_A_FLOAT32:
1190 case MESA_FORMAT_L_FLOAT32:
1191 case MESA_FORMAT_I_FLOAT32:
1192 case MESA_FORMAT_R_FLOAT32:
1193 *datatype = GL_FLOAT;
1194 *comps = 1;
1195 return;
1196 case MESA_FORMAT_A_FLOAT16:
1197 case MESA_FORMAT_L_FLOAT16:
1198 case MESA_FORMAT_I_FLOAT16:
1199 case MESA_FORMAT_R_FLOAT16:
1200 *datatype = GL_HALF_FLOAT_ARB;
1201 *comps = 1;
1202 return;
1203
1204 case MESA_FORMAT_A_UINT8:
1205 case MESA_FORMAT_L_UINT8:
1206 case MESA_FORMAT_I_UINT8:
1207 *datatype = GL_UNSIGNED_BYTE;
1208 *comps = 1;
1209 return;
1210 case MESA_FORMAT_LA_UINT8:
1211 *datatype = GL_UNSIGNED_BYTE;
1212 *comps = 2;
1213 return;
1214
1215 case MESA_FORMAT_A_UINT16:
1216 case MESA_FORMAT_L_UINT16:
1217 case MESA_FORMAT_I_UINT16:
1218 *datatype = GL_UNSIGNED_SHORT;
1219 *comps = 1;
1220 return;
1221 case MESA_FORMAT_LA_UINT16:
1222 *datatype = GL_UNSIGNED_SHORT;
1223 *comps = 2;
1224 return;
1225 case MESA_FORMAT_A_UINT32:
1226 case MESA_FORMAT_L_UINT32:
1227 case MESA_FORMAT_I_UINT32:
1228 *datatype = GL_UNSIGNED_INT;
1229 *comps = 1;
1230 return;
1231 case MESA_FORMAT_LA_UINT32:
1232 *datatype = GL_UNSIGNED_INT;
1233 *comps = 2;
1234 return;
1235 case MESA_FORMAT_A_SINT8:
1236 case MESA_FORMAT_L_SINT8:
1237 case MESA_FORMAT_I_SINT8:
1238 *datatype = GL_BYTE;
1239 *comps = 1;
1240 return;
1241 case MESA_FORMAT_LA_SINT8:
1242 *datatype = GL_BYTE;
1243 *comps = 2;
1244 return;
1245
1246 case MESA_FORMAT_A_SINT16:
1247 case MESA_FORMAT_L_SINT16:
1248 case MESA_FORMAT_I_SINT16:
1249 *datatype = GL_SHORT;
1250 *comps = 1;
1251 return;
1252 case MESA_FORMAT_LA_SINT16:
1253 *datatype = GL_SHORT;
1254 *comps = 2;
1255 return;
1256
1257 case MESA_FORMAT_A_SINT32:
1258 case MESA_FORMAT_L_SINT32:
1259 case MESA_FORMAT_I_SINT32:
1260 *datatype = GL_INT;
1261 *comps = 1;
1262 return;
1263 case MESA_FORMAT_LA_SINT32:
1264 *datatype = GL_INT;
1265 *comps = 2;
1266 return;
1267
1268 case MESA_FORMAT_R_SINT8:
1269 *datatype = GL_BYTE;
1270 *comps = 1;
1271 return;
1272 case MESA_FORMAT_RG_SINT8:
1273 *datatype = GL_BYTE;
1274 *comps = 2;
1275 return;
1276 case MESA_FORMAT_RGB_SINT8:
1277 *datatype = GL_BYTE;
1278 *comps = 3;
1279 return;
1280 case MESA_FORMAT_RGBA_SINT8:
1281 *datatype = GL_BYTE;
1282 *comps = 4;
1283 return;
1284 case MESA_FORMAT_R_SINT16:
1285 *datatype = GL_SHORT;
1286 *comps = 1;
1287 return;
1288 case MESA_FORMAT_RG_SINT16:
1289 *datatype = GL_SHORT;
1290 *comps = 2;
1291 return;
1292 case MESA_FORMAT_RGB_SINT16:
1293 *datatype = GL_SHORT;
1294 *comps = 3;
1295 return;
1296 case MESA_FORMAT_RGBA_SINT16:
1297 *datatype = GL_SHORT;
1298 *comps = 4;
1299 return;
1300 case MESA_FORMAT_R_SINT32:
1301 *datatype = GL_INT;
1302 *comps = 1;
1303 return;
1304 case MESA_FORMAT_RG_SINT32:
1305 *datatype = GL_INT;
1306 *comps = 2;
1307 return;
1308 case MESA_FORMAT_RGB_SINT32:
1309 *datatype = GL_INT;
1310 *comps = 3;
1311 return;
1312 case MESA_FORMAT_RGBA_SINT32:
1313 *datatype = GL_INT;
1314 *comps = 4;
1315 return;
1316
1317 /**
1318 * \name Non-normalized unsigned integer formats.
1319 */
1320 case MESA_FORMAT_R_UINT8:
1321 *datatype = GL_UNSIGNED_BYTE;
1322 *comps = 1;
1323 return;
1324 case MESA_FORMAT_RG_UINT8:
1325 *datatype = GL_UNSIGNED_BYTE;
1326 *comps = 2;
1327 return;
1328 case MESA_FORMAT_RGB_UINT8:
1329 *datatype = GL_UNSIGNED_BYTE;
1330 *comps = 3;
1331 return;
1332 case MESA_FORMAT_RGBA_UINT8:
1333 *datatype = GL_UNSIGNED_BYTE;
1334 *comps = 4;
1335 return;
1336 case MESA_FORMAT_R_UINT16:
1337 *datatype = GL_UNSIGNED_SHORT;
1338 *comps = 1;
1339 return;
1340 case MESA_FORMAT_RG_UINT16:
1341 *datatype = GL_UNSIGNED_SHORT;
1342 *comps = 2;
1343 return;
1344 case MESA_FORMAT_RGB_UINT16:
1345 *datatype = GL_UNSIGNED_SHORT;
1346 *comps = 3;
1347 return;
1348 case MESA_FORMAT_RGBA_UINT16:
1349 *datatype = GL_UNSIGNED_SHORT;
1350 *comps = 4;
1351 return;
1352 case MESA_FORMAT_R_UINT32:
1353 *datatype = GL_UNSIGNED_INT;
1354 *comps = 1;
1355 return;
1356 case MESA_FORMAT_RG_UINT32:
1357 *datatype = GL_UNSIGNED_INT;
1358 *comps = 2;
1359 return;
1360 case MESA_FORMAT_RGB_UINT32:
1361 *datatype = GL_UNSIGNED_INT;
1362 *comps = 3;
1363 return;
1364 case MESA_FORMAT_RGBA_UINT32:
1365 *datatype = GL_UNSIGNED_INT;
1366 *comps = 4;
1367 return;
1368
1369 case MESA_FORMAT_R9G9B9E5_FLOAT:
1370 *datatype = GL_UNSIGNED_INT_5_9_9_9_REV;
1371 *comps = 3;
1372 return;
1373
1374 case MESA_FORMAT_R11G11B10_FLOAT:
1375 *datatype = GL_UNSIGNED_INT_10F_11F_11F_REV;
1376 *comps = 3;
1377 return;
1378
1379 case MESA_FORMAT_B10G10R10A2_UINT:
1380 case MESA_FORMAT_R10G10B10A2_UINT:
1381 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1382 *comps = 4;
1383 return;
1384
1385 case MESA_FORMAT_R8G8B8X8_SRGB:
1386 case MESA_FORMAT_X8B8G8R8_SRGB:
1387 case MESA_FORMAT_RGBX_UINT8:
1388 *datatype = GL_UNSIGNED_BYTE;
1389 *comps = 4;
1390 return;
1391
1392 case MESA_FORMAT_R8G8B8X8_SNORM:
1393 case MESA_FORMAT_RGBX_SINT8:
1394 *datatype = GL_BYTE;
1395 *comps = 4;
1396 return;
1397
1398 case MESA_FORMAT_B10G10R10X2_UNORM:
1399 case MESA_FORMAT_R10G10B10X2_UNORM:
1400 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1401 *comps = 4;
1402 return;
1403
1404 case MESA_FORMAT_RGBX_UNORM16:
1405 case MESA_FORMAT_RGBX_UINT16:
1406 *datatype = GL_UNSIGNED_SHORT;
1407 *comps = 4;
1408 return;
1409
1410 case MESA_FORMAT_RGBX_SNORM16:
1411 case MESA_FORMAT_RGBX_SINT16:
1412 *datatype = GL_SHORT;
1413 *comps = 4;
1414 return;
1415
1416 case MESA_FORMAT_RGBX_FLOAT16:
1417 *datatype = GL_HALF_FLOAT;
1418 *comps = 4;
1419 return;
1420
1421 case MESA_FORMAT_RGBX_FLOAT32:
1422 *datatype = GL_FLOAT;
1423 *comps = 4;
1424 return;
1425
1426 case MESA_FORMAT_RGBX_UINT32:
1427 *datatype = GL_UNSIGNED_INT;
1428 *comps = 4;
1429 return;
1430
1431 case MESA_FORMAT_RGBX_SINT32:
1432 *datatype = GL_INT;
1433 *comps = 4;
1434 return;
1435
1436 case MESA_FORMAT_R10G10B10A2_UNORM:
1437 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1438 *comps = 4;
1439 return;
1440
1441 case MESA_FORMAT_G8R8_SNORM:
1442 *datatype = GL_BYTE;
1443 *comps = 2;
1444 return;
1445
1446 case MESA_FORMAT_G16R16_SNORM:
1447 *datatype = GL_SHORT;
1448 *comps = 2;
1449 return;
1450
1451 case MESA_FORMAT_B8G8R8X8_SRGB:
1452 case MESA_FORMAT_X8R8G8B8_SRGB:
1453 *datatype = GL_UNSIGNED_BYTE;
1454 *comps = 4;
1455 return;
1456
1457 case MESA_FORMAT_COUNT:
1458 assert(0);
1459 return;
1460 default:
1461 /* Warn if any formats are not handled */
1462 _mesa_problem(NULL, "bad format %s in _mesa_uncompressed_format_to_type_and_comps",
1463 _mesa_get_format_name(format));
1464 assert(format == MESA_FORMAT_NONE ||
1465 _mesa_is_format_compressed(format));
1466 *datatype = 0;
1467 *comps = 1;
1468 }
1469 }
1470
1471 /**
1472 * Check if a mesa_format exactly matches a GL format/type combination
1473 * such that we can use memcpy() from one to the other.
1474 * \param mesa_format a MESA_FORMAT_x value
1475 * \param format the user-specified image format
1476 * \param type the user-specified image datatype
1477 * \param swapBytes typically the current pixel pack/unpack byteswap state
1478 * \param[out] error GL_NO_ERROR if format is an expected input.
1479 * GL_INVALID_ENUM if format is an unexpected input.
1480 * \return GL_TRUE if the formats match, GL_FALSE otherwise.
1481 */
1482 GLboolean
1483 _mesa_format_matches_format_and_type(mesa_format mesa_format,
1484 GLenum format, GLenum type,
1485 GLboolean swapBytes, GLenum *error)
1486 {
1487 const GLboolean littleEndian = _mesa_little_endian();
1488 if (error)
1489 *error = GL_NO_ERROR;
1490
1491 /* Note: When reading a GL format/type combination, the format lists channel
1492 * assignments from most significant channel in the type to least
1493 * significant. A type with _REV indicates that the assignments are
1494 * swapped, so they are listed from least significant to most significant.
1495 *
1496 * Compressed formats will fall through and return GL_FALSE.
1497 *
1498 * For sanity, please keep this switch statement ordered the same as the
1499 * enums in formats.h.
1500 */
1501
1502 switch (mesa_format) {
1503
1504 case MESA_FORMAT_NONE:
1505 case MESA_FORMAT_COUNT:
1506 return GL_FALSE;
1507
1508 case MESA_FORMAT_A8B8G8R8_UNORM:
1509 case MESA_FORMAT_A8B8G8R8_SRGB:
1510 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
1511 return GL_TRUE;
1512
1513 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8_REV && swapBytes)
1514 return GL_TRUE;
1515
1516 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE && !littleEndian)
1517 return GL_TRUE;
1518
1519 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8_REV
1520 && !swapBytes)
1521 return GL_TRUE;
1522
1523 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8
1524 && swapBytes)
1525 return GL_TRUE;
1526
1527 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_BYTE && littleEndian)
1528 return GL_TRUE;
1529
1530 return GL_FALSE;
1531
1532 case MESA_FORMAT_R8G8B8A8_UNORM:
1533 case MESA_FORMAT_R8G8B8A8_SRGB:
1534 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1535 !swapBytes)
1536 return GL_TRUE;
1537
1538 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1539 return GL_TRUE;
1540
1541 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE && littleEndian)
1542 return GL_TRUE;
1543
1544 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8 &&
1545 !swapBytes)
1546 return GL_TRUE;
1547
1548 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1549 swapBytes)
1550 return GL_TRUE;
1551
1552 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_BYTE && !littleEndian)
1553 return GL_TRUE;
1554
1555 return GL_FALSE;
1556
1557 case MESA_FORMAT_B8G8R8A8_UNORM:
1558 case MESA_FORMAT_B8G8R8A8_SRGB:
1559 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1560 !swapBytes)
1561 return GL_TRUE;
1562
1563 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1564 return GL_TRUE;
1565
1566 if (format == GL_BGRA && type == GL_UNSIGNED_BYTE && littleEndian)
1567 return GL_TRUE;
1568
1569 return GL_FALSE;
1570
1571 case MESA_FORMAT_A8R8G8B8_UNORM:
1572 case MESA_FORMAT_A8R8G8B8_SRGB:
1573 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
1574 return GL_TRUE;
1575
1576 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1577 swapBytes)
1578 return GL_TRUE;
1579
1580 if (format == GL_BGRA && type == GL_UNSIGNED_BYTE && !littleEndian)
1581 return GL_TRUE;
1582
1583 return GL_FALSE;
1584
1585 case MESA_FORMAT_X8B8G8R8_UNORM:
1586 case MESA_FORMAT_R8G8B8X8_UNORM:
1587 return GL_FALSE;
1588
1589 case MESA_FORMAT_B8G8R8X8_UNORM:
1590 case MESA_FORMAT_X8R8G8B8_UNORM:
1591 return GL_FALSE;
1592
1593 case MESA_FORMAT_BGR_UNORM8:
1594 case MESA_FORMAT_BGR_SRGB8:
1595 return format == GL_BGR && type == GL_UNSIGNED_BYTE && littleEndian;
1596
1597 case MESA_FORMAT_RGB_UNORM8:
1598 return format == GL_RGB && type == GL_UNSIGNED_BYTE && littleEndian;
1599
1600 case MESA_FORMAT_B5G6R5_UNORM:
1601 return ((format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5) ||
1602 (format == GL_BGR && type == GL_UNSIGNED_SHORT_5_6_5_REV)) &&
1603 !swapBytes;
1604
1605 case MESA_FORMAT_R5G6B5_UNORM:
1606 return ((format == GL_BGR && type == GL_UNSIGNED_SHORT_5_6_5) ||
1607 (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5_REV)) &&
1608 !swapBytes;
1609
1610 case MESA_FORMAT_B4G4R4A4_UNORM:
1611 return format == GL_BGRA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV &&
1612 !swapBytes;
1613
1614 case MESA_FORMAT_A4R4G4B4_UNORM:
1615 return GL_FALSE;
1616
1617 case MESA_FORMAT_A1B5G5R5_UNORM:
1618 return format == GL_RGBA && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1619 !swapBytes;
1620
1621 case MESA_FORMAT_B5G5R5A1_UNORM:
1622 return format == GL_BGRA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV &&
1623 !swapBytes;
1624
1625 case MESA_FORMAT_A1R5G5B5_UNORM:
1626 return format == GL_BGRA && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1627 !swapBytes;
1628
1629 case MESA_FORMAT_L4A4_UNORM:
1630 return GL_FALSE;
1631 case MESA_FORMAT_L8A8_UNORM:
1632 case MESA_FORMAT_L8A8_SRGB:
1633 return format == GL_LUMINANCE_ALPHA && type == GL_UNSIGNED_BYTE && littleEndian;
1634 case MESA_FORMAT_A8L8_UNORM:
1635 case MESA_FORMAT_A8L8_SRGB:
1636 return GL_FALSE;
1637
1638 case MESA_FORMAT_L16A16_UNORM:
1639 return format == GL_LUMINANCE_ALPHA && type == GL_UNSIGNED_SHORT && littleEndian && !swapBytes;
1640 case MESA_FORMAT_A16L16_UNORM:
1641 return GL_FALSE;
1642
1643 case MESA_FORMAT_B2G3R3_UNORM:
1644 return format == GL_RGB && type == GL_UNSIGNED_BYTE_3_3_2;
1645
1646 case MESA_FORMAT_R3G3B2_UNORM:
1647 return format == GL_RGB && type == GL_UNSIGNED_BYTE_2_3_3_REV;
1648
1649 case MESA_FORMAT_A4B4G4R4_UNORM:
1650 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
1651 return GL_TRUE;
1652
1653 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && swapBytes)
1654 return GL_TRUE;
1655
1656 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !swapBytes)
1657 return GL_TRUE;
1658
1659 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4 && swapBytes)
1660 return GL_TRUE;
1661
1662 return GL_FALSE;
1663
1664 case MESA_FORMAT_R4G4B4A4_UNORM:
1665 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
1666 return GL_TRUE;
1667
1668 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && swapBytes)
1669 return GL_TRUE;
1670
1671 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !swapBytes)
1672 return GL_TRUE;
1673
1674 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4 && swapBytes)
1675 return GL_TRUE;
1676
1677 return GL_FALSE;
1678
1679 case MESA_FORMAT_R5G5B5A1_UNORM:
1680 return format == GL_RGBA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV;
1681
1682 case MESA_FORMAT_A2B10G10R10_UNORM:
1683 return format == GL_RGBA && type == GL_UNSIGNED_INT_10_10_10_2;
1684
1685 case MESA_FORMAT_A2B10G10R10_UINT:
1686 return format == GL_RGBA_INTEGER_EXT && type == GL_UNSIGNED_INT_10_10_10_2;
1687
1688 case MESA_FORMAT_A2R10G10B10_UNORM:
1689 return format == GL_BGRA && type == GL_UNSIGNED_INT_10_10_10_2;
1690
1691 case MESA_FORMAT_A2R10G10B10_UINT:
1692 return format == GL_BGRA_INTEGER_EXT && type == GL_UNSIGNED_INT_10_10_10_2;
1693
1694 case MESA_FORMAT_A_UNORM8:
1695 return format == GL_ALPHA && type == GL_UNSIGNED_BYTE;
1696 case MESA_FORMAT_A_UNORM16:
1697 return format == GL_ALPHA && type == GL_UNSIGNED_SHORT && !swapBytes;
1698 case MESA_FORMAT_L_UNORM8:
1699 case MESA_FORMAT_L_SRGB8:
1700 return format == GL_LUMINANCE && type == GL_UNSIGNED_BYTE;
1701 case MESA_FORMAT_L_UNORM16:
1702 return format == GL_LUMINANCE && type == GL_UNSIGNED_SHORT && !swapBytes;
1703 case MESA_FORMAT_I_UNORM8:
1704 return format == GL_RED && type == GL_UNSIGNED_BYTE;
1705 case MESA_FORMAT_I_UNORM16:
1706 return format == GL_RED && type == GL_UNSIGNED_SHORT && !swapBytes;
1707
1708 case MESA_FORMAT_YCBCR:
1709 return format == GL_YCBCR_MESA &&
1710 ((type == GL_UNSIGNED_SHORT_8_8_MESA && littleEndian != swapBytes) ||
1711 (type == GL_UNSIGNED_SHORT_8_8_REV_MESA && littleEndian == swapBytes));
1712 case MESA_FORMAT_YCBCR_REV:
1713 return format == GL_YCBCR_MESA &&
1714 ((type == GL_UNSIGNED_SHORT_8_8_MESA && littleEndian == swapBytes) ||
1715 (type == GL_UNSIGNED_SHORT_8_8_REV_MESA && littleEndian != swapBytes));
1716
1717 case MESA_FORMAT_R_UNORM8:
1718 return format == GL_RED && type == GL_UNSIGNED_BYTE;
1719 case MESA_FORMAT_R8G8_UNORM:
1720 return format == GL_RG && type == GL_UNSIGNED_BYTE && littleEndian;
1721 case MESA_FORMAT_G8R8_UNORM:
1722 return GL_FALSE;
1723
1724 case MESA_FORMAT_R_UNORM16:
1725 return format == GL_RED && type == GL_UNSIGNED_SHORT &&
1726 !swapBytes;
1727 case MESA_FORMAT_R16G16_UNORM:
1728 return format == GL_RG && type == GL_UNSIGNED_SHORT && littleEndian &&
1729 !swapBytes;
1730 case MESA_FORMAT_G16R16_UNORM:
1731 return GL_FALSE;
1732
1733 case MESA_FORMAT_B10G10R10A2_UNORM:
1734 return format == GL_BGRA && type == GL_UNSIGNED_INT_2_10_10_10_REV &&
1735 !swapBytes;
1736
1737 case MESA_FORMAT_S8_UINT_Z24_UNORM:
1738 return format == GL_DEPTH_STENCIL && type == GL_UNSIGNED_INT_24_8 &&
1739 !swapBytes;
1740 case MESA_FORMAT_X8_UINT_Z24_UNORM:
1741 case MESA_FORMAT_Z24_UNORM_S8_UINT:
1742 return GL_FALSE;
1743
1744 case MESA_FORMAT_Z_UNORM16:
1745 return format == GL_DEPTH_COMPONENT && type == GL_UNSIGNED_SHORT &&
1746 !swapBytes;
1747
1748 case MESA_FORMAT_Z24_UNORM_X8_UINT:
1749 return GL_FALSE;
1750
1751 case MESA_FORMAT_Z_UNORM32:
1752 return format == GL_DEPTH_COMPONENT && type == GL_UNSIGNED_INT &&
1753 !swapBytes;
1754
1755 case MESA_FORMAT_S_UINT8:
1756 return format == GL_STENCIL_INDEX && type == GL_UNSIGNED_BYTE;
1757
1758 case MESA_FORMAT_RGBA_FLOAT32:
1759 return format == GL_RGBA && type == GL_FLOAT && !swapBytes;
1760 case MESA_FORMAT_RGBA_FLOAT16:
1761 return format == GL_RGBA && type == GL_HALF_FLOAT && !swapBytes;
1762
1763 case MESA_FORMAT_RGB_FLOAT32:
1764 return format == GL_RGB && type == GL_FLOAT && !swapBytes;
1765 case MESA_FORMAT_RGB_FLOAT16:
1766 return format == GL_RGB && type == GL_HALF_FLOAT && !swapBytes;
1767
1768 case MESA_FORMAT_A_FLOAT32:
1769 return format == GL_ALPHA && type == GL_FLOAT && !swapBytes;
1770 case MESA_FORMAT_A_FLOAT16:
1771 return format == GL_ALPHA && type == GL_HALF_FLOAT && !swapBytes;
1772
1773 case MESA_FORMAT_L_FLOAT32:
1774 return format == GL_LUMINANCE && type == GL_FLOAT && !swapBytes;
1775 case MESA_FORMAT_L_FLOAT16:
1776 return format == GL_LUMINANCE && type == GL_HALF_FLOAT && !swapBytes;
1777
1778 case MESA_FORMAT_LA_FLOAT32:
1779 return format == GL_LUMINANCE_ALPHA && type == GL_FLOAT && !swapBytes;
1780 case MESA_FORMAT_LA_FLOAT16:
1781 return format == GL_LUMINANCE_ALPHA && type == GL_HALF_FLOAT && !swapBytes;
1782
1783 case MESA_FORMAT_I_FLOAT32:
1784 return format == GL_RED && type == GL_FLOAT && !swapBytes;
1785 case MESA_FORMAT_I_FLOAT16:
1786 return format == GL_RED && type == GL_HALF_FLOAT && !swapBytes;
1787
1788 case MESA_FORMAT_R_FLOAT32:
1789 return format == GL_RED && type == GL_FLOAT && !swapBytes;
1790 case MESA_FORMAT_R_FLOAT16:
1791 return format == GL_RED && type == GL_HALF_FLOAT && !swapBytes;
1792
1793 case MESA_FORMAT_RG_FLOAT32:
1794 return format == GL_RG && type == GL_FLOAT && !swapBytes;
1795 case MESA_FORMAT_RG_FLOAT16:
1796 return format == GL_RG && type == GL_HALF_FLOAT && !swapBytes;
1797
1798 case MESA_FORMAT_A_UINT8:
1799 return format == GL_ALPHA_INTEGER && type == GL_UNSIGNED_BYTE;
1800 case MESA_FORMAT_A_UINT16:
1801 return format == GL_ALPHA_INTEGER && type == GL_UNSIGNED_SHORT &&
1802 !swapBytes;
1803 case MESA_FORMAT_A_UINT32:
1804 return format == GL_ALPHA_INTEGER && type == GL_UNSIGNED_INT &&
1805 !swapBytes;
1806 case MESA_FORMAT_A_SINT8:
1807 return format == GL_ALPHA_INTEGER && type == GL_BYTE;
1808 case MESA_FORMAT_A_SINT16:
1809 return format == GL_ALPHA_INTEGER && type == GL_SHORT && !swapBytes;
1810 case MESA_FORMAT_A_SINT32:
1811 return format == GL_ALPHA_INTEGER && type == GL_INT && !swapBytes;
1812
1813 case MESA_FORMAT_I_UINT8:
1814 return format == GL_RED_INTEGER && type == GL_UNSIGNED_BYTE;
1815 case MESA_FORMAT_I_UINT16:
1816 return format == GL_RED_INTEGER && type == GL_UNSIGNED_SHORT && !swapBytes;
1817 case MESA_FORMAT_I_UINT32:
1818 return format == GL_RED_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1819 case MESA_FORMAT_I_SINT8:
1820 return format == GL_RED_INTEGER && type == GL_BYTE;
1821 case MESA_FORMAT_I_SINT16:
1822 return format == GL_RED_INTEGER && type == GL_SHORT && !swapBytes;
1823 case MESA_FORMAT_I_SINT32:
1824 return format == GL_RED_INTEGER && type == GL_INT && !swapBytes;
1825
1826 case MESA_FORMAT_L_UINT8:
1827 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_UNSIGNED_BYTE;
1828 case MESA_FORMAT_L_UINT16:
1829 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_UNSIGNED_SHORT &&
1830 !swapBytes;
1831 case MESA_FORMAT_L_UINT32:
1832 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_UNSIGNED_INT &&
1833 !swapBytes;
1834 case MESA_FORMAT_L_SINT8:
1835 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_BYTE;
1836 case MESA_FORMAT_L_SINT16:
1837 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_SHORT &&
1838 !swapBytes;
1839 case MESA_FORMAT_L_SINT32:
1840 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_INT && !swapBytes;
1841
1842 case MESA_FORMAT_LA_UINT8:
1843 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT &&
1844 type == GL_UNSIGNED_BYTE && !swapBytes;
1845 case MESA_FORMAT_LA_UINT16:
1846 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT &&
1847 type == GL_UNSIGNED_SHORT && !swapBytes;
1848 case MESA_FORMAT_LA_UINT32:
1849 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT &&
1850 type == GL_UNSIGNED_INT && !swapBytes;
1851 case MESA_FORMAT_LA_SINT8:
1852 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT && type == GL_BYTE &&
1853 !swapBytes;
1854 case MESA_FORMAT_LA_SINT16:
1855 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT && type == GL_SHORT &&
1856 !swapBytes;
1857 case MESA_FORMAT_LA_SINT32:
1858 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT && type == GL_INT &&
1859 !swapBytes;
1860
1861 case MESA_FORMAT_R_SINT8:
1862 return format == GL_RED_INTEGER && type == GL_BYTE;
1863 case MESA_FORMAT_RG_SINT8:
1864 return format == GL_RG_INTEGER && type == GL_BYTE && !swapBytes;
1865 case MESA_FORMAT_RGB_SINT8:
1866 return format == GL_RGB_INTEGER && type == GL_BYTE && !swapBytes;
1867 case MESA_FORMAT_RGBA_SINT8:
1868 return format == GL_RGBA_INTEGER && type == GL_BYTE && !swapBytes;
1869 case MESA_FORMAT_R_SINT16:
1870 return format == GL_RED_INTEGER && type == GL_SHORT && !swapBytes;
1871 case MESA_FORMAT_RG_SINT16:
1872 return format == GL_RG_INTEGER && type == GL_SHORT && !swapBytes;
1873 case MESA_FORMAT_RGB_SINT16:
1874 return format == GL_RGB_INTEGER && type == GL_SHORT && !swapBytes;
1875 case MESA_FORMAT_RGBA_SINT16:
1876 return format == GL_RGBA_INTEGER && type == GL_SHORT && !swapBytes;
1877 case MESA_FORMAT_R_SINT32:
1878 return format == GL_RED_INTEGER && type == GL_INT && !swapBytes;
1879 case MESA_FORMAT_RG_SINT32:
1880 return format == GL_RG_INTEGER && type == GL_INT && !swapBytes;
1881 case MESA_FORMAT_RGB_SINT32:
1882 return format == GL_RGB_INTEGER && type == GL_INT && !swapBytes;
1883 case MESA_FORMAT_RGBA_SINT32:
1884 return format == GL_RGBA_INTEGER && type == GL_INT && !swapBytes;
1885
1886 case MESA_FORMAT_R_UINT8:
1887 return format == GL_RED_INTEGER && type == GL_UNSIGNED_BYTE;
1888 case MESA_FORMAT_RG_UINT8:
1889 return format == GL_RG_INTEGER && type == GL_UNSIGNED_BYTE && !swapBytes;
1890 case MESA_FORMAT_RGB_UINT8:
1891 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_BYTE && !swapBytes;
1892 case MESA_FORMAT_RGBA_UINT8:
1893 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_BYTE &&
1894 !swapBytes;
1895 case MESA_FORMAT_R_UINT16:
1896 return format == GL_RED_INTEGER && type == GL_UNSIGNED_SHORT &&
1897 !swapBytes;
1898 case MESA_FORMAT_RG_UINT16:
1899 return format == GL_RG_INTEGER && type == GL_UNSIGNED_SHORT && !swapBytes;
1900 case MESA_FORMAT_RGB_UINT16:
1901 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_SHORT &&
1902 !swapBytes;
1903 case MESA_FORMAT_RGBA_UINT16:
1904 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT &&
1905 !swapBytes;
1906 case MESA_FORMAT_R_UINT32:
1907 return format == GL_RED_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1908 case MESA_FORMAT_RG_UINT32:
1909 return format == GL_RG_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1910 case MESA_FORMAT_RGB_UINT32:
1911 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1912 case MESA_FORMAT_RGBA_UINT32:
1913 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1914
1915 case MESA_FORMAT_R_SNORM8:
1916 return format == GL_RED && type == GL_BYTE;
1917 case MESA_FORMAT_R8G8_SNORM:
1918 return format == GL_RG && type == GL_BYTE && littleEndian &&
1919 !swapBytes;
1920 case MESA_FORMAT_X8B8G8R8_SNORM:
1921 return GL_FALSE;
1922
1923 case MESA_FORMAT_A8B8G8R8_SNORM:
1924 if (format == GL_RGBA && type == GL_BYTE && !littleEndian)
1925 return GL_TRUE;
1926
1927 if (format == GL_ABGR_EXT && type == GL_BYTE && littleEndian)
1928 return GL_TRUE;
1929
1930 return GL_FALSE;
1931
1932 case MESA_FORMAT_R8G8B8A8_SNORM:
1933 if (format == GL_RGBA && type == GL_BYTE && littleEndian)
1934 return GL_TRUE;
1935
1936 if (format == GL_ABGR_EXT && type == GL_BYTE && !littleEndian)
1937 return GL_TRUE;
1938
1939 return GL_FALSE;
1940
1941 case MESA_FORMAT_R_SNORM16:
1942 return format == GL_RED && type == GL_SHORT &&
1943 !swapBytes;
1944 case MESA_FORMAT_R16G16_SNORM:
1945 return format == GL_RG && type == GL_SHORT && littleEndian && !swapBytes;
1946 case MESA_FORMAT_RGB_SNORM16:
1947 return format == GL_RGB && type == GL_SHORT && !swapBytes;
1948 case MESA_FORMAT_RGBA_SNORM16:
1949 return format == GL_RGBA && type == GL_SHORT && !swapBytes;
1950 case MESA_FORMAT_RGBA_UNORM16:
1951 return format == GL_RGBA && type == GL_UNSIGNED_SHORT &&
1952 !swapBytes;
1953
1954 case MESA_FORMAT_A_SNORM8:
1955 return format == GL_ALPHA && type == GL_BYTE;
1956 case MESA_FORMAT_L_SNORM8:
1957 return format == GL_LUMINANCE && type == GL_BYTE;
1958 case MESA_FORMAT_L8A8_SNORM:
1959 return format == GL_LUMINANCE_ALPHA && type == GL_BYTE &&
1960 littleEndian && !swapBytes;
1961 case MESA_FORMAT_A8L8_SNORM:
1962 return format == GL_LUMINANCE_ALPHA && type == GL_BYTE &&
1963 !littleEndian && !swapBytes;
1964 case MESA_FORMAT_I_SNORM8:
1965 return format == GL_RED && type == GL_BYTE;
1966 case MESA_FORMAT_A_SNORM16:
1967 return format == GL_ALPHA && type == GL_SHORT && !swapBytes;
1968 case MESA_FORMAT_L_SNORM16:
1969 return format == GL_LUMINANCE && type == GL_SHORT && !swapBytes;
1970 case MESA_FORMAT_LA_SNORM16:
1971 return format == GL_LUMINANCE_ALPHA && type == GL_SHORT &&
1972 littleEndian && !swapBytes;
1973 case MESA_FORMAT_I_SNORM16:
1974 return format == GL_RED && type == GL_SHORT && littleEndian &&
1975 !swapBytes;
1976
1977 case MESA_FORMAT_B10G10R10A2_UINT:
1978 return (format == GL_BGRA_INTEGER_EXT &&
1979 type == GL_UNSIGNED_INT_2_10_10_10_REV &&
1980 !swapBytes);
1981
1982 case MESA_FORMAT_R10G10B10A2_UINT:
1983 return (format == GL_RGBA_INTEGER_EXT &&
1984 type == GL_UNSIGNED_INT_2_10_10_10_REV &&
1985 !swapBytes);
1986
1987 case MESA_FORMAT_B5G6R5_UINT:
1988 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_SHORT_5_6_5;
1989
1990 case MESA_FORMAT_R5G6B5_UINT:
1991 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_SHORT_5_6_5_REV;
1992
1993 case MESA_FORMAT_B2G3R3_UINT:
1994 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_BYTE_3_3_2;
1995
1996 case MESA_FORMAT_R3G3B2_UINT:
1997 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_BYTE_2_3_3_REV;
1998
1999 case MESA_FORMAT_A4B4G4R4_UINT:
2000 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
2001 return GL_TRUE;
2002
2003 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && swapBytes)
2004 return GL_TRUE;
2005 return GL_FALSE;
2006
2007 case MESA_FORMAT_R4G4B4A4_UINT:
2008 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !swapBytes)
2009 return GL_TRUE;
2010
2011 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4 && swapBytes)
2012 return GL_TRUE;
2013
2014 return GL_FALSE;
2015
2016 case MESA_FORMAT_B4G4R4A4_UINT:
2017 return format == GL_BGRA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4_REV &&
2018 !swapBytes;
2019
2020 case MESA_FORMAT_A4R4G4B4_UINT:
2021 return GL_FALSE;
2022
2023 case MESA_FORMAT_A1B5G5R5_UINT:
2024 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
2025 !swapBytes;
2026
2027 case MESA_FORMAT_B5G5R5A1_UINT:
2028 return format == GL_BGRA_INTEGER && type == GL_UNSIGNED_SHORT_1_5_5_5_REV &&
2029 !swapBytes;
2030
2031 case MESA_FORMAT_A1R5G5B5_UINT:
2032 return format == GL_BGRA_INTEGER && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
2033 !swapBytes;
2034
2035 case MESA_FORMAT_R5G5B5A1_UINT:
2036 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_1_5_5_5_REV;
2037
2038 case MESA_FORMAT_R9G9B9E5_FLOAT:
2039 return format == GL_RGB && type == GL_UNSIGNED_INT_5_9_9_9_REV &&
2040 !swapBytes;
2041
2042 case MESA_FORMAT_R11G11B10_FLOAT:
2043 return format == GL_RGB && type == GL_UNSIGNED_INT_10F_11F_11F_REV &&
2044 !swapBytes;
2045
2046 case MESA_FORMAT_Z_FLOAT32:
2047 return format == GL_DEPTH_COMPONENT && type == GL_FLOAT && !swapBytes;
2048
2049 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
2050 return format == GL_DEPTH_STENCIL &&
2051 type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV && !swapBytes;
2052
2053 case MESA_FORMAT_B4G4R4X4_UNORM:
2054 case MESA_FORMAT_B5G5R5X1_UNORM:
2055 case MESA_FORMAT_R8G8B8X8_SNORM:
2056 case MESA_FORMAT_R8G8B8X8_SRGB:
2057 case MESA_FORMAT_X8B8G8R8_SRGB:
2058 case MESA_FORMAT_RGBX_UINT8:
2059 case MESA_FORMAT_RGBX_SINT8:
2060 case MESA_FORMAT_B10G10R10X2_UNORM:
2061 case MESA_FORMAT_R10G10B10X2_UNORM:
2062 case MESA_FORMAT_RGBX_UNORM16:
2063 case MESA_FORMAT_RGBX_SNORM16:
2064 case MESA_FORMAT_RGBX_FLOAT16:
2065 case MESA_FORMAT_RGBX_UINT16:
2066 case MESA_FORMAT_RGBX_SINT16:
2067 case MESA_FORMAT_RGBX_FLOAT32:
2068 case MESA_FORMAT_RGBX_UINT32:
2069 case MESA_FORMAT_RGBX_SINT32:
2070 return GL_FALSE;
2071
2072 case MESA_FORMAT_R10G10B10A2_UNORM:
2073 return format == GL_RGBA && type == GL_UNSIGNED_INT_2_10_10_10_REV &&
2074 !swapBytes;
2075
2076 case MESA_FORMAT_G8R8_SNORM:
2077 return format == GL_RG && type == GL_BYTE && !littleEndian &&
2078 !swapBytes;
2079
2080 case MESA_FORMAT_G16R16_SNORM:
2081 return format == GL_RG && type == GL_SHORT && !littleEndian &&
2082 !swapBytes;
2083
2084 case MESA_FORMAT_B8G8R8X8_SRGB:
2085 case MESA_FORMAT_X8R8G8B8_SRGB:
2086 return GL_FALSE;
2087 default:
2088 assert(_mesa_is_format_compressed(mesa_format));
2089 if (error)
2090 *error = GL_INVALID_ENUM;
2091 }
2092 return GL_FALSE;
2093 }
2094