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(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 * XXX what about clipping???
266 * XXX need some control over blitting Z and/or stencil.
267 */
268 void
269 util_blit_pixels_writemask(struct blit_state *ctx,
270 struct pipe_surface *src,
271 int srcX0, int srcY0,
272 int srcX1, int srcY1,
273 struct pipe_surface *dst,
274 int dstX0, int dstY0,
275 int dstX1, int dstY1,
276 float z, uint filter,
277 uint writemask)
278 {
279 struct pipe_context *pipe = ctx->pipe;
280 struct pipe_screen *screen = pipe->screen;
281 struct pipe_texture *tex = NULL;
282 struct pipe_framebuffer_state fb;
283 const int srcW = abs(srcX1 - srcX0);
284 const int srcH = abs(srcY1 - srcY0);
285 unsigned offset;
286 boolean overlap;
287 float s0, t0, s1, t1;
288
289 assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
290 filter == PIPE_TEX_MIPFILTER_LINEAR);
291
292 assert(screen->is_format_supported(screen, src->format, PIPE_TEXTURE_2D,
293 PIPE_TEXTURE_USAGE_SAMPLER, 0));
294 assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D,
295 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0));
296
297 /* do the regions overlap? */
298 overlap = util_same_surface(src, dst) &&
299 regions_overlap(srcX0, srcY0, srcX1, srcY1,
300 dstX0, dstY0, dstX1, dstY1);
301
302 /*
303 * Check for simple case: no format conversion, no flipping, no stretching,
304 * no overlapping.
305 * Filter mode should not matter since there's no stretching.
306 */
307 if (pipe->surface_copy &&
308 dst->format == src->format &&
309 srcX0 < srcX1 &&
310 dstX0 < dstX1 &&
311 srcY0 < srcY1 &&
312 dstY0 < dstY1 &&
313 (dstX1 - dstX0) == (srcX1 - srcX0) &&
314 (dstY1 - dstY0) == (srcY1 - srcY0) &&
315 !overlap) {
316 pipe->surface_copy(pipe,
317 dst, dstX0, dstY0, /* dest */
318 src, srcX0, srcY0, /* src */
319 srcW, srcH); /* size */
320 return;
321 }
322
323 assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D,
324 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0));
325
326 /* Create a temporary texture when src and dest alias or when src
327 * is anything other than a single-level 2d texture.
328 *
329 * This can still be improved upon.
330 */
331 if (util_same_surface(src, dst) ||
332 src->texture->target != PIPE_TEXTURE_2D ||
333 src->texture->last_level != 0)
334 {
335 struct pipe_texture texTemp;
336 struct pipe_surface *texSurf;
337 const int srcLeft = MIN2(srcX0, srcX1);
338 const int srcTop = MIN2(srcY0, srcY1);
339
340 if (srcLeft != srcX0) {
341 /* left-right flip */
342 int tmp = dstX0;
343 dstX0 = dstX1;
344 dstX1 = tmp;
345 }
346
347 if (srcTop != srcY0) {
348 /* up-down flip */
349 int tmp = dstY0;
350 dstY0 = dstY1;
351 dstY1 = tmp;
352 }
353
354 /* create temp texture */
355 memset(&texTemp, 0, sizeof(texTemp));
356 texTemp.target = PIPE_TEXTURE_2D;
357 texTemp.format = src->format;
358 texTemp.last_level = 0;
359 texTemp.width0 = srcW;
360 texTemp.height0 = srcH;
361 texTemp.depth0 = 1;
362
363 tex = screen->texture_create(screen, &texTemp);
364 if (!tex)
365 return;
366
367 texSurf = screen->get_tex_surface(screen, tex, 0, 0, 0,
368 PIPE_BUFFER_USAGE_GPU_WRITE);
369
370 /* load temp texture */
371 if (pipe->surface_copy) {
372 pipe->surface_copy(pipe,
373 texSurf, 0, 0, /* dest */
374 src, srcLeft, srcTop, /* src */
375 srcW, srcH); /* size */
376 } else {
377 util_surface_copy(pipe, FALSE,
378 texSurf, 0, 0, /* dest */
379 src, srcLeft, srcTop, /* src */
380 srcW, srcH); /* size */
381 }
382
383 /* free the surface, update the texture if necessary.
384 */
385 pipe_surface_reference(&texSurf, NULL);
386 s0 = 0.0f;
387 s1 = 1.0f;
388 t0 = 0.0f;
389 t1 = 1.0f;
390 }
391 else {
392 pipe_texture_reference(&tex, src->texture);
393 s0 = srcX0 / (float)tex->width0;
394 s1 = srcX1 / (float)tex->width0;
395 t0 = srcY0 / (float)tex->height0;
396 t1 = srcY1 / (float)tex->height0;
397 }
398
399
400 /* save state (restored below) */
401 cso_save_blend(ctx->cso);
402 cso_save_depth_stencil_alpha(ctx->cso);
403 cso_save_rasterizer(ctx->cso);
404 cso_save_samplers(ctx->cso);
405 cso_save_sampler_textures(ctx->cso);
406 cso_save_framebuffer(ctx->cso);
407 cso_save_fragment_shader(ctx->cso);
408 cso_save_vertex_shader(ctx->cso);
409
410 /* set misc state we care about */
411 cso_set_blend(ctx->cso, &ctx->blend);
412 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
413 cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
414
415 /* sampler */
416 ctx->sampler.min_img_filter = filter;
417 ctx->sampler.mag_img_filter = filter;
418 cso_single_sampler(ctx->cso, 0, &ctx->sampler);
419 cso_single_sampler_done(ctx->cso);
420
421 /* texture */
422 cso_set_sampler_textures(ctx->cso, 1, &tex);
423
424 if (ctx->fs[writemask] == NULL)
425 ctx->fs[writemask] =
426 util_make_fragment_tex_shader_writemask(pipe, TGSI_TEXTURE_2D,
427 writemask);
428
429 /* shaders */
430 cso_set_fragment_shader_handle(ctx->cso, ctx->fs[writemask]);
431 cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
432
433 /* drawing dest */
434 memset(&fb, 0, sizeof(fb));
435 fb.width = dst->width;
436 fb.height = dst->height;
437 fb.nr_cbufs = 1;
438 fb.cbufs[0] = dst;
439 cso_set_framebuffer(ctx->cso, &fb);
440
441 /* draw quad */
442 offset = setup_vertex_data_tex(ctx,
443 (float) dstX0, (float) dstY0,
444 (float) dstX1, (float) dstY1,
445 s0, t0,
446 s1, t1,
447 z);
448
449 util_draw_vertex_buffer(ctx->pipe, ctx->vbuf, offset,
450 PIPE_PRIM_TRIANGLE_FAN,
451 4, /* verts */
452 2); /* attribs/vert */
453
454 /* restore state we changed */
455 cso_restore_blend(ctx->cso);
456 cso_restore_depth_stencil_alpha(ctx->cso);
457 cso_restore_rasterizer(ctx->cso);
458 cso_restore_samplers(ctx->cso);
459 cso_restore_sampler_textures(ctx->cso);
460 cso_restore_framebuffer(ctx->cso);
461 cso_restore_fragment_shader(ctx->cso);
462 cso_restore_vertex_shader(ctx->cso);
463
464 pipe_texture_reference(&tex, NULL);
465 }
466
467
468 void
469 util_blit_pixels(struct blit_state *ctx,
470 struct pipe_surface *src,
471 int srcX0, int srcY0,
472 int srcX1, int srcY1,
473 struct pipe_surface *dst,
474 int dstX0, int dstY0,
475 int dstX1, int dstY1,
476 float z, uint filter )
477 {
478 util_blit_pixels_writemask( ctx, src,
479 srcX0, srcY0,
480 srcX1, srcY1,
481 dst,
482 dstX0, dstY0,
483 dstX1, dstY1,
484 z, filter,
485 TGSI_WRITEMASK_XYZW );
486 }
487
488
489 /* Release vertex buffer at end of frame to avoid synchronous
490 * rendering.
491 */
492 void util_blit_flush( struct blit_state *ctx )
493 {
494 pipe_buffer_reference(&ctx->vbuf, NULL);
495 ctx->vbuf_slot = 0;
496 }
497
498
499
500 /**
501 * Copy pixel block from src texture to dst surface.
502 * Overlapping regions are acceptable.
503 *
504 * XXX Should support selection of level.
505 * XXX need some control over blitting Z and/or stencil.
506 */
507 void
508 util_blit_pixels_tex(struct blit_state *ctx,
509 struct pipe_texture *tex,
510 int srcX0, int srcY0,
511 int srcX1, int srcY1,
512 struct pipe_surface *dst,
513 int dstX0, int dstY0,
514 int dstX1, int dstY1,
515 float z, uint filter)
516 {
517 struct pipe_framebuffer_state fb;
518 float s0, t0, s1, t1;
519 unsigned offset;
520
521 assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
522 filter == PIPE_TEX_MIPFILTER_LINEAR);
523
524 assert(tex->width0 != 0);
525 assert(tex->height0 != 0);
526
527 s0 = srcX0 / (float)tex->width0;
528 s1 = srcX1 / (float)tex->width0;
529 t0 = srcY0 / (float)tex->height0;
530 t1 = srcY1 / (float)tex->height0;
531
532 assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format,
533 PIPE_TEXTURE_2D,
534 PIPE_TEXTURE_USAGE_RENDER_TARGET,
535 0));
536
537 /* save state (restored below) */
538 cso_save_blend(ctx->cso);
539 cso_save_depth_stencil_alpha(ctx->cso);
540 cso_save_rasterizer(ctx->cso);
541 cso_save_samplers(ctx->cso);
542 cso_save_sampler_textures(ctx->cso);
543 cso_save_framebuffer(ctx->cso);
544 cso_save_fragment_shader(ctx->cso);
545 cso_save_vertex_shader(ctx->cso);
546
547 /* set misc state we care about */
548 cso_set_blend(ctx->cso, &ctx->blend);
549 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
550 cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
551
552 /* sampler */
553 ctx->sampler.min_img_filter = filter;
554 ctx->sampler.mag_img_filter = filter;
555 cso_single_sampler(ctx->cso, 0, &ctx->sampler);
556 cso_single_sampler_done(ctx->cso);
557
558 /* texture */
559 cso_set_sampler_textures(ctx->cso, 1, &tex);
560
561 /* shaders */
562 cso_set_fragment_shader_handle(ctx->cso, ctx->fs[TGSI_WRITEMASK_XYZW]);
563 cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
564
565 /* drawing dest */
566 memset(&fb, 0, sizeof(fb));
567 fb.width = dst->width;
568 fb.height = dst->height;
569 fb.nr_cbufs = 1;
570 fb.cbufs[0] = dst;
571 cso_set_framebuffer(ctx->cso, &fb);
572
573 /* draw quad */
574 offset = setup_vertex_data_tex(ctx,
575 (float) dstX0, (float) dstY0,
576 (float) dstX1, (float) dstY1,
577 s0, t0, s1, t1,
578 z);
579
580 util_draw_vertex_buffer(ctx->pipe,
581 ctx->vbuf, offset,
582 PIPE_PRIM_TRIANGLE_FAN,
583 4, /* verts */
584 2); /* attribs/vert */
585
586 /* restore state we changed */
587 cso_restore_blend(ctx->cso);
588 cso_restore_depth_stencil_alpha(ctx->cso);
589 cso_restore_rasterizer(ctx->cso);
590 cso_restore_samplers(ctx->cso);
591 cso_restore_sampler_textures(ctx->cso);
592 cso_restore_framebuffer(ctx->cso);
593 cso_restore_fragment_shader(ctx->cso);
594 cso_restore_vertex_shader(ctx->cso);
595 }