image.c revision 1a06e8454ec714e950bc88882cd985534a18bf1f
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.5 4 * 5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved. 6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the "Software"), 10 * to deal in the Software without restriction, including without limitation 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 * and/or sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included 16 * in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26 27/** 28 * \file image.c 29 * Image handling. 30 */ 31 32 33#include "glheader.h" 34#include "colormac.h" 35#include "image.h" 36#include "imports.h" 37#include "macros.h" 38#include "mfeatures.h" 39#include "mtypes.h" 40 41 42 43/** 44 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise. 45 */ 46GLboolean 47_mesa_type_is_packed(GLenum type) 48{ 49 switch (type) { 50 case GL_UNSIGNED_BYTE_3_3_2: 51 case GL_UNSIGNED_BYTE_2_3_3_REV: 52 case MESA_UNSIGNED_BYTE_4_4: 53 case GL_UNSIGNED_SHORT_5_6_5: 54 case GL_UNSIGNED_SHORT_5_6_5_REV: 55 case GL_UNSIGNED_SHORT_4_4_4_4: 56 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 57 case GL_UNSIGNED_SHORT_5_5_5_1: 58 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 59 case GL_UNSIGNED_INT_8_8_8_8: 60 case GL_UNSIGNED_INT_8_8_8_8_REV: 61 case GL_UNSIGNED_INT_10_10_10_2: 62 case GL_UNSIGNED_INT_2_10_10_10_REV: 63 case GL_UNSIGNED_SHORT_8_8_MESA: 64 case GL_UNSIGNED_SHORT_8_8_REV_MESA: 65 case GL_UNSIGNED_INT_24_8_EXT: 66 case GL_UNSIGNED_INT_5_9_9_9_REV: 67 case GL_UNSIGNED_INT_10F_11F_11F_REV: 68 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: 69 return GL_TRUE; 70 } 71 72 return GL_FALSE; 73} 74 75 76 77/** 78 * Flip the order of the 2 bytes in each word in the given array. 79 * 80 * \param p array. 81 * \param n number of words. 82 */ 83void 84_mesa_swap2( GLushort *p, GLuint n ) 85{ 86 GLuint i; 87 for (i = 0; i < n; i++) { 88 p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00); 89 } 90} 91 92 93 94/* 95 * Flip the order of the 4 bytes in each word in the given array. 96 */ 97void 98_mesa_swap4( GLuint *p, GLuint n ) 99{ 100 GLuint i, a, b; 101 for (i = 0; i < n; i++) { 102 b = p[i]; 103 a = (b >> 24) 104 | ((b >> 8) & 0xff00) 105 | ((b << 8) & 0xff0000) 106 | ((b << 24) & 0xff000000); 107 p[i] = a; 108 } 109} 110 111 112/** 113 * Get the size of a GL data type. 114 * 115 * \param type GL data type. 116 * 117 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1 118 * if an invalid type enum. 119 */ 120GLint 121_mesa_sizeof_type( GLenum type ) 122{ 123 switch (type) { 124 case GL_BITMAP: 125 return 0; 126 case GL_UNSIGNED_BYTE: 127 return sizeof(GLubyte); 128 case GL_BYTE: 129 return sizeof(GLbyte); 130 case GL_UNSIGNED_SHORT: 131 return sizeof(GLushort); 132 case GL_SHORT: 133 return sizeof(GLshort); 134 case GL_UNSIGNED_INT: 135 return sizeof(GLuint); 136 case GL_INT: 137 return sizeof(GLint); 138 case GL_FLOAT: 139 return sizeof(GLfloat); 140 case GL_DOUBLE: 141 return sizeof(GLdouble); 142 case GL_HALF_FLOAT_ARB: 143 return sizeof(GLhalfARB); 144 case GL_FIXED: 145 return sizeof(GLfixed); 146 default: 147 return -1; 148 } 149} 150 151 152/** 153 * Same as _mesa_sizeof_type() but also accepting the packed pixel 154 * format data types. 155 */ 156GLint 157_mesa_sizeof_packed_type( GLenum type ) 158{ 159 switch (type) { 160 case GL_BITMAP: 161 return 0; 162 case GL_UNSIGNED_BYTE: 163 return sizeof(GLubyte); 164 case GL_BYTE: 165 return sizeof(GLbyte); 166 case GL_UNSIGNED_SHORT: 167 return sizeof(GLushort); 168 case GL_SHORT: 169 return sizeof(GLshort); 170 case GL_UNSIGNED_INT: 171 return sizeof(GLuint); 172 case GL_INT: 173 return sizeof(GLint); 174 case GL_HALF_FLOAT_ARB: 175 return sizeof(GLhalfARB); 176 case GL_FLOAT: 177 return sizeof(GLfloat); 178 case GL_UNSIGNED_BYTE_3_3_2: 179 case GL_UNSIGNED_BYTE_2_3_3_REV: 180 case MESA_UNSIGNED_BYTE_4_4: 181 return sizeof(GLubyte); 182 case GL_UNSIGNED_SHORT_5_6_5: 183 case GL_UNSIGNED_SHORT_5_6_5_REV: 184 case GL_UNSIGNED_SHORT_4_4_4_4: 185 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 186 case GL_UNSIGNED_SHORT_5_5_5_1: 187 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 188 case GL_UNSIGNED_SHORT_8_8_MESA: 189 case GL_UNSIGNED_SHORT_8_8_REV_MESA: 190 return sizeof(GLushort); 191 case GL_UNSIGNED_INT_8_8_8_8: 192 case GL_UNSIGNED_INT_8_8_8_8_REV: 193 case GL_UNSIGNED_INT_10_10_10_2: 194 case GL_UNSIGNED_INT_2_10_10_10_REV: 195 case GL_UNSIGNED_INT_24_8_EXT: 196 case GL_UNSIGNED_INT_5_9_9_9_REV: 197 case GL_UNSIGNED_INT_10F_11F_11F_REV: 198 return sizeof(GLuint); 199 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: 200 return 8; 201 default: 202 return -1; 203 } 204} 205 206 207/** 208 * Get the number of components in a pixel format. 209 * 210 * \param format pixel format. 211 * 212 * \return the number of components in the given format, or -1 if a bad format. 213 */ 214GLint 215_mesa_components_in_format( GLenum format ) 216{ 217 switch (format) { 218 case GL_COLOR_INDEX: 219 case GL_STENCIL_INDEX: 220 case GL_DEPTH_COMPONENT: 221 case GL_RED: 222 case GL_RED_INTEGER_EXT: 223 case GL_GREEN: 224 case GL_GREEN_INTEGER_EXT: 225 case GL_BLUE: 226 case GL_BLUE_INTEGER_EXT: 227 case GL_ALPHA: 228 case GL_ALPHA_INTEGER_EXT: 229 case GL_LUMINANCE: 230 case GL_LUMINANCE_INTEGER_EXT: 231 case GL_INTENSITY: 232 return 1; 233 234 case GL_LUMINANCE_ALPHA: 235 case GL_LUMINANCE_ALPHA_INTEGER_EXT: 236 case GL_RG: 237 case GL_YCBCR_MESA: 238 case GL_DEPTH_STENCIL_EXT: 239 case GL_DUDV_ATI: 240 case GL_DU8DV8_ATI: 241 case GL_RG_INTEGER: 242 return 2; 243 244 case GL_RGB: 245 case GL_BGR: 246 case GL_RGB_INTEGER_EXT: 247 case GL_BGR_INTEGER_EXT: 248 return 3; 249 250 case GL_RGBA: 251 case GL_BGRA: 252 case GL_ABGR_EXT: 253 case GL_RGBA_INTEGER_EXT: 254 case GL_BGRA_INTEGER_EXT: 255 return 4; 256 257 default: 258 return -1; 259 } 260} 261 262 263/** 264 * Get the bytes per pixel of pixel format type pair. 265 * 266 * \param format pixel format. 267 * \param type pixel type. 268 * 269 * \return bytes per pixel, or -1 if a bad format or type was given. 270 */ 271GLint 272_mesa_bytes_per_pixel( GLenum format, GLenum type ) 273{ 274 GLint comps = _mesa_components_in_format( format ); 275 if (comps < 0) 276 return -1; 277 278 switch (type) { 279 case GL_BITMAP: 280 return 0; /* special case */ 281 case GL_BYTE: 282 case GL_UNSIGNED_BYTE: 283 return comps * sizeof(GLubyte); 284 case GL_SHORT: 285 case GL_UNSIGNED_SHORT: 286 return comps * sizeof(GLshort); 287 case GL_INT: 288 case GL_UNSIGNED_INT: 289 return comps * sizeof(GLint); 290 case GL_FLOAT: 291 return comps * sizeof(GLfloat); 292 case GL_HALF_FLOAT_ARB: 293 return comps * sizeof(GLhalfARB); 294 case GL_UNSIGNED_BYTE_3_3_2: 295 case GL_UNSIGNED_BYTE_2_3_3_REV: 296 if (format == GL_RGB || format == GL_BGR || 297 format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT) 298 return sizeof(GLubyte); 299 else 300 return -1; /* error */ 301 case GL_UNSIGNED_SHORT_5_6_5: 302 case GL_UNSIGNED_SHORT_5_6_5_REV: 303 if (format == GL_RGB || format == GL_BGR || 304 format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT) 305 return sizeof(GLushort); 306 else 307 return -1; /* error */ 308 case GL_UNSIGNED_SHORT_4_4_4_4: 309 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 310 case GL_UNSIGNED_SHORT_5_5_5_1: 311 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 312 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT || 313 format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT) 314 return sizeof(GLushort); 315 else 316 return -1; 317 case GL_UNSIGNED_INT_8_8_8_8: 318 case GL_UNSIGNED_INT_8_8_8_8_REV: 319 case GL_UNSIGNED_INT_10_10_10_2: 320 case GL_UNSIGNED_INT_2_10_10_10_REV: 321 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT || 322 format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT) 323 return sizeof(GLuint); 324 else 325 return -1; 326 case GL_UNSIGNED_SHORT_8_8_MESA: 327 case GL_UNSIGNED_SHORT_8_8_REV_MESA: 328 if (format == GL_YCBCR_MESA) 329 return sizeof(GLushort); 330 else 331 return -1; 332 case GL_UNSIGNED_INT_24_8_EXT: 333 if (format == GL_DEPTH_STENCIL_EXT) 334 return sizeof(GLuint); 335 else 336 return -1; 337 case GL_UNSIGNED_INT_5_9_9_9_REV: 338 if (format == GL_RGB) 339 return sizeof(GLuint); 340 else 341 return -1; 342 case GL_UNSIGNED_INT_10F_11F_11F_REV: 343 if (format == GL_RGB) 344 return sizeof(GLuint); 345 else 346 return -1; 347 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: 348 if (format == GL_DEPTH_STENCIL) 349 return 8; 350 else 351 return -1; 352 default: 353 return -1; 354 } 355} 356 357 358/** 359 * Do error checking of format/type combinations for glReadPixels, 360 * glDrawPixels and glTex[Sub]Image. Note that depending on the format 361 * and type values, we may either generate GL_INVALID_OPERATION or 362 * GL_INVALID_ENUM. 363 * 364 * \param format pixel format. 365 * \param type pixel type. 366 * 367 * \return GL_INVALID_ENUM, GL_INVALID_OPERATION or GL_NO_ERROR 368 */ 369GLenum 370_mesa_error_check_format_and_type(const struct gl_context *ctx, 371 GLenum format, GLenum type) 372{ 373 /* special type-based checks (see glReadPixels, glDrawPixels error lists) */ 374 switch (type) { 375 case GL_BITMAP: 376 if (format != GL_COLOR_INDEX && format != GL_STENCIL_INDEX) { 377 return GL_INVALID_ENUM; 378 } 379 break; 380 381 case GL_UNSIGNED_BYTE_3_3_2: 382 case GL_UNSIGNED_BYTE_2_3_3_REV: 383 case GL_UNSIGNED_SHORT_5_6_5: 384 case GL_UNSIGNED_SHORT_5_6_5_REV: 385 if (format == GL_RGB) { 386 break; /* OK */ 387 } 388 if (format == GL_RGB_INTEGER_EXT && 389 ctx->Extensions.ARB_texture_rgb10_a2ui) { 390 break; /* OK */ 391 } 392 return GL_INVALID_OPERATION; 393 394 case GL_UNSIGNED_SHORT_4_4_4_4: 395 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 396 case GL_UNSIGNED_SHORT_5_5_5_1: 397 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 398 case GL_UNSIGNED_INT_8_8_8_8: 399 case GL_UNSIGNED_INT_8_8_8_8_REV: 400 case GL_UNSIGNED_INT_10_10_10_2: 401 case GL_UNSIGNED_INT_2_10_10_10_REV: 402 if (format == GL_RGBA || 403 format == GL_BGRA || 404 format == GL_ABGR_EXT) { 405 break; /* OK */ 406 } 407 if ((format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT) && 408 ctx->Extensions.ARB_texture_rgb10_a2ui) { 409 break; /* OK */ 410 } 411 return GL_INVALID_OPERATION; 412 413 case GL_UNSIGNED_INT_24_8: 414 if (!ctx->Extensions.EXT_packed_depth_stencil) { 415 return GL_INVALID_ENUM; 416 } 417 if (format != GL_DEPTH_STENCIL) { 418 return GL_INVALID_OPERATION; 419 } 420 return GL_NO_ERROR; 421 422 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: 423 if (!ctx->Extensions.ARB_depth_buffer_float) { 424 return GL_INVALID_ENUM; 425 } 426 if (format != GL_DEPTH_STENCIL) { 427 return GL_INVALID_OPERATION; 428 } 429 return GL_NO_ERROR; 430 431 case GL_UNSIGNED_INT_10F_11F_11F_REV: 432 if (!ctx->Extensions.EXT_packed_float) { 433 return GL_INVALID_ENUM; 434 } 435 if (format != GL_RGB) { 436 return GL_INVALID_OPERATION; 437 } 438 return GL_NO_ERROR; 439 440 default: 441 ; /* fall-through */ 442 } 443 444 /* now, for each format, check the type for compatibility */ 445 switch (format) { 446 case GL_COLOR_INDEX: 447 case GL_STENCIL_INDEX: 448 switch (type) { 449 case GL_BITMAP: 450 case GL_BYTE: 451 case GL_UNSIGNED_BYTE: 452 case GL_SHORT: 453 case GL_UNSIGNED_SHORT: 454 case GL_INT: 455 case GL_UNSIGNED_INT: 456 case GL_FLOAT: 457 return GL_NO_ERROR; 458 case GL_HALF_FLOAT: 459 return ctx->Extensions.ARB_half_float_pixel 460 ? GL_NO_ERROR : GL_INVALID_ENUM; 461 default: 462 return GL_INVALID_ENUM; 463 } 464 465 case GL_RED: 466 case GL_GREEN: 467 case GL_BLUE: 468 case GL_ALPHA: 469#if 0 /* not legal! see table 3.6 of the 1.5 spec */ 470 case GL_INTENSITY: 471#endif 472 case GL_LUMINANCE: 473 case GL_LUMINANCE_ALPHA: 474 case GL_DEPTH_COMPONENT: 475 switch (type) { 476 case GL_BYTE: 477 case GL_UNSIGNED_BYTE: 478 case GL_SHORT: 479 case GL_UNSIGNED_SHORT: 480 case GL_INT: 481 case GL_UNSIGNED_INT: 482 case GL_FLOAT: 483 return GL_NO_ERROR; 484 case GL_HALF_FLOAT: 485 return ctx->Extensions.ARB_half_float_pixel 486 ? GL_NO_ERROR : GL_INVALID_ENUM; 487 default: 488 return GL_INVALID_ENUM; 489 } 490 491 case GL_RG: 492 if (!ctx->Extensions.ARB_texture_rg) 493 return GL_INVALID_ENUM; 494 switch (type) { 495 case GL_BYTE: 496 case GL_UNSIGNED_BYTE: 497 case GL_SHORT: 498 case GL_UNSIGNED_SHORT: 499 case GL_INT: 500 case GL_UNSIGNED_INT: 501 case GL_FLOAT: 502 return GL_NO_ERROR; 503 case GL_HALF_FLOAT: 504 return ctx->Extensions.ARB_half_float_pixel 505 ? GL_NO_ERROR : GL_INVALID_ENUM; 506 default: 507 return GL_INVALID_ENUM; 508 } 509 510 case GL_RGB: 511 switch (type) { 512 case GL_BYTE: 513 case GL_UNSIGNED_BYTE: 514 case GL_SHORT: 515 case GL_UNSIGNED_SHORT: 516 case GL_INT: 517 case GL_UNSIGNED_INT: 518 case GL_FLOAT: 519 case GL_UNSIGNED_BYTE_3_3_2: 520 case GL_UNSIGNED_BYTE_2_3_3_REV: 521 case GL_UNSIGNED_SHORT_5_6_5: 522 case GL_UNSIGNED_SHORT_5_6_5_REV: 523 return GL_NO_ERROR; 524 case GL_HALF_FLOAT: 525 return ctx->Extensions.ARB_half_float_pixel 526 ? GL_NO_ERROR : GL_INVALID_ENUM; 527 case GL_UNSIGNED_INT_5_9_9_9_REV: 528 return ctx->Extensions.EXT_texture_shared_exponent 529 ? GL_NO_ERROR : GL_INVALID_ENUM; 530 case GL_UNSIGNED_INT_10F_11F_11F_REV: 531 return ctx->Extensions.EXT_packed_float 532 ? GL_NO_ERROR : GL_INVALID_ENUM; 533 default: 534 return GL_INVALID_ENUM; 535 } 536 537 case GL_BGR: 538 switch (type) { 539 /* NOTE: no packed types are supported with BGR. That's 540 * intentional, according to the GL spec. 541 */ 542 case GL_BYTE: 543 case GL_UNSIGNED_BYTE: 544 case GL_SHORT: 545 case GL_UNSIGNED_SHORT: 546 case GL_INT: 547 case GL_UNSIGNED_INT: 548 case GL_FLOAT: 549 return GL_NO_ERROR; 550 case GL_HALF_FLOAT: 551 return ctx->Extensions.ARB_half_float_pixel 552 ? GL_NO_ERROR : GL_INVALID_ENUM; 553 default: 554 return GL_INVALID_ENUM; 555 } 556 557 case GL_RGBA: 558 case GL_BGRA: 559 case GL_ABGR_EXT: 560 switch (type) { 561 case GL_BYTE: 562 case GL_UNSIGNED_BYTE: 563 case GL_SHORT: 564 case GL_UNSIGNED_SHORT: 565 case GL_INT: 566 case GL_UNSIGNED_INT: 567 case GL_FLOAT: 568 case GL_UNSIGNED_SHORT_4_4_4_4: 569 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 570 case GL_UNSIGNED_SHORT_5_5_5_1: 571 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 572 case GL_UNSIGNED_INT_8_8_8_8: 573 case GL_UNSIGNED_INT_8_8_8_8_REV: 574 case GL_UNSIGNED_INT_10_10_10_2: 575 case GL_UNSIGNED_INT_2_10_10_10_REV: 576 return GL_NO_ERROR; 577 case GL_HALF_FLOAT: 578 return ctx->Extensions.ARB_half_float_pixel 579 ? GL_NO_ERROR : GL_INVALID_ENUM; 580 default: 581 return GL_INVALID_ENUM; 582 } 583 584 case GL_YCBCR_MESA: 585 if (!ctx->Extensions.MESA_ycbcr_texture) 586 return GL_INVALID_ENUM; 587 if (type == GL_UNSIGNED_SHORT_8_8_MESA || 588 type == GL_UNSIGNED_SHORT_8_8_REV_MESA) 589 return GL_NO_ERROR; 590 else 591 return GL_INVALID_OPERATION; 592 593 case GL_DEPTH_STENCIL_EXT: 594 if (ctx->Extensions.EXT_packed_depth_stencil && 595 type == GL_UNSIGNED_INT_24_8) 596 return GL_NO_ERROR; 597 else if (ctx->Extensions.ARB_depth_buffer_float && 598 type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV) 599 return GL_NO_ERROR; 600 else 601 return GL_INVALID_ENUM; 602 603 case GL_DUDV_ATI: 604 case GL_DU8DV8_ATI: 605 if (!ctx->Extensions.ATI_envmap_bumpmap) 606 return GL_INVALID_ENUM; 607 switch (type) { 608 case GL_BYTE: 609 case GL_UNSIGNED_BYTE: 610 case GL_SHORT: 611 case GL_UNSIGNED_SHORT: 612 case GL_INT: 613 case GL_UNSIGNED_INT: 614 case GL_FLOAT: 615 return GL_NO_ERROR; 616 default: 617 return GL_INVALID_ENUM; 618 } 619 620 /* integer-valued formats */ 621 case GL_RED_INTEGER_EXT: 622 case GL_GREEN_INTEGER_EXT: 623 case GL_BLUE_INTEGER_EXT: 624 case GL_ALPHA_INTEGER_EXT: 625 case GL_RG_INTEGER: 626 switch (type) { 627 case GL_BYTE: 628 case GL_UNSIGNED_BYTE: 629 case GL_SHORT: 630 case GL_UNSIGNED_SHORT: 631 case GL_INT: 632 case GL_UNSIGNED_INT: 633 return (ctx->VersionMajor >= 3 || 634 ctx->Extensions.EXT_texture_integer) 635 ? GL_NO_ERROR : GL_INVALID_ENUM; 636 default: 637 return GL_INVALID_ENUM; 638 } 639 640 case GL_RGB_INTEGER_EXT: 641 switch (type) { 642 case GL_BYTE: 643 case GL_UNSIGNED_BYTE: 644 case GL_SHORT: 645 case GL_UNSIGNED_SHORT: 646 case GL_INT: 647 case GL_UNSIGNED_INT: 648 return (ctx->VersionMajor >= 3 || 649 ctx->Extensions.EXT_texture_integer) 650 ? GL_NO_ERROR : GL_INVALID_ENUM; 651 case GL_UNSIGNED_BYTE_3_3_2: 652 case GL_UNSIGNED_BYTE_2_3_3_REV: 653 case GL_UNSIGNED_SHORT_5_6_5: 654 case GL_UNSIGNED_SHORT_5_6_5_REV: 655 return ctx->Extensions.ARB_texture_rgb10_a2ui 656 ? GL_NO_ERROR : GL_INVALID_ENUM; 657 default: 658 return GL_INVALID_ENUM; 659 } 660 661 case GL_BGR_INTEGER_EXT: 662 switch (type) { 663 case GL_BYTE: 664 case GL_UNSIGNED_BYTE: 665 case GL_SHORT: 666 case GL_UNSIGNED_SHORT: 667 case GL_INT: 668 case GL_UNSIGNED_INT: 669 /* NOTE: no packed formats w/ BGR format */ 670 return (ctx->VersionMajor >= 3 || 671 ctx->Extensions.EXT_texture_integer) 672 ? GL_NO_ERROR : GL_INVALID_ENUM; 673 default: 674 return GL_INVALID_ENUM; 675 } 676 677 case GL_RGBA_INTEGER_EXT: 678 case GL_BGRA_INTEGER_EXT: 679 switch (type) { 680 case GL_BYTE: 681 case GL_UNSIGNED_BYTE: 682 case GL_SHORT: 683 case GL_UNSIGNED_SHORT: 684 case GL_INT: 685 case GL_UNSIGNED_INT: 686 return (ctx->VersionMajor >= 3 || 687 ctx->Extensions.EXT_texture_integer) 688 ? GL_NO_ERROR : GL_INVALID_ENUM; 689 case GL_UNSIGNED_SHORT_4_4_4_4: 690 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 691 case GL_UNSIGNED_SHORT_5_5_5_1: 692 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 693 case GL_UNSIGNED_INT_8_8_8_8: 694 case GL_UNSIGNED_INT_8_8_8_8_REV: 695 case GL_UNSIGNED_INT_10_10_10_2: 696 case GL_UNSIGNED_INT_2_10_10_10_REV: 697 return ctx->Extensions.ARB_texture_rgb10_a2ui 698 ? GL_NO_ERROR : GL_INVALID_ENUM; 699 default: 700 return GL_INVALID_ENUM; 701 } 702 703 case GL_LUMINANCE_INTEGER_EXT: 704 case GL_LUMINANCE_ALPHA_INTEGER_EXT: 705 switch (type) { 706 case GL_BYTE: 707 case GL_UNSIGNED_BYTE: 708 case GL_SHORT: 709 case GL_UNSIGNED_SHORT: 710 case GL_INT: 711 case GL_UNSIGNED_INT: 712 return ctx->Extensions.EXT_texture_integer 713 ? GL_NO_ERROR : GL_INVALID_ENUM; 714 default: 715 return GL_INVALID_ENUM; 716 } 717 718 default: 719 return GL_INVALID_ENUM; 720 } 721 return GL_NO_ERROR; 722} 723 724 725/** 726 * Test if the given image format is a color/RGBA format (i.e., not color 727 * index, depth, stencil, etc). 728 * \param format the image format value (may by an internal texture format) 729 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise. 730 */ 731GLboolean 732_mesa_is_color_format(GLenum format) 733{ 734 switch (format) { 735 case GL_RED: 736 case GL_GREEN: 737 case GL_BLUE: 738 case GL_ALPHA: 739 case GL_ALPHA4: 740 case GL_ALPHA8: 741 case GL_ALPHA12: 742 case GL_ALPHA16: 743 case 1: 744 case GL_LUMINANCE: 745 case GL_LUMINANCE4: 746 case GL_LUMINANCE8: 747 case GL_LUMINANCE12: 748 case GL_LUMINANCE16: 749 case 2: 750 case GL_LUMINANCE_ALPHA: 751 case GL_LUMINANCE4_ALPHA4: 752 case GL_LUMINANCE6_ALPHA2: 753 case GL_LUMINANCE8_ALPHA8: 754 case GL_LUMINANCE12_ALPHA4: 755 case GL_LUMINANCE12_ALPHA12: 756 case GL_LUMINANCE16_ALPHA16: 757 case GL_INTENSITY: 758 case GL_INTENSITY4: 759 case GL_INTENSITY8: 760 case GL_INTENSITY12: 761 case GL_INTENSITY16: 762 case GL_R8: 763 case GL_R16: 764 case GL_RG: 765 case GL_RG8: 766 case GL_RG16: 767 case 3: 768 case GL_RGB: 769 case GL_BGR: 770 case GL_R3_G3_B2: 771 case GL_RGB4: 772 case GL_RGB5: 773 case GL_RGB565: 774 case GL_RGB8: 775 case GL_RGB10: 776 case GL_RGB12: 777 case GL_RGB16: 778 case 4: 779 case GL_ABGR_EXT: 780 case GL_RGBA: 781 case GL_BGRA: 782 case GL_RGBA2: 783 case GL_RGBA4: 784 case GL_RGB5_A1: 785 case GL_RGBA8: 786 case GL_RGB10_A2: 787 case GL_RGBA12: 788 case GL_RGBA16: 789 /* float texture formats */ 790 case GL_ALPHA16F_ARB: 791 case GL_ALPHA32F_ARB: 792 case GL_LUMINANCE16F_ARB: 793 case GL_LUMINANCE32F_ARB: 794 case GL_LUMINANCE_ALPHA16F_ARB: 795 case GL_LUMINANCE_ALPHA32F_ARB: 796 case GL_INTENSITY16F_ARB: 797 case GL_INTENSITY32F_ARB: 798 case GL_R16F: 799 case GL_R32F: 800 case GL_RG16F: 801 case GL_RG32F: 802 case GL_RGB16F_ARB: 803 case GL_RGB32F_ARB: 804 case GL_RGBA16F_ARB: 805 case GL_RGBA32F_ARB: 806 /* compressed formats */ 807 case GL_COMPRESSED_ALPHA: 808 case GL_COMPRESSED_LUMINANCE: 809 case GL_COMPRESSED_LUMINANCE_ALPHA: 810 case GL_COMPRESSED_INTENSITY: 811 case GL_COMPRESSED_RED: 812 case GL_COMPRESSED_RG: 813 case GL_COMPRESSED_RGB: 814 case GL_COMPRESSED_RGBA: 815 case GL_RGB_S3TC: 816 case GL_RGB4_S3TC: 817 case GL_RGBA_S3TC: 818 case GL_RGBA4_S3TC: 819 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: 820 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: 821 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: 822 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: 823 case GL_COMPRESSED_RGB_FXT1_3DFX: 824 case GL_COMPRESSED_RGBA_FXT1_3DFX: 825#if FEATURE_EXT_texture_sRGB 826 case GL_SRGB_EXT: 827 case GL_SRGB8_EXT: 828 case GL_SRGB_ALPHA_EXT: 829 case GL_SRGB8_ALPHA8_EXT: 830 case GL_SLUMINANCE_ALPHA_EXT: 831 case GL_SLUMINANCE8_ALPHA8_EXT: 832 case GL_SLUMINANCE_EXT: 833 case GL_SLUMINANCE8_EXT: 834 case GL_COMPRESSED_SRGB_EXT: 835 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT: 836 case GL_COMPRESSED_SRGB_ALPHA_EXT: 837 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: 838 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT: 839 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: 840 case GL_COMPRESSED_SLUMINANCE_EXT: 841 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT: 842#endif /* FEATURE_EXT_texture_sRGB */ 843 case GL_COMPRESSED_RED_RGTC1: 844 case GL_COMPRESSED_SIGNED_RED_RGTC1: 845 case GL_COMPRESSED_RG_RGTC2: 846 case GL_COMPRESSED_SIGNED_RG_RGTC2: 847 case GL_COMPRESSED_LUMINANCE_LATC1_EXT: 848 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT: 849 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT: 850 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT: 851 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI: 852 case GL_ETC1_RGB8_OES: 853 /* generic integer formats */ 854 case GL_RED_INTEGER_EXT: 855 case GL_GREEN_INTEGER_EXT: 856 case GL_BLUE_INTEGER_EXT: 857 case GL_ALPHA_INTEGER_EXT: 858 case GL_RGB_INTEGER_EXT: 859 case GL_RGBA_INTEGER_EXT: 860 case GL_BGR_INTEGER_EXT: 861 case GL_BGRA_INTEGER_EXT: 862 case GL_RG_INTEGER: 863 case GL_LUMINANCE_INTEGER_EXT: 864 case GL_LUMINANCE_ALPHA_INTEGER_EXT: 865 /* sized integer formats */ 866 case GL_RGBA32UI_EXT: 867 case GL_RGB32UI_EXT: 868 case GL_RG32UI: 869 case GL_R32UI: 870 case GL_ALPHA32UI_EXT: 871 case GL_INTENSITY32UI_EXT: 872 case GL_LUMINANCE32UI_EXT: 873 case GL_LUMINANCE_ALPHA32UI_EXT: 874 case GL_RGBA16UI_EXT: 875 case GL_RGB16UI_EXT: 876 case GL_RG16UI: 877 case GL_R16UI: 878 case GL_ALPHA16UI_EXT: 879 case GL_INTENSITY16UI_EXT: 880 case GL_LUMINANCE16UI_EXT: 881 case GL_LUMINANCE_ALPHA16UI_EXT: 882 case GL_RGBA8UI_EXT: 883 case GL_RGB8UI_EXT: 884 case GL_RG8UI: 885 case GL_R8UI: 886 case GL_ALPHA8UI_EXT: 887 case GL_INTENSITY8UI_EXT: 888 case GL_LUMINANCE8UI_EXT: 889 case GL_LUMINANCE_ALPHA8UI_EXT: 890 case GL_RGBA32I_EXT: 891 case GL_RGB32I_EXT: 892 case GL_RG32I: 893 case GL_R32I: 894 case GL_ALPHA32I_EXT: 895 case GL_INTENSITY32I_EXT: 896 case GL_LUMINANCE32I_EXT: 897 case GL_LUMINANCE_ALPHA32I_EXT: 898 case GL_RGBA16I_EXT: 899 case GL_RGB16I_EXT: 900 case GL_RG16I: 901 case GL_R16I: 902 case GL_ALPHA16I_EXT: 903 case GL_INTENSITY16I_EXT: 904 case GL_LUMINANCE16I_EXT: 905 case GL_LUMINANCE_ALPHA16I_EXT: 906 case GL_RGBA8I_EXT: 907 case GL_RGB8I_EXT: 908 case GL_RG8I: 909 case GL_R8I: 910 case GL_ALPHA8I_EXT: 911 case GL_INTENSITY8I_EXT: 912 case GL_LUMINANCE8I_EXT: 913 case GL_LUMINANCE_ALPHA8I_EXT: 914 /* signed, normalized texture formats */ 915 case GL_RED_SNORM: 916 case GL_R8_SNORM: 917 case GL_R16_SNORM: 918 case GL_RG_SNORM: 919 case GL_RG8_SNORM: 920 case GL_RG16_SNORM: 921 case GL_RGB_SNORM: 922 case GL_RGB8_SNORM: 923 case GL_RGB16_SNORM: 924 case GL_RGBA_SNORM: 925 case GL_RGBA8_SNORM: 926 case GL_RGBA16_SNORM: 927 case GL_ALPHA_SNORM: 928 case GL_ALPHA8_SNORM: 929 case GL_ALPHA16_SNORM: 930 case GL_LUMINANCE_SNORM: 931 case GL_LUMINANCE8_SNORM: 932 case GL_LUMINANCE16_SNORM: 933 case GL_LUMINANCE_ALPHA_SNORM: 934 case GL_LUMINANCE8_ALPHA8_SNORM: 935 case GL_LUMINANCE16_ALPHA16_SNORM: 936 case GL_INTENSITY_SNORM: 937 case GL_INTENSITY8_SNORM: 938 case GL_INTENSITY16_SNORM: 939 case GL_RGB9_E5: 940 case GL_R11F_G11F_B10F: 941 case GL_RGB10_A2UI: 942 return GL_TRUE; 943 case GL_YCBCR_MESA: /* not considered to be RGB */ 944 /* fall-through */ 945 default: 946 return GL_FALSE; 947 } 948} 949 950 951/** 952 * Test if the given image format is a depth component format. 953 */ 954GLboolean 955_mesa_is_depth_format(GLenum format) 956{ 957 switch (format) { 958 case GL_DEPTH_COMPONENT: 959 case GL_DEPTH_COMPONENT16: 960 case GL_DEPTH_COMPONENT24: 961 case GL_DEPTH_COMPONENT32: 962 case GL_DEPTH_COMPONENT32F: 963 return GL_TRUE; 964 default: 965 return GL_FALSE; 966 } 967} 968 969 970/** 971 * Test if the given image format is a stencil format. 972 */ 973GLboolean 974_mesa_is_stencil_format(GLenum format) 975{ 976 switch (format) { 977 case GL_STENCIL_INDEX: 978 return GL_TRUE; 979 default: 980 return GL_FALSE; 981 } 982} 983 984 985/** 986 * Test if the given image format is a YCbCr format. 987 */ 988GLboolean 989_mesa_is_ycbcr_format(GLenum format) 990{ 991 switch (format) { 992 case GL_YCBCR_MESA: 993 return GL_TRUE; 994 default: 995 return GL_FALSE; 996 } 997} 998 999 1000/** 1001 * Test if the given image format is a depth+stencil format. 1002 */ 1003GLboolean 1004_mesa_is_depthstencil_format(GLenum format) 1005{ 1006 switch (format) { 1007 case GL_DEPTH24_STENCIL8_EXT: 1008 case GL_DEPTH_STENCIL_EXT: 1009 case GL_DEPTH32F_STENCIL8: 1010 return GL_TRUE; 1011 default: 1012 return GL_FALSE; 1013 } 1014} 1015 1016 1017/** 1018 * Test if the given image format is a depth or stencil format. 1019 */ 1020GLboolean 1021_mesa_is_depth_or_stencil_format(GLenum format) 1022{ 1023 switch (format) { 1024 case GL_DEPTH_COMPONENT: 1025 case GL_DEPTH_COMPONENT16: 1026 case GL_DEPTH_COMPONENT24: 1027 case GL_DEPTH_COMPONENT32: 1028 case GL_STENCIL_INDEX: 1029 case GL_STENCIL_INDEX1_EXT: 1030 case GL_STENCIL_INDEX4_EXT: 1031 case GL_STENCIL_INDEX8_EXT: 1032 case GL_STENCIL_INDEX16_EXT: 1033 case GL_DEPTH_STENCIL_EXT: 1034 case GL_DEPTH24_STENCIL8_EXT: 1035 case GL_DEPTH_COMPONENT32F: 1036 case GL_DEPTH32F_STENCIL8: 1037 return GL_TRUE; 1038 default: 1039 return GL_FALSE; 1040 } 1041} 1042 1043 1044/** 1045 * Test if the given image format is a dudv format. 1046 */ 1047GLboolean 1048_mesa_is_dudv_format(GLenum format) 1049{ 1050 switch (format) { 1051 case GL_DUDV_ATI: 1052 case GL_DU8DV8_ATI: 1053 return GL_TRUE; 1054 default: 1055 return GL_FALSE; 1056 } 1057} 1058 1059 1060/** 1061 * Test if the given format is an integer (non-normalized) format. 1062 */ 1063GLboolean 1064_mesa_is_integer_format(GLenum format) 1065{ 1066 switch (format) { 1067 /* generic integer formats */ 1068 case GL_RED_INTEGER_EXT: 1069 case GL_GREEN_INTEGER_EXT: 1070 case GL_BLUE_INTEGER_EXT: 1071 case GL_ALPHA_INTEGER_EXT: 1072 case GL_RGB_INTEGER_EXT: 1073 case GL_RGBA_INTEGER_EXT: 1074 case GL_BGR_INTEGER_EXT: 1075 case GL_BGRA_INTEGER_EXT: 1076 case GL_LUMINANCE_INTEGER_EXT: 1077 case GL_LUMINANCE_ALPHA_INTEGER_EXT: 1078 case GL_RG_INTEGER: 1079 /* specific integer formats */ 1080 case GL_RGBA32UI_EXT: 1081 case GL_RGB32UI_EXT: 1082 case GL_RG32UI: 1083 case GL_R32UI: 1084 case GL_ALPHA32UI_EXT: 1085 case GL_INTENSITY32UI_EXT: 1086 case GL_LUMINANCE32UI_EXT: 1087 case GL_LUMINANCE_ALPHA32UI_EXT: 1088 case GL_RGBA16UI_EXT: 1089 case GL_RGB16UI_EXT: 1090 case GL_RG16UI: 1091 case GL_R16UI: 1092 case GL_ALPHA16UI_EXT: 1093 case GL_INTENSITY16UI_EXT: 1094 case GL_LUMINANCE16UI_EXT: 1095 case GL_LUMINANCE_ALPHA16UI_EXT: 1096 case GL_RGBA8UI_EXT: 1097 case GL_RGB8UI_EXT: 1098 case GL_RG8UI: 1099 case GL_R8UI: 1100 case GL_ALPHA8UI_EXT: 1101 case GL_INTENSITY8UI_EXT: 1102 case GL_LUMINANCE8UI_EXT: 1103 case GL_LUMINANCE_ALPHA8UI_EXT: 1104 case GL_RGBA32I_EXT: 1105 case GL_RGB32I_EXT: 1106 case GL_RG32I: 1107 case GL_R32I: 1108 case GL_ALPHA32I_EXT: 1109 case GL_INTENSITY32I_EXT: 1110 case GL_LUMINANCE32I_EXT: 1111 case GL_LUMINANCE_ALPHA32I_EXT: 1112 case GL_RGBA16I_EXT: 1113 case GL_RGB16I_EXT: 1114 case GL_RG16I: 1115 case GL_R16I: 1116 case GL_ALPHA16I_EXT: 1117 case GL_INTENSITY16I_EXT: 1118 case GL_LUMINANCE16I_EXT: 1119 case GL_LUMINANCE_ALPHA16I_EXT: 1120 case GL_RGBA8I_EXT: 1121 case GL_RGB8I_EXT: 1122 case GL_RG8I: 1123 case GL_R8I: 1124 case GL_ALPHA8I_EXT: 1125 case GL_INTENSITY8I_EXT: 1126 case GL_LUMINANCE8I_EXT: 1127 case GL_LUMINANCE_ALPHA8I_EXT: 1128 case GL_RGB10_A2UI: 1129 return GL_TRUE; 1130 default: 1131 return GL_FALSE; 1132 } 1133} 1134 1135 1136/** 1137 * Test if an image format is a supported compressed format. 1138 * \param format the internal format token provided by the user. 1139 * \return GL_TRUE if compressed, GL_FALSE if uncompressed 1140 */ 1141GLboolean 1142_mesa_is_compressed_format(struct gl_context *ctx, GLenum format) 1143{ 1144 switch (format) { 1145 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: 1146 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: 1147 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: 1148 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: 1149 return ctx->Extensions.EXT_texture_compression_s3tc; 1150 case GL_RGB_S3TC: 1151 case GL_RGB4_S3TC: 1152 case GL_RGBA_S3TC: 1153 case GL_RGBA4_S3TC: 1154 return ctx->Extensions.S3_s3tc; 1155 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT: 1156 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: 1157 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT: 1158 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: 1159 return ctx->Extensions.EXT_texture_sRGB 1160 && ctx->Extensions.EXT_texture_compression_s3tc; 1161 case GL_COMPRESSED_RGB_FXT1_3DFX: 1162 case GL_COMPRESSED_RGBA_FXT1_3DFX: 1163 return ctx->Extensions.TDFX_texture_compression_FXT1; 1164 case GL_COMPRESSED_RED_RGTC1: 1165 case GL_COMPRESSED_SIGNED_RED_RGTC1: 1166 case GL_COMPRESSED_RG_RGTC2: 1167 case GL_COMPRESSED_SIGNED_RG_RGTC2: 1168 return ctx->Extensions.ARB_texture_compression_rgtc; 1169 case GL_COMPRESSED_LUMINANCE_LATC1_EXT: 1170 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT: 1171 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT: 1172 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT: 1173 return ctx->Extensions.EXT_texture_compression_latc; 1174 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI: 1175 return ctx->Extensions.ATI_texture_compression_3dc; 1176 case GL_ETC1_RGB8_OES: 1177 return ctx->Extensions.OES_compressed_ETC1_RGB8_texture; 1178#if FEATURE_ES 1179 case GL_PALETTE4_RGB8_OES: 1180 case GL_PALETTE4_RGBA8_OES: 1181 case GL_PALETTE4_R5_G6_B5_OES: 1182 case GL_PALETTE4_RGBA4_OES: 1183 case GL_PALETTE4_RGB5_A1_OES: 1184 case GL_PALETTE8_RGB8_OES: 1185 case GL_PALETTE8_RGBA8_OES: 1186 case GL_PALETTE8_R5_G6_B5_OES: 1187 case GL_PALETTE8_RGBA4_OES: 1188 case GL_PALETTE8_RGB5_A1_OES: 1189 return ctx->API == API_OPENGLES; 1190#endif 1191 default: 1192 return GL_FALSE; 1193 } 1194} 1195 1196 1197/** 1198 * Does the given base texture/renderbuffer format have the channel 1199 * named by 'pname'? 1200 */ 1201GLboolean 1202_mesa_base_format_has_channel(GLenum base_format, GLenum pname) 1203{ 1204 switch (pname) { 1205 case GL_TEXTURE_RED_SIZE: 1206 case GL_TEXTURE_RED_TYPE: 1207 case GL_RENDERBUFFER_RED_SIZE_EXT: 1208 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE: 1209 if (base_format == GL_RED || 1210 base_format == GL_RG || 1211 base_format == GL_RGB || 1212 base_format == GL_RGBA) { 1213 return GL_TRUE; 1214 } 1215 return GL_FALSE; 1216 case GL_TEXTURE_GREEN_SIZE: 1217 case GL_TEXTURE_GREEN_TYPE: 1218 case GL_RENDERBUFFER_GREEN_SIZE_EXT: 1219 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE: 1220 if (base_format == GL_RG || 1221 base_format == GL_RGB || 1222 base_format == GL_RGBA) { 1223 return GL_TRUE; 1224 } 1225 return GL_FALSE; 1226 case GL_TEXTURE_BLUE_SIZE: 1227 case GL_TEXTURE_BLUE_TYPE: 1228 case GL_RENDERBUFFER_BLUE_SIZE_EXT: 1229 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE: 1230 if (base_format == GL_RGB || 1231 base_format == GL_RGBA) { 1232 return GL_TRUE; 1233 } 1234 return GL_FALSE; 1235 case GL_TEXTURE_ALPHA_SIZE: 1236 case GL_TEXTURE_ALPHA_TYPE: 1237 case GL_RENDERBUFFER_ALPHA_SIZE_EXT: 1238 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE: 1239 if (base_format == GL_RGBA || 1240 base_format == GL_ALPHA || 1241 base_format == GL_LUMINANCE_ALPHA) { 1242 return GL_TRUE; 1243 } 1244 return GL_FALSE; 1245 case GL_TEXTURE_LUMINANCE_SIZE: 1246 case GL_TEXTURE_LUMINANCE_TYPE: 1247 if (base_format == GL_LUMINANCE || 1248 base_format == GL_LUMINANCE_ALPHA) { 1249 return GL_TRUE; 1250 } 1251 return GL_FALSE; 1252 case GL_TEXTURE_INTENSITY_SIZE: 1253 case GL_TEXTURE_INTENSITY_TYPE: 1254 if (base_format == GL_INTENSITY) { 1255 return GL_TRUE; 1256 } 1257 return GL_FALSE; 1258 case GL_TEXTURE_DEPTH_SIZE: 1259 case GL_TEXTURE_DEPTH_TYPE: 1260 case GL_RENDERBUFFER_DEPTH_SIZE_EXT: 1261 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE: 1262 if (base_format == GL_DEPTH_STENCIL || 1263 base_format == GL_DEPTH_COMPONENT) { 1264 return GL_TRUE; 1265 } 1266 return GL_FALSE; 1267 case GL_RENDERBUFFER_STENCIL_SIZE_EXT: 1268 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE: 1269 if (base_format == GL_DEPTH_STENCIL || 1270 base_format == GL_STENCIL_INDEX) { 1271 return GL_TRUE; 1272 } 1273 return GL_FALSE; 1274 default: 1275 _mesa_warning(NULL, "%s: Unexpected channel token 0x%x\n", 1276 __FUNCTION__, pname); 1277 return GL_FALSE; 1278 } 1279 1280 return GL_FALSE; 1281} 1282 1283 1284/** 1285 * Return the byte offset of a specific pixel in an image (1D, 2D or 3D). 1286 * 1287 * Pixel unpacking/packing parameters are observed according to \p packing. 1288 * 1289 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image 1290 * \param packing the pixelstore attributes 1291 * \param width the image width 1292 * \param height the image height 1293 * \param format the pixel format (must be validated beforehand) 1294 * \param type the pixel data type (must be validated beforehand) 1295 * \param img which image in the volume (0 for 1D or 2D images) 1296 * \param row row of pixel in the image (0 for 1D images) 1297 * \param column column of pixel in the image 1298 * 1299 * \return offset of pixel. 1300 * 1301 * \sa gl_pixelstore_attrib. 1302 */ 1303GLintptr 1304_mesa_image_offset( GLuint dimensions, 1305 const struct gl_pixelstore_attrib *packing, 1306 GLsizei width, GLsizei height, 1307 GLenum format, GLenum type, 1308 GLint img, GLint row, GLint column ) 1309{ 1310 GLint alignment; /* 1, 2 or 4 */ 1311 GLint pixels_per_row; 1312 GLint rows_per_image; 1313 GLint skiprows; 1314 GLint skippixels; 1315 GLint skipimages; /* for 3-D volume images */ 1316 GLintptr offset; 1317 1318 ASSERT(dimensions >= 1 && dimensions <= 3); 1319 1320 alignment = packing->Alignment; 1321 if (packing->RowLength > 0) { 1322 pixels_per_row = packing->RowLength; 1323 } 1324 else { 1325 pixels_per_row = width; 1326 } 1327 if (packing->ImageHeight > 0) { 1328 rows_per_image = packing->ImageHeight; 1329 } 1330 else { 1331 rows_per_image = height; 1332 } 1333 1334 skippixels = packing->SkipPixels; 1335 /* Note: SKIP_ROWS _is_ used for 1D images */ 1336 skiprows = packing->SkipRows; 1337 /* Note: SKIP_IMAGES is only used for 3D images */ 1338 skipimages = (dimensions == 3) ? packing->SkipImages : 0; 1339 1340 if (type == GL_BITMAP) { 1341 /* BITMAP data */ 1342 GLint bytes_per_row; 1343 GLint bytes_per_image; 1344 /* components per pixel for color or stencil index: */ 1345 const GLint comp_per_pixel = 1; 1346 1347 /* The pixel type and format should have been error checked earlier */ 1348 assert(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX); 1349 1350 bytes_per_row = alignment 1351 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment ); 1352 1353 bytes_per_image = bytes_per_row * rows_per_image; 1354 1355 offset = (skipimages + img) * bytes_per_image 1356 + (skiprows + row) * bytes_per_row 1357 + (skippixels + column) / 8; 1358 } 1359 else { 1360 /* Non-BITMAP data */ 1361 GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image; 1362 GLint topOfImage; 1363 1364 bytes_per_pixel = _mesa_bytes_per_pixel( format, type ); 1365 1366 /* The pixel type and format should have been error checked earlier */ 1367 assert(bytes_per_pixel > 0); 1368 1369 bytes_per_row = pixels_per_row * bytes_per_pixel; 1370 remainder = bytes_per_row % alignment; 1371 if (remainder > 0) 1372 bytes_per_row += (alignment - remainder); 1373 1374 ASSERT(bytes_per_row % alignment == 0); 1375 1376 bytes_per_image = bytes_per_row * rows_per_image; 1377 1378 if (packing->Invert) { 1379 /* set pixel_addr to the last row */ 1380 topOfImage = bytes_per_row * (height - 1); 1381 bytes_per_row = -bytes_per_row; 1382 } 1383 else { 1384 topOfImage = 0; 1385 } 1386 1387 /* compute final pixel address */ 1388 offset = (skipimages + img) * bytes_per_image 1389 + topOfImage 1390 + (skiprows + row) * bytes_per_row 1391 + (skippixels + column) * bytes_per_pixel; 1392 } 1393 1394 return offset; 1395} 1396 1397 1398/** 1399 * Return the address of a specific pixel in an image (1D, 2D or 3D). 1400 * 1401 * Pixel unpacking/packing parameters are observed according to \p packing. 1402 * 1403 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image 1404 * \param packing the pixelstore attributes 1405 * \param image starting address of image data 1406 * \param width the image width 1407 * \param height the image height 1408 * \param format the pixel format (must be validated beforehand) 1409 * \param type the pixel data type (must be validated beforehand) 1410 * \param img which image in the volume (0 for 1D or 2D images) 1411 * \param row row of pixel in the image (0 for 1D images) 1412 * \param column column of pixel in the image 1413 * 1414 * \return address of pixel. 1415 * 1416 * \sa gl_pixelstore_attrib. 1417 */ 1418GLvoid * 1419_mesa_image_address( GLuint dimensions, 1420 const struct gl_pixelstore_attrib *packing, 1421 const GLvoid *image, 1422 GLsizei width, GLsizei height, 1423 GLenum format, GLenum type, 1424 GLint img, GLint row, GLint column ) 1425{ 1426 const GLubyte *addr = (const GLubyte *) image; 1427 1428 addr += _mesa_image_offset(dimensions, packing, width, height, 1429 format, type, img, row, column); 1430 1431 return (GLvoid *) addr; 1432} 1433 1434 1435GLvoid * 1436_mesa_image_address1d( const struct gl_pixelstore_attrib *packing, 1437 const GLvoid *image, 1438 GLsizei width, 1439 GLenum format, GLenum type, 1440 GLint column ) 1441{ 1442 return _mesa_image_address(1, packing, image, width, 1, 1443 format, type, 0, 0, column); 1444} 1445 1446 1447GLvoid * 1448_mesa_image_address2d( const struct gl_pixelstore_attrib *packing, 1449 const GLvoid *image, 1450 GLsizei width, GLsizei height, 1451 GLenum format, GLenum type, 1452 GLint row, GLint column ) 1453{ 1454 return _mesa_image_address(2, packing, image, width, height, 1455 format, type, 0, row, column); 1456} 1457 1458 1459GLvoid * 1460_mesa_image_address3d( const struct gl_pixelstore_attrib *packing, 1461 const GLvoid *image, 1462 GLsizei width, GLsizei height, 1463 GLenum format, GLenum type, 1464 GLint img, GLint row, GLint column ) 1465{ 1466 return _mesa_image_address(3, packing, image, width, height, 1467 format, type, img, row, column); 1468} 1469 1470 1471 1472/** 1473 * Compute the stride (in bytes) between image rows. 1474 * 1475 * \param packing the pixelstore attributes 1476 * \param width image width. 1477 * \param format pixel format. 1478 * \param type pixel data type. 1479 * 1480 * \return the stride in bytes for the given parameters, or -1 if error 1481 */ 1482GLint 1483_mesa_image_row_stride( const struct gl_pixelstore_attrib *packing, 1484 GLint width, GLenum format, GLenum type ) 1485{ 1486 GLint bytesPerRow, remainder; 1487 1488 ASSERT(packing); 1489 1490 if (type == GL_BITMAP) { 1491 if (packing->RowLength == 0) { 1492 bytesPerRow = (width + 7) / 8; 1493 } 1494 else { 1495 bytesPerRow = (packing->RowLength + 7) / 8; 1496 } 1497 } 1498 else { 1499 /* Non-BITMAP data */ 1500 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type); 1501 if (bytesPerPixel <= 0) 1502 return -1; /* error */ 1503 if (packing->RowLength == 0) { 1504 bytesPerRow = bytesPerPixel * width; 1505 } 1506 else { 1507 bytesPerRow = bytesPerPixel * packing->RowLength; 1508 } 1509 } 1510 1511 remainder = bytesPerRow % packing->Alignment; 1512 if (remainder > 0) { 1513 bytesPerRow += (packing->Alignment - remainder); 1514 } 1515 1516 if (packing->Invert) { 1517 /* negate the bytes per row (negative row stride) */ 1518 bytesPerRow = -bytesPerRow; 1519 } 1520 1521 return bytesPerRow; 1522} 1523 1524 1525/* 1526 * Compute the stride between images in a 3D texture (in bytes) for the given 1527 * pixel packing parameters and image width, format and type. 1528 */ 1529GLint 1530_mesa_image_image_stride( const struct gl_pixelstore_attrib *packing, 1531 GLint width, GLint height, 1532 GLenum format, GLenum type ) 1533{ 1534 GLint bytesPerRow, bytesPerImage, remainder; 1535 1536 ASSERT(packing); 1537 1538 if (type == GL_BITMAP) { 1539 if (packing->RowLength == 0) { 1540 bytesPerRow = (width + 7) / 8; 1541 } 1542 else { 1543 bytesPerRow = (packing->RowLength + 7) / 8; 1544 } 1545 } 1546 else { 1547 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type); 1548 1549 if (bytesPerPixel <= 0) 1550 return -1; /* error */ 1551 if (packing->RowLength == 0) { 1552 bytesPerRow = bytesPerPixel * width; 1553 } 1554 else { 1555 bytesPerRow = bytesPerPixel * packing->RowLength; 1556 } 1557 } 1558 1559 remainder = bytesPerRow % packing->Alignment; 1560 if (remainder > 0) 1561 bytesPerRow += (packing->Alignment - remainder); 1562 1563 if (packing->ImageHeight == 0) 1564 bytesPerImage = bytesPerRow * height; 1565 else 1566 bytesPerImage = bytesPerRow * packing->ImageHeight; 1567 1568 return bytesPerImage; 1569} 1570 1571 1572 1573/** 1574 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel. 1575 * This is typically used to convert a bitmap into a GLubyte/pixel texture. 1576 * "On" bits will set texels to \p onValue. 1577 * "Off" bits will not modify texels. 1578 * \param width src bitmap width in pixels 1579 * \param height src bitmap height in pixels 1580 * \param unpack bitmap unpacking state 1581 * \param bitmap the src bitmap data 1582 * \param destBuffer start of dest buffer 1583 * \param destStride row stride in dest buffer 1584 * \param onValue if bit is 1, set destBuffer pixel to this value 1585 */ 1586void 1587_mesa_expand_bitmap(GLsizei width, GLsizei height, 1588 const struct gl_pixelstore_attrib *unpack, 1589 const GLubyte *bitmap, 1590 GLubyte *destBuffer, GLint destStride, 1591 GLubyte onValue) 1592{ 1593 const GLubyte *srcRow = (const GLubyte *) 1594 _mesa_image_address2d(unpack, bitmap, width, height, 1595 GL_COLOR_INDEX, GL_BITMAP, 0, 0); 1596 const GLint srcStride = _mesa_image_row_stride(unpack, width, 1597 GL_COLOR_INDEX, GL_BITMAP); 1598 GLint row, col; 1599 1600#define SET_PIXEL(COL, ROW) \ 1601 destBuffer[(ROW) * destStride + (COL)] = onValue; 1602 1603 for (row = 0; row < height; row++) { 1604 const GLubyte *src = srcRow; 1605 1606 if (unpack->LsbFirst) { 1607 /* Lsb first */ 1608 GLubyte mask = 1U << (unpack->SkipPixels & 0x7); 1609 for (col = 0; col < width; col++) { 1610 1611 if (*src & mask) { 1612 SET_PIXEL(col, row); 1613 } 1614 1615 if (mask == 128U) { 1616 src++; 1617 mask = 1U; 1618 } 1619 else { 1620 mask = mask << 1; 1621 } 1622 } 1623 1624 /* get ready for next row */ 1625 if (mask != 1) 1626 src++; 1627 } 1628 else { 1629 /* Msb first */ 1630 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7); 1631 for (col = 0; col < width; col++) { 1632 1633 if (*src & mask) { 1634 SET_PIXEL(col, row); 1635 } 1636 1637 if (mask == 1U) { 1638 src++; 1639 mask = 128U; 1640 } 1641 else { 1642 mask = mask >> 1; 1643 } 1644 } 1645 1646 /* get ready for next row */ 1647 if (mask != 128) 1648 src++; 1649 } 1650 1651 srcRow += srcStride; 1652 } /* row */ 1653 1654#undef SET_PIXEL 1655} 1656 1657 1658 1659 1660/** 1661 * Convert an array of RGBA colors from one datatype to another. 1662 * NOTE: src may equal dst. In that case, we use a temporary buffer. 1663 */ 1664void 1665_mesa_convert_colors(GLenum srcType, const GLvoid *src, 1666 GLenum dstType, GLvoid *dst, 1667 GLuint count, const GLubyte mask[]) 1668{ 1669 GLuint *tempBuffer; 1670 const GLboolean useTemp = (src == dst); 1671 1672 tempBuffer = malloc(count * MAX_PIXEL_BYTES); 1673 if (!tempBuffer) 1674 return; 1675 1676 ASSERT(srcType != dstType); 1677 1678 switch (srcType) { 1679 case GL_UNSIGNED_BYTE: 1680 if (dstType == GL_UNSIGNED_SHORT) { 1681 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src; 1682 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst); 1683 GLuint i; 1684 for (i = 0; i < count; i++) { 1685 if (!mask || mask[i]) { 1686 dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]); 1687 dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]); 1688 dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]); 1689 dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]); 1690 } 1691 } 1692 if (useTemp) 1693 memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort)); 1694 } 1695 else { 1696 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src; 1697 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst); 1698 GLuint i; 1699 ASSERT(dstType == GL_FLOAT); 1700 for (i = 0; i < count; i++) { 1701 if (!mask || mask[i]) { 1702 dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]); 1703 dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]); 1704 dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]); 1705 dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]); 1706 } 1707 } 1708 if (useTemp) 1709 memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat)); 1710 } 1711 break; 1712 case GL_UNSIGNED_SHORT: 1713 if (dstType == GL_UNSIGNED_BYTE) { 1714 const GLushort (*src2)[4] = (const GLushort (*)[4]) src; 1715 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst); 1716 GLuint i; 1717 for (i = 0; i < count; i++) { 1718 if (!mask || mask[i]) { 1719 dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]); 1720 dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]); 1721 dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]); 1722 dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]); 1723 } 1724 } 1725 if (useTemp) 1726 memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte)); 1727 } 1728 else { 1729 const GLushort (*src2)[4] = (const GLushort (*)[4]) src; 1730 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst); 1731 GLuint i; 1732 ASSERT(dstType == GL_FLOAT); 1733 for (i = 0; i < count; i++) { 1734 if (!mask || mask[i]) { 1735 dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]); 1736 dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]); 1737 dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]); 1738 dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]); 1739 } 1740 } 1741 if (useTemp) 1742 memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat)); 1743 } 1744 break; 1745 case GL_FLOAT: 1746 if (dstType == GL_UNSIGNED_BYTE) { 1747 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src; 1748 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst); 1749 GLuint i; 1750 for (i = 0; i < count; i++) { 1751 if (!mask || mask[i]) 1752 _mesa_unclamped_float_rgba_to_ubyte(dst1[i], src4[i]); 1753 } 1754 if (useTemp) 1755 memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte)); 1756 } 1757 else { 1758 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src; 1759 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst); 1760 GLuint i; 1761 ASSERT(dstType == GL_UNSIGNED_SHORT); 1762 for (i = 0; i < count; i++) { 1763 if (!mask || mask[i]) { 1764 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]); 1765 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]); 1766 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]); 1767 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]); 1768 } 1769 } 1770 if (useTemp) 1771 memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort)); 1772 } 1773 break; 1774 default: 1775 _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors"); 1776 } 1777 1778 free(tempBuffer); 1779} 1780 1781 1782 1783 1784/** 1785 * Perform basic clipping for glDrawPixels. The image's position and size 1786 * and the unpack SkipPixels and SkipRows are adjusted so that the image 1787 * region is entirely within the window and scissor bounds. 1788 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1). 1789 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which 1790 * we'll actually write. Beforehand, *destY-1 is the first drawing row. 1791 * 1792 * \return GL_TRUE if image is ready for drawing or 1793 * GL_FALSE if image was completely clipped away (draw nothing) 1794 */ 1795GLboolean 1796_mesa_clip_drawpixels(const struct gl_context *ctx, 1797 GLint *destX, GLint *destY, 1798 GLsizei *width, GLsizei *height, 1799 struct gl_pixelstore_attrib *unpack) 1800{ 1801 const struct gl_framebuffer *buffer = ctx->DrawBuffer; 1802 1803 if (unpack->RowLength == 0) { 1804 unpack->RowLength = *width; 1805 } 1806 1807 ASSERT(ctx->Pixel.ZoomX == 1.0F); 1808 ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F); 1809 1810 /* left clipping */ 1811 if (*destX < buffer->_Xmin) { 1812 unpack->SkipPixels += (buffer->_Xmin - *destX); 1813 *width -= (buffer->_Xmin - *destX); 1814 *destX = buffer->_Xmin; 1815 } 1816 /* right clipping */ 1817 if (*destX + *width > buffer->_Xmax) 1818 *width -= (*destX + *width - buffer->_Xmax); 1819 1820 if (*width <= 0) 1821 return GL_FALSE; 1822 1823 if (ctx->Pixel.ZoomY == 1.0F) { 1824 /* bottom clipping */ 1825 if (*destY < buffer->_Ymin) { 1826 unpack->SkipRows += (buffer->_Ymin - *destY); 1827 *height -= (buffer->_Ymin - *destY); 1828 *destY = buffer->_Ymin; 1829 } 1830 /* top clipping */ 1831 if (*destY + *height > buffer->_Ymax) 1832 *height -= (*destY + *height - buffer->_Ymax); 1833 } 1834 else { /* upside down */ 1835 /* top clipping */ 1836 if (*destY > buffer->_Ymax) { 1837 unpack->SkipRows += (*destY - buffer->_Ymax); 1838 *height -= (*destY - buffer->_Ymax); 1839 *destY = buffer->_Ymax; 1840 } 1841 /* bottom clipping */ 1842 if (*destY - *height < buffer->_Ymin) 1843 *height -= (buffer->_Ymin - (*destY - *height)); 1844 /* adjust destY so it's the first row to write to */ 1845 (*destY)--; 1846 } 1847 1848 if (*height <= 0) 1849 return GL_FALSE; 1850 1851 return GL_TRUE; 1852} 1853 1854 1855/** 1856 * Perform clipping for glReadPixels. The image's window position 1857 * and size, and the pack skipPixels, skipRows and rowLength are adjusted 1858 * so that the image region is entirely within the window bounds. 1859 * Note: this is different from _mesa_clip_drawpixels() in that the 1860 * scissor box is ignored, and we use the bounds of the current readbuffer 1861 * surface. 1862 * 1863 * \return GL_TRUE if region to read is in bounds 1864 * GL_FALSE if region is completely out of bounds (nothing to read) 1865 */ 1866GLboolean 1867_mesa_clip_readpixels(const struct gl_context *ctx, 1868 GLint *srcX, GLint *srcY, 1869 GLsizei *width, GLsizei *height, 1870 struct gl_pixelstore_attrib *pack) 1871{ 1872 const struct gl_framebuffer *buffer = ctx->ReadBuffer; 1873 1874 if (pack->RowLength == 0) { 1875 pack->RowLength = *width; 1876 } 1877 1878 /* left clipping */ 1879 if (*srcX < 0) { 1880 pack->SkipPixels += (0 - *srcX); 1881 *width -= (0 - *srcX); 1882 *srcX = 0; 1883 } 1884 /* right clipping */ 1885 if (*srcX + *width > (GLsizei) buffer->Width) 1886 *width -= (*srcX + *width - buffer->Width); 1887 1888 if (*width <= 0) 1889 return GL_FALSE; 1890 1891 /* bottom clipping */ 1892 if (*srcY < 0) { 1893 pack->SkipRows += (0 - *srcY); 1894 *height -= (0 - *srcY); 1895 *srcY = 0; 1896 } 1897 /* top clipping */ 1898 if (*srcY + *height > (GLsizei) buffer->Height) 1899 *height -= (*srcY + *height - buffer->Height); 1900 1901 if (*height <= 0) 1902 return GL_FALSE; 1903 1904 return GL_TRUE; 1905} 1906 1907 1908/** 1909 * Do clipping for a glCopyTexSubImage call. 1910 * The framebuffer source region might extend outside the framebuffer 1911 * bounds. Clip the source region against the framebuffer bounds and 1912 * adjust the texture/dest position and size accordingly. 1913 * 1914 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise. 1915 */ 1916GLboolean 1917_mesa_clip_copytexsubimage(const struct gl_context *ctx, 1918 GLint *destX, GLint *destY, 1919 GLint *srcX, GLint *srcY, 1920 GLsizei *width, GLsizei *height) 1921{ 1922 const struct gl_framebuffer *fb = ctx->ReadBuffer; 1923 const GLint srcX0 = *srcX, srcY0 = *srcY; 1924 1925 if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height, 1926 srcX, srcY, width, height)) { 1927 *destX = *destX + *srcX - srcX0; 1928 *destY = *destY + *srcY - srcY0; 1929 1930 return GL_TRUE; 1931 } 1932 else { 1933 return GL_FALSE; 1934 } 1935} 1936 1937 1938 1939/** 1940 * Clip the rectangle defined by (x, y, width, height) against the bounds 1941 * specified by [xmin, xmax) and [ymin, ymax). 1942 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise. 1943 */ 1944GLboolean 1945_mesa_clip_to_region(GLint xmin, GLint ymin, 1946 GLint xmax, GLint ymax, 1947 GLint *x, GLint *y, 1948 GLsizei *width, GLsizei *height ) 1949{ 1950 /* left clipping */ 1951 if (*x < xmin) { 1952 *width -= (xmin - *x); 1953 *x = xmin; 1954 } 1955 1956 /* right clipping */ 1957 if (*x + *width > xmax) 1958 *width -= (*x + *width - xmax); 1959 1960 if (*width <= 0) 1961 return GL_FALSE; 1962 1963 /* bottom (or top) clipping */ 1964 if (*y < ymin) { 1965 *height -= (ymin - *y); 1966 *y = ymin; 1967 } 1968 1969 /* top (or bottom) clipping */ 1970 if (*y + *height > ymax) 1971 *height -= (*y + *height - ymax); 1972 1973 if (*height <= 0) 1974 return GL_FALSE; 1975 1976 return GL_TRUE; 1977} 1978 1979 1980/** 1981 * Clip dst coords against Xmax (or Ymax). 1982 */ 1983static inline void 1984clip_right_or_top(GLint *srcX0, GLint *srcX1, 1985 GLint *dstX0, GLint *dstX1, 1986 GLint maxValue) 1987{ 1988 GLfloat t, bias; 1989 1990 if (*dstX1 > maxValue) { 1991 /* X1 outside right edge */ 1992 ASSERT(*dstX0 < maxValue); /* X0 should be inside right edge */ 1993 t = (GLfloat) (maxValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0); 1994 /* chop off [t, 1] part */ 1995 ASSERT(t >= 0.0 && t <= 1.0); 1996 *dstX1 = maxValue; 1997 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F; 1998 *srcX1 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias); 1999 } 2000 else if (*dstX0 > maxValue) { 2001 /* X0 outside right edge */ 2002 ASSERT(*dstX1 < maxValue); /* X1 should be inside right edge */ 2003 t = (GLfloat) (maxValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1); 2004 /* chop off [t, 1] part */ 2005 ASSERT(t >= 0.0 && t <= 1.0); 2006 *dstX0 = maxValue; 2007 bias = (*srcX0 < *srcX1) ? -0.5F : 0.5F; 2008 *srcX0 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias); 2009 } 2010} 2011 2012 2013/** 2014 * Clip dst coords against Xmin (or Ymin). 2015 */ 2016static inline void 2017clip_left_or_bottom(GLint *srcX0, GLint *srcX1, 2018 GLint *dstX0, GLint *dstX1, 2019 GLint minValue) 2020{ 2021 GLfloat t, bias; 2022 2023 if (*dstX0 < minValue) { 2024 /* X0 outside left edge */ 2025 ASSERT(*dstX1 > minValue); /* X1 should be inside left edge */ 2026 t = (GLfloat) (minValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0); 2027 /* chop off [0, t] part */ 2028 ASSERT(t >= 0.0 && t <= 1.0); 2029 *dstX0 = minValue; 2030 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F; /* flipped??? */ 2031 *srcX0 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias); 2032 } 2033 else if (*dstX1 < minValue) { 2034 /* X1 outside left edge */ 2035 ASSERT(*dstX0 > minValue); /* X0 should be inside left edge */ 2036 t = (GLfloat) (minValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1); 2037 /* chop off [0, t] part */ 2038 ASSERT(t >= 0.0 && t <= 1.0); 2039 *dstX1 = minValue; 2040 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F; 2041 *srcX1 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias); 2042 } 2043} 2044 2045 2046/** 2047 * Do clipping of blit src/dest rectangles. 2048 * The dest rect is clipped against both the buffer bounds and scissor bounds. 2049 * The src rect is just clipped against the buffer bounds. 2050 * 2051 * When either the src or dest rect is clipped, the other is also clipped 2052 * proportionately! 2053 * 2054 * Note that X0 need not be less than X1 (same for Y) for either the source 2055 * and dest rects. That makes the clipping a little trickier. 2056 * 2057 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped 2058 */ 2059GLboolean 2060_mesa_clip_blit(struct gl_context *ctx, 2061 GLint *srcX0, GLint *srcY0, GLint *srcX1, GLint *srcY1, 2062 GLint *dstX0, GLint *dstY0, GLint *dstX1, GLint *dstY1) 2063{ 2064 const GLint srcXmin = 0; 2065 const GLint srcXmax = ctx->ReadBuffer->Width; 2066 const GLint srcYmin = 0; 2067 const GLint srcYmax = ctx->ReadBuffer->Height; 2068 2069 /* these include scissor bounds */ 2070 const GLint dstXmin = ctx->DrawBuffer->_Xmin; 2071 const GLint dstXmax = ctx->DrawBuffer->_Xmax; 2072 const GLint dstYmin = ctx->DrawBuffer->_Ymin; 2073 const GLint dstYmax = ctx->DrawBuffer->_Ymax; 2074 2075 /* 2076 printf("PreClipX: src: %d .. %d dst: %d .. %d\n", 2077 *srcX0, *srcX1, *dstX0, *dstX1); 2078 printf("PreClipY: src: %d .. %d dst: %d .. %d\n", 2079 *srcY0, *srcY1, *dstY0, *dstY1); 2080 */ 2081 2082 /* trivial rejection tests */ 2083 if (*dstX0 == *dstX1) 2084 return GL_FALSE; /* no width */ 2085 if (*dstX0 <= dstXmin && *dstX1 <= dstXmin) 2086 return GL_FALSE; /* totally out (left) of bounds */ 2087 if (*dstX0 >= dstXmax && *dstX1 >= dstXmax) 2088 return GL_FALSE; /* totally out (right) of bounds */ 2089 2090 if (*dstY0 == *dstY1) 2091 return GL_FALSE; 2092 if (*dstY0 <= dstYmin && *dstY1 <= dstYmin) 2093 return GL_FALSE; 2094 if (*dstY0 >= dstYmax && *dstY1 >= dstYmax) 2095 return GL_FALSE; 2096 2097 if (*srcX0 == *srcX1) 2098 return GL_FALSE; 2099 if (*srcX0 <= srcXmin && *srcX1 <= srcXmin) 2100 return GL_FALSE; 2101 if (*srcX0 >= srcXmax && *srcX1 >= srcXmax) 2102 return GL_FALSE; 2103 2104 if (*srcY0 == *srcY1) 2105 return GL_FALSE; 2106 if (*srcY0 <= srcYmin && *srcY1 <= srcYmin) 2107 return GL_FALSE; 2108 if (*srcY0 >= srcYmax && *srcY1 >= srcYmax) 2109 return GL_FALSE; 2110 2111 /* 2112 * dest clip 2113 */ 2114 clip_right_or_top(srcX0, srcX1, dstX0, dstX1, dstXmax); 2115 clip_right_or_top(srcY0, srcY1, dstY0, dstY1, dstYmax); 2116 clip_left_or_bottom(srcX0, srcX1, dstX0, dstX1, dstXmin); 2117 clip_left_or_bottom(srcY0, srcY1, dstY0, dstY1, dstYmin); 2118 2119 /* 2120 * src clip (just swap src/dst values from above) 2121 */ 2122 clip_right_or_top(dstX0, dstX1, srcX0, srcX1, srcXmax); 2123 clip_right_or_top(dstY0, dstY1, srcY0, srcY1, srcYmax); 2124 clip_left_or_bottom(dstX0, dstX1, srcX0, srcX1, srcXmin); 2125 clip_left_or_bottom(dstY0, dstY1, srcY0, srcY1, srcYmin); 2126 2127 /* 2128 printf("PostClipX: src: %d .. %d dst: %d .. %d\n", 2129 *srcX0, *srcX1, *dstX0, *dstX1); 2130 printf("PostClipY: src: %d .. %d dst: %d .. %d\n", 2131 *srcY0, *srcY1, *dstY0, *dstY1); 2132 */ 2133 2134 ASSERT(*dstX0 >= dstXmin); 2135 ASSERT(*dstX0 <= dstXmax); 2136 ASSERT(*dstX1 >= dstXmin); 2137 ASSERT(*dstX1 <= dstXmax); 2138 2139 ASSERT(*dstY0 >= dstYmin); 2140 ASSERT(*dstY0 <= dstYmax); 2141 ASSERT(*dstY1 >= dstYmin); 2142 ASSERT(*dstY1 <= dstYmax); 2143 2144 ASSERT(*srcX0 >= srcXmin); 2145 ASSERT(*srcX0 <= srcXmax); 2146 ASSERT(*srcX1 >= srcXmin); 2147 ASSERT(*srcX1 <= srcXmax); 2148 2149 ASSERT(*srcY0 >= srcYmin); 2150 ASSERT(*srcY0 <= srcYmax); 2151 ASSERT(*srcY1 >= srcYmin); 2152 ASSERT(*srcY1 <= srcYmax); 2153 2154 return GL_TRUE; 2155} 2156