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