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