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