glsl: pass shader source keys to the disk cache
[mesa.git] / src / mesa / state_tracker / st_shader_cache.c
1 /*
2 * Copyright © 2017 Timothy Arceri
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 #include <stdio.h>
25 #include "st_debug.h"
26 #include "st_program.h"
27 #include "st_shader_cache.h"
28 #include "compiler/glsl/program.h"
29 #include "pipe/p_shader_tokens.h"
30 #include "program/ir_to_mesa.h"
31 #include "util/u_memory.h"
32
33 static void
34 write_stream_out_to_cache(struct blob *blob,
35 struct pipe_shader_state *tgsi)
36 {
37 blob_write_bytes(blob, &tgsi->stream_output,
38 sizeof(tgsi->stream_output));
39 }
40
41 static void
42 write_tgsi_to_cache(struct blob *blob, struct pipe_shader_state *tgsi,
43 struct st_context *st, unsigned char *sha1,
44 unsigned num_tokens)
45 {
46 blob_write_uint32(blob, num_tokens);
47 blob_write_bytes(blob, tgsi->tokens,
48 num_tokens * sizeof(struct tgsi_token));
49
50 disk_cache_put(st->ctx->Cache, sha1, blob->data, blob->size, NULL);
51 }
52
53 /**
54 * Store tgsi and any other required state in on-disk shader cache.
55 */
56 void
57 st_store_tgsi_in_disk_cache(struct st_context *st, struct gl_program *prog,
58 struct pipe_shader_state *out_state,
59 unsigned num_tokens)
60 {
61 if (!st->ctx->Cache)
62 return;
63
64 /* Exit early when we are dealing with a ff shader with no source file to
65 * generate a source from.
66 */
67 static const char zero[sizeof(prog->sh.data->sha1)] = {0};
68 if (memcmp(prog->sh.data->sha1, zero, sizeof(prog->sh.data->sha1)) == 0)
69 return;
70
71 unsigned char *sha1;
72 struct blob *blob = blob_create();
73
74 switch (prog->info.stage) {
75 case MESA_SHADER_VERTEX: {
76 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
77 sha1 = stvp->sha1;
78
79 blob_write_uint32(blob, stvp->num_inputs);
80 blob_write_bytes(blob, stvp->index_to_input,
81 sizeof(stvp->index_to_input));
82 blob_write_bytes(blob, stvp->result_to_output,
83 sizeof(stvp->result_to_output));
84
85 write_stream_out_to_cache(blob, &stvp->tgsi);
86 write_tgsi_to_cache(blob, &stvp->tgsi, st, sha1, num_tokens);
87 break;
88 }
89 case MESA_SHADER_TESS_CTRL:
90 case MESA_SHADER_TESS_EVAL:
91 case MESA_SHADER_GEOMETRY: {
92 struct st_common_program *p = st_common_program(prog);
93 sha1 = p->sha1;
94
95 write_stream_out_to_cache(blob, out_state);
96 write_tgsi_to_cache(blob, out_state, st, sha1, num_tokens);
97 break;
98 }
99 case MESA_SHADER_FRAGMENT: {
100 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
101 sha1 = stfp->sha1;
102
103 write_tgsi_to_cache(blob, &stfp->tgsi, st, sha1, num_tokens);
104 break;
105 }
106 case MESA_SHADER_COMPUTE: {
107 struct st_compute_program *stcp = (struct st_compute_program *) prog;
108 sha1 = stcp->sha1;
109
110 write_tgsi_to_cache(blob, out_state, st, sha1, num_tokens);
111 break;
112 }
113 default:
114 unreachable("Unsupported stage");
115 }
116
117 if (st->ctx->_Shader->Flags & GLSL_CACHE_INFO) {
118 char sha1_buf[41];
119 _mesa_sha1_format(sha1_buf, sha1);
120 fprintf(stderr, "putting %s tgsi_tokens in cache: %s\n",
121 _mesa_shader_stage_to_string(prog->info.stage), sha1_buf);
122 }
123
124 blob_destroy(blob);
125 }
126
127 static void
128 read_stream_out_from_cache(struct blob_reader *blob_reader,
129 struct pipe_shader_state *tgsi)
130 {
131 blob_copy_bytes(blob_reader, (uint8_t *) &tgsi->stream_output,
132 sizeof(tgsi->stream_output));
133 }
134
135 static void
136 read_tgsi_from_cache(struct blob_reader *blob_reader,
137 const struct tgsi_token **tokens)
138 {
139 uint32_t num_tokens = blob_read_uint32(blob_reader);
140 unsigned tokens_size = num_tokens * sizeof(struct tgsi_token);
141 *tokens = (const struct tgsi_token*) MALLOC(tokens_size);
142 blob_copy_bytes(blob_reader, (uint8_t *) *tokens, tokens_size);
143 }
144
145 bool
146 st_load_tgsi_from_disk_cache(struct gl_context *ctx,
147 struct gl_shader_program *prog)
148 {
149 if (!ctx->Cache)
150 return false;
151
152 unsigned char *stage_sha1[MESA_SHADER_STAGES];
153 char sha1_buf[41];
154
155 /* Compute and store sha1 for each stage. These will be reused by the
156 * cache store pass if we fail to find the cached tgsi.
157 */
158 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
159 if (prog->_LinkedShaders[i] == NULL)
160 continue;
161
162 char *buf = ralloc_strdup(NULL, "tgsi_tokens ");
163 _mesa_sha1_format(sha1_buf,
164 prog->_LinkedShaders[i]->Program->sh.data->sha1);
165 ralloc_strcat(&buf, sha1_buf);
166
167 struct gl_program *glprog = prog->_LinkedShaders[i]->Program;
168 switch (glprog->info.stage) {
169 case MESA_SHADER_VERTEX: {
170 struct st_vertex_program *stvp = (struct st_vertex_program *) glprog;
171 stage_sha1[i] = stvp->sha1;
172 ralloc_strcat(&buf, " vs");
173 disk_cache_compute_key(ctx->Cache, buf, strlen(buf), stage_sha1[i]);
174 break;
175 }
176 case MESA_SHADER_TESS_CTRL: {
177 struct st_common_program *stcp = st_common_program(glprog);
178 stage_sha1[i] = stcp->sha1;
179 ralloc_strcat(&buf, " tcs");
180 disk_cache_compute_key(ctx->Cache, buf, strlen(buf), stage_sha1[i]);
181 break;
182 }
183 case MESA_SHADER_TESS_EVAL: {
184 struct st_common_program *step = st_common_program(glprog);
185 stage_sha1[i] = step->sha1;
186 ralloc_strcat(&buf, " tes");
187 disk_cache_compute_key(ctx->Cache, buf, strlen(buf), stage_sha1[i]);
188 break;
189 }
190 case MESA_SHADER_GEOMETRY: {
191 struct st_common_program *stgp = st_common_program(glprog);
192 stage_sha1[i] = stgp->sha1;
193 ralloc_strcat(&buf, " gs");
194 disk_cache_compute_key(ctx->Cache, buf, strlen(buf), stage_sha1[i]);
195 break;
196 }
197 case MESA_SHADER_FRAGMENT: {
198 struct st_fragment_program *stfp =
199 (struct st_fragment_program *) glprog;
200 stage_sha1[i] = stfp->sha1;
201 ralloc_strcat(&buf, " fs");
202 disk_cache_compute_key(ctx->Cache, buf, strlen(buf), stage_sha1[i]);
203 break;
204 }
205 case MESA_SHADER_COMPUTE: {
206 struct st_compute_program *stcp =
207 (struct st_compute_program *) glprog;
208 stage_sha1[i] = stcp->sha1;
209 ralloc_strcat(&buf, " cs");
210 disk_cache_compute_key(ctx->Cache, buf, strlen(buf), stage_sha1[i]);
211 break;
212 }
213 default:
214 unreachable("Unsupported stage");
215 }
216
217 ralloc_free(buf);
218 }
219
220 /* Now that we have created the sha1 keys that will be used for writting to
221 * the tgsi cache fallback to the regular glsl to tgsi path if we didn't
222 * load the GLSL IR from cache. We do this as glsl to tgsi can alter things
223 * such as gl_program_parameter_list which holds things like uniforms.
224 */
225 if (prog->data->LinkStatus != linking_skipped)
226 return false;
227
228 uint8_t *buffer = NULL;
229 if (ctx->_Shader->Flags & GLSL_CACHE_FALLBACK) {
230 goto fallback_recompile;
231 }
232
233 struct st_context *st = st_context(ctx);
234 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
235 if (prog->_LinkedShaders[i] == NULL)
236 continue;
237
238 unsigned char *sha1 = stage_sha1[i];
239 size_t size;
240 buffer = (uint8_t *) disk_cache_get(ctx->Cache, sha1, &size);
241 if (buffer) {
242 struct blob_reader blob_reader;
243 blob_reader_init(&blob_reader, buffer, size);
244
245 struct gl_program *glprog = prog->_LinkedShaders[i]->Program;
246 switch (glprog->info.stage) {
247 case MESA_SHADER_VERTEX: {
248 struct st_vertex_program *stvp =
249 (struct st_vertex_program *) glprog;
250
251 st_release_vp_variants(st, stvp);
252
253 stvp->num_inputs = blob_read_uint32(&blob_reader);
254 blob_copy_bytes(&blob_reader, (uint8_t *) stvp->index_to_input,
255 sizeof(stvp->index_to_input));
256 blob_copy_bytes(&blob_reader, (uint8_t *) stvp->result_to_output,
257 sizeof(stvp->result_to_output));
258
259 read_stream_out_from_cache(&blob_reader, &stvp->tgsi);
260 read_tgsi_from_cache(&blob_reader, &stvp->tgsi.tokens);
261
262 if (st->vp == stvp)
263 st->dirty |= ST_NEW_VERTEX_PROGRAM(st, stvp);
264
265 break;
266 }
267 case MESA_SHADER_TESS_CTRL: {
268 struct st_common_program *sttcp = st_common_program(glprog);
269
270 st_release_basic_variants(st, sttcp->Base.Target,
271 &sttcp->variants, &sttcp->tgsi);
272
273 read_stream_out_from_cache(&blob_reader, &sttcp->tgsi);
274 read_tgsi_from_cache(&blob_reader, &sttcp->tgsi.tokens);
275
276 if (st->tcp == sttcp)
277 st->dirty |= sttcp->affected_states;
278
279 break;
280 }
281 case MESA_SHADER_TESS_EVAL: {
282 struct st_common_program *sttep = st_common_program(glprog);
283
284 st_release_basic_variants(st, sttep->Base.Target,
285 &sttep->variants, &sttep->tgsi);
286
287 read_stream_out_from_cache(&blob_reader, &sttep->tgsi);
288 read_tgsi_from_cache(&blob_reader, &sttep->tgsi.tokens);
289
290 if (st->tep == sttep)
291 st->dirty |= sttep->affected_states;
292
293 break;
294 }
295 case MESA_SHADER_GEOMETRY: {
296 struct st_common_program *stgp = st_common_program(glprog);
297
298 st_release_basic_variants(st, stgp->Base.Target, &stgp->variants,
299 &stgp->tgsi);
300
301 read_stream_out_from_cache(&blob_reader, &stgp->tgsi);
302 read_tgsi_from_cache(&blob_reader, &stgp->tgsi.tokens);
303
304 if (st->gp == stgp)
305 st->dirty |= stgp->affected_states;
306
307 break;
308 }
309 case MESA_SHADER_FRAGMENT: {
310 struct st_fragment_program *stfp =
311 (struct st_fragment_program *) glprog;
312
313 st_release_fp_variants(st, stfp);
314
315 read_tgsi_from_cache(&blob_reader, &stfp->tgsi.tokens);
316
317 if (st->fp == stfp)
318 st->dirty |= stfp->affected_states;
319
320 break;
321 }
322 case MESA_SHADER_COMPUTE: {
323 struct st_compute_program *stcp =
324 (struct st_compute_program *) glprog;
325
326 st_release_cp_variants(st, stcp);
327
328 read_tgsi_from_cache(&blob_reader,
329 (const struct tgsi_token**) &stcp->tgsi.prog);
330
331 stcp->tgsi.req_local_mem = stcp->Base.info.cs.shared_size;
332 stcp->tgsi.req_private_mem = 0;
333 stcp->tgsi.req_input_mem = 0;
334
335 if (st->cp == stcp)
336 st->dirty |= stcp->affected_states;
337
338 break;
339 }
340 default:
341 unreachable("Unsupported stage");
342 }
343
344 if (blob_reader.current != blob_reader.end || blob_reader.overrun) {
345 /* Something very bad has gone wrong discard the item from the
346 * cache and rebuild/link from source.
347 */
348 assert(!"Invalid TGSI shader disk cache item!");
349
350 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
351 fprintf(stderr, "Error reading program from cache (invalid "
352 "TGSI cache item)\n");
353 }
354
355 disk_cache_remove(ctx->Cache, sha1);
356
357 goto fallback_recompile;
358 }
359
360 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
361 _mesa_sha1_format(sha1_buf, sha1);
362 fprintf(stderr, "%s tgsi_tokens retrieved from cache: %s\n",
363 _mesa_shader_stage_to_string(i), sha1_buf);
364 }
365
366 st_set_prog_affected_state_flags(glprog);
367 _mesa_associate_uniform_storage(ctx, prog, glprog, false);
368
369 /* Create Gallium shaders now instead of on demand. */
370 if (ST_DEBUG & DEBUG_PRECOMPILE ||
371 st->shader_has_one_variant[glprog->info.stage])
372 st_precompile_shader_variant(st, glprog);
373
374 free(buffer);
375 } else {
376 /* Failed to find a matching cached shader so fallback to recompile.
377 */
378 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
379 fprintf(stderr, "TGSI cache item not found.\n");
380 }
381
382 goto fallback_recompile;
383 }
384 }
385
386 return true;
387
388 fallback_recompile:
389 free(buffer);
390
391 if (ctx->_Shader->Flags & GLSL_CACHE_INFO)
392 fprintf(stderr, "TGSI cache falling back to recompile.\n");
393
394 for (unsigned i = 0; i < prog->NumShaders; i++) {
395 _mesa_glsl_compile_shader(ctx, prog->Shaders[i], false, false, true);
396 }
397
398 prog->data->skip_cache = true;
399 _mesa_glsl_link_shader(ctx, prog);
400
401 return true;
402 }