u_blit.c revision f500f3a72c6be61ff9b8e1166f734e408d00aded
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 "pipe/p_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_math.h" 46#include "util/u_memory.h" 47#include "util/u_simple_shaders.h" 48 49#include "cso_cache/cso_context.h" 50 51 52struct blit_state 53{ 54 struct pipe_context *pipe; 55 struct cso_context *cso; 56 57 struct pipe_blend_state blend; 58 struct pipe_depth_stencil_alpha_state depthstencil; 59 struct pipe_rasterizer_state rasterizer; 60 struct pipe_sampler_state sampler; 61 struct pipe_viewport_state viewport; 62 63 struct pipe_shader_state vert_shader; 64 struct pipe_shader_state frag_shader; 65 void *vs; 66 void *fs; 67 68 struct pipe_buffer *vbuf; /**< quad vertices */ 69 unsigned vbuf_slot; 70 71 float vertices[4][2][4]; /**< vertex/texcoords for quad */ 72}; 73 74 75/** 76 * Create state object for blit. 77 * Intended to be created once and re-used for many blit() calls. 78 */ 79struct blit_state * 80util_create_blit(struct pipe_context *pipe, struct cso_context *cso) 81{ 82 struct blit_state *ctx; 83 uint i; 84 85 ctx = CALLOC_STRUCT(blit_state); 86 if (!ctx) 87 return NULL; 88 89 ctx->pipe = pipe; 90 ctx->cso = cso; 91 92 /* disabled blending/masking */ 93 memset(&ctx->blend, 0, sizeof(ctx->blend)); 94 ctx->blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE; 95 ctx->blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE; 96 ctx->blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO; 97 ctx->blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO; 98 ctx->blend.colormask = PIPE_MASK_RGBA; 99 100 /* no-op depth/stencil/alpha */ 101 memset(&ctx->depthstencil, 0, sizeof(ctx->depthstencil)); 102 103 /* rasterizer */ 104 memset(&ctx->rasterizer, 0, sizeof(ctx->rasterizer)); 105 ctx->rasterizer.front_winding = PIPE_WINDING_CW; 106 ctx->rasterizer.cull_mode = PIPE_WINDING_NONE; 107 ctx->rasterizer.bypass_clipping = 1; 108 /*ctx->rasterizer.bypass_vs = 1;*/ 109 ctx->rasterizer.gl_rasterization_rules = 1; 110 111 /* samplers */ 112 memset(&ctx->sampler, 0, sizeof(ctx->sampler)); 113 ctx->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 114 ctx->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 115 ctx->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 116 ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 117 ctx->sampler.min_img_filter = 0; /* set later */ 118 ctx->sampler.mag_img_filter = 0; /* set later */ 119 ctx->sampler.normalized_coords = 1; 120 121 /* viewport (identity, we setup vertices in wincoords) */ 122 ctx->viewport.scale[0] = 1.0; 123 ctx->viewport.scale[1] = 1.0; 124 ctx->viewport.scale[2] = 1.0; 125 ctx->viewport.scale[3] = 1.0; 126 ctx->viewport.translate[0] = 0.0; 127 ctx->viewport.translate[1] = 0.0; 128 ctx->viewport.translate[2] = 0.0; 129 ctx->viewport.translate[3] = 0.0; 130 131 /* vertex shader */ 132 { 133 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION, 134 TGSI_SEMANTIC_GENERIC }; 135 const uint semantic_indexes[] = { 0, 0 }; 136 ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names, 137 semantic_indexes, 138 &ctx->vert_shader); 139 } 140 141 /* fragment shader */ 142 ctx->fs = util_make_fragment_tex_shader(pipe, &ctx->frag_shader); 143 ctx->vbuf = NULL; 144 145 /* init vertex data that doesn't change */ 146 for (i = 0; i < 4; i++) { 147 ctx->vertices[i][0][3] = 1.0f; /* w */ 148 ctx->vertices[i][1][2] = 0.0f; /* r */ 149 ctx->vertices[i][1][3] = 1.0f; /* q */ 150 } 151 152 return ctx; 153} 154 155 156/** 157 * Destroy a blit context 158 */ 159void 160util_destroy_blit(struct blit_state *ctx) 161{ 162 struct pipe_context *pipe = ctx->pipe; 163 164 pipe->delete_vs_state(pipe, ctx->vs); 165 pipe->delete_fs_state(pipe, ctx->fs); 166 167 FREE((void*) ctx->vert_shader.tokens); 168 FREE((void*) ctx->frag_shader.tokens); 169 170 pipe_buffer_reference(&ctx->vbuf, NULL); 171 172 FREE(ctx); 173} 174 175 176static unsigned get_next_slot( struct blit_state *ctx ) 177{ 178 const unsigned max_slots = 4096 / sizeof ctx->vertices; 179 180 if (ctx->vbuf_slot >= max_slots) 181 util_blit_flush( ctx ); 182 183 if (!ctx->vbuf) { 184 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen, 185 32, 186 PIPE_BUFFER_USAGE_VERTEX, 187 max_slots * sizeof ctx->vertices); 188 } 189 190 return ctx->vbuf_slot++ * sizeof ctx->vertices; 191} 192 193 194 195/** 196 * Setup vertex data for the textured quad we'll draw. 197 * Note: y=0=top 198 */ 199static unsigned 200setup_vertex_data(struct blit_state *ctx, 201 float x0, float y0, float x1, float y1, float z) 202{ 203 unsigned offset; 204 205 ctx->vertices[0][0][0] = x0; 206 ctx->vertices[0][0][1] = y0; 207 ctx->vertices[0][0][2] = z; 208 ctx->vertices[0][1][0] = 0.0f; /*s*/ 209 ctx->vertices[0][1][1] = 0.0f; /*t*/ 210 211 ctx->vertices[1][0][0] = x1; 212 ctx->vertices[1][0][1] = y0; 213 ctx->vertices[1][0][2] = z; 214 ctx->vertices[1][1][0] = 1.0f; /*s*/ 215 ctx->vertices[1][1][1] = 0.0f; /*t*/ 216 217 ctx->vertices[2][0][0] = x1; 218 ctx->vertices[2][0][1] = y1; 219 ctx->vertices[2][0][2] = z; 220 ctx->vertices[2][1][0] = 1.0f; 221 ctx->vertices[2][1][1] = 1.0f; 222 223 ctx->vertices[3][0][0] = x0; 224 ctx->vertices[3][0][1] = y1; 225 ctx->vertices[3][0][2] = z; 226 ctx->vertices[3][1][0] = 0.0f; 227 ctx->vertices[3][1][1] = 1.0f; 228 229 offset = get_next_slot( ctx ); 230 231 pipe_buffer_write(ctx->pipe->screen, ctx->vbuf, 232 offset, sizeof(ctx->vertices), ctx->vertices); 233 234 return offset; 235} 236 237 238/** 239 * Setup vertex data for the textured quad we'll draw. 240 * Note: y=0=top 241 */ 242static unsigned 243setup_vertex_data_tex(struct blit_state *ctx, 244 float x0, float y0, float x1, float y1, 245 float s0, float t0, float s1, float t1, 246 float z) 247{ 248 unsigned offset; 249 250 ctx->vertices[0][0][0] = x0; 251 ctx->vertices[0][0][1] = y0; 252 ctx->vertices[0][0][2] = z; 253 ctx->vertices[0][1][0] = s0; /*s*/ 254 ctx->vertices[0][1][1] = t0; /*t*/ 255 256 ctx->vertices[1][0][0] = x1; 257 ctx->vertices[1][0][1] = y0; 258 ctx->vertices[1][0][2] = z; 259 ctx->vertices[1][1][0] = s1; /*s*/ 260 ctx->vertices[1][1][1] = t0; /*t*/ 261 262 ctx->vertices[2][0][0] = x1; 263 ctx->vertices[2][0][1] = y1; 264 ctx->vertices[2][0][2] = z; 265 ctx->vertices[2][1][0] = s1; 266 ctx->vertices[2][1][1] = t1; 267 268 ctx->vertices[3][0][0] = x0; 269 ctx->vertices[3][0][1] = y1; 270 ctx->vertices[3][0][2] = z; 271 ctx->vertices[3][1][0] = s0; 272 ctx->vertices[3][1][1] = t1; 273 274 offset = get_next_slot( ctx ); 275 276 pipe_buffer_write(ctx->pipe->screen, ctx->vbuf, 277 offset, sizeof(ctx->vertices), ctx->vertices); 278 279 return offset; 280} 281/** 282 * Copy pixel block from src surface to dst surface. 283 * Overlapping regions are acceptable. 284 * XXX need some control over blitting Z and/or stencil. 285 */ 286void 287util_blit_pixels(struct blit_state *ctx, 288 struct pipe_surface *src, 289 int srcX0, int srcY0, 290 int srcX1, int srcY1, 291 struct pipe_surface *dst, 292 int dstX0, int dstY0, 293 int dstX1, int dstY1, 294 float z, uint filter) 295{ 296 struct pipe_context *pipe = ctx->pipe; 297 struct pipe_screen *screen = pipe->screen; 298 struct pipe_texture texTemp, *tex; 299 struct pipe_surface *texSurf; 300 struct pipe_framebuffer_state fb; 301 const int srcW = abs(srcX1 - srcX0); 302 const int srcH = abs(srcY1 - srcY0); 303 const int srcLeft = MIN2(srcX0, srcX1); 304 const int srcTop = MIN2(srcY0, srcY1); 305 unsigned offset; 306 307 assert(filter == PIPE_TEX_MIPFILTER_NEAREST || 308 filter == PIPE_TEX_MIPFILTER_LINEAR); 309 310 if (srcLeft != srcX0) { 311 /* left-right flip */ 312 int tmp = dstX0; 313 dstX0 = dstX1; 314 dstX1 = tmp; 315 } 316 317 if (srcTop != srcY0) { 318 /* up-down flip */ 319 int tmp = dstY0; 320 dstY0 = dstY1; 321 dstY1 = tmp; 322 } 323 324 assert(screen->is_format_supported(screen, src->format, PIPE_TEXTURE_2D, 325 PIPE_TEXTURE_USAGE_SAMPLER, 0)); 326 assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D, 327 PIPE_TEXTURE_USAGE_SAMPLER, 0)); 328 329 if(dst->format == src->format && (dstX1 - dstX0) == srcW && (dstY1 - dstY0) == srcH) { 330 /* FIXME: this will most surely fail for overlapping rectangles */ 331 pipe->surface_copy(pipe, 332 dst, dstX0, dstY0, /* dest */ 333 src, srcX0, srcY0, /* src */ 334 srcW, srcH); /* size */ 335 return; 336 } 337 338 assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D, 339 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)); 340 341 /* 342 * XXX for now we're always creating a temporary texture. 343 * Strictly speaking that's not always needed. 344 */ 345 346 /* create temp texture */ 347 memset(&texTemp, 0, sizeof(texTemp)); 348 texTemp.target = PIPE_TEXTURE_2D; 349 texTemp.format = src->format; 350 texTemp.last_level = 0; 351 texTemp.width[0] = srcW; 352 texTemp.height[0] = srcH; 353 texTemp.depth[0] = 1; 354 texTemp.compressed = 0; 355 pf_get_block(src->format, &texTemp.block); 356 357 tex = screen->texture_create(screen, &texTemp); 358 if (!tex) 359 return; 360 361 texSurf = screen->get_tex_surface(screen, tex, 0, 0, 0, 362 PIPE_BUFFER_USAGE_GPU_WRITE); 363 364 /* load temp texture */ 365 pipe->surface_copy(pipe, 366 texSurf, 0, 0, /* dest */ 367 src, srcLeft, srcTop, /* src */ 368 srcW, srcH); /* size */ 369 370 /* free the surface, update the texture if necessary. 371 */ 372 pipe_surface_reference(&texSurf, NULL); 373 374 /* save state (restored below) */ 375 cso_save_blend(ctx->cso); 376 cso_save_depth_stencil_alpha(ctx->cso); 377 cso_save_rasterizer(ctx->cso); 378 cso_save_samplers(ctx->cso); 379 cso_save_sampler_textures(ctx->cso); 380 cso_save_framebuffer(ctx->cso); 381 cso_save_fragment_shader(ctx->cso); 382 cso_save_vertex_shader(ctx->cso); 383 cso_save_viewport(ctx->cso); 384 385 /* set misc state we care about */ 386 cso_set_blend(ctx->cso, &ctx->blend); 387 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil); 388 cso_set_rasterizer(ctx->cso, &ctx->rasterizer); 389 cso_set_viewport(ctx->cso, &ctx->viewport); 390 391 /* sampler */ 392 ctx->sampler.min_img_filter = filter; 393 ctx->sampler.mag_img_filter = filter; 394 cso_single_sampler(ctx->cso, 0, &ctx->sampler); 395 cso_single_sampler_done(ctx->cso); 396 397 /* texture */ 398 cso_set_sampler_textures(ctx->cso, 1, &tex); 399 400 /* shaders */ 401 cso_set_fragment_shader_handle(ctx->cso, ctx->fs); 402 cso_set_vertex_shader_handle(ctx->cso, ctx->vs); 403 404 /* drawing dest */ 405 memset(&fb, 0, sizeof(fb)); 406 fb.width = dst->width; 407 fb.height = dst->height; 408 fb.nr_cbufs = 1; 409 fb.cbufs[0] = dst; 410 cso_set_framebuffer(ctx->cso, &fb); 411 412 /* draw quad */ 413 offset = setup_vertex_data(ctx, 414 (float) dstX0, (float) dstY0, 415 (float) dstX1, (float) dstY1, z); 416 417 util_draw_vertex_buffer(ctx->pipe, ctx->vbuf, offset, 418 PIPE_PRIM_TRIANGLE_FAN, 419 4, /* verts */ 420 2); /* attribs/vert */ 421 422 /* restore state we changed */ 423 cso_restore_blend(ctx->cso); 424 cso_restore_depth_stencil_alpha(ctx->cso); 425 cso_restore_rasterizer(ctx->cso); 426 cso_restore_samplers(ctx->cso); 427 cso_restore_sampler_textures(ctx->cso); 428 cso_restore_framebuffer(ctx->cso); 429 cso_restore_fragment_shader(ctx->cso); 430 cso_restore_vertex_shader(ctx->cso); 431 cso_restore_viewport(ctx->cso); 432 433 pipe_texture_reference(&tex, NULL); 434} 435 436 437/* Release vertex buffer at end of frame to avoid synchronous 438 * rendering. 439 */ 440void util_blit_flush( struct blit_state *ctx ) 441{ 442 pipe_buffer_reference(&ctx->vbuf, NULL); 443 ctx->vbuf_slot = 0; 444} 445 446 447 448/** 449 * Copy pixel block from src texture to dst surface. 450 * Overlapping regions are acceptable. 451 * 452 * XXX Should support selection of level. 453 * XXX need some control over blitting Z and/or stencil. 454 */ 455void 456util_blit_pixels_tex(struct blit_state *ctx, 457 struct pipe_texture *tex, 458 int srcX0, int srcY0, 459 int srcX1, int srcY1, 460 struct pipe_surface *dst, 461 int dstX0, int dstY0, 462 int dstX1, int dstY1, 463 float z, uint filter) 464{ 465 struct pipe_framebuffer_state fb; 466 float s0, t0, s1, t1; 467 unsigned offset; 468 469 assert(filter == PIPE_TEX_MIPFILTER_NEAREST || 470 filter == PIPE_TEX_MIPFILTER_LINEAR); 471 472 assert(tex->width[0] != 0); 473 assert(tex->height[0] != 0); 474 475 s0 = srcX0 / (float)tex->width[0]; 476 s1 = srcX1 / (float)tex->width[0]; 477 t0 = srcY0 / (float)tex->height[0]; 478 t1 = srcY1 / (float)tex->height[0]; 479 480 assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format, 481 PIPE_TEXTURE_2D, 482 PIPE_TEXTURE_USAGE_RENDER_TARGET, 483 0)); 484 485 /* save state (restored below) */ 486 cso_save_blend(ctx->cso); 487 cso_save_depth_stencil_alpha(ctx->cso); 488 cso_save_rasterizer(ctx->cso); 489 cso_save_samplers(ctx->cso); 490 cso_save_sampler_textures(ctx->cso); 491 cso_save_framebuffer(ctx->cso); 492 cso_save_fragment_shader(ctx->cso); 493 cso_save_vertex_shader(ctx->cso); 494 cso_save_viewport(ctx->cso); 495 496 /* set misc state we care about */ 497 cso_set_blend(ctx->cso, &ctx->blend); 498 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil); 499 cso_set_rasterizer(ctx->cso, &ctx->rasterizer); 500 cso_set_viewport(ctx->cso, &ctx->viewport); 501 502 /* sampler */ 503 ctx->sampler.min_img_filter = filter; 504 ctx->sampler.mag_img_filter = filter; 505 cso_single_sampler(ctx->cso, 0, &ctx->sampler); 506 cso_single_sampler_done(ctx->cso); 507 508 /* texture */ 509 cso_set_sampler_textures(ctx->cso, 1, &tex); 510 511 /* shaders */ 512 cso_set_fragment_shader_handle(ctx->cso, ctx->fs); 513 cso_set_vertex_shader_handle(ctx->cso, ctx->vs); 514 515 /* drawing dest */ 516 memset(&fb, 0, sizeof(fb)); 517 fb.width = dst->width; 518 fb.height = dst->height; 519 fb.nr_cbufs = 1; 520 fb.cbufs[0] = dst; 521 cso_set_framebuffer(ctx->cso, &fb); 522 523 /* draw quad */ 524 offset = setup_vertex_data_tex(ctx, 525 (float) dstX0, (float) dstY0, 526 (float) dstX1, (float) dstY1, 527 s0, t0, s1, t1, 528 z); 529 530 util_draw_vertex_buffer(ctx->pipe, 531 ctx->vbuf, offset, 532 PIPE_PRIM_TRIANGLE_FAN, 533 4, /* verts */ 534 2); /* attribs/vert */ 535 536 /* restore state we changed */ 537 cso_restore_blend(ctx->cso); 538 cso_restore_depth_stencil_alpha(ctx->cso); 539 cso_restore_rasterizer(ctx->cso); 540 cso_restore_samplers(ctx->cso); 541 cso_restore_sampler_textures(ctx->cso); 542 cso_restore_framebuffer(ctx->cso); 543 cso_restore_fragment_shader(ctx->cso); 544 cso_restore_vertex_shader(ctx->cso); 545 cso_restore_viewport(ctx->cso); 546} 547