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