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