mesa: add support for memory object creation/import/delete
[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 *datatype = GL_UNSIGNED_SHORT_5_5_5_1;
879 *comps = 4;
880 return;
881
882 case MESA_FORMAT_L4A4_UNORM:
883 *datatype = MESA_UNSIGNED_BYTE_4_4;
884 *comps = 2;
885 return;
886
887 case MESA_FORMAT_L8A8_UNORM:
888 case MESA_FORMAT_A8L8_UNORM:
889 case MESA_FORMAT_R8G8_UNORM:
890 case MESA_FORMAT_G8R8_UNORM:
891 *datatype = GL_UNSIGNED_BYTE;
892 *comps = 2;
893 return;
894
895 case MESA_FORMAT_L16A16_UNORM:
896 case MESA_FORMAT_A16L16_UNORM:
897 case MESA_FORMAT_R16G16_UNORM:
898 case MESA_FORMAT_G16R16_UNORM:
899 *datatype = GL_UNSIGNED_SHORT;
900 *comps = 2;
901 return;
902
903 case MESA_FORMAT_R_UNORM16:
904 case MESA_FORMAT_A_UNORM16:
905 case MESA_FORMAT_L_UNORM16:
906 case MESA_FORMAT_I_UNORM16:
907 *datatype = GL_UNSIGNED_SHORT;
908 *comps = 1;
909 return;
910
911 case MESA_FORMAT_R3G3B2_UNORM:
912 case MESA_FORMAT_R3G3B2_UINT:
913 *datatype = GL_UNSIGNED_BYTE_2_3_3_REV;
914 *comps = 3;
915 return;
916 case MESA_FORMAT_A4B4G4R4_UNORM:
917 case MESA_FORMAT_A4B4G4R4_UINT:
918 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
919 *comps = 4;
920 return;
921
922 case MESA_FORMAT_R4G4B4A4_UNORM:
923 case MESA_FORMAT_R4G4B4A4_UINT:
924 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
925 *comps = 4;
926 return;
927 case MESA_FORMAT_R5G5B5A1_UNORM:
928 case MESA_FORMAT_R5G5B5A1_UINT:
929 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
930 *comps = 4;
931 return;
932 case MESA_FORMAT_A2B10G10R10_UNORM:
933 case MESA_FORMAT_A2B10G10R10_UINT:
934 *datatype = GL_UNSIGNED_INT_10_10_10_2;
935 *comps = 4;
936 return;
937 case MESA_FORMAT_A2R10G10B10_UNORM:
938 case MESA_FORMAT_A2R10G10B10_UINT:
939 *datatype = GL_UNSIGNED_INT_10_10_10_2;
940 *comps = 4;
941 return;
942
943 case MESA_FORMAT_B2G3R3_UNORM:
944 case MESA_FORMAT_B2G3R3_UINT:
945 *datatype = GL_UNSIGNED_BYTE_3_3_2;
946 *comps = 3;
947 return;
948
949 case MESA_FORMAT_A_UNORM8:
950 case MESA_FORMAT_L_UNORM8:
951 case MESA_FORMAT_I_UNORM8:
952 case MESA_FORMAT_R_UNORM8:
953 case MESA_FORMAT_S_UINT8:
954 *datatype = GL_UNSIGNED_BYTE;
955 *comps = 1;
956 return;
957
958 case MESA_FORMAT_YCBCR:
959 case MESA_FORMAT_YCBCR_REV:
960 *datatype = GL_UNSIGNED_SHORT;
961 *comps = 2;
962 return;
963
964 case MESA_FORMAT_S8_UINT_Z24_UNORM:
965 *datatype = GL_UNSIGNED_INT_24_8_MESA;
966 *comps = 2;
967 return;
968
969 case MESA_FORMAT_Z24_UNORM_S8_UINT:
970 *datatype = GL_UNSIGNED_INT_8_24_REV_MESA;
971 *comps = 2;
972 return;
973
974 case MESA_FORMAT_Z_UNORM16:
975 *datatype = GL_UNSIGNED_SHORT;
976 *comps = 1;
977 return;
978
979 case MESA_FORMAT_Z24_UNORM_X8_UINT:
980 *datatype = GL_UNSIGNED_INT;
981 *comps = 1;
982 return;
983
984 case MESA_FORMAT_X8_UINT_Z24_UNORM:
985 *datatype = GL_UNSIGNED_INT;
986 *comps = 1;
987 return;
988
989 case MESA_FORMAT_Z_UNORM32:
990 *datatype = GL_UNSIGNED_INT;
991 *comps = 1;
992 return;
993
994 case MESA_FORMAT_Z_FLOAT32:
995 *datatype = GL_FLOAT;
996 *comps = 1;
997 return;
998
999 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1000 *datatype = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
1001 *comps = 1;
1002 return;
1003
1004 case MESA_FORMAT_R_SNORM8:
1005 case MESA_FORMAT_A_SNORM8:
1006 case MESA_FORMAT_L_SNORM8:
1007 case MESA_FORMAT_I_SNORM8:
1008 *datatype = GL_BYTE;
1009 *comps = 1;
1010 return;
1011 case MESA_FORMAT_R8G8_SNORM:
1012 case MESA_FORMAT_L8A8_SNORM:
1013 case MESA_FORMAT_A8L8_SNORM:
1014 *datatype = GL_BYTE;
1015 *comps = 2;
1016 return;
1017 case MESA_FORMAT_A8B8G8R8_SNORM:
1018 case MESA_FORMAT_R8G8B8A8_SNORM:
1019 case MESA_FORMAT_X8B8G8R8_SNORM:
1020 *datatype = GL_BYTE;
1021 *comps = 4;
1022 return;
1023
1024 case MESA_FORMAT_RGBA_UNORM16:
1025 *datatype = GL_UNSIGNED_SHORT;
1026 *comps = 4;
1027 return;
1028
1029 case MESA_FORMAT_R_SNORM16:
1030 case MESA_FORMAT_A_SNORM16:
1031 case MESA_FORMAT_L_SNORM16:
1032 case MESA_FORMAT_I_SNORM16:
1033 *datatype = GL_SHORT;
1034 *comps = 1;
1035 return;
1036 case MESA_FORMAT_R16G16_SNORM:
1037 case MESA_FORMAT_LA_SNORM16:
1038 *datatype = GL_SHORT;
1039 *comps = 2;
1040 return;
1041 case MESA_FORMAT_RGB_SNORM16:
1042 *datatype = GL_SHORT;
1043 *comps = 3;
1044 return;
1045 case MESA_FORMAT_RGBA_SNORM16:
1046 *datatype = GL_SHORT;
1047 *comps = 4;
1048 return;
1049
1050 case MESA_FORMAT_BGR_SRGB8:
1051 *datatype = GL_UNSIGNED_BYTE;
1052 *comps = 3;
1053 return;
1054 case MESA_FORMAT_A8B8G8R8_SRGB:
1055 case MESA_FORMAT_B8G8R8A8_SRGB:
1056 case MESA_FORMAT_A8R8G8B8_SRGB:
1057 case MESA_FORMAT_R8G8B8A8_SRGB:
1058 *datatype = GL_UNSIGNED_BYTE;
1059 *comps = 4;
1060 return;
1061 case MESA_FORMAT_L_SRGB8:
1062 *datatype = GL_UNSIGNED_BYTE;
1063 *comps = 1;
1064 return;
1065 case MESA_FORMAT_L8A8_SRGB:
1066 case MESA_FORMAT_A8L8_SRGB:
1067 *datatype = GL_UNSIGNED_BYTE;
1068 *comps = 2;
1069 return;
1070
1071 case MESA_FORMAT_RGBA_FLOAT32:
1072 *datatype = GL_FLOAT;
1073 *comps = 4;
1074 return;
1075 case MESA_FORMAT_RGBA_FLOAT16:
1076 *datatype = GL_HALF_FLOAT_ARB;
1077 *comps = 4;
1078 return;
1079 case MESA_FORMAT_RGB_FLOAT32:
1080 *datatype = GL_FLOAT;
1081 *comps = 3;
1082 return;
1083 case MESA_FORMAT_RGB_FLOAT16:
1084 *datatype = GL_HALF_FLOAT_ARB;
1085 *comps = 3;
1086 return;
1087 case MESA_FORMAT_LA_FLOAT32:
1088 case MESA_FORMAT_RG_FLOAT32:
1089 *datatype = GL_FLOAT;
1090 *comps = 2;
1091 return;
1092 case MESA_FORMAT_LA_FLOAT16:
1093 case MESA_FORMAT_RG_FLOAT16:
1094 *datatype = GL_HALF_FLOAT_ARB;
1095 *comps = 2;
1096 return;
1097 case MESA_FORMAT_A_FLOAT32:
1098 case MESA_FORMAT_L_FLOAT32:
1099 case MESA_FORMAT_I_FLOAT32:
1100 case MESA_FORMAT_R_FLOAT32:
1101 *datatype = GL_FLOAT;
1102 *comps = 1;
1103 return;
1104 case MESA_FORMAT_A_FLOAT16:
1105 case MESA_FORMAT_L_FLOAT16:
1106 case MESA_FORMAT_I_FLOAT16:
1107 case MESA_FORMAT_R_FLOAT16:
1108 *datatype = GL_HALF_FLOAT_ARB;
1109 *comps = 1;
1110 return;
1111
1112 case MESA_FORMAT_A_UINT8:
1113 case MESA_FORMAT_L_UINT8:
1114 case MESA_FORMAT_I_UINT8:
1115 *datatype = GL_UNSIGNED_BYTE;
1116 *comps = 1;
1117 return;
1118 case MESA_FORMAT_LA_UINT8:
1119 *datatype = GL_UNSIGNED_BYTE;
1120 *comps = 2;
1121 return;
1122
1123 case MESA_FORMAT_A_UINT16:
1124 case MESA_FORMAT_L_UINT16:
1125 case MESA_FORMAT_I_UINT16:
1126 *datatype = GL_UNSIGNED_SHORT;
1127 *comps = 1;
1128 return;
1129 case MESA_FORMAT_LA_UINT16:
1130 *datatype = GL_UNSIGNED_SHORT;
1131 *comps = 2;
1132 return;
1133 case MESA_FORMAT_A_UINT32:
1134 case MESA_FORMAT_L_UINT32:
1135 case MESA_FORMAT_I_UINT32:
1136 *datatype = GL_UNSIGNED_INT;
1137 *comps = 1;
1138 return;
1139 case MESA_FORMAT_LA_UINT32:
1140 *datatype = GL_UNSIGNED_INT;
1141 *comps = 2;
1142 return;
1143 case MESA_FORMAT_A_SINT8:
1144 case MESA_FORMAT_L_SINT8:
1145 case MESA_FORMAT_I_SINT8:
1146 *datatype = GL_BYTE;
1147 *comps = 1;
1148 return;
1149 case MESA_FORMAT_LA_SINT8:
1150 *datatype = GL_BYTE;
1151 *comps = 2;
1152 return;
1153
1154 case MESA_FORMAT_A_SINT16:
1155 case MESA_FORMAT_L_SINT16:
1156 case MESA_FORMAT_I_SINT16:
1157 *datatype = GL_SHORT;
1158 *comps = 1;
1159 return;
1160 case MESA_FORMAT_LA_SINT16:
1161 *datatype = GL_SHORT;
1162 *comps = 2;
1163 return;
1164
1165 case MESA_FORMAT_A_SINT32:
1166 case MESA_FORMAT_L_SINT32:
1167 case MESA_FORMAT_I_SINT32:
1168 *datatype = GL_INT;
1169 *comps = 1;
1170 return;
1171 case MESA_FORMAT_LA_SINT32:
1172 *datatype = GL_INT;
1173 *comps = 2;
1174 return;
1175
1176 case MESA_FORMAT_R_SINT8:
1177 *datatype = GL_BYTE;
1178 *comps = 1;
1179 return;
1180 case MESA_FORMAT_RG_SINT8:
1181 *datatype = GL_BYTE;
1182 *comps = 2;
1183 return;
1184 case MESA_FORMAT_RGB_SINT8:
1185 *datatype = GL_BYTE;
1186 *comps = 3;
1187 return;
1188 case MESA_FORMAT_RGBA_SINT8:
1189 *datatype = GL_BYTE;
1190 *comps = 4;
1191 return;
1192 case MESA_FORMAT_R_SINT16:
1193 *datatype = GL_SHORT;
1194 *comps = 1;
1195 return;
1196 case MESA_FORMAT_RG_SINT16:
1197 *datatype = GL_SHORT;
1198 *comps = 2;
1199 return;
1200 case MESA_FORMAT_RGB_SINT16:
1201 *datatype = GL_SHORT;
1202 *comps = 3;
1203 return;
1204 case MESA_FORMAT_RGBA_SINT16:
1205 *datatype = GL_SHORT;
1206 *comps = 4;
1207 return;
1208 case MESA_FORMAT_R_SINT32:
1209 *datatype = GL_INT;
1210 *comps = 1;
1211 return;
1212 case MESA_FORMAT_RG_SINT32:
1213 *datatype = GL_INT;
1214 *comps = 2;
1215 return;
1216 case MESA_FORMAT_RGB_SINT32:
1217 *datatype = GL_INT;
1218 *comps = 3;
1219 return;
1220 case MESA_FORMAT_RGBA_SINT32:
1221 *datatype = GL_INT;
1222 *comps = 4;
1223 return;
1224
1225 /**
1226 * \name Non-normalized unsigned integer formats.
1227 */
1228 case MESA_FORMAT_R_UINT8:
1229 *datatype = GL_UNSIGNED_BYTE;
1230 *comps = 1;
1231 return;
1232 case MESA_FORMAT_RG_UINT8:
1233 *datatype = GL_UNSIGNED_BYTE;
1234 *comps = 2;
1235 return;
1236 case MESA_FORMAT_RGB_UINT8:
1237 *datatype = GL_UNSIGNED_BYTE;
1238 *comps = 3;
1239 return;
1240 case MESA_FORMAT_RGBA_UINT8:
1241 *datatype = GL_UNSIGNED_BYTE;
1242 *comps = 4;
1243 return;
1244 case MESA_FORMAT_R_UINT16:
1245 *datatype = GL_UNSIGNED_SHORT;
1246 *comps = 1;
1247 return;
1248 case MESA_FORMAT_RG_UINT16:
1249 *datatype = GL_UNSIGNED_SHORT;
1250 *comps = 2;
1251 return;
1252 case MESA_FORMAT_RGB_UINT16:
1253 *datatype = GL_UNSIGNED_SHORT;
1254 *comps = 3;
1255 return;
1256 case MESA_FORMAT_RGBA_UINT16:
1257 *datatype = GL_UNSIGNED_SHORT;
1258 *comps = 4;
1259 return;
1260 case MESA_FORMAT_R_UINT32:
1261 *datatype = GL_UNSIGNED_INT;
1262 *comps = 1;
1263 return;
1264 case MESA_FORMAT_RG_UINT32:
1265 *datatype = GL_UNSIGNED_INT;
1266 *comps = 2;
1267 return;
1268 case MESA_FORMAT_RGB_UINT32:
1269 *datatype = GL_UNSIGNED_INT;
1270 *comps = 3;
1271 return;
1272 case MESA_FORMAT_RGBA_UINT32:
1273 *datatype = GL_UNSIGNED_INT;
1274 *comps = 4;
1275 return;
1276
1277 case MESA_FORMAT_R9G9B9E5_FLOAT:
1278 *datatype = GL_UNSIGNED_INT_5_9_9_9_REV;
1279 *comps = 3;
1280 return;
1281
1282 case MESA_FORMAT_R11G11B10_FLOAT:
1283 *datatype = GL_UNSIGNED_INT_10F_11F_11F_REV;
1284 *comps = 3;
1285 return;
1286
1287 case MESA_FORMAT_B10G10R10A2_UINT:
1288 case MESA_FORMAT_R10G10B10A2_UINT:
1289 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1290 *comps = 4;
1291 return;
1292
1293 case MESA_FORMAT_R8G8B8X8_SRGB:
1294 case MESA_FORMAT_X8B8G8R8_SRGB:
1295 case MESA_FORMAT_RGBX_UINT8:
1296 *datatype = GL_UNSIGNED_BYTE;
1297 *comps = 4;
1298 return;
1299
1300 case MESA_FORMAT_R8G8B8X8_SNORM:
1301 case MESA_FORMAT_RGBX_SINT8:
1302 *datatype = GL_BYTE;
1303 *comps = 4;
1304 return;
1305
1306 case MESA_FORMAT_B10G10R10X2_UNORM:
1307 case MESA_FORMAT_R10G10B10X2_UNORM:
1308 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1309 *comps = 4;
1310 return;
1311
1312 case MESA_FORMAT_RGBX_UNORM16:
1313 case MESA_FORMAT_RGBX_UINT16:
1314 *datatype = GL_UNSIGNED_SHORT;
1315 *comps = 4;
1316 return;
1317
1318 case MESA_FORMAT_RGBX_SNORM16:
1319 case MESA_FORMAT_RGBX_SINT16:
1320 *datatype = GL_SHORT;
1321 *comps = 4;
1322 return;
1323
1324 case MESA_FORMAT_RGBX_FLOAT16:
1325 *datatype = GL_HALF_FLOAT;
1326 *comps = 4;
1327 return;
1328
1329 case MESA_FORMAT_RGBX_FLOAT32:
1330 *datatype = GL_FLOAT;
1331 *comps = 4;
1332 return;
1333
1334 case MESA_FORMAT_RGBX_UINT32:
1335 *datatype = GL_UNSIGNED_INT;
1336 *comps = 4;
1337 return;
1338
1339 case MESA_FORMAT_RGBX_SINT32:
1340 *datatype = GL_INT;
1341 *comps = 4;
1342 return;
1343
1344 case MESA_FORMAT_R10G10B10A2_UNORM:
1345 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1346 *comps = 4;
1347 return;
1348
1349 case MESA_FORMAT_G8R8_SNORM:
1350 *datatype = GL_BYTE;
1351 *comps = 2;
1352 return;
1353
1354 case MESA_FORMAT_G16R16_SNORM:
1355 *datatype = GL_SHORT;
1356 *comps = 2;
1357 return;
1358
1359 case MESA_FORMAT_B8G8R8X8_SRGB:
1360 case MESA_FORMAT_X8R8G8B8_SRGB:
1361 *datatype = GL_UNSIGNED_BYTE;
1362 *comps = 4;
1363 return;
1364
1365 case MESA_FORMAT_COUNT:
1366 assert(0);
1367 return;
1368 default:
1369 /* Warn if any formats are not handled */
1370 _mesa_problem(NULL, "bad format %s in _mesa_uncompressed_format_to_type_and_comps",
1371 _mesa_get_format_name(format));
1372 assert(format == MESA_FORMAT_NONE ||
1373 _mesa_is_format_compressed(format));
1374 *datatype = 0;
1375 *comps = 1;
1376 }
1377 }
1378
1379 /**
1380 * Check if a mesa_format exactly matches a GL format/type combination
1381 * such that we can use memcpy() from one to the other.
1382 * \param mesa_format a MESA_FORMAT_x value
1383 * \param format the user-specified image format
1384 * \param type the user-specified image datatype
1385 * \param swapBytes typically the current pixel pack/unpack byteswap state
1386 * \param[out] error GL_NO_ERROR if format is an expected input.
1387 * GL_INVALID_ENUM if format is an unexpected input.
1388 * \return GL_TRUE if the formats match, GL_FALSE otherwise.
1389 */
1390 GLboolean
1391 _mesa_format_matches_format_and_type(mesa_format mesa_format,
1392 GLenum format, GLenum type,
1393 GLboolean swapBytes, GLenum *error)
1394 {
1395 const GLboolean littleEndian = _mesa_little_endian();
1396 if (error)
1397 *error = GL_NO_ERROR;
1398
1399 /* Note: When reading a GL format/type combination, the format lists channel
1400 * assignments from most significant channel in the type to least
1401 * significant. A type with _REV indicates that the assignments are
1402 * swapped, so they are listed from least significant to most significant.
1403 *
1404 * Compressed formats will fall through and return GL_FALSE.
1405 *
1406 * For sanity, please keep this switch statement ordered the same as the
1407 * enums in formats.h.
1408 */
1409
1410 switch (mesa_format) {
1411
1412 case MESA_FORMAT_NONE:
1413 case MESA_FORMAT_COUNT:
1414 return GL_FALSE;
1415
1416 case MESA_FORMAT_A8B8G8R8_UNORM:
1417 case MESA_FORMAT_A8B8G8R8_SRGB:
1418 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
1419 return GL_TRUE;
1420
1421 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8_REV && swapBytes)
1422 return GL_TRUE;
1423
1424 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE && !littleEndian)
1425 return GL_TRUE;
1426
1427 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8_REV
1428 && !swapBytes)
1429 return GL_TRUE;
1430
1431 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8
1432 && swapBytes)
1433 return GL_TRUE;
1434
1435 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_BYTE && littleEndian)
1436 return GL_TRUE;
1437
1438 return GL_FALSE;
1439
1440 case MESA_FORMAT_R8G8B8A8_UNORM:
1441 case MESA_FORMAT_R8G8B8A8_SRGB:
1442 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1443 !swapBytes)
1444 return GL_TRUE;
1445
1446 if (format == GL_RGBA && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1447 return GL_TRUE;
1448
1449 if (format == GL_RGBA && type == GL_UNSIGNED_BYTE && littleEndian)
1450 return GL_TRUE;
1451
1452 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8 &&
1453 !swapBytes)
1454 return GL_TRUE;
1455
1456 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1457 swapBytes)
1458 return GL_TRUE;
1459
1460 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_BYTE && !littleEndian)
1461 return GL_TRUE;
1462
1463 return GL_FALSE;
1464
1465 case MESA_FORMAT_B8G8R8A8_UNORM:
1466 case MESA_FORMAT_B8G8R8A8_SRGB:
1467 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1468 !swapBytes)
1469 return GL_TRUE;
1470
1471 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1472 return GL_TRUE;
1473
1474 if (format == GL_BGRA && type == GL_UNSIGNED_BYTE && littleEndian)
1475 return GL_TRUE;
1476
1477 return GL_FALSE;
1478
1479 case MESA_FORMAT_A8R8G8B8_UNORM:
1480 case MESA_FORMAT_A8R8G8B8_SRGB:
1481 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
1482 return GL_TRUE;
1483
1484 if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1485 swapBytes)
1486 return GL_TRUE;
1487
1488 if (format == GL_BGRA && type == GL_UNSIGNED_BYTE && !littleEndian)
1489 return GL_TRUE;
1490
1491 return GL_FALSE;
1492
1493 case MESA_FORMAT_X8B8G8R8_UNORM:
1494 case MESA_FORMAT_R8G8B8X8_UNORM:
1495 return GL_FALSE;
1496
1497 case MESA_FORMAT_B8G8R8X8_UNORM:
1498 case MESA_FORMAT_X8R8G8B8_UNORM:
1499 return GL_FALSE;
1500
1501 case MESA_FORMAT_BGR_UNORM8:
1502 case MESA_FORMAT_BGR_SRGB8:
1503 return format == GL_BGR && type == GL_UNSIGNED_BYTE && littleEndian;
1504
1505 case MESA_FORMAT_RGB_UNORM8:
1506 return format == GL_RGB && type == GL_UNSIGNED_BYTE && littleEndian;
1507
1508 case MESA_FORMAT_B5G6R5_UNORM:
1509 return ((format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5) ||
1510 (format == GL_BGR && type == GL_UNSIGNED_SHORT_5_6_5_REV)) &&
1511 !swapBytes;
1512
1513 case MESA_FORMAT_R5G6B5_UNORM:
1514 return ((format == GL_BGR && type == GL_UNSIGNED_SHORT_5_6_5) ||
1515 (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5_REV)) &&
1516 !swapBytes;
1517
1518 case MESA_FORMAT_B4G4R4A4_UNORM:
1519 return format == GL_BGRA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV &&
1520 !swapBytes;
1521
1522 case MESA_FORMAT_A4R4G4B4_UNORM:
1523 return GL_FALSE;
1524
1525 case MESA_FORMAT_A1B5G5R5_UNORM:
1526 return format == GL_RGBA && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1527 !swapBytes;
1528
1529 case MESA_FORMAT_B5G5R5A1_UNORM:
1530 return format == GL_BGRA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV &&
1531 !swapBytes;
1532
1533 case MESA_FORMAT_A1R5G5B5_UNORM:
1534 return format == GL_BGRA && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1535 !swapBytes;
1536
1537 case MESA_FORMAT_L4A4_UNORM:
1538 return GL_FALSE;
1539 case MESA_FORMAT_L8A8_UNORM:
1540 case MESA_FORMAT_L8A8_SRGB:
1541 return format == GL_LUMINANCE_ALPHA && type == GL_UNSIGNED_BYTE && littleEndian;
1542 case MESA_FORMAT_A8L8_UNORM:
1543 case MESA_FORMAT_A8L8_SRGB:
1544 return GL_FALSE;
1545
1546 case MESA_FORMAT_L16A16_UNORM:
1547 return format == GL_LUMINANCE_ALPHA && type == GL_UNSIGNED_SHORT && littleEndian && !swapBytes;
1548 case MESA_FORMAT_A16L16_UNORM:
1549 return GL_FALSE;
1550
1551 case MESA_FORMAT_B2G3R3_UNORM:
1552 return format == GL_RGB && type == GL_UNSIGNED_BYTE_3_3_2;
1553
1554 case MESA_FORMAT_R3G3B2_UNORM:
1555 return format == GL_RGB && type == GL_UNSIGNED_BYTE_2_3_3_REV;
1556
1557 case MESA_FORMAT_A4B4G4R4_UNORM:
1558 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
1559 return GL_TRUE;
1560
1561 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && swapBytes)
1562 return GL_TRUE;
1563
1564 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !swapBytes)
1565 return GL_TRUE;
1566
1567 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4 && swapBytes)
1568 return GL_TRUE;
1569
1570 return GL_FALSE;
1571
1572 case MESA_FORMAT_R4G4B4A4_UNORM:
1573 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
1574 return GL_TRUE;
1575
1576 if (format == GL_ABGR_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && swapBytes)
1577 return GL_TRUE;
1578
1579 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !swapBytes)
1580 return GL_TRUE;
1581
1582 if (format == GL_RGBA && type == GL_UNSIGNED_SHORT_4_4_4_4 && swapBytes)
1583 return GL_TRUE;
1584
1585 return GL_FALSE;
1586
1587 case MESA_FORMAT_R5G5B5A1_UNORM:
1588 return format == GL_RGBA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV;
1589
1590 case MESA_FORMAT_A2B10G10R10_UNORM:
1591 return format == GL_RGBA && type == GL_UNSIGNED_INT_10_10_10_2;
1592
1593 case MESA_FORMAT_A2B10G10R10_UINT:
1594 return format == GL_RGBA_INTEGER_EXT && type == GL_UNSIGNED_INT_10_10_10_2;
1595
1596 case MESA_FORMAT_A2R10G10B10_UNORM:
1597 return format == GL_BGRA && type == GL_UNSIGNED_INT_10_10_10_2;
1598
1599 case MESA_FORMAT_A2R10G10B10_UINT:
1600 return format == GL_BGRA_INTEGER_EXT && type == GL_UNSIGNED_INT_10_10_10_2;
1601
1602 case MESA_FORMAT_A_UNORM8:
1603 return format == GL_ALPHA && type == GL_UNSIGNED_BYTE;
1604 case MESA_FORMAT_A_UNORM16:
1605 return format == GL_ALPHA && type == GL_UNSIGNED_SHORT && !swapBytes;
1606 case MESA_FORMAT_L_UNORM8:
1607 case MESA_FORMAT_L_SRGB8:
1608 return format == GL_LUMINANCE && type == GL_UNSIGNED_BYTE;
1609 case MESA_FORMAT_L_UNORM16:
1610 return format == GL_LUMINANCE && type == GL_UNSIGNED_SHORT && !swapBytes;
1611 case MESA_FORMAT_I_UNORM8:
1612 return format == GL_RED && type == GL_UNSIGNED_BYTE;
1613 case MESA_FORMAT_I_UNORM16:
1614 return format == GL_RED && type == GL_UNSIGNED_SHORT && !swapBytes;
1615
1616 case MESA_FORMAT_YCBCR:
1617 return format == GL_YCBCR_MESA &&
1618 ((type == GL_UNSIGNED_SHORT_8_8_MESA && littleEndian != swapBytes) ||
1619 (type == GL_UNSIGNED_SHORT_8_8_REV_MESA && littleEndian == swapBytes));
1620 case MESA_FORMAT_YCBCR_REV:
1621 return format == GL_YCBCR_MESA &&
1622 ((type == GL_UNSIGNED_SHORT_8_8_MESA && littleEndian == swapBytes) ||
1623 (type == GL_UNSIGNED_SHORT_8_8_REV_MESA && littleEndian != swapBytes));
1624
1625 case MESA_FORMAT_R_UNORM8:
1626 return format == GL_RED && type == GL_UNSIGNED_BYTE;
1627 case MESA_FORMAT_R8G8_UNORM:
1628 return format == GL_RG && type == GL_UNSIGNED_BYTE && littleEndian;
1629 case MESA_FORMAT_G8R8_UNORM:
1630 return GL_FALSE;
1631
1632 case MESA_FORMAT_R_UNORM16:
1633 return format == GL_RED && type == GL_UNSIGNED_SHORT &&
1634 !swapBytes;
1635 case MESA_FORMAT_R16G16_UNORM:
1636 return format == GL_RG && type == GL_UNSIGNED_SHORT && littleEndian &&
1637 !swapBytes;
1638 case MESA_FORMAT_G16R16_UNORM:
1639 return GL_FALSE;
1640
1641 case MESA_FORMAT_B10G10R10A2_UNORM:
1642 return format == GL_BGRA && type == GL_UNSIGNED_INT_2_10_10_10_REV &&
1643 !swapBytes;
1644
1645 case MESA_FORMAT_S8_UINT_Z24_UNORM:
1646 return format == GL_DEPTH_STENCIL && type == GL_UNSIGNED_INT_24_8 &&
1647 !swapBytes;
1648 case MESA_FORMAT_X8_UINT_Z24_UNORM:
1649 case MESA_FORMAT_Z24_UNORM_S8_UINT:
1650 return GL_FALSE;
1651
1652 case MESA_FORMAT_Z_UNORM16:
1653 return format == GL_DEPTH_COMPONENT && type == GL_UNSIGNED_SHORT &&
1654 !swapBytes;
1655
1656 case MESA_FORMAT_Z24_UNORM_X8_UINT:
1657 return GL_FALSE;
1658
1659 case MESA_FORMAT_Z_UNORM32:
1660 return format == GL_DEPTH_COMPONENT && type == GL_UNSIGNED_INT &&
1661 !swapBytes;
1662
1663 case MESA_FORMAT_S_UINT8:
1664 return format == GL_STENCIL_INDEX && type == GL_UNSIGNED_BYTE;
1665
1666 case MESA_FORMAT_RGBA_FLOAT32:
1667 return format == GL_RGBA && type == GL_FLOAT && !swapBytes;
1668 case MESA_FORMAT_RGBA_FLOAT16:
1669 return format == GL_RGBA && type == GL_HALF_FLOAT && !swapBytes;
1670
1671 case MESA_FORMAT_RGB_FLOAT32:
1672 return format == GL_RGB && type == GL_FLOAT && !swapBytes;
1673 case MESA_FORMAT_RGB_FLOAT16:
1674 return format == GL_RGB && type == GL_HALF_FLOAT && !swapBytes;
1675
1676 case MESA_FORMAT_A_FLOAT32:
1677 return format == GL_ALPHA && type == GL_FLOAT && !swapBytes;
1678 case MESA_FORMAT_A_FLOAT16:
1679 return format == GL_ALPHA && type == GL_HALF_FLOAT && !swapBytes;
1680
1681 case MESA_FORMAT_L_FLOAT32:
1682 return format == GL_LUMINANCE && type == GL_FLOAT && !swapBytes;
1683 case MESA_FORMAT_L_FLOAT16:
1684 return format == GL_LUMINANCE && type == GL_HALF_FLOAT && !swapBytes;
1685
1686 case MESA_FORMAT_LA_FLOAT32:
1687 return format == GL_LUMINANCE_ALPHA && type == GL_FLOAT && !swapBytes;
1688 case MESA_FORMAT_LA_FLOAT16:
1689 return format == GL_LUMINANCE_ALPHA && type == GL_HALF_FLOAT && !swapBytes;
1690
1691 case MESA_FORMAT_I_FLOAT32:
1692 return format == GL_RED && type == GL_FLOAT && !swapBytes;
1693 case MESA_FORMAT_I_FLOAT16:
1694 return format == GL_RED && type == GL_HALF_FLOAT && !swapBytes;
1695
1696 case MESA_FORMAT_R_FLOAT32:
1697 return format == GL_RED && type == GL_FLOAT && !swapBytes;
1698 case MESA_FORMAT_R_FLOAT16:
1699 return format == GL_RED && type == GL_HALF_FLOAT && !swapBytes;
1700
1701 case MESA_FORMAT_RG_FLOAT32:
1702 return format == GL_RG && type == GL_FLOAT && !swapBytes;
1703 case MESA_FORMAT_RG_FLOAT16:
1704 return format == GL_RG && type == GL_HALF_FLOAT && !swapBytes;
1705
1706 case MESA_FORMAT_A_UINT8:
1707 return format == GL_ALPHA_INTEGER && type == GL_UNSIGNED_BYTE;
1708 case MESA_FORMAT_A_UINT16:
1709 return format == GL_ALPHA_INTEGER && type == GL_UNSIGNED_SHORT &&
1710 !swapBytes;
1711 case MESA_FORMAT_A_UINT32:
1712 return format == GL_ALPHA_INTEGER && type == GL_UNSIGNED_INT &&
1713 !swapBytes;
1714 case MESA_FORMAT_A_SINT8:
1715 return format == GL_ALPHA_INTEGER && type == GL_BYTE;
1716 case MESA_FORMAT_A_SINT16:
1717 return format == GL_ALPHA_INTEGER && type == GL_SHORT && !swapBytes;
1718 case MESA_FORMAT_A_SINT32:
1719 return format == GL_ALPHA_INTEGER && type == GL_INT && !swapBytes;
1720
1721 case MESA_FORMAT_I_UINT8:
1722 return format == GL_RED_INTEGER && type == GL_UNSIGNED_BYTE;
1723 case MESA_FORMAT_I_UINT16:
1724 return format == GL_RED_INTEGER && type == GL_UNSIGNED_SHORT && !swapBytes;
1725 case MESA_FORMAT_I_UINT32:
1726 return format == GL_RED_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1727 case MESA_FORMAT_I_SINT8:
1728 return format == GL_RED_INTEGER && type == GL_BYTE;
1729 case MESA_FORMAT_I_SINT16:
1730 return format == GL_RED_INTEGER && type == GL_SHORT && !swapBytes;
1731 case MESA_FORMAT_I_SINT32:
1732 return format == GL_RED_INTEGER && type == GL_INT && !swapBytes;
1733
1734 case MESA_FORMAT_L_UINT8:
1735 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_UNSIGNED_BYTE;
1736 case MESA_FORMAT_L_UINT16:
1737 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_UNSIGNED_SHORT &&
1738 !swapBytes;
1739 case MESA_FORMAT_L_UINT32:
1740 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_UNSIGNED_INT &&
1741 !swapBytes;
1742 case MESA_FORMAT_L_SINT8:
1743 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_BYTE;
1744 case MESA_FORMAT_L_SINT16:
1745 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_SHORT &&
1746 !swapBytes;
1747 case MESA_FORMAT_L_SINT32:
1748 return format == GL_LUMINANCE_INTEGER_EXT && type == GL_INT && !swapBytes;
1749
1750 case MESA_FORMAT_LA_UINT8:
1751 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT &&
1752 type == GL_UNSIGNED_BYTE && !swapBytes;
1753 case MESA_FORMAT_LA_UINT16:
1754 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT &&
1755 type == GL_UNSIGNED_SHORT && !swapBytes;
1756 case MESA_FORMAT_LA_UINT32:
1757 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT &&
1758 type == GL_UNSIGNED_INT && !swapBytes;
1759 case MESA_FORMAT_LA_SINT8:
1760 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT && type == GL_BYTE &&
1761 !swapBytes;
1762 case MESA_FORMAT_LA_SINT16:
1763 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT && type == GL_SHORT &&
1764 !swapBytes;
1765 case MESA_FORMAT_LA_SINT32:
1766 return format == GL_LUMINANCE_ALPHA_INTEGER_EXT && type == GL_INT &&
1767 !swapBytes;
1768
1769 case MESA_FORMAT_R_SINT8:
1770 return format == GL_RED_INTEGER && type == GL_BYTE;
1771 case MESA_FORMAT_RG_SINT8:
1772 return format == GL_RG_INTEGER && type == GL_BYTE && !swapBytes;
1773 case MESA_FORMAT_RGB_SINT8:
1774 return format == GL_RGB_INTEGER && type == GL_BYTE && !swapBytes;
1775 case MESA_FORMAT_RGBA_SINT8:
1776 return format == GL_RGBA_INTEGER && type == GL_BYTE && !swapBytes;
1777 case MESA_FORMAT_R_SINT16:
1778 return format == GL_RED_INTEGER && type == GL_SHORT && !swapBytes;
1779 case MESA_FORMAT_RG_SINT16:
1780 return format == GL_RG_INTEGER && type == GL_SHORT && !swapBytes;
1781 case MESA_FORMAT_RGB_SINT16:
1782 return format == GL_RGB_INTEGER && type == GL_SHORT && !swapBytes;
1783 case MESA_FORMAT_RGBA_SINT16:
1784 return format == GL_RGBA_INTEGER && type == GL_SHORT && !swapBytes;
1785 case MESA_FORMAT_R_SINT32:
1786 return format == GL_RED_INTEGER && type == GL_INT && !swapBytes;
1787 case MESA_FORMAT_RG_SINT32:
1788 return format == GL_RG_INTEGER && type == GL_INT && !swapBytes;
1789 case MESA_FORMAT_RGB_SINT32:
1790 return format == GL_RGB_INTEGER && type == GL_INT && !swapBytes;
1791 case MESA_FORMAT_RGBA_SINT32:
1792 return format == GL_RGBA_INTEGER && type == GL_INT && !swapBytes;
1793
1794 case MESA_FORMAT_R_UINT8:
1795 return format == GL_RED_INTEGER && type == GL_UNSIGNED_BYTE;
1796 case MESA_FORMAT_RG_UINT8:
1797 return format == GL_RG_INTEGER && type == GL_UNSIGNED_BYTE && !swapBytes;
1798 case MESA_FORMAT_RGB_UINT8:
1799 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_BYTE && !swapBytes;
1800 case MESA_FORMAT_RGBA_UINT8:
1801 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_BYTE &&
1802 !swapBytes;
1803 case MESA_FORMAT_R_UINT16:
1804 return format == GL_RED_INTEGER && type == GL_UNSIGNED_SHORT &&
1805 !swapBytes;
1806 case MESA_FORMAT_RG_UINT16:
1807 return format == GL_RG_INTEGER && type == GL_UNSIGNED_SHORT && !swapBytes;
1808 case MESA_FORMAT_RGB_UINT16:
1809 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_SHORT &&
1810 !swapBytes;
1811 case MESA_FORMAT_RGBA_UINT16:
1812 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT &&
1813 !swapBytes;
1814 case MESA_FORMAT_R_UINT32:
1815 return format == GL_RED_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1816 case MESA_FORMAT_RG_UINT32:
1817 return format == GL_RG_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1818 case MESA_FORMAT_RGB_UINT32:
1819 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1820 case MESA_FORMAT_RGBA_UINT32:
1821 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT && !swapBytes;
1822
1823 case MESA_FORMAT_R_SNORM8:
1824 return format == GL_RED && type == GL_BYTE;
1825 case MESA_FORMAT_R8G8_SNORM:
1826 return format == GL_RG && type == GL_BYTE && littleEndian &&
1827 !swapBytes;
1828 case MESA_FORMAT_X8B8G8R8_SNORM:
1829 return GL_FALSE;
1830
1831 case MESA_FORMAT_A8B8G8R8_SNORM:
1832 if (format == GL_RGBA && type == GL_BYTE && !littleEndian)
1833 return GL_TRUE;
1834
1835 if (format == GL_ABGR_EXT && type == GL_BYTE && littleEndian)
1836 return GL_TRUE;
1837
1838 return GL_FALSE;
1839
1840 case MESA_FORMAT_R8G8B8A8_SNORM:
1841 if (format == GL_RGBA && type == GL_BYTE && littleEndian)
1842 return GL_TRUE;
1843
1844 if (format == GL_ABGR_EXT && type == GL_BYTE && !littleEndian)
1845 return GL_TRUE;
1846
1847 return GL_FALSE;
1848
1849 case MESA_FORMAT_R_SNORM16:
1850 return format == GL_RED && type == GL_SHORT &&
1851 !swapBytes;
1852 case MESA_FORMAT_R16G16_SNORM:
1853 return format == GL_RG && type == GL_SHORT && littleEndian && !swapBytes;
1854 case MESA_FORMAT_RGB_SNORM16:
1855 return format == GL_RGB && type == GL_SHORT && !swapBytes;
1856 case MESA_FORMAT_RGBA_SNORM16:
1857 return format == GL_RGBA && type == GL_SHORT && !swapBytes;
1858 case MESA_FORMAT_RGBA_UNORM16:
1859 return format == GL_RGBA && type == GL_UNSIGNED_SHORT &&
1860 !swapBytes;
1861
1862 case MESA_FORMAT_A_SNORM8:
1863 return format == GL_ALPHA && type == GL_BYTE;
1864 case MESA_FORMAT_L_SNORM8:
1865 return format == GL_LUMINANCE && type == GL_BYTE;
1866 case MESA_FORMAT_L8A8_SNORM:
1867 return format == GL_LUMINANCE_ALPHA && type == GL_BYTE &&
1868 littleEndian && !swapBytes;
1869 case MESA_FORMAT_A8L8_SNORM:
1870 return format == GL_LUMINANCE_ALPHA && type == GL_BYTE &&
1871 !littleEndian && !swapBytes;
1872 case MESA_FORMAT_I_SNORM8:
1873 return format == GL_RED && type == GL_BYTE;
1874 case MESA_FORMAT_A_SNORM16:
1875 return format == GL_ALPHA && type == GL_SHORT && !swapBytes;
1876 case MESA_FORMAT_L_SNORM16:
1877 return format == GL_LUMINANCE && type == GL_SHORT && !swapBytes;
1878 case MESA_FORMAT_LA_SNORM16:
1879 return format == GL_LUMINANCE_ALPHA && type == GL_SHORT &&
1880 littleEndian && !swapBytes;
1881 case MESA_FORMAT_I_SNORM16:
1882 return format == GL_RED && type == GL_SHORT && littleEndian &&
1883 !swapBytes;
1884
1885 case MESA_FORMAT_B10G10R10A2_UINT:
1886 return (format == GL_BGRA_INTEGER_EXT &&
1887 type == GL_UNSIGNED_INT_2_10_10_10_REV &&
1888 !swapBytes);
1889
1890 case MESA_FORMAT_R10G10B10A2_UINT:
1891 return (format == GL_RGBA_INTEGER_EXT &&
1892 type == GL_UNSIGNED_INT_2_10_10_10_REV &&
1893 !swapBytes);
1894
1895 case MESA_FORMAT_B5G6R5_UINT:
1896 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_SHORT_5_6_5;
1897
1898 case MESA_FORMAT_R5G6B5_UINT:
1899 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_SHORT_5_6_5_REV;
1900
1901 case MESA_FORMAT_B2G3R3_UINT:
1902 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_BYTE_3_3_2;
1903
1904 case MESA_FORMAT_R3G3B2_UINT:
1905 return format == GL_RGB_INTEGER && type == GL_UNSIGNED_BYTE_2_3_3_REV;
1906
1907 case MESA_FORMAT_A4B4G4R4_UINT:
1908 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4 && !swapBytes)
1909 return GL_TRUE;
1910
1911 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && swapBytes)
1912 return GL_TRUE;
1913 return GL_FALSE;
1914
1915 case MESA_FORMAT_R4G4B4A4_UINT:
1916 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4_REV && !swapBytes)
1917 return GL_TRUE;
1918
1919 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4 && swapBytes)
1920 return GL_TRUE;
1921
1922 return GL_FALSE;
1923
1924 case MESA_FORMAT_B4G4R4A4_UINT:
1925 return format == GL_BGRA_INTEGER && type == GL_UNSIGNED_SHORT_4_4_4_4_REV &&
1926 !swapBytes;
1927
1928 case MESA_FORMAT_A4R4G4B4_UINT:
1929 return GL_FALSE;
1930
1931 case MESA_FORMAT_A1B5G5R5_UINT:
1932 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1933 !swapBytes;
1934
1935 case MESA_FORMAT_B5G5R5A1_UINT:
1936 return format == GL_BGRA_INTEGER && type == GL_UNSIGNED_SHORT_1_5_5_5_REV &&
1937 !swapBytes;
1938
1939 case MESA_FORMAT_A1R5G5B5_UINT:
1940 return format == GL_BGRA_INTEGER && type == GL_UNSIGNED_SHORT_5_5_5_1 &&
1941 !swapBytes;
1942
1943 case MESA_FORMAT_R5G5B5A1_UINT:
1944 return format == GL_RGBA_INTEGER && type == GL_UNSIGNED_SHORT_1_5_5_5_REV;
1945
1946 case MESA_FORMAT_A8B8G8R8_UINT:
1947 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8 && !swapBytes)
1948 return GL_TRUE;
1949
1950 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8_REV && swapBytes)
1951 return GL_TRUE;
1952 return GL_FALSE;
1953
1954 case MESA_FORMAT_A8R8G8B8_UINT:
1955 if (format == GL_BGRA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8 &&
1956 !swapBytes)
1957 return GL_TRUE;
1958
1959 if (format == GL_BGRA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1960 swapBytes)
1961 return GL_TRUE;
1962
1963 return GL_FALSE;
1964
1965 case MESA_FORMAT_R8G8B8A8_UINT:
1966 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1967 !swapBytes)
1968 return GL_TRUE;
1969
1970 if (format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1971 return GL_TRUE;
1972
1973 return GL_FALSE;
1974
1975 case MESA_FORMAT_B8G8R8A8_UINT:
1976 if (format == GL_BGRA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8_REV &&
1977 !swapBytes)
1978 return GL_TRUE;
1979
1980 if (format == GL_BGRA_INTEGER && type == GL_UNSIGNED_INT_8_8_8_8 && swapBytes)
1981 return GL_TRUE;
1982
1983 return GL_FALSE;
1984
1985 case MESA_FORMAT_R9G9B9E5_FLOAT:
1986 return format == GL_RGB && type == GL_UNSIGNED_INT_5_9_9_9_REV &&
1987 !swapBytes;
1988
1989 case MESA_FORMAT_R11G11B10_FLOAT:
1990 return format == GL_RGB && type == GL_UNSIGNED_INT_10F_11F_11F_REV &&
1991 !swapBytes;
1992
1993 case MESA_FORMAT_Z_FLOAT32:
1994 return format == GL_DEPTH_COMPONENT && type == GL_FLOAT && !swapBytes;
1995
1996 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1997 return format == GL_DEPTH_STENCIL &&
1998 type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV && !swapBytes;
1999
2000 case MESA_FORMAT_B4G4R4X4_UNORM:
2001 case MESA_FORMAT_B5G5R5X1_UNORM:
2002 case MESA_FORMAT_R8G8B8X8_SNORM:
2003 case MESA_FORMAT_R8G8B8X8_SRGB:
2004 case MESA_FORMAT_X8B8G8R8_SRGB:
2005 case MESA_FORMAT_RGBX_UINT8:
2006 case MESA_FORMAT_RGBX_SINT8:
2007 case MESA_FORMAT_B10G10R10X2_UNORM:
2008 case MESA_FORMAT_R10G10B10X2_UNORM:
2009 case MESA_FORMAT_RGBX_UNORM16:
2010 case MESA_FORMAT_RGBX_SNORM16:
2011 case MESA_FORMAT_RGBX_FLOAT16:
2012 case MESA_FORMAT_RGBX_UINT16:
2013 case MESA_FORMAT_RGBX_SINT16:
2014 case MESA_FORMAT_RGBX_FLOAT32:
2015 case MESA_FORMAT_RGBX_UINT32:
2016 case MESA_FORMAT_RGBX_SINT32:
2017 return GL_FALSE;
2018
2019 case MESA_FORMAT_R10G10B10A2_UNORM:
2020 return format == GL_RGBA && type == GL_UNSIGNED_INT_2_10_10_10_REV &&
2021 !swapBytes;
2022
2023 case MESA_FORMAT_G8R8_SNORM:
2024 return format == GL_RG && type == GL_BYTE && !littleEndian &&
2025 !swapBytes;
2026
2027 case MESA_FORMAT_G16R16_SNORM:
2028 return format == GL_RG && type == GL_SHORT && !littleEndian &&
2029 !swapBytes;
2030
2031 case MESA_FORMAT_B8G8R8X8_SRGB:
2032 case MESA_FORMAT_X8R8G8B8_SRGB:
2033 return GL_FALSE;
2034 default:
2035 assert(_mesa_is_format_compressed(mesa_format));
2036 if (error)
2037 *error = GL_INVALID_ENUM;
2038 }
2039 return GL_FALSE;
2040 }
2041