c03a190cee1203fead7a5b95013357b8e2fa208f
[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 "shader_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 blob_write_bytes(metadata, prog->data->UniformStorage[i].opaque,
237 sizeof(prog->data->UniformStorage[i].opaque));
238 }
239 }
240
241 static void
242 read_uniforms(struct blob_reader *metadata, struct gl_shader_program *prog)
243 {
244 struct gl_uniform_storage *uniforms;
245 union gl_constant_value *data;
246
247 prog->SamplersValidated = blob_read_uint32(metadata);
248 prog->data->NumUniformStorage = blob_read_uint32(metadata);
249 prog->data->NumUniformDataSlots = blob_read_uint32(metadata);
250
251 uniforms = rzalloc_array(prog, struct gl_uniform_storage,
252 prog->data->NumUniformStorage);
253 prog->data->UniformStorage = uniforms;
254
255 data = rzalloc_array(uniforms, union gl_constant_value,
256 prog->data->NumUniformDataSlots);
257 prog->data->UniformDataSlots = data;
258
259 prog->UniformHash = new string_to_uint_map;
260
261 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
262 uniforms[i].type = decode_type_from_blob(metadata);
263 uniforms[i].array_elements = blob_read_uint32(metadata);
264 uniforms[i].name = ralloc_strdup(prog, blob_read_string (metadata));
265 uniforms[i].storage = data + blob_read_uint32(metadata);
266 uniforms[i].remap_location = blob_read_uint32(metadata);
267 uniforms[i].block_index = blob_read_uint32(metadata);
268 uniforms[i].atomic_buffer_index = blob_read_uint32(metadata);
269 uniforms[i].offset = blob_read_uint32(metadata);
270 uniforms[i].array_stride = blob_read_uint32(metadata);
271 uniforms[i].matrix_stride = blob_read_uint32(metadata);
272 uniforms[i].row_major = blob_read_uint32(metadata);
273 uniforms[i].num_compatible_subroutines = blob_read_uint32(metadata);
274 uniforms[i].top_level_array_size = blob_read_uint32(metadata);
275 uniforms[i].top_level_array_stride = blob_read_uint32(metadata);
276 prog->UniformHash->put(i, uniforms[i].name);
277
278 memcpy(uniforms[i].opaque,
279 blob_read_bytes(metadata, sizeof(uniforms[i].opaque)),
280 sizeof(uniforms[i].opaque));
281 }
282 }
283
284 enum uniform_remap_type
285 {
286 remap_type_inactive_explicit_location,
287 remap_type_null_ptr,
288 remap_type_uniform_offset
289 };
290
291 static void
292 write_uniform_remap_table_entry(struct blob *metadata,
293 gl_uniform_storage *uniform_storage,
294 gl_uniform_storage *entry)
295 {
296 if (entry == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
297 blob_write_uint32(metadata, remap_type_inactive_explicit_location);
298 } else if (entry == NULL) {
299 blob_write_uint32(metadata, remap_type_null_ptr);
300 } else {
301 blob_write_uint32(metadata, remap_type_uniform_offset);
302
303 uint32_t offset = entry - uniform_storage;
304 blob_write_uint32(metadata, offset);
305 }
306 }
307
308 static void
309 write_uniform_remap_table(struct blob *metadata,
310 struct gl_shader_program *prog)
311 {
312 blob_write_uint32(metadata, prog->NumUniformRemapTable);
313
314 for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
315 write_uniform_remap_table_entry(metadata, prog->data->UniformStorage,
316 prog->UniformRemapTable[i]);
317 }
318 }
319
320 static void
321 read_uniform_remap_table_entry(struct blob_reader *metadata,
322 gl_uniform_storage *uniform_storage,
323 gl_uniform_storage **entry,
324 enum uniform_remap_type type)
325 {
326 if (type == remap_type_inactive_explicit_location) {
327 *entry = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
328 } else if (type == remap_type_null_ptr) {
329 *entry = NULL;
330 } else {
331 uint32_t uni_offset = blob_read_uint32(metadata);
332 *entry = uniform_storage + uni_offset;
333 }
334 }
335
336 static void
337 read_uniform_remap_table(struct blob_reader *metadata,
338 struct gl_shader_program *prog)
339 {
340 prog->NumUniformRemapTable = blob_read_uint32(metadata);
341
342 prog->UniformRemapTable = rzalloc_array(prog, struct gl_uniform_storage *,
343 prog->NumUniformRemapTable);
344
345 for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
346 enum uniform_remap_type type =
347 (enum uniform_remap_type) blob_read_uint32(metadata);
348
349 read_uniform_remap_table_entry(metadata, prog->data->UniformStorage,
350 &prog->UniformRemapTable[i], type);
351 }
352 }
353
354 struct whte_closure
355 {
356 struct blob *blob;
357 size_t num_entries;
358 };
359
360 static void
361 write_hash_table_entry(const char *key, unsigned value, void *closure)
362 {
363 struct whte_closure *whte = (struct whte_closure *) closure;
364
365 blob_write_string(whte->blob, key);
366 blob_write_uint32(whte->blob, value);
367
368 whte->num_entries++;
369 }
370
371 static void
372 write_hash_table(struct blob *metadata, struct string_to_uint_map *hash)
373 {
374 size_t offset;
375 struct whte_closure whte;
376
377 whte.blob = metadata;
378 whte.num_entries = 0;
379
380 offset = metadata->size;
381
382 /* Write a placeholder for the hashtable size. */
383 blob_write_uint32 (metadata, 0);
384
385 hash->iterate(write_hash_table_entry, &whte);
386
387 /* Overwrite with the computed number of entries written. */
388 blob_overwrite_uint32 (metadata, offset, whte.num_entries);
389 }
390
391 static void
392 read_hash_table(struct blob_reader *metadata, struct string_to_uint_map *hash)
393 {
394 size_t i, num_entries;
395 const char *key;
396 uint32_t value;
397
398 num_entries = blob_read_uint32 (metadata);
399
400 for (i = 0; i < num_entries; i++) {
401 key = blob_read_string(metadata);
402 value = blob_read_uint32(metadata);
403
404 hash->put(value, key);
405 }
406 }
407
408 static void
409 write_hash_tables(struct blob *metadata, struct gl_shader_program *prog)
410 {
411 write_hash_table(metadata, prog->AttributeBindings);
412 write_hash_table(metadata, prog->FragDataBindings);
413 write_hash_table(metadata, prog->FragDataIndexBindings);
414 }
415
416 static void
417 read_hash_tables(struct blob_reader *metadata, struct gl_shader_program *prog)
418 {
419 read_hash_table(metadata, prog->AttributeBindings);
420 read_hash_table(metadata, prog->FragDataBindings);
421 read_hash_table(metadata, prog->FragDataIndexBindings);
422 }
423
424 static void
425 write_program_resource_data(struct blob *metadata,
426 struct gl_shader_program *prog,
427 struct gl_program_resource *res)
428 {
429 switch(res->Type) {
430 case GL_PROGRAM_INPUT:
431 case GL_PROGRAM_OUTPUT: {
432 const gl_shader_variable *var = (gl_shader_variable *)res->Data;
433 blob_write_bytes(metadata, var, sizeof(gl_shader_variable));
434 encode_type_to_blob(metadata, var->type);
435
436 if (var->interface_type)
437 encode_type_to_blob(metadata, var->interface_type);
438
439 if (var->outermost_struct_type)
440 encode_type_to_blob(metadata, var->outermost_struct_type);
441
442 blob_write_string(metadata, var->name);
443 break;
444 }
445 case GL_BUFFER_VARIABLE:
446 case GL_VERTEX_SUBROUTINE_UNIFORM:
447 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
448 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
449 case GL_COMPUTE_SUBROUTINE_UNIFORM:
450 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
451 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
452 case GL_UNIFORM:
453 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
454 if (strcmp(((gl_uniform_storage *)res->Data)->name,
455 prog->data->UniformStorage[i].name) == 0) {
456 blob_write_uint32(metadata, i);
457 break;
458 }
459 }
460 break;
461 default:
462 assert(!"Support for writing resource not yet implemented.");
463 }
464 }
465
466 static void
467 read_program_resource_data(struct blob_reader *metadata,
468 struct gl_shader_program *prog,
469 struct gl_program_resource *res)
470 {
471 switch(res->Type) {
472 case GL_PROGRAM_INPUT:
473 case GL_PROGRAM_OUTPUT: {
474 gl_shader_variable *var = ralloc(prog, struct gl_shader_variable);
475
476 blob_copy_bytes(metadata, (uint8_t *) var, sizeof(gl_shader_variable));
477 var->type = decode_type_from_blob(metadata);
478
479 if (var->interface_type)
480 var->interface_type = decode_type_from_blob(metadata);
481
482 if (var->outermost_struct_type)
483 var->outermost_struct_type = decode_type_from_blob(metadata);
484
485 var->name = ralloc_strdup(prog, blob_read_string(metadata));
486
487 res->Data = var;
488 break;
489 }
490 case GL_BUFFER_VARIABLE:
491 case GL_VERTEX_SUBROUTINE_UNIFORM:
492 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
493 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
494 case GL_COMPUTE_SUBROUTINE_UNIFORM:
495 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
496 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
497 case GL_UNIFORM:
498 res->Data = &prog->data->UniformStorage[blob_read_uint32(metadata)];
499 break;
500 default:
501 assert(!"Support for reading resource not yet implemented.");
502 }
503 }
504
505 static void
506 write_program_resource_list(struct blob *metadata,
507 struct gl_shader_program *prog)
508 {
509 blob_write_uint32(metadata, prog->data->NumProgramResourceList);
510
511 for (unsigned i = 0; i < prog->data->NumProgramResourceList; i++) {
512 blob_write_uint32(metadata, prog->data->ProgramResourceList[i].Type);
513 write_program_resource_data(metadata, prog,
514 &prog->data->ProgramResourceList[i]);
515 blob_write_bytes(metadata,
516 &prog->data->ProgramResourceList[i].StageReferences,
517 sizeof(prog->data->ProgramResourceList[i].StageReferences));
518 }
519 }
520
521 static void
522 read_program_resource_list(struct blob_reader *metadata,
523 struct gl_shader_program *prog)
524 {
525 prog->data->NumProgramResourceList = blob_read_uint32(metadata);
526
527 prog->data->ProgramResourceList =
528 ralloc_array(prog, gl_program_resource,
529 prog->data->NumProgramResourceList);
530
531 for (unsigned i = 0; i < prog->data->NumProgramResourceList; i++) {
532 prog->data->ProgramResourceList[i].Type = blob_read_uint32(metadata);
533 read_program_resource_data(metadata, prog,
534 &prog->data->ProgramResourceList[i]);
535 blob_copy_bytes(metadata,
536 (uint8_t *) &prog->data->ProgramResourceList[i].StageReferences,
537 sizeof(prog->data->ProgramResourceList[i].StageReferences));
538 }
539 }
540
541 static void
542 write_shader_parameters(struct blob *metadata,
543 struct gl_program_parameter_list *params)
544 {
545 blob_write_uint32(metadata, params->NumParameters);
546 uint32_t i = 0;
547
548 while (i < params->NumParameters) {
549 struct gl_program_parameter *param = &params->Parameters[i];
550
551 blob_write_uint32(metadata, param->Type);
552 blob_write_string(metadata, param->Name);
553 blob_write_uint32(metadata, param->Size);
554 blob_write_uint32(metadata, param->DataType);
555 blob_write_bytes(metadata, param->StateIndexes,
556 sizeof(param->StateIndexes));
557
558 i += (param->Size + 3) / 4;
559 }
560
561 blob_write_bytes(metadata, params->ParameterValues,
562 sizeof(gl_constant_value) * 4 * params->NumParameters);
563
564 blob_write_uint32(metadata, params->StateFlags);
565 }
566
567 static void
568 read_shader_parameters(struct blob_reader *metadata,
569 struct gl_program_parameter_list *params)
570 {
571 gl_state_index state_indexes[STATE_LENGTH];
572 uint32_t i = 0;
573 uint32_t num_parameters = blob_read_uint32(metadata);
574
575 while (i < num_parameters) {
576 gl_register_file type = (gl_register_file) blob_read_uint32(metadata);
577 const char *name = blob_read_string(metadata);
578 unsigned size = blob_read_uint32(metadata);
579 unsigned data_type = blob_read_uint32(metadata);
580 blob_copy_bytes(metadata, (uint8_t *) state_indexes,
581 sizeof(state_indexes));
582
583 _mesa_add_parameter(params, type, name, size, data_type,
584 NULL, state_indexes);
585
586 i += (size + 3) / 4;
587 }
588
589 blob_copy_bytes(metadata, (uint8_t *) params->ParameterValues,
590 sizeof(gl_constant_value) * 4 * params->NumParameters);
591
592 params->StateFlags = blob_read_uint32(metadata);
593 }
594
595 static void
596 write_shader_metadata(struct blob *metadata, gl_linked_shader *shader)
597 {
598 assert(shader->Program);
599 struct gl_program *glprog = shader->Program;
600
601 blob_write_bytes(metadata, glprog->TexturesUsed,
602 sizeof(glprog->TexturesUsed));
603 blob_write_uint64(metadata, glprog->SamplersUsed);
604
605 blob_write_bytes(metadata, glprog->SamplerUnits,
606 sizeof(glprog->SamplerUnits));
607 blob_write_bytes(metadata, glprog->sh.SamplerTargets,
608 sizeof(glprog->sh.SamplerTargets));
609 blob_write_uint32(metadata, glprog->ShadowSamplers);
610
611 write_shader_parameters(metadata, glprog->Parameters);
612 }
613
614 static void
615 read_shader_metadata(struct blob_reader *metadata,
616 struct gl_program *glprog,
617 gl_linked_shader *linked)
618 {
619 blob_copy_bytes(metadata, (uint8_t *) glprog->TexturesUsed,
620 sizeof(glprog->TexturesUsed));
621 glprog->SamplersUsed = blob_read_uint64(metadata);
622
623 blob_copy_bytes(metadata, (uint8_t *) glprog->SamplerUnits,
624 sizeof(glprog->SamplerUnits));
625 blob_copy_bytes(metadata, (uint8_t *) glprog->sh.SamplerTargets,
626 sizeof(glprog->sh.SamplerTargets));
627 glprog->ShadowSamplers = blob_read_uint32(metadata);
628
629 glprog->Parameters = _mesa_new_parameter_list();
630 read_shader_parameters(metadata, glprog->Parameters);
631 }
632
633 static void
634 create_binding_str(const char *key, unsigned value, void *closure)
635 {
636 char **bindings_str = (char **) closure;
637 ralloc_asprintf_append(bindings_str, "%s:%u,", key, value);
638 }
639
640 static void
641 create_linked_shader_and_program(struct gl_context *ctx,
642 gl_shader_stage stage,
643 struct gl_shader_program *prog,
644 struct blob_reader *metadata)
645 {
646 struct gl_program *glprog;
647
648 struct gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
649 linked->Stage = stage;
650
651 glprog = ctx->Driver.NewProgram(ctx, _mesa_shader_stage_to_program(stage),
652 prog->Name, false);
653 glprog->info.stage = stage;
654 linked->Program = glprog;
655
656 read_shader_metadata(metadata, glprog, linked);
657
658 /* Restore shader info */
659 blob_copy_bytes(metadata, (uint8_t *) &glprog->info, sizeof(shader_info));
660 if (glprog->info.name)
661 glprog->info.name = ralloc_strdup(glprog, blob_read_string(metadata));
662 if (glprog->info.label)
663 glprog->info.label = ralloc_strdup(glprog, blob_read_string(metadata));
664
665 _mesa_reference_shader_program_data(ctx, &glprog->sh.data, prog->data);
666 _mesa_reference_program(ctx, &linked->Program, glprog);
667 prog->_LinkedShaders[stage] = linked;
668 }
669
670 void
671 shader_cache_write_program_metadata(struct gl_context *ctx,
672 struct gl_shader_program *prog)
673 {
674 struct disk_cache *cache = ctx->Cache;
675 if (!cache)
676 return;
677
678 /* Exit early when we are dealing with a ff shader with no source file to
679 * generate a source from.
680 *
681 * TODO: In future we should use another method to generate a key for ff
682 * programs.
683 */
684 if (*prog->data->sha1 == 0)
685 return;
686
687 struct blob *metadata = blob_create(NULL);
688
689 write_uniforms(metadata, prog);
690
691 write_hash_tables(metadata, prog);
692
693 blob_write_uint32(metadata, prog->data->Version);
694 blob_write_uint32(metadata, prog->data->linked_stages);
695
696 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
697 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
698 if (sh) {
699 write_shader_metadata(metadata, sh);
700
701 /* Store nir shader info */
702 blob_write_bytes(metadata, &sh->Program->info, sizeof(shader_info));
703
704 if (sh->Program->info.name)
705 blob_write_string(metadata, sh->Program->info.name);
706
707 if (sh->Program->info.label)
708 blob_write_string(metadata, sh->Program->info.label);
709 }
710 }
711
712 write_uniform_remap_table(metadata, prog);
713
714 write_program_resource_list(metadata, prog);
715
716 char sha1_buf[41];
717 for (unsigned i = 0; i < prog->NumShaders; i++) {
718 disk_cache_put_key(cache, prog->Shaders[i]->sha1);
719 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
720 fprintf(stderr, "marking shader: %s\n",
721 _mesa_sha1_format(sha1_buf, prog->Shaders[i]->sha1));
722 }
723 }
724
725 disk_cache_put(cache, prog->data->sha1, metadata->data, metadata->size);
726
727 ralloc_free(metadata);
728
729 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
730 fprintf(stderr, "putting program metadata in cache: %s\n",
731 _mesa_sha1_format(sha1_buf, prog->data->sha1));
732 }
733 }
734
735 bool
736 shader_cache_read_program_metadata(struct gl_context *ctx,
737 struct gl_shader_program *prog)
738 {
739 /* Fixed function programs generated by Mesa are not cached. So don't
740 * try to read metadata for them from the cache.
741 */
742 if (prog->Name == 0)
743 return false;
744
745 struct disk_cache *cache = ctx->Cache;
746 if (!cache)
747 return false;
748
749 /* Include bindings when creating sha1. These bindings change the resulting
750 * binary so they are just as important as the shader source.
751 */
752 char *buf = ralloc_strdup(NULL, "vb: ");
753 prog->AttributeBindings->iterate(create_binding_str, &buf);
754 ralloc_strcat(&buf, "fb: ");
755 prog->FragDataBindings->iterate(create_binding_str, &buf);
756 ralloc_strcat(&buf, "fbi: ");
757 prog->FragDataIndexBindings->iterate(create_binding_str, &buf);
758
759 /* SSO has an effect on the linked program so include this when generating
760 * the sha also.
761 */
762 ralloc_asprintf_append(&buf, "sso: %s\n",
763 prog->SeparateShader ? "T" : "F");
764
765 char sha1buf[41];
766 for (unsigned i = 0; i < prog->NumShaders; i++) {
767 struct gl_shader *sh = prog->Shaders[i];
768 ralloc_asprintf_append(&buf, "%s: %s\n",
769 _mesa_shader_stage_to_abbrev(sh->Stage),
770 _mesa_sha1_format(sha1buf, sh->sha1));
771 }
772 _mesa_sha1_compute(buf, strlen(buf), prog->data->sha1);
773 ralloc_free(buf);
774
775 size_t size;
776 uint8_t *buffer = (uint8_t *) disk_cache_get(cache, prog->data->sha1,
777 &size);
778 if (buffer == NULL) {
779 /* Cached program not found. We may have seen the individual shaders
780 * before and skipped compiling but they may not have been used together
781 * in this combination before. Fall back to linking shaders but first
782 * re-compile the shaders.
783 *
784 * We could probably only compile the shaders which were skipped here
785 * but we need to be careful because the source may also have been
786 * changed since the last compile so for now we just recompile
787 * everything.
788 */
789 compile_shaders(ctx, prog);
790 return false;
791 }
792
793 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
794 fprintf(stderr, "loading shader program meta data from cache: %s\n",
795 _mesa_sha1_format(sha1buf, prog->data->sha1));
796 }
797
798 struct blob_reader metadata;
799 blob_reader_init(&metadata, buffer, size);
800
801 assert(prog->data->UniformStorage == NULL);
802
803 read_uniforms(&metadata, prog);
804
805 read_hash_tables(&metadata, prog);
806
807 prog->data->Version = blob_read_uint32(&metadata);
808 prog->data->linked_stages = blob_read_uint32(&metadata);
809
810 unsigned mask = prog->data->linked_stages;
811 while (mask) {
812 const int j = u_bit_scan(&mask);
813 create_linked_shader_and_program(ctx, (gl_shader_stage) j, prog,
814 &metadata);
815 }
816
817 read_uniform_remap_table(&metadata, prog);
818
819 read_program_resource_list(&metadata, prog);
820
821 if (metadata.current != metadata.end || metadata.overrun) {
822 /* Something has gone wrong discard the item from the cache and rebuild
823 * from source.
824 */
825 assert(!"Invalid GLSL shader disk cache item!");
826
827 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
828 fprintf(stderr, "Error reading program from cache (invalid GLSL "
829 "cache item)\n");
830 }
831
832 disk_cache_remove(cache, prog->data->sha1);
833 compile_shaders(ctx, prog);
834 free(buffer);
835 return false;
836 }
837
838 /* This is used to flag a shader retrieved from cache */
839 prog->data->LinkStatus = linking_skipped;
840
841 free (buffer);
842
843 return true;
844 }