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