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