glsl: add initial implementation of shader cache
[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 "blob.h"
48 #include "compiler/shader_info.h"
49 #include "glsl_symbol_table.h"
50 #include "glsl_parser_extras.h"
51 #include "ir.h"
52 #include "ir_optimization.h"
53 #include "ir_rvalue_visitor.h"
54 #include "ir_uniform.h"
55 #include "linker.h"
56 #include "link_varyings.h"
57 #include "main/core.h"
58 #include "nir.h"
59 #include "program.h"
60 #include "util/disk_cache.h"
61 #include "util/mesa-sha1.h"
62 #include "util/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 encode_type_to_blob(struct blob *blob, const glsl_type *type)
79 {
80 uint32_t encoding;
81
82 switch (type->base_type) {
83 case GLSL_TYPE_UINT:
84 case GLSL_TYPE_INT:
85 case GLSL_TYPE_FLOAT:
86 case GLSL_TYPE_BOOL:
87 case GLSL_TYPE_DOUBLE:
88 case GLSL_TYPE_UINT64:
89 case GLSL_TYPE_INT64:
90 encoding = (type->base_type << 24) |
91 (type->vector_elements << 4) |
92 (type->matrix_columns);
93 break;
94 case GLSL_TYPE_SAMPLER:
95 encoding = (type->base_type) << 24 |
96 (type->sampler_dimensionality << 4) |
97 (type->sampler_shadow << 3) |
98 (type->sampler_array << 2) |
99 (type->sampled_type);
100 break;
101 case GLSL_TYPE_SUBROUTINE:
102 encoding = type->base_type << 24;
103 blob_write_uint32(blob, encoding);
104 blob_write_string(blob, type->name);
105 return;
106 case GLSL_TYPE_IMAGE:
107 encoding = (type->base_type) << 24 |
108 (type->sampler_dimensionality << 3) |
109 (type->sampler_array << 2) |
110 (type->sampled_type);
111 break;
112 case GLSL_TYPE_ATOMIC_UINT:
113 encoding = (type->base_type << 24);
114 break;
115 case GLSL_TYPE_ARRAY:
116 blob_write_uint32(blob, (type->base_type) << 24);
117 blob_write_uint32(blob, type->length);
118 encode_type_to_blob(blob, type->fields.array);
119 return;
120 case GLSL_TYPE_STRUCT:
121 case GLSL_TYPE_INTERFACE:
122 blob_write_uint32(blob, (type->base_type) << 24);
123 blob_write_string(blob, type->name);
124 blob_write_uint32(blob, type->length);
125 blob_write_bytes(blob, type->fields.structure,
126 sizeof(glsl_struct_field) * type->length);
127 for (unsigned i = 0; i < type->length; i++) {
128 encode_type_to_blob(blob, type->fields.structure[i].type);
129 blob_write_string(blob, type->fields.structure[i].name);
130 }
131
132 if (type->base_type == GLSL_TYPE_INTERFACE) {
133 blob_write_uint32(blob, type->interface_packing);
134 blob_write_uint32(blob, type->interface_row_major);
135 }
136 return;
137 case GLSL_TYPE_VOID:
138 case GLSL_TYPE_ERROR:
139 default:
140 assert(!"Cannot encode type!");
141 encoding = 0;
142 break;
143 }
144
145 blob_write_uint32(blob, encoding);
146 }
147
148 static const glsl_type *
149 decode_type_from_blob(struct blob_reader *blob)
150 {
151 uint32_t u = blob_read_uint32(blob);
152 glsl_base_type base_type = (glsl_base_type) (u >> 24);
153
154 switch (base_type) {
155 case GLSL_TYPE_UINT:
156 case GLSL_TYPE_INT:
157 case GLSL_TYPE_FLOAT:
158 case GLSL_TYPE_BOOL:
159 case GLSL_TYPE_DOUBLE:
160 case GLSL_TYPE_UINT64:
161 case GLSL_TYPE_INT64:
162 return glsl_type::get_instance(base_type, (u >> 4) & 0x0f, u & 0x0f);
163 case GLSL_TYPE_SAMPLER:
164 return glsl_type::get_sampler_instance((enum glsl_sampler_dim) ((u >> 4) & 0x07),
165 (u >> 3) & 0x01,
166 (u >> 2) & 0x01,
167 (glsl_base_type) ((u >> 0) & 0x03));
168 case GLSL_TYPE_SUBROUTINE:
169 return glsl_type::get_subroutine_instance(blob_read_string(blob));
170 case GLSL_TYPE_IMAGE:
171 return glsl_type::get_image_instance((enum glsl_sampler_dim) ((u >> 3) & 0x07),
172 (u >> 2) & 0x01,
173 (glsl_base_type) ((u >> 0) & 0x03));
174 case GLSL_TYPE_ATOMIC_UINT:
175 return glsl_type::atomic_uint_type;
176 case GLSL_TYPE_ARRAY: {
177 unsigned length = blob_read_uint32(blob);
178 return glsl_type::get_array_instance(decode_type_from_blob(blob),
179 length);
180 }
181 case GLSL_TYPE_STRUCT:
182 case GLSL_TYPE_INTERFACE: {
183 char *name = blob_read_string(blob);
184 unsigned num_fields = blob_read_uint32(blob);
185 glsl_struct_field *fields = (glsl_struct_field *)
186 blob_read_bytes(blob, sizeof(glsl_struct_field) * num_fields);
187 for (unsigned i = 0; i < num_fields; i++) {
188 fields[i].type = decode_type_from_blob(blob);
189 fields[i].name = blob_read_string(blob);
190 }
191
192 if (base_type == GLSL_TYPE_INTERFACE) {
193 enum glsl_interface_packing packing =
194 (glsl_interface_packing) blob_read_uint32(blob);
195 bool row_major = blob_read_uint32(blob);
196 return glsl_type::get_interface_instance(fields, num_fields,
197 packing, row_major, name);
198 } else {
199 return glsl_type::get_record_instance(fields, num_fields, name);
200 }
201 }
202 case GLSL_TYPE_VOID:
203 case GLSL_TYPE_ERROR:
204 default:
205 assert(!"Cannot decode type!");
206 return NULL;
207 }
208 }
209
210 static void
211 write_uniforms(struct blob *metadata, struct gl_shader_program *prog)
212 {
213 blob_write_uint32(metadata, prog->SamplersValidated);
214 blob_write_uint32(metadata, prog->data->NumUniformStorage);
215 blob_write_uint32(metadata, prog->data->NumUniformDataSlots);
216
217 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
218 encode_type_to_blob(metadata, prog->data->UniformStorage[i].type);
219 blob_write_uint32(metadata, prog->data->UniformStorage[i].array_elements);
220 blob_write_string(metadata, prog->data->UniformStorage[i].name);
221 blob_write_uint32(metadata, prog->data->UniformStorage[i].storage -
222 prog->data->UniformDataSlots);
223 blob_write_uint32(metadata, prog->data->UniformStorage[i].remap_location);
224 blob_write_uint32(metadata, prog->data->UniformStorage[i].block_index);
225 blob_write_uint32(metadata, prog->data->UniformStorage[i].atomic_buffer_index);
226 blob_write_uint32(metadata, prog->data->UniformStorage[i].offset);
227 blob_write_uint32(metadata, prog->data->UniformStorage[i].array_stride);
228 blob_write_uint32(metadata, prog->data->UniformStorage[i].matrix_stride);
229 blob_write_uint32(metadata, prog->data->UniformStorage[i].row_major);
230 blob_write_uint32(metadata,
231 prog->data->UniformStorage[i].num_compatible_subroutines);
232 blob_write_uint32(metadata,
233 prog->data->UniformStorage[i].top_level_array_size);
234 blob_write_uint32(metadata,
235 prog->data->UniformStorage[i].top_level_array_stride);
236 }
237 }
238
239 static void
240 read_uniforms(struct blob_reader *metadata, struct gl_shader_program *prog)
241 {
242 struct gl_uniform_storage *uniforms;
243 union gl_constant_value *data;
244
245 prog->SamplersValidated = blob_read_uint32(metadata);
246 prog->data->NumUniformStorage = blob_read_uint32(metadata);
247 prog->data->NumUniformDataSlots = blob_read_uint32(metadata);
248
249 uniforms = rzalloc_array(prog, struct gl_uniform_storage,
250 prog->data->NumUniformStorage);
251 prog->data->UniformStorage = uniforms;
252
253 data = rzalloc_array(uniforms, union gl_constant_value,
254 prog->data->NumUniformDataSlots);
255 prog->data->UniformDataSlots = data;
256
257 prog->UniformHash = new string_to_uint_map;
258
259 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
260 uniforms[i].type = decode_type_from_blob(metadata);
261 uniforms[i].array_elements = blob_read_uint32(metadata);
262 uniforms[i].name = ralloc_strdup(prog, blob_read_string (metadata));
263 uniforms[i].storage = data + blob_read_uint32(metadata);
264 uniforms[i].remap_location = blob_read_uint32(metadata);
265 uniforms[i].block_index = blob_read_uint32(metadata);
266 uniforms[i].atomic_buffer_index = blob_read_uint32(metadata);
267 uniforms[i].offset = blob_read_uint32(metadata);
268 uniforms[i].array_stride = blob_read_uint32(metadata);
269 uniforms[i].matrix_stride = blob_read_uint32(metadata);
270 uniforms[i].row_major = blob_read_uint32(metadata);
271 uniforms[i].num_compatible_subroutines = blob_read_uint32(metadata);
272 uniforms[i].top_level_array_size = blob_read_uint32(metadata);
273 uniforms[i].top_level_array_stride = blob_read_uint32(metadata);
274 prog->UniformHash->put(i, uniforms[i].name);
275 }
276 }
277
278
279 static void
280 write_uniform_remap_table(struct blob *metadata,
281 struct gl_shader_program *prog)
282 {
283 blob_write_uint32(metadata, prog->NumUniformRemapTable);
284
285 for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
286 blob_write_uint32(metadata, prog->UniformRemapTable[i] -
287 prog->data->UniformStorage);
288 }
289 }
290
291 static void
292 read_uniform_remap_table(struct blob_reader *metadata,
293 struct gl_shader_program *prog)
294 {
295 prog->NumUniformRemapTable = blob_read_uint32(metadata);
296
297 prog->UniformRemapTable =rzalloc_array(prog, struct gl_uniform_storage *,
298 prog->NumUniformRemapTable);
299
300 for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
301 prog->UniformRemapTable[i] =
302 prog->data->UniformStorage + blob_read_uint32(metadata);
303 }
304 }
305
306 static void
307 write_shader_parameters(struct blob *metadata,
308 struct gl_program_parameter_list *params)
309 {
310 blob_write_uint32(metadata, params->NumParameters);
311 uint32_t i = 0;
312
313 while (i < params->NumParameters) {
314 struct gl_program_parameter *param = &params->Parameters[i];
315
316 blob_write_uint32(metadata, param->Type);
317 blob_write_string(metadata, param->Name);
318 blob_write_uint32(metadata, param->Size);
319 blob_write_uint32(metadata, param->DataType);
320 blob_write_bytes(metadata, param->StateIndexes,
321 sizeof(param->StateIndexes));
322
323 i += (param->Size + 3) / 4;
324 }
325
326 blob_write_bytes(metadata, params->ParameterValues,
327 sizeof(gl_constant_value) * 4 * params->NumParameters);
328
329 blob_write_uint32(metadata, params->StateFlags);
330 }
331
332 static void
333 read_shader_parameters(struct blob_reader *metadata,
334 struct gl_program_parameter_list *params)
335 {
336 gl_state_index state_indexes[STATE_LENGTH];
337 uint32_t i = 0;
338 uint32_t num_parameters = blob_read_uint32(metadata);
339
340 while (i < num_parameters) {
341 gl_register_file type = (gl_register_file) blob_read_uint32(metadata);
342 const char *name = blob_read_string(metadata);
343 unsigned size = blob_read_uint32(metadata);
344 unsigned data_type = blob_read_uint32(metadata);
345 blob_copy_bytes(metadata, (uint8_t *) state_indexes,
346 sizeof(state_indexes));
347
348 _mesa_add_parameter(params, type, name, size, data_type,
349 NULL, state_indexes);
350
351 i += (size + 3) / 4;
352 }
353
354 blob_copy_bytes(metadata, (uint8_t *) params->ParameterValues,
355 sizeof(gl_constant_value) * 4 * params->NumParameters);
356
357 params->StateFlags = blob_read_uint32(metadata);
358 }
359
360 static void
361 write_shader_metadata(struct blob *metadata, gl_linked_shader *shader)
362 {
363 assert(shader->Program);
364 struct gl_program *glprog = shader->Program;
365
366 blob_write_bytes(metadata, glprog->TexturesUsed,
367 sizeof(glprog->TexturesUsed));
368 blob_write_uint64(metadata, glprog->SamplersUsed);
369
370 write_shader_parameters(metadata, glprog->Parameters);
371 }
372
373 static void
374 read_shader_metadata(struct blob_reader *metadata,
375 struct gl_program *glprog,
376 gl_linked_shader *linked)
377 {
378 blob_copy_bytes(metadata, (uint8_t *) glprog->TexturesUsed,
379 sizeof(glprog->TexturesUsed));
380 glprog->SamplersUsed = blob_read_uint64(metadata);
381
382 glprog->Parameters = _mesa_new_parameter_list();
383 read_shader_parameters(metadata, glprog->Parameters);
384 }
385
386 static void
387 create_binding_str(const char *key, unsigned value, void *closure)
388 {
389 char **bindings_str = (char **) closure;
390 ralloc_asprintf_append(bindings_str, "%s:%u,", key, value);
391 }
392
393 static void
394 create_linked_shader_and_program(struct gl_context *ctx,
395 gl_shader_stage stage,
396 struct gl_shader_program *prog,
397 struct blob_reader *metadata)
398 {
399 struct gl_program *glprog;
400
401 struct gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
402 linked->Stage = stage;
403
404 glprog = ctx->Driver.NewProgram(ctx, _mesa_shader_stage_to_program(stage),
405 prog->Name, false);
406 glprog->info.stage = stage;
407 linked->Program = glprog;
408
409 read_shader_metadata(metadata, glprog, linked);
410
411 /* Restore shader info */
412 blob_copy_bytes(metadata, (uint8_t *) &glprog->info, sizeof(shader_info));
413 if (glprog->info.name)
414 glprog->info.name = ralloc_strdup(glprog, blob_read_string(metadata));
415 if (glprog->info.label)
416 glprog->info.label = ralloc_strdup(glprog, blob_read_string(metadata));
417
418 _mesa_reference_shader_program_data(ctx, &glprog->sh.data, prog->data);
419 _mesa_reference_program(ctx, &linked->Program, glprog);
420 prog->_LinkedShaders[stage] = linked;
421 }
422
423 void
424 shader_cache_write_program_metadata(struct gl_context *ctx,
425 struct gl_shader_program *prog)
426 {
427 struct disk_cache *cache = ctx->Cache;
428 if (!cache)
429 return;
430
431 /* Exit early when we are dealing with a ff shader with no source file to
432 * generate a source from.
433 *
434 * TODO: In future we should use another method to generate a key for ff
435 * programs.
436 */
437 if (*prog->data->sha1 == 0)
438 return;
439
440 struct blob *metadata = blob_create(NULL);
441
442 write_uniforms(metadata, prog);
443
444 blob_write_uint32(metadata, prog->data->Version);
445 blob_write_uint32(metadata, prog->data->linked_stages);
446
447 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
448 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
449 if (sh) {
450 write_shader_metadata(metadata, sh);
451
452 /* Store nir shader info */
453 blob_write_bytes(metadata, &sh->Program->info, sizeof(shader_info));
454
455 if (sh->Program->info.name)
456 blob_write_string(metadata, sh->Program->info.name);
457
458 if (sh->Program->info.label)
459 blob_write_string(metadata, sh->Program->info.label);
460 }
461 }
462
463 write_uniform_remap_table(metadata, prog);
464
465 char sha1_buf[41];
466 for (unsigned i = 0; i < prog->NumShaders; i++) {
467 disk_cache_put_key(cache, prog->Shaders[i]->sha1);
468 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
469 fprintf(stderr, "marking shader: %s\n",
470 _mesa_sha1_format(sha1_buf, prog->Shaders[i]->sha1));
471 }
472 }
473
474 disk_cache_put(cache, prog->data->sha1, metadata->data, metadata->size);
475
476 ralloc_free(metadata);
477
478 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
479 fprintf(stderr, "putting program metadata in cache: %s\n",
480 _mesa_sha1_format(sha1_buf, prog->data->sha1));
481 }
482 }
483
484 bool
485 shader_cache_read_program_metadata(struct gl_context *ctx,
486 struct gl_shader_program *prog)
487 {
488 /* Fixed function programs generated by Mesa are not cached. So don't
489 * try to read metadata for them from the cache.
490 */
491 if (prog->Name == 0)
492 return false;
493
494 struct disk_cache *cache = ctx->Cache;
495 if (!cache)
496 return false;
497
498 /* Include bindings when creating sha1. These bindings change the resulting
499 * binary so they are just as important as the shader source.
500 */
501 char *buf = ralloc_strdup(NULL, "vb: ");
502 prog->AttributeBindings->iterate(create_binding_str, &buf);
503 ralloc_strcat(&buf, "fb: ");
504 prog->FragDataBindings->iterate(create_binding_str, &buf);
505 ralloc_strcat(&buf, "fbi: ");
506 prog->FragDataIndexBindings->iterate(create_binding_str, &buf);
507
508 /* SSO has an effect on the linked program so include this when generating
509 * the sha also.
510 */
511 ralloc_asprintf_append(&buf, "sso: %s\n",
512 prog->SeparateShader ? "T" : "F");
513
514 char sha1buf[41];
515 for (unsigned i = 0; i < prog->NumShaders; i++) {
516 struct gl_shader *sh = prog->Shaders[i];
517 ralloc_asprintf_append(&buf, "%s: %s\n",
518 _mesa_shader_stage_to_abbrev(sh->Stage),
519 _mesa_sha1_format(sha1buf, sh->sha1));
520 }
521 _mesa_sha1_compute(buf, strlen(buf), prog->data->sha1);
522 ralloc_free(buf);
523
524 size_t size;
525 uint8_t *buffer = (uint8_t *) disk_cache_get(cache, prog->data->sha1,
526 &size);
527 if (buffer == NULL) {
528 /* Cached program not found. We may have seen the individual shaders
529 * before and skipped compiling but they may not have been used together
530 * in this combination before. Fall back to linking shaders but first
531 * re-compile the shaders.
532 *
533 * We could probably only compile the shaders which were skipped here
534 * but we need to be careful because the source may also have been
535 * changed since the last compile so for now we just recompile
536 * everything.
537 */
538 compile_shaders(ctx, prog);
539 return false;
540 }
541
542 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
543 fprintf(stderr, "loading shader program meta data from cache: %s\n",
544 _mesa_sha1_format(sha1buf, prog->data->sha1));
545 }
546
547 struct blob_reader metadata;
548 blob_reader_init(&metadata, buffer, size);
549
550 assert(prog->data->UniformStorage == NULL);
551
552 read_uniforms(&metadata, prog);
553
554 prog->data->Version = blob_read_uint32(&metadata);
555 prog->data->linked_stages = blob_read_uint32(&metadata);
556
557 unsigned mask = prog->data->linked_stages;
558 while (mask) {
559 const int j = u_bit_scan(&mask);
560 create_linked_shader_and_program(ctx, (gl_shader_stage) j, prog,
561 &metadata);
562 }
563
564 read_uniform_remap_table(&metadata, prog);
565
566 if (metadata.current != metadata.end || metadata.overrun) {
567 /* Something has gone wrong discard the item from the cache and rebuild
568 * from source.
569 */
570 assert(!"Invalid GLSL shader disk cache item!");
571
572 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
573 fprintf(stderr, "Error reading program from cache (invalid GLSL "
574 "cache item)\n");
575 }
576
577 disk_cache_remove(cache, prog->data->sha1);
578 compile_shaders(ctx, prog);
579 free(buffer);
580 return false;
581 }
582
583 /* This is used to flag a shader retrieved from cache */
584 prog->data->LinkStatus = linking_skipped;
585
586 free (buffer);
587
588 return true;
589 }