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