3 #include <msp/core/algorithm.h>
4 #include <msp/core/maputils.h>
5 #include <msp/core/raii.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_separate_shader_objects.h>
11 #include <msp/gl/extensions/arb_shader_objects.h>
12 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
13 #include <msp/gl/extensions/arb_vertex_shader.h>
14 #include <msp/gl/extensions/ext_gpu_shader4.h>
15 #include <msp/gl/extensions/khr_debug.h>
16 #include <msp/gl/extensions/nv_non_square_matrices.h>
17 #include <msp/io/print.h>
21 #include "resources.h"
22 #include "glsl/compiler.h"
34 Program::Program(const Module &mod, const map<string, int> &spec_values)
37 add_stages(mod, spec_values);
42 static Require _req(ARB_shader_objects);
44 id = glCreateProgram();
45 fill(stage_ids, stage_ids+MAX_STAGES, 0);
51 for(unsigned i=0; i<MAX_STAGES; ++i)
53 glDeleteShader(stage_ids[i]);
57 void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
60 throw invalid_operation("Program::add_stages");
62 TransientData transient;
63 switch(mod.get_format())
66 add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values, transient);
69 add_spirv_stages(static_cast<const SpirVModule &>(mod), spec_values, transient);
72 throw invalid_argument("Program::add_stages");
75 finalize(mod, transient);
78 bool Program::has_stages() const
80 for(unsigned i=0; i<MAX_STAGES; ++i)
86 unsigned Program::add_stage(Stage type)
91 case VERTEX: { static Require _req(ARB_vertex_shader); gl_type = GL_VERTEX_SHADER; } break;
92 case GEOMETRY: { static Require _req(ARB_geometry_shader4); gl_type = GL_GEOMETRY_SHADER; } break;
93 case FRAGMENT: { static Require _req(ARB_fragment_shader); gl_type = GL_FRAGMENT_SHADER; } break;
94 default: throw invalid_argument("Program::add_stage");
98 throw invalid_operation("Program::add_stage");
100 unsigned stage_id = glCreateShader(gl_type);
101 stage_ids[type] = stage_id;
102 glAttachShader(id, stage_id);
105 if(!debug_name.empty() && KHR_debug)
106 set_stage_debug_name(stage_id, type);
112 void Program::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values, TransientData &transient)
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();
126 throw invalid_argument("Program::add_glsl_stages");
128 for(SL::Stage::Type st: stages)
130 unsigned stage_id = 0;
133 case SL::Stage::VERTEX: stage_id = add_stage(VERTEX); break;
134 case SL::Stage::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
135 case SL::Stage::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
136 default: throw invalid_operation("Program::add_glsl_stages");
139 string stage_src = compiler.get_stage_glsl(st);
140 const char *src_ptr = stage_src.data();
141 int src_len = stage_src.size();
142 glShaderSource(stage_id, 1, &src_ptr, &src_len);
144 if(st==SL::Stage::VERTEX)
146 for(const auto &kvp: compiler.get_vertex_attributes())
147 glBindAttribLocation(id, kvp.second, kvp.first.c_str());
150 if(st==SL::Stage::FRAGMENT && EXT_gpu_shader4)
152 for(const auto &kvp: compiler.get_fragment_outputs())
153 glBindFragDataLocation(id, kvp.second, kvp.first.c_str());
156 compile_glsl_stage(mod, stage_id);
159 transient.textures = compiler.get_texture_bindings();
160 transient.blocks = compiler.get_uniform_block_bindings();
163 void Program::compile_glsl_stage(const GlslModule &mod, unsigned stage_id)
165 glCompileShader(stage_id);
167 glGetShaderiv(stage_id, GL_COMPILE_STATUS, &status);
169 int info_log_len = 0;
170 glGetShaderiv(stage_id, GL_INFO_LOG_LENGTH, &info_log_len);
171 string info_log(info_log_len+1, 0);
172 glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]);
173 info_log.erase(info_log_len);
174 info_log = mod.get_source_map().translate_errors(info_log);
177 throw compile_error(info_log);
179 if(!info_log.empty())
180 IO::print("Shader compile info log:\n%s", info_log);
184 void Program::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values, TransientData &transient)
186 static Require _req(ARB_gl_spirv);
187 static Require _req2(ARB_ES2_compatibility);
189 unsigned n_stages = 0;
190 unsigned used_stage_ids[MAX_STAGES];
191 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
193 unsigned stage_id = 0;
196 case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break;
197 case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
198 case SpirVModule::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
199 default: throw invalid_operation("Program::add_spirv_stages");
202 used_stage_ids[n_stages++] = stage_id;
206 throw invalid_argument("Program::add_spirv_stages");
208 const vector<uint32_t> &code = mod.get_code();
209 glShaderBinary(n_stages, used_stage_ids, GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
211 const vector<SpirVModule::Constant> &spec_consts = mod.get_spec_constants();
212 vector<unsigned> spec_id_array;
213 vector<unsigned> spec_value_array;
214 spec_id_array.reserve(spec_consts.size());
215 spec_value_array.reserve(spec_consts.size());
216 for(const SpirVModule::Constant &c: spec_consts)
218 auto i = spec_values.find(c.name);
219 if(i!=spec_values.end())
221 spec_id_array.push_back(c.constant_id);
222 spec_value_array.push_back(i->second);
223 transient.spec_values[c.constant_id] = i->second;
227 auto j = mod.get_entry_points().begin();
228 for(unsigned i=0; i<MAX_STAGES; ++i)
230 glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
233 void Program::finalize(const Module &mod, const TransientData &transient)
235 reflect_data = ReflectData();
239 glGetProgramiv(id, GL_LINK_STATUS, &status);
242 int info_log_len = 0;
243 glGetProgramiv(id, GL_INFO_LOG_LENGTH, &info_log_len);
244 string info_log(info_log_len+1, 0);
245 glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]);
246 info_log.erase(info_log_len);
247 if(mod.get_format()==Module::GLSL)
248 info_log = static_cast<const GlslModule &>(mod).get_source_map().translate_errors(info_log);
251 throw compile_error(info_log);
253 if(!info_log.empty())
254 IO::print("Program link info log:\n%s", info_log);
257 if(mod.get_format()==Module::GLSL)
261 for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
263 auto j = transient.blocks.find(reflect_data.uniform_blocks[i].name);
264 if(j!=transient.blocks.end())
266 glUniformBlockBinding(id, i, j->second);
267 reflect_data.uniform_blocks[i].bind_point = j->second;
271 if(!ARB_separate_shader_objects)
273 for(const auto &kvp: transient.textures)
275 int location = get_uniform_location(kvp.first);
278 if(ARB_separate_shader_objects)
279 glProgramUniform1i(id, location, kvp.second);
281 glUniform1i(location, kvp.second);
285 else if(mod.get_format()==Module::SPIR_V)
287 collect_uniforms(static_cast<const SpirVModule &>(mod), transient.spec_values);
288 collect_attributes(static_cast<const SpirVModule &>(mod));
291 for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
292 require_type(u.type);
293 for(const ReflectData::AttributeInfo &a: reflect_data.attributes)
294 require_type(a.type);
297 void Program::query_uniforms()
300 glGetProgramiv(id, GL_ACTIVE_UNIFORMS, reinterpret_cast<int *>(&count));
301 reflect_data.uniforms.reserve(count);
302 vector<string> uniform_names(count);
303 for(unsigned i=0; i<count; ++i)
309 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
310 if(len && strncmp(name, "gl_", 3))
312 /* Some implementations report the first element of a uniform array,
313 others report just the name of the array itself. */
314 if(len>3 && !strcmp(name+len-3, "[0]"))
317 reflect_data.uniforms.push_back(ReflectData::UniformInfo());
318 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
321 info.array_size = size;
322 info.type = from_gl_type(type);
323 uniform_names[i] = name;
327 sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
329 if(ARB_uniform_buffer_object)
331 vector<ReflectData::UniformInfo *> uniforms_by_index(count);
332 for(unsigned i=0; i<count; ++i)
333 if(!uniform_names[i].empty())
334 // The element is already known to be present
335 uniforms_by_index[i] = &*lower_bound_member(reflect_data.uniforms, Tag(uniform_names[i]), &ReflectData::UniformInfo::tag);
336 query_uniform_blocks(uniforms_by_index);
339 reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
340 ReflectData::UniformBlockInfo &default_block = reflect_data.uniform_blocks.back();
342 for(ReflectData::UniformInfo &u: reflect_data.uniforms)
345 u.location = glGetUniformLocation(id, u.name.c_str());
346 u.block = &default_block;
347 u.array_stride = get_type_size(u.type);
348 if(is_matrix(u.type))
349 u.matrix_stride = get_type_size(get_matrix_column_type(u.type));
350 default_block.uniforms.push_back(&u);
352 if(is_image(u.type) && u.location>=0)
353 glGetUniformiv(id, u.location, &u.binding);
356 default_block.update_layout_hash();
357 reflect_data.update_layout_hash();
360 void Program::query_uniform_blocks(const vector<ReflectData::UniformInfo *> &uniforms_by_index)
363 glGetProgramiv(id, GL_ACTIVE_UNIFORM_BLOCKS, reinterpret_cast<int *>(&count));
364 // Reserve an extra index for the default block
365 reflect_data.uniform_blocks.reserve(count+1);
366 for(unsigned i=0; i<count; ++i)
370 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
371 reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
372 ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
376 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
377 info.data_size = value;
379 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_BINDING, &value);
380 info.bind_point = 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]);
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> query_indices(indices.begin(), indices.end());
394 vector<int> values(indices.size());
395 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_OFFSET, &values[0]);
396 for(unsigned j=0; j<indices.size(); ++j)
397 uniforms_by_index[indices[j]]->offset = values[j];
399 query_indices.clear();
401 if(uniforms_by_index[j]->array_size>1)
402 query_indices.push_back(j);
403 if(!query_indices.empty())
405 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
406 for(unsigned j=0; j<query_indices.size(); ++j)
407 uniforms_by_index[query_indices[j]]->array_stride = values[j];
410 query_indices.clear();
413 DataType t = uniforms_by_index[j]->type;
415 query_indices.push_back(j);
417 if(!query_indices.empty())
419 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
420 for(unsigned j=0; j<query_indices.size(); ++j)
421 uniforms_by_index[query_indices[j]]->matrix_stride = values[j];
424 info.sort_uniforms();
425 info.update_layout_hash();
429 void Program::query_attributes()
432 glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, reinterpret_cast<int *>(&count));
433 reflect_data.attributes.reserve(count);
434 for(unsigned i=0; i<count; ++i)
440 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
441 if(len && strncmp(name, "gl_", 3))
443 if(len>3 && !strcmp(name+len-3, "[0]"))
446 reflect_data.attributes.push_back(ReflectData::AttributeInfo());
447 ReflectData::AttributeInfo &info = reflect_data.attributes.back();
449 info.location = glGetAttribLocation(id, name);
450 info.array_size = size;
451 info.type = from_gl_type(type);
456 void Program::collect_uniforms(const SpirVModule &mod, const map<unsigned, int> &spec_values)
458 // Prepare the default block
459 reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
460 vector<vector<string> > block_uniform_names(1);
462 for(const SpirVModule::Variable &v: mod.get_variables())
464 if(v.storage==SpirVModule::UNIFORM && v.struct_type)
466 reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
467 ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
468 info.name = v.struct_type->name;
469 info.bind_point = v.binding;
470 info.data_size = v.struct_type->size;
474 prefix = v.struct_type->name+".";
475 block_uniform_names.push_back(vector<string>());
476 collect_block_uniforms(*v.struct_type, prefix, 0, spec_values, block_uniform_names.back());
478 else if(v.storage==SpirVModule::UNIFORM_CONSTANT && v.location>=0)
480 block_uniform_names[0].push_back(v.name);
481 reflect_data.uniforms.push_back(ReflectData::UniformInfo());
482 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
485 info.location = v.location;
486 info.binding = v.binding;
487 info.array_size = v.array_size;
492 sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
494 for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
496 ReflectData::UniformBlockInfo &block = reflect_data.uniform_blocks[i];
497 for(const string &n: block_uniform_names[i])
499 // The element is already known to be present
500 ReflectData::UniformInfo &uni = *lower_bound_member(reflect_data.uniforms, Tag(n), &ReflectData::UniformInfo::tag);
501 block.uniforms.push_back(&uni);
504 block.sort_uniforms();
505 block.update_layout_hash();
508 reflect_data.update_layout_hash();
511 void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, const map<unsigned, int> &spec_values, vector<string> &uniform_names)
513 for(const SpirVModule::StructMember &m: strct.members)
515 unsigned offset = base_offset+m.offset;
518 unsigned array_size = m.array_size;
519 if(m.array_size_spec)
521 array_size = m.array_size_spec->i_value;
522 auto j = spec_values.find(m.array_size_spec->constant_id);
523 if(j!=spec_values.end())
524 array_size = j->second;
529 for(unsigned j=0; j<array_size; ++j, offset+=m.array_stride)
530 collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, spec_values, uniform_names);
533 collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, spec_values, uniform_names);
537 string name = prefix+m.name;
538 uniform_names.push_back(name);
539 reflect_data.uniforms.push_back(ReflectData::UniformInfo());
540 ReflectData::UniformInfo &info = reflect_data.uniforms.back();
543 info.offset = offset;
544 info.array_size = m.array_size;
545 info.array_stride = m.array_stride;
546 info.matrix_stride = m.matrix_stride;
552 void Program::collect_attributes(const SpirVModule &mod)
554 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
555 if(e.stage==SpirVModule::VERTEX && e.name=="main")
557 for(const SpirVModule::Variable *v: e.globals)
558 if(v->storage==SpirVModule::INPUT)
560 reflect_data.attributes.push_back(ReflectData::AttributeInfo());
561 ReflectData::AttributeInfo &info = reflect_data.attributes.back();
563 info.location = v->location;
564 info.array_size = v->array_size;
570 const ReflectData::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
572 auto i = find_member(reflect_data.uniform_blocks, name, &ReflectData::UniformBlockInfo::name);
573 if(i==reflect_data.uniform_blocks.end())
574 throw key_error(name);
578 const ReflectData::UniformInfo &Program::get_uniform_info(const string &name) const
580 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
581 if(i==reflect_data.uniforms.end() || i->name!=name)
582 throw key_error(name);
586 const ReflectData::UniformInfo &Program::get_uniform_info(Tag tag) const
588 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
589 if(i==reflect_data.uniforms.end() || i->tag!=tag)
590 throw key_error(tag);
594 int Program::get_uniform_location(const string &name) const
596 if(name[name.size()-1]==']')
597 throw invalid_argument("Program::get_uniform_location");
599 auto i = lower_bound_member(reflect_data.uniforms, Tag(name), &ReflectData::UniformInfo::tag);
600 return i!=reflect_data.uniforms.end() && i->name==name && i->block->bind_point<0 ? i->location : -1;
603 int Program::get_uniform_location(Tag tag) const
605 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
606 return i!=reflect_data.uniforms.end() && i->tag==tag && i->block->bind_point<0 ? i->location : -1;
609 int Program::get_uniform_binding(Tag tag) const
611 auto i = lower_bound_member(reflect_data.uniforms, tag, &ReflectData::UniformInfo::tag);
612 return i!=reflect_data.uniforms.end() && i->tag==tag ? i->binding : -1;
615 const ReflectData::AttributeInfo &Program::get_attribute_info(const string &name) const
617 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
618 if(i==reflect_data.attributes.end() || i->name!=name)
619 throw key_error(name);
623 int Program::get_attribute_location(const string &name) const
625 if(name[name.size()-1]==']')
626 throw invalid_argument("Program::get_attribute_location");
628 auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);
629 return i!=reflect_data.attributes.end() && i->name==name ? i->location : -1;
632 void Program::set_debug_name(const string &name)
638 glObjectLabel(GL_PROGRAM, id, name.size(), name.c_str());
639 for(unsigned i=0; i<MAX_STAGES; ++i)
641 set_stage_debug_name(stage_ids[i], static_cast<Stage>(i));
648 void Program::set_stage_debug_name(unsigned stage_id, Stage type)
651 static const char *const suffixes[] = { " [VS]", " [GS]", " [FS]" };
652 string name = debug_name+suffixes[type];
653 glObjectLabel(GL_SHADER, stage_id, name.size(), name.c_str());
655 (void)stage_id; (void)type;
660 Program::Loader::Loader(Program &p, Collection &c):
661 DataFile::CollectionObjectLoader<Program>(p, &c)
663 add("module", &Loader::module);
666 void Program::Loader::module(const string &n)
668 map<string, int> spec_values;
669 SpecializationLoader ldr(spec_values);
671 obj.add_stages(get_collection().get<Module>(n), spec_values);
675 DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions;
677 Program::SpecializationLoader::SpecializationLoader(map<string, int> &sv):
680 set_actions(shared_actions);
683 void Program::SpecializationLoader::init_actions()
685 add("specialize", &SpecializationLoader::specialize_bool);
686 add("specialize", &SpecializationLoader::specialize_int);
689 void Program::SpecializationLoader::specialize_bool(const string &name, bool value)
691 spec_values[name] = value;
694 void Program::SpecializationLoader::specialize_int(const string &name, int value)
696 spec_values[name] = value;