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