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