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