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, TransientData &transient)
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 transient.textures = compiler.get_texture_bindings();
150 transient.blocks = compiler.get_uniform_block_bindings();
152 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
153 rd.n_clip_distances = compiler.get_n_clip_distances();
156 void OpenGLProgram::compile_glsl_stage(const GlslModule &mod, unsigned stage_id)
158 glCompileShader(stage_id);
160 glGetShaderiv(stage_id, GL_COMPILE_STATUS, &status);
162 int info_log_len = 0;
163 glGetShaderiv(stage_id, GL_INFO_LOG_LENGTH, &info_log_len);
164 string info_log(info_log_len+1, 0);
165 glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]);
166 info_log.erase(info_log_len);
167 info_log = mod.get_source_map().translate_errors(info_log);
170 throw compile_error(info_log);
172 if(!info_log.empty())
173 IO::print("Shader compile info log:\n%s", info_log);
177 void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
179 static Require _req(ARB_gl_spirv);
180 static Require _req2(ARB_ES2_compatibility);
182 unsigned n_stages = 0;
183 unsigned used_stage_ids[MAX_STAGES];
184 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
186 unsigned stage_id = 0;
189 case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break;
190 case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
191 case SpirVModule::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
192 default: throw invalid_operation("OpenGLProgram::add_spirv_stages");
195 used_stage_ids[n_stages++] = stage_id;
199 throw invalid_argument("OpenGLProgram::add_spirv_stages");
201 const vector<uint32_t> &code = mod.get_code();
202 glShaderBinary(n_stages, used_stage_ids, GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
204 const vector<SpirVModule::Constant> &spec_consts = mod.get_spec_constants();
205 vector<unsigned> spec_id_array;
206 vector<unsigned> spec_value_array;
207 spec_id_array.reserve(spec_consts.size());
208 spec_value_array.reserve(spec_consts.size());
209 for(const SpirVModule::Constant &c: spec_consts)
211 auto i = spec_values.find(c.name);
212 if(i!=spec_values.end())
214 spec_id_array.push_back(c.constant_id);
215 spec_value_array.push_back(i->second);
219 auto j = mod.get_entry_points().begin();
220 for(unsigned i=0; i<MAX_STAGES; ++i)
222 glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
225 void OpenGLProgram::finalize(const Module &mod, TransientData &transient)
229 glGetProgramiv(id, GL_LINK_STATUS, &status);
232 int info_log_len = 0;
233 glGetProgramiv(id, GL_INFO_LOG_LENGTH, &info_log_len);
234 string info_log(info_log_len+1, 0);
235 glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]);
236 info_log.erase(info_log_len);
237 if(mod.get_format()==Module::GLSL)
238 info_log = static_cast<const GlslModule &>(mod).get_source_map().translate_errors(info_log);
241 throw compile_error(info_log);
243 if(!info_log.empty())
244 IO::print("Program link info log:\n%s", info_log);
247 if(mod.get_format()==Module::GLSL)
251 apply_bindings(transient);
255 void OpenGLProgram::query_uniforms()
257 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
260 glGetProgramiv(id, GL_ACTIVE_UNIFORMS, reinterpret_cast<int *>(&count));
261 rd.uniforms.reserve(count);
262 vector<string> uniform_names(count);
263 for(unsigned i=0; i<count; ++i)
269 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
270 if(len && strncmp(name, "gl_", 3))
272 /* Some implementations report the first element of a uniform array,
273 others report just the name of the array itself. */
274 if(len>3 && !strcmp(name+len-3, "[0]"))
277 rd.uniforms.push_back(ReflectData::UniformInfo());
278 ReflectData::UniformInfo &info = rd.uniforms.back();
281 info.array_size = size;
282 info.type = from_gl_type(type);
283 uniform_names[i] = name;
287 sort_member(rd.uniforms, &ReflectData::UniformInfo::tag);
289 if(ARB_uniform_buffer_object)
291 vector<ReflectData::UniformInfo *> uniforms_by_index(count);
292 for(unsigned i=0; i<count; ++i)
293 if(!uniform_names[i].empty())
294 // The element is already known to be present
295 uniforms_by_index[i] = &*lower_bound_member(rd.uniforms, Tag(uniform_names[i]), &ReflectData::UniformInfo::tag);
296 query_uniform_blocks(uniforms_by_index);
299 rd.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
300 ReflectData::UniformBlockInfo &default_block = rd.uniform_blocks.back();
302 for(ReflectData::UniformInfo &u: rd.uniforms)
305 u.location = glGetUniformLocation(id, u.name.c_str());
306 u.block = &default_block;
307 u.array_stride = get_type_size(u.type);
308 if(is_matrix(u.type))
309 u.matrix_stride = get_type_size(get_matrix_column_type(u.type));
310 default_block.uniforms.push_back(&u);
312 if(is_image(u.type) && u.location>=0)
313 glGetUniformiv(id, u.location, &u.binding);
316 default_block.sort_uniforms();
317 default_block.update_layout_hash();
318 rd.update_layout_hash();
321 void OpenGLProgram::query_uniform_blocks(const vector<ReflectData::UniformInfo *> &uniforms_by_index)
323 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
326 glGetProgramiv(id, GL_ACTIVE_UNIFORM_BLOCKS, reinterpret_cast<int *>(&count));
327 // Reserve an extra index for the default block
328 rd.uniform_blocks.reserve(count+1);
329 for(unsigned i=0; i<count; ++i)
333 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
334 rd.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
335 ReflectData::UniformBlockInfo &info = rd.uniform_blocks.back();
339 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
340 info.data_size = value;
342 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_BINDING, &value);
343 info.bind_point = value;
345 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
346 vector<int> indices(value);
347 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
350 if(!uniforms_by_index[j])
351 throw logic_error("OpenGLProgram::query_uniform_blocks");
352 info.uniforms.push_back(uniforms_by_index[j]);
353 uniforms_by_index[j]->block = &info;
356 vector<unsigned> query_indices(indices.begin(), indices.end());
357 vector<int> values(indices.size());
358 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_OFFSET, &values[0]);
359 for(unsigned j=0; j<indices.size(); ++j)
360 uniforms_by_index[indices[j]]->offset = values[j];
362 query_indices.clear();
364 if(uniforms_by_index[j]->array_size>1)
365 query_indices.push_back(j);
366 if(!query_indices.empty())
368 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
369 for(unsigned j=0; j<query_indices.size(); ++j)
370 uniforms_by_index[query_indices[j]]->array_stride = values[j];
373 query_indices.clear();
376 DataType t = uniforms_by_index[j]->type;
378 query_indices.push_back(j);
380 if(!query_indices.empty())
382 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
383 for(unsigned j=0; j<query_indices.size(); ++j)
384 uniforms_by_index[query_indices[j]]->matrix_stride = values[j];
387 info.sort_uniforms();
388 info.update_layout_hash();
392 void OpenGLProgram::query_attributes()
394 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
397 glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, reinterpret_cast<int *>(&count));
398 rd.attributes.reserve(count);
399 for(unsigned i=0; i<count; ++i)
405 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
406 if(len && strncmp(name, "gl_", 3))
408 if(len>3 && !strcmp(name+len-3, "[0]"))
411 rd.attributes.push_back(ReflectData::AttributeInfo());
412 ReflectData::AttributeInfo &info = rd.attributes.back();
414 info.location = glGetAttribLocation(id, name);
415 info.array_size = size;
416 info.type = from_gl_type(type);
421 void OpenGLProgram::apply_bindings(const TransientData &transient)
423 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
425 for(unsigned i=0; i<rd.uniform_blocks.size(); ++i)
427 auto j = transient.blocks.find(rd.uniform_blocks[i].name);
428 if(j!=transient.blocks.end())
430 glUniformBlockBinding(id, i, j->second);
431 rd.uniform_blocks[i].bind_point = j->second;
435 if(!ARB_separate_shader_objects)
437 for(const auto &kvp: transient.textures)
439 int location = static_cast<const Program *>(this)->get_uniform_location(kvp.first);
442 if(ARB_separate_shader_objects)
443 glProgramUniform1i(id, location, kvp.second);
445 glUniform1i(location, kvp.second);
450 void OpenGLProgram::finalize_uniforms()
452 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
454 auto i = find_if(rd.uniform_blocks, [](const ReflectData::UniformBlockInfo &b){ return b.bind_point<0; });
455 if(i!=rd.uniform_blocks.end() && !i->uniforms.empty())
457 for(const ReflectData::UniformInfo *u: i->uniforms)
460 UniformCall::FuncPtr func = 0;
462 func = &uniform_wrapper<float, glUniform1fv>;
463 else if(u->type==FLOAT_VEC2)
464 func = &uniform_wrapper<float, glUniform2fv>;
465 else if(u->type==FLOAT_VEC3)
466 func = &uniform_wrapper<float, glUniform3fv>;
467 else if(u->type==FLOAT_VEC4)
468 func = &uniform_wrapper<float, glUniform4fv>;
469 else if(u->type==INT)
470 func = &uniform_wrapper<int, glUniform1iv>;
471 else if(u->type==INT_VEC2)
472 func = &uniform_wrapper<int, glUniform2iv>;
473 else if(u->type==INT_VEC3)
474 func = &uniform_wrapper<int, glUniform3iv>;
475 else if(u->type==INT_VEC4)
476 func = &uniform_wrapper<int, glUniform4iv>;
477 else if(u->type==UNSIGNED_INT)
478 func = &uniform_wrapper<unsigned, glUniform1uiv>;
479 else if(u->type==UINT_VEC2)
480 func = &uniform_wrapper<unsigned, glUniform2uiv>;
481 else if(u->type==UINT_VEC3)
482 func = &uniform_wrapper<unsigned, glUniform3uiv>;
483 else if(u->type==UINT_VEC4)
484 func = &uniform_wrapper<unsigned, glUniform4uiv>;
485 else if(u->type==FLOAT_MAT2)
486 func = &uniform_matrix_wrapper<float, glUniformMatrix2fv>;
487 else if(u->type==FLOAT_MAT3)
488 func = &uniform_matrix_wrapper<float, glUniformMatrix3fv>;
489 else if(u->type==FLOAT_MAT4)
490 func = &uniform_matrix_wrapper<float, glUniformMatrix4fv>;
491 else if(u->type==FLOAT_MAT2x3)
492 func = &uniform_matrix_wrapper<float, glUniformMatrix2x3fv>;
493 else if(u->type==FLOAT_MAT3x2)
494 func = &uniform_matrix_wrapper<float, glUniformMatrix3x2fv>;
495 else if(u->type==FLOAT_MAT2x4)
496 func = &uniform_matrix_wrapper<float, glUniformMatrix2x4fv>;
497 else if(u->type==FLOAT_MAT4x2)
498 func = &uniform_matrix_wrapper<float, glUniformMatrix4x2fv>;
499 else if(u->type==FLOAT_MAT3x4)
500 func = &uniform_matrix_wrapper<float, glUniformMatrix3x4fv>;
501 else if(u->type==FLOAT_MAT4x3)
502 func = &uniform_matrix_wrapper<float, glUniformMatrix4x3fv>;
505 uniform_calls.push_back(UniformCall(u->location, u->array_size, func));
510 const ReflectData::UniformInfo &last = *i->uniforms.back();
511 i->data_size = last.location*16+last.array_size*get_type_size(last.type);
516 void OpenGLProgram::set_debug_name(const string &name)
522 glObjectLabel(GL_PROGRAM, id, name.size(), name.c_str());
523 for(unsigned i=0; i<MAX_STAGES; ++i)
525 set_stage_debug_name(stage_ids[i], static_cast<Stage>(i));
532 void OpenGLProgram::set_stage_debug_name(unsigned stage_id, Stage type)
535 static const char *const suffixes[] = { " [VS]", " [GS]", " [FS]" };
536 string name = debug_name+suffixes[type];
537 glObjectLabel(GL_SHADER, stage_id, name.size(), name.c_str());
539 (void)stage_id; (void)type;