st_texture.c revision 47e41b024e325f69ed514e551a6824afa58f1db6
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 "st_context.h" 29#include "st_format.h" 30#include "st_public.h" 31#include "st_texture.h" 32#include "st_cb_fbo.h" 33#include "st_inlines.h" 34#include "main/enums.h" 35#include "main/texobj.h" 36#include "main/teximage.h" 37#include "main/texstore.h" 38 39#undef Elements /* fix re-defined macro warning */ 40 41#include "pipe/p_state.h" 42#include "pipe/p_context.h" 43#include "pipe/p_defines.h" 44#include "pipe/p_inlines.h" 45#include "util/u_rect.h" 46 47 48#define DBG if(0) printf 49 50#if 0 51static GLenum 52target_to_target(GLenum target) 53{ 54 switch (target) { 55 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB: 56 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB: 57 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB: 58 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB: 59 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB: 60 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB: 61 return GL_TEXTURE_CUBE_MAP_ARB; 62 default: 63 return target; 64 } 65} 66#endif 67 68 69/** 70 * Allocate a new pipe_texture object 71 * width0, height0, depth0 are the dimensions of the level 0 image 72 * (the highest resolution). last_level indicates how many mipmap levels 73 * to allocate storage for. For non-mipmapped textures, this will be zero. 74 */ 75struct pipe_texture * 76st_texture_create(struct st_context *st, 77 enum pipe_texture_target target, 78 enum pipe_format format, 79 GLuint last_level, 80 GLuint width0, 81 GLuint height0, 82 GLuint depth0, 83 GLuint usage ) 84{ 85 struct pipe_texture pt, *newtex; 86 struct pipe_screen *screen = st->pipe->screen; 87 88 assert(target <= PIPE_TEXTURE_CUBE); 89 90 DBG("%s target %s format %s last_level %d\n", __FUNCTION__, 91 _mesa_lookup_enum_by_nr(target), 92 _mesa_lookup_enum_by_nr(format), last_level); 93 94 assert(format); 95 assert(screen->is_format_supported(screen, format, target, 96 PIPE_TEXTURE_USAGE_SAMPLER, 0)); 97 98 memset(&pt, 0, sizeof(pt)); 99 pt.target = target; 100 pt.format = format; 101 pt.last_level = last_level; 102 pt.width[0] = width0; 103 pt.height[0] = height0; 104 pt.depth[0] = depth0; 105 pf_get_block(format, &pt.block); 106 pt.tex_usage = usage; 107 108 newtex = screen->texture_create(screen, &pt); 109 110 assert(!newtex || pipe_is_referenced(&newtex->reference)); 111 112 return newtex; 113} 114 115 116/** 117 * Check if a texture image can be pulled into a unified mipmap texture. 118 */ 119GLboolean 120st_texture_match_image(const struct pipe_texture *pt, 121 const struct gl_texture_image *image, 122 GLuint face, GLuint level) 123{ 124 /* Images with borders are never pulled into mipmap textures. 125 */ 126 if (image->Border) 127 return GL_FALSE; 128 129 /* Check if this image's format matches the established texture's format. 130 */ 131 if (st_mesa_format_to_pipe_format(image->TexFormat->MesaFormat) != pt->format) 132 return GL_FALSE; 133 134 /* Test if this image's size matches what's expected in the 135 * established texture. 136 */ 137 if (image->Width != pt->width[level] || 138 image->Height != pt->height[level] || 139 image->Depth != pt->depth[level]) 140 return GL_FALSE; 141 142 return GL_TRUE; 143} 144 145 146#if 000 147/* Although we use the image_offset[] array to store relative offsets 148 * to cube faces, Mesa doesn't know anything about this and expects 149 * each cube face to be treated as a separate image. 150 * 151 * These functions present that view to mesa: 152 */ 153const GLuint * 154st_texture_depth_offsets(struct pipe_texture *pt, GLuint level) 155{ 156 static const GLuint zero = 0; 157 158 if (pt->target != PIPE_TEXTURE_3D || pt->level[level].nr_images == 1) 159 return &zero; 160 else 161 return pt->level[level].image_offset; 162} 163 164 165/** 166 * Return the offset to the given mipmap texture image within the 167 * texture memory buffer, in bytes. 168 */ 169GLuint 170st_texture_image_offset(const struct pipe_texture * pt, 171 GLuint face, GLuint level) 172{ 173 if (pt->target == PIPE_TEXTURE_CUBE) 174 return (pt->level[level].level_offset + 175 pt->level[level].image_offset[face] * pt->cpp); 176 else 177 return pt->level[level].level_offset; 178} 179#endif 180 181 182/** 183 * Map a teximage in a mipmap texture. 184 * \param row_stride returns row stride in bytes 185 * \param image_stride returns image stride in bytes (for 3D textures). 186 * \return address of mapping 187 */ 188GLubyte * 189st_texture_image_map(struct st_context *st, struct st_texture_image *stImage, 190 GLuint zoffset, enum pipe_transfer_usage usage, 191 GLuint x, GLuint y, GLuint w, GLuint h) 192{ 193 struct pipe_context *pipe = st->pipe; 194 struct pipe_screen *screen = pipe->screen; 195 struct pipe_texture *pt = stImage->pt; 196 197 DBG("%s \n", __FUNCTION__); 198 199 stImage->transfer = st_no_flush_get_tex_transfer(st, pt, stImage->face, 200 stImage->level, zoffset, 201 usage, x, y, w, h); 202 203 if (stImage->transfer) 204 return screen->transfer_map(screen, stImage->transfer); 205 else 206 return NULL; 207} 208 209 210void 211st_texture_image_unmap(struct st_context *st, 212 struct st_texture_image *stImage) 213{ 214 struct pipe_screen *screen = st->pipe->screen; 215 216 DBG("%s\n", __FUNCTION__); 217 218 screen->transfer_unmap(screen, stImage->transfer); 219 220 screen->tex_transfer_destroy(stImage->transfer); 221} 222 223 224 225/** 226 * Upload data to a rectangular sub-region. Lots of choices how to do this: 227 * 228 * - memcpy by span to current destination 229 * - upload data as new buffer and blit 230 * 231 * Currently always memcpy. 232 */ 233static void 234st_surface_data(struct pipe_context *pipe, 235 struct pipe_transfer *dst, 236 unsigned dstx, unsigned dsty, 237 const void *src, unsigned src_stride, 238 unsigned srcx, unsigned srcy, unsigned width, unsigned height) 239{ 240 struct pipe_screen *screen = pipe->screen; 241 void *map = screen->transfer_map(screen, dst); 242 243 util_copy_rect(map, 244 &dst->block, 245 dst->stride, 246 dstx, dsty, 247 width, height, 248 src, src_stride, 249 srcx, srcy); 250 251 screen->transfer_unmap(screen, dst); 252} 253 254 255/* Upload data for a particular image. 256 */ 257void 258st_texture_image_data(struct st_context *st, 259 struct pipe_texture *dst, 260 GLuint face, 261 GLuint level, 262 void *src, 263 GLuint src_row_stride, GLuint src_image_stride) 264{ 265 struct pipe_context *pipe = st->pipe; 266 struct pipe_screen *screen = pipe->screen; 267 GLuint depth = dst->depth[level]; 268 GLuint i; 269 const GLubyte *srcUB = src; 270 struct pipe_transfer *dst_transfer; 271 272 DBG("%s\n", __FUNCTION__); 273 274 for (i = 0; i < depth; i++) { 275 dst_transfer = st_no_flush_get_tex_transfer(st, dst, face, level, i, 276 PIPE_TRANSFER_WRITE, 0, 0, 277 dst->width[level], 278 dst->height[level]); 279 280 st_surface_data(pipe, dst_transfer, 281 0, 0, /* dstx, dsty */ 282 srcUB, 283 src_row_stride, 284 0, 0, /* source x, y */ 285 dst->width[level], dst->height[level]); /* width, height */ 286 287 screen->tex_transfer_destroy(dst_transfer); 288 289 srcUB += src_image_stride; 290 } 291} 292 293 294/* Copy mipmap image between textures 295 */ 296void 297st_texture_image_copy(struct pipe_context *pipe, 298 struct pipe_texture *dst, GLuint dstLevel, 299 struct pipe_texture *src, 300 GLuint face) 301{ 302 struct pipe_screen *screen = pipe->screen; 303 GLuint width = dst->width[dstLevel]; 304 GLuint height = dst->height[dstLevel]; 305 GLuint depth = dst->depth[dstLevel]; 306 struct pipe_surface *src_surface; 307 struct pipe_surface *dst_surface; 308 GLuint i; 309 310 for (i = 0; i < depth; i++) { 311 GLuint srcLevel; 312 313 /* find src texture level of needed size */ 314 for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) { 315 if (src->width[srcLevel] == width && 316 src->height[srcLevel] == height) { 317 break; 318 } 319 } 320 assert(src->width[srcLevel] == width); 321 assert(src->height[srcLevel] == height); 322 323#if 0 324 { 325 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i, 326 PIPE_BUFFER_USAGE_CPU_READ); 327 ubyte *map = screen->surface_map(screen, src_surface, PIPE_BUFFER_USAGE_CPU_READ); 328 map += src_surface->width * src_surface->height * 4 / 2; 329 printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n", 330 __FUNCTION__, 331 map[0], map[1], map[2], map[3], 332 src, srcLevel, dst, dstLevel); 333 334 screen->surface_unmap(screen, src_surface); 335 pipe_surface_reference(&src_surface, NULL); 336 } 337#endif 338 339 dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i, 340 PIPE_BUFFER_USAGE_GPU_WRITE); 341 342 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i, 343 PIPE_BUFFER_USAGE_GPU_READ); 344 345 pipe->surface_copy(pipe, 346 dst_surface, 347 0, 0, /* destX, Y */ 348 src_surface, 349 0, 0, /* srcX, Y */ 350 width, height); 351 352 pipe_surface_reference(&src_surface, NULL); 353 pipe_surface_reference(&dst_surface, NULL); 354 } 355} 356 357 358/** 359 * Bind a pipe surface to a texture object. After the call, 360 * the texture object is marked dirty and will be (re-)validated. 361 * 362 * If this is the first surface bound, the texture object is said to 363 * switch from normal to surface based. It will be cleared first in 364 * this case. 365 * 366 * \param ps pipe surface to be unbound 367 * \param target texture target 368 * \param level image level 369 * \param format internal format of the texture 370 */ 371int 372st_bind_texture_surface(struct pipe_surface *ps, int target, int level, 373 enum pipe_format format) 374{ 375 GET_CURRENT_CONTEXT(ctx); 376 const GLuint unit = ctx->Texture.CurrentUnit; 377 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; 378 struct gl_texture_object *texObj; 379 struct gl_texture_image *texImage; 380 struct st_texture_object *stObj; 381 struct st_texture_image *stImage; 382 GLenum internalFormat; 383 384 switch (target) { 385 case ST_TEXTURE_2D: 386 target = GL_TEXTURE_2D; 387 break; 388 case ST_TEXTURE_RECT: 389 target = GL_TEXTURE_RECTANGLE_ARB; 390 break; 391 default: 392 return 0; 393 } 394 395 /* map pipe format to base format for now */ 396 if (pf_get_component_bits(format, PIPE_FORMAT_COMP_A) > 0) 397 internalFormat = GL_RGBA; 398 else 399 internalFormat = GL_RGB; 400 401 texObj = _mesa_select_tex_object(ctx, texUnit, target); 402 _mesa_lock_texture(ctx, texObj); 403 404 stObj = st_texture_object(texObj); 405 /* switch to surface based */ 406 if (!stObj->surface_based) { 407 _mesa_clear_texture_object(ctx, texObj); 408 stObj->surface_based = GL_TRUE; 409 } 410 411 texImage = _mesa_get_tex_image(ctx, texObj, target, level); 412 stImage = st_texture_image(texImage); 413 414 _mesa_init_teximage_fields(ctx, target, texImage, 415 ps->width, ps->height, 1, 0, internalFormat); 416 texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat, 417 GL_RGBA, GL_UNSIGNED_BYTE); 418 _mesa_set_fetch_functions(texImage, 2); 419 pipe_texture_reference(&stImage->pt, ps->texture); 420 421 _mesa_dirty_texobj(ctx, texObj, GL_TRUE); 422 _mesa_unlock_texture(ctx, texObj); 423 424 return 1; 425} 426 427 428/** 429 * Unbind a pipe surface from a texture object. After the call, 430 * the texture object is marked dirty and will be (re-)validated. 431 * 432 * \param ps pipe surface to be unbound 433 * \param target texture target 434 * \param level image level 435 */ 436int 437st_unbind_texture_surface(struct pipe_surface *ps, int target, int level) 438{ 439 GET_CURRENT_CONTEXT(ctx); 440 const GLuint unit = ctx->Texture.CurrentUnit; 441 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; 442 struct gl_texture_object *texObj; 443 struct gl_texture_image *texImage; 444 struct st_texture_object *stObj; 445 struct st_texture_image *stImage; 446 447 switch (target) { 448 case ST_TEXTURE_2D: 449 target = GL_TEXTURE_2D; 450 break; 451 case ST_TEXTURE_RECT: 452 target = GL_TEXTURE_RECTANGLE_ARB; 453 break; 454 default: 455 return 0; 456 } 457 458 texObj = _mesa_select_tex_object(ctx, texUnit, target); 459 460 _mesa_lock_texture(ctx, texObj); 461 462 texImage = _mesa_get_tex_image(ctx, texObj, target, level); 463 stObj = st_texture_object(texObj); 464 stImage = st_texture_image(texImage); 465 466 /* Make sure the pipe surface is still bound. The texture object is still 467 * considered surface based even if this is the last bound surface. */ 468 if (stImage->pt == ps->texture) { 469 pipe_texture_reference(&stImage->pt, NULL); 470 _mesa_clear_texture_image(ctx, texImage); 471 472 _mesa_dirty_texobj(ctx, texObj, GL_TRUE); 473 } 474 475 _mesa_unlock_texture(ctx, texObj); 476 477 return 1; 478} 479 480 481/** Redirect rendering into stfb's surface to a texture image */ 482int 483st_bind_teximage(struct st_framebuffer *stfb, uint surfIndex, 484 int target, int format, int level) 485{ 486 GET_CURRENT_CONTEXT(ctx); 487 struct st_context *st = ctx->st; 488 struct pipe_context *pipe = st->pipe; 489 struct pipe_screen *screen = pipe->screen; 490 const GLuint unit = ctx->Texture.CurrentUnit; 491 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; 492 struct gl_texture_object *texObj; 493 struct gl_texture_image *texImage; 494 struct st_texture_image *stImage; 495 struct st_renderbuffer *strb; 496 GLint face = 0, slice = 0; 497 498 assert(surfIndex <= ST_SURFACE_DEPTH); 499 500 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer); 501 502 if (strb->texture_save || strb->surface_save) { 503 /* Error! */ 504 return 0; 505 } 506 507 if (target == ST_TEXTURE_2D) { 508 texObj = texUnit->CurrentTex[TEXTURE_2D_INDEX]; 509 texImage = _mesa_get_tex_image(ctx, texObj, GL_TEXTURE_2D, level); 510 stImage = st_texture_image(texImage); 511 } 512 else { 513 /* unsupported target */ 514 return 0; 515 } 516 517 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL); 518 519 /* save the renderbuffer's surface/texture info */ 520 pipe_texture_reference(&strb->texture_save, strb->texture); 521 pipe_surface_reference(&strb->surface_save, strb->surface); 522 523 /* plug in new surface/texture info */ 524 pipe_texture_reference(&strb->texture, stImage->pt); 525 strb->surface = screen->get_tex_surface(screen, strb->texture, 526 face, level, slice, 527 (PIPE_BUFFER_USAGE_GPU_READ | 528 PIPE_BUFFER_USAGE_GPU_WRITE)); 529 530 st->dirty.st |= ST_NEW_FRAMEBUFFER; 531 532 return 1; 533} 534 535 536/** Undo surface-to-texture binding */ 537int 538st_release_teximage(struct st_framebuffer *stfb, uint surfIndex, 539 int target, int format, int level) 540{ 541 GET_CURRENT_CONTEXT(ctx); 542 struct st_context *st = ctx->st; 543 struct st_renderbuffer *strb; 544 545 assert(surfIndex <= ST_SURFACE_DEPTH); 546 547 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer); 548 549 if (!strb->texture_save || !strb->surface_save) { 550 /* Error! */ 551 return 0; 552 } 553 554 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL); 555 556 /* free tex surface, restore original */ 557 pipe_surface_reference(&strb->surface, strb->surface_save); 558 pipe_texture_reference(&strb->texture, strb->texture_save); 559 560 pipe_surface_reference(&strb->surface_save, NULL); 561 pipe_texture_reference(&strb->texture_save, NULL); 562 563 st->dirty.st |= ST_NEW_FRAMEBUFFER; 564 565 return 1; 566} 567 568void 569st_teximage_flush_before_map(struct st_context *st, 570 struct pipe_texture *pt, 571 unsigned int face, 572 unsigned int level, 573 enum pipe_transfer_usage usage) 574{ 575 struct pipe_context *pipe = st->pipe; 576 unsigned referenced = 577 pipe->is_texture_referenced(pipe, pt, face, level); 578 579 if (referenced && ((referenced & PIPE_REFERENCED_FOR_WRITE) || 580 (usage & PIPE_TRANSFER_WRITE))) 581 st_flush(st, PIPE_FLUSH_RENDER_CACHE, NULL); 582} 583