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