u_blit.c revision 3f5a316f36e2d376104640033c8bcefef3810ef4
1/************************************************************************** 2 * 3 * Copyright 2008 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/** 29 * @file 30 * Copy/blit pixel rect between surfaces 31 * 32 * @author Brian Paul 33 */ 34 35 36#include "pipe/p_context.h" 37#include "util/u_debug.h" 38#include "pipe/p_defines.h" 39#include "pipe/p_inlines.h" 40#include "pipe/p_shader_tokens.h" 41#include "pipe/p_state.h" 42 43#include "util/u_blit.h" 44#include "util/u_draw_quad.h" 45#include "util/u_math.h" 46#include "util/u_memory.h" 47#include "util/u_simple_shaders.h" 48#include "util/u_surface.h" 49 50#include "cso_cache/cso_context.h" 51 52 53struct blit_state 54{ 55 struct pipe_context *pipe; 56 struct cso_context *cso; 57 58 struct pipe_blend_state blend; 59 struct pipe_depth_stencil_alpha_state depthstencil; 60 struct pipe_rasterizer_state rasterizer; 61 struct pipe_sampler_state sampler; 62 struct pipe_viewport_state viewport; 63 64 void *vs; 65 void *fs[TGSI_WRITEMASK_XYZW + 1]; 66 67 struct pipe_buffer *vbuf; /**< quad vertices */ 68 unsigned vbuf_slot; 69 70 float vertices[4][2][4]; /**< vertex/texcoords for quad */ 71}; 72 73 74/** 75 * Create state object for blit. 76 * Intended to be created once and re-used for many blit() calls. 77 */ 78struct blit_state * 79util_create_blit(struct pipe_context *pipe, struct cso_context *cso) 80{ 81 struct blit_state *ctx; 82 uint i; 83 84 ctx = CALLOC_STRUCT(blit_state); 85 if (!ctx) 86 return NULL; 87 88 ctx->pipe = pipe; 89 ctx->cso = cso; 90 91 /* disabled blending/masking */ 92 memset(&ctx->blend, 0, sizeof(ctx->blend)); 93 ctx->blend.colormask = PIPE_MASK_RGBA; 94 95 /* no-op depth/stencil/alpha */ 96 memset(&ctx->depthstencil, 0, sizeof(ctx->depthstencil)); 97 98 /* rasterizer */ 99 memset(&ctx->rasterizer, 0, sizeof(ctx->rasterizer)); 100 ctx->rasterizer.front_winding = PIPE_WINDING_CW; 101 ctx->rasterizer.cull_mode = PIPE_WINDING_NONE; 102 ctx->rasterizer.bypass_vs_clip_and_viewport = 1; 103 ctx->rasterizer.gl_rasterization_rules = 1; 104 105 /* samplers */ 106 memset(&ctx->sampler, 0, sizeof(ctx->sampler)); 107 ctx->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 108 ctx->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 109 ctx->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 110 ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 111 ctx->sampler.min_img_filter = 0; /* set later */ 112 ctx->sampler.mag_img_filter = 0; /* set later */ 113 ctx->sampler.normalized_coords = 1; 114 115 116 /* vertex shader - still required to provide the linkage between 117 * fragment shader input semantics and vertex_element/buffers. 118 */ 119 { 120 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION, 121 TGSI_SEMANTIC_GENERIC }; 122 const uint semantic_indexes[] = { 0, 0 }; 123 ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names, 124 semantic_indexes); 125 } 126 127 /* fragment shader */ 128 ctx->fs[TGSI_WRITEMASK_XYZW] = util_make_fragment_tex_shader(pipe); 129 ctx->vbuf = NULL; 130 131 /* init vertex data that doesn't change */ 132 for (i = 0; i < 4; i++) { 133 ctx->vertices[i][0][3] = 1.0f; /* w */ 134 ctx->vertices[i][1][2] = 0.0f; /* r */ 135 ctx->vertices[i][1][3] = 1.0f; /* q */ 136 } 137 138 return ctx; 139} 140 141 142/** 143 * Destroy a blit context 144 */ 145void 146util_destroy_blit(struct blit_state *ctx) 147{ 148 struct pipe_context *pipe = ctx->pipe; 149 unsigned i; 150 151 pipe->delete_vs_state(pipe, ctx->vs); 152 153 for (i = 0; i < Elements(ctx->fs); i++) 154 if (ctx->fs[i]) 155 pipe->delete_fs_state(pipe, ctx->fs[i]); 156 157 pipe_buffer_reference(&ctx->vbuf, NULL); 158 159 FREE(ctx); 160} 161 162 163/** 164 * Get offset of next free slot in vertex buffer for quad vertices. 165 */ 166static unsigned 167get_next_slot( struct blit_state *ctx ) 168{ 169 const unsigned max_slots = 4096 / sizeof ctx->vertices; 170 171 if (ctx->vbuf_slot >= max_slots) 172 util_blit_flush( ctx ); 173 174 if (!ctx->vbuf) { 175 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen, 176 32, 177 PIPE_BUFFER_USAGE_VERTEX, 178 max_slots * sizeof ctx->vertices); 179 } 180 181 return ctx->vbuf_slot++ * sizeof ctx->vertices; 182} 183 184 185 186 187 188/** 189 * Setup vertex data for the textured quad we'll draw. 190 * Note: y=0=top 191 */ 192static unsigned 193setup_vertex_data_tex(struct blit_state *ctx, 194 float x0, float y0, float x1, float y1, 195 float s0, float t0, float s1, float t1, 196 float z) 197{ 198 unsigned offset; 199 200 ctx->vertices[0][0][0] = x0; 201 ctx->vertices[0][0][1] = y0; 202 ctx->vertices[0][0][2] = z; 203 ctx->vertices[0][1][0] = s0; /*s*/ 204 ctx->vertices[0][1][1] = t0; /*t*/ 205 206 ctx->vertices[1][0][0] = x1; 207 ctx->vertices[1][0][1] = y0; 208 ctx->vertices[1][0][2] = z; 209 ctx->vertices[1][1][0] = s1; /*s*/ 210 ctx->vertices[1][1][1] = t0; /*t*/ 211 212 ctx->vertices[2][0][0] = x1; 213 ctx->vertices[2][0][1] = y1; 214 ctx->vertices[2][0][2] = z; 215 ctx->vertices[2][1][0] = s1; 216 ctx->vertices[2][1][1] = t1; 217 218 ctx->vertices[3][0][0] = x0; 219 ctx->vertices[3][0][1] = y1; 220 ctx->vertices[3][0][2] = z; 221 ctx->vertices[3][1][0] = s0; 222 ctx->vertices[3][1][1] = t1; 223 224 offset = get_next_slot( ctx ); 225 226 pipe_buffer_write(ctx->pipe->screen, ctx->vbuf, 227 offset, sizeof(ctx->vertices), ctx->vertices); 228 229 return offset; 230} 231 232 233/** 234 * \return TRUE if two regions overlap, FALSE otherwise 235 */ 236static boolean 237regions_overlap(int srcX0, int srcY0, 238 int srcX1, int srcY1, 239 int dstX0, int dstY0, 240 int dstX1, int dstY1) 241{ 242 if (MAX2(srcX0, srcX1) < MIN2(dstX0, dstX1)) 243 return FALSE; /* src completely left of dst */ 244 245 if (MAX2(dstX0, dstX1) < MIN2(srcX0, srcX1)) 246 return FALSE; /* dst completely left of src */ 247 248 if (MAX2(srcY0, srcY1) < MIN2(dstY0, dstY1)) 249 return FALSE; /* src completely above dst */ 250 251 if (MAX2(dstY0, dstY1) < MIN2(srcY0, srcY1)) 252 return FALSE; /* dst completely above src */ 253 254 return TRUE; /* some overlap */ 255} 256 257 258/** 259 * Copy pixel block from src surface to dst surface. 260 * Overlapping regions are acceptable. 261 * Flipping and stretching are supported. 262 * XXX what about clipping??? 263 * XXX need some control over blitting Z and/or stencil. 264 */ 265void 266util_blit_pixels_writemask(struct blit_state *ctx, 267 struct pipe_surface *src, 268 int srcX0, int srcY0, 269 int srcX1, int srcY1, 270 struct pipe_surface *dst, 271 int dstX0, int dstY0, 272 int dstX1, int dstY1, 273 float z, uint filter, 274 uint writemask) 275{ 276 struct pipe_context *pipe = ctx->pipe; 277 struct pipe_screen *screen = pipe->screen; 278 struct pipe_texture *tex = NULL; 279 struct pipe_framebuffer_state fb; 280 const int srcW = abs(srcX1 - srcX0); 281 const int srcH = abs(srcY1 - srcY0); 282 unsigned offset; 283 boolean overlap; 284 float s0, t0, s1, t1; 285 286 assert(filter == PIPE_TEX_MIPFILTER_NEAREST || 287 filter == PIPE_TEX_MIPFILTER_LINEAR); 288 289 assert(screen->is_format_supported(screen, src->format, PIPE_TEXTURE_2D, 290 PIPE_TEXTURE_USAGE_SAMPLER, 0)); 291 assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D, 292 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)); 293 294 /* do the regions overlap? */ 295 overlap = util_same_surface(src, dst) && 296 regions_overlap(srcX0, srcY0, srcX1, srcY1, 297 dstX0, dstY0, dstX1, dstY1); 298 299 /* 300 * Check for simple case: no format conversion, no flipping, no stretching, 301 * no overlapping. 302 * Filter mode should not matter since there's no stretching. 303 */ 304 if (dst->format == src->format && 305 srcX0 < srcX1 && 306 dstX0 < dstX1 && 307 srcY0 < srcY1 && 308 dstY0 < dstY1 && 309 (dstX1 - dstX0) == (srcX1 - srcX0) && 310 (dstY1 - dstY0) == (srcY1 - srcY0) && 311 !overlap) { 312 pipe->surface_copy(pipe, 313 dst, dstX0, dstY0, /* dest */ 314 src, srcX0, srcY0, /* src */ 315 srcW, srcH); /* size */ 316 return; 317 } 318 319 assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D, 320 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)); 321 322 /* Create a temporary texture when src and dest alias or when src 323 * is anything other than a single-level 2d texture. 324 * 325 * This can still be improved upon. 326 */ 327 if (util_same_surface(src, dst) || 328 src->texture->target != PIPE_TEXTURE_2D || 329 src->texture->last_level != 0) 330 { 331 struct pipe_texture texTemp; 332 struct pipe_surface *texSurf; 333 const int srcLeft = MIN2(srcX0, srcX1); 334 const int srcTop = MIN2(srcY0, srcY1); 335 336 if (srcLeft != srcX0) { 337 /* left-right flip */ 338 int tmp = dstX0; 339 dstX0 = dstX1; 340 dstX1 = tmp; 341 } 342 343 if (srcTop != srcY0) { 344 /* up-down flip */ 345 int tmp = dstY0; 346 dstY0 = dstY1; 347 dstY1 = tmp; 348 } 349 350 /* create temp texture */ 351 memset(&texTemp, 0, sizeof(texTemp)); 352 texTemp.target = PIPE_TEXTURE_2D; 353 texTemp.format = src->format; 354 texTemp.last_level = 0; 355 texTemp.width[0] = srcW; 356 texTemp.height[0] = srcH; 357 texTemp.depth[0] = 1; 358 pf_get_block(src->format, &texTemp.block); 359 360 tex = screen->texture_create(screen, &texTemp); 361 if (!tex) 362 return; 363 364 texSurf = screen->get_tex_surface(screen, tex, 0, 0, 0, 365 PIPE_BUFFER_USAGE_GPU_WRITE); 366 367 /* load temp texture */ 368 pipe->surface_copy(pipe, 369 texSurf, 0, 0, /* dest */ 370 src, srcLeft, srcTop, /* src */ 371 srcW, srcH); /* size */ 372 373 /* free the surface, update the texture if necessary. 374 */ 375 pipe_surface_reference(&texSurf, NULL); 376 s0 = 0.0f; 377 s1 = 1.0f; 378 t0 = 0.0f; 379 t1 = 1.0f; 380 } 381 else { 382 pipe_texture_reference(&tex, src->texture); 383 s0 = srcX0 / (float)tex->width[0]; 384 s1 = srcX1 / (float)tex->width[0]; 385 t0 = srcY0 / (float)tex->height[0]; 386 t1 = srcY1 / (float)tex->height[0]; 387 } 388 389 390 /* save state (restored below) */ 391 cso_save_blend(ctx->cso); 392 cso_save_depth_stencil_alpha(ctx->cso); 393 cso_save_rasterizer(ctx->cso); 394 cso_save_samplers(ctx->cso); 395 cso_save_sampler_textures(ctx->cso); 396 cso_save_framebuffer(ctx->cso); 397 cso_save_fragment_shader(ctx->cso); 398 cso_save_vertex_shader(ctx->cso); 399 400 /* set misc state we care about */ 401 cso_set_blend(ctx->cso, &ctx->blend); 402 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil); 403 cso_set_rasterizer(ctx->cso, &ctx->rasterizer); 404 405 /* sampler */ 406 ctx->sampler.min_img_filter = filter; 407 ctx->sampler.mag_img_filter = filter; 408 cso_single_sampler(ctx->cso, 0, &ctx->sampler); 409 cso_single_sampler_done(ctx->cso); 410 411 /* texture */ 412 cso_set_sampler_textures(ctx->cso, 1, &tex); 413 414 if (ctx->fs[writemask] == NULL) 415 ctx->fs[writemask] = util_make_fragment_tex_shader_writemask(pipe, writemask); 416 417 /* shaders */ 418 cso_set_fragment_shader_handle(ctx->cso, ctx->fs[writemask]); 419 cso_set_vertex_shader_handle(ctx->cso, ctx->vs); 420 421 /* drawing dest */ 422 memset(&fb, 0, sizeof(fb)); 423 fb.width = dst->width; 424 fb.height = dst->height; 425 fb.nr_cbufs = 1; 426 fb.cbufs[0] = dst; 427 cso_set_framebuffer(ctx->cso, &fb); 428 429 /* draw quad */ 430 offset = setup_vertex_data_tex(ctx, 431 (float) dstX0, (float) dstY0, 432 (float) dstX1, (float) dstY1, 433 s0, t0, 434 s1, t1, 435 z); 436 437 util_draw_vertex_buffer(ctx->pipe, ctx->vbuf, offset, 438 PIPE_PRIM_TRIANGLE_FAN, 439 4, /* verts */ 440 2); /* attribs/vert */ 441 442 /* restore state we changed */ 443 cso_restore_blend(ctx->cso); 444 cso_restore_depth_stencil_alpha(ctx->cso); 445 cso_restore_rasterizer(ctx->cso); 446 cso_restore_samplers(ctx->cso); 447 cso_restore_sampler_textures(ctx->cso); 448 cso_restore_framebuffer(ctx->cso); 449 cso_restore_fragment_shader(ctx->cso); 450 cso_restore_vertex_shader(ctx->cso); 451 452 pipe_texture_reference(&tex, NULL); 453} 454 455 456void 457util_blit_pixels(struct blit_state *ctx, 458 struct pipe_surface *src, 459 int srcX0, int srcY0, 460 int srcX1, int srcY1, 461 struct pipe_surface *dst, 462 int dstX0, int dstY0, 463 int dstX1, int dstY1, 464 float z, uint filter ) 465{ 466 util_blit_pixels_writemask( ctx, src, 467 srcX0, srcY0, 468 srcX1, srcY1, 469 dst, 470 dstX0, dstY0, 471 dstX1, dstY1, 472 z, filter, 473 TGSI_WRITEMASK_XYZW ); 474} 475 476 477/* Release vertex buffer at end of frame to avoid synchronous 478 * rendering. 479 */ 480void util_blit_flush( struct blit_state *ctx ) 481{ 482 pipe_buffer_reference(&ctx->vbuf, NULL); 483 ctx->vbuf_slot = 0; 484} 485 486 487 488/** 489 * Copy pixel block from src texture to dst surface. 490 * Overlapping regions are acceptable. 491 * 492 * XXX Should support selection of level. 493 * XXX need some control over blitting Z and/or stencil. 494 */ 495void 496util_blit_pixels_tex(struct blit_state *ctx, 497 struct pipe_texture *tex, 498 int srcX0, int srcY0, 499 int srcX1, int srcY1, 500 struct pipe_surface *dst, 501 int dstX0, int dstY0, 502 int dstX1, int dstY1, 503 float z, uint filter) 504{ 505 struct pipe_framebuffer_state fb; 506 float s0, t0, s1, t1; 507 unsigned offset; 508 509 assert(filter == PIPE_TEX_MIPFILTER_NEAREST || 510 filter == PIPE_TEX_MIPFILTER_LINEAR); 511 512 assert(tex->width[0] != 0); 513 assert(tex->height[0] != 0); 514 515 s0 = srcX0 / (float)tex->width[0]; 516 s1 = srcX1 / (float)tex->width[0]; 517 t0 = srcY0 / (float)tex->height[0]; 518 t1 = srcY1 / (float)tex->height[0]; 519 520 assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format, 521 PIPE_TEXTURE_2D, 522 PIPE_TEXTURE_USAGE_RENDER_TARGET, 523 0)); 524 525 /* save state (restored below) */ 526 cso_save_blend(ctx->cso); 527 cso_save_depth_stencil_alpha(ctx->cso); 528 cso_save_rasterizer(ctx->cso); 529 cso_save_samplers(ctx->cso); 530 cso_save_sampler_textures(ctx->cso); 531 cso_save_framebuffer(ctx->cso); 532 cso_save_fragment_shader(ctx->cso); 533 cso_save_vertex_shader(ctx->cso); 534 535 /* set misc state we care about */ 536 cso_set_blend(ctx->cso, &ctx->blend); 537 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil); 538 cso_set_rasterizer(ctx->cso, &ctx->rasterizer); 539 540 /* sampler */ 541 ctx->sampler.min_img_filter = filter; 542 ctx->sampler.mag_img_filter = filter; 543 cso_single_sampler(ctx->cso, 0, &ctx->sampler); 544 cso_single_sampler_done(ctx->cso); 545 546 /* texture */ 547 cso_set_sampler_textures(ctx->cso, 1, &tex); 548 549 /* shaders */ 550 cso_set_fragment_shader_handle(ctx->cso, ctx->fs[TGSI_WRITEMASK_XYZW]); 551 cso_set_vertex_shader_handle(ctx->cso, ctx->vs); 552 553 /* drawing dest */ 554 memset(&fb, 0, sizeof(fb)); 555 fb.width = dst->width; 556 fb.height = dst->height; 557 fb.nr_cbufs = 1; 558 fb.cbufs[0] = dst; 559 cso_set_framebuffer(ctx->cso, &fb); 560 561 /* draw quad */ 562 offset = setup_vertex_data_tex(ctx, 563 (float) dstX0, (float) dstY0, 564 (float) dstX1, (float) dstY1, 565 s0, t0, s1, t1, 566 z); 567 568 util_draw_vertex_buffer(ctx->pipe, 569 ctx->vbuf, offset, 570 PIPE_PRIM_TRIANGLE_FAN, 571 4, /* verts */ 572 2); /* attribs/vert */ 573 574 /* restore state we changed */ 575 cso_restore_blend(ctx->cso); 576 cso_restore_depth_stencil_alpha(ctx->cso); 577 cso_restore_rasterizer(ctx->cso); 578 cso_restore_samplers(ctx->cso); 579 cso_restore_sampler_textures(ctx->cso); 580 cso_restore_framebuffer(ctx->cso); 581 cso_restore_fragment_shader(ctx->cso); 582 cso_restore_vertex_shader(ctx->cso); 583} 584