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