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