fdb53afd5701b1b3fb6f7dbdff876fc756cddf01
[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 gl_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 GLubyte RedBits;
61 GLubyte GreenBits;
62 GLubyte BlueBits;
63 GLubyte AlphaBits;
64 GLubyte LuminanceBits;
65 GLubyte IntensityBits;
66 GLubyte DepthBits;
67 GLubyte StencilBits;
68
69 bool IsSRGBFormat;
70
71 /**
72 * To describe compressed formats. If not compressed, Width=Height=Depth=1.
73 */
74 GLubyte BlockWidth, BlockHeight, BlockDepth;
75 GLubyte BytesPerBlock;
76
77 uint8_t Swizzle[4];
78 mesa_array_format ArrayFormat;
79 };
80
81 #include "format_info.h"
82
83 static const struct gl_format_info *
84 _mesa_get_format_info(mesa_format format)
85 {
86 const struct gl_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 gl_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: not GLuint, so as not to coerce math to unsigned. cf. fdo #37351
109 */
110 GLint
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 gl_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 gl_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 GLuint
187 _mesa_get_format_max_bits(mesa_format format)
188 {
189 const struct gl_format_info *info = _mesa_get_format_info(format);
190 GLuint 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 gl_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 gl_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 gl_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, GLuint *bw, GLuint *bh)
322 {
323 const struct gl_format_info *info = _mesa_get_format_info(format);
324 /* Use _mesa_get_format_block_size_3d() for 3D blocks. */
325 assert(info->BlockDepth == 1);
326
327 *bw = info->BlockWidth;
328 *bh = info->BlockHeight;
329 }
330
331
332 /**
333 * Return the block size (in pixels) for the given format. Normally
334 * the block size is 1x1x1. But compressed formats will have block
335 * sizes of 4x4x4, 3x3x3 pixels, etc.
336 * \param bw returns block width in pixels
337 * \param bh returns block height in pixels
338 * \param bd returns block depth in pixels
339 */
340 void
341 _mesa_get_format_block_size_3d(mesa_format format,
342 GLuint *bw,
343 GLuint *bh,
344 GLuint *bd)
345 {
346 const struct gl_format_info *info = _mesa_get_format_info(format);
347 *bw = info->BlockWidth;
348 *bh = info->BlockHeight;
349 *bd = info->BlockDepth;
350 }
351
352
353 /**
354 * Returns the an array of four numbers representing the transformation
355 * from the RGBA or SZ colorspace to the given format. For array formats,
356 * the i'th RGBA component is given by:
357 *
358 * if (swizzle[i] <= MESA_FORMAT_SWIZZLE_W)
359 * comp = data[swizzle[i]];
360 * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_ZERO)
361 * comp = 0;
362 * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_ONE)
363 * comp = 1;
364 * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_NONE)
365 * // data does not contain a channel of this format
366 *
367 * For packed formats, the swizzle gives the number of components left of
368 * the least significant bit.
369 *
370 * Compressed formats have no swizzle.
371 */
372 void
373 _mesa_get_format_swizzle(mesa_format format, uint8_t swizzle_out[4])
374 {
375 const struct gl_format_info *info = _mesa_get_format_info(format);
376 memcpy(swizzle_out, info->Swizzle, sizeof(info->Swizzle));
377 }
378
379 mesa_array_format
380 _mesa_array_format_flip_channels(mesa_array_format format)
381 {
382 int num_channels;
383 uint8_t swizzle[4];
384
385 num_channels = _mesa_array_format_get_num_channels(format);
386 _mesa_array_format_get_swizzle(format, swizzle);
387
388 if (num_channels == 1)
389 return format;
390
391 if (num_channels == 2) {
392 /* Assert that the swizzle makes sense for 2 channels */
393 for (unsigned i = 0; i < 4; i++)
394 assert(swizzle[i] != 2 && swizzle[i] != 3);
395
396 static const uint8_t flip_xy[6] = { 1, 0, 2, 3, 4, 5 };
397 _mesa_array_format_set_swizzle(&format,
398 flip_xy[swizzle[0]], flip_xy[swizzle[1]],
399 flip_xy[swizzle[2]], flip_xy[swizzle[3]]);
400 return format;
401 }
402
403 if (num_channels == 4) {
404 static const uint8_t flip[6] = { 3, 2, 1, 0, 4, 5 };
405 _mesa_array_format_set_swizzle(&format,
406 flip[swizzle[0]], flip[swizzle[1]],
407 flip[swizzle[2]], flip[swizzle[3]]);
408 return format;
409 }
410
411 unreachable("Invalid array format");
412 }
413
414 uint32_t
415 _mesa_format_to_array_format(mesa_format format)
416 {
417 const struct gl_format_info *info = _mesa_get_format_info(format);
418 if (info->ArrayFormat && !_mesa_little_endian() &&
419 info->Layout == MESA_FORMAT_LAYOUT_PACKED)
420 return _mesa_array_format_flip_channels(info->ArrayFormat);
421 else
422 return info->ArrayFormat;
423 }
424
425 static struct hash_table *format_array_format_table;
426 static once_flag format_array_format_table_exists = ONCE_FLAG_INIT;
427
428 static bool
429 array_formats_equal(const void *a, const void *b)
430 {
431 return (intptr_t)a == (intptr_t)b;
432 }
433
434 static void
435 format_array_format_table_init(void)
436 {
437 const struct gl_format_info *info;
438 mesa_array_format array_format;
439 unsigned f;
440
441 format_array_format_table = _mesa_hash_table_create(NULL, NULL,
442 array_formats_equal);
443
444 if (!format_array_format_table) {
445 _mesa_error_no_memory(__func__);
446 return;
447 }
448
449 for (f = 1; f < MESA_FORMAT_COUNT; ++f) {
450 info = _mesa_get_format_info(f);
451 if (!info->ArrayFormat)
452 continue;
453
454 if (_mesa_little_endian()) {
455 array_format = info->ArrayFormat;
456 } else {
457 array_format = _mesa_array_format_flip_channels(info->ArrayFormat);
458 }
459
460 /* This can happen and does for some of the BGR formats. Let's take
461 * the first one in the list.
462 */
463 if (_mesa_hash_table_search_pre_hashed(format_array_format_table,
464 array_format,
465 (void *)(intptr_t)array_format))
466 continue;
467
468 _mesa_hash_table_insert_pre_hashed(format_array_format_table,
469 array_format,
470 (void *)(intptr_t)array_format,
471 (void *)(intptr_t)f);
472 }
473 }
474
475 mesa_format
476 _mesa_format_from_array_format(uint32_t array_format)
477 {
478 struct hash_entry *entry;
479
480 assert(_mesa_format_is_mesa_array_format(array_format));
481
482 call_once(&format_array_format_table_exists, format_array_format_table_init);
483
484 if (!format_array_format_table) {
485 static const once_flag once_flag_init = ONCE_FLAG_INIT;
486 format_array_format_table_exists = once_flag_init;
487 return MESA_FORMAT_NONE;
488 }
489
490 entry = _mesa_hash_table_search_pre_hashed(format_array_format_table,
491 array_format,
492 (void *)(intptr_t)array_format);
493 if (entry)
494 return (intptr_t)entry->data;
495 else
496 return MESA_FORMAT_NONE;
497 }
498
499 /** Is the given format a compressed format? */
500 GLboolean
501 _mesa_is_format_compressed(mesa_format format)
502 {
503 const struct gl_format_info *info = _mesa_get_format_info(format);
504 return info->BlockWidth > 1 || info->BlockHeight > 1;
505 }
506
507
508 /**
509 * Determine if the given format represents a packed depth/stencil buffer.
510 */
511 GLboolean
512 _mesa_is_format_packed_depth_stencil(mesa_format format)
513 {
514 const struct gl_format_info *info = _mesa_get_format_info(format);
515
516 return info->BaseFormat == GL_DEPTH_STENCIL;
517 }
518
519
520 /**
521 * Is the given format a signed/unsigned integer color format?
522 */
523 GLboolean
524 _mesa_is_format_integer_color(mesa_format format)
525 {
526 const struct gl_format_info *info = _mesa_get_format_info(format);
527 return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT) &&
528 info->BaseFormat != GL_DEPTH_COMPONENT &&
529 info->BaseFormat != GL_DEPTH_STENCIL &&
530 info->BaseFormat != GL_STENCIL_INDEX;
531 }
532
533
534 /**
535 * Is the given format an unsigned integer format?
536 */
537 GLboolean
538 _mesa_is_format_unsigned(mesa_format format)
539 {
540 const struct gl_format_info *info = _mesa_get_format_info(format);
541 return _mesa_is_type_unsigned(info->DataType);
542 }
543
544
545 /**
546 * Does the given format store signed values?
547 */
548 GLboolean
549 _mesa_is_format_signed(mesa_format format)
550 {
551 if (format == MESA_FORMAT_R11G11B10_FLOAT ||
552 format == MESA_FORMAT_R9G9B9E5_FLOAT) {
553 /* these packed float formats only store unsigned values */
554 return GL_FALSE;
555 }
556 else {
557 const struct gl_format_info *info = _mesa_get_format_info(format);
558 return (info->DataType == GL_SIGNED_NORMALIZED ||
559 info->DataType == GL_INT ||
560 info->DataType == GL_FLOAT);
561 }
562 }
563
564 /**
565 * Is the given format an integer format?
566 */
567 GLboolean
568 _mesa_is_format_integer(mesa_format format)
569 {
570 const struct gl_format_info *info = _mesa_get_format_info(format);
571 return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT);
572 }
573
574
575 /**
576 * Return true if the given format is a color format.
577 */
578 GLenum
579 _mesa_is_format_color_format(mesa_format format)
580 {
581 const struct gl_format_info *info = _mesa_get_format_info(format);
582 switch (info->BaseFormat) {
583 case GL_DEPTH_COMPONENT:
584 case GL_STENCIL_INDEX:
585 case GL_DEPTH_STENCIL:
586 return false;
587 default:
588 return true;
589 }
590 }
591
592
593 /**
594 * Return color encoding for given format.
595 * \return GL_LINEAR or GL_SRGB
596 */
597 GLenum
598 _mesa_get_format_color_encoding(mesa_format format)
599 {
600 const struct gl_format_info *info = _mesa_get_format_info(format);
601 return info->IsSRGBFormat ? GL_SRGB : GL_LINEAR;
602 }
603
604
605 /**
606 * Return TRUE if format is an ETC2 compressed format specified
607 * by GL_ARB_ES3_compatibility.
608 */
609 bool
610 _mesa_is_format_etc2(mesa_format format)
611 {
612 switch (format) {
613 case MESA_FORMAT_ETC2_RGB8:
614 case MESA_FORMAT_ETC2_SRGB8:
615 case MESA_FORMAT_ETC2_RGBA8_EAC:
616 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
617 case MESA_FORMAT_ETC2_R11_EAC:
618 case MESA_FORMAT_ETC2_RG11_EAC:
619 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
620 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
621 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
622 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
623 return GL_TRUE;
624 default:
625 return GL_FALSE;
626 }
627 }
628
629
630 /**
631 * If the given format is a compressed format, return a corresponding
632 * uncompressed format.
633 */
634 mesa_format
635 _mesa_get_uncompressed_format(mesa_format format)
636 {
637 switch (format) {
638 case MESA_FORMAT_RGB_FXT1:
639 return MESA_FORMAT_BGR_UNORM8;
640 case MESA_FORMAT_RGBA_FXT1:
641 return MESA_FORMAT_A8B8G8R8_UNORM;
642 case MESA_FORMAT_RGB_DXT1:
643 case MESA_FORMAT_SRGB_DXT1:
644 return MESA_FORMAT_BGR_UNORM8;
645 case MESA_FORMAT_RGBA_DXT1:
646 case MESA_FORMAT_SRGBA_DXT1:
647 return MESA_FORMAT_A8B8G8R8_UNORM;
648 case MESA_FORMAT_RGBA_DXT3:
649 case MESA_FORMAT_SRGBA_DXT3:
650 return MESA_FORMAT_A8B8G8R8_UNORM;
651 case MESA_FORMAT_RGBA_DXT5:
652 case MESA_FORMAT_SRGBA_DXT5:
653 return MESA_FORMAT_A8B8G8R8_UNORM;
654 case MESA_FORMAT_R_RGTC1_UNORM:
655 return MESA_FORMAT_R_UNORM8;
656 case MESA_FORMAT_R_RGTC1_SNORM:
657 return MESA_FORMAT_R_SNORM8;
658 case MESA_FORMAT_RG_RGTC2_UNORM:
659 return MESA_FORMAT_R8G8_UNORM;
660 case MESA_FORMAT_RG_RGTC2_SNORM:
661 return MESA_FORMAT_R8G8_SNORM;
662 case MESA_FORMAT_L_LATC1_UNORM:
663 return MESA_FORMAT_L_UNORM8;
664 case MESA_FORMAT_L_LATC1_SNORM:
665 return MESA_FORMAT_L_SNORM8;
666 case MESA_FORMAT_LA_LATC2_UNORM:
667 return MESA_FORMAT_L8A8_UNORM;
668 case MESA_FORMAT_LA_LATC2_SNORM:
669 return MESA_FORMAT_L8A8_SNORM;
670 case MESA_FORMAT_ETC1_RGB8:
671 case MESA_FORMAT_ETC2_RGB8:
672 case MESA_FORMAT_ETC2_SRGB8:
673 return MESA_FORMAT_BGR_UNORM8;
674 case MESA_FORMAT_ETC2_RGBA8_EAC:
675 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
676 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
677 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
678 return MESA_FORMAT_A8B8G8R8_UNORM;
679 case MESA_FORMAT_ETC2_R11_EAC:
680 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
681 return MESA_FORMAT_R_UNORM16;
682 case MESA_FORMAT_ETC2_RG11_EAC:
683 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
684 return MESA_FORMAT_R16G16_UNORM;
685 case MESA_FORMAT_BPTC_RGBA_UNORM:
686 case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
687 return MESA_FORMAT_A8B8G8R8_UNORM;
688 case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
689 case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
690 return MESA_FORMAT_RGB_FLOAT32;
691 default:
692 #ifdef DEBUG
693 assert(!_mesa_is_format_compressed(format));
694 #endif
695 return format;
696 }
697 }
698
699
700 GLuint
701 _mesa_format_num_components(mesa_format format)
702 {
703 const struct gl_format_info *info = _mesa_get_format_info(format);
704 return ((info->RedBits > 0) +
705 (info->GreenBits > 0) +
706 (info->BlueBits > 0) +
707 (info->AlphaBits > 0) +
708 (info->LuminanceBits > 0) +
709 (info->IntensityBits > 0) +
710 (info->DepthBits > 0) +
711 (info->StencilBits > 0));
712 }
713
714
715 /**
716 * Returns true if a color format has data stored in the R/G/B/A channels,
717 * given an index from 0 to 3.
718 */
719 bool
720 _mesa_format_has_color_component(mesa_format format, int component)
721 {
722 const struct gl_format_info *info = _mesa_get_format_info(format);
723
724 assert(info->BaseFormat != GL_DEPTH_COMPONENT &&
725 info->BaseFormat != GL_DEPTH_STENCIL &&
726 info->BaseFormat != GL_STENCIL_INDEX);
727
728 switch (component) {
729 case 0:
730 return (info->RedBits + info->IntensityBits + info->LuminanceBits) > 0;
731 case 1:
732 return (info->GreenBits + info->IntensityBits + info->LuminanceBits) > 0;
733 case 2:
734 return (info->BlueBits + info->IntensityBits + info->LuminanceBits) > 0;
735 case 3:
736 return (info->AlphaBits + info->IntensityBits) > 0;
737 default:
738 assert(!"Invalid color component: must be 0..3");
739 return false;
740 }
741 }
742
743
744 /**
745 * Return number of bytes needed to store an image of the given size
746 * in the given format.
747 */
748 GLuint
749 _mesa_format_image_size(mesa_format format, GLsizei width,
750 GLsizei height, GLsizei depth)
751 {
752 const struct gl_format_info *info = _mesa_get_format_info(format);
753 GLuint sz;
754 /* Strictly speaking, a conditional isn't needed here */
755 if (info->BlockWidth > 1 || info->BlockHeight > 1 || info->BlockDepth > 1) {
756 /* compressed format (2D only for now) */
757 const GLuint bw = info->BlockWidth;
758 const GLuint bh = info->BlockHeight;
759 const GLuint bd = info->BlockDepth;
760 const GLuint wblocks = (width + bw - 1) / bw;
761 const GLuint hblocks = (height + bh - 1) / bh;
762 const GLuint dblocks = (depth + bd - 1) / bd;
763 sz = wblocks * hblocks * dblocks * info->BytesPerBlock;
764 } else
765 /* non-compressed */
766 sz = width * height * depth * info->BytesPerBlock;
767
768 return sz;
769 }
770
771
772 /**
773 * Same as _mesa_format_image_size() but returns a 64-bit value to
774 * accommodate very large textures.
775 */
776 uint64_t
777 _mesa_format_image_size64(mesa_format format, GLsizei width,
778 GLsizei height, GLsizei depth)
779 {
780 const struct gl_format_info *info = _mesa_get_format_info(format);
781 uint64_t sz;
782 /* Strictly speaking, a conditional isn't needed here */
783 if (info->BlockWidth > 1 || info->BlockHeight > 1 || info->BlockDepth > 1) {
784 /* compressed format (2D only for now) */
785 const uint64_t bw = info->BlockWidth;
786 const uint64_t bh = info->BlockHeight;
787 const uint64_t bd = info->BlockDepth;
788 const uint64_t wblocks = (width + bw - 1) / bw;
789 const uint64_t hblocks = (height + bh - 1) / bh;
790 const uint64_t dblocks = (depth + bd - 1) / bd;
791 sz = wblocks * hblocks * dblocks * info->BytesPerBlock;
792 } else
793 /* non-compressed */
794 sz = ((uint64_t) width * (uint64_t) height *
795 (uint64_t) depth * info->BytesPerBlock);
796
797 return sz;
798 }
799
800
801
802 GLint
803 _mesa_format_row_stride(mesa_format format, GLsizei width)
804 {
805 const struct gl_format_info *info = _mesa_get_format_info(format);
806 /* Strictly speaking, a conditional isn't needed here */
807 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
808 /* compressed format */
809 const GLuint bw = info->BlockWidth;
810 const GLuint wblocks = (width + bw - 1) / bw;
811 const GLint stride = wblocks * info->BytesPerBlock;
812 return stride;
813 }
814 else {
815 const GLint stride = width * info->BytesPerBlock;
816 return stride;
817 }
818 }
819
820
821
822 /**
823 * Return datatype and number of components per texel for the given
824 * uncompressed mesa_format. Only used for mipmap generation code.
825 */
826 void
827 _mesa_uncompressed_format_to_type_and_comps(mesa_format format,
828 GLenum *datatype, GLuint *comps)
829 {
830 switch (format) {
831 case MESA_FORMAT_A8B8G8R8_UNORM:
832 case MESA_FORMAT_R8G8B8A8_UNORM:
833 case MESA_FORMAT_B8G8R8A8_UNORM:
834 case MESA_FORMAT_A8R8G8B8_UNORM:
835 case MESA_FORMAT_X8B8G8R8_UNORM:
836 case MESA_FORMAT_R8G8B8X8_UNORM:
837 case MESA_FORMAT_B8G8R8X8_UNORM:
838 case MESA_FORMAT_X8R8G8B8_UNORM:
839 case MESA_FORMAT_A8B8G8R8_UINT:
840 case MESA_FORMAT_R8G8B8A8_UINT:
841 case MESA_FORMAT_B8G8R8A8_UINT:
842 case MESA_FORMAT_A8R8G8B8_UINT:
843 *datatype = GL_UNSIGNED_BYTE;
844 *comps = 4;
845 return;
846 case MESA_FORMAT_BGR_UNORM8:
847 case MESA_FORMAT_RGB_UNORM8:
848 *datatype = GL_UNSIGNED_BYTE;
849 *comps = 3;
850 return;
851 case MESA_FORMAT_B5G6R5_UNORM:
852 case MESA_FORMAT_R5G6B5_UNORM:
853 case MESA_FORMAT_B5G6R5_UINT:
854 case MESA_FORMAT_R5G6B5_UINT:
855 *datatype = GL_UNSIGNED_SHORT_5_6_5;
856 *comps = 3;
857 return;
858
859 case MESA_FORMAT_B4G4R4A4_UNORM:
860 case MESA_FORMAT_A4R4G4B4_UNORM:
861 case MESA_FORMAT_B4G4R4X4_UNORM:
862 case MESA_FORMAT_B4G4R4A4_UINT:
863 case MESA_FORMAT_A4R4G4B4_UINT:
864 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
865 *comps = 4;
866 return;
867
868 case MESA_FORMAT_B5G5R5A1_UNORM:
869 case MESA_FORMAT_A1R5G5B5_UNORM:
870 case MESA_FORMAT_B5G5R5X1_UNORM:
871 case MESA_FORMAT_B5G5R5A1_UINT:
872 case MESA_FORMAT_A1R5G5B5_UINT:
873 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
874 *comps = 4;
875 return;
876
877 case MESA_FORMAT_B10G10R10A2_UNORM:
878 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
879 *comps = 4;
880 return;
881
882 case MESA_FORMAT_A1B5G5R5_UNORM:
883 case MESA_FORMAT_A1B5G5R5_UINT:
884 case MESA_FORMAT_X1B5G5R5_UNORM:
885 *datatype = GL_UNSIGNED_SHORT_5_5_5_1;
886 *comps = 4;
887 return;
888
889 case MESA_FORMAT_L4A4_UNORM:
890 *datatype = MESA_UNSIGNED_BYTE_4_4;
891 *comps = 2;
892 return;
893
894 case MESA_FORMAT_L8A8_UNORM:
895 case MESA_FORMAT_A8L8_UNORM:
896 case MESA_FORMAT_R8G8_UNORM:
897 case MESA_FORMAT_G8R8_UNORM:
898 *datatype = GL_UNSIGNED_BYTE;
899 *comps = 2;
900 return;
901
902 case MESA_FORMAT_L16A16_UNORM:
903 case MESA_FORMAT_A16L16_UNORM:
904 case MESA_FORMAT_R16G16_UNORM:
905 case MESA_FORMAT_G16R16_UNORM:
906 *datatype = GL_UNSIGNED_SHORT;
907 *comps = 2;
908 return;
909
910 case MESA_FORMAT_R_UNORM16:
911 case MESA_FORMAT_A_UNORM16:
912 case MESA_FORMAT_L_UNORM16:
913 case MESA_FORMAT_I_UNORM16:
914 *datatype = GL_UNSIGNED_SHORT;
915 *comps = 1;
916 return;
917
918 case MESA_FORMAT_R3G3B2_UNORM:
919 case MESA_FORMAT_R3G3B2_UINT:
920 *datatype = GL_UNSIGNED_BYTE_2_3_3_REV;
921 *comps = 3;
922 return;
923 case MESA_FORMAT_A4B4G4R4_UNORM:
924 case MESA_FORMAT_A4B4G4R4_UINT:
925 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
926 *comps = 4;
927 return;
928
929 case MESA_FORMAT_R4G4B4A4_UNORM:
930 case MESA_FORMAT_R4G4B4A4_UINT:
931 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
932 *comps = 4;
933 return;
934 case MESA_FORMAT_R5G5B5A1_UNORM:
935 case MESA_FORMAT_R5G5B5A1_UINT:
936 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
937 *comps = 4;
938 return;
939 case MESA_FORMAT_A2B10G10R10_UNORM:
940 case MESA_FORMAT_A2B10G10R10_UINT:
941 *datatype = GL_UNSIGNED_INT_10_10_10_2;
942 *comps = 4;
943 return;
944 case MESA_FORMAT_A2R10G10B10_UNORM:
945 case MESA_FORMAT_A2R10G10B10_UINT:
946 *datatype = GL_UNSIGNED_INT_10_10_10_2;
947 *comps = 4;
948 return;
949
950 case MESA_FORMAT_B2G3R3_UNORM:
951 case MESA_FORMAT_B2G3R3_UINT:
952 *datatype = GL_UNSIGNED_BYTE_3_3_2;
953 *comps = 3;
954 return;
955
956 case MESA_FORMAT_A_UNORM8:
957 case MESA_FORMAT_L_UNORM8:
958 case MESA_FORMAT_I_UNORM8:
959 case MESA_FORMAT_R_UNORM8:
960 case MESA_FORMAT_S_UINT8:
961 *datatype = GL_UNSIGNED_BYTE;
962 *comps = 1;
963 return;
964
965 case MESA_FORMAT_YCBCR:
966 case MESA_FORMAT_YCBCR_REV:
967 *datatype = GL_UNSIGNED_SHORT;
968 *comps = 2;
969 return;
970
971 case MESA_FORMAT_S8_UINT_Z24_UNORM:
972 *datatype = GL_UNSIGNED_INT_24_8_MESA;
973 *comps = 2;
974 return;
975
976 case MESA_FORMAT_Z24_UNORM_S8_UINT:
977 *datatype = GL_UNSIGNED_INT_8_24_REV_MESA;
978 *comps = 2;
979 return;
980
981 case MESA_FORMAT_Z_UNORM16:
982 *datatype = GL_UNSIGNED_SHORT;
983 *comps = 1;
984 return;
985
986 case MESA_FORMAT_Z24_UNORM_X8_UINT:
987 *datatype = GL_UNSIGNED_INT;
988 *comps = 1;
989 return;
990
991 case MESA_FORMAT_X8_UINT_Z24_UNORM:
992 *datatype = GL_UNSIGNED_INT;
993 *comps = 1;
994 return;
995
996 case MESA_FORMAT_Z_UNORM32:
997 *datatype = GL_UNSIGNED_INT;
998 *comps = 1;
999 return;
1000
1001 case MESA_FORMAT_Z_FLOAT32:
1002 *datatype = GL_FLOAT;
1003 *comps = 1;
1004 return;
1005
1006 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1007 *datatype = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
1008 *comps = 1;
1009 return;
1010
1011 case MESA_FORMAT_R_SNORM8:
1012 case MESA_FORMAT_A_SNORM8:
1013 case MESA_FORMAT_L_SNORM8:
1014 case MESA_FORMAT_I_SNORM8:
1015 *datatype = GL_BYTE;
1016 *comps = 1;
1017 return;
1018 case MESA_FORMAT_R8G8_SNORM:
1019 case MESA_FORMAT_L8A8_SNORM:
1020 case MESA_FORMAT_A8L8_SNORM:
1021 *datatype = GL_BYTE;
1022 *comps = 2;
1023 return;
1024 case MESA_FORMAT_A8B8G8R8_SNORM:
1025 case MESA_FORMAT_R8G8B8A8_SNORM:
1026 case MESA_FORMAT_X8B8G8R8_SNORM:
1027 *datatype = GL_BYTE;
1028 *comps = 4;
1029 return;
1030
1031 case MESA_FORMAT_RGBA_UNORM16:
1032 *datatype = GL_UNSIGNED_SHORT;
1033 *comps = 4;
1034 return;
1035
1036 case MESA_FORMAT_R_SNORM16:
1037 case MESA_FORMAT_A_SNORM16:
1038 case MESA_FORMAT_L_SNORM16:
1039 case MESA_FORMAT_I_SNORM16:
1040 *datatype = GL_SHORT;
1041 *comps = 1;
1042 return;
1043 case MESA_FORMAT_R16G16_SNORM:
1044 case MESA_FORMAT_LA_SNORM16:
1045 *datatype = GL_SHORT;
1046 *comps = 2;
1047 return;
1048 case MESA_FORMAT_RGB_SNORM16:
1049 *datatype = GL_SHORT;
1050 *comps = 3;
1051 return;
1052 case MESA_FORMAT_RGBA_SNORM16:
1053 *datatype = GL_SHORT;
1054 *comps = 4;
1055 return;
1056
1057 case MESA_FORMAT_BGR_SRGB8:
1058 *datatype = GL_UNSIGNED_BYTE;
1059 *comps = 3;
1060 return;
1061 case MESA_FORMAT_A8B8G8R8_SRGB:
1062 case MESA_FORMAT_B8G8R8A8_SRGB:
1063 case MESA_FORMAT_A8R8G8B8_SRGB:
1064 case MESA_FORMAT_R8G8B8A8_SRGB:
1065 *datatype = GL_UNSIGNED_BYTE;
1066 *comps = 4;
1067 return;
1068 case MESA_FORMAT_L_SRGB8:
1069 *datatype = GL_UNSIGNED_BYTE;
1070 *comps = 1;
1071 return;
1072 case MESA_FORMAT_L8A8_SRGB:
1073 case MESA_FORMAT_A8L8_SRGB:
1074 *datatype = GL_UNSIGNED_BYTE;
1075 *comps = 2;
1076 return;
1077
1078 case MESA_FORMAT_RGBA_FLOAT32:
1079 *datatype = GL_FLOAT;
1080 *comps = 4;
1081 return;
1082 case MESA_FORMAT_RGBA_FLOAT16:
1083 *datatype = GL_HALF_FLOAT_ARB;
1084 *comps = 4;
1085 return;
1086 case MESA_FORMAT_RGB_FLOAT32:
1087 *datatype = GL_FLOAT;
1088 *comps = 3;
1089 return;
1090 case MESA_FORMAT_RGB_FLOAT16:
1091 *datatype = GL_HALF_FLOAT_ARB;
1092 *comps = 3;
1093 return;
1094 case MESA_FORMAT_LA_FLOAT32:
1095 case MESA_FORMAT_RG_FLOAT32:
1096 *datatype = GL_FLOAT;
1097 *comps = 2;
1098 return;
1099 case MESA_FORMAT_LA_FLOAT16:
1100 case MESA_FORMAT_RG_FLOAT16:
1101 *datatype = GL_HALF_FLOAT_ARB;
1102 *comps = 2;
1103 return;
1104 case MESA_FORMAT_A_FLOAT32:
1105 case MESA_FORMAT_L_FLOAT32:
1106 case MESA_FORMAT_I_FLOAT32:
1107 case MESA_FORMAT_R_FLOAT32:
1108 *datatype = GL_FLOAT;
1109 *comps = 1;
1110 return;
1111 case MESA_FORMAT_A_FLOAT16:
1112 case MESA_FORMAT_L_FLOAT16:
1113 case MESA_FORMAT_I_FLOAT16:
1114 case MESA_FORMAT_R_FLOAT16:
1115 *datatype = GL_HALF_FLOAT_ARB;
1116 *comps = 1;
1117 return;
1118
1119 case MESA_FORMAT_A_UINT8:
1120 case MESA_FORMAT_L_UINT8:
1121 case MESA_FORMAT_I_UINT8:
1122 *datatype = GL_UNSIGNED_BYTE;
1123 *comps = 1;
1124 return;
1125 case MESA_FORMAT_LA_UINT8:
1126 *datatype = GL_UNSIGNED_BYTE;
1127 *comps = 2;
1128 return;
1129
1130 case MESA_FORMAT_A_UINT16:
1131 case MESA_FORMAT_L_UINT16:
1132 case MESA_FORMAT_I_UINT16:
1133 *datatype = GL_UNSIGNED_SHORT;
1134 *comps = 1;
1135 return;
1136 case MESA_FORMAT_LA_UINT16:
1137 *datatype = GL_UNSIGNED_SHORT;
1138 *comps = 2;
1139 return;
1140 case MESA_FORMAT_A_UINT32:
1141 case MESA_FORMAT_L_UINT32:
1142 case MESA_FORMAT_I_UINT32:
1143 *datatype = GL_UNSIGNED_INT;
1144 *comps = 1;
1145 return;
1146 case MESA_FORMAT_LA_UINT32:
1147 *datatype = GL_UNSIGNED_INT;
1148 *comps = 2;
1149 return;
1150 case MESA_FORMAT_A_SINT8:
1151 case MESA_FORMAT_L_SINT8:
1152 case MESA_FORMAT_I_SINT8:
1153 *datatype = GL_BYTE;
1154 *comps = 1;
1155 return;
1156 case MESA_FORMAT_LA_SINT8:
1157 *datatype = GL_BYTE;
1158 *comps = 2;
1159 return;
1160
1161 case MESA_FORMAT_A_SINT16:
1162 case MESA_FORMAT_L_SINT16:
1163 case MESA_FORMAT_I_SINT16:
1164 *datatype = GL_SHORT;
1165 *comps = 1;
1166 return;
1167 case MESA_FORMAT_LA_SINT16:
1168 *datatype = GL_SHORT;
1169 *comps = 2;
1170 return;
1171
1172 case MESA_FORMAT_A_SINT32:
1173 case MESA_FORMAT_L_SINT32:
1174 case MESA_FORMAT_I_SINT32:
1175 *datatype = GL_INT;
1176 *comps = 1;
1177 return;
1178 case MESA_FORMAT_LA_SINT32:
1179 *datatype = GL_INT;
1180 *comps = 2;
1181 return;
1182
1183 case MESA_FORMAT_R_SINT8:
1184 *datatype = GL_BYTE;
1185 *comps = 1;
1186 return;
1187 case MESA_FORMAT_RG_SINT8:
1188 *datatype = GL_BYTE;
1189 *comps = 2;
1190 return;
1191 case MESA_FORMAT_RGB_SINT8:
1192 *datatype = GL_BYTE;
1193 *comps = 3;
1194 return;
1195 case MESA_FORMAT_RGBA_SINT8:
1196 *datatype = GL_BYTE;
1197 *comps = 4;
1198 return;
1199 case MESA_FORMAT_R_SINT16:
1200 *datatype = GL_SHORT;
1201 *comps = 1;
1202 return;
1203 case MESA_FORMAT_RG_SINT16:
1204 *datatype = GL_SHORT;
1205 *comps = 2;
1206 return;
1207 case MESA_FORMAT_RGB_SINT16:
1208 *datatype = GL_SHORT;
1209 *comps = 3;
1210 return;
1211 case MESA_FORMAT_RGBA_SINT16:
1212 *datatype = GL_SHORT;
1213 *comps = 4;
1214 return;
1215 case MESA_FORMAT_R_SINT32:
1216 *datatype = GL_INT;
1217 *comps = 1;
1218 return;
1219 case MESA_FORMAT_RG_SINT32:
1220 *datatype = GL_INT;
1221 *comps = 2;
1222 return;
1223 case MESA_FORMAT_RGB_SINT32:
1224 *datatype = GL_INT;
1225 *comps = 3;
1226 return;
1227 case MESA_FORMAT_RGBA_SINT32:
1228 *datatype = GL_INT;
1229 *comps = 4;
1230 return;
1231
1232 /**
1233 * \name Non-normalized unsigned integer formats.
1234 */
1235 case MESA_FORMAT_R_UINT8:
1236 *datatype = GL_UNSIGNED_BYTE;
1237 *comps = 1;
1238 return;
1239 case MESA_FORMAT_RG_UINT8:
1240 *datatype = GL_UNSIGNED_BYTE;
1241 *comps = 2;
1242 return;
1243 case MESA_FORMAT_RGB_UINT8:
1244 *datatype = GL_UNSIGNED_BYTE;
1245 *comps = 3;
1246 return;
1247 case MESA_FORMAT_RGBA_UINT8:
1248 *datatype = GL_UNSIGNED_BYTE;
1249 *comps = 4;
1250 return;
1251 case MESA_FORMAT_R_UINT16:
1252 *datatype = GL_UNSIGNED_SHORT;
1253 *comps = 1;
1254 return;
1255 case MESA_FORMAT_RG_UINT16:
1256 *datatype = GL_UNSIGNED_SHORT;
1257 *comps = 2;
1258 return;
1259 case MESA_FORMAT_RGB_UINT16:
1260 *datatype = GL_UNSIGNED_SHORT;
1261 *comps = 3;
1262 return;
1263 case MESA_FORMAT_RGBA_UINT16:
1264 *datatype = GL_UNSIGNED_SHORT;
1265 *comps = 4;
1266 return;
1267 case MESA_FORMAT_R_UINT32:
1268 *datatype = GL_UNSIGNED_INT;
1269 *comps = 1;
1270 return;
1271 case MESA_FORMAT_RG_UINT32:
1272 *datatype = GL_UNSIGNED_INT;
1273 *comps = 2;
1274 return;
1275 case MESA_FORMAT_RGB_UINT32:
1276 *datatype = GL_UNSIGNED_INT;
1277 *comps = 3;
1278 return;
1279 case MESA_FORMAT_RGBA_UINT32:
1280 *datatype = GL_UNSIGNED_INT;
1281 *comps = 4;
1282 return;
1283
1284 case MESA_FORMAT_R9G9B9E5_FLOAT:
1285 *datatype = GL_UNSIGNED_INT_5_9_9_9_REV;
1286 *comps = 3;
1287 return;
1288
1289 case MESA_FORMAT_R11G11B10_FLOAT:
1290 *datatype = GL_UNSIGNED_INT_10F_11F_11F_REV;
1291 *comps = 3;
1292 return;
1293
1294 case MESA_FORMAT_B10G10R10A2_UINT:
1295 case MESA_FORMAT_R10G10B10A2_UINT:
1296 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1297 *comps = 4;
1298 return;
1299
1300 case MESA_FORMAT_R8G8B8X8_SRGB:
1301 case MESA_FORMAT_X8B8G8R8_SRGB:
1302 case MESA_FORMAT_RGBX_UINT8:
1303 *datatype = GL_UNSIGNED_BYTE;
1304 *comps = 4;
1305 return;
1306
1307 case MESA_FORMAT_R8G8B8X8_SNORM:
1308 case MESA_FORMAT_RGBX_SINT8:
1309 *datatype = GL_BYTE;
1310 *comps = 4;
1311 return;
1312
1313 case MESA_FORMAT_B10G10R10X2_UNORM:
1314 case MESA_FORMAT_R10G10B10X2_UNORM:
1315 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1316 *comps = 4;
1317 return;
1318
1319 case MESA_FORMAT_RGBX_UNORM16:
1320 case MESA_FORMAT_RGBX_UINT16:
1321 *datatype = GL_UNSIGNED_SHORT;
1322 *comps = 4;
1323 return;
1324
1325 case MESA_FORMAT_RGBX_SNORM16:
1326 case MESA_FORMAT_RGBX_SINT16:
1327 *datatype = GL_SHORT;
1328 *comps = 4;
1329 return;
1330
1331 case MESA_FORMAT_RGBX_FLOAT16:
1332 *datatype = GL_HALF_FLOAT;
1333 *comps = 4;
1334 return;
1335
1336 case MESA_FORMAT_RGBX_FLOAT32:
1337 *datatype = GL_FLOAT;
1338 *comps = 4;
1339 return;
1340
1341 case MESA_FORMAT_RGBX_UINT32:
1342 *datatype = GL_UNSIGNED_INT;
1343 *comps = 4;
1344 return;
1345
1346 case MESA_FORMAT_RGBX_SINT32:
1347 *datatype = GL_INT;
1348 *comps = 4;
1349 return;
1350
1351 case MESA_FORMAT_R10G10B10A2_UNORM:
1352 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1353 *comps = 4;
1354 return;
1355
1356 case MESA_FORMAT_G8R8_SNORM:
1357 *datatype = GL_BYTE;
1358 *comps = 2;
1359 return;
1360
1361 case MESA_FORMAT_G16R16_SNORM:
1362 *datatype = GL_SHORT;
1363 *comps = 2;
1364 return;
1365
1366 case MESA_FORMAT_B8G8R8X8_SRGB:
1367 case MESA_FORMAT_X8R8G8B8_SRGB:
1368 *datatype = GL_UNSIGNED_BYTE;
1369 *comps = 4;
1370 return;
1371
1372 case MESA_FORMAT_COUNT:
1373 assert(0);
1374 return;
1375 default:
1376 /* Warn if any formats are not handled */
1377 _mesa_problem(NULL, "bad format %s in _mesa_uncompressed_format_to_type_and_comps",
1378 _mesa_get_format_name(format));
1379 assert(format == MESA_FORMAT_NONE ||
1380 _mesa_is_format_compressed(format));
1381 *datatype = 0;
1382 *comps = 1;
1383 }
1384 }
1385
1386 /**
1387 * Check if a mesa_format exactly matches a GL format/type combination
1388 * such that we can use memcpy() from one to the other.
1389 * \param mesa_format a MESA_FORMAT_x value
1390 * \param format the user-specified image format
1391 * \param type the user-specified image datatype
1392 * \param swapBytes typically the current pixel pack/unpack byteswap state
1393 * \param[out] error GL_NO_ERROR if format is an expected input.
1394 * GL_INVALID_ENUM if format is an unexpected input.
1395 * \return GL_TRUE if the formats match, GL_FALSE otherwise.
1396 */
1397 GLboolean
1398 _mesa_format_matches_format_and_type(mesa_format mesa_format,
1399 GLenum format, GLenum type,
1400 GLboolean swapBytes, GLenum *error)
1401 {
1402 const GLboolean littleEndian = _mesa_little_endian();
1403 if (error)
1404 *error = GL_NO_ERROR;
1405
1406 /* Note: When reading a GL format/type combination, the format lists channel
1407 * assignments from most significant channel in the type to least
1408 * significant. A type with _REV indicates that the assignments are
1409 * swapped, so they are listed from least significant to most significant.
1410 *
1411 * Compressed formats will fall through and return GL_FALSE.
1412 *
1413 * For sanity, please keep this switch statement ordered the same as the
1414 * enums in formats.h.
1415 */
1416
1417 switch (mesa_format) {
1418
1419 case MESA_FORMAT_NONE:
1420 case MESA_FORMAT_COUNT:
1421 return GL_FALSE;
1422
1423 case MESA_FORMAT_A8B8G8R8_UNORM:
1424 case MESA_FORMAT_A8B8G8R8_SRGB:
1425 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
1426 return GL_TRUE;
1427
1428 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8_REV && swapBytes)
1429 return GL_TRUE;
1430
1431 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE && !littleEndian)
1432 return GL_TRUE;
1433
1434 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8_REV
1435 && !swapBytes)
1436 return GL_TRUE;
1437
1438 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8
1439 && swapBytes)
1440 return GL_TRUE;
1441
1442 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_BYTE && littleEndian)
1443 return GL_TRUE;
1444
1445 return GL_FALSE;
1446
1447 case MESA_FORMAT_R8G8B8A8_UNORM:
1448 case MESA_FORMAT_R8G8B8A8_SRGB:
1449 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1450 !swapBytes)
1451 return GL_TRUE;
1452
1453 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1454 return GL_TRUE;
1455
1456 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE && littleEndian)
1457 return GL_TRUE;
1458
1459 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8 &&
1460 !swapBytes)
1461 return GL_TRUE;
1462
1463 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1464 swapBytes)
1465 return GL_TRUE;
1466
1467 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_BYTE && !littleEndian)
1468 return GL_TRUE;
1469
1470 return GL_FALSE;
1471
1472 case MESA_FORMAT_B8G8R8A8_UNORM:
1473 case MESA_FORMAT_B8G8R8A8_SRGB:
1474 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1475 !swapBytes)
1476 return GL_TRUE;
1477
1478 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1479 return GL_TRUE;
1480
1481 if (format == GL_BGRA && type == GL_UNSIGNED_BYTE && littleEndian)
1482 return GL_TRUE;
1483
1484 return GL_FALSE;
1485
1486 case MESA_FORMAT_A8R8G8B8_UNORM:
1487 case MESA_FORMAT_A8R8G8B8_SRGB:
1488 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
1489 return GL_TRUE;
1490
1491 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1492 swapBytes)
1493 return GL_TRUE;
1494
1495 if (format == GL_BGRA && type == GL_UNSIGNED_BYTE && !littleEndian)
1496 return GL_TRUE;
1497
1498 return GL_FALSE;
1499
1500 case MESA_FORMAT_X8B8G8R8_UNORM:
1501 case MESA_FORMAT_R8G8B8X8_UNORM:
1502 return GL_FALSE;
1503
1504 case MESA_FORMAT_B8G8R8X8_UNORM:
1505 case MESA_FORMAT_X8R8G8B8_UNORM:
1506 return GL_FALSE;
1507
1508 case MESA_FORMAT_BGR_UNORM8:
1509 case MESA_FORMAT_BGR_SRGB8:
1510 return format == GL_BGR && type == GL_UNSIGNED_BYTE && littleEndian;
1511
1512 case MESA_FORMAT_RGB_UNORM8:
1513 return format == GL_RGB && type == GL_UNSIGNED_BYTE && littleEndian;
1514
1515 case MESA_FORMAT_B5G6R5_UNORM:
1516 return ((format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5) ||
1517 (format == GL_BGR && type == GL_UNSIGNED_SHORT_5_6_5_REV)) &&
1518 !swapBytes;
1519
1520 case MESA_FORMAT_R5G6B5_UNORM:
1521 return ((format == GL_BGR && type == GL_UNSIGNED_SHORT_5_6_5) ||
1522 (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5_REV)) &&
1523 !swapBytes;
1524
1525 case MESA_FORMAT_B4G4R4A4_UNORM:
1526 return format == GL_BGRA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV &&
1527 !swapBytes;
1528
1529 case MESA_FORMAT_A4R4G4B4_UNORM:
1530 return GL_FALSE;
1531
1532 case MESA_FORMAT_A1B5G5R5_UNORM:
1533 return format == GL_RGBA && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1534 !swapBytes;
1535
1536 case MESA_FORMAT_X1B5G5R5_UNORM:
1537 return format == GL_RGB && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1538 !swapBytes;
1539
1540 case MESA_FORMAT_B5G5R5A1_UNORM:
1541 return format == GL_BGRA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV &&
1542 !swapBytes;
1543
1544 case MESA_FORMAT_A1R5G5B5_UNORM:
1545 return format == GL_BGRA && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1546 !swapBytes;
1547
1548 case MESA_FORMAT_L4A4_UNORM:
1549 return GL_FALSE;
1550 case MESA_FORMAT_L8A8_UNORM:
1551 case MESA_FORMAT_L8A8_SRGB:
1552 return format == GL_LUMINANCE_ALPHA && type == GL_UNSIGNED_BYTE && littleEndian;
1553 case MESA_FORMAT_A8L8_UNORM:
1554 case MESA_FORMAT_A8L8_SRGB:
1555 return GL_FALSE;
1556
1557 case MESA_FORMAT_L16A16_UNORM:
1558 return format == GL_LUMINANCE_ALPHA && type == GL_UNSIGNED_SHORT && littleEndian && !swapBytes;
1559 case MESA_FORMAT_A16L16_UNORM:
1560 return GL_FALSE;
1561
1562 case MESA_FORMAT_B2G3R3_UNORM:
1563 return format == GL_RGB && type == GL_UNSIGNED_BYTE_3_3_2;
1564
1565 case MESA_FORMAT_R3G3B2_UNORM:
1566 return format == GL_RGB && type == GL_UNSIGNED_BYTE_2_3_3_REV;
1567
1568 case MESA_FORMAT_A4B4G4R4_UNORM:
1569 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
1570 return GL_TRUE;
1571
1572 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !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