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