2 #include <msp/core/algorithm.h>
3 #include <msp/gl/extensions/arb_es2_compatibility.h>
4 #include <msp/gl/extensions/arb_fragment_shader.h>
5 #include <msp/gl/extensions/arb_gl_spirv.h>
6 #include <msp/gl/extensions/arb_geometry_shader4.h>
7 #include <msp/gl/extensions/arb_separate_shader_objects.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/khr_debug.h>
13 #include <msp/gl/extensions/nv_non_square_matrices.h>
14 #include <msp/io/print.h>
18 #include "program_backend.h"
19 #include "glsl/compiler.h"
25 template<typename T, void (*&func)(GLint, GLsizei, const T *)>
26 void uniform_wrapper(unsigned index, unsigned count, const void *data)
28 func(index, count, static_cast<const T *>(data));
31 template<typename T, void (*&func)(GLint, GLsizei, GLboolean, const T *)>
32 void uniform_matrix_wrapper(unsigned index, unsigned count, const void *data)
34 func(index, count, false, static_cast<const T *>(data));
42 OpenGLProgram::OpenGLProgram()
44 static Require _req(ARB_shader_objects);
46 id = glCreateProgram();
49 OpenGLProgram::OpenGLProgram(OpenGLProgram &&other):
52 uniform_calls(move(other.uniform_calls)),
53 debug_name(move(other.debug_name))
55 move(other.stage_ids, other.stage_ids+MAX_STAGES, stage_ids);
57 fill(other.stage_ids, other.stage_ids+MAX_STAGES, 0);
60 OpenGLProgram::~OpenGLProgram()
62 for(unsigned i=0; i<MAX_STAGES; ++i)
64 glDeleteShader(stage_ids[i]);
68 bool OpenGLProgram::has_stages() const
70 for(unsigned i=0; i<MAX_STAGES; ++i)
76 unsigned OpenGLProgram::add_stage(Stage type)
81 case VERTEX: { static Require _req(ARB_vertex_shader); gl_type = GL_VERTEX_SHADER; } break;
82 case GEOMETRY: { static Require _req(ARB_geometry_shader4); gl_type = GL_GEOMETRY_SHADER; } break;
83 case FRAGMENT: { static Require _req(ARB_fragment_shader); gl_type = GL_FRAGMENT_SHADER; } break;
84 default: throw invalid_argument("OpenGLProgram::add_stage");
88 throw invalid_operation("OpenGLProgram::add_stage");
90 unsigned stage_id = glCreateShader(gl_type);
91 stage_ids[type] = stage_id;
92 glAttachShader(id, stage_id);
95 if(!debug_name.empty() && KHR_debug)
96 set_stage_debug_name(stage_id, type);
102 void OpenGLProgram::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values)
104 SL::Compiler compiler(Device::get_current().get_info().glsl_features);
105 compiler.set_source(mod.get_prepared_source(), "<module>");
106 compiler.specialize(spec_values);
107 compiler.compile(SL::Compiler::PROGRAM);
109 string diagnostics = compiler.get_diagnostics();
110 if(!diagnostics.empty())
111 IO::print("Program diagnostics:\n%s\n", diagnostics);
114 vector<SL::Stage::Type> stages = compiler.get_stages();
116 throw invalid_argument("OpenGLProgram::add_glsl_stages");
118 for(SL::Stage::Type st: stages)
120 unsigned stage_id = 0;
123 case SL::Stage::VERTEX: stage_id = add_stage(VERTEX); break;
124 case SL::Stage::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
125 case SL::Stage::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
126 default: throw invalid_operation("OpenGLProgram::add_glsl_stages");
129 string stage_src = compiler.get_stage_glsl(st);
130 const char *src_ptr = stage_src.data();
131 int src_len = stage_src.size();
132 glShaderSource(stage_id, 1, &src_ptr, &src_len);
134 if(st==SL::Stage::VERTEX)
136 for(const auto &kvp: compiler.get_vertex_attributes())
137 glBindAttribLocation(id, kvp.second, kvp.first.c_str());
140 if(st==SL::Stage::FRAGMENT && EXT_gpu_shader4)
142 for(const auto &kvp: compiler.get_fragment_outputs())
143 glBindFragDataLocation(id, kvp.second, kvp.first.c_str());
146 compile_glsl_stage(mod, stage_id);
149 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
150 rd.n_clip_distances = compiler.get_n_clip_distances();
156 const map<string, unsigned> &block_bindings = compiler.get_uniform_block_bindings();
157 if(!block_bindings.empty())
159 for(unsigned i=0; i<rd.uniform_blocks.size(); ++i)
161 auto j = block_bindings.find(rd.uniform_blocks[i].name);
162 if(j!=block_bindings.end())
164 glUniformBlockBinding(id, i, j->second);
165 rd.uniform_blocks[i].bind_point = j->second;
170 const map<string, unsigned> &tex_bindings = compiler.get_texture_bindings();
171 if(!tex_bindings.empty())
173 if(!ARB_separate_shader_objects)
175 for(const auto &kvp: tex_bindings)
177 int location = static_cast<const Program *>(this)->get_uniform_location(kvp.first);
180 if(ARB_separate_shader_objects)
181 glProgramUniform1i(id, location, kvp.second);
183 glUniform1i(location, kvp.second);
189 void OpenGLProgram::compile_glsl_stage(const GlslModule &mod, unsigned stage_id)
191 glCompileShader(stage_id);
193 glGetShaderiv(stage_id, GL_COMPILE_STATUS, &status);
195 int info_log_len = 0;
196 glGetShaderiv(stage_id, GL_INFO_LOG_LENGTH, &info_log_len);
197 string info_log(info_log_len+1, 0);
198 glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]);
199 info_log.erase(info_log_len);
200 info_log = mod.get_source_map().translate_errors(info_log);
203 throw compile_error(info_log);
205 if(!info_log.empty())
206 IO::print("Shader compile info log:\n%s", info_log);
210 void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
212 static Require _req(ARB_gl_spirv);
213 static Require _req2(ARB_ES2_compatibility);
215 unsigned n_stages = 0;
216 unsigned used_stage_ids[MAX_STAGES];
217 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
219 unsigned stage_id = 0;
222 case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break;
223 case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
224 case SpirVModule::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
225 default: throw invalid_operation("OpenGLProgram::add_spirv_stages");
228 used_stage_ids[n_stages++] = stage_id;
232 throw invalid_argument("OpenGLProgram::add_spirv_stages");
234 const vector<uint32_t> &code = mod.get_code();
235 glShaderBinary(n_stages, used_stage_ids, GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
237 const vector<SpirVModule::Constant> &spec_consts = mod.get_spec_constants();
238 vector<unsigned> spec_id_array;
239 vector<unsigned> spec_value_array;
240 spec_id_array.reserve(spec_consts.size());
241 spec_value_array.reserve(spec_consts.size());
242 for(const SpirVModule::Constant &c: spec_consts)
244 auto i = spec_values.find(c.name);
245 if(i!=spec_values.end())
247 spec_id_array.push_back(c.constant_id);
248 spec_value_array.push_back(i->second);
252 auto j = mod.get_entry_points().begin();
253 for(unsigned i=0; i<MAX_STAGES; ++i)
255 glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
260 void OpenGLProgram::link(const Module &mod)
264 glGetProgramiv(id, GL_LINK_STATUS, &status);
267 int info_log_len = 0;
268 glGetProgramiv(id, GL_INFO_LOG_LENGTH, &info_log_len);
269 string info_log(info_log_len+1, 0);
270 glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]);
271 info_log.erase(info_log_len);
272 if(mod.get_format()==Module::GLSL)
273 info_log = static_cast<const GlslModule &>(mod).get_source_map().translate_errors(info_log);
276 throw compile_error(info_log);
278 if(!info_log.empty())
279 IO::print("Program link info log:\n%s", info_log);
283 void OpenGLProgram::query_uniforms()
285 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
288 glGetProgramiv(id, GL_ACTIVE_UNIFORMS, reinterpret_cast<int *>(&count));
289 rd.uniforms.reserve(count);
290 vector<string> uniform_names(count);
291 for(unsigned i=0; i<count; ++i)
297 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
298 if(len && strncmp(name, "gl_", 3))
300 /* Some implementations report the first element of a uniform array,
301 others report just the name of the array itself. */
302 if(len>3 && !strcmp(name+len-3, "[0]"))
305 rd.uniforms.emplace_back();
306 ReflectData::UniformInfo &info = rd.uniforms.back();
309 info.array_size = size;
310 info.type = from_gl_type(type);
311 uniform_names[i] = name;
315 sort_member(rd.uniforms, &ReflectData::UniformInfo::tag);
317 if(ARB_uniform_buffer_object)
319 vector<ReflectData::UniformInfo *> uniforms_by_index(count);
320 for(unsigned i=0; i<count; ++i)
321 if(!uniform_names[i].empty())
322 // The element is already known to be present
323 uniforms_by_index[i] = &*lower_bound_member(rd.uniforms, Tag(uniform_names[i]), &ReflectData::UniformInfo::tag);
324 query_uniform_blocks(uniforms_by_index);
327 rd.uniform_blocks.emplace_back();
328 ReflectData::UniformBlockInfo &default_block = rd.uniform_blocks.back();
330 for(ReflectData::UniformInfo &u: rd.uniforms)
333 u.location = glGetUniformLocation(id, u.name.c_str());
334 u.block = &default_block;
335 u.array_stride = get_type_size(u.type);
336 if(is_matrix(u.type))
337 u.matrix_stride = get_type_size(get_matrix_column_type(u.type));
338 default_block.uniforms.push_back(&u);
340 if(is_image(u.type) && u.location>=0)
341 glGetUniformiv(id, u.location, &u.binding);
344 default_block.sort_uniforms();
345 default_block.update_layout_hash();
346 rd.update_layout_hash();
349 void OpenGLProgram::query_uniform_blocks(const vector<ReflectData::UniformInfo *> &uniforms_by_index)
351 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
354 glGetProgramiv(id, GL_ACTIVE_UNIFORM_BLOCKS, reinterpret_cast<int *>(&count));
355 // Reserve an extra index for the default block
356 rd.uniform_blocks.reserve(count+1);
357 for(unsigned i=0; i<count; ++i)
361 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
362 rd.uniform_blocks.emplace_back();
363 ReflectData::UniformBlockInfo &info = rd.uniform_blocks.back();
367 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
368 info.data_size = value;
370 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_BINDING, &value);
371 info.bind_point = value;
373 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
374 vector<int> indices(value);
375 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
378 if(!uniforms_by_index[j])
379 throw logic_error("OpenGLProgram::query_uniform_blocks");
380 info.uniforms.push_back(uniforms_by_index[j]);
381 uniforms_by_index[j]->block = &info;
384 vector<unsigned> query_indices(indices.begin(), indices.end());
385 vector<int> values(indices.size());
386 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_OFFSET, &values[0]);
387 for(unsigned j=0; j<indices.size(); ++j)
388 uniforms_by_index[indices[j]]->offset = values[j];
390 query_indices.clear();
392 if(uniforms_by_index[j]->array_size>1)
393 query_indices.push_back(j);
394 if(!query_indices.empty())
396 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
397 for(unsigned j=0; j<query_indices.size(); ++j)
398 uniforms_by_index[query_indices[j]]->array_stride = values[j];
401 query_indices.clear();
404 DataType t = uniforms_by_index[j]->type;
406 query_indices.push_back(j);
408 if(!query_indices.empty())
410 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
411 for(unsigned j=0; j<query_indices.size(); ++j)
412 uniforms_by_index[query_indices[j]]->matrix_stride = values[j];
415 info.sort_uniforms();
416 info.update_layout_hash();
420 void OpenGLProgram::query_attributes()
422 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
425 glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, reinterpret_cast<int *>(&count));
426 rd.attributes.reserve(count);
427 for(unsigned i=0; i<count; ++i)
433 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
434 if(len && strncmp(name, "gl_", 3))
436 if(len>3 && !strcmp(name+len-3, "[0]"))
439 rd.attributes.emplace_back();
440 ReflectData::AttributeInfo &info = rd.attributes.back();
442 info.location = glGetAttribLocation(id, name);
443 info.array_size = size;
444 info.type = from_gl_type(type);
448 sort_member(reflect_data.attributes, &ReflectData::AttributeInfo::name);
451 void OpenGLProgram::finalize_uniforms()
453 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
455 auto i = find_if(rd.uniform_blocks, [](const ReflectData::UniformBlockInfo &b){ return b.bind_point<0; });
456 if(i!=rd.uniform_blocks.end() && !i->uniforms.empty())
458 for(const ReflectData::UniformInfo *u: i->uniforms)
461 UniformCall::FuncPtr func = 0;
463 func = &uniform_wrapper<float, glUniform1fv>;
464 else if(u->type==FLOAT_VEC2)
465 func = &uniform_wrapper<float, glUniform2fv>;
466 else if(u->type==FLOAT_VEC3)
467 func = &uniform_wrapper<float, glUniform3fv>;
468 else if(u->type==FLOAT_VEC4)
469 func = &uniform_wrapper<float, glUniform4fv>;
470 else if(u->type==INT)
471 func = &uniform_wrapper<int, glUniform1iv>;
472 else if(u->type==INT_VEC2)
473 func = &uniform_wrapper<int, glUniform2iv>;
474 else if(u->type==INT_VEC3)
475 func = &uniform_wrapper<int, glUniform3iv>;
476 else if(u->type==INT_VEC4)
477 func = &uniform_wrapper<int, glUniform4iv>;
478 else if(u->type==UNSIGNED_INT)
479 func = &uniform_wrapper<unsigned, glUniform1uiv>;
480 else if(u->type==UINT_VEC2)
481 func = &uniform_wrapper<unsigned, glUniform2uiv>;
482 else if(u->type==UINT_VEC3)
483 func = &uniform_wrapper<unsigned, glUniform3uiv>;
484 else if(u->type==UINT_VEC4)
485 func = &uniform_wrapper<unsigned, glUniform4uiv>;
486 else if(u->type==FLOAT_MAT2)
487 func = &uniform_matrix_wrapper<float, glUniformMatrix2fv>;
488 else if(u->type==FLOAT_MAT3)
489 func = &uniform_matrix_wrapper<float, glUniformMatrix3fv>;
490 else if(u->type==FLOAT_MAT4)
491 func = &uniform_matrix_wrapper<float, glUniformMatrix4fv>;
492 else if(u->type==FLOAT_MAT2x3)
493 func = &uniform_matrix_wrapper<float, glUniformMatrix2x3fv>;
494 else if(u->type==FLOAT_MAT3x2)
495 func = &uniform_matrix_wrapper<float, glUniformMatrix3x2fv>;
496 else if(u->type==FLOAT_MAT2x4)
497 func = &uniform_matrix_wrapper<float, glUniformMatrix2x4fv>;
498 else if(u->type==FLOAT_MAT4x2)
499 func = &uniform_matrix_wrapper<float, glUniformMatrix4x2fv>;
500 else if(u->type==FLOAT_MAT3x4)
501 func = &uniform_matrix_wrapper<float, glUniformMatrix3x4fv>;
502 else if(u->type==FLOAT_MAT4x3)
503 func = &uniform_matrix_wrapper<float, glUniformMatrix4x3fv>;
506 uniform_calls.push_back(UniformCall(u->location, u->array_size, func));
511 const ReflectData::UniformInfo &last = *i->uniforms.back();
512 i->data_size = last.location*16+last.array_size*get_type_size(last.type);
517 void OpenGLProgram::set_debug_name(const string &name)
523 glObjectLabel(GL_PROGRAM, id, name.size(), name.c_str());
524 for(unsigned i=0; i<MAX_STAGES; ++i)
526 set_stage_debug_name(stage_ids[i], static_cast<Stage>(i));
533 void OpenGLProgram::set_stage_debug_name(unsigned stage_id, Stage type)
536 static const char *const suffixes[] = { " [VS]", " [GS]", " [FS]" };
537 string name = debug_name+suffixes[type];
538 glObjectLabel(GL_SHADER, stage_id, name.size(), name.c_str());
540 (void)stage_id; (void)type;