Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / auxiliary / util / u_blit.c
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_format.h"
46 #include "util/u_math.h"
47 #include "util/u_memory.h"
48 #include "util/u_simple_shaders.h"
49 #include "util/u_surface.h"
50 #include "util/u_rect.h"
51
52 #include "cso_cache/cso_context.h"
53
54
55 struct blit_state
56 {
57 struct pipe_context *pipe;
58 struct cso_context *cso;
59
60 struct pipe_blend_state blend;
61 struct pipe_depth_stencil_alpha_state depthstencil;
62 struct pipe_rasterizer_state rasterizer;
63 struct pipe_sampler_state sampler;
64 struct pipe_viewport_state viewport;
65
66 void *vs;
67 void *fs[TGSI_WRITEMASK_XYZW + 1];
68
69 struct pipe_buffer *vbuf; /**< quad vertices */
70 unsigned vbuf_slot;
71
72 float vertices[4][2][4]; /**< vertex/texcoords for quad */
73 };
74
75
76 /**
77 * Create state object for blit.
78 * Intended to be created once and re-used for many blit() calls.
79 */
80 struct blit_state *
81 util_create_blit(struct pipe_context *pipe, struct cso_context *cso)
82 {
83 struct blit_state *ctx;
84 uint i;
85
86 ctx = CALLOC_STRUCT(blit_state);
87 if (!ctx)
88 return NULL;
89
90 ctx->pipe = pipe;
91 ctx->cso = cso;
92
93 /* disabled blending/masking */
94 memset(&ctx->blend, 0, sizeof(ctx->blend));
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_vs_clip_and_viewport = 1;
105 ctx->rasterizer.gl_rasterization_rules = 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
118 /* vertex shader - still required to provide the linkage between
119 * fragment shader input semantics and vertex_element/buffers.
120 */
121 {
122 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
123 TGSI_SEMANTIC_GENERIC };
124 const uint semantic_indexes[] = { 0, 0 };
125 ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
126 semantic_indexes);
127 }
128
129 /* fragment shader */
130 ctx->fs[TGSI_WRITEMASK_XYZW] =
131 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_2D);
132 ctx->vbuf = NULL;
133
134 /* init vertex data that doesn't change */
135 for (i = 0; i < 4; i++) {
136 ctx->vertices[i][0][3] = 1.0f; /* w */
137 ctx->vertices[i][1][2] = 0.0f; /* r */
138 ctx->vertices[i][1][3] = 1.0f; /* q */
139 }
140
141 return ctx;
142 }
143
144
145 /**
146 * Destroy a blit context
147 */
148 void
149 util_destroy_blit(struct blit_state *ctx)
150 {
151 struct pipe_context *pipe = ctx->pipe;
152 unsigned i;
153
154 pipe->delete_vs_state(pipe, ctx->vs);
155
156 for (i = 0; i < Elements(ctx->fs); i++)
157 if (ctx->fs[i])
158 pipe->delete_fs_state(pipe, ctx->fs[i]);
159
160 pipe_buffer_reference(&ctx->vbuf, NULL);
161
162 FREE(ctx);
163 }
164
165
166 /**
167 * Get offset of next free slot in vertex buffer for quad vertices.
168 */
169 static unsigned
170 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
191 /**
192 * Setup vertex data for the textured quad we'll draw.
193 * Note: y=0=top
194 */
195 static unsigned
196 setup_vertex_data_tex(struct blit_state *ctx,
197 float x0, float y0, float x1, float y1,
198 float s0, float t0, float s1, float t1,
199 float z)
200 {
201 unsigned offset;
202
203 ctx->vertices[0][0][0] = x0;
204 ctx->vertices[0][0][1] = y0;
205 ctx->vertices[0][0][2] = z;
206 ctx->vertices[0][1][0] = s0; /*s*/
207 ctx->vertices[0][1][1] = t0; /*t*/
208
209 ctx->vertices[1][0][0] = x1;
210 ctx->vertices[1][0][1] = y0;
211 ctx->vertices[1][0][2] = z;
212 ctx->vertices[1][1][0] = s1; /*s*/
213 ctx->vertices[1][1][1] = t0; /*t*/
214
215 ctx->vertices[2][0][0] = x1;
216 ctx->vertices[2][0][1] = y1;
217 ctx->vertices[2][0][2] = z;
218 ctx->vertices[2][1][0] = s1;
219 ctx->vertices[2][1][1] = t1;
220
221 ctx->vertices[3][0][0] = x0;
222 ctx->vertices[3][0][1] = y1;
223 ctx->vertices[3][0][2] = z;
224 ctx->vertices[3][1][0] = s0;
225 ctx->vertices[3][1][1] = t1;
226
227 offset = get_next_slot( ctx );
228
229 pipe_buffer_write_nooverlap(ctx->pipe->screen, ctx->vbuf,
230 offset, sizeof(ctx->vertices), ctx->vertices);
231
232 return offset;
233 }
234
235
236 /**
237 * \return TRUE if two regions overlap, FALSE otherwise
238 */
239 static boolean
240 regions_overlap(int srcX0, int srcY0,
241 int srcX1, int srcY1,
242 int dstX0, int dstY0,
243 int dstX1, int dstY1)
244 {
245 if (MAX2(srcX0, srcX1) < MIN2(dstX0, dstX1))
246 return FALSE; /* src completely left of dst */
247
248 if (MAX2(dstX0, dstX1) < MIN2(srcX0, srcX1))
249 return FALSE; /* dst completely left of src */
250
251 if (MAX2(srcY0, srcY1) < MIN2(dstY0, dstY1))
252 return FALSE; /* src completely above dst */
253
254 if (MAX2(dstY0, dstY1) < MIN2(srcY0, srcY1))
255 return FALSE; /* dst completely above src */
256
257 return TRUE; /* some overlap */
258 }
259
260
261 /**
262 * Copy pixel block from src surface to dst surface.
263 * Overlapping regions are acceptable.
264 * Flipping and stretching are supported.
265 * \param filter one of PIPE_TEX_MIPFILTER_NEAREST/LINEAR
266 * \param writemask controls which channels in the dest surface are sourced
267 * from the src surface. Disabled channels are sourced
268 * from (0,0,0,1).
269 * XXX what about clipping???
270 * XXX need some control over blitting Z and/or stencil.
271 */
272 void
273 util_blit_pixels_writemask(struct blit_state *ctx,
274 struct pipe_surface *src,
275 int srcX0, int srcY0,
276 int srcX1, int srcY1,
277 struct pipe_surface *dst,
278 int dstX0, int dstY0,
279 int dstX1, int dstY1,
280 float z, uint filter,
281 uint writemask)
282 {
283 struct pipe_context *pipe = ctx->pipe;
284 struct pipe_screen *screen = pipe->screen;
285 struct pipe_texture *tex = NULL;
286 struct pipe_framebuffer_state fb;
287 const int srcW = abs(srcX1 - srcX0);
288 const int srcH = abs(srcY1 - srcY0);
289 unsigned offset;
290 boolean overlap;
291 float s0, t0, s1, t1;
292
293 assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
294 filter == PIPE_TEX_MIPFILTER_LINEAR);
295
296 assert(screen->is_format_supported(screen, src->format, PIPE_TEXTURE_2D,
297 PIPE_TEXTURE_USAGE_SAMPLER, 0));
298 assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D,
299 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0));
300
301 /* do the regions overlap? */
302 overlap = util_same_surface(src, dst) &&
303 regions_overlap(srcX0, srcY0, srcX1, srcY1,
304 dstX0, dstY0, dstX1, dstY1);
305
306 /*
307 * Check for simple case: no format conversion, no flipping, no stretching,
308 * no overlapping.
309 * Filter mode should not matter since there's no stretching.
310 */
311 if (pipe->surface_copy &&
312 dst->format == src->format &&
313 srcX0 < srcX1 &&
314 dstX0 < dstX1 &&
315 srcY0 < srcY1 &&
316 dstY0 < dstY1 &&
317 (dstX1 - dstX0) == (srcX1 - srcX0) &&
318 (dstY1 - dstY0) == (srcY1 - srcY0) &&
319 !overlap) {
320 pipe->surface_copy(pipe,
321 dst, dstX0, dstY0, /* dest */
322 src, srcX0, srcY0, /* src */
323 srcW, srcH); /* size */
324 return;
325 }
326
327 assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D,
328 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0));
329
330 /* Create a temporary texture when src and dest alias or when src
331 * is anything other than a single-level 2d texture.
332 *
333 * This can still be improved upon.
334 */
335 if (util_same_surface(src, dst) ||
336 src->texture->target != PIPE_TEXTURE_2D ||
337 src->texture->last_level != 0)
338 {
339 struct pipe_texture texTemp;
340 struct pipe_surface *texSurf;
341 const int srcLeft = MIN2(srcX0, srcX1);
342 const int srcTop = MIN2(srcY0, srcY1);
343
344 if (srcLeft != srcX0) {
345 /* left-right flip */
346 int tmp = dstX0;
347 dstX0 = dstX1;
348 dstX1 = tmp;
349 }
350
351 if (srcTop != srcY0) {
352 /* up-down flip */
353 int tmp = dstY0;
354 dstY0 = dstY1;
355 dstY1 = tmp;
356 }
357
358 /* create temp texture */
359 memset(&texTemp, 0, sizeof(texTemp));
360 texTemp.target = PIPE_TEXTURE_2D;
361 texTemp.format = src->format;
362 texTemp.last_level = 0;
363 texTemp.width0 = srcW;
364 texTemp.height0 = srcH;
365 texTemp.depth0 = 1;
366
367 tex = screen->texture_create(screen, &texTemp);
368 if (!tex)
369 return;
370
371 texSurf = screen->get_tex_surface(screen, tex, 0, 0, 0,
372 PIPE_BUFFER_USAGE_GPU_WRITE);
373
374 /* load temp texture */
375 if (pipe->surface_copy) {
376 pipe->surface_copy(pipe,
377 texSurf, 0, 0, /* dest */
378 src, srcLeft, srcTop, /* src */
379 srcW, srcH); /* size */
380 } else {
381 util_surface_copy(pipe, FALSE,
382 texSurf, 0, 0, /* dest */
383 src, srcLeft, srcTop, /* src */
384 srcW, srcH); /* size */
385 }
386
387 /* free the surface, update the texture if necessary.
388 */
389 pipe_surface_reference(&texSurf, NULL);
390 s0 = 0.0f;
391 s1 = 1.0f;
392 t0 = 0.0f;
393 t1 = 1.0f;
394 }
395 else {
396 pipe_texture_reference(&tex, src->texture);
397 s0 = srcX0 / (float)tex->width0;
398 s1 = srcX1 / (float)tex->width0;
399 t0 = srcY0 / (float)tex->height0;
400 t1 = srcY1 / (float)tex->height0;
401 }
402
403
404 /* save state (restored below) */
405 cso_save_blend(ctx->cso);
406 cso_save_depth_stencil_alpha(ctx->cso);
407 cso_save_rasterizer(ctx->cso);
408 cso_save_samplers(ctx->cso);
409 cso_save_sampler_textures(ctx->cso);
410 cso_save_framebuffer(ctx->cso);
411 cso_save_fragment_shader(ctx->cso);
412 cso_save_vertex_shader(ctx->cso);
413
414 /* set misc state we care about */
415 cso_set_blend(ctx->cso, &ctx->blend);
416 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
417 cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
418
419 /* sampler */
420 ctx->sampler.min_img_filter = filter;
421 ctx->sampler.mag_img_filter = filter;
422 cso_single_sampler(ctx->cso, 0, &ctx->sampler);
423 cso_single_sampler_done(ctx->cso);
424
425 /* texture */
426 cso_set_sampler_textures(ctx->cso, 1, &tex);
427
428 if (ctx->fs[writemask] == NULL)
429 ctx->fs[writemask] =
430 util_make_fragment_tex_shader_writemask(pipe, TGSI_TEXTURE_2D,
431 writemask);
432
433 /* shaders */
434 cso_set_fragment_shader_handle(ctx->cso, ctx->fs[writemask]);
435 cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
436
437 /* drawing dest */
438 memset(&fb, 0, sizeof(fb));
439 fb.width = dst->width;
440 fb.height = dst->height;
441 fb.nr_cbufs = 1;
442 fb.cbufs[0] = dst;
443 cso_set_framebuffer(ctx->cso, &fb);
444
445 /* draw quad */
446 offset = setup_vertex_data_tex(ctx,
447 (float) dstX0, (float) dstY0,
448 (float) dstX1, (float) dstY1,
449 s0, t0,
450 s1, t1,
451 z);
452
453 util_draw_vertex_buffer(ctx->pipe, ctx->vbuf, offset,
454 PIPE_PRIM_TRIANGLE_FAN,
455 4, /* verts */
456 2); /* attribs/vert */
457
458 /* restore state we changed */
459 cso_restore_blend(ctx->cso);
460 cso_restore_depth_stencil_alpha(ctx->cso);
461 cso_restore_rasterizer(ctx->cso);
462 cso_restore_samplers(ctx->cso);
463 cso_restore_sampler_textures(ctx->cso);
464 cso_restore_framebuffer(ctx->cso);
465 cso_restore_fragment_shader(ctx->cso);
466 cso_restore_vertex_shader(ctx->cso);
467
468 pipe_texture_reference(&tex, NULL);
469 }
470
471
472 void
473 util_blit_pixels(struct blit_state *ctx,
474 struct pipe_surface *src,
475 int srcX0, int srcY0,
476 int srcX1, int srcY1,
477 struct pipe_surface *dst,
478 int dstX0, int dstY0,
479 int dstX1, int dstY1,
480 float z, uint filter )
481 {
482 util_blit_pixels_writemask( ctx, src,
483 srcX0, srcY0,
484 srcX1, srcY1,
485 dst,
486 dstX0, dstY0,
487 dstX1, dstY1,
488 z, filter,
489 TGSI_WRITEMASK_XYZW );
490 }
491
492
493 /* Release vertex buffer at end of frame to avoid synchronous
494 * rendering.
495 */
496 void util_blit_flush( struct blit_state *ctx )
497 {
498 pipe_buffer_reference(&ctx->vbuf, NULL);
499 ctx->vbuf_slot = 0;
500 }
501
502
503
504 /**
505 * Copy pixel block from src texture to dst surface.
506 * Overlapping regions are acceptable.
507 *
508 * XXX Should support selection of level.
509 * XXX need some control over blitting Z and/or stencil.
510 */
511 void
512 util_blit_pixels_tex(struct blit_state *ctx,
513 struct pipe_texture *tex,
514 int srcX0, int srcY0,
515 int srcX1, int srcY1,
516 struct pipe_surface *dst,
517 int dstX0, int dstY0,
518 int dstX1, int dstY1,
519 float z, uint filter)
520 {
521 struct pipe_framebuffer_state fb;
522 float s0, t0, s1, t1;
523 unsigned offset;
524
525 assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
526 filter == PIPE_TEX_MIPFILTER_LINEAR);
527
528 assert(tex->width0 != 0);
529 assert(tex->height0 != 0);
530
531 s0 = srcX0 / (float)tex->width0;
532 s1 = srcX1 / (float)tex->width0;
533 t0 = srcY0 / (float)tex->height0;
534 t1 = srcY1 / (float)tex->height0;
535
536 assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format,
537 PIPE_TEXTURE_2D,
538 PIPE_TEXTURE_USAGE_RENDER_TARGET,
539 0));
540
541 /* save state (restored below) */
542 cso_save_blend(ctx->cso);
543 cso_save_depth_stencil_alpha(ctx->cso);
544 cso_save_rasterizer(ctx->cso);
545 cso_save_samplers(ctx->cso);
546 cso_save_sampler_textures(ctx->cso);
547 cso_save_framebuffer(ctx->cso);
548 cso_save_fragment_shader(ctx->cso);
549 cso_save_vertex_shader(ctx->cso);
550
551 /* set misc state we care about */
552 cso_set_blend(ctx->cso, &ctx->blend);
553 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
554 cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
555
556 /* sampler */
557 ctx->sampler.min_img_filter = filter;
558 ctx->sampler.mag_img_filter = filter;
559 cso_single_sampler(ctx->cso, 0, &ctx->sampler);
560 cso_single_sampler_done(ctx->cso);
561
562 /* texture */
563 cso_set_sampler_textures(ctx->cso, 1, &tex);
564
565 /* shaders */
566 cso_set_fragment_shader_handle(ctx->cso, ctx->fs[TGSI_WRITEMASK_XYZW]);
567 cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
568
569 /* drawing dest */
570 memset(&fb, 0, sizeof(fb));
571 fb.width = dst->width;
572 fb.height = dst->height;
573 fb.nr_cbufs = 1;
574 fb.cbufs[0] = dst;
575 cso_set_framebuffer(ctx->cso, &fb);
576
577 /* draw quad */
578 offset = setup_vertex_data_tex(ctx,
579 (float) dstX0, (float) dstY0,
580 (float) dstX1, (float) dstY1,
581 s0, t0, s1, t1,
582 z);
583
584 util_draw_vertex_buffer(ctx->pipe,
585 ctx->vbuf, offset,
586 PIPE_PRIM_TRIANGLE_FAN,
587 4, /* verts */
588 2); /* attribs/vert */
589
590 /* restore state we changed */
591 cso_restore_blend(ctx->cso);
592 cso_restore_depth_stencil_alpha(ctx->cso);
593 cso_restore_rasterizer(ctx->cso);
594 cso_restore_samplers(ctx->cso);
595 cso_restore_sampler_textures(ctx->cso);
596 cso_restore_framebuffer(ctx->cso);
597 cso_restore_fragment_shader(ctx->cso);
598 cso_restore_vertex_shader(ctx->cso);
599 }