glsl: add support for caching subroutines
[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_table(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
460 static void
461 read_uniform_remap_table_entry(struct blob_reader *metadata,
462 gl_uniform_storage *uniform_storage,
463 gl_uniform_storage **entry,
464 enum uniform_remap_type type)
465 {
466 if (type == remap_type_inactive_explicit_location) {
467 *entry = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
468 } else if (type == remap_type_null_ptr) {
469 *entry = NULL;
470 } else {
471 uint32_t uni_offset = blob_read_uint32(metadata);
472 *entry = uniform_storage + uni_offset;
473 }
474 }
475
476 static void
477 read_uniform_remap_table(struct blob_reader *metadata,
478 struct gl_shader_program *prog)
479 {
480 prog->NumUniformRemapTable = blob_read_uint32(metadata);
481
482 prog->UniformRemapTable = rzalloc_array(prog, struct gl_uniform_storage *,
483 prog->NumUniformRemapTable);
484
485 for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
486 enum uniform_remap_type type =
487 (enum uniform_remap_type) blob_read_uint32(metadata);
488
489 read_uniform_remap_table_entry(metadata, prog->data->UniformStorage,
490 &prog->UniformRemapTable[i], type);
491 }
492 }
493
494 struct whte_closure
495 {
496 struct blob *blob;
497 size_t num_entries;
498 };
499
500 static void
501 write_hash_table_entry(const char *key, unsigned value, void *closure)
502 {
503 struct whte_closure *whte = (struct whte_closure *) closure;
504
505 blob_write_string(whte->blob, key);
506 blob_write_uint32(whte->blob, value);
507
508 whte->num_entries++;
509 }
510
511 static void
512 write_hash_table(struct blob *metadata, struct string_to_uint_map *hash)
513 {
514 size_t offset;
515 struct whte_closure whte;
516
517 whte.blob = metadata;
518 whte.num_entries = 0;
519
520 offset = metadata->size;
521
522 /* Write a placeholder for the hashtable size. */
523 blob_write_uint32 (metadata, 0);
524
525 hash->iterate(write_hash_table_entry, &whte);
526
527 /* Overwrite with the computed number of entries written. */
528 blob_overwrite_uint32 (metadata, offset, whte.num_entries);
529 }
530
531 static void
532 read_hash_table(struct blob_reader *metadata, struct string_to_uint_map *hash)
533 {
534 size_t i, num_entries;
535 const char *key;
536 uint32_t value;
537
538 num_entries = blob_read_uint32 (metadata);
539
540 for (i = 0; i < num_entries; i++) {
541 key = blob_read_string(metadata);
542 value = blob_read_uint32(metadata);
543
544 hash->put(value, key);
545 }
546 }
547
548 static void
549 write_hash_tables(struct blob *metadata, struct gl_shader_program *prog)
550 {
551 write_hash_table(metadata, prog->AttributeBindings);
552 write_hash_table(metadata, prog->FragDataBindings);
553 write_hash_table(metadata, prog->FragDataIndexBindings);
554 }
555
556 static void
557 read_hash_tables(struct blob_reader *metadata, struct gl_shader_program *prog)
558 {
559 read_hash_table(metadata, prog->AttributeBindings);
560 read_hash_table(metadata, prog->FragDataBindings);
561 read_hash_table(metadata, prog->FragDataIndexBindings);
562 }
563
564 static void
565 write_shader_subroutine_index(struct blob *metadata,
566 struct gl_linked_shader *sh,
567 struct gl_program_resource *res)
568 {
569 assert(sh);
570
571 for (unsigned j = 0; j < sh->Program->sh.NumSubroutineFunctions; j++) {
572 if (strcmp(((gl_subroutine_function *)res->Data)->name,
573 sh->Program->sh.SubroutineFunctions[j].name) == 0) {
574 blob_write_uint32(metadata, j);
575 break;
576 }
577 }
578 }
579
580 static void
581 write_program_resource_data(struct blob *metadata,
582 struct gl_shader_program *prog,
583 struct gl_program_resource *res)
584 {
585 struct gl_linked_shader *sh;
586
587 switch(res->Type) {
588 case GL_PROGRAM_INPUT:
589 case GL_PROGRAM_OUTPUT: {
590 const gl_shader_variable *var = (gl_shader_variable *)res->Data;
591 blob_write_bytes(metadata, var, sizeof(gl_shader_variable));
592 encode_type_to_blob(metadata, var->type);
593
594 if (var->interface_type)
595 encode_type_to_blob(metadata, var->interface_type);
596
597 if (var->outermost_struct_type)
598 encode_type_to_blob(metadata, var->outermost_struct_type);
599
600 blob_write_string(metadata, var->name);
601 break;
602 }
603 case GL_BUFFER_VARIABLE:
604 case GL_VERTEX_SUBROUTINE_UNIFORM:
605 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
606 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
607 case GL_COMPUTE_SUBROUTINE_UNIFORM:
608 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
609 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
610 case GL_UNIFORM:
611 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
612 if (strcmp(((gl_uniform_storage *)res->Data)->name,
613 prog->data->UniformStorage[i].name) == 0) {
614 blob_write_uint32(metadata, i);
615 break;
616 }
617 }
618 break;
619 case GL_TRANSFORM_FEEDBACK_BUFFER:
620 for (unsigned i = 0; i < MAX_FEEDBACK_BUFFERS; i++) {
621 if (((gl_transform_feedback_buffer *)res->Data)->Binding ==
622 prog->last_vert_prog->sh.LinkedTransformFeedback->Buffers[i].Binding) {
623 blob_write_uint32(metadata, i);
624 break;
625 }
626 }
627 break;
628 case GL_TRANSFORM_FEEDBACK_VARYING:
629 for (int i = 0; i < prog->last_vert_prog->sh.LinkedTransformFeedback->NumVarying; i++) {
630 if (strcmp(((gl_transform_feedback_varying_info *)res->Data)->Name,
631 prog->last_vert_prog->sh.LinkedTransformFeedback->Varyings[i].Name) == 0) {
632 blob_write_uint32(metadata, i);
633 break;
634 }
635 }
636 break;
637 case GL_VERTEX_SUBROUTINE:
638 case GL_TESS_CONTROL_SUBROUTINE:
639 case GL_TESS_EVALUATION_SUBROUTINE:
640 case GL_GEOMETRY_SUBROUTINE:
641 case GL_FRAGMENT_SUBROUTINE:
642 case GL_COMPUTE_SUBROUTINE:
643 sh =
644 prog->_LinkedShaders[_mesa_shader_stage_from_subroutine(res->Type)];
645 write_shader_subroutine_index(metadata, sh, res);
646 break;
647 default:
648 assert(!"Support for writing resource not yet implemented.");
649 }
650 }
651
652 static void
653 read_program_resource_data(struct blob_reader *metadata,
654 struct gl_shader_program *prog,
655 struct gl_program_resource *res)
656 {
657 struct gl_linked_shader *sh;
658
659 switch(res->Type) {
660 case GL_PROGRAM_INPUT:
661 case GL_PROGRAM_OUTPUT: {
662 gl_shader_variable *var = ralloc(prog, struct gl_shader_variable);
663
664 blob_copy_bytes(metadata, (uint8_t *) var, sizeof(gl_shader_variable));
665 var->type = decode_type_from_blob(metadata);
666
667 if (var->interface_type)
668 var->interface_type = decode_type_from_blob(metadata);
669
670 if (var->outermost_struct_type)
671 var->outermost_struct_type = decode_type_from_blob(metadata);
672
673 var->name = ralloc_strdup(prog, blob_read_string(metadata));
674
675 res->Data = var;
676 break;
677 }
678 case GL_BUFFER_VARIABLE:
679 case GL_VERTEX_SUBROUTINE_UNIFORM:
680 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
681 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
682 case GL_COMPUTE_SUBROUTINE_UNIFORM:
683 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
684 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
685 case GL_UNIFORM:
686 res->Data = &prog->data->UniformStorage[blob_read_uint32(metadata)];
687 break;
688 case GL_TRANSFORM_FEEDBACK_BUFFER:
689 res->Data = &prog->last_vert_prog->
690 sh.LinkedTransformFeedback->Buffers[blob_read_uint32(metadata)];
691 break;
692 case GL_TRANSFORM_FEEDBACK_VARYING:
693 res->Data = &prog->last_vert_prog->
694 sh.LinkedTransformFeedback->Varyings[blob_read_uint32(metadata)];
695 break;
696 case GL_VERTEX_SUBROUTINE:
697 case GL_TESS_CONTROL_SUBROUTINE:
698 case GL_TESS_EVALUATION_SUBROUTINE:
699 case GL_GEOMETRY_SUBROUTINE:
700 case GL_FRAGMENT_SUBROUTINE:
701 case GL_COMPUTE_SUBROUTINE:
702 sh =
703 prog->_LinkedShaders[_mesa_shader_stage_from_subroutine(res->Type)];
704 res->Data =
705 &sh->Program->sh.SubroutineFunctions[blob_read_uint32(metadata)];
706 break;
707 default:
708 assert(!"Support for reading resource not yet implemented.");
709 }
710 }
711
712 static void
713 write_program_resource_list(struct blob *metadata,
714 struct gl_shader_program *prog)
715 {
716 blob_write_uint32(metadata, prog->data->NumProgramResourceList);
717
718 for (unsigned i = 0; i < prog->data->NumProgramResourceList; i++) {
719 blob_write_uint32(metadata, prog->data->ProgramResourceList[i].Type);
720 write_program_resource_data(metadata, prog,
721 &prog->data->ProgramResourceList[i]);
722 blob_write_bytes(metadata,
723 &prog->data->ProgramResourceList[i].StageReferences,
724 sizeof(prog->data->ProgramResourceList[i].StageReferences));
725 }
726 }
727
728 static void
729 read_program_resource_list(struct blob_reader *metadata,
730 struct gl_shader_program *prog)
731 {
732 prog->data->NumProgramResourceList = blob_read_uint32(metadata);
733
734 prog->data->ProgramResourceList =
735 ralloc_array(prog, gl_program_resource,
736 prog->data->NumProgramResourceList);
737
738 for (unsigned i = 0; i < prog->data->NumProgramResourceList; i++) {
739 prog->data->ProgramResourceList[i].Type = blob_read_uint32(metadata);
740 read_program_resource_data(metadata, prog,
741 &prog->data->ProgramResourceList[i]);
742 blob_copy_bytes(metadata,
743 (uint8_t *) &prog->data->ProgramResourceList[i].StageReferences,
744 sizeof(prog->data->ProgramResourceList[i].StageReferences));
745 }
746 }
747
748 static void
749 write_shader_parameters(struct blob *metadata,
750 struct gl_program_parameter_list *params)
751 {
752 blob_write_uint32(metadata, params->NumParameters);
753 uint32_t i = 0;
754
755 while (i < params->NumParameters) {
756 struct gl_program_parameter *param = &params->Parameters[i];
757
758 blob_write_uint32(metadata, param->Type);
759 blob_write_string(metadata, param->Name);
760 blob_write_uint32(metadata, param->Size);
761 blob_write_uint32(metadata, param->DataType);
762 blob_write_bytes(metadata, param->StateIndexes,
763 sizeof(param->StateIndexes));
764
765 i += (param->Size + 3) / 4;
766 }
767
768 blob_write_bytes(metadata, params->ParameterValues,
769 sizeof(gl_constant_value) * 4 * params->NumParameters);
770
771 blob_write_uint32(metadata, params->StateFlags);
772 }
773
774 static void
775 read_shader_parameters(struct blob_reader *metadata,
776 struct gl_program_parameter_list *params)
777 {
778 gl_state_index state_indexes[STATE_LENGTH];
779 uint32_t i = 0;
780 uint32_t num_parameters = blob_read_uint32(metadata);
781
782 while (i < num_parameters) {
783 gl_register_file type = (gl_register_file) blob_read_uint32(metadata);
784 const char *name = blob_read_string(metadata);
785 unsigned size = blob_read_uint32(metadata);
786 unsigned data_type = blob_read_uint32(metadata);
787 blob_copy_bytes(metadata, (uint8_t *) state_indexes,
788 sizeof(state_indexes));
789
790 _mesa_add_parameter(params, type, name, size, data_type,
791 NULL, state_indexes);
792
793 i += (size + 3) / 4;
794 }
795
796 blob_copy_bytes(metadata, (uint8_t *) params->ParameterValues,
797 sizeof(gl_constant_value) * 4 * params->NumParameters);
798
799 params->StateFlags = blob_read_uint32(metadata);
800 }
801
802 static void
803 write_shader_metadata(struct blob *metadata, gl_linked_shader *shader)
804 {
805 assert(shader->Program);
806 struct gl_program *glprog = shader->Program;
807
808 blob_write_bytes(metadata, glprog->TexturesUsed,
809 sizeof(glprog->TexturesUsed));
810 blob_write_uint64(metadata, glprog->SamplersUsed);
811
812 blob_write_bytes(metadata, glprog->SamplerUnits,
813 sizeof(glprog->SamplerUnits));
814 blob_write_bytes(metadata, glprog->sh.SamplerTargets,
815 sizeof(glprog->sh.SamplerTargets));
816 blob_write_uint32(metadata, glprog->ShadowSamplers);
817
818 write_shader_parameters(metadata, glprog->Parameters);
819 }
820
821 static void
822 read_shader_metadata(struct blob_reader *metadata,
823 struct gl_program *glprog,
824 gl_linked_shader *linked)
825 {
826 blob_copy_bytes(metadata, (uint8_t *) glprog->TexturesUsed,
827 sizeof(glprog->TexturesUsed));
828 glprog->SamplersUsed = blob_read_uint64(metadata);
829
830 blob_copy_bytes(metadata, (uint8_t *) glprog->SamplerUnits,
831 sizeof(glprog->SamplerUnits));
832 blob_copy_bytes(metadata, (uint8_t *) glprog->sh.SamplerTargets,
833 sizeof(glprog->sh.SamplerTargets));
834 glprog->ShadowSamplers = blob_read_uint32(metadata);
835
836 glprog->Parameters = _mesa_new_parameter_list();
837 read_shader_parameters(metadata, glprog->Parameters);
838 }
839
840 static void
841 create_binding_str(const char *key, unsigned value, void *closure)
842 {
843 char **bindings_str = (char **) closure;
844 ralloc_asprintf_append(bindings_str, "%s:%u,", key, value);
845 }
846
847 static void
848 create_linked_shader_and_program(struct gl_context *ctx,
849 gl_shader_stage stage,
850 struct gl_shader_program *prog,
851 struct blob_reader *metadata)
852 {
853 struct gl_program *glprog;
854
855 struct gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
856 linked->Stage = stage;
857
858 glprog = ctx->Driver.NewProgram(ctx, _mesa_shader_stage_to_program(stage),
859 prog->Name, false);
860 glprog->info.stage = stage;
861 linked->Program = glprog;
862
863 read_shader_metadata(metadata, glprog, linked);
864
865 /* Restore shader info */
866 blob_copy_bytes(metadata, (uint8_t *) &glprog->info, sizeof(shader_info));
867 if (glprog->info.name)
868 glprog->info.name = ralloc_strdup(glprog, blob_read_string(metadata));
869 if (glprog->info.label)
870 glprog->info.label = ralloc_strdup(glprog, blob_read_string(metadata));
871
872 _mesa_reference_shader_program_data(ctx, &glprog->sh.data, prog->data);
873 _mesa_reference_program(ctx, &linked->Program, glprog);
874 prog->_LinkedShaders[stage] = linked;
875 }
876
877 void
878 shader_cache_write_program_metadata(struct gl_context *ctx,
879 struct gl_shader_program *prog)
880 {
881 struct disk_cache *cache = ctx->Cache;
882 if (!cache)
883 return;
884
885 /* Exit early when we are dealing with a ff shader with no source file to
886 * generate a source from.
887 *
888 * TODO: In future we should use another method to generate a key for ff
889 * programs.
890 */
891 if (*prog->data->sha1 == 0)
892 return;
893
894 struct blob *metadata = blob_create(NULL);
895
896 write_uniforms(metadata, prog);
897
898 write_hash_tables(metadata, prog);
899
900 blob_write_uint32(metadata, prog->data->Version);
901 blob_write_uint32(metadata, prog->data->linked_stages);
902
903 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
904 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
905 if (sh) {
906 write_shader_metadata(metadata, sh);
907
908 /* Store nir shader info */
909 blob_write_bytes(metadata, &sh->Program->info, sizeof(shader_info));
910
911 if (sh->Program->info.name)
912 blob_write_string(metadata, sh->Program->info.name);
913
914 if (sh->Program->info.label)
915 blob_write_string(metadata, sh->Program->info.label);
916 }
917 }
918
919 write_xfb(metadata, prog);
920
921 write_uniform_remap_table(metadata, prog);
922
923 write_subroutines(metadata, prog);
924
925 write_program_resource_list(metadata, prog);
926
927 char sha1_buf[41];
928 for (unsigned i = 0; i < prog->NumShaders; i++) {
929 disk_cache_put_key(cache, prog->Shaders[i]->sha1);
930 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
931 fprintf(stderr, "marking shader: %s\n",
932 _mesa_sha1_format(sha1_buf, prog->Shaders[i]->sha1));
933 }
934 }
935
936 disk_cache_put(cache, prog->data->sha1, metadata->data, metadata->size);
937
938 ralloc_free(metadata);
939
940 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
941 fprintf(stderr, "putting program metadata in cache: %s\n",
942 _mesa_sha1_format(sha1_buf, prog->data->sha1));
943 }
944 }
945
946 bool
947 shader_cache_read_program_metadata(struct gl_context *ctx,
948 struct gl_shader_program *prog)
949 {
950 /* Fixed function programs generated by Mesa are not cached. So don't
951 * try to read metadata for them from the cache.
952 */
953 if (prog->Name == 0)
954 return false;
955
956 struct disk_cache *cache = ctx->Cache;
957 if (!cache)
958 return false;
959
960 /* Include bindings when creating sha1. These bindings change the resulting
961 * binary so they are just as important as the shader source.
962 */
963 char *buf = ralloc_strdup(NULL, "vb: ");
964 prog->AttributeBindings->iterate(create_binding_str, &buf);
965 ralloc_strcat(&buf, "fb: ");
966 prog->FragDataBindings->iterate(create_binding_str, &buf);
967 ralloc_strcat(&buf, "fbi: ");
968 prog->FragDataIndexBindings->iterate(create_binding_str, &buf);
969
970 /* SSO has an effect on the linked program so include this when generating
971 * the sha also.
972 */
973 ralloc_asprintf_append(&buf, "sso: %s\n",
974 prog->SeparateShader ? "T" : "F");
975
976 char sha1buf[41];
977 for (unsigned i = 0; i < prog->NumShaders; i++) {
978 struct gl_shader *sh = prog->Shaders[i];
979 ralloc_asprintf_append(&buf, "%s: %s\n",
980 _mesa_shader_stage_to_abbrev(sh->Stage),
981 _mesa_sha1_format(sha1buf, sh->sha1));
982 }
983 _mesa_sha1_compute(buf, strlen(buf), prog->data->sha1);
984 ralloc_free(buf);
985
986 size_t size;
987 uint8_t *buffer = (uint8_t *) disk_cache_get(cache, prog->data->sha1,
988 &size);
989 if (buffer == NULL) {
990 /* Cached program not found. We may have seen the individual shaders
991 * before and skipped compiling but they may not have been used together
992 * in this combination before. Fall back to linking shaders but first
993 * re-compile the shaders.
994 *
995 * We could probably only compile the shaders which were skipped here
996 * but we need to be careful because the source may also have been
997 * changed since the last compile so for now we just recompile
998 * everything.
999 */
1000 compile_shaders(ctx, prog);
1001 return false;
1002 }
1003
1004 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
1005 fprintf(stderr, "loading shader program meta data from cache: %s\n",
1006 _mesa_sha1_format(sha1buf, prog->data->sha1));
1007 }
1008
1009 struct blob_reader metadata;
1010 blob_reader_init(&metadata, buffer, size);
1011
1012 assert(prog->data->UniformStorage == NULL);
1013
1014 read_uniforms(&metadata, prog);
1015
1016 read_hash_tables(&metadata, prog);
1017
1018 prog->data->Version = blob_read_uint32(&metadata);
1019 prog->data->linked_stages = blob_read_uint32(&metadata);
1020
1021 unsigned mask = prog->data->linked_stages;
1022 while (mask) {
1023 const int j = u_bit_scan(&mask);
1024 create_linked_shader_and_program(ctx, (gl_shader_stage) j, prog,
1025 &metadata);
1026 }
1027
1028 read_xfb(&metadata, prog);
1029
1030 read_uniform_remap_table(&metadata, prog);
1031
1032 read_subroutines(&metadata, prog);
1033
1034 read_program_resource_list(&metadata, prog);
1035
1036 if (metadata.current != metadata.end || metadata.overrun) {
1037 /* Something has gone wrong discard the item from the cache and rebuild
1038 * from source.
1039 */
1040 assert(!"Invalid GLSL shader disk cache item!");
1041
1042 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
1043 fprintf(stderr, "Error reading program from cache (invalid GLSL "
1044 "cache item)\n");
1045 }
1046
1047 disk_cache_remove(cache, prog->data->sha1);
1048 compile_shaders(ctx, prog);
1049 free(buffer);
1050 return false;
1051 }
1052
1053 /* This is used to flag a shader retrieved from cache */
1054 prog->data->LinkStatus = linking_skipped;
1055
1056 free (buffer);
1057
1058 return true;
1059 }