2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file shader_cache.cpp
27 * GLSL shader cache implementation
29 * This uses disk_cache.c to write out a serialization of various
30 * state that's required in order to successfully load and use a
31 * binary written out by a drivers backend, this state is referred to as
32 * "metadata" throughout the implementation.
34 * The hash key for glsl metadata is a hash of the hashes of each GLSL
35 * source string as well as some API settings that change the final program
36 * such as SSO, attribute bindings, frag data bindings, etc.
38 * In order to avoid caching any actual IR we use the put_key/get_key support
39 * in the disk_cache to put the SHA-1 hash for each successfully compiled
40 * shader into the cache, and optimisticly return early from glCompileShader
41 * (if the identical shader had been successfully compiled in the past),
42 * in the hope that the final linked shader will be found in the cache.
43 * If anything goes wrong (shader variant not found, backend cache item is
44 * corrupt, etc) we will use a fallback path to compile and link the IR.
47 #include "compiler/shader_info.h"
48 #include "glsl_symbol_table.h"
49 #include "glsl_parser_extras.h"
51 #include "ir_optimization.h"
52 #include "ir_rvalue_visitor.h"
53 #include "ir_uniform.h"
55 #include "link_varyings.h"
58 #include "serialize.h"
59 #include "shader_cache.h"
60 #include "util/mesa-sha1.h"
61 #include "string_to_uint_map.h"
62 #include "main/mtypes.h"
65 #include "main/enums.h"
66 #include "main/shaderobj.h"
67 #include "program/program.h"
71 compile_shaders(struct gl_context
*ctx
, struct gl_shader_program
*prog
) {
72 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
73 _mesa_glsl_compile_shader(ctx
, prog
->Shaders
[i
], false, false, true);
78 create_binding_str(const char *key
, unsigned value
, void *closure
)
80 char **bindings_str
= (char **) closure
;
81 ralloc_asprintf_append(bindings_str
, "%s:%u,", key
, value
);
85 shader_cache_write_program_metadata(struct gl_context
*ctx
,
86 struct gl_shader_program
*prog
)
88 struct disk_cache
*cache
= ctx
->Cache
;
92 /* Exit early when we are dealing with a ff shader with no source file to
93 * generate a source from.
95 * TODO: In future we should use another method to generate a key for ff
98 static const char zero
[sizeof(prog
->data
->sha1
)] = {0};
99 if (memcmp(prog
->data
->sha1
, zero
, sizeof(prog
->data
->sha1
)) == 0)
102 struct blob metadata
;
103 blob_init(&metadata
);
105 if (ctx
->Driver
.ShaderCacheSerializeDriverBlob
) {
106 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
107 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
109 ctx
->Driver
.ShaderCacheSerializeDriverBlob(ctx
, sh
->Program
);
113 serialize_glsl_program(&metadata
, ctx
, prog
);
115 struct cache_item_metadata cache_item_metadata
;
116 cache_item_metadata
.type
= CACHE_ITEM_TYPE_GLSL
;
117 cache_item_metadata
.keys
=
118 (cache_key
*) malloc(prog
->NumShaders
* sizeof(cache_key
));
119 cache_item_metadata
.num_keys
= prog
->NumShaders
;
121 if (!cache_item_metadata
.keys
)
125 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
126 disk_cache_put_key(cache
, prog
->Shaders
[i
]->sha1
);
127 memcpy(cache_item_metadata
.keys
[i
], prog
->Shaders
[i
]->sha1
,
129 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
130 _mesa_sha1_format(sha1_buf
, prog
->Shaders
[i
]->sha1
);
131 fprintf(stderr
, "marking shader: %s\n", sha1_buf
);
135 disk_cache_put(cache
, prog
->data
->sha1
, metadata
.data
, metadata
.size
,
136 &cache_item_metadata
);
138 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
139 _mesa_sha1_format(sha1_buf
, prog
->data
->sha1
);
140 fprintf(stderr
, "putting program metadata in cache: %s\n", sha1_buf
);
144 free(cache_item_metadata
.keys
);
145 blob_finish(&metadata
);
149 shader_cache_read_program_metadata(struct gl_context
*ctx
,
150 struct gl_shader_program
*prog
)
152 /* Fixed function programs generated by Mesa are not cached. So don't
153 * try to read metadata for them from the cache.
158 struct disk_cache
*cache
= ctx
->Cache
;
162 /* Include bindings when creating sha1. These bindings change the resulting
163 * binary so they are just as important as the shader source.
165 char *buf
= ralloc_strdup(NULL
, "vb: ");
166 prog
->AttributeBindings
->iterate(create_binding_str
, &buf
);
167 ralloc_strcat(&buf
, "fb: ");
168 prog
->FragDataBindings
->iterate(create_binding_str
, &buf
);
169 ralloc_strcat(&buf
, "fbi: ");
170 prog
->FragDataIndexBindings
->iterate(create_binding_str
, &buf
);
171 ralloc_asprintf_append(&buf
, "tf: %d ", prog
->TransformFeedback
.BufferMode
);
172 for (unsigned int i
= 0; i
< prog
->TransformFeedback
.NumVarying
; i
++) {
173 ralloc_asprintf_append(&buf
, "%s:%d ",
174 prog
->TransformFeedback
.VaryingNames
[i
],
175 prog
->TransformFeedback
.BufferStride
[i
]);
178 /* SSO has an effect on the linked program so include this when generating
181 ralloc_asprintf_append(&buf
, "sso: %s\n",
182 prog
->SeparateShader
? "T" : "F");
184 /* A shader might end up producing different output depending on the glsl
185 * version supported by the compiler. For example a different path might be
186 * taken by the preprocessor, so add the version to the hash input.
188 ralloc_asprintf_append(&buf
, "api: %d glsl: %d fglsl: %d\n",
189 ctx
->API
, ctx
->Const
.GLSLVersion
,
190 ctx
->Const
.ForceGLSLVersion
);
192 /* We run the preprocessor on shaders after hashing them, so we need to
193 * add any extension override vars to the hash. If we don't do this the
194 * preprocessor could result in different output and we could load the
197 char *ext_override
= getenv("MESA_EXTENSION_OVERRIDE");
199 ralloc_asprintf_append(&buf
, "ext:%s", ext_override
);
202 /* DRI config options may also change the output from the compiler so
203 * include them as an input to sha1 creation.
206 _mesa_sha1_format(sha1buf
, ctx
->Const
.dri_config_options_sha1
);
207 ralloc_strcat(&buf
, sha1buf
);
209 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
210 struct gl_shader
*sh
= prog
->Shaders
[i
];
211 _mesa_sha1_format(sha1buf
, sh
->sha1
);
212 ralloc_asprintf_append(&buf
, "%s: %s\n",
213 _mesa_shader_stage_to_abbrev(sh
->Stage
), sha1buf
);
215 disk_cache_compute_key(cache
, buf
, strlen(buf
), prog
->data
->sha1
);
219 uint8_t *buffer
= (uint8_t *) disk_cache_get(cache
, prog
->data
->sha1
,
221 if (buffer
== NULL
) {
222 /* Cached program not found. We may have seen the individual shaders
223 * before and skipped compiling but they may not have been used together
224 * in this combination before. Fall back to linking shaders but first
225 * re-compile the shaders.
227 * We could probably only compile the shaders which were skipped here
228 * but we need to be careful because the source may also have been
229 * changed since the last compile so for now we just recompile
232 compile_shaders(ctx
, prog
);
236 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
237 _mesa_sha1_format(sha1buf
, prog
->data
->sha1
);
238 fprintf(stderr
, "loading shader program meta data from cache: %s\n",
242 struct blob_reader metadata
;
243 blob_reader_init(&metadata
, buffer
, size
);
245 bool deserialized
= deserialize_glsl_program(&metadata
, ctx
, prog
);
247 if (!deserialized
|| metadata
.current
!= metadata
.end
|| metadata
.overrun
) {
248 /* Something has gone wrong discard the item from the cache and rebuild
251 assert(!"Invalid GLSL shader disk cache item!");
253 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
254 fprintf(stderr
, "Error reading program from cache (invalid GLSL "
258 disk_cache_remove(cache
, prog
->data
->sha1
);
259 compile_shaders(ctx
, prog
);
264 /* This is used to flag a shader retrieved from cache */
265 prog
->data
->LinkStatus
= LINKING_SKIPPED
;
267 /* Since the program load was successful, CompileStatus of all shaders at
268 * this point should normally be compile_skipped. However because of how
269 * the eviction works, it may happen that some of the individual shader keys
270 * have been evicted, resulting in unnecessary recompiles on this load, so
271 * mark them again to skip such recompiles next time.
274 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
275 if (prog
->Shaders
[i
]->CompileStatus
== COMPILED_NO_OPTS
) {
276 disk_cache_put_key(cache
, prog
->Shaders
[i
]->sha1
);
277 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
278 _mesa_sha1_format(sha1_buf
, prog
->Shaders
[i
]->sha1
);
279 fprintf(stderr
, "re-marking shader: %s\n", sha1_buf
);