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