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