Patch introduces gl_shader_cache class that uses ir_cache to save and load shaders from the disk.
Known issues / bugs: - lightsmark textures black, unigine and glb2.7 texturing work just fine though .. something is broken there (?) - warsow going crazy at 'move_push_constants_to_pull_constants', (int pull_constant_loc[this->uniforms];), something broken TODO - recursive cache directory generation - env variable 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.pa...@intel.com> --- src/mesa/Makefile.sources | 1 + src/mesa/main/context.c | 2 + src/mesa/main/mtypes.h | 4 + src/mesa/main/shadercache.cpp | 198 ++++++++++++++++++++++++++++++++++++++++++ src/mesa/main/shadercache.h | 68 +++++++++++++++ 5 files changed, 273 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 122ea8e..f5ef82a 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/context.c b/src/mesa/main/context.c index 58f42cc..901a652 100644 --- a/src/mesa/main/context.c +++ b/src/mesa/main/context.c @@ -131,6 +131,7 @@ #include "program/prog_print.h" #include "math/m_matrix.h" #include "main/dispatch.h" /* for _gloffset_COUNT */ +#include "shadercache.h" #ifdef USE_SPARC_ASM #include "sparc/sparc.h" @@ -774,6 +775,7 @@ init_attrib_groups(struct gl_context *ctx) _mesa_init_rastpos( ctx ); _mesa_init_scissor( ctx ); _mesa_init_shader_state( ctx ); + _mesa_init_shader_cache( ctx ); _mesa_init_stencil( ctx ); _mesa_init_transform( ctx ); _mesa_init_transform_feedback( ctx ); diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h index c88c1c6..9716b16 100644 --- a/src/mesa/main/mtypes.h +++ b/src/mesa/main/mtypes.h @@ -72,6 +72,7 @@ struct gl_attrib_node; struct gl_list_extensions; struct gl_meta_state; struct gl_program_cache; +struct gl_shader_cache; struct gl_texture_object; struct gl_context; struct st_context; @@ -2006,6 +2007,9 @@ struct gl_vertex_program_state /** Cache of fixed-function programs */ struct gl_program_cache *Cache; + /** Cache of GLSL shaders */ + struct gl_shader_cache *ShaderCache; + GLboolean _Overriden; }; diff --git a/src/mesa/main/shadercache.cpp b/src/mesa/main/shadercache.cpp new file mode 100644 index 0000000..fd19c20 --- /dev/null +++ b/src/mesa/main/shadercache.cpp @@ -0,0 +1,198 @@ + +/* -*- 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; + } + + 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; + + /** + * ir_cache dumps state information, shader dump header, + * function prototypes and finally shader->ir to a given file + */ + int error = cache.serialize(shader, state, out, mesa_sha); + + fclose(out); + + /* errors during writing, destroy results */ + if (error != 0) { + rename(shader_path, shader_path_bad); + return error; + } + + 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 _______________________________________________ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev