u_blit.c revision dc4c821f0817a3db716f965692fb701079f66340
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 "util/u_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_format.h" 46#include "util/u_math.h" 47#include "util/u_memory.h" 48#include "util/u_sampler.h" 49#include "util/u_simple_shaders.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_keep; 61 struct pipe_depth_stencil_alpha_state depthstencil_write; 62 struct pipe_rasterizer_state rasterizer; 63 struct pipe_sampler_state sampler; 64 struct pipe_viewport_state viewport; 65 struct pipe_vertex_element velem[2]; 66 enum pipe_texture_target internal_target; 67 68 void *vs; 69 void *fs[TGSI_WRITEMASK_XYZW + 1]; 70 void *fs_depth; 71 72 struct pipe_resource *vbuf; /**< quad vertices */ 73 unsigned vbuf_slot; 74 75 float vertices[4][2][4]; /**< vertex/texcoords for quad */ 76}; 77 78 79/** 80 * Create state object for blit. 81 * Intended to be created once and re-used for many blit() calls. 82 */ 83struct blit_state * 84util_create_blit(struct pipe_context *pipe, struct cso_context *cso) 85{ 86 struct blit_state *ctx; 87 uint i; 88 89 ctx = CALLOC_STRUCT(blit_state); 90 if (!ctx) 91 return NULL; 92 93 ctx->pipe = pipe; 94 ctx->cso = cso; 95 96 /* disabled blending/masking */ 97 memset(&ctx->blend, 0, sizeof(ctx->blend)); 98 ctx->blend.rt[0].colormask = PIPE_MASK_RGBA; 99 100 /* no-op depth/stencil/alpha */ 101 memset(&ctx->depthstencil_keep, 0, sizeof(ctx->depthstencil_keep)); 102 memset(&ctx->depthstencil_write, 0, sizeof(ctx->depthstencil_write)); 103 ctx->depthstencil_write.depth.enabled = 1; 104 ctx->depthstencil_write.depth.writemask = 1; 105 ctx->depthstencil_write.depth.func = PIPE_FUNC_ALWAYS; 106 107 /* rasterizer */ 108 memset(&ctx->rasterizer, 0, sizeof(ctx->rasterizer)); 109 ctx->rasterizer.cull_face = PIPE_FACE_NONE; 110 ctx->rasterizer.gl_rasterization_rules = 1; 111 ctx->rasterizer.depth_clip = 1; 112 113 /* samplers */ 114 memset(&ctx->sampler, 0, sizeof(ctx->sampler)); 115 ctx->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 116 ctx->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 117 ctx->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 118 ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 119 ctx->sampler.min_img_filter = 0; /* set later */ 120 ctx->sampler.mag_img_filter = 0; /* set later */ 121 122 /* vertex elements state */ 123 memset(&ctx->velem[0], 0, sizeof(ctx->velem[0]) * 2); 124 for (i = 0; i < 2; i++) { 125 ctx->velem[i].src_offset = i * 4 * sizeof(float); 126 ctx->velem[i].instance_divisor = 0; 127 ctx->velem[i].vertex_buffer_index = 0; 128 ctx->velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 129 } 130 131 ctx->vbuf = NULL; 132 133 /* init vertex data that doesn't change */ 134 for (i = 0; i < 4; i++) { 135 ctx->vertices[i][0][3] = 1.0f; /* w */ 136 ctx->vertices[i][1][2] = 0.0f; /* r */ 137 ctx->vertices[i][1][3] = 1.0f; /* q */ 138 } 139 140 if(pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES)) 141 ctx->internal_target = PIPE_TEXTURE_2D; 142 else 143 ctx->internal_target = PIPE_TEXTURE_RECT; 144 145 return ctx; 146} 147 148 149/** 150 * Destroy a blit context 151 */ 152void 153util_destroy_blit(struct blit_state *ctx) 154{ 155 struct pipe_context *pipe = ctx->pipe; 156 unsigned i; 157 158 if (ctx->vs) 159 pipe->delete_vs_state(pipe, ctx->vs); 160 161 for (i = 0; i < Elements(ctx->fs); i++) 162 if (ctx->fs[i]) 163 pipe->delete_fs_state(pipe, ctx->fs[i]); 164 165 if (ctx->fs_depth) 166 pipe->delete_fs_state(pipe, ctx->fs_depth); 167 168 pipe_resource_reference(&ctx->vbuf, NULL); 169 170 FREE(ctx); 171} 172 173 174/** 175 * Helper function to set the fragment shaders. 176 */ 177static INLINE void 178set_fragment_shader(struct blit_state *ctx, uint writemask) 179{ 180 if (!ctx->fs[writemask]) 181 ctx->fs[writemask] = 182 util_make_fragment_tex_shader_writemask(ctx->pipe, TGSI_TEXTURE_2D, 183 TGSI_INTERPOLATE_LINEAR, 184 writemask); 185 186 cso_set_fragment_shader_handle(ctx->cso, ctx->fs[writemask]); 187} 188 189 190/** 191 * Helper function to set the depthwrite shader. 192 */ 193static INLINE void 194set_depth_fragment_shader(struct blit_state *ctx) 195{ 196 if (!ctx->fs_depth) 197 ctx->fs_depth = 198 util_make_fragment_tex_shader_writedepth(ctx->pipe, TGSI_TEXTURE_2D, 199 TGSI_INTERPOLATE_LINEAR); 200 201 cso_set_fragment_shader_handle(ctx->cso, ctx->fs_depth); 202} 203 204 205/** 206 * Helper function to set the vertex shader. 207 */ 208static INLINE void 209set_vertex_shader(struct blit_state *ctx) 210{ 211 /* vertex shader - still required to provide the linkage between 212 * fragment shader input semantics and vertex_element/buffers. 213 */ 214 if (!ctx->vs) { 215 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION, 216 TGSI_SEMANTIC_GENERIC }; 217 const uint semantic_indexes[] = { 0, 0 }; 218 ctx->vs = util_make_vertex_passthrough_shader(ctx->pipe, 2, 219 semantic_names, 220 semantic_indexes); 221 } 222 223 cso_set_vertex_shader_handle(ctx->cso, ctx->vs); 224} 225 226 227/** 228 * Get offset of next free slot in vertex buffer for quad vertices. 229 */ 230static unsigned 231get_next_slot( struct blit_state *ctx ) 232{ 233 const unsigned max_slots = 4096 / sizeof ctx->vertices; 234 235 if (ctx->vbuf_slot >= max_slots) 236 util_blit_flush( ctx ); 237 238 if (!ctx->vbuf) { 239 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen, 240 PIPE_BIND_VERTEX_BUFFER, 241 PIPE_USAGE_STREAM, 242 max_slots * sizeof ctx->vertices); 243 } 244 245 return ctx->vbuf_slot++ * sizeof ctx->vertices; 246} 247 248 249 250 251/** 252 * Setup vertex data for the textured quad we'll draw. 253 * Note: y=0=top 254 */ 255static unsigned 256setup_vertex_data_tex(struct blit_state *ctx, 257 float x0, float y0, float x1, float y1, 258 float s0, float t0, float s1, float t1, 259 float z) 260{ 261 unsigned offset; 262 263 ctx->vertices[0][0][0] = x0; 264 ctx->vertices[0][0][1] = y0; 265 ctx->vertices[0][0][2] = z; 266 ctx->vertices[0][1][0] = s0; /*s*/ 267 ctx->vertices[0][1][1] = t0; /*t*/ 268 269 ctx->vertices[1][0][0] = x1; 270 ctx->vertices[1][0][1] = y0; 271 ctx->vertices[1][0][2] = z; 272 ctx->vertices[1][1][0] = s1; /*s*/ 273 ctx->vertices[1][1][1] = t0; /*t*/ 274 275 ctx->vertices[2][0][0] = x1; 276 ctx->vertices[2][0][1] = y1; 277 ctx->vertices[2][0][2] = z; 278 ctx->vertices[2][1][0] = s1; 279 ctx->vertices[2][1][1] = t1; 280 281 ctx->vertices[3][0][0] = x0; 282 ctx->vertices[3][0][1] = y1; 283 ctx->vertices[3][0][2] = z; 284 ctx->vertices[3][1][0] = s0; 285 ctx->vertices[3][1][1] = t1; 286 287 offset = get_next_slot( ctx ); 288 289 if (ctx->vbuf) { 290 pipe_buffer_write_nooverlap(ctx->pipe, ctx->vbuf, 291 offset, sizeof(ctx->vertices), ctx->vertices); 292 } 293 294 return offset; 295} 296 297 298/** 299 * \return TRUE if two regions overlap, FALSE otherwise 300 */ 301static boolean 302regions_overlap(int srcX0, int srcY0, 303 int srcX1, int srcY1, 304 int dstX0, int dstY0, 305 int dstX1, int dstY1) 306{ 307 if (MAX2(srcX0, srcX1) < MIN2(dstX0, dstX1)) 308 return FALSE; /* src completely left of dst */ 309 310 if (MAX2(dstX0, dstX1) < MIN2(srcX0, srcX1)) 311 return FALSE; /* dst completely left of src */ 312 313 if (MAX2(srcY0, srcY1) < MIN2(dstY0, dstY1)) 314 return FALSE; /* src completely above dst */ 315 316 if (MAX2(dstY0, dstY1) < MIN2(srcY0, srcY1)) 317 return FALSE; /* dst completely above src */ 318 319 return TRUE; /* some overlap */ 320} 321 322 323/** 324 * Copy pixel block from src surface to dst surface. 325 * Overlapping regions are acceptable. 326 * Flipping and stretching are supported. 327 * \param filter one of PIPE_TEX_MIPFILTER_NEAREST/LINEAR 328 * \param writemask controls which channels in the dest surface are sourced 329 * from the src surface. Disabled channels are sourced 330 * from (0,0,0,1). 331 * XXX need some control over blitting stencil. 332 */ 333void 334util_blit_pixels_writemask(struct blit_state *ctx, 335 struct pipe_resource *src_tex, 336 unsigned src_level, 337 int srcX0, int srcY0, 338 int srcX1, int srcY1, 339 int srcZ0, 340 struct pipe_surface *dst, 341 int dstX0, int dstY0, 342 int dstX1, int dstY1, 343 float z, uint filter, 344 uint writemask) 345{ 346 struct pipe_context *pipe = ctx->pipe; 347 struct pipe_screen *screen = pipe->screen; 348 enum pipe_format src_format, dst_format; 349 struct pipe_sampler_view *sampler_view = NULL; 350 struct pipe_sampler_view sv_templ; 351 struct pipe_surface *dst_surface; 352 struct pipe_framebuffer_state fb; 353 const int srcW = abs(srcX1 - srcX0); 354 const int srcH = abs(srcY1 - srcY0); 355 unsigned offset; 356 boolean overlap, dst_is_depth; 357 float s0, t0, s1, t1; 358 boolean normalized; 359 360 assert(filter == PIPE_TEX_MIPFILTER_NEAREST || 361 filter == PIPE_TEX_MIPFILTER_LINEAR); 362 363 assert(src_level <= src_tex->last_level); 364 365 /* do the regions overlap? */ 366 overlap = src_tex == dst->texture && 367 dst->u.tex.level == src_level && 368 dst->u.tex.first_layer == srcZ0 && 369 regions_overlap(srcX0, srcY0, srcX1, srcY1, 370 dstX0, dstY0, dstX1, dstY1); 371 372 src_format = util_format_linear(src_tex->format); 373 dst_format = util_format_linear(dst->format); 374 375 /* 376 * Check for simple case: no format conversion, no flipping, no stretching, 377 * no overlapping. 378 * Filter mode should not matter since there's no stretching. 379 */ 380 if (dst_format == src_format && 381 srcX0 < srcX1 && 382 dstX0 < dstX1 && 383 srcY0 < srcY1 && 384 dstY0 < dstY1 && 385 (dstX1 - dstX0) == (srcX1 - srcX0) && 386 (dstY1 - dstY0) == (srcY1 - srcY0) && 387 !overlap) { 388 struct pipe_box src_box; 389 src_box.x = srcX0; 390 src_box.y = srcY0; 391 src_box.z = srcZ0; 392 src_box.width = srcW; 393 src_box.height = srcH; 394 src_box.depth = 1; 395 pipe->resource_copy_region(pipe, 396 dst->texture, dst->u.tex.level, 397 dstX0, dstY0, dst->u.tex.first_layer,/* dest */ 398 src_tex, src_level, 399 &src_box); 400 return; 401 } 402 403 if (dst_format == dst->format) { 404 dst_surface = dst; 405 } else { 406 struct pipe_surface templ = *dst; 407 templ.format = dst_format; 408 dst_surface = pipe->create_surface(pipe, dst->texture, &templ); 409 } 410 411 /* Create a temporary texture when src and dest alias or when src 412 * is anything other than a 2d texture. 413 * XXX should just use appropriate shader to access 1d / 3d slice / cube face, 414 * much like the u_blitter code does (should be pretty trivial). 415 * 416 * This can still be improved upon. 417 */ 418 if ((src_tex == dst_surface->texture && 419 dst_surface->u.tex.level == src_level && 420 dst_surface->u.tex.first_layer == srcZ0) || 421 (src_tex->target != PIPE_TEXTURE_2D && 422 src_tex->target != PIPE_TEXTURE_2D && 423 src_tex->target != PIPE_TEXTURE_RECT)) 424 { 425 struct pipe_resource texTemp; 426 struct pipe_resource *tex; 427 struct pipe_sampler_view sv_templ; 428 struct pipe_box src_box; 429 const int srcLeft = MIN2(srcX0, srcX1); 430 const int srcTop = MIN2(srcY0, srcY1); 431 432 if (srcLeft != srcX0) { 433 /* left-right flip */ 434 int tmp = dstX0; 435 dstX0 = dstX1; 436 dstX1 = tmp; 437 } 438 439 if (srcTop != srcY0) { 440 /* up-down flip */ 441 int tmp = dstY0; 442 dstY0 = dstY1; 443 dstY1 = tmp; 444 } 445 446 /* create temp texture */ 447 memset(&texTemp, 0, sizeof(texTemp)); 448 texTemp.target = ctx->internal_target; 449 texTemp.format = src_format; 450 texTemp.last_level = 0; 451 texTemp.width0 = srcW; 452 texTemp.height0 = srcH; 453 texTemp.depth0 = 1; 454 texTemp.array_size = 1; 455 texTemp.bind = PIPE_BIND_SAMPLER_VIEW; 456 457 tex = screen->resource_create(screen, &texTemp); 458 if (!tex) 459 return; 460 461 src_box.x = srcLeft; 462 src_box.y = srcTop; 463 src_box.z = srcZ0; 464 src_box.width = srcW; 465 src_box.height = srcH; 466 src_box.depth = 1; 467 /* load temp texture */ 468 pipe->resource_copy_region(pipe, 469 tex, 0, 0, 0, 0, /* dest */ 470 src_tex, src_level, &src_box); 471 472 normalized = tex->target != PIPE_TEXTURE_RECT; 473 if(normalized) { 474 s0 = 0.0f; 475 s1 = 1.0f; 476 t0 = 0.0f; 477 t1 = 1.0f; 478 } 479 else { 480 s0 = 0; 481 s1 = srcW; 482 t0 = 0; 483 t1 = srcH; 484 } 485 486 u_sampler_view_default_template(&sv_templ, tex, tex->format); 487 sampler_view = pipe->create_sampler_view(pipe, tex, &sv_templ); 488 489 if (!sampler_view) { 490 pipe_resource_reference(&tex, NULL); 491 return; 492 } 493 pipe_resource_reference(&tex, NULL); 494 } 495 else { 496 u_sampler_view_default_template(&sv_templ, src_tex, src_format); 497 sampler_view = pipe->create_sampler_view(pipe, src_tex, &sv_templ); 498 499 if (!sampler_view) { 500 return; 501 } 502 503 s0 = srcX0; 504 s1 = srcX1; 505 t0 = srcY0; 506 t1 = srcY1; 507 normalized = sampler_view->texture->target != PIPE_TEXTURE_RECT; 508 if(normalized) 509 { 510 s0 /= (float)(u_minify(sampler_view->texture->width0, src_level)); 511 s1 /= (float)(u_minify(sampler_view->texture->width0, src_level)); 512 t0 /= (float)(u_minify(sampler_view->texture->height0, src_level)); 513 t1 /= (float)(u_minify(sampler_view->texture->height0, src_level)); 514 } 515 } 516 517 dst_is_depth = util_format_is_depth_or_stencil(dst_format); 518 519 assert(screen->is_format_supported(screen, sampler_view->format, ctx->internal_target, 520 sampler_view->texture->nr_samples, 521 PIPE_BIND_SAMPLER_VIEW)); 522 assert(screen->is_format_supported(screen, dst_format, ctx->internal_target, 523 dst_surface->texture->nr_samples, 524 dst_is_depth ? PIPE_BIND_DEPTH_STENCIL : 525 PIPE_BIND_RENDER_TARGET)); 526 /* save state (restored below) */ 527 cso_save_blend(ctx->cso); 528 cso_save_depth_stencil_alpha(ctx->cso); 529 cso_save_rasterizer(ctx->cso); 530 cso_save_samplers(ctx->cso); 531 cso_save_fragment_sampler_views(ctx->cso); 532 cso_save_stream_outputs(ctx->cso); 533 cso_save_viewport(ctx->cso); 534 cso_save_framebuffer(ctx->cso); 535 cso_save_fragment_shader(ctx->cso); 536 cso_save_vertex_shader(ctx->cso); 537 cso_save_geometry_shader(ctx->cso); 538 cso_save_vertex_elements(ctx->cso); 539 cso_save_vertex_buffers(ctx->cso); 540 541 /* set misc state we care about */ 542 cso_set_blend(ctx->cso, &ctx->blend); 543 cso_set_depth_stencil_alpha(ctx->cso, 544 dst_is_depth ? &ctx->depthstencil_write : 545 &ctx->depthstencil_keep); 546 cso_set_rasterizer(ctx->cso, &ctx->rasterizer); 547 cso_set_vertex_elements(ctx->cso, 2, ctx->velem); 548 cso_set_stream_outputs(ctx->cso, 0, NULL, 0); 549 550 /* sampler */ 551 ctx->sampler.normalized_coords = normalized; 552 ctx->sampler.min_img_filter = filter; 553 ctx->sampler.mag_img_filter = filter; 554 ctx->sampler.min_lod = src_level; 555 ctx->sampler.max_lod = src_level; 556 cso_single_sampler(ctx->cso, 0, &ctx->sampler); 557 cso_single_sampler_done(ctx->cso); 558 559 /* viewport */ 560 ctx->viewport.scale[0] = 0.5f * dst_surface->width; 561 ctx->viewport.scale[1] = 0.5f * dst_surface->height; 562 ctx->viewport.scale[2] = 0.5f; 563 ctx->viewport.scale[3] = 1.0f; 564 ctx->viewport.translate[0] = 0.5f * dst_surface->width; 565 ctx->viewport.translate[1] = 0.5f * dst_surface->height; 566 ctx->viewport.translate[2] = 0.5f; 567 ctx->viewport.translate[3] = 0.0f; 568 cso_set_viewport(ctx->cso, &ctx->viewport); 569 570 /* texture */ 571 cso_set_fragment_sampler_views(ctx->cso, 1, &sampler_view); 572 573 /* shaders */ 574 if (dst_is_depth) { 575 set_depth_fragment_shader(ctx); 576 } else { 577 set_fragment_shader(ctx, writemask); 578 } 579 set_vertex_shader(ctx); 580 cso_set_geometry_shader_handle(ctx->cso, NULL); 581 582 /* drawing dest */ 583 memset(&fb, 0, sizeof(fb)); 584 fb.width = dst_surface->width; 585 fb.height = dst_surface->height; 586 if (dst_is_depth) { 587 fb.zsbuf = dst_surface; 588 } else { 589 fb.nr_cbufs = 1; 590 fb.cbufs[0] = dst_surface; 591 } 592 cso_set_framebuffer(ctx->cso, &fb); 593 594 /* draw quad */ 595 offset = setup_vertex_data_tex(ctx, 596 (float) dstX0 / dst_surface->width * 2.0f - 1.0f, 597 (float) dstY0 / dst_surface->height * 2.0f - 1.0f, 598 (float) dstX1 / dst_surface->width * 2.0f - 1.0f, 599 (float) dstY1 / dst_surface->height * 2.0f - 1.0f, 600 s0, t0, 601 s1, t1, 602 z); 603 604 if (ctx->vbuf) { 605 util_draw_vertex_buffer(ctx->pipe, ctx->cso, ctx->vbuf, offset, 606 PIPE_PRIM_TRIANGLE_FAN, 607 4, /* verts */ 608 2); /* attribs/vert */ 609 } 610 611 /* restore state we changed */ 612 cso_restore_blend(ctx->cso); 613 cso_restore_depth_stencil_alpha(ctx->cso); 614 cso_restore_rasterizer(ctx->cso); 615 cso_restore_samplers(ctx->cso); 616 cso_restore_fragment_sampler_views(ctx->cso); 617 cso_restore_viewport(ctx->cso); 618 cso_restore_framebuffer(ctx->cso); 619 cso_restore_fragment_shader(ctx->cso); 620 cso_restore_vertex_shader(ctx->cso); 621 cso_restore_geometry_shader(ctx->cso); 622 cso_restore_vertex_elements(ctx->cso); 623 cso_restore_vertex_buffers(ctx->cso); 624 cso_restore_stream_outputs(ctx->cso); 625 626 pipe_sampler_view_reference(&sampler_view, NULL); 627 if (dst_surface != dst) 628 pipe_surface_reference(&dst_surface, NULL); 629} 630 631 632void 633util_blit_pixels(struct blit_state *ctx, 634 struct pipe_resource *src_tex, 635 unsigned src_level, 636 int srcX0, int srcY0, 637 int srcX1, int srcY1, 638 int srcZ, 639 struct pipe_surface *dst, 640 int dstX0, int dstY0, 641 int dstX1, int dstY1, 642 float z, uint filter ) 643{ 644 util_blit_pixels_writemask( ctx, src_tex, 645 src_level, 646 srcX0, srcY0, 647 srcX1, srcY1, 648 srcZ, 649 dst, 650 dstX0, dstY0, 651 dstX1, dstY1, 652 z, filter, 653 TGSI_WRITEMASK_XYZW ); 654} 655 656 657/* Release vertex buffer at end of frame to avoid synchronous 658 * rendering. 659 */ 660void util_blit_flush( struct blit_state *ctx ) 661{ 662 pipe_resource_reference(&ctx->vbuf, NULL); 663 ctx->vbuf_slot = 0; 664} 665 666 667 668/** 669 * Copy pixel block from src texture to dst surface. 670 * The sampler view's first_level field indicates the source 671 * mipmap level to use. 672 * XXX need some control over blitting Z and/or stencil. 673 */ 674void 675util_blit_pixels_tex(struct blit_state *ctx, 676 struct pipe_sampler_view *src_sampler_view, 677 int srcX0, int srcY0, 678 int srcX1, int srcY1, 679 struct pipe_surface *dst, 680 int dstX0, int dstY0, 681 int dstX1, int dstY1, 682 float z, uint filter) 683{ 684 boolean normalized = src_sampler_view->texture->target != PIPE_TEXTURE_RECT; 685 struct pipe_framebuffer_state fb; 686 float s0, t0, s1, t1; 687 unsigned offset; 688 struct pipe_resource *tex = src_sampler_view->texture; 689 690 assert(filter == PIPE_TEX_MIPFILTER_NEAREST || 691 filter == PIPE_TEX_MIPFILTER_LINEAR); 692 693 assert(tex); 694 assert(tex->width0 != 0); 695 assert(tex->height0 != 0); 696 697 s0 = srcX0; 698 s1 = srcX1; 699 t0 = srcY0; 700 t1 = srcY1; 701 702 if(normalized) 703 { 704 /* normalize according to the mipmap level's size */ 705 int level = src_sampler_view->u.tex.first_level; 706 float w = (float) u_minify(tex->width0, level); 707 float h = (float) u_minify(tex->height0, level); 708 s0 /= w; 709 s1 /= w; 710 t0 /= h; 711 t1 /= h; 712 } 713 714 assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format, 715 PIPE_TEXTURE_2D, 716 dst->texture->nr_samples, 717 PIPE_BIND_RENDER_TARGET)); 718 719 /* save state (restored below) */ 720 cso_save_blend(ctx->cso); 721 cso_save_depth_stencil_alpha(ctx->cso); 722 cso_save_rasterizer(ctx->cso); 723 cso_save_samplers(ctx->cso); 724 cso_save_fragment_sampler_views(ctx->cso); 725 cso_save_stream_outputs(ctx->cso); 726 cso_save_viewport(ctx->cso); 727 cso_save_framebuffer(ctx->cso); 728 cso_save_fragment_shader(ctx->cso); 729 cso_save_vertex_shader(ctx->cso); 730 cso_save_geometry_shader(ctx->cso); 731 cso_save_vertex_elements(ctx->cso); 732 cso_save_vertex_buffers(ctx->cso); 733 734 /* set misc state we care about */ 735 cso_set_blend(ctx->cso, &ctx->blend); 736 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil_keep); 737 cso_set_rasterizer(ctx->cso, &ctx->rasterizer); 738 cso_set_vertex_elements(ctx->cso, 2, ctx->velem); 739 cso_set_stream_outputs(ctx->cso, 0, NULL, 0); 740 741 /* sampler */ 742 ctx->sampler.normalized_coords = normalized; 743 ctx->sampler.min_img_filter = filter; 744 ctx->sampler.mag_img_filter = filter; 745 cso_single_sampler(ctx->cso, 0, &ctx->sampler); 746 cso_single_sampler_done(ctx->cso); 747 748 /* viewport */ 749 ctx->viewport.scale[0] = 0.5f * dst->width; 750 ctx->viewport.scale[1] = 0.5f * dst->height; 751 ctx->viewport.scale[2] = 0.5f; 752 ctx->viewport.scale[3] = 1.0f; 753 ctx->viewport.translate[0] = 0.5f * dst->width; 754 ctx->viewport.translate[1] = 0.5f * dst->height; 755 ctx->viewport.translate[2] = 0.5f; 756 ctx->viewport.translate[3] = 0.0f; 757 cso_set_viewport(ctx->cso, &ctx->viewport); 758 759 /* texture */ 760 cso_set_fragment_sampler_views(ctx->cso, 1, &src_sampler_view); 761 762 /* shaders */ 763 set_fragment_shader(ctx, TGSI_WRITEMASK_XYZW); 764 set_vertex_shader(ctx); 765 cso_set_geometry_shader_handle(ctx->cso, NULL); 766 767 /* drawing dest */ 768 memset(&fb, 0, sizeof(fb)); 769 fb.width = dst->width; 770 fb.height = dst->height; 771 fb.nr_cbufs = 1; 772 fb.cbufs[0] = dst; 773 cso_set_framebuffer(ctx->cso, &fb); 774 775 /* draw quad */ 776 offset = setup_vertex_data_tex(ctx, 777 (float) dstX0 / dst->width * 2.0f - 1.0f, 778 (float) dstY0 / dst->height * 2.0f - 1.0f, 779 (float) dstX1 / dst->width * 2.0f - 1.0f, 780 (float) dstY1 / dst->height * 2.0f - 1.0f, 781 s0, t0, s1, t1, 782 z); 783 784 util_draw_vertex_buffer(ctx->pipe, ctx->cso, 785 ctx->vbuf, offset, 786 PIPE_PRIM_TRIANGLE_FAN, 787 4, /* verts */ 788 2); /* attribs/vert */ 789 790 /* restore state we changed */ 791 cso_restore_blend(ctx->cso); 792 cso_restore_depth_stencil_alpha(ctx->cso); 793 cso_restore_rasterizer(ctx->cso); 794 cso_restore_samplers(ctx->cso); 795 cso_restore_fragment_sampler_views(ctx->cso); 796 cso_restore_viewport(ctx->cso); 797 cso_restore_framebuffer(ctx->cso); 798 cso_restore_fragment_shader(ctx->cso); 799 cso_restore_vertex_shader(ctx->cso); 800 cso_restore_geometry_shader(ctx->cso); 801 cso_restore_vertex_elements(ctx->cso); 802 cso_restore_vertex_buffers(ctx->cso); 803 cso_restore_stream_outputs(ctx->cso); 804} 805