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