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