glsl: store subroutine remap table in 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 "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_subroutines(struct blob *metadata, struct gl_shader_program *prog)
212 {
213 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
214 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
215 if (!sh)
216 continue;
217
218 struct gl_program *glprog = sh->Program;
219
220 blob_write_uint32(metadata, glprog->sh.NumSubroutineUniforms);
221 blob_write_uint32(metadata, glprog->sh.MaxSubroutineFunctionIndex);
222 blob_write_uint32(metadata, glprog->sh.NumSubroutineFunctions);
223 for (unsigned j = 0; j < glprog->sh.NumSubroutineFunctions; j++) {
224 int num_types = glprog->sh.SubroutineFunctions[j].num_compat_types;
225
226 blob_write_string(metadata, glprog->sh.SubroutineFunctions[j].name);
227 blob_write_uint32(metadata, glprog->sh.SubroutineFunctions[j].index);
228 blob_write_uint32(metadata, num_types);
229
230 for (int k = 0; k < num_types; k++) {
231 encode_type_to_blob(metadata,
232 glprog->sh.SubroutineFunctions[j].types[k]);
233 }
234 }
235 }
236 }
237
238 static void
239 read_subroutines(struct blob_reader *metadata, struct gl_shader_program *prog)
240 {
241 struct gl_subroutine_function *subs;
242
243 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
244 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
245 if (!sh)
246 continue;
247
248 struct gl_program *glprog = sh->Program;
249
250 glprog->sh.NumSubroutineUniforms = blob_read_uint32(metadata);
251 glprog->sh.MaxSubroutineFunctionIndex = blob_read_uint32(metadata);
252 glprog->sh.NumSubroutineFunctions = blob_read_uint32(metadata);
253
254 subs = rzalloc_array(prog, struct gl_subroutine_function,
255 glprog->sh.NumSubroutineFunctions);
256 glprog->sh.SubroutineFunctions = subs;
257
258 for (unsigned j = 0; j < glprog->sh.NumSubroutineFunctions; j++) {
259 subs[j].name = ralloc_strdup(prog, blob_read_string (metadata));
260 subs[j].index = (int) blob_read_uint32(metadata);
261 subs[j].num_compat_types = (int) blob_read_uint32(metadata);
262
263 subs[j].types = rzalloc_array(prog, const struct glsl_type *,
264 subs[j].num_compat_types);
265 for (int k = 0; k < subs[j].num_compat_types; k++) {
266 subs[j].types[k] = decode_type_from_blob(metadata);
267 }
268 }
269 }
270 }
271
272 static void
273 write_xfb(struct blob *metadata, struct gl_shader_program *shProg)
274 {
275 struct gl_program *prog = shProg->last_vert_prog;
276
277 if (!prog) {
278 blob_write_uint32(metadata, ~0u);
279 return;
280 }
281
282 struct gl_transform_feedback_info *ltf = prog->sh.LinkedTransformFeedback;
283
284 blob_write_uint32(metadata, prog->info.stage);
285
286 blob_write_uint32(metadata, ltf->NumOutputs);
287 blob_write_uint32(metadata, ltf->ActiveBuffers);
288 blob_write_uint32(metadata, ltf->NumVarying);
289
290 blob_write_bytes(metadata, ltf->Outputs,
291 sizeof(struct gl_transform_feedback_output) *
292 ltf->NumOutputs);
293
294 for (int i = 0; i < ltf->NumVarying; i++) {
295 blob_write_string(metadata, ltf->Varyings[i].Name);
296 blob_write_uint32(metadata, ltf->Varyings[i].Type);
297 blob_write_uint32(metadata, ltf->Varyings[i].BufferIndex);
298 blob_write_uint32(metadata, ltf->Varyings[i].Size);
299 blob_write_uint32(metadata, ltf->Varyings[i].Offset);
300 }
301
302 blob_write_bytes(metadata, ltf->Buffers,
303 sizeof(struct gl_transform_feedback_buffer) *
304 MAX_FEEDBACK_BUFFERS);
305 }
306
307 static void
308 read_xfb(struct blob_reader *metadata, struct gl_shader_program *shProg)
309 {
310 unsigned xfb_stage = blob_read_uint32(metadata);
311
312 if (xfb_stage == ~0u)
313 return;
314
315 struct gl_program *prog = shProg->_LinkedShaders[xfb_stage]->Program;
316 struct gl_transform_feedback_info *ltf =
317 rzalloc(prog, struct gl_transform_feedback_info);
318
319 prog->sh.LinkedTransformFeedback = ltf;
320 shProg->last_vert_prog = prog;
321
322 ltf->NumOutputs = blob_read_uint32(metadata);
323 ltf->ActiveBuffers = blob_read_uint32(metadata);
324 ltf->NumVarying = blob_read_uint32(metadata);
325
326 ltf->Outputs = rzalloc_array(prog, struct gl_transform_feedback_output,
327 ltf->NumOutputs);
328
329 blob_copy_bytes(metadata, (uint8_t *) ltf->Outputs,
330 sizeof(struct gl_transform_feedback_output) *
331 ltf->NumOutputs);
332
333 ltf->Varyings = rzalloc_array(prog,
334 struct gl_transform_feedback_varying_info,
335 ltf->NumVarying);
336
337 for (int i = 0; i < ltf->NumVarying; i++) {
338 ltf->Varyings[i].Name = ralloc_strdup(prog, blob_read_string(metadata));
339 ltf->Varyings[i].Type = blob_read_uint32(metadata);
340 ltf->Varyings[i].BufferIndex = blob_read_uint32(metadata);
341 ltf->Varyings[i].Size = blob_read_uint32(metadata);
342 ltf->Varyings[i].Offset = blob_read_uint32(metadata);
343 }
344
345 blob_copy_bytes(metadata, (uint8_t *) ltf->Buffers,
346 sizeof(struct gl_transform_feedback_buffer) *
347 MAX_FEEDBACK_BUFFERS);
348 }
349
350 static void
351 write_uniforms(struct blob *metadata, struct gl_shader_program *prog)
352 {
353 blob_write_uint32(metadata, prog->SamplersValidated);
354 blob_write_uint32(metadata, prog->data->NumUniformStorage);
355 blob_write_uint32(metadata, prog->data->NumUniformDataSlots);
356
357 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
358 encode_type_to_blob(metadata, prog->data->UniformStorage[i].type);
359 blob_write_uint32(metadata, prog->data->UniformStorage[i].array_elements);
360 blob_write_string(metadata, prog->data->UniformStorage[i].name);
361 blob_write_uint32(metadata, prog->data->UniformStorage[i].storage -
362 prog->data->UniformDataSlots);
363 blob_write_uint32(metadata, prog->data->UniformStorage[i].remap_location);
364 blob_write_uint32(metadata, prog->data->UniformStorage[i].block_index);
365 blob_write_uint32(metadata, prog->data->UniformStorage[i].atomic_buffer_index);
366 blob_write_uint32(metadata, prog->data->UniformStorage[i].offset);
367 blob_write_uint32(metadata, prog->data->UniformStorage[i].array_stride);
368 blob_write_uint32(metadata, prog->data->UniformStorage[i].matrix_stride);
369 blob_write_uint32(metadata, prog->data->UniformStorage[i].row_major);
370 blob_write_uint32(metadata,
371 prog->data->UniformStorage[i].num_compatible_subroutines);
372 blob_write_uint32(metadata,
373 prog->data->UniformStorage[i].top_level_array_size);
374 blob_write_uint32(metadata,
375 prog->data->UniformStorage[i].top_level_array_stride);
376 blob_write_bytes(metadata, prog->data->UniformStorage[i].opaque,
377 sizeof(prog->data->UniformStorage[i].opaque));
378 }
379 }
380
381 static void
382 read_uniforms(struct blob_reader *metadata, struct gl_shader_program *prog)
383 {
384 struct gl_uniform_storage *uniforms;
385 union gl_constant_value *data;
386
387 prog->SamplersValidated = blob_read_uint32(metadata);
388 prog->data->NumUniformStorage = blob_read_uint32(metadata);
389 prog->data->NumUniformDataSlots = blob_read_uint32(metadata);
390
391 uniforms = rzalloc_array(prog, struct gl_uniform_storage,
392 prog->data->NumUniformStorage);
393 prog->data->UniformStorage = uniforms;
394
395 data = rzalloc_array(uniforms, union gl_constant_value,
396 prog->data->NumUniformDataSlots);
397 prog->data->UniformDataSlots = data;
398
399 prog->UniformHash = new string_to_uint_map;
400
401 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
402 uniforms[i].type = decode_type_from_blob(metadata);
403 uniforms[i].array_elements = blob_read_uint32(metadata);
404 uniforms[i].name = ralloc_strdup(prog, blob_read_string (metadata));
405 uniforms[i].storage = data + blob_read_uint32(metadata);
406 uniforms[i].remap_location = blob_read_uint32(metadata);
407 uniforms[i].block_index = blob_read_uint32(metadata);
408 uniforms[i].atomic_buffer_index = blob_read_uint32(metadata);
409 uniforms[i].offset = blob_read_uint32(metadata);
410 uniforms[i].array_stride = blob_read_uint32(metadata);
411 uniforms[i].matrix_stride = blob_read_uint32(metadata);
412 uniforms[i].row_major = blob_read_uint32(metadata);
413 uniforms[i].num_compatible_subroutines = blob_read_uint32(metadata);
414 uniforms[i].top_level_array_size = blob_read_uint32(metadata);
415 uniforms[i].top_level_array_stride = blob_read_uint32(metadata);
416 prog->UniformHash->put(i, uniforms[i].name);
417
418 memcpy(uniforms[i].opaque,
419 blob_read_bytes(metadata, sizeof(uniforms[i].opaque)),
420 sizeof(uniforms[i].opaque));
421 }
422 }
423
424 enum uniform_remap_type
425 {
426 remap_type_inactive_explicit_location,
427 remap_type_null_ptr,
428 remap_type_uniform_offset
429 };
430
431 static void
432 write_uniform_remap_table_entry(struct blob *metadata,
433 gl_uniform_storage *uniform_storage,
434 gl_uniform_storage *entry)
435 {
436 if (entry == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
437 blob_write_uint32(metadata, remap_type_inactive_explicit_location);
438 } else if (entry == NULL) {
439 blob_write_uint32(metadata, remap_type_null_ptr);
440 } else {
441 blob_write_uint32(metadata, remap_type_uniform_offset);
442
443 uint32_t offset = entry - uniform_storage;
444 blob_write_uint32(metadata, offset);
445 }
446 }
447
448 static void
449 write_uniform_remap_tables(struct blob *metadata,
450 struct gl_shader_program *prog)
451 {
452 blob_write_uint32(metadata, prog->NumUniformRemapTable);
453
454 for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
455 write_uniform_remap_table_entry(metadata, prog->data->UniformStorage,
456 prog->UniformRemapTable[i]);
457 }
458
459 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
460 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
461 if (sh) {
462 struct gl_program *glprog = sh->Program;
463 blob_write_uint32(metadata, glprog->sh.NumSubroutineUniformRemapTable);
464
465 for (unsigned j = 0; j < glprog->sh.NumSubroutineUniformRemapTable; j++) {
466 write_uniform_remap_table_entry(metadata,
467 prog->data->UniformStorage,
468 glprog->sh.SubroutineUniformRemapTable[j]);
469 }
470 }
471 }
472 }
473
474 static void
475 read_uniform_remap_table_entry(struct blob_reader *metadata,
476 gl_uniform_storage *uniform_storage,
477 gl_uniform_storage **entry,
478 enum uniform_remap_type type)
479 {
480 if (type == remap_type_inactive_explicit_location) {
481 *entry = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
482 } else if (type == remap_type_null_ptr) {
483 *entry = NULL;
484 } else {
485 uint32_t uni_offset = blob_read_uint32(metadata);
486 *entry = uniform_storage + uni_offset;
487 }
488 }
489
490 static void
491 read_uniform_remap_tables(struct blob_reader *metadata,
492 struct gl_shader_program *prog)
493 {
494 prog->NumUniformRemapTable = blob_read_uint32(metadata);
495
496 prog->UniformRemapTable = rzalloc_array(prog, struct gl_uniform_storage *,
497 prog->NumUniformRemapTable);
498
499 for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
500 enum uniform_remap_type type =
501 (enum uniform_remap_type) blob_read_uint32(metadata);
502
503 read_uniform_remap_table_entry(metadata, prog->data->UniformStorage,
504 &prog->UniformRemapTable[i], type);
505 }
506
507 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
508 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
509 if (sh) {
510 struct gl_program *glprog = sh->Program;
511 glprog->sh.NumSubroutineUniformRemapTable = blob_read_uint32(metadata);
512
513 glprog->sh.SubroutineUniformRemapTable =
514 rzalloc_array(glprog, struct gl_uniform_storage *,
515 glprog->sh.NumSubroutineUniformRemapTable);
516
517 for (unsigned j = 0; j < glprog->sh.NumSubroutineUniformRemapTable; j++) {
518 enum uniform_remap_type type =
519 (enum uniform_remap_type) blob_read_uint32(metadata);
520
521 read_uniform_remap_table_entry(metadata,
522 prog->data->UniformStorage,
523 &glprog->sh.SubroutineUniformRemapTable[j],
524 type);
525 }
526 }
527 }
528 }
529
530 struct whte_closure
531 {
532 struct blob *blob;
533 size_t num_entries;
534 };
535
536 static void
537 write_hash_table_entry(const char *key, unsigned value, void *closure)
538 {
539 struct whte_closure *whte = (struct whte_closure *) closure;
540
541 blob_write_string(whte->blob, key);
542 blob_write_uint32(whte->blob, value);
543
544 whte->num_entries++;
545 }
546
547 static void
548 write_hash_table(struct blob *metadata, struct string_to_uint_map *hash)
549 {
550 size_t offset;
551 struct whte_closure whte;
552
553 whte.blob = metadata;
554 whte.num_entries = 0;
555
556 offset = metadata->size;
557
558 /* Write a placeholder for the hashtable size. */
559 blob_write_uint32 (metadata, 0);
560
561 hash->iterate(write_hash_table_entry, &whte);
562
563 /* Overwrite with the computed number of entries written. */
564 blob_overwrite_uint32 (metadata, offset, whte.num_entries);
565 }
566
567 static void
568 read_hash_table(struct blob_reader *metadata, struct string_to_uint_map *hash)
569 {
570 size_t i, num_entries;
571 const char *key;
572 uint32_t value;
573
574 num_entries = blob_read_uint32 (metadata);
575
576 for (i = 0; i < num_entries; i++) {
577 key = blob_read_string(metadata);
578 value = blob_read_uint32(metadata);
579
580 hash->put(value, key);
581 }
582 }
583
584 static void
585 write_hash_tables(struct blob *metadata, struct gl_shader_program *prog)
586 {
587 write_hash_table(metadata, prog->AttributeBindings);
588 write_hash_table(metadata, prog->FragDataBindings);
589 write_hash_table(metadata, prog->FragDataIndexBindings);
590 }
591
592 static void
593 read_hash_tables(struct blob_reader *metadata, struct gl_shader_program *prog)
594 {
595 read_hash_table(metadata, prog->AttributeBindings);
596 read_hash_table(metadata, prog->FragDataBindings);
597 read_hash_table(metadata, prog->FragDataIndexBindings);
598 }
599
600 static void
601 write_shader_subroutine_index(struct blob *metadata,
602 struct gl_linked_shader *sh,
603 struct gl_program_resource *res)
604 {
605 assert(sh);
606
607 for (unsigned j = 0; j < sh->Program->sh.NumSubroutineFunctions; j++) {
608 if (strcmp(((gl_subroutine_function *)res->Data)->name,
609 sh->Program->sh.SubroutineFunctions[j].name) == 0) {
610 blob_write_uint32(metadata, j);
611 break;
612 }
613 }
614 }
615
616 static void
617 write_program_resource_data(struct blob *metadata,
618 struct gl_shader_program *prog,
619 struct gl_program_resource *res)
620 {
621 struct gl_linked_shader *sh;
622
623 switch(res->Type) {
624 case GL_PROGRAM_INPUT:
625 case GL_PROGRAM_OUTPUT: {
626 const gl_shader_variable *var = (gl_shader_variable *)res->Data;
627 blob_write_bytes(metadata, var, sizeof(gl_shader_variable));
628 encode_type_to_blob(metadata, var->type);
629
630 if (var->interface_type)
631 encode_type_to_blob(metadata, var->interface_type);
632
633 if (var->outermost_struct_type)
634 encode_type_to_blob(metadata, var->outermost_struct_type);
635
636 blob_write_string(metadata, var->name);
637 break;
638 }
639 case GL_BUFFER_VARIABLE:
640 case GL_VERTEX_SUBROUTINE_UNIFORM:
641 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
642 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
643 case GL_COMPUTE_SUBROUTINE_UNIFORM:
644 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
645 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
646 case GL_UNIFORM:
647 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
648 if (strcmp(((gl_uniform_storage *)res->Data)->name,
649 prog->data->UniformStorage[i].name) == 0) {
650 blob_write_uint32(metadata, i);
651 break;
652 }
653 }
654 break;
655 case GL_TRANSFORM_FEEDBACK_BUFFER:
656 for (unsigned i = 0; i < MAX_FEEDBACK_BUFFERS; i++) {
657 if (((gl_transform_feedback_buffer *)res->Data)->Binding ==
658 prog->last_vert_prog->sh.LinkedTransformFeedback->Buffers[i].Binding) {
659 blob_write_uint32(metadata, i);
660 break;
661 }
662 }
663 break;
664 case GL_TRANSFORM_FEEDBACK_VARYING:
665 for (int i = 0; i < prog->last_vert_prog->sh.LinkedTransformFeedback->NumVarying; i++) {
666 if (strcmp(((gl_transform_feedback_varying_info *)res->Data)->Name,
667 prog->last_vert_prog->sh.LinkedTransformFeedback->Varyings[i].Name) == 0) {
668 blob_write_uint32(metadata, i);
669 break;
670 }
671 }
672 break;
673 case GL_VERTEX_SUBROUTINE:
674 case GL_TESS_CONTROL_SUBROUTINE:
675 case GL_TESS_EVALUATION_SUBROUTINE:
676 case GL_GEOMETRY_SUBROUTINE:
677 case GL_FRAGMENT_SUBROUTINE:
678 case GL_COMPUTE_SUBROUTINE:
679 sh =
680 prog->_LinkedShaders[_mesa_shader_stage_from_subroutine(res->Type)];
681 write_shader_subroutine_index(metadata, sh, res);
682 break;
683 default:
684 assert(!"Support for writing resource not yet implemented.");
685 }
686 }
687
688 static void
689 read_program_resource_data(struct blob_reader *metadata,
690 struct gl_shader_program *prog,
691 struct gl_program_resource *res)
692 {
693 struct gl_linked_shader *sh;
694
695 switch(res->Type) {
696 case GL_PROGRAM_INPUT:
697 case GL_PROGRAM_OUTPUT: {
698 gl_shader_variable *var = ralloc(prog, struct gl_shader_variable);
699
700 blob_copy_bytes(metadata, (uint8_t *) var, sizeof(gl_shader_variable));
701 var->type = decode_type_from_blob(metadata);
702
703 if (var->interface_type)
704 var->interface_type = decode_type_from_blob(metadata);
705
706 if (var->outermost_struct_type)
707 var->outermost_struct_type = decode_type_from_blob(metadata);
708
709 var->name = ralloc_strdup(prog, blob_read_string(metadata));
710
711 res->Data = var;
712 break;
713 }
714 case GL_BUFFER_VARIABLE:
715 case GL_VERTEX_SUBROUTINE_UNIFORM:
716 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
717 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
718 case GL_COMPUTE_SUBROUTINE_UNIFORM:
719 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
720 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
721 case GL_UNIFORM:
722 res->Data = &prog->data->UniformStorage[blob_read_uint32(metadata)];
723 break;
724 case GL_TRANSFORM_FEEDBACK_BUFFER:
725 res->Data = &prog->last_vert_prog->
726 sh.LinkedTransformFeedback->Buffers[blob_read_uint32(metadata)];
727 break;
728 case GL_TRANSFORM_FEEDBACK_VARYING:
729 res->Data = &prog->last_vert_prog->
730 sh.LinkedTransformFeedback->Varyings[blob_read_uint32(metadata)];
731 break;
732 case GL_VERTEX_SUBROUTINE:
733 case GL_TESS_CONTROL_SUBROUTINE:
734 case GL_TESS_EVALUATION_SUBROUTINE:
735 case GL_GEOMETRY_SUBROUTINE:
736 case GL_FRAGMENT_SUBROUTINE:
737 case GL_COMPUTE_SUBROUTINE:
738 sh =
739 prog->_LinkedShaders[_mesa_shader_stage_from_subroutine(res->Type)];
740 res->Data =
741 &sh->Program->sh.SubroutineFunctions[blob_read_uint32(metadata)];
742 break;
743 default:
744 assert(!"Support for reading resource not yet implemented.");
745 }
746 }
747
748 static void
749 write_program_resource_list(struct blob *metadata,
750 struct gl_shader_program *prog)
751 {
752 blob_write_uint32(metadata, prog->data->NumProgramResourceList);
753
754 for (unsigned i = 0; i < prog->data->NumProgramResourceList; i++) {
755 blob_write_uint32(metadata, prog->data->ProgramResourceList[i].Type);
756 write_program_resource_data(metadata, prog,
757 &prog->data->ProgramResourceList[i]);
758 blob_write_bytes(metadata,
759 &prog->data->ProgramResourceList[i].StageReferences,
760 sizeof(prog->data->ProgramResourceList[i].StageReferences));
761 }
762 }
763
764 static void
765 read_program_resource_list(struct blob_reader *metadata,
766 struct gl_shader_program *prog)
767 {
768 prog->data->NumProgramResourceList = blob_read_uint32(metadata);
769
770 prog->data->ProgramResourceList =
771 ralloc_array(prog, gl_program_resource,
772 prog->data->NumProgramResourceList);
773
774 for (unsigned i = 0; i < prog->data->NumProgramResourceList; i++) {
775 prog->data->ProgramResourceList[i].Type = blob_read_uint32(metadata);
776 read_program_resource_data(metadata, prog,
777 &prog->data->ProgramResourceList[i]);
778 blob_copy_bytes(metadata,
779 (uint8_t *) &prog->data->ProgramResourceList[i].StageReferences,
780 sizeof(prog->data->ProgramResourceList[i].StageReferences));
781 }
782 }
783
784 static void
785 write_shader_parameters(struct blob *metadata,
786 struct gl_program_parameter_list *params)
787 {
788 blob_write_uint32(metadata, params->NumParameters);
789 uint32_t i = 0;
790
791 while (i < params->NumParameters) {
792 struct gl_program_parameter *param = &params->Parameters[i];
793
794 blob_write_uint32(metadata, param->Type);
795 blob_write_string(metadata, param->Name);
796 blob_write_uint32(metadata, param->Size);
797 blob_write_uint32(metadata, param->DataType);
798 blob_write_bytes(metadata, param->StateIndexes,
799 sizeof(param->StateIndexes));
800
801 i += (param->Size + 3) / 4;
802 }
803
804 blob_write_bytes(metadata, params->ParameterValues,
805 sizeof(gl_constant_value) * 4 * params->NumParameters);
806
807 blob_write_uint32(metadata, params->StateFlags);
808 }
809
810 static void
811 read_shader_parameters(struct blob_reader *metadata,
812 struct gl_program_parameter_list *params)
813 {
814 gl_state_index state_indexes[STATE_LENGTH];
815 uint32_t i = 0;
816 uint32_t num_parameters = blob_read_uint32(metadata);
817
818 while (i < num_parameters) {
819 gl_register_file type = (gl_register_file) blob_read_uint32(metadata);
820 const char *name = blob_read_string(metadata);
821 unsigned size = blob_read_uint32(metadata);
822 unsigned data_type = blob_read_uint32(metadata);
823 blob_copy_bytes(metadata, (uint8_t *) state_indexes,
824 sizeof(state_indexes));
825
826 _mesa_add_parameter(params, type, name, size, data_type,
827 NULL, state_indexes);
828
829 i += (size + 3) / 4;
830 }
831
832 blob_copy_bytes(metadata, (uint8_t *) params->ParameterValues,
833 sizeof(gl_constant_value) * 4 * params->NumParameters);
834
835 params->StateFlags = blob_read_uint32(metadata);
836 }
837
838 static void
839 write_shader_metadata(struct blob *metadata, gl_linked_shader *shader)
840 {
841 assert(shader->Program);
842 struct gl_program *glprog = shader->Program;
843
844 blob_write_bytes(metadata, glprog->TexturesUsed,
845 sizeof(glprog->TexturesUsed));
846 blob_write_uint64(metadata, glprog->SamplersUsed);
847
848 blob_write_bytes(metadata, glprog->SamplerUnits,
849 sizeof(glprog->SamplerUnits));
850 blob_write_bytes(metadata, glprog->sh.SamplerTargets,
851 sizeof(glprog->sh.SamplerTargets));
852 blob_write_uint32(metadata, glprog->ShadowSamplers);
853
854 write_shader_parameters(metadata, glprog->Parameters);
855 }
856
857 static void
858 read_shader_metadata(struct blob_reader *metadata,
859 struct gl_program *glprog,
860 gl_linked_shader *linked)
861 {
862 blob_copy_bytes(metadata, (uint8_t *) glprog->TexturesUsed,
863 sizeof(glprog->TexturesUsed));
864 glprog->SamplersUsed = blob_read_uint64(metadata);
865
866 blob_copy_bytes(metadata, (uint8_t *) glprog->SamplerUnits,
867 sizeof(glprog->SamplerUnits));
868 blob_copy_bytes(metadata, (uint8_t *) glprog->sh.SamplerTargets,
869 sizeof(glprog->sh.SamplerTargets));
870 glprog->ShadowSamplers = blob_read_uint32(metadata);
871
872 glprog->Parameters = _mesa_new_parameter_list();
873 read_shader_parameters(metadata, glprog->Parameters);
874 }
875
876 static void
877 create_binding_str(const char *key, unsigned value, void *closure)
878 {
879 char **bindings_str = (char **) closure;
880 ralloc_asprintf_append(bindings_str, "%s:%u,", key, value);
881 }
882
883 static void
884 create_linked_shader_and_program(struct gl_context *ctx,
885 gl_shader_stage stage,
886 struct gl_shader_program *prog,
887 struct blob_reader *metadata)
888 {
889 struct gl_program *glprog;
890
891 struct gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
892 linked->Stage = stage;
893
894 glprog = ctx->Driver.NewProgram(ctx, _mesa_shader_stage_to_program(stage),
895 prog->Name, false);
896 glprog->info.stage = stage;
897 linked->Program = glprog;
898
899 read_shader_metadata(metadata, glprog, linked);
900
901 /* Restore shader info */
902 blob_copy_bytes(metadata, (uint8_t *) &glprog->info, sizeof(shader_info));
903 if (glprog->info.name)
904 glprog->info.name = ralloc_strdup(glprog, blob_read_string(metadata));
905 if (glprog->info.label)
906 glprog->info.label = ralloc_strdup(glprog, blob_read_string(metadata));
907
908 _mesa_reference_shader_program_data(ctx, &glprog->sh.data, prog->data);
909 _mesa_reference_program(ctx, &linked->Program, glprog);
910 prog->_LinkedShaders[stage] = linked;
911 }
912
913 void
914 shader_cache_write_program_metadata(struct gl_context *ctx,
915 struct gl_shader_program *prog)
916 {
917 struct disk_cache *cache = ctx->Cache;
918 if (!cache)
919 return;
920
921 /* Exit early when we are dealing with a ff shader with no source file to
922 * generate a source from.
923 *
924 * TODO: In future we should use another method to generate a key for ff
925 * programs.
926 */
927 if (*prog->data->sha1 == 0)
928 return;
929
930 struct blob *metadata = blob_create(NULL);
931
932 write_uniforms(metadata, prog);
933
934 write_hash_tables(metadata, prog);
935
936 blob_write_uint32(metadata, prog->data->Version);
937 blob_write_uint32(metadata, prog->data->linked_stages);
938
939 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
940 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
941 if (sh) {
942 write_shader_metadata(metadata, sh);
943
944 /* Store nir shader info */
945 blob_write_bytes(metadata, &sh->Program->info, sizeof(shader_info));
946
947 if (sh->Program->info.name)
948 blob_write_string(metadata, sh->Program->info.name);
949
950 if (sh->Program->info.label)
951 blob_write_string(metadata, sh->Program->info.label);
952 }
953 }
954
955 write_xfb(metadata, prog);
956
957 write_uniform_remap_tables(metadata, prog);
958
959 write_subroutines(metadata, prog);
960
961 write_program_resource_list(metadata, prog);
962
963 char sha1_buf[41];
964 for (unsigned i = 0; i < prog->NumShaders; i++) {
965 disk_cache_put_key(cache, prog->Shaders[i]->sha1);
966 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
967 fprintf(stderr, "marking shader: %s\n",
968 _mesa_sha1_format(sha1_buf, prog->Shaders[i]->sha1));
969 }
970 }
971
972 disk_cache_put(cache, prog->data->sha1, metadata->data, metadata->size);
973
974 ralloc_free(metadata);
975
976 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
977 fprintf(stderr, "putting program metadata in cache: %s\n",
978 _mesa_sha1_format(sha1_buf, prog->data->sha1));
979 }
980 }
981
982 bool
983 shader_cache_read_program_metadata(struct gl_context *ctx,
984 struct gl_shader_program *prog)
985 {
986 /* Fixed function programs generated by Mesa are not cached. So don't
987 * try to read metadata for them from the cache.
988 */
989 if (prog->Name == 0)
990 return false;
991
992 struct disk_cache *cache = ctx->Cache;
993 if (!cache)
994 return false;
995
996 /* Include bindings when creating sha1. These bindings change the resulting
997 * binary so they are just as important as the shader source.
998 */
999 char *buf = ralloc_strdup(NULL, "vb: ");
1000 prog->AttributeBindings->iterate(create_binding_str, &buf);
1001 ralloc_strcat(&buf, "fb: ");
1002 prog->FragDataBindings->iterate(create_binding_str, &buf);
1003 ralloc_strcat(&buf, "fbi: ");
1004 prog->FragDataIndexBindings->iterate(create_binding_str, &buf);
1005
1006 /* SSO has an effect on the linked program so include this when generating
1007 * the sha also.
1008 */
1009 ralloc_asprintf_append(&buf, "sso: %s\n",
1010 prog->SeparateShader ? "T" : "F");
1011
1012 char sha1buf[41];
1013 for (unsigned i = 0; i < prog->NumShaders; i++) {
1014 struct gl_shader *sh = prog->Shaders[i];
1015 ralloc_asprintf_append(&buf, "%s: %s\n",
1016 _mesa_shader_stage_to_abbrev(sh->Stage),
1017 _mesa_sha1_format(sha1buf, sh->sha1));
1018 }
1019 _mesa_sha1_compute(buf, strlen(buf), prog->data->sha1);
1020 ralloc_free(buf);
1021
1022 size_t size;
1023 uint8_t *buffer = (uint8_t *) disk_cache_get(cache, prog->data->sha1,
1024 &size);
1025 if (buffer == NULL) {
1026 /* Cached program not found. We may have seen the individual shaders
1027 * before and skipped compiling but they may not have been used together
1028 * in this combination before. Fall back to linking shaders but first
1029 * re-compile the shaders.
1030 *
1031 * We could probably only compile the shaders which were skipped here
1032 * but we need to be careful because the source may also have been
1033 * changed since the last compile so for now we just recompile
1034 * everything.
1035 */
1036 compile_shaders(ctx, prog);
1037 return false;
1038 }
1039
1040 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
1041 fprintf(stderr, "loading shader program meta data from cache: %s\n",
1042 _mesa_sha1_format(sha1buf, prog->data->sha1));
1043 }
1044
1045 struct blob_reader metadata;
1046 blob_reader_init(&metadata, buffer, size);
1047
1048 assert(prog->data->UniformStorage == NULL);
1049
1050 read_uniforms(&metadata, prog);
1051
1052 read_hash_tables(&metadata, prog);
1053
1054 prog->data->Version = blob_read_uint32(&metadata);
1055 prog->data->linked_stages = blob_read_uint32(&metadata);
1056
1057 unsigned mask = prog->data->linked_stages;
1058 while (mask) {
1059 const int j = u_bit_scan(&mask);
1060 create_linked_shader_and_program(ctx, (gl_shader_stage) j, prog,
1061 &metadata);
1062 }
1063
1064 read_xfb(&metadata, prog);
1065
1066 read_uniform_remap_tables(&metadata, prog);
1067
1068 read_subroutines(&metadata, prog);
1069
1070 read_program_resource_list(&metadata, prog);
1071
1072 if (metadata.current != metadata.end || metadata.overrun) {
1073 /* Something has gone wrong discard the item from the cache and rebuild
1074 * from source.
1075 */
1076 assert(!"Invalid GLSL shader disk cache item!");
1077
1078 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
1079 fprintf(stderr, "Error reading program from cache (invalid GLSL "
1080 "cache item)\n");
1081 }
1082
1083 disk_cache_remove(cache, prog->data->sha1);
1084 compile_shaders(ctx, prog);
1085 free(buffer);
1086 return false;
1087 }
1088
1089 /* This is used to flag a shader retrieved from cache */
1090 prog->data->LinkStatus = linking_skipped;
1091
1092 free (buffer);
1093
1094 return true;
1095 }