]> git.tdb.fi Git - libs/gl.git/blob - source/core/program.cpp
919fceebe68eee80081116b1ab65397250056303
[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                         u.array_stride = get_type_size(u.type);
348                         if(is_matrix(u.type))
349                                 u.matrix_stride = get_type_size(get_matrix_column_type(u.type));
350                         default_block.uniforms.push_back(&u);
351
352                         if(is_image(u.type) && u.location>=0)
353                                 glGetUniformiv(id, u.location, &u.binding);
354                 }
355
356         default_block.update_layout_hash();
357         reflect_data.update_layout_hash();
358 }
359
360 void Program::query_uniform_blocks(const vector<ReflectData::UniformInfo *> &uniforms_by_index)
361 {
362         unsigned count = 0;
363         glGetProgramiv(id, GL_ACTIVE_UNIFORM_BLOCKS, reinterpret_cast<int *>(&count));
364         // Reserve an extra index for the default block
365         reflect_data.uniform_blocks.reserve(count+1);
366         for(unsigned i=0; i<count; ++i)
367         {
368                 char name[128];
369                 int len;
370                 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
371                 reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
372                 ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
373                 info.name = name;
374
375                 int value;
376                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
377                 info.data_size = value;
378
379                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_BINDING, &value);
380                 info.bind_point = value;
381
382                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
383                 vector<int> indices(value);
384                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
385                 for(int j: indices)
386                 {
387                         if(!uniforms_by_index[j])
388                                 throw logic_error("Program::link");
389                         info.uniforms.push_back(uniforms_by_index[j]);
390                         uniforms_by_index[j]->block = &info;
391                 }
392
393                 vector<unsigned> query_indices(indices.begin(), indices.end());
394                 vector<int> values(indices.size());
395                 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_OFFSET, &values[0]);
396                 for(unsigned j=0; j<indices.size(); ++j)
397                         uniforms_by_index[indices[j]]->offset = values[j];
398
399                 query_indices.clear();
400                 for(int j: indices)
401                         if(uniforms_by_index[j]->array_size>1)
402                                 query_indices.push_back(j);
403                 if(!query_indices.empty())
404                 {
405                         glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
406                         for(unsigned j=0; j<query_indices.size(); ++j)
407                                 uniforms_by_index[query_indices[j]]->array_stride = values[j];
408                 }
409
410                 query_indices.clear();
411                 for(int j: indices)
412                 {
413                         DataType t = uniforms_by_index[j]->type;
414                         if(is_matrix(t))
415                                 query_indices.push_back(j);
416                 }
417                 if(!query_indices.empty())
418                 {
419                         glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
420                         for(unsigned j=0; j<query_indices.size(); ++j)
421                                 uniforms_by_index[query_indices[j]]->matrix_stride = values[j];
422                 }
423
424                 info.sort_uniforms();
425                 info.update_layout_hash();
426         }
427 }
428
429 void Program::query_attributes()
430 {
431         unsigned count = 0;
432         glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, reinterpret_cast<int *>(&count));
433         reflect_data.attributes.reserve(count);
434         for(unsigned i=0; i<count; ++i)
435         {
436                 char name[128];
437                 int len = 0;
438                 int size;
439                 GLenum type;
440                 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
441                 if(len && strncmp(name, "gl_", 3))
442                 {
443                         if(len>3 && !strcmp(name+len-3, "[0]"))
444                                 name[len-3] = 0;
445
446                         reflect_data.attributes.push_back(ReflectData::AttributeInfo());
447                         ReflectData::AttributeInfo &info = reflect_data.attributes.back();
448                         info.name = name;
449                         info.location = glGetAttribLocation(id, name);
450                         info.array_size = size;
451                         info.type = from_gl_type(type);
452                 }
453         }
454 }
455
456 void Program::collect_uniforms(const SpirVModule &mod, const map<unsigned, int> &spec_values)
457 {
458         // Prepare the default block
459         reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
460         vector<vector<string> > block_uniform_names(1);
461
462         for(const SpirVModule::Variable &v: mod.get_variables())
463         {
464                 if(v.storage==SpirVModule::UNIFORM && v.struct_type)
465                 {
466                         reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
467                         ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
468                         info.name = v.struct_type->name;
469                         info.bind_point = v.binding;
470                         info.data_size = v.struct_type->size;
471
472                         string prefix;
473                         if(!v.name.empty())
474                                 prefix = v.struct_type->name+".";
475                         block_uniform_names.push_back(vector<string>());
476                         collect_block_uniforms(*v.struct_type, prefix, 0, spec_values, block_uniform_names.back());
477                 }
478                 else if(v.storage==SpirVModule::UNIFORM_CONSTANT && v.location>=0)
479                 {
480                         block_uniform_names[0].push_back(v.name);
481                         reflect_data.uniforms.push_back(ReflectData::UniformInfo());
482                         ReflectData::UniformInfo &info = reflect_data.uniforms.back();
483                         info.name = v.name;
484                         info.tag = v.name;
485                         info.location = v.location;
486                         info.binding = v.binding;
487                         info.array_size = v.array_size;
488                         info.type = v.type;
489                 }
490         }
491
492         sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
493
494         for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
495         {
496                 ReflectData::UniformBlockInfo &block = reflect_data.uniform_blocks[i];
497                 for(const string &n: block_uniform_names[i])
498                 {
499                         // The element is already known to be present
500                         ReflectData::UniformInfo &uni = *lower_bound_member(reflect_data.uniforms, Tag(n), &ReflectData::UniformInfo::tag);
501                         block.uniforms.push_back(&uni);
502                         uni.block = &block;
503                 }
504                 block.sort_uniforms();
505                 block.update_layout_hash();
506         }
507
508         reflect_data.update_layout_hash();
509 }
510
511 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)
512 {
513         for(const SpirVModule::StructMember &m: strct.members)
514         {
515                 unsigned offset = base_offset+m.offset;
516                 if(m.struct_type)
517                 {
518                         unsigned array_size = m.array_size;
519                         if(m.array_size_spec)
520                         {
521                                 array_size = m.array_size_spec->i_value;
522                                 auto j = spec_values.find(m.array_size_spec->constant_id);
523                                 if(j!=spec_values.end())
524                                         array_size = j->second;
525                         }
526
527                         if(array_size)
528                         {
529                                 for(unsigned j=0; j<array_size; ++j, offset+=m.array_stride)
530                                         collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, spec_values, uniform_names);
531                         }
532                         else
533                                 collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, spec_values, uniform_names);
534                 }
535                 else
536                 {
537                         string name = prefix+m.name;
538                         uniform_names.push_back(name);
539                         reflect_data.uniforms.push_back(ReflectData::UniformInfo());
540                         ReflectData::UniformInfo &info = reflect_data.uniforms.back();
541                         info.name = name;
542                         info.tag = name;
543                         info.offset = offset;
544                         info.array_size = m.array_size;
545                         info.array_stride = m.array_stride;
546                         info.matrix_stride = m.matrix_stride;
547                         info.type = m.type;
548                 }
549         }
550 }
551
552 void Program::collect_attributes(const SpirVModule &mod)
553 {
554         for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
555                 if(e.stage==SpirVModule::VERTEX && e.name=="main")
556                 {
557                         for(const SpirVModule::Variable *v: e.globals)
558                                 if(v->storage==SpirVModule::INPUT)
559                                 {
560                                         reflect_data.attributes.push_back(ReflectData::AttributeInfo());
561                                         ReflectData::AttributeInfo &info = reflect_data.attributes.back();
562                                         info.name = v->name;
563                                         info.location = v->location;
564                                         info.array_size = v->array_size;
565                                         info.type = v->type;
566                                 }
567                 }
568 }
569
570 const ReflectData::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
571 {
572         auto i = find_member(reflect_data.uniform_blocks, name, &ReflectData::UniformBlockInfo::name);
573         if(i==reflect_data.uniform_blocks.end())
574                 throw key_error(name);
575         return *i;
576 }
577
578 const ReflectData::UniformInfo &Program::get_uniform_info(const string &name) const
579 {
580         auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
581         if(i==reflect_data.uniforms.end() || i->name!=name)
582                 throw key_error(name);
583         return *i;
584 }
585
586 const ReflectData::UniformInfo &Program::get_uniform_info(Tag tag) const
587 {
588         auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
589         if(i==reflect_data.uniforms.end() || i->tag!=tag)
590                 throw key_error(tag);
591         return *i;
592 }
593
594 int Program::get_uniform_location(const string &name) const
595 {
596         if(name[name.size()-1]==']')
597                 throw invalid_argument("Program::get_uniform_location");
598
599         auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
600         return i!=reflect_data.uniforms.end() && i->name==name && i->block->bind_point<0 ? i->location : -1;
601 }
602
603 int Program::get_uniform_location(Tag tag) const
604 {
605         auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
606         return i!=reflect_data.uniforms.end() && i->tag==tag && i->block->bind_point<0 ? i->location : -1;
607 }
608
609 int Program::get_uniform_binding(Tag tag) const
610 {
611         auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
612         return i!=reflect_data.uniforms.end() && i->tag==tag ? i->binding : -1;
613 }
614
615 const ReflectData::AttributeInfo &Program::get_attribute_info(const string &name) const
616 {
617         auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
618         if(i==reflect_data.attributes.end() || i->name!=name)
619                 throw key_error(name);
620         return *i;
621 }
622
623 int Program::get_attribute_location(const string &name) const
624 {
625         if(name[name.size()-1]==']')
626                 throw invalid_argument("Program::get_attribute_location");
627
628         auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
629         return i!=reflect_data.attributes.end() && i->name==name ? i->location : -1;
630 }
631
632 void Program::set_debug_name(const string &name)
633 {
634 #ifdef DEBUG
635         debug_name = name;
636         if(KHR_debug)
637         {
638                 glObjectLabel(GL_PROGRAM, id, name.size(), name.c_str());
639                 for(unsigned i=0; i<MAX_STAGES; ++i)
640                         if(stage_ids[i])
641                                 set_stage_debug_name(stage_ids[i], static_cast<Stage>(i));
642         }
643 #else
644         (void)name;
645 #endif
646 }
647
648 void Program::set_stage_debug_name(unsigned stage_id, Stage type)
649 {
650 #ifdef DEBUG
651         static const char *const suffixes[] = { " [VS]", " [GS]", " [FS]" };
652         string name = debug_name+suffixes[type];
653         glObjectLabel(GL_SHADER, stage_id, name.size(), name.c_str());
654 #else
655         (void)stage_id; (void)type;
656 #endif
657 }
658
659
660 Program::Loader::Loader(Program &p, Collection &c):
661         DataFile::CollectionObjectLoader<Program>(p, &c)
662 {
663         add("module", &Loader::module);
664 }
665
666 void Program::Loader::module(const string &n)
667 {
668         map<string, int> spec_values;
669         SpecializationLoader ldr(spec_values);
670         load_sub_with(ldr);
671         obj.add_stages(get_collection().get<Module>(n), spec_values);
672 }
673
674
675 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
676
677 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
678         spec_values(sv)
679 {
680         set_actions(shared_actions);
681 }
682
683 void Program::SpecializationLoader::init_actions()
684 {
685         add("specialize", &SpecializationLoader::specialize_bool);
686         add("specialize", &SpecializationLoader::specialize_int);
687 }
688
689 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
690 {
691         spec_values[name] = value;
692 }
693
694 void Program::SpecializationLoader::specialize_int(const string &name, int value)
695 {
696         spec_values[name] = value;
697 }
698
699 } // namespace GL
700 } // namespace Msp