gallium/util: use new cso_save/restore_state() functions
[mesa.git] / src / gallium / auxiliary / util / u_blit.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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 "util/u_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_sampler.h"
49 #include "util/u_texture.h"
50 #include "util/u_simple_shaders.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_write_color;
61 struct pipe_depth_stencil_alpha_state dsa_keep_depthstencil;
62 struct pipe_rasterizer_state rasterizer;
63 struct pipe_sampler_state sampler;
64 struct pipe_viewport_state viewport;
65 struct pipe_vertex_element velem[2];
66
67 void *vs;
68 void *fs[PIPE_MAX_TEXTURE_TYPES][TGSI_WRITEMASK_XYZW + 1][3];
69
70 struct pipe_resource *vbuf; /**< quad vertices */
71 unsigned vbuf_slot;
72
73 float vertices[4][2][4]; /**< vertex/texcoords for quad */
74 };
75
76
77 /**
78 * Create state object for blit.
79 * Intended to be created once and re-used for many blit() calls.
80 */
81 struct blit_state *
82 util_create_blit(struct pipe_context *pipe, struct cso_context *cso)
83 {
84 struct blit_state *ctx;
85 uint i;
86
87 ctx = CALLOC_STRUCT(blit_state);
88 if (!ctx)
89 return NULL;
90
91 ctx->pipe = pipe;
92 ctx->cso = cso;
93
94 /* disabled blending/masking */
95 ctx->blend_write_color.rt[0].colormask = PIPE_MASK_RGBA;
96
97 /* rasterizer */
98 ctx->rasterizer.cull_face = PIPE_FACE_NONE;
99 ctx->rasterizer.half_pixel_center = 1;
100 ctx->rasterizer.bottom_edge_rule = 1;
101 ctx->rasterizer.depth_clip = 1;
102
103 /* samplers */
104 ctx->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
105 ctx->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
106 ctx->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
107 ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
108 ctx->sampler.min_img_filter = 0; /* set later */
109 ctx->sampler.mag_img_filter = 0; /* set later */
110
111 /* vertex elements state */
112 for (i = 0; i < 2; i++) {
113 ctx->velem[i].src_offset = i * 4 * sizeof(float);
114 ctx->velem[i].instance_divisor = 0;
115 ctx->velem[i].vertex_buffer_index = cso_get_aux_vertex_buffer_slot(cso);
116 ctx->velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
117 }
118
119 ctx->vbuf = NULL;
120
121 /* init vertex data that doesn't change */
122 for (i = 0; i < 4; i++) {
123 ctx->vertices[i][0][3] = 1.0f; /* w */
124 ctx->vertices[i][1][3] = 1.0f; /* q */
125 }
126
127 return ctx;
128 }
129
130
131 /**
132 * Destroy a blit context
133 */
134 void
135 util_destroy_blit(struct blit_state *ctx)
136 {
137 struct pipe_context *pipe = ctx->pipe;
138 unsigned i, j, k;
139
140 if (ctx->vs)
141 pipe->delete_vs_state(pipe, ctx->vs);
142
143 for (i = 0; i < Elements(ctx->fs); i++) {
144 for (j = 0; j < Elements(ctx->fs[i]); j++) {
145 for (k = 0; k < Elements(ctx->fs[i][j]); k++) {
146 if (ctx->fs[i][j][k])
147 pipe->delete_fs_state(pipe, ctx->fs[i][j][k]);
148 }
149 }
150 }
151
152 pipe_resource_reference(&ctx->vbuf, NULL);
153
154 FREE(ctx);
155 }
156
157
158 /**
159 * Helper function to set the fragment shaders.
160 */
161 static inline void
162 set_fragment_shader(struct blit_state *ctx, uint writemask,
163 enum pipe_format format,
164 enum pipe_texture_target pipe_tex)
165 {
166 enum tgsi_return_type stype;
167 unsigned idx;
168
169 if (util_format_is_pure_uint(format)) {
170 stype = TGSI_RETURN_TYPE_UINT;
171 idx = 0;
172 } else if (util_format_is_pure_sint(format)) {
173 stype = TGSI_RETURN_TYPE_SINT;
174 idx = 1;
175 } else {
176 stype = TGSI_RETURN_TYPE_FLOAT;
177 idx = 2;
178 }
179
180 if (!ctx->fs[pipe_tex][writemask][idx]) {
181 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(pipe_tex, 0);
182
183 ctx->fs[pipe_tex][writemask][idx] =
184 util_make_fragment_tex_shader_writemask(ctx->pipe, tgsi_tex,
185 TGSI_INTERPOLATE_LINEAR,
186 writemask,
187 stype);
188 }
189
190 cso_set_fragment_shader_handle(ctx->cso, ctx->fs[pipe_tex][writemask][idx]);
191 }
192
193
194 /**
195 * Helper function to set the vertex shader.
196 */
197 static inline void
198 set_vertex_shader(struct blit_state *ctx)
199 {
200 /* vertex shader - still required to provide the linkage between
201 * fragment shader input semantics and vertex_element/buffers.
202 */
203 if (!ctx->vs) {
204 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
205 TGSI_SEMANTIC_GENERIC };
206 const uint semantic_indexes[] = { 0, 0 };
207 ctx->vs = util_make_vertex_passthrough_shader(ctx->pipe, 2,
208 semantic_names,
209 semantic_indexes, FALSE);
210 }
211
212 cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
213 }
214
215
216 /**
217 * Get offset of next free slot in vertex buffer for quad vertices.
218 */
219 static unsigned
220 get_next_slot( struct blit_state *ctx )
221 {
222 const unsigned max_slots = 4096 / sizeof ctx->vertices;
223
224 if (ctx->vbuf_slot >= max_slots) {
225 pipe_resource_reference(&ctx->vbuf, NULL);
226 ctx->vbuf_slot = 0;
227 }
228
229 if (!ctx->vbuf) {
230 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
231 PIPE_BIND_VERTEX_BUFFER,
232 PIPE_USAGE_STREAM,
233 max_slots * sizeof ctx->vertices);
234 }
235
236 return ctx->vbuf_slot++ * sizeof ctx->vertices;
237 }
238
239
240
241
242 /**
243 * Setup vertex data for the textured quad we'll draw.
244 * Note: y=0=top
245 *
246 * FIXME: We should call util_map_texcoords2d_onto_cubemap
247 * for cubemaps.
248 */
249 static unsigned
250 setup_vertex_data_tex(struct blit_state *ctx,
251 unsigned src_target,
252 unsigned src_face,
253 float x0, float y0, float x1, float y1,
254 float s0, float t0, float s1, float t1,
255 float z)
256 {
257 unsigned offset;
258
259 ctx->vertices[0][0][0] = x0;
260 ctx->vertices[0][0][1] = y0;
261 ctx->vertices[0][0][2] = z;
262 ctx->vertices[0][1][0] = s0; /*s*/
263 ctx->vertices[0][1][1] = t0; /*t*/
264 ctx->vertices[0][1][2] = 0; /*r*/
265
266 ctx->vertices[1][0][0] = x1;
267 ctx->vertices[1][0][1] = y0;
268 ctx->vertices[1][0][2] = z;
269 ctx->vertices[1][1][0] = s1; /*s*/
270 ctx->vertices[1][1][1] = t0; /*t*/
271 ctx->vertices[1][1][2] = 0; /*r*/
272
273 ctx->vertices[2][0][0] = x1;
274 ctx->vertices[2][0][1] = y1;
275 ctx->vertices[2][0][2] = z;
276 ctx->vertices[2][1][0] = s1;
277 ctx->vertices[2][1][1] = t1;
278 ctx->vertices[3][1][2] = 0;
279
280 ctx->vertices[3][0][0] = x0;
281 ctx->vertices[3][0][1] = y1;
282 ctx->vertices[3][0][2] = z;
283 ctx->vertices[3][1][0] = s0;
284 ctx->vertices[3][1][1] = t1;
285 ctx->vertices[3][1][2] = 0;
286
287 if (src_target == PIPE_TEXTURE_CUBE ||
288 src_target == PIPE_TEXTURE_CUBE_ARRAY) {
289 /* Map cubemap texture coordinates inplace. */
290 const unsigned stride = sizeof ctx->vertices[0] / sizeof ctx->vertices[0][0][0];
291 util_map_texcoords2d_onto_cubemap(src_face,
292 &ctx->vertices[0][1][0], stride,
293 &ctx->vertices[0][1][0], stride,
294 TRUE);
295 }
296
297 offset = get_next_slot( ctx );
298
299 if (ctx->vbuf) {
300 pipe_buffer_write_nooverlap(ctx->pipe, ctx->vbuf,
301 offset, sizeof(ctx->vertices), ctx->vertices);
302 }
303
304 return offset;
305 }
306
307
308 /**
309 * \return TRUE if two regions overlap, FALSE otherwise
310 */
311 static boolean
312 regions_overlap(int srcX0, int srcY0,
313 int srcX1, int srcY1,
314 int dstX0, int dstY0,
315 int dstX1, int dstY1)
316 {
317 if (MAX2(srcX0, srcX1) < MIN2(dstX0, dstX1))
318 return FALSE; /* src completely left of dst */
319
320 if (MAX2(dstX0, dstX1) < MIN2(srcX0, srcX1))
321 return FALSE; /* dst completely left of src */
322
323 if (MAX2(srcY0, srcY1) < MIN2(dstY0, dstY1))
324 return FALSE; /* src completely above dst */
325
326 if (MAX2(dstY0, dstY1) < MIN2(srcY0, srcY1))
327 return FALSE; /* dst completely above src */
328
329 return TRUE; /* some overlap */
330 }
331
332
333 /**
334 * Can we blit from src format to dest format with a simple copy?
335 */
336 static boolean
337 formats_compatible(enum pipe_format src_format,
338 enum pipe_format dst_format)
339 {
340 if (src_format == dst_format) {
341 return TRUE;
342 }
343 else {
344 const struct util_format_description *src_desc =
345 util_format_description(src_format);
346 const struct util_format_description *dst_desc =
347 util_format_description(dst_format);
348 return util_is_format_compatible(src_desc, dst_desc);
349 }
350 }
351
352
353 /**
354 * Copy pixel block from src surface to dst surface.
355 * Overlapping regions are acceptable.
356 * Flipping and stretching are supported.
357 * \param filter one of PIPE_TEX_FILTER_NEAREST/LINEAR
358 * \param writemask bitmask of PIPE_MASK_[RGBAZS]. Controls which channels
359 * in the dest surface are sourced from the src surface.
360 * Disabled color channels are sourced from (0,0,0,1).
361 */
362 void
363 util_blit_pixels(struct blit_state *ctx,
364 struct pipe_resource *src_tex,
365 unsigned src_level,
366 int srcX0, int srcY0,
367 int srcX1, int srcY1,
368 int srcZ0,
369 struct pipe_surface *dst,
370 int dstX0, int dstY0,
371 int dstX1, int dstY1,
372 float z, uint filter,
373 uint writemask)
374 {
375 struct pipe_context *pipe = ctx->pipe;
376 enum pipe_format src_format, dst_format;
377 const int srcW = abs(srcX1 - srcX0);
378 const int srcH = abs(srcY1 - srcY0);
379 boolean overlap;
380 boolean is_stencil, is_depth, blit_depth, blit_stencil;
381 const struct util_format_description *src_desc =
382 util_format_description(src_tex->format);
383 struct pipe_blit_info info;
384
385 assert(filter == PIPE_TEX_FILTER_NEAREST ||
386 filter == PIPE_TEX_FILTER_LINEAR);
387
388 assert(src_level <= src_tex->last_level);
389
390 /* do the regions overlap? */
391 overlap = src_tex == dst->texture &&
392 dst->u.tex.level == src_level &&
393 dst->u.tex.first_layer == srcZ0 &&
394 regions_overlap(srcX0, srcY0, srcX1, srcY1,
395 dstX0, dstY0, dstX1, dstY1);
396
397 src_format = util_format_linear(src_tex->format);
398 dst_format = util_format_linear(dst->texture->format);
399
400 /* See whether we will blit depth or stencil. */
401 is_depth = util_format_has_depth(src_desc);
402 is_stencil = util_format_has_stencil(src_desc);
403
404 blit_depth = is_depth && (writemask & PIPE_MASK_Z);
405 blit_stencil = is_stencil && (writemask & PIPE_MASK_S);
406
407 if (is_depth || is_stencil) {
408 assert((writemask & PIPE_MASK_RGBA) == 0);
409 assert(blit_depth || blit_stencil);
410 }
411 else {
412 assert((writemask & PIPE_MASK_ZS) == 0);
413 assert(!blit_depth);
414 assert(!blit_stencil);
415 }
416
417 /*
418 * XXX: z parameter is deprecated. dst->u.tex.first_layer
419 * specificies the destination layer.
420 */
421 assert(z == 0.0f);
422
423 /*
424 * Check for simple case: no format conversion, no flipping, no stretching,
425 * no overlapping, same number of samples.
426 * Filter mode should not matter since there's no stretching.
427 */
428 if (formats_compatible(src_format, dst_format) &&
429 src_tex->nr_samples == dst->texture->nr_samples &&
430 is_stencil == blit_stencil &&
431 is_depth == blit_depth &&
432 srcX0 < srcX1 &&
433 dstX0 < dstX1 &&
434 srcY0 < srcY1 &&
435 dstY0 < dstY1 &&
436 (dstX1 - dstX0) == (srcX1 - srcX0) &&
437 (dstY1 - dstY0) == (srcY1 - srcY0) &&
438 !overlap) {
439 struct pipe_box src_box;
440 src_box.x = srcX0;
441 src_box.y = srcY0;
442 src_box.z = srcZ0;
443 src_box.width = srcW;
444 src_box.height = srcH;
445 src_box.depth = 1;
446 pipe->resource_copy_region(pipe,
447 dst->texture, dst->u.tex.level,
448 dstX0, dstY0, dst->u.tex.first_layer,/* dest */
449 src_tex, src_level,
450 &src_box);
451 return;
452 }
453
454 memset(&info, 0, sizeof info);
455 info.dst.resource = dst->texture;
456 info.dst.level = dst->u.tex.level;
457 info.dst.box.x = dstX0;
458 info.dst.box.y = dstY0;
459 info.dst.box.z = dst->u.tex.first_layer;
460 info.dst.box.width = dstX1 - dstX0;
461 info.dst.box.height = dstY1 - dstY0;
462 assert(info.dst.box.width >= 0);
463 assert(info.dst.box.height >= 0);
464 info.dst.box.depth = 1;
465 info.dst.format = dst_format;
466 info.src.resource = src_tex;
467 info.src.level = src_level;
468 info.src.box.x = srcX0;
469 info.src.box.y = srcY0;
470 info.src.box.z = srcZ0;
471 info.src.box.width = srcX1 - srcX0;
472 info.src.box.height = srcY1 - srcY0;
473 info.src.box.depth = 1;
474 info.src.format = src_format;
475 info.mask = writemask;
476 info.filter = filter;
477 info.scissor_enable = 0;
478
479 pipe->blit(pipe, &info);
480 }
481
482
483 /**
484 * Copy pixel block from src sampler view to dst surface.
485 *
486 * The sampler view's first_level field indicates the source
487 * mipmap level to use.
488 *
489 * The sampler view's first_layer indicate the layer to use, but for
490 * cube maps it must point to the first face. Face is passed in src_face.
491 *
492 * The main advantage over util_blit_pixels is that it allows to specify swizzles in
493 * pipe_sampler_view::swizzle_?.
494 *
495 * But there is no control over blitting Z and/or stencil.
496 */
497 void
498 util_blit_pixels_tex(struct blit_state *ctx,
499 struct pipe_sampler_view *src_sampler_view,
500 int srcX0, int srcY0,
501 int srcX1, int srcY1,
502 unsigned src_face,
503 struct pipe_surface *dst,
504 int dstX0, int dstY0,
505 int dstX1, int dstY1,
506 float z, uint filter)
507 {
508 boolean normalized = src_sampler_view->texture->target != PIPE_TEXTURE_RECT;
509 struct pipe_framebuffer_state fb;
510 float s0, t0, s1, t1;
511 unsigned offset;
512 struct pipe_resource *tex = src_sampler_view->texture;
513
514 assert(filter == PIPE_TEX_FILTER_NEAREST ||
515 filter == PIPE_TEX_FILTER_LINEAR);
516
517 assert(tex);
518 assert(tex->width0 != 0);
519 assert(tex->height0 != 0);
520
521 s0 = (float) srcX0;
522 s1 = (float) srcX1;
523 t0 = (float) srcY0;
524 t1 = (float) srcY1;
525
526 if(normalized)
527 {
528 /* normalize according to the mipmap level's size */
529 int level = src_sampler_view->u.tex.first_level;
530 float w = (float) u_minify(tex->width0, level);
531 float h = (float) u_minify(tex->height0, level);
532 s0 /= w;
533 s1 /= w;
534 t0 /= h;
535 t1 /= h;
536 }
537
538 assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format,
539 PIPE_TEXTURE_2D,
540 dst->texture->nr_samples,
541 PIPE_BIND_RENDER_TARGET));
542
543 /* save state (restored below) */
544 cso_save_state(ctx->cso, (CSO_BIT_BLEND |
545 CSO_BIT_DEPTH_STENCIL_ALPHA |
546 CSO_BIT_RASTERIZER |
547 CSO_BIT_SAMPLE_MASK |
548 CSO_BIT_MIN_SAMPLES |
549 CSO_BIT_FRAGMENT_SAMPLERS |
550 CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
551 CSO_BIT_STREAM_OUTPUTS |
552 CSO_BIT_VIEWPORT |
553 CSO_BIT_FRAMEBUFFER |
554 CSO_BIT_FRAGMENT_SHADER |
555 CSO_BIT_VERTEX_SHADER |
556 CSO_BIT_TESSCTRL_SHADER |
557 CSO_BIT_TESSEVAL_SHADER |
558 CSO_BIT_GEOMETRY_SHADER |
559 CSO_BIT_VERTEX_ELEMENTS |
560 CSO_BIT_AUX_VERTEX_BUFFER_SLOT));
561
562 /* set misc state we care about */
563 cso_set_blend(ctx->cso, &ctx->blend_write_color);
564 cso_set_depth_stencil_alpha(ctx->cso, &ctx->dsa_keep_depthstencil);
565 cso_set_sample_mask(ctx->cso, ~0);
566 cso_set_min_samples(ctx->cso, 1);
567 cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
568 cso_set_vertex_elements(ctx->cso, 2, ctx->velem);
569 cso_set_stream_outputs(ctx->cso, 0, NULL, NULL);
570
571 /* sampler */
572 ctx->sampler.normalized_coords = normalized;
573 ctx->sampler.min_img_filter = filter;
574 ctx->sampler.mag_img_filter = filter;
575 {
576 const struct pipe_sampler_state *samplers[] = {&ctx->sampler};
577 cso_set_samplers(ctx->cso, PIPE_SHADER_FRAGMENT, 1, samplers);
578 }
579
580 /* viewport */
581 ctx->viewport.scale[0] = 0.5f * dst->width;
582 ctx->viewport.scale[1] = 0.5f * dst->height;
583 ctx->viewport.scale[2] = 0.5f;
584 ctx->viewport.translate[0] = 0.5f * dst->width;
585 ctx->viewport.translate[1] = 0.5f * dst->height;
586 ctx->viewport.translate[2] = 0.5f;
587 cso_set_viewport(ctx->cso, &ctx->viewport);
588
589 /* texture */
590 cso_set_sampler_views(ctx->cso, PIPE_SHADER_FRAGMENT, 1, &src_sampler_view);
591
592 /* shaders */
593 set_fragment_shader(ctx, TGSI_WRITEMASK_XYZW,
594 src_sampler_view->format,
595 src_sampler_view->texture->target);
596 set_vertex_shader(ctx);
597 cso_set_tessctrl_shader_handle(ctx->cso, NULL);
598 cso_set_tesseval_shader_handle(ctx->cso, NULL);
599 cso_set_geometry_shader_handle(ctx->cso, NULL);
600
601 /* drawing dest */
602 memset(&fb, 0, sizeof(fb));
603 fb.width = dst->width;
604 fb.height = dst->height;
605 fb.nr_cbufs = 1;
606 fb.cbufs[0] = dst;
607 cso_set_framebuffer(ctx->cso, &fb);
608
609 /* draw quad */
610 offset = setup_vertex_data_tex(ctx,
611 src_sampler_view->texture->target,
612 src_face,
613 (float) dstX0 / dst->width * 2.0f - 1.0f,
614 (float) dstY0 / dst->height * 2.0f - 1.0f,
615 (float) dstX1 / dst->width * 2.0f - 1.0f,
616 (float) dstY1 / dst->height * 2.0f - 1.0f,
617 s0, t0, s1, t1,
618 z);
619
620 util_draw_vertex_buffer(ctx->pipe, ctx->cso, ctx->vbuf,
621 cso_get_aux_vertex_buffer_slot(ctx->cso),
622 offset,
623 PIPE_PRIM_TRIANGLE_FAN,
624 4, /* verts */
625 2); /* attribs/vert */
626
627 /* restore state we changed */
628 cso_restore_state(ctx->cso);
629 }