e43ed7aa675e726964282f42a5ebed2026696265
[mesa.git] / src / compiler / glsl / shader_cache.cpp
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file shader_cache.cpp
26 *
27 * GLSL shader cache implementation
28 *
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.
33 *
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.
37 *
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.
45 */
46
47 #include "compiler/shader_info.h"
48 #include "glsl_symbol_table.h"
49 #include "glsl_parser_extras.h"
50 #include "ir.h"
51 #include "ir_optimization.h"
52 #include "ir_rvalue_visitor.h"
53 #include "ir_uniform.h"
54 #include "linker.h"
55 #include "link_varyings.h"
56 #include "main/core.h"
57 #include "nir.h"
58 #include "program.h"
59 #include "serialize.h"
60 #include "shader_cache.h"
61 #include "util/mesa-sha1.h"
62 #include "string_to_uint_map.h"
63
64 extern "C" {
65 #include "main/enums.h"
66 #include "main/shaderobj.h"
67 #include "program/program.h"
68 }
69
70 static void
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);
74 }
75 }
76
77 static void
78 create_binding_str(const char *key, unsigned value, void *closure)
79 {
80 char **bindings_str = (char **) closure;
81 ralloc_asprintf_append(bindings_str, "%s:%u,", key, value);
82 }
83
84 void
85 shader_cache_write_program_metadata(struct gl_context *ctx,
86 struct gl_shader_program *prog)
87 {
88 struct disk_cache *cache = ctx->Cache;
89 if (!cache)
90 return;
91
92 /* Exit early when we are dealing with a ff shader with no source file to
93 * generate a source from.
94 *
95 * TODO: In future we should use another method to generate a key for ff
96 * programs.
97 */
98 static const char zero[sizeof(prog->data->sha1)] = {0};
99 if (memcmp(prog->data->sha1, zero, sizeof(prog->data->sha1)) == 0)
100 return;
101
102 struct blob metadata;
103 blob_init(&metadata);
104
105 serialize_glsl_program(&metadata, ctx, prog);
106
107 struct cache_item_metadata cache_item_metadata;
108 cache_item_metadata.type = CACHE_ITEM_TYPE_GLSL;
109 cache_item_metadata.keys =
110 (cache_key *) malloc(prog->NumShaders * sizeof(cache_key));
111 cache_item_metadata.num_keys = prog->NumShaders;
112
113 if (!cache_item_metadata.keys)
114 goto fail;
115
116 char sha1_buf[41];
117 for (unsigned i = 0; i < prog->NumShaders; i++) {
118 disk_cache_put_key(cache, prog->Shaders[i]->sha1);
119 memcpy(cache_item_metadata.keys[i], prog->Shaders[i]->sha1,
120 sizeof(cache_key));
121 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
122 _mesa_sha1_format(sha1_buf, prog->Shaders[i]->sha1);
123 fprintf(stderr, "marking shader: %s\n", sha1_buf);
124 }
125 }
126
127 disk_cache_put(cache, prog->data->sha1, metadata.data, metadata.size,
128 &cache_item_metadata);
129
130 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
131 _mesa_sha1_format(sha1_buf, prog->data->sha1);
132 fprintf(stderr, "putting program metadata in cache: %s\n", sha1_buf);
133 }
134
135 fail:
136 free(cache_item_metadata.keys);
137 blob_finish(&metadata);
138 }
139
140 bool
141 shader_cache_read_program_metadata(struct gl_context *ctx,
142 struct gl_shader_program *prog)
143 {
144 /* Fixed function programs generated by Mesa are not cached. So don't
145 * try to read metadata for them from the cache.
146 */
147 if (prog->Name == 0)
148 return false;
149
150 struct disk_cache *cache = ctx->Cache;
151 if (!cache)
152 return false;
153
154 /* Include bindings when creating sha1. These bindings change the resulting
155 * binary so they are just as important as the shader source.
156 */
157 char *buf = ralloc_strdup(NULL, "vb: ");
158 prog->AttributeBindings->iterate(create_binding_str, &buf);
159 ralloc_strcat(&buf, "fb: ");
160 prog->FragDataBindings->iterate(create_binding_str, &buf);
161 ralloc_strcat(&buf, "fbi: ");
162 prog->FragDataIndexBindings->iterate(create_binding_str, &buf);
163 ralloc_asprintf_append(&buf, "tf: %d ", prog->TransformFeedback.BufferMode);
164 for (unsigned int i = 0; i < prog->TransformFeedback.NumVarying; i++) {
165 ralloc_asprintf_append(&buf, "%s:%d ",
166 prog->TransformFeedback.VaryingNames[i],
167 prog->TransformFeedback.BufferStride[i]);
168 }
169
170 /* SSO has an effect on the linked program so include this when generating
171 * the sha also.
172 */
173 ralloc_asprintf_append(&buf, "sso: %s\n",
174 prog->SeparateShader ? "T" : "F");
175
176 /* A shader might end up producing different output depending on the glsl
177 * version supported by the compiler. For example a different path might be
178 * taken by the preprocessor, so add the version to the hash input.
179 */
180 ralloc_asprintf_append(&buf, "api: %d glsl: %d fglsl: %d\n",
181 ctx->API, ctx->Const.GLSLVersion,
182 ctx->Const.ForceGLSLVersion);
183
184 /* We run the preprocessor on shaders after hashing them, so we need to
185 * add any extension override vars to the hash. If we don't do this the
186 * preprocessor could result in different output and we could load the
187 * wrong shader.
188 */
189 char *ext_override = getenv("MESA_EXTENSION_OVERRIDE");
190 if (ext_override) {
191 ralloc_asprintf_append(&buf, "ext:%s", ext_override);
192 }
193
194 /* DRI config options may also change the output from the compiler so
195 * include them as an input to sha1 creation.
196 */
197 char sha1buf[41];
198 _mesa_sha1_format(sha1buf, ctx->Const.dri_config_options_sha1);
199 ralloc_strcat(&buf, sha1buf);
200
201 for (unsigned i = 0; i < prog->NumShaders; i++) {
202 struct gl_shader *sh = prog->Shaders[i];
203 _mesa_sha1_format(sha1buf, sh->sha1);
204 ralloc_asprintf_append(&buf, "%s: %s\n",
205 _mesa_shader_stage_to_abbrev(sh->Stage), sha1buf);
206 }
207 disk_cache_compute_key(cache, buf, strlen(buf), prog->data->sha1);
208 ralloc_free(buf);
209
210 size_t size;
211 uint8_t *buffer = (uint8_t *) disk_cache_get(cache, prog->data->sha1,
212 &size);
213 if (buffer == NULL) {
214 /* Cached program not found. We may have seen the individual shaders
215 * before and skipped compiling but they may not have been used together
216 * in this combination before. Fall back to linking shaders but first
217 * re-compile the shaders.
218 *
219 * We could probably only compile the shaders which were skipped here
220 * but we need to be careful because the source may also have been
221 * changed since the last compile so for now we just recompile
222 * everything.
223 */
224 compile_shaders(ctx, prog);
225 return false;
226 }
227
228 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
229 _mesa_sha1_format(sha1buf, prog->data->sha1);
230 fprintf(stderr, "loading shader program meta data from cache: %s\n",
231 sha1buf);
232 }
233
234 struct blob_reader metadata;
235 blob_reader_init(&metadata, buffer, size);
236
237 bool deserialized = deserialize_glsl_program(&metadata, ctx, prog);
238
239 if (!deserialized || metadata.current != metadata.end || metadata.overrun) {
240 /* Something has gone wrong discard the item from the cache and rebuild
241 * from source.
242 */
243 assert(!"Invalid GLSL shader disk cache item!");
244
245 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
246 fprintf(stderr, "Error reading program from cache (invalid GLSL "
247 "cache item)\n");
248 }
249
250 disk_cache_remove(cache, prog->data->sha1);
251 compile_shaders(ctx, prog);
252 free(buffer);
253 return false;
254 }
255
256 /* This is used to flag a shader retrieved from cache */
257 prog->data->LinkStatus = LINKING_SKIPPED;
258
259 /* Since the program load was successful, CompileStatus of all shaders at
260 * this point should normally be compile_skipped. However because of how
261 * the eviction works, it may happen that some of the individual shader keys
262 * have been evicted, resulting in unnecessary recompiles on this load, so
263 * mark them again to skip such recompiles next time.
264 */
265 char sha1_buf[41];
266 for (unsigned i = 0; i < prog->NumShaders; i++) {
267 if (prog->Shaders[i]->CompileStatus == COMPILED_NO_OPTS) {
268 disk_cache_put_key(cache, prog->Shaders[i]->sha1);
269 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
270 _mesa_sha1_format(sha1_buf, prog->Shaders[i]->sha1);
271 fprintf(stderr, "re-marking shader: %s\n", sha1_buf);
272 }
273 }
274 }
275
276 free (buffer);
277
278 return true;
279 }