st_cb_texture.c revision e79054cc4090a2be346236236c9e18ae85cad43d
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#include "main/bufferobj.h" 30#if FEATURE_convolve 31#include "main/convolve.h" 32#endif 33#include "main/enums.h" 34#include "main/image.h" 35#include "main/imports.h" 36#include "main/macros.h" 37#include "main/mipmap.h" 38#include "main/pixel.h" 39#include "main/texcompress.h" 40#include "main/texformat.h" 41#include "main/texgetimage.h" 42#include "main/teximage.h" 43#include "main/texobj.h" 44#include "main/texstore.h" 45 46#include "state_tracker/st_context.h" 47#include "state_tracker/st_cb_fbo.h" 48#include "state_tracker/st_cb_texture.h" 49#include "state_tracker/st_format.h" 50#include "state_tracker/st_public.h" 51#include "state_tracker/st_texture.h" 52#include "state_tracker/st_gen_mipmap.h" 53#include "state_tracker/st_inlines.h" 54#include "state_tracker/st_atom.h" 55 56#include "pipe/p_context.h" 57#include "pipe/p_defines.h" 58#include "pipe/p_inlines.h" 59#include "pipe/p_shader_tokens.h" 60#include "util/u_tile.h" 61#include "util/u_blit.h" 62#include "util/u_surface.h" 63 64 65#define DBG if (0) printf 66 67 68static enum pipe_texture_target 69gl_target_to_pipe(GLenum target) 70{ 71 switch (target) { 72 case GL_TEXTURE_1D: 73 return PIPE_TEXTURE_1D; 74 75 case GL_TEXTURE_2D: 76 case GL_TEXTURE_RECTANGLE_NV: 77 return PIPE_TEXTURE_2D; 78 79 case GL_TEXTURE_3D: 80 return PIPE_TEXTURE_3D; 81 82 case GL_TEXTURE_CUBE_MAP_ARB: 83 return PIPE_TEXTURE_CUBE; 84 85 default: 86 assert(0); 87 return 0; 88 } 89} 90 91 92/** 93 * Return nominal bytes per texel for a compressed format, 0 for non-compressed 94 * format. 95 */ 96static GLuint 97compressed_num_bytes(GLuint mesaFormat) 98{ 99 switch(mesaFormat) { 100#if FEATURE_texture_fxt1 101 case MESA_FORMAT_RGB_FXT1: 102 case MESA_FORMAT_RGBA_FXT1: 103#endif 104#if FEATURE_texture_s3tc 105 case MESA_FORMAT_RGB_DXT1: 106 case MESA_FORMAT_RGBA_DXT1: 107 return 2; 108 case MESA_FORMAT_RGBA_DXT3: 109 case MESA_FORMAT_RGBA_DXT5: 110 return 4; 111#endif 112 default: 113 return 0; 114 } 115} 116 117 118static GLboolean 119is_compressed_mesa_format(const struct gl_texture_format *format) 120{ 121 switch (format->MesaFormat) { 122 case MESA_FORMAT_RGB_DXT1: 123 case MESA_FORMAT_RGBA_DXT1: 124 case MESA_FORMAT_RGBA_DXT3: 125 case MESA_FORMAT_RGBA_DXT5: 126 case MESA_FORMAT_SRGB_DXT1: 127 case MESA_FORMAT_SRGBA_DXT1: 128 case MESA_FORMAT_SRGBA_DXT3: 129 case MESA_FORMAT_SRGBA_DXT5: 130 return GL_TRUE; 131 default: 132 return GL_FALSE; 133 } 134} 135 136 137/** called via ctx->Driver.NewTextureImage() */ 138static struct gl_texture_image * 139st_NewTextureImage(GLcontext * ctx) 140{ 141 DBG("%s\n", __FUNCTION__); 142 (void) ctx; 143 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image); 144} 145 146 147/** called via ctx->Driver.NewTextureObject() */ 148static struct gl_texture_object * 149st_NewTextureObject(GLcontext * ctx, GLuint name, GLenum target) 150{ 151 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object); 152 153 DBG("%s\n", __FUNCTION__); 154 _mesa_initialize_texture_object(&obj->base, name, target); 155 156 return &obj->base; 157} 158 159/** called via ctx->Driver.DeleteTextureImage() */ 160static void 161st_DeleteTextureObject(GLcontext *ctx, 162 struct gl_texture_object *texObj) 163{ 164 struct st_texture_object *stObj = st_texture_object(texObj); 165 if (stObj->pt) 166 pipe_texture_reference(&stObj->pt, NULL); 167 168 _mesa_delete_texture_object(ctx, texObj); 169} 170 171 172/** called via ctx->Driver.FreeTexImageData() */ 173static void 174st_FreeTextureImageData(GLcontext * ctx, struct gl_texture_image *texImage) 175{ 176 struct st_texture_image *stImage = st_texture_image(texImage); 177 178 DBG("%s\n", __FUNCTION__); 179 180 if (stImage->pt) { 181 pipe_texture_reference(&stImage->pt, NULL); 182 } 183 184 if (texImage->Data) { 185 _mesa_align_free(texImage->Data); 186 texImage->Data = NULL; 187 } 188} 189 190 191/** 192 * From linux kernel i386 header files, copes with odd sizes better 193 * than COPY_DWORDS would: 194 * XXX Put this in src/mesa/main/imports.h ??? 195 */ 196#if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86) 197static INLINE void * 198__memcpy(void *to, const void *from, size_t n) 199{ 200 int d0, d1, d2; 201 __asm__ __volatile__("rep ; movsl\n\t" 202 "testb $2,%b4\n\t" 203 "je 1f\n\t" 204 "movsw\n" 205 "1:\ttestb $1,%b4\n\t" 206 "je 2f\n\t" 207 "movsb\n" "2:":"=&c"(d0), "=&D"(d1), "=&S"(d2) 208 :"0"(n / 4), "q"(n), "1"((long) to), "2"((long) from) 209 :"memory"); 210 return (to); 211} 212#else 213#define __memcpy(a,b,c) memcpy(a,b,c) 214#endif 215 216 217/** 218 * The system memcpy (at least on ubuntu 5.10) has problems copying 219 * to agp (writecombined) memory from a source which isn't 64-byte 220 * aligned - there is a 4x performance falloff. 221 * 222 * The x86 __memcpy is immune to this but is slightly slower 223 * (10%-ish) than the system memcpy. 224 * 225 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but 226 * isn't much faster than x86_memcpy for agp copies. 227 * 228 * TODO: switch dynamically. 229 */ 230static void * 231do_memcpy(void *dest, const void *src, size_t n) 232{ 233 if ((((unsigned long) src) & 63) || (((unsigned long) dest) & 63)) { 234 return __memcpy(dest, src, n); 235 } 236 else 237 return memcpy(dest, src, n); 238} 239 240 241static INLINE unsigned 242logbase2(unsigned n) 243{ 244 unsigned log2 = 0; 245 while (n >>= 1) 246 ++log2; 247 return log2; 248} 249 250 251/** 252 * Return default texture usage bitmask for the given texture format. 253 */ 254static GLuint 255default_usage(enum pipe_format fmt) 256{ 257 GLuint usage = PIPE_TEXTURE_USAGE_SAMPLER; 258 if (pf_is_depth_stencil(fmt)) 259 usage |= PIPE_TEXTURE_USAGE_DEPTH_STENCIL; 260 else 261 usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET; 262 return usage; 263} 264 265 266/** 267 * Allocate a pipe_texture object for the given st_texture_object using 268 * the given st_texture_image to guess the mipmap size/levels. 269 * 270 * [comments...] 271 * Otherwise, store it in memory if (Border != 0) or (any dimension == 272 * 1). 273 * 274 * Otherwise, if max_level >= level >= min_level, create texture with 275 * space for images from min_level down to max_level. 276 * 277 * Otherwise, create texture with space for images from (level 0)..(1x1). 278 * Consider pruning this texture at a validation if the saving is worth it. 279 */ 280static void 281guess_and_alloc_texture(struct st_context *st, 282 struct st_texture_object *stObj, 283 const struct st_texture_image *stImage) 284{ 285 GLuint firstLevel; 286 GLuint lastLevel; 287 GLuint width = stImage->base.Width2; /* size w/out border */ 288 GLuint height = stImage->base.Height2; 289 GLuint depth = stImage->base.Depth2; 290 GLuint i, usage; 291 enum pipe_format fmt; 292 293 DBG("%s\n", __FUNCTION__); 294 295 assert(!stObj->pt); 296 297 if (stObj->pt && 298 (GLint) stImage->level > stObj->base.BaseLevel && 299 (stImage->base.Width == 1 || 300 (stObj->base.Target != GL_TEXTURE_1D && 301 stImage->base.Height == 1) || 302 (stObj->base.Target == GL_TEXTURE_3D && 303 stImage->base.Depth == 1))) 304 return; 305 306 /* If this image disrespects BaseLevel, allocate from level zero. 307 * Usually BaseLevel == 0, so it's unlikely to happen. 308 */ 309 if ((GLint) stImage->level < stObj->base.BaseLevel) 310 firstLevel = 0; 311 else 312 firstLevel = stObj->base.BaseLevel; 313 314 315 /* Figure out image dimensions at start level. 316 */ 317 for (i = stImage->level; i > firstLevel; i--) { 318 if (width != 1) 319 width <<= 1; 320 if (height != 1) 321 height <<= 1; 322 if (depth != 1) 323 depth <<= 1; 324 } 325 326 if (width == 0 || height == 0 || depth == 0) { 327 /* no texture needed */ 328 return; 329 } 330 331 /* Guess a reasonable value for lastLevel. This is probably going 332 * to be wrong fairly often and might mean that we have to look at 333 * resizable buffers, or require that buffers implement lazy 334 * pagetable arrangements. 335 */ 336 if ((stObj->base.MinFilter == GL_NEAREST || 337 stObj->base.MinFilter == GL_LINEAR || 338 stImage->base._BaseFormat == GL_DEPTH_COMPONENT || 339 stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) && 340 stImage->level == firstLevel) { 341 lastLevel = firstLevel; 342 } 343 else { 344 GLuint l2width = logbase2(width); 345 GLuint l2height = logbase2(height); 346 GLuint l2depth = logbase2(depth); 347 lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth); 348 } 349 350 fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat->MesaFormat); 351 352 usage = default_usage(fmt); 353 354 stObj->pt = st_texture_create(st, 355 gl_target_to_pipe(stObj->base.Target), 356 fmt, 357 lastLevel, 358 width, 359 height, 360 depth, 361 usage); 362 363 DBG("%s - success\n", __FUNCTION__); 364} 365 366 367/** 368 * Adjust pixel unpack params and image dimensions to strip off the 369 * texture border. 370 * Gallium doesn't support texture borders. They've seldem been used 371 * and seldom been implemented correctly anyway. 372 * \param unpackNew returns the new pixel unpack parameters 373 */ 374static void 375strip_texture_border(GLint border, 376 GLint *width, GLint *height, GLint *depth, 377 const struct gl_pixelstore_attrib *unpack, 378 struct gl_pixelstore_attrib *unpackNew) 379{ 380 assert(border > 0); /* sanity check */ 381 382 *unpackNew = *unpack; 383 384 if (unpackNew->RowLength == 0) 385 unpackNew->RowLength = *width; 386 387 if (depth && unpackNew->ImageHeight == 0) 388 unpackNew->ImageHeight = *height; 389 390 unpackNew->SkipPixels += border; 391 if (height) 392 unpackNew->SkipRows += border; 393 if (depth) 394 unpackNew->SkipImages += border; 395 396 assert(*width >= 3); 397 *width = *width - 2 * border; 398 if (height && *height >= 3) 399 *height = *height - 2 * border; 400 if (depth && *depth >= 3) 401 *depth = *depth - 2 * border; 402} 403 404 405/** 406 * Try to do texture compression via rendering. If the Gallium driver 407 * can render into a compressed surface this will allow us to do texture 408 * compression. 409 * \return GL_TRUE for success, GL_FALSE for failure 410 */ 411static GLboolean 412compress_with_blit(GLcontext * ctx, 413 GLenum target, GLint level, 414 GLint xoffset, GLint yoffset, GLint zoffset, 415 GLint width, GLint height, GLint depth, 416 GLenum format, GLenum type, const void *pixels, 417 const struct gl_pixelstore_attrib *unpack, 418 struct gl_texture_image *texImage) 419{ 420 const GLuint dstImageOffsets[1] = {0}; 421 struct st_texture_image *stImage = st_texture_image(texImage); 422 struct pipe_screen *screen = ctx->st->pipe->screen; 423 const struct gl_texture_format *mesa_format; 424 struct pipe_texture templ; 425 struct pipe_texture *src_tex; 426 struct pipe_surface *dst_surface; 427 struct pipe_transfer *tex_xfer; 428 void *map; 429 430 431 if (!stImage->pt) { 432 /* XXX: Can this happen? Should we assert? */ 433 return GL_FALSE; 434 } 435 436 /* get destination surface (in the compressed texture) */ 437 dst_surface = screen->get_tex_surface(screen, stImage->pt, 438 stImage->face, stImage->level, 0, 439 PIPE_BUFFER_USAGE_GPU_WRITE); 440 if (!dst_surface) { 441 /* can't render into this format (or other problem) */ 442 return GL_FALSE; 443 } 444 445 /* Choose format for the temporary RGBA texture image. 446 */ 447 mesa_format = st_ChooseTextureFormat(ctx, GL_RGBA, format, type); 448 assert(mesa_format); 449 if (!mesa_format) 450 return GL_FALSE; 451 452 /* Create the temporary source texture 453 */ 454 memset(&templ, 0, sizeof(templ)); 455 templ.target = PIPE_TEXTURE_2D; 456 templ.format = st_mesa_format_to_pipe_format(mesa_format->MesaFormat); 457 pf_get_block(templ.format, &templ.block); 458 templ.width[0] = width; 459 templ.height[0] = height; 460 templ.depth[0] = 1; 461 templ.last_level = 0; 462 templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER; 463 src_tex = screen->texture_create(screen, &templ); 464 465 if (!src_tex) 466 return GL_FALSE; 467 468 /* Put user's tex data into the temporary texture 469 */ 470 tex_xfer = st_cond_flush_get_tex_transfer(st_context(ctx), src_tex, 471 0, 0, 0, /* face, level are zero */ 472 PIPE_TRANSFER_WRITE, 473 0, 0, width, height); /* x, y, w, h */ 474 map = screen->transfer_map(screen, tex_xfer); 475 476 mesa_format->StoreImage(ctx, 2, GL_RGBA, mesa_format, 477 map, /* dest ptr */ 478 0, 0, 0, /* dest x/y/z offset */ 479 tex_xfer->stride, /* dest row stride (bytes) */ 480 dstImageOffsets, /* image offsets (for 3D only) */ 481 width, height, 1, /* size */ 482 format, type, /* source format/type */ 483 pixels, /* source data */ 484 unpack); /* source data packing */ 485 486 screen->transfer_unmap(screen, tex_xfer); 487 screen->tex_transfer_destroy(tex_xfer); 488 489 /* copy / compress image */ 490 util_blit_pixels_tex(ctx->st->blit, 491 src_tex, /* pipe_texture (src) */ 492 0, 0, /* src x0, y0 */ 493 width, height, /* src x1, y1 */ 494 dst_surface, /* pipe_surface (dst) */ 495 xoffset, yoffset, /* dst x0, y0 */ 496 xoffset + width, /* dst x1 */ 497 yoffset + height, /* dst y1 */ 498 0.0, /* z */ 499 PIPE_TEX_MIPFILTER_NEAREST); 500 501 pipe_surface_reference(&dst_surface, NULL); 502 pipe_texture_reference(&src_tex, NULL); 503 504 return GL_TRUE; 505} 506 507 508/** 509 * Do glTexImage1/2/3D(). 510 */ 511static void 512st_TexImage(GLcontext * ctx, 513 GLint dims, 514 GLenum target, GLint level, 515 GLint internalFormat, 516 GLint width, GLint height, GLint depth, 517 GLint border, 518 GLenum format, GLenum type, const void *pixels, 519 const struct gl_pixelstore_attrib *unpack, 520 struct gl_texture_object *texObj, 521 struct gl_texture_image *texImage, 522 GLsizei imageSize, GLboolean compressed_src) 523{ 524 struct pipe_screen *screen = ctx->st->pipe->screen; 525 struct st_texture_object *stObj = st_texture_object(texObj); 526 struct st_texture_image *stImage = st_texture_image(texImage); 527 GLint postConvWidth, postConvHeight; 528 GLint texelBytes, sizeInBytes; 529 GLuint dstRowStride; 530 struct gl_pixelstore_attrib unpackNB; 531 enum pipe_transfer_usage transfer_usage; 532 533 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__, 534 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border); 535 536 /* gallium does not support texture borders, strip it off */ 537 if (border) { 538 strip_texture_border(border, &width, &height, &depth, unpack, &unpackNB); 539 unpack = &unpackNB; 540 texImage->Width = width; 541 texImage->Height = height; 542 texImage->Depth = depth; 543 texImage->Border = 0; 544 border = 0; 545 } 546 547 postConvWidth = width; 548 postConvHeight = height; 549 550 stImage->face = _mesa_tex_target_to_face(target); 551 stImage->level = level; 552 553#if FEATURE_convolve 554 if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) { 555 _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth, 556 &postConvHeight); 557 } 558#endif 559 560 /* choose the texture format */ 561 texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat, 562 format, type); 563 564 _mesa_set_fetch_functions(texImage, dims); 565 566 if (texImage->TexFormat->TexelBytes == 0) { 567 /* must be a compressed format */ 568 texelBytes = 0; 569 texImage->IsCompressed = GL_TRUE; 570 texImage->CompressedSize = 571 ctx->Driver.CompressedTextureSize(ctx, texImage->Width, 572 texImage->Height, texImage->Depth, 573 texImage->TexFormat->MesaFormat); 574 } 575 else { 576 texelBytes = texImage->TexFormat->TexelBytes; 577 578 /* Minimum pitch of 32 bytes */ 579 if (postConvWidth * texelBytes < 32) { 580 postConvWidth = 32 / texelBytes; 581 texImage->RowStride = postConvWidth; 582 } 583 584 /* we'll set RowStride elsewhere when the texture is a "mapped" state */ 585 /*assert(texImage->RowStride == postConvWidth);*/ 586 } 587 588 /* Release the reference to a potentially orphaned buffer. 589 * Release any old malloced memory. 590 */ 591 if (stImage->pt) { 592 pipe_texture_reference(&stImage->pt, NULL); 593 assert(!texImage->Data); 594 } 595 else if (texImage->Data) { 596 _mesa_align_free(texImage->Data); 597 } 598 599 if (width == 0 || height == 0 || depth == 0) { 600 /* stop after freeing old image */ 601 return; 602 } 603 604 /* If this is the only mipmap level in the texture, could call 605 * bmBufferData with NULL data to free the old block and avoid 606 * waiting on any outstanding fences. 607 */ 608 if (stObj->pt) { 609 if (stObj->teximage_realloc || 610 level > (GLint) stObj->pt->last_level || 611 (stObj->pt->last_level == level && 612 stObj->pt->target != PIPE_TEXTURE_CUBE && 613 !st_texture_match_image(stObj->pt, &stImage->base, 614 stImage->face, stImage->level))) { 615 DBG("release it\n"); 616 pipe_texture_reference(&stObj->pt, NULL); 617 assert(!stObj->pt); 618 stObj->teximage_realloc = FALSE; 619 } 620 } 621 622 if (!stObj->pt) { 623 guess_and_alloc_texture(ctx->st, stObj, stImage); 624 if (!stObj->pt) { 625 /* Probably out of memory. 626 * Try flushing any pending rendering, then retry. 627 */ 628 st_finish(ctx->st); 629 guess_and_alloc_texture(ctx->st, stObj, stImage); 630 if (!stObj->pt) { 631 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); 632 return; 633 } 634 } 635 } 636 637 assert(!stImage->pt); 638 639 if (stObj->pt && 640 st_texture_match_image(stObj->pt, &stImage->base, 641 stImage->face, stImage->level)) { 642 643 pipe_texture_reference(&stImage->pt, stObj->pt); 644 assert(stImage->pt); 645 } 646 647 if (!stImage->pt) 648 DBG("XXX: Image did not fit into texture - storing in local memory!\n"); 649 650 /* st_CopyTexImage calls this function with pixels == NULL, with 651 * the expectation that the texture will be set up but nothing 652 * more will be done. This is where those calls return: 653 */ 654 if (compressed_src) { 655 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels, 656 unpack, 657 "glCompressedTexImage"); 658 } 659 else { 660 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1, 661 format, type, 662 pixels, unpack, "glTexImage"); 663 } 664 if (!pixels) 665 return; 666 667 /* See if we can do texture compression with a blit/render. 668 */ 669 if (!compressed_src && 670 !ctx->Mesa_DXTn && 671 is_compressed_mesa_format(texImage->TexFormat) && 672 screen->is_format_supported(screen, 673 stImage->pt->format, 674 stImage->pt->target, 675 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) { 676 if (compress_with_blit(ctx, target, level, 0, 0, 0, width, height, depth, 677 format, type, pixels, unpack, texImage)) { 678 goto done; 679 } 680 } 681 682 if (stImage->pt) { 683 if (format == GL_DEPTH_COMPONENT && 684 pf_is_depth_and_stencil(stImage->pt->format)) 685 transfer_usage = PIPE_TRANSFER_READ_WRITE; 686 else 687 transfer_usage = PIPE_TRANSFER_WRITE; 688 689 texImage->Data = st_texture_image_map(ctx->st, stImage, 0, 690 transfer_usage, 0, 0, 691 stImage->base.Width, 692 stImage->base.Height); 693 if(stImage->transfer) 694 dstRowStride = stImage->transfer->stride; 695 } 696 else { 697 /* Allocate regular memory and store the image there temporarily. */ 698 if (texImage->IsCompressed) { 699 sizeInBytes = texImage->CompressedSize; 700 dstRowStride = 701 _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width); 702 assert(dims != 3); 703 } 704 else { 705 dstRowStride = postConvWidth * texelBytes; 706 sizeInBytes = depth * dstRowStride * postConvHeight; 707 } 708 709 texImage->Data = _mesa_align_malloc(sizeInBytes, 16); 710 } 711 712 if (!texImage->Data) { 713 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); 714 return; 715 } 716 717 DBG("Upload image %dx%dx%d row_len %x pitch %x\n", 718 width, height, depth, width * texelBytes, dstRowStride); 719 720 /* Copy data. Would like to know when it's ok for us to eg. use 721 * the blitter to copy. Or, use the hardware to do the format 722 * conversion and copy: 723 */ 724 if (compressed_src) { 725 memcpy(texImage->Data, pixels, imageSize); 726 } 727 else { 728 const GLuint srcImageStride = 729 _mesa_image_image_stride(unpack, width, height, format, type); 730 GLint i; 731 const GLubyte *src = (const GLubyte *) pixels; 732 733 for (i = 0; i < depth; i++) { 734 if (!texImage->TexFormat->StoreImage(ctx, dims, 735 texImage->_BaseFormat, 736 texImage->TexFormat, 737 texImage->Data, 738 0, 0, 0, /* dstX/Y/Zoffset */ 739 dstRowStride, 740 texImage->ImageOffsets, 741 width, height, 1, 742 format, type, src, unpack)) { 743 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); 744 } 745 746 if (stImage->pt && i + 1 < depth) { 747 /* unmap this slice */ 748 st_texture_image_unmap(ctx->st, stImage); 749 /* map next slice of 3D texture */ 750 texImage->Data = st_texture_image_map(ctx->st, stImage, i + 1, 751 transfer_usage, 0, 0, 752 stImage->base.Width, 753 stImage->base.Height); 754 src += srcImageStride; 755 } 756 } 757 } 758 759 _mesa_unmap_teximage_pbo(ctx, unpack); 760 761done: 762 if (stImage->pt && texImage->Data) { 763 st_texture_image_unmap(ctx->st, stImage); 764 texImage->Data = NULL; 765 } 766 767 if (level == texObj->BaseLevel && texObj->GenerateMipmap) { 768 ctx->Driver.GenerateMipmap(ctx, target, texObj); 769 } 770} 771 772 773static void 774st_TexImage3D(GLcontext * ctx, 775 GLenum target, GLint level, 776 GLint internalFormat, 777 GLint width, GLint height, GLint depth, 778 GLint border, 779 GLenum format, GLenum type, const void *pixels, 780 const struct gl_pixelstore_attrib *unpack, 781 struct gl_texture_object *texObj, 782 struct gl_texture_image *texImage) 783{ 784 st_TexImage(ctx, 3, target, level, internalFormat, width, height, depth, 785 border, format, type, pixels, unpack, texObj, texImage, 786 0, GL_FALSE); 787} 788 789 790static void 791st_TexImage2D(GLcontext * ctx, 792 GLenum target, GLint level, 793 GLint internalFormat, 794 GLint width, GLint height, GLint border, 795 GLenum format, GLenum type, const void *pixels, 796 const struct gl_pixelstore_attrib *unpack, 797 struct gl_texture_object *texObj, 798 struct gl_texture_image *texImage) 799{ 800 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border, 801 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE); 802} 803 804 805static void 806st_TexImage1D(GLcontext * ctx, 807 GLenum target, GLint level, 808 GLint internalFormat, 809 GLint width, GLint border, 810 GLenum format, GLenum type, const void *pixels, 811 const struct gl_pixelstore_attrib *unpack, 812 struct gl_texture_object *texObj, 813 struct gl_texture_image *texImage) 814{ 815 st_TexImage(ctx, 1, target, level, internalFormat, width, 1, 1, border, 816 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE); 817} 818 819 820static void 821st_CompressedTexImage2D(GLcontext *ctx, GLenum target, GLint level, 822 GLint internalFormat, 823 GLint width, GLint height, GLint border, 824 GLsizei imageSize, const GLvoid *data, 825 struct gl_texture_object *texObj, 826 struct gl_texture_image *texImage) 827{ 828 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border, 829 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE); 830} 831 832 833 834/** 835 * glGetTexImage() helper: decompress a compressed texture by rendering 836 * a textured quad. Store the results in the user's buffer. 837 */ 838static void 839decompress_with_blit(GLcontext * ctx, GLenum target, GLint level, 840 GLenum format, GLenum type, GLvoid *pixels, 841 struct gl_texture_object *texObj, 842 struct gl_texture_image *texImage) 843{ 844 struct pipe_screen *screen = ctx->st->pipe->screen; 845 struct st_texture_image *stImage = st_texture_image(texImage); 846 const GLuint width = texImage->Width; 847 const GLuint height = texImage->Height; 848 struct pipe_surface *dst_surface; 849 struct pipe_texture *dst_texture; 850 struct pipe_transfer *tex_xfer; 851 852 /* create temp / dest surface */ 853 if (!util_create_rgba_surface(screen, width, height, 854 &dst_texture, &dst_surface)) { 855 _mesa_problem(ctx, "util_create_rgba_surface() failed " 856 "in decompress_with_blit()"); 857 return; 858 } 859 860 /* blit/render/decompress */ 861 util_blit_pixels_tex(ctx->st->blit, 862 stImage->pt, /* pipe_texture (src) */ 863 0, 0, /* src x0, y0 */ 864 width, height, /* src x1, y1 */ 865 dst_surface, /* pipe_surface (dst) */ 866 0, 0, /* dst x0, y0 */ 867 width, height, /* dst x1, y1 */ 868 0.0, /* z */ 869 PIPE_TEX_MIPFILTER_NEAREST); 870 871 /* map the dst_surface so we can read from it */ 872 tex_xfer = st_cond_flush_get_tex_transfer(st_context(ctx), 873 dst_texture, 0, 0, 0, 874 PIPE_TRANSFER_READ, 875 0, 0, width, height); 876 877 pixels = _mesa_map_readpix_pbo(ctx, &ctx->Pack, pixels); 878 879 /* copy/pack data into user buffer */ 880 if (st_equal_formats(stImage->pt->format, format, type)) { 881 /* memcpy */ 882 const uint bytesPerRow = width * pf_get_size(stImage->pt->format); 883 ubyte *map = screen->transfer_map(screen, tex_xfer); 884 GLuint row; 885 for (row = 0; row < height; row++) { 886 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width, 887 height, format, type, row, 0); 888 memcpy(dest, map, bytesPerRow); 889 map += tex_xfer->stride; 890 } 891 screen->transfer_unmap(screen, tex_xfer); 892 } 893 else { 894 /* format translation via floats */ 895 GLuint row; 896 for (row = 0; row < height; row++) { 897 const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */ 898 GLfloat rgba[4 * MAX_WIDTH]; 899 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width, 900 height, format, type, row, 0); 901 902 /* get float[4] rgba row from surface */ 903 pipe_get_tile_rgba(tex_xfer, 0, row, width, 1, rgba); 904 905 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format, 906 type, dest, &ctx->Pack, transferOps); 907 } 908 } 909 910 _mesa_unmap_readpix_pbo(ctx, &ctx->Pack); 911 912 /* destroy the temp / dest surface */ 913 util_destroy_rgba_surface(dst_texture, dst_surface); 914} 915 916 917 918/** 919 * Need to map texture image into memory before copying image data, 920 * then unmap it. 921 */ 922static void 923st_get_tex_image(GLcontext * ctx, GLenum target, GLint level, 924 GLenum format, GLenum type, GLvoid * pixels, 925 struct gl_texture_object *texObj, 926 struct gl_texture_image *texImage, GLboolean compressed_dst) 927{ 928 struct st_texture_image *stImage = st_texture_image(texImage); 929 const GLuint dstImageStride = 930 _mesa_image_image_stride(&ctx->Pack, texImage->Width, texImage->Height, 931 format, type); 932 GLuint depth, i; 933 GLubyte *dest; 934 935 if (stImage->pt && 936 pf_is_compressed(stImage->pt->format) && 937 !compressed_dst) { 938 /* Need to decompress the texture. 939 * We'll do this by rendering a textured quad. 940 * Note that we only expect RGBA formats (no Z/depth formats). 941 */ 942 decompress_with_blit(ctx, target, level, format, type, pixels, 943 texObj, texImage); 944 return; 945 } 946 947 /* Map */ 948 if (stImage->pt) { 949 /* Image is stored in hardware format in a buffer managed by the 950 * kernel. Need to explicitly map and unmap it. 951 */ 952 unsigned face = _mesa_tex_target_to_face(target); 953 954 st_teximage_flush_before_map(ctx->st, stImage->pt, face, level, 955 PIPE_TRANSFER_READ); 956 957 texImage->Data = st_texture_image_map(ctx->st, stImage, 0, 958 PIPE_TRANSFER_READ, 0, 0, 959 stImage->base.Width, 960 stImage->base.Height); 961 texImage->RowStride = stImage->transfer->stride / stImage->pt->block.size; 962 } 963 else { 964 /* Otherwise, the image should actually be stored in 965 * texImage->Data. This is pretty confusing for 966 * everybody, I'd much prefer to separate the two functions of 967 * texImage->Data - storage for texture images in main memory 968 * and access (ie mappings) of images. In other words, we'd 969 * create a new texImage->Map field and leave Data simply for 970 * storage. 971 */ 972 assert(texImage->Data); 973 } 974 975 depth = texImage->Depth; 976 texImage->Depth = 1; 977 978 dest = (GLubyte *) pixels; 979 980 for (i = 0; i < depth; i++) { 981 if (compressed_dst) { 982 _mesa_get_compressed_teximage(ctx, target, level, dest, 983 texObj, texImage); 984 } 985 else { 986 _mesa_get_teximage(ctx, target, level, format, type, dest, 987 texObj, texImage); 988 } 989 990 if (stImage->pt && i + 1 < depth) { 991 /* unmap this slice */ 992 st_texture_image_unmap(ctx->st, stImage); 993 /* map next slice of 3D texture */ 994 texImage->Data = st_texture_image_map(ctx->st, stImage, i + 1, 995 PIPE_TRANSFER_READ, 0, 0, 996 stImage->base.Width, 997 stImage->base.Height); 998 dest += dstImageStride; 999 } 1000 } 1001 1002 texImage->Depth = depth; 1003 1004 /* Unmap */ 1005 if (stImage->pt) { 1006 st_texture_image_unmap(ctx->st, stImage); 1007 texImage->Data = NULL; 1008 } 1009} 1010 1011 1012static void 1013st_GetTexImage(GLcontext * ctx, GLenum target, GLint level, 1014 GLenum format, GLenum type, GLvoid * pixels, 1015 struct gl_texture_object *texObj, 1016 struct gl_texture_image *texImage) 1017{ 1018 st_get_tex_image(ctx, target, level, format, type, pixels, texObj, texImage, 1019 GL_FALSE); 1020} 1021 1022 1023static void 1024st_GetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level, 1025 GLvoid *pixels, 1026 struct gl_texture_object *texObj, 1027 struct gl_texture_image *texImage) 1028{ 1029 st_get_tex_image(ctx, target, level, 0, 0, pixels, texObj, texImage, 1030 GL_TRUE); 1031} 1032 1033 1034 1035static void 1036st_TexSubimage(GLcontext *ctx, GLint dims, GLenum target, GLint level, 1037 GLint xoffset, GLint yoffset, GLint zoffset, 1038 GLint width, GLint height, GLint depth, 1039 GLenum format, GLenum type, const void *pixels, 1040 const struct gl_pixelstore_attrib *packing, 1041 struct gl_texture_object *texObj, 1042 struct gl_texture_image *texImage) 1043{ 1044 struct pipe_screen *screen = ctx->st->pipe->screen; 1045 struct st_texture_image *stImage = st_texture_image(texImage); 1046 GLuint dstRowStride; 1047 const GLuint srcImageStride = 1048 _mesa_image_image_stride(packing, width, height, format, type); 1049 GLint i; 1050 const GLubyte *src; 1051 /* init to silence warning only: */ 1052 enum pipe_transfer_usage transfer_usage = PIPE_TRANSFER_WRITE; 1053 1054 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__, 1055 _mesa_lookup_enum_by_nr(target), 1056 level, xoffset, yoffset, width, height); 1057 1058 pixels = 1059 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format, 1060 type, pixels, packing, "glTexSubImage2D"); 1061 if (!pixels) 1062 return; 1063 1064 /* See if we can do texture compression with a blit/render. 1065 */ 1066 if (!ctx->Mesa_DXTn && 1067 is_compressed_mesa_format(texImage->TexFormat) && 1068 screen->is_format_supported(screen, 1069 stImage->pt->format, 1070 stImage->pt->target, 1071 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) { 1072 if (compress_with_blit(ctx, target, level, 1073 xoffset, yoffset, zoffset, 1074 width, height, depth, 1075 format, type, pixels, packing, texImage)) { 1076 goto done; 1077 } 1078 } 1079 1080 /* Map buffer if necessary. Need to lock to prevent other contexts 1081 * from uploading the buffer under us. 1082 */ 1083 if (stImage->pt) { 1084 unsigned face = _mesa_tex_target_to_face(target); 1085 1086 if (format == GL_DEPTH_COMPONENT && 1087 pf_is_depth_and_stencil(stImage->pt->format)) 1088 transfer_usage = PIPE_TRANSFER_READ_WRITE; 1089 else 1090 transfer_usage = PIPE_TRANSFER_WRITE; 1091 1092 st_teximage_flush_before_map(ctx->st, stImage->pt, face, level, 1093 transfer_usage); 1094 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset, 1095 transfer_usage, 1096 xoffset, yoffset, 1097 width, height); 1098 } 1099 1100 if (!texImage->Data) { 1101 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage"); 1102 return; 1103 } 1104 1105 src = (const GLubyte *) pixels; 1106 dstRowStride = stImage->transfer->stride; 1107 1108 for (i = 0; i < depth; i++) { 1109 if (!texImage->TexFormat->StoreImage(ctx, dims, texImage->_BaseFormat, 1110 texImage->TexFormat, 1111 texImage->Data, 1112 0, 0, 0, 1113 dstRowStride, 1114 texImage->ImageOffsets, 1115 width, height, 1, 1116 format, type, src, packing)) { 1117 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage"); 1118 } 1119 1120 if (stImage->pt && i + 1 < depth) { 1121 /* unmap this slice */ 1122 st_texture_image_unmap(ctx->st, stImage); 1123 /* map next slice of 3D texture */ 1124 texImage->Data = st_texture_image_map(ctx->st, stImage, 1125 zoffset + i + 1, 1126 transfer_usage, 1127 xoffset, yoffset, 1128 width, height); 1129 src += srcImageStride; 1130 } 1131 } 1132 1133 _mesa_unmap_teximage_pbo(ctx, packing); 1134 1135done: 1136 if (stImage->pt) { 1137 st_texture_image_unmap(ctx->st, stImage); 1138 texImage->Data = NULL; 1139 } 1140 1141 if (level == texObj->BaseLevel && texObj->GenerateMipmap) { 1142 ctx->Driver.GenerateMipmap(ctx, target, texObj); 1143 } 1144} 1145 1146 1147 1148static void 1149st_TexSubImage3D(GLcontext *ctx, GLenum target, GLint level, 1150 GLint xoffset, GLint yoffset, GLint zoffset, 1151 GLsizei width, GLsizei height, GLsizei depth, 1152 GLenum format, GLenum type, const GLvoid *pixels, 1153 const struct gl_pixelstore_attrib *packing, 1154 struct gl_texture_object *texObj, 1155 struct gl_texture_image *texImage) 1156{ 1157 st_TexSubimage(ctx, 3, target, level, xoffset, yoffset, zoffset, 1158 width, height, depth, format, type, 1159 pixels, packing, texObj, texImage); 1160} 1161 1162 1163static void 1164st_TexSubImage2D(GLcontext *ctx, GLenum target, GLint level, 1165 GLint xoffset, GLint yoffset, 1166 GLsizei width, GLsizei height, 1167 GLenum format, GLenum type, const GLvoid * pixels, 1168 const struct gl_pixelstore_attrib *packing, 1169 struct gl_texture_object *texObj, 1170 struct gl_texture_image *texImage) 1171{ 1172 st_TexSubimage(ctx, 2, target, level, xoffset, yoffset, 0, 1173 width, height, 1, format, type, 1174 pixels, packing, texObj, texImage); 1175} 1176 1177 1178static void 1179st_TexSubImage1D(GLcontext *ctx, GLenum target, GLint level, 1180 GLint xoffset, GLsizei width, GLenum format, GLenum type, 1181 const GLvoid * pixels, 1182 const struct gl_pixelstore_attrib *packing, 1183 struct gl_texture_object *texObj, 1184 struct gl_texture_image *texImage) 1185{ 1186 st_TexSubimage(ctx, 1, target, level, xoffset, 0, 0, width, 1, 1, 1187 format, type, pixels, packing, texObj, texImage); 1188} 1189 1190 1191static void 1192st_CompressedTexSubImage1D(GLcontext *ctx, GLenum target, GLint level, 1193 GLint xoffset, GLsizei width, 1194 GLenum format, 1195 GLsizei imageSize, const GLvoid *data, 1196 struct gl_texture_object *texObj, 1197 struct gl_texture_image *texImage) 1198{ 1199 assert(0); 1200} 1201 1202 1203static void 1204st_CompressedTexSubImage2D(GLcontext *ctx, GLenum target, GLint level, 1205 GLint xoffset, GLint yoffset, 1206 GLsizei width, GLint height, 1207 GLenum format, 1208 GLsizei imageSize, const GLvoid *data, 1209 struct gl_texture_object *texObj, 1210 struct gl_texture_image *texImage) 1211{ 1212 struct st_texture_image *stImage = st_texture_image(texImage); 1213 struct pipe_format_block block; 1214 int srcBlockStride; 1215 int dstBlockStride; 1216 int y; 1217 1218 if (stImage->pt) { 1219 unsigned face = _mesa_tex_target_to_face(target); 1220 1221 st_teximage_flush_before_map(ctx->st, stImage->pt, face, level, 1222 PIPE_TRANSFER_WRITE); 1223 texImage->Data = st_texture_image_map(ctx->st, stImage, 0, 1224 PIPE_TRANSFER_WRITE, 1225 xoffset, yoffset, 1226 width, height); 1227 1228 block = stImage->pt->block; 1229 srcBlockStride = pf_get_stride(&block, width); 1230 dstBlockStride = stImage->transfer->stride; 1231 } else { 1232 assert(stImage->pt); 1233 /* TODO find good values for block and strides */ 1234 /* TODO also adjust texImage->data for yoffset/xoffset */ 1235 return; 1236 } 1237 1238 if (!texImage->Data) { 1239 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage"); 1240 return; 1241 } 1242 1243 assert(xoffset % block.width == 0); 1244 assert(yoffset % block.height == 0); 1245 assert(width % block.width == 0); 1246 assert(height % block.height == 0); 1247 1248 for (y = 0; y < height; y += block.height) { 1249 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */ 1250 const char *src = (const char*)data + srcBlockStride * pf_get_nblocksy(&block, y); 1251 char *dst = (char*)texImage->Data + dstBlockStride * pf_get_nblocksy(&block, y); 1252 memcpy(dst, src, pf_get_stride(&block, width)); 1253 } 1254 1255 if (stImage->pt) { 1256 st_texture_image_unmap(ctx->st, stImage); 1257 texImage->Data = NULL; 1258 } 1259} 1260 1261 1262static void 1263st_CompressedTexSubImage3D(GLcontext *ctx, GLenum target, GLint level, 1264 GLint xoffset, GLint yoffset, GLint zoffset, 1265 GLsizei width, GLint height, GLint depth, 1266 GLenum format, 1267 GLsizei imageSize, const GLvoid *data, 1268 struct gl_texture_object *texObj, 1269 struct gl_texture_image *texImage) 1270{ 1271 assert(0); 1272} 1273 1274 1275 1276/** 1277 * Do a CopyTexSubImage operation using a read transfer from the source, 1278 * a write transfer to the destination and get_tile()/put_tile() to access 1279 * the pixels/texels. 1280 * 1281 * Note: srcY=0=TOP of renderbuffer 1282 */ 1283static void 1284fallback_copy_texsubimage(GLcontext *ctx, GLenum target, GLint level, 1285 struct st_renderbuffer *strb, 1286 struct st_texture_image *stImage, 1287 GLenum baseFormat, 1288 GLint destX, GLint destY, GLint destZ, 1289 GLint srcX, GLint srcY, 1290 GLsizei width, GLsizei height) 1291{ 1292 struct pipe_context *pipe = ctx->st->pipe; 1293 struct pipe_screen *screen = pipe->screen; 1294 struct pipe_transfer *src_trans; 1295 GLvoid *texDest; 1296 enum pipe_transfer_usage transfer_usage; 1297 1298 assert(width <= MAX_WIDTH); 1299 1300 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 1301 srcY = strb->Base.Height - srcY - height; 1302 } 1303 1304 src_trans = st_cond_flush_get_tex_transfer( st_context(ctx), 1305 strb->texture, 1306 0, 0, 0, 1307 PIPE_TRANSFER_READ, 1308 srcX, srcY, 1309 width, height); 1310 1311 if (baseFormat == GL_DEPTH_COMPONENT && 1312 pf_is_depth_and_stencil(stImage->pt->format)) 1313 transfer_usage = PIPE_TRANSFER_READ_WRITE; 1314 else 1315 transfer_usage = PIPE_TRANSFER_WRITE; 1316 1317 st_teximage_flush_before_map(ctx->st, stImage->pt, 0, 0, 1318 transfer_usage); 1319 1320 texDest = st_texture_image_map(ctx->st, stImage, 0, transfer_usage, 1321 destX, destY, width, height); 1322 1323 if (baseFormat == GL_DEPTH_COMPONENT || 1324 baseFormat == GL_DEPTH24_STENCIL8) { 1325 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F || 1326 ctx->Pixel.DepthBias != 0.0F); 1327 GLint row, yStep; 1328 1329 /* determine bottom-to-top vs. top-to-bottom order for src buffer */ 1330 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 1331 srcY = height - 1; 1332 yStep = -1; 1333 } 1334 else { 1335 srcY = 0; 1336 yStep = 1; 1337 } 1338 1339 /* To avoid a large temp memory allocation, do copy row by row */ 1340 for (row = 0; row < height; row++, srcY += yStep) { 1341 uint data[MAX_WIDTH]; 1342 pipe_get_tile_z(src_trans, 0, srcY, width, 1, data); 1343 if (scaleOrBias) { 1344 _mesa_scale_and_bias_depth_uint(ctx, width, data); 1345 } 1346 pipe_put_tile_z(stImage->transfer, 0, row, width, 1, data); 1347 } 1348 } 1349 else { 1350 /* RGBA format */ 1351 GLfloat *tempSrc = 1352 (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat)); 1353 1354 if (tempSrc && texDest) { 1355 const GLint dims = 2; 1356 const GLint dstRowStride = stImage->transfer->stride; 1357 struct gl_texture_image *texImage = &stImage->base; 1358 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking; 1359 1360 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 1361 unpack.Invert = GL_TRUE; 1362 } 1363 1364 /* get float/RGBA image from framebuffer */ 1365 /* XXX this usually involves a lot of int/float conversion. 1366 * try to avoid that someday. 1367 */ 1368 pipe_get_tile_rgba(src_trans, 0, 0, width, height, tempSrc); 1369 1370 /* Store into texture memory. 1371 * Note that this does some special things such as pixel transfer 1372 * ops and format conversion. In particular, if the dest tex format 1373 * is actually RGBA but the user created the texture as GL_RGB we 1374 * need to fill-in/override the alpha channel with 1.0. 1375 */ 1376 texImage->TexFormat->StoreImage(ctx, dims, 1377 texImage->_BaseFormat, 1378 texImage->TexFormat, 1379 texDest, 1380 0, 0, 0, 1381 dstRowStride, 1382 texImage->ImageOffsets, 1383 width, height, 1, 1384 GL_RGBA, GL_FLOAT, tempSrc, /* src */ 1385 &unpack); 1386 } 1387 else { 1388 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage"); 1389 } 1390 1391 if (tempSrc) 1392 _mesa_free(tempSrc); 1393 } 1394 1395 st_texture_image_unmap(ctx->st, stImage); 1396 screen->tex_transfer_destroy(src_trans); 1397} 1398 1399 1400static unsigned 1401compatible_src_dst_formats(const struct gl_renderbuffer *src, 1402 const struct gl_texture_image *dst) 1403{ 1404 const GLenum srcFormat = src->_BaseFormat; 1405 const GLenum dstLogicalFormat = dst->_BaseFormat; 1406 1407 if (srcFormat == dstLogicalFormat) { 1408 /* This is the same as matching_base_formats, which should 1409 * always pass, as it did previously. 1410 */ 1411 return TGSI_WRITEMASK_XYZW; 1412 } 1413 else if (srcFormat == GL_RGBA && 1414 dstLogicalFormat == GL_RGB) { 1415 /* Add a single special case to cope with RGBA->RGB transfers, 1416 * setting A to 1.0 to cope with situations where the RGB 1417 * destination is actually stored as RGBA. 1418 */ 1419 return TGSI_WRITEMASK_XYZ; /* A ==> 1.0 */ 1420 } 1421 else { 1422 /* Otherwise fail. 1423 */ 1424 return 0; 1425 } 1426} 1427 1428 1429 1430/** 1431 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible. 1432 * Note that the region to copy has already been clipped so we know we 1433 * won't read from outside the source renderbuffer's bounds. 1434 * 1435 * Note: srcY=0=Bottom of renderbuffer (GL convention) 1436 */ 1437static void 1438st_copy_texsubimage(GLcontext *ctx, 1439 GLenum target, GLint level, 1440 GLint destX, GLint destY, GLint destZ, 1441 GLint srcX, GLint srcY, 1442 GLsizei width, GLsizei height) 1443{ 1444 struct gl_texture_unit *texUnit = 1445 &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; 1446 struct gl_texture_object *texObj = 1447 _mesa_select_tex_object(ctx, texUnit, target); 1448 struct gl_texture_image *texImage = 1449 _mesa_select_tex_image(ctx, texObj, target, level); 1450 struct st_texture_image *stImage = st_texture_image(texImage); 1451 const GLenum texBaseFormat = texImage->InternalFormat; 1452 struct gl_framebuffer *fb = ctx->ReadBuffer; 1453 struct st_renderbuffer *strb; 1454 struct pipe_context *pipe = ctx->st->pipe; 1455 struct pipe_screen *screen = pipe->screen; 1456 enum pipe_format dest_format, src_format; 1457 GLboolean use_fallback = GL_TRUE; 1458 GLboolean matching_base_formats; 1459 GLuint format_writemask; 1460 struct pipe_surface *dest_surface = NULL; 1461 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP); 1462 1463 /* any rendering in progress must flushed before we grab the fb image */ 1464 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL); 1465 1466 /* make sure finalize_textures has been called? 1467 */ 1468 if (0) st_validate_state(ctx->st); 1469 1470 /* determine if copying depth or color data */ 1471 if (texBaseFormat == GL_DEPTH_COMPONENT || 1472 texBaseFormat == GL_DEPTH24_STENCIL8) { 1473 strb = st_renderbuffer(fb->_DepthBuffer); 1474 } 1475 else if (texBaseFormat == GL_DEPTH_STENCIL_EXT) { 1476 strb = st_renderbuffer(fb->_StencilBuffer); 1477 } 1478 else { 1479 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */ 1480 strb = st_renderbuffer(fb->_ColorReadBuffer); 1481 } 1482 1483 if (!strb || !strb->surface || !stImage->pt) { 1484 debug_printf("%s: null strb or stImage\n", __FUNCTION__); 1485 return; 1486 } 1487 1488 if (srcX < 0) { 1489 width -= -srcX; 1490 destX += -srcX; 1491 srcX = 0; 1492 } 1493 1494 if (srcY < 0) { 1495 height -= -srcY; 1496 destY += -srcY; 1497 srcY = 0; 1498 } 1499 1500 if (destX < 0) { 1501 width -= -destX; 1502 srcX += -destX; 1503 destX = 0; 1504 } 1505 1506 if (destY < 0) { 1507 height -= -destY; 1508 srcY += -destY; 1509 destY = 0; 1510 } 1511 1512 if (width < 0 || height < 0) 1513 return; 1514 1515 1516 assert(strb); 1517 assert(strb->surface); 1518 assert(stImage->pt); 1519 1520 src_format = strb->surface->format; 1521 dest_format = stImage->pt->format; 1522 1523 /* 1524 * Determine if the src framebuffer and dest texture have the same 1525 * base format. We need this to detect a case such as the framebuffer 1526 * being GL_RGBA but the texture being GL_RGB. If the actual hardware 1527 * texture format stores RGBA we need to set A=1 (overriding the 1528 * framebuffer's alpha values). We can't do that with the blit or 1529 * textured-quad paths. 1530 */ 1531 matching_base_formats = (strb->Base._BaseFormat == texImage->_BaseFormat); 1532 format_writemask = compatible_src_dst_formats(&strb->Base, texImage); 1533 1534 if (ctx->_ImageTransferState == 0x0) { 1535 1536 if (matching_base_formats && 1537 src_format == dest_format && 1538 !do_flip) 1539 { 1540 /* use surface_copy() / blit */ 1541 1542 dest_surface = screen->get_tex_surface(screen, stImage->pt, 1543 stImage->face, stImage->level, 1544 destZ, 1545 PIPE_BUFFER_USAGE_GPU_WRITE); 1546 1547 /* for surface_copy(), y=0=top, always */ 1548 pipe->surface_copy(pipe, 1549 /* dest */ 1550 dest_surface, 1551 destX, destY, 1552 /* src */ 1553 strb->surface, 1554 srcX, srcY, 1555 /* size */ 1556 width, height); 1557 use_fallback = GL_FALSE; 1558 } 1559 else if (format_writemask && 1560 screen->is_format_supported(screen, src_format, 1561 PIPE_TEXTURE_2D, 1562 PIPE_TEXTURE_USAGE_SAMPLER, 1563 0) && 1564 screen->is_format_supported(screen, dest_format, 1565 PIPE_TEXTURE_2D, 1566 PIPE_TEXTURE_USAGE_RENDER_TARGET, 1567 0)) { 1568 /* draw textured quad to do the copy */ 1569 GLint srcY0, srcY1; 1570 1571 dest_surface = screen->get_tex_surface(screen, stImage->pt, 1572 stImage->face, stImage->level, 1573 destZ, 1574 PIPE_BUFFER_USAGE_GPU_WRITE); 1575 1576 if (do_flip) { 1577 srcY1 = strb->Base.Height - srcY - height; 1578 srcY0 = srcY1 + height; 1579 } 1580 else { 1581 srcY0 = srcY; 1582 srcY1 = srcY0 + height; 1583 } 1584 util_blit_pixels_writemask(ctx->st->blit, 1585 strb->surface, 1586 srcX, srcY0, 1587 srcX + width, srcY1, 1588 dest_surface, 1589 destX, destY, 1590 destX + width, destY + height, 1591 0.0, PIPE_TEX_MIPFILTER_NEAREST, 1592 format_writemask); 1593 use_fallback = GL_FALSE; 1594 } 1595 1596 if (dest_surface) 1597 pipe_surface_reference(&dest_surface, NULL); 1598 } 1599 1600 if (use_fallback) { 1601 /* software fallback */ 1602 fallback_copy_texsubimage(ctx, target, level, 1603 strb, stImage, texBaseFormat, 1604 destX, destY, destZ, 1605 srcX, srcY, width, height); 1606 } 1607 1608 if (level == texObj->BaseLevel && texObj->GenerateMipmap) { 1609 ctx->Driver.GenerateMipmap(ctx, target, texObj); 1610 } 1611} 1612 1613 1614 1615static void 1616st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level, 1617 GLenum internalFormat, 1618 GLint x, GLint y, GLsizei width, GLint border) 1619{ 1620 struct gl_texture_unit *texUnit = 1621 &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; 1622 struct gl_texture_object *texObj = 1623 _mesa_select_tex_object(ctx, texUnit, target); 1624 struct gl_texture_image *texImage = 1625 _mesa_select_tex_image(ctx, texObj, target, level); 1626 1627 /* Setup or redefine the texture object, texture and texture 1628 * image. Don't populate yet. 1629 */ 1630 ctx->Driver.TexImage1D(ctx, target, level, internalFormat, 1631 width, border, 1632 GL_RGBA, CHAN_TYPE, NULL, 1633 &ctx->DefaultPacking, texObj, texImage); 1634 1635 st_copy_texsubimage(ctx, target, level, 1636 0, 0, 0, /* destX,Y,Z */ 1637 x, y, width, 1); /* src X, Y, size */ 1638} 1639 1640 1641static void 1642st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level, 1643 GLenum internalFormat, 1644 GLint x, GLint y, GLsizei width, GLsizei height, 1645 GLint border) 1646{ 1647 struct gl_texture_unit *texUnit = 1648 &ctx->Texture.Unit[ctx->Texture.CurrentUnit]; 1649 struct gl_texture_object *texObj = 1650 _mesa_select_tex_object(ctx, texUnit, target); 1651 struct gl_texture_image *texImage = 1652 _mesa_select_tex_image(ctx, texObj, target, level); 1653 1654 /* Setup or redefine the texture object, texture and texture 1655 * image. Don't populate yet. 1656 */ 1657 ctx->Driver.TexImage2D(ctx, target, level, internalFormat, 1658 width, height, border, 1659 GL_RGBA, CHAN_TYPE, NULL, 1660 &ctx->DefaultPacking, texObj, texImage); 1661 1662 st_copy_texsubimage(ctx, target, level, 1663 0, 0, 0, /* destX,Y,Z */ 1664 x, y, width, height); /* src X, Y, size */ 1665} 1666 1667 1668static void 1669st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level, 1670 GLint xoffset, GLint x, GLint y, GLsizei width) 1671{ 1672 const GLint yoffset = 0, zoffset = 0; 1673 const GLsizei height = 1; 1674 st_copy_texsubimage(ctx, target, level, 1675 xoffset, yoffset, zoffset, /* destX,Y,Z */ 1676 x, y, width, height); /* src X, Y, size */ 1677} 1678 1679 1680static void 1681st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level, 1682 GLint xoffset, GLint yoffset, 1683 GLint x, GLint y, GLsizei width, GLsizei height) 1684{ 1685 const GLint zoffset = 0; 1686 st_copy_texsubimage(ctx, target, level, 1687 xoffset, yoffset, zoffset, /* destX,Y,Z */ 1688 x, y, width, height); /* src X, Y, size */ 1689} 1690 1691 1692static void 1693st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level, 1694 GLint xoffset, GLint yoffset, GLint zoffset, 1695 GLint x, GLint y, GLsizei width, GLsizei height) 1696{ 1697 st_copy_texsubimage(ctx, target, level, 1698 xoffset, yoffset, zoffset, /* destX,Y,Z */ 1699 x, y, width, height); /* src X, Y, size */ 1700} 1701 1702 1703/** 1704 * Compute which mipmap levels that really need to be sent to the hardware. 1705 * This depends on the base image size, GL_TEXTURE_MIN_LOD, 1706 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL. 1707 */ 1708static void 1709calculate_first_last_level(struct st_texture_object *stObj) 1710{ 1711 struct gl_texture_object *tObj = &stObj->base; 1712 1713 /* These must be signed values. MinLod and MaxLod can be negative numbers, 1714 * and having firstLevel and lastLevel as signed prevents the need for 1715 * extra sign checks. 1716 */ 1717 GLint firstLevel; 1718 GLint lastLevel; 1719 1720 /* Yes, this looks overly complicated, but it's all needed. 1721 */ 1722 switch (tObj->Target) { 1723 case GL_TEXTURE_1D: 1724 case GL_TEXTURE_2D: 1725 case GL_TEXTURE_3D: 1726 case GL_TEXTURE_CUBE_MAP: 1727 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) { 1728 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL. 1729 */ 1730 firstLevel = lastLevel = tObj->BaseLevel; 1731 } 1732 else { 1733 firstLevel = 0; 1734 lastLevel = MIN2(tObj->MaxLevel, 1735 (int) tObj->Image[0][tObj->BaseLevel]->WidthLog2); 1736 } 1737 break; 1738 case GL_TEXTURE_RECTANGLE_NV: 1739 case GL_TEXTURE_4D_SGIS: 1740 firstLevel = lastLevel = 0; 1741 break; 1742 default: 1743 return; 1744 } 1745 1746 stObj->lastLevel = lastLevel; 1747} 1748 1749 1750static void 1751copy_image_data_to_texture(struct st_context *st, 1752 struct st_texture_object *stObj, 1753 GLuint dstLevel, 1754 struct st_texture_image *stImage) 1755{ 1756 if (stImage->pt) { 1757 /* Copy potentially with the blitter: 1758 */ 1759 st_texture_image_copy(st->pipe, 1760 stObj->pt, dstLevel, /* dest texture, level */ 1761 stImage->pt, /* src texture */ 1762 stImage->face 1763 ); 1764 1765 pipe_texture_reference(&stImage->pt, NULL); 1766 } 1767 else if (stImage->base.Data) { 1768 assert(stImage->base.Data != NULL); 1769 1770 /* More straightforward upload. 1771 */ 1772 1773 st_teximage_flush_before_map(st, stObj->pt, stImage->face, dstLevel, 1774 PIPE_TRANSFER_WRITE); 1775 1776 1777 st_texture_image_data(st, 1778 stObj->pt, 1779 stImage->face, 1780 dstLevel, 1781 stImage->base.Data, 1782 stImage->base.RowStride * 1783 stObj->pt->block.size, 1784 stImage->base.RowStride * 1785 stImage->base.Height * 1786 stObj->pt->block.size); 1787 _mesa_align_free(stImage->base.Data); 1788 stImage->base.Data = NULL; 1789 } 1790 1791 pipe_texture_reference(&stImage->pt, stObj->pt); 1792} 1793 1794 1795/** 1796 * Called during state validation. When this function is finished, 1797 * the texture object should be ready for rendering. 1798 * \return GL_TRUE for success, GL_FALSE for failure (out of mem) 1799 */ 1800GLboolean 1801st_finalize_texture(GLcontext *ctx, 1802 struct pipe_context *pipe, 1803 struct gl_texture_object *tObj, 1804 GLboolean *needFlush) 1805{ 1806 struct st_texture_object *stObj = st_texture_object(tObj); 1807 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1; 1808 GLuint cpp, face; 1809 struct st_texture_image *firstImage; 1810 1811 *needFlush = GL_FALSE; 1812 1813 /* We know/require this is true by now: 1814 */ 1815 assert(stObj->base._Complete); 1816 1817 /* What levels must the texture include at a minimum? 1818 */ 1819 calculate_first_last_level(stObj); 1820 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]); 1821 1822 /* If both firstImage and stObj point to a texture which can contain 1823 * all active images, favour firstImage. Note that because of the 1824 * completeness requirement, we know that the image dimensions 1825 * will match. 1826 */ 1827 if (firstImage->pt && 1828 firstImage->pt != stObj->pt && 1829 firstImage->pt->last_level >= stObj->lastLevel) { 1830 pipe_texture_reference(&stObj->pt, firstImage->pt); 1831 } 1832 1833 /* FIXME: determine format block instead of cpp */ 1834 if (firstImage->base.IsCompressed) { 1835 cpp = compressed_num_bytes(firstImage->base.TexFormat->MesaFormat); 1836 } 1837 else { 1838 cpp = firstImage->base.TexFormat->TexelBytes; 1839 } 1840 1841 /* If we already have a gallium texture, check that it matches the texture 1842 * object's format, target, size, num_levels, etc. 1843 */ 1844 if (stObj->pt) { 1845 const enum pipe_format fmt = 1846 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat); 1847 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) || 1848 stObj->pt->format != fmt || 1849 stObj->pt->last_level < stObj->lastLevel || 1850 stObj->pt->width[0] != firstImage->base.Width2 || 1851 stObj->pt->height[0] != firstImage->base.Height2 || 1852 stObj->pt->depth[0] != firstImage->base.Depth2 || 1853 /* Nominal bytes per pixel: */ 1854 stObj->pt->block.size / stObj->pt->block.width != cpp) 1855 { 1856 pipe_texture_reference(&stObj->pt, NULL); 1857 ctx->st->dirty.st |= ST_NEW_FRAMEBUFFER; 1858 } 1859 } 1860 1861 /* May need to create a new gallium texture: 1862 */ 1863 if (!stObj->pt) { 1864 const enum pipe_format fmt = 1865 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat); 1866 GLuint usage = default_usage(fmt); 1867 1868 stObj->pt = st_texture_create(ctx->st, 1869 gl_target_to_pipe(stObj->base.Target), 1870 fmt, 1871 stObj->lastLevel, 1872 firstImage->base.Width2, 1873 firstImage->base.Height2, 1874 firstImage->base.Depth2, 1875 usage); 1876 1877 if (!stObj->pt) { 1878 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); 1879 return GL_FALSE; 1880 } 1881 } 1882 1883 /* Pull in any images not in the object's texture: 1884 */ 1885 for (face = 0; face < nr_faces; face++) { 1886 GLuint level; 1887 for (level = 0; level <= stObj->lastLevel; level++) { 1888 struct st_texture_image *stImage = 1889 st_texture_image(stObj->base.Image[face][stObj->base.BaseLevel + level]); 1890 1891 /* Need to import images in main memory or held in other textures. 1892 */ 1893 if (stImage && stObj->pt != stImage->pt) { 1894 copy_image_data_to_texture(ctx->st, stObj, level, stImage); 1895 *needFlush = GL_TRUE; 1896 } 1897 } 1898 } 1899 1900 return GL_TRUE; 1901} 1902 1903 1904/** 1905 * Returns pointer to a default/dummy texture. 1906 * This is typically used when the current shader has tex/sample instructions 1907 * but the user has not provided a (any) texture(s). 1908 */ 1909struct gl_texture_object * 1910st_get_default_texture(struct st_context *st) 1911{ 1912 if (!st->default_texture) { 1913 static const GLenum target = GL_TEXTURE_2D; 1914 GLubyte pixels[16][16][4]; 1915 struct gl_texture_object *texObj; 1916 struct gl_texture_image *texImg; 1917 GLuint i, j; 1918 1919 /* The ARB_fragment_program spec says (0,0,0,1) should be returned 1920 * when attempting to sample incomplete textures. 1921 */ 1922 for (i = 0; i < 16; i++) { 1923 for (j = 0; j < 16; j++) { 1924 pixels[i][j][0] = 0; 1925 pixels[i][j][1] = 0; 1926 pixels[i][j][2] = 0; 1927 pixels[i][j][3] = 255; 1928 } 1929 } 1930 1931 texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target); 1932 1933 texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0); 1934 1935 _mesa_init_teximage_fields(st->ctx, target, texImg, 1936 16, 16, 1, 0, /* w, h, d, border */ 1937 GL_RGBA); 1938 1939 st_TexImage(st->ctx, 2, target, 1940 0, GL_RGBA, /* level, intformat */ 1941 16, 16, 1, 0, /* w, h, d, border */ 1942 GL_RGBA, GL_UNSIGNED_BYTE, pixels, 1943 &st->ctx->DefaultPacking, 1944 texObj, texImg, 1945 0, 0); 1946 1947 texObj->MinFilter = GL_NEAREST; 1948 texObj->MagFilter = GL_NEAREST; 1949 texObj->_Complete = GL_TRUE; 1950 1951 st->default_texture = texObj; 1952 } 1953 return st->default_texture; 1954} 1955 1956 1957void 1958st_init_texture_functions(struct dd_function_table *functions) 1959{ 1960 functions->ChooseTextureFormat = st_ChooseTextureFormat; 1961 functions->TexImage1D = st_TexImage1D; 1962 functions->TexImage2D = st_TexImage2D; 1963 functions->TexImage3D = st_TexImage3D; 1964 functions->TexSubImage1D = st_TexSubImage1D; 1965 functions->TexSubImage2D = st_TexSubImage2D; 1966 functions->TexSubImage3D = st_TexSubImage3D; 1967 functions->CompressedTexSubImage1D = st_CompressedTexSubImage1D; 1968 functions->CompressedTexSubImage2D = st_CompressedTexSubImage2D; 1969 functions->CompressedTexSubImage3D = st_CompressedTexSubImage3D; 1970 functions->CopyTexImage1D = st_CopyTexImage1D; 1971 functions->CopyTexImage2D = st_CopyTexImage2D; 1972 functions->CopyTexSubImage1D = st_CopyTexSubImage1D; 1973 functions->CopyTexSubImage2D = st_CopyTexSubImage2D; 1974 functions->CopyTexSubImage3D = st_CopyTexSubImage3D; 1975 functions->GenerateMipmap = st_generate_mipmap; 1976 1977 functions->GetTexImage = st_GetTexImage; 1978 1979 /* compressed texture functions */ 1980 functions->CompressedTexImage2D = st_CompressedTexImage2D; 1981 functions->GetCompressedTexImage = st_GetCompressedTexImage; 1982 functions->CompressedTextureSize = _mesa_compressed_texture_size; 1983 1984 functions->NewTextureObject = st_NewTextureObject; 1985 functions->NewTextureImage = st_NewTextureImage; 1986 functions->DeleteTexture = st_DeleteTextureObject; 1987 functions->FreeTexImageData = st_FreeTextureImageData; 1988 functions->UpdateTexturePalette = 0; 1989 1990 functions->TextureMemCpy = do_memcpy; 1991 1992 /* XXX Temporary until we can query pipe's texture sizes */ 1993 functions->TestProxyTexImage = _mesa_test_proxy_teximage; 1994} 1995