st/mesa: simplify code due to unification to st_common_program
[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
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);
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 struct st_context *st = st_context(ctx);
229 uint8_t *buffer = NULL;
230 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
231 if (prog->_LinkedShaders[i] == NULL)
232 continue;
233
234 unsigned char *sha1 = stage_sha1[i];
235 size_t size;
236 buffer = (uint8_t *) disk_cache_get(ctx->Cache, sha1, &size);
237 if (buffer) {
238 struct blob_reader blob_reader;
239 blob_reader_init(&blob_reader, buffer, size);
240
241 struct gl_program *glprog = prog->_LinkedShaders[i]->Program;
242 switch (glprog->info.stage) {
243 case MESA_SHADER_VERTEX: {
244 struct st_vertex_program *stvp =
245 (struct st_vertex_program *) glprog;
246
247 st_release_vp_variants(st, stvp);
248
249 stvp->num_inputs = blob_read_uint32(&blob_reader);
250 blob_copy_bytes(&blob_reader, (uint8_t *) stvp->index_to_input,
251 sizeof(stvp->index_to_input));
252 blob_copy_bytes(&blob_reader, (uint8_t *) stvp->result_to_output,
253 sizeof(stvp->result_to_output));
254
255 read_stream_out_from_cache(&blob_reader, &stvp->tgsi);
256 read_tgsi_from_cache(&blob_reader, &stvp->tgsi.tokens);
257
258 if (st->vp == stvp)
259 st->dirty |= ST_NEW_VERTEX_PROGRAM(st, stvp);
260
261 break;
262 }
263 case MESA_SHADER_TESS_CTRL: {
264 struct st_common_program *sttcp = st_common_program(glprog);
265
266 st_release_basic_variants(st, sttcp->Base.Target,
267 &sttcp->variants, &sttcp->tgsi);
268
269 read_stream_out_from_cache(&blob_reader, &sttcp->tgsi);
270 read_tgsi_from_cache(&blob_reader, &sttcp->tgsi.tokens);
271
272 if (st->tcp == sttcp)
273 st->dirty |= sttcp->affected_states;
274
275 break;
276 }
277 case MESA_SHADER_TESS_EVAL: {
278 struct st_common_program *sttep = st_common_program(glprog);
279
280 st_release_basic_variants(st, sttep->Base.Target,
281 &sttep->variants, &sttep->tgsi);
282
283 read_stream_out_from_cache(&blob_reader, &sttep->tgsi);
284 read_tgsi_from_cache(&blob_reader, &sttep->tgsi.tokens);
285
286 if (st->tep == sttep)
287 st->dirty |= sttep->affected_states;
288
289 break;
290 }
291 case MESA_SHADER_GEOMETRY: {
292 struct st_common_program *stgp = st_common_program(glprog);
293
294 st_release_basic_variants(st, stgp->Base.Target, &stgp->variants,
295 &stgp->tgsi);
296
297 read_stream_out_from_cache(&blob_reader, &stgp->tgsi);
298 read_tgsi_from_cache(&blob_reader, &stgp->tgsi.tokens);
299
300 if (st->gp == stgp)
301 st->dirty |= stgp->affected_states;
302
303 break;
304 }
305 case MESA_SHADER_FRAGMENT: {
306 struct st_fragment_program *stfp =
307 (struct st_fragment_program *) glprog;
308
309 st_release_fp_variants(st, stfp);
310
311 read_tgsi_from_cache(&blob_reader, &stfp->tgsi.tokens);
312
313 if (st->fp == stfp)
314 st->dirty |= stfp->affected_states;
315
316 break;
317 }
318 case MESA_SHADER_COMPUTE: {
319 struct st_compute_program *stcp =
320 (struct st_compute_program *) glprog;
321
322 st_release_cp_variants(st, stcp);
323
324 read_tgsi_from_cache(&blob_reader,
325 (const struct tgsi_token**) &stcp->tgsi.prog);
326
327 stcp->tgsi.req_local_mem = stcp->Base.info.cs.shared_size;
328 stcp->tgsi.req_private_mem = 0;
329 stcp->tgsi.req_input_mem = 0;
330
331 if (st->cp == stcp)
332 st->dirty |= stcp->affected_states;
333
334 break;
335 }
336 default:
337 unreachable("Unsupported stage");
338 }
339
340 if (blob_reader.current != blob_reader.end || blob_reader.overrun) {
341 /* Something very bad has gone wrong discard the item from the
342 * cache and rebuild/link from source.
343 */
344 assert(!"Invalid TGSI shader disk cache item!");
345
346 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
347 fprintf(stderr, "Error reading program from cache (invalid "
348 "TGSI cache item)\n");
349 }
350
351 disk_cache_remove(ctx->Cache, sha1);
352
353 goto fallback_recompile;
354 }
355
356 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
357 _mesa_sha1_format(sha1_buf, sha1);
358 fprintf(stderr, "%s tgsi_tokens retrieved from cache: %s\n",
359 _mesa_shader_stage_to_string(i), sha1_buf);
360 }
361
362 st_set_prog_affected_state_flags(glprog);
363 _mesa_associate_uniform_storage(ctx, prog, glprog->Parameters,
364 false);
365
366 free(buffer);
367 } else {
368 /* Failed to find a matching cached shader so fallback to recompile.
369 */
370 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
371 fprintf(stderr, "TGSI cache item not found falling back to "
372 "compile.\n");
373 }
374
375 goto fallback_recompile;
376 }
377 }
378
379 return true;
380
381 fallback_recompile:
382 free(buffer);
383
384 for (unsigned i = 0; i < prog->NumShaders; i++) {
385 _mesa_glsl_compile_shader(ctx, prog->Shaders[i], false, false, true);
386 }
387
388 prog->data->cache_fallback = true;
389 _mesa_glsl_link_shader(ctx, prog);
390
391 return true;
392 }