util/u_blit: Support blits from cubemaps.
[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 "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, blend_keep_color;
61 struct pipe_depth_stencil_alpha_state dsa_keep_depthstencil;
62 struct pipe_depth_stencil_alpha_state dsa_write_depthstencil;
63 struct pipe_depth_stencil_alpha_state dsa_write_depth;
64 struct pipe_depth_stencil_alpha_state dsa_write_stencil;
65 struct pipe_rasterizer_state rasterizer;
66 struct pipe_sampler_state sampler;
67 struct pipe_viewport_state viewport;
68 struct pipe_vertex_element velem[2];
69 enum pipe_texture_target internal_target;
70
71 void *vs;
72 void *fs[PIPE_MAX_TEXTURE_TYPES][TGSI_WRITEMASK_XYZW + 1];
73 void *fs_depthstencil[PIPE_MAX_TEXTURE_TYPES];
74 void *fs_depth[PIPE_MAX_TEXTURE_TYPES];
75 void *fs_stencil[PIPE_MAX_TEXTURE_TYPES];
76
77 struct pipe_resource *vbuf; /**< quad vertices */
78 unsigned vbuf_slot;
79
80 float vertices[4][2][4]; /**< vertex/texcoords for quad */
81
82 boolean has_stencil_export;
83 };
84
85
86 /**
87 * Create state object for blit.
88 * Intended to be created once and re-used for many blit() calls.
89 */
90 struct blit_state *
91 util_create_blit(struct pipe_context *pipe, struct cso_context *cso)
92 {
93 struct blit_state *ctx;
94 uint i;
95
96 ctx = CALLOC_STRUCT(blit_state);
97 if (!ctx)
98 return NULL;
99
100 ctx->pipe = pipe;
101 ctx->cso = cso;
102
103 /* disabled blending/masking */
104 ctx->blend_write_color.rt[0].colormask = PIPE_MASK_RGBA;
105
106 /* depth stencil states */
107 ctx->dsa_write_depth.depth.enabled = 1;
108 ctx->dsa_write_depth.depth.writemask = 1;
109 ctx->dsa_write_depth.depth.func = PIPE_FUNC_ALWAYS;
110 ctx->dsa_write_stencil.stencil[0].enabled = 1;
111 ctx->dsa_write_stencil.stencil[0].func = PIPE_FUNC_ALWAYS;
112 ctx->dsa_write_stencil.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
113 ctx->dsa_write_stencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
114 ctx->dsa_write_stencil.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
115 ctx->dsa_write_stencil.stencil[0].valuemask = 0xff;
116 ctx->dsa_write_stencil.stencil[0].writemask = 0xff;
117 ctx->dsa_write_depthstencil.depth = ctx->dsa_write_depth.depth;
118 ctx->dsa_write_depthstencil.stencil[0] = ctx->dsa_write_stencil.stencil[0];
119
120 /* rasterizer */
121 ctx->rasterizer.cull_face = PIPE_FACE_NONE;
122 ctx->rasterizer.half_pixel_center = 1;
123 ctx->rasterizer.bottom_edge_rule = 1;
124 ctx->rasterizer.depth_clip = 1;
125
126 /* samplers */
127 ctx->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
128 ctx->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
129 ctx->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
130 ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
131 ctx->sampler.min_img_filter = 0; /* set later */
132 ctx->sampler.mag_img_filter = 0; /* set later */
133
134 /* vertex elements state */
135 for (i = 0; i < 2; i++) {
136 ctx->velem[i].src_offset = i * 4 * sizeof(float);
137 ctx->velem[i].instance_divisor = 0;
138 ctx->velem[i].vertex_buffer_index = cso_get_aux_vertex_buffer_slot(cso);
139 ctx->velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
140 }
141
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][3] = 1.0f; /* q */
148 }
149
150 if(pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
151 ctx->internal_target = PIPE_TEXTURE_2D;
152 else
153 ctx->internal_target = PIPE_TEXTURE_RECT;
154
155 ctx->has_stencil_export =
156 pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT);
157
158 return ctx;
159 }
160
161
162 /**
163 * Destroy a blit context
164 */
165 void
166 util_destroy_blit(struct blit_state *ctx)
167 {
168 struct pipe_context *pipe = ctx->pipe;
169 unsigned i, j;
170
171 if (ctx->vs)
172 pipe->delete_vs_state(pipe, ctx->vs);
173
174 for (i = 0; i < Elements(ctx->fs); i++) {
175 for (j = 0; j < Elements(ctx->fs[i]); j++) {
176 if (ctx->fs[i][j])
177 pipe->delete_fs_state(pipe, ctx->fs[i][j]);
178 }
179 }
180
181 for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
182 if (ctx->fs_depthstencil[i]) {
183 pipe->delete_fs_state(pipe, ctx->fs_depthstencil[i]);
184 }
185 if (ctx->fs_depth[i]) {
186 pipe->delete_fs_state(pipe, ctx->fs_depth[i]);
187 }
188 if (ctx->fs_stencil[i]) {
189 pipe->delete_fs_state(pipe, ctx->fs_stencil[i]);
190 }
191 }
192
193 pipe_resource_reference(&ctx->vbuf, NULL);
194
195 FREE(ctx);
196 }
197
198
199 /**
200 * Helper function to set the fragment shaders.
201 */
202 static INLINE void
203 set_fragment_shader(struct blit_state *ctx, uint writemask,
204 enum pipe_texture_target pipe_tex)
205 {
206 if (!ctx->fs[pipe_tex][writemask]) {
207 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(pipe_tex, 0);
208
209 ctx->fs[pipe_tex][writemask] =
210 util_make_fragment_tex_shader_writemask(ctx->pipe, tgsi_tex,
211 TGSI_INTERPOLATE_LINEAR,
212 writemask);
213 }
214
215 cso_set_fragment_shader_handle(ctx->cso, ctx->fs[pipe_tex][writemask]);
216 }
217
218
219 /**
220 * Helper function to set the shader which writes depth and stencil.
221 */
222 static INLINE void
223 set_depthstencil_fragment_shader(struct blit_state *ctx,
224 enum pipe_texture_target pipe_tex)
225 {
226 if (!ctx->fs_depthstencil[pipe_tex]) {
227 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(pipe_tex, 0);
228
229 ctx->fs_depthstencil[pipe_tex] =
230 util_make_fragment_tex_shader_writedepthstencil(ctx->pipe, tgsi_tex,
231 TGSI_INTERPOLATE_LINEAR);
232 }
233
234 cso_set_fragment_shader_handle(ctx->cso, ctx->fs_depthstencil[pipe_tex]);
235 }
236
237
238 /**
239 * Helper function to set the shader which writes depth.
240 */
241 static INLINE void
242 set_depth_fragment_shader(struct blit_state *ctx,
243 enum pipe_texture_target pipe_tex)
244 {
245 if (!ctx->fs_depth[pipe_tex]) {
246 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(pipe_tex, 0);
247
248 ctx->fs_depth[pipe_tex] =
249 util_make_fragment_tex_shader_writedepth(ctx->pipe, tgsi_tex,
250 TGSI_INTERPOLATE_LINEAR);
251 }
252
253 cso_set_fragment_shader_handle(ctx->cso, ctx->fs_depth[pipe_tex]);
254 }
255
256
257 /**
258 * Helper function to set the shader which writes stencil.
259 */
260 static INLINE void
261 set_stencil_fragment_shader(struct blit_state *ctx,
262 enum pipe_texture_target pipe_tex)
263 {
264 if (!ctx->fs_stencil[pipe_tex]) {
265 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(pipe_tex, 0);
266
267 ctx->fs_stencil[pipe_tex] =
268 util_make_fragment_tex_shader_writestencil(ctx->pipe, tgsi_tex,
269 TGSI_INTERPOLATE_LINEAR);
270 }
271
272 cso_set_fragment_shader_handle(ctx->cso, ctx->fs_stencil[pipe_tex]);
273 }
274
275
276 /**
277 * Helper function to set the vertex shader.
278 */
279 static INLINE void
280 set_vertex_shader(struct blit_state *ctx)
281 {
282 /* vertex shader - still required to provide the linkage between
283 * fragment shader input semantics and vertex_element/buffers.
284 */
285 if (!ctx->vs) {
286 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
287 TGSI_SEMANTIC_GENERIC };
288 const uint semantic_indexes[] = { 0, 0 };
289 ctx->vs = util_make_vertex_passthrough_shader(ctx->pipe, 2,
290 semantic_names,
291 semantic_indexes);
292 }
293
294 cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
295 }
296
297
298 /**
299 * Get offset of next free slot in vertex buffer for quad vertices.
300 */
301 static unsigned
302 get_next_slot( struct blit_state *ctx )
303 {
304 const unsigned max_slots = 4096 / sizeof ctx->vertices;
305
306 if (ctx->vbuf_slot >= max_slots) {
307 pipe_resource_reference(&ctx->vbuf, NULL);
308 ctx->vbuf_slot = 0;
309 }
310
311 if (!ctx->vbuf) {
312 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
313 PIPE_BIND_VERTEX_BUFFER,
314 PIPE_USAGE_STREAM,
315 max_slots * sizeof ctx->vertices);
316 }
317
318 return ctx->vbuf_slot++ * sizeof ctx->vertices;
319 }
320
321
322
323
324 /**
325 * Setup vertex data for the textured quad we'll draw.
326 * Note: y=0=top
327 */
328 static unsigned
329 setup_vertex_data_tex(struct blit_state *ctx,
330 unsigned src_target,
331 unsigned src_face,
332 float x0, float y0, float x1, float y1,
333 float s0, float t0, float s1, float t1,
334 float z)
335 {
336 unsigned offset;
337
338 ctx->vertices[0][0][0] = x0;
339 ctx->vertices[0][0][1] = y0;
340 ctx->vertices[0][0][2] = z;
341 ctx->vertices[0][1][0] = s0; /*s*/
342 ctx->vertices[0][1][1] = t0; /*t*/
343 ctx->vertices[0][1][2] = 0; /*r*/
344
345 ctx->vertices[1][0][0] = x1;
346 ctx->vertices[1][0][1] = y0;
347 ctx->vertices[1][0][2] = z;
348 ctx->vertices[1][1][0] = s1; /*s*/
349 ctx->vertices[1][1][1] = t0; /*t*/
350 ctx->vertices[1][1][2] = 0; /*r*/
351
352 ctx->vertices[2][0][0] = x1;
353 ctx->vertices[2][0][1] = y1;
354 ctx->vertices[2][0][2] = z;
355 ctx->vertices[2][1][0] = s1;
356 ctx->vertices[2][1][1] = t1;
357 ctx->vertices[3][1][2] = 0;
358
359 ctx->vertices[3][0][0] = x0;
360 ctx->vertices[3][0][1] = y1;
361 ctx->vertices[3][0][2] = z;
362 ctx->vertices[3][1][0] = s0;
363 ctx->vertices[3][1][1] = t1;
364 ctx->vertices[3][1][2] = 0;
365
366 if (src_target == PIPE_TEXTURE_CUBE ||
367 src_target == PIPE_TEXTURE_CUBE_ARRAY) {
368 /* Map cubemap texture coordinates inplace. */
369 const unsigned stride = sizeof ctx->vertices[0] / sizeof ctx->vertices[0][0][0];
370 util_map_texcoords2d_onto_cubemap(src_face,
371 &ctx->vertices[0][1][0], stride,
372 &ctx->vertices[0][1][0], stride);
373 }
374
375 offset = get_next_slot( ctx );
376
377 if (ctx->vbuf) {
378 pipe_buffer_write_nooverlap(ctx->pipe, ctx->vbuf,
379 offset, sizeof(ctx->vertices), ctx->vertices);
380 }
381
382 return offset;
383 }
384
385
386 /**
387 * \return TRUE if two regions overlap, FALSE otherwise
388 */
389 static boolean
390 regions_overlap(int srcX0, int srcY0,
391 int srcX1, int srcY1,
392 int dstX0, int dstY0,
393 int dstX1, int dstY1)
394 {
395 if (MAX2(srcX0, srcX1) < MIN2(dstX0, dstX1))
396 return FALSE; /* src completely left of dst */
397
398 if (MAX2(dstX0, dstX1) < MIN2(srcX0, srcX1))
399 return FALSE; /* dst completely left of src */
400
401 if (MAX2(srcY0, srcY1) < MIN2(dstY0, dstY1))
402 return FALSE; /* src completely above dst */
403
404 if (MAX2(dstY0, dstY1) < MIN2(srcY0, srcY1))
405 return FALSE; /* dst completely above src */
406
407 return TRUE; /* some overlap */
408 }
409
410
411 /**
412 * Can we blit from src format to dest format with a simple copy?
413 */
414 static boolean
415 formats_compatible(enum pipe_format src_format,
416 enum pipe_format dst_format)
417 {
418 if (src_format == dst_format) {
419 return TRUE;
420 }
421 else {
422 const struct util_format_description *src_desc =
423 util_format_description(src_format);
424 const struct util_format_description *dst_desc =
425 util_format_description(dst_format);
426 return util_is_format_compatible(src_desc, dst_desc);
427 }
428 }
429
430
431 /**
432 * Copy pixel block from src surface to dst surface.
433 * Overlapping regions are acceptable.
434 * Flipping and stretching are supported.
435 * \param filter one of PIPE_TEX_MIPFILTER_NEAREST/LINEAR
436 * \param writemask controls which channels in the dest surface are sourced
437 * from the src surface. Disabled channels are sourced
438 * from (0,0,0,1).
439 */
440 void
441 util_blit_pixels(struct blit_state *ctx,
442 struct pipe_resource *src_tex,
443 unsigned src_level,
444 int srcX0, int srcY0,
445 int srcX1, int srcY1,
446 int srcZ0,
447 struct pipe_surface *dst,
448 int dstX0, int dstY0,
449 int dstX1, int dstY1,
450 float z, uint filter,
451 uint writemask, uint zs_writemask)
452 {
453 struct pipe_context *pipe = ctx->pipe;
454 struct pipe_screen *screen = pipe->screen;
455 enum pipe_format src_format, dst_format;
456 struct pipe_sampler_view *sampler_view = NULL;
457 struct pipe_sampler_view sv_templ;
458 struct pipe_surface *dst_surface;
459 struct pipe_framebuffer_state fb;
460 const int srcW = abs(srcX1 - srcX0);
461 const int srcH = abs(srcY1 - srcY0);
462 unsigned offset;
463 boolean overlap;
464 float s0, t0, s1, t1;
465 boolean normalized;
466 boolean is_stencil, is_depth, blit_depth, blit_stencil;
467 const struct util_format_description *src_desc =
468 util_format_description(src_tex->format);
469
470 assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
471 filter == PIPE_TEX_MIPFILTER_LINEAR);
472
473 assert(src_level <= src_tex->last_level);
474
475 /* do the regions overlap? */
476 overlap = src_tex == dst->texture &&
477 dst->u.tex.level == src_level &&
478 dst->u.tex.first_layer == srcZ0 &&
479 regions_overlap(srcX0, srcY0, srcX1, srcY1,
480 dstX0, dstY0, dstX1, dstY1);
481
482 src_format = util_format_linear(src_tex->format);
483 dst_format = util_format_linear(dst->texture->format);
484
485 /* See whether we will blit depth or stencil. */
486 is_depth = util_format_has_depth(src_desc);
487 is_stencil = util_format_has_stencil(src_desc);
488
489 blit_depth = is_depth && (zs_writemask & BLIT_WRITEMASK_Z);
490 blit_stencil = is_stencil && (zs_writemask & BLIT_WRITEMASK_STENCIL);
491
492 assert((writemask && !zs_writemask && !is_depth && !is_stencil) ||
493 (!writemask && (blit_depth || blit_stencil)));
494
495 /*
496 * Check for simple case: no format conversion, no flipping, no stretching,
497 * no overlapping, same number of samples.
498 * Filter mode should not matter since there's no stretching.
499 */
500 if (formats_compatible(src_format, dst_format) &&
501 src_tex->nr_samples == dst->texture->nr_samples &&
502 is_stencil == blit_stencil &&
503 is_depth == blit_depth &&
504 srcX0 < srcX1 &&
505 dstX0 < dstX1 &&
506 srcY0 < srcY1 &&
507 dstY0 < dstY1 &&
508 (dstX1 - dstX0) == (srcX1 - srcX0) &&
509 (dstY1 - dstY0) == (srcY1 - srcY0) &&
510 !overlap) {
511 struct pipe_box src_box;
512 src_box.x = srcX0;
513 src_box.y = srcY0;
514 src_box.z = srcZ0;
515 src_box.width = srcW;
516 src_box.height = srcH;
517 src_box.depth = 1;
518 pipe->resource_copy_region(pipe,
519 dst->texture, dst->u.tex.level,
520 dstX0, dstY0, dst->u.tex.first_layer,/* dest */
521 src_tex, src_level,
522 &src_box);
523 return;
524 }
525
526 /* XXX Reading multisample textures is unimplemented. */
527 assert(src_tex->nr_samples <= 1);
528 if (src_tex->nr_samples > 1) {
529 return;
530 }
531
532 /* It's a mistake to call this function with a stencil format and
533 * without shader stencil export. We don't do software fallbacks here.
534 * Ignore stencil and only copy depth.
535 */
536 if (blit_stencil && !ctx->has_stencil_export) {
537 blit_stencil = FALSE;
538
539 if (!blit_depth)
540 return;
541 }
542
543 if (dst_format == dst->format) {
544 dst_surface = dst;
545 } else {
546 struct pipe_surface templ = *dst;
547 templ.format = dst_format;
548 dst_surface = pipe->create_surface(pipe, dst->texture, &templ);
549 }
550
551 /* Create a temporary texture when src and dest alias.
552 */
553 if (src_tex == dst_surface->texture &&
554 dst_surface->u.tex.level == src_level &&
555 dst_surface->u.tex.first_layer == srcZ0) {
556 /* Make a temporary texture which contains a copy of the source pixels.
557 * Then we'll sample from the temporary texture.
558 */
559 struct pipe_resource texTemp;
560 struct pipe_resource *tex;
561 struct pipe_sampler_view sv_templ;
562 struct pipe_box src_box;
563 const int srcLeft = MIN2(srcX0, srcX1);
564 const int srcTop = MIN2(srcY0, srcY1);
565
566 if (srcLeft != srcX0) {
567 /* left-right flip */
568 int tmp = dstX0;
569 dstX0 = dstX1;
570 dstX1 = tmp;
571 }
572
573 if (srcTop != srcY0) {
574 /* up-down flip */
575 int tmp = dstY0;
576 dstY0 = dstY1;
577 dstY1 = tmp;
578 }
579
580 /* create temp texture */
581 memset(&texTemp, 0, sizeof(texTemp));
582 texTemp.target = ctx->internal_target;
583 texTemp.format = src_format;
584 texTemp.last_level = 0;
585 texTemp.width0 = srcW;
586 texTemp.height0 = srcH;
587 texTemp.depth0 = 1;
588 texTemp.array_size = 1;
589 texTemp.bind = PIPE_BIND_SAMPLER_VIEW;
590
591 tex = screen->resource_create(screen, &texTemp);
592 if (!tex)
593 return;
594
595 src_box.x = srcLeft;
596 src_box.y = srcTop;
597 src_box.z = srcZ0;
598 src_box.width = srcW;
599 src_box.height = srcH;
600 src_box.depth = 1;
601 /* load temp texture */
602 pipe->resource_copy_region(pipe,
603 tex, 0, 0, 0, 0, /* dest */
604 src_tex, src_level, &src_box);
605
606 normalized = tex->target != PIPE_TEXTURE_RECT;
607 if(normalized) {
608 s0 = 0.0f;
609 s1 = 1.0f;
610 t0 = 0.0f;
611 t1 = 1.0f;
612 }
613 else {
614 s0 = 0.0f;
615 s1 = (float) srcW;
616 t0 = 0.0f;
617 t1 = (float) srcH;
618 }
619
620 u_sampler_view_default_template(&sv_templ, tex, tex->format);
621 if (!blit_depth && blit_stencil) {
622 /* set a stencil-only format, e.g. Z24S8 --> X24S8 */
623 sv_templ.format = util_format_stencil_only(tex->format);
624 assert(sv_templ.format != PIPE_FORMAT_NONE);
625 }
626 sampler_view = pipe->create_sampler_view(pipe, tex, &sv_templ);
627
628 if (!sampler_view) {
629 pipe_resource_reference(&tex, NULL);
630 return;
631 }
632 pipe_resource_reference(&tex, NULL);
633 }
634 else {
635 /* Directly sample from the source resource/texture */
636 u_sampler_view_default_template(&sv_templ, src_tex, src_format);
637 if (!blit_depth && blit_stencil) {
638 /* set a stencil-only format, e.g. Z24S8 --> X24S8 */
639 sv_templ.format = util_format_stencil_only(src_format);
640 assert(sv_templ.format != PIPE_FORMAT_NONE);
641 }
642 sampler_view = pipe->create_sampler_view(pipe, src_tex, &sv_templ);
643
644 if (!sampler_view) {
645 return;
646 }
647
648 s0 = (float) srcX0;
649 s1 = (float) srcX1;
650 t0 = (float) srcY0;
651 t1 = (float) srcY1;
652 normalized = sampler_view->texture->target != PIPE_TEXTURE_RECT;
653 if(normalized)
654 {
655 s0 /= (float)(u_minify(sampler_view->texture->width0, src_level));
656 s1 /= (float)(u_minify(sampler_view->texture->width0, src_level));
657 t0 /= (float)(u_minify(sampler_view->texture->height0, src_level));
658 t1 /= (float)(u_minify(sampler_view->texture->height0, src_level));
659 }
660 }
661
662 assert(screen->is_format_supported(screen, sampler_view->format,
663 ctx->internal_target, sampler_view->texture->nr_samples,
664 PIPE_BIND_SAMPLER_VIEW));
665 assert(screen->is_format_supported(screen, dst_format, ctx->internal_target,
666 dst_surface->texture->nr_samples,
667 is_depth || is_stencil ? PIPE_BIND_DEPTH_STENCIL :
668 PIPE_BIND_RENDER_TARGET));
669
670 /* save state (restored below) */
671 cso_save_blend(ctx->cso);
672 cso_save_depth_stencil_alpha(ctx->cso);
673 cso_save_rasterizer(ctx->cso);
674 cso_save_sample_mask(ctx->cso);
675 cso_save_samplers(ctx->cso, PIPE_SHADER_FRAGMENT);
676 cso_save_sampler_views(ctx->cso, PIPE_SHADER_FRAGMENT);
677 cso_save_stream_outputs(ctx->cso);
678 cso_save_viewport(ctx->cso);
679 cso_save_framebuffer(ctx->cso);
680 cso_save_fragment_shader(ctx->cso);
681 cso_save_vertex_shader(ctx->cso);
682 cso_save_geometry_shader(ctx->cso);
683 cso_save_vertex_elements(ctx->cso);
684 cso_save_aux_vertex_buffer_slot(ctx->cso);
685 cso_save_render_condition(ctx->cso);
686
687 /* set misc state we care about */
688 if (writemask)
689 cso_set_blend(ctx->cso, &ctx->blend_write_color);
690 else
691 cso_set_blend(ctx->cso, &ctx->blend_keep_color);
692
693 cso_set_sample_mask(ctx->cso, ~0);
694 cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
695 cso_set_vertex_elements(ctx->cso, 2, ctx->velem);
696 cso_set_stream_outputs(ctx->cso, 0, NULL, 0);
697 cso_set_render_condition(ctx->cso, NULL, FALSE, 0);
698
699 /* default sampler state */
700 ctx->sampler.normalized_coords = normalized;
701 ctx->sampler.min_img_filter = filter;
702 ctx->sampler.mag_img_filter = filter;
703 ctx->sampler.min_lod = (float) src_level;
704 ctx->sampler.max_lod = (float) src_level;
705
706 /* Depth stencil state, fragment shader and sampler setup depending on what
707 * we blit.
708 */
709 if (blit_depth && blit_stencil) {
710 cso_single_sampler(ctx->cso, PIPE_SHADER_FRAGMENT, 0, &ctx->sampler);
711 /* don't filter stencil */
712 ctx->sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
713 ctx->sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
714 cso_single_sampler(ctx->cso, PIPE_SHADER_FRAGMENT, 1, &ctx->sampler);
715
716 cso_set_depth_stencil_alpha(ctx->cso, &ctx->dsa_write_depthstencil);
717 set_depthstencil_fragment_shader(ctx, sampler_view->texture->target);
718 }
719 else if (blit_depth) {
720 cso_single_sampler(ctx->cso, PIPE_SHADER_FRAGMENT, 0, &ctx->sampler);
721 cso_set_depth_stencil_alpha(ctx->cso, &ctx->dsa_write_depth);
722 set_depth_fragment_shader(ctx, sampler_view->texture->target);
723 }
724 else if (blit_stencil) {
725 /* don't filter stencil */
726 ctx->sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
727 ctx->sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
728 cso_single_sampler(ctx->cso, PIPE_SHADER_FRAGMENT, 0, &ctx->sampler);
729
730 cso_set_depth_stencil_alpha(ctx->cso, &ctx->dsa_write_stencil);
731 set_stencil_fragment_shader(ctx, sampler_view->texture->target);
732 }
733 else { /* color */
734 cso_single_sampler(ctx->cso, PIPE_SHADER_FRAGMENT, 0, &ctx->sampler);
735 cso_set_depth_stencil_alpha(ctx->cso, &ctx->dsa_keep_depthstencil);
736 set_fragment_shader(ctx, writemask, sampler_view->texture->target);
737 }
738 cso_single_sampler_done(ctx->cso, PIPE_SHADER_FRAGMENT);
739
740 /* textures */
741 if (blit_depth && blit_stencil) {
742 /* Setup two samplers, one for depth and the other one for stencil. */
743 struct pipe_sampler_view templ;
744 struct pipe_sampler_view *views[2];
745
746 templ = *sampler_view;
747 templ.format = util_format_stencil_only(templ.format);
748 assert(templ.format != PIPE_FORMAT_NONE);
749
750 views[0] = sampler_view;
751 views[1] = pipe->create_sampler_view(pipe, views[0]->texture, &templ);
752 cso_set_sampler_views(ctx->cso, PIPE_SHADER_FRAGMENT, 2, views);
753
754 pipe_sampler_view_reference(&views[1], NULL);
755 }
756 else {
757 cso_set_sampler_views(ctx->cso, PIPE_SHADER_FRAGMENT, 1, &sampler_view);
758 }
759
760 /* viewport */
761 ctx->viewport.scale[0] = 0.5f * dst_surface->width;
762 ctx->viewport.scale[1] = 0.5f * dst_surface->height;
763 ctx->viewport.scale[2] = 0.5f;
764 ctx->viewport.scale[3] = 1.0f;
765 ctx->viewport.translate[0] = 0.5f * dst_surface->width;
766 ctx->viewport.translate[1] = 0.5f * dst_surface->height;
767 ctx->viewport.translate[2] = 0.5f;
768 ctx->viewport.translate[3] = 0.0f;
769 cso_set_viewport(ctx->cso, &ctx->viewport);
770
771 set_vertex_shader(ctx);
772 cso_set_geometry_shader_handle(ctx->cso, NULL);
773
774 /* drawing dest */
775 memset(&fb, 0, sizeof(fb));
776 fb.width = dst_surface->width;
777 fb.height = dst_surface->height;
778 if (blit_depth || blit_stencil) {
779 fb.zsbuf = dst_surface;
780 } else {
781 fb.nr_cbufs = 1;
782 fb.cbufs[0] = dst_surface;
783 }
784 cso_set_framebuffer(ctx->cso, &fb);
785
786 /* draw quad */
787 offset = setup_vertex_data_tex(ctx,
788 sampler_view->texture->target,
789 srcZ0 % 6,
790 (float) dstX0 / dst_surface->width * 2.0f - 1.0f,
791 (float) dstY0 / dst_surface->height * 2.0f - 1.0f,
792 (float) dstX1 / dst_surface->width * 2.0f - 1.0f,
793 (float) dstY1 / dst_surface->height * 2.0f - 1.0f,
794 s0, t0,
795 s1, t1,
796 z);
797
798 if (ctx->vbuf) {
799 util_draw_vertex_buffer(ctx->pipe, ctx->cso, ctx->vbuf,
800 cso_get_aux_vertex_buffer_slot(ctx->cso),
801 offset,
802 PIPE_PRIM_TRIANGLE_FAN,
803 4, /* verts */
804 2); /* attribs/vert */
805 }
806
807 /* restore state we changed */
808 cso_restore_blend(ctx->cso);
809 cso_restore_depth_stencil_alpha(ctx->cso);
810 cso_restore_rasterizer(ctx->cso);
811 cso_restore_sample_mask(ctx->cso);
812 cso_restore_samplers(ctx->cso, PIPE_SHADER_FRAGMENT);
813 cso_restore_sampler_views(ctx->cso, PIPE_SHADER_FRAGMENT);
814 cso_restore_viewport(ctx->cso);
815 cso_restore_framebuffer(ctx->cso);
816 cso_restore_fragment_shader(ctx->cso);
817 cso_restore_vertex_shader(ctx->cso);
818 cso_restore_geometry_shader(ctx->cso);
819 cso_restore_vertex_elements(ctx->cso);
820 cso_restore_aux_vertex_buffer_slot(ctx->cso);
821 cso_restore_stream_outputs(ctx->cso);
822 cso_restore_render_condition(ctx->cso);
823
824 pipe_sampler_view_reference(&sampler_view, NULL);
825 if (dst_surface != dst)
826 pipe_surface_reference(&dst_surface, NULL);
827 }
828
829
830 /**
831 * Copy pixel block from src sampler view to dst surface.
832 *
833 * The sampler view's first_level field indicates the source
834 * mipmap level to use.
835 *
836 * The sampler view's first_layer indicate the layer to use, but for
837 * cube maps it must point to the first face. Face is passed in src_face.
838 *
839 * The main advantage over util_blit_pixels is that it allows to specify swizzles in
840 * pipe_sampler_view::swizzle_?.
841 *
842 * But there is no control over blitting Z and/or stencil.
843 */
844 void
845 util_blit_pixels_tex(struct blit_state *ctx,
846 struct pipe_sampler_view *src_sampler_view,
847 int srcX0, int srcY0,
848 int srcX1, int srcY1,
849 unsigned src_face,
850 struct pipe_surface *dst,
851 int dstX0, int dstY0,
852 int dstX1, int dstY1,
853 float z, uint filter)
854 {
855 boolean normalized = src_sampler_view->texture->target != PIPE_TEXTURE_RECT;
856 struct pipe_framebuffer_state fb;
857 float s0, t0, s1, t1;
858 unsigned offset;
859 struct pipe_resource *tex = src_sampler_view->texture;
860
861 assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
862 filter == PIPE_TEX_MIPFILTER_LINEAR);
863
864 assert(tex);
865 assert(tex->width0 != 0);
866 assert(tex->height0 != 0);
867
868 s0 = (float) srcX0;
869 s1 = (float) srcX1;
870 t0 = (float) srcY0;
871 t1 = (float) srcY1;
872
873 if(normalized)
874 {
875 /* normalize according to the mipmap level's size */
876 int level = src_sampler_view->u.tex.first_level;
877 float w = (float) u_minify(tex->width0, level);
878 float h = (float) u_minify(tex->height0, level);
879 s0 /= w;
880 s1 /= w;
881 t0 /= h;
882 t1 /= h;
883 }
884
885 assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format,
886 PIPE_TEXTURE_2D,
887 dst->texture->nr_samples,
888 PIPE_BIND_RENDER_TARGET));
889
890 /* save state (restored below) */
891 cso_save_blend(ctx->cso);
892 cso_save_depth_stencil_alpha(ctx->cso);
893 cso_save_rasterizer(ctx->cso);
894 cso_save_sample_mask(ctx->cso);
895 cso_save_samplers(ctx->cso, PIPE_SHADER_FRAGMENT);
896 cso_save_sampler_views(ctx->cso, PIPE_SHADER_FRAGMENT);
897 cso_save_stream_outputs(ctx->cso);
898 cso_save_viewport(ctx->cso);
899 cso_save_framebuffer(ctx->cso);
900 cso_save_fragment_shader(ctx->cso);
901 cso_save_vertex_shader(ctx->cso);
902 cso_save_geometry_shader(ctx->cso);
903 cso_save_vertex_elements(ctx->cso);
904 cso_save_aux_vertex_buffer_slot(ctx->cso);
905
906 /* set misc state we care about */
907 cso_set_blend(ctx->cso, &ctx->blend_write_color);
908 cso_set_depth_stencil_alpha(ctx->cso, &ctx->dsa_keep_depthstencil);
909 cso_set_sample_mask(ctx->cso, ~0);
910 cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
911 cso_set_vertex_elements(ctx->cso, 2, ctx->velem);
912 cso_set_stream_outputs(ctx->cso, 0, NULL, 0);
913
914 /* sampler */
915 ctx->sampler.normalized_coords = normalized;
916 ctx->sampler.min_img_filter = filter;
917 ctx->sampler.mag_img_filter = filter;
918 cso_single_sampler(ctx->cso, PIPE_SHADER_FRAGMENT, 0, &ctx->sampler);
919 cso_single_sampler_done(ctx->cso, PIPE_SHADER_FRAGMENT);
920
921 /* viewport */
922 ctx->viewport.scale[0] = 0.5f * dst->width;
923 ctx->viewport.scale[1] = 0.5f * dst->height;
924 ctx->viewport.scale[2] = 0.5f;
925 ctx->viewport.scale[3] = 1.0f;
926 ctx->viewport.translate[0] = 0.5f * dst->width;
927 ctx->viewport.translate[1] = 0.5f * dst->height;
928 ctx->viewport.translate[2] = 0.5f;
929 ctx->viewport.translate[3] = 0.0f;
930 cso_set_viewport(ctx->cso, &ctx->viewport);
931
932 /* texture */
933 cso_set_sampler_views(ctx->cso, PIPE_SHADER_FRAGMENT, 1, &src_sampler_view);
934
935 /* shaders */
936 set_fragment_shader(ctx, TGSI_WRITEMASK_XYZW,
937 src_sampler_view->texture->target);
938 set_vertex_shader(ctx);
939 cso_set_geometry_shader_handle(ctx->cso, NULL);
940
941 /* drawing dest */
942 memset(&fb, 0, sizeof(fb));
943 fb.width = dst->width;
944 fb.height = dst->height;
945 fb.nr_cbufs = 1;
946 fb.cbufs[0] = dst;
947 cso_set_framebuffer(ctx->cso, &fb);
948
949 /* draw quad */
950 offset = setup_vertex_data_tex(ctx,
951 src_sampler_view->texture->target,
952 src_face,
953 (float) dstX0 / dst->width * 2.0f - 1.0f,
954 (float) dstY0 / dst->height * 2.0f - 1.0f,
955 (float) dstX1 / dst->width * 2.0f - 1.0f,
956 (float) dstY1 / dst->height * 2.0f - 1.0f,
957 s0, t0, s1, t1,
958 z);
959
960 util_draw_vertex_buffer(ctx->pipe, ctx->cso, ctx->vbuf,
961 cso_get_aux_vertex_buffer_slot(ctx->cso),
962 offset,
963 PIPE_PRIM_TRIANGLE_FAN,
964 4, /* verts */
965 2); /* attribs/vert */
966
967 /* restore state we changed */
968 cso_restore_blend(ctx->cso);
969 cso_restore_depth_stencil_alpha(ctx->cso);
970 cso_restore_rasterizer(ctx->cso);
971 cso_restore_sample_mask(ctx->cso);
972 cso_restore_samplers(ctx->cso, PIPE_SHADER_FRAGMENT);
973 cso_restore_sampler_views(ctx->cso, PIPE_SHADER_FRAGMENT);
974 cso_restore_viewport(ctx->cso);
975 cso_restore_framebuffer(ctx->cso);
976 cso_restore_fragment_shader(ctx->cso);
977 cso_restore_vertex_shader(ctx->cso);
978 cso_restore_geometry_shader(ctx->cso);
979 cso_restore_vertex_elements(ctx->cso);
980 cso_restore_aux_vertex_buffer_slot(ctx->cso);
981 cso_restore_stream_outputs(ctx->cso);
982 }