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