xref: /aosp_15_r20/external/mesa3d/src/compiler/glsl/shader_cache.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker  * Copyright © 2014 Intel Corporation
3*61046927SAndroid Build Coastguard Worker  *
4*61046927SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*61046927SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*61046927SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*61046927SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*61046927SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*61046927SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*61046927SAndroid Build Coastguard Worker  *
11*61046927SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*61046927SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*61046927SAndroid Build Coastguard Worker  * Software.
14*61046927SAndroid Build Coastguard Worker  *
15*61046927SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*61046927SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*61046927SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*61046927SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*61046927SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*61046927SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*61046927SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
22*61046927SAndroid Build Coastguard Worker  */
23*61046927SAndroid Build Coastguard Worker 
24*61046927SAndroid Build Coastguard Worker /**
25*61046927SAndroid Build Coastguard Worker  * \file shader_cache.cpp
26*61046927SAndroid Build Coastguard Worker  *
27*61046927SAndroid Build Coastguard Worker  * GLSL shader cache implementation
28*61046927SAndroid Build Coastguard Worker  *
29*61046927SAndroid Build Coastguard Worker  * This uses disk_cache.c to write out a serialization of various
30*61046927SAndroid Build Coastguard Worker  * state that's required in order to successfully load and use a
31*61046927SAndroid Build Coastguard Worker  * binary written out by a drivers backend, this state is referred to as
32*61046927SAndroid Build Coastguard Worker  * "metadata" throughout the implementation.
33*61046927SAndroid Build Coastguard Worker  *
34*61046927SAndroid Build Coastguard Worker  * The hash key for glsl metadata is a hash of the hashes of each GLSL
35*61046927SAndroid Build Coastguard Worker  * source string as well as some API settings that change the final program
36*61046927SAndroid Build Coastguard Worker  * such as SSO, attribute bindings, frag data bindings, etc.
37*61046927SAndroid Build Coastguard Worker  *
38*61046927SAndroid Build Coastguard Worker  * In order to avoid caching any actual IR we use the put_key/get_key support
39*61046927SAndroid Build Coastguard Worker  * in the disk_cache to put the SHA-1 hash for each successfully compiled
40*61046927SAndroid Build Coastguard Worker  * shader into the cache, and optimisticly return early from glCompileShader
41*61046927SAndroid Build Coastguard Worker  * (if the identical shader had been successfully compiled in the past),
42*61046927SAndroid Build Coastguard Worker  * in the hope that the final linked shader will be found in the cache.
43*61046927SAndroid Build Coastguard Worker  * If anything goes wrong (shader variant not found, backend cache item is
44*61046927SAndroid Build Coastguard Worker  * corrupt, etc) we will use a fallback path to compile and link the IR.
45*61046927SAndroid Build Coastguard Worker  */
46*61046927SAndroid Build Coastguard Worker 
47*61046927SAndroid Build Coastguard Worker #include "util/os_misc.h"
48*61046927SAndroid Build Coastguard Worker 
49*61046927SAndroid Build Coastguard Worker #include "compiler/shader_info.h"
50*61046927SAndroid Build Coastguard Worker #include "glsl_symbol_table.h"
51*61046927SAndroid Build Coastguard Worker #include "glsl_parser_extras.h"
52*61046927SAndroid Build Coastguard Worker #include "ir.h"
53*61046927SAndroid Build Coastguard Worker #include "ir_optimization.h"
54*61046927SAndroid Build Coastguard Worker #include "ir_rvalue_visitor.h"
55*61046927SAndroid Build Coastguard Worker #include "ir_uniform.h"
56*61046927SAndroid Build Coastguard Worker #include "linker.h"
57*61046927SAndroid Build Coastguard Worker #include "nir.h"
58*61046927SAndroid Build Coastguard Worker #include "program.h"
59*61046927SAndroid Build Coastguard Worker #include "serialize.h"
60*61046927SAndroid Build Coastguard Worker #include "shader_cache.h"
61*61046927SAndroid Build Coastguard Worker #include "util/mesa-sha1.h"
62*61046927SAndroid Build Coastguard Worker #include "string_to_uint_map.h"
63*61046927SAndroid Build Coastguard Worker #include "main/mtypes.h"
64*61046927SAndroid Build Coastguard Worker 
65*61046927SAndroid Build Coastguard Worker extern "C" {
66*61046927SAndroid Build Coastguard Worker #include "main/enums.h"
67*61046927SAndroid Build Coastguard Worker #include "main/shaderobj.h"
68*61046927SAndroid Build Coastguard Worker #include "program/program.h"
69*61046927SAndroid Build Coastguard Worker }
70*61046927SAndroid Build Coastguard Worker 
71*61046927SAndroid Build Coastguard Worker static void
compile_shaders(struct gl_context * ctx,struct gl_shader_program * prog)72*61046927SAndroid Build Coastguard Worker compile_shaders(struct gl_context *ctx, struct gl_shader_program *prog) {
73*61046927SAndroid Build Coastguard Worker    for (unsigned i = 0; i < prog->NumShaders; i++) {
74*61046927SAndroid Build Coastguard Worker       _mesa_glsl_compile_shader(ctx, prog->Shaders[i], false, false, true);
75*61046927SAndroid Build Coastguard Worker    }
76*61046927SAndroid Build Coastguard Worker }
77*61046927SAndroid Build Coastguard Worker 
78*61046927SAndroid Build Coastguard Worker static void
create_binding_str(const char * key,unsigned value,void * closure)79*61046927SAndroid Build Coastguard Worker create_binding_str(const char *key, unsigned value, void *closure)
80*61046927SAndroid Build Coastguard Worker {
81*61046927SAndroid Build Coastguard Worker    char **bindings_str = (char **) closure;
82*61046927SAndroid Build Coastguard Worker    ralloc_asprintf_append(bindings_str, "%s:%u,", key, value);
83*61046927SAndroid Build Coastguard Worker }
84*61046927SAndroid Build Coastguard Worker 
85*61046927SAndroid Build Coastguard Worker void
shader_cache_write_program_metadata(struct gl_context * ctx,struct gl_shader_program * prog)86*61046927SAndroid Build Coastguard Worker shader_cache_write_program_metadata(struct gl_context *ctx,
87*61046927SAndroid Build Coastguard Worker                                     struct gl_shader_program *prog)
88*61046927SAndroid Build Coastguard Worker {
89*61046927SAndroid Build Coastguard Worker    struct disk_cache *cache = ctx->Cache;
90*61046927SAndroid Build Coastguard Worker    if (!cache)
91*61046927SAndroid Build Coastguard Worker       return;
92*61046927SAndroid Build Coastguard Worker 
93*61046927SAndroid Build Coastguard Worker    /* Exit early when we are dealing with a ff shader with no source file to
94*61046927SAndroid Build Coastguard Worker     * generate a source from, or with a SPIR-V shader.
95*61046927SAndroid Build Coastguard Worker     *
96*61046927SAndroid Build Coastguard Worker     * TODO: In future we should use another method to generate a key for ff
97*61046927SAndroid Build Coastguard Worker     * programs, and SPIR-V shaders.
98*61046927SAndroid Build Coastguard Worker     */
99*61046927SAndroid Build Coastguard Worker    static const char zero[sizeof(prog->data->sha1)] = {0};
100*61046927SAndroid Build Coastguard Worker    if (memcmp(prog->data->sha1, zero, sizeof(prog->data->sha1)) == 0)
101*61046927SAndroid Build Coastguard Worker       return;
102*61046927SAndroid Build Coastguard Worker 
103*61046927SAndroid Build Coastguard Worker    struct blob metadata;
104*61046927SAndroid Build Coastguard Worker    blob_init(&metadata);
105*61046927SAndroid Build Coastguard Worker 
106*61046927SAndroid Build Coastguard Worker    if (ctx->Driver.ShaderCacheSerializeDriverBlob) {
107*61046927SAndroid Build Coastguard Worker       for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
108*61046927SAndroid Build Coastguard Worker          struct gl_linked_shader *sh = prog->_LinkedShaders[i];
109*61046927SAndroid Build Coastguard Worker          if (sh)
110*61046927SAndroid Build Coastguard Worker             ctx->Driver.ShaderCacheSerializeDriverBlob(ctx, sh->Program);
111*61046927SAndroid Build Coastguard Worker       }
112*61046927SAndroid Build Coastguard Worker    }
113*61046927SAndroid Build Coastguard Worker 
114*61046927SAndroid Build Coastguard Worker    serialize_glsl_program(&metadata, ctx, prog);
115*61046927SAndroid Build Coastguard Worker 
116*61046927SAndroid Build Coastguard Worker    struct cache_item_metadata cache_item_metadata;
117*61046927SAndroid Build Coastguard Worker    cache_item_metadata.type = CACHE_ITEM_TYPE_GLSL;
118*61046927SAndroid Build Coastguard Worker    cache_item_metadata.keys =
119*61046927SAndroid Build Coastguard Worker       (cache_key *) malloc(prog->NumShaders * sizeof(cache_key));
120*61046927SAndroid Build Coastguard Worker    cache_item_metadata.num_keys = prog->NumShaders;
121*61046927SAndroid Build Coastguard Worker 
122*61046927SAndroid Build Coastguard Worker    if (!cache_item_metadata.keys)
123*61046927SAndroid Build Coastguard Worker       goto fail;
124*61046927SAndroid Build Coastguard Worker 
125*61046927SAndroid Build Coastguard Worker    for (unsigned i = 0; i < prog->NumShaders; i++) {
126*61046927SAndroid Build Coastguard Worker       memcpy(cache_item_metadata.keys[i], prog->Shaders[i]->disk_cache_sha1,
127*61046927SAndroid Build Coastguard Worker              sizeof(cache_key));
128*61046927SAndroid Build Coastguard Worker    }
129*61046927SAndroid Build Coastguard Worker 
130*61046927SAndroid Build Coastguard Worker    disk_cache_put(cache, prog->data->sha1, metadata.data, metadata.size,
131*61046927SAndroid Build Coastguard Worker                   &cache_item_metadata);
132*61046927SAndroid Build Coastguard Worker 
133*61046927SAndroid Build Coastguard Worker    char sha1_buf[41];
134*61046927SAndroid Build Coastguard Worker    if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
135*61046927SAndroid Build Coastguard Worker       _mesa_sha1_format(sha1_buf, prog->data->sha1);
136*61046927SAndroid Build Coastguard Worker       fprintf(stderr, "putting program metadata in cache: %s\n", sha1_buf);
137*61046927SAndroid Build Coastguard Worker    }
138*61046927SAndroid Build Coastguard Worker 
139*61046927SAndroid Build Coastguard Worker fail:
140*61046927SAndroid Build Coastguard Worker    free(cache_item_metadata.keys);
141*61046927SAndroid Build Coastguard Worker    blob_finish(&metadata);
142*61046927SAndroid Build Coastguard Worker }
143*61046927SAndroid Build Coastguard Worker 
144*61046927SAndroid Build Coastguard Worker bool
shader_cache_read_program_metadata(struct gl_context * ctx,struct gl_shader_program * prog)145*61046927SAndroid Build Coastguard Worker shader_cache_read_program_metadata(struct gl_context *ctx,
146*61046927SAndroid Build Coastguard Worker                                    struct gl_shader_program *prog)
147*61046927SAndroid Build Coastguard Worker {
148*61046927SAndroid Build Coastguard Worker    /* Fixed function programs generated by Mesa, or SPIR-V shaders, are not
149*61046927SAndroid Build Coastguard Worker     * cached. So don't try to read metadata for them from the cache.
150*61046927SAndroid Build Coastguard Worker     */
151*61046927SAndroid Build Coastguard Worker    if (prog->Name == 0 || prog->data->spirv)
152*61046927SAndroid Build Coastguard Worker       return false;
153*61046927SAndroid Build Coastguard Worker 
154*61046927SAndroid Build Coastguard Worker    struct disk_cache *cache = ctx->Cache;
155*61046927SAndroid Build Coastguard Worker    if (!cache)
156*61046927SAndroid Build Coastguard Worker       return false;
157*61046927SAndroid Build Coastguard Worker 
158*61046927SAndroid Build Coastguard Worker    /* Include bindings when creating sha1. These bindings change the resulting
159*61046927SAndroid Build Coastguard Worker     * binary so they are just as important as the shader source.
160*61046927SAndroid Build Coastguard Worker     */
161*61046927SAndroid Build Coastguard Worker    char *buf = ralloc_strdup(NULL, "vb: ");
162*61046927SAndroid Build Coastguard Worker    prog->AttributeBindings->iterate(create_binding_str, &buf);
163*61046927SAndroid Build Coastguard Worker    ralloc_strcat(&buf, "fb: ");
164*61046927SAndroid Build Coastguard Worker    prog->FragDataBindings->iterate(create_binding_str, &buf);
165*61046927SAndroid Build Coastguard Worker    ralloc_strcat(&buf, "fbi: ");
166*61046927SAndroid Build Coastguard Worker    prog->FragDataIndexBindings->iterate(create_binding_str, &buf);
167*61046927SAndroid Build Coastguard Worker    ralloc_asprintf_append(&buf, "tf: %d ", prog->TransformFeedback.BufferMode);
168*61046927SAndroid Build Coastguard Worker    for (unsigned int i = 0; i < prog->TransformFeedback.NumVarying; i++) {
169*61046927SAndroid Build Coastguard Worker       ralloc_asprintf_append(&buf, "%s ",
170*61046927SAndroid Build Coastguard Worker                              prog->TransformFeedback.VaryingNames[i]);
171*61046927SAndroid Build Coastguard Worker    }
172*61046927SAndroid Build Coastguard Worker 
173*61046927SAndroid Build Coastguard Worker    /* SSO has an effect on the linked program so include this when generating
174*61046927SAndroid Build Coastguard Worker     * the sha also.
175*61046927SAndroid Build Coastguard Worker     */
176*61046927SAndroid Build Coastguard Worker    ralloc_asprintf_append(&buf, "sso: %s\n",
177*61046927SAndroid Build Coastguard Worker                           prog->SeparateShader ? "T" : "F");
178*61046927SAndroid Build Coastguard Worker 
179*61046927SAndroid Build Coastguard Worker    /* A shader might end up producing different output depending on the glsl
180*61046927SAndroid Build Coastguard Worker     * version supported by the compiler. For example a different path might be
181*61046927SAndroid Build Coastguard Worker     * taken by the preprocessor, so add the version to the hash input.
182*61046927SAndroid Build Coastguard Worker     */
183*61046927SAndroid Build Coastguard Worker    ralloc_asprintf_append(&buf, "api: %d glsl: %d fglsl: %d\n",
184*61046927SAndroid Build Coastguard Worker                           ctx->API, ctx->Const.GLSLVersion,
185*61046927SAndroid Build Coastguard Worker                           ctx->Const.ForceGLSLVersion);
186*61046927SAndroid Build Coastguard Worker 
187*61046927SAndroid Build Coastguard Worker    /* We run the preprocessor on shaders after hashing them, so we need to
188*61046927SAndroid Build Coastguard Worker     * add any extension override vars to the hash. If we don't do this the
189*61046927SAndroid Build Coastguard Worker     * preprocessor could result in different output and we could load the
190*61046927SAndroid Build Coastguard Worker     * wrong shader.
191*61046927SAndroid Build Coastguard Worker     */
192*61046927SAndroid Build Coastguard Worker    const char *ext_override = os_get_option("MESA_EXTENSION_OVERRIDE");
193*61046927SAndroid Build Coastguard Worker    if (ext_override) {
194*61046927SAndroid Build Coastguard Worker       ralloc_asprintf_append(&buf, "ext:%s", ext_override);
195*61046927SAndroid Build Coastguard Worker    }
196*61046927SAndroid Build Coastguard Worker 
197*61046927SAndroid Build Coastguard Worker    /* DRI config options may also change the output from the compiler so
198*61046927SAndroid Build Coastguard Worker     * include them as an input to sha1 creation.
199*61046927SAndroid Build Coastguard Worker     */
200*61046927SAndroid Build Coastguard Worker    char sha1buf[41];
201*61046927SAndroid Build Coastguard Worker    _mesa_sha1_format(sha1buf, ctx->Const.dri_config_options_sha1);
202*61046927SAndroid Build Coastguard Worker    ralloc_strcat(&buf, sha1buf);
203*61046927SAndroid Build Coastguard Worker 
204*61046927SAndroid Build Coastguard Worker    for (unsigned i = 0; i < prog->NumShaders; i++) {
205*61046927SAndroid Build Coastguard Worker       struct gl_shader *sh = prog->Shaders[i];
206*61046927SAndroid Build Coastguard Worker       _mesa_sha1_format(sha1buf, sh->disk_cache_sha1);
207*61046927SAndroid Build Coastguard Worker       ralloc_asprintf_append(&buf, "%s: %s\n",
208*61046927SAndroid Build Coastguard Worker                              _mesa_shader_stage_to_abbrev(sh->Stage), sha1buf);
209*61046927SAndroid Build Coastguard Worker    }
210*61046927SAndroid Build Coastguard Worker    disk_cache_compute_key(cache, buf, strlen(buf), prog->data->sha1);
211*61046927SAndroid Build Coastguard Worker    ralloc_free(buf);
212*61046927SAndroid Build Coastguard Worker 
213*61046927SAndroid Build Coastguard Worker    size_t size;
214*61046927SAndroid Build Coastguard Worker    uint8_t *buffer = (uint8_t *) disk_cache_get(cache, prog->data->sha1,
215*61046927SAndroid Build Coastguard Worker                                                 &size);
216*61046927SAndroid Build Coastguard Worker    if (buffer == NULL) {
217*61046927SAndroid Build Coastguard Worker       /* Cached program not found. We may have seen the individual shaders
218*61046927SAndroid Build Coastguard Worker        * before and skipped compiling but they may not have been used together
219*61046927SAndroid Build Coastguard Worker        * in this combination before. Fall back to linking shaders but first
220*61046927SAndroid Build Coastguard Worker        * re-compile the shaders.
221*61046927SAndroid Build Coastguard Worker        *
222*61046927SAndroid Build Coastguard Worker        * We could probably only compile the shaders which were skipped here
223*61046927SAndroid Build Coastguard Worker        * but we need to be careful because the source may also have been
224*61046927SAndroid Build Coastguard Worker        * changed since the last compile so for now we just recompile
225*61046927SAndroid Build Coastguard Worker        * everything.
226*61046927SAndroid Build Coastguard Worker        */
227*61046927SAndroid Build Coastguard Worker       compile_shaders(ctx, prog);
228*61046927SAndroid Build Coastguard Worker       return false;
229*61046927SAndroid Build Coastguard Worker    }
230*61046927SAndroid Build Coastguard Worker 
231*61046927SAndroid Build Coastguard Worker    if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
232*61046927SAndroid Build Coastguard Worker       _mesa_sha1_format(sha1buf, prog->data->sha1);
233*61046927SAndroid Build Coastguard Worker       fprintf(stderr, "loading shader program meta data from cache: %s\n",
234*61046927SAndroid Build Coastguard Worker               sha1buf);
235*61046927SAndroid Build Coastguard Worker    }
236*61046927SAndroid Build Coastguard Worker 
237*61046927SAndroid Build Coastguard Worker    struct blob_reader metadata;
238*61046927SAndroid Build Coastguard Worker    blob_reader_init(&metadata, buffer, size);
239*61046927SAndroid Build Coastguard Worker 
240*61046927SAndroid Build Coastguard Worker    bool deserialized = deserialize_glsl_program(&metadata, ctx, prog);
241*61046927SAndroid Build Coastguard Worker 
242*61046927SAndroid Build Coastguard Worker    if (!deserialized || metadata.current != metadata.end || metadata.overrun) {
243*61046927SAndroid Build Coastguard Worker       /* Something has gone wrong discard the item from the cache and rebuild
244*61046927SAndroid Build Coastguard Worker        * from source.
245*61046927SAndroid Build Coastguard Worker        */
246*61046927SAndroid Build Coastguard Worker       assert(!"Invalid GLSL shader disk cache item!");
247*61046927SAndroid Build Coastguard Worker 
248*61046927SAndroid Build Coastguard Worker       if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
249*61046927SAndroid Build Coastguard Worker          fprintf(stderr, "Error reading program from cache (invalid GLSL "
250*61046927SAndroid Build Coastguard Worker                  "cache item)\n");
251*61046927SAndroid Build Coastguard Worker       }
252*61046927SAndroid Build Coastguard Worker 
253*61046927SAndroid Build Coastguard Worker       disk_cache_remove(cache, prog->data->sha1);
254*61046927SAndroid Build Coastguard Worker       compile_shaders(ctx, prog);
255*61046927SAndroid Build Coastguard Worker       free(buffer);
256*61046927SAndroid Build Coastguard Worker       return false;
257*61046927SAndroid Build Coastguard Worker    }
258*61046927SAndroid Build Coastguard Worker 
259*61046927SAndroid Build Coastguard Worker    /* This is used to flag a shader retrieved from cache */
260*61046927SAndroid Build Coastguard Worker    prog->data->LinkStatus = LINKING_SKIPPED;
261*61046927SAndroid Build Coastguard Worker 
262*61046927SAndroid Build Coastguard Worker    free (buffer);
263*61046927SAndroid Build Coastguard Worker 
264*61046927SAndroid Build Coastguard Worker    return true;
265*61046927SAndroid Build Coastguard Worker }
266