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>
20 #include "resources.h"
22 #include "glsl/compiler.h"
34 Program::Program(const std::string &source)
39 mod.set_source(source);
46 Program::Program(const string &vert, const string &frag)
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
58 Program::Program(const Module &mod, const map<string, int> &spec_values)
61 add_stages(mod, spec_values);
67 static Require _req(ARB_shader_objects);
69 id = glCreateProgram();
76 for(vector<unsigned>::iterator i=stage_ids.begin(); i!=stage_ids.end(); ++i)
81 void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
83 if(!stage_ids.empty())
84 throw invalid_operation("Program::add_stages");
86 switch(mod.get_format())
88 case Module::GLSL: return add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values);
89 default: throw invalid_argument("Program::add_stages");
93 unsigned Program::add_stage(GLenum type)
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");
103 unsigned stage_id = glCreateShader(type);
104 stage_ids.push_back(stage_id);
105 glAttachShader(id, stage_id);
110 void Program::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values)
114 SL::Compiler compiler;
115 compiler.set_source(mod.get_prepared_source(), "<module>");
116 compiler.specialize(spec_values);
117 compiler.compile(SL::Compiler::PROGRAM);
119 string diagnostics = compiler.get_diagnostics();
120 if(!diagnostics.empty())
121 IO::print("Program diagnostics:\n%s\n", diagnostics);
124 vector<SL::Stage::Type> stages = compiler.get_stages();
125 for(vector<SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
127 unsigned stage_id = 0;
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");
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);
141 if(*i==SL::Stage::VERTEX)
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());
148 if(*i==SL::Stage::FRAGMENT && EXT_gpu_shader4)
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());
155 compile_glsl_stage(stage_id);
159 void Program::compile_glsl_stage(unsigned stage_id)
161 glCompileShader(stage_id);
162 bool compiled = get_shader_i(stage_id, GL_COMPILE_STATUS);
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);
172 throw compile_error(info_log);
174 if(!info_log.empty())
175 IO::print("Shader compile info log:\n%s", info_log);
179 #pragma GCC diagnostic push
180 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
181 void Program::attach_shader(Shader &shader)
183 unsigned shader_id = shader.steal_id();
185 throw invalid_argument("Program::attach_shader");
186 stage_ids.push_back(shader_id);
187 compile_glsl_stage(shader_id);
190 void Program::attach_shader_owned(Shader *shader)
192 attach_shader(*shader);
196 void Program::detach_shader(Shader &)
200 const vector<Shader *> &Program::get_attached_shaders() const
202 static vector<Shader *> dummy;
206 void Program::bind_attribute(unsigned index, const string &name)
208 static Require _req(ARB_vertex_shader);
209 glBindAttribLocation(id, index, name.c_str());
212 void Program::bind_attribute(VertexAttribute attr, const string &name)
214 bind_attribute(get_attribute_semantic(attr), name);
217 void Program::bind_fragment_data(unsigned index, const string &name)
219 static Require _req(EXT_gpu_shader4);
220 glBindFragDataLocation(id, index, name.c_str());
222 #pragma GCC diagnostic pop
226 if(stage_ids.empty())
227 throw invalid_operation("Program::link");
232 linked = get_program_i(id, GL_LINK_STATUS);
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);
242 throw compile_error(info_log);
244 if(!info_log.empty())
245 IO::print("Program link info log:\n%s", info_log);
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);
257 void Program::query_uniforms()
259 unsigned count = get_program_i(id, GL_ACTIVE_UNIFORMS);
260 vector<UniformInfo *> uniforms_by_index(count);
261 for(unsigned i=0; i<count; ++i)
267 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
268 if(len && strncmp(name, "gl_", 3))
270 /* Some implementations report the first element of a uniform array,
271 others report just the name of the array itself. */
272 if(len>3 && !strcmp(name+len-3, "[0]"))
275 UniformInfo &info = uniforms[name];
278 info.array_size = size;
279 info.array_stride = 0;
280 info.matrix_stride = 0;
281 info.type = from_gl_type(type);
282 uniforms_by_index[i] = &info;
286 if(ARB_uniform_buffer_object)
287 query_uniform_blocks(uniforms_by_index);
289 UniformBlockInfo &default_block = uniform_blocks[string()];
290 default_block.data_size = 0;
291 default_block.bind_point = -1;
293 for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
296 i->second.location = glGetUniformLocation(id, i->second.name.c_str());
297 i->second.block = &default_block;
298 default_block.uniforms.push_back(&i->second);
301 default_block.layout_hash = compute_layout_hash(default_block.uniforms);
303 string layout_descriptor;
304 for(UniformBlockMap::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
305 layout_descriptor += format("%d:%x\n", i->second.bind_point, i->second.layout_hash);
306 uniform_layout_hash = hash32(layout_descriptor);
309 void Program::query_uniform_blocks(const vector<UniformInfo *> &uniforms_by_index)
311 uniform_blocks.clear();
313 std::set<unsigned> used_bind_points;
314 unsigned count = get_program_i(id, GL_ACTIVE_UNIFORM_BLOCKS);
315 for(unsigned i=0; i<count; ++i)
319 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
320 UniformBlockInfo &info = uniform_blocks[name];
324 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
325 info.data_size = value;
327 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
328 vector<int> indices(value);
329 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
330 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
332 if(!uniforms_by_index[*j])
333 throw logic_error("Program::link");
334 info.uniforms.push_back(uniforms_by_index[*j]);
335 uniforms_by_index[*j]->block = &info;
338 vector<unsigned> indices2(indices.begin(), indices.end());
339 vector<int> values(indices.size());
340 glGetActiveUniformsiv(id, indices.size(), &indices2[0], GL_UNIFORM_OFFSET, &values[0]);
341 for(unsigned j=0; j<indices.size(); ++j)
342 uniforms_by_index[indices[j]]->location = values[j];
345 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
346 if(uniforms_by_index[*j]->array_size>1)
347 indices2.push_back(*j);
348 if(!indices2.empty())
350 glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
351 for(unsigned j=0; j<indices2.size(); ++j)
352 uniforms_by_index[indices2[j]]->array_stride = values[j];
356 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
358 DataType t = uniforms_by_index[*j]->type;
360 indices2.push_back(*j);
362 if(!indices2.empty())
364 glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
365 for(unsigned j=0; j<indices2.size(); ++j)
366 uniforms_by_index[indices2[j]]->matrix_stride = values[j];
369 sort(info.uniforms.begin(), info.uniforms.end(), uniform_location_compare);
370 info.layout_hash = compute_layout_hash(info.uniforms);
371 unsigned n_bindings = BufferRange::get_n_uniform_buffer_bindings();
372 info.bind_point = info.layout_hash%n_bindings;
373 while(used_bind_points.count(info.bind_point))
374 info.bind_point = (info.bind_point+1)%n_bindings;
375 glUniformBlockBinding(id, i, info.bind_point);
376 used_bind_points.insert(info.bind_point);
380 void Program::query_attributes()
382 unsigned count = get_program_i(id, GL_ACTIVE_ATTRIBUTES);
383 for(unsigned i=0; i<count; ++i)
389 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
390 if(len && strncmp(name, "gl_", 3))
392 if(len>3 && !strcmp(name+len-3, "[0]"))
395 AttributeInfo &info = attributes[name];
397 info.location = glGetAttribLocation(id, name);
398 info.array_size = size;
399 info.type = from_gl_type(type);
404 Program::LayoutHash Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
406 string layout_descriptor;
407 for(vector<const UniformInfo *>::const_iterator i = uniforms.begin(); i!=uniforms.end(); ++i)
408 layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->array_size);
409 return hash32(layout_descriptor);
412 bool Program::uniform_location_compare(const UniformInfo *uni1, const UniformInfo *uni2)
414 return uni1->location<uni2->location;
417 string Program::get_info_log() const
419 GLsizei len = get_program_i(id, GL_INFO_LOG_LENGTH);
420 string log(len+1, 0);
421 glGetProgramInfoLog(id, len+1, &len, &log[0]);
426 const Program::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
428 return get_item(uniform_blocks, name);
431 const Program::UniformInfo &Program::get_uniform_info(const string &name) const
433 return get_item(uniforms, name);
436 int Program::get_uniform_location(const string &n) const
438 if(n[n.size()-1]==']')
439 throw invalid_argument("Program::get_uniform_location");
441 UniformMap::const_iterator i = uniforms.find(n);
442 if(i==uniforms.end())
445 return i->second.block->bind_point<0 ? i->second.location : -1;
448 const Program::AttributeInfo &Program::get_attribute_info(const string &name) const
450 return get_item(attributes, name);
453 int Program::get_attribute_location(const string &n) const
455 if(n[n.size()-1]==']')
456 throw invalid_argument("Program::get_attribute_location");
458 AttributeMap::const_iterator i = attributes.find(n);
459 return i!=attributes.end() ? i->second.location : -1;
462 void Program::bind() const
465 throw invalid_operation("Program::bind");
467 if(!set_current(this))
473 void Program::unbind()
482 Program::Loader::Loader(Program &p, Collection &c):
483 DataFile::CollectionObjectLoader<Program>(p, &c)
485 add("module", &Loader::module);
488 add("attribute", &Loader::attribute);
489 add("fragment_shader", &Loader::fragment_shader);
490 add("geometry_shader", &Loader::geometry_shader);
491 add("vertex_shader", &Loader::vertex_shader);
494 void Program::Loader::finish()
499 void Program::Loader::module(const string &n)
501 map<string, int> spec_values;
502 SpecializationLoader ldr(spec_values);
504 obj.add_stages(get_collection().get<Module>(n), spec_values);
507 #pragma GCC diagnostic push
508 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
509 void Program::Loader::attribute(unsigned i, const string &n)
511 obj.bind_attribute(i, n);
514 void Program::Loader::fragment_shader(const string &src)
516 obj.attach_shader_owned(new FragmentShader(src));
519 void Program::Loader::geometry_shader(const string &src)
521 obj.attach_shader_owned(new GeometryShader(src));
524 void Program::Loader::vertex_shader(const string &src)
526 obj.attach_shader_owned(new VertexShader(src));
528 #pragma GCC diagnostic pop
531 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
533 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
536 set_actions(shared_actions);
539 void Program::SpecializationLoader::init_actions()
541 add("specialize", &SpecializationLoader::specialize_bool);
542 add("specialize", &SpecializationLoader::specialize_int);
545 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
547 spec_values[name] = value;
550 void Program::SpecializationLoader::specialize_int(const string &name, int value)
552 spec_values[name] = value;