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