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/glsl/serialize.h"
33 #include "main/errors.h"
34 #include "main/mtypes.h"
35 #include "main/shaderapi.h"
36 #include "util/bitscan.h"
37 #include "util/blob.h"
38 #include "util/crc32.h"
39 #include "program_binary.h"
40 #include "program/prog_parameter.h"
41
42 #include "state_tracker/st_shader_cache.h"
43
44 /**
45 * Mesa supports one binary format, but it must differentiate between formats
46 * produced by different drivers and different Mesa versions.
47 *
48 * Mesa uses a uint32_t value to specify an internal format. The only format
49 * defined has one uint32_t value of 0, followed by 20 bytes specifying a sha1
50 * that uniquely identifies the Mesa driver type and version.
51 */
52
53 struct program_binary_header {
54 /* If internal_format is 0, it must be followed by the 20 byte sha1 that
55 * identifies the Mesa driver and version supported. If we want to support
56 * something besides a sha1, then a new internal_format value can be added.
57 */
58 uint32_t internal_format;
59 uint8_t sha1[20];
60 /* Fields following sha1 can be changed since the sha1 will guarantee that
61 * the binary only works with the same Mesa version.
62 */
63 uint32_t size;
64 uint32_t crc32;
65 };
66
67 /**
68 * Returns the header size needed for a binary
69 */
70 static unsigned
get_program_binary_header_size(void)71 get_program_binary_header_size(void)
72 {
73 return sizeof(struct program_binary_header);
74 }
75
76 static bool
write_program_binary(const void * payload,unsigned payload_size,const void * sha1,void * binary,unsigned binary_size,GLenum * binary_format)77 write_program_binary(const void *payload, unsigned payload_size,
78 const void *sha1, void *binary, unsigned binary_size,
79 GLenum *binary_format)
80 {
81 struct program_binary_header *hdr = binary;
82
83 if (binary_size < sizeof(*hdr))
84 return false;
85
86 /* binary_size is the size of the buffer provided by the application.
87 * Make sure our program (payload) will fit in the buffer.
88 */
89 if (payload_size > binary_size - sizeof(*hdr))
90 return false;
91
92 hdr->internal_format = 0;
93 memcpy(hdr->sha1, sha1, sizeof(hdr->sha1));
94 memcpy(hdr + 1, payload, payload_size);
95 hdr->size = payload_size;
96
97 hdr->crc32 = util_hash_crc32(hdr + 1, payload_size);
98 *binary_format = GL_PROGRAM_BINARY_FORMAT_MESA;
99
100 return true;
101 }
102
103 static bool
simple_header_checks(const struct program_binary_header * hdr,unsigned length)104 simple_header_checks(const struct program_binary_header *hdr, unsigned length)
105 {
106 if (hdr == NULL || length < sizeof(*hdr))
107 return false;
108
109 if (hdr->internal_format != 0)
110 return false;
111
112 return true;
113 }
114
115 static bool
check_crc32(const struct program_binary_header * hdr,unsigned length)116 check_crc32(const struct program_binary_header *hdr, unsigned length)
117 {
118 uint32_t crc32;
119 unsigned crc32_len;
120
121 crc32_len = hdr->size;
122 if (crc32_len > length - sizeof(*hdr))
123 return false;
124
125 crc32 = util_hash_crc32(hdr + 1, crc32_len);
126 if (hdr->crc32 != crc32)
127 return false;
128
129 return true;
130 }
131
132 static bool
is_program_binary_valid(GLenum binary_format,const void * sha1,const struct program_binary_header * hdr,unsigned length)133 is_program_binary_valid(GLenum binary_format, const void *sha1,
134 const struct program_binary_header *hdr,
135 unsigned length)
136 {
137 if (binary_format != GL_PROGRAM_BINARY_FORMAT_MESA)
138 return false;
139
140 if (!simple_header_checks(hdr, length))
141 return false;
142
143 if (memcmp(hdr->sha1, sha1, sizeof(hdr->sha1)) != 0)
144 return false;
145
146 if (!check_crc32(hdr, length))
147 return false;
148
149 return true;
150 }
151
152 /**
153 * Returns the payload within the binary.
154 *
155 * If NULL is returned, then the binary not supported. If non-NULL is
156 * returned, it will be a pointer contained within the specified `binary`
157 * buffer.
158 *
159 * This can be used to access the payload of `binary` during the
160 * glProgramBinary call.
161 */
162 static const void*
get_program_binary_payload(GLenum binary_format,const void * sha1,const void * binary,unsigned length)163 get_program_binary_payload(GLenum binary_format, const void *sha1,
164 const void *binary, unsigned length)
165 {
166 const struct program_binary_header *hdr = binary;
167 if (!is_program_binary_valid(binary_format, sha1, hdr, length))
168 return NULL;
169 return (const uint8_t*)binary + sizeof(*hdr);
170 }
171
172 static void
write_program_payload(struct gl_context * ctx,struct blob * blob,struct gl_shader_program * sh_prog)173 write_program_payload(struct gl_context *ctx, struct blob *blob,
174 struct gl_shader_program *sh_prog)
175 {
176 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
177 struct gl_linked_shader *shader = sh_prog->_LinkedShaders[stage];
178 if (shader)
179 ctx->Driver.ProgramBinarySerializeDriverBlob(ctx, sh_prog,
180 shader->Program);
181 }
182
183 blob_write_uint32(blob, sh_prog->SeparateShader);
184
185 serialize_glsl_program(blob, ctx, sh_prog);
186
187 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
188 struct gl_linked_shader *shader = sh_prog->_LinkedShaders[stage];
189 if (shader) {
190 struct gl_program *prog = sh_prog->_LinkedShaders[stage]->Program;
191 ralloc_free(prog->driver_cache_blob);
192 prog->driver_cache_blob = NULL;
193 prog->driver_cache_blob_size = 0;
194 }
195 }
196 }
197
198 static bool
read_program_payload(struct gl_context * ctx,struct blob_reader * blob,GLenum binary_format,struct gl_shader_program * sh_prog)199 read_program_payload(struct gl_context *ctx, struct blob_reader *blob,
200 GLenum binary_format, struct gl_shader_program *sh_prog)
201 {
202 sh_prog->SeparateShader = blob_read_uint32(blob);
203
204 if (!deserialize_glsl_program(blob, ctx, sh_prog))
205 return false;
206
207 unsigned int stage;
208 for (stage = 0; stage < ARRAY_SIZE(sh_prog->_LinkedShaders); stage++) {
209 struct gl_linked_shader *shader = sh_prog->_LinkedShaders[stage];
210 if (!shader)
211 continue;
212
213 ctx->Driver.ProgramBinaryDeserializeDriverBlob(ctx, sh_prog,
214 shader->Program);
215 }
216
217 return true;
218 }
219
220 void
_mesa_get_program_binary_length(struct gl_context * ctx,struct gl_shader_program * sh_prog,GLint * length)221 _mesa_get_program_binary_length(struct gl_context *ctx,
222 struct gl_shader_program *sh_prog,
223 GLint *length)
224 {
225 struct blob blob;
226 blob_init_fixed(&blob, NULL, SIZE_MAX);
227 write_program_payload(ctx, &blob, sh_prog);
228 *length = get_program_binary_header_size() + blob.size;
229 blob_finish(&blob);
230 }
231
232 void
_mesa_get_program_binary(struct gl_context * ctx,struct gl_shader_program * sh_prog,GLsizei buf_size,GLsizei * length,GLenum * binary_format,GLvoid * binary)233 _mesa_get_program_binary(struct gl_context *ctx,
234 struct gl_shader_program *sh_prog,
235 GLsizei buf_size, GLsizei *length,
236 GLenum *binary_format, GLvoid *binary)
237 {
238 struct blob blob;
239 uint8_t driver_sha1[20];
240 unsigned header_size = get_program_binary_header_size();
241
242 st_get_program_binary_driver_sha1(ctx, driver_sha1);
243
244 blob_init(&blob);
245
246 if (buf_size < header_size)
247 goto fail;
248
249 write_program_payload(ctx, &blob, sh_prog);
250 if (blob.size + header_size > buf_size ||
251 blob.out_of_memory)
252 goto fail;
253
254 bool written = write_program_binary(blob.data, blob.size, driver_sha1,
255 binary, buf_size, binary_format);
256 if (!written || blob.out_of_memory)
257 goto fail;
258
259 *length = header_size + blob.size;
260
261 blob_finish(&blob);
262 return;
263
264 fail:
265 _mesa_error(ctx, GL_INVALID_OPERATION,
266 "glGetProgramBinary(buffer too small)");
267 *length = 0;
268 blob_finish(&blob);
269 }
270
271 void
_mesa_program_binary(struct gl_context * ctx,struct gl_shader_program * sh_prog,GLenum binary_format,const GLvoid * binary,GLsizei length)272 _mesa_program_binary(struct gl_context *ctx, struct gl_shader_program *sh_prog,
273 GLenum binary_format, const GLvoid *binary,
274 GLsizei length)
275 {
276 uint8_t driver_sha1[20];
277 unsigned header_size = get_program_binary_header_size();
278
279 st_get_program_binary_driver_sha1(ctx, driver_sha1);
280
281 const void *payload = get_program_binary_payload(binary_format, driver_sha1,
282 binary, length);
283
284 if (payload == NULL) {
285 sh_prog->data->LinkStatus = LINKING_FAILURE;
286 return;
287 }
288
289 struct blob_reader blob;
290 blob_reader_init(&blob, payload, length - header_size);
291
292 unsigned programs_in_use = 0;
293 if (ctx->_Shader)
294 for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
295 if (ctx->_Shader->CurrentProgram[stage] &&
296 ctx->_Shader->CurrentProgram[stage]->Id == sh_prog->Name) {
297 programs_in_use |= 1 << stage;
298 }
299 }
300
301 if (!read_program_payload(ctx, &blob, binary_format, sh_prog)) {
302 sh_prog->data->LinkStatus = LINKING_FAILURE;
303 return;
304 }
305
306 _mesa_create_program_resource_hash(sh_prog);
307
308 /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
309 *
310 * "If LinkProgram or ProgramBinary successfully re-links a program
311 * object that is active for any shader stage, then the newly generated
312 * executable code will be installed as part of the current rendering
313 * state for all shader stages where the program is active.
314 * Additionally, the newly generated executable code is made part of
315 * the state of any program pipeline for all stages where the program
316 * is attached."
317 */
318 while (programs_in_use) {
319 const int stage = u_bit_scan(&programs_in_use);
320
321 struct gl_program *prog = NULL;
322 if (sh_prog->_LinkedShaders[stage])
323 prog = sh_prog->_LinkedShaders[stage]->Program;
324
325 _mesa_use_program(ctx, stage, sh_prog, prog, ctx->_Shader);
326 }
327
328 sh_prog->data->LinkStatus = LINKING_SKIPPED;
329 }
330