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