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