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