st/mesa: call nir_serialize only once per shader
[mesa.git] / src / mesa / state_tracker / st_shader_cache.c
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 "pipe/p_shader_tokens.h"
33 #include "program/ir_to_mesa.h"
34 #include "tgsi/tgsi_parse.h"
35 #include "util/u_memory.h"
36
37 void
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
44 write_stream_out_to_cache(struct blob *blob,
45 struct pipe_shader_state *state)
46 {
47 blob_write_bytes(blob, &state->stream_output,
48 sizeof(state->stream_output));
49 }
50
51 static void
52 copy_blob_to_driver_cache_blob(struct blob *blob, struct gl_program *prog)
53 {
54 prog->driver_cache_blob = ralloc_size(NULL, blob->size);
55 memcpy(prog->driver_cache_blob, blob->data, blob->size);
56 prog->driver_cache_blob_size = blob->size;
57 }
58
59 static void
60 write_tgsi_to_cache(struct blob *blob, const struct tgsi_token *tokens,
61 struct gl_program *prog)
62 {
63 unsigned num_tokens = tgsi_num_tokens(tokens);
64
65 blob_write_uint32(blob, num_tokens);
66 blob_write_bytes(blob, tokens, num_tokens * sizeof(struct tgsi_token));
67 copy_blob_to_driver_cache_blob(blob, prog);
68 }
69
70 static void
71 write_nir_to_cache(struct blob *blob, struct gl_program *prog)
72 {
73 struct st_program *stp = (struct st_program *)prog;
74
75 st_serialize_nir(stp);
76
77 blob_write_intptr(blob, stp->nir_size);
78 blob_write_bytes(blob, stp->nir_binary, stp->nir_size);
79
80 copy_blob_to_driver_cache_blob(blob, prog);
81 }
82
83 static void
84 st_serialise_ir_program(struct gl_context *ctx, struct gl_program *prog,
85 bool nir)
86 {
87 if (prog->driver_cache_blob)
88 return;
89
90 struct st_program *stp = (struct st_program *)prog;
91 struct blob blob;
92 blob_init(&blob);
93
94 if (prog->info.stage == MESA_SHADER_VERTEX) {
95 struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
96
97 blob_write_uint32(&blob, stvp->num_inputs);
98 blob_write_bytes(&blob, stvp->index_to_input,
99 sizeof(stvp->index_to_input));
100 blob_write_bytes(&blob, stvp->input_to_index,
101 sizeof(stvp->input_to_index));
102 blob_write_bytes(&blob, stvp->result_to_output,
103 sizeof(stvp->result_to_output));
104 }
105
106 if (prog->info.stage == MESA_SHADER_VERTEX ||
107 prog->info.stage == MESA_SHADER_TESS_EVAL ||
108 prog->info.stage == MESA_SHADER_GEOMETRY)
109 write_stream_out_to_cache(&blob, &stp->state);
110
111 if (nir)
112 write_nir_to_cache(&blob, prog);
113 else
114 write_tgsi_to_cache(&blob, stp->state.tokens, prog);
115
116 blob_finish(&blob);
117 }
118
119 /**
120 * Store TGSI or NIR and any other required state in on-disk shader cache.
121 */
122 void
123 st_store_ir_in_disk_cache(struct st_context *st, struct gl_program *prog,
124 bool nir)
125 {
126 if (!st->ctx->Cache)
127 return;
128
129 /* Exit early when we are dealing with a ff shader with no source file to
130 * generate a source from.
131 */
132 static const char zero[sizeof(prog->sh.data->sha1)] = {0};
133 if (memcmp(prog->sh.data->sha1, zero, sizeof(prog->sh.data->sha1)) == 0)
134 return;
135
136 st_serialise_ir_program(st->ctx, prog, nir);
137
138 if (st->ctx->_Shader->Flags & GLSL_CACHE_INFO) {
139 fprintf(stderr, "putting %s state tracker IR in cache\n",
140 _mesa_shader_stage_to_string(prog->info.stage));
141 }
142 }
143
144 static void
145 read_stream_out_from_cache(struct blob_reader *blob_reader,
146 struct pipe_shader_state *state)
147 {
148 blob_copy_bytes(blob_reader, (uint8_t *) &state->stream_output,
149 sizeof(state->stream_output));
150 }
151
152 static void
153 read_tgsi_from_cache(struct blob_reader *blob_reader,
154 const struct tgsi_token **tokens)
155 {
156 unsigned num_tokens = blob_read_uint32(blob_reader);
157 unsigned tokens_size = num_tokens * sizeof(struct tgsi_token);
158 *tokens = (const struct tgsi_token*) MALLOC(tokens_size);
159 blob_copy_bytes(blob_reader, (uint8_t *) *tokens, tokens_size);
160 }
161
162 static void
163 st_deserialise_ir_program(struct gl_context *ctx,
164 struct gl_shader_program *shProg,
165 struct gl_program *prog, bool nir)
166 {
167 struct st_context *st = st_context(ctx);
168 size_t size = prog->driver_cache_blob_size;
169 uint8_t *buffer = (uint8_t *) prog->driver_cache_blob;
170
171 st_set_prog_affected_state_flags(prog);
172 _mesa_associate_uniform_storage(ctx, shProg, prog);
173
174 assert(prog->driver_cache_blob && prog->driver_cache_blob_size > 0);
175
176 struct st_program *stp = st_program(prog);
177 struct blob_reader blob_reader;
178 blob_reader_init(&blob_reader, buffer, size);
179
180 if (prog->info.stage == MESA_SHADER_VERTEX) {
181 st_release_vp_variants(st, stp);
182
183 struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
184 stvp->num_inputs = blob_read_uint32(&blob_reader);
185 blob_copy_bytes(&blob_reader, (uint8_t *) stvp->index_to_input,
186 sizeof(stvp->index_to_input));
187 blob_copy_bytes(&blob_reader, (uint8_t *) stvp->input_to_index,
188 sizeof(stvp->input_to_index));
189 blob_copy_bytes(&blob_reader, (uint8_t *) stvp->result_to_output,
190 sizeof(stvp->result_to_output));
191 } else if (prog->info.stage == MESA_SHADER_FRAGMENT) {
192 st_release_fp_variants(st, stp);
193 } else {
194 st_release_common_variants(st, stp);
195 }
196
197 if (prog->info.stage == MESA_SHADER_VERTEX ||
198 prog->info.stage == MESA_SHADER_TESS_EVAL ||
199 prog->info.stage == MESA_SHADER_GEOMETRY)
200 read_stream_out_from_cache(&blob_reader, &stp->state);
201
202 if (nir) {
203 assert(prog->nir == NULL);
204 assert(stp->state.ir.nir == NULL);
205 assert(stp->nir_binary == NULL);
206
207 /* The remainder of the binary is NIR. */
208 stp->state.type = PIPE_SHADER_IR_NIR;
209 stp->nir_size = blob_read_intptr(&blob_reader);
210 stp->nir_binary = malloc(stp->nir_size);
211 blob_copy_bytes(&blob_reader, stp->nir_binary, stp->nir_size);
212 stp->shader_program = shProg;
213 } else {
214 read_tgsi_from_cache(&blob_reader, &stp->state.tokens);
215 }
216
217 /* Make sure we don't try to read more data than we wrote. This should
218 * never happen in release builds but its useful to have this check to
219 * catch development bugs.
220 */
221 if (blob_reader.current != blob_reader.end || blob_reader.overrun) {
222 assert(!"Invalid TGSI shader disk cache item!");
223
224 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
225 fprintf(stderr, "Error reading program from cache (invalid "
226 "TGSI cache item)\n");
227 }
228 }
229
230 st_finalize_program(st, prog);
231 }
232
233 bool
234 st_load_ir_from_disk_cache(struct gl_context *ctx,
235 struct gl_shader_program *prog,
236 bool nir)
237 {
238 if (!ctx->Cache)
239 return false;
240
241 /* If we didn't load the GLSL metadata from cache then we could not have
242 * loaded TGSI or NIR either.
243 */
244 if (prog->data->LinkStatus != LINKING_SKIPPED)
245 return false;
246
247 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
248 if (prog->_LinkedShaders[i] == NULL)
249 continue;
250
251 struct gl_program *glprog = prog->_LinkedShaders[i]->Program;
252 st_deserialise_ir_program(ctx, prog, glprog, nir);
253
254 /* We don't need the cached blob anymore so free it */
255 ralloc_free(glprog->driver_cache_blob);
256 glprog->driver_cache_blob = NULL;
257 glprog->driver_cache_blob_size = 0;
258
259 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
260 fprintf(stderr, "%s state tracker IR retrieved from cache\n",
261 _mesa_shader_stage_to_string(i));
262 }
263 }
264
265 return true;
266 }
267
268 void
269 st_serialise_tgsi_program(struct gl_context *ctx, struct gl_program *prog)
270 {
271 st_serialise_ir_program(ctx, prog, false);
272 }
273
274 void
275 st_serialise_tgsi_program_binary(struct gl_context *ctx,
276 struct gl_shader_program *shProg,
277 struct gl_program *prog)
278 {
279 st_serialise_ir_program(ctx, prog, false);
280 }
281
282 void
283 st_deserialise_tgsi_program(struct gl_context *ctx,
284 struct gl_shader_program *shProg,
285 struct gl_program *prog)
286 {
287 st_deserialise_ir_program(ctx, shProg, prog, false);
288 }
289
290 void
291 st_serialise_nir_program(struct gl_context *ctx, struct gl_program *prog)
292 {
293 st_serialise_ir_program(ctx, prog, true);
294 }
295
296 void
297 st_serialise_nir_program_binary(struct gl_context *ctx,
298 struct gl_shader_program *shProg,
299 struct gl_program *prog)
300 {
301 st_serialise_ir_program(ctx, prog, true);
302 }
303
304 void
305 st_deserialise_nir_program(struct gl_context *ctx,
306 struct gl_shader_program *shProg,
307 struct gl_program *prog)
308 {
309 st_deserialise_ir_program(ctx, shProg, prog, true);
310 }