u_blitter: add a msaa parameter to util_blitter_clear
[mesa.git] / src / gallium / auxiliary / util / u_blitter.c
index 16bf90fc9d620ba13abc52c1630b063ebf0c211e..8d6909ec4261b4ad7ebdf8ebeb08927b0e168726 100644 (file)
@@ -61,8 +61,6 @@ struct blitter_context_priv
 {
    struct blitter_context base;
 
-   struct u_upload_mgr *upload;
-
    float vertices[4][2][4];   /**< {pos, color} or {pos, texcoord} */
 
    /* Templates for various state objects. */
@@ -70,7 +68,9 @@ struct blitter_context_priv
    /* Constant state objects. */
    /* Vertex shaders. */
    void *vs; /**< Vertex shader which passes {pos, generic} to the output.*/
-   void *vs_pos_only; /**< Vertex shader which passes pos to the output.*/
+   void *vs_nogeneric;
+   void *vs_pos_only[4]; /**< Vertex shader which passes pos to the output
+                              for clear_buffer/copy_buffer.*/
    void *vs_layered; /**< Vertex shader which sets LAYER = INSTANCEID. */
 
    /* Fragment shaders. */
@@ -78,29 +78,37 @@ struct blitter_context_priv
    void *fs_write_one_cbuf;
    void *fs_write_all_cbufs;
 
-   /* FS which outputs a color from a texture,
-      where the index is PIPE_TEXTURE_* to be sampled. */
-   void *fs_texfetch_col[PIPE_MAX_TEXTURE_TYPES];
+   /* FS which outputs a color from a texture where
+    * the 1st index indicates the texture type / destination type,
+    * the 2nd index is the PIPE_TEXTURE_* to be sampled,
+    * the 3rd index is 0 = use TEX, 1 = use TXF.
+    */
+   void *fs_texfetch_col[5][PIPE_MAX_TEXTURE_TYPES][2];
 
-   /* FS which outputs a depth from a texture,
-      where the index is PIPE_TEXTURE_* to be sampled. */
-   void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES];
-   void *fs_texfetch_depthstencil[PIPE_MAX_TEXTURE_TYPES];
-   void *fs_texfetch_stencil[PIPE_MAX_TEXTURE_TYPES];
+   /* FS which outputs a depth from a texture, where
+    * the 1st index is the PIPE_TEXTURE_* to be sampled,
+    * the 2nd index is 0 = use TEX, 1 = use TXF.
+    */
+   void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES][2];
+   void *fs_texfetch_depthstencil[PIPE_MAX_TEXTURE_TYPES][2];
+   void *fs_texfetch_stencil[PIPE_MAX_TEXTURE_TYPES][2];
 
    /* FS which outputs one sample from a multisample texture. */
-   void *fs_texfetch_col_msaa[PIPE_MAX_TEXTURE_TYPES];
+   void *fs_texfetch_col_msaa[5][PIPE_MAX_TEXTURE_TYPES];
    void *fs_texfetch_depth_msaa[PIPE_MAX_TEXTURE_TYPES];
    void *fs_texfetch_depthstencil_msaa[PIPE_MAX_TEXTURE_TYPES];
    void *fs_texfetch_stencil_msaa[PIPE_MAX_TEXTURE_TYPES];
 
    /* FS which outputs an average of all samples. */
    void *fs_resolve[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
-   void *fs_resolve_sint[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
-   void *fs_resolve_uint[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
+
+   /* FS which unpacks color to ZS or packs ZS to color, matching
+    * the ZS format. See util_blitter_get_color_format_for_zs().
+    */
+   void *fs_pack_color_zs[TGSI_TEXTURE_COUNT][10];
 
    /* Blend state. */
-   void *blend[PIPE_MASK_RGBA+1]; /**< blend state with writemask */
+   void *blend[PIPE_MASK_RGBA+1][2]; /**< blend state with writemask */
    void *blend_clear[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR)+1];
 
    /* Depth stencil alpha state. */
@@ -120,22 +128,25 @@ struct blitter_context_priv
    void *sampler_state_rect_linear;
 
    /* Rasterizer state. */
-   void *rs_state, *rs_state_scissor, *rs_discard_state;
-
-   /* Viewport state. */
-   struct pipe_viewport_state viewport;
+   void *rs_state[2][2];  /**< [scissor][msaa] */
+   void *rs_discard_state;
 
    /* Destination surface dimensions. */
    unsigned dst_width;
    unsigned dst_height;
 
-   boolean has_geometry_shader;
-   boolean has_tessellation;
-   boolean has_layered;
-   boolean has_stream_out;
-   boolean has_stencil_export;
-   boolean has_texture_multisample;
-   boolean cached_all_shaders;
+   void *custom_vs;
+
+   bool has_geometry_shader;
+   bool has_tessellation;
+   bool has_layered;
+   bool has_stream_out;
+   bool has_stencil_export;
+   bool has_texture_multisample;
+   bool has_tex_lz;
+   bool has_txf;
+   bool cube_as_2darray;
+   bool cached_all_shaders;
 
    /* The Draw module overrides these functions.
     * Always create the blitter before Draw. */
@@ -143,10 +154,6 @@ struct blitter_context_priv
    void   (*delete_fs_state)(struct pipe_context *, void *);
 };
 
-static struct pipe_surface *
-util_blitter_get_next_surface_layer(struct pipe_context *pipe,
-                                    struct pipe_surface *surf);
-
 struct blitter_context *util_blitter_create(struct pipe_context *pipe)
 {
    struct blitter_context_priv *ctx;
@@ -155,7 +162,7 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe)
    struct pipe_rasterizer_state rs_state;
    struct pipe_sampler_state sampler_state;
    struct pipe_vertex_element velem[2];
-   unsigned i;
+   unsigned i, j;
 
    ctx = CALLOC_STRUCT(blitter_context_priv);
    if (!ctx)
@@ -163,7 +170,6 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe)
 
    ctx->base.pipe = pipe;
    ctx->base.draw_rectangle = util_blitter_draw_rectangle;
-   ctx->base.get_next_surface_layer = util_blitter_get_next_surface_layer;
 
    ctx->bind_fs_state = pipe->bind_fs_state;
    ctx->delete_fs_state = pipe->delete_fs_state;
@@ -200,12 +206,31 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe)
    ctx->has_texture_multisample =
       pipe->screen->get_param(pipe->screen, PIPE_CAP_TEXTURE_MULTISAMPLE);
 
+   ctx->has_tex_lz = pipe->screen->get_param(pipe->screen,
+                                             PIPE_CAP_TGSI_TEX_TXF_LZ);
+   ctx->has_txf = pipe->screen->get_param(pipe->screen,
+                                          PIPE_CAP_GLSL_FEATURE_LEVEL) > 130;
+   ctx->cube_as_2darray = pipe->screen->get_param(pipe->screen,
+                                                  PIPE_CAP_SAMPLER_VIEW_TARGET);
+
    /* blend state objects */
    memset(&blend, 0, sizeof(blend));
 
    for (i = 0; i <= PIPE_MASK_RGBA; i++) {
-      blend.rt[0].colormask = i;
-      ctx->blend[i] = pipe->create_blend_state(pipe, &blend);
+      for (j = 0; j < 2; j++) {
+         memset(&blend.rt[0], 0, sizeof(blend.rt[0]));
+         blend.rt[0].colormask = i;
+         if (j) {
+            blend.rt[0].blend_enable = 1;
+            blend.rt[0].rgb_func = PIPE_BLEND_ADD;
+            blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
+            blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
+            blend.rt[0].alpha_func = PIPE_BLEND_ADD;
+            blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
+            blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
+         }
+         ctx->blend[i][j] = pipe->create_blend_state(pipe, &blend);
+      }
    }
 
    /* depth stencil alpha state objects */
@@ -257,18 +282,26 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe)
    rs_state.half_pixel_center = 1;
    rs_state.bottom_edge_rule = 1;
    rs_state.flatshade = 1;
-   rs_state.depth_clip = 1;
-   ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
-
-   rs_state.scissor = 1;
-   ctx->rs_state_scissor = pipe->create_rasterizer_state(pipe, &rs_state);
+   rs_state.depth_clip_near = 1;
+   rs_state.depth_clip_far = 1;
+
+   unsigned scissor, msaa;
+   for (scissor = 0; scissor < 2; scissor++) {
+      for (msaa = 0; msaa < 2; msaa++) {
+         rs_state.scissor = scissor;
+         rs_state.multisample = msaa;
+         ctx->rs_state[scissor][msaa] =
+            pipe->create_rasterizer_state(pipe, &rs_state);
+      }
+   }
 
    if (ctx->has_stream_out) {
-      rs_state.scissor = 0;
+      rs_state.scissor = rs_state.multisample = 0;
       rs_state.rasterizer_discard = 1;
       ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state);
    }
 
+   ctx->base.cb_slot = 0; /* 0 for now */
    ctx->base.vb_slot = 0; /* 0 for now */
 
    /* vertex elements states */
@@ -304,59 +337,78 @@ struct blitter_context *util_blitter_create(struct pipe_context *pipe)
    for (i = 0; i < 4; i++)
       ctx->vertices[i][0][3] = 1; /*v.w*/
 
-   ctx->upload = u_upload_create(pipe, 65536, 4, PIPE_BIND_VERTEX_BUFFER);
-
    return &ctx->base;
 }
 
-static void bind_vs_pos_only(struct blitter_context_priv *ctx)
+static void bind_vs_pos_only(struct blitter_context_priv *ctx,
+                             unsigned num_so_channels)
 {
    struct pipe_context *pipe = ctx->base.pipe;
+   int index = num_so_channels ? num_so_channels - 1 : 0;
 
-   if (!ctx->vs_pos_only) {
+   if (!ctx->vs_pos_only[index]) {
       struct pipe_stream_output_info so;
-      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION };
+      static const enum tgsi_semantic semantic_names[] =
+         { TGSI_SEMANTIC_POSITION };
       const uint semantic_indices[] = { 0 };
 
       memset(&so, 0, sizeof(so));
       so.num_outputs = 1;
-      so.output[0].num_components = 1;
-      so.stride[0] = 1;
+      so.output[0].num_components = num_so_channels;
+      so.stride[0] = num_so_channels;
 
-      ctx->vs_pos_only =
+      ctx->vs_pos_only[index] =
          util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names,
-                                                     semantic_indices, FALSE,
-                                                     &so);
+                                                     semantic_indices, false,
+                                                     false, &so);
    }
 
-   pipe->bind_vs_state(pipe, ctx->vs_pos_only);
+   pipe->bind_vs_state(pipe, ctx->vs_pos_only[index]);
 }
 
-static void bind_vs_passthrough(struct blitter_context_priv *ctx)
+static void *get_vs_passthrough_pos_generic(struct blitter_context *blitter)
 {
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->base.pipe;
 
    if (!ctx->vs) {
-      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
-                                      TGSI_SEMANTIC_GENERIC };
+      static const enum tgsi_semantic semantic_names[] =
+         { TGSI_SEMANTIC_POSITION, TGSI_SEMANTIC_GENERIC };
       const uint semantic_indices[] = { 0, 0 };
       ctx->vs =
          util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
-                                             semantic_indices, FALSE);
+                                             semantic_indices, false);
    }
+   return ctx->vs;
+}
 
-   pipe->bind_vs_state(pipe, ctx->vs);
+static void *get_vs_passthrough_pos(struct blitter_context *blitter)
+{
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
+   struct pipe_context *pipe = ctx->base.pipe;
+
+   if (!ctx->vs_nogeneric) {
+      static const enum tgsi_semantic semantic_names[] =
+         { TGSI_SEMANTIC_POSITION };
+      const uint semantic_indices[] = { 0 };
+
+      ctx->vs_nogeneric =
+         util_make_vertex_passthrough_shader(pipe, 1,
+                                             semantic_names,
+                                             semantic_indices, false);
+   }
+   return ctx->vs_nogeneric;
 }
 
-static void bind_vs_layered(struct blitter_context_priv *ctx)
+static void *get_vs_layered(struct blitter_context *blitter)
 {
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->base.pipe;
 
    if (!ctx->vs_layered) {
       ctx->vs_layered = util_make_layered_clear_vertex_shader(pipe);
    }
-
-   pipe->bind_vs_state(pipe, ctx->vs_layered);
+   return ctx->vs_layered;
 }
 
 static void bind_fs_empty(struct blitter_context_priv *ctx)
@@ -379,7 +431,7 @@ static void bind_fs_write_one_cbuf(struct blitter_context_priv *ctx)
       assert(!ctx->cached_all_shaders);
       ctx->fs_write_one_cbuf =
          util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
-                                               TGSI_INTERPOLATE_CONSTANT, FALSE);
+                                               TGSI_INTERPOLATE_CONSTANT, false);
    }
 
    ctx->bind_fs_state(pipe, ctx->fs_write_one_cbuf);
@@ -393,7 +445,7 @@ static void bind_fs_write_all_cbufs(struct blitter_context_priv *ctx)
       assert(!ctx->cached_all_shaders);
       ctx->fs_write_all_cbufs =
          util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
-                                               TGSI_INTERPOLATE_CONSTANT, TRUE);
+                                               TGSI_INTERPOLATE_CONSTANT, true);
    }
 
    ctx->bind_fs_state(pipe, ctx->fs_write_all_cbufs);
@@ -403,12 +455,13 @@ void util_blitter_destroy(struct blitter_context *blitter)
 {
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = blitter->pipe;
-   int i, j, f;
+   unsigned i, j, f;
 
-   for (i = 0; i <= PIPE_MASK_RGBA; i++) {
-      pipe->delete_blend_state(pipe, ctx->blend[i]);
-   }
-   for (i = 0; i < Elements(ctx->blend_clear); i++) {
+   for (i = 0; i <= PIPE_MASK_RGBA; i++)
+      for (j = 0; j < 2; j++)
+         pipe->delete_blend_state(pipe, ctx->blend[i][j]);
+
+   for (i = 0; i < ARRAY_SIZE(ctx->blend_clear); i++) {
       if (ctx->blend_clear[i])
          pipe->delete_blend_state(pipe, ctx->blend_clear[i]);
    }
@@ -418,14 +471,22 @@ void util_blitter_destroy(struct blitter_context *blitter)
    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_rasterizer_state(pipe, ctx->rs_state_scissor);
+   unsigned scissor, msaa;
+   for (scissor = 0; scissor < 2; scissor++) {
+      for (msaa = 0; msaa < 2; msaa++) {
+         pipe->delete_rasterizer_state(pipe, ctx->rs_state[scissor][msaa]);
+      }
+   }
+
    if (ctx->rs_discard_state)
       pipe->delete_rasterizer_state(pipe, ctx->rs_discard_state);
    if (ctx->vs)
       pipe->delete_vs_state(pipe, ctx->vs);
-   if (ctx->vs_pos_only)
-      pipe->delete_vs_state(pipe, ctx->vs_pos_only);
+   if (ctx->vs_nogeneric)
+      pipe->delete_vs_state(pipe, ctx->vs_nogeneric);
+   for (i = 0; i < 4; i++)
+      if (ctx->vs_pos_only[i])
+         pipe->delete_vs_state(pipe, ctx->vs_pos_only[i]);
    if (ctx->vs_layered)
       pipe->delete_vs_state(pipe, ctx->vs_layered);
    pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
@@ -436,17 +497,24 @@ void util_blitter_destroy(struct blitter_context *blitter)
    }
 
    for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
-      if (ctx->fs_texfetch_col[i])
-         ctx->delete_fs_state(pipe, ctx->fs_texfetch_col[i]);
-      if (ctx->fs_texfetch_depth[i])
-         ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
-      if (ctx->fs_texfetch_depthstencil[i])
-         ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil[i]);
-      if (ctx->fs_texfetch_stencil[i])
-         ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil[i]);
-
-      if (ctx->fs_texfetch_col_msaa[i])
-         ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_msaa[i]);
+      for (unsigned type = 0; type < ARRAY_SIZE(ctx->fs_texfetch_col); ++type) {
+         for (unsigned inst = 0; inst < 2; inst++) {
+            if (ctx->fs_texfetch_col[type][i][inst])
+               ctx->delete_fs_state(pipe, ctx->fs_texfetch_col[type][i][inst]);
+         }
+         if (ctx->fs_texfetch_col_msaa[type][i])
+            ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_msaa[type][i]);
+      }
+
+      for (unsigned inst = 0; inst < 2; inst++) {
+         if (ctx->fs_texfetch_depth[i][inst])
+            ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth[i][inst]);
+         if (ctx->fs_texfetch_depthstencil[i][inst])
+            ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil[i][inst]);
+         if (ctx->fs_texfetch_stencil[i][inst])
+            ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil[i][inst]);
+      }
+
       if (ctx->fs_texfetch_depth_msaa[i])
          ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth_msaa[i]);
       if (ctx->fs_texfetch_depthstencil_msaa[i])
@@ -454,20 +522,17 @@ void util_blitter_destroy(struct blitter_context *blitter)
       if (ctx->fs_texfetch_stencil_msaa[i])
          ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil_msaa[i]);
 
-      for (j = 0; j< Elements(ctx->fs_resolve[i]); j++)
+      for (j = 0; j< ARRAY_SIZE(ctx->fs_resolve[i]); j++)
          for (f = 0; f < 2; f++)
             if (ctx->fs_resolve[i][j][f])
                ctx->delete_fs_state(pipe, ctx->fs_resolve[i][j][f]);
+   }
 
-      for (j = 0; j< Elements(ctx->fs_resolve_sint[i]); j++)
-         for (f = 0; f < 2; f++)
-            if (ctx->fs_resolve_sint[i][j][f])
-               ctx->delete_fs_state(pipe, ctx->fs_resolve_sint[i][j][f]);
-
-      for (j = 0; j< Elements(ctx->fs_resolve_uint[i]); j++)
-         for (f = 0; f < 2; f++)
-            if (ctx->fs_resolve_uint[i][j][f])
-               ctx->delete_fs_state(pipe, ctx->fs_resolve_uint[i][j][f]);
+   for (i = 0; i < ARRAY_SIZE(ctx->fs_pack_color_zs); i++) {
+      for (j = 0; j < ARRAY_SIZE(ctx->fs_pack_color_zs[0]); j++) {
+         if (ctx->fs_pack_color_zs[i][j])
+            ctx->delete_fs_state(pipe, ctx->fs_pack_color_zs[i][j]);
+      }
    }
 
    if (ctx->fs_empty)
@@ -481,60 +546,67 @@ void util_blitter_destroy(struct blitter_context *blitter)
    pipe->delete_sampler_state(pipe, ctx->sampler_state_rect);
    pipe->delete_sampler_state(pipe, ctx->sampler_state_linear);
    pipe->delete_sampler_state(pipe, ctx->sampler_state);
-   u_upload_destroy(ctx->upload);
    FREE(ctx);
 }
 
 void util_blitter_set_texture_multisample(struct blitter_context *blitter,
-                                          boolean supported)
+                                          bool supported)
 {
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
 
    ctx->has_texture_multisample = supported;
 }
 
-static void blitter_set_running_flag(struct blitter_context_priv *ctx)
+void util_blitter_set_running_flag(struct blitter_context *blitter)
 {
-   if (ctx->base.running) {
+   if (blitter->running) {
       _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
                     __LINE__);
    }
-   ctx->base.running = TRUE;
+   blitter->running = true;
+
+   blitter->pipe->set_active_query_state(blitter->pipe, false);
 }
 
-static void blitter_unset_running_flag(struct blitter_context_priv *ctx)
+void util_blitter_unset_running_flag(struct blitter_context *blitter)
 {
-   if (!ctx->base.running) {
+   if (!blitter->running) {
       _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
                     __LINE__);
    }
-   ctx->base.running = FALSE;
+   blitter->running = false;
+
+   blitter->pipe->set_active_query_state(blitter->pipe, true);
 }
 
-static void blitter_check_saved_vertex_states(struct blitter_context_priv *ctx)
+static void blitter_check_saved_vertex_states(MAYBE_UNUSED struct blitter_context_priv *ctx)
 {
-   assert(ctx->base.saved_velem_state != INVALID_PTR);
    assert(ctx->base.saved_vs != INVALID_PTR);
    assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR);
    assert(!ctx->has_tessellation || ctx->base.saved_tcs != INVALID_PTR);
    assert(!ctx->has_tessellation || ctx->base.saved_tes != INVALID_PTR);
-   assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0);
+   assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0u);
    assert(ctx->base.saved_rs_state != INVALID_PTR);
 }
 
-static void blitter_restore_vertex_states(struct blitter_context_priv *ctx)
+void util_blitter_restore_vertex_states(struct blitter_context *blitter)
 {
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->base.pipe;
    unsigned i;
 
    /* Vertex buffer. */
-   pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1,
-                            &ctx->base.saved_vertex_buffer);
-   pipe_resource_reference(&ctx->base.saved_vertex_buffer.buffer, NULL);
+   if (ctx->base.saved_vertex_buffer.buffer.resource) {
+      pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1,
+                               &ctx->base.saved_vertex_buffer);
+      pipe_vertex_buffer_unreference(&ctx->base.saved_vertex_buffer);
+   }
 
    /* Vertex elements. */
-   pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state);
-   ctx->base.saved_velem_state = INVALID_PTR;
+   if (ctx->base.saved_velem_state != INVALID_PTR) {
+      pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state);
+      ctx->base.saved_velem_state = INVALID_PTR;
+   }
 
    /* Vertex shader. */
    pipe->bind_vs_state(pipe, ctx->base.saved_vs);
@@ -573,15 +645,16 @@ static void blitter_restore_vertex_states(struct blitter_context_priv *ctx)
    ctx->base.saved_rs_state = INVALID_PTR;
 }
 
-static void blitter_check_saved_fragment_states(struct blitter_context_priv *ctx)
+static void blitter_check_saved_fragment_states(MAYBE_UNUSED struct blitter_context_priv *ctx)
 {
    assert(ctx->base.saved_fs != INVALID_PTR);
    assert(ctx->base.saved_dsa_state != INVALID_PTR);
    assert(ctx->base.saved_blend_state != INVALID_PTR);
 }
 
-static void blitter_restore_fragment_states(struct blitter_context_priv *ctx)
+void util_blitter_restore_fragment_states(struct blitter_context *blitter)
 {
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->base.pipe;
 
    /* Fragment shader. */
@@ -599,19 +672,28 @@ static void blitter_restore_fragment_states(struct blitter_context_priv *ctx)
    /* Sample mask. */
    if (ctx->base.is_sample_mask_saved) {
       pipe->set_sample_mask(pipe, ctx->base.saved_sample_mask);
-      ctx->base.is_sample_mask_saved = FALSE;
+      ctx->base.is_sample_mask_saved = false;
    }
 
    /* Miscellaneous states. */
    /* XXX check whether these are saved and whether they need to be restored
     * (depending on the operation) */
    pipe->set_stencil_ref(pipe, &ctx->base.saved_stencil_ref);
-   pipe->set_viewport_states(pipe, 0, 1, &ctx->base.saved_viewport);
+
+   if (!blitter->skip_viewport_restore)
+      pipe->set_viewport_states(pipe, 0, 1, &ctx->base.saved_viewport);
+
+   if (blitter->saved_num_window_rectangles) {
+      pipe->set_window_rectangles(pipe,
+                                  blitter->saved_window_rectangles_include,
+                                  blitter->saved_num_window_rectangles,
+                                  blitter->saved_window_rectangles);
+   }
 }
 
-static void blitter_check_saved_fb_state(struct blitter_context_priv *ctx)
+static void blitter_check_saved_fb_state(MAYBE_UNUSED struct blitter_context_priv *ctx)
 {
-   assert(ctx->base.saved_fb_state.nr_cbufs != ~0);
+   assert(ctx->base.saved_fb_state.nr_cbufs != (ubyte) ~0);
 }
 
 static void blitter_disable_render_cond(struct blitter_context_priv *ctx)
@@ -619,12 +701,13 @@ static void blitter_disable_render_cond(struct blitter_context_priv *ctx)
    struct pipe_context *pipe = ctx->base.pipe;
 
    if (ctx->base.saved_render_cond_query) {
-      pipe->render_condition(pipe, NULL, FALSE, 0);
+      pipe->render_condition(pipe, NULL, false, 0);
    }
 }
 
-static void blitter_restore_render_cond(struct blitter_context_priv *ctx)
+void util_blitter_restore_render_cond(struct blitter_context *blitter)
 {
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->base.pipe;
 
    if (ctx->base.saved_render_cond_query) {
@@ -635,22 +718,24 @@ static void blitter_restore_render_cond(struct blitter_context_priv *ctx)
    }
 }
 
-static void blitter_restore_fb_state(struct blitter_context_priv *ctx)
+void util_blitter_restore_fb_state(struct blitter_context *blitter)
 {
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->base.pipe;
 
    pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state);
    util_unreference_framebuffer_state(&ctx->base.saved_fb_state);
 }
 
-static void blitter_check_saved_textures(struct blitter_context_priv *ctx)
+static void blitter_check_saved_textures(MAYBE_UNUSED struct blitter_context_priv *ctx)
 {
-   assert(ctx->base.saved_num_sampler_states != ~0);
-   assert(ctx->base.saved_num_sampler_views != ~0);
+   assert(ctx->base.saved_num_sampler_states != ~0u);
+   assert(ctx->base.saved_num_sampler_views != ~0u);
 }
 
-static void blitter_restore_textures(struct blitter_context_priv *ctx)
+void util_blitter_restore_textures(struct blitter_context *blitter)
 {
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->base.pipe;
    unsigned i;
 
@@ -672,6 +757,15 @@ static void blitter_restore_textures(struct blitter_context_priv *ctx)
    ctx->base.saved_num_sampler_views = ~0;
 }
 
+void util_blitter_restore_constant_buffer_state(struct blitter_context *blitter)
+{
+   struct pipe_context *pipe = blitter->pipe;
+
+   pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, blitter->cb_slot,
+                            &blitter->saved_fs_constant_buffer);
+   pipe_resource_reference(&blitter->saved_fs_constant_buffer.buffer, NULL);
+}
+
 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
                                   int x1, int y1, int x2, int y2,
                                   float depth)
@@ -695,133 +789,84 @@ static void blitter_set_rectangle(struct blitter_context_priv *ctx,
       ctx->vertices[i][0][2] = depth; /*z*/
 
    /* viewport */
-   ctx->viewport.scale[0] = 0.5f * ctx->dst_width;
-   ctx->viewport.scale[1] = 0.5f * ctx->dst_height;
-   ctx->viewport.scale[2] = 1.0f;
-   ctx->viewport.translate[0] = 0.5f * ctx->dst_width;
-   ctx->viewport.translate[1] = 0.5f * ctx->dst_height;
-   ctx->viewport.translate[2] = 0.0f;
-   ctx->base.pipe->set_viewport_states(ctx->base.pipe, 0, 1, &ctx->viewport);
+   struct pipe_viewport_state viewport;
+   viewport.scale[0] = 0.5f * ctx->dst_width;
+   viewport.scale[1] = 0.5f * ctx->dst_height;
+   viewport.scale[2] = 1.0f;
+   viewport.translate[0] = 0.5f * ctx->dst_width;
+   viewport.translate[1] = 0.5f * ctx->dst_height;
+   viewport.translate[2] = 0.0f;
+   ctx->base.pipe->set_viewport_states(ctx->base.pipe, 0, 1, &viewport);
 }
 
 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
-                                    const union pipe_color_union *color)
+                                    const float color[4])
 {
    int i;
 
    if (color) {
-      for (i = 0; i < 4; i++) {
-         uint32_t *uiverts = (uint32_t *)ctx->vertices[i][1];
-         uiverts[0] = color->ui[0];
-         uiverts[1] = color->ui[1];
-         uiverts[2] = color->ui[2];
-         uiverts[3] = color->ui[3];
-      }
+      for (i = 0; i < 4; i++)
+         memcpy(&ctx->vertices[i][1][0], color, sizeof(uint32_t) * 4);
    } 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;
-      }
+      for (i = 0; i < 4; i++)
+         memset(&ctx->vertices[i][1][0], 0, sizeof(uint32_t) * 4);
    }
 }
 
 static void get_texcoords(struct pipe_sampler_view *src,
                           unsigned src_width0, unsigned src_height0,
                           int x1, int y1, int x2, int y2,
-                          float out[4])
+                          float layer, unsigned sample,
+                          bool uses_txf, union blitter_attrib *out)
 {
-   struct pipe_resource *tex = src->texture;
    unsigned level = src->u.tex.first_level;
-   boolean normalized = tex->target != PIPE_TEXTURE_RECT &&
-                        tex->nr_samples <= 1;
+   bool normalized = !uses_txf &&
+                        src->target != PIPE_TEXTURE_RECT &&
+                        src->texture->nr_samples <= 1;
 
    if (normalized) {
-      out[0] = x1 / (float)u_minify(src_width0,  level);
-      out[1] = y1 / (float)u_minify(src_height0, level);
-      out[2] = x2 / (float)u_minify(src_width0,  level);
-      out[3] = y2 / (float)u_minify(src_height0, level);
+      out->texcoord.x1 = x1 / (float)u_minify(src_width0,  level);
+      out->texcoord.y1 = y1 / (float)u_minify(src_height0, level);
+      out->texcoord.x2 = x2 / (float)u_minify(src_width0,  level);
+      out->texcoord.y2 = y2 / (float)u_minify(src_height0, level);
    } else {
-      out[0] = (float) x1;
-      out[1] = (float) y1;
-      out[2] = (float) x2;
-      out[3] = (float) y2;
+      out->texcoord.x1 = x1;
+      out->texcoord.y1 = y1;
+      out->texcoord.x2 = x2;
+      out->texcoord.y2 = y2;
    }
-}
 
-static void set_texcoords_in_vertices(const float coord[4],
-                                      float *out, unsigned stride)
-{
-   out[0] = coord[0]; /*t0.s*/
-   out[1] = coord[1]; /*t0.t*/
-   out += stride;
-   out[0] = coord[2]; /*t1.s*/
-   out[1] = coord[1]; /*t1.t*/
-   out += stride;
-   out[0] = coord[2]; /*t2.s*/
-   out[1] = coord[3]; /*t2.t*/
-   out += stride;
-   out[0] = coord[0]; /*t3.s*/
-   out[1] = coord[3]; /*t3.t*/
-}
-
-static void blitter_set_texcoords(struct blitter_context_priv *ctx,
-                                  struct pipe_sampler_view *src,
-                                  unsigned src_width0, unsigned src_height0,
-                                  float layer, unsigned sample,
-                                  int x1, int y1, int x2, int y2)
-{
-   unsigned i;
-   float coord[4];
-   float face_coord[4][2];
-
-   get_texcoords(src, src_width0, src_height0, x1, y1, x2, y2, coord);
-
-   if (src->texture->target == PIPE_TEXTURE_CUBE ||
-       src->texture->target == PIPE_TEXTURE_CUBE_ARRAY) {
-      set_texcoords_in_vertices(coord, &face_coord[0][0], 2);
-      util_map_texcoords2d_onto_cubemap((unsigned)layer % 6,
-                                        /* pointer, stride in floats */
-                                        &face_coord[0][0], 2,
-                                        &ctx->vertices[0][1][0], 8,
-                                        FALSE);
-   } else {
-      set_texcoords_in_vertices(coord, &ctx->vertices[0][1][0], 8);
-   }
+   out->texcoord.z = 0;
+   out->texcoord.w = 0;
 
    /* Set the layer. */
-   switch (src->texture->target) {
+   switch (src->target) {
    case PIPE_TEXTURE_3D:
       {
-         float r = layer / (float)u_minify(src->texture->depth0,
-                                           src->u.tex.first_level);
-         for (i = 0; i < 4; i++)
-            ctx->vertices[i][1][2] = r; /*r*/
+         float r = layer;
+
+         if (!uses_txf)
+            r /= u_minify(src->texture->depth0, src->u.tex.first_level);
+
+         out->texcoord.z = r;
       }
       break;
 
    case PIPE_TEXTURE_1D_ARRAY:
-      for (i = 0; i < 4; i++)
-         ctx->vertices[i][1][1] = (float) layer; /*t*/
+      out->texcoord.y1 = out->texcoord.y2 = layer;
       break;
 
    case PIPE_TEXTURE_2D_ARRAY:
-      for (i = 0; i < 4; i++) {
-         ctx->vertices[i][1][2] = (float) layer;  /*r*/
-         ctx->vertices[i][1][3] = (float) sample; /*q*/
-      }
+      out->texcoord.z = layer;
+      out->texcoord.w = sample;
       break;
 
    case PIPE_TEXTURE_CUBE_ARRAY:
-      for (i = 0; i < 4; i++)
-         ctx->vertices[i][1][3] = (float) ((unsigned)layer / 6); /*w*/
+      out->texcoord.w = (unsigned)layer / 6;
       break;
 
    case PIPE_TEXTURE_2D:
-      for (i = 0; i < 4; i++) {
-         ctx->vertices[i][1][3] = (float) sample; /*r*/
-      }
+      out->texcoord.w = sample;
       break;
 
    default:;
@@ -835,49 +880,94 @@ static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx,
    ctx->dst_height = height;
 }
 
+static void set_texcoords_in_vertices(const union blitter_attrib *attrib,
+                                      float *out, unsigned stride)
+{
+   out[0] = attrib->texcoord.x1;
+   out[1] = attrib->texcoord.y1;
+   out += stride;
+   out[0] = attrib->texcoord.x2;
+   out[1] = attrib->texcoord.y1;
+   out += stride;
+   out[0] = attrib->texcoord.x2;
+   out[1] = attrib->texcoord.y2;
+   out += stride;
+   out[0] = attrib->texcoord.x1;
+   out[1] = attrib->texcoord.y2;
+}
+
 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
-                                         enum pipe_format format,
+                                         enum pipe_format src_format,
+                                         enum pipe_format dst_format,
                                          enum pipe_texture_target target,
                                          unsigned src_nr_samples,
                                          unsigned dst_nr_samples,
-                                         unsigned filter)
+                                         unsigned filter,
+                                         bool use_txf)
 {
    struct pipe_context *pipe = ctx->base.pipe;
-   unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(target, src_nr_samples);
+   enum tgsi_texture_type tgsi_tex =
+      util_pipe_tex_to_tgsi_tex(target, src_nr_samples);
+   enum tgsi_return_type stype;
+   enum tgsi_return_type dtype;
+   unsigned type;
 
    assert(target < PIPE_MAX_TEXTURE_TYPES);
 
+   if (util_format_is_pure_uint(src_format)) {
+      stype = TGSI_RETURN_TYPE_UINT;
+      if (util_format_is_pure_uint(dst_format)) {
+         dtype = TGSI_RETURN_TYPE_UINT;
+         type = 0;
+      } else {
+         assert(util_format_is_pure_sint(dst_format));
+         dtype = TGSI_RETURN_TYPE_SINT;
+         type = 1;
+      }
+   } else if (util_format_is_pure_sint(src_format)) {
+      stype = TGSI_RETURN_TYPE_SINT;
+      if (util_format_is_pure_sint(dst_format)) {
+         dtype = TGSI_RETURN_TYPE_SINT;
+         type = 2;
+      } else {
+         assert(util_format_is_pure_uint(dst_format));
+         dtype = TGSI_RETURN_TYPE_UINT;
+         type = 3;
+      }
+   } else {
+      assert(!util_format_is_pure_uint(dst_format) &&
+             !util_format_is_pure_sint(dst_format));
+      dtype = stype = TGSI_RETURN_TYPE_FLOAT;
+      type = 4;
+   }
+
    if (src_nr_samples > 1) {
       void **shader;
 
-      if (dst_nr_samples <= 1) {
+      /* OpenGL requires that integer textures just copy 1 sample instead
+       * of averaging.
+       */
+      if (dst_nr_samples <= 1 &&
+          stype != TGSI_RETURN_TYPE_UINT &&
+          stype != TGSI_RETURN_TYPE_SINT) {
          /* The destination has one sample, so we'll do color resolve. */
-         boolean is_uint, is_sint;
          unsigned index = GET_MSAA_RESOLVE_FS_IDX(src_nr_samples);
 
-         is_uint = util_format_is_pure_uint(format);
-         is_sint = util_format_is_pure_sint(format);
-
          assert(filter < 2);
 
-         if (is_uint)
-            shader = &ctx->fs_resolve_uint[target][index][filter];
-         else if (is_sint)
-            shader = &ctx->fs_resolve_sint[target][index][filter];
-         else
-            shader = &ctx->fs_resolve[target][index][filter];
+         shader = &ctx->fs_resolve[target][index][filter];
 
          if (!*shader) {
             assert(!ctx->cached_all_shaders);
             if (filter == PIPE_TEX_FILTER_LINEAR) {
                *shader = util_make_fs_msaa_resolve_bilinear(pipe, tgsi_tex,
                                                    src_nr_samples,
-                                                   is_uint, is_sint);
+                                                   stype);
             }
             else {
                *shader = util_make_fs_msaa_resolve(pipe, tgsi_tex,
                                                    src_nr_samples,
-                                                   is_uint, is_sint);
+                                                   stype);
             }
          }
       }
@@ -885,34 +975,80 @@ static void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
          /* The destination has multiple samples, we'll do
           * an MSAA->MSAA copy.
           */
-         shader = &ctx->fs_texfetch_col_msaa[target];
+         shader = &ctx->fs_texfetch_col_msaa[type][target];
 
          /* Create the fragment shader on-demand. */
          if (!*shader) {
             assert(!ctx->cached_all_shaders);
-            *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex);
+            *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex, stype, dtype);
          }
       }
 
       return *shader;
    } else {
-      void **shader = &ctx->fs_texfetch_col[target];
+      void **shader;
+
+      if (use_txf)
+         shader = &ctx->fs_texfetch_col[type][target][1];
+      else
+         shader = &ctx->fs_texfetch_col[type][target][0];
 
       /* Create the fragment shader on-demand. */
       if (!*shader) {
          assert(!ctx->cached_all_shaders);
          *shader = util_make_fragment_tex_shader(pipe, tgsi_tex,
-                                                 TGSI_INTERPOLATE_LINEAR);
+                                                 TGSI_INTERPOLATE_LINEAR,
+                                                 stype, dtype,
+                                                 ctx->has_tex_lz, use_txf);
       }
 
       return *shader;
    }
 }
 
-static INLINE
+static inline
+void *blitter_get_fs_pack_color_zs(struct blitter_context_priv *ctx,
+                                   enum pipe_texture_target target,
+                                   unsigned nr_samples,
+                                   enum pipe_format zs_format,
+                                   bool dst_is_color)
+{
+   struct pipe_context *pipe = ctx->base.pipe;
+   enum tgsi_texture_type tgsi_tex =
+      util_pipe_tex_to_tgsi_tex(target, nr_samples);
+   int format_index = zs_format == PIPE_FORMAT_Z24_UNORM_S8_UINT ? 0 :
+                      zs_format == PIPE_FORMAT_S8_UINT_Z24_UNORM ? 1 :
+                      zs_format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ? 2 :
+                      zs_format == PIPE_FORMAT_Z24X8_UNORM ? 3 :
+                      zs_format == PIPE_FORMAT_X8Z24_UNORM ? 4 : -1;
+
+   if (format_index == -1) {
+      assert(0);
+      return NULL;
+   }
+
+   /* The first 5 shaders pack ZS to color, the last 5 shaders unpack color
+    * to ZS.
+    */
+   if (dst_is_color)
+      format_index += 5;
+
+   void **shader = &ctx->fs_pack_color_zs[tgsi_tex][format_index];
+
+   /* Create the fragment shader on-demand. */
+   if (!*shader) {
+      assert(!ctx->cached_all_shaders);
+      *shader = util_make_fs_pack_color_zs(pipe, tgsi_tex, zs_format,
+                                           dst_is_color);
+   }
+   return *shader;
+}
+
+static inline
 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
                                     enum pipe_texture_target target,
-                                    unsigned nr_samples)
+                                    unsigned nr_samples,
+                                    bool use_txf)
 {
    struct pipe_context *pipe = ctx->base.pipe;
 
@@ -923,7 +1059,7 @@ void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
 
       /* Create the fragment shader on-demand. */
       if (!*shader) {
-         unsigned tgsi_tex;
+         enum tgsi_texture_type tgsi_tex;
          assert(!ctx->cached_all_shaders);
          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
          *shader = util_make_fs_blit_msaa_depth(pipe, tgsi_tex);
@@ -931,26 +1067,31 @@ void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
 
       return *shader;
    } else {
-      void **shader = &ctx->fs_texfetch_depth[target];
+      void **shader;
+
+      if (use_txf)
+         shader = &ctx->fs_texfetch_depth[target][1];
+      else
+         shader = &ctx->fs_texfetch_depth[target][0];
 
       /* Create the fragment shader on-demand. */
       if (!*shader) {
-         unsigned tgsi_tex;
+         enum tgsi_texture_type tgsi_tex;
          assert(!ctx->cached_all_shaders);
          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
-         *shader =
-            util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
-                                                     TGSI_INTERPOLATE_LINEAR);
+         *shader = util_make_fs_blit_zs(pipe, PIPE_MASK_Z, tgsi_tex,
+                                        ctx->has_tex_lz, use_txf);
       }
 
       return *shader;
    }
 }
 
-static INLINE
+static inline
 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
                                            enum pipe_texture_target target,
-                                           unsigned nr_samples)
+                                           unsigned nr_samples,
+                                           bool use_txf)
 {
    struct pipe_context *pipe = ctx->base.pipe;
 
@@ -961,7 +1102,7 @@ void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
 
       /* Create the fragment shader on-demand. */
       if (!*shader) {
-         unsigned tgsi_tex;
+         enum tgsi_texture_type tgsi_tex;
          assert(!ctx->cached_all_shaders);
          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
          *shader = util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex);
@@ -969,26 +1110,31 @@ void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
 
       return *shader;
    } else {
-      void **shader = &ctx->fs_texfetch_depthstencil[target];
+      void **shader;
+
+      if (use_txf)
+         shader = &ctx->fs_texfetch_depthstencil[target][1];
+      else
+         shader = &ctx->fs_texfetch_depthstencil[target][0];
 
       /* Create the fragment shader on-demand. */
       if (!*shader) {
-         unsigned tgsi_tex;
+         enum tgsi_texture_type tgsi_tex;
          assert(!ctx->cached_all_shaders);
          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
-         *shader =
-            util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex,
-                                                     TGSI_INTERPOLATE_LINEAR);
+         *shader = util_make_fs_blit_zs(pipe, PIPE_MASK_ZS, tgsi_tex,
+                                        ctx->has_tex_lz, use_txf);
       }
 
       return *shader;
    }
 }
 
-static INLINE
+static inline
 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
                                       enum pipe_texture_target target,
-                                      unsigned nr_samples)
+                                      unsigned nr_samples,
+                                      bool use_txf)
 {
    struct pipe_context *pipe = ctx->base.pipe;
 
@@ -999,7 +1145,7 @@ void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
 
       /* Create the fragment shader on-demand. */
       if (!*shader) {
-         unsigned tgsi_tex;
+         enum tgsi_texture_type tgsi_tex;
          assert(!ctx->cached_all_shaders);
          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
          *shader = util_make_fs_blit_msaa_stencil(pipe, tgsi_tex);
@@ -1007,16 +1153,20 @@ void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
 
       return *shader;
    } else {
-      void **shader = &ctx->fs_texfetch_stencil[target];
+      void **shader;
+
+      if (use_txf)
+         shader = &ctx->fs_texfetch_stencil[target][1];
+      else
+         shader = &ctx->fs_texfetch_stencil[target][0];
 
       /* Create the fragment shader on-demand. */
       if (!*shader) {
-         unsigned tgsi_tex;
+         enum tgsi_texture_type tgsi_tex;
          assert(!ctx->cached_all_shaders);
          tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
-         *shader =
-            util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex,
-                                                       TGSI_INTERPOLATE_LINEAR);
+         *shader = util_make_fs_blit_zs(pipe, PIPE_MASK_S, tgsi_tex,
+                                        ctx->has_tex_lz, use_txf);
       }
 
       return *shader;
@@ -1035,8 +1185,8 @@ void util_blitter_cache_all_shaders(struct blitter_context *blitter)
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = blitter->pipe;
    struct pipe_screen *screen = pipe->screen;
-   unsigned samples, j, f, target, max_samples;
-   boolean has_arraytex, has_cubearraytex;
+   unsigned samples, j, f, target, max_samples, use_txf;
+   bool has_arraytex, has_cubearraytex;
 
    max_samples = ctx->has_texture_multisample ? 2 : 1;
    has_arraytex = screen->get_param(screen,
@@ -1047,49 +1197,73 @@ void util_blitter_cache_all_shaders(struct blitter_context *blitter)
    /* It only matters if i <= 1 or > 1. */
    for (samples = 1; samples <= max_samples; samples++) {
       for (target = PIPE_TEXTURE_1D; target < PIPE_MAX_TEXTURE_TYPES; target++) {
-         if (!has_arraytex &&
-             (target == PIPE_TEXTURE_1D_ARRAY ||
-              target == PIPE_TEXTURE_2D_ARRAY)) {
-            continue;
-         }
-         if (!has_cubearraytex &&
-             (target == PIPE_TEXTURE_CUBE_ARRAY))
-            continue;
-
-        if (samples > 1 &&
-            (target != PIPE_TEXTURE_2D &&
-             target != PIPE_TEXTURE_2D_ARRAY))
-           continue;
+         for (use_txf = 0; use_txf <= ctx->has_txf; use_txf++) {
+            if (!has_arraytex &&
+                (target == PIPE_TEXTURE_1D_ARRAY ||
+                 target == PIPE_TEXTURE_2D_ARRAY)) {
+               continue;
+            }
+            if (!has_cubearraytex &&
+                (target == PIPE_TEXTURE_CUBE_ARRAY))
+               continue;
 
-         /* If samples == 1, the shaders read one texel. If samples >= 1,
-          * they read one sample.
-          */
-         blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT, target,
-                                     samples, samples, 0);
-         blitter_get_fs_texfetch_depth(ctx, target, samples);
-         if (ctx->has_stencil_export) {
-            blitter_get_fs_texfetch_depthstencil(ctx, target, samples);
-            blitter_get_fs_texfetch_stencil(ctx, target, samples);
-         }
+            if (samples > 1 &&
+                (target != PIPE_TEXTURE_2D &&
+                 target != PIPE_TEXTURE_2D_ARRAY))
+               continue;
 
-         if (samples == 1)
-            continue;
+            if (samples > 1 && use_txf)
+               continue; /* TXF is the only option, use_txf has no effect */
+
+            /* If samples == 1, the shaders read one texel. If samples >= 1,
+             * they read one sample.
+             */
+            blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT,
+                                        PIPE_FORMAT_R32_FLOAT, target,
+                                        samples, samples, 0, use_txf);
+            blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
+                                        PIPE_FORMAT_R32_UINT, target,
+                                        samples, samples, 0, use_txf);
+            blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
+                                        PIPE_FORMAT_R32_SINT, target,
+                                        samples, samples, 0, use_txf);
+            blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
+                                        PIPE_FORMAT_R32_SINT, target,
+                                        samples, samples, 0, use_txf);
+            blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
+                                        PIPE_FORMAT_R32_UINT, target,
+                                        samples, samples, 0, use_txf);
+            blitter_get_fs_texfetch_depth(ctx, target, samples, use_txf);
+            if (ctx->has_stencil_export) {
+               blitter_get_fs_texfetch_depthstencil(ctx, target, samples, use_txf);
+               blitter_get_fs_texfetch_stencil(ctx, target, samples, use_txf);
+            }
 
-         /* MSAA resolve shaders. */
-         for (j = 2; j < 32; j++) {
-            if (!screen->is_format_supported(screen, PIPE_FORMAT_R32_FLOAT,
-                                             target, j,
-                                             PIPE_BIND_SAMPLER_VIEW)) {
+            if (samples == 1)
                continue;
-            }
 
-            for (f = 0; f < 2; f++) {
-               blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT, target,
-                                           j, 1, f);
-               blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT, target,
-                                           j, 1, f);
-               blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT, target,
-                                           j, 1, f);
+            /* MSAA resolve shaders. */
+            for (j = 2; j < 32; j++) {
+               if (!screen->is_format_supported(screen, PIPE_FORMAT_R32_FLOAT,
+                                                target, j, j,
+                                                PIPE_BIND_SAMPLER_VIEW)) {
+                  continue;
+               }
+
+               for (f = 0; f < 2; f++) {
+                  if (f != PIPE_TEX_FILTER_NEAREST && use_txf)
+                     continue;
+
+                  blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT,
+                                              PIPE_FORMAT_R32_FLOAT, target,
+                                              j, 1, f, use_txf);
+                  blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
+                                              PIPE_FORMAT_R32_UINT, target,
+                                              j, 1, f, use_txf);
+                  blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
+                                              PIPE_FORMAT_R32_SINT, target,
+                                              j, 1, f, use_txf);
+               }
             }
          }
       }
@@ -1099,27 +1273,24 @@ void util_blitter_cache_all_shaders(struct blitter_context *blitter)
 
    ctx->fs_write_one_cbuf =
       util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
-                                            TGSI_INTERPOLATE_CONSTANT, FALSE);
+                                            TGSI_INTERPOLATE_CONSTANT, false);
 
    ctx->fs_write_all_cbufs =
       util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
-                                            TGSI_INTERPOLATE_CONSTANT, TRUE);
+                                            TGSI_INTERPOLATE_CONSTANT, true);
 
-   ctx->cached_all_shaders = TRUE;
+   ctx->cached_all_shaders = true;
 }
 
 static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx,
-                                               boolean scissor,
-                                               boolean vs_layered)
+                                               bool scissor, bool msaa)
 {
    struct pipe_context *pipe = ctx->base.pipe;
 
-   pipe->bind_rasterizer_state(pipe, scissor ? ctx->rs_state_scissor
-                                             : ctx->rs_state);
-   if (vs_layered)
-      bind_vs_layered(ctx);
-   else
-      bind_vs_passthrough(ctx);
+   if (ctx->base.saved_num_window_rectangles)
+      pipe->set_window_rectangles(pipe, false, 0, NULL);
+
+   pipe->bind_rasterizer_state(pipe, ctx->rs_state[scissor][msaa]);
 
    if (ctx->has_geometry_shader)
       pipe->bind_gs_state(pipe, NULL);
@@ -1132,6 +1303,8 @@ static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx,
 }
 
 static void blitter_draw(struct blitter_context_priv *ctx,
+                         void *vertex_elements_cso,
+                         blitter_get_vs_func get_vs,
                          int x1, int y1, int x2, int y2, float depth,
                          unsigned num_instances)
 {
@@ -1142,36 +1315,63 @@ static void blitter_draw(struct blitter_context_priv *ctx,
 
    vb.stride = 8 * sizeof(float);
 
-   u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), ctx->vertices,
-                 &vb.buffer_offset, &vb.buffer);
-   u_upload_unmap(ctx->upload);
+   u_upload_data(pipe->stream_uploader, 0, sizeof(ctx->vertices), 4, ctx->vertices,
+                 &vb.buffer_offset, &vb.buffer.resource);
+   if (!vb.buffer.resource)
+      return;
+   u_upload_unmap(pipe->stream_uploader);
 
    pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
-   util_draw_arrays_instanced(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
-                              0, num_instances);
-   pipe_resource_reference(&vb.buffer, NULL);
+   pipe->bind_vertex_elements_state(pipe, vertex_elements_cso);
+   pipe->bind_vs_state(pipe, get_vs(&ctx->base));
+
+   if (ctx->base.use_index_buffer) {
+      /* Note that for V3D,
+       * dEQP-GLES3.functional.fbo.blit.rect.nearest_consistency_* require
+       * that the last vert of the two tris be the same.
+       */
+      static uint8_t indices[6] = { 0, 1, 2, 0, 3, 2 };
+      util_draw_elements_instanced(pipe, indices, 1, 0,
+                                   PIPE_PRIM_TRIANGLES, 0, 6,
+                                   0, num_instances);
+   } else {
+      util_draw_arrays_instanced(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
+                                 0, num_instances);
+   }
+   pipe_resource_reference(&vb.buffer.resource, NULL);
 }
 
 void util_blitter_draw_rectangle(struct blitter_context *blitter,
-                                 int x1, int y1, int x2, int y2, float depth,
+                                 void *vertex_elements_cso,
+                                 blitter_get_vs_func get_vs,
+                                 int x1, int y1, int x2, int y2,
+                                 float depth, unsigned num_instances,
                                  enum blitter_attrib_type type,
-                                 const union pipe_color_union *attrib)
+                                 const union blitter_attrib *attrib)
 {
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
+   unsigned i;
 
    switch (type) {
       case UTIL_BLITTER_ATTRIB_COLOR:
-         blitter_set_clear_color(ctx, attrib);
+         blitter_set_clear_color(ctx, attrib->color);
          break;
 
-      case UTIL_BLITTER_ATTRIB_TEXCOORD:
-         set_texcoords_in_vertices(attrib->f, &ctx->vertices[0][1][0], 8);
+      case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW:
+         for (i = 0; i < 4; i++) {
+            ctx->vertices[i][1][2] = attrib->texcoord.z;
+            ctx->vertices[i][1][3] = attrib->texcoord.w;
+         }
+         /* fall through */
+      case UTIL_BLITTER_ATTRIB_TEXCOORD_XY:
+         set_texcoords_in_vertices(attrib, &ctx->vertices[0][1][0], 8);
          break;
 
       default:;
    }
 
-   blitter_draw(ctx, x1, y1, x2, y2, depth, 1);
+   blitter_draw(ctx, vertex_elements_cso, get_vs, x1, y1, x2, y2, depth,
+                num_instances);
 }
 
 static void *get_clear_blend_state(struct blitter_context_priv *ctx,
@@ -1184,7 +1384,7 @@ static void *get_clear_blend_state(struct blitter_context_priv *ctx,
 
    /* Return an existing blend state. */
    if (!clear_buffers)
-      return ctx->blend[0];
+      return ctx->blend[0][0];
 
    index = GET_CLEAR_BLEND_STATE_IDX(clear_buffers);
 
@@ -1209,21 +1409,15 @@ static void *get_clear_blend_state(struct blitter_context_priv *ctx,
    return ctx->blend_clear[index];
 }
 
-static void util_blitter_clear_custom(struct blitter_context *blitter,
-                                      unsigned width, unsigned height,
-                                      unsigned num_layers,
-                                      unsigned clear_buffers,
-                                      const union pipe_color_union *color,
-                                      double depth, unsigned stencil,
-                                      void *custom_blend, void *custom_dsa)
+void util_blitter_common_clear_setup(struct blitter_context *blitter,
+                                     unsigned width, unsigned height,
+                                     unsigned clear_buffers,
+                                     void *custom_blend, void *custom_dsa)
 {
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->base.pipe;
-   struct pipe_stencil_ref sr = { { 0 } };
-
-   assert(ctx->has_layered || num_layers <= 1);
 
-   blitter_set_running_flag(ctx);
+   util_blitter_set_running_flag(blitter);
    blitter_check_saved_vertex_states(ctx);
    blitter_check_saved_fragment_states(ctx);
    blitter_disable_render_cond(ctx);
@@ -1247,50 +1441,86 @@ static void util_blitter_clear_custom(struct blitter_context *blitter,
       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
    }
 
+   pipe->set_sample_mask(pipe, ~0);
+   blitter_set_dst_dimensions(ctx, width, height);
+}
+
+static void util_blitter_clear_custom(struct blitter_context *blitter,
+                                      unsigned width, unsigned height,
+                                      unsigned num_layers,
+                                      unsigned clear_buffers,
+                                      const union pipe_color_union *color,
+                                      double depth, unsigned stencil,
+                                      void *custom_blend, void *custom_dsa,
+                                      bool msaa)
+{
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
+   struct pipe_context *pipe = ctx->base.pipe;
+   struct pipe_stencil_ref sr = { { 0 } };
+
+   assert(ctx->has_layered || num_layers <= 1);
+
+   util_blitter_common_clear_setup(blitter, width, height, clear_buffers,
+                                   custom_blend, custom_dsa);
+
    sr.ref_value[0] = stencil & 0xff;
    pipe->set_stencil_ref(pipe, &sr);
 
-   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
    bind_fs_write_all_cbufs(ctx);
-   pipe->set_sample_mask(pipe, ~0);
 
-   blitter_set_dst_dimensions(ctx, width, height);
+   union blitter_attrib attrib;
+   memcpy(attrib.color, color->ui, sizeof(color->ui));
+
+   bool pass_generic = (clear_buffers & PIPE_CLEAR_COLOR) != 0;
+   enum blitter_attrib_type type = pass_generic ? UTIL_BLITTER_ATTRIB_COLOR :
+                                                  UTIL_BLITTER_ATTRIB_NONE;
 
    if (num_layers > 1 && ctx->has_layered) {
-      blitter_set_common_draw_rect_state(ctx, FALSE, TRUE);
-      blitter_set_clear_color(ctx, color);
-      blitter_draw(ctx, 0, 0, width, height, depth, num_layers);
-   }
-   else {
-      blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
-      blitter->draw_rectangle(blitter, 0, 0, width, height, (float) depth,
-                              UTIL_BLITTER_ATTRIB_COLOR, color);
+      blitter_get_vs_func get_vs = get_vs_layered;
+
+      blitter_set_common_draw_rect_state(ctx, false, msaa);
+      blitter->draw_rectangle(blitter, ctx->velem_state, get_vs,
+                              0, 0, width, height,
+                              (float) depth, num_layers, type, &attrib);
+   } else {
+      blitter_get_vs_func get_vs;
+
+      if (pass_generic)
+         get_vs = get_vs_passthrough_pos_generic;
+      else
+         get_vs = get_vs_passthrough_pos;
+
+      blitter_set_common_draw_rect_state(ctx, false, msaa);
+      blitter->draw_rectangle(blitter, ctx->velem_state, get_vs,
+                              0, 0, width, height,
+                              (float) depth, 1, type, &attrib);
    }
 
-   blitter_restore_vertex_states(ctx);
-   blitter_restore_fragment_states(ctx);
-   blitter_restore_render_cond(ctx);
-   blitter_unset_running_flag(ctx);
+   util_blitter_restore_vertex_states(blitter);
+   util_blitter_restore_fragment_states(blitter);
+   util_blitter_restore_render_cond(blitter);
+   util_blitter_unset_running_flag(blitter);
 }
 
 void util_blitter_clear(struct blitter_context *blitter,
                         unsigned width, unsigned height, unsigned num_layers,
                         unsigned clear_buffers,
                         const union pipe_color_union *color,
-                        double depth, unsigned stencil)
+                        double depth, unsigned stencil,
+                        bool msaa)
 {
    util_blitter_clear_custom(blitter, width, height, num_layers,
                              clear_buffers, color, depth, stencil,
-                             NULL, NULL);
+                             NULL, NULL, msaa);
 }
 
 void util_blitter_custom_clear_depth(struct blitter_context *blitter,
                                      unsigned width, unsigned height,
                                      double depth, void *custom_dsa)
 {
-    static const union pipe_color_union color;
-    util_blitter_clear_custom(blitter, width, height, 0, 0, &color, depth, 0,
-                              NULL, custom_dsa);
+   static const union pipe_color_union color;
+   util_blitter_clear_custom(blitter, width, height, 0, 0, &color, depth, 0,
+                             NULL, custom_dsa, false);
 }
 
 void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
@@ -1298,11 +1528,11 @@ void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
                                       unsigned dstlevel,
                                       unsigned dstz)
 {
-    memset(dst_templ, 0, sizeof(*dst_templ));
-    dst_templ->format = util_format_linear(dst->format);
-    dst_templ->u.tex.level = dstlevel;
-    dst_templ->u.tex.first_layer = dstz;
-    dst_templ->u.tex.last_layer = dstz;
+   memset(dst_templ, 0, sizeof(*dst_templ));
+   dst_templ->format = util_format_linear(dst->format);
+   dst_templ->u.tex.level = dstlevel;
+   dst_templ->u.tex.first_layer = dstz;
+   dst_templ->u.tex.last_layer = dstz;
 }
 
 static struct pipe_surface *
@@ -1320,31 +1550,41 @@ util_blitter_get_next_surface_layer(struct pipe_context *pipe,
    return pipe->create_surface(pipe, surf->texture, &dst_templ);
 }
 
-void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
+void util_blitter_default_src_texture(struct blitter_context *blitter,
+                                      struct pipe_sampler_view *src_templ,
                                       struct pipe_resource *src,
                                       unsigned srclevel)
 {
-    memset(src_templ, 0, sizeof(*src_templ));
-    src_templ->target = src->target;
-    src_templ->format = util_format_linear(src->format);
-    src_templ->u.tex.first_level = srclevel;
-    src_templ->u.tex.last_level = srclevel;
-    src_templ->u.tex.first_layer = 0;
-    src_templ->u.tex.last_layer =
-        src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
-                                       : src->array_size - 1;
-    src_templ->swizzle_r = PIPE_SWIZZLE_RED;
-    src_templ->swizzle_g = PIPE_SWIZZLE_GREEN;
-    src_templ->swizzle_b = PIPE_SWIZZLE_BLUE;
-    src_templ->swizzle_a = PIPE_SWIZZLE_ALPHA;
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
+
+   memset(src_templ, 0, sizeof(*src_templ));
+
+   if (ctx->cube_as_2darray &&
+       (src->target == PIPE_TEXTURE_CUBE ||
+        src->target == PIPE_TEXTURE_CUBE_ARRAY))
+      src_templ->target = PIPE_TEXTURE_2D_ARRAY;
+   else
+      src_templ->target = src->target;
+
+   src_templ->format = util_format_linear(src->format);
+   src_templ->u.tex.first_level = srclevel;
+   src_templ->u.tex.last_level = srclevel;
+   src_templ->u.tex.first_layer = 0;
+   src_templ->u.tex.last_layer =
+      src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
+                                     : (unsigned)(src->array_size - 1);
+   src_templ->swizzle_r = PIPE_SWIZZLE_X;
+   src_templ->swizzle_g = PIPE_SWIZZLE_Y;
+   src_templ->swizzle_b = PIPE_SWIZZLE_Z;
+   src_templ->swizzle_a = PIPE_SWIZZLE_W;
 }
 
-static boolean is_blit_generic_supported(struct blitter_context *blitter,
-                                         const struct pipe_resource *dst,
-                                         enum pipe_format dst_format,
-                                         const struct pipe_resource *src,
-                                         enum pipe_format src_format,
-                                         unsigned mask)
+static bool is_blit_generic_supported(struct blitter_context *blitter,
+                                      const struct pipe_resource *dst,
+                                      enum pipe_format dst_format,
+                                      const struct pipe_resource *src,
+                                      enum pipe_format src_format,
+                                      unsigned mask)
 {
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_screen *screen = ctx->base.pipe->screen;
@@ -1353,12 +1593,12 @@ static boolean is_blit_generic_supported(struct blitter_context *blitter,
       unsigned bind;
       const struct util_format_description *desc =
             util_format_description(dst_format);
-      boolean dst_has_stencil = util_format_has_stencil(desc);
+      bool dst_has_stencil = util_format_has_stencil(desc);
 
       /* Stencil export must be supported for stencil copy. */
       if ((mask & PIPE_MASK_S) && dst_has_stencil &&
           !ctx->has_stencil_export) {
-         return FALSE;
+         return false;
       }
 
       if (dst_has_stencil || util_format_has_depth(desc))
@@ -1367,19 +1607,21 @@ static boolean is_blit_generic_supported(struct blitter_context *blitter,
          bind = PIPE_BIND_RENDER_TARGET;
 
       if (!screen->is_format_supported(screen, dst_format, dst->target,
-                                       dst->nr_samples, bind)) {
-         return FALSE;
+                                       dst->nr_samples, dst->nr_storage_samples,
+                                       bind)) {
+         return false;
       }
    }
 
    if (src) {
       if (src->nr_samples > 1 && !ctx->has_texture_multisample) {
-         return FALSE;
+         return false;
       }
 
       if (!screen->is_format_supported(screen, src_format, src->target,
-                                 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
-         return FALSE;
+                                       src->nr_samples, src->nr_storage_samples,
+                                       PIPE_BIND_SAMPLER_VIEW)) {
+         return false;
       }
 
       /* Check stencil sampler support for stencil copy. */
@@ -1392,26 +1634,27 @@ static boolean is_blit_generic_supported(struct blitter_context *blitter,
             if (stencil_format != src_format &&
                 !screen->is_format_supported(screen, stencil_format,
                                              src->target, src->nr_samples,
+                                             src->nr_storage_samples,
                                              PIPE_BIND_SAMPLER_VIEW)) {
-               return FALSE;
+               return false;
             }
          }
       }
    }
 
-   return TRUE;
+   return true;
 }
 
-boolean util_blitter_is_copy_supported(struct blitter_context *blitter,
-                                       const struct pipe_resource *dst,
-                                       const struct pipe_resource *src)
+bool util_blitter_is_copy_supported(struct blitter_context *blitter,
+                                    const struct pipe_resource *dst,
+                                    const struct pipe_resource *src)
 {
    return is_blit_generic_supported(blitter, dst, dst->format,
                                     src, src->format, PIPE_MASK_RGBAZS);
 }
 
-boolean util_blitter_is_blit_supported(struct blitter_context *blitter,
-                                      const struct pipe_blit_info *info)
+bool util_blitter_is_blit_supported(struct blitter_context *blitter,
+                                    const struct pipe_blit_info *info)
 {
    return is_blit_generic_supported(blitter,
                                     info->dst.resource, info->dst.format,
@@ -1444,18 +1687,181 @@ void util_blitter_copy_texture(struct blitter_context *blitter,
    dst_view = pipe->create_surface(pipe, dst, &dst_templ);
 
    /* Initialize the sampler view. */
-   util_blitter_default_src_texture(&src_templ, src, src_level);
+   util_blitter_default_src_texture(blitter, &src_templ, src, src_level);
    src_view = pipe->create_sampler_view(pipe, src, &src_templ);
 
    /* Copy. */
    util_blitter_blit_generic(blitter, dst_view, &dstbox,
                              src_view, srcbox, src->width0, src->height0,
-                             PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
+                             PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
+                             false);
 
    pipe_surface_reference(&dst_view, NULL);
    pipe_sampler_view_reference(&src_view, NULL);
 }
 
+static void
+blitter_draw_tex(struct blitter_context_priv *ctx,
+                 int dst_x1, int dst_y1, int dst_x2, int dst_y2,
+                 struct pipe_sampler_view *src,
+                 unsigned src_width0, unsigned src_height0,
+                 int src_x1, int src_y1, int src_x2, int src_y2,
+                 float layer, unsigned sample,
+                 bool uses_txf, enum blitter_attrib_type type)
+{
+   union blitter_attrib coord;
+   blitter_get_vs_func get_vs = get_vs_passthrough_pos_generic;
+
+   get_texcoords(src, src_width0, src_height0,
+                 src_x1, src_y1, src_x2, src_y2, layer, sample,
+                 uses_txf, &coord);
+
+   if (src->target == PIPE_TEXTURE_CUBE ||
+       src->target == PIPE_TEXTURE_CUBE_ARRAY) {
+      float face_coord[4][2];
+
+      set_texcoords_in_vertices(&coord, &face_coord[0][0], 2);
+      util_map_texcoords2d_onto_cubemap((unsigned)layer % 6,
+                                        /* pointer, stride in floats */
+                                        &face_coord[0][0], 2,
+                                        &ctx->vertices[0][1][0], 8,
+                                        false);
+      for (unsigned i = 0; i < 4; i++)
+         ctx->vertices[i][1][3] = coord.texcoord.w;
+
+      /* Cubemaps don't use draw_rectangle. */
+      blitter_draw(ctx, ctx->velem_state, get_vs,
+                   dst_x1, dst_y1, dst_x2, dst_y2, 0, 1);
+   } else {
+      ctx->base.draw_rectangle(&ctx->base, ctx->velem_state, get_vs,
+                               dst_x1, dst_y1, dst_x2, dst_y2,
+                               0, 1, type, &coord);
+   }
+}
+
+static void do_blits(struct blitter_context_priv *ctx,
+                     struct pipe_surface *dst,
+                     const struct pipe_box *dstbox,
+                     struct pipe_sampler_view *src,
+                     unsigned src_width0,
+                     unsigned src_height0,
+                     const struct pipe_box *srcbox,
+                     bool is_zsbuf,
+                     bool uses_txf)
+{
+   struct pipe_context *pipe = ctx->base.pipe;
+   unsigned src_samples = src->texture->nr_samples;
+   unsigned dst_samples = dst->texture->nr_samples;
+   enum pipe_texture_target src_target = src->target;
+   struct pipe_framebuffer_state fb_state = {0};
+
+   /* Initialize framebuffer state. */
+   fb_state.width = dst->width;
+   fb_state.height = dst->height;
+   fb_state.nr_cbufs = is_zsbuf ? 0 : 1;
+
+   blitter_set_dst_dimensions(ctx, fb_state.width, fb_state.height);
+
+   if ((src_target == PIPE_TEXTURE_1D ||
+        src_target == PIPE_TEXTURE_2D ||
+        src_target == PIPE_TEXTURE_RECT) &&
+       src_samples <= 1) {
+      /* Set framebuffer state. */
+      if (is_zsbuf) {
+         fb_state.zsbuf = dst;
+      } else {
+         fb_state.cbufs[0] = dst;
+      }
+      pipe->set_framebuffer_state(pipe, &fb_state);
+
+      /* Draw. */
+      pipe->set_sample_mask(pipe, ~0);
+      blitter_draw_tex(ctx, dstbox->x, dstbox->y,
+                       dstbox->x + dstbox->width,
+                       dstbox->y + dstbox->height,
+                       src, src_width0, src_height0, srcbox->x, srcbox->y,
+                       srcbox->x + srcbox->width, srcbox->y + srcbox->height,
+                       0, 0, uses_txf, UTIL_BLITTER_ATTRIB_TEXCOORD_XY);
+   } else {
+      /* Draw the quad with the generic codepath. */
+      int dst_z;
+      for (dst_z = 0; dst_z < dstbox->depth; dst_z++) {
+         struct pipe_surface *old;
+         float dst2src_scale = srcbox->depth / (float)dstbox->depth;
+
+         /* Scale Z properly if the blit is scaled.
+          *
+          * When downscaling, we want the coordinates centered, so that
+          * mipmapping works for 3D textures. For example, when generating
+          * a 4x4x4 level, this wouldn't average the pixels:
+          *
+          *   src Z:  0 1 2 3 4 5 6 7
+          *   dst Z:  0   1   2   3
+          *
+          * Because the pixels are not centered below the pixels of the higher
+          * level. Therefore, we want this:
+          *   src Z:  0 1 2 3 4 5 6 7
+          *   dst Z:   0   1   2   3
+          *
+          * dst_offset defines the offset needed for centering the pixels and
+          * it works with any scaling (not just 2x).
+          */
+         float dst_offset = ((srcbox->depth - 1) -
+                             (dstbox->depth - 1) * dst2src_scale) * 0.5;
+         float src_z = (dst_z + dst_offset) * dst2src_scale;
+
+         /* Set framebuffer state. */
+         if (is_zsbuf) {
+            fb_state.zsbuf = dst;
+         } else {
+            fb_state.cbufs[0] = dst;
+         }
+         pipe->set_framebuffer_state(pipe, &fb_state);
+
+         /* See if we need to blit a multisample or singlesample buffer. */
+         if (src_samples == dst_samples && dst_samples > 1) {
+            /* MSAA copy. */
+            unsigned i, max_sample = dst_samples - 1;
+
+            for (i = 0; i <= max_sample; i++) {
+               pipe->set_sample_mask(pipe, 1 << i);
+               blitter_draw_tex(ctx, dstbox->x, dstbox->y,
+                                dstbox->x + dstbox->width,
+                                dstbox->y + dstbox->height,
+                                src, src_width0, src_height0,
+                                srcbox->x, srcbox->y,
+                                srcbox->x + srcbox->width,
+                                srcbox->y + srcbox->height,
+                                srcbox->z + src_z, i, uses_txf,
+                                UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW);
+            }
+         } else {
+            /* Normal copy, MSAA upsampling, or MSAA resolve. */
+            pipe->set_sample_mask(pipe, ~0);
+            blitter_draw_tex(ctx, dstbox->x, dstbox->y,
+                             dstbox->x + dstbox->width,
+                             dstbox->y + dstbox->height,
+                             src, src_width0, src_height0,
+                             srcbox->x, srcbox->y,
+                             srcbox->x + srcbox->width,
+                             srcbox->y + srcbox->height,
+                             srcbox->z + src_z, 0, uses_txf,
+                             UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW);
+         }
+
+         /* Get the next surface or (if this is the last iteration)
+          * just unreference the last one. */
+         old = dst;
+         if (dst_z < dstbox->depth-1) {
+            dst = util_blitter_get_next_surface_layer(ctx->base.pipe, dst);
+         }
+         if (dst_z) {
+            pipe_surface_reference(&old, NULL);
+         }
+      }
+   }
+}
+
 void util_blitter_blit_generic(struct blitter_context *blitter,
                                struct pipe_surface *dst,
                                const struct pipe_box *dstbox,
@@ -1463,88 +1869,147 @@ void util_blitter_blit_generic(struct blitter_context *blitter,
                                const struct pipe_box *srcbox,
                                unsigned src_width0, unsigned src_height0,
                                unsigned mask, unsigned filter,
-                               const struct pipe_scissor_state *scissor)
+                               const struct pipe_scissor_state *scissor,
+                               bool alpha_blend)
 {
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->base.pipe;
-   struct pipe_framebuffer_state fb_state;
-   enum pipe_texture_target src_target = src->texture->target;
+   enum pipe_texture_target src_target = src->target;
    unsigned src_samples = src->texture->nr_samples;
    unsigned dst_samples = dst->texture->nr_samples;
-   boolean has_depth, has_stencil, has_color;
-   boolean blit_stencil, blit_depth, blit_color;
    void *sampler_state;
    const struct util_format_description *src_desc =
          util_format_description(src->format);
    const struct util_format_description *dst_desc =
          util_format_description(dst->format);
 
-   has_color = src_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS &&
-               dst_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
-   has_depth = util_format_has_depth(src_desc) &&
-               util_format_has_depth(dst_desc);
-   has_stencil = util_format_has_stencil(src_desc) &&
-                 util_format_has_stencil(dst_desc);
+   bool src_has_color = src_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
+   bool src_has_depth = util_format_has_depth(src_desc);
+   bool src_has_stencil = util_format_has_stencil(src_desc);
 
-   blit_color = has_color && (mask & PIPE_MASK_RGBA);
-   blit_depth = has_depth && (mask & PIPE_MASK_Z);
-   blit_stencil = has_stencil && (mask & PIPE_MASK_S) &&
-                  ctx->has_stencil_export;
+   bool dst_has_color = mask & PIPE_MASK_RGBA &&
+                        dst_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
+   bool dst_has_depth = mask & PIPE_MASK_Z &&
+                        util_format_has_depth(dst_desc);
+   bool dst_has_stencil = ctx->has_stencil_export &&
+                          mask & PIPE_MASK_S &&
+                          util_format_has_stencil(dst_desc);
 
-   if (!blit_stencil && !blit_depth && !blit_color) {
+   /* Return if there is nothing to do. */
+   if (!dst_has_color && !dst_has_depth && !dst_has_stencil) {
       return;
    }
 
-   if (blit_stencil ||
-       (dstbox->width == abs(srcbox->width) &&
-        dstbox->height == abs(srcbox->height))) {
+   bool is_scaled = dstbox->width != abs(srcbox->width) ||
+                    dstbox->height != abs(srcbox->height);
+
+   if (src_has_stencil || !is_scaled)
       filter = PIPE_TEX_FILTER_NEAREST;
+
+   bool use_txf = false;
+
+   /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
+   if (ctx->has_txf &&
+       !is_scaled &&
+       filter == PIPE_TEX_FILTER_NEAREST &&
+       src->target != PIPE_TEXTURE_CUBE &&
+       src->target != PIPE_TEXTURE_CUBE_ARRAY) {
+      int src_width = u_minify(src_width0, src->u.tex.first_level);
+      int src_height = u_minify(src_height0, src->u.tex.first_level);
+      int src_depth = src->u.tex.last_layer + 1;
+      struct pipe_box box = *srcbox;
+
+      /* Eliminate negative width/height/depth. */
+      if (box.width < 0) {
+         box.x += box.width;
+         box.width *= -1;
+      }
+      if (box.height < 0) {
+         box.y += box.height;
+         box.height *= -1;
+      }
+      if (box.depth < 0) {
+         box.z += box.depth;
+         box.depth *= -1;
+      }
+
+      /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
+      use_txf =
+         box.x >= 0 && box.x < src_width &&
+         box.y >= 0 && box.y < src_height &&
+         box.z >= 0 && box.z < src_depth &&
+         box.x + box.width > 0 && box.x + box.width <= src_width &&
+         box.y + box.height > 0 && box.y + box.height <= src_height &&
+         box.z + box.depth > 0 && box.z + box.depth <= src_depth;
    }
 
    /* Check whether the states are properly saved. */
-   blitter_set_running_flag(ctx);
+   util_blitter_set_running_flag(blitter);
    blitter_check_saved_vertex_states(ctx);
    blitter_check_saved_fragment_states(ctx);
    blitter_check_saved_textures(ctx);
    blitter_check_saved_fb_state(ctx);
    blitter_disable_render_cond(ctx);
 
-   /* Initialize framebuffer state. */
-   fb_state.width = dst->width;
-   fb_state.height = dst->height;
-   fb_state.nr_cbufs = blit_depth || blit_stencil ? 0 : 1;
-   fb_state.cbufs[0] = NULL;
-   fb_state.zsbuf = NULL;
-
-   if (blit_depth || blit_stencil) {
-      pipe->bind_blend_state(pipe, ctx->blend[0]);
-
-      if (blit_depth && blit_stencil) {
-         pipe->bind_depth_stencil_alpha_state(pipe,
-                                              ctx->dsa_write_depth_stencil);
+   /* Blend, DSA, fragment shader. */
+   if (dst_has_depth && dst_has_stencil) {
+      pipe->bind_blend_state(pipe, ctx->blend[0][0]);
+      pipe->bind_depth_stencil_alpha_state(pipe,
+                                           ctx->dsa_write_depth_stencil);
+      if (src_has_color) {
+         assert(use_txf);
          ctx->bind_fs_state(pipe,
-               blitter_get_fs_texfetch_depthstencil(ctx, src_target,
-                                                    src_samples));
-      } else if (blit_depth) {
-         pipe->bind_depth_stencil_alpha_state(pipe,
-                                              ctx->dsa_write_depth_keep_stencil);
+            blitter_get_fs_pack_color_zs(ctx, src_target,
+                                         src_samples, dst->format, false));
+      } else {
+         ctx->bind_fs_state(pipe,
+            blitter_get_fs_texfetch_depthstencil(ctx, src_target,
+                                                 src_samples, use_txf));
+      }
+   } else if (dst_has_depth) {
+      pipe->bind_blend_state(pipe, ctx->blend[0][0]);
+      pipe->bind_depth_stencil_alpha_state(pipe,
+                                           ctx->dsa_write_depth_keep_stencil);
+      if (src_has_color &&
+          (src->format == PIPE_FORMAT_R32_UINT ||
+           src->format == PIPE_FORMAT_R32G32_UINT)) {
+         assert(use_txf);
          ctx->bind_fs_state(pipe,
-               blitter_get_fs_texfetch_depth(ctx, src_target,
-                                             src_samples));
-      } else { /* is_stencil */
-         pipe->bind_depth_stencil_alpha_state(pipe,
-                                              ctx->dsa_keep_depth_write_stencil);
+            blitter_get_fs_pack_color_zs(ctx, src_target,
+                                         src_samples, dst->format, false));
+      } else {
          ctx->bind_fs_state(pipe,
-               blitter_get_fs_texfetch_stencil(ctx, src_target,
-                                               src_samples));
+            blitter_get_fs_texfetch_depth(ctx, src_target,
+                                          src_samples, use_txf));
       }
+   } else if (dst_has_stencil) {
+      pipe->bind_blend_state(pipe, ctx->blend[0][0]);
+      pipe->bind_depth_stencil_alpha_state(pipe,
+                                           ctx->dsa_keep_depth_write_stencil);
 
+      assert(src_has_stencil); /* unpacking from color is unsupported */
+      ctx->bind_fs_state(pipe,
+         blitter_get_fs_texfetch_stencil(ctx, src_target,
+                                         src_samples, use_txf));
    } else {
-      pipe->bind_blend_state(pipe, ctx->blend[mask & PIPE_MASK_RGBA]);
+      unsigned colormask = mask & PIPE_MASK_RGBA;
+
+      pipe->bind_blend_state(pipe, ctx->blend[colormask][alpha_blend]);
       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
-      ctx->bind_fs_state(pipe,
-            blitter_get_fs_texfetch_col(ctx, src->format, src_target,
-                                        src_samples, dst_samples, filter));
+
+      if (src_has_depth &&
+          (dst->format == PIPE_FORMAT_R32_UINT ||
+           dst->format == PIPE_FORMAT_R32G32_UINT)) {
+         assert(use_txf);
+         ctx->bind_fs_state(pipe,
+            blitter_get_fs_pack_color_zs(ctx, src_target,
+                                         src_samples, src->format, true));
+      } else {
+         ctx->bind_fs_state(pipe,
+            blitter_get_fs_texfetch_col(ctx, src->format, dst->format, src_target,
+                                        src_samples, dst_samples, filter,
+                                        use_txf));
+      }
    }
 
    /* Set the linear filter only for scaled color non-MSAA blits. */
@@ -1563,7 +2028,8 @@ void util_blitter_blit_generic(struct blitter_context *blitter,
    }
 
    /* Set samplers. */
-   if (blit_depth && blit_stencil) {
+   if (src_has_depth && src_has_stencil &&
+       (dst_has_color || (dst_has_depth && dst_has_stencil))) {
       /* Setup two samplers, one for depth and the other one for stencil. */
       struct pipe_sampler_view templ;
       struct pipe_sampler_view *views[2];
@@ -1580,7 +2046,7 @@ void util_blitter_blit_generic(struct blitter_context *blitter,
       pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, 2, samplers);
 
       pipe_sampler_view_reference(&views[1], NULL);
-   } else if (blit_stencil) {
+   } else if (src_has_stencil && dst_has_stencil) {
       /* Set a stencil-only sampler view for it not to sample depth instead. */
       struct pipe_sampler_view templ;
       struct pipe_sampler_view *view;
@@ -1602,134 +2068,29 @@ void util_blitter_blit_generic(struct blitter_context *blitter,
                                 0, 1, &sampler_state);
    }
 
-   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
    if (scissor) {
       pipe->set_scissor_states(pipe, 0, 1, scissor);
    }
 
-   blitter_set_common_draw_rect_state(ctx, scissor != NULL, FALSE);
-   blitter_set_dst_dimensions(ctx, dst->width, dst->height);
+   blitter_set_common_draw_rect_state(ctx, scissor != NULL, dst_samples > 1);
 
-   if ((src_target == PIPE_TEXTURE_1D ||
-        src_target == PIPE_TEXTURE_2D ||
-        src_target == PIPE_TEXTURE_RECT) &&
-       src_samples <= 1) {
-      /* Draw the quad with the draw_rectangle callback. */
+   do_blits(ctx, dst, dstbox, src, src_width0, src_height0,
+            srcbox, dst_has_depth || dst_has_stencil, use_txf);
 
-      /* Set texture coordinates. - use a pipe color union
-       * for interface purposes.
-       * XXX pipe_color_union is a wrong name since we use that to set
-       * texture coordinates too.
-       */
-      union pipe_color_union coord;
-      get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y,
-                    srcbox->x+srcbox->width, srcbox->y+srcbox->height, coord.f);
-
-      /* Set framebuffer state. */
-      if (blit_depth || blit_stencil) {
-         fb_state.zsbuf = dst;
-      } else {
-         fb_state.cbufs[0] = dst;
-      }
-      pipe->set_framebuffer_state(pipe, &fb_state);
-
-      /* Draw. */
-      pipe->set_sample_mask(pipe, ~0);
-      blitter->draw_rectangle(blitter, dstbox->x, dstbox->y,
-                              dstbox->x + dstbox->width,
-                              dstbox->y + dstbox->height, 0,
-                              UTIL_BLITTER_ATTRIB_TEXCOORD, &coord);
-   } else {
-      /* Draw the quad with the generic codepath. */
-      int dst_z;
-      for (dst_z = 0; dst_z < dstbox->depth; dst_z++) {
-         struct pipe_surface *old;
-         float dst2src_scale = srcbox->depth / (float)dstbox->depth;
-
-         /* Scale Z properly if the blit is scaled.
-          *
-          * When downscaling, we want the coordinates centered, so that
-          * mipmapping works for 3D textures. For example, when generating
-          * a 4x4x4 level, this wouldn't average the pixels:
-          *
-          *   src Z:  0 1 2 3 4 5 6 7
-          *   dst Z:  0   1   2   3
-          *
-          * Because the pixels are not centered below the pixels of the higher
-          * level. Therefore, we want this:
-          *   src Z:  0 1 2 3 4 5 6 7
-          *   dst Z:   0   1   2   3
-          *
-          * dst_offset defines the offset needed for centering the pixels and
-          * it works with any scaling (not just 2x).
-          */
-         float dst_offset = ((srcbox->depth - 1) -
-                             (dstbox->depth - 1) * dst2src_scale) * 0.5;
-         float src_z = (dst_z + dst_offset) * dst2src_scale;
-
-         /* Set framebuffer state. */
-         if (blit_depth || blit_stencil) {
-            fb_state.zsbuf = dst;
-         } else {
-            fb_state.cbufs[0] = dst;
-         }
-         pipe->set_framebuffer_state(pipe, &fb_state);
-
-         /* See if we need to blit a multisample or singlesample buffer. */
-         if (src_samples == dst_samples && dst_samples > 1) {
-            /* MSAA copy. */
-            unsigned i, max_sample = dst_samples - 1;
-
-            for (i = 0; i <= max_sample; i++) {
-               pipe->set_sample_mask(pipe, 1 << i);
-               blitter_set_texcoords(ctx, src, src_width0, src_height0,
-                                     srcbox->z + src_z,
-                                     i, srcbox->x, srcbox->y,
-                                     srcbox->x + srcbox->width,
-                                     srcbox->y + srcbox->height);
-               blitter_draw(ctx, dstbox->x, dstbox->y,
-                            dstbox->x + dstbox->width,
-                            dstbox->y + dstbox->height, 0, 1);
-            }
-         } else {
-            /* Normal copy, MSAA upsampling, or MSAA resolve. */
-            pipe->set_sample_mask(pipe, ~0);
-            blitter_set_texcoords(ctx, src, src_width0, src_height0,
-                                  srcbox->z + src_z, 0,
-                                  srcbox->x, srcbox->y,
-                                  srcbox->x + srcbox->width,
-                                  srcbox->y + srcbox->height);
-            blitter_draw(ctx, dstbox->x, dstbox->y,
-                         dstbox->x + dstbox->width,
-                         dstbox->y + dstbox->height, 0, 1);
-         }
-
-         /* Get the next surface or (if this is the last iteration)
-          * just unreference the last one. */
-         old = dst;
-         if (dst_z < dstbox->depth-1) {
-            dst = ctx->base.get_next_surface_layer(ctx->base.pipe, dst);
-         }
-         if (dst_z) {
-            pipe_surface_reference(&old, NULL);
-         }
-      }
-   }
-
-   blitter_restore_vertex_states(ctx);
-   blitter_restore_fragment_states(ctx);
-   blitter_restore_textures(ctx);
-   blitter_restore_fb_state(ctx);
+   util_blitter_restore_vertex_states(blitter);
+   util_blitter_restore_fragment_states(blitter);
+   util_blitter_restore_textures(blitter);
+   util_blitter_restore_fb_state(blitter);
    if (scissor) {
       pipe->set_scissor_states(pipe, 0, 1, &ctx->base.saved_scissor);
    }
-   blitter_restore_render_cond(ctx);
-   blitter_unset_running_flag(ctx);
+   util_blitter_restore_render_cond(blitter);
+   util_blitter_unset_running_flag(blitter);
 }
 
 void
 util_blitter_blit(struct blitter_context *blitter,
-                 const struct pipe_blit_info *info)
+                  const struct pipe_blit_info *info)
 {
    struct pipe_resource *dst = info->dst.resource;
    struct pipe_resource *src = info->src.resource;
@@ -1745,7 +2106,7 @@ util_blitter_blit(struct blitter_context *blitter,
    dst_view = pipe->create_surface(pipe, dst, &dst_templ);
 
    /* Initialize the sampler view. */
-   util_blitter_default_src_texture(&src_templ, src, info->src.level);
+   util_blitter_default_src_texture(blitter, &src_templ, src, info->src.level);
    src_templ.format = info->src.format;
    src_view = pipe->create_sampler_view(pipe, src, &src_templ);
 
@@ -1753,12 +2114,119 @@ util_blitter_blit(struct blitter_context *blitter,
    util_blitter_blit_generic(blitter, dst_view, &info->dst.box,
                              src_view, &info->src.box, src->width0, src->height0,
                              info->mask, info->filter,
-                             info->scissor_enable ? &info->scissor : NULL);
+                             info->scissor_enable ? &info->scissor : NULL,
+                             info->alpha_blend);
 
    pipe_surface_reference(&dst_view, NULL);
    pipe_sampler_view_reference(&src_view, NULL);
 }
 
+void util_blitter_generate_mipmap(struct blitter_context *blitter,
+                                  struct pipe_resource *tex,
+                                  enum pipe_format format,
+                                  unsigned base_level, unsigned last_level,
+                                  unsigned first_layer, unsigned last_layer)
+{
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
+   struct pipe_context *pipe = ctx->base.pipe;
+   struct pipe_surface dst_templ, *dst_view;
+   struct pipe_sampler_view src_templ, *src_view;
+   bool is_depth;
+   void *sampler_state;
+   const struct util_format_description *desc =
+         util_format_description(format);
+   unsigned src_level;
+   unsigned target = tex->target;
+
+   if (ctx->cube_as_2darray &&
+       (target == PIPE_TEXTURE_CUBE || target == PIPE_TEXTURE_CUBE_ARRAY))
+      target = PIPE_TEXTURE_2D_ARRAY;
+
+   assert(tex->nr_samples <= 1);
+   /* Disallow stencil formats without depth. */
+   assert(!util_format_has_stencil(desc) || util_format_has_depth(desc));
+
+   is_depth = desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS;
+
+   /* Check whether the states are properly saved. */
+   util_blitter_set_running_flag(blitter);
+   blitter_check_saved_vertex_states(ctx);
+   blitter_check_saved_fragment_states(ctx);
+   blitter_check_saved_textures(ctx);
+   blitter_check_saved_fb_state(ctx);
+   blitter_disable_render_cond(ctx);
+
+   /* Set states. */
+   if (is_depth) {
+      pipe->bind_blend_state(pipe, ctx->blend[0][0]);
+      pipe->bind_depth_stencil_alpha_state(pipe,
+                                           ctx->dsa_write_depth_keep_stencil);
+      ctx->bind_fs_state(pipe,
+                         blitter_get_fs_texfetch_depth(ctx, target, 1, false));
+   } else {
+      pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
+      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
+      ctx->bind_fs_state(pipe,
+            blitter_get_fs_texfetch_col(ctx, tex->format, tex->format, target,
+                                        1, 1, PIPE_TEX_FILTER_LINEAR, false));
+   }
+
+   if (target == PIPE_TEXTURE_RECT) {
+      sampler_state = ctx->sampler_state_rect_linear;
+   } else {
+      sampler_state = ctx->sampler_state_linear;
+   }
+   pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
+                             0, 1, &sampler_state);
+
+   blitter_set_common_draw_rect_state(ctx, false, false);
+
+   for (src_level = base_level; src_level < last_level; src_level++) {
+      struct pipe_box dstbox = {0}, srcbox = {0};
+      unsigned dst_level = src_level + 1;
+
+      dstbox.width = u_minify(tex->width0, dst_level);
+      dstbox.height = u_minify(tex->height0, dst_level);
+
+      srcbox.width = u_minify(tex->width0, src_level);
+      srcbox.height = u_minify(tex->height0, src_level);
+
+      if (target == PIPE_TEXTURE_3D) {
+         dstbox.depth = util_num_layers(tex, dst_level);
+         srcbox.depth = util_num_layers(tex, src_level);
+      } else {
+         dstbox.z = srcbox.z = first_layer;
+         dstbox.depth = srcbox.depth = last_layer - first_layer + 1;
+      }
+
+      /* Initialize the surface. */
+      util_blitter_default_dst_texture(&dst_templ, tex, dst_level,
+                                       first_layer);
+      dst_templ.format = format;
+      dst_view = pipe->create_surface(pipe, tex, &dst_templ);
+
+      /* Initialize the sampler view. */
+      util_blitter_default_src_texture(blitter, &src_templ, tex, src_level);
+      src_templ.format = format;
+      src_view = pipe->create_sampler_view(pipe, tex, &src_templ);
+
+      pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src_view);
+
+      do_blits(ctx, dst_view, &dstbox, src_view, tex->width0, tex->height0,
+               &srcbox, is_depth, false);
+
+      pipe_surface_reference(&dst_view, NULL);
+      pipe_sampler_view_reference(&src_view, NULL);
+   }
+
+   util_blitter_restore_vertex_states(blitter);
+   util_blitter_restore_fragment_states(blitter);
+   util_blitter_restore_textures(blitter);
+   util_blitter_restore_fb_state(blitter);
+   util_blitter_restore_render_cond(blitter);
+   util_blitter_unset_running_flag(blitter);
+}
+
 /* 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,
@@ -1769,23 +2237,24 @@ void util_blitter_clear_render_target(struct blitter_context *blitter,
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->base.pipe;
    struct pipe_framebuffer_state fb_state;
+   bool msaa;
+   unsigned num_layers;
 
    assert(dstsurf->texture);
    if (!dstsurf->texture)
       return;
 
    /* check the saved state */
-   blitter_set_running_flag(ctx);
+   util_blitter_set_running_flag(blitter);
    blitter_check_saved_vertex_states(ctx);
    blitter_check_saved_fragment_states(ctx);
    blitter_check_saved_fb_state(ctx);
    blitter_disable_render_cond(ctx);
 
    /* bind states */
-   pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA]);
+   pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
    pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
    bind_fs_write_one_cbuf(ctx);
-   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
 
    /* set a framebuffer state */
    fb_state.width = dstsurf->width;
@@ -1795,17 +2264,32 @@ void util_blitter_clear_render_target(struct blitter_context *blitter,
    fb_state.zsbuf = 0;
    pipe->set_framebuffer_state(pipe, &fb_state);
    pipe->set_sample_mask(pipe, ~0);
+   msaa = util_framebuffer_get_num_samples(&fb_state) > 1;
 
-   blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
    blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
-   blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
-                           UTIL_BLITTER_ATTRIB_COLOR, color);
-
-   blitter_restore_vertex_states(ctx);
-   blitter_restore_fragment_states(ctx);
-   blitter_restore_fb_state(ctx);
-   blitter_restore_render_cond(ctx);
-   blitter_unset_running_flag(ctx);
+
+   union blitter_attrib attrib;
+   memcpy(attrib.color, color->ui, sizeof(color->ui));
+
+   num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
+   if (num_layers > 1 && ctx->has_layered) {
+      blitter_set_common_draw_rect_state(ctx, false, msaa);
+      blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_layered,
+                              dstx, dsty, dstx+width, dsty+height, 0,
+                              num_layers, UTIL_BLITTER_ATTRIB_COLOR, &attrib);
+   } else {
+      blitter_set_common_draw_rect_state(ctx, false, msaa);
+      blitter->draw_rectangle(blitter, ctx->velem_state,
+                              get_vs_passthrough_pos_generic,
+                              dstx, dsty, dstx+width, dsty+height, 0,
+                              1, UTIL_BLITTER_ATTRIB_COLOR, &attrib);
+   }
+
+   util_blitter_restore_vertex_states(blitter);
+   util_blitter_restore_fragment_states(blitter);
+   util_blitter_restore_fb_state(blitter);
+   util_blitter_restore_render_cond(blitter);
+   util_blitter_unset_running_flag(blitter);
 }
 
 /* Clear a region of a depth stencil surface. */
@@ -1821,20 +2305,21 @@ void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
    struct pipe_context *pipe = ctx->base.pipe;
    struct pipe_framebuffer_state fb_state;
    struct pipe_stencil_ref sr = { { 0 } };
+   unsigned num_layers;
 
    assert(dstsurf->texture);
    if (!dstsurf->texture)
       return;
 
    /* check the saved state */
-   blitter_set_running_flag(ctx);
+   util_blitter_set_running_flag(blitter);
    blitter_check_saved_vertex_states(ctx);
    blitter_check_saved_fragment_states(ctx);
    blitter_check_saved_fb_state(ctx);
    blitter_disable_render_cond(ctx);
 
    /* bind states */
-   pipe->bind_blend_state(pipe, ctx->blend[0]);
+   pipe->bind_blend_state(pipe, ctx->blend[0][0]);
    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);
@@ -1853,7 +2338,6 @@ void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
       pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
 
    bind_fs_empty(ctx);
-   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
 
    /* set a framebuffer state */
    fb_state.width = dstsurf->width;
@@ -1864,25 +2348,35 @@ void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
    pipe->set_framebuffer_state(pipe, &fb_state);
    pipe->set_sample_mask(pipe, ~0);
 
-   blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
    blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
-   blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height,
-                           (float) depth,
-                           UTIL_BLITTER_ATTRIB_NONE, NULL);
-
-   blitter_restore_vertex_states(ctx);
-   blitter_restore_fragment_states(ctx);
-   blitter_restore_fb_state(ctx);
-   blitter_restore_render_cond(ctx);
-   blitter_unset_running_flag(ctx);
+
+   num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
+   if (num_layers > 1 && ctx->has_layered) {
+      blitter_set_common_draw_rect_state(ctx, false, false);
+      blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_layered,
+                              dstx, dsty, dstx+width, dsty+height, depth,
+                              num_layers, UTIL_BLITTER_ATTRIB_NONE, NULL);
+   } else {
+      blitter_set_common_draw_rect_state(ctx, false, false);
+      blitter->draw_rectangle(blitter, ctx->velem_state,
+                              get_vs_passthrough_pos,
+                              dstx, dsty, dstx+width, dsty+height, depth, 1,
+                              UTIL_BLITTER_ATTRIB_NONE, NULL);
+   }
+
+   util_blitter_restore_vertex_states(blitter);
+   util_blitter_restore_fragment_states(blitter);
+   util_blitter_restore_fb_state(blitter);
+   util_blitter_restore_render_cond(blitter);
+   util_blitter_unset_running_flag(blitter);
 }
 
 /* draw a rectangle across a region using a custom dsa stage - for r600g */
 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
-                                      struct pipe_surface *zsurf,
-                                      struct pipe_surface *cbsurf,
-                                      unsigned sample_mask,
-                                      void *dsa_stage, float depth)
+                                       struct pipe_surface *zsurf,
+                                       struct pipe_surface *cbsurf,
+                                       unsigned sample_mask,
+                                       void *dsa_stage, float depth)
 {
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->base.pipe;
@@ -1893,47 +2387,48 @@ void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
       return;
 
    /* check the saved state */
-   blitter_set_running_flag(ctx);
+   util_blitter_set_running_flag(blitter);
    blitter_check_saved_vertex_states(ctx);
    blitter_check_saved_fragment_states(ctx);
    blitter_check_saved_fb_state(ctx);
    blitter_disable_render_cond(ctx);
 
    /* bind states */
-   pipe->bind_blend_state(pipe, cbsurf ? ctx->blend[PIPE_MASK_RGBA] :
-                                         ctx->blend[0]);
+   pipe->bind_blend_state(pipe, cbsurf ? ctx->blend[PIPE_MASK_RGBA][0] :
+                                         ctx->blend[0][0]);
    pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
    if (cbsurf)
       bind_fs_write_one_cbuf(ctx);
    else
       bind_fs_empty(ctx);
-   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
 
    /* set a framebuffer state */
    fb_state.width = zsurf->width;
    fb_state.height = zsurf->height;
    fb_state.nr_cbufs = 1;
    if (cbsurf) {
-          fb_state.cbufs[0] = cbsurf;
-          fb_state.nr_cbufs = 1;
+      fb_state.cbufs[0] = cbsurf;
+      fb_state.nr_cbufs = 1;
    } else {
-          fb_state.cbufs[0] = NULL;
-          fb_state.nr_cbufs = 0;
+      fb_state.cbufs[0] = NULL;
+      fb_state.nr_cbufs = 0;
    }
    fb_state.zsbuf = zsurf;
    pipe->set_framebuffer_state(pipe, &fb_state);
    pipe->set_sample_mask(pipe, sample_mask);
 
-   blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
+   blitter_set_common_draw_rect_state(ctx, false,
+      util_framebuffer_get_num_samples(&fb_state) > 1);
    blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
-   blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth,
-                           UTIL_BLITTER_ATTRIB_NONE, NULL);
-
-   blitter_restore_vertex_states(ctx);
-   blitter_restore_fragment_states(ctx);
-   blitter_restore_fb_state(ctx);
-   blitter_restore_render_cond(ctx);
-   blitter_unset_running_flag(ctx);
+   blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
+                           0, 0, zsurf->width, zsurf->height, depth,
+                           1, UTIL_BLITTER_ATTRIB_NONE, NULL);
+
+   util_blitter_restore_vertex_states(blitter);
+   util_blitter_restore_fragment_states(blitter);
+   util_blitter_restore_fb_state(blitter);
+   util_blitter_restore_render_cond(blitter);
+   util_blitter_unset_running_flag(blitter);
 }
 
 void util_blitter_copy_buffer(struct blitter_context *blitter,
@@ -1973,17 +2468,18 @@ void util_blitter_copy_buffer(struct blitter_context *blitter,
       return;
    }
 
-   blitter_set_running_flag(ctx);
+   util_blitter_set_running_flag(blitter);
    blitter_check_saved_vertex_states(ctx);
    blitter_disable_render_cond(ctx);
 
-   vb.buffer = src;
+   vb.is_user_buffer = false;
+   vb.buffer.resource = src;
    vb.buffer_offset = srcx;
    vb.stride = 4;
 
    pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
    pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf[0]);
-   bind_vs_pos_only(ctx);
+   bind_vs_pos_only(ctx, 1);
    if (ctx->has_geometry_shader)
       pipe->bind_gs_state(pipe, NULL);
    if (ctx->has_tessellation) {
@@ -1997,9 +2493,9 @@ void util_blitter_copy_buffer(struct blitter_context *blitter,
 
    util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
 
-   blitter_restore_vertex_states(ctx);
-   blitter_restore_render_cond(ctx);
-   blitter_unset_running_flag(ctx);
+   util_blitter_restore_vertex_states(blitter);
+   util_blitter_restore_render_cond(blitter);
+   util_blitter_unset_running_flag(blitter);
    pipe_so_target_reference(&so_target, NULL);
 }
 
@@ -2012,7 +2508,7 @@ void util_blitter_clear_buffer(struct blitter_context *blitter,
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
    struct pipe_context *pipe = ctx->base.pipe;
    struct pipe_vertex_buffer vb = {0};
-   struct pipe_stream_output_target *so_target;
+   struct pipe_stream_output_target *so_target = NULL;
    unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
 
    assert(num_channels >= 1);
@@ -2036,18 +2532,21 @@ void util_blitter_clear_buffer(struct blitter_context *blitter,
       return;
    }
 
-   u_upload_data(ctx->upload, 0, num_channels*4, clear_value,
-                 &vb.buffer_offset, &vb.buffer);
+   u_upload_data(pipe->stream_uploader, 0, num_channels*4, 4, clear_value,
+                 &vb.buffer_offset, &vb.buffer.resource);
+   if (!vb.buffer.resource)
+      goto out;
+
    vb.stride = 0;
 
-   blitter_set_running_flag(ctx);
+   util_blitter_set_running_flag(blitter);
    blitter_check_saved_vertex_states(ctx);
    blitter_disable_render_cond(ctx);
 
    pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
    pipe->bind_vertex_elements_state(pipe,
                                     ctx->velem_state_readbuf[num_channels-1]);
-   bind_vs_pos_only(ctx);
+   bind_vs_pos_only(ctx, num_channels);
    if (ctx->has_geometry_shader)
       pipe->bind_gs_state(pipe, NULL);
    if (ctx->has_tessellation) {
@@ -2061,22 +2560,23 @@ void util_blitter_clear_buffer(struct blitter_context *blitter,
 
    util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
 
-   blitter_restore_vertex_states(ctx);
-   blitter_restore_render_cond(ctx);
-   blitter_unset_running_flag(ctx);
+out:
+   util_blitter_restore_vertex_states(blitter);
+   util_blitter_restore_render_cond(blitter);
+   util_blitter_unset_running_flag(blitter);
    pipe_so_target_reference(&so_target, NULL);
-   pipe_resource_reference(&vb.buffer, NULL);
+   pipe_resource_reference(&vb.buffer.resource, NULL);
 }
 
 /* probably radeon specific */
 void util_blitter_custom_resolve_color(struct blitter_context *blitter,
-                                      struct pipe_resource *dst,
-                                      unsigned dst_level,
-                                      unsigned dst_layer,
-                                      struct pipe_resource *src,
-                                      unsigned src_layer,
-                                      unsigned sample_mask,
-                                      void *custom_blend,
+                                       struct pipe_resource *dst,
+                                       unsigned dst_level,
+                                       unsigned dst_layer,
+                                       struct pipe_resource *src,
+                                       unsigned src_layer,
+                                       unsigned sample_mask,
+                                       void *custom_blend,
                                        enum pipe_format format)
 {
    struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
@@ -2084,7 +2584,7 @@ void util_blitter_custom_resolve_color(struct blitter_context *blitter,
    struct pipe_framebuffer_state fb_state;
    struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
 
-   blitter_set_running_flag(ctx);
+   util_blitter_set_running_flag(blitter);
    blitter_check_saved_vertex_states(ctx);
    blitter_check_saved_fragment_states(ctx);
    blitter_disable_render_cond(ctx);
@@ -2092,7 +2592,6 @@ void util_blitter_custom_resolve_color(struct blitter_context *blitter,
    /* bind states */
    pipe->bind_blend_state(pipe, custom_blend);
    pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
-   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
    bind_fs_write_one_cbuf(ctx);
    pipe->set_sample_mask(pipe, sample_mask);
 
@@ -2119,15 +2618,17 @@ void util_blitter_custom_resolve_color(struct blitter_context *blitter,
    fb_state.zsbuf = NULL;
    pipe->set_framebuffer_state(pipe, &fb_state);
 
-   blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
+   blitter_set_common_draw_rect_state(ctx, false,
+      util_framebuffer_get_num_samples(&fb_state) > 1);
    blitter_set_dst_dimensions(ctx, src->width0, src->height0);
-   blitter->draw_rectangle(blitter, 0, 0, src->width0, src->height0,
-                           0, 0, NULL);
-   blitter_restore_fb_state(ctx);
-   blitter_restore_vertex_states(ctx);
-   blitter_restore_fragment_states(ctx);
-   blitter_restore_render_cond(ctx);
-   blitter_unset_running_flag(ctx);
+   blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
+                           0, 0, src->width0, src->height0,
+                           0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
+   util_blitter_restore_fb_state(blitter);
+   util_blitter_restore_vertex_states(blitter);
+   util_blitter_restore_fragment_states(blitter);
+   util_blitter_restore_render_cond(blitter);
+   util_blitter_unset_running_flag(blitter);
 
    pipe_surface_reference(&srcsurf, NULL);
    pipe_surface_reference(&dstsurf, NULL);
@@ -2146,7 +2647,7 @@ void util_blitter_custom_color(struct blitter_context *blitter,
       return;
 
    /* check the saved state */
-   blitter_set_running_flag(ctx);
+   util_blitter_set_running_flag(blitter);
    blitter_check_saved_vertex_states(ctx);
    blitter_check_saved_fragment_states(ctx);
    blitter_check_saved_fb_state(ctx);
@@ -2154,10 +2655,9 @@ void util_blitter_custom_color(struct blitter_context *blitter,
 
    /* bind states */
    pipe->bind_blend_state(pipe, custom_blend ? custom_blend
-                                             : ctx->blend[PIPE_MASK_RGBA]);
+                                             : ctx->blend[PIPE_MASK_RGBA][0]);
    pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
    bind_fs_write_one_cbuf(ctx);
-   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
    pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
 
    /* set a framebuffer state */
@@ -2169,14 +2669,79 @@ void util_blitter_custom_color(struct blitter_context *blitter,
    pipe->set_framebuffer_state(pipe, &fb_state);
    pipe->set_sample_mask(pipe, ~0);
 
-   blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
+   blitter_set_common_draw_rect_state(ctx, false,
+      util_framebuffer_get_num_samples(&fb_state) > 1);
+   blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
+   blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
+                           0, 0, dstsurf->width, dstsurf->height,
+                           0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
+
+   util_blitter_restore_vertex_states(blitter);
+   util_blitter_restore_fragment_states(blitter);
+   util_blitter_restore_fb_state(blitter);
+   util_blitter_restore_render_cond(blitter);
+   util_blitter_unset_running_flag(blitter);
+}
+
+static void *get_custom_vs(struct blitter_context *blitter)
+{
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
+
+   return ctx->custom_vs;
+}
+
+/**
+ * Performs a custom blit to the destination surface, using the VS and FS
+ * provided.
+ *
+ * Used by vc4 for the 8-bit linear-to-tiled blit.
+ */
+void util_blitter_custom_shader(struct blitter_context *blitter,
+                                struct pipe_surface *dstsurf,
+                                void *custom_vs, void *custom_fs)
+{
+   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
+   struct pipe_context *pipe = ctx->base.pipe;
+   struct pipe_framebuffer_state fb_state;
+
+   ctx->custom_vs = custom_vs;
+
+   assert(dstsurf->texture);
+   if (!dstsurf->texture)
+      return;
+
+   /* check the saved state */
+   util_blitter_set_running_flag(blitter);
+   blitter_check_saved_vertex_states(ctx);
+   blitter_check_saved_fragment_states(ctx);
+   blitter_check_saved_fb_state(ctx);
+   blitter_disable_render_cond(ctx);
+
+   /* bind states */
+   pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
+   pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
+   pipe->bind_fs_state(pipe, custom_fs);
+   pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
+
+   /* 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);
+   pipe->set_sample_mask(pipe, ~0);
+
+   blitter_set_common_draw_rect_state(ctx, false,
+      util_framebuffer_get_num_samples(&fb_state) > 1);
    blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
-   blitter->draw_rectangle(blitter, 0, 0, dstsurf->width, dstsurf->height,
-                           0, 0, NULL);
-
-   blitter_restore_vertex_states(ctx);
-   blitter_restore_fragment_states(ctx);
-   blitter_restore_fb_state(ctx);
-   blitter_restore_render_cond(ctx);
-   blitter_unset_running_flag(ctx);
+   blitter->draw_rectangle(blitter, ctx->velem_state, get_custom_vs,
+                           0, 0, dstsurf->width, dstsurf->height,
+                           0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
+
+   util_blitter_restore_vertex_states(blitter);
+   util_blitter_restore_fragment_states(blitter);
+   util_blitter_restore_fb_state(blitter);
+   util_blitter_restore_render_cond(blitter);
+   util_blitter_unset_running_flag(blitter);
 }