1 /*
2 * Copyright © 2014 Intel Corporation
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 /**
25 * \file serialize.cpp
26 *
27 * GLSL serialization
28 *
29 * Supports serializing and deserializing glsl programs using a blob.
30 */
31
32 #include "compiler/glsl_types.h"
33 #include "compiler/shader_info.h"
34 #include "ir_uniform.h"
35 #include "main/mtypes.h"
36 #include "main/shaderobj.h"
37 #include "program/program.h"
38 #include "string_to_uint_map.h"
39 #include "util/bitscan.h"
40
41
42 static void
write_subroutines(struct blob * metadata,struct gl_shader_program * prog)43 write_subroutines(struct blob *metadata, struct gl_shader_program *prog)
44 {
45 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
46 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
47 if (!sh)
48 continue;
49
50 struct gl_program *glprog = sh->Program;
51
52 blob_write_uint32(metadata, glprog->sh.NumSubroutineUniforms);
53 blob_write_uint32(metadata, glprog->sh.MaxSubroutineFunctionIndex);
54 blob_write_uint32(metadata, glprog->sh.NumSubroutineFunctions);
55 for (unsigned j = 0; j < glprog->sh.NumSubroutineFunctions; j++) {
56 int num_types = glprog->sh.SubroutineFunctions[j].num_compat_types;
57
58 blob_write_string(metadata, glprog->sh.SubroutineFunctions[j].name);
59 blob_write_uint32(metadata, glprog->sh.SubroutineFunctions[j].index);
60 blob_write_uint32(metadata, num_types);
61
62 for (int k = 0; k < num_types; k++) {
63 encode_type_to_blob(metadata,
64 glprog->sh.SubroutineFunctions[j].types[k]);
65 }
66 }
67 }
68 }
69
70 static void
read_subroutines(struct blob_reader * metadata,struct gl_shader_program * prog)71 read_subroutines(struct blob_reader *metadata, struct gl_shader_program *prog)
72 {
73 struct gl_subroutine_function *subs;
74
75 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
76 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
77 if (!sh)
78 continue;
79
80 struct gl_program *glprog = sh->Program;
81
82 glprog->sh.NumSubroutineUniforms = blob_read_uint32(metadata);
83 glprog->sh.MaxSubroutineFunctionIndex = blob_read_uint32(metadata);
84 glprog->sh.NumSubroutineFunctions = blob_read_uint32(metadata);
85
86 subs = rzalloc_array(prog, struct gl_subroutine_function,
87 glprog->sh.NumSubroutineFunctions);
88 glprog->sh.SubroutineFunctions = subs;
89
90 for (unsigned j = 0; j < glprog->sh.NumSubroutineFunctions; j++) {
91 subs[j].name = ralloc_strdup(prog, blob_read_string (metadata));
92 subs[j].index = (int) blob_read_uint32(metadata);
93 subs[j].num_compat_types = (int) blob_read_uint32(metadata);
94
95 subs[j].types = rzalloc_array(prog, const struct glsl_type *,
96 subs[j].num_compat_types);
97 for (int k = 0; k < subs[j].num_compat_types; k++) {
98 subs[j].types[k] = decode_type_from_blob(metadata);
99 }
100 }
101 }
102 }
103
104 static void
write_buffer_block(struct blob * metadata,struct gl_uniform_block * b)105 write_buffer_block(struct blob *metadata, struct gl_uniform_block *b)
106 {
107 blob_write_string(metadata, b->Name);
108 blob_write_uint32(metadata, b->NumUniforms);
109 blob_write_uint32(metadata, b->Binding);
110 blob_write_uint32(metadata, b->UniformBufferSize);
111 blob_write_uint32(metadata, b->stageref);
112
113 for (unsigned j = 0; j < b->NumUniforms; j++) {
114 blob_write_string(metadata, b->Uniforms[j].Name);
115 blob_write_string(metadata, b->Uniforms[j].IndexName);
116 encode_type_to_blob(metadata, b->Uniforms[j].Type);
117 blob_write_uint32(metadata, b->Uniforms[j].Offset);
118 }
119 }
120
121 static void
write_buffer_blocks(struct blob * metadata,struct gl_shader_program * prog)122 write_buffer_blocks(struct blob *metadata, struct gl_shader_program *prog)
123 {
124 blob_write_uint32(metadata, prog->data->NumUniformBlocks);
125 blob_write_uint32(metadata, prog->data->NumShaderStorageBlocks);
126
127 for (unsigned i = 0; i < prog->data->NumUniformBlocks; i++) {
128 write_buffer_block(metadata, &prog->data->UniformBlocks[i]);
129 }
130
131 for (unsigned i = 0; i < prog->data->NumShaderStorageBlocks; i++) {
132 write_buffer_block(metadata, &prog->data->ShaderStorageBlocks[i]);
133 }
134
135 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
136 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
137 if (!sh)
138 continue;
139
140 struct gl_program *glprog = sh->Program;
141
142 blob_write_uint32(metadata, glprog->info.num_ubos);
143 blob_write_uint32(metadata, glprog->info.num_ssbos);
144
145 for (unsigned j = 0; j < glprog->info.num_ubos; j++) {
146 uint32_t offset =
147 glprog->sh.UniformBlocks[j] - prog->data->UniformBlocks;
148 blob_write_uint32(metadata, offset);
149 }
150
151 for (unsigned j = 0; j < glprog->info.num_ssbos; j++) {
152 uint32_t offset = glprog->sh.ShaderStorageBlocks[j] -
153 prog->data->ShaderStorageBlocks;
154 blob_write_uint32(metadata, offset);
155 }
156 }
157 }
158
159 static void
read_buffer_block(struct blob_reader * metadata,struct gl_uniform_block * b,struct gl_shader_program * prog)160 read_buffer_block(struct blob_reader *metadata, struct gl_uniform_block *b,
161 struct gl_shader_program *prog)
162 {
163 b->Name = ralloc_strdup(prog->data, blob_read_string (metadata));
164 b->NumUniforms = blob_read_uint32(metadata);
165 b->Binding = blob_read_uint32(metadata);
166 b->UniformBufferSize = blob_read_uint32(metadata);
167 b->stageref = blob_read_uint32(metadata);
168
169 b->Uniforms =
170 rzalloc_array(prog->data, struct gl_uniform_buffer_variable,
171 b->NumUniforms);
172 for (unsigned j = 0; j < b->NumUniforms; j++) {
173 b->Uniforms[j].Name = ralloc_strdup(prog->data,
174 blob_read_string (metadata));
175
176 char *index_name = blob_read_string(metadata);
177 if (strcmp(b->Uniforms[j].Name, index_name) == 0) {
178 b->Uniforms[j].IndexName = b->Uniforms[j].Name;
179 } else {
180 b->Uniforms[j].IndexName = ralloc_strdup(prog->data, index_name);
181 }
182
183 b->Uniforms[j].Type = decode_type_from_blob(metadata);
184 b->Uniforms[j].Offset = blob_read_uint32(metadata);
185 }
186 }
187
188 static void
read_buffer_blocks(struct blob_reader * metadata,struct gl_shader_program * prog)189 read_buffer_blocks(struct blob_reader *metadata,
190 struct gl_shader_program *prog)
191 {
192 prog->data->NumUniformBlocks = blob_read_uint32(metadata);
193 prog->data->NumShaderStorageBlocks = blob_read_uint32(metadata);
194
195 prog->data->UniformBlocks =
196 rzalloc_array(prog->data, struct gl_uniform_block,
197 prog->data->NumUniformBlocks);
198
199 prog->data->ShaderStorageBlocks =
200 rzalloc_array(prog->data, struct gl_uniform_block,
201 prog->data->NumShaderStorageBlocks);
202
203 for (unsigned i = 0; i < prog->data->NumUniformBlocks; i++) {
204 read_buffer_block(metadata, &prog->data->UniformBlocks[i], prog);
205 }
206
207 for (unsigned i = 0; i < prog->data->NumShaderStorageBlocks; i++) {
208 read_buffer_block(metadata, &prog->data->ShaderStorageBlocks[i], prog);
209 }
210
211 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
212 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
213 if (!sh)
214 continue;
215
216 struct gl_program *glprog = sh->Program;
217
218 glprog->info.num_ubos = blob_read_uint32(metadata);
219 glprog->info.num_ssbos = blob_read_uint32(metadata);
220
221 glprog->sh.UniformBlocks =
222 rzalloc_array(glprog, gl_uniform_block *, glprog->info.num_ubos);
223 glprog->sh.ShaderStorageBlocks =
224 rzalloc_array(glprog, gl_uniform_block *, glprog->info.num_ssbos);
225
226 for (unsigned j = 0; j < glprog->info.num_ubos; j++) {
227 uint32_t offset = blob_read_uint32(metadata);
228 glprog->sh.UniformBlocks[j] = prog->data->UniformBlocks + offset;
229 }
230
231 for (unsigned j = 0; j < glprog->info.num_ssbos; j++) {
232 uint32_t offset = blob_read_uint32(metadata);
233 glprog->sh.ShaderStorageBlocks[j] =
234 prog->data->ShaderStorageBlocks + offset;
235 }
236 }
237 }
238
239 static void
write_atomic_buffers(struct blob * metadata,struct gl_shader_program * prog)240 write_atomic_buffers(struct blob *metadata, struct gl_shader_program *prog)
241 {
242 blob_write_uint32(metadata, prog->data->NumAtomicBuffers);
243
244 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
245 if (prog->_LinkedShaders[i]) {
246 struct gl_program *glprog = prog->_LinkedShaders[i]->Program;
247 blob_write_uint32(metadata, glprog->info.num_abos);
248 }
249 }
250
251 for (unsigned i = 0; i < prog->data->NumAtomicBuffers; i++) {
252 blob_write_uint32(metadata, prog->data->AtomicBuffers[i].Binding);
253 blob_write_uint32(metadata, prog->data->AtomicBuffers[i].MinimumSize);
254 blob_write_uint32(metadata, prog->data->AtomicBuffers[i].NumUniforms);
255
256 blob_write_bytes(metadata, prog->data->AtomicBuffers[i].StageReferences,
257 sizeof(prog->data->AtomicBuffers[i].StageReferences));
258
259 for (unsigned j = 0; j < prog->data->AtomicBuffers[i].NumUniforms; j++) {
260 blob_write_uint32(metadata, prog->data->AtomicBuffers[i].Uniforms[j]);
261 }
262 }
263 }
264
265 static void
read_atomic_buffers(struct blob_reader * metadata,struct gl_shader_program * prog)266 read_atomic_buffers(struct blob_reader *metadata,
267 struct gl_shader_program *prog)
268 {
269 prog->data->NumAtomicBuffers = blob_read_uint32(metadata);
270 prog->data->AtomicBuffers =
271 rzalloc_array(prog, gl_active_atomic_buffer,
272 prog->data->NumAtomicBuffers);
273
274 struct gl_active_atomic_buffer **stage_buff_list[MESA_SHADER_STAGES];
275 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
276 if (prog->_LinkedShaders[i]) {
277 struct gl_program *glprog = prog->_LinkedShaders[i]->Program;
278
279 glprog->info.num_abos = blob_read_uint32(metadata);
280 glprog->sh.AtomicBuffers =
281 rzalloc_array(glprog, gl_active_atomic_buffer *,
282 glprog->info.num_abos);
283 stage_buff_list[i] = glprog->sh.AtomicBuffers;
284 }
285 }
286
287 for (unsigned i = 0; i < prog->data->NumAtomicBuffers; i++) {
288 prog->data->AtomicBuffers[i].Binding = blob_read_uint32(metadata);
289 prog->data->AtomicBuffers[i].MinimumSize = blob_read_uint32(metadata);
290 prog->data->AtomicBuffers[i].NumUniforms = blob_read_uint32(metadata);
291
292 blob_copy_bytes(metadata,
293 (uint8_t *) &prog->data->AtomicBuffers[i].StageReferences,
294 sizeof(prog->data->AtomicBuffers[i].StageReferences));
295
296 prog->data->AtomicBuffers[i].Uniforms = rzalloc_array(prog, unsigned,
297 prog->data->AtomicBuffers[i].NumUniforms);
298
299 for (unsigned j = 0; j < prog->data->AtomicBuffers[i].NumUniforms; j++) {
300 prog->data->AtomicBuffers[i].Uniforms[j] = blob_read_uint32(metadata);
301 }
302
303 for (unsigned j = 0; j < MESA_SHADER_STAGES; j++) {
304 if (prog->data->AtomicBuffers[i].StageReferences[j]) {
305 *stage_buff_list[j] = &prog->data->AtomicBuffers[i];
306 stage_buff_list[j]++;
307 }
308 }
309 }
310 }
311
312 static void
write_xfb(struct blob * metadata,struct gl_shader_program * shProg)313 write_xfb(struct blob *metadata, struct gl_shader_program *shProg)
314 {
315 struct gl_program *prog = shProg->last_vert_prog;
316
317 if (!prog) {
318 blob_write_uint32(metadata, ~0u);
319 return;
320 }
321
322 struct gl_transform_feedback_info *ltf = prog->sh.LinkedTransformFeedback;
323
324 blob_write_uint32(metadata, prog->info.stage);
325
326 blob_write_uint32(metadata, ltf->NumOutputs);
327 blob_write_uint32(metadata, ltf->ActiveBuffers);
328 blob_write_uint32(metadata, ltf->NumVarying);
329
330 blob_write_bytes(metadata, ltf->Outputs,
331 sizeof(struct gl_transform_feedback_output) *
332 ltf->NumOutputs);
333
334 for (int i = 0; i < ltf->NumVarying; i++) {
335 blob_write_string(metadata, ltf->Varyings[i].Name);
336 blob_write_uint32(metadata, ltf->Varyings[i].Type);
337 blob_write_uint32(metadata, ltf->Varyings[i].BufferIndex);
338 blob_write_uint32(metadata, ltf->Varyings[i].Size);
339 blob_write_uint32(metadata, ltf->Varyings[i].Offset);
340 }
341
342 blob_write_bytes(metadata, ltf->Buffers,
343 sizeof(struct gl_transform_feedback_buffer) *
344 MAX_FEEDBACK_BUFFERS);
345 }
346
347 static void
read_xfb(struct blob_reader * metadata,struct gl_shader_program * shProg)348 read_xfb(struct blob_reader *metadata, struct gl_shader_program *shProg)
349 {
350 unsigned xfb_stage = blob_read_uint32(metadata);
351
352 if (xfb_stage == ~0u)
353 return;
354
355 struct gl_program *prog = shProg->_LinkedShaders[xfb_stage]->Program;
356 struct gl_transform_feedback_info *ltf =
357 rzalloc(prog, struct gl_transform_feedback_info);
358
359 prog->sh.LinkedTransformFeedback = ltf;
360 shProg->last_vert_prog = prog;
361
362 ltf->NumOutputs = blob_read_uint32(metadata);
363 ltf->ActiveBuffers = blob_read_uint32(metadata);
364 ltf->NumVarying = blob_read_uint32(metadata);
365
366 ltf->Outputs = rzalloc_array(prog, struct gl_transform_feedback_output,
367 ltf->NumOutputs);
368
369 blob_copy_bytes(metadata, (uint8_t *) ltf->Outputs,
370 sizeof(struct gl_transform_feedback_output) *
371 ltf->NumOutputs);
372
373 ltf->Varyings = rzalloc_array(prog,
374 struct gl_transform_feedback_varying_info,
375 ltf->NumVarying);
376
377 for (int i = 0; i < ltf->NumVarying; i++) {
378 ltf->Varyings[i].Name = ralloc_strdup(prog, blob_read_string(metadata));
379 ltf->Varyings[i].Type = blob_read_uint32(metadata);
380 ltf->Varyings[i].BufferIndex = blob_read_uint32(metadata);
381 ltf->Varyings[i].Size = blob_read_uint32(metadata);
382 ltf->Varyings[i].Offset = blob_read_uint32(metadata);
383 }
384
385 blob_copy_bytes(metadata, (uint8_t *) ltf->Buffers,
386 sizeof(struct gl_transform_feedback_buffer) *
387 MAX_FEEDBACK_BUFFERS);
388 }
389
390 static bool
has_uniform_storage(struct gl_shader_program * prog,unsigned idx)391 has_uniform_storage(struct gl_shader_program *prog, unsigned idx)
392 {
393 if (!prog->data->UniformStorage[idx].builtin &&
394 !prog->data->UniformStorage[idx].is_shader_storage &&
395 prog->data->UniformStorage[idx].block_index == -1)
396 return true;
397
398 return false;
399 }
400
401 static void
write_uniforms(struct blob * metadata,struct gl_shader_program * prog)402 write_uniforms(struct blob *metadata, struct gl_shader_program *prog)
403 {
404 blob_write_uint32(metadata, prog->SamplersValidated);
405 blob_write_uint32(metadata, prog->data->NumUniformStorage);
406 blob_write_uint32(metadata, prog->data->NumUniformDataSlots);
407
408 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
409 encode_type_to_blob(metadata, prog->data->UniformStorage[i].type);
410 blob_write_uint32(metadata, prog->data->UniformStorage[i].array_elements);
411 blob_write_string(metadata, prog->data->UniformStorage[i].name);
412 blob_write_uint32(metadata, prog->data->UniformStorage[i].builtin);
413 blob_write_uint32(metadata, prog->data->UniformStorage[i].remap_location);
414 blob_write_uint32(metadata, prog->data->UniformStorage[i].block_index);
415 blob_write_uint32(metadata, prog->data->UniformStorage[i].atomic_buffer_index);
416 blob_write_uint32(metadata, prog->data->UniformStorage[i].offset);
417 blob_write_uint32(metadata, prog->data->UniformStorage[i].array_stride);
418 blob_write_uint32(metadata, prog->data->UniformStorage[i].hidden);
419 blob_write_uint32(metadata, prog->data->UniformStorage[i].is_shader_storage);
420 blob_write_uint32(metadata, prog->data->UniformStorage[i].active_shader_mask);
421 blob_write_uint32(metadata, prog->data->UniformStorage[i].matrix_stride);
422 blob_write_uint32(metadata, prog->data->UniformStorage[i].row_major);
423 blob_write_uint32(metadata, prog->data->UniformStorage[i].is_bindless);
424 blob_write_uint32(metadata,
425 prog->data->UniformStorage[i].num_compatible_subroutines);
426 blob_write_uint32(metadata,
427 prog->data->UniformStorage[i].top_level_array_size);
428 blob_write_uint32(metadata,
429 prog->data->UniformStorage[i].top_level_array_stride);
430
431 if (has_uniform_storage(prog, i)) {
432 blob_write_uint32(metadata, prog->data->UniformStorage[i].storage -
433 prog->data->UniformDataSlots);
434 }
435
436 blob_write_bytes(metadata, prog->data->UniformStorage[i].opaque,
437 sizeof(prog->data->UniformStorage[i].opaque));
438 }
439
440 /* Here we cache all uniform values. We do this to retain values for
441 * uniforms with initialisers and also hidden uniforms that may be lowered
442 * constant arrays. We could possibly just store the values we need but for
443 * now we just store everything.
444 */
445 blob_write_uint32(metadata, prog->data->NumHiddenUniforms);
446 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
447 if (has_uniform_storage(prog, i)) {
448 unsigned vec_size =
449 prog->data->UniformStorage[i].type->component_slots() *
450 MAX2(prog->data->UniformStorage[i].array_elements, 1);
451 unsigned slot =
452 prog->data->UniformStorage[i].storage -
453 prog->data->UniformDataSlots;
454 blob_write_bytes(metadata, &prog->data->UniformDataDefaults[slot],
455 sizeof(union gl_constant_value) * vec_size);
456 }
457 }
458 }
459
460 static void
read_uniforms(struct blob_reader * metadata,struct gl_shader_program * prog)461 read_uniforms(struct blob_reader *metadata, struct gl_shader_program *prog)
462 {
463 struct gl_uniform_storage *uniforms;
464 union gl_constant_value *data;
465
466 prog->SamplersValidated = blob_read_uint32(metadata);
467 prog->data->NumUniformStorage = blob_read_uint32(metadata);
468 prog->data->NumUniformDataSlots = blob_read_uint32(metadata);
469
470 uniforms = rzalloc_array(prog->data, struct gl_uniform_storage,
471 prog->data->NumUniformStorage);
472 prog->data->UniformStorage = uniforms;
473
474 data = rzalloc_array(uniforms, union gl_constant_value,
475 prog->data->NumUniformDataSlots);
476 prog->data->UniformDataSlots = data;
477 prog->data->UniformDataDefaults =
478 rzalloc_array(uniforms, union gl_constant_value,
479 prog->data->NumUniformDataSlots);
480
481 prog->UniformHash = new string_to_uint_map;
482
483 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
484 uniforms[i].type = decode_type_from_blob(metadata);
485 uniforms[i].array_elements = blob_read_uint32(metadata);
486 uniforms[i].name = ralloc_strdup(prog, blob_read_string (metadata));
487 uniforms[i].builtin = blob_read_uint32(metadata);
488 uniforms[i].remap_location = blob_read_uint32(metadata);
489 uniforms[i].block_index = blob_read_uint32(metadata);
490 uniforms[i].atomic_buffer_index = blob_read_uint32(metadata);
491 uniforms[i].offset = blob_read_uint32(metadata);
492 uniforms[i].array_stride = blob_read_uint32(metadata);
493 uniforms[i].hidden = blob_read_uint32(metadata);
494 uniforms[i].is_shader_storage = blob_read_uint32(metadata);
495 uniforms[i].active_shader_mask = blob_read_uint32(metadata);
496 uniforms[i].matrix_stride = blob_read_uint32(metadata);
497 uniforms[i].row_major = blob_read_uint32(metadata);
498 uniforms[i].is_bindless = blob_read_uint32(metadata);
499 uniforms[i].num_compatible_subroutines = blob_read_uint32(metadata);
500 uniforms[i].top_level_array_size = blob_read_uint32(metadata);
501 uniforms[i].top_level_array_stride = blob_read_uint32(metadata);
502 prog->UniformHash->put(i, uniforms[i].name);
503
504 if (has_uniform_storage(prog, i)) {
505 uniforms[i].storage = data + blob_read_uint32(metadata);
506 }
507
508 memcpy(uniforms[i].opaque,
509 blob_read_bytes(metadata, sizeof(uniforms[i].opaque)),
510 sizeof(uniforms[i].opaque));
511 }
512
513 /* Restore uniform values. */
514 prog->data->NumHiddenUniforms = blob_read_uint32(metadata);
515 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
516 if (has_uniform_storage(prog, i)) {
517 unsigned vec_size =
518 prog->data->UniformStorage[i].type->component_slots() *
519 MAX2(prog->data->UniformStorage[i].array_elements, 1);
520 unsigned slot =
521 prog->data->UniformStorage[i].storage -
522 prog->data->UniformDataSlots;
523 blob_copy_bytes(metadata,
524 (uint8_t *) &prog->data->UniformDataSlots[slot],
525 sizeof(union gl_constant_value) * vec_size);
526
527 assert(vec_size + prog->data->UniformStorage[i].storage <=
528 data + prog->data->NumUniformDataSlots);
529 }
530 }
531
532 memcpy(prog->data->UniformDataDefaults, prog->data->UniformDataSlots,
533 sizeof(union gl_constant_value) * prog->data->NumUniformDataSlots);
534 }
535
536 enum uniform_remap_type
537 {
538 remap_type_inactive_explicit_location,
539 remap_type_null_ptr,
540 remap_type_uniform_offset
541 };
542
543 static void
write_uniform_remap_table_entry(struct blob * metadata,gl_uniform_storage * uniform_storage,gl_uniform_storage * entry)544 write_uniform_remap_table_entry(struct blob *metadata,
545 gl_uniform_storage *uniform_storage,
546 gl_uniform_storage *entry)
547 {
548 if (entry == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
549 blob_write_uint32(metadata, remap_type_inactive_explicit_location);
550 } else if (entry == NULL) {
551 blob_write_uint32(metadata, remap_type_null_ptr);
552 } else {
553 blob_write_uint32(metadata, remap_type_uniform_offset);
554
555 uint32_t offset = entry - uniform_storage;
556 blob_write_uint32(metadata, offset);
557 }
558 }
559
560 static void
write_uniform_remap_tables(struct blob * metadata,struct gl_shader_program * prog)561 write_uniform_remap_tables(struct blob *metadata,
562 struct gl_shader_program *prog)
563 {
564 blob_write_uint32(metadata, prog->NumUniformRemapTable);
565
566 for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
567 write_uniform_remap_table_entry(metadata, prog->data->UniformStorage,
568 prog->UniformRemapTable[i]);
569 }
570
571 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
572 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
573 if (sh) {
574 struct gl_program *glprog = sh->Program;
575 blob_write_uint32(metadata, glprog->sh.NumSubroutineUniformRemapTable);
576
577 for (unsigned j = 0; j < glprog->sh.NumSubroutineUniformRemapTable; j++) {
578 write_uniform_remap_table_entry(metadata,
579 prog->data->UniformStorage,
580 glprog->sh.SubroutineUniformRemapTable[j]);
581 }
582 }
583 }
584 }
585
586 static void
read_uniform_remap_table_entry(struct blob_reader * metadata,gl_uniform_storage * uniform_storage,gl_uniform_storage ** entry,enum uniform_remap_type type)587 read_uniform_remap_table_entry(struct blob_reader *metadata,
588 gl_uniform_storage *uniform_storage,
589 gl_uniform_storage **entry,
590 enum uniform_remap_type type)
591 {
592 if (type == remap_type_inactive_explicit_location) {
593 *entry = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
594 } else if (type == remap_type_null_ptr) {
595 *entry = NULL;
596 } else {
597 uint32_t uni_offset = blob_read_uint32(metadata);
598 *entry = uniform_storage + uni_offset;
599 }
600 }
601
602 static void
read_uniform_remap_tables(struct blob_reader * metadata,struct gl_shader_program * prog)603 read_uniform_remap_tables(struct blob_reader *metadata,
604 struct gl_shader_program *prog)
605 {
606 prog->NumUniformRemapTable = blob_read_uint32(metadata);
607
608 prog->UniformRemapTable = rzalloc_array(prog, struct gl_uniform_storage *,
609 prog->NumUniformRemapTable);
610
611 for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
612 enum uniform_remap_type type =
613 (enum uniform_remap_type) blob_read_uint32(metadata);
614
615 read_uniform_remap_table_entry(metadata, prog->data->UniformStorage,
616 &prog->UniformRemapTable[i], type);
617 }
618
619 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
620 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
621 if (sh) {
622 struct gl_program *glprog = sh->Program;
623 glprog->sh.NumSubroutineUniformRemapTable = blob_read_uint32(metadata);
624
625 glprog->sh.SubroutineUniformRemapTable =
626 rzalloc_array(glprog, struct gl_uniform_storage *,
627 glprog->sh.NumSubroutineUniformRemapTable);
628
629 for (unsigned j = 0; j < glprog->sh.NumSubroutineUniformRemapTable; j++) {
630 enum uniform_remap_type type =
631 (enum uniform_remap_type) blob_read_uint32(metadata);
632
633 read_uniform_remap_table_entry(metadata,
634 prog->data->UniformStorage,
635 &glprog->sh.SubroutineUniformRemapTable[j],
636 type);
637 }
638 }
639 }
640 }
641
642 struct whte_closure
643 {
644 struct blob *blob;
645 size_t num_entries;
646 };
647
648 static void
write_hash_table_entry(const char * key,unsigned value,void * closure)649 write_hash_table_entry(const char *key, unsigned value, void *closure)
650 {
651 struct whte_closure *whte = (struct whte_closure *) closure;
652
653 blob_write_string(whte->blob, key);
654 blob_write_uint32(whte->blob, value);
655
656 whte->num_entries++;
657 }
658
659 static void
write_hash_table(struct blob * metadata,struct string_to_uint_map * hash)660 write_hash_table(struct blob *metadata, struct string_to_uint_map *hash)
661 {
662 size_t offset;
663 struct whte_closure whte;
664
665 whte.blob = metadata;
666 whte.num_entries = 0;
667
668 offset = metadata->size;
669
670 /* Write a placeholder for the hashtable size. */
671 blob_write_uint32 (metadata, 0);
672
673 hash->iterate(write_hash_table_entry, &whte);
674
675 /* Overwrite with the computed number of entries written. */
676 blob_overwrite_uint32 (metadata, offset, whte.num_entries);
677 }
678
679 static void
read_hash_table(struct blob_reader * metadata,struct string_to_uint_map * hash)680 read_hash_table(struct blob_reader *metadata, struct string_to_uint_map *hash)
681 {
682 size_t i, num_entries;
683 const char *key;
684 uint32_t value;
685
686 num_entries = blob_read_uint32 (metadata);
687
688 for (i = 0; i < num_entries; i++) {
689 key = blob_read_string(metadata);
690 value = blob_read_uint32(metadata);
691
692 hash->put(value, key);
693 }
694 }
695
696 static void
write_hash_tables(struct blob * metadata,struct gl_shader_program * prog)697 write_hash_tables(struct blob *metadata, struct gl_shader_program *prog)
698 {
699 write_hash_table(metadata, prog->AttributeBindings);
700 write_hash_table(metadata, prog->FragDataBindings);
701 write_hash_table(metadata, prog->FragDataIndexBindings);
702 }
703
704 static void
read_hash_tables(struct blob_reader * metadata,struct gl_shader_program * prog)705 read_hash_tables(struct blob_reader *metadata, struct gl_shader_program *prog)
706 {
707 read_hash_table(metadata, prog->AttributeBindings);
708 read_hash_table(metadata, prog->FragDataBindings);
709 read_hash_table(metadata, prog->FragDataIndexBindings);
710 }
711
712 static void
write_shader_subroutine_index(struct blob * metadata,struct gl_linked_shader * sh,struct gl_program_resource * res)713 write_shader_subroutine_index(struct blob *metadata,
714 struct gl_linked_shader *sh,
715 struct gl_program_resource *res)
716 {
717 assert(sh);
718
719 for (unsigned j = 0; j < sh->Program->sh.NumSubroutineFunctions; j++) {
720 if (strcmp(((gl_subroutine_function *)res->Data)->name,
721 sh->Program->sh.SubroutineFunctions[j].name) == 0) {
722 blob_write_uint32(metadata, j);
723 break;
724 }
725 }
726 }
727
728 static void
get_shader_var_and_pointer_sizes(size_t * s_var_size,size_t * s_var_ptrs,const gl_shader_variable * var)729 get_shader_var_and_pointer_sizes(size_t *s_var_size, size_t *s_var_ptrs,
730 const gl_shader_variable *var)
731 {
732 *s_var_size = sizeof(gl_shader_variable);
733 *s_var_ptrs =
734 sizeof(var->type) +
735 sizeof(var->interface_type) +
736 sizeof(var->outermost_struct_type) +
737 sizeof(var->name);
738 }
739
740 static void
write_program_resource_data(struct blob * metadata,struct gl_shader_program * prog,struct gl_program_resource * res)741 write_program_resource_data(struct blob *metadata,
742 struct gl_shader_program *prog,
743 struct gl_program_resource *res)
744 {
745 struct gl_linked_shader *sh;
746
747 switch(res->Type) {
748 case GL_PROGRAM_INPUT:
749 case GL_PROGRAM_OUTPUT: {
750 const gl_shader_variable *var = (gl_shader_variable *)res->Data;
751
752 encode_type_to_blob(metadata, var->type);
753 encode_type_to_blob(metadata, var->interface_type);
754 encode_type_to_blob(metadata, var->outermost_struct_type);
755
756 blob_write_string(metadata, var->name);
757
758 size_t s_var_size, s_var_ptrs;
759 get_shader_var_and_pointer_sizes(&s_var_size, &s_var_ptrs, var);
760
761 /* Write gl_shader_variable skipping over the pointers */
762 blob_write_bytes(metadata, ((char *)var) + s_var_ptrs,
763 s_var_size - s_var_ptrs);
764 break;
765 }
766 case GL_UNIFORM_BLOCK:
767 for (unsigned i = 0; i < prog->data->NumUniformBlocks; i++) {
768 if (strcmp(((gl_uniform_block *)res->Data)->Name,
769 prog->data->UniformBlocks[i].Name) == 0) {
770 blob_write_uint32(metadata, i);
771 break;
772 }
773 }
774 break;
775 case GL_SHADER_STORAGE_BLOCK:
776 for (unsigned i = 0; i < prog->data->NumShaderStorageBlocks; i++) {
777 if (strcmp(((gl_uniform_block *)res->Data)->Name,
778 prog->data->ShaderStorageBlocks[i].Name) == 0) {
779 blob_write_uint32(metadata, i);
780 break;
781 }
782 }
783 break;
784 case GL_BUFFER_VARIABLE:
785 case GL_VERTEX_SUBROUTINE_UNIFORM:
786 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
787 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
788 case GL_COMPUTE_SUBROUTINE_UNIFORM:
789 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
790 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
791 case GL_UNIFORM:
792 for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
793 if (strcmp(((gl_uniform_storage *)res->Data)->name,
794 prog->data->UniformStorage[i].name) == 0) {
795 blob_write_uint32(metadata, i);
796 break;
797 }
798 }
799 break;
800 case GL_ATOMIC_COUNTER_BUFFER:
801 for (unsigned i = 0; i < prog->data->NumAtomicBuffers; i++) {
802 if (((gl_active_atomic_buffer *)res->Data)->Binding ==
803 prog->data->AtomicBuffers[i].Binding) {
804 blob_write_uint32(metadata, i);
805 break;
806 }
807 }
808 break;
809 case GL_TRANSFORM_FEEDBACK_BUFFER:
810 for (unsigned i = 0; i < MAX_FEEDBACK_BUFFERS; i++) {
811 if (((gl_transform_feedback_buffer *)res->Data)->Binding ==
812 prog->last_vert_prog->sh.LinkedTransformFeedback->Buffers[i].Binding) {
813 blob_write_uint32(metadata, i);
814 break;
815 }
816 }
817 break;
818 case GL_TRANSFORM_FEEDBACK_VARYING:
819 for (int i = 0; i < prog->last_vert_prog->sh.LinkedTransformFeedback->NumVarying; i++) {
820 if (strcmp(((gl_transform_feedback_varying_info *)res->Data)->Name,
821 prog->last_vert_prog->sh.LinkedTransformFeedback->Varyings[i].Name) == 0) {
822 blob_write_uint32(metadata, i);
823 break;
824 }
825 }
826 break;
827 case GL_VERTEX_SUBROUTINE:
828 case GL_TESS_CONTROL_SUBROUTINE:
829 case GL_TESS_EVALUATION_SUBROUTINE:
830 case GL_GEOMETRY_SUBROUTINE:
831 case GL_FRAGMENT_SUBROUTINE:
832 case GL_COMPUTE_SUBROUTINE:
833 sh =
834 prog->_LinkedShaders[_mesa_shader_stage_from_subroutine(res->Type)];
835 write_shader_subroutine_index(metadata, sh, res);
836 break;
837 default:
838 assert(!"Support for writing resource not yet implemented.");
839 }
840 }
841
842 static void
read_program_resource_data(struct blob_reader * metadata,struct gl_shader_program * prog,struct gl_program_resource * res)843 read_program_resource_data(struct blob_reader *metadata,
844 struct gl_shader_program *prog,
845 struct gl_program_resource *res)
846 {
847 struct gl_linked_shader *sh;
848
849 switch(res->Type) {
850 case GL_PROGRAM_INPUT:
851 case GL_PROGRAM_OUTPUT: {
852 gl_shader_variable *var = ralloc(prog, struct gl_shader_variable);
853
854 var->type = decode_type_from_blob(metadata);
855 var->interface_type = decode_type_from_blob(metadata);
856 var->outermost_struct_type = decode_type_from_blob(metadata);
857
858 var->name = ralloc_strdup(prog, blob_read_string(metadata));
859
860 size_t s_var_size, s_var_ptrs;
861 get_shader_var_and_pointer_sizes(&s_var_size, &s_var_ptrs, var);
862
863 blob_copy_bytes(metadata, ((uint8_t *) var) + s_var_ptrs,
864 s_var_size - s_var_ptrs);
865
866 res->Data = var;
867 break;
868 }
869 case GL_UNIFORM_BLOCK:
870 res->Data = &prog->data->UniformBlocks[blob_read_uint32(metadata)];
871 break;
872 case GL_SHADER_STORAGE_BLOCK:
873 res->Data = &prog->data->ShaderStorageBlocks[blob_read_uint32(metadata)];
874 break;
875 case GL_BUFFER_VARIABLE:
876 case GL_VERTEX_SUBROUTINE_UNIFORM:
877 case GL_GEOMETRY_SUBROUTINE_UNIFORM:
878 case GL_FRAGMENT_SUBROUTINE_UNIFORM:
879 case GL_COMPUTE_SUBROUTINE_UNIFORM:
880 case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
881 case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
882 case GL_UNIFORM:
883 res->Data = &prog->data->UniformStorage[blob_read_uint32(metadata)];
884 break;
885 case GL_ATOMIC_COUNTER_BUFFER:
886 res->Data = &prog->data->AtomicBuffers[blob_read_uint32(metadata)];
887 break;
888 case GL_TRANSFORM_FEEDBACK_BUFFER:
889 res->Data = &prog->last_vert_prog->
890 sh.LinkedTransformFeedback->Buffers[blob_read_uint32(metadata)];
891 break;
892 case GL_TRANSFORM_FEEDBACK_VARYING:
893 res->Data = &prog->last_vert_prog->
894 sh.LinkedTransformFeedback->Varyings[blob_read_uint32(metadata)];
895 break;
896 case GL_VERTEX_SUBROUTINE:
897 case GL_TESS_CONTROL_SUBROUTINE:
898 case GL_TESS_EVALUATION_SUBROUTINE:
899 case GL_GEOMETRY_SUBROUTINE:
900 case GL_FRAGMENT_SUBROUTINE:
901 case GL_COMPUTE_SUBROUTINE:
902 sh =
903 prog->_LinkedShaders[_mesa_shader_stage_from_subroutine(res->Type)];
904 res->Data =
905 &sh->Program->sh.SubroutineFunctions[blob_read_uint32(metadata)];
906 break;
907 default:
908 assert(!"Support for reading resource not yet implemented.");
909 }
910 }
911
912 static void
write_program_resource_list(struct blob * metadata,struct gl_shader_program * prog)913 write_program_resource_list(struct blob *metadata,
914 struct gl_shader_program *prog)
915 {
916 blob_write_uint32(metadata, prog->data->NumProgramResourceList);
917
918 for (unsigned i = 0; i < prog->data->NumProgramResourceList; i++) {
919 blob_write_uint32(metadata, prog->data->ProgramResourceList[i].Type);
920 write_program_resource_data(metadata, prog,
921 &prog->data->ProgramResourceList[i]);
922 blob_write_bytes(metadata,
923 &prog->data->ProgramResourceList[i].StageReferences,
924 sizeof(prog->data->ProgramResourceList[i].StageReferences));
925 }
926 }
927
928 static void
read_program_resource_list(struct blob_reader * metadata,struct gl_shader_program * prog)929 read_program_resource_list(struct blob_reader *metadata,
930 struct gl_shader_program *prog)
931 {
932 prog->data->NumProgramResourceList = blob_read_uint32(metadata);
933
934 prog->data->ProgramResourceList =
935 ralloc_array(prog->data, gl_program_resource,
936 prog->data->NumProgramResourceList);
937
938 for (unsigned i = 0; i < prog->data->NumProgramResourceList; i++) {
939 prog->data->ProgramResourceList[i].Type = blob_read_uint32(metadata);
940 read_program_resource_data(metadata, prog,
941 &prog->data->ProgramResourceList[i]);
942 blob_copy_bytes(metadata,
943 (uint8_t *) &prog->data->ProgramResourceList[i].StageReferences,
944 sizeof(prog->data->ProgramResourceList[i].StageReferences));
945 }
946 }
947
948 static void
write_shader_parameters(struct blob * metadata,struct gl_program_parameter_list * params)949 write_shader_parameters(struct blob *metadata,
950 struct gl_program_parameter_list *params)
951 {
952 blob_write_uint32(metadata, params->NumParameters);
953 uint32_t i = 0;
954
955 while (i < params->NumParameters) {
956 struct gl_program_parameter *param = ¶ms->Parameters[i];
957
958 blob_write_uint32(metadata, param->Type);
959 blob_write_string(metadata, param->Name);
960 blob_write_uint32(metadata, param->Size);
961 blob_write_uint32(metadata, param->DataType);
962 blob_write_bytes(metadata, param->StateIndexes,
963 sizeof(param->StateIndexes));
964
965 i++;
966 }
967
968 blob_write_bytes(metadata, params->ParameterValues,
969 sizeof(gl_constant_value) * 4 * params->NumParameters);
970
971 blob_write_uint32(metadata, params->StateFlags);
972 }
973
974 static void
read_shader_parameters(struct blob_reader * metadata,struct gl_program_parameter_list * params)975 read_shader_parameters(struct blob_reader *metadata,
976 struct gl_program_parameter_list *params)
977 {
978 gl_state_index state_indexes[STATE_LENGTH];
979 uint32_t i = 0;
980 uint32_t num_parameters = blob_read_uint32(metadata);
981
982 _mesa_reserve_parameter_storage(params, num_parameters);
983 while (i < num_parameters) {
984 gl_register_file type = (gl_register_file) blob_read_uint32(metadata);
985 const char *name = blob_read_string(metadata);
986 unsigned size = blob_read_uint32(metadata);
987 unsigned data_type = blob_read_uint32(metadata);
988 blob_copy_bytes(metadata, (uint8_t *) state_indexes,
989 sizeof(state_indexes));
990
991 _mesa_add_parameter(params, type, name, size, data_type,
992 NULL, state_indexes);
993
994 i++;
995 }
996
997 blob_copy_bytes(metadata, (uint8_t *) params->ParameterValues,
998 sizeof(gl_constant_value) * 4 * params->NumParameters);
999
1000 params->StateFlags = blob_read_uint32(metadata);
1001 }
1002
1003 static void
write_shader_metadata(struct blob * metadata,gl_linked_shader * shader)1004 write_shader_metadata(struct blob *metadata, gl_linked_shader *shader)
1005 {
1006 assert(shader->Program);
1007 struct gl_program *glprog = shader->Program;
1008 unsigned i;
1009
1010 blob_write_bytes(metadata, glprog->TexturesUsed,
1011 sizeof(glprog->TexturesUsed));
1012 blob_write_uint64(metadata, glprog->SamplersUsed);
1013
1014 blob_write_bytes(metadata, glprog->SamplerUnits,
1015 sizeof(glprog->SamplerUnits));
1016 blob_write_bytes(metadata, glprog->sh.SamplerTargets,
1017 sizeof(glprog->sh.SamplerTargets));
1018 blob_write_uint32(metadata, glprog->ShadowSamplers);
1019
1020 blob_write_bytes(metadata, glprog->sh.ImageAccess,
1021 sizeof(glprog->sh.ImageAccess));
1022 blob_write_bytes(metadata, glprog->sh.ImageUnits,
1023 sizeof(glprog->sh.ImageUnits));
1024
1025 size_t ptr_size = sizeof(GLvoid *);
1026
1027 blob_write_uint32(metadata, glprog->sh.NumBindlessSamplers);
1028 blob_write_uint32(metadata, glprog->sh.HasBoundBindlessSampler);
1029 for (i = 0; i < glprog->sh.NumBindlessSamplers; i++) {
1030 blob_write_bytes(metadata, &glprog->sh.BindlessSamplers[i],
1031 sizeof(struct gl_bindless_sampler) - ptr_size);
1032 }
1033
1034 blob_write_uint32(metadata, glprog->sh.NumBindlessImages);
1035 blob_write_uint32(metadata, glprog->sh.HasBoundBindlessImage);
1036 for (i = 0; i < glprog->sh.NumBindlessImages; i++) {
1037 blob_write_bytes(metadata, &glprog->sh.BindlessImages[i],
1038 sizeof(struct gl_bindless_image) - ptr_size);
1039 }
1040
1041 blob_write_bytes(metadata, &glprog->sh.fs.BlendSupport,
1042 sizeof(glprog->sh.fs.BlendSupport));
1043
1044 write_shader_parameters(metadata, glprog->Parameters);
1045
1046 assert((glprog->driver_cache_blob == NULL) ==
1047 (glprog->driver_cache_blob_size == 0));
1048 blob_write_uint32(metadata, (uint32_t)glprog->driver_cache_blob_size);
1049 if (glprog->driver_cache_blob_size > 0) {
1050 blob_write_bytes(metadata, glprog->driver_cache_blob,
1051 glprog->driver_cache_blob_size);
1052 }
1053 }
1054
1055 static void
read_shader_metadata(struct blob_reader * metadata,struct gl_program * glprog,gl_linked_shader * linked)1056 read_shader_metadata(struct blob_reader *metadata,
1057 struct gl_program *glprog,
1058 gl_linked_shader *linked)
1059 {
1060 unsigned i;
1061
1062 blob_copy_bytes(metadata, (uint8_t *) glprog->TexturesUsed,
1063 sizeof(glprog->TexturesUsed));
1064 glprog->SamplersUsed = blob_read_uint64(metadata);
1065
1066 blob_copy_bytes(metadata, (uint8_t *) glprog->SamplerUnits,
1067 sizeof(glprog->SamplerUnits));
1068 blob_copy_bytes(metadata, (uint8_t *) glprog->sh.SamplerTargets,
1069 sizeof(glprog->sh.SamplerTargets));
1070 glprog->ShadowSamplers = blob_read_uint32(metadata);
1071
1072 blob_copy_bytes(metadata, (uint8_t *) glprog->sh.ImageAccess,
1073 sizeof(glprog->sh.ImageAccess));
1074 blob_copy_bytes(metadata, (uint8_t *) glprog->sh.ImageUnits,
1075 sizeof(glprog->sh.ImageUnits));
1076
1077 size_t ptr_size = sizeof(GLvoid *);
1078
1079 glprog->sh.NumBindlessSamplers = blob_read_uint32(metadata);
1080 glprog->sh.HasBoundBindlessSampler = blob_read_uint32(metadata);
1081 if (glprog->sh.NumBindlessSamplers > 0) {
1082 glprog->sh.BindlessSamplers =
1083 rzalloc_array(glprog, gl_bindless_sampler,
1084 glprog->sh.NumBindlessSamplers);
1085
1086 for (i = 0; i < glprog->sh.NumBindlessSamplers; i++) {
1087 blob_copy_bytes(metadata, (uint8_t *) &glprog->sh.BindlessSamplers[i],
1088 sizeof(struct gl_bindless_sampler) - ptr_size);
1089 }
1090 }
1091
1092 glprog->sh.NumBindlessImages = blob_read_uint32(metadata);
1093 glprog->sh.HasBoundBindlessImage = blob_read_uint32(metadata);
1094 if (glprog->sh.NumBindlessImages > 0) {
1095 glprog->sh.BindlessImages =
1096 rzalloc_array(glprog, gl_bindless_image,
1097 glprog->sh.NumBindlessImages);
1098
1099 for (i = 0; i < glprog->sh.NumBindlessImages; i++) {
1100 blob_copy_bytes(metadata, (uint8_t *) &glprog->sh.BindlessImages[i],
1101 sizeof(struct gl_bindless_image) - ptr_size);
1102 }
1103 }
1104
1105 blob_copy_bytes(metadata, (uint8_t *) &glprog->sh.fs.BlendSupport,
1106 sizeof(glprog->sh.fs.BlendSupport));
1107
1108 glprog->Parameters = _mesa_new_parameter_list();
1109 read_shader_parameters(metadata, glprog->Parameters);
1110
1111 glprog->driver_cache_blob_size = (size_t)blob_read_uint32(metadata);
1112 if (glprog->driver_cache_blob_size > 0) {
1113 glprog->driver_cache_blob =
1114 (uint8_t*)ralloc_size(glprog, glprog->driver_cache_blob_size);
1115 blob_copy_bytes(metadata, glprog->driver_cache_blob,
1116 glprog->driver_cache_blob_size);
1117 }
1118 }
1119
1120 static void
get_shader_info_and_pointer_sizes(size_t * s_info_size,size_t * s_info_ptrs,shader_info * info)1121 get_shader_info_and_pointer_sizes(size_t *s_info_size, size_t *s_info_ptrs,
1122 shader_info *info)
1123 {
1124 *s_info_size = sizeof(shader_info);
1125 *s_info_ptrs = sizeof(info->name) + sizeof(info->label);
1126 }
1127
1128 static void
create_linked_shader_and_program(struct gl_context * ctx,gl_shader_stage stage,struct gl_shader_program * prog,struct blob_reader * metadata)1129 create_linked_shader_and_program(struct gl_context *ctx,
1130 gl_shader_stage stage,
1131 struct gl_shader_program *prog,
1132 struct blob_reader *metadata)
1133 {
1134 struct gl_program *glprog;
1135
1136 struct gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
1137 linked->Stage = stage;
1138
1139 glprog = ctx->Driver.NewProgram(ctx, _mesa_shader_stage_to_program(stage),
1140 prog->Name, false);
1141 glprog->info.stage = stage;
1142 linked->Program = glprog;
1143
1144 read_shader_metadata(metadata, glprog, linked);
1145
1146 glprog->info.name = ralloc_strdup(glprog, blob_read_string(metadata));
1147 glprog->info.label = ralloc_strdup(glprog, blob_read_string(metadata));
1148
1149 size_t s_info_size, s_info_ptrs;
1150 get_shader_info_and_pointer_sizes(&s_info_size, &s_info_ptrs,
1151 &glprog->info);
1152
1153 /* Restore shader info */
1154 blob_copy_bytes(metadata, ((uint8_t *) &glprog->info) + s_info_ptrs,
1155 s_info_size - s_info_ptrs);
1156
1157 _mesa_reference_shader_program_data(ctx, &glprog->sh.data, prog->data);
1158 _mesa_reference_program(ctx, &linked->Program, glprog);
1159 prog->_LinkedShaders[stage] = linked;
1160 }
1161
1162 extern "C" void
serialize_glsl_program(struct blob * blob,struct gl_context * ctx,struct gl_shader_program * prog)1163 serialize_glsl_program(struct blob *blob, struct gl_context *ctx,
1164 struct gl_shader_program *prog)
1165 {
1166 write_uniforms(blob, prog);
1167
1168 write_hash_tables(blob, prog);
1169
1170 blob_write_uint32(blob, prog->data->Version);
1171 blob_write_uint32(blob, prog->data->linked_stages);
1172
1173 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1174 struct gl_linked_shader *sh = prog->_LinkedShaders[i];
1175 if (sh) {
1176 write_shader_metadata(blob, sh);
1177
1178 if (sh->Program->info.name)
1179 blob_write_string(blob, sh->Program->info.name);
1180 else
1181 blob_write_string(blob, "");
1182
1183 if (sh->Program->info.label)
1184 blob_write_string(blob, sh->Program->info.label);
1185 else
1186 blob_write_string(blob, "");
1187
1188 size_t s_info_size, s_info_ptrs;
1189 get_shader_info_and_pointer_sizes(&s_info_size, &s_info_ptrs,
1190 &sh->Program->info);
1191
1192 /* Store shader info */
1193 blob_write_bytes(blob,
1194 ((char *) &sh->Program->info) + s_info_ptrs,
1195 s_info_size - s_info_ptrs);
1196 }
1197 }
1198
1199 write_xfb(blob, prog);
1200
1201 write_uniform_remap_tables(blob, prog);
1202
1203 write_atomic_buffers(blob, prog);
1204
1205 write_buffer_blocks(blob, prog);
1206
1207 write_subroutines(blob, prog);
1208
1209 write_program_resource_list(blob, prog);
1210 }
1211
1212 extern "C" bool
deserialize_glsl_program(struct blob_reader * blob,struct gl_context * ctx,struct gl_shader_program * prog)1213 deserialize_glsl_program(struct blob_reader *blob, struct gl_context *ctx,
1214 struct gl_shader_program *prog)
1215 {
1216 /* Fixed function programs generated by Mesa can't be serialized. */
1217 if (prog->Name == 0)
1218 return false;
1219
1220 assert(prog->data->UniformStorage == NULL);
1221
1222 read_uniforms(blob, prog);
1223
1224 read_hash_tables(blob, prog);
1225
1226 prog->data->Version = blob_read_uint32(blob);
1227 prog->data->linked_stages = blob_read_uint32(blob);
1228
1229 unsigned mask = prog->data->linked_stages;
1230 while (mask) {
1231 const int j = u_bit_scan(&mask);
1232 create_linked_shader_and_program(ctx, (gl_shader_stage) j, prog,
1233 blob);
1234 }
1235
1236 read_xfb(blob, prog);
1237
1238 read_uniform_remap_tables(blob, prog);
1239
1240 read_atomic_buffers(blob, prog);
1241
1242 read_buffer_blocks(blob, prog);
1243
1244 read_subroutines(blob, prog);
1245
1246 read_program_resource_list(blob, prog);
1247
1248 return !blob->overrun;
1249 }
1250