st_cb_texture.c revision 6dd839f23a8ee7b6853f7320a2715df44040b358
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 case GL_PROXY_TEXTURE_1D: 77 return PIPE_TEXTURE_1D; 78 case GL_TEXTURE_2D: 79 case GL_PROXY_TEXTURE_2D: 80 case GL_TEXTURE_EXTERNAL_OES: 81 return PIPE_TEXTURE_2D; 82 case GL_TEXTURE_RECTANGLE_NV: 83 case GL_PROXY_TEXTURE_RECTANGLE_NV: 84 return PIPE_TEXTURE_RECT; 85 case GL_TEXTURE_3D: 86 case GL_PROXY_TEXTURE_3D: 87 return PIPE_TEXTURE_3D; 88 case GL_TEXTURE_CUBE_MAP_ARB: 89 case GL_PROXY_TEXTURE_CUBE_MAP_ARB: 90 case GL_TEXTURE_CUBE_MAP_POSITIVE_X: 91 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: 92 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: 93 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: 94 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: 95 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: 96 return PIPE_TEXTURE_CUBE; 97 case GL_TEXTURE_1D_ARRAY_EXT: 98 case GL_PROXY_TEXTURE_1D_ARRAY_EXT: 99 return PIPE_TEXTURE_1D_ARRAY; 100 case GL_TEXTURE_2D_ARRAY_EXT: 101 case GL_PROXY_TEXTURE_2D_ARRAY_EXT: 102 return PIPE_TEXTURE_2D_ARRAY; 103 case GL_TEXTURE_BUFFER: 104 return PIPE_BUFFER; 105 case GL_TEXTURE_CUBE_MAP_ARRAY: 106 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY: 107 return PIPE_TEXTURE_CUBE_ARRAY; 108 default: 109 assert(0); 110 return 0; 111 } 112} 113 114 115/** called via ctx->Driver.NewTextureImage() */ 116static struct gl_texture_image * 117st_NewTextureImage(struct gl_context * ctx) 118{ 119 DBG("%s\n", __FUNCTION__); 120 (void) ctx; 121 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image); 122} 123 124 125/** called via ctx->Driver.DeleteTextureImage() */ 126static void 127st_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img) 128{ 129 /* nothing special (yet) for st_texture_image */ 130 _mesa_delete_texture_image(ctx, img); 131} 132 133 134/** called via ctx->Driver.NewTextureObject() */ 135static struct gl_texture_object * 136st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target) 137{ 138 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object); 139 140 DBG("%s\n", __FUNCTION__); 141 _mesa_initialize_texture_object(&obj->base, name, target); 142 143 return &obj->base; 144} 145 146/** called via ctx->Driver.DeleteTextureObject() */ 147static void 148st_DeleteTextureObject(struct gl_context *ctx, 149 struct gl_texture_object *texObj) 150{ 151 struct st_context *st = st_context(ctx); 152 struct st_texture_object *stObj = st_texture_object(texObj); 153 if (stObj->pt) 154 pipe_resource_reference(&stObj->pt, NULL); 155 if (stObj->sampler_view) { 156 pipe_sampler_view_release(st->pipe, &stObj->sampler_view); 157 } 158 _mesa_delete_texture_object(ctx, texObj); 159} 160 161 162/** called via ctx->Driver.FreeTextureImageBuffer() */ 163static void 164st_FreeTextureImageBuffer(struct gl_context *ctx, 165 struct gl_texture_image *texImage) 166{ 167 struct st_texture_image *stImage = st_texture_image(texImage); 168 169 DBG("%s\n", __FUNCTION__); 170 171 if (stImage->pt) { 172 pipe_resource_reference(&stImage->pt, NULL); 173 } 174 175 if (stImage->TexData) { 176 _mesa_align_free(stImage->TexData); 177 stImage->TexData = NULL; 178 } 179} 180 181 182/** called via ctx->Driver.MapTextureImage() */ 183static void 184st_MapTextureImage(struct gl_context *ctx, 185 struct gl_texture_image *texImage, 186 GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h, 187 GLbitfield mode, 188 GLubyte **mapOut, GLint *rowStrideOut) 189{ 190 struct st_context *st = st_context(ctx); 191 struct st_texture_image *stImage = st_texture_image(texImage); 192 unsigned pipeMode; 193 GLubyte *map; 194 195 pipeMode = 0x0; 196 if (mode & GL_MAP_READ_BIT) 197 pipeMode |= PIPE_TRANSFER_READ; 198 if (mode & GL_MAP_WRITE_BIT) 199 pipeMode |= PIPE_TRANSFER_WRITE; 200 if (mode & GL_MAP_INVALIDATE_RANGE_BIT) 201 pipeMode |= PIPE_TRANSFER_DISCARD_RANGE; 202 203 map = st_texture_image_map(st, stImage, slice, pipeMode, x, y, w, h); 204 if (map) { 205 *mapOut = map; 206 *rowStrideOut = stImage->transfer->stride; 207 } 208 else { 209 *mapOut = NULL; 210 *rowStrideOut = 0; 211 } 212} 213 214 215/** called via ctx->Driver.UnmapTextureImage() */ 216static void 217st_UnmapTextureImage(struct gl_context *ctx, 218 struct gl_texture_image *texImage, 219 GLuint slice) 220{ 221 struct st_context *st = st_context(ctx); 222 struct st_texture_image *stImage = st_texture_image(texImage); 223 st_texture_image_unmap(st, stImage); 224} 225 226 227/** 228 * Return default texture resource binding bitmask for the given format. 229 */ 230static GLuint 231default_bindings(struct st_context *st, enum pipe_format format) 232{ 233 struct pipe_screen *screen = st->pipe->screen; 234 const unsigned target = PIPE_TEXTURE_2D; 235 unsigned bindings; 236 237 if (util_format_is_depth_or_stencil(format)) 238 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL; 239 else 240 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET; 241 242 if (screen->is_format_supported(screen, format, target, 0, bindings)) 243 return bindings; 244 else { 245 /* Try non-sRGB. */ 246 format = util_format_linear(format); 247 248 if (screen->is_format_supported(screen, format, target, 0, bindings)) 249 return bindings; 250 else 251 return PIPE_BIND_SAMPLER_VIEW; 252 } 253} 254 255 256/** 257 * Given the size of a mipmap image, try to compute the size of the level=0 258 * mipmap image. 259 * 260 * Note that this isn't always accurate for odd-sized, non-POW textures. 261 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81. 262 * 263 * \return GL_TRUE for success, GL_FALSE for failure 264 */ 265static GLboolean 266guess_base_level_size(GLenum target, 267 GLuint width, GLuint height, GLuint depth, GLuint level, 268 GLuint *width0, GLuint *height0, GLuint *depth0) 269{ 270 assert(width >= 1); 271 assert(height >= 1); 272 assert(depth >= 1); 273 274 if (level > 0) { 275 /* Guess the size of the base level. 276 * Depending on the image's size, we can't always make a guess here. 277 */ 278 switch (target) { 279 case GL_TEXTURE_1D: 280 case GL_TEXTURE_1D_ARRAY: 281 width <<= level; 282 break; 283 284 case GL_TEXTURE_2D: 285 case GL_TEXTURE_2D_ARRAY: 286 /* We can't make a good guess here, because the base level dimensions 287 * can be non-square. 288 */ 289 if (width == 1 || height == 1) { 290 return GL_FALSE; 291 } 292 width <<= level; 293 height <<= level; 294 break; 295 296 case GL_TEXTURE_CUBE_MAP: 297 case GL_TEXTURE_CUBE_MAP_ARRAY: 298 width <<= level; 299 height <<= level; 300 break; 301 302 case GL_TEXTURE_3D: 303 /* We can't make a good guess here, because the base level dimensions 304 * can be non-cube. 305 */ 306 if (width == 1 || height == 1 || depth == 1) { 307 return GL_FALSE; 308 } 309 width <<= level; 310 height <<= level; 311 depth <<= level; 312 break; 313 314 case GL_TEXTURE_RECTANGLE: 315 break; 316 317 default: 318 assert(0); 319 } 320 } 321 322 *width0 = width; 323 *height0 = height; 324 *depth0 = depth; 325 326 return GL_TRUE; 327} 328 329 330/** 331 * Try to allocate a pipe_resource object for the given st_texture_object. 332 * 333 * We use the given st_texture_image as a clue to determine the size of the 334 * mipmap image at level=0. 335 * 336 * \return GL_TRUE for success, GL_FALSE if out of memory. 337 */ 338static GLboolean 339guess_and_alloc_texture(struct st_context *st, 340 struct st_texture_object *stObj, 341 const struct st_texture_image *stImage) 342{ 343 GLuint lastLevel, width, height, depth; 344 GLuint bindings; 345 GLuint ptWidth, ptHeight, ptDepth, ptLayers; 346 enum pipe_format fmt; 347 348 DBG("%s\n", __FUNCTION__); 349 350 assert(!stObj->pt); 351 352 if (!guess_base_level_size(stObj->base.Target, 353 stImage->base.Width2, 354 stImage->base.Height2, 355 stImage->base.Depth2, 356 stImage->base.Level, 357 &width, &height, &depth)) { 358 /* we can't determine the image size at level=0 */ 359 stObj->width0 = stObj->height0 = stObj->depth0 = 0; 360 /* this is not an out of memory error */ 361 return GL_TRUE; 362 } 363 364 /* At this point, (width x height x depth) is the expected size of 365 * the level=0 mipmap image. 366 */ 367 368 /* Guess a reasonable value for lastLevel. With OpenGL we have no 369 * idea how many mipmap levels will be in a texture until we start 370 * to render with it. Make an educated guess here but be prepared 371 * to re-allocating a texture buffer with space for more (or fewer) 372 * mipmap levels later. 373 */ 374 if ((stObj->base.Sampler.MinFilter == GL_NEAREST || 375 stObj->base.Sampler.MinFilter == GL_LINEAR || 376 (stObj->base.BaseLevel == 0 && 377 stObj->base.MaxLevel == 0) || 378 stImage->base._BaseFormat == GL_DEPTH_COMPONENT || 379 stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) && 380 !stObj->base.GenerateMipmap && 381 stImage->base.Level == 0) { 382 /* only alloc space for a single mipmap level */ 383 lastLevel = 0; 384 } 385 else { 386 /* alloc space for a full mipmap */ 387 lastLevel = _mesa_get_tex_max_num_levels(stObj->base.Target, 388 width, height, depth) - 1; 389 } 390 391 /* Save the level=0 dimensions */ 392 stObj->width0 = width; 393 stObj->height0 = height; 394 stObj->depth0 = depth; 395 396 fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat); 397 398 bindings = default_bindings(st, fmt); 399 400 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, 401 width, height, depth, 402 &ptWidth, &ptHeight, &ptDepth, &ptLayers); 403 404 stObj->pt = st_texture_create(st, 405 gl_target_to_pipe(stObj->base.Target), 406 fmt, 407 lastLevel, 408 ptWidth, 409 ptHeight, 410 ptDepth, 411 ptLayers, 412 bindings); 413 414 stObj->lastLevel = lastLevel; 415 416 DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL)); 417 418 return stObj->pt != NULL; 419} 420 421 422/** 423 * Called via ctx->Driver.AllocTextureImageBuffer(). 424 * If the texture object/buffer already has space for the indicated image, 425 * we're done. Otherwise, allocate memory for the new texture image. 426 */ 427static GLboolean 428st_AllocTextureImageBuffer(struct gl_context *ctx, 429 struct gl_texture_image *texImage) 430{ 431 struct st_context *st = st_context(ctx); 432 struct st_texture_image *stImage = st_texture_image(texImage); 433 struct st_texture_object *stObj = st_texture_object(texImage->TexObject); 434 const GLuint level = texImage->Level; 435 GLuint width = texImage->Width; 436 GLuint height = texImage->Height; 437 GLuint depth = texImage->Depth; 438 439 DBG("%s\n", __FUNCTION__); 440 441 assert(!stImage->TexData); 442 assert(!stImage->pt); /* xxx this might be wrong */ 443 444 /* Look if the parent texture object has space for this image */ 445 if (stObj->pt && 446 level <= stObj->pt->last_level && 447 st_texture_match_image(stObj->pt, texImage)) { 448 /* this image will fit in the existing texture object's memory */ 449 pipe_resource_reference(&stImage->pt, stObj->pt); 450 return GL_TRUE; 451 } 452 453 /* The parent texture object does not have space for this image */ 454 455 pipe_resource_reference(&stObj->pt, NULL); 456 pipe_sampler_view_release(st->pipe, &stObj->sampler_view); 457 458 if (!guess_and_alloc_texture(st, stObj, stImage)) { 459 /* Probably out of memory. 460 * Try flushing any pending rendering, then retry. 461 */ 462 st_finish(st); 463 if (!guess_and_alloc_texture(st, stObj, stImage)) { 464 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); 465 return GL_FALSE; 466 } 467 } 468 469 if (stObj->pt && 470 st_texture_match_image(stObj->pt, texImage)) { 471 /* The image will live in the object's mipmap memory */ 472 pipe_resource_reference(&stImage->pt, stObj->pt); 473 assert(stImage->pt); 474 return GL_TRUE; 475 } 476 else { 477 /* Create a new, temporary texture/resource/buffer to hold this 478 * one texture image. Note that when we later access this image 479 * (either for mapping or copying) we'll want to always specify 480 * mipmap level=0, even if the image represents some other mipmap 481 * level. 482 */ 483 enum pipe_format format = 484 st_mesa_format_to_pipe_format(texImage->TexFormat); 485 GLuint bindings = default_bindings(st, format); 486 GLuint ptWidth, ptHeight, ptDepth, ptLayers; 487 488 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, 489 width, height, depth, 490 &ptWidth, &ptHeight, &ptDepth, &ptLayers); 491 492 stImage->pt = st_texture_create(st, 493 gl_target_to_pipe(stObj->base.Target), 494 format, 495 0, /* lastLevel */ 496 ptWidth, 497 ptHeight, 498 ptDepth, 499 ptLayers, 500 bindings); 501 return stImage->pt != NULL; 502 } 503} 504 505 506/** 507 * Preparation prior to glTexImage. Basically check the 'surface_based' 508 * field and switch to a "normal" tex image if necessary. 509 */ 510static void 511prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage, 512 GLenum format, GLenum type) 513{ 514 struct gl_texture_object *texObj = texImage->TexObject; 515 struct st_texture_object *stObj = st_texture_object(texObj); 516 517 /* switch to "normal" */ 518 if (stObj->surface_based) { 519 const GLenum target = texObj->Target; 520 const GLuint level = texImage->Level; 521 gl_format texFormat; 522 523 _mesa_clear_texture_object(ctx, texObj); 524 pipe_resource_reference(&stObj->pt, NULL); 525 526 /* oops, need to init this image again */ 527 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level, 528 texImage->InternalFormat, format, 529 type); 530 531 _mesa_init_teximage_fields(ctx, texImage, 532 texImage->Width, texImage->Height, 533 texImage->Depth, texImage->Border, 534 texImage->InternalFormat, texFormat); 535 536 stObj->surface_based = GL_FALSE; 537 } 538} 539 540 541static void 542st_TexImage(struct gl_context * ctx, GLuint dims, 543 struct gl_texture_image *texImage, 544 GLenum format, GLenum type, const void *pixels, 545 const struct gl_pixelstore_attrib *unpack) 546{ 547 prep_teximage(ctx, texImage, format, type); 548 _mesa_store_teximage(ctx, dims, texImage, format, type, pixels, unpack); 549} 550 551 552static void 553st_CompressedTexImage(struct gl_context *ctx, GLuint dims, 554 struct gl_texture_image *texImage, 555 GLsizei imageSize, const GLvoid *data) 556{ 557 prep_teximage(ctx, texImage, GL_NONE, GL_NONE); 558 _mesa_store_compressed_teximage(ctx, dims, texImage, imageSize, data); 559} 560 561 562 563/** 564 * glGetTexImage() helper: decompress a compressed texture by rendering 565 * a textured quad. Store the results in the user's buffer. 566 */ 567static void 568decompress_with_blit(struct gl_context * ctx, 569 GLenum format, GLenum type, GLvoid *pixels, 570 struct gl_texture_image *texImage) 571{ 572 struct st_context *st = st_context(ctx); 573 struct pipe_context *pipe = st->pipe; 574 struct st_texture_image *stImage = st_texture_image(texImage); 575 struct st_texture_object *stObj = st_texture_object(texImage->TexObject); 576 const GLuint width = texImage->Width; 577 const GLuint height = texImage->Height; 578 struct pipe_resource *dst_texture; 579 struct pipe_blit_info blit; 580 unsigned bind = (PIPE_BIND_RENDER_TARGET | PIPE_BIND_TRANSFER_READ); 581 struct pipe_transfer *tex_xfer; 582 ubyte *map; 583 584 /* create temp / dest surface */ 585 if (!util_create_rgba_texture(pipe, width, height, bind, 586 &dst_texture)) { 587 _mesa_problem(ctx, "util_create_rgba_texture() failed " 588 "in decompress_with_blit()"); 589 return; 590 } 591 592 blit.src.resource = stObj->pt; 593 blit.src.level = texImage->Level; 594 blit.src.format = util_format_linear(stObj->pt->format); 595 blit.dst.resource = dst_texture; 596 blit.dst.level = 0; 597 blit.dst.format = dst_texture->format; 598 blit.src.box.x = blit.dst.box.x = 0; 599 blit.src.box.y = blit.dst.box.y = 0; 600 blit.src.box.z = 0; /* XXX compressed array textures? */ 601 blit.dst.box.z = 0; 602 blit.src.box.width = blit.dst.box.width = width; 603 blit.src.box.height = blit.dst.box.height = height; 604 blit.src.box.depth = blit.dst.box.depth = 1; 605 blit.mask = PIPE_MASK_RGBA; 606 blit.filter = PIPE_TEX_FILTER_NEAREST; 607 blit.scissor_enable = FALSE; 608 609 /* blit/render/decompress */ 610 st->pipe->blit(st->pipe, &blit); 611 612 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels); 613 614 map = pipe_transfer_map(pipe, dst_texture, 0, 0, 615 PIPE_TRANSFER_READ, 616 0, 0, width, height, &tex_xfer); 617 if (!map) { 618 goto end; 619 } 620 621 /* copy/pack data into user buffer */ 622 if (_mesa_format_matches_format_and_type(stImage->base.TexFormat, 623 format, type, 624 ctx->Pack.SwapBytes)) { 625 /* memcpy */ 626 const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format); 627 /* map the dst_surface so we can read from it */ 628 GLuint row; 629 for (row = 0; row < height; row++) { 630 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width, 631 height, format, type, row, 0); 632 memcpy(dest, map, bytesPerRow); 633 map += tex_xfer->stride; 634 } 635 pipe_transfer_unmap(pipe, tex_xfer); 636 } 637 else { 638 /* format translation via floats */ 639 GLuint row; 640 enum pipe_format pformat = util_format_linear(dst_texture->format); 641 GLfloat *rgba; 642 643 rgba = malloc(width * 4 * sizeof(GLfloat)); 644 if (!rgba) { 645 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()"); 646 goto end; 647 } 648 649 for (row = 0; row < height; row++) { 650 const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */ 651 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width, 652 height, format, type, row, 0); 653 654 if (ST_DEBUG & DEBUG_FALLBACK) 655 debug_printf("%s: fallback format translation\n", __FUNCTION__); 656 657 /* get float[4] rgba row from surface */ 658 pipe_get_tile_rgba_format(tex_xfer, map, 0, row, width, 1, 659 pformat, rgba); 660 661 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format, 662 type, dest, &ctx->Pack, transferOps); 663 } 664 665 free(rgba); 666 } 667 668end: 669 if (map) 670 pipe_transfer_unmap(pipe, tex_xfer); 671 672 _mesa_unmap_pbo_dest(ctx, &ctx->Pack); 673 pipe_resource_reference(&dst_texture, NULL); 674} 675 676 677 678/** 679 * Called via ctx->Driver.GetTexImage() 680 */ 681static void 682st_GetTexImage(struct gl_context * ctx, 683 GLenum format, GLenum type, GLvoid * pixels, 684 struct gl_texture_image *texImage) 685{ 686 struct st_texture_image *stImage = st_texture_image(texImage); 687 688 if (stImage->pt && util_format_is_s3tc(stImage->pt->format)) { 689 /* Need to decompress the texture. 690 * We'll do this by rendering a textured quad (which is hopefully 691 * faster than using the fallback code in texcompress.c). 692 * Note that we only expect RGBA formats (no Z/depth formats). 693 */ 694 decompress_with_blit(ctx, format, type, pixels, texImage); 695 } 696 else { 697 _mesa_get_teximage(ctx, format, type, pixels, texImage); 698 } 699} 700 701 702/** 703 * Do a CopyTexSubImage operation using a read transfer from the source, 704 * a write transfer to the destination and get_tile()/put_tile() to access 705 * the pixels/texels. 706 * 707 * Note: srcY=0=TOP of renderbuffer 708 */ 709static void 710fallback_copy_texsubimage(struct gl_context *ctx, 711 struct st_renderbuffer *strb, 712 struct st_texture_image *stImage, 713 GLenum baseFormat, 714 GLint destX, GLint destY, GLint destZ, 715 GLint srcX, GLint srcY, 716 GLsizei width, GLsizei height) 717{ 718 struct st_context *st = st_context(ctx); 719 struct pipe_context *pipe = st->pipe; 720 struct pipe_transfer *src_trans; 721 GLvoid *texDest; 722 enum pipe_transfer_usage transfer_usage; 723 void *map; 724 725 if (ST_DEBUG & DEBUG_FALLBACK) 726 debug_printf("%s: fallback processing\n", __FUNCTION__); 727 728 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 729 srcY = strb->Base.Height - srcY - height; 730 } 731 732 map = pipe_transfer_map(pipe, 733 strb->texture, 734 strb->rtt_level, 735 strb->rtt_face + strb->rtt_slice, 736 PIPE_TRANSFER_READ, 737 srcX, srcY, 738 width, height, &src_trans); 739 740 if ((baseFormat == GL_DEPTH_COMPONENT || 741 baseFormat == GL_DEPTH_STENCIL) && 742 util_format_is_depth_and_stencil(stImage->pt->format)) 743 transfer_usage = PIPE_TRANSFER_READ_WRITE; 744 else 745 transfer_usage = PIPE_TRANSFER_WRITE; 746 747 /* XXX this used to ignore destZ param */ 748 texDest = st_texture_image_map(st, stImage, destZ, transfer_usage, 749 destX, destY, width, height); 750 751 if (baseFormat == GL_DEPTH_COMPONENT || 752 baseFormat == GL_DEPTH_STENCIL) { 753 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F || 754 ctx->Pixel.DepthBias != 0.0F); 755 GLint row, yStep; 756 uint *data; 757 758 /* determine bottom-to-top vs. top-to-bottom order for src buffer */ 759 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 760 srcY = height - 1; 761 yStep = -1; 762 } 763 else { 764 srcY = 0; 765 yStep = 1; 766 } 767 768 data = malloc(width * sizeof(uint)); 769 770 if (data) { 771 /* To avoid a large temp memory allocation, do copy row by row */ 772 for (row = 0; row < height; row++, srcY += yStep) { 773 pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data); 774 if (scaleOrBias) { 775 _mesa_scale_and_bias_depth_uint(ctx, width, data); 776 } 777 pipe_put_tile_z(stImage->transfer, texDest, 0, row, width, 1, 778 data); 779 } 780 } 781 else { 782 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()"); 783 } 784 785 free(data); 786 } 787 else { 788 /* RGBA format */ 789 GLfloat *tempSrc = 790 malloc(width * height * 4 * sizeof(GLfloat)); 791 792 if (tempSrc && texDest) { 793 const GLint dims = 2; 794 const GLint dstRowStride = stImage->transfer->stride; 795 struct gl_texture_image *texImage = &stImage->base; 796 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking; 797 798 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) { 799 unpack.Invert = GL_TRUE; 800 } 801 802 /* get float/RGBA image from framebuffer */ 803 /* XXX this usually involves a lot of int/float conversion. 804 * try to avoid that someday. 805 */ 806 pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height, 807 util_format_linear(strb->texture->format), 808 tempSrc); 809 810 /* Store into texture memory. 811 * Note that this does some special things such as pixel transfer 812 * ops and format conversion. In particular, if the dest tex format 813 * is actually RGBA but the user created the texture as GL_RGB we 814 * need to fill-in/override the alpha channel with 1.0. 815 */ 816 _mesa_texstore(ctx, dims, 817 texImage->_BaseFormat, 818 texImage->TexFormat, 819 dstRowStride, 820 (GLubyte **) &texDest, 821 width, height, 1, 822 GL_RGBA, GL_FLOAT, tempSrc, /* src */ 823 &unpack); 824 } 825 else { 826 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage"); 827 } 828 829 free(tempSrc); 830 } 831 832 st_texture_image_unmap(st, stImage); 833 pipe->transfer_unmap(pipe, src_trans); 834} 835 836 837 838/** 839 * If the format of the src renderbuffer and the format of the dest 840 * texture are compatible (in terms of blitting), return a TGSI writemask 841 * to be used during the blit. 842 * If the src/dest are incompatible, return 0. 843 */ 844static unsigned 845compatible_src_dst_formats(struct gl_context *ctx, 846 const struct gl_renderbuffer *src, 847 const struct gl_texture_image *dst) 848{ 849 /* Get logical base formats for the src and dest. 850 * That is, use the user-requested formats and not the actual, device- 851 * chosen formats. 852 * For example, the user may have requested an A8 texture but the 853 * driver may actually be using an RGBA texture format. When we 854 * copy/blit to that texture, we only want to copy the Alpha channel 855 * and not the RGB channels. 856 * 857 * Similarly, when the src FBO was created an RGB format may have been 858 * requested but the driver actually chose an RGBA format. In that case, 859 * we don't want to copy the undefined Alpha channel to the dest texture 860 * (it should be 1.0). 861 */ 862 const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat); 863 const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat); 864 865 /** 866 * XXX when we have red-only and red/green renderbuffers we'll need 867 * to add more cases here (or implement a general-purpose routine that 868 * queries the existance of the R,G,B,A channels in the src and dest). 869 */ 870 if (srcFormat == dstFormat) { 871 /* This is the same as matching_base_formats, which should 872 * always pass, as it did previously. 873 */ 874 return TGSI_WRITEMASK_XYZW; 875 } 876 else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) { 877 /* Make sure that A in the dest is 1. The actual src format 878 * may be RGBA and have undefined A values. 879 */ 880 return TGSI_WRITEMASK_XYZ; 881 } 882 else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) { 883 /* Make sure that A in the dest is 1. The actual dst format 884 * may be RGBA and will need A=1 to provide proper alpha values 885 * when sampled later. 886 */ 887 return TGSI_WRITEMASK_XYZ; 888 } 889 else { 890 if (ST_DEBUG & DEBUG_FALLBACK) 891 debug_printf("%s failed for src %s, dst %s\n", 892 __FUNCTION__, 893 _mesa_lookup_enum_by_nr(srcFormat), 894 _mesa_lookup_enum_by_nr(dstFormat)); 895 896 /* Otherwise fail. 897 */ 898 return 0; 899 } 900} 901 902 903 904/** 905 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible. 906 * Note that the region to copy has already been clipped so we know we 907 * won't read from outside the source renderbuffer's bounds. 908 * 909 * Note: srcY=0=Bottom of renderbuffer (GL convention) 910 */ 911static void 912st_CopyTexSubImage(struct gl_context *ctx, GLuint dims, 913 struct gl_texture_image *texImage, 914 GLint destX, GLint destY, GLint destZ, 915 struct gl_renderbuffer *rb, 916 GLint srcX, GLint srcY, GLsizei width, GLsizei height) 917{ 918 struct st_texture_image *stImage = st_texture_image(texImage); 919 const GLenum texBaseFormat = texImage->_BaseFormat; 920 struct st_renderbuffer *strb = st_renderbuffer(rb); 921 struct st_context *st = st_context(ctx); 922 struct pipe_context *pipe = st->pipe; 923 struct pipe_screen *screen = pipe->screen; 924 enum pipe_format dest_format, src_format; 925 GLboolean matching_base_formats; 926 GLuint color_writemask, zs_writemask, sample_count; 927 struct pipe_surface *dest_surface = NULL; 928 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP); 929 struct pipe_surface surf_tmpl; 930 unsigned int dst_usage; 931 GLint srcY0, srcY1; 932 933 /* make sure finalize_textures has been called? 934 */ 935 if (0) st_validate_state(st); 936 937 if (!strb || !strb->surface || !stImage->pt) { 938 debug_printf("%s: null strb or stImage\n", __FUNCTION__); 939 return; 940 } 941 942 sample_count = strb->surface->texture->nr_samples; 943 /* I believe this would be legal, presumably would need to do a resolve 944 for color, and for depth/stencil spec says to just use one of the 945 depth/stencil samples per pixel? Need some transfer clarifications. */ 946 assert(sample_count < 2); 947 948 assert(strb); 949 assert(strb->surface); 950 assert(stImage->pt); 951 952 src_format = strb->surface->format; 953 dest_format = stImage->pt->format; 954 955 /* 956 * Determine if the src framebuffer and dest texture have the same 957 * base format. We need this to detect a case such as the framebuffer 958 * being GL_RGBA but the texture being GL_RGB. If the actual hardware 959 * texture format stores RGBA we need to set A=1 (overriding the 960 * framebuffer's alpha values). We can't do that with the blit or 961 * textured-quad paths. 962 */ 963 matching_base_formats = 964 (_mesa_get_format_base_format(strb->Base.Format) == 965 _mesa_get_format_base_format(texImage->TexFormat)); 966 967 if (ctx->_ImageTransferState) { 968 goto fallback; 969 } 970 971 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) { 972 /* 1D arrays might be thought of as 2D images but the actual layout 973 * might not be that way. At some points, we convert OpenGL's 1D 974 * array 'height' into gallium 'layers' and that prevents the blit 975 * utility code from doing the right thing. Simpy use the memcpy-based 976 * fallback. 977 */ 978 goto fallback; 979 } 980 981 if (matching_base_formats && 982 src_format == dest_format && 983 !do_flip) { 984 /* use surface_copy() / blit */ 985 struct pipe_box src_box; 986 unsigned dstLevel; 987 988 u_box_2d_zslice(srcX, srcY, strb->surface->u.tex.first_layer, 989 width, height, &src_box); 990 991 /* If stImage->pt is an independent image (not a pointer into a full 992 * mipmap) stImage->pt.last_level will be zero and we need to use that 993 * as the dest level. 994 */ 995 dstLevel = MIN2(stImage->base.Level, stImage->pt->last_level); 996 997 /* for resource_copy_region(), y=0=top, always */ 998 pipe->resource_copy_region(pipe, 999 /* dest */ 1000 stImage->pt, 1001 dstLevel, 1002 destX, destY, destZ + stImage->base.Face, 1003 /* src */ 1004 strb->texture, 1005 strb->surface->u.tex.level, 1006 &src_box); 1007 return; 1008 } 1009 1010 if (texBaseFormat == GL_DEPTH_STENCIL) { 1011 goto fallback; 1012 } 1013 1014 if (texBaseFormat == GL_DEPTH_COMPONENT) { 1015 color_writemask = 0; 1016 zs_writemask = BLIT_WRITEMASK_Z; 1017 dst_usage = PIPE_BIND_DEPTH_STENCIL; 1018 } 1019 else { 1020 color_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage); 1021 zs_writemask = 0; 1022 dst_usage = PIPE_BIND_RENDER_TARGET; 1023 } 1024 1025 if ((!color_writemask && !zs_writemask) || 1026 !screen->is_format_supported(screen, src_format, 1027 PIPE_TEXTURE_2D, sample_count, 1028 PIPE_BIND_SAMPLER_VIEW) || 1029 !screen->is_format_supported(screen, dest_format, 1030 PIPE_TEXTURE_2D, 0, 1031 dst_usage)) { 1032 goto fallback; 1033 } 1034 1035 if (do_flip) { 1036 srcY1 = strb->Base.Height - srcY - height; 1037 srcY0 = srcY1 + height; 1038 } 1039 else { 1040 srcY0 = srcY; 1041 srcY1 = srcY0 + height; 1042 } 1043 1044 /* Disable conditional rendering. */ 1045 if (st->render_condition) { 1046 pipe->render_condition(pipe, NULL, 0); 1047 } 1048 1049 memset(&surf_tmpl, 0, sizeof(surf_tmpl)); 1050 surf_tmpl.format = util_format_linear(stImage->pt->format); 1051 surf_tmpl.usage = dst_usage; 1052 surf_tmpl.u.tex.level = stImage->base.Level; 1053 surf_tmpl.u.tex.first_layer = stImage->base.Face + destZ; 1054 surf_tmpl.u.tex.last_layer = stImage->base.Face + destZ; 1055 1056 dest_surface = pipe->create_surface(pipe, stImage->pt, 1057 &surf_tmpl); 1058 util_blit_pixels(st->blit, 1059 strb->texture, 1060 strb->surface->u.tex.level, 1061 srcX, srcY0, 1062 srcX + width, srcY1, 1063 strb->surface->u.tex.first_layer, 1064 dest_surface, 1065 destX, destY, 1066 destX + width, destY + height, 1067 0.0, PIPE_TEX_MIPFILTER_NEAREST, 1068 color_writemask, zs_writemask); 1069 pipe_surface_reference(&dest_surface, NULL); 1070 1071 /* Restore conditional rendering state. */ 1072 if (st->render_condition) { 1073 pipe->render_condition(pipe, st->render_condition, 1074 st->condition_mode); 1075 } 1076 1077 return; 1078 1079fallback: 1080 /* software fallback */ 1081 fallback_copy_texsubimage(ctx, 1082 strb, stImage, texBaseFormat, 1083 destX, destY, destZ, 1084 srcX, srcY, width, height); 1085} 1086 1087 1088/** 1089 * Copy image data from stImage into the texture object 'stObj' at level 1090 * 'dstLevel'. 1091 */ 1092static void 1093copy_image_data_to_texture(struct st_context *st, 1094 struct st_texture_object *stObj, 1095 GLuint dstLevel, 1096 struct st_texture_image *stImage) 1097{ 1098 /* debug checks */ 1099 { 1100 const struct gl_texture_image *dstImage = 1101 stObj->base.Image[stImage->base.Face][dstLevel]; 1102 assert(dstImage); 1103 assert(dstImage->Width == stImage->base.Width); 1104 assert(dstImage->Height == stImage->base.Height); 1105 assert(dstImage->Depth == stImage->base.Depth); 1106 } 1107 1108 if (stImage->pt) { 1109 /* Copy potentially with the blitter: 1110 */ 1111 GLuint src_level; 1112 if (stImage->pt->last_level == 0) 1113 src_level = 0; 1114 else 1115 src_level = stImage->base.Level; 1116 1117 assert(src_level <= stImage->pt->last_level); 1118 assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width); 1119 assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY || 1120 u_minify(stImage->pt->height0, src_level) == stImage->base.Height); 1121 assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY || 1122 stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY || 1123 u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth); 1124 1125 st_texture_image_copy(st->pipe, 1126 stObj->pt, dstLevel, /* dest texture, level */ 1127 stImage->pt, src_level, /* src texture, level */ 1128 stImage->base.Face); 1129 1130 pipe_resource_reference(&stImage->pt, NULL); 1131 } 1132 else if (stImage->TexData) { 1133 /* Copy from malloc'd memory */ 1134 /* XXX this should be re-examined/tested with a compressed format */ 1135 GLuint blockSize = util_format_get_blocksize(stObj->pt->format); 1136 GLuint srcRowStride = stImage->base.Width * blockSize; 1137 GLuint srcSliceStride = stImage->base.Height * srcRowStride; 1138 st_texture_image_data(st, 1139 stObj->pt, 1140 stImage->base.Face, 1141 dstLevel, 1142 stImage->TexData, 1143 srcRowStride, 1144 srcSliceStride); 1145 _mesa_align_free(stImage->TexData); 1146 stImage->TexData = NULL; 1147 } 1148 1149 pipe_resource_reference(&stImage->pt, stObj->pt); 1150} 1151 1152 1153/** 1154 * Called during state validation. When this function is finished, 1155 * the texture object should be ready for rendering. 1156 * \return GL_TRUE for success, GL_FALSE for failure (out of mem) 1157 */ 1158GLboolean 1159st_finalize_texture(struct gl_context *ctx, 1160 struct pipe_context *pipe, 1161 struct gl_texture_object *tObj) 1162{ 1163 struct st_context *st = st_context(ctx); 1164 struct st_texture_object *stObj = st_texture_object(tObj); 1165 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1; 1166 GLuint face; 1167 struct st_texture_image *firstImage; 1168 enum pipe_format firstImageFormat; 1169 GLuint ptWidth, ptHeight, ptDepth, ptLayers; 1170 1171 if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) { 1172 /* The texture is complete and we know exactly how many mipmap levels 1173 * are present/needed. This is conditional because we may be called 1174 * from the st_generate_mipmap() function when the texture object is 1175 * incomplete. In that case, we'll have set stObj->lastLevel before 1176 * we get here. 1177 */ 1178 if (stObj->base.Sampler.MinFilter == GL_LINEAR || 1179 stObj->base.Sampler.MinFilter == GL_NEAREST) 1180 stObj->lastLevel = stObj->base.BaseLevel; 1181 else 1182 stObj->lastLevel = stObj->base._MaxLevel; 1183 } 1184 1185 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]); 1186 assert(firstImage); 1187 1188 /* If both firstImage and stObj point to a texture which can contain 1189 * all active images, favour firstImage. Note that because of the 1190 * completeness requirement, we know that the image dimensions 1191 * will match. 1192 */ 1193 if (firstImage->pt && 1194 firstImage->pt != stObj->pt && 1195 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) { 1196 pipe_resource_reference(&stObj->pt, firstImage->pt); 1197 pipe_sampler_view_release(st->pipe, &stObj->sampler_view); 1198 } 1199 1200 /* Find gallium format for the Mesa texture */ 1201 firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat); 1202 1203 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */ 1204 { 1205 GLuint width, height, depth; 1206 if (!guess_base_level_size(stObj->base.Target, 1207 firstImage->base.Width2, 1208 firstImage->base.Height2, 1209 firstImage->base.Depth2, 1210 firstImage->base.Level, 1211 &width, &height, &depth)) { 1212 width = stObj->width0; 1213 height = stObj->height0; 1214 depth = stObj->depth0; 1215 } 1216 /* convert GL dims to Gallium dims */ 1217 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth, 1218 &ptWidth, &ptHeight, &ptDepth, &ptLayers); 1219 } 1220 1221 /* If we already have a gallium texture, check that it matches the texture 1222 * object's format, target, size, num_levels, etc. 1223 */ 1224 if (stObj->pt) { 1225 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) || 1226 !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) || 1227 stObj->pt->last_level < stObj->lastLevel || 1228 stObj->pt->width0 != ptWidth || 1229 stObj->pt->height0 != ptHeight || 1230 stObj->pt->depth0 != ptDepth || 1231 stObj->pt->array_size != ptLayers) 1232 { 1233 /* The gallium texture does not match the Mesa texture so delete the 1234 * gallium texture now. We'll make a new one below. 1235 */ 1236 pipe_resource_reference(&stObj->pt, NULL); 1237 pipe_sampler_view_release(st->pipe, &stObj->sampler_view); 1238 st->dirty.st |= ST_NEW_FRAMEBUFFER; 1239 } 1240 } 1241 1242 /* May need to create a new gallium texture: 1243 */ 1244 if (!stObj->pt) { 1245 GLuint bindings = default_bindings(st, firstImageFormat); 1246 1247 stObj->pt = st_texture_create(st, 1248 gl_target_to_pipe(stObj->base.Target), 1249 firstImageFormat, 1250 stObj->lastLevel, 1251 ptWidth, 1252 ptHeight, 1253 ptDepth, 1254 ptLayers, 1255 bindings); 1256 1257 if (!stObj->pt) { 1258 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage"); 1259 return GL_FALSE; 1260 } 1261 } 1262 1263 /* Pull in any images not in the object's texture: 1264 */ 1265 for (face = 0; face < nr_faces; face++) { 1266 GLuint level; 1267 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) { 1268 struct st_texture_image *stImage = 1269 st_texture_image(stObj->base.Image[face][level]); 1270 1271 /* Need to import images in main memory or held in other textures. 1272 */ 1273 if (stImage && stObj->pt != stImage->pt) { 1274 if (level == 0 || 1275 (stImage->base.Width == u_minify(stObj->width0, level) && 1276 stImage->base.Height == u_minify(stObj->height0, level) && 1277 stImage->base.Depth == u_minify(stObj->depth0, level))) { 1278 /* src image fits expected dest mipmap level size */ 1279 copy_image_data_to_texture(st, stObj, level, stImage); 1280 } 1281 } 1282 } 1283 } 1284 1285 return GL_TRUE; 1286} 1287 1288 1289/** 1290 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory 1291 * for a whole mipmap stack. 1292 */ 1293static GLboolean 1294st_AllocTextureStorage(struct gl_context *ctx, 1295 struct gl_texture_object *texObj, 1296 GLsizei levels, GLsizei width, 1297 GLsizei height, GLsizei depth) 1298{ 1299 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target); 1300 struct st_context *st = st_context(ctx); 1301 struct st_texture_object *stObj = st_texture_object(texObj); 1302 GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings; 1303 enum pipe_format fmt; 1304 GLint level; 1305 1306 assert(levels > 0); 1307 1308 /* Save the level=0 dimensions */ 1309 stObj->width0 = width; 1310 stObj->height0 = height; 1311 stObj->depth0 = depth; 1312 stObj->lastLevel = levels - 1; 1313 1314 fmt = st_mesa_format_to_pipe_format(texObj->Image[0][0]->TexFormat); 1315 1316 bindings = default_bindings(st, fmt); 1317 1318 st_gl_texture_dims_to_pipe_dims(texObj->Target, 1319 width, height, depth, 1320 &ptWidth, &ptHeight, &ptDepth, &ptLayers); 1321 1322 stObj->pt = st_texture_create(st, 1323 gl_target_to_pipe(texObj->Target), 1324 fmt, 1325 levels, 1326 ptWidth, 1327 ptHeight, 1328 ptDepth, 1329 ptLayers, 1330 bindings); 1331 if (!stObj->pt) 1332 return GL_FALSE; 1333 1334 /* Set image resource pointers */ 1335 for (level = 0; level < levels; level++) { 1336 GLuint face; 1337 for (face = 0; face < numFaces; face++) { 1338 struct st_texture_image *stImage = 1339 st_texture_image(texObj->Image[face][level]); 1340 pipe_resource_reference(&stImage->pt, stObj->pt); 1341 } 1342 } 1343 1344 return GL_TRUE; 1345} 1346 1347 1348static GLboolean 1349st_TestProxyTexImage(struct gl_context *ctx, GLenum target, 1350 GLint level, gl_format format, 1351 GLint width, GLint height, 1352 GLint depth, GLint border) 1353{ 1354 struct st_context *st = st_context(ctx); 1355 struct pipe_context *pipe = st->pipe; 1356 1357 if (width == 0 || height == 0 || depth == 0) { 1358 /* zero-sized images are legal, and always fit! */ 1359 return GL_TRUE; 1360 } 1361 1362 if (pipe->screen->can_create_resource) { 1363 /* Ask the gallium driver if the texture is too large */ 1364 struct gl_texture_object *texObj = 1365 _mesa_get_current_tex_object(ctx, target); 1366 struct pipe_resource pt; 1367 1368 /* Setup the pipe_resource object 1369 */ 1370 memset(&pt, 0, sizeof(pt)); 1371 1372 pt.target = gl_target_to_pipe(target); 1373 pt.format = st_mesa_format_to_pipe_format(format); 1374 1375 st_gl_texture_dims_to_pipe_dims(target, 1376 width, height, depth, 1377 &pt.width0, &pt.height0, 1378 &pt.depth0, &pt.array_size); 1379 1380 if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR || 1381 texObj->Sampler.MinFilter == GL_NEAREST)) { 1382 /* assume just one mipmap level */ 1383 pt.last_level = 0; 1384 } 1385 else { 1386 /* assume a full set of mipmaps */ 1387 pt.last_level = _mesa_logbase2(MAX3(width, height, depth)); 1388 } 1389 1390 return pipe->screen->can_create_resource(pipe->screen, &pt); 1391 } 1392 else { 1393 /* Use core Mesa fallback */ 1394 return _mesa_test_proxy_teximage(ctx, target, level, format, 1395 width, height, depth, border); 1396 } 1397} 1398 1399 1400void 1401st_init_texture_functions(struct dd_function_table *functions) 1402{ 1403 functions->ChooseTextureFormat = st_ChooseTextureFormat; 1404 functions->TexImage = st_TexImage; 1405 functions->TexSubImage = _mesa_store_texsubimage; 1406 functions->CompressedTexSubImage = _mesa_store_compressed_texsubimage; 1407 functions->CopyTexSubImage = st_CopyTexSubImage; 1408 functions->GenerateMipmap = st_generate_mipmap; 1409 1410 functions->GetTexImage = st_GetTexImage; 1411 1412 /* compressed texture functions */ 1413 functions->CompressedTexImage = st_CompressedTexImage; 1414 functions->GetCompressedTexImage = _mesa_get_compressed_teximage; 1415 1416 functions->NewTextureObject = st_NewTextureObject; 1417 functions->NewTextureImage = st_NewTextureImage; 1418 functions->DeleteTextureImage = st_DeleteTextureImage; 1419 functions->DeleteTexture = st_DeleteTextureObject; 1420 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer; 1421 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer; 1422 functions->MapTextureImage = st_MapTextureImage; 1423 functions->UnmapTextureImage = st_UnmapTextureImage; 1424 1425 /* XXX Temporary until we can query pipe's texture sizes */ 1426 functions->TestProxyTexImage = st_TestProxyTexImage; 1427 1428 functions->AllocTextureStorage = st_AllocTextureStorage; 1429} 1430