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