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