[Mesa-dev] [PATCH 4/4] glsl: use gl_shader_cache when compiling (WIP)

Tapani Pälli tapani.palli at intel.com
Fri Sep 20 02:52:44 PDT 2013


Patch takes gl_shader_cache in to use. When compiling a shader, we
first search the cache if it exists already and can be used from there.
If cache did not exist, we attempt to cache the shader after compilation.

Signed-off-by: Tapani Pälli <tapani.palli at intel.com>
---
 src/glsl/glsl_parser_extras.cpp | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/src/glsl/glsl_parser_extras.cpp b/src/glsl/glsl_parser_extras.cpp
index cac5a18..cb546f7 100644
--- a/src/glsl/glsl_parser_extras.cpp
+++ b/src/glsl/glsl_parser_extras.cpp
@@ -30,6 +30,7 @@ extern "C" {
 #include "main/context.h"
 #include "main/shaderobj.h"
 }
+#include "main/shadercache.h"
 
 #include "ralloc.h"
 #include "ast.h"
@@ -37,6 +38,7 @@ extern "C" {
 #include "glsl_parser.h"
 #include "ir_optimization.h"
 #include "loop_analysis.h"
+#include "linker.h"
 
 /**
  * Format a short human-readable description of the given GLSL version.
@@ -1456,6 +1458,40 @@ _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,
    state->error = glcpp_preprocess(state, &source, &state->info_log,
                              &ctx->Extensions, ctx);
 
+   /* check if we have same shader cached */
+   struct gl_shader *existing =
+      ctx->VertexProgram.ShaderCache->find(shader, source, state);
+   if (existing) {
+      /* use cached shader, clone ir list, populate symbol table */
+      shader->CompileStatus = GL_TRUE;
+      shader->InfoLog = ralloc_strdup(shader, "cached shader");
+      shader->Version = existing->Version;
+      shader->Type = existing->Type;
+      shader->IsES = existing->IsES;
+
+      /**
+       * NOTE - following should not be needed and should be removed
+       * as takes a lot of time. Problem with this is that we might currently
+       * bail out while reading cache and decide to use the original. This
+       * should be decided/known already when writing cache.
+       */
+      ralloc_free(shader->ir);
+      shader->ir = new(shader) exec_list;
+      clone_ir_list(shader, shader->ir, existing->ir);
+      ralloc_free(existing->ir);
+      ralloc_free(existing);
+
+      populate_symbol_table(shader);
+
+      memcpy(shader->builtins_to_link, state->builtins_to_link,
+         sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
+      shader->num_builtins_to_link = state->num_builtins_to_link;
+
+      _mesa_debug(ctx, "used a shader from cache\n");
+      return;
+   }
+
+
    if (!state->error) {
      _mesa_glsl_lexer_ctor(state, source);
      _mesa_glsl_parse(state);
@@ -1524,6 +1560,10 @@ _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,
    /* Retain any live IR, but trash the rest. */
    reparent_ir(shader->ir, shader->ir);
 
+   /* attempt to cache this shader */
+   if (ctx->VertexProgram.ShaderCache->cache(shader, source, state) == 0)
+      _mesa_debug(ctx, "cached a shader\n");
+
    ralloc_free(state);
 }
 
-- 
1.8.1.4



More information about the mesa-dev mailing list