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