[Mesa-dev] [v2 2/4] mesa: gl_shader_cache class (WIP)

Tapani Pälli tapani.palli at intel.com
Sun Sep 29 23:14:57 PDT 2013


Patch introduces gl_shader_cache class that uses ir_cache
to save and load shaders from the disk. This can be used
to implement automatic 'behind the scenes' shader cache for
the compiler.

TODO

- recursive cache directory generation
- env variable to enable/disable cache
- configure option to enable/disable cache
- create new exec_list directly to the given shader (not
  having to copy it later as it takes extra time)

Signed-off-by: Tapani Pälli <tapani.palli at intel.com>
---
 src/mesa/Makefile.sources     |   1 +
 src/mesa/main/shadercache.cpp | 208 ++++++++++++++++++++++++++++++++++++++++++
 src/mesa/main/shadercache.h   |  68 ++++++++++++++
 3 files changed, 277 insertions(+)
 create mode 100644 src/mesa/main/shadercache.cpp
 create mode 100644 src/mesa/main/shadercache.h

diff --git a/src/mesa/Makefile.sources b/src/mesa/Makefile.sources
index ff242b4..23e3b52 100644
--- a/src/mesa/Makefile.sources
+++ b/src/mesa/Makefile.sources
@@ -41,6 +41,7 @@ MAIN_FILES = \
 	$(SRCDIR)main/feedback.c \
 	$(SRCDIR)main/ffvertex_prog.c \
 	$(SRCDIR)main/ff_fragment_shader.cpp \
+	$(SRCDIR)main/shadercache.cpp \
 	$(SRCDIR)main/fog.c \
 	$(SRCDIR)main/formatquery.c \
 	$(SRCDIR)main/formats.c \
diff --git a/src/mesa/main/shadercache.cpp b/src/mesa/main/shadercache.cpp
new file mode 100644
index 0000000..538496e
--- /dev/null
+++ b/src/mesa/main/shadercache.cpp
@@ -0,0 +1,208 @@
+
+/* -*- c++ -*- */
+/*
+ * Copyright © 2013 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "imports.h"
+#include "shadercache.h"
+#include "ir_cache.h"
+
+/**
+ * How it works:
+ *
+ * Shader cache uses ir_cache class to serialize instructions
+ * in a binary form to ~/.cache/mesa directory. When loading, a
+ * new IR exec_list is constructed from loaded data and new shader
+ * created and returned.
+ */
+
+
+void gl_shader_cache::init_cache_path()
+{
+   const char *tmp = "/tmp";
+   const char *cache_root = getenv("XDG_CACHE_DIR");
+   if (!cache_root)
+      cache_root = getenv("HOME");
+   if (!cache_root)
+      cache_root = tmp;
+
+   _mesa_snprintf(cache_path, MAX_CACHE_PATHLEN,
+      "%s/.cache/mesa", cache_root);
+
+   struct stat stat_info;
+
+   /**
+    * FIXME construct the whole path, now assumes ~/.cache
+    */
+   if(stat(cache_path, &stat_info) != 0) {
+      if(mkdir(cache_path, 0775))
+         fprintf(stderr, "%s: error creating shader cache [%s]\n",
+		__func__, cache_path);
+      else
+         printf("%s: created shader cache [%s]\n", __func__, cache_path);
+   }
+}
+
+
+extern "C" void
+_mesa_init_shader_cache(struct gl_context *ctx)
+{
+   ctx->VertexProgram.ShaderCache = new gl_shader_cache;
+}
+
+
+gl_shader_cache::gl_shader_cache()
+{
+   init_cache_path();
+   mem_ctx = ralloc_context(NULL);
+}
+
+
+gl_shader_cache::~gl_shader_cache()
+{
+   ralloc_free(mem_ctx);
+   mem_ctx = NULL;
+}
+
+
+uint32_t
+gl_shader_cache::generate_key(const char *source)
+{
+   /* Is this enough or do we need something like sha1? */
+   return _mesa_str_checksum(source);
+}
+
+
+/**
+ * Search cache for a shader, returns generated gl_shader object if found
+ */
+struct gl_shader *
+gl_shader_cache::find(struct gl_shader *shader,
+   const char *source, struct _mesa_glsl_parse_state *state,
+   const char *mesa_sha)
+{
+   char shader_path[MAX_CACHE_PATHLEN];
+   char shader_path_bad[MAX_CACHE_PATHLEN];
+   _mesa_snprintf(shader_path, MAX_CACHE_PATHLEN, "%s/%d",
+      cache_path, generate_key(source));
+   _mesa_snprintf(shader_path_bad, MAX_CACHE_PATHLEN, "%s_bad",
+      shader_path);
+
+   struct stat stat_info;
+
+   /* if bad cache entry found, return */
+   if(stat(shader_path_bad, &stat_info) == 0)
+      return NULL;
+
+   if(stat(shader_path, &stat_info) != 0)
+      return NULL;
+
+   ir_cache cache;
+   memory_map map;
+
+   if(map.map(shader_path)) {
+      CACHE_DEBUG("error mapping shader from cache\n");
+      return NULL;
+   }
+
+   long shader_size;
+   map.read_value(&shader_size);
+   CACHE_DEBUG("shader %s size %d\n", shader_path, shader_size);
+
+   int error = 0;
+   struct gl_shader *result =
+      cache.unserialize(mem_ctx, map, state, mesa_sha, &error);
+
+   if (!result) {
+      CACHE_DEBUG("error reading shader from cache\n(%s)\n", shader_path);
+
+      if (error == ir_cache::DIFFERENT_MESA_SHA) {
+         /* cache produced with different mesa version, invalidate cache */
+         unlink(shader_path);
+      } else {
+         /* read errors, save cache for debugging purposes */
+         _mesa_snprintf(shader_path_bad, MAX_CACHE_PATHLEN, "%s_bad",
+            shader_path);
+         rename(shader_path, shader_path_bad);
+      }
+      return NULL;
+   }
+
+   /*_mesa_print_ir(result->ir, NULL);*/
+
+   return result;
+}
+
+
+/**
+ * Cache a gl_shader to disk, exec_list + anything else required
+ */
+int
+gl_shader_cache::cache(struct gl_shader *shader,
+   const char *source, struct _mesa_glsl_parse_state *state,
+   const char *mesa_sha)
+{
+   char shader_path[MAX_CACHE_PATHLEN];
+   char shader_path_bad[MAX_CACHE_PATHLEN];
+   _mesa_snprintf(shader_path, MAX_CACHE_PATHLEN, "%s/%d",
+      cache_path, generate_key(source));
+   _mesa_snprintf(shader_path_bad, MAX_CACHE_PATHLEN, "%s_bad",
+      shader_path);
+
+   /*_mesa_print_ir(shader->ir, NULL);*/
+
+   struct stat stat_info;
+
+   /* if bad cache entry found, return */
+   if(stat(shader_path_bad, &stat_info) == 0)
+      return -1;
+
+   FILE *out = fopen(shader_path, "w+");
+
+   if (!out)
+      return -1;
+
+   ir_cache cache;
+   size_t size = 0;
+
+   /**
+    * ir_cache dumps a header, function prototypes
+    * and finally shader->ir to memory
+    */
+   char *blob = cache.serialize(shader, state, mesa_sha, &size);
+
+   if (blob)
+      fwrite(blob, size, 1, out);
+
+   fclose(out);
+
+   /* errors during writing, destroy results */
+   if (!blob) {
+      rename(shader_path, shader_path_bad);
+      return -1;
+   }
+
+   free(blob);
+
+   return 0;
+}
diff --git a/src/mesa/main/shadercache.h b/src/mesa/main/shadercache.h
new file mode 100644
index 0000000..0c60103
--- /dev/null
+++ b/src/mesa/main/shadercache.h
@@ -0,0 +1,68 @@
+/* -*- c++ -*- */
+/*
+ * Copyright © 2013 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#pragma once
+#ifndef SHADERCACHE_DOT_H
+#define SHADERCACHE_DOT_H
+
+#include "mtypes.h"
+
+/* FIXME - does not exist yet .. */
+/*#include "git_sha1.h"          */
+
+static const char *MESA_GIT_SHA1 = "0xdeadbeef";
+
+/* FIXME, static only for now */
+#define MAX_CACHE_PATHLEN 256
+
+#ifdef __cplusplus
+class gl_shader_cache {
+public:
+   gl_shader_cache();
+   ~gl_shader_cache();
+
+   struct gl_shader *find(struct gl_shader *shader,
+      const char *source,
+      struct _mesa_glsl_parse_state *state,
+      const char *mesa_sha = MESA_GIT_SHA1);
+
+   int cache(struct gl_shader *shader,
+      const char *source,
+      struct _mesa_glsl_parse_state *state,
+      const char *mesa_sha = MESA_GIT_SHA1);
+
+private:
+   void init_cache_path();
+   uint32_t generate_key(const char *source);
+
+   char cache_path[MAX_CACHE_PATHLEN];
+
+   void *mem_ctx;
+};
+
+extern "C"
+#endif
+void _mesa_init_shader_cache(struct gl_context *ctx);
+#endif
+
-- 
1.8.1.4



More information about the mesa-dev mailing list