u_blit.c revision d5062fb3a315c46d77d5c954a3e3c14be1907d33
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                           unsigned src_level,
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(src_level <= src_tex->last_level);
320
321   /* do the regions overlap? */
322   overlap = src_tex == dst->texture &&
323             dst->u.tex.level == src_level &&
324             dst->u.tex.first_layer == srcZ0 &&
325      regions_overlap(srcX0, srcY0, srcX1, srcY1,
326                      dstX0, dstY0, dstX1, dstY1);
327
328   /*
329    * Check for simple case:  no format conversion, no flipping, no stretching,
330    * no overlapping.
331    * Filter mode should not matter since there's no stretching.
332    */
333   if (dst->format == src_tex->format &&
334       srcX0 < srcX1 &&
335       dstX0 < dstX1 &&
336       srcY0 < srcY1 &&
337       dstY0 < dstY1 &&
338       (dstX1 - dstX0) == (srcX1 - srcX0) &&
339       (dstY1 - dstY0) == (srcY1 - srcY0) &&
340       !overlap) {
341      struct pipe_box src_box;
342      src_box.x = srcX0;
343      src_box.y = srcY0;
344      src_box.z = srcZ0;
345      src_box.width = srcW;
346      src_box.height = srcH;
347      src_box.depth = 1;
348      pipe->resource_copy_region(pipe,
349                                 dst->texture, dst->u.tex.level,
350                                 dstX0, dstY0, dst->u.tex.first_layer,/* dest */
351                                 src_tex, src_level,
352                                 &src_box);
353       return;
354   }
355
356   /* Create a temporary texture when src and dest alias or when src
357    * is anything other than a 2d texture.
358    * XXX should just use appropriate shader to access 1d / 3d slice / cube face,
359    * much like the u_blitter code does (should be pretty trivial).
360    *
361    * This can still be improved upon.
362    */
363   if ((src_tex == dst->texture &&
364       dst->u.tex.level == src_level &&
365       dst->u.tex.first_layer == srcZ0) ||
366       (src_tex->target != PIPE_TEXTURE_2D &&
367       src_tex->target != PIPE_TEXTURE_2D &&
368       src_tex->target != PIPE_TEXTURE_RECT))
369   {
370      struct pipe_resource texTemp;
371      struct pipe_resource *tex;
372      struct pipe_sampler_view sv_templ;
373      struct pipe_box src_box;
374      const int srcLeft = MIN2(srcX0, srcX1);
375      const int srcTop = MIN2(srcY0, srcY1);
376
377      if (srcLeft != srcX0) {
378         /* left-right flip */
379         int tmp = dstX0;
380         dstX0 = dstX1;
381         dstX1 = tmp;
382      }
383
384      if (srcTop != srcY0) {
385         /* up-down flip */
386         int tmp = dstY0;
387         dstY0 = dstY1;
388         dstY1 = tmp;
389      }
390
391      /* create temp texture */
392      memset(&texTemp, 0, sizeof(texTemp));
393      texTemp.target = ctx->internal_target;
394      texTemp.format = src_tex->format;
395      texTemp.last_level = 0;
396      texTemp.width0 = srcW;
397      texTemp.height0 = srcH;
398      texTemp.depth0 = 1;
399      texTemp.array_size = 1;
400      texTemp.bind = PIPE_BIND_SAMPLER_VIEW;
401
402      tex = screen->resource_create(screen, &texTemp);
403      if (!tex)
404         return;
405
406      src_box.x = srcLeft;
407      src_box.y = srcTop;
408      src_box.z = srcZ0;
409      src_box.width = srcW;
410      src_box.height = srcH;
411      src_box.depth = 1;
412      /* load temp texture */
413      pipe->resource_copy_region(pipe,
414                                 tex, 0, 0, 0, 0,  /* dest */
415                                 src_tex, src_level, &src_box);
416
417      normalized = tex->target != PIPE_TEXTURE_RECT;
418      if(normalized) {
419         s0 = 0.0f;
420         s1 = 1.0f;
421         t0 = 0.0f;
422         t1 = 1.0f;
423      }
424      else {
425         s0 = 0;
426         s1 = srcW;
427         t0 = 0;
428         t1 = srcH;
429      }
430
431      u_sampler_view_default_template(&sv_templ, tex, tex->format);
432      sampler_view = pipe->create_sampler_view(pipe, tex, &sv_templ);
433
434      if (!sampler_view) {
435         pipe_resource_reference(&tex, NULL);
436         return;
437      }
438      pipe_resource_reference(&tex, NULL);
439   }
440   else {
441      u_sampler_view_default_template(&sv_templ, src_tex, src_tex->format);
442      sampler_view = pipe->create_sampler_view(pipe, src_tex, &sv_templ);
443
444      if (!sampler_view) {
445         return;
446      }
447
448      s0 = srcX0;
449      s1 = srcX1;
450      t0 = srcY0;
451      t1 = srcY1;
452      normalized = sampler_view->texture->target != PIPE_TEXTURE_RECT;
453      if(normalized)
454      {
455         s0 /= (float)(u_minify(sampler_view->texture->width0, src_level));
456         s1 /= (float)(u_minify(sampler_view->texture->width0, src_level));
457         t0 /= (float)(u_minify(sampler_view->texture->height0, src_level));
458         t1 /= (float)(u_minify(sampler_view->texture->height0, src_level));
459      }
460   }
461
462   dst_is_depth = util_format_is_depth_or_stencil(dst->format);
463
464   assert(screen->is_format_supported(screen, sampler_view->format, ctx->internal_target,
465                                      sampler_view->texture->nr_samples,
466                                      PIPE_BIND_SAMPLER_VIEW, 0));
467   assert(screen->is_format_supported(screen, dst->format, ctx->internal_target,
468                                      dst->texture->nr_samples,
469                                      dst_is_depth ? PIPE_BIND_DEPTH_STENCIL :
470                                                     PIPE_BIND_RENDER_TARGET, 0));
471   /* save state (restored below) */
472   cso_save_blend(ctx->cso);
473   cso_save_depth_stencil_alpha(ctx->cso);
474   cso_save_rasterizer(ctx->cso);
475   cso_save_samplers(ctx->cso);
476   cso_save_fragment_sampler_views(ctx->cso);
477   cso_save_viewport(ctx->cso);
478   cso_save_framebuffer(ctx->cso);
479   cso_save_fragment_shader(ctx->cso);
480   cso_save_vertex_shader(ctx->cso);
481   cso_save_clip(ctx->cso);
482   cso_save_vertex_elements(ctx->cso);
483   cso_save_vertex_buffers(ctx->cso);
484
485   /* set misc state we care about */
486   cso_set_blend(ctx->cso, &ctx->blend);
487   cso_set_depth_stencil_alpha(ctx->cso,
488                               dst_is_depth ? &ctx->depthstencil_write :
489                                              &ctx->depthstencil_keep);
490   cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
491   cso_set_clip(ctx->cso, &ctx->clip);
492   cso_set_vertex_elements(ctx->cso, 2, ctx->velem);
493
494   /* sampler */
495   ctx->sampler.normalized_coords = normalized;
496   ctx->sampler.min_img_filter = filter;
497   ctx->sampler.mag_img_filter = filter;
498   ctx->sampler.min_lod = src_level;
499   ctx->sampler.max_lod = src_level;
500   cso_single_sampler(ctx->cso, 0, &ctx->sampler);
501   cso_single_sampler_done(ctx->cso);
502
503   /* viewport */
504   ctx->viewport.scale[0] = 0.5f * dst->width;
505   ctx->viewport.scale[1] = 0.5f * dst->height;
506   ctx->viewport.scale[2] = 0.5f;
507   ctx->viewport.scale[3] = 1.0f;
508   ctx->viewport.translate[0] = 0.5f * dst->width;
509   ctx->viewport.translate[1] = 0.5f * dst->height;
510   ctx->viewport.translate[2] = 0.5f;
511   ctx->viewport.translate[3] = 0.0f;
512   cso_set_viewport(ctx->cso, &ctx->viewport);
513
514   /* texture */
515   cso_set_fragment_sampler_views(ctx->cso, 1, &sampler_view);
516
517   /* shaders */
518   if (dst_is_depth) {
519      if (ctx->fs_depth == NULL)
520         ctx->fs_depth =
521            util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_2D,
522                                                     TGSI_INTERPOLATE_LINEAR);
523
524      cso_set_fragment_shader_handle(ctx->cso, ctx->fs_depth);
525   } else {
526      if (ctx->fs[writemask] == NULL)
527         ctx->fs[writemask] =
528            util_make_fragment_tex_shader_writemask(pipe, TGSI_TEXTURE_2D,
529                                                    TGSI_INTERPOLATE_LINEAR,
530                                                    writemask);
531
532      cso_set_fragment_shader_handle(ctx->cso, ctx->fs[writemask]);
533   }
534   cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
535
536   /* drawing dest */
537   memset(&fb, 0, sizeof(fb));
538   fb.width = dst->width;
539   fb.height = dst->height;
540   if (dst_is_depth) {
541      fb.zsbuf = dst;
542   } else {
543      fb.nr_cbufs = 1;
544      fb.cbufs[0] = dst;
545   }
546   cso_set_framebuffer(ctx->cso, &fb);
547
548   /* draw quad */
549   offset = setup_vertex_data_tex(ctx,
550                                  (float) dstX0 / dst->width * 2.0f - 1.0f,
551                                  (float) dstY0 / dst->height * 2.0f - 1.0f,
552                                  (float) dstX1 / dst->width * 2.0f - 1.0f,
553                                  (float) dstY1 / dst->height * 2.0f - 1.0f,
554                                  s0, t0,
555                                  s1, t1,
556                                  z);
557
558   util_draw_vertex_buffer(ctx->pipe, ctx->cso, ctx->vbuf, offset,
559                           PIPE_PRIM_TRIANGLE_FAN,
560                           4,  /* verts */
561                           2); /* attribs/vert */
562
563   /* restore state we changed */
564   cso_restore_blend(ctx->cso);
565   cso_restore_depth_stencil_alpha(ctx->cso);
566   cso_restore_rasterizer(ctx->cso);
567   cso_restore_samplers(ctx->cso);
568   cso_restore_fragment_sampler_views(ctx->cso);
569   cso_restore_viewport(ctx->cso);
570   cso_restore_framebuffer(ctx->cso);
571   cso_restore_fragment_shader(ctx->cso);
572   cso_restore_vertex_shader(ctx->cso);
573   cso_restore_clip(ctx->cso);
574   cso_restore_vertex_elements(ctx->cso);
575   cso_restore_vertex_buffers(ctx->cso);
576
577   pipe_sampler_view_reference(&sampler_view, NULL);
578}
579
580
581void
582util_blit_pixels(struct blit_state *ctx,
583                 struct pipe_resource *src_tex,
584                 unsigned src_level,
585                 int srcX0, int srcY0,
586                 int srcX1, int srcY1,
587                 int srcZ,
588                 struct pipe_surface *dst,
589                 int dstX0, int dstY0,
590                 int dstX1, int dstY1,
591                 float z, uint filter )
592{
593   util_blit_pixels_writemask( ctx, src_tex,
594                               src_level,
595                               srcX0, srcY0,
596                               srcX1, srcY1,
597                               srcZ,
598                               dst,
599                               dstX0, dstY0,
600                               dstX1, dstY1,
601                               z, filter,
602                               TGSI_WRITEMASK_XYZW );
603}
604
605
606/* Release vertex buffer at end of frame to avoid synchronous
607 * rendering.
608 */
609void util_blit_flush( struct blit_state *ctx )
610{
611   pipe_resource_reference(&ctx->vbuf, NULL);
612   ctx->vbuf_slot = 0;
613}
614
615
616
617/**
618 * Copy pixel block from src texture to dst surface.
619 *
620 * XXX Should support selection of level.
621 * XXX need some control over blitting Z and/or stencil.
622 */
623void
624util_blit_pixels_tex(struct blit_state *ctx,
625                     struct pipe_sampler_view *src_sampler_view,
626                     int srcX0, int srcY0,
627                     int srcX1, int srcY1,
628                     struct pipe_surface *dst,
629                     int dstX0, int dstY0,
630                     int dstX1, int dstY1,
631                     float z, uint filter)
632{
633   boolean normalized = src_sampler_view->texture->target != PIPE_TEXTURE_RECT;
634   struct pipe_framebuffer_state fb;
635   float s0, t0, s1, t1;
636   unsigned offset;
637   struct pipe_resource *tex = src_sampler_view->texture;
638
639   assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
640          filter == PIPE_TEX_MIPFILTER_LINEAR);
641
642   assert(tex);
643   assert(tex->width0 != 0);
644   assert(tex->height0 != 0);
645
646   s0 = srcX0;
647   s1 = srcX1;
648   t0 = srcY0;
649   t1 = srcY1;
650
651   if(normalized)
652   {
653      s0 /= (float)tex->width0;
654      s1 /= (float)tex->width0;
655      t0 /= (float)tex->height0;
656      t1 /= (float)tex->height0;
657   }
658
659   assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format,
660                                                 PIPE_TEXTURE_2D,
661                                                 dst->texture->nr_samples,
662                                                 PIPE_BIND_RENDER_TARGET,
663                                                 0));
664
665   /* save state (restored below) */
666   cso_save_blend(ctx->cso);
667   cso_save_depth_stencil_alpha(ctx->cso);
668   cso_save_rasterizer(ctx->cso);
669   cso_save_samplers(ctx->cso);
670   cso_save_fragment_sampler_views(ctx->cso);
671   cso_save_viewport(ctx->cso);
672   cso_save_framebuffer(ctx->cso);
673   cso_save_fragment_shader(ctx->cso);
674   cso_save_vertex_shader(ctx->cso);
675   cso_save_clip(ctx->cso);
676   cso_save_vertex_elements(ctx->cso);
677   cso_save_vertex_buffers(ctx->cso);
678
679   /* set misc state we care about */
680   cso_set_blend(ctx->cso, &ctx->blend);
681   cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil_keep);
682   cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
683   cso_set_clip(ctx->cso, &ctx->clip);
684   cso_set_vertex_elements(ctx->cso, 2, ctx->velem);
685
686   /* sampler */
687   ctx->sampler.normalized_coords = normalized;
688   ctx->sampler.min_img_filter = filter;
689   ctx->sampler.mag_img_filter = filter;
690   cso_single_sampler(ctx->cso, 0, &ctx->sampler);
691   cso_single_sampler_done(ctx->cso);
692
693   /* viewport */
694   ctx->viewport.scale[0] = 0.5f * dst->width;
695   ctx->viewport.scale[1] = 0.5f * dst->height;
696   ctx->viewport.scale[2] = 0.5f;
697   ctx->viewport.scale[3] = 1.0f;
698   ctx->viewport.translate[0] = 0.5f * dst->width;
699   ctx->viewport.translate[1] = 0.5f * dst->height;
700   ctx->viewport.translate[2] = 0.5f;
701   ctx->viewport.translate[3] = 0.0f;
702   cso_set_viewport(ctx->cso, &ctx->viewport);
703
704   /* texture */
705   cso_set_fragment_sampler_views(ctx->cso, 1, &src_sampler_view);
706
707   /* shaders */
708   cso_set_fragment_shader_handle(ctx->cso, ctx->fs[TGSI_WRITEMASK_XYZW]);
709   cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
710
711   /* drawing dest */
712   memset(&fb, 0, sizeof(fb));
713   fb.width = dst->width;
714   fb.height = dst->height;
715   fb.nr_cbufs = 1;
716   fb.cbufs[0] = dst;
717   cso_set_framebuffer(ctx->cso, &fb);
718
719   /* draw quad */
720   offset = setup_vertex_data_tex(ctx,
721                                  (float) dstX0 / dst->width * 2.0f - 1.0f,
722                                  (float) dstY0 / dst->height * 2.0f - 1.0f,
723                                  (float) dstX1 / dst->width * 2.0f - 1.0f,
724                                  (float) dstY1 / dst->height * 2.0f - 1.0f,
725                                  s0, t0, s1, t1,
726                                  z);
727
728   util_draw_vertex_buffer(ctx->pipe, ctx->cso,
729                           ctx->vbuf, offset,
730                           PIPE_PRIM_TRIANGLE_FAN,
731                           4,  /* verts */
732                           2); /* attribs/vert */
733
734   /* restore state we changed */
735   cso_restore_blend(ctx->cso);
736   cso_restore_depth_stencil_alpha(ctx->cso);
737   cso_restore_rasterizer(ctx->cso);
738   cso_restore_samplers(ctx->cso);
739   cso_restore_fragment_sampler_views(ctx->cso);
740   cso_restore_viewport(ctx->cso);
741   cso_restore_framebuffer(ctx->cso);
742   cso_restore_fragment_shader(ctx->cso);
743   cso_restore_vertex_shader(ctx->cso);
744   cso_restore_clip(ctx->cso);
745   cso_restore_vertex_elements(ctx->cso);
746   cso_restore_vertex_buffers(ctx->cso);
747}
748