xref: /aosp_15_r20/external/mesa3d/src/mesa/state_tracker/st_shader_cache.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2017 Timothy Arceri
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 #include <stdio.h>
25 #include "st_debug.h"
26 #include "st_program.h"
27 #include "st_shader_cache.h"
28 #include "st_util.h"
29 #include "compiler/glsl/program.h"
30 #include "compiler/nir/nir.h"
31 #include "compiler/nir/nir_serialize.h"
32 #include "main/uniforms.h"
33 #include "pipe/p_shader_tokens.h"
34 #include "util/u_memory.h"
35 #include "util/perf/cpu_trace.h"
36 
37 void
st_get_program_binary_driver_sha1(struct gl_context * ctx,uint8_t * sha1)38 st_get_program_binary_driver_sha1(struct gl_context *ctx, uint8_t *sha1)
39 {
40    disk_cache_compute_key(ctx->Cache, NULL, 0, sha1);
41 }
42 
43 static void
write_stream_out_to_cache(struct blob * blob,struct pipe_shader_state * state)44 write_stream_out_to_cache(struct blob *blob,
45                           struct pipe_shader_state *state)
46 {
47    blob_write_uint32(blob, state->stream_output.num_outputs);
48    if (state->stream_output.num_outputs) {
49       blob_write_bytes(blob, &state->stream_output.stride,
50                        sizeof(state->stream_output.stride));
51       blob_write_bytes(blob, &state->stream_output.output,
52                        sizeof(state->stream_output.output));
53    }
54 }
55 
56 static void
copy_blob_to_driver_cache_blob(struct blob * blob,struct gl_program * prog)57 copy_blob_to_driver_cache_blob(struct blob *blob, struct gl_program *prog)
58 {
59    prog->driver_cache_blob = ralloc_memdup(NULL, blob->data, blob->size);
60    prog->driver_cache_blob_size = blob->size;
61 }
62 
63 static void
write_nir_to_cache(struct blob * blob,struct gl_program * prog)64 write_nir_to_cache(struct blob *blob, struct gl_program *prog)
65 {
66    st_serialize_nir(prog);
67 
68    blob_write_intptr(blob, prog->serialized_nir_size);
69    blob_write_bytes(blob, prog->serialized_nir, prog->serialized_nir_size);
70 
71    copy_blob_to_driver_cache_blob(blob, prog);
72 }
73 
74 void
st_serialise_nir_program(struct gl_context * ctx,struct gl_program * prog)75 st_serialise_nir_program(struct gl_context *ctx, struct gl_program *prog)
76 {
77    if (prog->driver_cache_blob)
78       return;
79 
80    struct blob blob;
81    blob_init(&blob);
82 
83    if (prog->info.stage == MESA_SHADER_VERTEX) {
84       struct gl_vertex_program *vp = (struct gl_vertex_program *)prog;
85 
86       blob_write_uint32(&blob, vp->num_inputs);
87       blob_write_uint32(&blob, vp->vert_attrib_mask);
88       blob_write_bytes(&blob, vp->result_to_output,
89                        sizeof(vp->result_to_output));
90    }
91 
92    if (prog->info.stage == MESA_SHADER_VERTEX ||
93        prog->info.stage == MESA_SHADER_TESS_EVAL ||
94        prog->info.stage == MESA_SHADER_GEOMETRY)
95       write_stream_out_to_cache(&blob, &prog->state);
96 
97    write_nir_to_cache(&blob, prog);
98 
99    blob_finish(&blob);
100 }
101 
102 /**
103  * Store NIR and any other required state in on-disk shader cache.
104  */
105 void
st_store_nir_in_disk_cache(struct st_context * st,struct gl_program * prog)106 st_store_nir_in_disk_cache(struct st_context *st, struct gl_program *prog)
107 {
108    if (!st->ctx->Cache)
109       return;
110 
111    /* Exit early when we are dealing with a ff shader with no source file to
112     * generate a source from.
113     */
114    static const char zero[sizeof(prog->sh.data->sha1)] = {0};
115    if (memcmp(prog->sh.data->sha1, zero, sizeof(prog->sh.data->sha1)) == 0)
116       return;
117 
118    st_serialise_nir_program(st->ctx, prog);
119 
120    if (st->ctx->_Shader->Flags & GLSL_CACHE_INFO) {
121       fprintf(stderr, "putting %s state tracker IR in cache\n",
122               _mesa_shader_stage_to_string(prog->info.stage));
123    }
124 }
125 
126 static void
read_stream_out_from_cache(struct blob_reader * blob_reader,struct pipe_shader_state * state)127 read_stream_out_from_cache(struct blob_reader *blob_reader,
128                            struct pipe_shader_state *state)
129 {
130    memset(&state->stream_output, 0, sizeof(state->stream_output));
131    state->stream_output.num_outputs = blob_read_uint32(blob_reader);
132    if (state->stream_output.num_outputs) {
133       blob_copy_bytes(blob_reader, &state->stream_output.stride,
134                       sizeof(state->stream_output.stride));
135       blob_copy_bytes(blob_reader, &state->stream_output.output,
136                       sizeof(state->stream_output.output));
137    }
138 }
139 
140 void
st_deserialise_nir_program(struct gl_context * ctx,struct gl_shader_program * shProg,struct gl_program * prog)141 st_deserialise_nir_program(struct gl_context *ctx,
142                           struct gl_shader_program *shProg,
143                           struct gl_program *prog)
144 {
145    struct st_context *st = st_context(ctx);
146    size_t size = prog->driver_cache_blob_size;
147    uint8_t *buffer = (uint8_t *) prog->driver_cache_blob;
148 
149    MESA_TRACE_FUNC();
150 
151    st_set_prog_affected_state_flags(prog);
152 
153    /* Avoid reallocation of the program parameter list, because the uniform
154     * storage is only associated with the original parameter list.
155     * This should be enough for Bitmap and DrawPixels constants.
156     */
157    _mesa_ensure_and_associate_uniform_storage(ctx, shProg, prog, 16);
158 
159    assert(prog->driver_cache_blob && prog->driver_cache_blob_size > 0);
160 
161    struct blob_reader blob_reader;
162    blob_reader_init(&blob_reader, buffer, size);
163 
164    st_release_variants(st, prog);
165 
166    if (prog->info.stage == MESA_SHADER_VERTEX) {
167       struct gl_vertex_program *vp = (struct gl_vertex_program *)prog;
168       vp->num_inputs = blob_read_uint32(&blob_reader);
169       vp->vert_attrib_mask = blob_read_uint32(&blob_reader);
170       blob_copy_bytes(&blob_reader, (uint8_t *) vp->result_to_output,
171                       sizeof(vp->result_to_output));
172    }
173 
174    if (prog->info.stage == MESA_SHADER_VERTEX ||
175        prog->info.stage == MESA_SHADER_TESS_EVAL ||
176        prog->info.stage == MESA_SHADER_GEOMETRY)
177       read_stream_out_from_cache(&blob_reader, &prog->state);
178 
179    assert(prog->nir == NULL);
180    assert(prog->serialized_nir == NULL);
181 
182    prog->state.type = PIPE_SHADER_IR_NIR;
183    prog->serialized_nir_size = blob_read_intptr(&blob_reader);
184    prog->serialized_nir = malloc(prog->serialized_nir_size);
185    blob_copy_bytes(&blob_reader, prog->serialized_nir, prog->serialized_nir_size);
186    prog->shader_program = shProg;
187 
188    /* Make sure we don't try to read more data than we wrote. This should
189     * never happen in release builds but its useful to have this check to
190     * catch development bugs.
191     */
192    if (blob_reader.current != blob_reader.end || blob_reader.overrun) {
193       assert(!"Invalid shader disk cache item!");
194 
195       if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
196          fprintf(stderr, "Error reading program from cache (invalid "
197                  "cache item)\n");
198       }
199    }
200 
201    st_finalize_program(st, prog);
202 }
203 
204 bool
st_load_nir_from_disk_cache(struct gl_context * ctx,struct gl_shader_program * prog)205 st_load_nir_from_disk_cache(struct gl_context *ctx,
206                             struct gl_shader_program *prog)
207 {
208    if (!ctx->Cache)
209       return false;
210 
211    /* If we didn't load the GLSL metadata from cache then we could not have
212     * loaded the NIR either.
213     */
214    if (prog->data->LinkStatus != LINKING_SKIPPED)
215       return false;
216 
217    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
218       if (prog->_LinkedShaders[i] == NULL)
219          continue;
220 
221       struct gl_program *glprog = prog->_LinkedShaders[i]->Program;
222       st_deserialise_nir_program(ctx, prog, glprog);
223 
224       /* We don't need the cached blob anymore so free it */
225       ralloc_free(glprog->driver_cache_blob);
226       glprog->driver_cache_blob = NULL;
227       glprog->driver_cache_blob_size = 0;
228 
229       if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
230          fprintf(stderr, "%s state tracker IR retrieved from cache\n",
231                  _mesa_shader_stage_to_string(i));
232       }
233    }
234 
235    return true;
236 }
237 
238 void
st_serialise_nir_program_binary(struct gl_context * ctx,struct gl_shader_program * shProg,struct gl_program * prog)239 st_serialise_nir_program_binary(struct gl_context *ctx,
240                                 struct gl_shader_program *shProg,
241                                 struct gl_program *prog)
242 {
243    st_serialise_nir_program(ctx, prog);
244 }
245