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