[Mesa-dev] [PATCH 5/5] st/mesa: don't set 16 scissors and 16 viewports if they're unused

Marek Olšák maraeo at gmail.com
Wed Jun 14 21:41:17 UTC 2017


From: Marek Olšák <marek.olsak at amd.com>

Only do so if there is a shader writing gl_ViewportIndex.
This removes a lot of CPU overhead for the most common case.
---
 src/mesa/state_tracker/st_atom.c          | 18 ++++++++++++++++++
 src/mesa/state_tracker/st_atom_scissor.c  | 10 +++++++---
 src/mesa/state_tracker/st_atom_viewport.c | 11 ++++++++---
 src/mesa/state_tracker/st_context.h       |  1 +
 4 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/src/mesa/state_tracker/st_atom.c b/src/mesa/state_tracker/st_atom.c
index bcfbcf8..253b508 100644
--- a/src/mesa/state_tracker/st_atom.c
+++ b/src/mesa/state_tracker/st_atom.c
@@ -69,20 +69,21 @@ static void check_program_state( struct st_context *st )
    struct st_common_program *old_tep = st->tep;
    struct st_common_program *old_gp = st->gp;
    struct st_fragment_program *old_fp = st->fp;
 
    struct gl_program *new_vp = ctx->VertexProgram._Current;
    struct gl_program *new_tcp = ctx->TessCtrlProgram._Current;
    struct gl_program *new_tep = ctx->TessEvalProgram._Current;
    struct gl_program *new_gp = ctx->GeometryProgram._Current;
    struct gl_program *new_fp = ctx->FragmentProgram._Current;
    uint64_t dirty = 0;
+   unsigned num_viewports = 1;
 
    /* Flag states used by both new and old shaders to unbind shader resources
     * properly when transitioning to shaders that don't use them.
     */
    if (unlikely(new_vp != &old_vp->Base)) {
       if (old_vp)
          dirty |= old_vp->affected_states;
       if (new_vp)
          dirty |= ST_NEW_VERTEX_PROGRAM(st, st_vertex_program(new_vp));
    }
@@ -108,20 +109,37 @@ static void check_program_state( struct st_context *st )
          dirty |= st_common_program(new_gp)->affected_states;
    }
 
    if (unlikely(new_fp != &old_fp->Base)) {
       if (old_fp)
          dirty |= old_fp->affected_states;
       if (new_fp)
          dirty |= st_fragment_program(new_fp)->affected_states;
    }
 
+   /* Find out the number of viewports. This determines how many scissors
+    * and viewport states we need to update.
+    */
+   struct gl_program *last_prim_shader = new_gp ? new_gp :
+                                         new_tep ? new_tep : new_vp;
+   if (last_prim_shader &&
+       last_prim_shader->info.outputs_written & VARYING_BIT_VIEWPORT)
+      num_viewports = ctx->Const.MaxViewports;
+
+   if (st->state.num_viewports != num_viewports) {
+      st->state.num_viewports = num_viewports;
+      dirty |= ST_NEW_VIEWPORT;
+
+      if (ctx->Scissor.EnableFlags & u_bit_consecutive(0, num_viewports))
+         dirty |= ST_NEW_SCISSOR;
+   }
+
    st->dirty |= dirty;
 }
 
 static void check_attrib_edgeflag(struct st_context *st)
 {
    const struct gl_vertex_array **arrays = st->ctx->Array._DrawArrays;
    GLboolean vertdata_edgeflags, edgeflag_culls_prims, edgeflags_enabled;
    struct gl_program *vp = st->ctx->VertexProgram._Current;
 
    if (!arrays)
diff --git a/src/mesa/state_tracker/st_atom_scissor.c b/src/mesa/state_tracker/st_atom_scissor.c
index ccd6e8e..a87d029 100644
--- a/src/mesa/state_tracker/st_atom_scissor.c
+++ b/src/mesa/state_tracker/st_atom_scissor.c
@@ -46,21 +46,21 @@ st_update_scissor( struct st_context *st )
 {
    struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
    const struct gl_context *ctx = st->ctx;
    const struct gl_framebuffer *fb = ctx->DrawBuffer;
    const unsigned int fb_width = _mesa_geometric_width(fb);
    const unsigned int fb_height = _mesa_geometric_height(fb);
    GLint miny, maxy;
    unsigned i;
    bool changed = false;
 
-   for (i = 0 ; i < ctx->Const.MaxViewports; i++) {
+   for (i = 0 ; i < st->state.num_viewports; i++) {
       scissor[i].minx = 0;
       scissor[i].miny = 0;
       scissor[i].maxx = fb_width;
       scissor[i].maxy = fb_height;
 
       if (ctx->Scissor.EnableFlags & (1 << i)) {
          /* need to be careful here with xmax or ymax < 0 */
          GLint xmax = MAX2(0, ctx->Scissor.ScissorArray[i].X + ctx->Scissor.ScissorArray[i].Width);
          GLint ymax = MAX2(0, ctx->Scissor.ScissorArray[i].Y + ctx->Scissor.ScissorArray[i].Height);
 
@@ -88,22 +88,26 @@ st_update_scissor( struct st_context *st )
          scissor[i].miny = miny;
          scissor[i].maxy = maxy;
       }
 
       if (memcmp(&scissor[i], &st->state.scissor[i], sizeof(scissor[0])) != 0) {
          /* state has changed */
          st->state.scissor[i] = scissor[i];  /* struct copy */
          changed = true;
       }
    }
-   if (changed)
-      st->pipe->set_scissor_states(st->pipe, 0, ctx->Const.MaxViewports, scissor); /* activate */
+
+   if (changed) {
+      struct pipe_context *pipe = st->pipe;
+
+      pipe->set_scissor_states(pipe, 0, st->state.num_viewports, scissor);
+   }
 }
 
 void
 st_update_window_rectangles(struct st_context *st)
 {
    struct pipe_scissor_state new_rects[PIPE_MAX_WINDOW_RECTANGLES];
    const struct gl_context *ctx = st->ctx;
    const struct gl_scissor_attrib *scissor = &ctx->Scissor;
    unsigned i;
    bool changed = false;
diff --git a/src/mesa/state_tracker/st_atom_viewport.c b/src/mesa/state_tracker/st_atom_viewport.c
index 9a9d570..b434f0d 100644
--- a/src/mesa/state_tracker/st_atom_viewport.c
+++ b/src/mesa/state_tracker/st_atom_viewport.c
@@ -40,27 +40,32 @@
  *  - window pos/size or FBO size
  */
 void
 st_update_viewport( struct st_context *st )
 {
    struct gl_context *ctx = st->ctx;
    unsigned i;
 
    /* _NEW_VIEWPORT 
     */
-   for (i = 0; i < ctx->Const.MaxViewports; i++) {
+   for (i = 0; i < st->state.num_viewports; i++) {
       float *scale = st->state.viewport[i].scale;
       float *translate = st->state.viewport[i].translate;
 
       _mesa_get_viewport_xform(ctx, i, scale, translate);
 
       /* _NEW_BUFFERS */
       if (st->state.fb_orientation == Y_0_TOP) {
          scale[1] *= -1;
          translate[1] = translate[1] * -1 + st->state.fb_height;
       }
    }
 
    cso_set_viewport(st->cso_context, &st->state.viewport[0]);
-   if (ctx->Const.MaxViewports > 1)
-      st->pipe->set_viewport_states(st->pipe, 1, ctx->Const.MaxViewports - 1, &st->state.viewport[1]);
+
+   if (st->state.num_viewports > 1) {
+      struct pipe_context *pipe = st->pipe;
+
+      pipe->set_viewport_states(pipe, 1, st->state.num_viewports - 1,
+                                &st->state.viewport[1]);
+   }
 }
diff --git a/src/mesa/state_tracker/st_context.h b/src/mesa/state_tracker/st_context.h
index 2fe9d92..5c7c58d 100644
--- a/src/mesa/state_tracker/st_context.h
+++ b/src/mesa/state_tracker/st_context.h
@@ -142,20 +142,21 @@ struct st_context
       GLuint num_sampler_views[PIPE_SHADER_TYPES];
       struct pipe_clip_state clip;
       struct {
          void *ptr;
          unsigned size;
       } constants[PIPE_SHADER_TYPES];
       unsigned fb_width;
       unsigned fb_height;
       unsigned fb_num_samples;
       unsigned fb_num_layers;
+      unsigned num_viewports;
       struct pipe_scissor_state scissor[PIPE_MAX_VIEWPORTS];
       struct pipe_viewport_state viewport[PIPE_MAX_VIEWPORTS];
       struct {
          unsigned num;
          boolean include;
          struct pipe_scissor_state rects[PIPE_MAX_WINDOW_RECTANGLES];
       } window_rects;
 
       GLuint poly_stipple[32];  /**< In OpenGL's bottom-to-top order */
 
-- 
2.7.4



More information about the mesa-dev mailing list