u_blit.c revision f68ab0398bde5be6a6d4212bea23c9bbaa75d464
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 /* Make a temporary texture which contains a copy of the source pixels. 448 * Then we'll sample from the temporary texture. 449 */ 450 struct pipe_resource texTemp; 451 struct pipe_resource *tex; 452 struct pipe_sampler_view sv_templ; 453 struct pipe_box src_box; 454 const int srcLeft = MIN2(srcX0, srcX1); 455 const int srcTop = MIN2(srcY0, srcY1); 456 457 if (srcLeft != srcX0) { 458 /* left-right flip */ 459 int tmp = dstX0; 460 dstX0 = dstX1; 461 dstX1 = tmp; 462 } 463 464 if (srcTop != srcY0) { 465 /* up-down flip */ 466 int tmp = dstY0; 467 dstY0 = dstY1; 468 dstY1 = tmp; 469 } 470 471 /* create temp texture */ 472 memset(&texTemp, 0, sizeof(texTemp)); 473 texTemp.target = ctx->internal_target; 474 texTemp.format = src_format; 475 texTemp.last_level = 0; 476 texTemp.width0 = srcW; 477 texTemp.height0 = srcH; 478 texTemp.depth0 = 1; 479 texTemp.array_size = 1; 480 texTemp.bind = PIPE_BIND_SAMPLER_VIEW; 481 482 tex = screen->resource_create(screen, &texTemp); 483 if (!tex) 484 return; 485 486 src_box.x = srcLeft; 487 src_box.y = srcTop; 488 src_box.z = srcZ0; 489 src_box.width = srcW; 490 src_box.height = srcH; 491 src_box.depth = 1; 492 /* load temp texture */ 493 pipe->resource_copy_region(pipe, 494 tex, 0, 0, 0, 0, /* dest */ 495 src_tex, src_level, &src_box); 496 497 normalized = tex->target != PIPE_TEXTURE_RECT; 498 if(normalized) { 499 s0 = 0.0f; 500 s1 = 1.0f; 501 t0 = 0.0f; 502 t1 = 1.0f; 503 } 504 else { 505 s0 = 0; 506 s1 = srcW; 507 t0 = 0; 508 t1 = srcH; 509 } 510 511 u_sampler_view_default_template(&sv_templ, tex, tex->format); 512 sampler_view = pipe->create_sampler_view(pipe, tex, &sv_templ); 513 514 if (!sampler_view) { 515 pipe_resource_reference(&tex, NULL); 516 return; 517 } 518 pipe_resource_reference(&tex, NULL); 519 } 520 else { 521 /* Directly sample from the source resource/texture */ 522 u_sampler_view_default_template(&sv_templ, src_tex, src_format); 523 sampler_view = pipe->create_sampler_view(pipe, src_tex, &sv_templ); 524 525 if (!sampler_view) { 526 return; 527 } 528 529 s0 = srcX0; 530 s1 = srcX1; 531 t0 = srcY0; 532 t1 = srcY1; 533 normalized = sampler_view->texture->target != PIPE_TEXTURE_RECT; 534 if(normalized) 535 { 536 s0 /= (float)(u_minify(sampler_view->texture->width0, src_level)); 537 s1 /= (float)(u_minify(sampler_view->texture->width0, src_level)); 538 t0 /= (float)(u_minify(sampler_view->texture->height0, src_level)); 539 t1 /= (float)(u_minify(sampler_view->texture->height0, src_level)); 540 } 541 } 542 543 dst_is_depth = util_format_is_depth_or_stencil(dst_format); 544 545 assert(screen->is_format_supported(screen, sampler_view->format, ctx->internal_target, 546 sampler_view->texture->nr_samples, 547 PIPE_BIND_SAMPLER_VIEW)); 548 assert(screen->is_format_supported(screen, dst_format, ctx->internal_target, 549 dst_surface->texture->nr_samples, 550 dst_is_depth ? PIPE_BIND_DEPTH_STENCIL : 551 PIPE_BIND_RENDER_TARGET)); 552 /* save state (restored below) */ 553 cso_save_blend(ctx->cso); 554 cso_save_depth_stencil_alpha(ctx->cso); 555 cso_save_rasterizer(ctx->cso); 556 cso_save_samplers(ctx->cso); 557 cso_save_fragment_sampler_views(ctx->cso); 558 cso_save_stream_outputs(ctx->cso); 559 cso_save_viewport(ctx->cso); 560 cso_save_framebuffer(ctx->cso); 561 cso_save_fragment_shader(ctx->cso); 562 cso_save_vertex_shader(ctx->cso); 563 cso_save_geometry_shader(ctx->cso); 564 cso_save_vertex_elements(ctx->cso); 565 cso_save_vertex_buffers(ctx->cso); 566 567 /* set misc state we care about */ 568 cso_set_blend(ctx->cso, &ctx->blend); 569 cso_set_depth_stencil_alpha(ctx->cso, 570 dst_is_depth ? &ctx->depthstencil_write : 571 &ctx->depthstencil_keep); 572 cso_set_rasterizer(ctx->cso, &ctx->rasterizer); 573 cso_set_vertex_elements(ctx->cso, 2, ctx->velem); 574 cso_set_stream_outputs(ctx->cso, 0, NULL, 0); 575 576 /* sampler */ 577 ctx->sampler.normalized_coords = normalized; 578 ctx->sampler.min_img_filter = filter; 579 ctx->sampler.mag_img_filter = filter; 580 ctx->sampler.min_lod = src_level; 581 ctx->sampler.max_lod = src_level; 582 cso_single_sampler(ctx->cso, 0, &ctx->sampler); 583 cso_single_sampler_done(ctx->cso); 584 585 /* viewport */ 586 ctx->viewport.scale[0] = 0.5f * dst_surface->width; 587 ctx->viewport.scale[1] = 0.5f * dst_surface->height; 588 ctx->viewport.scale[2] = 0.5f; 589 ctx->viewport.scale[3] = 1.0f; 590 ctx->viewport.translate[0] = 0.5f * dst_surface->width; 591 ctx->viewport.translate[1] = 0.5f * dst_surface->height; 592 ctx->viewport.translate[2] = 0.5f; 593 ctx->viewport.translate[3] = 0.0f; 594 cso_set_viewport(ctx->cso, &ctx->viewport); 595 596 /* texture */ 597 cso_set_fragment_sampler_views(ctx->cso, 1, &sampler_view); 598 599 /* shaders */ 600 if (dst_is_depth) { 601 set_depth_fragment_shader(ctx); 602 } else { 603 set_fragment_shader(ctx, writemask); 604 } 605 set_vertex_shader(ctx); 606 cso_set_geometry_shader_handle(ctx->cso, NULL); 607 608 /* drawing dest */ 609 memset(&fb, 0, sizeof(fb)); 610 fb.width = dst_surface->width; 611 fb.height = dst_surface->height; 612 if (dst_is_depth) { 613 fb.zsbuf = dst_surface; 614 } else { 615 fb.nr_cbufs = 1; 616 fb.cbufs[0] = dst_surface; 617 } 618 cso_set_framebuffer(ctx->cso, &fb); 619 620 /* draw quad */ 621 offset = setup_vertex_data_tex(ctx, 622 (float) dstX0 / dst_surface->width * 2.0f - 1.0f, 623 (float) dstY0 / dst_surface->height * 2.0f - 1.0f, 624 (float) dstX1 / dst_surface->width * 2.0f - 1.0f, 625 (float) dstY1 / dst_surface->height * 2.0f - 1.0f, 626 s0, t0, 627 s1, t1, 628 z); 629 630 if (ctx->vbuf) { 631 util_draw_vertex_buffer(ctx->pipe, ctx->cso, ctx->vbuf, offset, 632 PIPE_PRIM_TRIANGLE_FAN, 633 4, /* verts */ 634 2); /* attribs/vert */ 635 } 636 637 /* restore state we changed */ 638 cso_restore_blend(ctx->cso); 639 cso_restore_depth_stencil_alpha(ctx->cso); 640 cso_restore_rasterizer(ctx->cso); 641 cso_restore_samplers(ctx->cso); 642 cso_restore_fragment_sampler_views(ctx->cso); 643 cso_restore_viewport(ctx->cso); 644 cso_restore_framebuffer(ctx->cso); 645 cso_restore_fragment_shader(ctx->cso); 646 cso_restore_vertex_shader(ctx->cso); 647 cso_restore_geometry_shader(ctx->cso); 648 cso_restore_vertex_elements(ctx->cso); 649 cso_restore_vertex_buffers(ctx->cso); 650 cso_restore_stream_outputs(ctx->cso); 651 652 pipe_sampler_view_reference(&sampler_view, NULL); 653 if (dst_surface != dst) 654 pipe_surface_reference(&dst_surface, NULL); 655} 656 657 658void 659util_blit_pixels(struct blit_state *ctx, 660 struct pipe_resource *src_tex, 661 unsigned src_level, 662 int srcX0, int srcY0, 663 int srcX1, int srcY1, 664 int srcZ, 665 struct pipe_surface *dst, 666 int dstX0, int dstY0, 667 int dstX1, int dstY1, 668 float z, uint filter ) 669{ 670 util_blit_pixels_writemask( ctx, src_tex, 671 src_level, 672 srcX0, srcY0, 673 srcX1, srcY1, 674 srcZ, 675 dst, 676 dstX0, dstY0, 677 dstX1, dstY1, 678 z, filter, 679 TGSI_WRITEMASK_XYZW ); 680} 681 682 683/** 684 * Copy pixel block from src texture to dst surface. 685 * The sampler view's first_level field indicates the source 686 * mipmap level to use. 687 * XXX need some control over blitting Z and/or stencil. 688 */ 689void 690util_blit_pixels_tex(struct blit_state *ctx, 691 struct pipe_sampler_view *src_sampler_view, 692 int srcX0, int srcY0, 693 int srcX1, int srcY1, 694 struct pipe_surface *dst, 695 int dstX0, int dstY0, 696 int dstX1, int dstY1, 697 float z, uint filter) 698{ 699 boolean normalized = src_sampler_view->texture->target != PIPE_TEXTURE_RECT; 700 struct pipe_framebuffer_state fb; 701 float s0, t0, s1, t1; 702 unsigned offset; 703 struct pipe_resource *tex = src_sampler_view->texture; 704 705 assert(filter == PIPE_TEX_MIPFILTER_NEAREST || 706 filter == PIPE_TEX_MIPFILTER_LINEAR); 707 708 assert(tex); 709 assert(tex->width0 != 0); 710 assert(tex->height0 != 0); 711 712 s0 = srcX0; 713 s1 = srcX1; 714 t0 = srcY0; 715 t1 = srcY1; 716 717 if(normalized) 718 { 719 /* normalize according to the mipmap level's size */ 720 int level = src_sampler_view->u.tex.first_level; 721 float w = (float) u_minify(tex->width0, level); 722 float h = (float) u_minify(tex->height0, level); 723 s0 /= w; 724 s1 /= w; 725 t0 /= h; 726 t1 /= h; 727 } 728 729 assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format, 730 PIPE_TEXTURE_2D, 731 dst->texture->nr_samples, 732 PIPE_BIND_RENDER_TARGET)); 733 734 /* save state (restored below) */ 735 cso_save_blend(ctx->cso); 736 cso_save_depth_stencil_alpha(ctx->cso); 737 cso_save_rasterizer(ctx->cso); 738 cso_save_samplers(ctx->cso); 739 cso_save_fragment_sampler_views(ctx->cso); 740 cso_save_stream_outputs(ctx->cso); 741 cso_save_viewport(ctx->cso); 742 cso_save_framebuffer(ctx->cso); 743 cso_save_fragment_shader(ctx->cso); 744 cso_save_vertex_shader(ctx->cso); 745 cso_save_geometry_shader(ctx->cso); 746 cso_save_vertex_elements(ctx->cso); 747 cso_save_vertex_buffers(ctx->cso); 748 749 /* set misc state we care about */ 750 cso_set_blend(ctx->cso, &ctx->blend); 751 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil_keep); 752 cso_set_rasterizer(ctx->cso, &ctx->rasterizer); 753 cso_set_vertex_elements(ctx->cso, 2, ctx->velem); 754 cso_set_stream_outputs(ctx->cso, 0, NULL, 0); 755 756 /* sampler */ 757 ctx->sampler.normalized_coords = normalized; 758 ctx->sampler.min_img_filter = filter; 759 ctx->sampler.mag_img_filter = filter; 760 cso_single_sampler(ctx->cso, 0, &ctx->sampler); 761 cso_single_sampler_done(ctx->cso); 762 763 /* viewport */ 764 ctx->viewport.scale[0] = 0.5f * dst->width; 765 ctx->viewport.scale[1] = 0.5f * dst->height; 766 ctx->viewport.scale[2] = 0.5f; 767 ctx->viewport.scale[3] = 1.0f; 768 ctx->viewport.translate[0] = 0.5f * dst->width; 769 ctx->viewport.translate[1] = 0.5f * dst->height; 770 ctx->viewport.translate[2] = 0.5f; 771 ctx->viewport.translate[3] = 0.0f; 772 cso_set_viewport(ctx->cso, &ctx->viewport); 773 774 /* texture */ 775 cso_set_fragment_sampler_views(ctx->cso, 1, &src_sampler_view); 776 777 /* shaders */ 778 set_fragment_shader(ctx, TGSI_WRITEMASK_XYZW); 779 set_vertex_shader(ctx); 780 cso_set_geometry_shader_handle(ctx->cso, NULL); 781 782 /* drawing dest */ 783 memset(&fb, 0, sizeof(fb)); 784 fb.width = dst->width; 785 fb.height = dst->height; 786 fb.nr_cbufs = 1; 787 fb.cbufs[0] = dst; 788 cso_set_framebuffer(ctx->cso, &fb); 789 790 /* draw quad */ 791 offset = setup_vertex_data_tex(ctx, 792 (float) dstX0 / dst->width * 2.0f - 1.0f, 793 (float) dstY0 / dst->height * 2.0f - 1.0f, 794 (float) dstX1 / dst->width * 2.0f - 1.0f, 795 (float) dstY1 / dst->height * 2.0f - 1.0f, 796 s0, t0, s1, t1, 797 z); 798 799 util_draw_vertex_buffer(ctx->pipe, ctx->cso, 800 ctx->vbuf, offset, 801 PIPE_PRIM_TRIANGLE_FAN, 802 4, /* verts */ 803 2); /* attribs/vert */ 804 805 /* restore state we changed */ 806 cso_restore_blend(ctx->cso); 807 cso_restore_depth_stencil_alpha(ctx->cso); 808 cso_restore_rasterizer(ctx->cso); 809 cso_restore_samplers(ctx->cso); 810 cso_restore_fragment_sampler_views(ctx->cso); 811 cso_restore_viewport(ctx->cso); 812 cso_restore_framebuffer(ctx->cso); 813 cso_restore_fragment_shader(ctx->cso); 814 cso_restore_vertex_shader(ctx->cso); 815 cso_restore_geometry_shader(ctx->cso); 816 cso_restore_vertex_elements(ctx->cso); 817 cso_restore_vertex_buffers(ctx->cso); 818 cso_restore_stream_outputs(ctx->cso); 819} 820