util: replace format equality test with compatibility test in blit code
[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_simple_shaders.h"
50
51 #include "cso_cache/cso_context.h"
52
53
54 struct blit_state
55 {
56 struct pipe_context *pipe;
57 struct cso_context *cso;
58
59 struct pipe_blend_state blend;
60 struct pipe_depth_stencil_alpha_state depthstencil_keep;
61 struct pipe_depth_stencil_alpha_state depthstencil_write;
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 enum pipe_texture_target internal_target;
67
68 void *vs;
69 void *fs[TGSI_WRITEMASK_XYZW + 1];
70 void *fs_depth;
71
72 struct pipe_resource *vbuf; /**< quad vertices */
73 unsigned vbuf_slot;
74
75 float vertices[4][2][4]; /**< vertex/texcoords for quad */
76 };
77
78
79 /**
80 * Create state object for blit.
81 * Intended to be created once and re-used for many blit() calls.
82 */
83 struct blit_state *
84 util_create_blit(struct pipe_context *pipe, struct cso_context *cso)
85 {
86 struct blit_state *ctx;
87 uint i;
88
89 ctx = CALLOC_STRUCT(blit_state);
90 if (!ctx)
91 return NULL;
92
93 ctx->pipe = pipe;
94 ctx->cso = cso;
95
96 /* disabled blending/masking */
97 memset(&ctx->blend, 0, sizeof(ctx->blend));
98 ctx->blend.rt[0].colormask = PIPE_MASK_RGBA;
99
100 /* no-op depth/stencil/alpha */
101 memset(&ctx->depthstencil_keep, 0, sizeof(ctx->depthstencil_keep));
102 memset(&ctx->depthstencil_write, 0, sizeof(ctx->depthstencil_write));
103 ctx->depthstencil_write.depth.enabled = 1;
104 ctx->depthstencil_write.depth.writemask = 1;
105 ctx->depthstencil_write.depth.func = PIPE_FUNC_ALWAYS;
106
107 /* rasterizer */
108 memset(&ctx->rasterizer, 0, sizeof(ctx->rasterizer));
109 ctx->rasterizer.cull_face = PIPE_FACE_NONE;
110 ctx->rasterizer.gl_rasterization_rules = 1;
111 ctx->rasterizer.depth_clip = 1;
112
113 /* samplers */
114 memset(&ctx->sampler, 0, sizeof(ctx->sampler));
115 ctx->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
116 ctx->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
117 ctx->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
118 ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
119 ctx->sampler.min_img_filter = 0; /* set later */
120 ctx->sampler.mag_img_filter = 0; /* set later */
121
122 /* vertex elements state */
123 memset(&ctx->velem[0], 0, sizeof(ctx->velem[0]) * 2);
124 for (i = 0; i < 2; i++) {
125 ctx->velem[i].src_offset = i * 4 * sizeof(float);
126 ctx->velem[i].instance_divisor = 0;
127 ctx->velem[i].vertex_buffer_index = 0;
128 ctx->velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
129 }
130
131 ctx->vbuf = NULL;
132
133 /* init vertex data that doesn't change */
134 for (i = 0; i < 4; i++) {
135 ctx->vertices[i][0][3] = 1.0f; /* w */
136 ctx->vertices[i][1][2] = 0.0f; /* r */
137 ctx->vertices[i][1][3] = 1.0f; /* q */
138 }
139
140 if(pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
141 ctx->internal_target = PIPE_TEXTURE_2D;
142 else
143 ctx->internal_target = PIPE_TEXTURE_RECT;
144
145 return ctx;
146 }
147
148
149 /**
150 * Destroy a blit context
151 */
152 void
153 util_destroy_blit(struct blit_state *ctx)
154 {
155 struct pipe_context *pipe = ctx->pipe;
156 unsigned i;
157
158 if (ctx->vs)
159 pipe->delete_vs_state(pipe, ctx->vs);
160
161 for (i = 0; i < Elements(ctx->fs); i++)
162 if (ctx->fs[i])
163 pipe->delete_fs_state(pipe, ctx->fs[i]);
164
165 if (ctx->fs_depth)
166 pipe->delete_fs_state(pipe, ctx->fs_depth);
167
168 pipe_resource_reference(&ctx->vbuf, NULL);
169
170 FREE(ctx);
171 }
172
173
174 /**
175 * Helper function to set the fragment shaders.
176 */
177 static INLINE void
178 set_fragment_shader(struct blit_state *ctx, uint writemask)
179 {
180 if (!ctx->fs[writemask])
181 ctx->fs[writemask] =
182 util_make_fragment_tex_shader_writemask(ctx->pipe, TGSI_TEXTURE_2D,
183 TGSI_INTERPOLATE_LINEAR,
184 writemask);
185
186 cso_set_fragment_shader_handle(ctx->cso, ctx->fs[writemask]);
187 }
188
189
190 /**
191 * Helper function to set the depthwrite shader.
192 */
193 static INLINE void
194 set_depth_fragment_shader(struct blit_state *ctx)
195 {
196 if (!ctx->fs_depth)
197 ctx->fs_depth =
198 util_make_fragment_tex_shader_writedepth(ctx->pipe, TGSI_TEXTURE_2D,
199 TGSI_INTERPOLATE_LINEAR);
200
201 cso_set_fragment_shader_handle(ctx->cso, ctx->fs_depth);
202 }
203
204
205 /**
206 * Helper function to set the vertex shader.
207 */
208 static INLINE void
209 set_vertex_shader(struct blit_state *ctx)
210 {
211 /* vertex shader - still required to provide the linkage between
212 * fragment shader input semantics and vertex_element/buffers.
213 */
214 if (!ctx->vs) {
215 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
216 TGSI_SEMANTIC_GENERIC };
217 const uint semantic_indexes[] = { 0, 0 };
218 ctx->vs = util_make_vertex_passthrough_shader(ctx->pipe, 2,
219 semantic_names,
220 semantic_indexes);
221 }
222
223 cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
224 }
225
226
227 /**
228 * Get offset of next free slot in vertex buffer for quad vertices.
229 */
230 static unsigned
231 get_next_slot( struct blit_state *ctx )
232 {
233 const unsigned max_slots = 4096 / sizeof ctx->vertices;
234
235 if (ctx->vbuf_slot >= max_slots)
236 util_blit_flush( ctx );
237
238 if (!ctx->vbuf) {
239 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
240 PIPE_BIND_VERTEX_BUFFER,
241 PIPE_USAGE_STREAM,
242 max_slots * sizeof ctx->vertices);
243 }
244
245 return ctx->vbuf_slot++ * sizeof ctx->vertices;
246 }
247
248
249
250
251 /**
252 * Setup vertex data for the textured quad we'll draw.
253 * Note: y=0=top
254 */
255 static unsigned
256 setup_vertex_data_tex(struct blit_state *ctx,
257 float x0, float y0, float x1, float y1,
258 float s0, float t0, float s1, float t1,
259 float z)
260 {
261 unsigned offset;
262
263 ctx->vertices[0][0][0] = x0;
264 ctx->vertices[0][0][1] = y0;
265 ctx->vertices[0][0][2] = z;
266 ctx->vertices[0][1][0] = s0; /*s*/
267 ctx->vertices[0][1][1] = t0; /*t*/
268
269 ctx->vertices[1][0][0] = x1;
270 ctx->vertices[1][0][1] = y0;
271 ctx->vertices[1][0][2] = z;
272 ctx->vertices[1][1][0] = s1; /*s*/
273 ctx->vertices[1][1][1] = t0; /*t*/
274
275 ctx->vertices[2][0][0] = x1;
276 ctx->vertices[2][0][1] = y1;
277 ctx->vertices[2][0][2] = z;
278 ctx->vertices[2][1][0] = s1;
279 ctx->vertices[2][1][1] = t1;
280
281 ctx->vertices[3][0][0] = x0;
282 ctx->vertices[3][0][1] = y1;
283 ctx->vertices[3][0][2] = z;
284 ctx->vertices[3][1][0] = s0;
285 ctx->vertices[3][1][1] = t1;
286
287 offset = get_next_slot( ctx );
288
289 if (ctx->vbuf) {
290 pipe_buffer_write_nooverlap(ctx->pipe, ctx->vbuf,
291 offset, sizeof(ctx->vertices), ctx->vertices);
292 }
293
294 return offset;
295 }
296
297
298 /**
299 * \return TRUE if two regions overlap, FALSE otherwise
300 */
301 static boolean
302 regions_overlap(int srcX0, int srcY0,
303 int srcX1, int srcY1,
304 int dstX0, int dstY0,
305 int dstX1, int dstY1)
306 {
307 if (MAX2(srcX0, srcX1) < MIN2(dstX0, dstX1))
308 return FALSE; /* src completely left of dst */
309
310 if (MAX2(dstX0, dstX1) < MIN2(srcX0, srcX1))
311 return FALSE; /* dst completely left of src */
312
313 if (MAX2(srcY0, srcY1) < MIN2(dstY0, dstY1))
314 return FALSE; /* src completely above dst */
315
316 if (MAX2(dstY0, dstY1) < MIN2(srcY0, srcY1))
317 return FALSE; /* dst completely above src */
318
319 return TRUE; /* some overlap */
320 }
321
322
323 /**
324 * Can we blit from src format to dest format with a simple copy?
325 */
326 static boolean
327 formats_compatible(enum pipe_format src_format,
328 enum pipe_format dst_format)
329 {
330 if (src_format == dst_format) {
331 return TRUE;
332 }
333 else {
334 const struct util_format_description *src_desc =
335 util_format_description(src_format);
336 const struct util_format_description *dst_desc =
337 util_format_description(dst_format);
338 return util_is_format_compatible(src_desc, dst_desc);
339 }
340 }
341
342
343 /**
344 * Copy pixel block from src surface to dst surface.
345 * Overlapping regions are acceptable.
346 * Flipping and stretching are supported.
347 * \param filter one of PIPE_TEX_MIPFILTER_NEAREST/LINEAR
348 * \param writemask controls which channels in the dest surface are sourced
349 * from the src surface. Disabled channels are sourced
350 * from (0,0,0,1).
351 * XXX need some control over blitting stencil.
352 */
353 void
354 util_blit_pixels_writemask(struct blit_state *ctx,
355 struct pipe_resource *src_tex,
356 unsigned src_level,
357 int srcX0, int srcY0,
358 int srcX1, int srcY1,
359 int srcZ0,
360 struct pipe_surface *dst,
361 int dstX0, int dstY0,
362 int dstX1, int dstY1,
363 float z, uint filter,
364 uint writemask)
365 {
366 struct pipe_context *pipe = ctx->pipe;
367 struct pipe_screen *screen = pipe->screen;
368 enum pipe_format src_format, dst_format;
369 struct pipe_sampler_view *sampler_view = NULL;
370 struct pipe_sampler_view sv_templ;
371 struct pipe_surface *dst_surface;
372 struct pipe_framebuffer_state fb;
373 const int srcW = abs(srcX1 - srcX0);
374 const int srcH = abs(srcY1 - srcY0);
375 unsigned offset;
376 boolean overlap, dst_is_depth;
377 float s0, t0, s1, t1;
378 boolean normalized;
379
380 assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
381 filter == PIPE_TEX_MIPFILTER_LINEAR);
382
383 assert(src_level <= src_tex->last_level);
384
385 /* do the regions overlap? */
386 overlap = src_tex == dst->texture &&
387 dst->u.tex.level == src_level &&
388 dst->u.tex.first_layer == srcZ0 &&
389 regions_overlap(srcX0, srcY0, srcX1, srcY1,
390 dstX0, dstY0, dstX1, dstY1);
391
392 src_format = util_format_linear(src_tex->format);
393 dst_format = util_format_linear(dst->format);
394
395 /*
396 * Check for simple case: no format conversion, no flipping, no stretching,
397 * no overlapping.
398 * Filter mode should not matter since there's no stretching.
399 */
400 if (formats_compatible(src_format, dst_format) &&
401 srcX0 < srcX1 &&
402 dstX0 < dstX1 &&
403 srcY0 < srcY1 &&
404 dstY0 < dstY1 &&
405 (dstX1 - dstX0) == (srcX1 - srcX0) &&
406 (dstY1 - dstY0) == (srcY1 - srcY0) &&
407 !overlap) {
408 struct pipe_box src_box;
409 src_box.x = srcX0;
410 src_box.y = srcY0;
411 src_box.z = srcZ0;
412 src_box.width = srcW;
413 src_box.height = srcH;
414 src_box.depth = 1;
415 pipe->resource_copy_region(pipe,
416 dst->texture, dst->u.tex.level,
417 dstX0, dstY0, dst->u.tex.first_layer,/* dest */
418 src_tex, src_level,
419 &src_box);
420 return;
421 }
422
423 if (dst_format == dst->format) {
424 dst_surface = dst;
425 } else {
426 struct pipe_surface templ = *dst;
427 templ.format = dst_format;
428 dst_surface = pipe->create_surface(pipe, dst->texture, &templ);
429 }
430
431 /* Create a temporary texture when src and dest alias or when src
432 * is anything other than a 2d texture.
433 * XXX should just use appropriate shader to access 1d / 3d slice / cube face,
434 * much like the u_blitter code does (should be pretty trivial).
435 *
436 * This can still be improved upon.
437 */
438 if ((src_tex == dst_surface->texture &&
439 dst_surface->u.tex.level == src_level &&
440 dst_surface->u.tex.first_layer == srcZ0) ||
441 (src_tex->target != PIPE_TEXTURE_2D &&
442 src_tex->target != PIPE_TEXTURE_2D &&
443 src_tex->target != PIPE_TEXTURE_RECT))
444 {
445 struct pipe_resource texTemp;
446 struct pipe_resource *tex;
447 struct pipe_sampler_view sv_templ;
448 struct pipe_box src_box;
449 const int srcLeft = MIN2(srcX0, srcX1);
450 const int srcTop = MIN2(srcY0, srcY1);
451
452 if (srcLeft != srcX0) {
453 /* left-right flip */
454 int tmp = dstX0;
455 dstX0 = dstX1;
456 dstX1 = tmp;
457 }
458
459 if (srcTop != srcY0) {
460 /* up-down flip */
461 int tmp = dstY0;
462 dstY0 = dstY1;
463 dstY1 = tmp;
464 }
465
466 /* create temp texture */
467 memset(&texTemp, 0, sizeof(texTemp));
468 texTemp.target = ctx->internal_target;
469 texTemp.format = src_format;
470 texTemp.last_level = 0;
471 texTemp.width0 = srcW;
472 texTemp.height0 = srcH;
473 texTemp.depth0 = 1;
474 texTemp.array_size = 1;
475 texTemp.bind = PIPE_BIND_SAMPLER_VIEW;
476
477 tex = screen->resource_create(screen, &texTemp);
478 if (!tex)
479 return;
480
481 src_box.x = srcLeft;
482 src_box.y = srcTop;
483 src_box.z = srcZ0;
484 src_box.width = srcW;
485 src_box.height = srcH;
486 src_box.depth = 1;
487 /* load temp texture */
488 pipe->resource_copy_region(pipe,
489 tex, 0, 0, 0, 0, /* dest */
490 src_tex, src_level, &src_box);
491
492 normalized = tex->target != PIPE_TEXTURE_RECT;
493 if(normalized) {
494 s0 = 0.0f;
495 s1 = 1.0f;
496 t0 = 0.0f;
497 t1 = 1.0f;
498 }
499 else {
500 s0 = 0;
501 s1 = srcW;
502 t0 = 0;
503 t1 = srcH;
504 }
505
506 u_sampler_view_default_template(&sv_templ, tex, tex->format);
507 sampler_view = pipe->create_sampler_view(pipe, tex, &sv_templ);
508
509 if (!sampler_view) {
510 pipe_resource_reference(&tex, NULL);
511 return;
512 }
513 pipe_resource_reference(&tex, NULL);
514 }
515 else {
516 u_sampler_view_default_template(&sv_templ, src_tex, src_format);
517 sampler_view = pipe->create_sampler_view(pipe, src_tex, &sv_templ);
518
519 if (!sampler_view) {
520 return;
521 }
522
523 s0 = srcX0;
524 s1 = srcX1;
525 t0 = srcY0;
526 t1 = srcY1;
527 normalized = sampler_view->texture->target != PIPE_TEXTURE_RECT;
528 if(normalized)
529 {
530 s0 /= (float)(u_minify(sampler_view->texture->width0, src_level));
531 s1 /= (float)(u_minify(sampler_view->texture->width0, src_level));
532 t0 /= (float)(u_minify(sampler_view->texture->height0, src_level));
533 t1 /= (float)(u_minify(sampler_view->texture->height0, src_level));
534 }
535 }
536
537 dst_is_depth = util_format_is_depth_or_stencil(dst_format);
538
539 assert(screen->is_format_supported(screen, sampler_view->format, ctx->internal_target,
540 sampler_view->texture->nr_samples,
541 PIPE_BIND_SAMPLER_VIEW));
542 assert(screen->is_format_supported(screen, dst_format, ctx->internal_target,
543 dst_surface->texture->nr_samples,
544 dst_is_depth ? PIPE_BIND_DEPTH_STENCIL :
545 PIPE_BIND_RENDER_TARGET));
546 /* save state (restored below) */
547 cso_save_blend(ctx->cso);
548 cso_save_depth_stencil_alpha(ctx->cso);
549 cso_save_rasterizer(ctx->cso);
550 cso_save_samplers(ctx->cso);
551 cso_save_fragment_sampler_views(ctx->cso);
552 cso_save_stream_outputs(ctx->cso);
553 cso_save_viewport(ctx->cso);
554 cso_save_framebuffer(ctx->cso);
555 cso_save_fragment_shader(ctx->cso);
556 cso_save_vertex_shader(ctx->cso);
557 cso_save_geometry_shader(ctx->cso);
558 cso_save_vertex_elements(ctx->cso);
559 cso_save_vertex_buffers(ctx->cso);
560
561 /* set misc state we care about */
562 cso_set_blend(ctx->cso, &ctx->blend);
563 cso_set_depth_stencil_alpha(ctx->cso,
564 dst_is_depth ? &ctx->depthstencil_write :
565 &ctx->depthstencil_keep);
566 cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
567 cso_set_vertex_elements(ctx->cso, 2, ctx->velem);
568 cso_set_stream_outputs(ctx->cso, 0, NULL, 0);
569
570 /* sampler */
571 ctx->sampler.normalized_coords = normalized;
572 ctx->sampler.min_img_filter = filter;
573 ctx->sampler.mag_img_filter = filter;
574 ctx->sampler.min_lod = src_level;
575 ctx->sampler.max_lod = src_level;
576 cso_single_sampler(ctx->cso, 0, &ctx->sampler);
577 cso_single_sampler_done(ctx->cso);
578
579 /* viewport */
580 ctx->viewport.scale[0] = 0.5f * dst_surface->width;
581 ctx->viewport.scale[1] = 0.5f * dst_surface->height;
582 ctx->viewport.scale[2] = 0.5f;
583 ctx->viewport.scale[3] = 1.0f;
584 ctx->viewport.translate[0] = 0.5f * dst_surface->width;
585 ctx->viewport.translate[1] = 0.5f * dst_surface->height;
586 ctx->viewport.translate[2] = 0.5f;
587 ctx->viewport.translate[3] = 0.0f;
588 cso_set_viewport(ctx->cso, &ctx->viewport);
589
590 /* texture */
591 cso_set_fragment_sampler_views(ctx->cso, 1, &sampler_view);
592
593 /* shaders */
594 if (dst_is_depth) {
595 set_depth_fragment_shader(ctx);
596 } else {
597 set_fragment_shader(ctx, writemask);
598 }
599 set_vertex_shader(ctx);
600 cso_set_geometry_shader_handle(ctx->cso, NULL);
601
602 /* drawing dest */
603 memset(&fb, 0, sizeof(fb));
604 fb.width = dst_surface->width;
605 fb.height = dst_surface->height;
606 if (dst_is_depth) {
607 fb.zsbuf = dst_surface;
608 } else {
609 fb.nr_cbufs = 1;
610 fb.cbufs[0] = dst_surface;
611 }
612 cso_set_framebuffer(ctx->cso, &fb);
613
614 /* draw quad */
615 offset = setup_vertex_data_tex(ctx,
616 (float) dstX0 / dst_surface->width * 2.0f - 1.0f,
617 (float) dstY0 / dst_surface->height * 2.0f - 1.0f,
618 (float) dstX1 / dst_surface->width * 2.0f - 1.0f,
619 (float) dstY1 / dst_surface->height * 2.0f - 1.0f,
620 s0, t0,
621 s1, t1,
622 z);
623
624 if (ctx->vbuf) {
625 util_draw_vertex_buffer(ctx->pipe, ctx->cso, ctx->vbuf, offset,
626 PIPE_PRIM_TRIANGLE_FAN,
627 4, /* verts */
628 2); /* attribs/vert */
629 }
630
631 /* restore state we changed */
632 cso_restore_blend(ctx->cso);
633 cso_restore_depth_stencil_alpha(ctx->cso);
634 cso_restore_rasterizer(ctx->cso);
635 cso_restore_samplers(ctx->cso);
636 cso_restore_fragment_sampler_views(ctx->cso);
637 cso_restore_viewport(ctx->cso);
638 cso_restore_framebuffer(ctx->cso);
639 cso_restore_fragment_shader(ctx->cso);
640 cso_restore_vertex_shader(ctx->cso);
641 cso_restore_geometry_shader(ctx->cso);
642 cso_restore_vertex_elements(ctx->cso);
643 cso_restore_vertex_buffers(ctx->cso);
644 cso_restore_stream_outputs(ctx->cso);
645
646 pipe_sampler_view_reference(&sampler_view, NULL);
647 if (dst_surface != dst)
648 pipe_surface_reference(&dst_surface, NULL);
649 }
650
651
652 void
653 util_blit_pixels(struct blit_state *ctx,
654 struct pipe_resource *src_tex,
655 unsigned src_level,
656 int srcX0, int srcY0,
657 int srcX1, int srcY1,
658 int srcZ,
659 struct pipe_surface *dst,
660 int dstX0, int dstY0,
661 int dstX1, int dstY1,
662 float z, uint filter )
663 {
664 util_blit_pixels_writemask( ctx, src_tex,
665 src_level,
666 srcX0, srcY0,
667 srcX1, srcY1,
668 srcZ,
669 dst,
670 dstX0, dstY0,
671 dstX1, dstY1,
672 z, filter,
673 TGSI_WRITEMASK_XYZW );
674 }
675
676
677 /* Release vertex buffer at end of frame to avoid synchronous
678 * rendering.
679 */
680 void util_blit_flush( struct blit_state *ctx )
681 {
682 pipe_resource_reference(&ctx->vbuf, NULL);
683 ctx->vbuf_slot = 0;
684 }
685
686
687
688 /**
689 * Copy pixel block from src texture to dst surface.
690 * The sampler view's first_level field indicates the source
691 * mipmap level to use.
692 * XXX need some control over blitting Z and/or stencil.
693 */
694 void
695 util_blit_pixels_tex(struct blit_state *ctx,
696 struct pipe_sampler_view *src_sampler_view,
697 int srcX0, int srcY0,
698 int srcX1, int srcY1,
699 struct pipe_surface *dst,
700 int dstX0, int dstY0,
701 int dstX1, int dstY1,
702 float z, uint filter)
703 {
704 boolean normalized = src_sampler_view->texture->target != PIPE_TEXTURE_RECT;
705 struct pipe_framebuffer_state fb;
706 float s0, t0, s1, t1;
707 unsigned offset;
708 struct pipe_resource *tex = src_sampler_view->texture;
709
710 assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
711 filter == PIPE_TEX_MIPFILTER_LINEAR);
712
713 assert(tex);
714 assert(tex->width0 != 0);
715 assert(tex->height0 != 0);
716
717 s0 = srcX0;
718 s1 = srcX1;
719 t0 = srcY0;
720 t1 = srcY1;
721
722 if(normalized)
723 {
724 /* normalize according to the mipmap level's size */
725 int level = src_sampler_view->u.tex.first_level;
726 float w = (float) u_minify(tex->width0, level);
727 float h = (float) u_minify(tex->height0, level);
728 s0 /= w;
729 s1 /= w;
730 t0 /= h;
731 t1 /= h;
732 }
733
734 assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format,
735 PIPE_TEXTURE_2D,
736 dst->texture->nr_samples,
737 PIPE_BIND_RENDER_TARGET));
738
739 /* save state (restored below) */
740 cso_save_blend(ctx->cso);
741 cso_save_depth_stencil_alpha(ctx->cso);
742 cso_save_rasterizer(ctx->cso);
743 cso_save_samplers(ctx->cso);
744 cso_save_fragment_sampler_views(ctx->cso);
745 cso_save_stream_outputs(ctx->cso);
746 cso_save_viewport(ctx->cso);
747 cso_save_framebuffer(ctx->cso);
748 cso_save_fragment_shader(ctx->cso);
749 cso_save_vertex_shader(ctx->cso);
750 cso_save_geometry_shader(ctx->cso);
751 cso_save_vertex_elements(ctx->cso);
752 cso_save_vertex_buffers(ctx->cso);
753
754 /* set misc state we care about */
755 cso_set_blend(ctx->cso, &ctx->blend);
756 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil_keep);
757 cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
758 cso_set_vertex_elements(ctx->cso, 2, ctx->velem);
759 cso_set_stream_outputs(ctx->cso, 0, NULL, 0);
760
761 /* sampler */
762 ctx->sampler.normalized_coords = normalized;
763 ctx->sampler.min_img_filter = filter;
764 ctx->sampler.mag_img_filter = filter;
765 cso_single_sampler(ctx->cso, 0, &ctx->sampler);
766 cso_single_sampler_done(ctx->cso);
767
768 /* viewport */
769 ctx->viewport.scale[0] = 0.5f * dst->width;
770 ctx->viewport.scale[1] = 0.5f * dst->height;
771 ctx->viewport.scale[2] = 0.5f;
772 ctx->viewport.scale[3] = 1.0f;
773 ctx->viewport.translate[0] = 0.5f * dst->width;
774 ctx->viewport.translate[1] = 0.5f * dst->height;
775 ctx->viewport.translate[2] = 0.5f;
776 ctx->viewport.translate[3] = 0.0f;
777 cso_set_viewport(ctx->cso, &ctx->viewport);
778
779 /* texture */
780 cso_set_fragment_sampler_views(ctx->cso, 1, &src_sampler_view);
781
782 /* shaders */
783 set_fragment_shader(ctx, TGSI_WRITEMASK_XYZW);
784 set_vertex_shader(ctx);
785 cso_set_geometry_shader_handle(ctx->cso, NULL);
786
787 /* drawing dest */
788 memset(&fb, 0, sizeof(fb));
789 fb.width = dst->width;
790 fb.height = dst->height;
791 fb.nr_cbufs = 1;
792 fb.cbufs[0] = dst;
793 cso_set_framebuffer(ctx->cso, &fb);
794
795 /* draw quad */
796 offset = setup_vertex_data_tex(ctx,
797 (float) dstX0 / dst->width * 2.0f - 1.0f,
798 (float) dstY0 / dst->height * 2.0f - 1.0f,
799 (float) dstX1 / dst->width * 2.0f - 1.0f,
800 (float) dstY1 / dst->height * 2.0f - 1.0f,
801 s0, t0, s1, t1,
802 z);
803
804 util_draw_vertex_buffer(ctx->pipe, ctx->cso,
805 ctx->vbuf, offset,
806 PIPE_PRIM_TRIANGLE_FAN,
807 4, /* verts */
808 2); /* attribs/vert */
809
810 /* restore state we changed */
811 cso_restore_blend(ctx->cso);
812 cso_restore_depth_stencil_alpha(ctx->cso);
813 cso_restore_rasterizer(ctx->cso);
814 cso_restore_samplers(ctx->cso);
815 cso_restore_fragment_sampler_views(ctx->cso);
816 cso_restore_viewport(ctx->cso);
817 cso_restore_framebuffer(ctx->cso);
818 cso_restore_fragment_shader(ctx->cso);
819 cso_restore_vertex_shader(ctx->cso);
820 cso_restore_geometry_shader(ctx->cso);
821 cso_restore_vertex_elements(ctx->cso);
822 cso_restore_vertex_buffers(ctx->cso);
823 cso_restore_stream_outputs(ctx->cso);
824 }