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