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