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