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