[Mesa-dev] [PATCH 12/65] glsl, i965: make use of on disk shader cache

Timothy Arceri timothy.arceri at collabora.com
Fri Apr 29 13:33:11 UTC 2016


From: Carl Worth <cworth at cworth.org>

The hash key for glsl metadata is a hash of the hashes of each GLSL
source string.

This commit uses the put_key/get_key support in the cache put the SHA-1
hash of the source string for each successfully compiled shader into the
cache. This allows for early, optimistic returns from glCompileShader
(if the identical source string had been successfully compiled in the pase),
in the hope that the final, linked shader will be found in the cache.

Signed-off-by: Timothy Arceri <timothy.arceri at collabora.com>
---
 src/compiler/glsl/glsl_parser_extras.cpp     | 14 ++++++++++++++
 src/compiler/glsl/linker.cpp                 |  8 ++++++++
 src/mesa/drivers/dri/i965/brw_state_upload.c |  8 ++++++++
 src/mesa/program/ir_to_mesa.cpp              | 15 +++++++++++++++
 4 files changed, 45 insertions(+)

diff --git a/src/compiler/glsl/glsl_parser_extras.cpp b/src/compiler/glsl/glsl_parser_extras.cpp
index fb64350..762b192 100644
--- a/src/compiler/glsl/glsl_parser_extras.cpp
+++ b/src/compiler/glsl/glsl_parser_extras.cpp
@@ -36,6 +36,8 @@
 #include "glsl_parser.h"
 #include "ir_optimization.h"
 #include "loop_analysis.h"
+#include "cache.h"
+#include "util/mesa-sha1.h"
 
 /**
  * Format a short human-readable description of the given GLSL version.
@@ -1761,6 +1763,18 @@ _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,
    state->error = glcpp_preprocess(state, &source, &state->info_log,
                              &ctx->Extensions, ctx);
 
+#ifdef ENABLE_SHADER_CACHE
+   char buf[41];
+   _mesa_sha1_compute(source, strlen(source), shader->sha1);
+   if (ctx->Cache && cache_has_key(ctx->Cache, shader->sha1)) {
+      /* We've seen this shader before and know it compiles */
+      printf("deferring compile of shader: %s\n",
+             _mesa_sha1_format(buf, shader->sha1));
+      shader->CompileStatus = true;
+      return;
+   }
+#endif
+
    if (!state->error) {
      _mesa_glsl_lexer_ctor(state, source);
      _mesa_glsl_parse(state);
diff --git a/src/compiler/glsl/linker.cpp b/src/compiler/glsl/linker.cpp
index dcc8a57..2e30dae 100644
--- a/src/compiler/glsl/linker.cpp
+++ b/src/compiler/glsl/linker.cpp
@@ -77,6 +77,9 @@
 #include "ir_optimization.h"
 #include "ir_rvalue_visitor.h"
 #include "ir_uniform.h"
+#include "util/mesa-sha1.h"
+#include "cache.h"
+#include "shader_cache.h"
 
 #include "main/shaderobj.h"
 #include "main/enums.h"
@@ -4311,6 +4314,11 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
    char **varying_names = NULL;
    tfeedback_decl *tfeedback_decls = NULL;
 
+#ifdef ENABLE_SHADER_CACHE
+   if (shader_cache_read_program_metadata(ctx, prog))
+      return;
+#endif
+
    void *mem_ctx = ralloc_context(NULL); // temporary linker context
 
    prog->ARB_fragment_coord_conventions_enable = false;
diff --git a/src/mesa/drivers/dri/i965/brw_state_upload.c b/src/mesa/drivers/dri/i965/brw_state_upload.c
index 56bb95c..556c9d1 100644
--- a/src/mesa/drivers/dri/i965/brw_state_upload.c
+++ b/src/mesa/drivers/dri/i965/brw_state_upload.c
@@ -724,6 +724,10 @@ brw_upload_programs(struct brw_context *brw,
                     enum brw_pipeline pipeline)
 {
    if (pipeline == BRW_RENDER_PIPELINE) {
+#ifdef ENABLE_SHADER_CACHE
+      upload_cached_program(brw);
+#endif
+
       brw_upload_vs_prog(brw);
       brw_upload_tess_programs(brw);
 
@@ -750,6 +754,10 @@ brw_upload_programs(struct brw_context *brw,
          brw->ctx.NewDriverState |= BRW_NEW_VUE_MAP_GEOM_OUT;
 
       brw_upload_wm_prog(brw);
+
+#ifdef ENABLE_SHADER_CACHE
+      write_cached_program(brw);
+#endif
    } else if (pipeline == BRW_COMPUTE_PIPELINE) {
       brw_upload_cs_prog(brw);
    }
diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp
index 35a6856..c061dce 100644
--- a/src/mesa/program/ir_to_mesa.cpp
+++ b/src/mesa/program/ir_to_mesa.cpp
@@ -46,6 +46,9 @@
 #include "compiler/glsl_types.h"
 #include "compiler/glsl/linker.h"
 #include "compiler/glsl/program.h"
+#include "compiler/glsl/shader_cache.h"
+#include "program/hash_table.h"
+
 #include "program/hash_table.h"
 #include "program/prog_instruction.h"
 #include "program/prog_optimize.h"
@@ -3002,6 +3005,14 @@ _mesa_glsl_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
       link_shaders(ctx, prog);
    }
 
+   /* FIXME: We look at prog->Version to determine whether we actually linked
+    * the program or just loaded the uniform meta data from cache.  We
+    * probably want to turn prog->LinkStatus into an enum that captures the
+    * different states.
+    */
+   if (prog->LinkStatus && prog->Version == 0)
+      return;
+
    if (prog->LinkStatus) {
       if (!ctx->Driver.LinkShader(ctx, prog)) {
 	 prog->LinkStatus = GL_FALSE;
@@ -3018,6 +3029,10 @@ _mesa_glsl_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
 	 fprintf(stderr, "%s\n", prog->InfoLog);
       }
    }
+
+#ifdef ENABLE_SHADER_CACHE
+   shader_cache_write_program_metadata(ctx, prog);
+#endif
 }
 
 } /* extern "C" */
-- 
2.5.5



More information about the mesa-dev mailing list