st/mesa: don't serialize all streamout state if there are no SO outputs
[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 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 stp->state.type = PIPE_SHADER_IR_NIR;
211 stp->state.ir.nir = nir_deserialize(NULL, options, &blob_reader);
212 stp->shader_program = shProg;
213 prog->nir = stp->state.ir.nir;
214 } else {
215 read_tgsi_from_cache(&blob_reader, &stp->state.tokens);
216 }
217
218 /* Make sure we don't try to read more data than we wrote. This should
219 * never happen in release builds but its useful to have this check to
220 * catch development bugs.
221 */
222 if (blob_reader.current != blob_reader.end || blob_reader.overrun) {
223 assert(!"Invalid TGSI shader disk cache item!");
224
225 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
226 fprintf(stderr, "Error reading program from cache (invalid "
227 "TGSI cache item)\n");
228 }
229 }
230
231 st_finalize_program(st, prog);
232 }
233
234 bool
235 st_load_ir_from_disk_cache(struct gl_context *ctx,
236 struct gl_shader_program *prog,
237 bool nir)
238 {
239 if (!ctx->Cache)
240 return false;
241
242 /* If we didn't load the GLSL metadata from cache then we could not have
243 * loaded TGSI or NIR either.
244 */
245 if (prog->data->LinkStatus != LINKING_SKIPPED)
246 return false;
247
248 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
249 if (prog->_LinkedShaders[i] == NULL)
250 continue;
251
252 struct gl_program *glprog = prog->_LinkedShaders[i]->Program;
253 st_deserialise_ir_program(ctx, prog, glprog, nir);
254
255 /* We don't need the cached blob anymore so free it */
256 ralloc_free(glprog->driver_cache_blob);
257 glprog->driver_cache_blob = NULL;
258 glprog->driver_cache_blob_size = 0;
259
260 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
261 fprintf(stderr, "%s state tracker IR retrieved from cache\n",
262 _mesa_shader_stage_to_string(i));
263 }
264 }
265
266 return true;
267 }
268
269 void
270 st_serialise_tgsi_program(struct gl_context *ctx, struct gl_program *prog)
271 {
272 st_serialise_ir_program(ctx, prog, false);
273 }
274
275 void
276 st_serialise_tgsi_program_binary(struct gl_context *ctx,
277 struct gl_shader_program *shProg,
278 struct gl_program *prog)
279 {
280 st_serialise_ir_program(ctx, prog, false);
281 }
282
283 void
284 st_deserialise_tgsi_program(struct gl_context *ctx,
285 struct gl_shader_program *shProg,
286 struct gl_program *prog)
287 {
288 st_deserialise_ir_program(ctx, shProg, prog, false);
289 }
290
291 void
292 st_serialise_nir_program(struct gl_context *ctx, struct gl_program *prog)
293 {
294 st_serialise_ir_program(ctx, prog, true);
295 }
296
297 void
298 st_serialise_nir_program_binary(struct gl_context *ctx,
299 struct gl_shader_program *shProg,
300 struct gl_program *prog)
301 {
302 st_serialise_ir_program(ctx, prog, true);
303 }
304
305 void
306 st_deserialise_nir_program(struct gl_context *ctx,
307 struct gl_shader_program *shProg,
308 struct gl_program *prog)
309 {
310 st_deserialise_ir_program(ctx, shProg, prog, true);
311 }