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