u_blit.c revision 462dcf617303a507a1b000f52abbfa3fe695bb95
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 pipe_resource_reference(&ctx->vbuf, NULL); 237 ctx->vbuf_slot = 0; 238 } 239 240 if (!ctx->vbuf) { 241 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen, 242 PIPE_BIND_VERTEX_BUFFER, 243 PIPE_USAGE_STREAM, 244 max_slots * sizeof ctx->vertices); 245 } 246 247 return ctx->vbuf_slot++ * sizeof ctx->vertices; 248} 249 250 251 252 253/** 254 * Setup vertex data for the textured quad we'll draw. 255 * Note: y=0=top 256 */ 257static unsigned 258setup_vertex_data_tex(struct blit_state *ctx, 259 float x0, float y0, float x1, float y1, 260 float s0, float t0, float s1, float t1, 261 float z) 262{ 263 unsigned offset; 264 265 ctx->vertices[0][0][0] = x0; 266 ctx->vertices[0][0][1] = y0; 267 ctx->vertices[0][0][2] = z; 268 ctx->vertices[0][1][0] = s0; /*s*/ 269 ctx->vertices[0][1][1] = t0; /*t*/ 270 271 ctx->vertices[1][0][0] = x1; 272 ctx->vertices[1][0][1] = y0; 273 ctx->vertices[1][0][2] = z; 274 ctx->vertices[1][1][0] = s1; /*s*/ 275 ctx->vertices[1][1][1] = t0; /*t*/ 276 277 ctx->vertices[2][0][0] = x1; 278 ctx->vertices[2][0][1] = y1; 279 ctx->vertices[2][0][2] = z; 280 ctx->vertices[2][1][0] = s1; 281 ctx->vertices[2][1][1] = t1; 282 283 ctx->vertices[3][0][0] = x0; 284 ctx->vertices[3][0][1] = y1; 285 ctx->vertices[3][0][2] = z; 286 ctx->vertices[3][1][0] = s0; 287 ctx->vertices[3][1][1] = t1; 288 289 offset = get_next_slot( ctx ); 290 291 if (ctx->vbuf) { 292 pipe_buffer_write_nooverlap(ctx->pipe, ctx->vbuf, 293 offset, sizeof(ctx->vertices), ctx->vertices); 294 } 295 296 return offset; 297} 298 299 300/** 301 * \return TRUE if two regions overlap, FALSE otherwise 302 */ 303static boolean 304regions_overlap(int srcX0, int srcY0, 305 int srcX1, int srcY1, 306 int dstX0, int dstY0, 307 int dstX1, int dstY1) 308{ 309 if (MAX2(srcX0, srcX1) < MIN2(dstX0, dstX1)) 310 return FALSE; /* src completely left of dst */ 311 312 if (MAX2(dstX0, dstX1) < MIN2(srcX0, srcX1)) 313 return FALSE; /* dst completely left of src */ 314 315 if (MAX2(srcY0, srcY1) < MIN2(dstY0, dstY1)) 316 return FALSE; /* src completely above dst */ 317 318 if (MAX2(dstY0, dstY1) < MIN2(srcY0, srcY1)) 319 return FALSE; /* dst completely above src */ 320 321 return TRUE; /* some overlap */ 322} 323 324 325/** 326 * Can we blit from src format to dest format with a simple copy? 327 */ 328static boolean 329formats_compatible(enum pipe_format src_format, 330 enum pipe_format dst_format) 331{ 332 if (src_format == dst_format) { 333 return TRUE; 334 } 335 else { 336 const struct util_format_description *src_desc = 337 util_format_description(src_format); 338 const struct util_format_description *dst_desc = 339 util_format_description(dst_format); 340 return util_is_format_compatible(src_desc, dst_desc); 341 } 342} 343 344 345/** 346 * Copy pixel block from src surface to dst surface. 347 * Overlapping regions are acceptable. 348 * Flipping and stretching are supported. 349 * \param filter one of PIPE_TEX_MIPFILTER_NEAREST/LINEAR 350 * \param writemask controls which channels in the dest surface are sourced 351 * from the src surface. Disabled channels are sourced 352 * from (0,0,0,1). 353 * XXX need some control over blitting stencil. 354 */ 355void 356util_blit_pixels_writemask(struct blit_state *ctx, 357 struct pipe_resource *src_tex, 358 unsigned src_level, 359 int srcX0, int srcY0, 360 int srcX1, int srcY1, 361 int srcZ0, 362 struct pipe_surface *dst, 363 int dstX0, int dstY0, 364 int dstX1, int dstY1, 365 float z, uint filter, 366 uint writemask) 367{ 368 struct pipe_context *pipe = ctx->pipe; 369 struct pipe_screen *screen = pipe->screen; 370 enum pipe_format src_format, dst_format; 371 struct pipe_sampler_view *sampler_view = NULL; 372 struct pipe_sampler_view sv_templ; 373 struct pipe_surface *dst_surface; 374 struct pipe_framebuffer_state fb; 375 const int srcW = abs(srcX1 - srcX0); 376 const int srcH = abs(srcY1 - srcY0); 377 unsigned offset; 378 boolean overlap, dst_is_depth; 379 float s0, t0, s1, t1; 380 boolean normalized; 381 382 assert(filter == PIPE_TEX_MIPFILTER_NEAREST || 383 filter == PIPE_TEX_MIPFILTER_LINEAR); 384 385 assert(src_level <= src_tex->last_level); 386 387 /* do the regions overlap? */ 388 overlap = src_tex == dst->texture && 389 dst->u.tex.level == src_level && 390 dst->u.tex.first_layer == srcZ0 && 391 regions_overlap(srcX0, srcY0, srcX1, srcY1, 392 dstX0, dstY0, dstX1, dstY1); 393 394 src_format = util_format_linear(src_tex->format); 395 dst_format = util_format_linear(dst->format); 396 397 /* 398 * Check for simple case: no format conversion, no flipping, no stretching, 399 * no overlapping. 400 * Filter mode should not matter since there's no stretching. 401 */ 402 if (formats_compatible(src_format, dst_format) && 403 srcX0 < srcX1 && 404 dstX0 < dstX1 && 405 srcY0 < srcY1 && 406 dstY0 < dstY1 && 407 (dstX1 - dstX0) == (srcX1 - srcX0) && 408 (dstY1 - dstY0) == (srcY1 - srcY0) && 409 !overlap) { 410 struct pipe_box src_box; 411 src_box.x = srcX0; 412 src_box.y = srcY0; 413 src_box.z = srcZ0; 414 src_box.width = srcW; 415 src_box.height = srcH; 416 src_box.depth = 1; 417 pipe->resource_copy_region(pipe, 418 dst->texture, dst->u.tex.level, 419 dstX0, dstY0, dst->u.tex.first_layer,/* dest */ 420 src_tex, src_level, 421 &src_box); 422 return; 423 } 424 425 if (dst_format == dst->format) { 426 dst_surface = dst; 427 } else { 428 struct pipe_surface templ = *dst; 429 templ.format = dst_format; 430 dst_surface = pipe->create_surface(pipe, dst->texture, &templ); 431 } 432 433 /* Create a temporary texture when src and dest alias or when src 434 * is anything other than a 2d texture. 435 * XXX should just use appropriate shader to access 1d / 3d slice / cube face, 436 * much like the u_blitter code does (should be pretty trivial). 437 * 438 * This can still be improved upon. 439 */ 440 if ((src_tex == dst_surface->texture && 441 dst_surface->u.tex.level == src_level && 442 dst_surface->u.tex.first_layer == srcZ0) || 443 (src_tex->target != PIPE_TEXTURE_2D && 444 src_tex->target != PIPE_TEXTURE_2D && 445 src_tex->target != PIPE_TEXTURE_RECT)) 446 { 447 struct pipe_resource texTemp; 448 struct pipe_resource *tex; 449 struct pipe_sampler_view sv_templ; 450 struct pipe_box src_box; 451 const int srcLeft = MIN2(srcX0, srcX1); 452 const int srcTop = MIN2(srcY0, srcY1); 453 454 if (srcLeft != srcX0) { 455 /* left-right flip */ 456 int tmp = dstX0; 457 dstX0 = dstX1; 458 dstX1 = tmp; 459 } 460 461 if (srcTop != srcY0) { 462 /* up-down flip */ 463 int tmp = dstY0; 464 dstY0 = dstY1; 465 dstY1 = tmp; 466 } 467 468 /* create temp texture */ 469 memset(&texTemp, 0, sizeof(texTemp)); 470 texTemp.target = ctx->internal_target; 471 texTemp.format = src_format; 472 texTemp.last_level = 0; 473 texTemp.width0 = srcW; 474 texTemp.height0 = srcH; 475 texTemp.depth0 = 1; 476 texTemp.array_size = 1; 477 texTemp.bind = PIPE_BIND_SAMPLER_VIEW; 478 479 tex = screen->resource_create(screen, &texTemp); 480 if (!tex) 481 return; 482 483 src_box.x = srcLeft; 484 src_box.y = srcTop; 485 src_box.z = srcZ0; 486 src_box.width = srcW; 487 src_box.height = srcH; 488 src_box.depth = 1; 489 /* load temp texture */ 490 pipe->resource_copy_region(pipe, 491 tex, 0, 0, 0, 0, /* dest */ 492 src_tex, src_level, &src_box); 493 494 normalized = tex->target != PIPE_TEXTURE_RECT; 495 if(normalized) { 496 s0 = 0.0f; 497 s1 = 1.0f; 498 t0 = 0.0f; 499 t1 = 1.0f; 500 } 501 else { 502 s0 = 0; 503 s1 = srcW; 504 t0 = 0; 505 t1 = srcH; 506 } 507 508 u_sampler_view_default_template(&sv_templ, tex, tex->format); 509 sampler_view = pipe->create_sampler_view(pipe, tex, &sv_templ); 510 511 if (!sampler_view) { 512 pipe_resource_reference(&tex, NULL); 513 return; 514 } 515 pipe_resource_reference(&tex, NULL); 516 } 517 else { 518 u_sampler_view_default_template(&sv_templ, src_tex, src_format); 519 sampler_view = pipe->create_sampler_view(pipe, src_tex, &sv_templ); 520 521 if (!sampler_view) { 522 return; 523 } 524 525 s0 = srcX0; 526 s1 = srcX1; 527 t0 = srcY0; 528 t1 = srcY1; 529 normalized = sampler_view->texture->target != PIPE_TEXTURE_RECT; 530 if(normalized) 531 { 532 s0 /= (float)(u_minify(sampler_view->texture->width0, src_level)); 533 s1 /= (float)(u_minify(sampler_view->texture->width0, src_level)); 534 t0 /= (float)(u_minify(sampler_view->texture->height0, src_level)); 535 t1 /= (float)(u_minify(sampler_view->texture->height0, src_level)); 536 } 537 } 538 539 dst_is_depth = util_format_is_depth_or_stencil(dst_format); 540 541 assert(screen->is_format_supported(screen, sampler_view->format, ctx->internal_target, 542 sampler_view->texture->nr_samples, 543 PIPE_BIND_SAMPLER_VIEW)); 544 assert(screen->is_format_supported(screen, dst_format, ctx->internal_target, 545 dst_surface->texture->nr_samples, 546 dst_is_depth ? PIPE_BIND_DEPTH_STENCIL : 547 PIPE_BIND_RENDER_TARGET)); 548 /* save state (restored below) */ 549 cso_save_blend(ctx->cso); 550 cso_save_depth_stencil_alpha(ctx->cso); 551 cso_save_rasterizer(ctx->cso); 552 cso_save_samplers(ctx->cso); 553 cso_save_fragment_sampler_views(ctx->cso); 554 cso_save_stream_outputs(ctx->cso); 555 cso_save_viewport(ctx->cso); 556 cso_save_framebuffer(ctx->cso); 557 cso_save_fragment_shader(ctx->cso); 558 cso_save_vertex_shader(ctx->cso); 559 cso_save_geometry_shader(ctx->cso); 560 cso_save_vertex_elements(ctx->cso); 561 cso_save_vertex_buffers(ctx->cso); 562 563 /* set misc state we care about */ 564 cso_set_blend(ctx->cso, &ctx->blend); 565 cso_set_depth_stencil_alpha(ctx->cso, 566 dst_is_depth ? &ctx->depthstencil_write : 567 &ctx->depthstencil_keep); 568 cso_set_rasterizer(ctx->cso, &ctx->rasterizer); 569 cso_set_vertex_elements(ctx->cso, 2, ctx->velem); 570 cso_set_stream_outputs(ctx->cso, 0, NULL, 0); 571 572 /* sampler */ 573 ctx->sampler.normalized_coords = normalized; 574 ctx->sampler.min_img_filter = filter; 575 ctx->sampler.mag_img_filter = filter; 576 ctx->sampler.min_lod = src_level; 577 ctx->sampler.max_lod = src_level; 578 cso_single_sampler(ctx->cso, 0, &ctx->sampler); 579 cso_single_sampler_done(ctx->cso); 580 581 /* viewport */ 582 ctx->viewport.scale[0] = 0.5f * dst_surface->width; 583 ctx->viewport.scale[1] = 0.5f * dst_surface->height; 584 ctx->viewport.scale[2] = 0.5f; 585 ctx->viewport.scale[3] = 1.0f; 586 ctx->viewport.translate[0] = 0.5f * dst_surface->width; 587 ctx->viewport.translate[1] = 0.5f * dst_surface->height; 588 ctx->viewport.translate[2] = 0.5f; 589 ctx->viewport.translate[3] = 0.0f; 590 cso_set_viewport(ctx->cso, &ctx->viewport); 591 592 /* texture */ 593 cso_set_fragment_sampler_views(ctx->cso, 1, &sampler_view); 594 595 /* shaders */ 596 if (dst_is_depth) { 597 set_depth_fragment_shader(ctx); 598 } else { 599 set_fragment_shader(ctx, writemask); 600 } 601 set_vertex_shader(ctx); 602 cso_set_geometry_shader_handle(ctx->cso, NULL); 603 604 /* drawing dest */ 605 memset(&fb, 0, sizeof(fb)); 606 fb.width = dst_surface->width; 607 fb.height = dst_surface->height; 608 if (dst_is_depth) { 609 fb.zsbuf = dst_surface; 610 } else { 611 fb.nr_cbufs = 1; 612 fb.cbufs[0] = dst_surface; 613 } 614 cso_set_framebuffer(ctx->cso, &fb); 615 616 /* draw quad */ 617 offset = setup_vertex_data_tex(ctx, 618 (float) dstX0 / dst_surface->width * 2.0f - 1.0f, 619 (float) dstY0 / dst_surface->height * 2.0f - 1.0f, 620 (float) dstX1 / dst_surface->width * 2.0f - 1.0f, 621 (float) dstY1 / dst_surface->height * 2.0f - 1.0f, 622 s0, t0, 623 s1, t1, 624 z); 625 626 if (ctx->vbuf) { 627 util_draw_vertex_buffer(ctx->pipe, ctx->cso, ctx->vbuf, offset, 628 PIPE_PRIM_TRIANGLE_FAN, 629 4, /* verts */ 630 2); /* attribs/vert */ 631 } 632 633 /* restore state we changed */ 634 cso_restore_blend(ctx->cso); 635 cso_restore_depth_stencil_alpha(ctx->cso); 636 cso_restore_rasterizer(ctx->cso); 637 cso_restore_samplers(ctx->cso); 638 cso_restore_fragment_sampler_views(ctx->cso); 639 cso_restore_viewport(ctx->cso); 640 cso_restore_framebuffer(ctx->cso); 641 cso_restore_fragment_shader(ctx->cso); 642 cso_restore_vertex_shader(ctx->cso); 643 cso_restore_geometry_shader(ctx->cso); 644 cso_restore_vertex_elements(ctx->cso); 645 cso_restore_vertex_buffers(ctx->cso); 646 cso_restore_stream_outputs(ctx->cso); 647 648 pipe_sampler_view_reference(&sampler_view, NULL); 649 if (dst_surface != dst) 650 pipe_surface_reference(&dst_surface, NULL); 651} 652 653 654void 655util_blit_pixels(struct blit_state *ctx, 656 struct pipe_resource *src_tex, 657 unsigned src_level, 658 int srcX0, int srcY0, 659 int srcX1, int srcY1, 660 int srcZ, 661 struct pipe_surface *dst, 662 int dstX0, int dstY0, 663 int dstX1, int dstY1, 664 float z, uint filter ) 665{ 666 util_blit_pixels_writemask( ctx, src_tex, 667 src_level, 668 srcX0, srcY0, 669 srcX1, srcY1, 670 srcZ, 671 dst, 672 dstX0, dstY0, 673 dstX1, dstY1, 674 z, filter, 675 TGSI_WRITEMASK_XYZW ); 676} 677 678 679/** 680 * Copy pixel block from src texture to dst surface. 681 * The sampler view's first_level field indicates the source 682 * mipmap level to use. 683 * XXX need some control over blitting Z and/or stencil. 684 */ 685void 686util_blit_pixels_tex(struct blit_state *ctx, 687 struct pipe_sampler_view *src_sampler_view, 688 int srcX0, int srcY0, 689 int srcX1, int srcY1, 690 struct pipe_surface *dst, 691 int dstX0, int dstY0, 692 int dstX1, int dstY1, 693 float z, uint filter) 694{ 695 boolean normalized = src_sampler_view->texture->target != PIPE_TEXTURE_RECT; 696 struct pipe_framebuffer_state fb; 697 float s0, t0, s1, t1; 698 unsigned offset; 699 struct pipe_resource *tex = src_sampler_view->texture; 700 701 assert(filter == PIPE_TEX_MIPFILTER_NEAREST || 702 filter == PIPE_TEX_MIPFILTER_LINEAR); 703 704 assert(tex); 705 assert(tex->width0 != 0); 706 assert(tex->height0 != 0); 707 708 s0 = srcX0; 709 s1 = srcX1; 710 t0 = srcY0; 711 t1 = srcY1; 712 713 if(normalized) 714 { 715 /* normalize according to the mipmap level's size */ 716 int level = src_sampler_view->u.tex.first_level; 717 float w = (float) u_minify(tex->width0, level); 718 float h = (float) u_minify(tex->height0, level); 719 s0 /= w; 720 s1 /= w; 721 t0 /= h; 722 t1 /= h; 723 } 724 725 assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format, 726 PIPE_TEXTURE_2D, 727 dst->texture->nr_samples, 728 PIPE_BIND_RENDER_TARGET)); 729 730 /* save state (restored below) */ 731 cso_save_blend(ctx->cso); 732 cso_save_depth_stencil_alpha(ctx->cso); 733 cso_save_rasterizer(ctx->cso); 734 cso_save_samplers(ctx->cso); 735 cso_save_fragment_sampler_views(ctx->cso); 736 cso_save_stream_outputs(ctx->cso); 737 cso_save_viewport(ctx->cso); 738 cso_save_framebuffer(ctx->cso); 739 cso_save_fragment_shader(ctx->cso); 740 cso_save_vertex_shader(ctx->cso); 741 cso_save_geometry_shader(ctx->cso); 742 cso_save_vertex_elements(ctx->cso); 743 cso_save_vertex_buffers(ctx->cso); 744 745 /* set misc state we care about */ 746 cso_set_blend(ctx->cso, &ctx->blend); 747 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil_keep); 748 cso_set_rasterizer(ctx->cso, &ctx->rasterizer); 749 cso_set_vertex_elements(ctx->cso, 2, ctx->velem); 750 cso_set_stream_outputs(ctx->cso, 0, NULL, 0); 751 752 /* sampler */ 753 ctx->sampler.normalized_coords = normalized; 754 ctx->sampler.min_img_filter = filter; 755 ctx->sampler.mag_img_filter = filter; 756 cso_single_sampler(ctx->cso, 0, &ctx->sampler); 757 cso_single_sampler_done(ctx->cso); 758 759 /* viewport */ 760 ctx->viewport.scale[0] = 0.5f * dst->width; 761 ctx->viewport.scale[1] = 0.5f * dst->height; 762 ctx->viewport.scale[2] = 0.5f; 763 ctx->viewport.scale[3] = 1.0f; 764 ctx->viewport.translate[0] = 0.5f * dst->width; 765 ctx->viewport.translate[1] = 0.5f * dst->height; 766 ctx->viewport.translate[2] = 0.5f; 767 ctx->viewport.translate[3] = 0.0f; 768 cso_set_viewport(ctx->cso, &ctx->viewport); 769 770 /* texture */ 771 cso_set_fragment_sampler_views(ctx->cso, 1, &src_sampler_view); 772 773 /* shaders */ 774 set_fragment_shader(ctx, TGSI_WRITEMASK_XYZW); 775 set_vertex_shader(ctx); 776 cso_set_geometry_shader_handle(ctx->cso, NULL); 777 778 /* drawing dest */ 779 memset(&fb, 0, sizeof(fb)); 780 fb.width = dst->width; 781 fb.height = dst->height; 782 fb.nr_cbufs = 1; 783 fb.cbufs[0] = dst; 784 cso_set_framebuffer(ctx->cso, &fb); 785 786 /* draw quad */ 787 offset = setup_vertex_data_tex(ctx, 788 (float) dstX0 / dst->width * 2.0f - 1.0f, 789 (float) dstY0 / dst->height * 2.0f - 1.0f, 790 (float) dstX1 / dst->width * 2.0f - 1.0f, 791 (float) dstY1 / dst->height * 2.0f - 1.0f, 792 s0, t0, s1, t1, 793 z); 794 795 util_draw_vertex_buffer(ctx->pipe, ctx->cso, 796 ctx->vbuf, offset, 797 PIPE_PRIM_TRIANGLE_FAN, 798 4, /* verts */ 799 2); /* attribs/vert */ 800 801 /* restore state we changed */ 802 cso_restore_blend(ctx->cso); 803 cso_restore_depth_stencil_alpha(ctx->cso); 804 cso_restore_rasterizer(ctx->cso); 805 cso_restore_samplers(ctx->cso); 806 cso_restore_fragment_sampler_views(ctx->cso); 807 cso_restore_viewport(ctx->cso); 808 cso_restore_framebuffer(ctx->cso); 809 cso_restore_fragment_shader(ctx->cso); 810 cso_restore_vertex_shader(ctx->cso); 811 cso_restore_geometry_shader(ctx->cso); 812 cso_restore_vertex_elements(ctx->cso); 813 cso_restore_vertex_buffers(ctx->cso); 814 cso_restore_stream_outputs(ctx->cso); 815} 816