mesa: Always call ProgramBinarySerializeDriverBlob
[mesa.git] / src / mesa / main / program_binary.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (c) 2017 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file program_binary.c
27 *
28 * Helper functions for serializing a binary program.
29 */
30
31
32 #include "compiler/blob.h"
33 #include "compiler/glsl/serialize.h"
34 #include "main/errors.h"
35 #include "main/mtypes.h"
36 #include "main/shaderapi.h"
37 #include "util/bitscan.h"
38 #include "util/crc32.h"
39 #include "program_binary.h"
40 #include "program/prog_parameter.h"
41
42 /**
43 * Mesa supports one binary format, but it must differentiate between formats
44 * produced by different drivers and different Mesa versions.
45 *
46 * Mesa uses a uint32_t value to specify an internal format. The only format
47 * defined has one uint32_t value of 0, followed by 20 bytes specifying a sha1
48 * that uniquely identifies the Mesa driver type and version.
49 */
50
51 struct program_binary_header {
52 /* If internal_format is 0, it must be followed by the 20 byte sha1 that
53 * identifies the Mesa driver and version supported. If we want to support
54 * something besides a sha1, then a new internal_format value can be added.
55 */
56 uint32_t internal_format;
57 uint8_t sha1[20];
58 /* Fields following sha1 can be changed since the sha1 will guarantee that
59 * the binary only works with the same Mesa version.
60 */
61 uint32_t size;
62 uint32_t crc32;
63 };
64
65 /**
66 * Returns the header size needed for a binary
67 */
68 static unsigned
69 get_program_binary_header_size(void)
70 {
71 return sizeof(struct program_binary_header);
72 }
73
74 static bool
75 write_program_binary(const void *payload, unsigned payload_size,
76 const void *sha1, void *binary, unsigned binary_size,
77 GLenum *binary_format)
78 {
79 struct program_binary_header *hdr = binary;
80
81 if (binary_size < sizeof(*hdr))
82 return false;
83
84 /* binary_size is the size of the buffer provided by the application.
85 * Make sure our program (payload) will fit in the buffer.
86 */
87 if (payload_size > binary_size - sizeof(*hdr))
88 return false;
89
90 hdr->internal_format = 0;
91 memcpy(hdr->sha1, sha1, sizeof(hdr->sha1));
92 memcpy(hdr + 1, payload, payload_size);
93 hdr->size = payload_size;
94
95 hdr->crc32 = util_hash_crc32(hdr + 1, payload_size);
96 *binary_format = GL_PROGRAM_BINARY_FORMAT_MESA;
97
98 return true;
99 }
100
101 static bool
102 simple_header_checks(const struct program_binary_header *hdr, unsigned length)
103 {
104 if (hdr == NULL || length < sizeof(*hdr))
105 return false;
106
107 if (hdr->internal_format != 0)
108 return false;
109
110 return true;
111 }
112
113 static bool
114 check_crc32(const struct program_binary_header *hdr, unsigned length)
115 {
116 uint32_t crc32;
117 unsigned crc32_len;
118
119 crc32_len = hdr->size;
120 if (crc32_len > length - sizeof(*hdr))
121 return false;
122
123 crc32 = util_hash_crc32(hdr + 1, crc32_len);
124 if (hdr->crc32 != crc32)
125 return false;
126
127 return true;
128 }
129
130 static bool
131 is_program_binary_valid(GLenum binary_format, const void *sha1,
132 const struct program_binary_header *hdr,
133 unsigned length)
134 {
135 if (binary_format != GL_PROGRAM_BINARY_FORMAT_MESA)
136 return false;
137
138 if (!simple_header_checks(hdr, length))
139 return false;
140
141 if (memcmp(hdr->sha1, sha1, sizeof(hdr->sha1)) != 0)
142 return false;
143
144 if (!check_crc32(hdr, length))
145 return false;
146
147 return true;
148 }
149
150 /**
151 * Returns the payload within the binary.
152 *
153 * If NULL is returned, then the binary not supported. If non-NULL is
154 * returned, it will be a pointer contained within the specified `binary`
155 * buffer.
156 *
157 * This can be used to access the payload of `binary` during the
158 * glProgramBinary call.
159 */
160 static const void*
161 get_program_binary_payload(GLenum binary_format, const void *sha1,
162 const void *binary, unsigned length)
163 {
164 const struct program_binary_header *hdr = binary;
165 if (!is_program_binary_valid(binary_format, sha1, hdr, length))
166 return NULL;
167 return (const uint8_t*)binary + sizeof(*hdr);
168 }
169
170 static void
171 write_program_payload(struct gl_context *ctx, struct blob *blob,
172 struct gl_shader_program *sh_prog)
173 {
174 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
175 struct gl_linked_shader *shader = sh_prog->_LinkedShaders[stage];
176 if (shader)
177 ctx->Driver.ProgramBinarySerializeDriverBlob(ctx, shader->Program);
178 }
179
180 serialize_glsl_program(blob, ctx, sh_prog);
181
182 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
183 struct gl_linked_shader *shader = sh_prog->_LinkedShaders[stage];
184 if (shader) {
185 struct gl_program *prog = sh_prog->_LinkedShaders[stage]->Program;
186 ralloc_free(prog->driver_cache_blob);
187 prog->driver_cache_blob = NULL;
188 prog->driver_cache_blob_size = 0;
189 }
190 }
191 }
192
193 static bool
194 read_program_payload(struct gl_context *ctx, struct blob_reader *blob,
195 GLenum binary_format, struct gl_shader_program *sh_prog)
196 {
197 if (!deserialize_glsl_program(blob, ctx, sh_prog))
198 return false;
199
200 unsigned int stage;
201 for (stage = 0; stage < ARRAY_SIZE(sh_prog->_LinkedShaders); stage++) {
202 struct gl_linked_shader *shader = sh_prog->_LinkedShaders[stage];
203 if (!shader)
204 continue;
205
206 ctx->Driver.ProgramBinaryDeserializeDriverBlob(ctx, sh_prog,
207 shader->Program);
208 }
209
210 return true;
211 }
212
213 void
214 _mesa_get_program_binary_length(struct gl_context *ctx,
215 struct gl_shader_program *sh_prog,
216 GLint *length)
217 {
218 struct blob blob;
219 blob_init_fixed(&blob, NULL, SIZE_MAX);
220 write_program_payload(ctx, &blob, sh_prog);
221 *length = get_program_binary_header_size() + blob.size;
222 blob_finish(&blob);
223 }
224
225 void
226 _mesa_get_program_binary(struct gl_context *ctx,
227 struct gl_shader_program *sh_prog,
228 GLsizei buf_size, GLsizei *length,
229 GLenum *binary_format, GLvoid *binary)
230 {
231 struct blob blob;
232 uint8_t driver_sha1[20];
233 unsigned header_size = get_program_binary_header_size();
234
235 ctx->Driver.GetProgramBinaryDriverSHA1(ctx, driver_sha1);
236
237 blob_init(&blob);
238
239 if (buf_size < header_size)
240 goto fail;
241
242 write_program_payload(ctx, &blob, sh_prog);
243 if (blob.size + header_size > buf_size ||
244 blob.out_of_memory)
245 goto fail;
246
247 bool written = write_program_binary(blob.data, blob.size, driver_sha1,
248 binary, buf_size, binary_format);
249 if (!written || blob.out_of_memory)
250 goto fail;
251
252 *length = header_size + blob.size;
253
254 blob_finish(&blob);
255 return;
256
257 fail:
258 _mesa_error(ctx, GL_INVALID_OPERATION,
259 "glGetProgramBinary(buffer too small)");
260 *length = 0;
261 blob_finish(&blob);
262 }
263
264 void
265 _mesa_program_binary(struct gl_context *ctx, struct gl_shader_program *sh_prog,
266 GLenum binary_format, const GLvoid *binary,
267 GLsizei length)
268 {
269 uint8_t driver_sha1[20];
270 unsigned header_size = get_program_binary_header_size();
271
272 ctx->Driver.GetProgramBinaryDriverSHA1(ctx, driver_sha1);
273
274 const void *payload = get_program_binary_payload(binary_format, driver_sha1,
275 binary, length);
276
277 if (payload == NULL) {
278 sh_prog->data->LinkStatus = LINKING_FAILURE;
279 return;
280 }
281
282 struct blob_reader blob;
283 blob_reader_init(&blob, payload, length - header_size);
284
285 unsigned programs_in_use = 0;
286 if (ctx->_Shader)
287 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
288 if (ctx->_Shader->CurrentProgram[stage] &&
289 ctx->_Shader->CurrentProgram[stage]->Id == sh_prog->Name) {
290 programs_in_use |= 1 << stage;
291 }
292 }
293
294 if (!read_program_payload(ctx, &blob, binary_format, sh_prog)) {
295 sh_prog->data->LinkStatus = LINKING_FAILURE;
296 return;
297 }
298
299 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
300 *
301 * "If LinkProgram or ProgramBinary successfully re-links a program
302 * object that is active for any shader stage, then the newly generated
303 * executable code will be installed as part of the current rendering
304 * state for all shader stages where the program is active.
305 * Additionally, the newly generated executable code is made part of
306 * the state of any program pipeline for all stages where the program
307 * is attached."
308 */
309 while (programs_in_use) {
310 const int stage = u_bit_scan(&programs_in_use);
311
312 struct gl_program *prog = NULL;
313 if (sh_prog->_LinkedShaders[stage])
314 prog = sh_prog->_LinkedShaders[stage]->Program;
315
316 _mesa_use_program(ctx, stage, sh_prog, prog, ctx->_Shader);
317 }
318
319 sh_prog->data->LinkStatus = LINKING_SKIPPED;
320 }