gallium: split depth_clip into depth_clip_near & depth_clip_far
[mesa.git] / src / gallium / auxiliary / util / u_blitter.c
index f3f81d2732eb79e836b0c6cfbacf2b415c657f11..e19fde9873d711b76a72903db3be51985123d9fc 100644 (file)
@@ -125,13 +125,12 @@ struct blitter_context_priv
    /* Rasterizer state. */
    void *rs_state, *rs_state_scissor, *rs_discard_state;
 
-   /* Viewport state. */
-   struct pipe_viewport_state viewport;
-
    /* Destination surface dimensions. */
    unsigned dst_width;
    unsigned dst_height;
 
+   void *custom_vs;
+
    bool has_geometry_shader;
    bool has_tessellation;
    bool has_layered;
@@ -277,7 +276,8 @@ 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;
+   rs_state.depth_clip_near = 1;
+   rs_state.depth_clip_far = 1;
    ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
 
    rs_state.scissor = 1;
@@ -336,7 +336,8 @@ static void bind_vs_pos_only(struct blitter_context_priv *ctx,
 
    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));
@@ -353,28 +354,30 @@ static void bind_vs_pos_only(struct blitter_context_priv *ctx,
    pipe->bind_vs_state(pipe, ctx->vs_pos_only[index]);
 }
 
-static void bind_vs_passthrough_pos_generic(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);
    }
-
-   pipe->bind_vs_state(pipe, ctx->vs);
+   return ctx->vs;
 }
 
-static void bind_vs_passthrough_pos(struct blitter_context_priv *ctx)
+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) {
-      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION };
+      static const enum tgsi_semantic semantic_names[] =
+         { TGSI_SEMANTIC_POSITION };
       const uint semantic_indices[] = { 0 };
 
       ctx->vs_nogeneric =
@@ -382,19 +385,18 @@ static void bind_vs_passthrough_pos(struct blitter_context_priv *ctx)
                                              semantic_names,
                                              semantic_indices, false);
    }
-
-   pipe->bind_vs_state(pipe, ctx->vs_nogeneric);
+   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)
@@ -553,9 +555,8 @@ void util_blitter_unset_running_flag(struct blitter_context *blitter)
    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);
@@ -571,13 +572,17 @@ void util_blitter_restore_vertex_states(struct blitter_context *blitter)
    unsigned i;
 
    /* Vertex buffer. */
-   pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1,
-                            &ctx->base.saved_vertex_buffer);
-   pipe_vertex_buffer_unreference(&ctx->base.saved_vertex_buffer);
+   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);
@@ -616,7 +621,7 @@ void util_blitter_restore_vertex_states(struct blitter_context *blitter)
    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);
@@ -650,12 +655,21 @@ void util_blitter_restore_fragment_states(struct blitter_context *blitter)
    /* 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 != ~0u);
+   assert(ctx->base.saved_fb_state.nr_cbufs != (ubyte) ~0);
 }
 
 static void blitter_disable_render_cond(struct blitter_context_priv *ctx)
@@ -689,7 +703,7 @@ void util_blitter_restore_fb_state(struct blitter_context *blitter)
    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 != ~0u);
    assert(ctx->base.saved_num_sampler_views != ~0u);
@@ -751,13 +765,14 @@ 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,
@@ -1175,7 +1190,7 @@ void util_blitter_cache_all_shaders(struct blitter_context *blitter)
             /* MSAA resolve shaders. */
             for (j = 2; j < 32; j++) {
                if (!screen->is_format_supported(screen, PIPE_FORMAT_R32_FLOAT,
-                                                target, j,
+                                                target, j, j,
                                                 PIPE_BIND_SAMPLER_VIEW)) {
                   continue;
                }
@@ -1213,21 +1228,15 @@ void util_blitter_cache_all_shaders(struct blitter_context *blitter)
 }
 
 static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx,
-                                               bool scissor,
-                                               bool vs_layered,
-                                               bool vs_pass_generic)
+                                               bool scissor)
 {
    struct pipe_context *pipe = ctx->base.pipe;
 
+   if (ctx->base.saved_num_window_rectangles)
+      pipe->set_window_rectangles(pipe, false, 0, NULL);
+
    pipe->bind_rasterizer_state(pipe, scissor ? ctx->rs_state_scissor
                                              : ctx->rs_state);
-   if (vs_layered)
-      bind_vs_layered(ctx);
-   else if (vs_pass_generic)
-      bind_vs_passthrough_pos_generic(ctx);
-   else
-      bind_vs_passthrough_pos(ctx);
-
    if (ctx->has_geometry_shader)
       pipe->bind_gs_state(pipe, NULL);
    if (ctx->has_tessellation) {
@@ -1239,6 +1248,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)
 {
@@ -1256,12 +1267,28 @@ static void blitter_draw(struct blitter_context_priv *ctx,
    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->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,
+                                 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,
@@ -1288,7 +1315,8 @@ void util_blitter_draw_rectangle(struct blitter_context *blitter,
       default:;
    }
 
-   blitter_draw(ctx, x1, y1, x2, y2, depth, num_instances);
+   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,
@@ -1382,7 +1410,6 @@ static void util_blitter_clear_custom(struct blitter_context *blitter,
    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);
 
    union blitter_attrib attrib;
@@ -1393,13 +1420,24 @@ static void util_blitter_clear_custom(struct blitter_context *blitter,
                                                   UTIL_BLITTER_ATTRIB_NONE;
 
    if (num_layers > 1 && ctx->has_layered) {
-      blitter_set_common_draw_rect_state(ctx, false, true, pass_generic);
-      blitter->draw_rectangle(blitter, 0, 0, width, height, (float) depth,
-                              num_layers, type, &attrib);
+      blitter_get_vs_func get_vs = get_vs_layered;
+
+      blitter_set_common_draw_rect_state(ctx, false);
+      blitter->draw_rectangle(blitter, ctx->velem_state, get_vs,
+                              0, 0, width, height,
+                              (float) depth, num_layers, type, &attrib);
    } else {
-      blitter_set_common_draw_rect_state(ctx, false, false, pass_generic);
-      blitter->draw_rectangle(blitter, 0, 0, width, height, (float) depth,
-                              1, type, &attrib);
+      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);
+      blitter->draw_rectangle(blitter, ctx->velem_state, get_vs,
+                              0, 0, width, height,
+                              (float) depth, 1, type, &attrib);
    }
 
    util_blitter_restore_vertex_states(blitter);
@@ -1477,7 +1515,7 @@ void util_blitter_default_src_texture(struct blitter_context *blitter,
    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;
+                                     : (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;
@@ -1512,7 +1550,8 @@ static bool 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)) {
+                                       dst->nr_samples, dst->nr_storage_samples,
+                                       bind)) {
          return false;
       }
    }
@@ -1523,7 +1562,8 @@ static bool is_blit_generic_supported(struct blitter_context *blitter,
       }
 
       if (!screen->is_format_supported(screen, src_format, src->target,
-                                 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
+                                       src->nr_samples, src->nr_storage_samples,
+                                       PIPE_BIND_SAMPLER_VIEW)) {
          return false;
       }
 
@@ -1537,6 +1577,7 @@ static bool 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;
             }
@@ -1556,7 +1597,7 @@ bool util_blitter_is_copy_supported(struct blitter_context *blitter,
 }
 
 bool util_blitter_is_blit_supported(struct blitter_context *blitter,
-                                   const struct pipe_blit_info *info)
+                                    const struct pipe_blit_info *info)
 {
    return is_blit_generic_supported(blitter,
                                     info->dst.resource, info->dst.format,
@@ -1612,6 +1653,7 @@ blitter_draw_tex(struct blitter_context_priv *ctx,
                  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,
@@ -1631,10 +1673,12 @@ blitter_draw_tex(struct blitter_context_priv *ctx,
          ctx->vertices[i][1][3] = coord.texcoord.w;
 
       /* Cubemaps don't use draw_rectangle. */
-      blitter_draw(ctx, dst_x1, dst_y1, dst_x2, dst_y2, 0, 1);
+      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, dst_x1, dst_y1, dst_x2, dst_y2, 0,
-                               1, type, &coord);
+      ctx->base.draw_rectangle(&ctx->base, ctx->velem_state, get_vs,
+                               dst_x1, dst_y1, dst_x2, dst_y2,
+                               0, 1, type, &coord);
    }
 }
 
@@ -1940,12 +1984,11 @@ 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, true);
+   blitter_set_common_draw_rect_state(ctx, scissor != NULL);
 
    do_blits(ctx, dst, dstbox, src, src_width0, src_height0,
             srcbox, blit_depth || blit_stencil, use_txf);
@@ -1963,7 +2006,7 @@ void util_blitter_blit_generic(struct blitter_context *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;
@@ -1995,7 +2038,7 @@ util_blitter_blit(struct blitter_context *blitter,
 }
 
 void util_blitter_generate_mipmap(struct blitter_context *blitter,
-                                 struct pipe_resource *tex,
+                                  struct pipe_resource *tex,
                                   enum pipe_format format,
                                   unsigned base_level, unsigned last_level,
                                   unsigned first_layer, unsigned last_layer)
@@ -2051,8 +2094,7 @@ void util_blitter_generate_mipmap(struct blitter_context *blitter,
    pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
                              0, 1, &sampler_state);
 
-   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
-   blitter_set_common_draw_rect_state(ctx, false, false, true);
+   blitter_set_common_draw_rect_state(ctx, false);
 
    for (src_level = base_level; src_level < last_level; src_level++) {
       struct pipe_box dstbox = {0}, srcbox = {0};
@@ -2065,8 +2107,8 @@ void util_blitter_generate_mipmap(struct blitter_context *blitter,
       srcbox.height = u_minify(tex->height0, src_level);
 
       if (target == PIPE_TEXTURE_3D) {
-         dstbox.depth = util_max_layer(tex, dst_level) + 1;
-         srcbox.depth = util_max_layer(tex, src_level) + 1;
+         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;
@@ -2127,7 +2169,6 @@ void util_blitter_clear_render_target(struct blitter_context *blitter,
    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;
@@ -2145,12 +2186,15 @@ void util_blitter_clear_render_target(struct blitter_context *blitter,
 
    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, true, true);
-      blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
+      blitter_set_common_draw_rect_state(ctx, false);
+      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, false, true);
-      blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
+      blitter_set_common_draw_rect_state(ctx, false);
+      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);
    }
 
@@ -2207,7 +2251,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;
@@ -2222,14 +2265,15 @@ void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
 
    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, true, false);
-      blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height,
-                              depth, num_layers,
-                              UTIL_BLITTER_ATTRIB_NONE, NULL);
+      blitter_set_common_draw_rect_state(ctx, 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, false);
-      blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height,
-                              depth, 1,
+      blitter_set_common_draw_rect_state(ctx, 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);
    }
 
@@ -2242,10 +2286,10 @@ void util_blitter_clear_depth_stencil(struct blitter_context *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;
@@ -2270,26 +2314,26 @@ void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
       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, false);
+   blitter_set_common_draw_rect_state(ctx, false);
    blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
-   blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth,
+   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);
@@ -2438,13 +2482,13 @@ out:
 
 /* 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;
@@ -2460,7 +2504,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);
 
@@ -2487,10 +2530,11 @@ 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, false);
+   blitter_set_common_draw_rect_state(ctx, false);
    blitter_set_dst_dimensions(ctx, src->width0, src->height0);
-   blitter->draw_rectangle(blitter, 0, 0, src->width0, src->height0,
-                           0, 1, 0, NULL);
+   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);
@@ -2525,7 +2569,6 @@ void util_blitter_custom_color(struct blitter_context *blitter,
                                              : 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 */
@@ -2537,10 +2580,73 @@ 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, false);
+   blitter_set_common_draw_rect_state(ctx, false);
+   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);
    blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
-   blitter->draw_rectangle(blitter, 0, 0, dstsurf->width, dstsurf->height,
-                           0, 1, 0, NULL);
+   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);