mesa: Implement a new GL_MESA_tile_raster_order extension.
[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 * If the given format is a compressed format, return a corresponding
626 * uncompressed format.
627 */
628 mesa_format
629 _mesa_get_uncompressed_format(mesa_format format)
630 {
631 switch (format) {
632 case MESA_FORMAT_RGB_FXT1:
633 return MESA_FORMAT_BGR_UNORM8;
634 case MESA_FORMAT_RGBA_FXT1:
635 return MESA_FORMAT_A8B8G8R8_UNORM;
636 case MESA_FORMAT_RGB_DXT1:
637 case MESA_FORMAT_SRGB_DXT1:
638 return MESA_FORMAT_BGR_UNORM8;
639 case MESA_FORMAT_RGBA_DXT1:
640 case MESA_FORMAT_SRGBA_DXT1:
641 return MESA_FORMAT_A8B8G8R8_UNORM;
642 case MESA_FORMAT_RGBA_DXT3:
643 case MESA_FORMAT_SRGBA_DXT3:
644 return MESA_FORMAT_A8B8G8R8_UNORM;
645 case MESA_FORMAT_RGBA_DXT5:
646 case MESA_FORMAT_SRGBA_DXT5:
647 return MESA_FORMAT_A8B8G8R8_UNORM;
648 case MESA_FORMAT_R_RGTC1_UNORM:
649 return MESA_FORMAT_R_UNORM8;
650 case MESA_FORMAT_R_RGTC1_SNORM:
651 return MESA_FORMAT_R_SNORM8;
652 case MESA_FORMAT_RG_RGTC2_UNORM:
653 return MESA_FORMAT_R8G8_UNORM;
654 case MESA_FORMAT_RG_RGTC2_SNORM:
655 return MESA_FORMAT_R8G8_SNORM;
656 case MESA_FORMAT_L_LATC1_UNORM:
657 return MESA_FORMAT_L_UNORM8;
658 case MESA_FORMAT_L_LATC1_SNORM:
659 return MESA_FORMAT_L_SNORM8;
660 case MESA_FORMAT_LA_LATC2_UNORM:
661 return MESA_FORMAT_L8A8_UNORM;
662 case MESA_FORMAT_LA_LATC2_SNORM:
663 return MESA_FORMAT_L8A8_SNORM;
664 case MESA_FORMAT_ETC1_RGB8:
665 case MESA_FORMAT_ETC2_RGB8:
666 case MESA_FORMAT_ETC2_SRGB8:
667 return MESA_FORMAT_BGR_UNORM8;
668 case MESA_FORMAT_ETC2_RGBA8_EAC:
669 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
670 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
671 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
672 return MESA_FORMAT_A8B8G8R8_UNORM;
673 case MESA_FORMAT_ETC2_R11_EAC:
674 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
675 return MESA_FORMAT_R_UNORM16;
676 case MESA_FORMAT_ETC2_RG11_EAC:
677 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
678 return MESA_FORMAT_R16G16_UNORM;
679 case MESA_FORMAT_BPTC_RGBA_UNORM:
680 case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
681 return MESA_FORMAT_A8B8G8R8_UNORM;
682 case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
683 case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
684 return MESA_FORMAT_RGB_FLOAT32;
685 default:
686 #ifdef DEBUG
687 assert(!_mesa_is_format_compressed(format));
688 #endif
689 return format;
690 }
691 }
692
693
694 GLuint
695 _mesa_format_num_components(mesa_format format)
696 {
697 const struct gl_format_info *info = _mesa_get_format_info(format);
698 return ((info->RedBits > 0) +
699 (info->GreenBits > 0) +
700 (info->BlueBits > 0) +
701 (info->AlphaBits > 0) +
702 (info->LuminanceBits > 0) +
703 (info->IntensityBits > 0) +
704 (info->DepthBits > 0) +
705 (info->StencilBits > 0));
706 }
707
708
709 /**
710 * Returns true if a color format has data stored in the R/G/B/A channels,
711 * given an index from 0 to 3.
712 */
713 bool
714 _mesa_format_has_color_component(mesa_format format, int component)
715 {
716 const struct gl_format_info *info = _mesa_get_format_info(format);
717
718 assert(info->BaseFormat != GL_DEPTH_COMPONENT &&
719 info->BaseFormat != GL_DEPTH_STENCIL &&
720 info->BaseFormat != GL_STENCIL_INDEX);
721
722 switch (component) {
723 case 0:
724 return (info->RedBits + info->IntensityBits + info->LuminanceBits) > 0;
725 case 1:
726 return (info->GreenBits + info->IntensityBits + info->LuminanceBits) > 0;
727 case 2:
728 return (info->BlueBits + info->IntensityBits + info->LuminanceBits) > 0;
729 case 3:
730 return (info->AlphaBits + info->IntensityBits) > 0;
731 default:
732 assert(!"Invalid color component: must be 0..3");
733 return false;
734 }
735 }
736
737
738 /**
739 * Return number of bytes needed to store an image of the given size
740 * in the given format.
741 */
742 GLuint
743 _mesa_format_image_size(mesa_format format, GLsizei width,
744 GLsizei height, GLsizei depth)
745 {
746 const struct gl_format_info *info = _mesa_get_format_info(format);
747 GLuint sz;
748 /* Strictly speaking, a conditional isn't needed here */
749 if (info->BlockWidth > 1 || info->BlockHeight > 1 || info->BlockDepth > 1) {
750 /* compressed format (2D only for now) */
751 const GLuint bw = info->BlockWidth;
752 const GLuint bh = info->BlockHeight;
753 const GLuint bd = info->BlockDepth;
754 const GLuint wblocks = (width + bw - 1) / bw;
755 const GLuint hblocks = (height + bh - 1) / bh;
756 const GLuint dblocks = (depth + bd - 1) / bd;
757 sz = wblocks * hblocks * dblocks * info->BytesPerBlock;
758 } else
759 /* non-compressed */
760 sz = width * height * depth * info->BytesPerBlock;
761
762 return sz;
763 }
764
765
766 /**
767 * Same as _mesa_format_image_size() but returns a 64-bit value to
768 * accommodate very large textures.
769 */
770 uint64_t
771 _mesa_format_image_size64(mesa_format format, GLsizei width,
772 GLsizei height, GLsizei depth)
773 {
774 const struct gl_format_info *info = _mesa_get_format_info(format);
775 uint64_t sz;
776 /* Strictly speaking, a conditional isn't needed here */
777 if (info->BlockWidth > 1 || info->BlockHeight > 1 || info->BlockDepth > 1) {
778 /* compressed format (2D only for now) */
779 const uint64_t bw = info->BlockWidth;
780 const uint64_t bh = info->BlockHeight;
781 const uint64_t bd = info->BlockDepth;
782 const uint64_t wblocks = (width + bw - 1) / bw;
783 const uint64_t hblocks = (height + bh - 1) / bh;
784 const uint64_t dblocks = (depth + bd - 1) / bd;
785 sz = wblocks * hblocks * dblocks * info->BytesPerBlock;
786 } else
787 /* non-compressed */
788 sz = ((uint64_t) width * (uint64_t) height *
789 (uint64_t) depth * info->BytesPerBlock);
790
791 return sz;
792 }
793
794
795
796 GLint
797 _mesa_format_row_stride(mesa_format format, GLsizei width)
798 {
799 const struct gl_format_info *info = _mesa_get_format_info(format);
800 /* Strictly speaking, a conditional isn't needed here */
801 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
802 /* compressed format */
803 const GLuint bw = info->BlockWidth;
804 const GLuint wblocks = (width + bw - 1) / bw;
805 const GLint stride = wblocks * info->BytesPerBlock;
806 return stride;
807 }
808 else {
809 const GLint stride = width * info->BytesPerBlock;
810 return stride;
811 }
812 }
813
814
815
816 /**
817 * Return datatype and number of components per texel for the given
818 * uncompressed mesa_format. Only used for mipmap generation code.
819 */
820 void
821 _mesa_uncompressed_format_to_type_and_comps(mesa_format format,
822 GLenum *datatype, GLuint *comps)
823 {
824 switch (format) {
825 case MESA_FORMAT_A8B8G8R8_UNORM:
826 case MESA_FORMAT_R8G8B8A8_UNORM:
827 case MESA_FORMAT_B8G8R8A8_UNORM:
828 case MESA_FORMAT_A8R8G8B8_UNORM:
829 case MESA_FORMAT_X8B8G8R8_UNORM:
830 case MESA_FORMAT_R8G8B8X8_UNORM:
831 case MESA_FORMAT_B8G8R8X8_UNORM:
832 case MESA_FORMAT_X8R8G8B8_UNORM:
833 case MESA_FORMAT_A8B8G8R8_UINT:
834 case MESA_FORMAT_R8G8B8A8_UINT:
835 case MESA_FORMAT_B8G8R8A8_UINT:
836 case MESA_FORMAT_A8R8G8B8_UINT:
837 *datatype = GL_UNSIGNED_BYTE;
838 *comps = 4;
839 return;
840 case MESA_FORMAT_BGR_UNORM8:
841 case MESA_FORMAT_RGB_UNORM8:
842 *datatype = GL_UNSIGNED_BYTE;
843 *comps = 3;
844 return;
845 case MESA_FORMAT_B5G6R5_UNORM:
846 case MESA_FORMAT_R5G6B5_UNORM:
847 case MESA_FORMAT_B5G6R5_UINT:
848 case MESA_FORMAT_R5G6B5_UINT:
849 *datatype = GL_UNSIGNED_SHORT_5_6_5;
850 *comps = 3;
851 return;
852
853 case MESA_FORMAT_B4G4R4A4_UNORM:
854 case MESA_FORMAT_A4R4G4B4_UNORM:
855 case MESA_FORMAT_B4G4R4X4_UNORM:
856 case MESA_FORMAT_B4G4R4A4_UINT:
857 case MESA_FORMAT_A4R4G4B4_UINT:
858 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
859 *comps = 4;
860 return;
861
862 case MESA_FORMAT_B5G5R5A1_UNORM:
863 case MESA_FORMAT_A1R5G5B5_UNORM:
864 case MESA_FORMAT_B5G5R5X1_UNORM:
865 case MESA_FORMAT_B5G5R5A1_UINT:
866 case MESA_FORMAT_A1R5G5B5_UINT:
867 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
868 *comps = 4;
869 return;
870
871 case MESA_FORMAT_B10G10R10A2_UNORM:
872 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
873 *comps = 4;
874 return;
875
876 case MESA_FORMAT_A1B5G5R5_UNORM:
877 case MESA_FORMAT_A1B5G5R5_UINT:
878 case MESA_FORMAT_X1B5G5R5_UNORM:
879 *datatype = GL_UNSIGNED_SHORT_5_5_5_1;
880 *comps = 4;
881 return;
882
883 case MESA_FORMAT_L4A4_UNORM:
884 *datatype = MESA_UNSIGNED_BYTE_4_4;
885 *comps = 2;
886 return;
887
888 case MESA_FORMAT_L8A8_UNORM:
889 case MESA_FORMAT_A8L8_UNORM:
890 case MESA_FORMAT_R8G8_UNORM:
891 case MESA_FORMAT_G8R8_UNORM:
892 *datatype = GL_UNSIGNED_BYTE;
893 *comps = 2;
894 return;
895
896 case MESA_FORMAT_L16A16_UNORM:
897 case MESA_FORMAT_A16L16_UNORM:
898 case MESA_FORMAT_R16G16_UNORM:
899 case MESA_FORMAT_G16R16_UNORM:
900 *datatype = GL_UNSIGNED_SHORT;
901 *comps = 2;
902 return;
903
904 case MESA_FORMAT_R_UNORM16:
905 case MESA_FORMAT_A_UNORM16:
906 case MESA_FORMAT_L_UNORM16:
907 case MESA_FORMAT_I_UNORM16:
908 *datatype = GL_UNSIGNED_SHORT;
909 *comps = 1;
910 return;
911
912 case MESA_FORMAT_R3G3B2_UNORM:
913 case MESA_FORMAT_R3G3B2_UINT:
914 *datatype = GL_UNSIGNED_BYTE_2_3_3_REV;
915 *comps = 3;
916 return;
917 case MESA_FORMAT_A4B4G4R4_UNORM:
918 case MESA_FORMAT_A4B4G4R4_UINT:
919 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
920 *comps = 4;
921 return;
922
923 case MESA_FORMAT_R4G4B4A4_UNORM:
924 case MESA_FORMAT_R4G4B4A4_UINT:
925 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
926 *comps = 4;
927 return;
928 case MESA_FORMAT_R5G5B5A1_UNORM:
929 case MESA_FORMAT_R5G5B5A1_UINT:
930 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
931 *comps = 4;
932 return;
933 case MESA_FORMAT_A2B10G10R10_UNORM:
934 case MESA_FORMAT_A2B10G10R10_UINT:
935 *datatype = GL_UNSIGNED_INT_10_10_10_2;
936 *comps = 4;
937 return;
938 case MESA_FORMAT_A2R10G10B10_UNORM:
939 case MESA_FORMAT_A2R10G10B10_UINT:
940 *datatype = GL_UNSIGNED_INT_10_10_10_2;
941 *comps = 4;
942 return;
943
944 case MESA_FORMAT_B2G3R3_UNORM:
945 case MESA_FORMAT_B2G3R3_UINT:
946 *datatype = GL_UNSIGNED_BYTE_3_3_2;
947 *comps = 3;
948 return;
949
950 case MESA_FORMAT_A_UNORM8:
951 case MESA_FORMAT_L_UNORM8:
952 case MESA_FORMAT_I_UNORM8:
953 case MESA_FORMAT_R_UNORM8:
954 case MESA_FORMAT_S_UINT8:
955 *datatype = GL_UNSIGNED_BYTE;
956 *comps = 1;
957 return;
958
959 case MESA_FORMAT_YCBCR:
960 case MESA_FORMAT_YCBCR_REV:
961 *datatype = GL_UNSIGNED_SHORT;
962 *comps = 2;
963 return;
964
965 case MESA_FORMAT_S8_UINT_Z24_UNORM:
966 *datatype = GL_UNSIGNED_INT_24_8_MESA;
967 *comps = 2;
968 return;
969
970 case MESA_FORMAT_Z24_UNORM_S8_UINT:
971 *datatype = GL_UNSIGNED_INT_8_24_REV_MESA;
972 *comps = 2;
973 return;
974
975 case MESA_FORMAT_Z_UNORM16:
976 *datatype = GL_UNSIGNED_SHORT;
977 *comps = 1;
978 return;
979
980 case MESA_FORMAT_Z24_UNORM_X8_UINT:
981 *datatype = GL_UNSIGNED_INT;
982 *comps = 1;
983 return;
984
985 case MESA_FORMAT_X8_UINT_Z24_UNORM:
986 *datatype = GL_UNSIGNED_INT;
987 *comps = 1;
988 return;
989
990 case MESA_FORMAT_Z_UNORM32:
991 *datatype = GL_UNSIGNED_INT;
992 *comps = 1;
993 return;
994
995 case MESA_FORMAT_Z_FLOAT32:
996 *datatype = GL_FLOAT;
997 *comps = 1;
998 return;
999
1000 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1001 *datatype = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
1002 *comps = 1;
1003 return;
1004
1005 case MESA_FORMAT_R_SNORM8:
1006 case MESA_FORMAT_A_SNORM8:
1007 case MESA_FORMAT_L_SNORM8:
1008 case MESA_FORMAT_I_SNORM8:
1009 *datatype = GL_BYTE;
1010 *comps = 1;
1011 return;
1012 case MESA_FORMAT_R8G8_SNORM:
1013 case MESA_FORMAT_L8A8_SNORM:
1014 case MESA_FORMAT_A8L8_SNORM:
1015 *datatype = GL_BYTE;
1016 *comps = 2;
1017 return;
1018 case MESA_FORMAT_A8B8G8R8_SNORM:
1019 case MESA_FORMAT_R8G8B8A8_SNORM:
1020 case MESA_FORMAT_X8B8G8R8_SNORM:
1021 *datatype = GL_BYTE;
1022 *comps = 4;
1023 return;
1024
1025 case MESA_FORMAT_RGBA_UNORM16:
1026 *datatype = GL_UNSIGNED_SHORT;
1027 *comps = 4;
1028 return;
1029
1030 case MESA_FORMAT_R_SNORM16:
1031 case MESA_FORMAT_A_SNORM16:
1032 case MESA_FORMAT_L_SNORM16:
1033 case MESA_FORMAT_I_SNORM16:
1034 *datatype = GL_SHORT;
1035 *comps = 1;
1036 return;
1037 case MESA_FORMAT_R16G16_SNORM:
1038 case MESA_FORMAT_LA_SNORM16:
1039 *datatype = GL_SHORT;
1040 *comps = 2;
1041 return;
1042 case MESA_FORMAT_RGB_SNORM16:
1043 *datatype = GL_SHORT;
1044 *comps = 3;
1045 return;
1046 case MESA_FORMAT_RGBA_SNORM16:
1047 *datatype = GL_SHORT;
1048 *comps = 4;
1049 return;
1050
1051 case MESA_FORMAT_BGR_SRGB8:
1052 *datatype = GL_UNSIGNED_BYTE;
1053 *comps = 3;
1054 return;
1055 case MESA_FORMAT_A8B8G8R8_SRGB:
1056 case MESA_FORMAT_B8G8R8A8_SRGB:
1057 case MESA_FORMAT_A8R8G8B8_SRGB:
1058 case MESA_FORMAT_R8G8B8A8_SRGB:
1059 *datatype = GL_UNSIGNED_BYTE;
1060 *comps = 4;
1061 return;
1062 case MESA_FORMAT_L_SRGB8:
1063 *datatype = GL_UNSIGNED_BYTE;
1064 *comps = 1;
1065 return;
1066 case MESA_FORMAT_L8A8_SRGB:
1067 case MESA_FORMAT_A8L8_SRGB:
1068 *datatype = GL_UNSIGNED_BYTE;
1069 *comps = 2;
1070 return;
1071
1072 case MESA_FORMAT_RGBA_FLOAT32:
1073 *datatype = GL_FLOAT;
1074 *comps = 4;
1075 return;
1076 case MESA_FORMAT_RGBA_FLOAT16:
1077 *datatype = GL_HALF_FLOAT_ARB;
1078 *comps = 4;
1079 return;
1080 case MESA_FORMAT_RGB_FLOAT32:
1081 *datatype = GL_FLOAT;
1082 *comps = 3;
1083 return;
1084 case MESA_FORMAT_RGB_FLOAT16:
1085 *datatype = GL_HALF_FLOAT_ARB;
1086 *comps = 3;
1087 return;
1088 case MESA_FORMAT_LA_FLOAT32:
1089 case MESA_FORMAT_RG_FLOAT32:
1090 *datatype = GL_FLOAT;
1091 *comps = 2;
1092 return;
1093 case MESA_FORMAT_LA_FLOAT16:
1094 case MESA_FORMAT_RG_FLOAT16:
1095 *datatype = GL_HALF_FLOAT_ARB;
1096 *comps = 2;
1097 return;
1098 case MESA_FORMAT_A_FLOAT32:
1099 case MESA_FORMAT_L_FLOAT32:
1100 case MESA_FORMAT_I_FLOAT32:
1101 case MESA_FORMAT_R_FLOAT32:
1102 *datatype = GL_FLOAT;
1103 *comps = 1;
1104 return;
1105 case MESA_FORMAT_A_FLOAT16:
1106 case MESA_FORMAT_L_FLOAT16:
1107 case MESA_FORMAT_I_FLOAT16:
1108 case MESA_FORMAT_R_FLOAT16:
1109 *datatype = GL_HALF_FLOAT_ARB;
1110 *comps = 1;
1111 return;
1112
1113 case MESA_FORMAT_A_UINT8:
1114 case MESA_FORMAT_L_UINT8:
1115 case MESA_FORMAT_I_UINT8:
1116 *datatype = GL_UNSIGNED_BYTE;
1117 *comps = 1;
1118 return;
1119 case MESA_FORMAT_LA_UINT8:
1120 *datatype = GL_UNSIGNED_BYTE;
1121 *comps = 2;
1122 return;
1123
1124 case MESA_FORMAT_A_UINT16:
1125 case MESA_FORMAT_L_UINT16:
1126 case MESA_FORMAT_I_UINT16:
1127 *datatype = GL_UNSIGNED_SHORT;
1128 *comps = 1;
1129 return;
1130 case MESA_FORMAT_LA_UINT16:
1131 *datatype = GL_UNSIGNED_SHORT;
1132 *comps = 2;
1133 return;
1134 case MESA_FORMAT_A_UINT32:
1135 case MESA_FORMAT_L_UINT32:
1136 case MESA_FORMAT_I_UINT32:
1137 *datatype = GL_UNSIGNED_INT;
1138 *comps = 1;
1139 return;
1140 case MESA_FORMAT_LA_UINT32:
1141 *datatype = GL_UNSIGNED_INT;
1142 *comps = 2;
1143 return;
1144 case MESA_FORMAT_A_SINT8:
1145 case MESA_FORMAT_L_SINT8:
1146 case MESA_FORMAT_I_SINT8:
1147 *datatype = GL_BYTE;
1148 *comps = 1;
1149 return;
1150 case MESA_FORMAT_LA_SINT8:
1151 *datatype = GL_BYTE;
1152 *comps = 2;
1153 return;
1154
1155 case MESA_FORMAT_A_SINT16:
1156 case MESA_FORMAT_L_SINT16:
1157 case MESA_FORMAT_I_SINT16:
1158 *datatype = GL_SHORT;
1159 *comps = 1;
1160 return;
1161 case MESA_FORMAT_LA_SINT16:
1162 *datatype = GL_SHORT;
1163 *comps = 2;
1164 return;
1165
1166 case MESA_FORMAT_A_SINT32:
1167 case MESA_FORMAT_L_SINT32:
1168 case MESA_FORMAT_I_SINT32:
1169 *datatype = GL_INT;
1170 *comps = 1;
1171 return;
1172 case MESA_FORMAT_LA_SINT32:
1173 *datatype = GL_INT;
1174 *comps = 2;
1175 return;
1176
1177 case MESA_FORMAT_R_SINT8:
1178 *datatype = GL_BYTE;
1179 *comps = 1;
1180 return;
1181 case MESA_FORMAT_RG_SINT8:
1182 *datatype = GL_BYTE;
1183 *comps = 2;
1184 return;
1185 case MESA_FORMAT_RGB_SINT8:
1186 *datatype = GL_BYTE;
1187 *comps = 3;
1188 return;
1189 case MESA_FORMAT_RGBA_SINT8:
1190 *datatype = GL_BYTE;
1191 *comps = 4;
1192 return;
1193 case MESA_FORMAT_R_SINT16:
1194 *datatype = GL_SHORT;
1195 *comps = 1;
1196 return;
1197 case MESA_FORMAT_RG_SINT16:
1198 *datatype = GL_SHORT;
1199 *comps = 2;
1200 return;
1201 case MESA_FORMAT_RGB_SINT16:
1202 *datatype = GL_SHORT;
1203 *comps = 3;
1204 return;
1205 case MESA_FORMAT_RGBA_SINT16:
1206 *datatype = GL_SHORT;
1207 *comps = 4;
1208 return;
1209 case MESA_FORMAT_R_SINT32:
1210 *datatype = GL_INT;
1211 *comps = 1;
1212 return;
1213 case MESA_FORMAT_RG_SINT32:
1214 *datatype = GL_INT;
1215 *comps = 2;
1216 return;
1217 case MESA_FORMAT_RGB_SINT32:
1218 *datatype = GL_INT;
1219 *comps = 3;
1220 return;
1221 case MESA_FORMAT_RGBA_SINT32:
1222 *datatype = GL_INT;
1223 *comps = 4;
1224 return;
1225
1226 /**
1227 * \name Non-normalized unsigned integer formats.
1228 */
1229 case MESA_FORMAT_R_UINT8:
1230 *datatype = GL_UNSIGNED_BYTE;
1231 *comps = 1;
1232 return;
1233 case MESA_FORMAT_RG_UINT8:
1234 *datatype = GL_UNSIGNED_BYTE;
1235 *comps = 2;
1236 return;
1237 case MESA_FORMAT_RGB_UINT8:
1238 *datatype = GL_UNSIGNED_BYTE;
1239 *comps = 3;
1240 return;
1241 case MESA_FORMAT_RGBA_UINT8:
1242 *datatype = GL_UNSIGNED_BYTE;
1243 *comps = 4;
1244 return;
1245 case MESA_FORMAT_R_UINT16:
1246 *datatype = GL_UNSIGNED_SHORT;
1247 *comps = 1;
1248 return;
1249 case MESA_FORMAT_RG_UINT16:
1250 *datatype = GL_UNSIGNED_SHORT;
1251 *comps = 2;
1252 return;
1253 case MESA_FORMAT_RGB_UINT16:
1254 *datatype = GL_UNSIGNED_SHORT;
1255 *comps = 3;
1256 return;
1257 case MESA_FORMAT_RGBA_UINT16:
1258 *datatype = GL_UNSIGNED_SHORT;
1259 *comps = 4;
1260 return;
1261 case MESA_FORMAT_R_UINT32:
1262 *datatype = GL_UNSIGNED_INT;
1263 *comps = 1;
1264 return;
1265 case MESA_FORMAT_RG_UINT32:
1266 *datatype = GL_UNSIGNED_INT;
1267 *comps = 2;
1268 return;
1269 case MESA_FORMAT_RGB_UINT32:
1270 *datatype = GL_UNSIGNED_INT;
1271 *comps = 3;
1272 return;
1273 case MESA_FORMAT_RGBA_UINT32:
1274 *datatype = GL_UNSIGNED_INT;
1275 *comps = 4;
1276 return;
1277
1278 case MESA_FORMAT_R9G9B9E5_FLOAT:
1279 *datatype = GL_UNSIGNED_INT_5_9_9_9_REV;
1280 *comps = 3;
1281 return;
1282
1283 case MESA_FORMAT_R11G11B10_FLOAT:
1284 *datatype = GL_UNSIGNED_INT_10F_11F_11F_REV;
1285 *comps = 3;
1286 return;
1287
1288 case MESA_FORMAT_B10G10R10A2_UINT:
1289 case MESA_FORMAT_R10G10B10A2_UINT:
1290 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1291 *comps = 4;
1292 return;
1293
1294 case MESA_FORMAT_R8G8B8X8_SRGB:
1295 case MESA_FORMAT_X8B8G8R8_SRGB:
1296 case MESA_FORMAT_RGBX_UINT8:
1297 *datatype = GL_UNSIGNED_BYTE;
1298 *comps = 4;
1299 return;
1300
1301 case MESA_FORMAT_R8G8B8X8_SNORM:
1302 case MESA_FORMAT_RGBX_SINT8:
1303 *datatype = GL_BYTE;
1304 *comps = 4;
1305 return;
1306
1307 case MESA_FORMAT_B10G10R10X2_UNORM:
1308 case MESA_FORMAT_R10G10B10X2_UNORM:
1309 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1310 *comps = 4;
1311 return;
1312
1313 case MESA_FORMAT_RGBX_UNORM16:
1314 case MESA_FORMAT_RGBX_UINT16:
1315 *datatype = GL_UNSIGNED_SHORT;
1316 *comps = 4;
1317 return;
1318
1319 case MESA_FORMAT_RGBX_SNORM16:
1320 case MESA_FORMAT_RGBX_SINT16:
1321 *datatype = GL_SHORT;
1322 *comps = 4;
1323 return;
1324
1325 case MESA_FORMAT_RGBX_FLOAT16:
1326 *datatype = GL_HALF_FLOAT;
1327 *comps = 4;
1328 return;
1329
1330 case MESA_FORMAT_RGBX_FLOAT32:
1331 *datatype = GL_FLOAT;
1332 *comps = 4;
1333 return;
1334
1335 case MESA_FORMAT_RGBX_UINT32:
1336 *datatype = GL_UNSIGNED_INT;
1337 *comps = 4;
1338 return;
1339
1340 case MESA_FORMAT_RGBX_SINT32:
1341 *datatype = GL_INT;
1342 *comps = 4;
1343 return;
1344
1345 case MESA_FORMAT_R10G10B10A2_UNORM:
1346 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1347 *comps = 4;
1348 return;
1349
1350 case MESA_FORMAT_G8R8_SNORM:
1351 *datatype = GL_BYTE;
1352 *comps = 2;
1353 return;
1354
1355 case MESA_FORMAT_G16R16_SNORM:
1356 *datatype = GL_SHORT;
1357 *comps = 2;
1358 return;
1359
1360 case MESA_FORMAT_B8G8R8X8_SRGB:
1361 case MESA_FORMAT_X8R8G8B8_SRGB:
1362 *datatype = GL_UNSIGNED_BYTE;
1363 *comps = 4;
1364 return;
1365
1366 case MESA_FORMAT_COUNT:
1367 assert(0);
1368 return;
1369 default:
1370 /* Warn if any formats are not handled */
1371 _mesa_problem(NULL, "bad format %s in _mesa_uncompressed_format_to_type_and_comps",
1372 _mesa_get_format_name(format));
1373 assert(format == MESA_FORMAT_NONE ||
1374 _mesa_is_format_compressed(format));
1375 *datatype = 0;
1376 *comps = 1;
1377 }
1378 }
1379
1380 /**
1381 * Check if a mesa_format exactly matches a GL format/type combination
1382 * such that we can use memcpy() from one to the other.
1383 * \param mesa_format a MESA_FORMAT_x value
1384 * \param format the user-specified image format
1385 * \param type the user-specified image datatype
1386 * \param swapBytes typically the current pixel pack/unpack byteswap state
1387 * \param[out] error GL_NO_ERROR if format is an expected input.
1388 * GL_INVALID_ENUM if format is an unexpected input.
1389 * \return GL_TRUE if the formats match, GL_FALSE otherwise.
1390 */
1391 GLboolean
1392 _mesa_format_matches_format_and_type(mesa_format mesa_format,
1393 GLenum format, GLenum type,
1394 GLboolean swapBytes, GLenum *error)
1395 {
1396 const GLboolean littleEndian = _mesa_little_endian();
1397 if (error)
1398 *error = GL_NO_ERROR;
1399
1400 /* Note: When reading a GL format/type combination, the format lists channel
1401 * assignments from most significant channel in the type to least
1402 * significant. A type with _REV indicates that the assignments are
1403 * swapped, so they are listed from least significant to most significant.
1404 *
1405 * Compressed formats will fall through and return GL_FALSE.
1406 *
1407 * For sanity, please keep this switch statement ordered the same as the
1408 * enums in formats.h.
1409 */
1410
1411 switch (mesa_format) {
1412
1413 case MESA_FORMAT_NONE:
1414 case MESA_FORMAT_COUNT:
1415 return GL_FALSE;
1416
1417 case MESA_FORMAT_A8B8G8R8_UNORM:
1418 case MESA_FORMAT_A8B8G8R8_SRGB:
1419 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
1420 return GL_TRUE;
1421
1422 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8_REV && swapBytes)
1423 return GL_TRUE;
1424
1425 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE && !littleEndian)
1426 return GL_TRUE;
1427
1428 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8_REV
1429 && !swapBytes)
1430 return GL_TRUE;
1431
1432 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8
1433 && swapBytes)
1434 return GL_TRUE;
1435
1436 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_BYTE && littleEndian)
1437 return GL_TRUE;
1438
1439 return GL_FALSE;
1440
1441 case MESA_FORMAT_R8G8B8A8_UNORM:
1442 case MESA_FORMAT_R8G8B8A8_SRGB:
1443 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1444 !swapBytes)
1445 return GL_TRUE;
1446
1447 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1448 return GL_TRUE;
1449
1450 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE && littleEndian)
1451 return GL_TRUE;
1452
1453 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8 &&
1454 !swapBytes)
1455 return GL_TRUE;
1456
1457 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1458 swapBytes)
1459 return GL_TRUE;
1460
1461 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_BYTE && !littleEndian)
1462 return GL_TRUE;
1463
1464 return GL_FALSE;
1465
1466 case MESA_FORMAT_B8G8R8A8_UNORM:
1467 case MESA_FORMAT_B8G8R8A8_SRGB:
1468 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1469 !swapBytes)
1470 return GL_TRUE;
1471
1472 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1473 return GL_TRUE;
1474
1475 if (format == GL_BGRA && type == GL_UNSIGNED_BYTE && littleEndian)
1476 return GL_TRUE;
1477
1478 return GL_FALSE;
1479
1480 case MESA_FORMAT_A8R8G8B8_UNORM:
1481 case MESA_FORMAT_A8R8G8B8_SRGB:
1482 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
1483 return GL_TRUE;
1484
1485 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1486 swapBytes)
1487 return GL_TRUE;
1488
1489 if (format == GL_BGRA && type == GL_UNSIGNED_BYTE && !littleEndian)
1490 return GL_TRUE;
1491
1492 return GL_FALSE;
1493
1494 case MESA_FORMAT_X8B8G8R8_UNORM:
1495 case MESA_FORMAT_R8G8B8X8_UNORM:
1496 return GL_FALSE;
1497
1498 case MESA_FORMAT_B8G8R8X8_UNORM:
1499 case MESA_FORMAT_X8R8G8B8_UNORM:
1500 return GL_FALSE;
1501
1502 case MESA_FORMAT_BGR_UNORM8:
1503 case MESA_FORMAT_BGR_SRGB8:
1504 return format == GL_BGR && type == GL_UNSIGNED_BYTE && littleEndian;
1505
1506 case MESA_FORMAT_RGB_UNORM8:
1507 return format == GL_RGB && type == GL_UNSIGNED_BYTE && littleEndian;
1508
1509 case MESA_FORMAT_B5G6R5_UNORM:
1510 return ((format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5) ||
1511 (format == GL_BGR && type == GL_UNSIGNED_SHORT_5_6_5_REV)) &&
1512 !swapBytes;
1513
1514 case MESA_FORMAT_R5G6B5_UNORM:
1515 return ((format == GL_BGR && type == GL_UNSIGNED_SHORT_5_6_5) ||
1516 (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5_REV)) &&
1517 !swapBytes;
1518
1519 case MESA_FORMAT_B4G4R4A4_UNORM:
1520 return format == GL_BGRA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV &&
1521 !swapBytes;
1522
1523 case MESA_FORMAT_A4R4G4B4_UNORM:
1524 return GL_FALSE;
1525
1526 case MESA_FORMAT_A1B5G5R5_UNORM:
1527 return format == GL_RGBA && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1528 !swapBytes;
1529
1530 case MESA_FORMAT_X1B5G5R5_UNORM:
1531 return format == GL_RGB && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1532 !swapBytes;
1533
1534 case MESA_FORMAT_B5G5R5A1_UNORM:
1535 return format == GL_BGRA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV &&
1536 !swapBytes;
1537
1538 case MESA_FORMAT_A1R5G5B5_UNORM:
1539 return format == GL_BGRA && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1540 !swapBytes;
1541
1542 case MESA_FORMAT_L4A4_UNORM:
1543 return GL_FALSE;
1544 case MESA_FORMAT_L8A8_UNORM:
1545 case MESA_FORMAT_L8A8_SRGB:
1546 return format == GL_LUMINANCE_ALPHA && type == GL_UNSIGNED_BYTE && littleEndian;
1547 case MESA_FORMAT_A8L8_UNORM:
1548 case MESA_FORMAT_A8L8_SRGB:
1549 return GL_FALSE;
1550
1551 case MESA_FORMAT_L16A16_UNORM:
1552 return format == GL_LUMINANCE_ALPHA && type == GL_UNSIGNED_SHORT && littleEndian && !swapBytes;
1553 case MESA_FORMAT_A16L16_UNORM:
1554 return GL_FALSE;
1555
1556 case MESA_FORMAT_B2G3R3_UNORM:
1557 return format == GL_RGB && type == GL_UNSIGNED_BYTE_3_3_2;
1558
1559 case MESA_FORMAT_R3G3B2_UNORM:
1560 return format == GL_RGB && type == GL_UNSIGNED_BYTE_2_3_3_REV;
1561
1562 case MESA_FORMAT_A4B4G4R4_UNORM:
1563 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
1564 return GL_TRUE;
1565
1566 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && swapBytes)
1567 return GL_TRUE;
1568
1569 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !swapBytes)
1570 return GL_TRUE;
1571
1572 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4 && swapBytes)
1573 return GL_TRUE;
1574
1575 return GL_FALSE;
1576
1577 case MESA_FORMAT_R4G4B4A4_UNORM:
1578 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
1579 return GL_TRUE;
1580
1581 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && swapBytes)
1582 return GL_TRUE;
1583
1584 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !swapBytes)
1585 return GL_TRUE;
1586
1587 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4 && swapBytes)
1588 return GL_TRUE;
1589
1590 return GL_FALSE;
1591
1592 case MESA_FORMAT_R5G5B5A1_UNORM:
1593 return format == GL_RGBA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV;
1594
1595 case MESA_FORMAT_A2B10G10R10_UNORM:
1596 return format == GL_RGBA && type == GL_UNSIGNED_INT_10_10_10_2;
1597
1598 case MESA_FORMAT_A2B10G10R10_UINT:
1599 return format == GL_RGBA_INTEGER_EXT && type == GL_UNSIGNED_INT_10_10_10_2;
1600
1601 case MESA_FORMAT_A2R10G10B10_UNORM:
1602 return format == GL_BGRA && type == GL_UNSIGNED_INT_10_10_10_2;
1603
1604 case MESA_FORMAT_A2R10G10B10_UINT:
1605 return format == GL_BGRA_INTEGER_EXT && type == GL_UNSIGNED_INT_10_10_10_2;
1606
1607 case MESA_FORMAT_A_UNORM8:
1608 return format == GL_ALPHA && type == GL_UNSIGNED_BYTE;
1609 case MESA_FORMAT_A_UNORM16:
1610 return format == GL_ALPHA && type == GL_UNSIGNED_SHORT && !swapBytes;
1611 case MESA_FORMAT_L_UNORM8:
1612 case MESA_FORMAT_L_SRGB8:
1613 return format == GL_LUMINANCE && type == GL_UNSIGNED_BYTE;
1614 case MESA_FORMAT_L_UNORM16:
1615 return format == GL_LUMINANCE && type == GL_UNSIGNED_SHORT && !swapBytes;
1616 case MESA_FORMAT_I_UNORM8:
1617 return format == GL_RED && type == GL_UNSIGNED_BYTE;
1618 case MESA_FORMAT_I_UNORM16:
1619 return format == GL_RED && type == GL_UNSIGNED_SHORT && !swapBytes;
1620
1621 case MESA_FORMAT_YCBCR:
1622 return format == GL_YCBCR_MESA &&
1623 ((type == GL_UNSIGNED_SHORT_8_8_MESA && littleEndian != swapBytes) ||
1624 (type == GL_UNSIGNED_SHORT_8_8_REV_MESA && littleEndian == swapBytes));
1625 case MESA_FORMAT_YCBCR_REV:
1626 return format == GL_YCBCR_MESA &&
1627 ((type == GL_UNSIGNED_SHORT_8_8_MESA && littleEndian == swapBytes) ||
1628 (type == GL_UNSIGNED_SHORT_8_8_REV_MESA && littleEndian != swapBytes));
1629
1630 case MESA_FORMAT_R_UNORM8:
1631 return format == GL_RED && type == GL_UNSIGNED_BYTE;
1632 case MESA_FORMAT_R8G8_UNORM:
1633 return format == GL_RG && type == GL_UNSIGNED_BYTE && littleEndian;
1634 case MESA_FORMAT_G8R8_UNORM:
1635 return GL_FALSE;
1636
1637 case MESA_FORMAT_R_UNORM16:
1638 return format == GL_RED && type == GL_UNSIGNED_SHORT &&
1639 !swapBytes;
1640 case MESA_FORMAT_R16G16_UNORM:
1641 return format == GL_RG && type == GL_UNSIGNED_SHORT && littleEndian &&
1642 !swapBytes;
1643 case MESA_FORMAT_G16R16_UNORM:
1644 return GL_FALSE;
1645
1646 case MESA_FORMAT_B10G10R10A2_UNORM:
1647 return format == GL_BGRA && type == GL_UNSIGNED_INT_2_10_10_10_REV &&
1648 !swapBytes;
1649
1650 case MESA_FORMAT_S8_UINT_Z24_UNORM:
1651 return format == GL_DEPTH_STENCIL && type == GL_UNSIGNED_INT_24_8 &&
1652 !swapBytes;
1653 case MESA_FORMAT_X8_UINT_Z24_UNORM:
1654 case MESA_FORMAT_Z24_UNORM_S8_UINT:
1655 return GL_FALSE;
1656
1657 case MESA_FORMAT_Z_UNORM16:
1658 return format == GL_DEPTH_COMPONENT && type == GL_UNSIGNED_SHORT &&
1659 !swapBytes;
1660
1661 case MESA_FORMAT_Z24_UNORM_X8_UINT:
1662 return GL_FALSE;
1663
1664 case MESA_FORMAT_Z_UNORM32:
1665 return format == GL_DEPTH_COMPONENT && type == GL_UNSIGNED_INT &&
1666 !swapBytes;
1667
1668 case MESA_FORMAT_S_UINT8:
1669 return format == GL_STENCIL_INDEX && type == GL_UNSIGNED_BYTE;
1670
1671 case MESA_FORMAT_RGBA_FLOAT32:
1672 return format == GL_RGBA && type == GL_FLOAT && !swapBytes;
1673 case MESA_FORMAT_RGBA_FLOAT16:
1674 return format == GL_RGBA && type == GL_HALF_FLOAT && !swapBytes;
1675
1676 case MESA_FORMAT_RGB_FLOAT32:
1677 return format == GL_RGB && type == GL_FLOAT && !swapBytes;
1678 case MESA_FORMAT_RGB_FLOAT16:
1679 return format == GL_RGB && type == GL_HALF_FLOAT && !swapBytes;
1680
1681 case MESA_FORMAT_A_FLOAT32:
1682 return format == GL_ALPHA && type == GL_FLOAT && !swapBytes;
1683 case MESA_FORMAT_A_FLOAT16:
1684 return format == GL_ALPHA && type == GL_HALF_FLOAT && !swapBytes;
1685
1686 case MESA_FORMAT_L_FLOAT32:
1687 return format == GL_LUMINANCE && type == GL_FLOAT && !swapBytes;
1688 case MESA_FORMAT_L_FLOAT16:
1689 return format == GL_LUMINANCE && type == GL_HALF_FLOAT && !swapBytes;
1690
1691 case MESA_FORMAT_LA_FLOAT32:
1692 return format == GL_LUMINANCE_ALPHA && type == GL_FLOAT && !swapBytes;
1693 case MESA_FORMAT_LA_FLOAT16:
1694 return format == GL_LUMINANCE_ALPHA && type == GL_HALF_FLOAT && !swapBytes;
1695
1696 case MESA_FORMAT_I_FLOAT32:
1697 return format == GL_RED && type == GL_FLOAT && !swapBytes;
1698 case MESA_FORMAT_I_FLOAT16:
1699 return format == GL_RED && type == GL_HALF_FLOAT && !swapBytes;
1700
1701 case MESA_FORMAT_R_FLOAT32:
1702 return format == GL_RED && type == GL_FLOAT && !swapBytes;
1703 case MESA_FORMAT_R_FLOAT16:
1704 return format == GL_RED && type == GL_HALF_FLOAT && !swapBytes;
1705
1706 case MESA_FORMAT_RG_FLOAT32:
1707 return format == GL_RG && type == GL_FLOAT && !swapBytes;
1708 case MESA_FORMAT_RG_FLOAT16:
1709 return format == GL_RG && type == GL_HALF_FLOAT && !swapBytes;
1710
1711 case MESA_FORMAT_A_UINT8:
1712 return format == GL_ALPHA_INTEGER && type == GL_UNSIGNED_BYTE;
1713 case MESA_FORMAT_A_UINT16:
1714 return format == GL_ALPHA_INTEGER && type == GL_UNSIGNED_SHORT &&
1715 !swapBytes;
1716 case MESA_FORMAT_A_UINT32:
1717 return format == GL_ALPHA_INTEGER && type == GL_UNSIGNED_INT &&
1718 !swapBytes;
1719 case MESA_FORMAT_A_SINT8:
1720 return format == GL_ALPHA_INTEGER && type == GL_BYTE;
1721 case MESA_FORMAT_A_SINT16:
1722 return format == GL_ALPHA_INTEGER && type == GL_SHORT && !swapBytes;
1723 case MESA_FORMAT_A_SINT32:
1724 return format == GL_ALPHA_INTEGER && type == GL_INT && !swapBytes;
1725
1726 case MESA_FORMAT_I_UINT8:
1727 return format == GL_RED_INTEGER && type == GL_UNSIGNED_BYTE;
1728 case MESA_FORMAT_I_UINT16:
1729 return format == GL_RED_INTEGER && type == GL_UNSIGNED_SHORT && !swapBytes;
1730 case MESA_FORMAT_I_UINT32:
1731 return format == GL_RED_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1732 case MESA_FORMAT_I_SINT8:
1733 return format == GL_RED_INTEGER && type == GL_BYTE;
1734 case MESA_FORMAT_I_SINT16:
1735 return format == GL_RED_INTEGER && type == GL_SHORT && !swapBytes;
1736 case MESA_FORMAT_I_SINT32:
1737 return format == GL_RED_INTEGER && type == GL_INT && !swapBytes;
1738
1739 case MESA_FORMAT_L_UINT8:
1740 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_UNSIGNED_BYTE;
1741 case MESA_FORMAT_L_UINT16:
1742 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_UNSIGNED_SHORT &&
1743 !swapBytes;
1744 case MESA_FORMAT_L_UINT32:
1745 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_UNSIGNED_INT &&
1746 !swapBytes;
1747 case MESA_FORMAT_L_SINT8:
1748 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_BYTE;
1749 case MESA_FORMAT_L_SINT16:
1750 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_SHORT &&
1751 !swapBytes;
1752 case MESA_FORMAT_L_SINT32:
1753 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_INT && !swapBytes;
1754
1755 case MESA_FORMAT_LA_UINT8:
1756 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT &&
1757 type == GL_UNSIGNED_BYTE && !swapBytes;
1758 case MESA_FORMAT_LA_UINT16:
1759 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT &&
1760 type == GL_UNSIGNED_SHORT && !swapBytes;
1761 case MESA_FORMAT_LA_UINT32:
1762 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT &&
1763 type == GL_UNSIGNED_INT && !swapBytes;
1764 case MESA_FORMAT_LA_SINT8:
1765 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT && type == GL_BYTE &&
1766 !swapBytes;
1767 case MESA_FORMAT_LA_SINT16:
1768 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT && type == GL_SHORT &&
1769 !swapBytes;
1770 case MESA_FORMAT_LA_SINT32:
1771 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT && type == GL_INT &&
1772 !swapBytes;
1773
1774 case MESA_FORMAT_R_SINT8:
1775 return format == GL_RED_INTEGER && type == GL_BYTE;
1776 case MESA_FORMAT_RG_SINT8:
1777 return format == GL_RG_INTEGER && type == GL_BYTE && !swapBytes;
1778 case MESA_FORMAT_RGB_SINT8:
1779 return format == GL_RGB_INTEGER && type == GL_BYTE && !swapBytes;
1780 case MESA_FORMAT_RGBA_SINT8:
1781 return format == GL_RGBA_INTEGER && type == GL_BYTE && !swapBytes;
1782 case MESA_FORMAT_R_SINT16:
1783 return format == GL_RED_INTEGER && type == GL_SHORT && !swapBytes;
1784 case MESA_FORMAT_RG_SINT16:
1785 return format == GL_RG_INTEGER && type == GL_SHORT && !swapBytes;
1786 case MESA_FORMAT_RGB_SINT16:
1787 return format == GL_RGB_INTEGER && type == GL_SHORT && !swapBytes;
1788 case MESA_FORMAT_RGBA_SINT16:
1789 return format == GL_RGBA_INTEGER && type == GL_SHORT && !swapBytes;
1790 case MESA_FORMAT_R_SINT32:
1791 return format == GL_RED_INTEGER && type == GL_INT && !swapBytes;
1792 case MESA_FORMAT_RG_SINT32:
1793 return format == GL_RG_INTEGER && type == GL_INT && !swapBytes;
1794 case MESA_FORMAT_RGB_SINT32:
1795 return format == GL_RGB_INTEGER && type == GL_INT && !swapBytes;
1796 case MESA_FORMAT_RGBA_SINT32:
1797 return format == GL_RGBA_INTEGER && type == GL_INT && !swapBytes;
1798
1799 case MESA_FORMAT_R_UINT8:
1800 return format == GL_RED_INTEGER && type == GL_UNSIGNED_BYTE;
1801 case MESA_FORMAT_RG_UINT8:
1802 return format == GL_RG_INTEGER && type == GL_UNSIGNED_BYTE && !swapBytes;
1803 case MESA_FORMAT_RGB_UINT8:
1804 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_BYTE && !swapBytes;
1805 case MESA_FORMAT_RGBA_UINT8:
1806 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_BYTE &&
1807 !swapBytes;
1808 case MESA_FORMAT_R_UINT16:
1809 return format == GL_RED_INTEGER && type == GL_UNSIGNED_SHORT &&
1810 !swapBytes;
1811 case MESA_FORMAT_RG_UINT16:
1812 return format == GL_RG_INTEGER && type == GL_UNSIGNED_SHORT && !swapBytes;
1813 case MESA_FORMAT_RGB_UINT16:
1814 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_SHORT &&
1815 !swapBytes;
1816 case MESA_FORMAT_RGBA_UINT16:
1817 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT &&
1818 !swapBytes;
1819 case MESA_FORMAT_R_UINT32:
1820 return format == GL_RED_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1821 case MESA_FORMAT_RG_UINT32:
1822 return format == GL_RG_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1823 case MESA_FORMAT_RGB_UINT32:
1824 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1825 case MESA_FORMAT_RGBA_UINT32:
1826 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1827
1828 case MESA_FORMAT_R_SNORM8:
1829 return format == GL_RED && type == GL_BYTE;
1830 case MESA_FORMAT_R8G8_SNORM:
1831 return format == GL_RG && type == GL_BYTE && littleEndian &&
1832 !swapBytes;
1833 case MESA_FORMAT_X8B8G8R8_SNORM:
1834 return GL_FALSE;
1835
1836 case MESA_FORMAT_A8B8G8R8_SNORM:
1837 if (format == GL_RGBA && type == GL_BYTE && !littleEndian)
1838 return GL_TRUE;
1839
1840 if (format == GL_ABGR_EXT && type == GL_BYTE && littleEndian)
1841 return GL_TRUE;
1842
1843 return GL_FALSE;
1844
1845 case MESA_FORMAT_R8G8B8A8_SNORM:
1846 if (format == GL_RGBA && type == GL_BYTE && littleEndian)
1847 return GL_TRUE;
1848
1849 if (format == GL_ABGR_EXT && type == GL_BYTE && !littleEndian)
1850 return GL_TRUE;
1851
1852 return GL_FALSE;
1853
1854 case MESA_FORMAT_R_SNORM16:
1855 return format == GL_RED && type == GL_SHORT &&
1856 !swapBytes;
1857 case MESA_FORMAT_R16G16_SNORM:
1858 return format == GL_RG && type == GL_SHORT && littleEndian && !swapBytes;
1859 case MESA_FORMAT_RGB_SNORM16:
1860 return format == GL_RGB && type == GL_SHORT && !swapBytes;
1861 case MESA_FORMAT_RGBA_SNORM16:
1862 return format == GL_RGBA && type == GL_SHORT && !swapBytes;
1863 case MESA_FORMAT_RGBA_UNORM16:
1864 return format == GL_RGBA && type == GL_UNSIGNED_SHORT &&
1865 !swapBytes;
1866
1867 case MESA_FORMAT_A_SNORM8:
1868 return format == GL_ALPHA && type == GL_BYTE;
1869 case MESA_FORMAT_L_SNORM8:
1870 return format == GL_LUMINANCE && type == GL_BYTE;
1871 case MESA_FORMAT_L8A8_SNORM:
1872 return format == GL_LUMINANCE_ALPHA && type == GL_BYTE &&
1873 littleEndian && !swapBytes;
1874 case MESA_FORMAT_A8L8_SNORM:
1875 return format == GL_LUMINANCE_ALPHA && type == GL_BYTE &&
1876 !littleEndian && !swapBytes;
1877 case MESA_FORMAT_I_SNORM8:
1878 return format == GL_RED && type == GL_BYTE;
1879 case MESA_FORMAT_A_SNORM16:
1880 return format == GL_ALPHA && type == GL_SHORT && !swapBytes;
1881 case MESA_FORMAT_L_SNORM16:
1882 return format == GL_LUMINANCE && type == GL_SHORT && !swapBytes;
1883 case MESA_FORMAT_LA_SNORM16:
1884 return format == GL_LUMINANCE_ALPHA && type == GL_SHORT &&
1885 littleEndian && !swapBytes;
1886 case MESA_FORMAT_I_SNORM16:
1887 return format == GL_RED && type == GL_SHORT && littleEndian &&
1888 !swapBytes;
1889
1890 case MESA_FORMAT_B10G10R10A2_UINT:
1891 return (format == GL_BGRA_INTEGER_EXT &&
1892 type == GL_UNSIGNED_INT_2_10_10_10_REV &&
1893 !swapBytes);
1894
1895 case MESA_FORMAT_R10G10B10A2_UINT:
1896 return (format == GL_RGBA_INTEGER_EXT &&
1897 type == GL_UNSIGNED_INT_2_10_10_10_REV &&
1898 !swapBytes);
1899
1900 case MESA_FORMAT_B5G6R5_UINT:
1901 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_SHORT_5_6_5;
1902
1903 case MESA_FORMAT_R5G6B5_UINT:
1904 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_SHORT_5_6_5_REV;
1905
1906 case MESA_FORMAT_B2G3R3_UINT:
1907 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_BYTE_3_3_2;
1908
1909 case MESA_FORMAT_R3G3B2_UINT:
1910 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_BYTE_2_3_3_REV;
1911
1912 case MESA_FORMAT_A4B4G4R4_UINT:
1913 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
1914 return GL_TRUE;
1915
1916 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && swapBytes)
1917 return GL_TRUE;
1918 return GL_FALSE;
1919
1920 case MESA_FORMAT_R4G4B4A4_UINT:
1921 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !swapBytes)
1922 return GL_TRUE;
1923
1924 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4 && swapBytes)
1925 return GL_TRUE;
1926
1927 return GL_FALSE;
1928
1929 case MESA_FORMAT_B4G4R4A4_UINT:
1930 return format == GL_BGRA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4_REV &&
1931 !swapBytes;
1932
1933 case MESA_FORMAT_A4R4G4B4_UINT:
1934 return GL_FALSE;
1935
1936 case MESA_FORMAT_A1B5G5R5_UINT:
1937 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1938 !swapBytes;
1939
1940 case MESA_FORMAT_B5G5R5A1_UINT:
1941 return format == GL_BGRA_INTEGER && type == GL_UNSIGNED_SHORT_1_5_5_5_REV &&
1942 !swapBytes;
1943
1944 case MESA_FORMAT_A1R5G5B5_UINT:
1945 return format == GL_BGRA_INTEGER && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1946 !swapBytes;
1947
1948 case MESA_FORMAT_R5G5B5A1_UINT:
1949 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_1_5_5_5_REV;
1950
1951 case MESA_FORMAT_A8B8G8R8_UINT:
1952 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
1953 return GL_TRUE;
1954
1955 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8_REV && swapBytes)
1956 return GL_TRUE;
1957 return GL_FALSE;
1958
1959 case MESA_FORMAT_A8R8G8B8_UINT:
1960 if (format == GL_BGRA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8 &&
1961 !swapBytes)
1962 return GL_TRUE;
1963
1964 if (format == GL_BGRA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1965 swapBytes)
1966 return GL_TRUE;
1967
1968 return GL_FALSE;
1969
1970 case MESA_FORMAT_R8G8B8A8_UINT:
1971 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1972 !swapBytes)
1973 return GL_TRUE;
1974
1975 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1976 return GL_TRUE;
1977
1978 return GL_FALSE;
1979
1980 case MESA_FORMAT_B8G8R8A8_UINT:
1981 if (format == GL_BGRA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1982 !swapBytes)
1983 return GL_TRUE;
1984
1985 if (format == GL_BGRA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1986 return GL_TRUE;
1987
1988 return GL_FALSE;
1989
1990 case MESA_FORMAT_R9G9B9E5_FLOAT:
1991 return format == GL_RGB && type == GL_UNSIGNED_INT_5_9_9_9_REV &&
1992 !swapBytes;
1993
1994 case MESA_FORMAT_R11G11B10_FLOAT:
1995 return format == GL_RGB && type == GL_UNSIGNED_INT_10F_11F_11F_REV &&
1996 !swapBytes;
1997
1998 case MESA_FORMAT_Z_FLOAT32:
1999 return format == GL_DEPTH_COMPONENT && type == GL_FLOAT && !swapBytes;
2000
2001 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
2002 return format == GL_DEPTH_STENCIL &&
2003 type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV && !swapBytes;
2004
2005 case MESA_FORMAT_B4G4R4X4_UNORM:
2006 case MESA_FORMAT_B5G5R5X1_UNORM:
2007 case MESA_FORMAT_R8G8B8X8_SNORM:
2008 case MESA_FORMAT_R8G8B8X8_SRGB:
2009 case MESA_FORMAT_X8B8G8R8_SRGB:
2010 case MESA_FORMAT_RGBX_UINT8:
2011 case MESA_FORMAT_RGBX_SINT8:
2012 case MESA_FORMAT_B10G10R10X2_UNORM:
2013 case MESA_FORMAT_RGBX_UNORM16:
2014 case MESA_FORMAT_RGBX_SNORM16:
2015 case MESA_FORMAT_RGBX_FLOAT16:
2016 case MESA_FORMAT_RGBX_UINT16:
2017 case MESA_FORMAT_RGBX_SINT16:
2018 case MESA_FORMAT_RGBX_FLOAT32:
2019 case MESA_FORMAT_RGBX_UINT32:
2020 case MESA_FORMAT_RGBX_SINT32:
2021 return GL_FALSE;
2022
2023 case MESA_FORMAT_R10G10B10X2_UNORM:
2024 return format == GL_RGB && type == GL_UNSIGNED_INT_2_10_10_10_REV &&
2025 !swapBytes;
2026 case MESA_FORMAT_R10G10B10A2_UNORM:
2027 return format == GL_RGBA && type == GL_UNSIGNED_INT_2_10_10_10_REV &&
2028 !swapBytes;
2029
2030 case MESA_FORMAT_G8R8_SNORM:
2031 return format == GL_RG && type == GL_BYTE && !littleEndian &&
2032 !swapBytes;
2033
2034 case MESA_FORMAT_G16R16_SNORM:
2035 return format == GL_RG && type == GL_SHORT && !littleEndian &&
2036 !swapBytes;
2037
2038 case MESA_FORMAT_B8G8R8X8_SRGB:
2039 case MESA_FORMAT_X8R8G8B8_SRGB:
2040 return GL_FALSE;
2041 default:
2042 assert(_mesa_is_format_compressed(mesa_format));
2043 if (error)
2044 *error = GL_INVALID_ENUM;
2045 }
2046 return GL_FALSE;
2047 }
2048