2 * Copyright © 2014 Intel Corporation
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:
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
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.
25 * \file shader_cache.cpp
27 * GLSL shader cache implementation
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.
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.
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.
48 #include "compiler/shader_info.h"
49 #include "glsl_symbol_table.h"
50 #include "glsl_parser_extras.h"
52 #include "ir_optimization.h"
53 #include "ir_rvalue_visitor.h"
54 #include "ir_uniform.h"
56 #include "link_varyings.h"
57 #include "main/core.h"
60 #include "util/disk_cache.h"
61 #include "util/mesa-sha1.h"
62 #include "util/string_to_uint_map.h"
65 #include "main/enums.h"
66 #include "main/shaderobj.h"
67 #include "program/program.h"
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);
78 encode_type_to_blob(struct blob
*blob
, const glsl_type
*type
)
82 switch (type
->base_type
) {
87 case GLSL_TYPE_DOUBLE
:
88 case GLSL_TYPE_UINT64
:
90 encoding
= (type
->base_type
<< 24) |
91 (type
->vector_elements
<< 4) |
92 (type
->matrix_columns
);
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) |
101 case GLSL_TYPE_SUBROUTINE
:
102 encoding
= type
->base_type
<< 24;
103 blob_write_uint32(blob
, encoding
);
104 blob_write_string(blob
, type
->name
);
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
);
112 case GLSL_TYPE_ATOMIC_UINT
:
113 encoding
= (type
->base_type
<< 24);
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
);
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
);
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
);
138 case GLSL_TYPE_ERROR
:
140 assert(!"Cannot encode type!");
145 blob_write_uint32(blob
, encoding
);
148 static const glsl_type
*
149 decode_type_from_blob(struct blob_reader
*blob
)
151 uint32_t u
= blob_read_uint32(blob
);
152 glsl_base_type base_type
= (glsl_base_type
) (u
>> 24);
157 case GLSL_TYPE_FLOAT
:
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),
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),
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
),
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
);
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
);
199 return glsl_type::get_record_instance(fields
, num_fields
, name
);
203 case GLSL_TYPE_ERROR
:
205 assert(!"Cannot decode type!");
211 write_uniforms(struct blob
*metadata
, struct gl_shader_program
*prog
)
213 blob_write_uint32(metadata
, prog
->SamplersValidated
);
214 blob_write_uint32(metadata
, prog
->data
->NumUniformStorage
);
215 blob_write_uint32(metadata
, prog
->data
->NumUniformDataSlots
);
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
);
240 read_uniforms(struct blob_reader
*metadata
, struct gl_shader_program
*prog
)
242 struct gl_uniform_storage
*uniforms
;
243 union gl_constant_value
*data
;
245 prog
->SamplersValidated
= blob_read_uint32(metadata
);
246 prog
->data
->NumUniformStorage
= blob_read_uint32(metadata
);
247 prog
->data
->NumUniformDataSlots
= blob_read_uint32(metadata
);
249 uniforms
= rzalloc_array(prog
, struct gl_uniform_storage
,
250 prog
->data
->NumUniformStorage
);
251 prog
->data
->UniformStorage
= uniforms
;
253 data
= rzalloc_array(uniforms
, union gl_constant_value
,
254 prog
->data
->NumUniformDataSlots
);
255 prog
->data
->UniformDataSlots
= data
;
257 prog
->UniformHash
= new string_to_uint_map
;
259 for (unsigned i
= 0; i
< prog
->data
->NumUniformStorage
; i
++) {
260 uniforms
[i
].type
= decode_type_from_blob(metadata
);
261 uniforms
[i
].array_elements
= blob_read_uint32(metadata
);
262 uniforms
[i
].name
= ralloc_strdup(prog
, blob_read_string (metadata
));
263 uniforms
[i
].storage
= data
+ blob_read_uint32(metadata
);
264 uniforms
[i
].remap_location
= blob_read_uint32(metadata
);
265 uniforms
[i
].block_index
= blob_read_uint32(metadata
);
266 uniforms
[i
].atomic_buffer_index
= blob_read_uint32(metadata
);
267 uniforms
[i
].offset
= blob_read_uint32(metadata
);
268 uniforms
[i
].array_stride
= blob_read_uint32(metadata
);
269 uniforms
[i
].matrix_stride
= blob_read_uint32(metadata
);
270 uniforms
[i
].row_major
= blob_read_uint32(metadata
);
271 uniforms
[i
].num_compatible_subroutines
= blob_read_uint32(metadata
);
272 uniforms
[i
].top_level_array_size
= blob_read_uint32(metadata
);
273 uniforms
[i
].top_level_array_stride
= blob_read_uint32(metadata
);
274 prog
->UniformHash
->put(i
, uniforms
[i
].name
);
280 write_uniform_remap_table(struct blob
*metadata
,
281 struct gl_shader_program
*prog
)
283 blob_write_uint32(metadata
, prog
->NumUniformRemapTable
);
285 for (unsigned i
= 0; i
< prog
->NumUniformRemapTable
; i
++) {
286 blob_write_uint32(metadata
, prog
->UniformRemapTable
[i
] -
287 prog
->data
->UniformStorage
);
292 read_uniform_remap_table(struct blob_reader
*metadata
,
293 struct gl_shader_program
*prog
)
295 prog
->NumUniformRemapTable
= blob_read_uint32(metadata
);
297 prog
->UniformRemapTable
=rzalloc_array(prog
, struct gl_uniform_storage
*,
298 prog
->NumUniformRemapTable
);
300 for (unsigned i
= 0; i
< prog
->NumUniformRemapTable
; i
++) {
301 prog
->UniformRemapTable
[i
] =
302 prog
->data
->UniformStorage
+ blob_read_uint32(metadata
);
307 write_shader_parameters(struct blob
*metadata
,
308 struct gl_program_parameter_list
*params
)
310 blob_write_uint32(metadata
, params
->NumParameters
);
313 while (i
< params
->NumParameters
) {
314 struct gl_program_parameter
*param
= ¶ms
->Parameters
[i
];
316 blob_write_uint32(metadata
, param
->Type
);
317 blob_write_string(metadata
, param
->Name
);
318 blob_write_uint32(metadata
, param
->Size
);
319 blob_write_uint32(metadata
, param
->DataType
);
320 blob_write_bytes(metadata
, param
->StateIndexes
,
321 sizeof(param
->StateIndexes
));
323 i
+= (param
->Size
+ 3) / 4;
326 blob_write_bytes(metadata
, params
->ParameterValues
,
327 sizeof(gl_constant_value
) * 4 * params
->NumParameters
);
329 blob_write_uint32(metadata
, params
->StateFlags
);
333 read_shader_parameters(struct blob_reader
*metadata
,
334 struct gl_program_parameter_list
*params
)
336 gl_state_index state_indexes
[STATE_LENGTH
];
338 uint32_t num_parameters
= blob_read_uint32(metadata
);
340 while (i
< num_parameters
) {
341 gl_register_file type
= (gl_register_file
) blob_read_uint32(metadata
);
342 const char *name
= blob_read_string(metadata
);
343 unsigned size
= blob_read_uint32(metadata
);
344 unsigned data_type
= blob_read_uint32(metadata
);
345 blob_copy_bytes(metadata
, (uint8_t *) state_indexes
,
346 sizeof(state_indexes
));
348 _mesa_add_parameter(params
, type
, name
, size
, data_type
,
349 NULL
, state_indexes
);
354 blob_copy_bytes(metadata
, (uint8_t *) params
->ParameterValues
,
355 sizeof(gl_constant_value
) * 4 * params
->NumParameters
);
357 params
->StateFlags
= blob_read_uint32(metadata
);
361 write_shader_metadata(struct blob
*metadata
, gl_linked_shader
*shader
)
363 assert(shader
->Program
);
364 struct gl_program
*glprog
= shader
->Program
;
366 blob_write_bytes(metadata
, glprog
->TexturesUsed
,
367 sizeof(glprog
->TexturesUsed
));
368 blob_write_uint64(metadata
, glprog
->SamplersUsed
);
370 write_shader_parameters(metadata
, glprog
->Parameters
);
374 read_shader_metadata(struct blob_reader
*metadata
,
375 struct gl_program
*glprog
,
376 gl_linked_shader
*linked
)
378 blob_copy_bytes(metadata
, (uint8_t *) glprog
->TexturesUsed
,
379 sizeof(glprog
->TexturesUsed
));
380 glprog
->SamplersUsed
= blob_read_uint64(metadata
);
382 glprog
->Parameters
= _mesa_new_parameter_list();
383 read_shader_parameters(metadata
, glprog
->Parameters
);
387 create_binding_str(const char *key
, unsigned value
, void *closure
)
389 char **bindings_str
= (char **) closure
;
390 ralloc_asprintf_append(bindings_str
, "%s:%u,", key
, value
);
394 create_linked_shader_and_program(struct gl_context
*ctx
,
395 gl_shader_stage stage
,
396 struct gl_shader_program
*prog
,
397 struct blob_reader
*metadata
)
399 struct gl_program
*glprog
;
401 struct gl_linked_shader
*linked
= rzalloc(NULL
, struct gl_linked_shader
);
402 linked
->Stage
= stage
;
404 glprog
= ctx
->Driver
.NewProgram(ctx
, _mesa_shader_stage_to_program(stage
),
406 glprog
->info
.stage
= stage
;
407 linked
->Program
= glprog
;
409 read_shader_metadata(metadata
, glprog
, linked
);
411 /* Restore shader info */
412 blob_copy_bytes(metadata
, (uint8_t *) &glprog
->info
, sizeof(shader_info
));
413 if (glprog
->info
.name
)
414 glprog
->info
.name
= ralloc_strdup(glprog
, blob_read_string(metadata
));
415 if (glprog
->info
.label
)
416 glprog
->info
.label
= ralloc_strdup(glprog
, blob_read_string(metadata
));
418 _mesa_reference_shader_program_data(ctx
, &glprog
->sh
.data
, prog
->data
);
419 _mesa_reference_program(ctx
, &linked
->Program
, glprog
);
420 prog
->_LinkedShaders
[stage
] = linked
;
424 shader_cache_write_program_metadata(struct gl_context
*ctx
,
425 struct gl_shader_program
*prog
)
427 struct disk_cache
*cache
= ctx
->Cache
;
431 /* Exit early when we are dealing with a ff shader with no source file to
432 * generate a source from.
434 * TODO: In future we should use another method to generate a key for ff
437 if (*prog
->data
->sha1
== 0)
440 struct blob
*metadata
= blob_create(NULL
);
442 write_uniforms(metadata
, prog
);
444 blob_write_uint32(metadata
, prog
->data
->Version
);
445 blob_write_uint32(metadata
, prog
->data
->linked_stages
);
447 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
448 struct gl_linked_shader
*sh
= prog
->_LinkedShaders
[i
];
450 write_shader_metadata(metadata
, sh
);
452 /* Store nir shader info */
453 blob_write_bytes(metadata
, &sh
->Program
->info
, sizeof(shader_info
));
455 if (sh
->Program
->info
.name
)
456 blob_write_string(metadata
, sh
->Program
->info
.name
);
458 if (sh
->Program
->info
.label
)
459 blob_write_string(metadata
, sh
->Program
->info
.label
);
463 write_uniform_remap_table(metadata
, prog
);
466 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
467 disk_cache_put_key(cache
, prog
->Shaders
[i
]->sha1
);
468 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
469 fprintf(stderr
, "marking shader: %s\n",
470 _mesa_sha1_format(sha1_buf
, prog
->Shaders
[i
]->sha1
));
474 disk_cache_put(cache
, prog
->data
->sha1
, metadata
->data
, metadata
->size
);
476 ralloc_free(metadata
);
478 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
479 fprintf(stderr
, "putting program metadata in cache: %s\n",
480 _mesa_sha1_format(sha1_buf
, prog
->data
->sha1
));
485 shader_cache_read_program_metadata(struct gl_context
*ctx
,
486 struct gl_shader_program
*prog
)
488 /* Fixed function programs generated by Mesa are not cached. So don't
489 * try to read metadata for them from the cache.
494 struct disk_cache
*cache
= ctx
->Cache
;
498 /* Include bindings when creating sha1. These bindings change the resulting
499 * binary so they are just as important as the shader source.
501 char *buf
= ralloc_strdup(NULL
, "vb: ");
502 prog
->AttributeBindings
->iterate(create_binding_str
, &buf
);
503 ralloc_strcat(&buf
, "fb: ");
504 prog
->FragDataBindings
->iterate(create_binding_str
, &buf
);
505 ralloc_strcat(&buf
, "fbi: ");
506 prog
->FragDataIndexBindings
->iterate(create_binding_str
, &buf
);
508 /* SSO has an effect on the linked program so include this when generating
511 ralloc_asprintf_append(&buf
, "sso: %s\n",
512 prog
->SeparateShader
? "T" : "F");
515 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
516 struct gl_shader
*sh
= prog
->Shaders
[i
];
517 ralloc_asprintf_append(&buf
, "%s: %s\n",
518 _mesa_shader_stage_to_abbrev(sh
->Stage
),
519 _mesa_sha1_format(sha1buf
, sh
->sha1
));
521 _mesa_sha1_compute(buf
, strlen(buf
), prog
->data
->sha1
);
525 uint8_t *buffer
= (uint8_t *) disk_cache_get(cache
, prog
->data
->sha1
,
527 if (buffer
== NULL
) {
528 /* Cached program not found. We may have seen the individual shaders
529 * before and skipped compiling but they may not have been used together
530 * in this combination before. Fall back to linking shaders but first
531 * re-compile the shaders.
533 * We could probably only compile the shaders which were skipped here
534 * but we need to be careful because the source may also have been
535 * changed since the last compile so for now we just recompile
538 compile_shaders(ctx
, prog
);
542 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
543 fprintf(stderr
, "loading shader program meta data from cache: %s\n",
544 _mesa_sha1_format(sha1buf
, prog
->data
->sha1
));
547 struct blob_reader metadata
;
548 blob_reader_init(&metadata
, buffer
, size
);
550 assert(prog
->data
->UniformStorage
== NULL
);
552 read_uniforms(&metadata
, prog
);
554 prog
->data
->Version
= blob_read_uint32(&metadata
);
555 prog
->data
->linked_stages
= blob_read_uint32(&metadata
);
557 unsigned mask
= prog
->data
->linked_stages
;
559 const int j
= u_bit_scan(&mask
);
560 create_linked_shader_and_program(ctx
, (gl_shader_stage
) j
, prog
,
564 read_uniform_remap_table(&metadata
, prog
);
566 if (metadata
.current
!= metadata
.end
|| metadata
.overrun
) {
567 /* Something has gone wrong discard the item from the cache and rebuild
570 assert(!"Invalid GLSL shader disk cache item!");
572 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
573 fprintf(stderr
, "Error reading program from cache (invalid GLSL "
577 disk_cache_remove(cache
, prog
->data
->sha1
);
578 compile_shaders(ctx
, prog
);
583 /* This is used to flag a shader retrieved from cache */
584 prog
->data
->LinkStatus
= linking_skipped
;