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