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