util: rename PIPE_ARCH_*_ENDIAN to UTIL_ARCH_*_ENDIAN
[mesa.git] / src / mesa / main / formats.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2008-2009 VMware, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #include "errors.h"
28 #include "imports.h"
29 #include "formats.h"
30 #include "macros.h"
31 #include "glformats.h"
32 #include "c11/threads.h"
33 #include "util/hash_table.h"
34
35 /**
36 * Information about texture formats.
37 */
38 struct mesa_format_info
39 {
40 mesa_format Name;
41
42 /** text name for debugging */
43 const char *StrName;
44
45 enum mesa_format_layout Layout;
46
47 /**
48 * Base format is one of GL_RED, GL_RG, GL_RGB, GL_RGBA, GL_ALPHA,
49 * GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_INTENSITY, GL_YCBCR_MESA,
50 * GL_DEPTH_COMPONENT, GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
51 */
52 GLenum BaseFormat;
53
54 /**
55 * Logical data type: one of GL_UNSIGNED_NORMALIZED, GL_SIGNED_NORMALIZED,
56 * GL_UNSIGNED_INT, GL_INT, GL_FLOAT.
57 */
58 GLenum DataType;
59
60 uint8_t RedBits;
61 uint8_t GreenBits;
62 uint8_t BlueBits;
63 uint8_t AlphaBits;
64 uint8_t LuminanceBits;
65 uint8_t IntensityBits;
66 uint8_t DepthBits;
67 uint8_t StencilBits;
68
69 bool IsSRGBFormat;
70
71 /**
72 * To describe compressed formats. If not compressed, Width=Height=Depth=1.
73 */
74 uint8_t BlockWidth, BlockHeight, BlockDepth;
75 uint8_t BytesPerBlock;
76
77 uint8_t Swizzle[4];
78 mesa_array_format ArrayFormat;
79 };
80
81 #include "format_info.h"
82
83 static const struct mesa_format_info *
84 _mesa_get_format_info(mesa_format format)
85 {
86 const struct mesa_format_info *info = &format_info[format];
87 STATIC_ASSERT(ARRAY_SIZE(format_info) == MESA_FORMAT_COUNT);
88 assert(info->Name == format);
89 return info;
90 }
91
92
93 /** Return string name of format (for debugging) */
94 const char *
95 _mesa_get_format_name(mesa_format format)
96 {
97 const struct mesa_format_info *info = _mesa_get_format_info(format);
98 return info->StrName;
99 }
100
101
102
103 /**
104 * Return bytes needed to store a block of pixels in the given format.
105 * Normally, a block is 1x1 (a single pixel). But for compressed formats
106 * a block may be 4x4 or 8x4, etc.
107 *
108 * Note: return is signed, so as not to coerce math to unsigned. cf. fdo #37351
109 */
110 int
111 _mesa_get_format_bytes(mesa_format format)
112 {
113 if (_mesa_format_is_mesa_array_format(format)) {
114 return _mesa_array_format_get_type_size(format) *
115 _mesa_array_format_get_num_channels(format);
116 }
117
118 const struct mesa_format_info *info = _mesa_get_format_info(format);
119 assert(info->BytesPerBlock);
120 assert(info->BytesPerBlock <= MAX_PIXEL_BYTES ||
121 _mesa_is_format_compressed(format));
122 return info->BytesPerBlock;
123 }
124
125
126 /**
127 * Return bits per component for the given format.
128 * \param format one of MESA_FORMAT_x
129 * \param pname the component, such as GL_RED_BITS, GL_TEXTURE_BLUE_BITS, etc.
130 */
131 GLint
132 _mesa_get_format_bits(mesa_format format, GLenum pname)
133 {
134 const struct mesa_format_info *info = _mesa_get_format_info(format);
135
136 switch (pname) {
137 case GL_RED_BITS:
138 case GL_TEXTURE_RED_SIZE:
139 case GL_RENDERBUFFER_RED_SIZE_EXT:
140 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
141 case GL_INTERNALFORMAT_RED_SIZE:
142 return info->RedBits;
143 case GL_GREEN_BITS:
144 case GL_TEXTURE_GREEN_SIZE:
145 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
146 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
147 case GL_INTERNALFORMAT_GREEN_SIZE:
148 return info->GreenBits;
149 case GL_BLUE_BITS:
150 case GL_TEXTURE_BLUE_SIZE:
151 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
152 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
153 case GL_INTERNALFORMAT_BLUE_SIZE:
154 return info->BlueBits;
155 case GL_ALPHA_BITS:
156 case GL_TEXTURE_ALPHA_SIZE:
157 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
158 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
159 case GL_INTERNALFORMAT_ALPHA_SIZE:
160 return info->AlphaBits;
161 case GL_TEXTURE_INTENSITY_SIZE:
162 return info->IntensityBits;
163 case GL_TEXTURE_LUMINANCE_SIZE:
164 return info->LuminanceBits;
165 case GL_INDEX_BITS:
166 return 0;
167 case GL_DEPTH_BITS:
168 case GL_TEXTURE_DEPTH_SIZE_ARB:
169 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
170 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
171 case GL_INTERNALFORMAT_DEPTH_SIZE:
172 return info->DepthBits;
173 case GL_STENCIL_BITS:
174 case GL_TEXTURE_STENCIL_SIZE_EXT:
175 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
176 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
177 case GL_INTERNALFORMAT_STENCIL_SIZE:
178 return info->StencilBits;
179 default:
180 _mesa_problem(NULL, "bad pname in _mesa_get_format_bits()");
181 return 0;
182 }
183 }
184
185
186 unsigned int
187 _mesa_get_format_max_bits(mesa_format format)
188 {
189 const struct mesa_format_info *info = _mesa_get_format_info(format);
190 unsigned int max = MAX2(info->RedBits, info->GreenBits);
191 max = MAX2(max, info->BlueBits);
192 max = MAX2(max, info->AlphaBits);
193 max = MAX2(max, info->LuminanceBits);
194 max = MAX2(max, info->IntensityBits);
195 max = MAX2(max, info->DepthBits);
196 max = MAX2(max, info->StencilBits);
197 return max;
198 }
199
200
201 /**
202 * Return the layout type of the given format.
203 */
204 extern enum mesa_format_layout
205 _mesa_get_format_layout(mesa_format format)
206 {
207 const struct mesa_format_info *info = _mesa_get_format_info(format);
208 return info->Layout;
209 }
210
211
212 /**
213 * Return the data type (or more specifically, the data representation)
214 * for the given format.
215 * The return value will be one of:
216 * GL_UNSIGNED_NORMALIZED = unsigned int representing [0,1]
217 * GL_SIGNED_NORMALIZED = signed int representing [-1, 1]
218 * GL_UNSIGNED_INT = an ordinary unsigned integer
219 * GL_INT = an ordinary signed integer
220 * GL_FLOAT = an ordinary float
221 */
222 GLenum
223 _mesa_get_format_datatype(mesa_format format)
224 {
225 const struct mesa_format_info *info = _mesa_get_format_info(format);
226 return info->DataType;
227 }
228
229 static GLenum
230 get_base_format_for_array_format(mesa_array_format format)
231 {
232 uint8_t swizzle[4];
233 int num_channels;
234
235 switch (_mesa_array_format_get_base_format(format)) {
236 case MESA_ARRAY_FORMAT_BASE_FORMAT_DEPTH:
237 return GL_DEPTH_COMPONENT;
238 case MESA_ARRAY_FORMAT_BASE_FORMAT_STENCIL:
239 return GL_STENCIL_INDEX;
240 case MESA_ARRAY_FORMAT_BASE_FORMAT_RGBA_VARIANTS:
241 break;
242 }
243
244 _mesa_array_format_get_swizzle(format, swizzle);
245 num_channels = _mesa_array_format_get_num_channels(format);
246
247 switch (num_channels) {
248 case 4:
249 /* FIXME: RGBX formats have 4 channels, but their base format is GL_RGB.
250 * This is not really a problem for now because we only create array
251 * formats from GL format/type combinations, and these cannot specify
252 * RGBX formats.
253 */
254 return GL_RGBA;
255 case 3:
256 return GL_RGB;
257 case 2:
258 if (swizzle[0] == 0 &&
259 swizzle[1] == 0 &&
260 swizzle[2] == 0 &&
261 swizzle[3] == 1)
262 return GL_LUMINANCE_ALPHA;
263 if (swizzle[0] == 1 &&
264 swizzle[1] == 1 &&
265 swizzle[2] == 1 &&
266 swizzle[3] == 0)
267 return GL_LUMINANCE_ALPHA;
268 if (swizzle[0] == 0 &&
269 swizzle[1] == 1 &&
270 swizzle[2] == 4 &&
271 swizzle[3] == 5)
272 return GL_RG;
273 if (swizzle[0] == 1 &&
274 swizzle[1] == 0 &&
275 swizzle[2] == 4 &&
276 swizzle[3] == 5)
277 return GL_RG;
278 break;
279 case 1:
280 if (swizzle[0] == 0 &&
281 swizzle[1] == 0 &&
282 swizzle[2] == 0 &&
283 swizzle[3] == 5)
284 return GL_LUMINANCE;
285 if (swizzle[0] == 0 &&
286 swizzle[1] == 0 &&
287 swizzle[2] == 0 &&
288 swizzle[3] == 0)
289 return GL_INTENSITY;
290 if (swizzle[0] <= MESA_FORMAT_SWIZZLE_W)
291 return GL_RED;
292 if (swizzle[1] <= MESA_FORMAT_SWIZZLE_W)
293 return GL_GREEN;
294 if (swizzle[2] <= MESA_FORMAT_SWIZZLE_W)
295 return GL_BLUE;
296 if (swizzle[3] <= MESA_FORMAT_SWIZZLE_W)
297 return GL_ALPHA;
298 break;
299 }
300
301 unreachable("Unsupported format");
302 }
303
304 /**
305 * Return the basic format for the given type. The result will be one of
306 * GL_RGB, GL_RGBA, GL_ALPHA, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_INTENSITY,
307 * GL_YCBCR_MESA, GL_DEPTH_COMPONENT, GL_STENCIL_INDEX, GL_DEPTH_STENCIL.
308 * This functions accepts a mesa_format or a mesa_array_format.
309 */
310 GLenum
311 _mesa_get_format_base_format(uint32_t format)
312 {
313 if (!_mesa_format_is_mesa_array_format(format)) {
314 const struct mesa_format_info *info = _mesa_get_format_info(format);
315 return info->BaseFormat;
316 } else {
317 return get_base_format_for_array_format(format);
318 }
319 }
320
321
322 /**
323 * Return the block size (in pixels) for the given format. Normally
324 * the block size is 1x1. But compressed formats will have block sizes
325 * of 4x4 or 8x4 pixels, etc.
326 * \param bw returns block width in pixels
327 * \param bh returns block height in pixels
328 */
329 void
330 _mesa_get_format_block_size(mesa_format format,
331 unsigned int *bw, unsigned int *bh)
332 {
333 const struct mesa_format_info *info = _mesa_get_format_info(format);
334 /* Use _mesa_get_format_block_size_3d() for 3D blocks. */
335 assert(info->BlockDepth == 1);
336
337 *bw = info->BlockWidth;
338 *bh = info->BlockHeight;
339 }
340
341
342 /**
343 * Return the block size (in pixels) for the given format. Normally
344 * the block size is 1x1x1. But compressed formats will have block
345 * sizes of 4x4x4, 3x3x3 pixels, etc.
346 * \param bw returns block width in pixels
347 * \param bh returns block height in pixels
348 * \param bd returns block depth in pixels
349 */
350 void
351 _mesa_get_format_block_size_3d(mesa_format format,
352 unsigned int *bw,
353 unsigned int *bh,
354 unsigned int *bd)
355 {
356 const struct mesa_format_info *info = _mesa_get_format_info(format);
357 *bw = info->BlockWidth;
358 *bh = info->BlockHeight;
359 *bd = info->BlockDepth;
360 }
361
362
363 /**
364 * Returns the an array of four numbers representing the transformation
365 * from the RGBA or SZ colorspace to the given format. For array formats,
366 * the i'th RGBA component is given by:
367 *
368 * if (swizzle[i] <= MESA_FORMAT_SWIZZLE_W)
369 * comp = data[swizzle[i]];
370 * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_ZERO)
371 * comp = 0;
372 * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_ONE)
373 * comp = 1;
374 * else if (swizzle[i] == MESA_FORMAT_SWIZZLE_NONE)
375 * // data does not contain a channel of this format
376 *
377 * For packed formats, the swizzle gives the number of components left of
378 * the least significant bit.
379 *
380 * Compressed formats have no swizzle.
381 */
382 void
383 _mesa_get_format_swizzle(mesa_format format, uint8_t swizzle_out[4])
384 {
385 const struct mesa_format_info *info = _mesa_get_format_info(format);
386 memcpy(swizzle_out, info->Swizzle, sizeof(info->Swizzle));
387 }
388
389 mesa_array_format
390 _mesa_array_format_flip_channels(mesa_array_format format)
391 {
392 int num_channels;
393 uint8_t swizzle[4];
394
395 num_channels = _mesa_array_format_get_num_channels(format);
396 _mesa_array_format_get_swizzle(format, swizzle);
397
398 if (num_channels == 1)
399 return format;
400
401 if (num_channels == 2) {
402 /* Assert that the swizzle makes sense for 2 channels */
403 for (unsigned i = 0; i < 4; i++)
404 assert(swizzle[i] != 2 && swizzle[i] != 3);
405
406 static const uint8_t flip_xy[6] = { 1, 0, 2, 3, 4, 5 };
407 _mesa_array_format_set_swizzle(&format,
408 flip_xy[swizzle[0]], flip_xy[swizzle[1]],
409 flip_xy[swizzle[2]], flip_xy[swizzle[3]]);
410 return format;
411 }
412
413 if (num_channels == 4) {
414 static const uint8_t flip[6] = { 3, 2, 1, 0, 4, 5 };
415 _mesa_array_format_set_swizzle(&format,
416 flip[swizzle[0]], flip[swizzle[1]],
417 flip[swizzle[2]], flip[swizzle[3]]);
418 return format;
419 }
420
421 unreachable("Invalid array format");
422 }
423
424 uint32_t
425 _mesa_format_to_array_format(mesa_format format)
426 {
427 const struct mesa_format_info *info = _mesa_get_format_info(format);
428 #if UTIL_ARCH_BIG_ENDIAN
429 if (info->ArrayFormat && info->Layout == MESA_FORMAT_LAYOUT_PACKED)
430 return _mesa_array_format_flip_channels(info->ArrayFormat);
431 else
432 #endif
433 return info->ArrayFormat;
434 }
435
436 static struct hash_table *format_array_format_table;
437 static once_flag format_array_format_table_exists = ONCE_FLAG_INIT;
438
439 static void
440 format_array_format_table_destroy(void)
441 {
442 _mesa_hash_table_destroy(format_array_format_table, NULL);
443 }
444
445 static bool
446 array_formats_equal(const void *a, const void *b)
447 {
448 return (intptr_t)a == (intptr_t)b;
449 }
450
451 static void
452 format_array_format_table_init(void)
453 {
454 const struct mesa_format_info *info;
455 mesa_array_format array_format;
456 unsigned f;
457
458 format_array_format_table = _mesa_hash_table_create(NULL, NULL,
459 array_formats_equal);
460
461 if (!format_array_format_table) {
462 _mesa_error_no_memory(__func__);
463 return;
464 }
465
466 for (f = 1; f < MESA_FORMAT_COUNT; ++f) {
467 info = _mesa_get_format_info(f);
468 if (!info->ArrayFormat)
469 continue;
470
471 #if UTIL_ARCH_LITTLE_ENDIAN
472 array_format = info->ArrayFormat;
473 #else
474 array_format = _mesa_array_format_flip_channels(info->ArrayFormat);
475 #endif
476
477 /* This can happen and does for some of the BGR formats. Let's take
478 * the first one in the list.
479 */
480 if (_mesa_hash_table_search_pre_hashed(format_array_format_table,
481 array_format,
482 (void *)(intptr_t)array_format))
483 continue;
484
485 _mesa_hash_table_insert_pre_hashed(format_array_format_table,
486 array_format,
487 (void *)(intptr_t)array_format,
488 (void *)(intptr_t)f);
489 }
490
491 atexit(format_array_format_table_destroy);
492 }
493
494 mesa_format
495 _mesa_format_from_array_format(uint32_t array_format)
496 {
497 struct hash_entry *entry;
498
499 assert(_mesa_format_is_mesa_array_format(array_format));
500
501 call_once(&format_array_format_table_exists, format_array_format_table_init);
502
503 if (!format_array_format_table) {
504 static const once_flag once_flag_init = ONCE_FLAG_INIT;
505 format_array_format_table_exists = once_flag_init;
506 return MESA_FORMAT_NONE;
507 }
508
509 entry = _mesa_hash_table_search_pre_hashed(format_array_format_table,
510 array_format,
511 (void *)(intptr_t)array_format);
512 if (entry)
513 return (intptr_t)entry->data;
514 else
515 return MESA_FORMAT_NONE;
516 }
517
518 /** Is the given format a compressed format? */
519 bool
520 _mesa_is_format_compressed(mesa_format format)
521 {
522 const struct mesa_format_info *info = _mesa_get_format_info(format);
523 return info->BlockWidth > 1 || info->BlockHeight > 1;
524 }
525
526
527 /**
528 * Determine if the given format represents a packed depth/stencil buffer.
529 */
530 bool
531 _mesa_is_format_packed_depth_stencil(mesa_format format)
532 {
533 const struct mesa_format_info *info = _mesa_get_format_info(format);
534
535 return info->BaseFormat == GL_DEPTH_STENCIL;
536 }
537
538
539 /**
540 * Is the given format a signed/unsigned integer color format?
541 */
542 bool
543 _mesa_is_format_integer_color(mesa_format format)
544 {
545 const struct mesa_format_info *info = _mesa_get_format_info(format);
546 return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT) &&
547 info->BaseFormat != GL_DEPTH_COMPONENT &&
548 info->BaseFormat != GL_DEPTH_STENCIL &&
549 info->BaseFormat != GL_STENCIL_INDEX;
550 }
551
552
553 /**
554 * Is the given format an unsigned integer format?
555 */
556 bool
557 _mesa_is_format_unsigned(mesa_format format)
558 {
559 const struct mesa_format_info *info = _mesa_get_format_info(format);
560 return _mesa_is_type_unsigned(info->DataType);
561 }
562
563
564 /**
565 * Does the given format store signed values?
566 */
567 bool
568 _mesa_is_format_signed(mesa_format format)
569 {
570 if (format == MESA_FORMAT_R11G11B10_FLOAT ||
571 format == MESA_FORMAT_R9G9B9E5_FLOAT) {
572 /* these packed float formats only store unsigned values */
573 return false;
574 }
575 else {
576 const struct mesa_format_info *info = _mesa_get_format_info(format);
577 return (info->DataType == GL_SIGNED_NORMALIZED ||
578 info->DataType == GL_INT ||
579 info->DataType == GL_FLOAT);
580 }
581 }
582
583 /**
584 * Is the given format an integer format?
585 */
586 bool
587 _mesa_is_format_integer(mesa_format format)
588 {
589 const struct mesa_format_info *info = _mesa_get_format_info(format);
590 return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT);
591 }
592
593
594 /**
595 * Return true if the given format is a color format.
596 */
597 bool
598 _mesa_is_format_color_format(mesa_format format)
599 {
600 const struct mesa_format_info *info = _mesa_get_format_info(format);
601 switch (info->BaseFormat) {
602 case GL_DEPTH_COMPONENT:
603 case GL_STENCIL_INDEX:
604 case GL_DEPTH_STENCIL:
605 return false;
606 default:
607 return true;
608 }
609 }
610
611 bool
612 _mesa_is_format_srgb(mesa_format format)
613 {
614 const struct mesa_format_info *info = _mesa_get_format_info(format);
615 return info->IsSRGBFormat;
616 }
617
618 /**
619 * Return TRUE if format is an ETC2 compressed format specified
620 * by GL_ARB_ES3_compatibility.
621 */
622 bool
623 _mesa_is_format_etc2(mesa_format format)
624 {
625 switch (format) {
626 case MESA_FORMAT_ETC2_RGB8:
627 case MESA_FORMAT_ETC2_SRGB8:
628 case MESA_FORMAT_ETC2_RGBA8_EAC:
629 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
630 case MESA_FORMAT_ETC2_R11_EAC:
631 case MESA_FORMAT_ETC2_RG11_EAC:
632 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
633 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
634 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
635 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
636 return true;
637 default:
638 return false;
639 }
640 }
641
642
643 /**
644 * Return TRUE if format is an ASTC 2D compressed format.
645 */
646 bool
647 _mesa_is_format_astc_2d(mesa_format format)
648 {
649 switch (format) {
650 case MESA_FORMAT_RGBA_ASTC_4x4:
651 case MESA_FORMAT_RGBA_ASTC_5x4:
652 case MESA_FORMAT_RGBA_ASTC_5x5:
653 case MESA_FORMAT_RGBA_ASTC_6x5:
654 case MESA_FORMAT_RGBA_ASTC_6x6:
655 case MESA_FORMAT_RGBA_ASTC_8x5:
656 case MESA_FORMAT_RGBA_ASTC_8x6:
657 case MESA_FORMAT_RGBA_ASTC_8x8:
658 case MESA_FORMAT_RGBA_ASTC_10x5:
659 case MESA_FORMAT_RGBA_ASTC_10x6:
660 case MESA_FORMAT_RGBA_ASTC_10x8:
661 case MESA_FORMAT_RGBA_ASTC_10x10:
662 case MESA_FORMAT_RGBA_ASTC_12x10:
663 case MESA_FORMAT_RGBA_ASTC_12x12:
664 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4:
665 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4:
666 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5:
667 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5:
668 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6:
669 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5:
670 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6:
671 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8:
672 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5:
673 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6:
674 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8:
675 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10:
676 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10:
677 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12:
678 return true;
679 default:
680 return false;
681 }
682 }
683
684
685 /**
686 * If the given format is a compressed format, return a corresponding
687 * uncompressed format.
688 */
689 mesa_format
690 _mesa_get_uncompressed_format(mesa_format format)
691 {
692 switch (format) {
693 case MESA_FORMAT_RGB_FXT1:
694 return MESA_FORMAT_BGR_UNORM8;
695 case MESA_FORMAT_RGBA_FXT1:
696 return MESA_FORMAT_A8B8G8R8_UNORM;
697 case MESA_FORMAT_RGB_DXT1:
698 case MESA_FORMAT_SRGB_DXT1:
699 return MESA_FORMAT_BGR_UNORM8;
700 case MESA_FORMAT_RGBA_DXT1:
701 case MESA_FORMAT_SRGBA_DXT1:
702 return MESA_FORMAT_A8B8G8R8_UNORM;
703 case MESA_FORMAT_RGBA_DXT3:
704 case MESA_FORMAT_SRGBA_DXT3:
705 return MESA_FORMAT_A8B8G8R8_UNORM;
706 case MESA_FORMAT_RGBA_DXT5:
707 case MESA_FORMAT_SRGBA_DXT5:
708 return MESA_FORMAT_A8B8G8R8_UNORM;
709 case MESA_FORMAT_R_RGTC1_UNORM:
710 return MESA_FORMAT_R_UNORM8;
711 case MESA_FORMAT_R_RGTC1_SNORM:
712 return MESA_FORMAT_R_SNORM8;
713 case MESA_FORMAT_RG_RGTC2_UNORM:
714 return MESA_FORMAT_RG_UNORM8;
715 case MESA_FORMAT_RG_RGTC2_SNORM:
716 return MESA_FORMAT_RG_SNORM8;
717 case MESA_FORMAT_L_LATC1_UNORM:
718 return MESA_FORMAT_L_UNORM8;
719 case MESA_FORMAT_L_LATC1_SNORM:
720 return MESA_FORMAT_L_SNORM8;
721 case MESA_FORMAT_LA_LATC2_UNORM:
722 return MESA_FORMAT_LA_UNORM8;
723 case MESA_FORMAT_LA_LATC2_SNORM:
724 return MESA_FORMAT_LA_SNORM8;
725 case MESA_FORMAT_ETC1_RGB8:
726 case MESA_FORMAT_ETC2_RGB8:
727 case MESA_FORMAT_ETC2_SRGB8:
728 case MESA_FORMAT_ATC_RGB:
729 return MESA_FORMAT_BGR_UNORM8;
730 case MESA_FORMAT_ETC2_RGBA8_EAC:
731 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
732 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
733 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
734 case MESA_FORMAT_ATC_RGBA_EXPLICIT:
735 case MESA_FORMAT_ATC_RGBA_INTERPOLATED:
736 return MESA_FORMAT_A8B8G8R8_UNORM;
737 case MESA_FORMAT_ETC2_R11_EAC:
738 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
739 return MESA_FORMAT_R_UNORM16;
740 case MESA_FORMAT_ETC2_RG11_EAC:
741 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
742 return MESA_FORMAT_RG_UNORM16;
743 case MESA_FORMAT_BPTC_RGBA_UNORM:
744 case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
745 return MESA_FORMAT_A8B8G8R8_UNORM;
746 case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
747 case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
748 return MESA_FORMAT_RGB_FLOAT32;
749 default:
750 assert(!_mesa_is_format_compressed(format));
751 return format;
752 }
753 }
754
755
756 unsigned int
757 _mesa_format_num_components(mesa_format format)
758 {
759 const struct mesa_format_info *info = _mesa_get_format_info(format);
760 return ((info->RedBits > 0) +
761 (info->GreenBits > 0) +
762 (info->BlueBits > 0) +
763 (info->AlphaBits > 0) +
764 (info->LuminanceBits > 0) +
765 (info->IntensityBits > 0) +
766 (info->DepthBits > 0) +
767 (info->StencilBits > 0));
768 }
769
770
771 /**
772 * Returns true if a color format has data stored in the R/G/B/A channels,
773 * given an index from 0 to 3.
774 */
775 bool
776 _mesa_format_has_color_component(mesa_format format, int component)
777 {
778 const struct mesa_format_info *info = _mesa_get_format_info(format);
779
780 assert(info->BaseFormat != GL_DEPTH_COMPONENT &&
781 info->BaseFormat != GL_DEPTH_STENCIL &&
782 info->BaseFormat != GL_STENCIL_INDEX);
783
784 switch (component) {
785 case 0:
786 return (info->RedBits + info->IntensityBits + info->LuminanceBits) > 0;
787 case 1:
788 return (info->GreenBits + info->IntensityBits + info->LuminanceBits) > 0;
789 case 2:
790 return (info->BlueBits + info->IntensityBits + info->LuminanceBits) > 0;
791 case 3:
792 return (info->AlphaBits + info->IntensityBits) > 0;
793 default:
794 assert(!"Invalid color component: must be 0..3");
795 return false;
796 }
797 }
798
799
800 /**
801 * Return number of bytes needed to store an image of the given size
802 * in the given format.
803 */
804 uint32_t
805 _mesa_format_image_size(mesa_format format, int width,
806 int height, int depth)
807 {
808 const struct mesa_format_info *info = _mesa_get_format_info(format);
809 uint32_t sz;
810 /* Strictly speaking, a conditional isn't needed here */
811 if (info->BlockWidth > 1 || info->BlockHeight > 1 || info->BlockDepth > 1) {
812 /* compressed format (2D only for now) */
813 const uint32_t bw = info->BlockWidth;
814 const uint32_t bh = info->BlockHeight;
815 const uint32_t bd = info->BlockDepth;
816 const uint32_t wblocks = (width + bw - 1) / bw;
817 const uint32_t hblocks = (height + bh - 1) / bh;
818 const uint32_t dblocks = (depth + bd - 1) / bd;
819 sz = wblocks * hblocks * dblocks * info->BytesPerBlock;
820 } else
821 /* non-compressed */
822 sz = width * height * depth * info->BytesPerBlock;
823
824 return sz;
825 }
826
827
828 /**
829 * Same as _mesa_format_image_size() but returns a 64-bit value to
830 * accommodate very large textures.
831 */
832 uint64_t
833 _mesa_format_image_size64(mesa_format format, int width,
834 int height, int depth)
835 {
836 const struct mesa_format_info *info = _mesa_get_format_info(format);
837 uint64_t sz;
838 /* Strictly speaking, a conditional isn't needed here */
839 if (info->BlockWidth > 1 || info->BlockHeight > 1 || info->BlockDepth > 1) {
840 /* compressed format (2D only for now) */
841 const uint64_t bw = info->BlockWidth;
842 const uint64_t bh = info->BlockHeight;
843 const uint64_t bd = info->BlockDepth;
844 const uint64_t wblocks = (width + bw - 1) / bw;
845 const uint64_t hblocks = (height + bh - 1) / bh;
846 const uint64_t dblocks = (depth + bd - 1) / bd;
847 sz = wblocks * hblocks * dblocks * info->BytesPerBlock;
848 } else
849 /* non-compressed */
850 sz = ((uint64_t) width * (uint64_t) height *
851 (uint64_t) depth * info->BytesPerBlock);
852
853 return sz;
854 }
855
856
857
858 int32_t
859 _mesa_format_row_stride(mesa_format format, int width)
860 {
861 const struct mesa_format_info *info = _mesa_get_format_info(format);
862 /* Strictly speaking, a conditional isn't needed here */
863 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
864 /* compressed format */
865 const uint32_t bw = info->BlockWidth;
866 const uint32_t wblocks = (width + bw - 1) / bw;
867 const int32_t stride = wblocks * info->BytesPerBlock;
868 return stride;
869 }
870 else {
871 const int32_t stride = width * info->BytesPerBlock;
872 return stride;
873 }
874 }
875
876
877
878 /**
879 * Return datatype and number of components per texel for the given
880 * uncompressed mesa_format. Only used for mipmap generation code.
881 */
882 void
883 _mesa_uncompressed_format_to_type_and_comps(mesa_format format,
884 GLenum *datatype, GLuint *comps)
885 {
886 switch (format) {
887 case MESA_FORMAT_A8B8G8R8_UNORM:
888 case MESA_FORMAT_R8G8B8A8_UNORM:
889 case MESA_FORMAT_B8G8R8A8_UNORM:
890 case MESA_FORMAT_A8R8G8B8_UNORM:
891 case MESA_FORMAT_X8B8G8R8_UNORM:
892 case MESA_FORMAT_R8G8B8X8_UNORM:
893 case MESA_FORMAT_B8G8R8X8_UNORM:
894 case MESA_FORMAT_X8R8G8B8_UNORM:
895 case MESA_FORMAT_A8B8G8R8_UINT:
896 case MESA_FORMAT_R8G8B8A8_UINT:
897 case MESA_FORMAT_B8G8R8A8_UINT:
898 case MESA_FORMAT_A8R8G8B8_UINT:
899 *datatype = GL_UNSIGNED_BYTE;
900 *comps = 4;
901 return;
902 case MESA_FORMAT_BGR_UNORM8:
903 case MESA_FORMAT_RGB_UNORM8:
904 *datatype = GL_UNSIGNED_BYTE;
905 *comps = 3;
906 return;
907 case MESA_FORMAT_B5G6R5_UNORM:
908 case MESA_FORMAT_R5G6B5_UNORM:
909 case MESA_FORMAT_B5G6R5_UINT:
910 case MESA_FORMAT_R5G6B5_UINT:
911 *datatype = GL_UNSIGNED_SHORT_5_6_5;
912 *comps = 3;
913 return;
914
915 case MESA_FORMAT_B4G4R4A4_UNORM:
916 case MESA_FORMAT_A4R4G4B4_UNORM:
917 case MESA_FORMAT_B4G4R4X4_UNORM:
918 case MESA_FORMAT_B4G4R4A4_UINT:
919 case MESA_FORMAT_A4R4G4B4_UINT:
920 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
921 *comps = 4;
922 return;
923
924 case MESA_FORMAT_B5G5R5A1_UNORM:
925 case MESA_FORMAT_A1R5G5B5_UNORM:
926 case MESA_FORMAT_B5G5R5X1_UNORM:
927 case MESA_FORMAT_B5G5R5A1_UINT:
928 case MESA_FORMAT_A1R5G5B5_UINT:
929 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
930 *comps = 4;
931 return;
932
933 case MESA_FORMAT_B10G10R10A2_UNORM:
934 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
935 *comps = 4;
936 return;
937
938 case MESA_FORMAT_A1B5G5R5_UNORM:
939 case MESA_FORMAT_A1B5G5R5_UINT:
940 case MESA_FORMAT_X1B5G5R5_UNORM:
941 *datatype = GL_UNSIGNED_SHORT_5_5_5_1;
942 *comps = 4;
943 return;
944
945 case MESA_FORMAT_L4A4_UNORM:
946 *datatype = MESA_UNSIGNED_BYTE_4_4;
947 *comps = 2;
948 return;
949
950 case MESA_FORMAT_LA_UNORM8:
951 case MESA_FORMAT_RG_UNORM8:
952 *datatype = GL_UNSIGNED_BYTE;
953 *comps = 2;
954 return;
955
956 case MESA_FORMAT_LA_UNORM16:
957 case MESA_FORMAT_RG_UNORM16:
958 *datatype = GL_UNSIGNED_SHORT;
959 *comps = 2;
960 return;
961
962 case MESA_FORMAT_R_UNORM16:
963 case MESA_FORMAT_A_UNORM16:
964 case MESA_FORMAT_L_UNORM16:
965 case MESA_FORMAT_I_UNORM16:
966 *datatype = GL_UNSIGNED_SHORT;
967 *comps = 1;
968 return;
969
970 case MESA_FORMAT_R3G3B2_UNORM:
971 case MESA_FORMAT_R3G3B2_UINT:
972 *datatype = GL_UNSIGNED_BYTE_2_3_3_REV;
973 *comps = 3;
974 return;
975 case MESA_FORMAT_A4B4G4R4_UNORM:
976 case MESA_FORMAT_A4B4G4R4_UINT:
977 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
978 *comps = 4;
979 return;
980
981 case MESA_FORMAT_R4G4B4A4_UNORM:
982 case MESA_FORMAT_R4G4B4A4_UINT:
983 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
984 *comps = 4;
985 return;
986 case MESA_FORMAT_R5G5B5A1_UNORM:
987 case MESA_FORMAT_R5G5B5A1_UINT:
988 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
989 *comps = 4;
990 return;
991 case MESA_FORMAT_A2B10G10R10_UNORM:
992 case MESA_FORMAT_A2B10G10R10_UINT:
993 *datatype = GL_UNSIGNED_INT_10_10_10_2;
994 *comps = 4;
995 return;
996 case MESA_FORMAT_A2R10G10B10_UNORM:
997 case MESA_FORMAT_A2R10G10B10_UINT:
998 *datatype = GL_UNSIGNED_INT_10_10_10_2;
999 *comps = 4;
1000 return;
1001
1002 case MESA_FORMAT_B2G3R3_UNORM:
1003 case MESA_FORMAT_B2G3R3_UINT:
1004 *datatype = GL_UNSIGNED_BYTE_3_3_2;
1005 *comps = 3;
1006 return;
1007
1008 case MESA_FORMAT_A_UNORM8:
1009 case MESA_FORMAT_L_UNORM8:
1010 case MESA_FORMAT_I_UNORM8:
1011 case MESA_FORMAT_R_UNORM8:
1012 case MESA_FORMAT_S_UINT8:
1013 *datatype = GL_UNSIGNED_BYTE;
1014 *comps = 1;
1015 return;
1016
1017 case MESA_FORMAT_YCBCR:
1018 case MESA_FORMAT_YCBCR_REV:
1019 *datatype = GL_UNSIGNED_SHORT;
1020 *comps = 2;
1021 return;
1022
1023 case MESA_FORMAT_S8_UINT_Z24_UNORM:
1024 *datatype = GL_UNSIGNED_INT_24_8_MESA;
1025 *comps = 2;
1026 return;
1027
1028 case MESA_FORMAT_Z24_UNORM_S8_UINT:
1029 *datatype = GL_UNSIGNED_INT_8_24_REV_MESA;
1030 *comps = 2;
1031 return;
1032
1033 case MESA_FORMAT_Z_UNORM16:
1034 *datatype = GL_UNSIGNED_SHORT;
1035 *comps = 1;
1036 return;
1037
1038 case MESA_FORMAT_Z24_UNORM_X8_UINT:
1039 *datatype = GL_UNSIGNED_INT;
1040 *comps = 1;
1041 return;
1042
1043 case MESA_FORMAT_X8_UINT_Z24_UNORM:
1044 *datatype = GL_UNSIGNED_INT;
1045 *comps = 1;
1046 return;
1047
1048 case MESA_FORMAT_Z_UNORM32:
1049 *datatype = GL_UNSIGNED_INT;
1050 *comps = 1;
1051 return;
1052
1053 case MESA_FORMAT_Z_FLOAT32:
1054 *datatype = GL_FLOAT;
1055 *comps = 1;
1056 return;
1057
1058 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1059 *datatype = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
1060 *comps = 1;
1061 return;
1062
1063 case MESA_FORMAT_R_SNORM8:
1064 case MESA_FORMAT_A_SNORM8:
1065 case MESA_FORMAT_L_SNORM8:
1066 case MESA_FORMAT_I_SNORM8:
1067 *datatype = GL_BYTE;
1068 *comps = 1;
1069 return;
1070 case MESA_FORMAT_RG_SNORM8:
1071 case MESA_FORMAT_LA_SNORM8:
1072 *datatype = GL_BYTE;
1073 *comps = 2;
1074 return;
1075 case MESA_FORMAT_A8B8G8R8_SNORM:
1076 case MESA_FORMAT_R8G8B8A8_SNORM:
1077 case MESA_FORMAT_X8B8G8R8_SNORM:
1078 *datatype = GL_BYTE;
1079 *comps = 4;
1080 return;
1081
1082 case MESA_FORMAT_RGBA_UNORM16:
1083 *datatype = GL_UNSIGNED_SHORT;
1084 *comps = 4;
1085 return;
1086
1087 case MESA_FORMAT_R_SNORM16:
1088 case MESA_FORMAT_A_SNORM16:
1089 case MESA_FORMAT_L_SNORM16:
1090 case MESA_FORMAT_I_SNORM16:
1091 *datatype = GL_SHORT;
1092 *comps = 1;
1093 return;
1094 case MESA_FORMAT_RG_SNORM16:
1095 case MESA_FORMAT_LA_SNORM16:
1096 *datatype = GL_SHORT;
1097 *comps = 2;
1098 return;
1099 case MESA_FORMAT_RGB_SNORM16:
1100 *datatype = GL_SHORT;
1101 *comps = 3;
1102 return;
1103 case MESA_FORMAT_RGBA_SNORM16:
1104 *datatype = GL_SHORT;
1105 *comps = 4;
1106 return;
1107
1108 case MESA_FORMAT_BGR_SRGB8:
1109 *datatype = GL_UNSIGNED_BYTE;
1110 *comps = 3;
1111 return;
1112 case MESA_FORMAT_A8B8G8R8_SRGB:
1113 case MESA_FORMAT_B8G8R8A8_SRGB:
1114 case MESA_FORMAT_A8R8G8B8_SRGB:
1115 case MESA_FORMAT_R8G8B8A8_SRGB:
1116 *datatype = GL_UNSIGNED_BYTE;
1117 *comps = 4;
1118 return;
1119 case MESA_FORMAT_L_SRGB8:
1120 case MESA_FORMAT_R_SRGB8:
1121 *datatype = GL_UNSIGNED_BYTE;
1122 *comps = 1;
1123 return;
1124 case MESA_FORMAT_LA_SRGB8:
1125 *datatype = GL_UNSIGNED_BYTE;
1126 *comps = 2;
1127 return;
1128
1129 case MESA_FORMAT_RGBA_FLOAT32:
1130 *datatype = GL_FLOAT;
1131 *comps = 4;
1132 return;
1133 case MESA_FORMAT_RGBA_FLOAT16:
1134 *datatype = GL_HALF_FLOAT_ARB;
1135 *comps = 4;
1136 return;
1137 case MESA_FORMAT_RGB_FLOAT32:
1138 *datatype = GL_FLOAT;
1139 *comps = 3;
1140 return;
1141 case MESA_FORMAT_RGB_FLOAT16:
1142 *datatype = GL_HALF_FLOAT_ARB;
1143 *comps = 3;
1144 return;
1145 case MESA_FORMAT_LA_FLOAT32:
1146 case MESA_FORMAT_RG_FLOAT32:
1147 *datatype = GL_FLOAT;
1148 *comps = 2;
1149 return;
1150 case MESA_FORMAT_LA_FLOAT16:
1151 case MESA_FORMAT_RG_FLOAT16:
1152 *datatype = GL_HALF_FLOAT_ARB;
1153 *comps = 2;
1154 return;
1155 case MESA_FORMAT_A_FLOAT32:
1156 case MESA_FORMAT_L_FLOAT32:
1157 case MESA_FORMAT_I_FLOAT32:
1158 case MESA_FORMAT_R_FLOAT32:
1159 *datatype = GL_FLOAT;
1160 *comps = 1;
1161 return;
1162 case MESA_FORMAT_A_FLOAT16:
1163 case MESA_FORMAT_L_FLOAT16:
1164 case MESA_FORMAT_I_FLOAT16:
1165 case MESA_FORMAT_R_FLOAT16:
1166 *datatype = GL_HALF_FLOAT_ARB;
1167 *comps = 1;
1168 return;
1169
1170 case MESA_FORMAT_A_UINT8:
1171 case MESA_FORMAT_L_UINT8:
1172 case MESA_FORMAT_I_UINT8:
1173 *datatype = GL_UNSIGNED_BYTE;
1174 *comps = 1;
1175 return;
1176 case MESA_FORMAT_LA_UINT8:
1177 *datatype = GL_UNSIGNED_BYTE;
1178 *comps = 2;
1179 return;
1180
1181 case MESA_FORMAT_A_UINT16:
1182 case MESA_FORMAT_L_UINT16:
1183 case MESA_FORMAT_I_UINT16:
1184 *datatype = GL_UNSIGNED_SHORT;
1185 *comps = 1;
1186 return;
1187 case MESA_FORMAT_LA_UINT16:
1188 *datatype = GL_UNSIGNED_SHORT;
1189 *comps = 2;
1190 return;
1191 case MESA_FORMAT_A_UINT32:
1192 case MESA_FORMAT_L_UINT32:
1193 case MESA_FORMAT_I_UINT32:
1194 *datatype = GL_UNSIGNED_INT;
1195 *comps = 1;
1196 return;
1197 case MESA_FORMAT_LA_UINT32:
1198 *datatype = GL_UNSIGNED_INT;
1199 *comps = 2;
1200 return;
1201 case MESA_FORMAT_A_SINT8:
1202 case MESA_FORMAT_L_SINT8:
1203 case MESA_FORMAT_I_SINT8:
1204 *datatype = GL_BYTE;
1205 *comps = 1;
1206 return;
1207 case MESA_FORMAT_LA_SINT8:
1208 *datatype = GL_BYTE;
1209 *comps = 2;
1210 return;
1211
1212 case MESA_FORMAT_A_SINT16:
1213 case MESA_FORMAT_L_SINT16:
1214 case MESA_FORMAT_I_SINT16:
1215 *datatype = GL_SHORT;
1216 *comps = 1;
1217 return;
1218 case MESA_FORMAT_LA_SINT16:
1219 *datatype = GL_SHORT;
1220 *comps = 2;
1221 return;
1222
1223 case MESA_FORMAT_A_SINT32:
1224 case MESA_FORMAT_L_SINT32:
1225 case MESA_FORMAT_I_SINT32:
1226 *datatype = GL_INT;
1227 *comps = 1;
1228 return;
1229 case MESA_FORMAT_LA_SINT32:
1230 *datatype = GL_INT;
1231 *comps = 2;
1232 return;
1233
1234 case MESA_FORMAT_R_SINT8:
1235 *datatype = GL_BYTE;
1236 *comps = 1;
1237 return;
1238 case MESA_FORMAT_RG_SINT8:
1239 *datatype = GL_BYTE;
1240 *comps = 2;
1241 return;
1242 case MESA_FORMAT_RGB_SINT8:
1243 *datatype = GL_BYTE;
1244 *comps = 3;
1245 return;
1246 case MESA_FORMAT_RGBA_SINT8:
1247 *datatype = GL_BYTE;
1248 *comps = 4;
1249 return;
1250 case MESA_FORMAT_R_SINT16:
1251 *datatype = GL_SHORT;
1252 *comps = 1;
1253 return;
1254 case MESA_FORMAT_RG_SINT16:
1255 *datatype = GL_SHORT;
1256 *comps = 2;
1257 return;
1258 case MESA_FORMAT_RGB_SINT16:
1259 *datatype = GL_SHORT;
1260 *comps = 3;
1261 return;
1262 case MESA_FORMAT_RGBA_SINT16:
1263 *datatype = GL_SHORT;
1264 *comps = 4;
1265 return;
1266 case MESA_FORMAT_R_SINT32:
1267 *datatype = GL_INT;
1268 *comps = 1;
1269 return;
1270 case MESA_FORMAT_RG_SINT32:
1271 *datatype = GL_INT;
1272 *comps = 2;
1273 return;
1274 case MESA_FORMAT_RGB_SINT32:
1275 *datatype = GL_INT;
1276 *comps = 3;
1277 return;
1278 case MESA_FORMAT_RGBA_SINT32:
1279 *datatype = GL_INT;
1280 *comps = 4;
1281 return;
1282
1283 /**
1284 * \name Non-normalized unsigned integer formats.
1285 */
1286 case MESA_FORMAT_R_UINT8:
1287 *datatype = GL_UNSIGNED_BYTE;
1288 *comps = 1;
1289 return;
1290 case MESA_FORMAT_RG_UINT8:
1291 *datatype = GL_UNSIGNED_BYTE;
1292 *comps = 2;
1293 return;
1294 case MESA_FORMAT_RGB_UINT8:
1295 *datatype = GL_UNSIGNED_BYTE;
1296 *comps = 3;
1297 return;
1298 case MESA_FORMAT_RGBA_UINT8:
1299 *datatype = GL_UNSIGNED_BYTE;
1300 *comps = 4;
1301 return;
1302 case MESA_FORMAT_R_UINT16:
1303 *datatype = GL_UNSIGNED_SHORT;
1304 *comps = 1;
1305 return;
1306 case MESA_FORMAT_RG_UINT16:
1307 *datatype = GL_UNSIGNED_SHORT;
1308 *comps = 2;
1309 return;
1310 case MESA_FORMAT_RGB_UINT16:
1311 *datatype = GL_UNSIGNED_SHORT;
1312 *comps = 3;
1313 return;
1314 case MESA_FORMAT_RGBA_UINT16:
1315 *datatype = GL_UNSIGNED_SHORT;
1316 *comps = 4;
1317 return;
1318 case MESA_FORMAT_R_UINT32:
1319 *datatype = GL_UNSIGNED_INT;
1320 *comps = 1;
1321 return;
1322 case MESA_FORMAT_RG_UINT32:
1323 *datatype = GL_UNSIGNED_INT;
1324 *comps = 2;
1325 return;
1326 case MESA_FORMAT_RGB_UINT32:
1327 *datatype = GL_UNSIGNED_INT;
1328 *comps = 3;
1329 return;
1330 case MESA_FORMAT_RGBA_UINT32:
1331 *datatype = GL_UNSIGNED_INT;
1332 *comps = 4;
1333 return;
1334
1335 case MESA_FORMAT_R9G9B9E5_FLOAT:
1336 *datatype = GL_UNSIGNED_INT_5_9_9_9_REV;
1337 *comps = 3;
1338 return;
1339
1340 case MESA_FORMAT_R11G11B10_FLOAT:
1341 *datatype = GL_UNSIGNED_INT_10F_11F_11F_REV;
1342 *comps = 3;
1343 return;
1344
1345 case MESA_FORMAT_B10G10R10A2_UINT:
1346 case MESA_FORMAT_R10G10B10A2_UINT:
1347 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1348 *comps = 4;
1349 return;
1350
1351 case MESA_FORMAT_R8G8B8X8_SRGB:
1352 case MESA_FORMAT_X8B8G8R8_SRGB:
1353 case MESA_FORMAT_RGBX_UINT8:
1354 *datatype = GL_UNSIGNED_BYTE;
1355 *comps = 4;
1356 return;
1357
1358 case MESA_FORMAT_R8G8B8X8_SNORM:
1359 case MESA_FORMAT_RGBX_SINT8:
1360 *datatype = GL_BYTE;
1361 *comps = 4;
1362 return;
1363
1364 case MESA_FORMAT_B10G10R10X2_UNORM:
1365 case MESA_FORMAT_R10G10B10X2_UNORM:
1366 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1367 *comps = 4;
1368 return;
1369
1370 case MESA_FORMAT_RGBX_UNORM16:
1371 case MESA_FORMAT_RGBX_UINT16:
1372 *datatype = GL_UNSIGNED_SHORT;
1373 *comps = 4;
1374 return;
1375
1376 case MESA_FORMAT_RGBX_SNORM16:
1377 case MESA_FORMAT_RGBX_SINT16:
1378 *datatype = GL_SHORT;
1379 *comps = 4;
1380 return;
1381
1382 case MESA_FORMAT_RGBX_FLOAT16:
1383 *datatype = GL_HALF_FLOAT;
1384 *comps = 4;
1385 return;
1386
1387 case MESA_FORMAT_RGBX_FLOAT32:
1388 *datatype = GL_FLOAT;
1389 *comps = 4;
1390 return;
1391
1392 case MESA_FORMAT_RGBX_UINT32:
1393 *datatype = GL_UNSIGNED_INT;
1394 *comps = 4;
1395 return;
1396
1397 case MESA_FORMAT_RGBX_SINT32:
1398 *datatype = GL_INT;
1399 *comps = 4;
1400 return;
1401
1402 case MESA_FORMAT_R10G10B10A2_UNORM:
1403 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1404 *comps = 4;
1405 return;
1406
1407 case MESA_FORMAT_B8G8R8X8_SRGB:
1408 case MESA_FORMAT_X8R8G8B8_SRGB:
1409 *datatype = GL_UNSIGNED_BYTE;
1410 *comps = 4;
1411 return;
1412
1413 case MESA_FORMAT_COUNT:
1414 assert(0);
1415 return;
1416 default:
1417 /* Warn if any formats are not handled */
1418 _mesa_problem(NULL, "bad format %s in _mesa_uncompressed_format_to_type_and_comps",
1419 _mesa_get_format_name(format));
1420 assert(format == MESA_FORMAT_NONE ||
1421 _mesa_is_format_compressed(format));
1422 *datatype = 0;
1423 *comps = 1;
1424 }
1425 }
1426
1427 /**
1428 * Check if a mesa_format exactly matches a GL format/type combination
1429 * such that we can use memcpy() from one to the other.
1430 * \param mesa_format a MESA_FORMAT_x value
1431 * \param format the user-specified image format
1432 * \param type the user-specified image datatype
1433 * \param swapBytes typically the current pixel pack/unpack byteswap state
1434 * \param[out] error GL_NO_ERROR if format is an expected input.
1435 * GL_INVALID_ENUM if format is an unexpected input.
1436 * \return true if the formats match, false otherwise.
1437 */
1438 bool
1439 _mesa_format_matches_format_and_type(mesa_format mformat,
1440 GLenum format, GLenum type,
1441 bool swapBytes, GLenum *error)
1442 {
1443 if (error)
1444 *error = GL_NO_ERROR;
1445
1446 if (_mesa_is_format_compressed(mformat)) {
1447 if (error)
1448 *error = GL_INVALID_ENUM;
1449 return false;
1450 }
1451
1452 if (swapBytes && !_mesa_swap_bytes_in_type_enum(&type))
1453 return false;
1454
1455 /* format/type don't include srgb and should match regardless of it. */
1456 mformat = _mesa_get_srgb_format_linear(mformat);
1457
1458 /* intensity formats are uploaded with GL_RED, and we want to find
1459 * memcpy matches for them.
1460 */
1461 mformat = _mesa_get_intensity_format_red(mformat);
1462
1463 if (format == GL_COLOR_INDEX)
1464 return false;
1465
1466 mesa_format other_format = _mesa_format_from_format_and_type(format, type);
1467 if (_mesa_format_is_mesa_array_format(other_format))
1468 other_format = _mesa_format_from_array_format(other_format);
1469
1470 return other_format == mformat;
1471 }
1472