3 #include <msp/core/algorithm.h>
4 #include <msp/core/hash.h>
5 #include <msp/core/maputils.h>
6 #include <msp/core/raii.h>
7 #include <msp/gl/extensions/arb_es2_compatibility.h>
8 #include <msp/gl/extensions/arb_fragment_shader.h>
9 #include <msp/gl/extensions/arb_gl_spirv.h>
10 #include <msp/gl/extensions/arb_geometry_shader4.h>
11 #include <msp/gl/extensions/arb_separate_shader_objects.h>
12 #include <msp/gl/extensions/arb_shader_objects.h>
13 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
14 #include <msp/gl/extensions/arb_vertex_shader.h>
15 #include <msp/gl/extensions/ext_gpu_shader4.h>
16 #include <msp/gl/extensions/khr_debug.h>
17 #include <msp/gl/extensions/nv_non_square_matrices.h>
18 #include <msp/io/print.h>
19 #include <msp/strings/format.h>
23 #include "resources.h"
24 #include "glsl/compiler.h"
36 Program::Program(const Module &mod, const map<string, int> &spec_values)
39 add_stages(mod, spec_values);
45 static Require _req(ARB_shader_objects);
47 id = glCreateProgram();
48 fill(stage_ids, stage_ids+MAX_STAGES, 0);
56 for(unsigned i=0; i<MAX_STAGES; ++i)
58 glDeleteShader(stage_ids[i]);
62 void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
65 throw invalid_operation("Program::add_stages");
67 switch(mod.get_format())
69 case Module::GLSL: return add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values);
70 case Module::SPIR_V: return add_spirv_stages(static_cast<const SpirVModule &>(mod), spec_values);
71 default: throw invalid_argument("Program::add_stages");
75 bool Program::has_stages() const
77 for(unsigned i=0; i<MAX_STAGES; ++i)
83 unsigned Program::add_stage(Stage type)
88 case VERTEX: { static Require _req(ARB_vertex_shader); gl_type = GL_VERTEX_SHADER; } break;
89 case GEOMETRY: { static Require _req(ARB_geometry_shader4); gl_type = GL_GEOMETRY_SHADER; } break;
90 case FRAGMENT: { static Require _req(ARB_fragment_shader); gl_type = GL_FRAGMENT_SHADER; } break;
91 default: throw invalid_argument("Program::add_stage");
95 throw invalid_operation("Program::add_stage");
97 unsigned stage_id = glCreateShader(gl_type);
98 stage_ids[type] = stage_id;
99 glAttachShader(id, stage_id);
102 if(!debug_name.empty() && KHR_debug)
103 set_stage_debug_name(stage_id, type);
109 void Program::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values)
113 SL::Compiler compiler;
114 compiler.set_source(mod.get_prepared_source(), "<module>");
115 compiler.specialize(spec_values);
116 compiler.compile(SL::Compiler::PROGRAM);
118 string diagnostics = compiler.get_diagnostics();
119 if(!diagnostics.empty())
120 IO::print("Program diagnostics:\n%s\n", diagnostics);
123 vector<SL::Stage::Type> stages = compiler.get_stages();
124 for(SL::Stage::Type st: stages)
126 unsigned stage_id = 0;
129 case SL::Stage::VERTEX: stage_id = add_stage(VERTEX); break;
130 case SL::Stage::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
131 case SL::Stage::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
132 default: throw invalid_operation("Program::add_glsl_stages");
135 string stage_src = compiler.get_stage_glsl(st);
136 const char *src_ptr = stage_src.data();
137 int src_len = stage_src.size();
138 glShaderSource(stage_id, 1, &src_ptr, &src_len);
140 if(st==SL::Stage::VERTEX)
142 for(const auto &kvp: compiler.get_vertex_attributes())
143 glBindAttribLocation(id, kvp.second, kvp.first.c_str());
146 if(st==SL::Stage::FRAGMENT && EXT_gpu_shader4)
148 for(const auto &kvp: compiler.get_fragment_outputs())
149 glBindFragDataLocation(id, kvp.second, kvp.first.c_str());
152 compile_glsl_stage(stage_id);
156 transient = new TransientData;
157 transient->textures = compiler.get_texture_bindings();
158 transient->blocks = compiler.get_uniform_block_bindings();
161 void Program::compile_glsl_stage(unsigned stage_id)
163 glCompileShader(stage_id);
165 glGetShaderiv(stage_id, GL_COMPILE_STATUS, &status);
167 int info_log_len = 0;
168 glGetShaderiv(stage_id, GL_INFO_LOG_LENGTH, &info_log_len);
169 string info_log(info_log_len+1, 0);
170 glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]);
171 info_log.erase(info_log_len);
172 if(module && module->get_format()==Module::GLSL)
173 info_log = static_cast<const GlslModule *>(module)->get_source_map().translate_errors(info_log);
176 throw compile_error(info_log);
178 if(!info_log.empty())
179 IO::print("Shader compile info log:\n%s", info_log);
183 void Program::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
185 static Require _req(ARB_gl_spirv);
186 static Require _req2(ARB_ES2_compatibility);
190 unsigned n_stages = 0;
191 unsigned used_stage_ids[MAX_STAGES];
192 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
194 unsigned stage_id = 0;
197 case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break;
198 case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
199 case SpirVModule::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
200 default: throw invalid_operation("Program::add_spirv_stages");
203 used_stage_ids[n_stages++] = stage_id;
206 const vector<uint32_t> &code = mod.get_code();
207 glShaderBinary(n_stages, used_stage_ids, GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
209 if(!spec_values.empty() && !transient)
210 transient = new TransientData;
212 const vector<SpirVModule::Constant> &spec_consts = mod.get_spec_constants();
213 vector<unsigned> spec_id_array;
214 vector<unsigned> spec_value_array;
215 spec_id_array.reserve(spec_consts.size());
216 spec_value_array.reserve(spec_consts.size());
217 for(const SpirVModule::Constant &c: spec_consts)
219 auto i = spec_values.find(c.name);
220 if(i!=spec_values.end())
222 spec_id_array.push_back(c.constant_id);
223 spec_value_array.push_back(i->second);
224 transient->spec_values[c.constant_id] = i->second;
228 auto j = mod.get_entry_points().begin();
229 for(unsigned i=0; i<MAX_STAGES; ++i)
231 glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
237 throw invalid_operation("Program::link");
240 uniform_blocks.clear();
245 glGetProgramiv(id, GL_LINK_STATUS, &status);
248 int info_log_len = 0;
249 glGetProgramiv(id, GL_INFO_LOG_LENGTH, &info_log_len);
250 string info_log(info_log_len+1, 0);
251 glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]);
252 info_log.erase(info_log_len);
253 if(module && module->get_format()==Module::GLSL)
254 info_log = static_cast<const GlslModule *>(module)->get_source_map().translate_errors(info_log);
257 throw compile_error(info_log);
259 if(!info_log.empty())
260 IO::print("Program link info log:\n%s", info_log);
263 if(module->get_format()==Module::GLSL)
269 for(unsigned i=0; i<uniform_blocks.size(); ++i)
271 auto j = transient->blocks.find(uniform_blocks[i].name);
272 if(j!=transient->blocks.end())
274 glUniformBlockBinding(id, i, j->second);
275 uniform_blocks[i].bind_point = j->second;
279 if(!ARB_separate_shader_objects)
281 for(const auto &kvp: transient->textures)
283 int location = get_uniform_location(kvp.first);
286 if(ARB_separate_shader_objects)
287 glProgramUniform1i(id, location, kvp.second);
289 glUniform1i(location, kvp.second);
294 else if(module->get_format()==Module::SPIR_V)
297 collect_attributes();
303 for(const UniformInfo &u: uniforms)
304 require_type(u.type);
305 for(const AttributeInfo &a: attributes)
306 require_type(a.type);
309 void Program::query_uniforms()
312 glGetProgramiv(id, GL_ACTIVE_UNIFORMS, reinterpret_cast<int *>(&count));
313 uniforms.reserve(count);
314 vector<string> uniform_names(count);
315 for(unsigned i=0; i<count; ++i)
321 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
322 if(len && strncmp(name, "gl_", 3))
324 /* Some implementations report the first element of a uniform array,
325 others report just the name of the array itself. */
326 if(len>3 && !strcmp(name+len-3, "[0]"))
329 uniforms.push_back(UniformInfo());
330 UniformInfo &info = uniforms.back();
333 info.array_size = size;
334 info.type = from_gl_type(type);
335 uniform_names[i] = name;
339 sort_member(uniforms, &UniformInfo::tag);
341 if(ARB_uniform_buffer_object)
343 vector<UniformInfo *> uniforms_by_index(count);
344 for(unsigned i=0; i<count; ++i)
345 if(!uniform_names[i].empty())
346 // The element is already known to be present
347 uniforms_by_index[i] = &*lower_bound_member(uniforms, Tag(uniform_names[i]), &UniformInfo::tag);
348 query_uniform_blocks(uniforms_by_index);
351 uniform_blocks.push_back(UniformBlockInfo());
352 UniformBlockInfo &default_block = uniform_blocks.back();
354 for(UniformInfo &u: uniforms)
357 u.location = glGetUniformLocation(id, u.name.c_str());
358 u.block = &default_block;
359 default_block.uniforms.push_back(&u);
361 if(is_image(u.type) && u.location>=0)
362 glGetUniformiv(id, u.location, &u.binding);
365 default_block.layout_hash = compute_layout_hash(default_block.uniforms);
367 update_layout_hash();
370 void Program::query_uniform_blocks(const vector<UniformInfo *> &uniforms_by_index)
373 glGetProgramiv(id, GL_ACTIVE_UNIFORM_BLOCKS, reinterpret_cast<int *>(&count));
374 // Reserve an extra index for the default block
375 uniform_blocks.reserve(count+1);
376 for(unsigned i=0; i<count; ++i)
380 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
381 uniform_blocks.push_back(UniformBlockInfo());
382 UniformBlockInfo &info = uniform_blocks.back();
386 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
387 info.data_size = value;
389 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_BINDING, &value);
390 info.bind_point = value;
392 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
393 vector<int> indices(value);
394 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
397 if(!uniforms_by_index[j])
398 throw logic_error("Program::link");
399 info.uniforms.push_back(uniforms_by_index[j]);
400 uniforms_by_index[j]->block = &info;
403 vector<unsigned> query_indices(indices.begin(), indices.end());
404 vector<int> values(indices.size());
405 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_OFFSET, &values[0]);
406 for(unsigned j=0; j<indices.size(); ++j)
407 uniforms_by_index[indices[j]]->offset = values[j];
409 query_indices.clear();
411 if(uniforms_by_index[j]->array_size>1)
412 query_indices.push_back(j);
413 if(!query_indices.empty())
415 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
416 for(unsigned j=0; j<query_indices.size(); ++j)
417 uniforms_by_index[query_indices[j]]->array_stride = values[j];
420 query_indices.clear();
423 DataType t = uniforms_by_index[j]->type;
425 query_indices.push_back(j);
427 if(!query_indices.empty())
429 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
430 for(unsigned j=0; j<query_indices.size(); ++j)
431 uniforms_by_index[query_indices[j]]->matrix_stride = values[j];
434 sort(info.uniforms, uniform_location_compare);
435 info.layout_hash = compute_layout_hash(info.uniforms);
439 void Program::query_attributes()
442 glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, reinterpret_cast<int *>(&count));
443 attributes.reserve(count);
444 for(unsigned i=0; i<count; ++i)
450 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
451 if(len && strncmp(name, "gl_", 3))
453 if(len>3 && !strcmp(name+len-3, "[0]"))
456 attributes.push_back(AttributeInfo());
457 AttributeInfo &info = attributes.back();
459 info.location = glGetAttribLocation(id, name);
460 info.array_size = size;
461 info.type = from_gl_type(type);
466 void Program::collect_uniforms()
468 const SpirVModule &mod = static_cast<const SpirVModule &>(*module);
470 // Prepare the default block
471 uniform_blocks.push_back(UniformBlockInfo());
472 vector<vector<string> > block_uniform_names(1);
474 for(const SpirVModule::Variable &v: mod.get_variables())
476 if(v.storage==SpirVModule::UNIFORM && v.struct_type)
478 uniform_blocks.push_back(UniformBlockInfo());
479 UniformBlockInfo &info = uniform_blocks.back();
480 info.name = v.struct_type->name;
481 info.bind_point = v.binding;
482 info.data_size = v.struct_type->size;
486 prefix = v.struct_type->name+".";
487 block_uniform_names.push_back(vector<string>());
488 collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back());
490 else if(v.storage==SpirVModule::UNIFORM_CONSTANT && v.location>=0)
492 block_uniform_names[0].push_back(v.name);
493 uniforms.push_back(UniformInfo());
494 UniformInfo &info = uniforms.back();
497 info.location = v.location;
498 info.binding = v.binding;
499 info.array_size = v.array_size;
504 sort_member(uniforms, &UniformInfo::tag);
506 for(unsigned i=0; i<uniform_blocks.size(); ++i)
508 UniformBlockInfo &block = uniform_blocks[i];
509 for(const string &n: block_uniform_names[i])
511 // The element is already known to be present
512 UniformInfo &uni = *lower_bound_member(uniforms, Tag(n), &UniformInfo::tag);
513 block.uniforms.push_back(&uni);
516 sort(block.uniforms, uniform_location_compare);
517 block.layout_hash = compute_layout_hash(block.uniforms);
520 update_layout_hash();
523 void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
525 for(const SpirVModule::StructMember &m: strct.members)
527 unsigned offset = base_offset+m.offset;
530 unsigned array_size = m.array_size;
531 if(m.array_size_spec)
533 array_size = m.array_size_spec->i_value;
536 auto j = transient->spec_values.find(m.array_size_spec->constant_id);
537 if(j!=transient->spec_values.end())
538 array_size = j->second;
544 for(unsigned j=0; j<array_size; ++j, offset+=m.array_stride)
545 collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, uniform_names);
548 collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, uniform_names);
552 string name = prefix+m.name;
553 uniform_names.push_back(name);
554 uniforms.push_back(UniformInfo());
555 UniformInfo &info = uniforms.back();
558 info.offset = offset;
559 info.array_size = m.array_size;
560 info.array_stride = m.array_stride;
561 info.matrix_stride = m.matrix_stride;
567 void Program::collect_attributes()
569 const SpirVModule &mod = static_cast<const SpirVModule &>(*module);
571 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
572 if(e.stage==SpirVModule::VERTEX && e.name=="main")
574 for(const SpirVModule::Variable *v: e.globals)
575 if(v->storage==SpirVModule::INPUT)
577 attributes.push_back(AttributeInfo());
578 AttributeInfo &info = attributes.back();
580 info.location = v->location;
581 info.array_size = v->array_size;
587 void Program::update_layout_hash()
589 string layout_descriptor;
590 for(const UniformBlockInfo &b: uniform_blocks)
591 layout_descriptor += format("%d:%x\n", b.bind_point, b.layout_hash);
592 uniform_layout_hash = hash32(layout_descriptor);
595 Program::LayoutHash Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
597 string layout_descriptor;
598 for(const UniformInfo *u: uniforms)
599 layout_descriptor += format("%d:%s:%x:%d\n", u->location, u->name, u->type, u->array_size);
600 return hash32(layout_descriptor);
603 bool Program::uniform_location_compare(const UniformInfo *uni1, const UniformInfo *uni2)
605 return uni1->location<uni2->location;
608 const Program::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
610 auto i = find_member(uniform_blocks, name, &UniformBlockInfo::name);
611 if(i==uniform_blocks.end())
612 throw key_error(name);
616 const Program::UniformInfo &Program::get_uniform_info(const string &name) const
618 auto i = lower_bound_member(uniforms, Tag(name), &UniformInfo::tag);
619 if(i==uniforms.end() || i->name!=name)
620 throw key_error(name);
624 const Program::UniformInfo &Program::get_uniform_info(Tag tag) const
626 auto i = lower_bound_member(uniforms, tag, &UniformInfo::tag);
627 if(i==uniforms.end() || i->tag!=tag)
628 throw key_error(tag);
632 int Program::get_uniform_location(const string &name) const
634 if(name[name.size()-1]==']')
635 throw invalid_argument("Program::get_uniform_location");
637 auto i = lower_bound_member(uniforms, Tag(name), &UniformInfo::tag);
638 return i!=uniforms.end() && i->name==name && i->block->bind_point<0 ? i->location : -1;
641 int Program::get_uniform_location(Tag tag) const
643 auto i = lower_bound_member(uniforms, tag, &UniformInfo::tag);
644 return i!=uniforms.end() && i->tag==tag && i->block->bind_point<0 ? i->location : -1;
647 int Program::get_uniform_binding(Tag tag) const
649 auto i = lower_bound_member(uniforms, tag, &UniformInfo::tag);
650 return i!=uniforms.end() && i->tag==tag ? i->binding : -1;
653 const Program::AttributeInfo &Program::get_attribute_info(const string &name) const
655 auto i = lower_bound_member(attributes, name, &AttributeInfo::name);
656 if(i==attributes.end() || i->name!=name)
657 throw key_error(name);
661 int Program::get_attribute_location(const string &name) const
663 if(name[name.size()-1]==']')
664 throw invalid_argument("Program::get_attribute_location");
666 auto i = lower_bound_member(attributes, name, &AttributeInfo::name);
667 return i!=attributes.end() && i->name==name ? i->location : -1;
670 void Program::set_debug_name(const string &name)
676 glObjectLabel(GL_PROGRAM, id, name.size(), name.c_str());
677 for(unsigned i=0; i<MAX_STAGES; ++i)
679 set_stage_debug_name(stage_ids[i], static_cast<Stage>(i));
686 void Program::set_stage_debug_name(unsigned stage_id, Stage type)
689 static const char *const suffixes[] = { " [VS]", " [GS]", " [FS]" };
690 string name = debug_name+suffixes[type];
691 glObjectLabel(GL_SHADER, stage_id, name.size(), name.c_str());
693 (void)stage_id; (void)type;
698 Program::UniformInfo::UniformInfo():
709 Program::UniformBlockInfo::UniformBlockInfo():
716 Program::AttributeInfo::AttributeInfo():
723 Program::Loader::Loader(Program &p, Collection &c):
724 DataFile::CollectionObjectLoader<Program>(p, &c)
726 add("module", &Loader::module);
729 void Program::Loader::finish()
734 void Program::Loader::module(const string &n)
736 map<string, int> spec_values;
737 SpecializationLoader ldr(spec_values);
739 obj.add_stages(get_collection().get<Module>(n), spec_values);
743 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
745 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
748 set_actions(shared_actions);
751 void Program::SpecializationLoader::init_actions()
753 add("specialize", &SpecializationLoader::specialize_bool);
754 add("specialize", &SpecializationLoader::specialize_int);
757 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
759 spec_values[name] = value;
762 void Program::SpecializationLoader::specialize_int(const string &name, int value)
764 spec_values[name] = value;