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