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