[Mesa-dev] [PATCH 5/8] st/mesa: when changing shaders, only dirty states that are affected by them

Marek Olšák maraeo at gmail.com
Sun Aug 7 01:12:21 UTC 2016


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

This reduces the amount of state processing that has no effect.
---
 src/mesa/state_tracker/st_atom.c        | 78 ++++++++++++++++++++++++++-------
 src/mesa/state_tracker/st_atom.h        | 72 ++----------------------------
 src/mesa/state_tracker/st_cb_feedback.c |  6 ++-
 src/mesa/state_tracker/st_cb_program.c  | 14 +++---
 4 files changed, 79 insertions(+), 91 deletions(-)

diff --git a/src/mesa/state_tracker/st_atom.c b/src/mesa/state_tracker/st_atom.c
index 9985168..dddc5ff 100644
--- a/src/mesa/state_tracker/st_atom.c
+++ b/src/mesa/state_tracker/st_atom.c
@@ -56,55 +56,93 @@ void st_destroy_atoms( struct st_context *st )
 {
    /* no-op */
 }
 
 
 /* Too complex to figure out, just check every time:
  */
 static void check_program_state( struct st_context *st )
 {
    struct gl_context *ctx = st->ctx;
+   struct st_vertex_program *old_vp = st->vp;
+   struct st_tessctrl_program *old_tcp = st->tcp;
+   struct st_tesseval_program *old_tep = st->tep;
+   struct st_geometry_program *old_gp = st->gp;
+   struct st_fragment_program *old_fp = st->fp;
+
+   struct gl_vertex_program *new_vp = ctx->VertexProgram._Current;
+   struct gl_tess_ctrl_program *new_tcp = ctx->TessCtrlProgram._Current;
+   struct gl_tess_eval_program *new_tep = ctx->TessEvalProgram._Current;
+   struct gl_geometry_program *new_gp = ctx->GeometryProgram._Current;
+   struct gl_fragment_program *new_fp = ctx->FragmentProgram._Current;
+   uint64_t dirty = 0;
+
+   /* 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));
+   }
 
-   if (ctx->VertexProgram._Current != &st->vp->Base)
-      st->dirty |= ST_NEW_VERTEX_PROGRAM(st);
-
-   if (ctx->FragmentProgram._Current != &st->fp->Base)
-      st->dirty |= ST_NEW_FRAGMENT_PROGRAM;
+   if (unlikely(new_tcp != &old_tcp->Base)) {
+      if (old_tcp)
+         dirty |= old_tcp->affected_states;
+      if (new_tcp)
+         dirty |= st_tessctrl_program(new_tcp)->affected_states;
+   }
 
-   if (ctx->GeometryProgram._Current != &st->gp->Base)
-      st->dirty |= ST_NEW_GEOMETRY_PROGRAM;
+   if (unlikely(new_tep != &old_tep->Base)) {
+      if (old_tep)
+         dirty |= old_tep->affected_states;
+      if (new_tep)
+         dirty |= st_tesseval_program(new_tep)->affected_states;
+   }
 
-   if (ctx->TessCtrlProgram._Current != &st->tcp->Base)
-      st->dirty |= ST_NEW_TESSCTRL_PROGRAM;
+   if (unlikely(new_gp != &old_gp->Base)) {
+      if (old_gp)
+         dirty |= old_gp->affected_states;
+      if (new_gp)
+         dirty |= st_geometry_program(new_gp)->affected_states;
+   }
 
-   if (ctx->TessEvalProgram._Current != &st->tep->Base)
-      st->dirty |= ST_NEW_TESSEVAL_PROGRAM;
+   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;
+   }
 
+   st->dirty |= dirty;
    st->gfx_shaders_may_be_dirty = false;
 }
 
 static void check_attrib_edgeflag(struct st_context *st)
 {
    const struct gl_client_array **arrays = st->ctx->Array._DrawArrays;
    GLboolean vertdata_edgeflags, edgeflag_culls_prims, edgeflags_enabled;
+   struct gl_vertex_program *vp = st->ctx->VertexProgram._Current;
 
    if (!arrays)
       return;
 
    edgeflags_enabled = st->ctx->Polygon.FrontMode != GL_FILL ||
                        st->ctx->Polygon.BackMode != GL_FILL;
 
    vertdata_edgeflags = edgeflags_enabled &&
                         arrays[VERT_ATTRIB_EDGEFLAG]->StrideB != 0;
    if (vertdata_edgeflags != st->vertdata_edgeflags) {
       st->vertdata_edgeflags = vertdata_edgeflags;
-      st->dirty |= ST_NEW_VERTEX_PROGRAM(st);
+      if (vp)
+         st->dirty |= ST_NEW_VERTEX_PROGRAM(st, st_vertex_program(vp));
    }
 
    edgeflag_culls_prims = edgeflags_enabled && !vertdata_edgeflags &&
                           !st->ctx->Current.Attrib[VERT_ATTRIB_EDGEFLAG][0];
    if (edgeflag_culls_prims != st->edgeflag_culls_prims) {
       st->edgeflag_culls_prims = edgeflag_culls_prims;
       st->dirty |= ST_NEW_RASTERIZER;
    }
 }
 
@@ -127,27 +165,37 @@ void st_validate_state( struct st_context *st, enum st_pipeline pipeline )
    switch (pipeline) {
    case ST_PIPELINE_RENDER:
       if (st->ctx->API == API_OPENGL_COMPAT)
          check_attrib_edgeflag(st);
 
       check_program_state(st);
       st_manager_validate_framebuffers(st);
 
       pipeline_mask = ST_PIPELINE_RENDER_STATE_MASK;
       break;
-   case ST_PIPELINE_COMPUTE:
-      if (ctx->ComputeProgram._Current != &st->cp->Base)
-         st->dirty |= ST_NEW_COMPUTE_PROGRAM;
+
+   case ST_PIPELINE_COMPUTE: {
+      struct st_compute_program *old_cp = st->cp;
+      struct gl_compute_program *new_cp = ctx->ComputeProgram._Current;
+
+      if (new_cp != &old_cp->Base) {
+         if (old_cp)
+            st->dirty |= old_cp->affected_states;
+         assert(new_cp);
+         st->dirty |= st_compute_program(new_cp)->affected_states;
+      }
 
       st->compute_shader_may_be_dirty = false;
       pipeline_mask = ST_PIPELINE_COMPUTE_STATE_MASK;
       break;
+   }
+
    default:
       unreachable("Invalid pipeline specified");
    }
 
    dirty = st->dirty & pipeline_mask;
    if (!dirty)
       return;
 
    dirty_lo = dirty;
    dirty_hi = dirty >> 32;
diff --git a/src/mesa/state_tracker/st_atom.h b/src/mesa/state_tracker/st_atom.h
index 7c8cd3a..930dfd5 100644
--- a/src/mesa/state_tracker/st_atom.h
+++ b/src/mesa/state_tracker/st_atom.h
@@ -81,86 +81,23 @@ enum {
 #undef ST_STATE
 
 /* Combined state flags. */
 #define ST_NEW_SAMPLERS         (ST_NEW_RENDER_SAMPLERS | \
                                  ST_NEW_CS_SAMPLERS)
 
 #define ST_NEW_FRAMEBUFFER      (ST_NEW_FB_STATE | \
                                  ST_NEW_SAMPLE_MASK | \
                                  ST_NEW_SAMPLE_SHADING)
 
-#define ST_NEW_VERTEX_PROGRAM(st) (ST_NEW_VS_STATE | \
-                                   ST_NEW_VS_SAMPLER_VIEWS | \
-                                   ST_NEW_VS_IMAGES | \
-                                   ST_NEW_VS_CONSTANTS | \
-                                   ST_NEW_VS_UBOS | \
-                                   ST_NEW_VS_ATOMICS | \
-                                   ST_NEW_VS_SSBOS | \
-                                   ST_NEW_VERTEX_ARRAYS | \
-                                   (st_user_clip_planes_enabled(st->ctx) ? \
-                                    ST_NEW_CLIP_STATE : 0) | \
-                                   ST_NEW_RASTERIZER | \
-                                   ST_NEW_RENDER_SAMPLERS)
-
-#define ST_NEW_TCS_RESOURCES    (ST_NEW_TCS_SAMPLER_VIEWS | \
-                                 ST_NEW_TCS_IMAGES | \
-                                 ST_NEW_TCS_CONSTANTS | \
-                                 ST_NEW_TCS_UBOS | \
-                                 ST_NEW_TCS_ATOMICS | \
-                                 ST_NEW_TCS_SSBOS)
-
-#define ST_NEW_TESSCTRL_PROGRAM (ST_NEW_TCS_STATE | \
-                                 ST_NEW_TCS_RESOURCES | \
-                                 ST_NEW_RENDER_SAMPLERS)
-
-#define ST_NEW_TES_RESOURCES    (ST_NEW_TES_SAMPLER_VIEWS | \
-                                 ST_NEW_TES_IMAGES | \
-                                 ST_NEW_TES_CONSTANTS | \
-                                 ST_NEW_TES_UBOS | \
-                                 ST_NEW_TES_ATOMICS | \
-                                 ST_NEW_TES_SSBOS)
-
-#define ST_NEW_TESSEVAL_PROGRAM (ST_NEW_TES_STATE | \
-                                 ST_NEW_TES_RESOURCES | \
-                                 ST_NEW_RASTERIZER | \
-                                 ST_NEW_RENDER_SAMPLERS)
-
-#define ST_NEW_GS_RESOURCES     (ST_NEW_GS_SAMPLER_VIEWS | \
-                                 ST_NEW_GS_IMAGES | \
-                                 ST_NEW_GS_CONSTANTS | \
-                                 ST_NEW_GS_UBOS | \
-                                 ST_NEW_GS_ATOMICS | \
-                                 ST_NEW_GS_SSBOS)
-
-#define ST_NEW_GEOMETRY_PROGRAM (ST_NEW_GS_STATE | \
-                                 ST_NEW_GS_RESOURCES | \
-                                 ST_NEW_RASTERIZER | \
-                                 ST_NEW_RENDER_SAMPLERS)
-
-#define ST_NEW_FRAGMENT_PROGRAM (ST_NEW_FS_STATE | \
-                                 ST_NEW_FS_SAMPLER_VIEWS | \
-                                 ST_NEW_FS_IMAGES | \
-                                 ST_NEW_FS_CONSTANTS | \
-                                 ST_NEW_FS_UBOS | \
-                                 ST_NEW_FS_ATOMICS | \
-                                 ST_NEW_FS_SSBOS | \
-                                 ST_NEW_SAMPLE_SHADING | \
-                                 ST_NEW_RENDER_SAMPLERS)
-
-#define ST_NEW_COMPUTE_PROGRAM  (ST_NEW_CS_STATE | \
-                                 ST_NEW_CS_SAMPLER_VIEWS | \
-                                 ST_NEW_CS_IMAGES | \
-                                 ST_NEW_CS_CONSTANTS | \
-                                 ST_NEW_CS_UBOS | \
-                                 ST_NEW_CS_ATOMICS | \
-                                 ST_NEW_CS_SSBOS | \
-                                 ST_NEW_CS_SAMPLERS)
+#define ST_NEW_VERTEX_PROGRAM(st, p) (p->affected_states | \
+                                      (st_user_clip_planes_enabled(st->ctx) ? \
+                                       ST_NEW_CLIP_STATE : 0))
 
 #define ST_NEW_CONSTANTS        (ST_NEW_VS_CONSTANTS | \
                                  ST_NEW_TCS_CONSTANTS | \
                                  ST_NEW_TES_CONSTANTS | \
                                  ST_NEW_FS_CONSTANTS | \
                                  ST_NEW_GS_CONSTANTS | \
                                  ST_NEW_CS_CONSTANTS)
 
 #define ST_NEW_UNIFORM_BUFFER   (ST_NEW_VS_UBOS | \
                                  ST_NEW_TCS_UBOS | \
@@ -192,17 +129,16 @@ enum {
 
 #define ST_NEW_IMAGE_UNITS      (ST_NEW_VS_IMAGES | \
                                  ST_NEW_TCS_IMAGES | \
                                  ST_NEW_TES_IMAGES | \
                                  ST_NEW_GS_IMAGES | \
                                  ST_NEW_FS_IMAGES | \
                                  ST_NEW_CS_IMAGES)
 
 /* All state flags within each group: */
 #define ST_PIPELINE_RENDER_STATE_MASK  (ST_NEW_CS_STATE - 1)
-#define ST_PIPELINE_COMPUTE_STATE_MASK (ST_NEW_COMPUTE_PROGRAM | \
-                                        ST_NEW_CS_SAMPLERS)
+#define ST_PIPELINE_COMPUTE_STATE_MASK (0xffllu << ST_NEW_CS_STATE_INDEX)
 
 #define ST_ALL_STATES_MASK (ST_PIPELINE_RENDER_STATE_MASK | \
                             ST_PIPELINE_COMPUTE_STATE_MASK)
 
 #endif
diff --git a/src/mesa/state_tracker/st_cb_feedback.c b/src/mesa/state_tracker/st_cb_feedback.c
index db682cc..d624d9f 100644
--- a/src/mesa/state_tracker/st_cb_feedback.c
+++ b/src/mesa/state_tracker/st_cb_feedback.c
@@ -39,20 +39,21 @@
 
 #include "main/imports.h"
 #include "main/context.h"
 #include "main/feedback.h"
 
 #include "vbo/vbo.h"
 
 #include "st_context.h"
 #include "st_draw.h"
 #include "st_cb_feedback.h"
+#include "st_program.h"
 
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
 
 #include "draw/draw_context.h"
 #include "draw/draw_pipe.h"
 
 
 /**
  * This is actually used for both feedback and selection.
@@ -284,26 +285,29 @@ st_RenderMode(struct gl_context *ctx, GLenum newMode )
       vbo_set_draw_func(ctx, st_draw_vbo);
    }
    else if (newMode == GL_SELECT) {
       if (!st->selection_stage)
          st->selection_stage = draw_glselect_stage(ctx, draw);
       draw_set_rasterize_stage(draw, st->selection_stage);
       /* Plug in new vbo draw function */
       vbo_set_draw_func(ctx, st_feedback_draw_vbo);
    }
    else {
+      struct gl_vertex_program *vp = st->ctx->VertexProgram._Current;
+
       if (!st->feedback_stage)
          st->feedback_stage = draw_glfeedback_stage(ctx, draw);
       draw_set_rasterize_stage(draw, st->feedback_stage);
       /* Plug in new vbo draw function */
       vbo_set_draw_func(ctx, st_feedback_draw_vbo);
       /* need to generate/use a vertex program that emits pos/color/tex */
-      st->dirty |= ST_NEW_VERTEX_PROGRAM(st);
+      if (vp)
+         st->dirty |= ST_NEW_VERTEX_PROGRAM(st, st_vertex_program(vp));
    }
 }
 
 
 
 void st_init_feedback_functions(struct dd_function_table *functions)
 {
    functions->RenderMode = st_RenderMode;
 }
diff --git a/src/mesa/state_tracker/st_cb_program.c b/src/mesa/state_tracker/st_cb_program.c
index 1783a1c..1fd5019 100644
--- a/src/mesa/state_tracker/st_cb_program.c
+++ b/src/mesa/state_tracker/st_cb_program.c
@@ -187,93 +187,93 @@ st_program_string_notify( struct gl_context *ctx,
    gl_shader_stage stage = _mesa_program_enum_to_shader_stage(target);
 
    if (target == GL_FRAGMENT_PROGRAM_ARB) {
       struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
 
       st_release_fp_variants(st, stfp);
       if (!st_translate_fragment_program(st, stfp))
          return false;
 
       if (st->fp == stfp)
-	 st->dirty |= ST_NEW_FRAGMENT_PROGRAM;
+	 st->dirty |= stfp->affected_states;
    }
    else if (target == GL_GEOMETRY_PROGRAM_NV) {
       struct st_geometry_program *stgp = (struct st_geometry_program *) prog;
 
       st_release_basic_variants(st, stgp->Base.Base.Target,
                                 &stgp->variants, &stgp->tgsi);
       if (!st_translate_geometry_program(st, stgp))
          return false;
 
       if (st->gp == stgp)
-	 st->dirty |= ST_NEW_GEOMETRY_PROGRAM;
+	 st->dirty |= stgp->affected_states;
    }
    else if (target == GL_VERTEX_PROGRAM_ARB) {
       struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
 
       st_release_vp_variants(st, stvp);
       if (!st_translate_vertex_program(st, stvp))
          return false;
 
       if (st->vp == stvp)
-	 st->dirty |= ST_NEW_VERTEX_PROGRAM(st);
+	 st->dirty |= ST_NEW_VERTEX_PROGRAM(st, stvp);
    }
    else if (target == GL_TESS_CONTROL_PROGRAM_NV) {
       struct st_tessctrl_program *sttcp =
          (struct st_tessctrl_program *) prog;
 
       st_release_basic_variants(st, sttcp->Base.Base.Target,
                                 &sttcp->variants, &sttcp->tgsi);
       if (!st_translate_tessctrl_program(st, sttcp))
          return false;
 
       if (st->tcp == sttcp)
-         st->dirty |= ST_NEW_TESSCTRL_PROGRAM;
+         st->dirty |= sttcp->affected_states;
    }
    else if (target == GL_TESS_EVALUATION_PROGRAM_NV) {
       struct st_tesseval_program *sttep =
          (struct st_tesseval_program *) prog;
 
       st_release_basic_variants(st, sttep->Base.Base.Target,
                                 &sttep->variants, &sttep->tgsi);
       if (!st_translate_tesseval_program(st, sttep))
          return false;
 
       if (st->tep == sttep)
-         st->dirty |= ST_NEW_TESSEVAL_PROGRAM;
+         st->dirty |= sttep->affected_states;
    }
    else if (target == GL_COMPUTE_PROGRAM_NV) {
       struct st_compute_program *stcp =
          (struct st_compute_program *) prog;
 
       st_release_cp_variants(st, stcp);
       if (!st_translate_compute_program(st, stcp))
          return false;
 
       if (st->cp == stcp)
-         st->dirty |= ST_NEW_COMPUTE_PROGRAM;
+         st->dirty |= stcp->affected_states;
    }
    else if (target == GL_FRAGMENT_SHADER_ATI) {
       assert(prog);
 
       struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
       assert(stfp->ati_fs);
       assert(stfp->ati_fs->Program == prog);
 
       st_init_atifs_prog(ctx, prog);
 
       st_release_fp_variants(st, stfp);
       if (!st_translate_fragment_program(st, stfp))
          return false;
 
       if (st->fp == stfp)
-         st->dirty |= ST_NEW_FRAGMENT_PROGRAM;
+         st->dirty |= stfp->affected_states;
    }
 
    if (ST_DEBUG & DEBUG_PRECOMPILE ||
        st->shader_has_one_variant[stage])
       st_precompile_shader_variant(st, prog);
 
    return GL_TRUE;
 }
 
 /**
-- 
2.7.4



More information about the mesa-dev mailing list