mesa: Redefine the RG formats as array formats.
[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 (info->ArrayFormat && !_mesa_little_endian() &&
429 info->Layout == MESA_FORMAT_LAYOUT_PACKED)
430 return _mesa_array_format_flip_channels(info->ArrayFormat);
431 else
432 return info->ArrayFormat;
433 }
434
435 static struct hash_table *format_array_format_table;
436 static once_flag format_array_format_table_exists = ONCE_FLAG_INIT;
437
438 static void
439 format_array_format_table_destroy(void)
440 {
441 _mesa_hash_table_destroy(format_array_format_table, NULL);
442 }
443
444 static bool
445 array_formats_equal(const void *a, const void *b)
446 {
447 return (intptr_t)a == (intptr_t)b;
448 }
449
450 static void
451 format_array_format_table_init(void)
452 {
453 const struct mesa_format_info *info;
454 mesa_array_format array_format;
455 unsigned f;
456
457 format_array_format_table = _mesa_hash_table_create(NULL, NULL,
458 array_formats_equal);
459
460 if (!format_array_format_table) {
461 _mesa_error_no_memory(__func__);
462 return;
463 }
464
465 for (f = 1; f < MESA_FORMAT_COUNT; ++f) {
466 info = _mesa_get_format_info(f);
467 if (!info->ArrayFormat)
468 continue;
469
470 if (_mesa_little_endian()) {
471 array_format = info->ArrayFormat;
472 } else {
473 array_format = _mesa_array_format_flip_channels(info->ArrayFormat);
474 }
475
476 /* This can happen and does for some of the BGR formats. Let's take
477 * the first one in the list.
478 */
479 if (_mesa_hash_table_search_pre_hashed(format_array_format_table,
480 array_format,
481 (void *)(intptr_t)array_format))
482 continue;
483
484 _mesa_hash_table_insert_pre_hashed(format_array_format_table,
485 array_format,
486 (void *)(intptr_t)array_format,
487 (void *)(intptr_t)f);
488 }
489
490 atexit(format_array_format_table_destroy);
491 }
492
493 mesa_format
494 _mesa_format_from_array_format(uint32_t array_format)
495 {
496 struct hash_entry *entry;
497
498 assert(_mesa_format_is_mesa_array_format(array_format));
499
500 call_once(&format_array_format_table_exists, format_array_format_table_init);
501
502 if (!format_array_format_table) {
503 static const once_flag once_flag_init = ONCE_FLAG_INIT;
504 format_array_format_table_exists = once_flag_init;
505 return MESA_FORMAT_NONE;
506 }
507
508 entry = _mesa_hash_table_search_pre_hashed(format_array_format_table,
509 array_format,
510 (void *)(intptr_t)array_format);
511 if (entry)
512 return (intptr_t)entry->data;
513 else
514 return MESA_FORMAT_NONE;
515 }
516
517 /** Is the given format a compressed format? */
518 bool
519 _mesa_is_format_compressed(mesa_format format)
520 {
521 const struct mesa_format_info *info = _mesa_get_format_info(format);
522 return info->BlockWidth > 1 || info->BlockHeight > 1;
523 }
524
525
526 /**
527 * Determine if the given format represents a packed depth/stencil buffer.
528 */
529 bool
530 _mesa_is_format_packed_depth_stencil(mesa_format format)
531 {
532 const struct mesa_format_info *info = _mesa_get_format_info(format);
533
534 return info->BaseFormat == GL_DEPTH_STENCIL;
535 }
536
537
538 /**
539 * Is the given format a signed/unsigned integer color format?
540 */
541 bool
542 _mesa_is_format_integer_color(mesa_format format)
543 {
544 const struct mesa_format_info *info = _mesa_get_format_info(format);
545 return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT) &&
546 info->BaseFormat != GL_DEPTH_COMPONENT &&
547 info->BaseFormat != GL_DEPTH_STENCIL &&
548 info->BaseFormat != GL_STENCIL_INDEX;
549 }
550
551
552 /**
553 * Is the given format an unsigned integer format?
554 */
555 bool
556 _mesa_is_format_unsigned(mesa_format format)
557 {
558 const struct mesa_format_info *info = _mesa_get_format_info(format);
559 return _mesa_is_type_unsigned(info->DataType);
560 }
561
562
563 /**
564 * Does the given format store signed values?
565 */
566 bool
567 _mesa_is_format_signed(mesa_format format)
568 {
569 if (format == MESA_FORMAT_R11G11B10_FLOAT ||
570 format == MESA_FORMAT_R9G9B9E5_FLOAT) {
571 /* these packed float formats only store unsigned values */
572 return false;
573 }
574 else {
575 const struct mesa_format_info *info = _mesa_get_format_info(format);
576 return (info->DataType == GL_SIGNED_NORMALIZED ||
577 info->DataType == GL_INT ||
578 info->DataType == GL_FLOAT);
579 }
580 }
581
582 /**
583 * Is the given format an integer format?
584 */
585 bool
586 _mesa_is_format_integer(mesa_format format)
587 {
588 const struct mesa_format_info *info = _mesa_get_format_info(format);
589 return (info->DataType == GL_INT || info->DataType == GL_UNSIGNED_INT);
590 }
591
592
593 /**
594 * Return true if the given format is a color format.
595 */
596 bool
597 _mesa_is_format_color_format(mesa_format format)
598 {
599 const struct mesa_format_info *info = _mesa_get_format_info(format);
600 switch (info->BaseFormat) {
601 case GL_DEPTH_COMPONENT:
602 case GL_STENCIL_INDEX:
603 case GL_DEPTH_STENCIL:
604 return false;
605 default:
606 return true;
607 }
608 }
609
610 bool
611 _mesa_is_format_srgb(mesa_format format)
612 {
613 const struct mesa_format_info *info = _mesa_get_format_info(format);
614 return info->IsSRGBFormat;
615 }
616
617 /**
618 * Return TRUE if format is an ETC2 compressed format specified
619 * by GL_ARB_ES3_compatibility.
620 */
621 bool
622 _mesa_is_format_etc2(mesa_format format)
623 {
624 switch (format) {
625 case MESA_FORMAT_ETC2_RGB8:
626 case MESA_FORMAT_ETC2_SRGB8:
627 case MESA_FORMAT_ETC2_RGBA8_EAC:
628 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
629 case MESA_FORMAT_ETC2_R11_EAC:
630 case MESA_FORMAT_ETC2_RG11_EAC:
631 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
632 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
633 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
634 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
635 return true;
636 default:
637 return false;
638 }
639 }
640
641
642 /**
643 * Return TRUE if format is an ASTC 2D compressed format.
644 */
645 bool
646 _mesa_is_format_astc_2d(mesa_format format)
647 {
648 switch (format) {
649 case MESA_FORMAT_RGBA_ASTC_4x4:
650 case MESA_FORMAT_RGBA_ASTC_5x4:
651 case MESA_FORMAT_RGBA_ASTC_5x5:
652 case MESA_FORMAT_RGBA_ASTC_6x5:
653 case MESA_FORMAT_RGBA_ASTC_6x6:
654 case MESA_FORMAT_RGBA_ASTC_8x5:
655 case MESA_FORMAT_RGBA_ASTC_8x6:
656 case MESA_FORMAT_RGBA_ASTC_8x8:
657 case MESA_FORMAT_RGBA_ASTC_10x5:
658 case MESA_FORMAT_RGBA_ASTC_10x6:
659 case MESA_FORMAT_RGBA_ASTC_10x8:
660 case MESA_FORMAT_RGBA_ASTC_10x10:
661 case MESA_FORMAT_RGBA_ASTC_12x10:
662 case MESA_FORMAT_RGBA_ASTC_12x12:
663 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_4x4:
664 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x4:
665 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5:
666 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x5:
667 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_6x6:
668 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x5:
669 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x6:
670 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_8x8:
671 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x5:
672 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x6:
673 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x8:
674 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_10x10:
675 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x10:
676 case MESA_FORMAT_SRGB8_ALPHA8_ASTC_12x12:
677 return true;
678 default:
679 return false;
680 }
681 }
682
683
684 /**
685 * If the given format is a compressed format, return a corresponding
686 * uncompressed format.
687 */
688 mesa_format
689 _mesa_get_uncompressed_format(mesa_format format)
690 {
691 switch (format) {
692 case MESA_FORMAT_RGB_FXT1:
693 return MESA_FORMAT_BGR_UNORM8;
694 case MESA_FORMAT_RGBA_FXT1:
695 return MESA_FORMAT_A8B8G8R8_UNORM;
696 case MESA_FORMAT_RGB_DXT1:
697 case MESA_FORMAT_SRGB_DXT1:
698 return MESA_FORMAT_BGR_UNORM8;
699 case MESA_FORMAT_RGBA_DXT1:
700 case MESA_FORMAT_SRGBA_DXT1:
701 return MESA_FORMAT_A8B8G8R8_UNORM;
702 case MESA_FORMAT_RGBA_DXT3:
703 case MESA_FORMAT_SRGBA_DXT3:
704 return MESA_FORMAT_A8B8G8R8_UNORM;
705 case MESA_FORMAT_RGBA_DXT5:
706 case MESA_FORMAT_SRGBA_DXT5:
707 return MESA_FORMAT_A8B8G8R8_UNORM;
708 case MESA_FORMAT_R_RGTC1_UNORM:
709 return MESA_FORMAT_R_UNORM8;
710 case MESA_FORMAT_R_RGTC1_SNORM:
711 return MESA_FORMAT_R_SNORM8;
712 case MESA_FORMAT_RG_RGTC2_UNORM:
713 return MESA_FORMAT_RG_UNORM8;
714 case MESA_FORMAT_RG_RGTC2_SNORM:
715 return MESA_FORMAT_RG_SNORM8;
716 case MESA_FORMAT_L_LATC1_UNORM:
717 return MESA_FORMAT_L_UNORM8;
718 case MESA_FORMAT_L_LATC1_SNORM:
719 return MESA_FORMAT_L_SNORM8;
720 case MESA_FORMAT_LA_LATC2_UNORM:
721 return MESA_FORMAT_LA_UNORM8;
722 case MESA_FORMAT_LA_LATC2_SNORM:
723 return MESA_FORMAT_LA_SNORM8;
724 case MESA_FORMAT_ETC1_RGB8:
725 case MESA_FORMAT_ETC2_RGB8:
726 case MESA_FORMAT_ETC2_SRGB8:
727 case MESA_FORMAT_ATC_RGB:
728 return MESA_FORMAT_BGR_UNORM8;
729 case MESA_FORMAT_ETC2_RGBA8_EAC:
730 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
731 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
732 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
733 case MESA_FORMAT_ATC_RGBA_EXPLICIT:
734 case MESA_FORMAT_ATC_RGBA_INTERPOLATED:
735 return MESA_FORMAT_A8B8G8R8_UNORM;
736 case MESA_FORMAT_ETC2_R11_EAC:
737 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
738 return MESA_FORMAT_R_UNORM16;
739 case MESA_FORMAT_ETC2_RG11_EAC:
740 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
741 return MESA_FORMAT_RG_UNORM16;
742 case MESA_FORMAT_BPTC_RGBA_UNORM:
743 case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
744 return MESA_FORMAT_A8B8G8R8_UNORM;
745 case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
746 case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
747 return MESA_FORMAT_RGB_FLOAT32;
748 default:
749 assert(!_mesa_is_format_compressed(format));
750 return format;
751 }
752 }
753
754
755 unsigned int
756 _mesa_format_num_components(mesa_format format)
757 {
758 const struct mesa_format_info *info = _mesa_get_format_info(format);
759 return ((info->RedBits > 0) +
760 (info->GreenBits > 0) +
761 (info->BlueBits > 0) +
762 (info->AlphaBits > 0) +
763 (info->LuminanceBits > 0) +
764 (info->IntensityBits > 0) +
765 (info->DepthBits > 0) +
766 (info->StencilBits > 0));
767 }
768
769
770 /**
771 * Returns true if a color format has data stored in the R/G/B/A channels,
772 * given an index from 0 to 3.
773 */
774 bool
775 _mesa_format_has_color_component(mesa_format format, int component)
776 {
777 const struct mesa_format_info *info = _mesa_get_format_info(format);
778
779 assert(info->BaseFormat != GL_DEPTH_COMPONENT &&
780 info->BaseFormat != GL_DEPTH_STENCIL &&
781 info->BaseFormat != GL_STENCIL_INDEX);
782
783 switch (component) {
784 case 0:
785 return (info->RedBits + info->IntensityBits + info->LuminanceBits) > 0;
786 case 1:
787 return (info->GreenBits + info->IntensityBits + info->LuminanceBits) > 0;
788 case 2:
789 return (info->BlueBits + info->IntensityBits + info->LuminanceBits) > 0;
790 case 3:
791 return (info->AlphaBits + info->IntensityBits) > 0;
792 default:
793 assert(!"Invalid color component: must be 0..3");
794 return false;
795 }
796 }
797
798
799 /**
800 * Return number of bytes needed to store an image of the given size
801 * in the given format.
802 */
803 uint32_t
804 _mesa_format_image_size(mesa_format format, int width,
805 int height, int depth)
806 {
807 const struct mesa_format_info *info = _mesa_get_format_info(format);
808 uint32_t sz;
809 /* Strictly speaking, a conditional isn't needed here */
810 if (info->BlockWidth > 1 || info->BlockHeight > 1 || info->BlockDepth > 1) {
811 /* compressed format (2D only for now) */
812 const uint32_t bw = info->BlockWidth;
813 const uint32_t bh = info->BlockHeight;
814 const uint32_t bd = info->BlockDepth;
815 const uint32_t wblocks = (width + bw - 1) / bw;
816 const uint32_t hblocks = (height + bh - 1) / bh;
817 const uint32_t dblocks = (depth + bd - 1) / bd;
818 sz = wblocks * hblocks * dblocks * info->BytesPerBlock;
819 } else
820 /* non-compressed */
821 sz = width * height * depth * info->BytesPerBlock;
822
823 return sz;
824 }
825
826
827 /**
828 * Same as _mesa_format_image_size() but returns a 64-bit value to
829 * accommodate very large textures.
830 */
831 uint64_t
832 _mesa_format_image_size64(mesa_format format, int width,
833 int height, int depth)
834 {
835 const struct mesa_format_info *info = _mesa_get_format_info(format);
836 uint64_t sz;
837 /* Strictly speaking, a conditional isn't needed here */
838 if (info->BlockWidth > 1 || info->BlockHeight > 1 || info->BlockDepth > 1) {
839 /* compressed format (2D only for now) */
840 const uint64_t bw = info->BlockWidth;
841 const uint64_t bh = info->BlockHeight;
842 const uint64_t bd = info->BlockDepth;
843 const uint64_t wblocks = (width + bw - 1) / bw;
844 const uint64_t hblocks = (height + bh - 1) / bh;
845 const uint64_t dblocks = (depth + bd - 1) / bd;
846 sz = wblocks * hblocks * dblocks * info->BytesPerBlock;
847 } else
848 /* non-compressed */
849 sz = ((uint64_t) width * (uint64_t) height *
850 (uint64_t) depth * info->BytesPerBlock);
851
852 return sz;
853 }
854
855
856
857 int32_t
858 _mesa_format_row_stride(mesa_format format, int width)
859 {
860 const struct mesa_format_info *info = _mesa_get_format_info(format);
861 /* Strictly speaking, a conditional isn't needed here */
862 if (info->BlockWidth > 1 || info->BlockHeight > 1) {
863 /* compressed format */
864 const uint32_t bw = info->BlockWidth;
865 const uint32_t wblocks = (width + bw - 1) / bw;
866 const int32_t stride = wblocks * info->BytesPerBlock;
867 return stride;
868 }
869 else {
870 const int32_t stride = width * info->BytesPerBlock;
871 return stride;
872 }
873 }
874
875
876
877 /**
878 * Return datatype and number of components per texel for the given
879 * uncompressed mesa_format. Only used for mipmap generation code.
880 */
881 void
882 _mesa_uncompressed_format_to_type_and_comps(mesa_format format,
883 GLenum *datatype, GLuint *comps)
884 {
885 switch (format) {
886 case MESA_FORMAT_A8B8G8R8_UNORM:
887 case MESA_FORMAT_R8G8B8A8_UNORM:
888 case MESA_FORMAT_B8G8R8A8_UNORM:
889 case MESA_FORMAT_A8R8G8B8_UNORM:
890 case MESA_FORMAT_X8B8G8R8_UNORM:
891 case MESA_FORMAT_R8G8B8X8_UNORM:
892 case MESA_FORMAT_B8G8R8X8_UNORM:
893 case MESA_FORMAT_X8R8G8B8_UNORM:
894 case MESA_FORMAT_A8B8G8R8_UINT:
895 case MESA_FORMAT_R8G8B8A8_UINT:
896 case MESA_FORMAT_B8G8R8A8_UINT:
897 case MESA_FORMAT_A8R8G8B8_UINT:
898 *datatype = GL_UNSIGNED_BYTE;
899 *comps = 4;
900 return;
901 case MESA_FORMAT_BGR_UNORM8:
902 case MESA_FORMAT_RGB_UNORM8:
903 *datatype = GL_UNSIGNED_BYTE;
904 *comps = 3;
905 return;
906 case MESA_FORMAT_B5G6R5_UNORM:
907 case MESA_FORMAT_R5G6B5_UNORM:
908 case MESA_FORMAT_B5G6R5_UINT:
909 case MESA_FORMAT_R5G6B5_UINT:
910 *datatype = GL_UNSIGNED_SHORT_5_6_5;
911 *comps = 3;
912 return;
913
914 case MESA_FORMAT_B4G4R4A4_UNORM:
915 case MESA_FORMAT_A4R4G4B4_UNORM:
916 case MESA_FORMAT_B4G4R4X4_UNORM:
917 case MESA_FORMAT_B4G4R4A4_UINT:
918 case MESA_FORMAT_A4R4G4B4_UINT:
919 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
920 *comps = 4;
921 return;
922
923 case MESA_FORMAT_B5G5R5A1_UNORM:
924 case MESA_FORMAT_A1R5G5B5_UNORM:
925 case MESA_FORMAT_B5G5R5X1_UNORM:
926 case MESA_FORMAT_B5G5R5A1_UINT:
927 case MESA_FORMAT_A1R5G5B5_UINT:
928 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
929 *comps = 4;
930 return;
931
932 case MESA_FORMAT_B10G10R10A2_UNORM:
933 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
934 *comps = 4;
935 return;
936
937 case MESA_FORMAT_A1B5G5R5_UNORM:
938 case MESA_FORMAT_A1B5G5R5_UINT:
939 case MESA_FORMAT_X1B5G5R5_UNORM:
940 *datatype = GL_UNSIGNED_SHORT_5_5_5_1;
941 *comps = 4;
942 return;
943
944 case MESA_FORMAT_L4A4_UNORM:
945 *datatype = MESA_UNSIGNED_BYTE_4_4;
946 *comps = 2;
947 return;
948
949 case MESA_FORMAT_LA_UNORM8:
950 case MESA_FORMAT_RG_UNORM8:
951 *datatype = GL_UNSIGNED_BYTE;
952 *comps = 2;
953 return;
954
955 case MESA_FORMAT_LA_UNORM16:
956 case MESA_FORMAT_RG_UNORM16:
957 *datatype = GL_UNSIGNED_SHORT;
958 *comps = 2;
959 return;
960
961 case MESA_FORMAT_R_UNORM16:
962 case MESA_FORMAT_A_UNORM16:
963 case MESA_FORMAT_L_UNORM16:
964 case MESA_FORMAT_I_UNORM16:
965 *datatype = GL_UNSIGNED_SHORT;
966 *comps = 1;
967 return;
968
969 case MESA_FORMAT_R3G3B2_UNORM:
970 case MESA_FORMAT_R3G3B2_UINT:
971 *datatype = GL_UNSIGNED_BYTE_2_3_3_REV;
972 *comps = 3;
973 return;
974 case MESA_FORMAT_A4B4G4R4_UNORM:
975 case MESA_FORMAT_A4B4G4R4_UINT:
976 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
977 *comps = 4;
978 return;
979
980 case MESA_FORMAT_R4G4B4A4_UNORM:
981 case MESA_FORMAT_R4G4B4A4_UINT:
982 *datatype = GL_UNSIGNED_SHORT_4_4_4_4;
983 *comps = 4;
984 return;
985 case MESA_FORMAT_R5G5B5A1_UNORM:
986 case MESA_FORMAT_R5G5B5A1_UINT:
987 *datatype = GL_UNSIGNED_SHORT_1_5_5_5_REV;
988 *comps = 4;
989 return;
990 case MESA_FORMAT_A2B10G10R10_UNORM:
991 case MESA_FORMAT_A2B10G10R10_UINT:
992 *datatype = GL_UNSIGNED_INT_10_10_10_2;
993 *comps = 4;
994 return;
995 case MESA_FORMAT_A2R10G10B10_UNORM:
996 case MESA_FORMAT_A2R10G10B10_UINT:
997 *datatype = GL_UNSIGNED_INT_10_10_10_2;
998 *comps = 4;
999 return;
1000
1001 case MESA_FORMAT_B2G3R3_UNORM:
1002 case MESA_FORMAT_B2G3R3_UINT:
1003 *datatype = GL_UNSIGNED_BYTE_3_3_2;
1004 *comps = 3;
1005 return;
1006
1007 case MESA_FORMAT_A_UNORM8:
1008 case MESA_FORMAT_L_UNORM8:
1009 case MESA_FORMAT_I_UNORM8:
1010 case MESA_FORMAT_R_UNORM8:
1011 case MESA_FORMAT_S_UINT8:
1012 *datatype = GL_UNSIGNED_BYTE;
1013 *comps = 1;
1014 return;
1015
1016 case MESA_FORMAT_YCBCR:
1017 case MESA_FORMAT_YCBCR_REV:
1018 *datatype = GL_UNSIGNED_SHORT;
1019 *comps = 2;
1020 return;
1021
1022 case MESA_FORMAT_S8_UINT_Z24_UNORM:
1023 *datatype = GL_UNSIGNED_INT_24_8_MESA;
1024 *comps = 2;
1025 return;
1026
1027 case MESA_FORMAT_Z24_UNORM_S8_UINT:
1028 *datatype = GL_UNSIGNED_INT_8_24_REV_MESA;
1029 *comps = 2;
1030 return;
1031
1032 case MESA_FORMAT_Z_UNORM16:
1033 *datatype = GL_UNSIGNED_SHORT;
1034 *comps = 1;
1035 return;
1036
1037 case MESA_FORMAT_Z24_UNORM_X8_UINT:
1038 *datatype = GL_UNSIGNED_INT;
1039 *comps = 1;
1040 return;
1041
1042 case MESA_FORMAT_X8_UINT_Z24_UNORM:
1043 *datatype = GL_UNSIGNED_INT;
1044 *comps = 1;
1045 return;
1046
1047 case MESA_FORMAT_Z_UNORM32:
1048 *datatype = GL_UNSIGNED_INT;
1049 *comps = 1;
1050 return;
1051
1052 case MESA_FORMAT_Z_FLOAT32:
1053 *datatype = GL_FLOAT;
1054 *comps = 1;
1055 return;
1056
1057 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1058 *datatype = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
1059 *comps = 1;
1060 return;
1061
1062 case MESA_FORMAT_R_SNORM8:
1063 case MESA_FORMAT_A_SNORM8:
1064 case MESA_FORMAT_L_SNORM8:
1065 case MESA_FORMAT_I_SNORM8:
1066 *datatype = GL_BYTE;
1067 *comps = 1;
1068 return;
1069 case MESA_FORMAT_RG_SNORM8:
1070 case MESA_FORMAT_LA_SNORM8:
1071 *datatype = GL_BYTE;
1072 *comps = 2;
1073 return;
1074 case MESA_FORMAT_A8B8G8R8_SNORM:
1075 case MESA_FORMAT_R8G8B8A8_SNORM:
1076 case MESA_FORMAT_X8B8G8R8_SNORM:
1077 *datatype = GL_BYTE;
1078 *comps = 4;
1079 return;
1080
1081 case MESA_FORMAT_RGBA_UNORM16:
1082 *datatype = GL_UNSIGNED_SHORT;
1083 *comps = 4;
1084 return;
1085
1086 case MESA_FORMAT_R_SNORM16:
1087 case MESA_FORMAT_A_SNORM16:
1088 case MESA_FORMAT_L_SNORM16:
1089 case MESA_FORMAT_I_SNORM16:
1090 *datatype = GL_SHORT;
1091 *comps = 1;
1092 return;
1093 case MESA_FORMAT_RG_SNORM16:
1094 case MESA_FORMAT_LA_SNORM16:
1095 *datatype = GL_SHORT;
1096 *comps = 2;
1097 return;
1098 case MESA_FORMAT_RGB_SNORM16:
1099 *datatype = GL_SHORT;
1100 *comps = 3;
1101 return;
1102 case MESA_FORMAT_RGBA_SNORM16:
1103 *datatype = GL_SHORT;
1104 *comps = 4;
1105 return;
1106
1107 case MESA_FORMAT_BGR_SRGB8:
1108 *datatype = GL_UNSIGNED_BYTE;
1109 *comps = 3;
1110 return;
1111 case MESA_FORMAT_A8B8G8R8_SRGB:
1112 case MESA_FORMAT_B8G8R8A8_SRGB:
1113 case MESA_FORMAT_A8R8G8B8_SRGB:
1114 case MESA_FORMAT_R8G8B8A8_SRGB:
1115 *datatype = GL_UNSIGNED_BYTE;
1116 *comps = 4;
1117 return;
1118 case MESA_FORMAT_L_SRGB8:
1119 case MESA_FORMAT_R_SRGB8:
1120 *datatype = GL_UNSIGNED_BYTE;
1121 *comps = 1;
1122 return;
1123 case MESA_FORMAT_LA_SRGB8:
1124 *datatype = GL_UNSIGNED_BYTE;
1125 *comps = 2;
1126 return;
1127
1128 case MESA_FORMAT_RGBA_FLOAT32:
1129 *datatype = GL_FLOAT;
1130 *comps = 4;
1131 return;
1132 case MESA_FORMAT_RGBA_FLOAT16:
1133 *datatype = GL_HALF_FLOAT_ARB;
1134 *comps = 4;
1135 return;
1136 case MESA_FORMAT_RGB_FLOAT32:
1137 *datatype = GL_FLOAT;
1138 *comps = 3;
1139 return;
1140 case MESA_FORMAT_RGB_FLOAT16:
1141 *datatype = GL_HALF_FLOAT_ARB;
1142 *comps = 3;
1143 return;
1144 case MESA_FORMAT_LA_FLOAT32:
1145 case MESA_FORMAT_RG_FLOAT32:
1146 *datatype = GL_FLOAT;
1147 *comps = 2;
1148 return;
1149 case MESA_FORMAT_LA_FLOAT16:
1150 case MESA_FORMAT_RG_FLOAT16:
1151 *datatype = GL_HALF_FLOAT_ARB;
1152 *comps = 2;
1153 return;
1154 case MESA_FORMAT_A_FLOAT32:
1155 case MESA_FORMAT_L_FLOAT32:
1156 case MESA_FORMAT_I_FLOAT32:
1157 case MESA_FORMAT_R_FLOAT32:
1158 *datatype = GL_FLOAT;
1159 *comps = 1;
1160 return;
1161 case MESA_FORMAT_A_FLOAT16:
1162 case MESA_FORMAT_L_FLOAT16:
1163 case MESA_FORMAT_I_FLOAT16:
1164 case MESA_FORMAT_R_FLOAT16:
1165 *datatype = GL_HALF_FLOAT_ARB;
1166 *comps = 1;
1167 return;
1168
1169 case MESA_FORMAT_A_UINT8:
1170 case MESA_FORMAT_L_UINT8:
1171 case MESA_FORMAT_I_UINT8:
1172 *datatype = GL_UNSIGNED_BYTE;
1173 *comps = 1;
1174 return;
1175 case MESA_FORMAT_LA_UINT8:
1176 *datatype = GL_UNSIGNED_BYTE;
1177 *comps = 2;
1178 return;
1179
1180 case MESA_FORMAT_A_UINT16:
1181 case MESA_FORMAT_L_UINT16:
1182 case MESA_FORMAT_I_UINT16:
1183 *datatype = GL_UNSIGNED_SHORT;
1184 *comps = 1;
1185 return;
1186 case MESA_FORMAT_LA_UINT16:
1187 *datatype = GL_UNSIGNED_SHORT;
1188 *comps = 2;
1189 return;
1190 case MESA_FORMAT_A_UINT32:
1191 case MESA_FORMAT_L_UINT32:
1192 case MESA_FORMAT_I_UINT32:
1193 *datatype = GL_UNSIGNED_INT;
1194 *comps = 1;
1195 return;
1196 case MESA_FORMAT_LA_UINT32:
1197 *datatype = GL_UNSIGNED_INT;
1198 *comps = 2;
1199 return;
1200 case MESA_FORMAT_A_SINT8:
1201 case MESA_FORMAT_L_SINT8:
1202 case MESA_FORMAT_I_SINT8:
1203 *datatype = GL_BYTE;
1204 *comps = 1;
1205 return;
1206 case MESA_FORMAT_LA_SINT8:
1207 *datatype = GL_BYTE;
1208 *comps = 2;
1209 return;
1210
1211 case MESA_FORMAT_A_SINT16:
1212 case MESA_FORMAT_L_SINT16:
1213 case MESA_FORMAT_I_SINT16:
1214 *datatype = GL_SHORT;
1215 *comps = 1;
1216 return;
1217 case MESA_FORMAT_LA_SINT16:
1218 *datatype = GL_SHORT;
1219 *comps = 2;
1220 return;
1221
1222 case MESA_FORMAT_A_SINT32:
1223 case MESA_FORMAT_L_SINT32:
1224 case MESA_FORMAT_I_SINT32:
1225 *datatype = GL_INT;
1226 *comps = 1;
1227 return;
1228 case MESA_FORMAT_LA_SINT32:
1229 *datatype = GL_INT;
1230 *comps = 2;
1231 return;
1232
1233 case MESA_FORMAT_R_SINT8:
1234 *datatype = GL_BYTE;
1235 *comps = 1;
1236 return;
1237 case MESA_FORMAT_RG_SINT8:
1238 *datatype = GL_BYTE;
1239 *comps = 2;
1240 return;
1241 case MESA_FORMAT_RGB_SINT8:
1242 *datatype = GL_BYTE;
1243 *comps = 3;
1244 return;
1245 case MESA_FORMAT_RGBA_SINT8:
1246 *datatype = GL_BYTE;
1247 *comps = 4;
1248 return;
1249 case MESA_FORMAT_R_SINT16:
1250 *datatype = GL_SHORT;
1251 *comps = 1;
1252 return;
1253 case MESA_FORMAT_RG_SINT16:
1254 *datatype = GL_SHORT;
1255 *comps = 2;
1256 return;
1257 case MESA_FORMAT_RGB_SINT16:
1258 *datatype = GL_SHORT;
1259 *comps = 3;
1260 return;
1261 case MESA_FORMAT_RGBA_SINT16:
1262 *datatype = GL_SHORT;
1263 *comps = 4;
1264 return;
1265 case MESA_FORMAT_R_SINT32:
1266 *datatype = GL_INT;
1267 *comps = 1;
1268 return;
1269 case MESA_FORMAT_RG_SINT32:
1270 *datatype = GL_INT;
1271 *comps = 2;
1272 return;
1273 case MESA_FORMAT_RGB_SINT32:
1274 *datatype = GL_INT;
1275 *comps = 3;
1276 return;
1277 case MESA_FORMAT_RGBA_SINT32:
1278 *datatype = GL_INT;
1279 *comps = 4;
1280 return;
1281
1282 /**
1283 * \name Non-normalized unsigned integer formats.
1284 */
1285 case MESA_FORMAT_R_UINT8:
1286 *datatype = GL_UNSIGNED_BYTE;
1287 *comps = 1;
1288 return;
1289 case MESA_FORMAT_RG_UINT8:
1290 *datatype = GL_UNSIGNED_BYTE;
1291 *comps = 2;
1292 return;
1293 case MESA_FORMAT_RGB_UINT8:
1294 *datatype = GL_UNSIGNED_BYTE;
1295 *comps = 3;
1296 return;
1297 case MESA_FORMAT_RGBA_UINT8:
1298 *datatype = GL_UNSIGNED_BYTE;
1299 *comps = 4;
1300 return;
1301 case MESA_FORMAT_R_UINT16:
1302 *datatype = GL_UNSIGNED_SHORT;
1303 *comps = 1;
1304 return;
1305 case MESA_FORMAT_RG_UINT16:
1306 *datatype = GL_UNSIGNED_SHORT;
1307 *comps = 2;
1308 return;
1309 case MESA_FORMAT_RGB_UINT16:
1310 *datatype = GL_UNSIGNED_SHORT;
1311 *comps = 3;
1312 return;
1313 case MESA_FORMAT_RGBA_UINT16:
1314 *datatype = GL_UNSIGNED_SHORT;
1315 *comps = 4;
1316 return;
1317 case MESA_FORMAT_R_UINT32:
1318 *datatype = GL_UNSIGNED_INT;
1319 *comps = 1;
1320 return;
1321 case MESA_FORMAT_RG_UINT32:
1322 *datatype = GL_UNSIGNED_INT;
1323 *comps = 2;
1324 return;
1325 case MESA_FORMAT_RGB_UINT32:
1326 *datatype = GL_UNSIGNED_INT;
1327 *comps = 3;
1328 return;
1329 case MESA_FORMAT_RGBA_UINT32:
1330 *datatype = GL_UNSIGNED_INT;
1331 *comps = 4;
1332 return;
1333
1334 case MESA_FORMAT_R9G9B9E5_FLOAT:
1335 *datatype = GL_UNSIGNED_INT_5_9_9_9_REV;
1336 *comps = 3;
1337 return;
1338
1339 case MESA_FORMAT_R11G11B10_FLOAT:
1340 *datatype = GL_UNSIGNED_INT_10F_11F_11F_REV;
1341 *comps = 3;
1342 return;
1343
1344 case MESA_FORMAT_B10G10R10A2_UINT:
1345 case MESA_FORMAT_R10G10B10A2_UINT:
1346 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1347 *comps = 4;
1348 return;
1349
1350 case MESA_FORMAT_R8G8B8X8_SRGB:
1351 case MESA_FORMAT_X8B8G8R8_SRGB:
1352 case MESA_FORMAT_RGBX_UINT8:
1353 *datatype = GL_UNSIGNED_BYTE;
1354 *comps = 4;
1355 return;
1356
1357 case MESA_FORMAT_R8G8B8X8_SNORM:
1358 case MESA_FORMAT_RGBX_SINT8:
1359 *datatype = GL_BYTE;
1360 *comps = 4;
1361 return;
1362
1363 case MESA_FORMAT_B10G10R10X2_UNORM:
1364 case MESA_FORMAT_R10G10B10X2_UNORM:
1365 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1366 *comps = 4;
1367 return;
1368
1369 case MESA_FORMAT_RGBX_UNORM16:
1370 case MESA_FORMAT_RGBX_UINT16:
1371 *datatype = GL_UNSIGNED_SHORT;
1372 *comps = 4;
1373 return;
1374
1375 case MESA_FORMAT_RGBX_SNORM16:
1376 case MESA_FORMAT_RGBX_SINT16:
1377 *datatype = GL_SHORT;
1378 *comps = 4;
1379 return;
1380
1381 case MESA_FORMAT_RGBX_FLOAT16:
1382 *datatype = GL_HALF_FLOAT;
1383 *comps = 4;
1384 return;
1385
1386 case MESA_FORMAT_RGBX_FLOAT32:
1387 *datatype = GL_FLOAT;
1388 *comps = 4;
1389 return;
1390
1391 case MESA_FORMAT_RGBX_UINT32:
1392 *datatype = GL_UNSIGNED_INT;
1393 *comps = 4;
1394 return;
1395
1396 case MESA_FORMAT_RGBX_SINT32:
1397 *datatype = GL_INT;
1398 *comps = 4;
1399 return;
1400
1401 case MESA_FORMAT_R10G10B10A2_UNORM:
1402 *datatype = GL_UNSIGNED_INT_2_10_10_10_REV;
1403 *comps = 4;
1404 return;
1405
1406 case MESA_FORMAT_B8G8R8X8_SRGB:
1407 case MESA_FORMAT_X8R8G8B8_SRGB:
1408 *datatype = GL_UNSIGNED_BYTE;
1409 *comps = 4;
1410 return;
1411
1412 case MESA_FORMAT_COUNT:
1413 assert(0);
1414 return;
1415 default:
1416 /* Warn if any formats are not handled */
1417 _mesa_problem(NULL, "bad format %s in _mesa_uncompressed_format_to_type_and_comps",
1418 _mesa_get_format_name(format));
1419 assert(format == MESA_FORMAT_NONE ||
1420 _mesa_is_format_compressed(format));
1421 *datatype = 0;
1422 *comps = 1;
1423 }
1424 }
1425
1426 /**
1427 * Check if a mesa_format exactly matches a GL format/type combination
1428 * such that we can use memcpy() from one to the other.
1429 * \param mesa_format a MESA_FORMAT_x value
1430 * \param format the user-specified image format
1431 * \param type the user-specified image datatype
1432 * \param swapBytes typically the current pixel pack/unpack byteswap state
1433 * \param[out] error GL_NO_ERROR if format is an expected input.
1434 * GL_INVALID_ENUM if format is an unexpected input.
1435 * \return true if the formats match, false otherwise.
1436 */
1437 bool
1438 _mesa_format_matches_format_and_type(mesa_format mformat,
1439 GLenum format, GLenum type,
1440 bool swapBytes, GLenum *error)
1441 {
1442 if (error)
1443 *error = GL_NO_ERROR;
1444
1445 if (_mesa_is_format_compressed(mformat)) {
1446 if (error)
1447 *error = GL_INVALID_ENUM;
1448 return false;
1449 }
1450
1451 if (swapBytes && !_mesa_swap_bytes_in_type_enum(&type))
1452 return false;
1453
1454 /* format/type don't include srgb and should match regardless of it. */
1455 mformat = _mesa_get_srgb_format_linear(mformat);
1456
1457 /* intensity formats are uploaded with GL_RED, and we want to find
1458 * memcpy matches for them.
1459 */
1460 mformat = _mesa_get_intensity_format_red(mformat);
1461
1462 if (format == GL_COLOR_INDEX)
1463 return false;
1464
1465 mesa_format other_format = _mesa_format_from_format_and_type(format, type);
1466 if (_mesa_format_is_mesa_array_format(other_format))
1467 other_format = _mesa_format_from_array_format(other_format);
1468
1469 return other_format == mformat;
1470 }
1471