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