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

Timothy Arceri timothy.arceri at collabora.com
Wed Jun 1 06:22:53 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 843998d..dfdd10e 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.
@@ -1800,6 +1802,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 e712ee3..4980984 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"
@@ -4478,6 +4481,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 0b47ebe..7c2d8fe 100644
--- a/src/mesa/drivers/dri/i965/brw_state_upload.c
+++ b/src/mesa/drivers/dri/i965/brw_state_upload.c
@@ -707,6 +707,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);
 
@@ -739,6 +743,10 @@ brw_upload_programs(struct brw_context *brw,
       }
 
       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 7f24a9e..9799099 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"
@@ -3063,6 +3066,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;
@@ -3079,6 +3090,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