Merge branch 'gallium-newclear'
[mesa.git] / src / gallium / auxiliary / util / u_blitter.c
index 956aedc8a15a66af726912450a9a894d80d6e9c3..183ffe5670ff01dcbd0fab4b6f9e3567f43f0ad2 100644 (file)
@@ -26,8 +26,8 @@
 
 /**
  * @file
- * Blitter utility to facilitate acceleration of the clear, surface_copy,
- * and surface_fill functions.
+ * Blitter utility to facilitate acceleration of the clear, clear_render_target, clear_depth_stencil
+ * resource_copy_region functions.
  *
  * @author Marek Olšák
  */
@@ -44,9 +44,9 @@
 #include "util/u_blitter.h"
 #include "util/u_draw_quad.h"
 #include "util/u_pack_color.h"
-#include "util/u_rect.h"
 #include "util/u_sampler.h"
 #include "util/u_simple_shaders.h"
+#include "util/u_surface.h"
 #include "util/u_texture.h"
 
 #define INVALID_PTR ((void*)~0)
@@ -69,8 +69,8 @@ struct blitter_context_priv
    void *vs_tex; /**< Vertex shader which passes {pos, texcoord} to the output.*/
 
    /* Fragment shaders. */
-   /* FS which outputs a color to multiple color buffers. */
-   void *fs_col[PIPE_MAX_COLOR_BUFS];
+   /* The shader at index i outputs color to color buffers 0,1,...,i-1. */
+   void *fs_col[PIPE_MAX_COLOR_BUFS+1];
 
    /* FS which outputs a color from a texture,
       where the index is PIPE_TEXTURE_* to be sampled. */
@@ -88,6 +88,7 @@ struct blitter_context_priv
    void *dsa_write_depth_stencil;
    void *dsa_write_depth_keep_stencil;
    void *dsa_keep_depth_stencil;
+   void *dsa_keep_depth_write_stencil;
 
    void *velem_state;
 
@@ -161,8 +162,12 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe)
    dsa.stencil[0].writemask = 0xff;
    ctx->dsa_write_depth_stencil =
       pipe->create_depth_stencil_alpha_state(pipe, &dsa);
-   /* The DSA state objects which write depth and stencil are created
-    * on-demand. */
+
+
+   dsa.depth.enabled = 0;
+   dsa.depth.writemask = 0;
+   ctx->dsa_keep_depth_write_stencil =
+      pipe->create_depth_stencil_alpha_state(pipe, &dsa);
 
    /* sampler state */
    sampler_state = &ctx->template_sampler_state;
@@ -175,8 +180,7 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe)
 
    /* rasterizer state */
    memset(&rs_state, 0, sizeof(rs_state));
-   rs_state.front_winding = PIPE_WINDING_CW;
-   rs_state.cull_mode = PIPE_WINDING_NONE;
+   rs_state.cull_face = PIPE_FACE_NONE;
    rs_state.gl_rasterization_rules = 1;
    rs_state.flatshade = 1;
    ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
@@ -235,6 +239,7 @@ void util_blitter_destroy(struct blitter_context *blitter)
    pipe->delete_depth_stencil_alpha_state(pipe,
                                           ctx->dsa_write_depth_keep_stencil);
    pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
+   pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
 
    pipe->delete_rasterizer_state(pipe, ctx->rs_state);
    pipe->delete_vs_state(pipe, ctx->vs_col);
@@ -248,7 +253,7 @@ void util_blitter_destroy(struct blitter_context *blitter)
          pipe->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
    }
 
-   for (i = 0; i < PIPE_MAX_COLOR_BUFS && ctx->fs_col[i]; i++)
+   for (i = 0; i <= PIPE_MAX_COLOR_BUFS && ctx->fs_col[i]; i++)
       if (ctx->fs_col[i])
          pipe->delete_fs_state(pipe, ctx->fs_col[i]);
 
@@ -322,8 +327,8 @@ static void blitter_restore_CSOs(struct blitter_context_priv *ctx)
 
    if (ctx->blitter.saved_num_vertex_buffers != ~0) {
       pipe->set_vertex_buffers(pipe,
-                                       ctx->blitter.saved_num_vertex_buffers,
-                                       ctx->blitter.saved_vertex_buffers);
+                               ctx->blitter.saved_num_vertex_buffers,
+                               ctx->blitter.saved_vertex_buffers);
       ctx->blitter.saved_num_vertex_buffers = ~0;
    }
 }
@@ -372,24 +377,34 @@ static void blitter_set_clear_color(struct blitter_context_priv *ctx,
 {
    int i;
 
-   for (i = 0; i < 4; i++) {
-      ctx->vertices[i][1][0] = rgba[0];
-      ctx->vertices[i][1][1] = rgba[1];
-      ctx->vertices[i][1][2] = rgba[2];
-      ctx->vertices[i][1][3] = rgba[3];
+   if (rgba) {
+      for (i = 0; i < 4; i++) {
+         ctx->vertices[i][1][0] = rgba[0];
+         ctx->vertices[i][1][1] = rgba[1];
+         ctx->vertices[i][1][2] = rgba[2];
+         ctx->vertices[i][1][3] = rgba[3];
+      }
+   } else {
+      for (i = 0; i < 4; i++) {
+         ctx->vertices[i][1][0] = 0;
+         ctx->vertices[i][1][1] = 0;
+         ctx->vertices[i][1][2] = 0;
+         ctx->vertices[i][1][3] = 0;
+      }
    }
 }
 
 static void blitter_set_texcoords_2d(struct blitter_context_priv *ctx,
-                                     struct pipe_surface *surf,
+                                     struct pipe_resource *src,
+                                     struct pipe_subresource subsrc,
                                      unsigned x1, unsigned y1,
                                      unsigned x2, unsigned y2)
 {
    int i;
-   float s1 = x1 / (float)surf->width;
-   float t1 = y1 / (float)surf->height;
-   float s2 = x2 / (float)surf->width;
-   float t2 = y2 / (float)surf->height;
+   float s1 = x1 / (float)u_minify(src->width0,  subsrc.level);
+   float t1 = y1 / (float)u_minify(src->height0, subsrc.level);
+   float s2 = x2 / (float)u_minify(src->width0,  subsrc.level);
+   float t2 = y2 / (float)u_minify(src->height0, subsrc.level);
 
    ctx->vertices[0][1][0] = s1; /*t0.s*/
    ctx->vertices[0][1][1] = t1; /*t0.t*/
@@ -410,30 +425,32 @@ static void blitter_set_texcoords_2d(struct blitter_context_priv *ctx,
 }
 
 static void blitter_set_texcoords_3d(struct blitter_context_priv *ctx,
-                                     struct pipe_surface *surf,
+                                     struct pipe_resource *src,
+                                     struct pipe_subresource subsrc,
+                                     unsigned zslice,
                                      unsigned x1, unsigned y1,
                                      unsigned x2, unsigned y2)
 {
    int i;
-   float depth = u_minify(surf->texture->depth0, surf->level);
-   float r = surf->zslice / depth;
+   float r = zslice / (float)u_minify(src->depth0, subsrc.level);
 
-   blitter_set_texcoords_2d(ctx, surf, x1, y1, x2, y2);
+   blitter_set_texcoords_2d(ctx, src, subsrc, x1, y1, x2, y2);
 
    for (i = 0; i < 4; i++)
       ctx->vertices[i][1][2] = r; /*r*/
 }
 
 static void blitter_set_texcoords_cube(struct blitter_context_priv *ctx,
-                                       struct pipe_surface *surf,
+                                       struct pipe_resource *src,
+                                       struct pipe_subresource subsrc,
                                        unsigned x1, unsigned y1,
                                        unsigned x2, unsigned y2)
 {
    int i;
-   float s1 = x1 / (float)surf->width;
-   float t1 = y1 / (float)surf->height;
-   float s2 = x2 / (float)surf->width;
-   float t2 = y2 / (float)surf->height;
+   float s1 = x1 / (float)u_minify(src->width0,  subsrc.level);
+   float t1 = y1 / (float)u_minify(src->height0, subsrc.level);
+   float s2 = x2 / (float)u_minify(src->width0,  subsrc.level);
+   float t2 = y2 / (float)u_minify(src->height0, subsrc.level);
    float st[4][2];
 
    st[0][0] = s1;
@@ -445,7 +462,7 @@ static void blitter_set_texcoords_cube(struct blitter_context_priv *ctx,
    st[3][0] = s1;
    st[3][1] = t2;
 
-   util_map_texcoords2d_onto_cubemap(surf->face,
+   util_map_texcoords2d_onto_cubemap(subsrc.face,
                                      /* pointer, stride in floats */
                                      &st[0][0], 2,
                                      &ctx->vertices[0][1][0], 8);
@@ -495,17 +512,36 @@ static INLINE
 void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs)
 {
    struct pipe_context *pipe = ctx->pipe;
-   unsigned index = num_cbufs ? num_cbufs - 1 : 0;
 
    assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
 
-   if (!ctx->fs_col[index])
-      ctx->fs_col[index] =
+   if (!ctx->fs_col[num_cbufs])
+      ctx->fs_col[num_cbufs] =
          util_make_fragment_clonecolor_shader(pipe, num_cbufs);
 
-   return ctx->fs_col[index];
+   return ctx->fs_col[num_cbufs];
 }
 
+/** Convert PIPE_TEXTURE_x to TGSI_TEXTURE_x */
+static unsigned
+pipe_tex_to_tgsi_tex(unsigned pipe_tex_target)
+{
+   switch (pipe_tex_target) {
+   case PIPE_TEXTURE_1D:
+      return TGSI_TEXTURE_1D;
+   case PIPE_TEXTURE_2D:
+      return TGSI_TEXTURE_2D;
+   case PIPE_TEXTURE_3D:
+      return TGSI_TEXTURE_3D;
+   case PIPE_TEXTURE_CUBE:
+      return TGSI_TEXTURE_CUBE;
+   default:
+      assert(0 && "unexpected texture target");
+      return TGSI_TEXTURE_UNKNOWN;
+   }
+}
+
+
 static INLINE
 void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
                                   unsigned tex_target)
@@ -516,25 +552,10 @@ void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
 
    /* Create the fragment shader on-demand. */
    if (!ctx->fs_texfetch_col[tex_target]) {
-      switch (tex_target) {
-         case PIPE_TEXTURE_1D:
-            ctx->fs_texfetch_col[PIPE_TEXTURE_1D] =
-               util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_1D);
-            break;
-         case PIPE_TEXTURE_2D:
-            ctx->fs_texfetch_col[PIPE_TEXTURE_2D] =
-               util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_2D);
-            break;
-         case PIPE_TEXTURE_3D:
-            ctx->fs_texfetch_col[PIPE_TEXTURE_3D] =
-               util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_3D);
-            break;
-         case PIPE_TEXTURE_CUBE:
-            ctx->fs_texfetch_col[PIPE_TEXTURE_CUBE] =
-               util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_CUBE);
-            break;
-         default:;
-      }
+      unsigned tgsi_tex = pipe_tex_to_tgsi_tex(tex_target);
+
+      ctx->fs_texfetch_col[tex_target] =
+        util_make_fragment_tex_shader(pipe, tgsi_tex, TGSI_INTERPOLATE_LINEAR);
    }
 
    return ctx->fs_texfetch_col[tex_target];
@@ -550,25 +571,11 @@ void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
 
    /* Create the fragment shader on-demand. */
    if (!ctx->fs_texfetch_depth[tex_target]) {
-      switch (tex_target) {
-         case PIPE_TEXTURE_1D:
-            ctx->fs_texfetch_depth[PIPE_TEXTURE_1D] =
-               util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_1D);
-            break;
-         case PIPE_TEXTURE_2D:
-            ctx->fs_texfetch_depth[PIPE_TEXTURE_2D] =
-               util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_2D);
-            break;
-         case PIPE_TEXTURE_3D:
-            ctx->fs_texfetch_depth[PIPE_TEXTURE_3D] =
-               util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_3D);
-            break;
-         case PIPE_TEXTURE_CUBE:
-            ctx->fs_texfetch_depth[PIPE_TEXTURE_CUBE] =
-               util_make_fragment_tex_shader_writedepth(pipe,TGSI_TEXTURE_CUBE);
-            break;
-         default:;
-      }
+      unsigned tgsi_tex = pipe_tex_to_tgsi_tex(tex_target);
+
+      ctx->fs_texfetch_depth[tex_target] =
+         util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
+                                                  TGSI_INTERPOLATE_LINEAR);
    }
 
    return ctx->fs_texfetch_depth[tex_target];
@@ -595,11 +602,19 @@ void util_blitter_clear(struct blitter_context *blitter,
    else
       pipe->bind_blend_state(pipe, ctx->blend_keep_color);
 
-   if (clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) {
+   if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
       sr.ref_value[0] = stencil & 0xff;
       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
       pipe->set_stencil_ref(pipe, &sr);
    }
+   else if (clear_buffers & PIPE_CLEAR_DEPTH) {
+      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
+   }
+   else if (clear_buffers & PIPE_CLEAR_STENCIL) {
+      sr.ref_value[0] = stencil & 0xff;
+      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
+      pipe->set_stencil_ref(pipe, &sr);
+   }
    else
       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
 
@@ -614,261 +629,244 @@ void util_blitter_clear(struct blitter_context *blitter,
    blitter_restore_CSOs(ctx);
 }
 
-static boolean
-is_overlap(unsigned sx1, unsigned sx2, unsigned sy1, unsigned sy2,
-           unsigned dx1, unsigned dx2, unsigned dy1, unsigned dy2)
+static
+boolean is_overlap(unsigned sx1, unsigned sx2, unsigned sy1, unsigned sy2,
+                   unsigned dx1, unsigned dx2, unsigned dy1, unsigned dy2)
 {
-    if (sx1 >= dx2 || sx2 <= dx1 || sy1 >= dy2 || sy2 <= dy1) {
-        return FALSE;
-    } else {
-        return TRUE;
-    }
+   return sx1 < dx2 && sx2 > dx1 && sy1 < dy2 && sy2 > dy1;
 }
 
-static void util_blitter_do_copy(struct blitter_context *blitter,
-                                struct pipe_surface *dst,
-                                unsigned dstx, unsigned dsty,
-                                struct pipe_surface *src,
-                                unsigned srcx, unsigned srcy,
-                                unsigned width, unsigned height,
-                                boolean is_depth)
+void util_blitter_copy_region(struct blitter_context *blitter,
+                              struct pipe_resource *dst,
+                              struct pipe_subresource subdst,
+                              unsigned dstx, unsigned dsty, unsigned dstz,
+                              struct pipe_resource *src,
+                              struct pipe_subresource subsrc,
+                              unsigned srcx, unsigned srcy, unsigned srcz,
+                              unsigned width, unsigned height,
+                              boolean ignore_stencil)
 {
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->pipe;
+   struct pipe_screen *screen = pipe->screen;
+   struct pipe_surface *dstsurf;
    struct pipe_framebuffer_state fb_state;
    struct pipe_sampler_view viewTempl, *view;
+   unsigned bind;
+   boolean is_stencil, is_depth;
+
+   /* Give up if textures are not set. */
+   assert(dst && src);
+   if (!dst || !src)
+      return;
+
+   /* Sanity checks. */
+   if (dst == src) {
+      assert(!is_overlap(srcx, srcx + width, srcy, srcy + height,
+                         dstx, dstx + width, dsty, dsty + height));
+   } else {
+      assert(dst->format == src->format);
+   }
+   assert(src->target < PIPE_MAX_TEXTURE_TYPES);
 
+   /* Is this a ZS format? */
+   is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0;
+   is_stencil = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 1) != 0;
+
+   if (is_depth || is_stencil)
+      bind = PIPE_BIND_DEPTH_STENCIL;
+   else
+      bind = PIPE_BIND_RENDER_TARGET;
+
+   /* Check if we can sample from and render to the surfaces. */
+   /* (assuming copying a stencil buffer is not possible) */
+    if ((!ignore_stencil && is_stencil) ||
+       !screen->is_format_supported(screen, dst->format, dst->target,
+                                    dst->nr_samples, bind, 0) ||
+       !screen->is_format_supported(screen, src->format, src->target,
+                                    src->nr_samples, PIPE_BIND_SAMPLER_VIEW, 0)) {
+      util_resource_copy_region(pipe, dst, subdst, dstx, dsty, dstz,
+                                src, subsrc, srcx, srcy, srcz, width, height);
+      return;
+   }
+
+   /* Get surfaces. */
+   dstsurf = screen->get_tex_surface(screen, dst,
+                                     subdst.face, subdst.level, dstz,
+                                     bind);
+
+   /* Check whether the states are properly saved. */
+   blitter_check_saved_CSOs(ctx);
    assert(blitter->saved_fb_state.nr_cbufs != ~0);
    assert(blitter->saved_num_sampler_views != ~0);
    assert(blitter->saved_num_sampler_states != ~0);
-   assert(src->texture->target < PIPE_MAX_TEXTURE_TYPES);
 
-   /* bind CSOs */
-   fb_state.width = dst->width;
-   fb_state.height = dst->height;
+   /* Initialize framebuffer state. */
+   fb_state.width = dstsurf->width;
+   fb_state.height = dstsurf->height;
 
    if (is_depth) {
       pipe->bind_blend_state(pipe, ctx->blend_keep_color);
       pipe->bind_depth_stencil_alpha_state(pipe,
                                            ctx->dsa_write_depth_keep_stencil);
       pipe->bind_fs_state(pipe,
-         blitter_get_fs_texfetch_depth(ctx, src->texture->target));
+                          blitter_get_fs_texfetch_depth(ctx, src->target));
 
       fb_state.nr_cbufs = 0;
-      fb_state.zsbuf = dst;
+      fb_state.zsbuf = dstsurf;
    } else {
       pipe->bind_blend_state(pipe, ctx->blend_write_color);
       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
       pipe->bind_fs_state(pipe,
-         blitter_get_fs_texfetch_col(ctx, src->texture->target));
+                          blitter_get_fs_texfetch_col(ctx, src->target));
 
       fb_state.nr_cbufs = 1;
-      fb_state.cbufs[0] = dst;
+      fb_state.cbufs[0] = dstsurf;
       fb_state.zsbuf = 0;
    }
 
-   u_sampler_view_default_template(&viewTempl,
-                                   src->texture,
-                                   src->texture->format);
-   view = pipe->create_sampler_view(pipe,
-                                    src->texture,
-                                    &viewTempl);
+   /* Initialize sampler view. */
+   u_sampler_view_default_template(&viewTempl, src, src->format);
+   view = pipe->create_sampler_view(pipe, src, &viewTempl);
 
    if (ctx->sampler_view) {
       pipe_sampler_view_reference(&ctx->sampler_view, NULL);
    }
    ctx->sampler_view = view;
 
+   /* Set rasterizer state, shaders, and textures. */
    pipe->bind_rasterizer_state(pipe, ctx->rs_state);
    pipe->bind_vs_state(pipe, ctx->vs_tex);
    pipe->bind_fragment_sampler_states(pipe, 1,
-      blitter_get_sampler_state(ctx, src->level));
+                                      blitter_get_sampler_state(ctx, subsrc.level));
    pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
    pipe->set_fragment_sampler_views(pipe, 1, &view);
    pipe->set_framebuffer_state(pipe, &fb_state);
 
-   /* set texture coordinates */
-   switch (src->texture->target) {
+   /* Set texture coordinates. */
+   switch (src->target) {
       case PIPE_TEXTURE_1D:
       case PIPE_TEXTURE_2D:
-         blitter_set_texcoords_2d(ctx, src, srcx, srcy,
-                                  srcx+width, srcy+height);
+         blitter_set_texcoords_2d(ctx, src, subsrc,
+                                  srcx, srcy, srcx+width, srcy+height);
          break;
       case PIPE_TEXTURE_3D:
-         blitter_set_texcoords_3d(ctx, src, srcx, srcy,
-                                  srcx+width, srcy+height);
+         blitter_set_texcoords_3d(ctx, src, subsrc, srcz,
+                                  srcx, srcy, srcx+width, srcy+height);
          break;
       case PIPE_TEXTURE_CUBE:
-         blitter_set_texcoords_cube(ctx, src, srcx, srcy,
-                                    srcx+width, srcy+height);
+         blitter_set_texcoords_cube(ctx, src, subsrc,
+                                    srcx, srcy, srcx+width, srcy+height);
          break;
       default:
          assert(0);
+         return;
    }
 
-   blitter_set_rectangle(ctx, dstx, dsty, dstx+width, dsty+height, dst->width, dst->height, 0);
+   blitter_set_rectangle(ctx, dstx, dsty, dstx+width, dsty+height,
+                         dstsurf->width, dstsurf->height, 0);
    blitter_draw_quad(ctx);
+   blitter_restore_CSOs(ctx);
 
+   pipe_surface_reference(&dstsurf, NULL);
 }
 
-static void util_blitter_overlap_copy(struct blitter_context *blitter,
-                                     struct pipe_surface *dst,
-                                     unsigned dstx, unsigned dsty,
-                                     struct pipe_surface *src,
-                                     unsigned srcx, unsigned srcy,
-                                     unsigned width, unsigned height)
+/* Clear a region of a color surface to a constant value. */
+void util_blitter_clear_render_target(struct blitter_context *blitter,
+                                      struct pipe_surface *dstsurf,
+                                      const float *rgba,
+                                      unsigned dstx, unsigned dsty,
+                                      unsigned width, unsigned height)
 {
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->pipe;
-   struct pipe_screen *screen = pipe->screen;
-
-   struct pipe_resource texTemp;
-   struct pipe_resource *texture;
-   struct pipe_surface *tex_surf;
-
-   /* check whether the states are properly saved */
-   blitter_check_saved_CSOs(ctx);
-
-   memset(&texTemp, 0, sizeof(texTemp));
-   texTemp.target = PIPE_TEXTURE_2D;
-   texTemp.format = dst->texture->format; /* XXX verify supported by driver! */
-   texTemp.last_level = 0;
-   texTemp.width0 = width;
-   texTemp.height0 = height;
-   texTemp.depth0 = 1;
+   struct pipe_framebuffer_state fb_state;
 
-   texture = screen->resource_create(screen, &texTemp);
-   if (!texture)
+   assert(dstsurf->texture);
+   if (!dstsurf->texture)
       return;
 
-   tex_surf = screen->get_tex_surface(screen, texture, 0, 0, 0,
-                                     PIPE_BIND_BLIT_SOURCE | 
-                                     PIPE_BIND_BLIT_DESTINATION);
-
-   /* blit from the src to the temp */
-   util_blitter_do_copy(blitter, tex_surf, 0, 0,
-                       src, srcx, srcy,
-                       width, height,
-                       FALSE);
-   util_blitter_do_copy(blitter, dst, dstx, dsty,
-                       tex_surf, 0, 0,
-                       width, height,
-                       FALSE);
-   pipe_surface_reference(&tex_surf, NULL);
-   pipe_resource_reference(&texture, NULL);
-   blitter_restore_CSOs(ctx);
-}
-
-void util_blitter_copy(struct blitter_context *blitter,
-                       struct pipe_surface *dst,
-                       unsigned dstx, unsigned dsty,
-                       struct pipe_surface *src,
-                       unsigned srcx, unsigned srcy,
-                       unsigned width, unsigned height,
-                       boolean ignore_stencil)
-{
-   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
-   struct pipe_context *pipe = ctx->pipe;
-   struct pipe_screen *screen = pipe->screen;
-   boolean is_stencil, is_depth;
-   unsigned dst_tex_usage;
-
-   /* give up if textures are not set */
-   assert(dst->texture && src->texture);
-   if (!dst->texture || !src->texture)
-      return;
+   /* check the saved state */
+   blitter_check_saved_CSOs(ctx);
+   assert(blitter->saved_fb_state.nr_cbufs != ~0);
 
-   if (dst->texture == src->texture) {
-      if (is_overlap(srcx, srcx + width, srcy, srcy + height,
-                            dstx, dstx + width, dsty, dsty + height)) {
-         util_blitter_overlap_copy(blitter, dst, dstx, dsty, src, srcx, srcy,
-                                   width, height);
-         return;
-      }
-   }
-                  
-   is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0;
-   is_stencil = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 1) != 0;
-   dst_tex_usage = is_depth || is_stencil ? PIPE_BIND_DEPTH_STENCIL :
-                                            PIPE_BIND_RENDER_TARGET;
+   /* bind CSOs */
+   pipe->bind_blend_state(pipe, ctx->blend_write_color);
+   pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
+   pipe->bind_rasterizer_state(pipe, ctx->rs_state);
+   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1));
+   pipe->bind_vs_state(pipe, ctx->vs_col);
+   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
 
-   /* check if we can sample from and render to the surfaces */
-   /* (assuming copying a stencil buffer is not possible) */
-   if ((!ignore_stencil && is_stencil) ||
-       !screen->is_format_supported(screen, dst->format, dst->texture->target,
-                                    dst_tex_usage, 0) ||
-       !screen->is_format_supported(screen, src->format, src->texture->target,
-                                    PIPE_BIND_SAMPLER_VIEW, 0)) {
-      util_surface_copy(pipe, FALSE, dst, dstx, dsty, src, srcx, srcy,
-                        width, height);
-      return;
-   }
+   /* set a framebuffer state */
+   fb_state.width = dstsurf->width;
+   fb_state.height = dstsurf->height;
+   fb_state.nr_cbufs = 1;
+   fb_state.cbufs[0] = dstsurf;
+   fb_state.zsbuf = 0;
+   pipe->set_framebuffer_state(pipe, &fb_state);
 
-   /* check whether the states are properly saved */
-   blitter_check_saved_CSOs(ctx);
-   util_blitter_do_copy(blitter,
-                       dst, dstx, dsty,
-                       src, srcx, srcy,
-                       width, height, is_depth);
+   blitter_set_clear_color(ctx, rgba);
+   blitter_set_rectangle(ctx, 0, 0, width, height, dstsurf->width, dstsurf->height, 0);
+   blitter_draw_quad(ctx);
    blitter_restore_CSOs(ctx);
 }
 
-void util_blitter_fill(struct blitter_context *blitter,
-                       struct pipe_surface *dst,
-                       unsigned dstx, unsigned dsty,
-                       unsigned width, unsigned height,
-                       unsigned value)
+/* Clear a region of a depth stencil surface. */
+void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
+                                      struct pipe_surface *dstsurf,
+                                      unsigned clear_flags,
+                                      double depth,
+                                      unsigned stencil,
+                                      unsigned dstx, unsigned dsty,
+                                      unsigned width, unsigned height)
 {
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->pipe;
-   struct pipe_screen *screen = pipe->screen;
    struct pipe_framebuffer_state fb_state;
-   float rgba[4];
-   ubyte ub_rgba[4] = {0};
-   union util_color color;
-   int i;
-
-   assert(dst->texture);
-   if (!dst->texture)
-      return;
+   struct pipe_stencil_ref sr = { { 0 } };
 
-   /* check if we can render to the surface */
-   if (util_format_is_depth_or_stencil(dst->format) || /* unlikely, but you never know */
-       !screen->is_format_supported(screen, dst->format, dst->texture->target,
-                                    PIPE_BIND_RENDER_TARGET, 0)) {
-      util_surface_fill(pipe, dst, dstx, dsty, width, height, value);
+   assert(dstsurf->texture);
+   if (!dstsurf->texture)
       return;
-   }
-
-   /* unpack the color */
-   color.ui = value;
-   util_unpack_color_ub(dst->format, &color,
-                        ub_rgba, ub_rgba+1, ub_rgba+2, ub_rgba+3);
-   for (i = 0; i < 4; i++)
-      rgba[i] = ubyte_to_float(ub_rgba[i]);
 
    /* check the saved state */
    blitter_check_saved_CSOs(ctx);
    assert(blitter->saved_fb_state.nr_cbufs != ~0);
 
    /* bind CSOs */
-   pipe->bind_blend_state(pipe, ctx->blend_write_color);
-   pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
+   pipe->bind_blend_state(pipe, ctx->blend_keep_color);
+   if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
+      sr.ref_value[0] = stencil & 0xff;
+      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
+      pipe->set_stencil_ref(pipe, &sr);
+   }
+   else if (clear_flags & PIPE_CLEAR_DEPTH) {
+      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
+   }
+   else if (clear_flags & PIPE_CLEAR_STENCIL) {
+      sr.ref_value[0] = stencil & 0xff;
+      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
+      pipe->set_stencil_ref(pipe, &sr);
+   }
+   else
+      /* hmm that should be illegal probably, or make it a no-op somewhere */
+      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
+
    pipe->bind_rasterizer_state(pipe, ctx->rs_state);
-   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1));
+   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0));
    pipe->bind_vs_state(pipe, ctx->vs_col);
    pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
 
    /* set a framebuffer state */
-   fb_state.width = dst->width;
-   fb_state.height = dst->height;
-   fb_state.nr_cbufs = 1;
-   fb_state.cbufs[0] = dst;
-   fb_state.zsbuf = 0;
+   fb_state.width = dstsurf->width;
+   fb_state.height = dstsurf->height;
+   fb_state.nr_cbufs = 0;
+   fb_state.cbufs[0] = 0;
+   fb_state.zsbuf = dstsurf;
    pipe->set_framebuffer_state(pipe, &fb_state);
 
-   blitter_set_clear_color(ctx, rgba);
-   blitter_set_rectangle(ctx, 0, 0, width, height, dst->width, dst->height, 0);
+   blitter_set_rectangle(ctx, 0, 0, width, height, dstsurf->width, dstsurf->height, depth);
    blitter_draw_quad(ctx);
    blitter_restore_CSOs(ctx);
 }