4 #include <msp/core/hash.h>
5 #include <msp/core/maputils.h>
6 #include <msp/gl/extensions/arb_es2_compatibility.h>
7 #include <msp/gl/extensions/arb_fragment_shader.h>
8 #include <msp/gl/extensions/arb_gl_spirv.h>
9 #include <msp/gl/extensions/arb_geometry_shader4.h>
10 #include <msp/gl/extensions/arb_shader_objects.h>
11 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
12 #include <msp/gl/extensions/arb_vertex_shader.h>
13 #include <msp/gl/extensions/ext_gpu_shader4.h>
14 #include <msp/gl/extensions/nv_non_square_matrices.h>
15 #include <msp/io/print.h>
16 #include <msp/strings/format.h>
21 #include "resources.h"
23 #include "glsl/compiler.h"
35 Program::Program(const std::string &source)
40 mod.set_source(source);
47 Program::Program(const string &vert, const string &frag)
51 #pragma GCC diagnostic push
52 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
53 attach_shader_owned(new VertexShader(vert));
54 attach_shader_owned(new FragmentShader(frag));
55 #pragma GCC diagnostic pop
59 Program::Program(const Module &mod, const map<string, int> &spec_values)
62 add_stages(mod, spec_values);
68 static Require _req(ARB_shader_objects);
70 id = glCreateProgram();
77 for(vector<unsigned>::iterator i=stage_ids.begin(); i!=stage_ids.end(); ++i)
82 void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
84 if(!stage_ids.empty())
85 throw invalid_operation("Program::add_stages");
87 switch(mod.get_format())
89 case Module::GLSL: return add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values);
90 case Module::SPIR_V: return add_spirv_stages(static_cast<const SpirVModule &>(mod), spec_values);
91 default: throw invalid_argument("Program::add_stages");
95 unsigned Program::add_stage(GLenum type)
99 case GL_VERTEX_SHADER: { static Require _req(ARB_vertex_shader); } break;
100 case GL_GEOMETRY_SHADER: { static Require _req(ARB_geometry_shader4); } break;
101 case GL_FRAGMENT_SHADER: { static Require _req(ARB_fragment_shader); } break;
102 default: throw invalid_argument("Program::add_stage");
105 unsigned stage_id = glCreateShader(type);
106 stage_ids.push_back(stage_id);
107 glAttachShader(id, stage_id);
112 void Program::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values)
116 SL::Compiler compiler;
117 compiler.set_source(mod.get_prepared_source(), "<module>");
118 compiler.specialize(spec_values);
119 compiler.compile(SL::Compiler::PROGRAM);
121 string diagnostics = compiler.get_diagnostics();
122 if(!diagnostics.empty())
123 IO::print("Program diagnostics:\n%s\n", diagnostics);
126 vector<SL::Stage::Type> stages = compiler.get_stages();
127 for(vector<SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
129 unsigned stage_id = 0;
132 case SL::Stage::VERTEX: stage_id = add_stage(GL_VERTEX_SHADER); break;
133 case SL::Stage::GEOMETRY: stage_id = add_stage(GL_GEOMETRY_SHADER); break;
134 case SL::Stage::FRAGMENT: stage_id = add_stage(GL_FRAGMENT_SHADER); break;
135 default: throw invalid_operation("Program::add_glsl_stages");
138 string stage_src = compiler.get_stage_glsl(*i);
139 const char *src_ptr = stage_src.data();
140 int src_len = stage_src.size();
141 glShaderSource(stage_id, 1, &src_ptr, &src_len);
143 if(*i==SL::Stage::VERTEX)
145 const map<string, unsigned> &attribs = compiler.get_vertex_attributes();
146 for(map<string, unsigned>::const_iterator j=attribs.begin(); j!=attribs.end(); ++j)
147 glBindAttribLocation(id, j->second, j->first.c_str());
150 if(*i==SL::Stage::FRAGMENT && EXT_gpu_shader4)
152 const map<string, unsigned> &frag_outs = compiler.get_fragment_outputs();
153 for(map<string, unsigned>::const_iterator j=frag_outs.begin(); j!=frag_outs.end(); ++j)
154 glBindFragDataLocation(id, j->second, j->first.c_str());
157 compile_glsl_stage(stage_id);
161 void Program::compile_glsl_stage(unsigned stage_id)
163 glCompileShader(stage_id);
164 bool compiled = get_shader_i(stage_id, GL_COMPILE_STATUS);
166 GLsizei info_log_len = get_shader_i(stage_id, GL_INFO_LOG_LENGTH);
167 string info_log(info_log_len+1, 0);
168 glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]);
169 info_log.erase(info_log_len);
170 if(module && module->get_format()==Module::GLSL)
171 info_log = static_cast<const GlslModule *>(module)->get_source_map().translate_errors(info_log);
174 throw compile_error(info_log);
176 if(!info_log.empty())
177 IO::print("Shader compile info log:\n%s", info_log);
181 void Program::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
183 static Require _req(ARB_gl_spirv);
184 static Require _req2(ARB_ES2_compatibility);
188 const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
189 std::set<SpirVModule::Stage> stages;
190 for(vector<SpirVModule::EntryPoint>::const_iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
192 if(stages.count(i->stage))
193 throw invalid_argument("Program::add_spirv_stages");
197 case SpirVModule::VERTEX: add_stage(GL_VERTEX_SHADER); break;
198 case SpirVModule::GEOMETRY: add_stage(GL_GEOMETRY_SHADER); break;
199 case SpirVModule::FRAGMENT: add_stage(GL_FRAGMENT_SHADER); break;
200 default: throw invalid_operation("Program::add_spirv_stages");
203 stages.insert(i->stage);
206 const vector<UInt32> &code = mod.get_code();
207 glShaderBinary(stage_ids.size(), &stage_ids[0], GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
209 const vector<SpirVModule::SpecConstant> &spec_consts = mod.get_spec_constants();
210 vector<unsigned> spec_id_array;
211 vector<unsigned> spec_value_array;
212 spec_id_array.reserve(spec_consts.size());
213 spec_value_array.reserve(spec_consts.size());
214 for(vector<SpirVModule::SpecConstant>::const_iterator i=spec_consts.begin(); i!=spec_consts.end(); ++i)
216 map<string, int>::const_iterator j = spec_values.find(i->name);
217 if(j!=spec_values.end())
219 spec_id_array.push_back(i->constant_id);
220 spec_value_array.push_back(j->second);
224 vector<SpirVModule::EntryPoint>::const_iterator j=entry_points.begin();
225 for(vector<unsigned>::const_iterator i=stage_ids.begin(); i!=stage_ids.end(); ++i, ++j)
226 glSpecializeShader(*i, j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
229 #pragma GCC diagnostic push
230 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
231 void Program::attach_shader(Shader &shader)
233 unsigned shader_id = shader.steal_id();
235 throw invalid_argument("Program::attach_shader");
236 stage_ids.push_back(shader_id);
237 compile_glsl_stage(shader_id);
240 void Program::attach_shader_owned(Shader *shader)
242 attach_shader(*shader);
246 void Program::detach_shader(Shader &)
250 const vector<Shader *> &Program::get_attached_shaders() const
252 static vector<Shader *> dummy;
256 void Program::bind_attribute(unsigned index, const string &name)
258 static Require _req(ARB_vertex_shader);
259 glBindAttribLocation(id, index, name.c_str());
262 void Program::bind_attribute(VertexAttribute attr, const string &name)
264 bind_attribute(get_attribute_semantic(attr), name);
267 void Program::bind_fragment_data(unsigned index, const string &name)
269 static Require _req(EXT_gpu_shader4);
270 glBindFragDataLocation(id, index, name.c_str());
272 #pragma GCC diagnostic pop
276 if(stage_ids.empty())
277 throw invalid_operation("Program::link");
282 linked = get_program_i(id, GL_LINK_STATUS);
284 GLsizei info_log_len = get_program_i(id, GL_INFO_LOG_LENGTH);
285 string info_log(info_log_len+1, 0);
286 glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]);
287 info_log.erase(info_log_len);
288 if(module && module->get_format()==Module::GLSL)
289 info_log = static_cast<const GlslModule *>(module)->get_source_map().translate_errors(info_log);
292 throw compile_error(info_log);
294 if(!info_log.empty())
295 IO::print("Program link info log:\n%s", info_log);
298 if(module->get_format()==Module::GLSL)
303 else if(module->get_format()==Module::SPIR_V)
306 collect_attributes();
309 for(UniformMap::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
310 require_type(i->second.type);
311 for(AttributeMap::const_iterator i=attributes.begin(); i!=attributes.end(); ++i)
312 require_type(i->second.type);
315 void Program::query_uniforms()
317 unsigned count = get_program_i(id, GL_ACTIVE_UNIFORMS);
318 vector<UniformInfo *> uniforms_by_index(count);
319 for(unsigned i=0; i<count; ++i)
325 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
326 if(len && strncmp(name, "gl_", 3))
328 /* Some implementations report the first element of a uniform array,
329 others report just the name of the array itself. */
330 if(len>3 && !strcmp(name+len-3, "[0]"))
333 UniformInfo &info = uniforms[name];
336 info.array_size = size;
337 info.array_stride = 0;
338 info.matrix_stride = 0;
339 info.type = from_gl_type(type);
340 uniforms_by_index[i] = &info;
344 if(ARB_uniform_buffer_object)
345 query_uniform_blocks(uniforms_by_index);
347 UniformBlockInfo &default_block = uniform_blocks[string()];
348 default_block.data_size = 0;
349 default_block.bind_point = -1;
351 for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
354 i->second.location = glGetUniformLocation(id, i->second.name.c_str());
355 i->second.block = &default_block;
356 default_block.uniforms.push_back(&i->second);
359 default_block.layout_hash = compute_layout_hash(default_block.uniforms);
361 update_layout_hash();
364 void Program::query_uniform_blocks(const vector<UniformInfo *> &uniforms_by_index)
366 uniform_blocks.clear();
368 std::set<unsigned> used_bind_points;
369 unsigned count = get_program_i(id, GL_ACTIVE_UNIFORM_BLOCKS);
370 for(unsigned i=0; i<count; ++i)
374 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
375 UniformBlockInfo &info = uniform_blocks[name];
379 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
380 info.data_size = value;
382 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
383 vector<int> indices(value);
384 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
385 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
387 if(!uniforms_by_index[*j])
388 throw logic_error("Program::link");
389 info.uniforms.push_back(uniforms_by_index[*j]);
390 uniforms_by_index[*j]->block = &info;
393 vector<unsigned> indices2(indices.begin(), indices.end());
394 vector<int> values(indices.size());
395 glGetActiveUniformsiv(id, indices.size(), &indices2[0], GL_UNIFORM_OFFSET, &values[0]);
396 for(unsigned j=0; j<indices.size(); ++j)
397 uniforms_by_index[indices[j]]->location = values[j];
400 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
401 if(uniforms_by_index[*j]->array_size>1)
402 indices2.push_back(*j);
403 if(!indices2.empty())
405 glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
406 for(unsigned j=0; j<indices2.size(); ++j)
407 uniforms_by_index[indices2[j]]->array_stride = values[j];
411 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
413 DataType t = uniforms_by_index[*j]->type;
415 indices2.push_back(*j);
417 if(!indices2.empty())
419 glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
420 for(unsigned j=0; j<indices2.size(); ++j)
421 uniforms_by_index[indices2[j]]->matrix_stride = values[j];
424 sort(info.uniforms.begin(), info.uniforms.end(), uniform_location_compare);
425 info.layout_hash = compute_layout_hash(info.uniforms);
426 unsigned n_bindings = BufferRange::get_n_uniform_buffer_bindings();
427 info.bind_point = info.layout_hash%n_bindings;
428 while(used_bind_points.count(info.bind_point))
429 info.bind_point = (info.bind_point+1)%n_bindings;
430 glUniformBlockBinding(id, i, info.bind_point);
431 used_bind_points.insert(info.bind_point);
435 void Program::query_attributes()
437 unsigned count = get_program_i(id, GL_ACTIVE_ATTRIBUTES);
438 for(unsigned i=0; i<count; ++i)
444 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
445 if(len && strncmp(name, "gl_", 3))
447 if(len>3 && !strcmp(name+len-3, "[0]"))
450 AttributeInfo &info = attributes[name];
452 info.location = glGetAttribLocation(id, name);
453 info.array_size = size;
454 info.type = from_gl_type(type);
459 void Program::collect_uniforms()
461 const SpirVModule &mod = static_cast<const SpirVModule &>(*module);
463 UniformBlockInfo &default_block = uniform_blocks[string()];
464 default_block.data_size = 0;
465 default_block.bind_point = -1;
467 const vector<SpirVModule::Variable> &variables = mod.get_variables();
468 for(vector<SpirVModule::Variable>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
470 if(i->storage==SpirVModule::UNIFORM && i->struct_type)
472 UniformBlockInfo &info = uniform_blocks[i->struct_type->name];
473 info.name = i->struct_type->name;
474 info.bind_point = i->binding;
475 info.data_size = i->struct_type->size;
479 prefix = i->struct_type->name+".";
480 collect_block_uniforms(info, *i->struct_type, prefix, 0);
482 info.layout_hash = compute_layout_hash(info.uniforms);
484 else if(i->storage==SpirVModule::UNIFORM_CONSTANT && i->location>=0)
486 UniformInfo &info = uniforms[i->name];
488 info.block = &default_block;
489 info.location = i->location;
490 info.array_size = i->array_size;
491 info.array_stride = 0;
492 info.matrix_stride = 0;
494 default_block.uniforms.push_back(&info);
498 default_block.layout_hash = compute_layout_hash(default_block.uniforms);
500 update_layout_hash();
503 void Program::collect_block_uniforms(UniformBlockInfo &block, const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset)
505 for(vector<SpirVModule::StructMember>::const_iterator i=strct.members.begin(); i!=strct.members.end(); ++i)
511 for(unsigned j=0; j<i->array_size; ++j)
512 collect_block_uniforms(block, *i->struct_type, format("%s%s[%d].", prefix, i->name, j), base_offset+i->offset+i->array_stride*j);
515 collect_block_uniforms(block, *i->struct_type, prefix+i->name+".", base_offset+i->offset);
519 string name = prefix+i->name;
520 UniformInfo &info = uniforms[name];
523 info.location = i->offset;
524 info.array_size = i->array_size;
525 info.array_stride = i->array_stride;
526 info.matrix_stride = i->matrix_stride;
528 block.uniforms.push_back(&info);
533 void Program::collect_attributes()
535 const SpirVModule &mod = static_cast<const SpirVModule &>(*module);
537 const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
538 for(vector<SpirVModule::EntryPoint>::const_iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
539 if(i->stage==SpirVModule::VERTEX && i->name=="main")
541 for(vector<const SpirVModule::Variable *>::const_iterator j=i->globals.begin(); j!=i->globals.end(); ++j)
542 if((*j)->storage==SpirVModule::INPUT)
544 AttributeInfo &info = attributes[(*j)->name];
545 info.location = (*j)->location;
546 info.array_size = (*j)->array_size;
547 info.type = (*j)->type;
552 void Program::update_layout_hash()
554 string layout_descriptor;
555 for(UniformBlockMap::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
556 layout_descriptor += format("%d:%x\n", i->second.bind_point, i->second.layout_hash);
557 uniform_layout_hash = hash32(layout_descriptor);
560 Program::LayoutHash Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
562 string layout_descriptor;
563 for(vector<const UniformInfo *>::const_iterator i = uniforms.begin(); i!=uniforms.end(); ++i)
564 layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->array_size);
565 return hash32(layout_descriptor);
568 bool Program::uniform_location_compare(const UniformInfo *uni1, const UniformInfo *uni2)
570 return uni1->location<uni2->location;
573 string Program::get_info_log() const
575 GLsizei len = get_program_i(id, GL_INFO_LOG_LENGTH);
576 string log(len+1, 0);
577 glGetProgramInfoLog(id, len+1, &len, &log[0]);
582 const Program::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
584 return get_item(uniform_blocks, name);
587 const Program::UniformInfo &Program::get_uniform_info(const string &name) const
589 return get_item(uniforms, name);
592 int Program::get_uniform_location(const string &n) const
594 if(n[n.size()-1]==']')
595 throw invalid_argument("Program::get_uniform_location");
597 UniformMap::const_iterator i = uniforms.find(n);
598 if(i==uniforms.end())
601 return i->second.block->bind_point<0 ? i->second.location : -1;
604 const Program::AttributeInfo &Program::get_attribute_info(const string &name) const
606 return get_item(attributes, name);
609 int Program::get_attribute_location(const string &n) const
611 if(n[n.size()-1]==']')
612 throw invalid_argument("Program::get_attribute_location");
614 AttributeMap::const_iterator i = attributes.find(n);
615 return i!=attributes.end() ? i->second.location : -1;
618 void Program::bind() const
621 throw invalid_operation("Program::bind");
623 if(!set_current(this))
629 void Program::unbind()
638 Program::Loader::Loader(Program &p, Collection &c):
639 DataFile::CollectionObjectLoader<Program>(p, &c)
641 add("module", &Loader::module);
644 add("attribute", &Loader::attribute);
645 add("fragment_shader", &Loader::fragment_shader);
646 add("geometry_shader", &Loader::geometry_shader);
647 add("vertex_shader", &Loader::vertex_shader);
650 void Program::Loader::finish()
655 void Program::Loader::module(const string &n)
657 map<string, int> spec_values;
658 SpecializationLoader ldr(spec_values);
660 obj.add_stages(get_collection().get<Module>(n), spec_values);
663 #pragma GCC diagnostic push
664 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
665 void Program::Loader::attribute(unsigned i, const string &n)
667 obj.bind_attribute(i, n);
670 void Program::Loader::fragment_shader(const string &src)
672 obj.attach_shader_owned(new FragmentShader(src));
675 void Program::Loader::geometry_shader(const string &src)
677 obj.attach_shader_owned(new GeometryShader(src));
680 void Program::Loader::vertex_shader(const string &src)
682 obj.attach_shader_owned(new VertexShader(src));
684 #pragma GCC diagnostic pop
687 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
689 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
692 set_actions(shared_actions);
695 void Program::SpecializationLoader::init_actions()
697 add("specialize", &SpecializationLoader::specialize_bool);
698 add("specialize", &SpecializationLoader::specialize_int);
701 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
703 spec_values[name] = value;
706 void Program::SpecializationLoader::specialize_int(const string &name, int value)
708 spec_values[name] = value;