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