]> git.tdb.fi Git - libs/gl.git/blob - source/core/program.cpp
Refactor program stage management
[libs/gl.git] / source / core / program.cpp
1 #include <cstring>
2 #include <set>
3 #include <msp/core/algorithm.h>
4 #include <msp/core/hash.h>
5 #include <msp/core/maputils.h>
6 #include <msp/core/raii.h>
7 #include <msp/gl/extensions/arb_es2_compatibility.h>
8 #include <msp/gl/extensions/arb_fragment_shader.h>
9 #include <msp/gl/extensions/arb_gl_spirv.h>
10 #include <msp/gl/extensions/arb_geometry_shader4.h>
11 #include <msp/gl/extensions/arb_separate_shader_objects.h>
12 #include <msp/gl/extensions/arb_shader_objects.h>
13 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
14 #include <msp/gl/extensions/arb_vertex_shader.h>
15 #include <msp/gl/extensions/ext_gpu_shader4.h>
16 #include <msp/gl/extensions/nv_non_square_matrices.h>
17 #include <msp/io/print.h>
18 #include <msp/strings/format.h>
19 #include "buffer.h"
20 #include "error.h"
21 #include "misc.h"
22 #include "program.h"
23 #include "resources.h"
24 #include "shader.h"
25 #include "glsl/compiler.h"
26
27 using namespace std;
28
29 namespace Msp {
30 namespace GL {
31
32 Program::Program()
33 {
34         init();
35 }
36
37 Program::Program(const string &source)
38 {
39         init();
40
41         GlslModule mod;
42         mod.set_source(source);
43         add_stages(mod);
44
45         link();
46         module = 0;
47 }
48
49 Program::Program(const string &vert, const string &frag)
50 {
51         init();
52
53 #pragma GCC diagnostic push
54 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
55         attach_shader_owned(new VertexShader(vert));
56         attach_shader_owned(new FragmentShader(frag));
57 #pragma GCC diagnostic pop
58         link();
59 }
60
61 Program::Program(const Module &mod, const map<string, int> &spec_values)
62 {
63         init();
64         add_stages(mod, spec_values);
65         link();
66 }
67
68 void Program::init()
69 {
70         static Require _req(ARB_shader_objects);
71
72         id = glCreateProgram();
73         fill(stage_ids, stage_ids+MAX_STAGES, 0);
74         module = 0;
75         transient = 0;
76         linked = false;
77 }
78
79 Program::~Program()
80 {
81         for(unsigned i=0; i<MAX_STAGES; ++i)
82                 if(stage_ids[i])
83                         glDeleteShader(stage_ids[i]);
84         glDeleteProgram(id);
85 }
86
87 void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
88 {
89         if(has_stages())
90                 throw invalid_operation("Program::add_stages");
91
92         switch(mod.get_format())
93         {
94         case Module::GLSL: return add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values);
95         case Module::SPIR_V: return add_spirv_stages(static_cast<const SpirVModule &>(mod), spec_values);
96         default: throw invalid_argument("Program::add_stages");
97         }
98 }
99
100 bool Program::has_stages() const
101 {
102         for(unsigned i=0; i<MAX_STAGES; ++i)
103                 if(stage_ids[i])
104                         return true;
105         return false;
106 }
107
108 unsigned Program::add_stage(Stage type)
109 {
110         GLenum gl_type;
111         switch(type)
112         {
113         case VERTEX: { static Require _req(ARB_vertex_shader); gl_type = GL_VERTEX_SHADER; } break;
114         case GEOMETRY: { static Require _req(ARB_geometry_shader4); gl_type = GL_GEOMETRY_SHADER; } break;
115         case FRAGMENT: { static Require _req(ARB_fragment_shader); gl_type = GL_FRAGMENT_SHADER; } break;
116         default: throw invalid_argument("Program::add_stage");
117         }
118
119         if(stage_ids[type])
120                 throw invalid_operation("Program::add_stage");
121
122         unsigned stage_id = glCreateShader(gl_type);
123         stage_ids[type] = stage_id;
124         glAttachShader(id, stage_id);
125
126         return stage_id;
127 }
128
129 void Program::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values)
130 {
131         module = &mod;
132
133         SL::Compiler compiler;
134         compiler.set_source(mod.get_prepared_source(), "<module>");
135         compiler.specialize(spec_values);
136         compiler.compile(SL::Compiler::PROGRAM);
137 #ifdef DEBUG
138         string diagnostics = compiler.get_diagnostics();
139         if(!diagnostics.empty())
140                 IO::print("Program diagnostics:\n%s\n", diagnostics);
141 #endif
142
143         vector<SL::Stage::Type> stages = compiler.get_stages();
144         for(vector<SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
145         {
146                 unsigned stage_id = 0;
147                 switch(*i)
148                 {
149                 case SL::Stage::VERTEX: stage_id = add_stage(VERTEX); break;
150                 case SL::Stage::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
151                 case SL::Stage::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
152                 default: throw invalid_operation("Program::add_glsl_stages");
153                 }
154
155                 string stage_src = compiler.get_stage_glsl(*i);
156                 const char *src_ptr = stage_src.data();
157                 int src_len = stage_src.size();
158                 glShaderSource(stage_id, 1, &src_ptr, &src_len);
159
160                 if(*i==SL::Stage::VERTEX)
161                 {
162                         const map<string, unsigned> &attribs = compiler.get_vertex_attributes();
163                         for(map<string, unsigned>::const_iterator j=attribs.begin(); j!=attribs.end(); ++j)
164                                 glBindAttribLocation(id, j->second, j->first.c_str());
165                 }
166
167                 if(*i==SL::Stage::FRAGMENT && EXT_gpu_shader4)
168                 {
169                         const map<string, unsigned> &frag_outs = compiler.get_fragment_outputs();
170                         for(map<string, unsigned>::const_iterator j=frag_outs.begin(); j!=frag_outs.end(); ++j)
171                                 glBindFragDataLocation(id, j->second, j->first.c_str());
172                 }
173
174                 compile_glsl_stage(stage_id);
175         }
176
177         if(!transient)
178                 transient = new TransientData;
179         transient->textures = compiler.get_texture_bindings();
180         transient->blocks = compiler.get_uniform_block_bindings();
181 }
182
183 void Program::compile_glsl_stage(unsigned stage_id)
184 {
185         glCompileShader(stage_id);
186         bool compiled = get_shader_i(stage_id, GL_COMPILE_STATUS);
187
188         GLsizei info_log_len = get_shader_i(stage_id, GL_INFO_LOG_LENGTH);
189         string info_log(info_log_len+1, 0);
190         glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]);
191         info_log.erase(info_log_len);
192         if(module && module->get_format()==Module::GLSL)
193                 info_log = static_cast<const GlslModule *>(module)->get_source_map().translate_errors(info_log);
194
195         if(!compiled)
196                 throw compile_error(info_log);
197 #ifdef DEBUG
198         if(!info_log.empty())
199                 IO::print("Shader compile info log:\n%s", info_log);
200 #endif
201 }
202
203 void Program::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
204 {
205         static Require _req(ARB_gl_spirv);
206         static Require _req2(ARB_ES2_compatibility);
207
208         module = &mod;
209
210         const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
211         unsigned n_stages = 0;
212         unsigned used_stage_ids[MAX_STAGES];
213         for(vector<SpirVModule::EntryPoint>::const_iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
214         {
215                 unsigned stage_id = 0;
216                 switch(i->stage)
217                 {
218                 case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break;
219                 case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
220                 case SpirVModule::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
221                 default: throw invalid_operation("Program::add_spirv_stages");
222                 }
223
224                 used_stage_ids[n_stages++] = stage_id;
225         }
226
227         const vector<UInt32> &code = mod.get_code();
228         glShaderBinary(n_stages, used_stage_ids, GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
229
230         if(!spec_values.empty() && !transient)
231                 transient = new TransientData;
232
233         const vector<SpirVModule::Constant> &spec_consts = mod.get_spec_constants();
234         vector<unsigned> spec_id_array;
235         vector<unsigned> spec_value_array;
236         spec_id_array.reserve(spec_consts.size());
237         spec_value_array.reserve(spec_consts.size());
238         for(vector<SpirVModule::Constant>::const_iterator i=spec_consts.begin(); i!=spec_consts.end(); ++i)
239         {
240                 map<string, int>::const_iterator j = spec_values.find(i->name);
241                 if(j!=spec_values.end())
242                 {
243                         spec_id_array.push_back(i->constant_id);
244                         spec_value_array.push_back(j->second);
245                         transient->spec_values[i->constant_id] = j->second;
246                 }
247         }
248
249         vector<SpirVModule::EntryPoint>::const_iterator j=entry_points.begin();
250         for(unsigned i=0; i<MAX_STAGES; ++i)
251                 if(stage_ids[i])
252                         glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
253 }
254
255 #pragma GCC diagnostic push
256 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
257 void Program::attach_shader(Shader &shader)
258 {
259         unsigned shader_id = shader.steal_id();
260         if(!shader_id)
261                 throw invalid_argument("Program::attach_shader");
262
263         int type;
264         glGetShaderiv(shader_id, GL_SHADER_TYPE, &type);
265         switch(type)
266         {
267         case GL_VERTEX_SHADER: stage_ids[VERTEX] = shader_id; break;
268         case GL_GEOMETRY_SHADER: stage_ids[GEOMETRY] = shader_id; break;
269         case GL_FRAGMENT_SHADER: stage_ids[FRAGMENT] = shader_id; break;
270         }
271
272         glAttachShader(id, shader_id);
273         compile_glsl_stage(shader_id);
274 }
275
276 void Program::attach_shader_owned(Shader *shader)
277 {
278         attach_shader(*shader);
279         delete shader;
280 }
281
282 void Program::detach_shader(Shader &)
283 {
284 }
285
286 const vector<Shader *> &Program::get_attached_shaders() const
287 {
288         static vector<Shader *> dummy;
289         return dummy;
290 }
291
292 void Program::bind_attribute(unsigned index, const string &name)
293 {
294         static Require _req(ARB_vertex_shader);
295         glBindAttribLocation(id, index, name.c_str());
296 }
297
298 void Program::bind_attribute(VertexAttribute attr, const string &name)
299 {
300         bind_attribute(get_attribute_semantic(attr), name);
301 }
302
303 void Program::bind_fragment_data(unsigned index, const string &name)
304 {
305         static Require _req(EXT_gpu_shader4);
306         glBindFragDataLocation(id, index, name.c_str());
307 }
308 #pragma GCC diagnostic pop
309
310 void Program::link()
311 {
312         if(!has_stages())
313                 throw invalid_operation("Program::link");
314
315         uniforms.clear();
316         uniform_blocks.clear();
317         attributes.clear();
318
319         glLinkProgram(id);
320         linked = get_program_i(id, GL_LINK_STATUS);
321
322         GLsizei info_log_len = get_program_i(id, GL_INFO_LOG_LENGTH);
323         string info_log(info_log_len+1, 0);
324         glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]);
325         info_log.erase(info_log_len);
326         if(module && module->get_format()==Module::GLSL)
327                 info_log = static_cast<const GlslModule *>(module)->get_source_map().translate_errors(info_log);
328
329         if(!linked)
330                 throw compile_error(info_log);
331 #ifdef DEBUG
332         if(!info_log.empty())
333                 IO::print("Program link info log:\n%s", info_log);
334 #endif
335
336         if(module->get_format()==Module::GLSL)
337         {
338                 query_uniforms();
339                 query_attributes();
340                 if(transient)
341                 {
342                         for(unsigned i=0; i<uniform_blocks.size(); ++i)
343                         {
344                                 map<string, unsigned>::const_iterator j = transient->blocks.find(uniform_blocks[i].name);
345                                 if(j!=transient->blocks.end())
346                                 {
347                                         glUniformBlockBinding(id, i, j->second);
348                                         uniform_blocks[i].bind_point = j->second;
349                                 }
350                         }
351
352                         Conditional<BindRestore> _bind(!ARB_separate_shader_objects, this);
353                         for(map<string, unsigned>::const_iterator i=transient->textures.begin(); i!=transient->textures.end(); ++i)
354                         {
355                                 int location = get_uniform_location(i->first);
356                                 if(location>=0)
357                                 {
358                                         if(ARB_separate_shader_objects)
359                                                 glProgramUniform1i(id, location, i->second);
360                                         else
361                                                 glUniform1i(location, i->second);
362                                 }
363                         }
364                 }
365         }
366         else if(module->get_format()==Module::SPIR_V)
367         {
368                 collect_uniforms();
369                 collect_attributes();
370         }
371
372         delete transient;
373         transient = 0;
374
375         for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
376                 require_type(i->type);
377         for(vector<AttributeInfo>::const_iterator i=attributes.begin(); i!=attributes.end(); ++i)
378                 require_type(i->type);
379 }
380
381 void Program::query_uniforms()
382 {
383         unsigned count = get_program_i(id, GL_ACTIVE_UNIFORMS);
384         uniforms.reserve(count);
385         vector<string> uniform_names(count);
386         for(unsigned i=0; i<count; ++i)
387         {
388                 char name[128];
389                 int len = 0;
390                 int size;
391                 GLenum type;
392                 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
393                 if(len && strncmp(name, "gl_", 3))
394                 {
395                         /* Some implementations report the first element of a uniform array,
396                         others report just the name of the array itself. */
397                         if(len>3 && !strcmp(name+len-3, "[0]"))
398                                 name[len-3] = 0;
399
400                         uniforms.push_back(UniformInfo());
401                         UniformInfo &info = uniforms.back();
402                         info.name = name;
403                         info.tag = name;
404                         info.array_size = size;
405                         info.type = from_gl_type(type);
406                         uniform_names[i] = name;
407                 }
408         }
409
410         sort_member(uniforms, &UniformInfo::tag);
411
412         if(ARB_uniform_buffer_object)
413         {
414                 vector<UniformInfo *> uniforms_by_index(count);
415                 for(unsigned i=0; i<count; ++i)
416                         if(!uniform_names[i].empty())
417                                 // The element is already known to be present
418                                 uniforms_by_index[i] = &*lower_bound_member(uniforms, Tag(uniform_names[i]), &UniformInfo::tag);
419                 query_uniform_blocks(uniforms_by_index);
420         }
421
422         uniform_blocks.push_back(UniformBlockInfo());
423         UniformBlockInfo &default_block = uniform_blocks.back();
424
425         for(vector<UniformInfo>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
426                 if(!i->block)
427                 {
428                         i->location = glGetUniformLocation(id, i->name.c_str());
429                         i->block = &default_block;
430                         default_block.uniforms.push_back(&*i);
431
432                         if(is_image(i->type) && i->location>=0)
433                                 glGetUniformiv(id, i->location, &i->binding);
434                 }
435
436         default_block.layout_hash = compute_layout_hash(default_block.uniforms);
437
438         update_layout_hash();
439 }
440
441 void Program::query_uniform_blocks(const vector<UniformInfo *> &uniforms_by_index)
442 {
443         unsigned count = get_program_i(id, GL_ACTIVE_UNIFORM_BLOCKS);
444         // Reserve an extra index for the default block
445         uniform_blocks.reserve(count+1);
446         for(unsigned i=0; i<count; ++i)
447         {
448                 char name[128];
449                 int len;
450                 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
451                 uniform_blocks.push_back(UniformBlockInfo());
452                 UniformBlockInfo &info = uniform_blocks.back();
453                 info.name = name;
454
455                 int value;
456                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
457                 info.data_size = value;
458
459                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_BINDING, &value);
460                 info.bind_point = value;
461
462                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
463                 vector<int> indices(value);
464                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
465                 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
466                 {
467                         if(!uniforms_by_index[*j])
468                                 throw logic_error("Program::link");
469                         info.uniforms.push_back(uniforms_by_index[*j]);
470                         uniforms_by_index[*j]->block = &info;
471                 }
472
473                 vector<unsigned> query_indices(indices.begin(), indices.end());
474                 vector<int> values(indices.size());
475                 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_OFFSET, &values[0]);
476                 for(unsigned j=0; j<indices.size(); ++j)
477                         uniforms_by_index[indices[j]]->offset = values[j];
478
479                 query_indices.clear();
480                 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
481                         if(uniforms_by_index[*j]->array_size>1)
482                                 query_indices.push_back(*j);
483                 if(!query_indices.empty())
484                 {
485                         glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
486                         for(unsigned j=0; j<query_indices.size(); ++j)
487                                 uniforms_by_index[query_indices[j]]->array_stride = values[j];
488                 }
489
490                 query_indices.clear();
491                 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
492                 {
493                         DataType t = uniforms_by_index[*j]->type;
494                         if(is_matrix(t))
495                                 query_indices.push_back(*j);
496                 }
497                 if(!query_indices.empty())
498                 {
499                         glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
500                         for(unsigned j=0; j<query_indices.size(); ++j)
501                                 uniforms_by_index[query_indices[j]]->matrix_stride = values[j];
502                 }
503
504                 sort(info.uniforms, uniform_location_compare);
505                 info.layout_hash = compute_layout_hash(info.uniforms);
506         }
507 }
508
509 void Program::query_attributes()
510 {
511         unsigned count = get_program_i(id, GL_ACTIVE_ATTRIBUTES);
512         attributes.reserve(count);
513         for(unsigned i=0; i<count; ++i)
514         {
515                 char name[128];
516                 int len = 0;
517                 int size;
518                 GLenum type;
519                 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
520                 if(len && strncmp(name, "gl_", 3))
521                 {
522                         if(len>3 && !strcmp(name+len-3, "[0]"))
523                                 name[len-3] = 0;
524
525                         attributes.push_back(AttributeInfo());
526                         AttributeInfo &info = attributes.back();
527                         info.name = name;
528                         info.location = glGetAttribLocation(id, name);
529                         info.array_size = size;
530                         info.type = from_gl_type(type);
531                 }
532         }
533 }
534
535 void Program::collect_uniforms()
536 {
537         const SpirVModule &mod = static_cast<const SpirVModule &>(*module);
538
539         // Prepare the default block
540         uniform_blocks.push_back(UniformBlockInfo());
541         vector<vector<string> > block_uniform_names(1);
542
543         const vector<SpirVModule::Variable> &variables = mod.get_variables();
544         for(vector<SpirVModule::Variable>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
545         {
546                 if(i->storage==SpirVModule::UNIFORM && i->struct_type)
547                 {
548                         uniform_blocks.push_back(UniformBlockInfo());
549                         UniformBlockInfo &info = uniform_blocks.back();
550                         info.name = i->struct_type->name;
551                         info.bind_point = i->binding;
552                         info.data_size = i->struct_type->size;
553
554                         string prefix;
555                         if(!i->name.empty())
556                                 prefix = i->struct_type->name+".";
557                         block_uniform_names.push_back(vector<string>());
558                         collect_block_uniforms(*i->struct_type, prefix, 0, block_uniform_names.back());
559                 }
560                 else if(i->storage==SpirVModule::UNIFORM_CONSTANT && i->location>=0)
561                 {
562                         block_uniform_names[0].push_back(i->name);
563                         uniforms.push_back(UniformInfo());
564                         UniformInfo &info = uniforms.back();
565                         info.name = i->name;
566                         info.tag = i->name;
567                         info.location = i->location;
568                         info.binding = i->binding;
569                         info.array_size = i->array_size;
570                         info.type = i->type;
571                 }
572         }
573
574         sort_member(uniforms, &UniformInfo::tag);
575
576         for(unsigned i=0; i<uniform_blocks.size(); ++i)
577         {
578                 UniformBlockInfo &block = uniform_blocks[i];
579                 const vector<string> &names = block_uniform_names[i];
580                 for(vector<string>::const_iterator j=names.begin(); j!=names.end(); ++j)
581                 {
582                         // The element is already known to be present
583                         UniformInfo &uni = *lower_bound_member(uniforms, Tag(*j), &UniformInfo::tag);
584                         block.uniforms.push_back(&uni);
585                         uni.block = &block;
586                 }
587                 sort(block.uniforms, uniform_location_compare);
588                 block.layout_hash = compute_layout_hash(block.uniforms);
589         }
590
591         update_layout_hash();
592 }
593
594 void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
595 {
596         for(vector<SpirVModule::StructMember>::const_iterator i=strct.members.begin(); i!=strct.members.end(); ++i)
597         {
598                 unsigned offset = base_offset+i->offset;
599                 if(i->struct_type)
600                 {
601                         unsigned array_size = i->array_size;
602                         if(i->array_size_spec)
603                         {
604                                 array_size = i->array_size_spec->i_value;
605                                 if(transient)
606                                 {
607                                         map<unsigned, int>::const_iterator j = transient->spec_values.find(i->array_size_spec->constant_id);
608                                         if(j!=transient->spec_values.end())
609                                                 array_size = j->second;
610                                 }
611                         }
612
613                         if(array_size)
614                         {
615                                 for(unsigned j=0; j<array_size; ++j, offset+=i->array_stride)
616                                         collect_block_uniforms(*i->struct_type, format("%s%s[%d].", prefix, i->name, j), offset, uniform_names);
617                         }
618                         else
619                                 collect_block_uniforms(*i->struct_type, prefix+i->name+".", offset, uniform_names);
620                 }
621                 else
622                 {
623                         string name = prefix+i->name;
624                         uniform_names.push_back(name);
625                         uniforms.push_back(UniformInfo());
626                         UniformInfo &info = uniforms.back();
627                         info.name = name;
628                         info.tag = name;
629                         info.offset = offset;
630                         info.array_size = i->array_size;
631                         info.array_stride = i->array_stride;
632                         info.matrix_stride = i->matrix_stride;
633                         info.type = i->type;
634                 }
635         }
636 }
637
638 void Program::collect_attributes()
639 {
640         const SpirVModule &mod = static_cast<const SpirVModule &>(*module);
641
642         const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
643         for(vector<SpirVModule::EntryPoint>::const_iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
644                 if(i->stage==SpirVModule::VERTEX && i->name=="main")
645                 {
646                         for(vector<const SpirVModule::Variable *>::const_iterator j=i->globals.begin(); j!=i->globals.end(); ++j)
647                                 if((*j)->storage==SpirVModule::INPUT)
648                                 {
649                                         attributes.push_back(AttributeInfo());
650                                         AttributeInfo &info = attributes.back();
651                                         info.name = (*j)->name;
652                                         info.location = (*j)->location;
653                                         info.array_size = (*j)->array_size;
654                                         info.type = (*j)->type;
655                                 }
656                 }
657 }
658
659 void Program::update_layout_hash()
660 {
661         string layout_descriptor;
662         for(vector<UniformBlockInfo>::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
663                 layout_descriptor += format("%d:%x\n", i->bind_point, i->layout_hash);
664         uniform_layout_hash = hash32(layout_descriptor);
665 }
666
667 Program::LayoutHash Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
668 {
669         string layout_descriptor;
670         for(vector<const UniformInfo *>::const_iterator i = uniforms.begin(); i!=uniforms.end(); ++i)
671                 layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->array_size);
672         return hash32(layout_descriptor);
673 }
674
675 bool Program::uniform_location_compare(const UniformInfo *uni1, const UniformInfo *uni2)
676 {
677         return uni1->location<uni2->location;
678 }
679
680 string Program::get_info_log() const
681 {
682         GLsizei len = get_program_i(id, GL_INFO_LOG_LENGTH);
683         string log(len+1, 0);
684         glGetProgramInfoLog(id, len+1, &len, &log[0]);
685         log.erase(len);
686         return log;
687 }
688
689 const Program::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
690 {
691         for(vector<UniformBlockInfo>::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
692                 if(i->name==name)
693                         return *i;
694         throw key_error(name);
695 }
696
697 const Program::UniformInfo &Program::get_uniform_info(const string &name) const
698 {
699         vector<UniformInfo>::const_iterator i = lower_bound_member(uniforms, Tag(name), &UniformInfo::tag);
700         if(i==uniforms.end() || i->name!=name)
701                 throw key_error(name);
702         return *i;
703 }
704
705 const Program::UniformInfo &Program::get_uniform_info(Tag tag) const
706 {
707         vector<UniformInfo>::const_iterator i = lower_bound_member(uniforms, tag, &UniformInfo::tag);
708         if(i==uniforms.end() || i->tag!=tag)
709                 throw key_error(tag);
710         return *i;
711 }
712
713 int Program::get_uniform_location(const string &name) const
714 {
715         if(name[name.size()-1]==']')
716                 throw invalid_argument("Program::get_uniform_location");
717
718         vector<UniformInfo>::const_iterator i = lower_bound_member(uniforms, Tag(name), &UniformInfo::tag);
719         return i!=uniforms.end() && i->name==name && i->block->bind_point<0 ? i->location : -1;
720 }
721
722 int Program::get_uniform_location(Tag tag) const
723 {
724         vector<UniformInfo>::const_iterator i = lower_bound_member(uniforms, tag, &UniformInfo::tag);
725         return i!=uniforms.end() && i->tag==tag && i->block->bind_point<0 ? i->location : -1;
726 }
727
728 int Program::get_uniform_binding(Tag tag) const
729 {
730         vector<UniformInfo>::const_iterator i = lower_bound_member(uniforms, tag, &UniformInfo::tag);
731         return i!=uniforms.end() && i->tag==tag ? i->binding : -1;
732 }
733
734 const Program::AttributeInfo &Program::get_attribute_info(const string &name) const
735 {
736         vector<AttributeInfo>::const_iterator i = lower_bound_member(attributes, name, &AttributeInfo::name);
737         if(i==attributes.end() || i->name!=name)
738                 throw key_error(name);
739         return *i;
740 }
741
742 int Program::get_attribute_location(const string &name) const
743 {
744         if(name[name.size()-1]==']')
745                 throw invalid_argument("Program::get_attribute_location");
746
747         vector<AttributeInfo>::const_iterator i = lower_bound_member(attributes, name, &AttributeInfo::name);
748         return i!=attributes.end() && i->name==name ? i->location : -1;
749 }
750
751 void Program::bind() const
752 {
753         if(!linked)
754                 throw invalid_operation("Program::bind");
755
756         if(!set_current(this))
757                 return;
758
759         glUseProgram(id);
760 }
761
762 void Program::unbind()
763 {
764         if(!set_current(0))
765                 return;
766
767         glUseProgram(0);
768 }
769
770
771 Program::UniformInfo::UniformInfo():
772         block(0),
773         location(-1),
774         array_size(0),
775         array_stride(0),
776         matrix_stride(0),
777         type(VOID),
778         binding(-1)
779 { }
780
781
782 Program::UniformBlockInfo::UniformBlockInfo():
783         data_size(0),
784         bind_point(-1),
785         layout_hash(0)
786 { }
787
788
789 Program::AttributeInfo::AttributeInfo():
790         location(-1),
791         array_size(0),
792         type(VOID)
793 { }
794
795
796 Program::Loader::Loader(Program &p, Collection &c):
797         DataFile::CollectionObjectLoader<Program>(p, &c)
798 {
799         add("module",          &Loader::module);
800
801         // Deprecated
802         add("attribute",       &Loader::attribute);
803         add("fragment_shader", &Loader::fragment_shader);
804         add("geometry_shader", &Loader::geometry_shader);
805         add("vertex_shader",   &Loader::vertex_shader);
806 }
807
808 void Program::Loader::finish()
809 {
810         obj.link();
811 }
812
813 void Program::Loader::module(const string &n)
814 {
815         map<string, int> spec_values;
816         SpecializationLoader ldr(spec_values);
817         load_sub_with(ldr);
818         obj.add_stages(get_collection().get<Module>(n), spec_values);
819 }
820
821 #pragma GCC diagnostic push
822 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
823 void Program::Loader::attribute(unsigned i, const string &n)
824 {
825         obj.bind_attribute(i, n);
826 }
827
828 void Program::Loader::fragment_shader(const string &src)
829 {
830         obj.attach_shader_owned(new FragmentShader(src));
831 }
832
833 void Program::Loader::geometry_shader(const string &src)
834 {
835         obj.attach_shader_owned(new GeometryShader(src));
836 }
837
838 void Program::Loader::vertex_shader(const string &src)
839 {
840         obj.attach_shader_owned(new VertexShader(src));
841 }
842 #pragma GCC diagnostic pop
843
844
845 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
846
847 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
848         spec_values(sv)
849 {
850         set_actions(shared_actions);
851 }
852
853 void Program::SpecializationLoader::init_actions()
854 {
855         add("specialize", &SpecializationLoader::specialize_bool);
856         add("specialize", &SpecializationLoader::specialize_int);
857 }
858
859 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
860 {
861         spec_values[name] = value;
862 }
863
864 void Program::SpecializationLoader::specialize_int(const string &name, int value)
865 {
866         spec_values[name] = value;
867 }
868
869 } // namespace GL
870 } // namespace Msp