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