st/mesa: deduplicate cases in st_deserialise_ir_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 #include "st_debug.h"
26 #include "st_program.h"
27 #include "st_shader_cache.h"
28 #include "st_util.h"
29 #include "compiler/glsl/program.h"
30 #include "compiler/nir/nir.h"
31 #include "compiler/nir/nir_serialize.h"
32 #include "pipe/p_shader_tokens.h"
33 #include "program/ir_to_mesa.h"
34 #include "util/u_memory.h"
35
36 void
37 st_get_program_binary_driver_sha1(struct gl_context *ctx, uint8_t *sha1)
38 {
39 disk_cache_compute_key(ctx->Cache, NULL, 0, sha1);
40 }
41
42 static void
43 write_stream_out_to_cache(struct blob *blob,
44 struct pipe_shader_state *tgsi)
45 {
46 blob_write_bytes(blob, &tgsi->stream_output,
47 sizeof(tgsi->stream_output));
48 }
49
50 static void
51 copy_blob_to_driver_cache_blob(struct blob *blob, struct gl_program *prog)
52 {
53 prog->driver_cache_blob = ralloc_size(NULL, blob->size);
54 memcpy(prog->driver_cache_blob, blob->data, blob->size);
55 prog->driver_cache_blob_size = blob->size;
56 }
57
58 static void
59 write_tgsi_to_cache(struct blob *blob, const struct tgsi_token *tokens,
60 struct gl_program *prog, unsigned num_tokens)
61 {
62 blob_write_uint32(blob, num_tokens);
63 blob_write_bytes(blob, tokens, num_tokens * sizeof(struct tgsi_token));
64 copy_blob_to_driver_cache_blob(blob, prog);
65 }
66
67 static void
68 write_nir_to_cache(struct blob *blob, struct gl_program *prog)
69 {
70 nir_serialize(blob, prog->nir, false);
71 copy_blob_to_driver_cache_blob(blob, prog);
72 }
73
74 static void
75 st_serialise_ir_program(struct gl_context *ctx, struct gl_program *prog,
76 bool nir)
77 {
78 if (prog->driver_cache_blob)
79 return;
80
81 struct blob blob;
82 blob_init(&blob);
83
84 switch (prog->info.stage) {
85 case MESA_SHADER_VERTEX: {
86 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
87
88 blob_write_uint32(&blob, stvp->num_inputs);
89 blob_write_bytes(&blob, stvp->index_to_input,
90 sizeof(stvp->index_to_input));
91 blob_write_bytes(&blob, stvp->input_to_index,
92 sizeof(stvp->input_to_index));
93 blob_write_bytes(&blob, stvp->result_to_output,
94 sizeof(stvp->result_to_output));
95
96 write_stream_out_to_cache(&blob, &stvp->tgsi);
97
98 if (nir)
99 write_nir_to_cache(&blob, prog);
100 else
101 write_tgsi_to_cache(&blob, stvp->tgsi.tokens, prog,
102 stvp->num_tgsi_tokens);
103 break;
104 }
105 case MESA_SHADER_TESS_CTRL:
106 case MESA_SHADER_TESS_EVAL:
107 case MESA_SHADER_GEOMETRY:
108 case MESA_SHADER_COMPUTE: {
109 struct st_common_program *stcp = (struct st_common_program *) prog;
110
111 if (prog->info.stage == MESA_SHADER_TESS_EVAL ||
112 prog->info.stage == MESA_SHADER_GEOMETRY)
113 write_stream_out_to_cache(&blob, &stcp->tgsi);
114
115 if (nir)
116 write_nir_to_cache(&blob, prog);
117 else
118 write_tgsi_to_cache(&blob, stcp->tgsi.tokens, prog,
119 stcp->num_tgsi_tokens);
120 break;
121 }
122 case MESA_SHADER_FRAGMENT: {
123 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
124
125 if (nir)
126 write_nir_to_cache(&blob, prog);
127 else
128 write_tgsi_to_cache(&blob, stfp->tgsi.tokens, prog,
129 stfp->num_tgsi_tokens);
130 break;
131 }
132 default:
133 unreachable("Unsupported stage");
134 }
135
136 blob_finish(&blob);
137 }
138
139 /**
140 * Store tgsi and any other required state in on-disk shader cache.
141 */
142 void
143 st_store_ir_in_disk_cache(struct st_context *st, struct gl_program *prog,
144 bool nir)
145 {
146 if (!st->ctx->Cache)
147 return;
148
149 /* Exit early when we are dealing with a ff shader with no source file to
150 * generate a source from.
151 */
152 static const char zero[sizeof(prog->sh.data->sha1)] = {0};
153 if (memcmp(prog->sh.data->sha1, zero, sizeof(prog->sh.data->sha1)) == 0)
154 return;
155
156 st_serialise_ir_program(st->ctx, prog, nir);
157
158 if (st->ctx->_Shader->Flags & GLSL_CACHE_INFO) {
159 fprintf(stderr, "putting %s state tracker IR in cache\n",
160 _mesa_shader_stage_to_string(prog->info.stage));
161 }
162 }
163
164 static void
165 read_stream_out_from_cache(struct blob_reader *blob_reader,
166 struct pipe_shader_state *tgsi)
167 {
168 blob_copy_bytes(blob_reader, (uint8_t *) &tgsi->stream_output,
169 sizeof(tgsi->stream_output));
170 }
171
172 static void
173 read_tgsi_from_cache(struct blob_reader *blob_reader,
174 const struct tgsi_token **tokens,
175 unsigned *num_tokens)
176 {
177 *num_tokens = blob_read_uint32(blob_reader);
178 unsigned tokens_size = *num_tokens * sizeof(struct tgsi_token);
179 *tokens = (const struct tgsi_token*) MALLOC(tokens_size);
180 blob_copy_bytes(blob_reader, (uint8_t *) *tokens, tokens_size);
181 }
182
183 static void
184 st_deserialise_ir_program(struct gl_context *ctx,
185 struct gl_shader_program *shProg,
186 struct gl_program *prog, bool nir)
187 {
188 struct st_context *st = st_context(ctx);
189 size_t size = prog->driver_cache_blob_size;
190 uint8_t *buffer = (uint8_t *) prog->driver_cache_blob;
191 const struct nir_shader_compiler_options *options =
192 ctx->Const.ShaderCompilerOptions[prog->info.stage].NirOptions;
193
194 assert(prog->driver_cache_blob && prog->driver_cache_blob_size > 0);
195
196 struct blob_reader blob_reader;
197 blob_reader_init(&blob_reader, buffer, size);
198
199 switch (prog->info.stage) {
200 case MESA_SHADER_VERTEX: {
201 struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
202
203 st_release_vp_variants(st, stvp);
204
205 stvp->num_inputs = blob_read_uint32(&blob_reader);
206 blob_copy_bytes(&blob_reader, (uint8_t *) stvp->index_to_input,
207 sizeof(stvp->index_to_input));
208 blob_copy_bytes(&blob_reader, (uint8_t *) stvp->input_to_index,
209 sizeof(stvp->input_to_index));
210 blob_copy_bytes(&blob_reader, (uint8_t *) stvp->result_to_output,
211 sizeof(stvp->result_to_output));
212
213 read_stream_out_from_cache(&blob_reader, &stvp->tgsi);
214
215 if (nir) {
216 stvp->tgsi.type = PIPE_SHADER_IR_NIR;
217 stvp->shader_program = shProg;
218 stvp->tgsi.ir.nir = nir_deserialize(NULL, options, &blob_reader);
219 prog->nir = stvp->tgsi.ir.nir;
220 } else {
221 read_tgsi_from_cache(&blob_reader, &stvp->tgsi.tokens,
222 &stvp->num_tgsi_tokens);
223 }
224
225 if (st->vp == stvp)
226 st->dirty |= ST_NEW_VERTEX_PROGRAM(st, stvp);
227
228 break;
229 }
230 case MESA_SHADER_TESS_CTRL:
231 case MESA_SHADER_TESS_EVAL:
232 case MESA_SHADER_GEOMETRY:
233 case MESA_SHADER_COMPUTE: {
234 struct st_common_program *stcp = st_common_program(prog);
235
236 st_release_basic_variants(st, stcp);
237
238 if (prog->info.stage == MESA_SHADER_TESS_EVAL ||
239 prog->info.stage == MESA_SHADER_GEOMETRY)
240 read_stream_out_from_cache(&blob_reader, &stcp->tgsi);
241
242 if (nir) {
243 stcp->tgsi.type = PIPE_SHADER_IR_NIR;
244 stcp->tgsi.ir.nir = nir_deserialize(NULL, options, &blob_reader);
245 stcp->shader_program = shProg;
246 prog->nir = stcp->tgsi.ir.nir;
247 } else {
248 read_tgsi_from_cache(&blob_reader, &stcp->tgsi.tokens,
249 &stcp->num_tgsi_tokens);
250 }
251
252 if ((prog->info.stage == MESA_SHADER_TESS_CTRL && st->tcp == stcp) ||
253 (prog->info.stage == MESA_SHADER_TESS_EVAL && st->tep == stcp) ||
254 (prog->info.stage == MESA_SHADER_GEOMETRY && st->gp == stcp) ||
255 (prog->info.stage == MESA_SHADER_COMPUTE && st->cp == stcp))
256 st->dirty |= stcp->affected_states;
257 break;
258 }
259 case MESA_SHADER_FRAGMENT: {
260 struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
261
262 st_release_fp_variants(st, stfp);
263
264 if (nir) {
265 stfp->tgsi.type = PIPE_SHADER_IR_NIR;
266 stfp->shader_program = shProg;
267 stfp->tgsi.ir.nir = nir_deserialize(NULL, options, &blob_reader);
268 prog->nir = stfp->tgsi.ir.nir;
269 } else {
270 read_tgsi_from_cache(&blob_reader, &stfp->tgsi.tokens,
271 &stfp->num_tgsi_tokens);
272 }
273
274 if (st->fp == stfp)
275 st->dirty |= stfp->affected_states;
276
277 break;
278 }
279 default:
280 unreachable("Unsupported stage");
281 }
282
283 /* Make sure we don't try to read more data than we wrote. This should
284 * never happen in release builds but its useful to have this check to
285 * catch development bugs.
286 */
287 if (blob_reader.current != blob_reader.end || blob_reader.overrun) {
288 assert(!"Invalid TGSI shader disk cache item!");
289
290 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
291 fprintf(stderr, "Error reading program from cache (invalid "
292 "TGSI cache item)\n");
293 }
294 }
295
296 st_set_prog_affected_state_flags(prog);
297 _mesa_associate_uniform_storage(ctx, shProg, prog);
298
299 /* Create Gallium shaders now instead of on demand. */
300 if (ST_DEBUG & DEBUG_PRECOMPILE ||
301 st->shader_has_one_variant[prog->info.stage])
302 st_precompile_shader_variant(st, prog);
303 }
304
305 bool
306 st_load_ir_from_disk_cache(struct gl_context *ctx,
307 struct gl_shader_program *prog,
308 bool nir)
309 {
310 if (!ctx->Cache)
311 return false;
312
313 /* If we didn't load the GLSL metadata from cache then we could not have
314 * loaded the tgsi either.
315 */
316 if (prog->data->LinkStatus != LINKING_SKIPPED)
317 return false;
318
319 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
320 if (prog->_LinkedShaders[i] == NULL)
321 continue;
322
323 struct gl_program *glprog = prog->_LinkedShaders[i]->Program;
324 st_deserialise_ir_program(ctx, prog, glprog, nir);
325
326 /* We don't need the cached blob anymore so free it */
327 ralloc_free(glprog->driver_cache_blob);
328 glprog->driver_cache_blob = NULL;
329 glprog->driver_cache_blob_size = 0;
330
331 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
332 fprintf(stderr, "%s state tracker IR retrieved from cache\n",
333 _mesa_shader_stage_to_string(i));
334 }
335 }
336
337 return true;
338 }
339
340 void
341 st_serialise_tgsi_program(struct gl_context *ctx, struct gl_program *prog)
342 {
343 st_serialise_ir_program(ctx, prog, false);
344 }
345
346 void
347 st_serialise_tgsi_program_binary(struct gl_context *ctx,
348 struct gl_shader_program *shProg,
349 struct gl_program *prog)
350 {
351 st_serialise_ir_program(ctx, prog, false);
352 }
353
354 void
355 st_deserialise_tgsi_program(struct gl_context *ctx,
356 struct gl_shader_program *shProg,
357 struct gl_program *prog)
358 {
359 st_deserialise_ir_program(ctx, shProg, prog, false);
360 }
361
362 void
363 st_serialise_nir_program(struct gl_context *ctx, struct gl_program *prog)
364 {
365 st_serialise_ir_program(ctx, prog, true);
366 }
367
368 void
369 st_serialise_nir_program_binary(struct gl_context *ctx,
370 struct gl_shader_program *shProg,
371 struct gl_program *prog)
372 {
373 st_serialise_ir_program(ctx, prog, true);
374 }
375
376 void
377 st_deserialise_nir_program(struct gl_context *ctx,
378 struct gl_shader_program *shProg,
379 struct gl_program *prog)
380 {
381 st_deserialise_ir_program(ctx, shProg, prog, true);
382 }