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/nir/nir.h"
30 #include "compiler/nir/nir_serialize.h"
31 #include "main/uniforms.h"
32 #include "pipe/p_shader_tokens.h"
33 #include "util/u_memory.h"
34 #include "util/perf/cpu_trace.h"
35
36 void
st_get_program_binary_driver_sha1(struct gl_context * ctx,uint8_t * sha1)37 st_get_program_binary_driver_sha1(struct gl_context *ctx, uint8_t *sha1)
38 {
39 disk_cache_compute_key(ctx->Cache, NULL, 0, sha1);
40 }
41
42 static void
write_stream_out_to_cache(struct blob * blob,struct pipe_shader_state * state)43 write_stream_out_to_cache(struct blob *blob,
44 struct pipe_shader_state *state)
45 {
46 blob_write_uint32(blob, state->stream_output.num_outputs);
47 if (state->stream_output.num_outputs) {
48 blob_write_bytes(blob, &state->stream_output.stride,
49 sizeof(state->stream_output.stride));
50 blob_write_bytes(blob, &state->stream_output.output,
51 sizeof(state->stream_output.output));
52 }
53 }
54
55 static void
copy_blob_to_driver_cache_blob(struct blob * blob,struct gl_program * prog)56 copy_blob_to_driver_cache_blob(struct blob *blob, struct gl_program *prog)
57 {
58 prog->driver_cache_blob = ralloc_memdup(NULL, blob->data, blob->size);
59 prog->driver_cache_blob_size = blob->size;
60 }
61
62 static void
write_nir_to_cache(struct blob * blob,struct gl_program * prog)63 write_nir_to_cache(struct blob *blob, struct gl_program *prog)
64 {
65 st_serialize_nir(prog);
66
67 blob_write_intptr(blob, prog->serialized_nir_size);
68 blob_write_bytes(blob, prog->serialized_nir, prog->serialized_nir_size);
69
70 copy_blob_to_driver_cache_blob(blob, prog);
71 }
72
73 void
st_serialise_nir_program(struct gl_context * ctx,struct gl_program * prog)74 st_serialise_nir_program(struct gl_context *ctx, struct gl_program *prog)
75 {
76 if (prog->driver_cache_blob)
77 return;
78
79 struct blob blob;
80 blob_init(&blob);
81
82 if (prog->info.stage == MESA_SHADER_VERTEX) {
83 struct gl_vertex_program *vp = (struct gl_vertex_program *)prog;
84
85 blob_write_uint32(&blob, vp->num_inputs);
86 blob_write_uint32(&blob, vp->vert_attrib_mask);
87 blob_write_bytes(&blob, vp->result_to_output,
88 sizeof(vp->result_to_output));
89 }
90
91 if (prog->info.stage == MESA_SHADER_VERTEX ||
92 prog->info.stage == MESA_SHADER_TESS_EVAL ||
93 prog->info.stage == MESA_SHADER_GEOMETRY)
94 write_stream_out_to_cache(&blob, &prog->state);
95
96 write_nir_to_cache(&blob, prog);
97
98 blob_finish(&blob);
99 }
100
101 /**
102 * Store NIR and any other required state in on-disk shader cache.
103 */
104 void
st_store_nir_in_disk_cache(struct st_context * st,struct gl_program * prog)105 st_store_nir_in_disk_cache(struct st_context *st, struct gl_program *prog)
106 {
107 if (!st->ctx->Cache)
108 return;
109
110 /* Exit early when we are dealing with a ff shader with no source file to
111 * generate a source from.
112 */
113 static const char zero[sizeof(prog->sh.data->sha1)] = {0};
114 if (memcmp(prog->sh.data->sha1, zero, sizeof(prog->sh.data->sha1)) == 0)
115 return;
116
117 st_serialise_nir_program(st->ctx, prog);
118
119 if (st->ctx->_Shader->Flags & GLSL_CACHE_INFO) {
120 fprintf(stderr, "putting %s state tracker IR in cache\n",
121 _mesa_shader_stage_to_string(prog->info.stage));
122 }
123 }
124
125 static void
read_stream_out_from_cache(struct blob_reader * blob_reader,struct pipe_shader_state * state)126 read_stream_out_from_cache(struct blob_reader *blob_reader,
127 struct pipe_shader_state *state)
128 {
129 memset(&state->stream_output, 0, sizeof(state->stream_output));
130 state->stream_output.num_outputs = blob_read_uint32(blob_reader);
131 if (state->stream_output.num_outputs) {
132 blob_copy_bytes(blob_reader, &state->stream_output.stride,
133 sizeof(state->stream_output.stride));
134 blob_copy_bytes(blob_reader, &state->stream_output.output,
135 sizeof(state->stream_output.output));
136 }
137 }
138
139 void
st_deserialise_nir_program(struct gl_context * ctx,struct gl_shader_program * shProg,struct gl_program * prog)140 st_deserialise_nir_program(struct gl_context *ctx,
141 struct gl_shader_program *shProg,
142 struct gl_program *prog)
143 {
144 struct st_context *st = st_context(ctx);
145 size_t size = prog->driver_cache_blob_size;
146 uint8_t *buffer = (uint8_t *) prog->driver_cache_blob;
147
148 MESA_TRACE_FUNC();
149
150 st_set_prog_affected_state_flags(prog);
151
152 /* Avoid reallocation of the program parameter list, because the uniform
153 * storage is only associated with the original parameter list.
154 * This should be enough for Bitmap and DrawPixels constants.
155 */
156 _mesa_ensure_and_associate_uniform_storage(ctx, shProg, prog, 16);
157
158 assert(prog->driver_cache_blob && prog->driver_cache_blob_size > 0);
159
160 struct blob_reader blob_reader;
161 blob_reader_init(&blob_reader, buffer, size);
162
163 st_release_variants(st, prog);
164
165 if (prog->info.stage == MESA_SHADER_VERTEX) {
166 struct gl_vertex_program *vp = (struct gl_vertex_program *)prog;
167 vp->num_inputs = blob_read_uint32(&blob_reader);
168 vp->vert_attrib_mask = blob_read_uint32(&blob_reader);
169 blob_copy_bytes(&blob_reader, (uint8_t *) vp->result_to_output,
170 sizeof(vp->result_to_output));
171 }
172
173 if (prog->info.stage == MESA_SHADER_VERTEX ||
174 prog->info.stage == MESA_SHADER_TESS_EVAL ||
175 prog->info.stage == MESA_SHADER_GEOMETRY)
176 read_stream_out_from_cache(&blob_reader, &prog->state);
177
178 assert(prog->nir == NULL);
179 assert(prog->serialized_nir == NULL);
180
181 prog->state.type = PIPE_SHADER_IR_NIR;
182 prog->serialized_nir_size = blob_read_intptr(&blob_reader);
183 prog->serialized_nir = malloc(prog->serialized_nir_size);
184 blob_copy_bytes(&blob_reader, prog->serialized_nir, prog->serialized_nir_size);
185 prog->shader_program = shProg;
186
187 /* Make sure we don't try to read more data than we wrote. This should
188 * never happen in release builds but its useful to have this check to
189 * catch development bugs.
190 */
191 if (blob_reader.current != blob_reader.end || blob_reader.overrun) {
192 assert(!"Invalid shader disk cache item!");
193
194 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
195 fprintf(stderr, "Error reading program from cache (invalid "
196 "cache item)\n");
197 }
198 }
199
200 st_finalize_program(st, prog);
201 }
202
203 bool
st_load_nir_from_disk_cache(struct gl_context * ctx,struct gl_shader_program * prog)204 st_load_nir_from_disk_cache(struct gl_context *ctx,
205 struct gl_shader_program *prog)
206 {
207 if (!ctx->Cache)
208 return false;
209
210 /* If we didn't load the GLSL metadata from cache then we could not have
211 * loaded the NIR either.
212 */
213 if (prog->data->LinkStatus != LINKING_SKIPPED)
214 return false;
215
216 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
217 if (prog->_LinkedShaders[i] == NULL)
218 continue;
219
220 struct gl_program *glprog = prog->_LinkedShaders[i]->Program;
221 st_deserialise_nir_program(ctx, prog, glprog);
222
223 /* We don't need the cached blob anymore so free it */
224 ralloc_free(glprog->driver_cache_blob);
225 glprog->driver_cache_blob = NULL;
226 glprog->driver_cache_blob_size = 0;
227
228 if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
229 fprintf(stderr, "%s state tracker IR retrieved from cache\n",
230 _mesa_shader_stage_to_string(i));
231 }
232 }
233
234 return true;
235 }
236
237 void
st_serialise_nir_program_binary(struct gl_context * ctx,struct gl_shader_program * shProg,struct gl_program * prog)238 st_serialise_nir_program_binary(struct gl_context *ctx,
239 struct gl_shader_program *shProg,
240 struct gl_program *prog)
241 {
242 st_serialise_nir_program(ctx, prog);
243 }
244