st_cb_texture.c revision c3a8a41faabed4c9b84a6fbaf7a86a089b8fcbba
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 st_texture_object *stObj = st_texture_object(texObj); 482 struct st_texture_image *stImage = st_texture_image(texImage); 483 GLint postConvWidth, postConvHeight; 484 GLint texelBytes, sizeInBytes; 485 GLuint dstRowStride; 486 struct gl_pixelstore_attrib unpackNB; 487 488 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__, 489 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border); 490 491 /* gallium does not support texture borders, strip it off */ 492 if (border) { 493 strip_texture_border(border, &width, &height, &depth, 494 unpack, &unpackNB); 495 unpack = &unpackNB; 496 texImage->Width = width; 497 texImage->Height = height; 498 texImage->Depth = depth; 499 texImage->Border = 0; 500 border = 0; 501 } 502 503 postConvWidth = width; 504 postConvHeight = height; 505 506 stImage->face = _mesa_tex_target_to_face(target); 507 stImage->level = level; 508 509 if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) { 510 _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth, 511 &postConvHeight); 512 } 513 514 /* choose the texture format */ 515 texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat, 516 format, type); 517 518 _mesa_set_fetch_functions(texImage, dims); 519 520 if (texImage->TexFormat->TexelBytes == 0) { 521 /* must be a compressed format */ 522 texelBytes = 0; 523 texImage->IsCompressed = GL_TRUE; 524 texImage->CompressedSize = 525 ctx->Driver.CompressedTextureSize(ctx, texImage->Width, 526 texImage->Height, texImage->Depth, 527 texImage->TexFormat->MesaFormat); 528 } 529 else { 530 texelBytes = texImage->TexFormat->TexelBytes; 531 532 /* Minimum pitch of 32 bytes */ 533 if (postConvWidth * texelBytes < 32) { 534 postConvWidth = 32 / texelBytes; 535 texImage->RowStride = postConvWidth; 536 } 537 538 /* we'll set RowStride elsewhere when the texture is a "mapped" state */ 539 /*assert(texImage->RowStride == postConvWidth);*/ 540 } 541 542 /* Release the reference to a potentially orphaned buffer. 543 * Release any old malloced memory. 544 */ 545 if (stImage->pt) { 546 pipe_texture_release(&stImage->pt); 547 assert(!texImage->Data); 548 } 549 else if (texImage->Data) { 550 _mesa_align_free(texImage->Data); 551 } 552 553 /* If this is the only mipmap level in the texture, could call 554 * bmBufferData with NULL data to free the old block and avoid 555 * waiting on any outstanding fences. 556 */ 557 if (stObj->pt && 558 /*stObj->pt->first_level == level &&*/ 559 stObj->pt->last_level == level && 560 stObj->pt->target != PIPE_TEXTURE_CUBE && 561 !st_texture_match_image(stObj->pt, &stImage->base, 562 stImage->face, stImage->level)) { 563 564 DBG("release it\n"); 565 pipe_texture_release(&stObj->pt); 566 assert(!stObj->pt); 567 } 568 569 if (!stObj->pt) { 570 guess_and_alloc_texture(ctx->st, stObj, stImage); 571 if (!stObj->pt) { 572 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); 573 return; 574 } 575 } 576 577 assert(!stImage->pt); 578 579 if (stObj->pt && 580 st_texture_match_image(stObj->pt, &stImage->base, 581 stImage->face, stImage->level)) { 582 583 pipe_texture_reference(&stImage->pt, stObj->pt); 584 assert(stImage->pt); 585 } 586 587 if (!stImage->pt) 588 DBG("XXX: Image did not fit into texture - storing in local memory!\n"); 589 590#if 0 /* XXX FIX when st_buffer_objects are in place */ 591 /* PBO fastpaths: 592 */ 593 if (dims <= 2 && 594 stImage->pt && 595 intel_buffer_object(unpack->BufferObj) && 596 check_pbo_format(internalFormat, format, 597 type, texImage->TexFormat)) { 598 599 DBG("trying pbo upload\n"); 600 601 602 603 /* Otherwise, attempt to use the blitter for PBO image uploads. 604 */ 605 if (try_pbo_upload(intel, stImage, unpack, 606 internalFormat, 607 width, height, format, type, pixels)) { 608 DBG("pbo upload succeeded\n"); 609 return; 610 } 611 612 DBG("pbo upload failed\n"); 613 } 614#else 615 (void) try_pbo_upload; 616 (void) check_pbo_format; 617#endif 618 619 620 /* st_CopyTexImage calls this function with pixels == NULL, with 621 * the expectation that the texture will be set up but nothing 622 * more will be done. This is where those calls return: 623 */ 624 if (compressed) { 625 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels, 626 unpack, 627 "glCompressedTexImage"); 628 } else { 629 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1, 630 format, type, 631 pixels, unpack, "glTexImage"); 632 } 633 if (!pixels) 634 return; 635 636 if (stImage->pt) { 637 texImage->Data = st_texture_image_map(ctx->st, stImage, 0, 638 PIPE_BUFFER_USAGE_CPU_WRITE); 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(ctx->st, stImage); 688 texImage->Data = st_texture_image_map(ctx->st, stImage, i, 689 PIPE_BUFFER_USAGE_CPU_WRITE); 690 src += srcImageStride; 691 } 692 } 693 } 694 695 _mesa_unmap_teximage_pbo(ctx, unpack); 696 697 if (stImage->pt) { 698 st_texture_image_unmap(ctx->st, stImage); 699 texImage->Data = NULL; 700 } 701 702 if (level == texObj->BaseLevel && texObj->GenerateMipmap) { 703 ctx->Driver.GenerateMipmap(ctx, target, texObj); 704 } 705} 706 707 708static void 709st_TexImage3D(GLcontext * ctx, 710 GLenum target, GLint level, 711 GLint internalFormat, 712 GLint width, GLint height, GLint depth, 713 GLint border, 714 GLenum format, GLenum type, const void *pixels, 715 const struct gl_pixelstore_attrib *unpack, 716 struct gl_texture_object *texObj, 717 struct gl_texture_image *texImage) 718{ 719 st_TexImage(ctx, 3, target, level, 720 internalFormat, width, height, depth, border, 721 format, type, pixels, unpack, texObj, texImage, 0, 0); 722} 723 724 725static void 726st_TexImage2D(GLcontext * ctx, 727 GLenum target, GLint level, 728 GLint internalFormat, 729 GLint width, GLint height, GLint border, 730 GLenum format, GLenum type, const void *pixels, 731 const struct gl_pixelstore_attrib *unpack, 732 struct gl_texture_object *texObj, 733 struct gl_texture_image *texImage) 734{ 735 st_TexImage(ctx, 2, target, level, 736 internalFormat, width, height, 1, border, 737 format, type, pixels, unpack, texObj, texImage, 0, 0); 738} 739 740 741static void 742st_TexImage1D(GLcontext * ctx, 743 GLenum target, GLint level, 744 GLint internalFormat, 745 GLint width, GLint border, 746 GLenum format, GLenum type, const void *pixels, 747 const struct gl_pixelstore_attrib *unpack, 748 struct gl_texture_object *texObj, 749 struct gl_texture_image *texImage) 750{ 751 st_TexImage(ctx, 1, target, level, 752 internalFormat, width, 1, 1, border, 753 format, type, pixels, unpack, texObj, texImage, 0, 0); 754} 755 756 757static void 758st_CompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level, 759 GLint internalFormat, 760 GLint width, GLint height, GLint border, 761 GLsizei imageSize, const GLvoid *data, 762 struct gl_texture_object *texObj, 763 struct gl_texture_image *texImage ) 764{ 765 st_TexImage(ctx, 2, target, level, 766 internalFormat, width, height, 1, border, 767 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, 1); 768} 769 770 771/** 772 * Need to map texture image into memory before copying image data, 773 * then unmap it. 774 */ 775static void 776st_get_tex_image(GLcontext * ctx, GLenum target, GLint level, 777 GLenum format, GLenum type, GLvoid * pixels, 778 struct gl_texture_object *texObj, 779 struct gl_texture_image *texImage, int compressed) 780{ 781 struct st_texture_image *stImage = st_texture_image(texImage); 782 GLuint dstImageStride = _mesa_image_image_stride(&ctx->Pack, texImage->Width, 783 texImage->Height, format, 784 type); 785 GLuint depth; 786 int i; 787 GLubyte *dest; 788 789 /* Map */ 790 if (stImage->pt) { 791 /* Image is stored in hardware format in a buffer managed by the 792 * kernel. Need to explicitly map and unmap it. 793 */ 794 texImage->Data = st_texture_image_map(ctx->st, stImage, 0, 795 PIPE_BUFFER_USAGE_CPU_READ); 796 texImage->RowStride = stImage->surface->pitch; 797 } 798 else { 799 /* Otherwise, the image should actually be stored in 800 * texImage->Data. This is pretty confusing for 801 * everybody, I'd much prefer to separate the two functions of 802 * texImage->Data - storage for texture images in main memory 803 * and access (ie mappings) of images. In other words, we'd 804 * create a new texImage->Map field and leave Data simply for 805 * storage. 806 */ 807 assert(texImage->Data); 808 } 809 810 depth = texImage->Depth; 811 texImage->Depth = 1; 812 813 dest = (GLubyte *) pixels; 814 815 for (i = 0; i++ < depth;) { 816 if (compressed) { 817 _mesa_get_compressed_teximage(ctx, target, level, dest, 818 texObj, texImage); 819 } else { 820 _mesa_get_teximage(ctx, target, level, format, type, dest, 821 texObj, texImage); 822 } 823 824 if (stImage->pt && i < depth) { 825 st_texture_image_unmap(ctx->st, stImage); 826 texImage->Data = st_texture_image_map(ctx->st, stImage, i, 827 PIPE_BUFFER_USAGE_CPU_READ); 828 dest += dstImageStride; 829 } 830 } 831 832 texImage->Depth = depth; 833 834 /* Unmap */ 835 if (stImage->pt) { 836 st_texture_image_unmap(ctx->st, 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 st_texture_image *stImage = st_texture_image(texImage); 878 GLuint dstRowStride; 879 GLuint srcImageStride = _mesa_image_image_stride(packing, width, height, 880 format, type); 881 int i; 882 const GLubyte *src; 883 884 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__, 885 _mesa_lookup_enum_by_nr(target), 886 level, xoffset, yoffset, width, height); 887 888 pixels = 889 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format, 890 type, pixels, packing, "glTexSubImage2D"); 891 if (!pixels) 892 return; 893 894 /* Map buffer if necessary. Need to lock to prevent other contexts 895 * from uploading the buffer under us. 896 */ 897 if (stImage->pt) { 898 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset, 899 PIPE_BUFFER_USAGE_CPU_WRITE); 900 dstRowStride = stImage->surface->pitch * stImage->surface->cpp; 901 } 902 903 if (!texImage->Data) { 904 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage"); 905 return; 906 } 907 908 src = (const GLubyte *) pixels; 909 910 for (i = 0; i++ < depth;) { 911 if (!texImage->TexFormat->StoreImage(ctx, dims, texImage->_BaseFormat, 912 texImage->TexFormat, 913 texImage->Data, 914 xoffset, yoffset, 0, 915 dstRowStride, 916 texImage->ImageOffsets, 917 width, height, 1, 918 format, type, src, packing)) { 919 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage"); 920 } 921 922 if (stImage->pt && i < depth) { 923 /* map next slice of 3D texture */ 924 st_texture_image_unmap(ctx->st, stImage); 925 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset + i, 926 PIPE_BUFFER_USAGE_CPU_WRITE); 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(ctx->st, stImage); 939 texImage->Data = NULL; 940 } 941} 942 943 944 945static void 946st_TexSubImage3D(GLcontext * ctx, 947 GLenum target, 948 GLint level, 949 GLint xoffset, GLint yoffset, GLint zoffset, 950 GLsizei width, GLsizei height, GLsizei depth, 951 GLenum format, GLenum type, 952 const GLvoid * pixels, 953 const struct gl_pixelstore_attrib *packing, 954 struct gl_texture_object *texObj, 955 struct gl_texture_image *texImage) 956{ 957 st_TexSubimage(ctx, 3, target, level, 958 xoffset, yoffset, zoffset, 959 width, height, depth, 960 format, type, pixels, packing, texObj, texImage); 961} 962 963 964 965static void 966st_TexSubImage2D(GLcontext * ctx, 967 GLenum target, 968 GLint level, 969 GLint xoffset, GLint yoffset, 970 GLsizei width, GLsizei height, 971 GLenum format, GLenum type, 972 const GLvoid * pixels, 973 const struct gl_pixelstore_attrib *packing, 974 struct gl_texture_object *texObj, 975 struct gl_texture_image *texImage) 976{ 977 st_TexSubimage(ctx, 2, target, level, 978 xoffset, yoffset, 0, 979 width, height, 1, 980 format, type, pixels, packing, texObj, texImage); 981} 982 983 984static void 985st_TexSubImage1D(GLcontext * ctx, 986 GLenum target, 987 GLint level, 988 GLint xoffset, 989 GLsizei width, 990 GLenum format, GLenum type, 991 const GLvoid * pixels, 992 const struct gl_pixelstore_attrib *packing, 993 struct gl_texture_object *texObj, 994 struct gl_texture_image *texImage) 995{ 996 st_TexSubimage(ctx, 1, target, level, 997 xoffset, 0, 0, 998 width, 1, 1, 999 format, type, pixels, packing, texObj, texImage); 1000} 1001 1002 1003 1004/** 1005 * Return 0 for GL_TEXTURE_CUBE_MAP_POSITIVE_X, 1006 * 1 for GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 1007 * etc. 1008 * XXX duplicated from main/teximage.c 1009 */ 1010static uint 1011texture_face(GLenum target) 1012{ 1013 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB && 1014 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) 1015 return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X; 1016 else 1017 return 0; 1018} 1019 1020 1021 1022/** 1023 * Do a CopyTexSubImage operation by mapping the source surface and 1024 * dest surface and using get_tile()/put_tile() to access the pixels/texels. 1025 * 1026 * Note: srcY=0=TOP of renderbuffer 1027 */ 1028static void 1029fallback_copy_texsubimage(GLcontext *ctx, 1030 GLenum target, 1031 GLint level, 1032 struct st_renderbuffer *strb, 1033 struct st_texture_image *stImage, 1034 GLenum baseFormat, 1035 GLint destX, GLint destY, GLint destZ, 1036 GLint srcX, GLint srcY, 1037 GLsizei width, GLsizei height) 1038{ 1039 struct pipe_context *pipe = ctx->st->pipe; 1040 struct pipe_screen *screen = pipe->screen; 1041 const uint face = texture_face(target); 1042 struct pipe_texture *pt = stImage->pt; 1043 struct pipe_surface *src_surf, *dest_surf; 1044 GLint row, yStep; 1045 1046 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL); 1047 1048 /* determine bottom-to-top vs. top-to-bottom order */ 1049 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 1050 destY = height - 1 - destY; 1051 yStep = -1; 1052 } 1053 else { 1054 yStep = 1; 1055 } 1056 1057 src_surf = strb->surface; 1058 1059 dest_surf = screen->get_tex_surface(screen, pt, face, level, destZ, 1060 PIPE_BUFFER_USAGE_CPU_WRITE); 1061 1062 assert(width <= MAX_WIDTH); 1063 1064 /* 1065 * To avoid a large temp memory allocation, do copy row by row. 1066 */ 1067 if (baseFormat == GL_DEPTH_COMPONENT) { 1068 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F || 1069 ctx->Pixel.DepthBias != 0.0F); 1070 1071 for (row = 0; row < height; row++, srcY++, destY += yStep) { 1072 uint data[MAX_WIDTH]; 1073 pipe_get_tile_z(pipe, src_surf, srcX, srcY, width, 1, data); 1074 if (scaleOrBias) { 1075 _mesa_scale_and_bias_depth_uint(ctx, width, data); 1076 } 1077 pipe_put_tile_z(pipe, dest_surf, destX, destY, width, 1, data); 1078 } 1079 } 1080 else { 1081 /* RGBA format */ 1082 for (row = 0; row < height; row++, srcY++, destY += yStep) { 1083 float data[4 * MAX_WIDTH]; 1084 pipe_get_tile_rgba(pipe, src_surf, srcX, srcY, width, 1, data); 1085 /* XXX we're ignoring convolution for now */ 1086 if (ctx->_ImageTransferState) { 1087 _mesa_apply_rgba_transfer_ops(ctx, 1088 ctx->_ImageTransferState & ~IMAGE_CONVOLUTION_BIT, 1089 width, (GLfloat (*)[4]) data); 1090 } 1091 pipe_put_tile_rgba(pipe, dest_surf, destX, destY, width, 1, data); 1092 } 1093 } 1094} 1095 1096 1097 1098 1099/** 1100 * Do a CopyTex[Sub]Image using an optimized hardware (blit) path. 1101 * Note that the region to copy has already been clip tested. 1102 * 1103 * Note: srcY=0=Bottom of renderbuffer 1104 * 1105 * \return GL_TRUE if success, GL_FALSE if failure (use a fallback) 1106 */ 1107static void 1108do_copy_texsubimage(GLcontext *ctx, 1109 GLenum target, GLint level, 1110 GLint destX, GLint destY, GLint destZ, 1111 GLint srcX, GLint srcY, 1112 GLsizei width, GLsizei height) 1113{ 1114 struct gl_texture_unit *texUnit = 1115 &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; 1116 struct gl_texture_object *texObj = 1117 _mesa_select_tex_object(ctx, texUnit, target); 1118 struct gl_texture_image *texImage = 1119 _mesa_select_tex_image(ctx, texObj, target, level); 1120 struct st_texture_image *stImage = st_texture_image(texImage); 1121 GLenum baseFormat = texImage->InternalFormat; 1122 struct gl_framebuffer *fb = ctx->ReadBuffer; 1123 struct st_renderbuffer *strb; 1124 struct pipe_context *pipe = ctx->st->pipe; 1125 struct pipe_screen *screen = pipe->screen; 1126 struct pipe_surface *dest_surface; 1127 uint dest_format, src_format; 1128 uint do_flip = FALSE; 1129 GLboolean use_fallback = GL_TRUE; 1130 1131 (void) texImage; 1132 1133 /* determine if copying depth or color data */ 1134 if (baseFormat == GL_DEPTH_COMPONENT) { 1135 strb = st_renderbuffer(fb->_DepthBuffer); 1136 } 1137 else if (baseFormat == GL_DEPTH_STENCIL_EXT) { 1138 strb = st_renderbuffer(fb->_StencilBuffer); 1139 } 1140 else { 1141 /* baseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */ 1142 strb = st_renderbuffer(fb->_ColorReadBuffer); 1143 } 1144 1145 assert(strb); 1146 assert(strb->surface); 1147 assert(stImage->pt); 1148 1149 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 1150 srcY = strb->Base.Height - srcY - height; 1151 do_flip = TRUE; 1152 } 1153 1154 src_format = strb->surface->format; 1155 dest_format = stImage->pt->format; 1156 1157 dest_surface = screen->get_tex_surface(screen, stImage->pt, stImage->face, 1158 stImage->level, destZ, 1159 PIPE_BUFFER_USAGE_CPU_WRITE); 1160 1161 if (ctx->_ImageTransferState == 0x0 && 1162 strb->surface->buffer && 1163 dest_surface->buffer) { 1164 /* do blit-style copy */ 1165 1166 /* XXX may need to invert image depending on window 1167 * vs. user-created FBO 1168 */ 1169 1170#if 0 1171 /* A bit of fiddling to get the blitter to work with -ve 1172 * pitches. But we get a nice inverted blit this way, so it's 1173 * worth it: 1174 */ 1175 intelEmitCopyBlit(intel, 1176 stImage->pt->cpp, 1177 -src->pitch, 1178 src->buffer, 1179 src->height * src->pitch * src->cpp, 1180 stImage->pt->pitch, 1181 stImage->pt->region->buffer, 1182 dest_offset, 1183 x, y + height, dstx, dsty, width, height, 1184 GL_COPY); /* ? */ 1185#else 1186 1187 if (src_format == dest_format) { 1188 pipe->surface_copy(pipe, 1189 do_flip, 1190 /* dest */ 1191 dest_surface, 1192 destX, destY, 1193 /* src */ 1194 strb->surface, 1195 srcX, srcY, 1196 /* size */ 1197 width, height); 1198 use_fallback = GL_FALSE; 1199 } 1200 else if (screen->is_format_supported(screen, strb->surface->format, 1201 PIPE_TEXTURE) && 1202 screen->is_format_supported(screen, dest_surface->format, 1203 PIPE_SURFACE)) { 1204 util_blit_pixels(ctx->st->blit, 1205 strb->surface, 1206 srcX, do_flip ? srcY + height : srcY, 1207 srcX + width, do_flip ? srcY : srcY + height, 1208 dest_surface, 1209 destX, destY, destX + width, destY + height, 1210 0.0, PIPE_TEX_MIPFILTER_NEAREST); 1211 use_fallback = GL_FALSE; 1212 } 1213#endif 1214 } 1215 1216 if (use_fallback) { 1217 fallback_copy_texsubimage(ctx, target, level, 1218 strb, stImage, baseFormat, 1219 destX, destY, destZ, 1220 srcX, srcY, width, height); 1221 } 1222 1223 pipe_surface_reference(&dest_surface, NULL); 1224 1225 if (level == texObj->BaseLevel && texObj->GenerateMipmap) { 1226 ctx->Driver.GenerateMipmap(ctx, target, texObj); 1227 } 1228} 1229 1230 1231 1232static void 1233st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level, 1234 GLenum internalFormat, 1235 GLint x, GLint y, GLsizei width, GLint border) 1236{ 1237 struct gl_texture_unit *texUnit = 1238 &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; 1239 struct gl_texture_object *texObj = 1240 _mesa_select_tex_object(ctx, texUnit, target); 1241 struct gl_texture_image *texImage = 1242 _mesa_select_tex_image(ctx, texObj, target, level); 1243 1244#if 0 1245 if (border) 1246 goto fail; 1247#endif 1248 1249 /* Setup or redefine the texture object, texture and texture 1250 * image. Don't populate yet. 1251 */ 1252 ctx->Driver.TexImage1D(ctx, target, level, internalFormat, 1253 width, border, 1254 GL_RGBA, CHAN_TYPE, NULL, 1255 &ctx->DefaultPacking, texObj, texImage); 1256 1257 do_copy_texsubimage(ctx, target, level, 1258 0, 0, 0, 1259 x, y, width, 1); 1260} 1261 1262 1263static void 1264st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level, 1265 GLenum internalFormat, 1266 GLint x, GLint y, GLsizei width, GLsizei height, 1267 GLint border) 1268{ 1269 struct gl_texture_unit *texUnit = 1270 &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; 1271 struct gl_texture_object *texObj = 1272 _mesa_select_tex_object(ctx, texUnit, target); 1273 struct gl_texture_image *texImage = 1274 _mesa_select_tex_image(ctx, texObj, target, level); 1275 1276#if 0 1277 if (border) 1278 goto fail; 1279#endif 1280 1281 /* Setup or redefine the texture object, texture and texture 1282 * image. Don't populate yet. 1283 */ 1284 ctx->Driver.TexImage2D(ctx, target, level, internalFormat, 1285 width, height, border, 1286 GL_RGBA, CHAN_TYPE, NULL, 1287 &ctx->DefaultPacking, texObj, texImage); 1288 1289 1290 do_copy_texsubimage(ctx, target, level, 1291 0, 0, 0, 1292 x, y, width, height); 1293} 1294 1295 1296static void 1297st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level, 1298 GLint xoffset, GLint x, GLint y, GLsizei width) 1299{ 1300 const GLint yoffset = 0, zoffset = 0; 1301 const GLsizei height = 1; 1302 do_copy_texsubimage(ctx, target, level, 1303 xoffset, yoffset, zoffset, 1304 x, y, width, height); 1305} 1306 1307 1308static void 1309st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level, 1310 GLint xoffset, GLint yoffset, 1311 GLint x, GLint y, GLsizei width, GLsizei height) 1312{ 1313 const GLint zoffset = 0; 1314 do_copy_texsubimage(ctx, target, level, 1315 xoffset, yoffset, zoffset, 1316 x, y, width, height); 1317} 1318 1319 1320static void 1321st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level, 1322 GLint xoffset, GLint yoffset, GLint zoffset, 1323 GLint x, GLint y, GLsizei width, GLsizei height) 1324{ 1325 do_copy_texsubimage(ctx, target, level, 1326 xoffset, yoffset, zoffset, 1327 x, y, width, height); 1328} 1329 1330 1331 1332 1333/** 1334 * Compute which mipmap levels that really need to be sent to the hardware. 1335 * This depends on the base image size, GL_TEXTURE_MIN_LOD, 1336 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL. 1337 */ 1338static void 1339calculate_first_last_level(struct st_texture_object *stObj) 1340{ 1341 struct gl_texture_object *tObj = &stObj->base; 1342 1343 /* These must be signed values. MinLod and MaxLod can be negative numbers, 1344 * and having firstLevel and lastLevel as signed prevents the need for 1345 * extra sign checks. 1346 */ 1347 int firstLevel; 1348 int lastLevel; 1349 1350 /* Yes, this looks overly complicated, but it's all needed. 1351 */ 1352 switch (tObj->Target) { 1353 case GL_TEXTURE_1D: 1354 case GL_TEXTURE_2D: 1355 case GL_TEXTURE_3D: 1356 case GL_TEXTURE_CUBE_MAP: 1357 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) { 1358 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL. 1359 */ 1360 firstLevel = lastLevel = tObj->BaseLevel; 1361 } 1362 else { 1363 firstLevel = 0; 1364 lastLevel = MIN2(tObj->MaxLevel, tObj->Image[0][0]->WidthLog2); 1365 } 1366 break; 1367 case GL_TEXTURE_RECTANGLE_NV: 1368 case GL_TEXTURE_4D_SGIS: 1369 firstLevel = lastLevel = 0; 1370 break; 1371 default: 1372 return; 1373 } 1374 1375 stObj->lastLevel = lastLevel; 1376} 1377 1378 1379static void 1380copy_image_data_to_texture(struct st_context *st, 1381 struct st_texture_object *stObj, 1382 GLuint dstLevel, 1383 struct st_texture_image *stImage) 1384{ 1385 if (stImage->pt) { 1386 /* Copy potentially with the blitter: 1387 */ 1388 st_texture_image_copy(st->pipe, 1389 stObj->pt, dstLevel, /* dest texture, level */ 1390 stImage->pt, /* src texture */ 1391 stImage->face 1392 ); 1393 1394 pipe_texture_release(&stImage->pt); 1395 } 1396 else if (stImage->base.Data) { 1397 assert(stImage->base.Data != NULL); 1398 1399 /* More straightforward upload. 1400 */ 1401 st_texture_image_data(st->pipe, 1402 stObj->pt, 1403 stImage->face, 1404 dstLevel, 1405 stImage->base.Data, 1406 stImage->base.RowStride, 1407 stImage->base.RowStride * 1408 stImage->base.Height); 1409 _mesa_align_free(stImage->base.Data); 1410 stImage->base.Data = NULL; 1411 } 1412 1413 pipe_texture_reference(&stImage->pt, stObj->pt); 1414} 1415 1416 1417/** 1418 * Called during state validation. When this function is finished, 1419 * the texture object should be ready for rendering. 1420 * \return GL_TRUE for success, GL_FALSE for failure (out of mem) 1421 */ 1422GLboolean 1423st_finalize_texture(GLcontext *ctx, 1424 struct pipe_context *pipe, 1425 struct gl_texture_object *tObj, 1426 GLboolean *needFlush) 1427{ 1428 struct st_texture_object *stObj = st_texture_object(tObj); 1429 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1; 1430 int comp_byte = 0; 1431 int cpp; 1432 GLuint face; 1433 struct st_texture_image *firstImage; 1434 1435 *needFlush = GL_FALSE; 1436 1437 /* We know/require this is true by now: 1438 */ 1439 assert(stObj->base._Complete); 1440 1441 /* What levels must the texture include at a minimum? 1442 */ 1443 calculate_first_last_level(stObj); 1444 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]); 1445 1446#if 0 1447 /* Fallback case: 1448 */ 1449 if (firstImage->base.Border) { 1450 if (stObj->pt) { 1451 pipe_texture_release(&stObj->pt); 1452 } 1453 return GL_FALSE; 1454 } 1455#endif 1456 1457 /* If both firstImage and stObj point to a texture which can contain 1458 * all active images, favour firstImage. Note that because of the 1459 * completeness requirement, we know that the image dimensions 1460 * will match. 1461 */ 1462 if (firstImage->pt && 1463 firstImage->pt != stObj->pt && 1464 firstImage->pt->last_level >= stObj->lastLevel) { 1465 1466 if (stObj->pt) 1467 pipe_texture_release(&stObj->pt); 1468 1469 pipe_texture_reference(&stObj->pt, firstImage->pt); 1470 } 1471 1472 if (firstImage->base.IsCompressed) { 1473 comp_byte = compressed_num_bytes(firstImage->base.TexFormat->MesaFormat); 1474 cpp = comp_byte; 1475 } 1476 else { 1477 cpp = firstImage->base.TexFormat->TexelBytes; 1478 } 1479 1480 /* Check texture can hold all active levels. Check texture matches 1481 * target, imageFormat, etc. 1482 */ 1483 if (stObj->pt && 1484 (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) || 1485 stObj->pt->format != 1486 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat) || 1487 stObj->pt->last_level < stObj->lastLevel || 1488 stObj->pt->cpp != cpp || 1489 stObj->pt->compressed != firstImage->base.IsCompressed)) { 1490 pipe_texture_release(&stObj->pt); 1491 } 1492 1493 1494 /* May need to create a new texture: 1495 */ 1496 if (!stObj->pt) { 1497 stObj->pt = st_texture_create(ctx->st, 1498 gl_target_to_pipe(stObj->base.Target), 1499 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat), 1500 stObj->lastLevel, 1501 firstImage->base.Width2, 1502 firstImage->base.Height2, 1503 firstImage->base.Depth2, 1504 comp_byte); 1505 if (!stObj->pt) { 1506 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); 1507 return GL_FALSE; 1508 } 1509 } 1510 1511 /* Pull in any images not in the object's texture: 1512 */ 1513 for (face = 0; face < nr_faces; face++) { 1514 GLuint level; 1515 for (level = 0; level <= stObj->lastLevel; level++) { 1516 struct st_texture_image *stImage = 1517 st_texture_image(stObj->base.Image[face][stObj->base.BaseLevel + level]); 1518 1519 /* Need to import images in main memory or held in other textures. 1520 */ 1521 if (stImage && stObj->pt != stImage->pt) { 1522 copy_image_data_to_texture(ctx->st, stObj, level, stImage); 1523 *needFlush = GL_TRUE; 1524 } 1525 } 1526 } 1527 1528 return GL_TRUE; 1529} 1530 1531 1532 1533 1534void 1535st_init_texture_functions(struct dd_function_table *functions) 1536{ 1537 functions->ChooseTextureFormat = st_ChooseTextureFormat; 1538 functions->TexImage1D = st_TexImage1D; 1539 functions->TexImage2D = st_TexImage2D; 1540 functions->TexImage3D = st_TexImage3D; 1541 functions->TexSubImage1D = st_TexSubImage1D; 1542 functions->TexSubImage2D = st_TexSubImage2D; 1543 functions->TexSubImage3D = st_TexSubImage3D; 1544 functions->CopyTexImage1D = st_CopyTexImage1D; 1545 functions->CopyTexImage2D = st_CopyTexImage2D; 1546 functions->CopyTexSubImage1D = st_CopyTexSubImage1D; 1547 functions->CopyTexSubImage2D = st_CopyTexSubImage2D; 1548 functions->CopyTexSubImage3D = st_CopyTexSubImage3D; 1549 functions->GenerateMipmap = st_generate_mipmap; 1550 1551 functions->GetTexImage = st_GetTexImage; 1552 1553 /* compressed texture functions */ 1554 functions->CompressedTexImage2D = st_CompressedTexImage2D; 1555 functions->GetCompressedTexImage = st_GetCompressedTexImage; 1556 functions->CompressedTextureSize = _mesa_compressed_texture_size; 1557 1558 functions->NewTextureObject = st_NewTextureObject; 1559 functions->NewTextureImage = st_NewTextureImage; 1560 functions->DeleteTexture = st_DeleteTextureObject; 1561 functions->FreeTexImageData = st_FreeTextureImageData; 1562 functions->UpdateTexturePalette = 0; 1563 functions->IsTextureResident = st_IsTextureResident; 1564 1565 functions->TextureMemCpy = do_memcpy; 1566 1567 /* XXX Temporary until we can query pipe's texture sizes */ 1568 functions->TestProxyTexImage = _mesa_test_proxy_teximage; 1569} 1570