st_cb_texture.c revision 1ad2484f03cbe9ae6bd4ebe50d6894e570d65952
1/************************************************************************** 2 * 3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * 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 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include "main/mfeatures.h" 29#if FEATURE_convolve 30#include "main/convolve.h" 31#endif 32#include "main/enums.h" 33#include "main/image.h" 34#include "main/imports.h" 35#include "main/macros.h" 36#include "main/mipmap.h" 37#include "main/pixel.h" 38#include "main/texcompress.h" 39#include "main/texformat.h" 40#include "main/texgetimage.h" 41#include "main/teximage.h" 42#include "main/texobj.h" 43#include "main/texstore.h" 44 45#include "state_tracker/st_context.h" 46#include "state_tracker/st_cb_fbo.h" 47#include "state_tracker/st_cb_texture.h" 48#include "state_tracker/st_format.h" 49#include "state_tracker/st_public.h" 50#include "state_tracker/st_texture.h" 51#include "state_tracker/st_gen_mipmap.h" 52 53#include "pipe/p_context.h" 54#include "pipe/p_defines.h" 55#include "pipe/p_inlines.h" 56#include "util/u_tile.h" 57#include "util/u_blit.h" 58 59 60#define DBG if (0) printf 61 62 63static enum pipe_texture_target 64gl_target_to_pipe(GLenum target) 65{ 66 switch (target) { 67 case GL_TEXTURE_1D: 68 return PIPE_TEXTURE_1D; 69 70 case GL_TEXTURE_2D: 71 case GL_TEXTURE_RECTANGLE_NV: 72 return PIPE_TEXTURE_2D; 73 74 case GL_TEXTURE_3D: 75 return PIPE_TEXTURE_3D; 76 77 case GL_TEXTURE_CUBE_MAP_ARB: 78 return PIPE_TEXTURE_CUBE; 79 80 default: 81 assert(0); 82 return 0; 83 } 84} 85 86 87/** 88 * Return nominal bytes per texel for a compressed format, 0 for non-compressed 89 * format. 90 */ 91static GLuint 92compressed_num_bytes(GLuint mesaFormat) 93{ 94 switch(mesaFormat) { 95#if FEATURE_texture_fxt1 96 case MESA_FORMAT_RGB_FXT1: 97 case MESA_FORMAT_RGBA_FXT1: 98#endif 99#if FEATURE_texture_s3tc 100 case MESA_FORMAT_RGB_DXT1: 101 case MESA_FORMAT_RGBA_DXT1: 102 return 2; 103 case MESA_FORMAT_RGBA_DXT3: 104 case MESA_FORMAT_RGBA_DXT5: 105 return 4; 106#endif 107 default: 108 return 0; 109 } 110} 111 112 113/** called via ctx->Driver.NewTextureImage() */ 114static struct gl_texture_image * 115st_NewTextureImage(GLcontext * ctx) 116{ 117 DBG("%s\n", __FUNCTION__); 118 (void) ctx; 119 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image); 120} 121 122 123/** called via ctx->Driver.NewTextureObject() */ 124static struct gl_texture_object * 125st_NewTextureObject(GLcontext * ctx, GLuint name, GLenum target) 126{ 127 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object); 128 129 DBG("%s\n", __FUNCTION__); 130 _mesa_initialize_texture_object(&obj->base, name, target); 131 132 return &obj->base; 133} 134 135/** called via ctx->Driver.DeleteTextureImage() */ 136static void 137st_DeleteTextureObject(GLcontext *ctx, 138 struct gl_texture_object *texObj) 139{ 140 struct st_texture_object *stObj = st_texture_object(texObj); 141 if (stObj->pt) 142 pipe_texture_reference(&stObj->pt, NULL); 143 144 _mesa_delete_texture_object(ctx, texObj); 145} 146 147 148/** called via ctx->Driver.FreeTexImageData() */ 149static void 150st_FreeTextureImageData(GLcontext * ctx, struct gl_texture_image *texImage) 151{ 152 struct st_texture_image *stImage = st_texture_image(texImage); 153 154 DBG("%s\n", __FUNCTION__); 155 156 if (stImage->pt) { 157 pipe_texture_reference(&stImage->pt, NULL); 158 } 159 160 if (texImage->Data) { 161 _mesa_align_free(texImage->Data); 162 texImage->Data = NULL; 163 } 164} 165 166 167/** 168 * From linux kernel i386 header files, copes with odd sizes better 169 * than COPY_DWORDS would: 170 * XXX Put this in src/mesa/main/imports.h ??? 171 */ 172#if defined(i386) || defined(__i386__) 173static INLINE void * 174__memcpy(void *to, const void *from, size_t n) 175{ 176 int d0, d1, d2; 177 __asm__ __volatile__("rep ; movsl\n\t" 178 "testb $2,%b4\n\t" 179 "je 1f\n\t" 180 "movsw\n" 181 "1:\ttestb $1,%b4\n\t" 182 "je 2f\n\t" 183 "movsb\n" "2:":"=&c"(d0), "=&D"(d1), "=&S"(d2) 184 :"0"(n / 4), "q"(n), "1"((long) to), "2"((long) from) 185 :"memory"); 186 return (to); 187} 188#else 189#define __memcpy(a,b,c) memcpy(a,b,c) 190#endif 191 192 193/** 194 * The system memcpy (at least on ubuntu 5.10) has problems copying 195 * to agp (writecombined) memory from a source which isn't 64-byte 196 * aligned - there is a 4x performance falloff. 197 * 198 * The x86 __memcpy is immune to this but is slightly slower 199 * (10%-ish) than the system memcpy. 200 * 201 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but 202 * isn't much faster than x86_memcpy for agp copies. 203 * 204 * TODO: switch dynamically. 205 */ 206static void * 207do_memcpy(void *dest, const void *src, size_t n) 208{ 209 if ((((unsigned long) src) & 63) || (((unsigned long) dest) & 63)) { 210 return __memcpy(dest, src, n); 211 } 212 else 213 return memcpy(dest, src, n); 214} 215 216 217static int 218logbase2(int n) 219{ 220 GLint i = 1, log2 = 0; 221 while (n > i) { 222 i *= 2; 223 log2++; 224 } 225 return log2; 226} 227 228 229/** 230 * Return default texture usage bitmask for the given texture format. 231 */ 232static GLuint 233default_usage(enum pipe_format fmt) 234{ 235 GLuint usage = PIPE_TEXTURE_USAGE_SAMPLER; 236 if (pf_is_depth_stencil(fmt)) 237 usage |= PIPE_TEXTURE_USAGE_DEPTH_STENCIL; 238 else 239 usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET; 240 return usage; 241} 242 243 244/** 245 * Allocate a pipe_texture object for the given st_texture_object using 246 * the given st_texture_image to guess the mipmap size/levels. 247 * 248 * [comments...] 249 * Otherwise, store it in memory if (Border != 0) or (any dimension == 250 * 1). 251 * 252 * Otherwise, if max_level >= level >= min_level, create texture with 253 * space for images from min_level down to max_level. 254 * 255 * Otherwise, create texture with space for images from (level 0)..(1x1). 256 * Consider pruning this texture at a validation if the saving is worth it. 257 */ 258static void 259guess_and_alloc_texture(struct st_context *st, 260 struct st_texture_object *stObj, 261 const struct st_texture_image *stImage) 262{ 263 GLuint firstLevel; 264 GLuint lastLevel; 265 GLuint width = stImage->base.Width2; /* size w/out border */ 266 GLuint height = stImage->base.Height2; 267 GLuint depth = stImage->base.Depth2; 268 GLuint i, comp_byte = 0, usage; 269 enum pipe_format fmt; 270 271 DBG("%s\n", __FUNCTION__); 272 273 assert(!stObj->pt); 274 275 if (stObj->pt && 276 (GLint) stImage->level > stObj->base.BaseLevel && 277 (stImage->base.Width == 1 || 278 (stObj->base.Target != GL_TEXTURE_1D && 279 stImage->base.Height == 1) || 280 (stObj->base.Target == GL_TEXTURE_3D && 281 stImage->base.Depth == 1))) 282 return; 283 284 /* If this image disrespects BaseLevel, allocate from level zero. 285 * Usually BaseLevel == 0, so it's unlikely to happen. 286 */ 287 if ((GLint) stImage->level < stObj->base.BaseLevel) 288 firstLevel = 0; 289 else 290 firstLevel = stObj->base.BaseLevel; 291 292 293 /* Figure out image dimensions at start level. 294 */ 295 for (i = stImage->level; i > firstLevel; i--) { 296 if (width != 1) 297 width <<= 1; 298 if (height != 1) 299 height <<= 1; 300 if (depth != 1) 301 depth <<= 1; 302 } 303 304 if (width == 0 || height == 0 || depth == 0) { 305 /* no texture needed */ 306 return; 307 } 308 309 /* Guess a reasonable value for lastLevel. This is probably going 310 * to be wrong fairly often and might mean that we have to look at 311 * resizable buffers, or require that buffers implement lazy 312 * pagetable arrangements. 313 */ 314 if ((stObj->base.MinFilter == GL_NEAREST || 315 stObj->base.MinFilter == GL_LINEAR) && 316 stImage->level == firstLevel) { 317 lastLevel = firstLevel; 318 } 319 else { 320 GLuint l2width = logbase2(width); 321 GLuint l2height = logbase2(height); 322 GLuint l2depth = logbase2(depth); 323 lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth); 324 } 325 326 if (stImage->base.IsCompressed) 327 comp_byte = compressed_num_bytes(stImage->base.TexFormat->MesaFormat); 328 329 fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat->MesaFormat); 330 331 usage = default_usage(fmt); 332 333 stObj->pt = st_texture_create(st, 334 gl_target_to_pipe(stObj->base.Target), 335 fmt, 336 lastLevel, 337 width, 338 height, 339 depth, 340 comp_byte, 341 usage); 342 343 DBG("%s - success\n", __FUNCTION__); 344} 345 346 347/** 348 * Adjust pixel unpack params and image dimensions to strip off the 349 * texture border. 350 * Gallium doesn't support texture borders. They've seldem been used 351 * and seldom been implemented correctly anyway. 352 * \param unpackNew returns the new pixel unpack parameters 353 */ 354static void 355strip_texture_border(GLint border, 356 GLint *width, GLint *height, GLint *depth, 357 const struct gl_pixelstore_attrib *unpack, 358 struct gl_pixelstore_attrib *unpackNew) 359{ 360 assert(border > 0); /* sanity check */ 361 362 *unpackNew = *unpack; 363 364 if (unpackNew->RowLength == 0) 365 unpackNew->RowLength = *width; 366 367 if (depth && unpackNew->ImageHeight == 0) 368 unpackNew->ImageHeight = *height; 369 370 unpackNew->SkipPixels += border; 371 if (height) 372 unpackNew->SkipRows += border; 373 if (depth) 374 unpackNew->SkipImages += border; 375 376 assert(*width >= 3); 377 *width = *width - 2 * border; 378 if (height && *height >= 3) 379 *height = *height - 2 * border; 380 if (depth && *depth >= 3) 381 *depth = *depth - 2 * border; 382} 383 384 385/** 386 * Do glTexImage1/2/3D(). 387 */ 388static void 389st_TexImage(GLcontext * ctx, 390 GLint dims, 391 GLenum target, GLint level, 392 GLint internalFormat, 393 GLint width, GLint height, GLint depth, 394 GLint border, 395 GLenum format, GLenum type, const void *pixels, 396 const struct gl_pixelstore_attrib *unpack, 397 struct gl_texture_object *texObj, 398 struct gl_texture_image *texImage, 399 GLsizei imageSize, GLboolean compressed) 400{ 401 struct st_texture_object *stObj = st_texture_object(texObj); 402 struct st_texture_image *stImage = st_texture_image(texImage); 403 GLint postConvWidth, postConvHeight; 404 GLint texelBytes, sizeInBytes; 405 GLuint dstRowStride; 406 struct gl_pixelstore_attrib unpackNB; 407 408 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__, 409 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border); 410 411 /* gallium does not support texture borders, strip it off */ 412 if (border) { 413 strip_texture_border(border, &width, &height, &depth, unpack, &unpackNB); 414 unpack = &unpackNB; 415 texImage->Width = width; 416 texImage->Height = height; 417 texImage->Depth = depth; 418 texImage->Border = 0; 419 border = 0; 420 } 421 422 postConvWidth = width; 423 postConvHeight = height; 424 425 stImage->face = _mesa_tex_target_to_face(target); 426 stImage->level = level; 427 428#if FEATURE_convolve 429 if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) { 430 _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth, 431 &postConvHeight); 432 } 433#endif 434 435 /* choose the texture format */ 436 texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat, 437 format, type); 438 439 _mesa_set_fetch_functions(texImage, dims); 440 441 if (texImage->TexFormat->TexelBytes == 0) { 442 /* must be a compressed format */ 443 texelBytes = 0; 444 texImage->IsCompressed = GL_TRUE; 445 texImage->CompressedSize = 446 ctx->Driver.CompressedTextureSize(ctx, texImage->Width, 447 texImage->Height, texImage->Depth, 448 texImage->TexFormat->MesaFormat); 449 } 450 else { 451 texelBytes = texImage->TexFormat->TexelBytes; 452 453 /* Minimum pitch of 32 bytes */ 454 if (postConvWidth * texelBytes < 32) { 455 postConvWidth = 32 / texelBytes; 456 texImage->RowStride = postConvWidth; 457 } 458 459 /* we'll set RowStride elsewhere when the texture is a "mapped" state */ 460 /*assert(texImage->RowStride == postConvWidth);*/ 461 } 462 463 /* Release the reference to a potentially orphaned buffer. 464 * Release any old malloced memory. 465 */ 466 if (stImage->pt) { 467 pipe_texture_reference(&stImage->pt, NULL); 468 assert(!texImage->Data); 469 } 470 else if (texImage->Data) { 471 _mesa_align_free(texImage->Data); 472 } 473 474 if (width == 0 || height == 0 || depth == 0) { 475 /* stop after freeing old image */ 476 return; 477 } 478 479 /* If this is the only mipmap level in the texture, could call 480 * bmBufferData with NULL data to free the old block and avoid 481 * waiting on any outstanding fences. 482 */ 483 if (stObj->pt) { 484 if (stObj->teximage_realloc || 485 level > (GLint) stObj->pt->last_level || 486 (stObj->pt->last_level == level && 487 stObj->pt->target != PIPE_TEXTURE_CUBE && 488 !st_texture_match_image(stObj->pt, &stImage->base, 489 stImage->face, stImage->level))) { 490 DBG("release it\n"); 491 pipe_texture_reference(&stObj->pt, NULL); 492 assert(!stObj->pt); 493 stObj->teximage_realloc = FALSE; 494 } 495 } 496 497 if (!stObj->pt) { 498 guess_and_alloc_texture(ctx->st, stObj, stImage); 499 if (!stObj->pt) { 500 /* Probably out of memory. 501 * Try flushing any pending rendering, then retry. 502 */ 503 st_finish(ctx->st); 504 guess_and_alloc_texture(ctx->st, stObj, stImage); 505 if (!stObj->pt) { 506 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); 507 return; 508 } 509 } 510 } 511 512 assert(!stImage->pt); 513 514 if (stObj->pt && 515 st_texture_match_image(stObj->pt, &stImage->base, 516 stImage->face, stImage->level)) { 517 518 pipe_texture_reference(&stImage->pt, stObj->pt); 519 assert(stImage->pt); 520 } 521 522 if (!stImage->pt) 523 DBG("XXX: Image did not fit into texture - storing in local memory!\n"); 524 525 /* st_CopyTexImage calls this function with pixels == NULL, with 526 * the expectation that the texture will be set up but nothing 527 * more will be done. This is where those calls return: 528 */ 529 if (compressed) { 530 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels, 531 unpack, 532 "glCompressedTexImage"); 533 } 534 else { 535 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1, 536 format, type, 537 pixels, unpack, "glTexImage"); 538 } 539 if (!pixels) 540 return; 541 542 if (stImage->pt) { 543 texImage->Data = st_texture_image_map(ctx->st, stImage, 0, 544 PIPE_TRANSFER_WRITE, 0, 0, 545 stImage->base.Width, 546 stImage->base.Height); 547 dstRowStride = stImage->transfer->stride; 548 } 549 else { 550 /* Allocate regular memory and store the image there temporarily. */ 551 if (texImage->IsCompressed) { 552 sizeInBytes = texImage->CompressedSize; 553 dstRowStride = 554 _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width); 555 assert(dims != 3); 556 } 557 else { 558 dstRowStride = postConvWidth * texelBytes; 559 sizeInBytes = depth * dstRowStride * postConvHeight; 560 } 561 562 texImage->Data = _mesa_align_malloc(sizeInBytes, 16); 563 } 564 565 if (!texImage->Data) { 566 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); 567 return; 568 } 569 570 DBG("Upload image %dx%dx%d row_len %x pitch %x\n", 571 width, height, depth, width * texelBytes, dstRowStride); 572 573 /* Copy data. Would like to know when it's ok for us to eg. use 574 * the blitter to copy. Or, use the hardware to do the format 575 * conversion and copy: 576 */ 577 if (compressed) { 578 memcpy(texImage->Data, pixels, imageSize); 579 } 580 else { 581 const GLuint srcImageStride = 582 _mesa_image_image_stride(unpack, width, height, format, type); 583 GLint i; 584 const GLubyte *src = (const GLubyte *) pixels; 585 586 for (i = 0; i < depth; i++) { 587 if (!texImage->TexFormat->StoreImage(ctx, dims, 588 texImage->_BaseFormat, 589 texImage->TexFormat, 590 texImage->Data, 591 0, 0, 0, /* dstX/Y/Zoffset */ 592 dstRowStride, 593 texImage->ImageOffsets, 594 width, height, 1, 595 format, type, src, unpack)) { 596 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); 597 } 598 599 if (stImage->pt && i + 1 < depth) { 600 /* unmap this slice */ 601 st_texture_image_unmap(ctx->st, stImage); 602 /* map next slice of 3D texture */ 603 texImage->Data = st_texture_image_map(ctx->st, stImage, i + 1, 604 PIPE_TRANSFER_WRITE, 0, 0, 605 stImage->base.Width, 606 stImage->base.Height); 607 src += srcImageStride; 608 } 609 } 610 } 611 612 _mesa_unmap_teximage_pbo(ctx, unpack); 613 614 if (stImage->pt) { 615 st_texture_image_unmap(ctx->st, stImage); 616 texImage->Data = NULL; 617 } 618 619 if (level == texObj->BaseLevel && texObj->GenerateMipmap) { 620 ctx->Driver.GenerateMipmap(ctx, target, texObj); 621 } 622} 623 624 625static void 626st_TexImage3D(GLcontext * ctx, 627 GLenum target, GLint level, 628 GLint internalFormat, 629 GLint width, GLint height, GLint depth, 630 GLint border, 631 GLenum format, GLenum type, const void *pixels, 632 const struct gl_pixelstore_attrib *unpack, 633 struct gl_texture_object *texObj, 634 struct gl_texture_image *texImage) 635{ 636 st_TexImage(ctx, 3, target, level, internalFormat, width, height, depth, 637 border, format, type, pixels, unpack, texObj, texImage, 638 0, GL_FALSE); 639} 640 641 642static void 643st_TexImage2D(GLcontext * ctx, 644 GLenum target, GLint level, 645 GLint internalFormat, 646 GLint width, GLint height, GLint border, 647 GLenum format, GLenum type, const void *pixels, 648 const struct gl_pixelstore_attrib *unpack, 649 struct gl_texture_object *texObj, 650 struct gl_texture_image *texImage) 651{ 652 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border, 653 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE); 654} 655 656 657static void 658st_TexImage1D(GLcontext * ctx, 659 GLenum target, GLint level, 660 GLint internalFormat, 661 GLint width, GLint border, 662 GLenum format, GLenum type, const void *pixels, 663 const struct gl_pixelstore_attrib *unpack, 664 struct gl_texture_object *texObj, 665 struct gl_texture_image *texImage) 666{ 667 st_TexImage(ctx, 1, target, level, internalFormat, width, 1, 1, border, 668 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE); 669} 670 671 672static void 673st_CompressedTexImage2D(GLcontext *ctx, GLenum target, GLint level, 674 GLint internalFormat, 675 GLint width, GLint height, GLint border, 676 GLsizei imageSize, const GLvoid *data, 677 struct gl_texture_object *texObj, 678 struct gl_texture_image *texImage) 679{ 680 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border, 681 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE); 682} 683 684 685/** 686 * Need to map texture image into memory before copying image data, 687 * then unmap it. 688 */ 689static void 690st_get_tex_image(GLcontext * ctx, GLenum target, GLint level, 691 GLenum format, GLenum type, GLvoid * pixels, 692 struct gl_texture_object *texObj, 693 struct gl_texture_image *texImage, GLboolean compressed) 694{ 695 struct st_texture_image *stImage = st_texture_image(texImage); 696 const GLuint dstImageStride = 697 _mesa_image_image_stride(&ctx->Pack, texImage->Width, texImage->Height, 698 format, type); 699 GLuint depth, i; 700 GLubyte *dest; 701 702 /* Map */ 703 if (stImage->pt) { 704 /* Image is stored in hardware format in a buffer managed by the 705 * kernel. Need to explicitly map and unmap it. 706 */ 707 texImage->Data = st_texture_image_map(ctx->st, stImage, 0, 708 PIPE_TRANSFER_READ, 0, 0, 709 stImage->base.Width, 710 stImage->base.Height); 711 texImage->RowStride = stImage->transfer->stride / stImage->pt->block.size; 712 } 713 else { 714 /* Otherwise, the image should actually be stored in 715 * texImage->Data. This is pretty confusing for 716 * everybody, I'd much prefer to separate the two functions of 717 * texImage->Data - storage for texture images in main memory 718 * and access (ie mappings) of images. In other words, we'd 719 * create a new texImage->Map field and leave Data simply for 720 * storage. 721 */ 722 assert(texImage->Data); 723 } 724 725 depth = texImage->Depth; 726 texImage->Depth = 1; 727 728 dest = (GLubyte *) pixels; 729 730 for (i = 0; i < depth; i++) { 731 if (compressed) { 732 _mesa_get_compressed_teximage(ctx, target, level, dest, 733 texObj, texImage); 734 } 735 else { 736 _mesa_get_teximage(ctx, target, level, format, type, dest, 737 texObj, texImage); 738 } 739 740 if (stImage->pt && i + 1 < depth) { 741 /* unmap this slice */ 742 st_texture_image_unmap(ctx->st, stImage); 743 /* map next slice of 3D texture */ 744 texImage->Data = st_texture_image_map(ctx->st, stImage, i + 1, 745 PIPE_TRANSFER_READ, 0, 0, 746 stImage->base.Width, 747 stImage->base.Height); 748 dest += dstImageStride; 749 } 750 } 751 752 texImage->Depth = depth; 753 754 /* Unmap */ 755 if (stImage->pt) { 756 st_texture_image_unmap(ctx->st, stImage); 757 texImage->Data = NULL; 758 } 759} 760 761 762static void 763st_GetTexImage(GLcontext * ctx, GLenum target, GLint level, 764 GLenum format, GLenum type, GLvoid * pixels, 765 struct gl_texture_object *texObj, 766 struct gl_texture_image *texImage) 767{ 768 st_get_tex_image(ctx, target, level, format, type, pixels, texObj, texImage, 769 GL_FALSE); 770} 771 772 773static void 774st_GetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level, 775 GLvoid *pixels, 776 struct gl_texture_object *texObj, 777 struct gl_texture_image *texImage) 778{ 779 st_get_tex_image(ctx, target, level, 0, 0, pixels, texObj, texImage, 780 GL_TRUE); 781} 782 783 784 785static void 786st_TexSubimage(GLcontext *ctx, GLint dims, GLenum target, GLint level, 787 GLint xoffset, GLint yoffset, GLint zoffset, 788 GLint width, GLint height, GLint depth, 789 GLenum format, GLenum type, const void *pixels, 790 const struct gl_pixelstore_attrib *packing, 791 struct gl_texture_object *texObj, 792 struct gl_texture_image *texImage) 793{ 794 struct st_texture_image *stImage = st_texture_image(texImage); 795 GLuint dstRowStride; 796 const GLuint srcImageStride = 797 _mesa_image_image_stride(packing, width, height, format, type); 798 GLint i; 799 const GLubyte *src; 800 801 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__, 802 _mesa_lookup_enum_by_nr(target), 803 level, xoffset, yoffset, width, height); 804 805 pixels = 806 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format, 807 type, pixels, packing, "glTexSubImage2D"); 808 if (!pixels) 809 return; 810 811 /* Map buffer if necessary. Need to lock to prevent other contexts 812 * from uploading the buffer under us. 813 */ 814 if (stImage->pt) { 815 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset, 816 PIPE_TRANSFER_WRITE, 817 xoffset, yoffset, 818 width, height); 819 } 820 821 if (!texImage->Data) { 822 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage"); 823 return; 824 } 825 826 src = (const GLubyte *) pixels; 827 dstRowStride = stImage->transfer->stride; 828 829 for (i = 0; i < depth; i++) { 830 if (!texImage->TexFormat->StoreImage(ctx, dims, texImage->_BaseFormat, 831 texImage->TexFormat, 832 texImage->Data, 833 0, 0, 0, 834 dstRowStride, 835 texImage->ImageOffsets, 836 width, height, 1, 837 format, type, src, packing)) { 838 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage"); 839 } 840 841 if (stImage->pt && i + 1 < depth) { 842 /* unmap this slice */ 843 st_texture_image_unmap(ctx->st, stImage); 844 /* map next slice of 3D texture */ 845 texImage->Data = st_texture_image_map(ctx->st, stImage, 846 zoffset + i + 1, 847 PIPE_TRANSFER_WRITE, 848 xoffset, yoffset, 849 width, height); 850 src += srcImageStride; 851 } 852 } 853 854 if (level == texObj->BaseLevel && texObj->GenerateMipmap) { 855 ctx->Driver.GenerateMipmap(ctx, target, texObj); 856 } 857 858 _mesa_unmap_teximage_pbo(ctx, packing); 859 860 if (stImage->pt) { 861 st_texture_image_unmap(ctx->st, stImage); 862 texImage->Data = NULL; 863 } 864} 865 866 867 868static void 869st_TexSubImage3D(GLcontext *ctx, GLenum target, GLint level, 870 GLint xoffset, GLint yoffset, GLint zoffset, 871 GLsizei width, GLsizei height, GLsizei depth, 872 GLenum format, GLenum type, const GLvoid *pixels, 873 const struct gl_pixelstore_attrib *packing, 874 struct gl_texture_object *texObj, 875 struct gl_texture_image *texImage) 876{ 877 st_TexSubimage(ctx, 3, target, level, xoffset, yoffset, zoffset, 878 width, height, depth, format, type, 879 pixels, packing, texObj, texImage); 880} 881 882 883static void 884st_TexSubImage2D(GLcontext *ctx, GLenum target, GLint level, 885 GLint xoffset, GLint yoffset, 886 GLsizei width, GLsizei height, 887 GLenum format, GLenum type, const GLvoid * pixels, 888 const struct gl_pixelstore_attrib *packing, 889 struct gl_texture_object *texObj, 890 struct gl_texture_image *texImage) 891{ 892 st_TexSubimage(ctx, 2, target, level, xoffset, yoffset, 0, 893 width, height, 1, format, type, 894 pixels, packing, texObj, texImage); 895} 896 897 898static void 899st_TexSubImage1D(GLcontext *ctx, GLenum target, GLint level, 900 GLint xoffset, GLsizei width, GLenum format, GLenum type, 901 const GLvoid * pixels, 902 const struct gl_pixelstore_attrib *packing, 903 struct gl_texture_object *texObj, 904 struct gl_texture_image *texImage) 905{ 906 st_TexSubimage(ctx, 1, target, level, xoffset, 0, 0, width, 1, 1, 907 format, type, pixels, packing, texObj, texImage); 908} 909 910 911 912/** 913 * Do a CopyTexSubImage operation using a read transfer from the source, 914 * a write transfer to the destination and get_tile()/put_tile() to access 915 * the pixels/texels. 916 * 917 * Note: srcY=0=TOP of renderbuffer 918 */ 919static void 920fallback_copy_texsubimage(GLcontext *ctx, GLenum target, GLint level, 921 struct st_renderbuffer *strb, 922 struct st_texture_image *stImage, 923 GLenum baseFormat, 924 GLint destX, GLint destY, GLint destZ, 925 GLint srcX, GLint srcY, 926 GLsizei width, GLsizei height) 927{ 928 struct pipe_context *pipe = ctx->st->pipe; 929 struct pipe_screen *screen = pipe->screen; 930 struct pipe_transfer *src_trans; 931 GLvoid *texDest; 932 933 assert(width <= MAX_WIDTH); 934 935 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 936 srcY = strb->Base.Height - srcY - height; 937 } 938 939 src_trans = screen->get_tex_transfer( screen, 940 strb->texture, 941 0, 0, 0, 942 PIPE_TRANSFER_READ, 943 srcX, srcY, 944 width, height); 945 946 texDest = st_texture_image_map(ctx->st, stImage, 0, PIPE_TRANSFER_WRITE, 947 destX, destY, width, height); 948 949 if (baseFormat == GL_DEPTH_COMPONENT || 950 baseFormat == GL_DEPTH24_STENCIL8) { 951 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F || 952 ctx->Pixel.DepthBias != 0.0F); 953 GLint row, yStep; 954 955 /* determine bottom-to-top vs. top-to-bottom order for src buffer */ 956 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 957 srcY = height - 1; 958 yStep = -1; 959 } 960 else { 961 srcY = 0; 962 yStep = 1; 963 } 964 965 /* To avoid a large temp memory allocation, do copy row by row */ 966 for (row = 0; row < height; row++, srcY += yStep) { 967 uint data[MAX_WIDTH]; 968 pipe_get_tile_z(src_trans, 0, srcY, width, 1, data); 969 if (scaleOrBias) { 970 _mesa_scale_and_bias_depth_uint(ctx, width, data); 971 } 972 pipe_put_tile_z(stImage->transfer, 0, row, width, 1, data); 973 } 974 } 975 else { 976 /* RGBA format */ 977 GLfloat *tempSrc = 978 (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat)); 979 980 if (tempSrc && texDest) { 981 const GLint dims = 2; 982 const GLint dstRowStride = stImage->transfer->stride; 983 struct gl_texture_image *texImage = &stImage->base; 984 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking; 985 986 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 987 unpack.Invert = GL_TRUE; 988 } 989 990 /* get float/RGBA image from framebuffer */ 991 /* XXX this usually involves a lot of int/float conversion. 992 * try to avoid that someday. 993 */ 994 pipe_get_tile_rgba(src_trans, 0, 0, width, height, tempSrc); 995 996 /* Store into texture memory. 997 * Note that this does some special things such as pixel transfer 998 * ops and format conversion. In particular, if the dest tex format 999 * is actually RGBA but the user created the texture as GL_RGB we 1000 * need to fill-in/override the alpha channel with 1.0. 1001 */ 1002 texImage->TexFormat->StoreImage(ctx, dims, 1003 texImage->_BaseFormat, 1004 texImage->TexFormat, 1005 texDest, 1006 0, 0, 0, 1007 dstRowStride, 1008 texImage->ImageOffsets, 1009 width, height, 1, 1010 GL_RGBA, GL_FLOAT, tempSrc, /* src */ 1011 &unpack); 1012 } 1013 else { 1014 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage"); 1015 } 1016 1017 if (tempSrc) 1018 _mesa_free(tempSrc); 1019 } 1020 1021 st_texture_image_unmap(ctx->st, stImage); 1022 screen->tex_transfer_destroy(src_trans); 1023} 1024 1025 1026/** 1027 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible. 1028 * Note that the region to copy has already been clipped so we know we 1029 * won't read from outside the source renderbuffer's bounds. 1030 * 1031 * Note: srcY=0=Bottom of renderbuffer (GL convention) 1032 */ 1033static void 1034st_copy_texsubimage(GLcontext *ctx, 1035 GLenum target, GLint level, 1036 GLint destX, GLint destY, GLint destZ, 1037 GLint srcX, GLint srcY, 1038 GLsizei width, GLsizei height) 1039{ 1040 struct gl_texture_unit *texUnit = 1041 &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; 1042 struct gl_texture_object *texObj = 1043 _mesa_select_tex_object(ctx, texUnit, target); 1044 struct gl_texture_image *texImage = 1045 _mesa_select_tex_image(ctx, texObj, target, level); 1046 struct st_texture_image *stImage = st_texture_image(texImage); 1047 const GLenum texBaseFormat = texImage->InternalFormat; 1048 struct gl_framebuffer *fb = ctx->ReadBuffer; 1049 struct st_renderbuffer *strb; 1050 struct pipe_context *pipe = ctx->st->pipe; 1051 struct pipe_screen *screen = pipe->screen; 1052 enum pipe_format dest_format, src_format; 1053 GLboolean use_fallback = GL_TRUE; 1054 GLboolean matching_base_formats; 1055 1056 /* any rendering in progress must complete before we grab the fb image */ 1057 st_finish(ctx->st); 1058 1059 /* determine if copying depth or color data */ 1060 if (texBaseFormat == GL_DEPTH_COMPONENT || 1061 texBaseFormat == GL_DEPTH24_STENCIL8) { 1062 strb = st_renderbuffer(fb->_DepthBuffer); 1063 } 1064 else if (texBaseFormat == GL_DEPTH_STENCIL_EXT) { 1065 strb = st_renderbuffer(fb->_StencilBuffer); 1066 } 1067 else { 1068 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */ 1069 strb = st_renderbuffer(fb->_ColorReadBuffer); 1070 } 1071 1072 assert(strb); 1073 assert(strb->surface); 1074 assert(stImage->pt); 1075 1076 src_format = strb->surface->format; 1077 dest_format = stImage->pt->format; 1078 1079 /* 1080 * Determine if the src framebuffer and dest texture have the same 1081 * base format. We need this to detect a case such as the framebuffer 1082 * being GL_RGBA but the texture being GL_RGB. If the actual hardware 1083 * texture format stores RGBA we need to set A=1 (overriding the 1084 * framebuffer's alpha values). We can't do that with the blit or 1085 * textured-quad paths. 1086 */ 1087 matching_base_formats = (strb->Base._BaseFormat == texImage->_BaseFormat); 1088 1089 if (matching_base_formats && ctx->_ImageTransferState == 0x0) { 1090 /* try potential hardware path */ 1091 struct pipe_surface *dest_surface = NULL; 1092 boolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP); 1093 1094 if (src_format == dest_format && !do_flip) { 1095 /* use surface_copy() / blit */ 1096 1097 dest_surface = screen->get_tex_surface(screen, stImage->pt, 1098 stImage->face, stImage->level, 1099 destZ, 1100 PIPE_BUFFER_USAGE_GPU_WRITE); 1101 1102 /* for surface_copy(), y=0=top, always */ 1103 pipe->surface_copy(pipe, 1104 /* dest */ 1105 dest_surface, 1106 destX, destY, 1107 /* src */ 1108 strb->surface, 1109 srcX, srcY, 1110 /* size */ 1111 width, height); 1112 use_fallback = GL_FALSE; 1113 } 1114 else if (screen->is_format_supported(screen, src_format, 1115 PIPE_TEXTURE_2D, 1116 PIPE_TEXTURE_USAGE_SAMPLER, 1117 0) && 1118 screen->is_format_supported(screen, dest_format, 1119 PIPE_TEXTURE_2D, 1120 PIPE_TEXTURE_USAGE_RENDER_TARGET, 1121 0)) { 1122 /* draw textured quad to do the copy */ 1123 GLint srcY0, srcY1; 1124 1125 dest_surface = screen->get_tex_surface(screen, stImage->pt, 1126 stImage->face, stImage->level, 1127 destZ, 1128 PIPE_BUFFER_USAGE_GPU_WRITE); 1129 1130 if (do_flip) { 1131 srcY1 = strb->Base.Height - srcY - height; 1132 srcY0 = srcY1 + height; 1133 } 1134 else { 1135 srcY0 = srcY; 1136 srcY1 = srcY0 + height; 1137 } 1138 util_blit_pixels(ctx->st->blit, 1139 strb->surface, 1140 srcX, srcY0, 1141 srcX + width, srcY1, 1142 dest_surface, 1143 destX, destY, 1144 destX + width, destY + height, 1145 0.0, PIPE_TEX_MIPFILTER_NEAREST); 1146 use_fallback = GL_FALSE; 1147 } 1148 1149 if (dest_surface) 1150 pipe_surface_reference(&dest_surface, NULL); 1151 } 1152 1153 if (use_fallback) { 1154 /* software fallback */ 1155 fallback_copy_texsubimage(ctx, target, level, 1156 strb, stImage, texBaseFormat, 1157 destX, destY, destZ, 1158 srcX, srcY, width, height); 1159 } 1160 1161 if (level == texObj->BaseLevel && texObj->GenerateMipmap) { 1162 ctx->Driver.GenerateMipmap(ctx, target, texObj); 1163 } 1164} 1165 1166 1167 1168static void 1169st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level, 1170 GLenum internalFormat, 1171 GLint x, GLint y, GLsizei width, GLint border) 1172{ 1173 struct gl_texture_unit *texUnit = 1174 &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; 1175 struct gl_texture_object *texObj = 1176 _mesa_select_tex_object(ctx, texUnit, target); 1177 struct gl_texture_image *texImage = 1178 _mesa_select_tex_image(ctx, texObj, target, level); 1179 1180 /* Setup or redefine the texture object, texture and texture 1181 * image. Don't populate yet. 1182 */ 1183 ctx->Driver.TexImage1D(ctx, target, level, internalFormat, 1184 width, border, 1185 GL_RGBA, CHAN_TYPE, NULL, 1186 &ctx->DefaultPacking, texObj, texImage); 1187 1188 st_copy_texsubimage(ctx, target, level, 1189 0, 0, 0, /* destX,Y,Z */ 1190 x, y, width, 1); /* src X, Y, size */ 1191} 1192 1193 1194static void 1195st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level, 1196 GLenum internalFormat, 1197 GLint x, GLint y, GLsizei width, GLsizei height, 1198 GLint border) 1199{ 1200 struct gl_texture_unit *texUnit = 1201 &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; 1202 struct gl_texture_object *texObj = 1203 _mesa_select_tex_object(ctx, texUnit, target); 1204 struct gl_texture_image *texImage = 1205 _mesa_select_tex_image(ctx, texObj, target, level); 1206 1207 /* Setup or redefine the texture object, texture and texture 1208 * image. Don't populate yet. 1209 */ 1210 ctx->Driver.TexImage2D(ctx, target, level, internalFormat, 1211 width, height, border, 1212 GL_RGBA, CHAN_TYPE, NULL, 1213 &ctx->DefaultPacking, texObj, texImage); 1214 1215 st_copy_texsubimage(ctx, target, level, 1216 0, 0, 0, /* destX,Y,Z */ 1217 x, y, width, height); /* src X, Y, size */ 1218} 1219 1220 1221static void 1222st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level, 1223 GLint xoffset, GLint x, GLint y, GLsizei width) 1224{ 1225 const GLint yoffset = 0, zoffset = 0; 1226 const GLsizei height = 1; 1227 st_copy_texsubimage(ctx, target, level, 1228 xoffset, yoffset, zoffset, /* destX,Y,Z */ 1229 x, y, width, height); /* src X, Y, size */ 1230} 1231 1232 1233static void 1234st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level, 1235 GLint xoffset, GLint yoffset, 1236 GLint x, GLint y, GLsizei width, GLsizei height) 1237{ 1238 const GLint zoffset = 0; 1239 st_copy_texsubimage(ctx, target, level, 1240 xoffset, yoffset, zoffset, /* destX,Y,Z */ 1241 x, y, width, height); /* src X, Y, size */ 1242} 1243 1244 1245static void 1246st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level, 1247 GLint xoffset, GLint yoffset, GLint zoffset, 1248 GLint x, GLint y, GLsizei width, GLsizei height) 1249{ 1250 st_copy_texsubimage(ctx, target, level, 1251 xoffset, yoffset, zoffset, /* destX,Y,Z */ 1252 x, y, width, height); /* src X, Y, size */ 1253} 1254 1255 1256/** 1257 * Compute which mipmap levels that really need to be sent to the hardware. 1258 * This depends on the base image size, GL_TEXTURE_MIN_LOD, 1259 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL. 1260 */ 1261static void 1262calculate_first_last_level(struct st_texture_object *stObj) 1263{ 1264 struct gl_texture_object *tObj = &stObj->base; 1265 1266 /* These must be signed values. MinLod and MaxLod can be negative numbers, 1267 * and having firstLevel and lastLevel as signed prevents the need for 1268 * extra sign checks. 1269 */ 1270 GLint firstLevel; 1271 GLint lastLevel; 1272 1273 /* Yes, this looks overly complicated, but it's all needed. 1274 */ 1275 switch (tObj->Target) { 1276 case GL_TEXTURE_1D: 1277 case GL_TEXTURE_2D: 1278 case GL_TEXTURE_3D: 1279 case GL_TEXTURE_CUBE_MAP: 1280 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) { 1281 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL. 1282 */ 1283 firstLevel = lastLevel = tObj->BaseLevel; 1284 } 1285 else { 1286 firstLevel = 0; 1287 lastLevel = MIN2(tObj->MaxLevel, 1288 (int) tObj->Image[0][tObj->BaseLevel]->WidthLog2); 1289 } 1290 break; 1291 case GL_TEXTURE_RECTANGLE_NV: 1292 case GL_TEXTURE_4D_SGIS: 1293 firstLevel = lastLevel = 0; 1294 break; 1295 default: 1296 return; 1297 } 1298 1299 stObj->lastLevel = lastLevel; 1300} 1301 1302 1303static void 1304copy_image_data_to_texture(struct st_context *st, 1305 struct st_texture_object *stObj, 1306 GLuint dstLevel, 1307 struct st_texture_image *stImage) 1308{ 1309 if (stImage->pt) { 1310 /* Copy potentially with the blitter: 1311 */ 1312 st_texture_image_copy(st->pipe, 1313 stObj->pt, dstLevel, /* dest texture, level */ 1314 stImage->pt, /* src texture */ 1315 stImage->face 1316 ); 1317 1318 pipe_texture_reference(&stImage->pt, NULL); 1319 } 1320 else if (stImage->base.Data) { 1321 assert(stImage->base.Data != NULL); 1322 1323 /* More straightforward upload. 1324 */ 1325 st_texture_image_data(st->pipe, 1326 stObj->pt, 1327 stImage->face, 1328 dstLevel, 1329 stImage->base.Data, 1330 stImage->base.RowStride * 1331 stObj->pt->block.size, 1332 stImage->base.RowStride * 1333 stImage->base.Height * 1334 stObj->pt->block.size); 1335 _mesa_align_free(stImage->base.Data); 1336 stImage->base.Data = NULL; 1337 } 1338 1339 pipe_texture_reference(&stImage->pt, stObj->pt); 1340} 1341 1342 1343/** 1344 * Called during state validation. When this function is finished, 1345 * the texture object should be ready for rendering. 1346 * \return GL_TRUE for success, GL_FALSE for failure (out of mem) 1347 */ 1348GLboolean 1349st_finalize_texture(GLcontext *ctx, 1350 struct pipe_context *pipe, 1351 struct gl_texture_object *tObj, 1352 GLboolean *needFlush) 1353{ 1354 struct st_texture_object *stObj = st_texture_object(tObj); 1355 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1; 1356 GLuint comp_byte = 0, cpp, face; 1357 struct st_texture_image *firstImage; 1358 1359 *needFlush = GL_FALSE; 1360 1361 /* We know/require this is true by now: 1362 */ 1363 assert(stObj->base._Complete); 1364 1365 /* What levels must the texture include at a minimum? 1366 */ 1367 calculate_first_last_level(stObj); 1368 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]); 1369 1370 /* If both firstImage and stObj point to a texture which can contain 1371 * all active images, favour firstImage. Note that because of the 1372 * completeness requirement, we know that the image dimensions 1373 * will match. 1374 */ 1375 if (firstImage->pt && 1376 firstImage->pt != stObj->pt && 1377 firstImage->pt->last_level >= stObj->lastLevel) { 1378 pipe_texture_reference(&stObj->pt, firstImage->pt); 1379 } 1380 1381 /* FIXME: determine format block instead of cpp */ 1382 if (firstImage->base.IsCompressed) { 1383 comp_byte = compressed_num_bytes(firstImage->base.TexFormat->MesaFormat); 1384 cpp = comp_byte; 1385 } 1386 else { 1387 cpp = firstImage->base.TexFormat->TexelBytes; 1388 } 1389 1390 /* If we already have a gallium texture, check that it matches the texture 1391 * object's format, target, size, num_levels, etc. 1392 */ 1393 if (stObj->pt) { 1394 const enum pipe_format fmt = 1395 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat); 1396 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) || 1397 stObj->pt->format != fmt || 1398 stObj->pt->last_level < stObj->lastLevel || 1399 stObj->pt->width[0] != firstImage->base.Width2 || 1400 stObj->pt->height[0] != firstImage->base.Height2 || 1401 stObj->pt->depth[0] != firstImage->base.Depth2 || 1402 stObj->pt->block.size/stObj->pt->block.width != cpp || /* Nominal bytes per pixel */ 1403 stObj->pt->compressed != firstImage->base.IsCompressed) { 1404 pipe_texture_reference(&stObj->pt, NULL); 1405 ctx->st->dirty.st |= ST_NEW_FRAMEBUFFER; 1406 } 1407 } 1408 1409 /* May need to create a new gallium texture: 1410 */ 1411 if (!stObj->pt) { 1412 const enum pipe_format fmt = 1413 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat); 1414 GLuint usage = default_usage(fmt); 1415 1416 stObj->pt = st_texture_create(ctx->st, 1417 gl_target_to_pipe(stObj->base.Target), 1418 fmt, 1419 stObj->lastLevel, 1420 firstImage->base.Width2, 1421 firstImage->base.Height2, 1422 firstImage->base.Depth2, 1423 comp_byte, 1424 usage); 1425 1426 if (!stObj->pt) { 1427 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); 1428 return GL_FALSE; 1429 } 1430 } 1431 1432 /* Pull in any images not in the object's texture: 1433 */ 1434 for (face = 0; face < nr_faces; face++) { 1435 GLuint level; 1436 for (level = 0; level <= stObj->lastLevel; level++) { 1437 struct st_texture_image *stImage = 1438 st_texture_image(stObj->base.Image[face][stObj->base.BaseLevel + level]); 1439 1440 /* Need to import images in main memory or held in other textures. 1441 */ 1442 if (stImage && stObj->pt != stImage->pt) { 1443 copy_image_data_to_texture(ctx->st, stObj, level, stImage); 1444 *needFlush = GL_TRUE; 1445 } 1446 } 1447 } 1448 1449 return GL_TRUE; 1450} 1451 1452 1453/** 1454 * Returns pointer to a default/dummy texture. 1455 * This is typically used when the current shader has tex/sample instructions 1456 * but the user has not provided a (any) texture(s). 1457 */ 1458struct gl_texture_object * 1459st_get_default_texture(struct st_context *st) 1460{ 1461 if (!st->default_texture) { 1462 static const GLenum target = GL_TEXTURE_2D; 1463 GLubyte pixels[16][16][4]; 1464 struct gl_texture_object *texObj; 1465 struct gl_texture_image *texImg; 1466 GLuint i, j; 1467 1468 /* The ARB_fragment_program spec says (0,0,0,1) should be returned 1469 * when attempting to sample incomplete textures. 1470 */ 1471 for (i = 0; i < 16; i++) { 1472 for (j = 0; j < 16; j++) { 1473 pixels[i][j][0] = 0; 1474 pixels[i][j][1] = 0; 1475 pixels[i][j][2] = 0; 1476 pixels[i][j][3] = 255; 1477 } 1478 } 1479 1480 texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target); 1481 1482 texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0); 1483 1484 _mesa_init_teximage_fields(st->ctx, target, texImg, 1485 16, 16, 1, 0, /* w, h, d, border */ 1486 GL_RGBA); 1487 1488 st_TexImage(st->ctx, 2, target, 1489 0, GL_RGBA, /* level, intformat */ 1490 16, 16, 1, 0, /* w, h, d, border */ 1491 GL_RGBA, GL_UNSIGNED_BYTE, pixels, 1492 &st->ctx->DefaultPacking, 1493 texObj, texImg, 1494 0, 0); 1495 1496 texObj->MinFilter = GL_NEAREST; 1497 texObj->MagFilter = GL_NEAREST; 1498 texObj->_Complete = GL_TRUE; 1499 1500 st->default_texture = texObj; 1501 } 1502 return st->default_texture; 1503} 1504 1505 1506void 1507st_init_texture_functions(struct dd_function_table *functions) 1508{ 1509 functions->ChooseTextureFormat = st_ChooseTextureFormat; 1510 functions->TexImage1D = st_TexImage1D; 1511 functions->TexImage2D = st_TexImage2D; 1512 functions->TexImage3D = st_TexImage3D; 1513 functions->TexSubImage1D = st_TexSubImage1D; 1514 functions->TexSubImage2D = st_TexSubImage2D; 1515 functions->TexSubImage3D = st_TexSubImage3D; 1516 functions->CopyTexImage1D = st_CopyTexImage1D; 1517 functions->CopyTexImage2D = st_CopyTexImage2D; 1518 functions->CopyTexSubImage1D = st_CopyTexSubImage1D; 1519 functions->CopyTexSubImage2D = st_CopyTexSubImage2D; 1520 functions->CopyTexSubImage3D = st_CopyTexSubImage3D; 1521 functions->GenerateMipmap = st_generate_mipmap; 1522 1523 functions->GetTexImage = st_GetTexImage; 1524 1525 /* compressed texture functions */ 1526 functions->CompressedTexImage2D = st_CompressedTexImage2D; 1527 functions->GetCompressedTexImage = st_GetCompressedTexImage; 1528 functions->CompressedTextureSize = _mesa_compressed_texture_size; 1529 1530 functions->NewTextureObject = st_NewTextureObject; 1531 functions->NewTextureImage = st_NewTextureImage; 1532 functions->DeleteTexture = st_DeleteTextureObject; 1533 functions->FreeTexImageData = st_FreeTextureImageData; 1534 functions->UpdateTexturePalette = 0; 1535 1536 functions->TextureMemCpy = do_memcpy; 1537 1538 /* XXX Temporary until we can query pipe's texture sizes */ 1539 functions->TestProxyTexImage = _mesa_test_proxy_teximage; 1540} 1541