gallium/u_blitter: let drivers decide which VS to use for draw_rectangle
authorMarek Olšák <marek.olsak@amd.com>
Thu, 5 Oct 2017 02:02:05 +0000 (04:02 +0200)
committerMarek Olšák <marek.olsak@amd.com>
Sat, 7 Oct 2017 16:26:35 +0000 (18:26 +0200)
This approach allows drivers to set their own vertex shader and skip
compilation of u_blitter vertex shaders.

Reviewed-by: Nicolai Hähnle <nicolai.haehnle@amd.com>
src/gallium/auxiliary/util/u_blitter.c
src/gallium/auxiliary/util/u_blitter.h
src/gallium/drivers/r300/r300_context.h
src/gallium/drivers/r300/r300_render.c
src/gallium/drivers/r600/r600_pipe_common.c
src/gallium/drivers/r600/r600_pipe_common.h
src/gallium/drivers/radeonsi/si_state.h
src/gallium/drivers/radeonsi/si_state_draw.c

index ec03e5f7d128997290d79378ea69fcbc581f4f7c..47042e4e826e529e9f6779878a7f91ccb1f7ed46 100644 (file)
@@ -350,8 +350,9 @@ 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) {
@@ -362,12 +363,12 @@ static void bind_vs_passthrough_pos_generic(struct blitter_context_priv *ctx)
          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) {
@@ -379,19 +380,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)
@@ -1211,21 +1211,12 @@ 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;
 
    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) {
@@ -1238,6 +1229,7 @@ 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,6 +1248,7 @@ static void blitter_draw(struct blitter_context_priv *ctx,
 
    pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
    pipe->bind_vertex_elements_state(pipe, vertex_elements_cso);
+   pipe->bind_vs_state(pipe, get_vs(&ctx->base));
    util_draw_arrays_instanced(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
                               0, num_instances);
    pipe_resource_reference(&vb.buffer.resource, NULL);
@@ -1263,6 +1256,7 @@ static void blitter_draw(struct blitter_context_priv *ctx,
 
 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,
@@ -1289,7 +1283,8 @@ void util_blitter_draw_rectangle(struct blitter_context *blitter,
       default:;
    }
 
-   blitter_draw(ctx, vertex_elements_cso, 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,
@@ -1393,12 +1388,23 @@ 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, ctx->velem_state, 0, 0, width, height,
+      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, ctx->velem_state, 0, 0, width, height,
+      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);
    }
 
@@ -1612,6 +1618,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 +1638,10 @@ 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, ctx->velem_state,
+      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,
+      ctx->base.draw_rectangle(&ctx->base, ctx->velem_state, get_vs,
                                dst_x1, dst_y1, dst_x2, dst_y2,
                                0, 1, type, &coord);
    }
@@ -1946,7 +1953,7 @@ void util_blitter_blit_generic(struct blitter_context *blitter,
       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);
@@ -2052,7 +2059,7 @@ void util_blitter_generate_mipmap(struct blitter_context *blitter,
    pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
                              0, 1, &sampler_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};
@@ -2144,13 +2151,14 @@ 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, ctx->velem_state,
+      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_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);
    }
@@ -2222,13 +2230,14 @@ 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, ctx->velem_state,
+      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_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);
    }
@@ -2286,10 +2295,10 @@ void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
    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, ctx->velem_state, 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);
@@ -2486,9 +2495,9 @@ 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, ctx->velem_state,
+   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);
@@ -2536,9 +2545,9 @@ 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,
+   blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
                            0, 0, dstsurf->width, dstsurf->height,
                            0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
 
index 868f1da5c9c5394e5593e798c2f86021546b890b..0dd896d3818aca9b8eb17fd774e878b1ee1efeae 100644 (file)
@@ -53,11 +53,20 @@ union blitter_attrib {
    } texcoord;
 };
 
+struct blitter_context;
+
+typedef void *(*blitter_get_vs_func)(struct blitter_context *blitter);
+
 struct blitter_context
 {
    /**
     * Draw a rectangle.
     *
+    * \param get_vs  Callback for obtaining the vertex shader for the draw call.
+    *                It might invoke the shader compiler. The driver is
+    *                responsible for setting the vertex shader, and the callback
+    *                allows the driver to query the vertex shader CSO if it
+    *                wants to use the default one.
     * \param x1      An X coordinate of the top-left corner.
     * \param y1      A Y coordinate of the top-left corner.
     * \param x2      An X coordinate of the bottom-right corner.
@@ -82,6 +91,7 @@ struct blitter_context
     */
    void (*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,
@@ -157,6 +167,7 @@ void util_blitter_set_texture_multisample(struct blitter_context *blitter,
  * inside of the draw_rectangle callback if the driver overrides it. */
 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,
index b2d7f6da30e13eb7cc7b614646b9f51f299d01aa..220251577647009e63bc103b92fdbbccc531f2b9 100644 (file)
@@ -744,6 +744,7 @@ void r300_plug_in_stencil_ref_fallback(struct r300_context *r300);
 void r500_emit_index_bias(struct r300_context *r300, int index_bias);
 void r300_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,
index 7808669e25300102047e22562b0e04f96f6cfe8f..4cae7660347ad3b3f845b0b5c71deef13cdc1ee1 100644 (file)
@@ -1114,6 +1114,7 @@ struct draw_stage* r300_draw_stage(struct r300_context* r300)
  * somewhat inefficient. Instead we use a rectangular point sprite. */
 void r300_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,
@@ -1135,7 +1136,8 @@ void r300_blitter_draw_rectangle(struct blitter_context *blitter,
     if ((!r300->screen->caps.has_tcl && type == UTIL_BLITTER_ATTRIB_NONE) ||
         type == UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW ||
         num_instances > 1) {
-        util_blitter_draw_rectangle(blitter, vertex_elements_cso, x1, y1, x2, y2,
+        util_blitter_draw_rectangle(blitter, vertex_elements_cso, get_vs,
+                                    x1, y1, x2, y2,
                                     depth, num_instances, type, attrib);
         return;
     }
@@ -1144,6 +1146,7 @@ void r300_blitter_draw_rectangle(struct blitter_context *blitter,
         return;
 
     r300->context.bind_vertex_elements_state(&r300->context, vertex_elements_cso);
+    r300->context.bind_vs_state(&r300->context, get_vs(blitter));
 
     if (type == UTIL_BLITTER_ATTRIB_TEXCOORD_XY)
         r300->sprite_coord_enable = 1;
index 0e8a12a1b64cb4f21d0783dcd8975797f25ab5ab..56297cf4db81c45f4a2476c700b3d0780d5d37c4 100644 (file)
@@ -212,6 +212,7 @@ void r600_gfx_wait_fence(struct r600_common_context *ctx,
 
 void r600_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,
@@ -225,6 +226,7 @@ void r600_draw_rectangle(struct blitter_context *blitter,
        float *vb;
 
        rctx->b.bind_vertex_elements_state(&rctx->b, vertex_elements_cso);
+       rctx->b.bind_vs_state(&rctx->b, get_vs(blitter));
 
        /* Some operations (like color resolve on r6xx) don't work
         * with the conventional primitive types.
index 37dcbd3d31691a7c4bf9c9fb1997d25a0021bdeb..52f39cdb7ac000a70653bfe82e0fa054822c4ca5 100644 (file)
@@ -763,6 +763,7 @@ void r600_gfx_wait_fence(struct r600_common_context *ctx,
                         uint64_t va, uint32_t ref, uint32_t mask);
 void r600_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,
index c9745a58cb7bd2a98b314b409db12bb3b2cb446c..9cc323dc9be30f8a944060f11d8ba9c608ffa163 100644 (file)
@@ -409,6 +409,7 @@ void si_emit_cache_flush(struct si_context *sctx);
 void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *dinfo);
 void si_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,
index ceb90d287f5c3a9208cf8b087d138f742c8055c8..9cb4274115359970adc8e9fd5b00eb7fd9c21920 100644 (file)
@@ -1492,6 +1492,7 @@ void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *info)
 
 void si_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,
@@ -1565,6 +1566,7 @@ void si_draw_rectangle(struct blitter_context *blitter,
        vbuffer.buffer_offset = offset;
 
        pipe->set_vertex_buffers(pipe, blitter->vb_slot, 1, &vbuffer);
+       pipe->bind_vs_state(pipe, get_vs(blitter));
 
        if (sctx->vertex_elements != vertex_elements_cso)
                pipe->bind_vertex_elements_state(pipe, vertex_elements_cso);