]> git.tdb.fi Git - libs/gl.git/blob - source/core/program.cpp
c41976ed8eef270a238de46b837e529ad4904dd9
[libs/gl.git] / source / core / program.cpp
1 #include <algorithm>
2 #include <cstring>
3 #include <set>
4 #include <msp/core/hash.h>
5 #include <msp/core/maputils.h>
6 #include <msp/gl/extensions/arb_fragment_shader.h>
7 #include <msp/gl/extensions/arb_geometry_shader4.h>
8 #include <msp/gl/extensions/arb_shader_objects.h>
9 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
10 #include <msp/gl/extensions/arb_vertex_shader.h>
11 #include <msp/gl/extensions/ext_gpu_shader4.h>
12 #include <msp/gl/extensions/nv_non_square_matrices.h>
13 #include <msp/io/print.h>
14 #include <msp/strings/format.h>
15 #include "buffer.h"
16 #include "error.h"
17 #include "misc.h"
18 #include "module.h"
19 #include "program.h"
20 #include "resources.h"
21 #include "shader.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 std::string &source)
35 {
36         init();
37
38         GlslModule mod;
39         mod.set_source(source);
40         add_stages(mod);
41
42         link();
43         module = 0;
44 }
45
46 Program::Program(const string &vert, const string &frag)
47 {
48         init();
49
50 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
52         attach_shader_owned(new VertexShader(vert));
53         attach_shader_owned(new FragmentShader(frag));
54 #pragma GCC diagnostic pop
55         link();
56 }
57
58 Program::Program(const Module &mod, const map<string, int> &spec_values)
59 {
60         init();
61         add_stages(mod, spec_values);
62         link();
63 }
64
65 void Program::init()
66 {
67         static Require _req(ARB_shader_objects);
68
69         id = glCreateProgram();
70         module = 0;
71         linked = false;
72 }
73
74 Program::~Program()
75 {
76         for(vector<unsigned>::iterator i=stage_ids.begin(); i!=stage_ids.end(); ++i)
77                 glDeleteShader(*i);
78         glDeleteProgram(id);
79 }
80
81 void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
82 {
83         if(!stage_ids.empty())
84                 throw invalid_operation("Program::add_stages");
85
86         switch(mod.get_format())
87         {
88         case Module::GLSL: return add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values);
89         default: throw invalid_argument("Program::add_stages");
90         }
91 }
92
93 unsigned Program::add_stage(GLenum type)
94 {
95         switch(type)
96         {
97         case GL_VERTEX_SHADER: { static Require _req(ARB_vertex_shader); } break;
98         case GL_GEOMETRY_SHADER: { static Require _req(ARB_geometry_shader4); } break;
99         case GL_FRAGMENT_SHADER: { static Require _req(ARB_fragment_shader); } break;
100         default: throw invalid_argument("Program::add_stage");
101         }
102
103         unsigned stage_id = glCreateShader(type);
104         stage_ids.push_back(stage_id);
105         glAttachShader(id, stage_id);
106
107         return stage_id;
108 }
109
110 void Program::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values)
111 {
112         module = &mod;
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         for(vector<SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
126         {
127                 unsigned stage_id = 0;
128                 switch(*i)
129                 {
130                 case SL::Stage::VERTEX: stage_id = add_stage(GL_VERTEX_SHADER); break;
131                 case SL::Stage::GEOMETRY: stage_id = add_stage(GL_GEOMETRY_SHADER); break;
132                 case SL::Stage::FRAGMENT: stage_id = add_stage(GL_FRAGMENT_SHADER); break;
133                 default: throw invalid_operation("Program::add_glsl_stages");
134                 }
135
136                 string stage_src = compiler.get_stage_glsl(*i);
137                 const char *src_ptr = stage_src.data();
138                 int src_len = stage_src.size();
139                 glShaderSource(stage_id, 1, &src_ptr, &src_len);
140
141                 if(*i==SL::Stage::VERTEX)
142                 {
143                         const map<string, unsigned> &attribs = compiler.get_vertex_attributes();
144                         for(map<string, unsigned>::const_iterator j=attribs.begin(); j!=attribs.end(); ++j)
145                                 glBindAttribLocation(id, j->second, j->first.c_str());
146                 }
147
148                 if(*i==SL::Stage::FRAGMENT && EXT_gpu_shader4)
149                 {
150                         const map<string, unsigned> &frag_outs = compiler.get_fragment_outputs();
151                         for(map<string, unsigned>::const_iterator j=frag_outs.begin(); j!=frag_outs.end(); ++j)
152                                 glBindFragDataLocation(id, j->second, j->first.c_str());
153                 }
154
155                 compile_glsl_stage(stage_id);
156         }
157 }
158
159 void Program::compile_glsl_stage(unsigned stage_id)
160 {
161         glCompileShader(stage_id);
162         bool compiled = get_shader_i(stage_id, GL_COMPILE_STATUS);
163
164         GLsizei info_log_len = get_shader_i(stage_id, GL_INFO_LOG_LENGTH);
165         string info_log(info_log_len+1, 0);
166         glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]);
167         info_log.erase(info_log_len);
168         if(module && module->get_format()==Module::GLSL)
169                 info_log = static_cast<const GlslModule *>(module)->get_source_map().translate_errors(info_log);
170
171         if(!compiled)
172                 throw compile_error(info_log);
173 #ifdef DEBUG
174         if(!info_log.empty())
175                 IO::print("Shader compile info log:\n%s", info_log);
176 #endif
177 }
178
179 #pragma GCC diagnostic push
180 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
181 void Program::attach_shader(Shader &shader)
182 {
183         unsigned shader_id = shader.steal_id();
184         if(!shader_id)
185                 throw invalid_argument("Program::attach_shader");
186         stage_ids.push_back(shader_id);
187         compile_glsl_stage(shader_id);
188 }
189
190 void Program::attach_shader_owned(Shader *shader)
191 {
192         attach_shader(*shader);
193         delete shader;
194 }
195
196 void Program::detach_shader(Shader &)
197 {
198 }
199
200 const vector<Shader *> &Program::get_attached_shaders() const
201 {
202         static vector<Shader *> dummy;
203         return dummy;
204 }
205
206 void Program::bind_attribute(unsigned index, const string &name)
207 {
208         static Require _req(ARB_vertex_shader);
209         glBindAttribLocation(id, index, name.c_str());
210 }
211
212 void Program::bind_attribute(VertexAttribute attr, const string &name)
213 {
214         bind_attribute(get_attribute_semantic(attr), name);
215 }
216
217 void Program::bind_fragment_data(unsigned index, const string &name)
218 {
219         static Require _req(EXT_gpu_shader4);
220         glBindFragDataLocation(id, index, name.c_str());
221 }
222 #pragma GCC diagnostic pop
223
224 void Program::link()
225 {
226         if(stage_ids.empty())
227                 throw invalid_operation("Program::link");
228
229         uniforms.clear();
230
231         glLinkProgram(id);
232         linked = get_program_i(id, GL_LINK_STATUS);
233
234         GLsizei info_log_len = get_program_i(id, GL_INFO_LOG_LENGTH);
235         string info_log(info_log_len+1, 0);
236         glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]);
237         info_log.erase(info_log_len);
238         if(module && module->get_format()==Module::GLSL)
239                 info_log = static_cast<const GlslModule *>(module)->get_source_map().translate_errors(info_log);
240
241         if(!linked)
242                 throw compile_error(info_log);
243 #ifdef DEBUG
244         if(!info_log.empty())
245                 IO::print("Program link info log:\n%s", info_log);
246 #endif
247
248         query_uniforms();
249         query_attributes();
250
251         for(UniformMap::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
252                 require_type(i->second.type);
253         for(AttributeMap::const_iterator i=attributes.begin(); i!=attributes.end(); ++i)
254                 require_type(i->second.type);
255 }
256
257 void Program::require_type(GLenum t)
258 {
259         switch(t)
260         {
261         case GL_FLOAT_MAT2x3:
262         case GL_FLOAT_MAT2x4:
263         case GL_FLOAT_MAT3x2:
264         case GL_FLOAT_MAT3x4:
265         case GL_FLOAT_MAT4x2:
266         case GL_FLOAT_MAT4x3:
267                 { static Require _req(NV_non_square_matrices); }
268                 break;
269         }
270 }
271
272 void Program::query_uniforms()
273 {
274         unsigned count = get_program_i(id, GL_ACTIVE_UNIFORMS);
275         vector<UniformInfo *> uniforms_by_index(count);
276         for(unsigned i=0; i<count; ++i)
277         {
278                 char name[128];
279                 int len = 0;
280                 int size;
281                 GLenum type;
282                 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
283                 if(len && strncmp(name, "gl_", 3))
284                 {
285                         /* Some implementations report the first element of a uniform array,
286                         others report just the name of the array itself. */
287                         if(len>3 && !strcmp(name+len-3, "[0]"))
288                                 name[len-3] = 0;
289
290                         UniformInfo &info = uniforms[name];
291                         info.block = 0;
292                         info.name = name;
293                         info.size = size;
294                         info.array_stride = 0;
295                         info.matrix_stride = 0;
296                         info.type = type;
297                         uniforms_by_index[i] = &info;
298                 }
299         }
300
301         if(ARB_uniform_buffer_object)
302                 query_uniform_blocks(uniforms_by_index);
303
304         UniformBlockInfo &default_block = uniform_blocks[string()];
305         default_block.data_size = 0;
306         default_block.bind_point = -1;
307
308         for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
309                 if(!i->second.block)
310                 {
311                         i->second.location = glGetUniformLocation(id, i->second.name.c_str());
312                         i->second.block = &default_block;
313                         default_block.uniforms.push_back(&i->second);
314                 }
315
316         default_block.layout_hash = compute_layout_hash(default_block.uniforms);
317
318         string layout_descriptor;
319         for(UniformBlockMap::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
320                 layout_descriptor += format("%d:%x\n", i->second.bind_point, i->second.layout_hash);
321         uniform_layout_hash = hash32(layout_descriptor);
322 }
323
324 void Program::query_uniform_blocks(const vector<UniformInfo *> &uniforms_by_index)
325 {
326         uniform_blocks.clear();
327
328         std::set<unsigned> used_bind_points;
329         unsigned count = get_program_i(id, GL_ACTIVE_UNIFORM_BLOCKS);
330         for(unsigned i=0; i<count; ++i)
331         {
332                 char name[128];
333                 int len;
334                 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
335                 UniformBlockInfo &info = uniform_blocks[name];
336                 info.name = name;
337
338                 int value;
339                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
340                 info.data_size = value;
341
342                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
343                 vector<int> indices(value);
344                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
345                 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
346                 {
347                         if(!uniforms_by_index[*j])
348                                 throw logic_error("Program::link");
349                         info.uniforms.push_back(uniforms_by_index[*j]);
350                         uniforms_by_index[*j]->block = &info;
351                 }
352
353                 vector<unsigned> indices2(indices.begin(), indices.end());
354                 vector<int> values(indices.size());
355                 glGetActiveUniformsiv(id, indices.size(), &indices2[0], GL_UNIFORM_OFFSET, &values[0]);
356                 for(unsigned j=0; j<indices.size(); ++j)
357                         uniforms_by_index[indices[j]]->location = values[j];
358
359                 indices2.clear();
360                 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
361                         if(uniforms_by_index[*j]->size>1)
362                                 indices2.push_back(*j);
363                 if(!indices2.empty())
364                 {
365                         glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
366                         for(unsigned j=0; j<indices2.size(); ++j)
367                                 uniforms_by_index[indices2[j]]->array_stride = values[j];
368                 }
369
370                 indices2.clear();
371                 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
372                 {
373                         GLenum t = uniforms_by_index[*j]->type;
374                         if(t==GL_FLOAT_MAT4 || t==GL_FLOAT_MAT3 || t==GL_FLOAT_MAT2 ||
375                                 t==GL_FLOAT_MAT2x3 || t==GL_FLOAT_MAT2x4 || t==GL_FLOAT_MAT3x2 ||
376                                 t==GL_FLOAT_MAT3x4 || t==GL_FLOAT_MAT4x2 || t==GL_FLOAT_MAT4x3)
377                                 indices2.push_back(*j);
378                 }
379                 if(!indices2.empty())
380                 {
381                         glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
382                         for(unsigned j=0; j<indices2.size(); ++j)
383                                 uniforms_by_index[indices2[j]]->matrix_stride = values[j];
384                 }
385
386                 sort(info.uniforms.begin(), info.uniforms.end(), uniform_location_compare);
387                 info.layout_hash = compute_layout_hash(info.uniforms);
388                 unsigned n_bindings = BufferRange::get_n_uniform_buffer_bindings();
389                 info.bind_point = info.layout_hash%n_bindings;
390                 while(used_bind_points.count(info.bind_point))
391                         info.bind_point = (info.bind_point+1)%n_bindings;
392                 glUniformBlockBinding(id, i, info.bind_point);
393                 used_bind_points.insert(info.bind_point);
394         }
395 }
396
397 void Program::query_attributes()
398 {
399         unsigned count = get_program_i(id, GL_ACTIVE_ATTRIBUTES);
400         for(unsigned i=0; i<count; ++i)
401         {
402                 char name[128];
403                 int len = 0;
404                 int size;
405                 GLenum type;
406                 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
407                 if(len && strncmp(name, "gl_", 3))
408                 {
409                         if(len>3 && !strcmp(name+len-3, "[0]"))
410                                 name[len-3] = 0;
411
412                         AttributeInfo &info = attributes[name];
413                         info.name = name;
414                         info.location = glGetAttribLocation(id, name);
415                         info.size = size;
416                         info.type = type;
417                 }
418         }
419 }
420
421 Program::LayoutHash Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
422 {
423         string layout_descriptor;
424         for(vector<const UniformInfo *>::const_iterator i = uniforms.begin(); i!=uniforms.end(); ++i)
425                 layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->size);
426         return hash32(layout_descriptor);
427 }
428
429 bool Program::uniform_location_compare(const UniformInfo *uni1, const UniformInfo *uni2)
430 {
431         return uni1->location<uni2->location;
432 }
433
434 string Program::get_info_log() const
435 {
436         GLsizei len = get_program_i(id, GL_INFO_LOG_LENGTH);
437         string log(len+1, 0);
438         glGetProgramInfoLog(id, len+1, &len, &log[0]);
439         log.erase(len);
440         return log;
441 }
442
443 const Program::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
444 {
445         return get_item(uniform_blocks, name);
446 }
447
448 const Program::UniformInfo &Program::get_uniform_info(const string &name) const
449 {
450         return get_item(uniforms, name);
451 }
452
453 int Program::get_uniform_location(const string &n) const
454 {
455         if(n[n.size()-1]==']')
456                 throw invalid_argument("Program::get_uniform_location");
457
458         UniformMap::const_iterator i = uniforms.find(n);
459         if(i==uniforms.end())
460                 return -1;
461
462         return i->second.block->bind_point<0 ? i->second.location : -1;
463 }
464
465 const Program::AttributeInfo &Program::get_attribute_info(const string &name) const
466 {
467         return get_item(attributes, name);
468 }
469
470 int Program::get_attribute_location(const string &n) const
471 {
472         if(n[n.size()-1]==']')
473                 throw invalid_argument("Program::get_attribute_location");
474
475         AttributeMap::const_iterator i = attributes.find(n);
476         return i!=attributes.end() ? i->second.location : -1;
477 }
478
479 void Program::bind() const
480 {
481         if(!linked)
482                 throw invalid_operation("Program::bind");
483
484         if(!set_current(this))
485                 return;
486
487         glUseProgram(id);
488 }
489
490 void Program::unbind()
491 {
492         if(!set_current(0))
493                 return;
494
495         glUseProgram(0);
496 }
497
498
499 Program::Loader::Loader(Program &p, Collection &c):
500         DataFile::CollectionObjectLoader<Program>(p, &c)
501 {
502         add("module",          &Loader::module);
503
504         // Deprecated
505         add("attribute",       &Loader::attribute);
506         add("fragment_shader", &Loader::fragment_shader);
507         add("geometry_shader", &Loader::geometry_shader);
508         add("vertex_shader",   &Loader::vertex_shader);
509 }
510
511 void Program::Loader::finish()
512 {
513         obj.link();
514 }
515
516 void Program::Loader::module(const string &n)
517 {
518         map<string, int> spec_values;
519         SpecializationLoader ldr(spec_values);
520         load_sub_with(ldr);
521         obj.add_stages(get_collection().get<Module>(n), spec_values);
522 }
523
524 #pragma GCC diagnostic push
525 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
526 void Program::Loader::attribute(unsigned i, const string &n)
527 {
528         obj.bind_attribute(i, n);
529 }
530
531 void Program::Loader::fragment_shader(const string &src)
532 {
533         obj.attach_shader_owned(new FragmentShader(src));
534 }
535
536 void Program::Loader::geometry_shader(const string &src)
537 {
538         obj.attach_shader_owned(new GeometryShader(src));
539 }
540
541 void Program::Loader::vertex_shader(const string &src)
542 {
543         obj.attach_shader_owned(new VertexShader(src));
544 }
545 #pragma GCC diagnostic pop
546
547
548 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
549
550 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
551         spec_values(sv)
552 {
553         set_actions(shared_actions);
554 }
555
556 void Program::SpecializationLoader::init_actions()
557 {
558         add("specialize", &SpecializationLoader::specialize_bool);
559         add("specialize", &SpecializationLoader::specialize_int);
560 }
561
562 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
563 {
564         spec_values[name] = value;
565 }
566
567 void Program::SpecializationLoader::specialize_int(const string &name, int value)
568 {
569         spec_values[name] = value;
570 }
571
572 } // namespace GL
573 } // namespace Msp