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