2 #include <msp/core/algorithm.h>
3 #include <msp/gl/extensions/arb_compute_shader.h>
4 #include <msp/gl/extensions/arb_es2_compatibility.h>
5 #include <msp/gl/extensions/arb_fragment_shader.h>
6 #include <msp/gl/extensions/arb_gl_spirv.h>
7 #include <msp/gl/extensions/arb_geometry_shader4.h>
8 #include <msp/gl/extensions/arb_separate_shader_objects.h>
9 #include <msp/gl/extensions/arb_shader_objects.h>
10 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
11 #include <msp/gl/extensions/arb_vertex_shader.h>
12 #include <msp/gl/extensions/ext_gpu_shader4.h>
13 #include <msp/gl/extensions/khr_debug.h>
14 #include <msp/gl/extensions/nv_non_square_matrices.h>
15 #include <msp/io/print.h>
19 #include "program_backend.h"
20 #include "glsl/compiler.h"
26 template<typename T, void (*&func)(GLint, GLsizei, const T *)>
27 void uniform_wrapper(unsigned index, unsigned count, const void *data)
29 func(index, count, static_cast<const T *>(data));
32 template<typename T, void (*&func)(GLint, GLsizei, GLboolean, const T *)>
33 void uniform_matrix_wrapper(unsigned index, unsigned count, const void *data)
35 func(index, count, false, static_cast<const T *>(data));
43 OpenGLProgram::OpenGLProgram()
45 static Require _req(ARB_shader_objects);
47 id = glCreateProgram();
50 OpenGLProgram::OpenGLProgram(OpenGLProgram &&other):
53 uniform_calls(move(other.uniform_calls)),
54 debug_name(move(other.debug_name))
56 move(other.stage_ids, other.stage_ids+MAX_STAGES, stage_ids);
58 fill(other.stage_ids, other.stage_ids+MAX_STAGES, 0);
61 OpenGLProgram::~OpenGLProgram()
63 for(unsigned i=0; i<MAX_STAGES; ++i)
65 glDeleteShader(stage_ids[i]);
69 bool OpenGLProgram::has_stages() const
71 for(unsigned i=0; i<MAX_STAGES; ++i)
77 unsigned OpenGLProgram::add_stage(Stage type)
82 case VERTEX: { static Require _req(ARB_vertex_shader); gl_type = GL_VERTEX_SHADER; } break;
83 case GEOMETRY: { static Require _req(ARB_geometry_shader4); gl_type = GL_GEOMETRY_SHADER; } break;
84 case FRAGMENT: { static Require _req(ARB_fragment_shader); gl_type = GL_FRAGMENT_SHADER; } break;
85 case COMPUTE: { static Require _req(ARB_compute_shader); gl_type = GL_COMPUTE_SHADER; } break;
86 default: throw invalid_argument("OpenGLProgram::add_stage");
90 throw invalid_operation("OpenGLProgram::add_stage");
92 unsigned stage_id = glCreateShader(gl_type);
93 stage_ids[type] = stage_id;
94 glAttachShader(id, stage_id);
97 if(!debug_name.empty() && KHR_debug)
98 set_stage_debug_name(stage_id, type);
104 void OpenGLProgram::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values)
106 SL::Compiler compiler(Device::get_current().get_info().glsl_features);
107 compiler.set_source(mod.get_prepared_source(), "<module>");
108 compiler.specialize(spec_values);
109 compiler.compile(SL::Compiler::PROGRAM);
111 string diagnostics = compiler.get_diagnostics();
112 if(!diagnostics.empty())
113 IO::print("Program diagnostics:\n%s\n", diagnostics);
116 vector<SL::Stage::Type> stages = compiler.get_stages();
118 throw invalid_argument("OpenGLProgram::add_glsl_stages");
120 for(SL::Stage::Type st: stages)
122 unsigned stage_id = 0;
125 case SL::Stage::VERTEX: stage_id = add_stage(VERTEX); break;
126 case SL::Stage::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
127 case SL::Stage::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
128 case SL::Stage::COMPUTE: stage_id = add_stage(COMPUTE); break;
129 default: throw invalid_operation("OpenGLProgram::add_glsl_stages");
132 string stage_src = compiler.get_stage_glsl(st);
133 const char *src_ptr = stage_src.data();
134 int src_len = stage_src.size();
135 glShaderSource(stage_id, 1, &src_ptr, &src_len);
137 if(st==SL::Stage::VERTEX)
139 for(const auto &kvp: compiler.get_vertex_attributes())
140 glBindAttribLocation(id, kvp.second, kvp.first.c_str());
143 if(st==SL::Stage::FRAGMENT && EXT_gpu_shader4)
145 for(const auto &kvp: compiler.get_fragment_outputs())
146 glBindFragDataLocation(id, kvp.second, kvp.first.c_str());
149 compile_glsl_stage(mod, stage_id);
152 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
153 rd.n_clip_distances = compiler.get_n_clip_distances();
161 glGetProgramiv(id, GL_COMPUTE_WORK_GROUP_SIZE, wg_size);
162 rd.compute_wg_size = LinAl::Vector<unsigned, 3>(wg_size[0], wg_size[1], wg_size[2]);
165 const map<string, unsigned> &block_bindings = compiler.get_uniform_block_bindings();
166 if(!block_bindings.empty())
168 for(unsigned i=0; i<rd.uniform_blocks.size(); ++i)
170 auto j = block_bindings.find(rd.uniform_blocks[i].name);
171 if(j!=block_bindings.end())
173 glUniformBlockBinding(id, i, j->second);
174 rd.uniform_blocks[i].bind_point = j->second;
179 const map<string, unsigned> &tex_bindings = compiler.get_texture_bindings();
180 if(!tex_bindings.empty())
182 if(!ARB_separate_shader_objects)
184 for(const auto &kvp: tex_bindings)
186 int location = static_cast<const Program *>(this)->get_uniform_location(kvp.first);
189 if(ARB_separate_shader_objects)
190 glProgramUniform1i(id, location, kvp.second);
192 glUniform1i(location, kvp.second);
198 void OpenGLProgram::compile_glsl_stage(const GlslModule &mod, unsigned stage_id)
200 glCompileShader(stage_id);
202 glGetShaderiv(stage_id, GL_COMPILE_STATUS, &status);
204 int info_log_len = 0;
205 glGetShaderiv(stage_id, GL_INFO_LOG_LENGTH, &info_log_len);
206 string info_log(info_log_len+1, 0);
207 glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]);
208 info_log.erase(info_log_len);
209 info_log = mod.get_source_map().translate_errors(info_log);
212 throw compile_error(info_log);
214 if(!info_log.empty())
215 IO::print("Shader compile info log:\n%s", info_log);
219 void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
221 static Require _req(ARB_gl_spirv);
222 static Require _req2(ARB_ES2_compatibility);
224 unsigned n_stages = 0;
225 unsigned used_stage_ids[MAX_STAGES];
226 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
228 unsigned stage_id = 0;
231 case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break;
232 case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
233 case SpirVModule::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
234 case SpirVModule::COMPUTE: stage_id = add_stage(COMPUTE); break;
235 default: throw invalid_operation("OpenGLProgram::add_spirv_stages");
238 used_stage_ids[n_stages++] = stage_id;
242 throw invalid_argument("OpenGLProgram::add_spirv_stages");
244 const vector<uint32_t> &code = mod.get_code();
245 glShaderBinary(n_stages, used_stage_ids, GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
247 const vector<SpirVModule::Constant> &spec_consts = mod.get_spec_constants();
248 vector<unsigned> spec_id_array;
249 vector<unsigned> spec_value_array;
250 spec_id_array.reserve(spec_consts.size());
251 spec_value_array.reserve(spec_consts.size());
252 for(const SpirVModule::Constant &c: spec_consts)
254 auto i = spec_values.find(c.name);
255 if(i!=spec_values.end())
257 spec_id_array.push_back(c.constant_id);
258 spec_value_array.push_back(i->second);
262 auto j = mod.get_entry_points().begin();
263 for(unsigned i=0; i<MAX_STAGES; ++i)
265 glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
270 void OpenGLProgram::link(const Module &mod)
274 glGetProgramiv(id, GL_LINK_STATUS, &status);
277 int info_log_len = 0;
278 glGetProgramiv(id, GL_INFO_LOG_LENGTH, &info_log_len);
279 string info_log(info_log_len+1, 0);
280 glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]);
281 info_log.erase(info_log_len);
282 if(mod.get_format()==Module::GLSL)
283 info_log = static_cast<const GlslModule &>(mod).get_source_map().translate_errors(info_log);
286 throw compile_error(info_log);
288 if(!info_log.empty())
289 IO::print("Program link info log:\n%s", info_log);
293 void OpenGLProgram::query_uniforms()
295 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
298 glGetProgramiv(id, GL_ACTIVE_UNIFORMS, reinterpret_cast<int *>(&count));
299 rd.uniforms.reserve(count);
300 vector<string> uniform_names(count);
301 for(unsigned i=0; i<count; ++i)
307 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
308 if(len && strncmp(name, "gl_", 3))
310 /* Some implementations report the first element of a uniform array,
311 others report just the name of the array itself. */
312 if(len>3 && !strcmp(name+len-3, "[0]"))
315 rd.uniforms.emplace_back();
316 ReflectData::UniformInfo &info = rd.uniforms.back();
319 info.array_size = size;
320 info.type = from_gl_type(type);
321 uniform_names[i] = name;
325 sort_member(rd.uniforms, &ReflectData::UniformInfo::tag);
327 if(ARB_uniform_buffer_object)
329 vector<ReflectData::UniformInfo *> uniforms_by_index(count);
330 for(unsigned i=0; i<count; ++i)
331 if(!uniform_names[i].empty())
332 // The element is already known to be present
333 uniforms_by_index[i] = &*lower_bound_member(rd.uniforms, Tag(uniform_names[i]), &ReflectData::UniformInfo::tag);
334 query_uniform_blocks(uniforms_by_index);
337 rd.uniform_blocks.emplace_back();
338 ReflectData::UniformBlockInfo &default_block = rd.uniform_blocks.back();
340 for(ReflectData::UniformInfo &u: rd.uniforms)
343 u.location = glGetUniformLocation(id, u.name.c_str());
344 u.block = &default_block;
345 u.array_stride = get_type_size(u.type);
346 if(is_matrix(u.type))
347 u.matrix_stride = get_type_size(get_matrix_column_type(u.type));
348 default_block.uniforms.push_back(&u);
350 if(is_image(u.type) && u.location>=0)
351 glGetUniformiv(id, u.location, &u.binding);
354 default_block.sort_uniforms();
355 default_block.update_layout_hash();
356 rd.update_layout_hash();
359 void OpenGLProgram::query_uniform_blocks(const vector<ReflectData::UniformInfo *> &uniforms_by_index)
361 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
364 glGetProgramiv(id, GL_ACTIVE_UNIFORM_BLOCKS, reinterpret_cast<int *>(&count));
365 // Reserve an extra index for the default block
366 rd.uniform_blocks.reserve(count+1);
367 for(unsigned i=0; i<count; ++i)
371 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
372 rd.uniform_blocks.emplace_back();
373 ReflectData::UniformBlockInfo &info = rd.uniform_blocks.back();
377 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
378 info.data_size = value;
380 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_BINDING, &value);
381 info.bind_point = value;
383 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
384 vector<int> indices(value);
385 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
388 if(!uniforms_by_index[j])
389 throw logic_error("OpenGLProgram::query_uniform_blocks");
390 info.uniforms.push_back(uniforms_by_index[j]);
391 uniforms_by_index[j]->block = &info;
394 vector<unsigned> query_indices(indices.begin(), indices.end());
395 vector<int> values(indices.size());
396 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_OFFSET, &values[0]);
397 for(unsigned j=0; j<indices.size(); ++j)
398 uniforms_by_index[indices[j]]->offset = values[j];
400 query_indices.clear();
402 if(uniforms_by_index[j]->array_size>1)
403 query_indices.push_back(j);
404 if(!query_indices.empty())
406 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
407 for(unsigned j=0; j<query_indices.size(); ++j)
408 uniforms_by_index[query_indices[j]]->array_stride = values[j];
411 query_indices.clear();
414 DataType t = uniforms_by_index[j]->type;
416 query_indices.push_back(j);
418 if(!query_indices.empty())
420 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
421 for(unsigned j=0; j<query_indices.size(); ++j)
422 uniforms_by_index[query_indices[j]]->matrix_stride = values[j];
425 info.sort_uniforms();
426 info.update_layout_hash();
430 void OpenGLProgram::query_attributes()
432 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
435 glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, reinterpret_cast<int *>(&count));
436 rd.attributes.reserve(count);
437 for(unsigned i=0; i<count; ++i)
443 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
444 if(len && strncmp(name, "gl_", 3))
446 if(len>3 && !strcmp(name+len-3, "[0]"))
449 rd.attributes.emplace_back();
450 ReflectData::AttributeInfo &info = rd.attributes.back();
452 info.location = glGetAttribLocation(id, name);
453 info.array_size = size;
454 info.type = from_gl_type(type);
458 sort_member(rd.attributes, &ReflectData::AttributeInfo::name);
461 void OpenGLProgram::finalize_uniforms()
463 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
465 auto i = find_if(rd.uniform_blocks, [](const ReflectData::UniformBlockInfo &b){ return b.bind_point<0; });
466 if(i!=rd.uniform_blocks.end() && !i->uniforms.empty())
468 for(const ReflectData::UniformInfo *u: i->uniforms)
471 UniformCall::FuncPtr func = 0;
473 func = &uniform_wrapper<float, glUniform1fv>;
474 else if(u->type==FLOAT_VEC2)
475 func = &uniform_wrapper<float, glUniform2fv>;
476 else if(u->type==FLOAT_VEC3)
477 func = &uniform_wrapper<float, glUniform3fv>;
478 else if(u->type==FLOAT_VEC4)
479 func = &uniform_wrapper<float, glUniform4fv>;
480 else if(u->type==INT)
481 func = &uniform_wrapper<int, glUniform1iv>;
482 else if(u->type==INT_VEC2)
483 func = &uniform_wrapper<int, glUniform2iv>;
484 else if(u->type==INT_VEC3)
485 func = &uniform_wrapper<int, glUniform3iv>;
486 else if(u->type==INT_VEC4)
487 func = &uniform_wrapper<int, glUniform4iv>;
488 else if(u->type==UNSIGNED_INT)
489 func = &uniform_wrapper<unsigned, glUniform1uiv>;
490 else if(u->type==UINT_VEC2)
491 func = &uniform_wrapper<unsigned, glUniform2uiv>;
492 else if(u->type==UINT_VEC3)
493 func = &uniform_wrapper<unsigned, glUniform3uiv>;
494 else if(u->type==UINT_VEC4)
495 func = &uniform_wrapper<unsigned, glUniform4uiv>;
496 else if(u->type==FLOAT_MAT2)
497 func = &uniform_matrix_wrapper<float, glUniformMatrix2fv>;
498 else if(u->type==FLOAT_MAT3)
499 func = &uniform_matrix_wrapper<float, glUniformMatrix3fv>;
500 else if(u->type==FLOAT_MAT4)
501 func = &uniform_matrix_wrapper<float, glUniformMatrix4fv>;
502 else if(u->type==FLOAT_MAT2x3)
503 func = &uniform_matrix_wrapper<float, glUniformMatrix2x3fv>;
504 else if(u->type==FLOAT_MAT3x2)
505 func = &uniform_matrix_wrapper<float, glUniformMatrix3x2fv>;
506 else if(u->type==FLOAT_MAT2x4)
507 func = &uniform_matrix_wrapper<float, glUniformMatrix2x4fv>;
508 else if(u->type==FLOAT_MAT4x2)
509 func = &uniform_matrix_wrapper<float, glUniformMatrix4x2fv>;
510 else if(u->type==FLOAT_MAT3x4)
511 func = &uniform_matrix_wrapper<float, glUniformMatrix3x4fv>;
512 else if(u->type==FLOAT_MAT4x3)
513 func = &uniform_matrix_wrapper<float, glUniformMatrix4x3fv>;
516 uniform_calls.push_back(UniformCall(u->location, u->array_size, func));
521 const ReflectData::UniformInfo &last = *i->uniforms.back();
522 i->data_size = last.location*16+last.array_size*get_type_size(last.type);
527 void OpenGLProgram::set_debug_name(const string &name)
533 glObjectLabel(GL_PROGRAM, id, name.size(), name.c_str());
534 for(unsigned i=0; i<MAX_STAGES; ++i)
536 set_stage_debug_name(stage_ids[i], static_cast<Stage>(i));
543 void OpenGLProgram::set_stage_debug_name(unsigned stage_id, Stage type)
546 static const char *const suffixes[] = { " [VS]", " [GS]", " [FS]", " [CS]" };
547 string name = debug_name+suffixes[type];
548 glObjectLabel(GL_SHADER, stage_id, name.size(), name.c_str());
550 (void)stage_id; (void)type;