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>
17 #include "program_backend.h"
18 #include "glsl/compiler.h"
24 template<typename T, void (*&func)(GLint, GLsizei, const T *)>
25 void uniform_wrapper(unsigned index, unsigned count, const void *data)
27 func(index, count, static_cast<const T *>(data));
30 template<typename T, void (*&func)(GLint, GLsizei, GLboolean, const T *)>
31 void uniform_matrix_wrapper(unsigned index, unsigned count, const void *data)
33 func(index, count, false, static_cast<const T *>(data));
41 OpenGLProgram::OpenGLProgram()
43 static Require _req(ARB_shader_objects);
45 id = glCreateProgram();
48 OpenGLProgram::~OpenGLProgram()
50 for(unsigned i=0; i<MAX_STAGES; ++i)
52 glDeleteShader(stage_ids[i]);
56 bool OpenGLProgram::has_stages() const
58 for(unsigned i=0; i<MAX_STAGES; ++i)
64 unsigned OpenGLProgram::add_stage(Stage type)
69 case VERTEX: { static Require _req(ARB_vertex_shader); gl_type = GL_VERTEX_SHADER; } break;
70 case GEOMETRY: { static Require _req(ARB_geometry_shader4); gl_type = GL_GEOMETRY_SHADER; } break;
71 case FRAGMENT: { static Require _req(ARB_fragment_shader); gl_type = GL_FRAGMENT_SHADER; } break;
72 default: throw invalid_argument("OpenGLProgram::add_stage");
76 throw invalid_operation("OpenGLProgram::add_stage");
78 unsigned stage_id = glCreateShader(gl_type);
79 stage_ids[type] = stage_id;
80 glAttachShader(id, stage_id);
83 if(!debug_name.empty() && KHR_debug)
84 set_stage_debug_name(stage_id, type);
90 void OpenGLProgram::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values, TransientData &transient)
92 SL::Compiler compiler;
93 compiler.set_source(mod.get_prepared_source(), "<module>");
94 compiler.specialize(spec_values);
95 compiler.compile(SL::Compiler::PROGRAM);
97 string diagnostics = compiler.get_diagnostics();
98 if(!diagnostics.empty())
99 IO::print("Program diagnostics:\n%s\n", diagnostics);
102 vector<SL::Stage::Type> stages = compiler.get_stages();
104 throw invalid_argument("OpenGLProgram::add_glsl_stages");
106 for(SL::Stage::Type st: stages)
108 unsigned stage_id = 0;
111 case SL::Stage::VERTEX: stage_id = add_stage(VERTEX); break;
112 case SL::Stage::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
113 case SL::Stage::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
114 default: throw invalid_operation("OpenGLProgram::add_glsl_stages");
117 string stage_src = compiler.get_stage_glsl(st);
118 const char *src_ptr = stage_src.data();
119 int src_len = stage_src.size();
120 glShaderSource(stage_id, 1, &src_ptr, &src_len);
122 if(st==SL::Stage::VERTEX)
124 for(const auto &kvp: compiler.get_vertex_attributes())
125 glBindAttribLocation(id, kvp.second, kvp.first.c_str());
128 if(st==SL::Stage::FRAGMENT && EXT_gpu_shader4)
130 for(const auto &kvp: compiler.get_fragment_outputs())
131 glBindFragDataLocation(id, kvp.second, kvp.first.c_str());
134 compile_glsl_stage(mod, stage_id);
137 transient.textures = compiler.get_texture_bindings();
138 transient.blocks = compiler.get_uniform_block_bindings();
140 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
141 rd.n_clip_distances = compiler.get_n_clip_distances();
144 void OpenGLProgram::compile_glsl_stage(const GlslModule &mod, unsigned stage_id)
146 glCompileShader(stage_id);
148 glGetShaderiv(stage_id, GL_COMPILE_STATUS, &status);
150 int info_log_len = 0;
151 glGetShaderiv(stage_id, GL_INFO_LOG_LENGTH, &info_log_len);
152 string info_log(info_log_len+1, 0);
153 glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]);
154 info_log.erase(info_log_len);
155 info_log = mod.get_source_map().translate_errors(info_log);
158 throw compile_error(info_log);
160 if(!info_log.empty())
161 IO::print("Shader compile info log:\n%s", info_log);
165 void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
167 static Require _req(ARB_gl_spirv);
168 static Require _req2(ARB_ES2_compatibility);
170 unsigned n_stages = 0;
171 unsigned used_stage_ids[MAX_STAGES];
172 for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
174 unsigned stage_id = 0;
177 case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break;
178 case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
179 case SpirVModule::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
180 default: throw invalid_operation("OpenGLProgram::add_spirv_stages");
183 used_stage_ids[n_stages++] = stage_id;
187 throw invalid_argument("OpenGLProgram::add_spirv_stages");
189 const vector<uint32_t> &code = mod.get_code();
190 glShaderBinary(n_stages, used_stage_ids, GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
192 const vector<SpirVModule::Constant> &spec_consts = mod.get_spec_constants();
193 vector<unsigned> spec_id_array;
194 vector<unsigned> spec_value_array;
195 spec_id_array.reserve(spec_consts.size());
196 spec_value_array.reserve(spec_consts.size());
197 for(const SpirVModule::Constant &c: spec_consts)
199 auto i = spec_values.find(c.name);
200 if(i!=spec_values.end())
202 spec_id_array.push_back(c.constant_id);
203 spec_value_array.push_back(i->second);
207 auto j = mod.get_entry_points().begin();
208 for(unsigned i=0; i<MAX_STAGES; ++i)
210 glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
213 void OpenGLProgram::finalize(const Module &mod, TransientData &transient)
217 glGetProgramiv(id, GL_LINK_STATUS, &status);
220 int info_log_len = 0;
221 glGetProgramiv(id, GL_INFO_LOG_LENGTH, &info_log_len);
222 string info_log(info_log_len+1, 0);
223 glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]);
224 info_log.erase(info_log_len);
225 if(mod.get_format()==Module::GLSL)
226 info_log = static_cast<const GlslModule &>(mod).get_source_map().translate_errors(info_log);
229 throw compile_error(info_log);
231 if(!info_log.empty())
232 IO::print("Program link info log:\n%s", info_log);
235 if(mod.get_format()==Module::GLSL)
239 apply_bindings(transient);
243 void OpenGLProgram::query_uniforms()
245 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
248 glGetProgramiv(id, GL_ACTIVE_UNIFORMS, reinterpret_cast<int *>(&count));
249 rd.uniforms.reserve(count);
250 vector<string> uniform_names(count);
251 for(unsigned i=0; i<count; ++i)
257 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
258 if(len && strncmp(name, "gl_", 3))
260 /* Some implementations report the first element of a uniform array,
261 others report just the name of the array itself. */
262 if(len>3 && !strcmp(name+len-3, "[0]"))
265 rd.uniforms.push_back(ReflectData::UniformInfo());
266 ReflectData::UniformInfo &info = rd.uniforms.back();
269 info.array_size = size;
270 info.type = from_gl_type(type);
271 uniform_names[i] = name;
275 sort_member(rd.uniforms, &ReflectData::UniformInfo::tag);
277 if(ARB_uniform_buffer_object)
279 vector<ReflectData::UniformInfo *> uniforms_by_index(count);
280 for(unsigned i=0; i<count; ++i)
281 if(!uniform_names[i].empty())
282 // The element is already known to be present
283 uniforms_by_index[i] = &*lower_bound_member(rd.uniforms, Tag(uniform_names[i]), &ReflectData::UniformInfo::tag);
284 query_uniform_blocks(uniforms_by_index);
287 rd.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
288 ReflectData::UniformBlockInfo &default_block = rd.uniform_blocks.back();
290 for(ReflectData::UniformInfo &u: rd.uniforms)
293 u.location = glGetUniformLocation(id, u.name.c_str());
294 u.block = &default_block;
295 u.array_stride = get_type_size(u.type);
296 if(is_matrix(u.type))
297 u.matrix_stride = get_type_size(get_matrix_column_type(u.type));
298 default_block.uniforms.push_back(&u);
300 if(is_image(u.type) && u.location>=0)
301 glGetUniformiv(id, u.location, &u.binding);
304 default_block.sort_uniforms();
305 default_block.update_layout_hash();
306 rd.update_layout_hash();
309 void OpenGLProgram::query_uniform_blocks(const vector<ReflectData::UniformInfo *> &uniforms_by_index)
311 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
314 glGetProgramiv(id, GL_ACTIVE_UNIFORM_BLOCKS, reinterpret_cast<int *>(&count));
315 // Reserve an extra index for the default block
316 rd.uniform_blocks.reserve(count+1);
317 for(unsigned i=0; i<count; ++i)
321 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
322 rd.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
323 ReflectData::UniformBlockInfo &info = rd.uniform_blocks.back();
327 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
328 info.data_size = value;
330 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_BINDING, &value);
331 info.bind_point = value;
333 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
334 vector<int> indices(value);
335 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
338 if(!uniforms_by_index[j])
339 throw logic_error("OpenGLProgram::query_uniform_blocks");
340 info.uniforms.push_back(uniforms_by_index[j]);
341 uniforms_by_index[j]->block = &info;
344 vector<unsigned> query_indices(indices.begin(), indices.end());
345 vector<int> values(indices.size());
346 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_OFFSET, &values[0]);
347 for(unsigned j=0; j<indices.size(); ++j)
348 uniforms_by_index[indices[j]]->offset = values[j];
350 query_indices.clear();
352 if(uniforms_by_index[j]->array_size>1)
353 query_indices.push_back(j);
354 if(!query_indices.empty())
356 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
357 for(unsigned j=0; j<query_indices.size(); ++j)
358 uniforms_by_index[query_indices[j]]->array_stride = values[j];
361 query_indices.clear();
364 DataType t = uniforms_by_index[j]->type;
366 query_indices.push_back(j);
368 if(!query_indices.empty())
370 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
371 for(unsigned j=0; j<query_indices.size(); ++j)
372 uniforms_by_index[query_indices[j]]->matrix_stride = values[j];
375 info.sort_uniforms();
376 info.update_layout_hash();
380 void OpenGLProgram::query_attributes()
382 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
385 glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, reinterpret_cast<int *>(&count));
386 rd.attributes.reserve(count);
387 for(unsigned i=0; i<count; ++i)
393 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
394 if(len && strncmp(name, "gl_", 3))
396 if(len>3 && !strcmp(name+len-3, "[0]"))
399 rd.attributes.push_back(ReflectData::AttributeInfo());
400 ReflectData::AttributeInfo &info = rd.attributes.back();
402 info.location = glGetAttribLocation(id, name);
403 info.array_size = size;
404 info.type = from_gl_type(type);
409 void OpenGLProgram::apply_bindings(const TransientData &transient)
411 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
413 for(unsigned i=0; i<rd.uniform_blocks.size(); ++i)
415 auto j = transient.blocks.find(rd.uniform_blocks[i].name);
416 if(j!=transient.blocks.end())
418 glUniformBlockBinding(id, i, j->second);
419 rd.uniform_blocks[i].bind_point = j->second;
423 if(!ARB_separate_shader_objects)
425 for(const auto &kvp: transient.textures)
427 int location = static_cast<const Program *>(this)->get_uniform_location(kvp.first);
430 if(ARB_separate_shader_objects)
431 glProgramUniform1i(id, location, kvp.second);
433 glUniform1i(location, kvp.second);
438 void OpenGLProgram::finalize_uniforms()
440 ReflectData &rd = static_cast<Program *>(this)->reflect_data;
442 auto i = find_if(rd.uniform_blocks, [](const ReflectData::UniformBlockInfo &b){ return b.bind_point<0; });
443 if(i!=rd.uniform_blocks.end() && !i->uniforms.empty())
445 for(const ReflectData::UniformInfo *u: i->uniforms)
448 UniformCall::FuncPtr func = 0;
450 func = &uniform_wrapper<float, glUniform1fv>;
451 else if(u->type==FLOAT_VEC2)
452 func = &uniform_wrapper<float, glUniform2fv>;
453 else if(u->type==FLOAT_VEC3)
454 func = &uniform_wrapper<float, glUniform3fv>;
455 else if(u->type==FLOAT_VEC4)
456 func = &uniform_wrapper<float, glUniform4fv>;
457 else if(u->type==INT)
458 func = &uniform_wrapper<int, glUniform1iv>;
459 else if(u->type==INT_VEC2)
460 func = &uniform_wrapper<int, glUniform2iv>;
461 else if(u->type==INT_VEC3)
462 func = &uniform_wrapper<int, glUniform3iv>;
463 else if(u->type==INT_VEC4)
464 func = &uniform_wrapper<int, glUniform4iv>;
465 else if(u->type==UNSIGNED_INT)
466 func = &uniform_wrapper<unsigned, glUniform1uiv>;
467 else if(u->type==UINT_VEC2)
468 func = &uniform_wrapper<unsigned, glUniform2uiv>;
469 else if(u->type==UINT_VEC3)
470 func = &uniform_wrapper<unsigned, glUniform3uiv>;
471 else if(u->type==UINT_VEC4)
472 func = &uniform_wrapper<unsigned, glUniform4uiv>;
473 else if(u->type==FLOAT_MAT2)
474 func = &uniform_matrix_wrapper<float, glUniformMatrix2fv>;
475 else if(u->type==FLOAT_MAT3)
476 func = &uniform_matrix_wrapper<float, glUniformMatrix3fv>;
477 else if(u->type==FLOAT_MAT4)
478 func = &uniform_matrix_wrapper<float, glUniformMatrix4fv>;
479 else if(u->type==FLOAT_MAT2x3)
480 func = &uniform_matrix_wrapper<float, glUniformMatrix2x3fv>;
481 else if(u->type==FLOAT_MAT3x2)
482 func = &uniform_matrix_wrapper<float, glUniformMatrix3x2fv>;
483 else if(u->type==FLOAT_MAT2x4)
484 func = &uniform_matrix_wrapper<float, glUniformMatrix2x4fv>;
485 else if(u->type==FLOAT_MAT4x2)
486 func = &uniform_matrix_wrapper<float, glUniformMatrix4x2fv>;
487 else if(u->type==FLOAT_MAT3x4)
488 func = &uniform_matrix_wrapper<float, glUniformMatrix3x4fv>;
489 else if(u->type==FLOAT_MAT4x3)
490 func = &uniform_matrix_wrapper<float, glUniformMatrix4x3fv>;
493 uniform_calls.push_back(UniformCall(u->location, u->array_size, func));
498 const ReflectData::UniformInfo &last = *i->uniforms.back();
499 i->data_size = last.location*16+last.array_size*get_type_size(last.type);
504 void OpenGLProgram::set_debug_name(const string &name)
510 glObjectLabel(GL_PROGRAM, id, name.size(), name.c_str());
511 for(unsigned i=0; i<MAX_STAGES; ++i)
513 set_stage_debug_name(stage_ids[i], static_cast<Stage>(i));
520 void OpenGLProgram::set_stage_debug_name(unsigned stage_id, Stage type)
523 static const char *const suffixes[] = { " [VS]", " [GS]", " [FS]" };
524 string name = debug_name+suffixes[type];
525 glObjectLabel(GL_SHADER, stage_id, name.size(), name.c_str());
527 (void)stage_id; (void)type;