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