util: util_blitter_fill no longer needs to handle overlap
[mesa.git] / src / gallium / auxiliary / util / u_blitter.c
index cef3b69e46ddf21e05d90658a9d096b4c7514c3e..8b18ba35f0113192112dcf66f26c6f3c391edd09 100644 (file)
 
 /**
  * @file
- * Blitter utility to facilitate acceleration of the clear, surface_copy,
- * and surface_fill functions.
+ * Blitter utility to facilitate acceleration of the clear, resource_copy_region,
+ * and resource_fill_region functions.
  *
  * @author Marek Olšák
  */
 
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
-#include "pipe/p_inlines.h"
+#include "util/u_inlines.h"
 #include "pipe/p_shader_tokens.h"
 #include "pipe/p_state.h"
 
@@ -45,7 +45,9 @@
 #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)
@@ -55,18 +57,17 @@ struct blitter_context_priv
    struct blitter_context blitter;
 
    struct pipe_context *pipe; /**< pipe context */
-   struct pipe_buffer *vbuf;  /**< quad */
+   struct pipe_resource *vbuf;  /**< quad */
 
    float vertices[4][2][4];   /**< {pos, color} or {pos, texcoord} */
 
    /* Templates for various state objects. */
-   struct pipe_depth_stencil_alpha_state template_dsa;
    struct pipe_sampler_state template_sampler_state;
 
    /* Constant state objects. */
    /* Vertex shaders. */
    void *vs_col; /**< Vertex shader which passes {pos, color} to the output */
-   void *vs_tex; /**<Vertex shader which passes {pos, texcoord} to the output.*/
+   void *vs_tex; /**< Vertex shader which passes {pos, texcoord} to the output.*/
 
    /* Fragment shaders. */
    /* FS which outputs a color to multiple color buffers. */
@@ -85,24 +86,35 @@ struct blitter_context_priv
    void *blend_keep_color;    /**< blend state with writemask of 0 */
 
    /* Depth stencil alpha state. */
-   void *dsa_write_depth_stencil[0xff]; /**< indices are stencil clear values */
+   void *dsa_write_depth_stencil;
    void *dsa_write_depth_keep_stencil;
    void *dsa_keep_depth_stencil;
 
+   void *velem_state;
+
    /* Sampler state for clamping to a miplevel. */
    void *sampler_state[PIPE_MAX_TEXTURE_LEVELS];
 
    /* Rasterizer state. */
    void *rs_state;
+
+   struct pipe_sampler_view *sampler_view;
+
+   /* Viewport state. */
+   struct pipe_viewport_state viewport;
+
+   /* Clip state. */
+   struct pipe_clip_state clip;
 };
 
 struct blitter_context *util_blitter_create(struct pipe_context *pipe)
 {
    struct blitter_context_priv *ctx;
    struct pipe_blend_state blend;
-   struct pipe_depth_stencil_alpha_state *dsa;
+   struct pipe_depth_stencil_alpha_state dsa;
    struct pipe_rasterizer_state rs_state;
    struct pipe_sampler_state *sampler_state;
+   struct pipe_vertex_element velem[2];
    unsigned i;
 
    ctx = CALLOC_STRUCT(blitter_context_priv);
@@ -117,35 +129,39 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe)
    ctx->blitter.saved_rs_state = INVALID_PTR;
    ctx->blitter.saved_fs = INVALID_PTR;
    ctx->blitter.saved_vs = INVALID_PTR;
+   ctx->blitter.saved_velem_state = INVALID_PTR;
    ctx->blitter.saved_fb_state.nr_cbufs = ~0;
-   ctx->blitter.saved_num_textures = ~0;
+   ctx->blitter.saved_num_sampler_views = ~0;
    ctx->blitter.saved_num_sampler_states = ~0;
+   ctx->blitter.saved_num_vertex_buffers = ~0;
 
    /* blend state objects */
    memset(&blend, 0, sizeof(blend));
    ctx->blend_keep_color = pipe->create_blend_state(pipe, &blend);
 
-   blend.colormask = PIPE_MASK_RGBA;
+   blend.rt[0].colormask = PIPE_MASK_RGBA;
    ctx->blend_write_color = pipe->create_blend_state(pipe, &blend);
 
    /* depth stencil alpha state objects */
-   dsa = &ctx->template_dsa;
+   memset(&dsa, 0, sizeof(dsa));
    ctx->dsa_keep_depth_stencil =
-      pipe->create_depth_stencil_alpha_state(pipe, dsa);
+      pipe->create_depth_stencil_alpha_state(pipe, &dsa);
 
-   dsa->depth.enabled = 1;
-   dsa->depth.writemask = 1;
-   dsa->depth.func = PIPE_FUNC_ALWAYS;
+   dsa.depth.enabled = 1;
+   dsa.depth.writemask = 1;
+   dsa.depth.func = PIPE_FUNC_ALWAYS;
    ctx->dsa_write_depth_keep_stencil =
-      pipe->create_depth_stencil_alpha_state(pipe, dsa);
-
-   dsa->stencil[0].enabled = 1;
-   dsa->stencil[0].func = PIPE_FUNC_ALWAYS;
-   dsa->stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
-   dsa->stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
-   dsa->stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
-   dsa->stencil[0].valuemask = 0xff;
-   dsa->stencil[0].writemask = 0xff;
+      pipe->create_depth_stencil_alpha_state(pipe, &dsa);
+
+   dsa.stencil[0].enabled = 1;
+   dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
+   dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
+   dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
+   dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
+   dsa.stencil[0].valuemask = 0xff;
+   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. */
 
@@ -154,6 +170,7 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe)
    sampler_state->wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
    sampler_state->wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
    sampler_state->wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
+   sampler_state->normalized_coords = TRUE;
    /* The sampler state objects which sample from a specified mipmap level
     * are created on-demand. */
 
@@ -161,10 +178,20 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe)
    memset(&rs_state, 0, sizeof(rs_state));
    rs_state.front_winding = PIPE_WINDING_CW;
    rs_state.cull_mode = PIPE_WINDING_NONE;
-   rs_state.bypass_vs_clip_and_viewport = 1;
    rs_state.gl_rasterization_rules = 1;
+   rs_state.flatshade = 1;
    ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
 
+   /* vertex elements state */
+   memset(&velem[0], 0, sizeof(velem[0]) * 2);
+   for (i = 0; i < 2; i++) {
+      velem[i].src_offset = i * 4 * sizeof(float);
+      velem[i].instance_divisor = 0;
+      velem[i].vertex_buffer_index = 0;
+      velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
+   }
+   ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
+
    /* fragment shaders are created on-demand */
 
    /* vertex shaders */
@@ -191,8 +218,7 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe)
 
    /* create the vertex buffer */
    ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
-                                  32,
-                                  PIPE_BUFFER_USAGE_VERTEX,
+                                  PIPE_BIND_VERTEX_BUFFER,
                                   sizeof(ctx->vertices));
 
    return &ctx->blitter;
@@ -209,15 +235,12 @@ void util_blitter_destroy(struct blitter_context *blitter)
    pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
    pipe->delete_depth_stencil_alpha_state(pipe,
                                           ctx->dsa_write_depth_keep_stencil);
-
-   for (i = 0; i < 0xff; i++)
-      if (ctx->dsa_write_depth_stencil[i])
-         pipe->delete_depth_stencil_alpha_state(pipe,
-            ctx->dsa_write_depth_stencil[i]);
+   pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
 
    pipe->delete_rasterizer_state(pipe, ctx->rs_state);
    pipe->delete_vs_state(pipe, ctx->vs_col);
    pipe->delete_vs_state(pipe, ctx->vs_tex);
+   pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
 
    for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
       if (ctx->fs_texfetch_col[i])
@@ -234,7 +257,11 @@ void util_blitter_destroy(struct blitter_context *blitter)
       if (ctx->sampler_state[i])
          pipe->delete_sampler_state(pipe, ctx->sampler_state[i]);
 
-   pipe_buffer_reference(&ctx->vbuf, NULL);
+   if (ctx->sampler_view) {
+      pipe_sampler_view_reference(&ctx->sampler_view, NULL);
+   }
+
+   pipe_resource_reference(&ctx->vbuf, NULL);
    FREE(ctx);
 }
 
@@ -245,7 +272,8 @@ static void blitter_check_saved_CSOs(struct blitter_context_priv *ctx)
           ctx->blitter.saved_dsa_state != INVALID_PTR &&
           ctx->blitter.saved_rs_state != INVALID_PTR &&
           ctx->blitter.saved_fs != INVALID_PTR &&
-          ctx->blitter.saved_vs != INVALID_PTR);
+          ctx->blitter.saved_vs != INVALID_PTR &&
+          ctx->blitter.saved_velem_state != INVALID_PTR);
 }
 
 static void blitter_restore_CSOs(struct blitter_context_priv *ctx)
@@ -258,12 +286,19 @@ static void blitter_restore_CSOs(struct blitter_context_priv *ctx)
    pipe->bind_rasterizer_state(pipe, ctx->blitter.saved_rs_state);
    pipe->bind_fs_state(pipe, ctx->blitter.saved_fs);
    pipe->bind_vs_state(pipe, ctx->blitter.saved_vs);
+   pipe->bind_vertex_elements_state(pipe, ctx->blitter.saved_velem_state);
 
    ctx->blitter.saved_blend_state = INVALID_PTR;
    ctx->blitter.saved_dsa_state = INVALID_PTR;
    ctx->blitter.saved_rs_state = INVALID_PTR;
    ctx->blitter.saved_fs = INVALID_PTR;
    ctx->blitter.saved_vs = INVALID_PTR;
+   ctx->blitter.saved_velem_state = INVALID_PTR;
+
+   pipe->set_stencil_ref(pipe, &ctx->blitter.saved_stencil_ref);
+
+   pipe->set_viewport_state(pipe, &ctx->blitter.saved_viewport);
+   pipe->set_clip_state(pipe, &ctx->blitter.saved_clip);
 
    /* restore the state objects which are required to be saved before copy/fill
     */
@@ -279,36 +314,58 @@ static void blitter_restore_CSOs(struct blitter_context_priv *ctx)
       ctx->blitter.saved_num_sampler_states = ~0;
    }
 
-   if (ctx->blitter.saved_num_textures != ~0) {
-      pipe->set_fragment_sampler_textures(pipe,
-                                          ctx->blitter.saved_num_textures,
-                                          ctx->blitter.saved_textures);
-      ctx->blitter.saved_num_textures = ~0;
+   if (ctx->blitter.saved_num_sampler_views != ~0) {
+      pipe->set_fragment_sampler_views(pipe,
+                                       ctx->blitter.saved_num_sampler_views,
+                                       ctx->blitter.saved_sampler_views);
+      ctx->blitter.saved_num_sampler_views = ~0;
+   }
+
+   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 = ~0;
    }
 }
 
 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
                                   unsigned x1, unsigned y1,
                                   unsigned x2, unsigned y2,
+                                  unsigned width, unsigned height,
                                   float depth)
 {
    int i;
 
    /* set vertex positions */
-   ctx->vertices[0][0][0] = x1; /*v0.x*/
-   ctx->vertices[0][0][1] = y1; /*v0.y*/
+   ctx->vertices[0][0][0] = (float)x1 / width * 2.0f - 1.0f; /*v0.x*/
+   ctx->vertices[0][0][1] = (float)y1 / height * 2.0f - 1.0f; /*v0.y*/
 
-   ctx->vertices[1][0][0] = x2; /*v1.x*/
-   ctx->vertices[1][0][1] = y1; /*v1.y*/
+   ctx->vertices[1][0][0] = (float)x2 / width * 2.0f - 1.0f; /*v1.x*/
+   ctx->vertices[1][0][1] = (float)y1 / height * 2.0f - 1.0f; /*v1.y*/
 
-   ctx->vertices[2][0][0] = x2; /*v2.x*/
-   ctx->vertices[2][0][1] = y2; /*v2.y*/
+   ctx->vertices[2][0][0] = (float)x2 / width * 2.0f - 1.0f; /*v2.x*/
+   ctx->vertices[2][0][1] = (float)y2 / height * 2.0f - 1.0f; /*v2.y*/
 
-   ctx->vertices[3][0][0] = x1; /*v3.x*/
-   ctx->vertices[3][0][1] = y2; /*v3.y*/
+   ctx->vertices[3][0][0] = (float)x1 / width * 2.0f - 1.0f; /*v3.x*/
+   ctx->vertices[3][0][1] = (float)y2 / height * 2.0f - 1.0f; /*v3.y*/
 
    for (i = 0; i < 4; i++)
       ctx->vertices[i][0][2] = depth; /*z*/
+
+   /* viewport */
+   ctx->viewport.scale[0] = 0.5f * width;
+   ctx->viewport.scale[1] = 0.5f * height;
+   ctx->viewport.scale[2] = 1.0f;
+   ctx->viewport.scale[3] = 1.0f;
+   ctx->viewport.translate[0] = 0.5f * width;
+   ctx->viewport.translate[1] = 0.5f * height;
+   ctx->viewport.translate[2] = 0.0f;
+   ctx->viewport.translate[3] = 0.0f;
+   ctx->pipe->set_viewport_state(ctx->pipe, &ctx->viewport);
+
+   /* clip */
+   ctx->pipe->set_clip_state(ctx->pipe, &ctx->clip);
 }
 
 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
@@ -378,9 +435,16 @@ static void blitter_set_texcoords_cube(struct blitter_context_priv *ctx,
    float t1 = y1 / (float)surf->height;
    float s2 = x2 / (float)surf->width;
    float t2 = y2 / (float)surf->height;
-   const float st[4][2] = {
-      {s1, t1}, {s2, t1}, {s2, t2}, {s1, t2}
-   };
+   float st[4][2];
+
+   st[0][0] = s1;
+   st[0][1] = t1;
+   st[1][0] = s2;
+   st[1][1] = t1;
+   st[2][0] = s2;
+   st[2][1] = t2;
+   st[3][0] = s1;
+   st[3][1] = t2;
 
    util_map_texcoords2d_onto_cubemap(surf->face,
                                      /* pointer, stride in floats */
@@ -396,7 +460,7 @@ static void blitter_draw_quad(struct blitter_context_priv *ctx)
    struct pipe_context *pipe = ctx->pipe;
 
    /* write vertices and draw them */
-   pipe_buffer_write(pipe->screen, ctx->vbuf,
+   pipe_buffer_write(pipe, ctx->vbuf,
                      0, sizeof(ctx->vertices), ctx->vertices);
 
    util_draw_vertex_buffer(pipe, ctx->vbuf, 0, PIPE_PRIM_TRIANGLE_FAN,
@@ -404,26 +468,6 @@ static void blitter_draw_quad(struct blitter_context_priv *ctx)
                            2); /* attribs/vert */
 }
 
-static INLINE
-void *blitter_get_state_write_depth_stencil(
-               struct blitter_context_priv *ctx,
-               unsigned stencil)
-{
-   struct pipe_context *pipe = ctx->pipe;
-
-   stencil &= 0xff;
-
-   /* Create the DSA state on-demand. */
-   if (!ctx->dsa_write_depth_stencil[stencil]) {
-      ctx->template_dsa.stencil[0].ref_value = stencil;
-
-      ctx->dsa_write_depth_stencil[stencil] =
-         pipe->create_depth_stencil_alpha_state(pipe, &ctx->template_dsa);
-   }
-
-   return ctx->dsa_write_depth_stencil[stencil];
-}
-
 static INLINE
 void **blitter_get_sampler_state(struct blitter_context_priv *ctx,
                                  int miplevel)
@@ -540,6 +584,7 @@ void util_blitter_clear(struct blitter_context *blitter,
 {
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->pipe;
+   struct pipe_stencil_ref sr = { { 0 } };
 
    assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
 
@@ -551,63 +596,51 @@ void util_blitter_clear(struct blitter_context *blitter,
    else
       pipe->bind_blend_state(pipe, ctx->blend_keep_color);
 
-   if (clear_buffers & PIPE_CLEAR_DEPTHSTENCIL)
-      pipe->bind_depth_stencil_alpha_state(pipe,
-         blitter_get_state_write_depth_stencil(ctx, stencil));
+   if (clear_buffers & 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
       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
 
    pipe->bind_rasterizer_state(pipe, ctx->rs_state);
+   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
    pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs));
    pipe->bind_vs_state(pipe, ctx->vs_col);
 
    blitter_set_clear_color(ctx, rgba);
-   blitter_set_rectangle(ctx, 0, 0, width, height, depth);
+   blitter_set_rectangle(ctx, 0, 0, width, height, width, height, depth);
    blitter_draw_quad(ctx);
    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)
+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;
+    }
+}
+
+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)
 {
    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;
-   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;
-
-   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_TEXTURE_USAGE_DEPTH_STENCIL :
-                                            PIPE_TEXTURE_USAGE_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->texture->target,
-                                    dst_tex_usage, 0) ||
-       !screen->is_format_supported(screen, src->format, src->texture->target,
-                                    PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
-      util_surface_copy(pipe, FALSE, dst, dstx, dsty, src, srcx, srcy,
-                        width, height);
-      return;
-   }
+   struct pipe_sampler_view viewTempl, *view;
 
-   /* check whether the states are properly saved */
-   blitter_check_saved_CSOs(ctx);
    assert(blitter->saved_fb_state.nr_cbufs != ~0);
-   assert(blitter->saved_num_textures != ~0);
+   assert(blitter->saved_num_sampler_views != ~0);
    assert(blitter->saved_num_sampler_states != ~0);
    assert(src->texture->target < PIPE_MAX_TEXTURE_TYPES);
 
@@ -635,11 +668,24 @@ void util_blitter_copy(struct blitter_context *blitter,
       fb_state.zsbuf = 0;
    }
 
+   u_sampler_view_default_template(&viewTempl,
+                                   src->texture,
+                                   src->texture->format);
+   view = pipe->create_sampler_view(pipe,
+                                    src->texture,
+                                    &viewTempl);
+
+   if (ctx->sampler_view) {
+      pipe_sampler_view_reference(&ctx->sampler_view, NULL);
+   }
+   ctx->sampler_view = view;
+
    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));
-   pipe->set_fragment_sampler_textures(pipe, 1, &src->texture);
+   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 */
@@ -661,8 +707,66 @@ void util_blitter_copy(struct blitter_context *blitter,
          assert(0);
    }
 
-   blitter_set_rectangle(ctx, dstx, dsty, dstx+width, dsty+height, 0);
+   blitter_set_rectangle(ctx, dstx, dsty, dstx+width, dsty+height, dst->width, dst->height, 0);
    blitter_draw_quad(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;
+
+   if (dst->texture == src->texture) {
+      assert(!is_overlap(srcx, srcx + width, srcy, srcy + height,
+             dstx, dstx + width, dsty, dsty + height))
+      }
+   }
+
+   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;
+
+   /* 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->texture->nr_samples, dst_tex_usage, 0) ||
+       !screen->is_format_supported(screen, src->format, src->texture->target,
+                                    src->texture->nr_samples, PIPE_BIND_SAMPLER_VIEW, 0)) {
+      struct pipe_subresource subdst, subsrc;
+      subdst.face = dst->face;
+      subdst.level = dst->level;
+      subsrc.face = src->face;
+      subsrc.level = src->level;
+      util_resource_copy_region(pipe, dst, subdst, dstx, dsty, dst->zslice,
+                                src, subsrc, srcx, srcy, src->zslice,
+                                width, height);
+      return;
+   }
+
+   /* 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_restore_CSOs(ctx);
 }
 
@@ -688,8 +792,13 @@ void util_blitter_fill(struct blitter_context *blitter,
    /* 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_TEXTURE_USAGE_RENDER_TARGET, 0)) {
-      util_surface_fill(pipe, dst, dstx, dsty, width, height, value);
+                                    dst->texture->nr_samples,
+                                    PIPE_BIND_RENDER_TARGET, 0)) {
+      struct pipe_subresource subdst;
+      subdst.face = dst->face;
+      subdst.level = dst->level;
+      util_resource_fill_region(pipe, dst->texture, subdst, dstx, dsty,
+                                dst->zslice, width, height, value);
       return;
    }
 
@@ -710,6 +819,7 @@ void util_blitter_fill(struct blitter_context *blitter,
    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);
 
    /* set a framebuffer state */
    fb_state.width = dst->width;
@@ -720,7 +830,7 @@ void util_blitter_fill(struct blitter_context *blitter,
    pipe->set_framebuffer_state(pipe, &fb_state);
 
    blitter_set_clear_color(ctx, rgba);
-   blitter_set_rectangle(ctx, 0, 0, width, height, 0);
+   blitter_set_rectangle(ctx, 0, 0, width, height, dst->width, dst->height, 0);
    blitter_draw_quad(ctx);
    blitter_restore_CSOs(ctx);
 }