1 #include "precompiled.h" 2 // 3 // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 // 7 8 // utilities.cpp: Conversion functions and other utility routines. 9 10 #include "libGLESv2/utilities.h" 11 #include "libGLESv2/mathutil.h" 12 13 namespace gl 14 { 15 16 int UniformComponentCount(GLenum type) 17 { 18 switch (type) 19 { 20 case GL_BOOL: 21 case GL_FLOAT: 22 case GL_INT: 23 case GL_SAMPLER_2D: 24 case GL_SAMPLER_CUBE: 25 return 1; 26 case GL_BOOL_VEC2: 27 case GL_FLOAT_VEC2: 28 case GL_INT_VEC2: 29 return 2; 30 case GL_INT_VEC3: 31 case GL_FLOAT_VEC3: 32 case GL_BOOL_VEC3: 33 return 3; 34 case GL_BOOL_VEC4: 35 case GL_FLOAT_VEC4: 36 case GL_INT_VEC4: 37 case GL_FLOAT_MAT2: 38 return 4; 39 case GL_FLOAT_MAT3: 40 return 9; 41 case GL_FLOAT_MAT4: 42 return 16; 43 default: 44 UNREACHABLE(); 45 } 46 47 return 0; 48 } 49 50 GLenum UniformComponentType(GLenum type) 51 { 52 switch(type) 53 { 54 case GL_BOOL: 55 case GL_BOOL_VEC2: 56 case GL_BOOL_VEC3: 57 case GL_BOOL_VEC4: 58 return GL_BOOL; 59 case GL_FLOAT: 60 case GL_FLOAT_VEC2: 61 case GL_FLOAT_VEC3: 62 case GL_FLOAT_VEC4: 63 case GL_FLOAT_MAT2: 64 case GL_FLOAT_MAT3: 65 case GL_FLOAT_MAT4: 66 return GL_FLOAT; 67 case GL_INT: 68 case GL_SAMPLER_2D: 69 case GL_SAMPLER_CUBE: 70 case GL_INT_VEC2: 71 case GL_INT_VEC3: 72 case GL_INT_VEC4: 73 return GL_INT; 74 default: 75 UNREACHABLE(); 76 } 77 78 return GL_NONE; 79 } 80 81 size_t UniformComponentSize(GLenum type) 82 { 83 switch(type) 84 { 85 case GL_BOOL: return sizeof(GLint); 86 case GL_FLOAT: return sizeof(GLfloat); 87 case GL_INT: return sizeof(GLint); 88 default: UNREACHABLE(); 89 } 90 91 return 0; 92 } 93 94 size_t UniformInternalSize(GLenum type) 95 { 96 // Expanded to 4-element vectors 97 return UniformComponentSize(UniformComponentType(type)) * VariableRowCount(type) * 4; 98 } 99 100 size_t UniformExternalSize(GLenum type) 101 { 102 return UniformComponentSize(UniformComponentType(type)) * UniformComponentCount(type); 103 } 104 105 int VariableRowCount(GLenum type) 106 { 107 switch (type) 108 { 109 case GL_NONE: 110 return 0; 111 case GL_BOOL: 112 case GL_FLOAT: 113 case GL_INT: 114 case GL_BOOL_VEC2: 115 case GL_FLOAT_VEC2: 116 case GL_INT_VEC2: 117 case GL_INT_VEC3: 118 case GL_FLOAT_VEC3: 119 case GL_BOOL_VEC3: 120 case GL_BOOL_VEC4: 121 case GL_FLOAT_VEC4: 122 case GL_INT_VEC4: 123 case GL_SAMPLER_2D: 124 case GL_SAMPLER_CUBE: 125 return 1; 126 case GL_FLOAT_MAT2: 127 return 2; 128 case GL_FLOAT_MAT3: 129 return 3; 130 case GL_FLOAT_MAT4: 131 return 4; 132 default: 133 UNREACHABLE(); 134 } 135 136 return 0; 137 } 138 139 int VariableColumnCount(GLenum type) 140 { 141 switch (type) 142 { 143 case GL_NONE: 144 return 0; 145 case GL_BOOL: 146 case GL_FLOAT: 147 case GL_INT: 148 case GL_SAMPLER_2D: 149 case GL_SAMPLER_CUBE: 150 return 1; 151 case GL_BOOL_VEC2: 152 case GL_FLOAT_VEC2: 153 case GL_INT_VEC2: 154 case GL_FLOAT_MAT2: 155 return 2; 156 case GL_INT_VEC3: 157 case GL_FLOAT_VEC3: 158 case GL_BOOL_VEC3: 159 case GL_FLOAT_MAT3: 160 return 3; 161 case GL_BOOL_VEC4: 162 case GL_FLOAT_VEC4: 163 case GL_INT_VEC4: 164 case GL_FLOAT_MAT4: 165 return 4; 166 default: 167 UNREACHABLE(); 168 } 169 170 return 0; 171 } 172 173 int AllocateFirstFreeBits(unsigned int *bits, unsigned int allocationSize, unsigned int bitsSize) 174 { 175 ASSERT(allocationSize <= bitsSize); 176 177 unsigned int mask = std::numeric_limits<unsigned int>::max() >> (std::numeric_limits<unsigned int>::digits - allocationSize); 178 179 for (unsigned int i = 0; i < bitsSize - allocationSize + 1; i++) 180 { 181 if ((*bits & mask) == 0) 182 { 183 *bits |= mask; 184 return i; 185 } 186 187 mask <<= 1; 188 } 189 190 return -1; 191 } 192 193 GLsizei ComputePitch(GLsizei width, GLint internalformat, GLint alignment) 194 { 195 ASSERT(alignment > 0 && isPow2(alignment)); 196 197 GLsizei rawPitch = ComputePixelSize(internalformat) * width; 198 return (rawPitch + alignment - 1) & ~(alignment - 1); 199 } 200 201 GLsizei ComputeCompressedPitch(GLsizei width, GLenum internalformat) 202 { 203 return ComputeCompressedSize(width, 1, internalformat); 204 } 205 206 GLsizei ComputeCompressedSize(GLsizei width, GLsizei height, GLenum internalformat) 207 { 208 switch (internalformat) 209 { 210 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: 211 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: 212 return 8 * ((width + 3) / 4) * ((height + 3) / 4); 213 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: 214 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: 215 return 16 * ((width + 3) / 4) * ((height + 3) / 4); 216 default: 217 return 0; 218 } 219 } 220 221 GLsizei ComputeTypeSize(GLenum type) 222 { 223 switch (type) 224 { 225 case GL_BYTE: return 1; 226 case GL_UNSIGNED_BYTE: return 1; 227 case GL_SHORT: return 2; 228 case GL_UNSIGNED_SHORT: return 2; 229 case GL_INT: return 4; 230 case GL_UNSIGNED_INT: return 4; 231 case GL_FLOAT: return 4; 232 case GL_HALF_FLOAT_OES: return 2; 233 case GL_UNSIGNED_SHORT_5_6_5: return 2; 234 case GL_UNSIGNED_SHORT_4_4_4_4: return 2; 235 case GL_UNSIGNED_SHORT_5_5_5_1: return 2; 236 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: return 2; 237 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: return 2; 238 case GL_UNSIGNED_INT_2_10_10_10_REV_EXT: return 4; 239 case GL_UNSIGNED_INT_24_8_OES: return 4; 240 default: UNREACHABLE(); return 0; 241 } 242 } 243 244 bool IsCompressed(GLenum format) 245 { 246 if(format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT || 247 format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || 248 format == GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE || 249 format == GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE) 250 { 251 return true; 252 } 253 else 254 { 255 return false; 256 } 257 } 258 259 bool IsDepthTexture(GLenum format) 260 { 261 if (format == GL_DEPTH_COMPONENT || 262 format == GL_DEPTH_STENCIL_OES || 263 format == GL_DEPTH_COMPONENT16 || 264 format == GL_DEPTH_COMPONENT32_OES || 265 format == GL_DEPTH24_STENCIL8_OES) 266 { 267 return true; 268 } 269 270 return false; 271 } 272 273 bool IsStencilTexture(GLenum format) 274 { 275 if (format == GL_DEPTH_STENCIL_OES || 276 format == GL_DEPTH24_STENCIL8_OES) 277 { 278 return true; 279 } 280 281 return false; 282 } 283 284 void MakeValidSize(bool isImage, bool isCompressed, GLsizei *requestWidth, GLsizei *requestHeight, int *levelOffset) 285 { 286 int upsampleCount = 0; 287 288 if (isCompressed) 289 { 290 // Don't expand the size of full textures that are at least 4x4 291 // already. 292 if (isImage || *requestWidth < 4 || *requestHeight < 4) 293 { 294 while (*requestWidth % 4 != 0 || *requestHeight % 4 != 0) 295 { 296 *requestWidth <<= 1; 297 *requestHeight <<= 1; 298 upsampleCount++; 299 } 300 } 301 } 302 *levelOffset = upsampleCount; 303 } 304 305 // Returns the size, in bytes, of a single texel in an Image 306 int ComputePixelSize(GLint internalformat) 307 { 308 switch (internalformat) 309 { 310 case GL_ALPHA8_EXT: return sizeof(unsigned char); 311 case GL_LUMINANCE8_EXT: return sizeof(unsigned char); 312 case GL_ALPHA32F_EXT: return sizeof(float); 313 case GL_LUMINANCE32F_EXT: return sizeof(float); 314 case GL_ALPHA16F_EXT: return sizeof(unsigned short); 315 case GL_LUMINANCE16F_EXT: return sizeof(unsigned short); 316 case GL_LUMINANCE8_ALPHA8_EXT: return sizeof(unsigned char) * 2; 317 case GL_LUMINANCE_ALPHA32F_EXT: return sizeof(float) * 2; 318 case GL_LUMINANCE_ALPHA16F_EXT: return sizeof(unsigned short) * 2; 319 case GL_RGB8_OES: return sizeof(unsigned char) * 3; 320 case GL_RGB565: return sizeof(unsigned short); 321 case GL_RGB32F_EXT: return sizeof(float) * 3; 322 case GL_RGB16F_EXT: return sizeof(unsigned short) * 3; 323 case GL_RGBA8_OES: return sizeof(unsigned char) * 4; 324 case GL_RGBA4: return sizeof(unsigned short); 325 case GL_RGB5_A1: return sizeof(unsigned short); 326 case GL_RGBA32F_EXT: return sizeof(float) * 4; 327 case GL_RGBA16F_EXT: return sizeof(unsigned short) * 4; 328 case GL_BGRA8_EXT: return sizeof(unsigned char) * 4; 329 case GL_BGRA4_ANGLEX: return sizeof(unsigned short); 330 case GL_BGR5_A1_ANGLEX: return sizeof(unsigned short); 331 default: UNREACHABLE(); 332 } 333 334 return 0; 335 } 336 337 bool IsCubemapTextureTarget(GLenum target) 338 { 339 return (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z); 340 } 341 342 bool IsInternalTextureTarget(GLenum target) 343 { 344 return target == GL_TEXTURE_2D || IsCubemapTextureTarget(target); 345 } 346 347 GLint ConvertSizedInternalFormat(GLenum format, GLenum type) 348 { 349 switch (format) 350 { 351 case GL_ALPHA: 352 switch (type) 353 { 354 case GL_UNSIGNED_BYTE: return GL_ALPHA8_EXT; 355 case GL_FLOAT: return GL_ALPHA32F_EXT; 356 case GL_HALF_FLOAT_OES: return GL_ALPHA16F_EXT; 357 default: UNIMPLEMENTED(); 358 } 359 break; 360 case GL_LUMINANCE: 361 switch (type) 362 { 363 case GL_UNSIGNED_BYTE: return GL_LUMINANCE8_EXT; 364 case GL_FLOAT: return GL_LUMINANCE32F_EXT; 365 case GL_HALF_FLOAT_OES: return GL_LUMINANCE16F_EXT; 366 default: UNIMPLEMENTED(); 367 } 368 break; 369 case GL_LUMINANCE_ALPHA: 370 switch (type) 371 { 372 case GL_UNSIGNED_BYTE: return GL_LUMINANCE8_ALPHA8_EXT; 373 case GL_FLOAT: return GL_LUMINANCE_ALPHA32F_EXT; 374 case GL_HALF_FLOAT_OES: return GL_LUMINANCE_ALPHA16F_EXT; 375 default: UNIMPLEMENTED(); 376 } 377 break; 378 case GL_RGB: 379 switch (type) 380 { 381 case GL_UNSIGNED_BYTE: return GL_RGB8_OES; 382 case GL_UNSIGNED_SHORT_5_6_5: return GL_RGB565; 383 case GL_FLOAT: return GL_RGB32F_EXT; 384 case GL_HALF_FLOAT_OES: return GL_RGB16F_EXT; 385 default: UNIMPLEMENTED(); 386 } 387 break; 388 case GL_RGBA: 389 switch (type) 390 { 391 case GL_UNSIGNED_BYTE: return GL_RGBA8_OES; 392 case GL_UNSIGNED_SHORT_4_4_4_4: return GL_RGBA4; 393 case GL_UNSIGNED_SHORT_5_5_5_1: return GL_RGB5_A1; 394 case GL_FLOAT: return GL_RGBA32F_EXT; 395 case GL_HALF_FLOAT_OES: return GL_RGBA16F_EXT; 396 break; 397 default: UNIMPLEMENTED(); 398 } 399 break; 400 case GL_BGRA_EXT: 401 switch (type) 402 { 403 case GL_UNSIGNED_BYTE: return GL_BGRA8_EXT; 404 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: return GL_BGRA4_ANGLEX; 405 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: return GL_BGR5_A1_ANGLEX; 406 default: UNIMPLEMENTED(); 407 } 408 break; 409 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: 410 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: 411 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: 412 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: 413 return format; 414 case GL_DEPTH_COMPONENT: 415 switch (type) 416 { 417 case GL_UNSIGNED_SHORT: return GL_DEPTH_COMPONENT16; 418 case GL_UNSIGNED_INT: return GL_DEPTH_COMPONENT32_OES; 419 default: UNIMPLEMENTED(); 420 } 421 break; 422 case GL_DEPTH_STENCIL_OES: 423 switch (type) 424 { 425 case GL_UNSIGNED_INT_24_8_OES: return GL_DEPTH24_STENCIL8_OES; 426 default: UNIMPLEMENTED(); 427 } 428 break; 429 default: 430 UNIMPLEMENTED(); 431 } 432 433 return GL_NONE; 434 } 435 436 GLenum ExtractFormat(GLenum internalformat) 437 { 438 switch (internalformat) 439 { 440 case GL_RGB565: return GL_RGB; 441 case GL_RGBA4: return GL_RGBA; 442 case GL_RGB5_A1: return GL_RGBA; 443 case GL_RGB8_OES: return GL_RGB; 444 case GL_RGBA8_OES: return GL_RGBA; 445 case GL_LUMINANCE8_ALPHA8_EXT: return GL_LUMINANCE_ALPHA; 446 case GL_LUMINANCE8_EXT: return GL_LUMINANCE; 447 case GL_ALPHA8_EXT: return GL_ALPHA; 448 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return GL_COMPRESSED_RGB_S3TC_DXT1_EXT; 449 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; 450 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: return GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE; 451 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: return GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE; 452 case GL_RGBA32F_EXT: return GL_RGBA; 453 case GL_RGB32F_EXT: return GL_RGB; 454 case GL_ALPHA32F_EXT: return GL_ALPHA; 455 case GL_LUMINANCE32F_EXT: return GL_LUMINANCE; 456 case GL_LUMINANCE_ALPHA32F_EXT: return GL_LUMINANCE_ALPHA; 457 case GL_RGBA16F_EXT: return GL_RGBA; 458 case GL_RGB16F_EXT: return GL_RGB; 459 case GL_ALPHA16F_EXT: return GL_ALPHA; 460 case GL_LUMINANCE16F_EXT: return GL_LUMINANCE; 461 case GL_LUMINANCE_ALPHA16F_EXT: return GL_LUMINANCE_ALPHA; 462 case GL_BGRA8_EXT: return GL_BGRA_EXT; 463 case GL_DEPTH_COMPONENT16: return GL_DEPTH_COMPONENT; 464 case GL_DEPTH_COMPONENT32_OES: return GL_DEPTH_COMPONENT; 465 case GL_DEPTH24_STENCIL8_OES: return GL_DEPTH_STENCIL_OES; 466 default: return GL_NONE; // Unsupported 467 } 468 } 469 470 GLenum ExtractType(GLenum internalformat) 471 { 472 switch (internalformat) 473 { 474 case GL_RGB565: return GL_UNSIGNED_SHORT_5_6_5; 475 case GL_RGBA4: return GL_UNSIGNED_SHORT_4_4_4_4; 476 case GL_RGB5_A1: return GL_UNSIGNED_SHORT_5_5_5_1; 477 case GL_RGB8_OES: return GL_UNSIGNED_BYTE; 478 case GL_RGBA8_OES: return GL_UNSIGNED_BYTE; 479 case GL_LUMINANCE8_ALPHA8_EXT: return GL_UNSIGNED_BYTE; 480 case GL_LUMINANCE8_EXT: return GL_UNSIGNED_BYTE; 481 case GL_ALPHA8_EXT: return GL_UNSIGNED_BYTE; 482 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: return GL_UNSIGNED_BYTE; 483 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return GL_UNSIGNED_BYTE; 484 case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: return GL_UNSIGNED_BYTE; 485 case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: return GL_UNSIGNED_BYTE; 486 case GL_RGBA32F_EXT: return GL_FLOAT; 487 case GL_RGB32F_EXT: return GL_FLOAT; 488 case GL_ALPHA32F_EXT: return GL_FLOAT; 489 case GL_LUMINANCE32F_EXT: return GL_FLOAT; 490 case GL_LUMINANCE_ALPHA32F_EXT: return GL_FLOAT; 491 case GL_RGBA16F_EXT: return GL_HALF_FLOAT_OES; 492 case GL_RGB16F_EXT: return GL_HALF_FLOAT_OES; 493 case GL_ALPHA16F_EXT: return GL_HALF_FLOAT_OES; 494 case GL_LUMINANCE16F_EXT: return GL_HALF_FLOAT_OES; 495 case GL_LUMINANCE_ALPHA16F_EXT: return GL_HALF_FLOAT_OES; 496 case GL_BGRA8_EXT: return GL_UNSIGNED_BYTE; 497 case GL_DEPTH_COMPONENT16: return GL_UNSIGNED_SHORT; 498 case GL_DEPTH_COMPONENT32_OES: return GL_UNSIGNED_INT; 499 case GL_DEPTH24_STENCIL8_OES: return GL_UNSIGNED_INT_24_8_OES; 500 default: return GL_NONE; // Unsupported 501 } 502 } 503 504 bool IsColorRenderable(GLenum internalformat) 505 { 506 switch (internalformat) 507 { 508 case GL_RGBA4: 509 case GL_RGB5_A1: 510 case GL_RGB565: 511 case GL_RGB8_OES: 512 case GL_RGBA8_OES: 513 return true; 514 case GL_DEPTH_COMPONENT16: 515 case GL_STENCIL_INDEX8: 516 case GL_DEPTH24_STENCIL8_OES: 517 return false; 518 case GL_BGRA8_EXT: 519 return true; 520 default: 521 UNIMPLEMENTED(); 522 } 523 524 return false; 525 } 526 527 bool IsDepthRenderable(GLenum internalformat) 528 { 529 switch (internalformat) 530 { 531 case GL_DEPTH_COMPONENT16: 532 case GL_DEPTH24_STENCIL8_OES: 533 return true; 534 case GL_STENCIL_INDEX8: 535 case GL_RGBA4: 536 case GL_RGB5_A1: 537 case GL_RGB565: 538 case GL_RGB8_OES: 539 case GL_RGBA8_OES: 540 return false; 541 default: 542 UNIMPLEMENTED(); 543 } 544 545 return false; 546 } 547 548 bool IsStencilRenderable(GLenum internalformat) 549 { 550 switch (internalformat) 551 { 552 case GL_STENCIL_INDEX8: 553 case GL_DEPTH24_STENCIL8_OES: 554 return true; 555 case GL_RGBA4: 556 case GL_RGB5_A1: 557 case GL_RGB565: 558 case GL_RGB8_OES: 559 case GL_RGBA8_OES: 560 case GL_DEPTH_COMPONENT16: 561 return false; 562 default: 563 UNIMPLEMENTED(); 564 } 565 566 return false; 567 } 568 569 bool IsFloat32Format(GLint internalformat) 570 { 571 switch (internalformat) 572 { 573 case GL_RGBA32F_EXT: 574 case GL_RGB32F_EXT: 575 case GL_ALPHA32F_EXT: 576 case GL_LUMINANCE32F_EXT: 577 case GL_LUMINANCE_ALPHA32F_EXT: 578 return true; 579 default: 580 return false; 581 } 582 } 583 584 bool IsFloat16Format(GLint internalformat) 585 { 586 switch (internalformat) 587 { 588 case GL_RGBA16F_EXT: 589 case GL_RGB16F_EXT: 590 case GL_ALPHA16F_EXT: 591 case GL_LUMINANCE16F_EXT: 592 case GL_LUMINANCE_ALPHA16F_EXT: 593 return true; 594 default: 595 return false; 596 } 597 } 598 599 unsigned int GetAlphaSize(GLenum colorFormat) 600 { 601 switch (colorFormat) 602 { 603 case GL_RGBA16F_EXT: 604 return 16; 605 case GL_RGBA32F_EXT: 606 return 32; 607 case GL_RGBA4: 608 return 4; 609 case GL_RGBA8_OES: 610 case GL_BGRA8_EXT: 611 return 8; 612 case GL_RGB5_A1: 613 return 1; 614 case GL_RGB8_OES: 615 case GL_RGB565: 616 case GL_RGB32F_EXT: 617 case GL_RGB16F_EXT: 618 return 0; 619 default: 620 return 0; 621 } 622 } 623 624 unsigned int GetRedSize(GLenum colorFormat) 625 { 626 switch (colorFormat) 627 { 628 case GL_RGBA16F_EXT: 629 case GL_RGB16F_EXT: 630 return 16; 631 case GL_RGBA32F_EXT: 632 case GL_RGB32F_EXT: 633 return 32; 634 case GL_RGBA4: 635 return 4; 636 case GL_RGBA8_OES: 637 case GL_BGRA8_EXT: 638 case GL_RGB8_OES: 639 return 8; 640 case GL_RGB5_A1: 641 case GL_RGB565: 642 return 5; 643 default: 644 return 0; 645 } 646 } 647 648 unsigned int GetGreenSize(GLenum colorFormat) 649 { 650 switch (colorFormat) 651 { 652 case GL_RGBA16F_EXT: 653 case GL_RGB16F_EXT: 654 return 16; 655 case GL_RGBA32F_EXT: 656 case GL_RGB32F_EXT: 657 return 32; 658 case GL_RGBA4: 659 return 4; 660 case GL_RGBA8_OES: 661 case GL_BGRA8_EXT: 662 case GL_RGB8_OES: 663 return 8; 664 case GL_RGB5_A1: 665 return 5; 666 case GL_RGB565: 667 return 6; 668 default: 669 return 0; 670 } 671 } 672 673 unsigned int GetBlueSize(GLenum colorFormat) 674 { 675 switch (colorFormat) 676 { 677 case GL_RGBA16F_EXT: 678 case GL_RGB16F_EXT: 679 return 16; 680 case GL_RGBA32F_EXT: 681 case GL_RGB32F_EXT: 682 return 32; 683 case GL_RGBA4: 684 return 4; 685 case GL_RGBA8_OES: 686 case GL_BGRA8_EXT: 687 case GL_RGB8_OES: 688 return 8; 689 case GL_RGB5_A1: 690 case GL_RGB565: 691 return 5; 692 default: 693 return 0; 694 } 695 } 696 697 unsigned int GetDepthSize(GLenum depthFormat) 698 { 699 switch (depthFormat) 700 { 701 case GL_DEPTH_COMPONENT16: return 16; 702 case GL_DEPTH_COMPONENT32_OES: return 32; 703 case GL_DEPTH24_STENCIL8_OES: return 24; 704 default: return 0; 705 } 706 } 707 708 unsigned int GetStencilSize(GLenum stencilFormat) 709 { 710 switch (stencilFormat) 711 { 712 case GL_DEPTH24_STENCIL8_OES: return 8; 713 default: return 0; 714 } 715 } 716 717 bool IsTriangleMode(GLenum drawMode) 718 { 719 switch (drawMode) 720 { 721 case GL_TRIANGLES: 722 case GL_TRIANGLE_FAN: 723 case GL_TRIANGLE_STRIP: 724 return true; 725 case GL_POINTS: 726 case GL_LINES: 727 case GL_LINE_LOOP: 728 case GL_LINE_STRIP: 729 return false; 730 default: UNREACHABLE(); 731 } 732 733 return false; 734 } 735 736 } 737 738 std::string getTempPath() 739 { 740 char path[MAX_PATH]; 741 DWORD pathLen = GetTempPathA(sizeof(path) / sizeof(path[0]), path); 742 if (pathLen == 0) 743 { 744 UNREACHABLE(); 745 return std::string(); 746 } 747 748 UINT unique = GetTempFileNameA(path, "sh", 0, path); 749 if (unique == 0) 750 { 751 UNREACHABLE(); 752 return std::string(); 753 } 754 755 return path; 756 } 757 758 void writeFile(const char* path, const void* content, size_t size) 759 { 760 FILE* file = fopen(path, "w"); 761 if (!file) 762 { 763 UNREACHABLE(); 764 return; 765 } 766 767 fwrite(content, sizeof(char), size, file); 768 fclose(file); 769 } 770