• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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->sh.NumUniformBlocks);
143       blob_write_uint32(metadata, glprog->info.num_ssbos);
144 
145       for (unsigned j = 0; j < glprog->sh.NumUniformBlocks; 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->sh.NumUniformBlocks = 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->sh.NumUniformBlocks);
223       glprog->sh.ShaderStorageBlocks =
224          rzalloc_array(glprog, gl_uniform_block *, glprog->info.num_ssbos);
225 
226       for (unsigned j = 0; j < glprog->sh.NumUniformBlocks; 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    /* Data set by glTransformFeedbackVaryings. */
327    blob_write_uint32(metadata, shProg->TransformFeedback.BufferMode);
328    blob_write_bytes(metadata, shProg->TransformFeedback.BufferStride,
329                     sizeof(shProg->TransformFeedback.BufferStride));
330    blob_write_uint32(metadata, shProg->TransformFeedback.NumVarying);
331    for (unsigned i = 0; i < shProg->TransformFeedback.NumVarying; i++)
332       blob_write_string(metadata, shProg->TransformFeedback.VaryingNames[i]);
333 
334    blob_write_uint32(metadata, ltf->NumOutputs);
335    blob_write_uint32(metadata, ltf->ActiveBuffers);
336    blob_write_uint32(metadata, ltf->NumVarying);
337 
338    blob_write_bytes(metadata, ltf->Outputs,
339                     sizeof(struct gl_transform_feedback_output) *
340                        ltf->NumOutputs);
341 
342    for (int i = 0; i < ltf->NumVarying; i++) {
343       blob_write_string(metadata, ltf->Varyings[i].Name);
344       blob_write_uint32(metadata, ltf->Varyings[i].Type);
345       blob_write_uint32(metadata, ltf->Varyings[i].BufferIndex);
346       blob_write_uint32(metadata, ltf->Varyings[i].Size);
347       blob_write_uint32(metadata, ltf->Varyings[i].Offset);
348    }
349 
350    blob_write_bytes(metadata, ltf->Buffers,
351                     sizeof(struct gl_transform_feedback_buffer) *
352                        MAX_FEEDBACK_BUFFERS);
353 }
354 
355 static void
read_xfb(struct blob_reader * metadata,struct gl_shader_program * shProg)356 read_xfb(struct blob_reader *metadata, struct gl_shader_program *shProg)
357 {
358    unsigned xfb_stage = blob_read_uint32(metadata);
359 
360    if (xfb_stage == ~0u)
361       return;
362 
363    if (shProg->TransformFeedback.VaryingNames)  {
364       for (unsigned i = 0; i < shProg->TransformFeedback.NumVarying; ++i)
365          free(shProg->TransformFeedback.VaryingNames[i]);
366    }
367 
368    /* Data set by glTransformFeedbackVaryings. */
369    shProg->TransformFeedback.BufferMode = blob_read_uint32(metadata);
370    blob_copy_bytes(metadata, &shProg->TransformFeedback.BufferStride,
371                    sizeof(shProg->TransformFeedback.BufferStride));
372    shProg->TransformFeedback.NumVarying = blob_read_uint32(metadata);
373 
374    shProg->TransformFeedback.VaryingNames = (char **)
375       realloc(shProg->TransformFeedback.VaryingNames,
376              shProg->TransformFeedback.NumVarying * sizeof(GLchar *));
377    /* Note, malloc used with VaryingNames. */
378    for (unsigned i = 0; i < shProg->TransformFeedback.NumVarying; i++)
379       shProg->TransformFeedback.VaryingNames[i] =
380          strdup(blob_read_string(metadata));
381 
382    struct gl_program *prog = shProg->_LinkedShaders[xfb_stage]->Program;
383    struct gl_transform_feedback_info *ltf =
384       rzalloc(prog, struct gl_transform_feedback_info);
385 
386    prog->sh.LinkedTransformFeedback = ltf;
387    shProg->last_vert_prog = prog;
388 
389    ltf->NumOutputs = blob_read_uint32(metadata);
390    ltf->ActiveBuffers = blob_read_uint32(metadata);
391    ltf->NumVarying = blob_read_uint32(metadata);
392 
393    ltf->Outputs = rzalloc_array(prog, struct gl_transform_feedback_output,
394                                 ltf->NumOutputs);
395 
396    blob_copy_bytes(metadata, (uint8_t *) ltf->Outputs,
397                    sizeof(struct gl_transform_feedback_output) *
398                       ltf->NumOutputs);
399 
400    ltf->Varyings = rzalloc_array(prog,
401                                  struct gl_transform_feedback_varying_info,
402                                  ltf->NumVarying);
403 
404    for (int i = 0; i < ltf->NumVarying; i++) {
405       ltf->Varyings[i].Name = ralloc_strdup(prog, blob_read_string(metadata));
406       ltf->Varyings[i].Type = blob_read_uint32(metadata);
407       ltf->Varyings[i].BufferIndex = blob_read_uint32(metadata);
408       ltf->Varyings[i].Size = blob_read_uint32(metadata);
409       ltf->Varyings[i].Offset = blob_read_uint32(metadata);
410    }
411 
412    blob_copy_bytes(metadata, (uint8_t *) ltf->Buffers,
413                    sizeof(struct gl_transform_feedback_buffer) *
414                       MAX_FEEDBACK_BUFFERS);
415 }
416 
417 static bool
has_uniform_storage(struct gl_shader_program * prog,unsigned idx)418 has_uniform_storage(struct gl_shader_program *prog, unsigned idx)
419 {
420    if (!prog->data->UniformStorage[idx].builtin &&
421        !prog->data->UniformStorage[idx].is_shader_storage &&
422        prog->data->UniformStorage[idx].block_index == -1)
423       return true;
424 
425    return false;
426 }
427 
428 static void
write_uniforms(struct blob * metadata,struct gl_shader_program * prog)429 write_uniforms(struct blob *metadata, struct gl_shader_program *prog)
430 {
431    blob_write_uint32(metadata, prog->SamplersValidated);
432    blob_write_uint32(metadata, prog->data->NumUniformStorage);
433    blob_write_uint32(metadata, prog->data->NumUniformDataSlots);
434 
435    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
436       encode_type_to_blob(metadata, prog->data->UniformStorage[i].type);
437       blob_write_uint32(metadata, prog->data->UniformStorage[i].array_elements);
438       if (prog->data->UniformStorage[i].name) {
439          blob_write_string(metadata, prog->data->UniformStorage[i].name);
440       } else {
441          blob_write_string(metadata, "");
442       }
443       blob_write_uint32(metadata, prog->data->UniformStorage[i].builtin);
444       blob_write_uint32(metadata, prog->data->UniformStorage[i].remap_location);
445       blob_write_uint32(metadata, prog->data->UniformStorage[i].block_index);
446       blob_write_uint32(metadata, prog->data->UniformStorage[i].atomic_buffer_index);
447       blob_write_uint32(metadata, prog->data->UniformStorage[i].offset);
448       blob_write_uint32(metadata, prog->data->UniformStorage[i].array_stride);
449       blob_write_uint32(metadata, prog->data->UniformStorage[i].hidden);
450       blob_write_uint32(metadata, prog->data->UniformStorage[i].is_shader_storage);
451       blob_write_uint32(metadata, prog->data->UniformStorage[i].active_shader_mask);
452       blob_write_uint32(metadata, prog->data->UniformStorage[i].matrix_stride);
453       blob_write_uint32(metadata, prog->data->UniformStorage[i].row_major);
454       blob_write_uint32(metadata, prog->data->UniformStorage[i].is_bindless);
455       blob_write_uint32(metadata,
456                         prog->data->UniformStorage[i].num_compatible_subroutines);
457       blob_write_uint32(metadata,
458                         prog->data->UniformStorage[i].top_level_array_size);
459       blob_write_uint32(metadata,
460                         prog->data->UniformStorage[i].top_level_array_stride);
461 
462      if (has_uniform_storage(prog, i)) {
463          blob_write_uint32(metadata, prog->data->UniformStorage[i].storage -
464                                      prog->data->UniformDataSlots);
465       }
466 
467       blob_write_bytes(metadata, prog->data->UniformStorage[i].opaque,
468                        sizeof(prog->data->UniformStorage[i].opaque));
469    }
470 
471    /* Here we cache all uniform values. We do this to retain values for
472     * uniforms with initialisers and also hidden uniforms that may be lowered
473     * constant arrays. We could possibly just store the values we need but for
474     * now we just store everything.
475     */
476    blob_write_uint32(metadata, prog->data->NumHiddenUniforms);
477    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
478       if (has_uniform_storage(prog, i)) {
479          unsigned vec_size =
480             prog->data->UniformStorage[i].type->component_slots() *
481             MAX2(prog->data->UniformStorage[i].array_elements, 1);
482          unsigned slot =
483             prog->data->UniformStorage[i].storage -
484             prog->data->UniformDataSlots;
485          blob_write_bytes(metadata, &prog->data->UniformDataDefaults[slot],
486                           sizeof(union gl_constant_value) * vec_size);
487       }
488    }
489 }
490 
491 static void
read_uniforms(struct blob_reader * metadata,struct gl_shader_program * prog)492 read_uniforms(struct blob_reader *metadata, struct gl_shader_program *prog)
493 {
494    struct gl_uniform_storage *uniforms;
495    union gl_constant_value *data;
496 
497    prog->SamplersValidated = blob_read_uint32(metadata);
498    prog->data->NumUniformStorage = blob_read_uint32(metadata);
499    prog->data->NumUniformDataSlots = blob_read_uint32(metadata);
500 
501    uniforms = rzalloc_array(prog->data, struct gl_uniform_storage,
502                             prog->data->NumUniformStorage);
503    prog->data->UniformStorage = uniforms;
504 
505    data = rzalloc_array(uniforms, union gl_constant_value,
506                         prog->data->NumUniformDataSlots);
507    prog->data->UniformDataSlots = data;
508    prog->data->UniformDataDefaults =
509       rzalloc_array(uniforms, union gl_constant_value,
510                     prog->data->NumUniformDataSlots);
511 
512    prog->UniformHash = new string_to_uint_map;
513 
514    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
515       uniforms[i].type = decode_type_from_blob(metadata);
516       uniforms[i].array_elements = blob_read_uint32(metadata);
517       uniforms[i].name = ralloc_strdup(prog, blob_read_string (metadata));
518       uniforms[i].builtin = blob_read_uint32(metadata);
519       uniforms[i].remap_location = blob_read_uint32(metadata);
520       uniforms[i].block_index = blob_read_uint32(metadata);
521       uniforms[i].atomic_buffer_index = blob_read_uint32(metadata);
522       uniforms[i].offset = blob_read_uint32(metadata);
523       uniforms[i].array_stride = blob_read_uint32(metadata);
524       uniforms[i].hidden = blob_read_uint32(metadata);
525       uniforms[i].is_shader_storage = blob_read_uint32(metadata);
526       uniforms[i].active_shader_mask = blob_read_uint32(metadata);
527       uniforms[i].matrix_stride = blob_read_uint32(metadata);
528       uniforms[i].row_major = blob_read_uint32(metadata);
529       uniforms[i].is_bindless = blob_read_uint32(metadata);
530       uniforms[i].num_compatible_subroutines = blob_read_uint32(metadata);
531       uniforms[i].top_level_array_size = blob_read_uint32(metadata);
532       uniforms[i].top_level_array_stride = blob_read_uint32(metadata);
533       prog->UniformHash->put(i, uniforms[i].name);
534 
535       if (has_uniform_storage(prog, i)) {
536          uniforms[i].storage = data + blob_read_uint32(metadata);
537       }
538 
539       memcpy(uniforms[i].opaque,
540              blob_read_bytes(metadata, sizeof(uniforms[i].opaque)),
541              sizeof(uniforms[i].opaque));
542    }
543 
544    /* Restore uniform values. */
545    prog->data->NumHiddenUniforms = blob_read_uint32(metadata);
546    for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
547       if (has_uniform_storage(prog, i)) {
548          unsigned vec_size =
549             prog->data->UniformStorage[i].type->component_slots() *
550             MAX2(prog->data->UniformStorage[i].array_elements, 1);
551          unsigned slot =
552             prog->data->UniformStorage[i].storage -
553             prog->data->UniformDataSlots;
554          blob_copy_bytes(metadata,
555                          (uint8_t *) &prog->data->UniformDataSlots[slot],
556                          sizeof(union gl_constant_value) * vec_size);
557 
558         assert(vec_size + prog->data->UniformStorage[i].storage <=
559                data +  prog->data->NumUniformDataSlots);
560       }
561    }
562 
563    memcpy(prog->data->UniformDataDefaults, prog->data->UniformDataSlots,
564           sizeof(union gl_constant_value) * prog->data->NumUniformDataSlots);
565 }
566 
567 enum uniform_remap_type
568 {
569    remap_type_inactive_explicit_location,
570    remap_type_null_ptr,
571    remap_type_uniform_offset,
572    remap_type_uniform_offsets_equal,
573 };
574 
575 static void
write_uniform_remap_table(struct blob * metadata,unsigned num_entries,gl_uniform_storage * uniform_storage,gl_uniform_storage ** remap_table)576 write_uniform_remap_table(struct blob *metadata,
577                           unsigned num_entries,
578                           gl_uniform_storage *uniform_storage,
579                           gl_uniform_storage **remap_table)
580 {
581    blob_write_uint32(metadata, num_entries);
582 
583    for (unsigned i = 0; i < num_entries; i++) {
584       gl_uniform_storage *entry = remap_table[i];
585       uint32_t offset = entry - uniform_storage;
586 
587       if (entry == INACTIVE_UNIFORM_EXPLICIT_LOCATION) {
588          blob_write_uint32(metadata, remap_type_inactive_explicit_location);
589       } else if (entry == NULL) {
590          blob_write_uint32(metadata, remap_type_null_ptr);
591       } else if (i+1 < num_entries && entry == remap_table[i+1]) {
592          blob_write_uint32(metadata, remap_type_uniform_offsets_equal);
593 
594          /* If many offsets are equal, write only one offset and the number
595           * of consecutive entries being equal.
596           */
597          unsigned count = 1;
598          for (unsigned j = i + 1; j < num_entries; j++) {
599             if (entry != remap_table[j])
600                break;
601 
602             count++;
603          }
604 
605          blob_write_uint32(metadata, offset);
606          blob_write_uint32(metadata, count);
607          i += count - 1;
608       } else {
609          blob_write_uint32(metadata, remap_type_uniform_offset);
610 
611          blob_write_uint32(metadata, offset);
612       }
613    }
614 }
615 
616 static void
write_uniform_remap_tables(struct blob * metadata,struct gl_shader_program * prog)617 write_uniform_remap_tables(struct blob *metadata,
618                            struct gl_shader_program *prog)
619 {
620    write_uniform_remap_table(metadata, prog->NumUniformRemapTable,
621                              prog->data->UniformStorage,
622                              prog->UniformRemapTable);
623 
624    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
625       struct gl_linked_shader *sh = prog->_LinkedShaders[i];
626       if (sh) {
627          write_uniform_remap_table(metadata,
628                                    sh->Program->sh.NumSubroutineUniformRemapTable,
629                                    prog->data->UniformStorage,
630                                    sh->Program->sh.SubroutineUniformRemapTable);
631       }
632    }
633 }
634 
635 static struct gl_uniform_storage **
read_uniform_remap_table(struct blob_reader * metadata,struct gl_shader_program * prog,unsigned * num_entries,gl_uniform_storage * uniform_storage)636 read_uniform_remap_table(struct blob_reader *metadata,
637                          struct gl_shader_program *prog,
638                          unsigned *num_entries,
639                          gl_uniform_storage *uniform_storage)
640 {
641    unsigned num = blob_read_uint32(metadata);
642    *num_entries = num;
643 
644    struct gl_uniform_storage **remap_table =
645       rzalloc_array(prog, struct gl_uniform_storage *, num);
646 
647    for (unsigned i = 0; i < num; i++) {
648       enum uniform_remap_type type =
649          (enum uniform_remap_type) blob_read_uint32(metadata);
650 
651       if (type == remap_type_inactive_explicit_location) {
652          remap_table[i] = INACTIVE_UNIFORM_EXPLICIT_LOCATION;
653       } else if (type == remap_type_null_ptr) {
654          remap_table[i] = NULL;
655       } else if (type == remap_type_uniform_offsets_equal) {
656          uint32_t uni_offset = blob_read_uint32(metadata);
657          uint32_t count = blob_read_uint32(metadata);
658          struct gl_uniform_storage *entry = uniform_storage + uni_offset;
659 
660          for (unsigned j = 0; j < count; j++)
661             remap_table[i+j] = entry;
662          i += count - 1;
663       } else {
664          uint32_t uni_offset = blob_read_uint32(metadata);
665          remap_table[i] = uniform_storage + uni_offset;
666       }
667    }
668    return remap_table;
669 }
670 
671 static void
read_uniform_remap_tables(struct blob_reader * metadata,struct gl_shader_program * prog)672 read_uniform_remap_tables(struct blob_reader *metadata,
673                           struct gl_shader_program *prog)
674 {
675    prog->UniformRemapTable =
676       read_uniform_remap_table(metadata, prog, &prog->NumUniformRemapTable,
677                                prog->data->UniformStorage);
678 
679    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
680       struct gl_linked_shader *sh = prog->_LinkedShaders[i];
681       if (sh) {
682          struct gl_program *glprog = sh->Program;
683 
684          glprog->sh.SubroutineUniformRemapTable =
685             read_uniform_remap_table(metadata, prog,
686                                      &glprog->sh.NumSubroutineUniformRemapTable,
687                                      prog->data->UniformStorage);
688       }
689    }
690 }
691 
692 struct whte_closure
693 {
694    struct blob *blob;
695    size_t num_entries;
696 };
697 
698 static void
write_hash_table_entry(const char * key,unsigned value,void * closure)699 write_hash_table_entry(const char *key, unsigned value, void *closure)
700 {
701    struct whte_closure *whte = (struct whte_closure *) closure;
702 
703    blob_write_string(whte->blob, key);
704    blob_write_uint32(whte->blob, value);
705 
706    whte->num_entries++;
707 }
708 
709 static void
write_hash_table(struct blob * metadata,struct string_to_uint_map * hash)710 write_hash_table(struct blob *metadata, struct string_to_uint_map *hash)
711 {
712    size_t offset;
713    struct whte_closure whte;
714 
715    whte.blob = metadata;
716    whte.num_entries = 0;
717 
718    offset = metadata->size;
719 
720    /* Write a placeholder for the hashtable size. */
721    blob_write_uint32 (metadata, 0);
722 
723    hash->iterate(write_hash_table_entry, &whte);
724 
725    /* Overwrite with the computed number of entries written. */
726    blob_overwrite_uint32 (metadata, offset, whte.num_entries);
727 }
728 
729 static void
read_hash_table(struct blob_reader * metadata,struct string_to_uint_map * hash)730 read_hash_table(struct blob_reader *metadata, struct string_to_uint_map *hash)
731 {
732    size_t i, num_entries;
733    const char *key;
734    uint32_t value;
735 
736    num_entries = blob_read_uint32 (metadata);
737 
738    for (i = 0; i < num_entries; i++) {
739       key = blob_read_string(metadata);
740       value = blob_read_uint32(metadata);
741 
742       hash->put(value, key);
743    }
744 }
745 
746 static void
write_hash_tables(struct blob * metadata,struct gl_shader_program * prog)747 write_hash_tables(struct blob *metadata, struct gl_shader_program *prog)
748 {
749    write_hash_table(metadata, prog->AttributeBindings);
750    write_hash_table(metadata, prog->FragDataBindings);
751    write_hash_table(metadata, prog->FragDataIndexBindings);
752 }
753 
754 static void
read_hash_tables(struct blob_reader * metadata,struct gl_shader_program * prog)755 read_hash_tables(struct blob_reader *metadata, struct gl_shader_program *prog)
756 {
757    read_hash_table(metadata, prog->AttributeBindings);
758    read_hash_table(metadata, prog->FragDataBindings);
759    read_hash_table(metadata, prog->FragDataIndexBindings);
760 }
761 
762 static void
write_shader_subroutine_index(struct blob * metadata,struct gl_linked_shader * sh,struct gl_program_resource * res)763 write_shader_subroutine_index(struct blob *metadata,
764                               struct gl_linked_shader *sh,
765                               struct gl_program_resource *res)
766 {
767    assert(sh);
768 
769    for (unsigned j = 0; j < sh->Program->sh.NumSubroutineFunctions; j++) {
770       if (strcmp(((gl_subroutine_function *)res->Data)->name,
771                  sh->Program->sh.SubroutineFunctions[j].name) == 0) {
772          blob_write_uint32(metadata, j);
773          break;
774       }
775    }
776 }
777 
778 static void
get_shader_var_and_pointer_sizes(size_t * s_var_size,size_t * s_var_ptrs,const gl_shader_variable * var)779 get_shader_var_and_pointer_sizes(size_t *s_var_size, size_t *s_var_ptrs,
780                                  const gl_shader_variable *var)
781 {
782    *s_var_size = sizeof(gl_shader_variable);
783    *s_var_ptrs =
784       sizeof(var->type) +
785       sizeof(var->interface_type) +
786       sizeof(var->outermost_struct_type) +
787       sizeof(var->name);
788 }
789 
790 enum uniform_type
791 {
792    uniform_remapped,
793    uniform_not_remapped
794 };
795 
796 static void
write_program_resource_data(struct blob * metadata,struct gl_shader_program * prog,struct gl_program_resource * res)797 write_program_resource_data(struct blob *metadata,
798                             struct gl_shader_program *prog,
799                             struct gl_program_resource *res)
800 {
801    struct gl_linked_shader *sh;
802 
803    switch(res->Type) {
804    case GL_PROGRAM_INPUT:
805    case GL_PROGRAM_OUTPUT: {
806       const gl_shader_variable *var = (gl_shader_variable *)res->Data;
807 
808       encode_type_to_blob(metadata, var->type);
809       encode_type_to_blob(metadata, var->interface_type);
810       encode_type_to_blob(metadata, var->outermost_struct_type);
811 
812       if (var->name) {
813          blob_write_string(metadata, var->name);
814       } else {
815          blob_write_string(metadata, "");
816       }
817 
818       size_t s_var_size, s_var_ptrs;
819       get_shader_var_and_pointer_sizes(&s_var_size, &s_var_ptrs, var);
820 
821       /* Write gl_shader_variable skipping over the pointers */
822       blob_write_bytes(metadata, ((char *)var) + s_var_ptrs,
823                        s_var_size - s_var_ptrs);
824       break;
825    }
826    case GL_UNIFORM_BLOCK:
827       for (unsigned i = 0; i < prog->data->NumUniformBlocks; i++) {
828          if (strcmp(((gl_uniform_block *)res->Data)->Name,
829                     prog->data->UniformBlocks[i].Name) == 0) {
830             blob_write_uint32(metadata, i);
831             break;
832          }
833       }
834       break;
835    case GL_SHADER_STORAGE_BLOCK:
836       for (unsigned i = 0; i < prog->data->NumShaderStorageBlocks; i++) {
837          if (strcmp(((gl_uniform_block *)res->Data)->Name,
838                     prog->data->ShaderStorageBlocks[i].Name) == 0) {
839             blob_write_uint32(metadata, i);
840             break;
841          }
842       }
843       break;
844    case GL_BUFFER_VARIABLE:
845    case GL_VERTEX_SUBROUTINE_UNIFORM:
846    case GL_GEOMETRY_SUBROUTINE_UNIFORM:
847    case GL_FRAGMENT_SUBROUTINE_UNIFORM:
848    case GL_COMPUTE_SUBROUTINE_UNIFORM:
849    case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
850    case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
851    case GL_UNIFORM:
852       if (((gl_uniform_storage *)res->Data)->builtin ||
853           res->Type != GL_UNIFORM) {
854          blob_write_uint32(metadata, uniform_not_remapped);
855          for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
856             if (strcmp(((gl_uniform_storage *)res->Data)->name,
857                        prog->data->UniformStorage[i].name) == 0) {
858                blob_write_uint32(metadata, i);
859                break;
860             }
861          }
862       } else {
863          blob_write_uint32(metadata, uniform_remapped);
864          blob_write_uint32(metadata, ((gl_uniform_storage *)res->Data)->remap_location);
865       }
866       break;
867    case GL_ATOMIC_COUNTER_BUFFER:
868       for (unsigned i = 0; i < prog->data->NumAtomicBuffers; i++) {
869          if (((gl_active_atomic_buffer *)res->Data)->Binding ==
870              prog->data->AtomicBuffers[i].Binding) {
871             blob_write_uint32(metadata, i);
872             break;
873          }
874       }
875       break;
876    case GL_TRANSFORM_FEEDBACK_BUFFER:
877       for (unsigned i = 0; i < MAX_FEEDBACK_BUFFERS; i++) {
878          if (((gl_transform_feedback_buffer *)res->Data)->Binding ==
879              prog->last_vert_prog->sh.LinkedTransformFeedback->Buffers[i].Binding) {
880             blob_write_uint32(metadata, i);
881             break;
882          }
883       }
884       break;
885    case GL_TRANSFORM_FEEDBACK_VARYING:
886       for (int i = 0; i < prog->last_vert_prog->sh.LinkedTransformFeedback->NumVarying; i++) {
887          if (strcmp(((gl_transform_feedback_varying_info *)res->Data)->Name,
888                     prog->last_vert_prog->sh.LinkedTransformFeedback->Varyings[i].Name) == 0) {
889             blob_write_uint32(metadata, i);
890             break;
891          }
892       }
893       break;
894    case GL_VERTEX_SUBROUTINE:
895    case GL_TESS_CONTROL_SUBROUTINE:
896    case GL_TESS_EVALUATION_SUBROUTINE:
897    case GL_GEOMETRY_SUBROUTINE:
898    case GL_FRAGMENT_SUBROUTINE:
899    case GL_COMPUTE_SUBROUTINE:
900       sh =
901          prog->_LinkedShaders[_mesa_shader_stage_from_subroutine(res->Type)];
902       write_shader_subroutine_index(metadata, sh, res);
903       break;
904    default:
905       assert(!"Support for writing resource not yet implemented.");
906    }
907 }
908 
909 static void
read_program_resource_data(struct blob_reader * metadata,struct gl_shader_program * prog,struct gl_program_resource * res)910 read_program_resource_data(struct blob_reader *metadata,
911                            struct gl_shader_program *prog,
912                            struct gl_program_resource *res)
913 {
914    struct gl_linked_shader *sh;
915 
916    switch(res->Type) {
917    case GL_PROGRAM_INPUT:
918    case GL_PROGRAM_OUTPUT: {
919       gl_shader_variable *var = ralloc(prog, struct gl_shader_variable);
920 
921       var->type = decode_type_from_blob(metadata);
922       var->interface_type = decode_type_from_blob(metadata);
923       var->outermost_struct_type = decode_type_from_blob(metadata);
924 
925       var->name = ralloc_strdup(prog, blob_read_string(metadata));
926 
927       size_t s_var_size, s_var_ptrs;
928       get_shader_var_and_pointer_sizes(&s_var_size, &s_var_ptrs, var);
929 
930       blob_copy_bytes(metadata, ((uint8_t *) var) + s_var_ptrs,
931                       s_var_size - s_var_ptrs);
932 
933       res->Data = var;
934       break;
935    }
936    case GL_UNIFORM_BLOCK:
937       res->Data = &prog->data->UniformBlocks[blob_read_uint32(metadata)];
938       break;
939    case GL_SHADER_STORAGE_BLOCK:
940       res->Data = &prog->data->ShaderStorageBlocks[blob_read_uint32(metadata)];
941       break;
942    case GL_BUFFER_VARIABLE:
943    case GL_VERTEX_SUBROUTINE_UNIFORM:
944    case GL_GEOMETRY_SUBROUTINE_UNIFORM:
945    case GL_FRAGMENT_SUBROUTINE_UNIFORM:
946    case GL_COMPUTE_SUBROUTINE_UNIFORM:
947    case GL_TESS_CONTROL_SUBROUTINE_UNIFORM:
948    case GL_TESS_EVALUATION_SUBROUTINE_UNIFORM:
949    case GL_UNIFORM: {
950       enum uniform_type type = (enum uniform_type) blob_read_uint32(metadata);
951       if (type == uniform_not_remapped) {
952          res->Data = &prog->data->UniformStorage[blob_read_uint32(metadata)];
953       } else {
954          res->Data = prog->UniformRemapTable[blob_read_uint32(metadata)];
955       }
956       break;
957    }
958    case GL_ATOMIC_COUNTER_BUFFER:
959       res->Data = &prog->data->AtomicBuffers[blob_read_uint32(metadata)];
960       break;
961    case GL_TRANSFORM_FEEDBACK_BUFFER:
962       res->Data = &prog->last_vert_prog->
963          sh.LinkedTransformFeedback->Buffers[blob_read_uint32(metadata)];
964       break;
965    case GL_TRANSFORM_FEEDBACK_VARYING:
966       res->Data = &prog->last_vert_prog->
967          sh.LinkedTransformFeedback->Varyings[blob_read_uint32(metadata)];
968       break;
969    case GL_VERTEX_SUBROUTINE:
970    case GL_TESS_CONTROL_SUBROUTINE:
971    case GL_TESS_EVALUATION_SUBROUTINE:
972    case GL_GEOMETRY_SUBROUTINE:
973    case GL_FRAGMENT_SUBROUTINE:
974    case GL_COMPUTE_SUBROUTINE:
975       sh =
976          prog->_LinkedShaders[_mesa_shader_stage_from_subroutine(res->Type)];
977       res->Data =
978          &sh->Program->sh.SubroutineFunctions[blob_read_uint32(metadata)];
979       break;
980    default:
981       assert(!"Support for reading resource not yet implemented.");
982    }
983 }
984 
985 static void
write_program_resource_list(struct blob * metadata,struct gl_shader_program * prog)986 write_program_resource_list(struct blob *metadata,
987                             struct gl_shader_program *prog)
988 {
989    blob_write_uint32(metadata, prog->data->NumProgramResourceList);
990 
991    for (unsigned i = 0; i < prog->data->NumProgramResourceList; i++) {
992       blob_write_uint32(metadata, prog->data->ProgramResourceList[i].Type);
993       write_program_resource_data(metadata, prog,
994                                   &prog->data->ProgramResourceList[i]);
995       blob_write_bytes(metadata,
996                        &prog->data->ProgramResourceList[i].StageReferences,
997                        sizeof(prog->data->ProgramResourceList[i].StageReferences));
998    }
999 }
1000 
1001 static void
read_program_resource_list(struct blob_reader * metadata,struct gl_shader_program * prog)1002 read_program_resource_list(struct blob_reader *metadata,
1003                            struct gl_shader_program *prog)
1004 {
1005    prog->data->NumProgramResourceList = blob_read_uint32(metadata);
1006 
1007    prog->data->ProgramResourceList =
1008       ralloc_array(prog->data, gl_program_resource,
1009                    prog->data->NumProgramResourceList);
1010 
1011    for (unsigned i = 0; i < prog->data->NumProgramResourceList; i++) {
1012       prog->data->ProgramResourceList[i].Type = blob_read_uint32(metadata);
1013       read_program_resource_data(metadata, prog,
1014                                  &prog->data->ProgramResourceList[i]);
1015       blob_copy_bytes(metadata,
1016                       (uint8_t *) &prog->data->ProgramResourceList[i].StageReferences,
1017                       sizeof(prog->data->ProgramResourceList[i].StageReferences));
1018    }
1019 }
1020 
1021 static void
write_shader_parameters(struct blob * metadata,struct gl_program_parameter_list * params)1022 write_shader_parameters(struct blob *metadata,
1023                         struct gl_program_parameter_list *params)
1024 {
1025    blob_write_uint32(metadata, params->NumParameters);
1026    uint32_t i = 0;
1027 
1028    while (i < params->NumParameters) {
1029       struct gl_program_parameter *param = &params->Parameters[i];
1030       blob_write_uint32(metadata, param->Type);
1031       blob_write_string(metadata, param->Name);
1032       blob_write_uint32(metadata, param->Size);
1033       blob_write_uint32(metadata, param->Padded);
1034       blob_write_uint32(metadata, param->DataType);
1035       blob_write_bytes(metadata, param->StateIndexes,
1036                        sizeof(param->StateIndexes));
1037       blob_write_uint32(metadata, param->UniformStorageIndex);
1038       blob_write_uint32(metadata, param->MainUniformStorageIndex);
1039 
1040       i++;
1041    }
1042 
1043    blob_write_bytes(metadata, params->ParameterValues,
1044                     sizeof(gl_constant_value) * params->NumParameterValues);
1045 
1046    blob_write_uint32(metadata, params->StateFlags);
1047    blob_write_uint32(metadata, params->UniformBytes);
1048    blob_write_uint32(metadata, params->FirstStateVarIndex);
1049    blob_write_uint32(metadata, params->LastStateVarIndex);
1050 }
1051 
1052 static void
read_shader_parameters(struct blob_reader * metadata,struct gl_program_parameter_list * params)1053 read_shader_parameters(struct blob_reader *metadata,
1054                        struct gl_program_parameter_list *params)
1055 {
1056    gl_state_index16 state_indexes[STATE_LENGTH];
1057    uint32_t i = 0;
1058    uint32_t num_parameters = blob_read_uint32(metadata);
1059 
1060    _mesa_reserve_parameter_storage(params, num_parameters, num_parameters);
1061    while (i < num_parameters) {
1062       gl_register_file type = (gl_register_file) blob_read_uint32(metadata);
1063       const char *name = blob_read_string(metadata);
1064       unsigned size = blob_read_uint32(metadata);
1065       bool padded = blob_read_uint32(metadata);
1066       unsigned data_type = blob_read_uint32(metadata);
1067       blob_copy_bytes(metadata, (uint8_t *) state_indexes,
1068                       sizeof(state_indexes));
1069 
1070       _mesa_add_parameter(params, type, name, size, data_type,
1071                           NULL, state_indexes, padded);
1072 
1073       gl_program_parameter *param = &params->Parameters[i];
1074       param->UniformStorageIndex = blob_read_uint32(metadata);
1075       param->MainUniformStorageIndex = blob_read_uint32(metadata);
1076 
1077       i++;
1078    }
1079 
1080    blob_copy_bytes(metadata, (uint8_t *) params->ParameterValues,
1081                    sizeof(gl_constant_value) * params->NumParameterValues);
1082 
1083    params->StateFlags = blob_read_uint32(metadata);
1084    params->UniformBytes = blob_read_uint32(metadata);
1085    params->FirstStateVarIndex = blob_read_uint32(metadata);
1086    params->LastStateVarIndex = blob_read_uint32(metadata);
1087 }
1088 
1089 static void
write_shader_metadata(struct blob * metadata,gl_linked_shader * shader)1090 write_shader_metadata(struct blob *metadata, gl_linked_shader *shader)
1091 {
1092    assert(shader->Program);
1093    struct gl_program *glprog = shader->Program;
1094    unsigned i;
1095 
1096    blob_write_uint64(metadata, glprog->DualSlotInputs);
1097    blob_write_bytes(metadata, glprog->TexturesUsed,
1098                     sizeof(glprog->TexturesUsed));
1099    blob_write_uint64(metadata, glprog->SamplersUsed);
1100 
1101    blob_write_bytes(metadata, glprog->SamplerUnits,
1102                     sizeof(glprog->SamplerUnits));
1103    blob_write_bytes(metadata, glprog->sh.SamplerTargets,
1104                     sizeof(glprog->sh.SamplerTargets));
1105    blob_write_uint32(metadata, glprog->ShadowSamplers);
1106    blob_write_uint32(metadata, glprog->ExternalSamplersUsed);
1107    blob_write_uint32(metadata, glprog->sh.ShaderStorageBlocksWriteAccess);
1108 
1109    blob_write_bytes(metadata, glprog->sh.ImageAccess,
1110                     sizeof(glprog->sh.ImageAccess));
1111    blob_write_bytes(metadata, glprog->sh.ImageUnits,
1112                     sizeof(glprog->sh.ImageUnits));
1113 
1114    size_t ptr_size = sizeof(GLvoid *);
1115 
1116    blob_write_uint32(metadata, glprog->sh.NumBindlessSamplers);
1117    blob_write_uint32(metadata, glprog->sh.HasBoundBindlessSampler);
1118    for (i = 0; i < glprog->sh.NumBindlessSamplers; i++) {
1119       blob_write_bytes(metadata, &glprog->sh.BindlessSamplers[i],
1120                        sizeof(struct gl_bindless_sampler) - ptr_size);
1121    }
1122 
1123    blob_write_uint32(metadata, glprog->sh.NumBindlessImages);
1124    blob_write_uint32(metadata, glprog->sh.HasBoundBindlessImage);
1125    for (i = 0; i < glprog->sh.NumBindlessImages; i++) {
1126       blob_write_bytes(metadata, &glprog->sh.BindlessImages[i],
1127                        sizeof(struct gl_bindless_image) - ptr_size);
1128    }
1129 
1130    write_shader_parameters(metadata, glprog->Parameters);
1131 
1132    assert((glprog->driver_cache_blob == NULL) ==
1133           (glprog->driver_cache_blob_size == 0));
1134    blob_write_uint32(metadata, (uint32_t)glprog->driver_cache_blob_size);
1135    if (glprog->driver_cache_blob_size > 0) {
1136       blob_write_bytes(metadata, glprog->driver_cache_blob,
1137                        glprog->driver_cache_blob_size);
1138    }
1139 }
1140 
1141 static void
read_shader_metadata(struct blob_reader * metadata,struct gl_program * glprog,gl_linked_shader * linked)1142 read_shader_metadata(struct blob_reader *metadata,
1143                      struct gl_program *glprog,
1144                      gl_linked_shader *linked)
1145 {
1146    unsigned i;
1147 
1148    glprog->DualSlotInputs = blob_read_uint64(metadata);
1149    blob_copy_bytes(metadata, (uint8_t *) glprog->TexturesUsed,
1150                    sizeof(glprog->TexturesUsed));
1151    glprog->SamplersUsed = blob_read_uint64(metadata);
1152 
1153    blob_copy_bytes(metadata, (uint8_t *) glprog->SamplerUnits,
1154                    sizeof(glprog->SamplerUnits));
1155    blob_copy_bytes(metadata, (uint8_t *) glprog->sh.SamplerTargets,
1156                    sizeof(glprog->sh.SamplerTargets));
1157    glprog->ShadowSamplers = blob_read_uint32(metadata);
1158    glprog->ExternalSamplersUsed = blob_read_uint32(metadata);
1159    glprog->sh.ShaderStorageBlocksWriteAccess = blob_read_uint32(metadata);
1160 
1161    blob_copy_bytes(metadata, (uint8_t *) glprog->sh.ImageAccess,
1162                    sizeof(glprog->sh.ImageAccess));
1163    blob_copy_bytes(metadata, (uint8_t *) glprog->sh.ImageUnits,
1164                    sizeof(glprog->sh.ImageUnits));
1165 
1166    size_t ptr_size = sizeof(GLvoid *);
1167 
1168    glprog->sh.NumBindlessSamplers = blob_read_uint32(metadata);
1169    glprog->sh.HasBoundBindlessSampler = blob_read_uint32(metadata);
1170    if (glprog->sh.NumBindlessSamplers > 0) {
1171       glprog->sh.BindlessSamplers =
1172          rzalloc_array(glprog, gl_bindless_sampler,
1173                        glprog->sh.NumBindlessSamplers);
1174 
1175       for (i = 0; i < glprog->sh.NumBindlessSamplers; i++) {
1176          blob_copy_bytes(metadata, (uint8_t *) &glprog->sh.BindlessSamplers[i],
1177                          sizeof(struct gl_bindless_sampler) - ptr_size);
1178       }
1179    }
1180 
1181    glprog->sh.NumBindlessImages = blob_read_uint32(metadata);
1182    glprog->sh.HasBoundBindlessImage = blob_read_uint32(metadata);
1183    if (glprog->sh.NumBindlessImages > 0) {
1184       glprog->sh.BindlessImages =
1185          rzalloc_array(glprog, gl_bindless_image,
1186                        glprog->sh.NumBindlessImages);
1187 
1188       for (i = 0; i < glprog->sh.NumBindlessImages; i++) {
1189          blob_copy_bytes(metadata, (uint8_t *) &glprog->sh.BindlessImages[i],
1190                         sizeof(struct gl_bindless_image) - ptr_size);
1191       }
1192    }
1193 
1194    glprog->Parameters = _mesa_new_parameter_list();
1195    read_shader_parameters(metadata, glprog->Parameters);
1196 
1197    glprog->driver_cache_blob_size = (size_t)blob_read_uint32(metadata);
1198    if (glprog->driver_cache_blob_size > 0) {
1199       glprog->driver_cache_blob =
1200          (uint8_t*)ralloc_size(glprog, glprog->driver_cache_blob_size);
1201       blob_copy_bytes(metadata, glprog->driver_cache_blob,
1202                       glprog->driver_cache_blob_size);
1203    }
1204 }
1205 
1206 static void
get_shader_info_and_pointer_sizes(size_t * s_info_size,size_t * s_info_ptrs,shader_info * info)1207 get_shader_info_and_pointer_sizes(size_t *s_info_size, size_t *s_info_ptrs,
1208                                   shader_info *info)
1209 {
1210    *s_info_size = sizeof(shader_info);
1211    *s_info_ptrs = sizeof(info->name) + sizeof(info->label);
1212 }
1213 
1214 static void
create_linked_shader_and_program(struct gl_context * ctx,gl_shader_stage stage,struct gl_shader_program * prog,struct blob_reader * metadata)1215 create_linked_shader_and_program(struct gl_context *ctx,
1216                                  gl_shader_stage stage,
1217                                  struct gl_shader_program *prog,
1218                                  struct blob_reader *metadata)
1219 {
1220    struct gl_program *glprog;
1221 
1222    struct gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
1223    linked->Stage = stage;
1224 
1225    glprog = ctx->Driver.NewProgram(ctx, stage, prog->Name, false);
1226    glprog->info.stage = stage;
1227    linked->Program = glprog;
1228 
1229    read_shader_metadata(metadata, glprog, linked);
1230 
1231    glprog->info.name = ralloc_strdup(glprog, blob_read_string(metadata));
1232    glprog->info.label = ralloc_strdup(glprog, blob_read_string(metadata));
1233 
1234    size_t s_info_size, s_info_ptrs;
1235    get_shader_info_and_pointer_sizes(&s_info_size, &s_info_ptrs,
1236                                      &glprog->info);
1237 
1238    /* Restore shader info */
1239    blob_copy_bytes(metadata, ((uint8_t *) &glprog->info) + s_info_ptrs,
1240                    s_info_size - s_info_ptrs);
1241 
1242    _mesa_reference_shader_program_data(ctx, &glprog->sh.data, prog->data);
1243    _mesa_reference_program(ctx, &linked->Program, glprog);
1244    prog->_LinkedShaders[stage] = linked;
1245 }
1246 
1247 extern "C" void
serialize_glsl_program(struct blob * blob,struct gl_context * ctx,struct gl_shader_program * prog)1248 serialize_glsl_program(struct blob *blob, struct gl_context *ctx,
1249                        struct gl_shader_program *prog)
1250 {
1251    blob_write_bytes(blob, prog->data->sha1, sizeof(prog->data->sha1));
1252 
1253    write_uniforms(blob, prog);
1254 
1255    write_hash_tables(blob, prog);
1256 
1257    blob_write_uint32(blob, prog->data->Version);
1258    blob_write_uint32(blob, prog->IsES);
1259    blob_write_uint32(blob, prog->data->linked_stages);
1260 
1261    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
1262       struct gl_linked_shader *sh = prog->_LinkedShaders[i];
1263       if (sh) {
1264          write_shader_metadata(blob, sh);
1265 
1266          if (sh->Program->info.name)
1267             blob_write_string(blob, sh->Program->info.name);
1268          else
1269             blob_write_string(blob, "");
1270 
1271          if (sh->Program->info.label)
1272             blob_write_string(blob, sh->Program->info.label);
1273          else
1274             blob_write_string(blob, "");
1275 
1276          size_t s_info_size, s_info_ptrs;
1277          get_shader_info_and_pointer_sizes(&s_info_size, &s_info_ptrs,
1278                                            &sh->Program->info);
1279 
1280          /* Store shader info */
1281          blob_write_bytes(blob,
1282                           ((char *) &sh->Program->info) + s_info_ptrs,
1283                           s_info_size - s_info_ptrs);
1284       }
1285    }
1286 
1287    write_xfb(blob, prog);
1288 
1289    write_uniform_remap_tables(blob, prog);
1290 
1291    write_atomic_buffers(blob, prog);
1292 
1293    write_buffer_blocks(blob, prog);
1294 
1295    write_subroutines(blob, prog);
1296 
1297    write_program_resource_list(blob, prog);
1298 }
1299 
1300 extern "C" bool
deserialize_glsl_program(struct blob_reader * blob,struct gl_context * ctx,struct gl_shader_program * prog)1301 deserialize_glsl_program(struct blob_reader *blob, struct gl_context *ctx,
1302                          struct gl_shader_program *prog)
1303 {
1304    /* Fixed function programs generated by Mesa can't be serialized. */
1305    if (prog->Name == 0)
1306       return false;
1307 
1308    assert(prog->data->UniformStorage == NULL);
1309 
1310    blob_copy_bytes(blob, prog->data->sha1, sizeof(prog->data->sha1));
1311 
1312    read_uniforms(blob, prog);
1313 
1314    read_hash_tables(blob, prog);
1315 
1316    prog->data->Version = blob_read_uint32(blob);
1317    prog->IsES = blob_read_uint32(blob);
1318    prog->data->linked_stages = blob_read_uint32(blob);
1319 
1320    unsigned mask = prog->data->linked_stages;
1321    while (mask) {
1322       const int j = u_bit_scan(&mask);
1323       create_linked_shader_and_program(ctx, (gl_shader_stage) j, prog,
1324                                        blob);
1325    }
1326 
1327    read_xfb(blob, prog);
1328 
1329    read_uniform_remap_tables(blob, prog);
1330 
1331    read_atomic_buffers(blob, prog);
1332 
1333    read_buffer_blocks(blob, prog);
1334 
1335    read_subroutines(blob, prog);
1336 
1337    read_program_resource_list(blob, prog);
1338 
1339    return !blob->overrun;
1340 }
1341