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