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