]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/program_backend.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / backends / opengl / program_backend.cpp
1 #include <cstring>
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_tessellation_shader.h>
12 #include <msp/gl/extensions/arb_vertex_shader.h>
13 #include <msp/gl/extensions/ext_gpu_shader4.h>
14 #include <msp/gl/extensions/khr_debug.h>
15 #include <msp/gl/extensions/nv_non_square_matrices.h>
16 #include <msp/io/print.h>
17 #include "device.h"
18 #include "error.h"
19 #include "program.h"
20 #include "program_backend.h"
21 #include "glsl/compiler.h"
22
23 using namespace std;
24
25 namespace {
26
27 template<typename T, void (*&func)(GLint, GLsizei, const T *)>
28 void uniform_wrapper(unsigned index, unsigned count, const void *data)
29 {
30         func(index, count, static_cast<const T *>(data));
31 }
32
33 template<typename T, void (*&func)(GLint, GLsizei, GLboolean, const T *)>
34 void uniform_matrix_wrapper(unsigned index, unsigned count, const void *data)
35 {
36         func(index, count, false, static_cast<const T *>(data));
37 }
38
39 }
40
41 namespace Msp {
42 namespace GL {
43
44 OpenGLProgram::OpenGLProgram()
45 {
46         static Require _req(ARB_shader_objects);
47
48         id = glCreateProgram();
49 }
50
51 OpenGLProgram::OpenGLProgram(OpenGLProgram &&other):
52         id(other.id),
53         linked(other.linked),
54         uniform_calls(move(other.uniform_calls)),
55         debug_name(move(other.debug_name))
56 {
57         move(other.stage_ids, other.stage_ids+MAX_STAGES, stage_ids);
58         other.id = 0;
59         fill(other.stage_ids, other.stage_ids+MAX_STAGES, 0);
60 }
61
62 OpenGLProgram::~OpenGLProgram()
63 {
64         for(unsigned i=0; i<MAX_STAGES; ++i)
65                 if(stage_ids[i])
66                         glDeleteShader(stage_ids[i]);
67         glDeleteProgram(id);
68 }
69
70 bool OpenGLProgram::has_stages() const
71 {
72         for(unsigned i=0; i<MAX_STAGES; ++i)
73                 if(stage_ids[i])
74                         return true;
75         return false;
76 }
77
78 unsigned OpenGLProgram::add_stage(Stage type)
79 {
80         GLenum gl_type;
81         switch(type)
82         {
83         case VERTEX: { static Require _req(ARB_vertex_shader); gl_type = GL_VERTEX_SHADER; } break;
84         case TESS_CONTROL: { static Require _req(ARB_tessellation_shader); gl_type = GL_TESS_CONTROL_SHADER; } break;
85         case TESS_EVAL: { static Require _req(ARB_tessellation_shader); gl_type = GL_TESS_EVALUATION_SHADER; } break;
86         case GEOMETRY: { static Require _req(ARB_geometry_shader4); gl_type = GL_GEOMETRY_SHADER; } break;
87         case FRAGMENT: { static Require _req(ARB_fragment_shader); gl_type = GL_FRAGMENT_SHADER; } break;
88         case COMPUTE: { static Require _req(ARB_compute_shader); gl_type = GL_COMPUTE_SHADER; } break;
89         default: throw invalid_argument("OpenGLProgram::add_stage");
90         }
91
92         if(stage_ids[type])
93                 throw invalid_operation("OpenGLProgram::add_stage");
94
95         unsigned stage_id = glCreateShader(gl_type);
96         stage_ids[type] = stage_id;
97         glAttachShader(id, stage_id);
98
99 #ifdef DEBUG
100         if(!debug_name.empty() && KHR_debug)
101                 set_stage_debug_name(stage_id, type);
102 #endif
103
104         return stage_id;
105 }
106
107 void OpenGLProgram::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values)
108 {
109         SL::Compiler compiler(Device::get_current().get_info().glsl_features);
110         compiler.set_source(mod.get_prepared_source(), "<module>");
111         compiler.specialize(spec_values);
112         compiler.compile(SL::Compiler::PROGRAM);
113 #ifdef DEBUG
114         string diagnostics = compiler.get_diagnostics();
115         if(!diagnostics.empty())
116                 IO::print("Program diagnostics:\n%s\n", diagnostics);
117 #endif
118
119         vector<SL::Stage::Type> stages = compiler.get_stages();
120         if(stages.empty())
121                 throw invalid_argument("OpenGLProgram::add_glsl_stages");
122
123         for(SL::Stage::Type st: stages)
124         {
125                 unsigned stage_id = 0;
126                 switch(st)
127                 {
128                 case SL::Stage::VERTEX: stage_id = add_stage(VERTEX); break;
129                 case SL::Stage::TESS_CONTROL: stage_id = add_stage(TESS_CONTROL); break;
130                 case SL::Stage::TESS_EVAL: stage_id = add_stage(TESS_EVAL); break;
131                 case SL::Stage::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
132                 case SL::Stage::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
133                 case SL::Stage::COMPUTE: stage_id = add_stage(COMPUTE); break;
134                 default: throw invalid_operation("OpenGLProgram::add_glsl_stages");
135                 }
136
137                 string stage_src = compiler.get_stage_glsl(st);
138                 const char *src_ptr = stage_src.data();
139                 int src_len = stage_src.size();
140                 glShaderSource(stage_id, 1, &src_ptr, &src_len);
141
142                 if(st==SL::Stage::VERTEX)
143                 {
144                         for(const auto &kvp: compiler.get_vertex_attributes())
145                                 glBindAttribLocation(id, kvp.second, kvp.first.c_str());
146                 }
147
148                 if(st==SL::Stage::FRAGMENT && EXT_gpu_shader4)
149                 {
150                         for(const auto &kvp: compiler.get_fragment_outputs())
151                                 glBindFragDataLocation(id, kvp.second, kvp.first.c_str());
152                 }
153
154                 compile_glsl_stage(mod, stage_id);
155         }
156
157         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
158         rd.n_clip_distances = compiler.get_n_clip_distances();
159
160         link(mod);
161         query_uniforms();
162         query_attributes();
163         if(is_compute())
164         {
165                 int wg_size[3];
166                 glGetProgramiv(id, GL_COMPUTE_WORK_GROUP_SIZE, wg_size);
167                 rd.compute_wg_size = LinAl::Vector<unsigned, 3>(wg_size[0], wg_size[1], wg_size[2]);
168         }
169
170         const map<string, unsigned> &block_bindings = compiler.get_uniform_block_bindings();
171         if(!block_bindings.empty())
172         {
173                 for(unsigned i=0; i<rd.uniform_blocks.size(); ++i)
174                 {
175                         auto j = block_bindings.find(rd.uniform_blocks[i].name);
176                         if(j!=block_bindings.end())
177                         {
178                                 glUniformBlockBinding(id, i, j->second);
179                                 rd.uniform_blocks[i].bind_point = j->second;
180                         }
181                 }
182         }
183
184         const map<string, unsigned> &tex_bindings = compiler.get_texture_bindings();
185         if(!tex_bindings.empty())
186         {
187                 if(!ARB_separate_shader_objects)
188                         glUseProgram(id);
189                 for(const auto &kvp: tex_bindings)
190                 {
191                         int location = static_cast<const Program *>(this)->get_uniform_location(kvp.first);
192                         if(location>=0)
193                         {
194                                 if(ARB_separate_shader_objects)
195                                         glProgramUniform1i(id, location, kvp.second);
196                                 else
197                                         glUniform1i(location, kvp.second);
198                         }
199                 }
200         }
201 }
202
203 void OpenGLProgram::compile_glsl_stage(const GlslModule &mod, unsigned stage_id)
204 {
205         glCompileShader(stage_id);
206         int status = 0;
207         glGetShaderiv(stage_id, GL_COMPILE_STATUS, &status);
208
209         int info_log_len = 0;
210         glGetShaderiv(stage_id, GL_INFO_LOG_LENGTH, &info_log_len);
211         string info_log(info_log_len+1, 0);
212         glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]);
213         info_log.erase(info_log_len);
214         info_log = mod.get_source_map().translate_errors(info_log);
215
216         if(!status)
217                 throw compile_error(info_log);
218 #ifdef DEBUG
219         if(!info_log.empty())
220                 IO::print("Shader compile info log:\n%s", info_log);
221 #endif
222 }
223
224 void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
225 {
226         static Require _req(ARB_gl_spirv);
227         static Require _req2(ARB_ES2_compatibility);
228
229         unsigned n_stages = 0;
230         unsigned used_stage_ids[MAX_STAGES];
231         for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
232         {
233                 unsigned stage_id = 0;
234                 switch(e.stage)
235                 {
236                 case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break;
237                 case SpirVModule::TESS_CONTROL: stage_id = add_stage(TESS_CONTROL); break;
238                 case SpirVModule::TESS_EVAL: stage_id = add_stage(TESS_EVAL); break;
239                 case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
240                 case SpirVModule::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
241                 case SpirVModule::COMPUTE: stage_id = add_stage(COMPUTE); break;
242                 default: throw invalid_operation("OpenGLProgram::add_spirv_stages");
243                 }
244
245                 used_stage_ids[n_stages++] = stage_id;
246         }
247
248         if(!n_stages)
249                 throw invalid_argument("OpenGLProgram::add_spirv_stages");
250
251         const vector<uint32_t> &code = mod.get_code();
252         glShaderBinary(n_stages, used_stage_ids, GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
253
254         const vector<SpirVModule::Constant> &spec_consts = mod.get_spec_constants();
255         vector<unsigned> spec_id_array;
256         vector<unsigned> spec_value_array;
257         spec_id_array.reserve(spec_consts.size());
258         spec_value_array.reserve(spec_consts.size());
259         for(const SpirVModule::Constant &c: spec_consts)
260         {
261                 auto i = spec_values.find(c.name);
262                 if(i!=spec_values.end())
263                 {
264                         spec_id_array.push_back(c.constant_id);
265                         spec_value_array.push_back(i->second);
266                 }
267         }
268
269         auto j = mod.get_entry_points().begin();
270         for(unsigned i=0; i<MAX_STAGES; ++i)
271                 if(stage_ids[i])
272                         glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
273
274         link(mod);
275 }
276
277 void OpenGLProgram::link(const Module &mod)
278 {
279         glLinkProgram(id);
280         int status = 0;
281         glGetProgramiv(id, GL_LINK_STATUS, &status);
282         linked = status;
283
284         int info_log_len = 0;
285         glGetProgramiv(id, GL_INFO_LOG_LENGTH, &info_log_len);
286         string info_log(info_log_len+1, 0);
287         glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]);
288         info_log.erase(info_log_len);
289         if(mod.get_format()==Module::GLSL)
290                 info_log = static_cast<const GlslModule &>(mod).get_source_map().translate_errors(info_log);
291
292         if(!linked)
293                 throw compile_error(info_log);
294 #ifdef DEBUG
295         if(!info_log.empty())
296                 IO::print("Program link info log:\n%s", info_log);
297 #endif
298 }
299
300 void OpenGLProgram::query_uniforms()
301 {
302         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
303
304         unsigned count = 0;
305         glGetProgramiv(id, GL_ACTIVE_UNIFORMS, reinterpret_cast<int *>(&count));
306         rd.uniforms.reserve(count);
307         vector<string> uniform_names(count);
308         for(unsigned i=0; i<count; ++i)
309         {
310                 char name[128];
311                 int len = 0;
312                 int size;
313                 GLenum type;
314                 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
315                 if(len && strncmp(name, "gl_", 3))
316                 {
317                         /* Some implementations report the first element of a uniform array,
318                         others report just the name of the array itself. */
319                         if(len>3 && !strcmp(name+len-3, "[0]"))
320                                 name[len-3] = 0;
321
322                         rd.uniforms.emplace_back();
323                         ReflectData::UniformInfo &info = rd.uniforms.back();
324                         info.name = name;
325                         info.tag = name;
326                         info.array_size = size;
327                         info.type = from_gl_type(type);
328                         uniform_names[i] = name;
329                 }
330         }
331
332         sort_member(rd.uniforms, &ReflectData::UniformInfo::tag);
333
334         if(ARB_uniform_buffer_object)
335         {
336                 vector<ReflectData::UniformInfo *> uniforms_by_index(count);
337                 for(unsigned i=0; i<count; ++i)
338                         if(!uniform_names[i].empty())
339                                 // The element is already known to be present
340                                 uniforms_by_index[i] = &*lower_bound_member(rd.uniforms, Tag(uniform_names[i]), &ReflectData::UniformInfo::tag);
341                 query_uniform_blocks(uniforms_by_index);
342         }
343
344         rd.uniform_blocks.emplace_back();
345         ReflectData::UniformBlockInfo &default_block = rd.uniform_blocks.back();
346
347         for(ReflectData::UniformInfo &u: rd.uniforms)
348                 if(!u.block)
349                 {
350                         u.location = glGetUniformLocation(id, u.name.c_str());
351                         u.block = &default_block;
352                         u.array_stride = get_type_size(u.type);
353                         if(is_matrix(u.type))
354                                 u.matrix_stride = get_type_size(get_matrix_column_type(u.type));
355                         default_block.uniforms.push_back(&u);
356
357                         if(is_image(u.type) && u.location>=0)
358                                 glGetUniformiv(id, u.location, &u.binding);
359                 }
360
361         default_block.sort_uniforms();
362         default_block.update_layout_hash();
363         rd.update_layout_hash();
364 }
365
366 void OpenGLProgram::query_uniform_blocks(const vector<ReflectData::UniformInfo *> &uniforms_by_index)
367 {
368         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
369
370         unsigned count = 0;
371         glGetProgramiv(id, GL_ACTIVE_UNIFORM_BLOCKS, reinterpret_cast<int *>(&count));
372         // Reserve an extra index for the default block
373         rd.uniform_blocks.reserve(count+1);
374         for(unsigned i=0; i<count; ++i)
375         {
376                 char name[128];
377                 int len;
378                 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
379                 rd.uniform_blocks.emplace_back();
380                 ReflectData::UniformBlockInfo &info = rd.uniform_blocks.back();
381                 info.name = name;
382
383                 int value;
384                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
385                 info.data_size = value;
386
387                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_BINDING, &value);
388                 info.bind_point = value;
389
390                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
391                 vector<int> indices(value);
392                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
393                 for(int j: indices)
394                 {
395                         if(!uniforms_by_index[j])
396                                 throw logic_error("OpenGLProgram::query_uniform_blocks");
397                         info.uniforms.push_back(uniforms_by_index[j]);
398                         uniforms_by_index[j]->block = &info;
399                 }
400
401                 vector<unsigned> query_indices(indices.begin(), indices.end());
402                 vector<int> values(indices.size());
403                 glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_OFFSET, &values[0]);
404                 for(unsigned j=0; j<indices.size(); ++j)
405                         uniforms_by_index[indices[j]]->offset = values[j];
406
407                 query_indices.clear();
408                 for(int j: indices)
409                         if(uniforms_by_index[j]->array_size>1)
410                                 query_indices.push_back(j);
411                 if(!query_indices.empty())
412                 {
413                         glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
414                         for(unsigned j=0; j<query_indices.size(); ++j)
415                                 uniforms_by_index[query_indices[j]]->array_stride = values[j];
416                 }
417
418                 query_indices.clear();
419                 for(int j: indices)
420                 {
421                         DataType t = uniforms_by_index[j]->type;
422                         if(is_matrix(t))
423                                 query_indices.push_back(j);
424                 }
425                 if(!query_indices.empty())
426                 {
427                         glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
428                         for(unsigned j=0; j<query_indices.size(); ++j)
429                                 uniforms_by_index[query_indices[j]]->matrix_stride = values[j];
430                 }
431
432                 info.sort_uniforms();
433                 info.update_layout_hash();
434         }
435 }
436
437 void OpenGLProgram::query_attributes()
438 {
439         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
440
441         unsigned count = 0;
442         glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, reinterpret_cast<int *>(&count));
443         rd.attributes.reserve(count);
444         for(unsigned i=0; i<count; ++i)
445         {
446                 char name[128];
447                 int len = 0;
448                 int size;
449                 GLenum type;
450                 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
451                 if(len && strncmp(name, "gl_", 3))
452                 {
453                         if(len>3 && !strcmp(name+len-3, "[0]"))
454                                 name[len-3] = 0;
455
456                         rd.attributes.emplace_back();
457                         ReflectData::AttributeInfo &info = rd.attributes.back();
458                         info.name = name;
459                         info.location = glGetAttribLocation(id, name);
460                         info.array_size = size;
461                         info.type = from_gl_type(type);
462                 }
463         }
464
465         sort_member(rd.attributes, &ReflectData::AttributeInfo::name);
466 }
467
468 void OpenGLProgram::finalize_uniforms()
469 {
470         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
471
472         auto i = find_if(rd.uniform_blocks, [](const ReflectData::UniformBlockInfo &b){ return b.bind_point<0; });
473         if(i!=rd.uniform_blocks.end() && !i->uniforms.empty())
474         {
475                 for(const ReflectData::UniformInfo *u: i->uniforms)
476                         if(u->location>=0)
477                         {
478                                 UniformCall::FuncPtr func = 0;
479                                 if(u->type==FLOAT)
480                                         func = &uniform_wrapper<float, glUniform1fv>;
481                                 else if(u->type==FLOAT_VEC2)
482                                         func = &uniform_wrapper<float, glUniform2fv>;
483                                 else if(u->type==FLOAT_VEC3)
484                                         func = &uniform_wrapper<float, glUniform3fv>;
485                                 else if(u->type==FLOAT_VEC4)
486                                         func = &uniform_wrapper<float, glUniform4fv>;
487                                 else if(u->type==INT)
488                                         func = &uniform_wrapper<int, glUniform1iv>;
489                                 else if(u->type==INT_VEC2)
490                                         func = &uniform_wrapper<int, glUniform2iv>;
491                                 else if(u->type==INT_VEC3)
492                                         func = &uniform_wrapper<int, glUniform3iv>;
493                                 else if(u->type==INT_VEC4)
494                                         func = &uniform_wrapper<int, glUniform4iv>;
495                                 else if(u->type==UNSIGNED_INT)
496                                         func = &uniform_wrapper<unsigned, glUniform1uiv>;
497                                 else if(u->type==UINT_VEC2)
498                                         func = &uniform_wrapper<unsigned, glUniform2uiv>;
499                                 else if(u->type==UINT_VEC3)
500                                         func = &uniform_wrapper<unsigned, glUniform3uiv>;
501                                 else if(u->type==UINT_VEC4)
502                                         func = &uniform_wrapper<unsigned, glUniform4uiv>;
503                                 else if(u->type==FLOAT_MAT2)
504                                         func = &uniform_matrix_wrapper<float, glUniformMatrix2fv>;
505                                 else if(u->type==FLOAT_MAT3)
506                                         func = &uniform_matrix_wrapper<float, glUniformMatrix3fv>;
507                                 else if(u->type==FLOAT_MAT4)
508                                         func = &uniform_matrix_wrapper<float, glUniformMatrix4fv>;
509                                 else if(u->type==FLOAT_MAT2x3)
510                                         func = &uniform_matrix_wrapper<float, glUniformMatrix2x3fv>;
511                                 else if(u->type==FLOAT_MAT3x2)
512                                         func = &uniform_matrix_wrapper<float, glUniformMatrix3x2fv>;
513                                 else if(u->type==FLOAT_MAT2x4)
514                                         func = &uniform_matrix_wrapper<float, glUniformMatrix2x4fv>;
515                                 else if(u->type==FLOAT_MAT4x2)
516                                         func = &uniform_matrix_wrapper<float, glUniformMatrix4x2fv>;
517                                 else if(u->type==FLOAT_MAT3x4)
518                                         func = &uniform_matrix_wrapper<float, glUniformMatrix3x4fv>;
519                                 else if(u->type==FLOAT_MAT4x3)
520                                         func = &uniform_matrix_wrapper<float, glUniformMatrix4x3fv>;
521
522                                 if(func)
523                                         uniform_calls.push_back(UniformCall(u->location, u->array_size, func));
524                         }
525
526                 if(i->data_size<=0)
527                 {
528                         const ReflectData::UniformInfo &last = *i->uniforms.back();
529                         i->data_size = last.location*16+last.array_size*get_type_size(last.type);
530                 }
531         }
532 }
533
534 void OpenGLProgram::set_debug_name(const string &name)
535 {
536 #ifdef DEBUG
537         debug_name = name;
538         if(KHR_debug)
539         {
540                 glObjectLabel(GL_PROGRAM, id, name.size(), name.c_str());
541                 for(unsigned i=0; i<MAX_STAGES; ++i)
542                         if(stage_ids[i])
543                                 set_stage_debug_name(stage_ids[i], static_cast<Stage>(i));
544         }
545 #else
546         (void)name;
547 #endif
548 }
549
550 void OpenGLProgram::set_stage_debug_name(unsigned stage_id, Stage type)
551 {
552 #ifdef DEBUG
553         static const char *const suffixes[] = { " [VS]", " [TCS]", " [TES]", " [GS]", " [FS]", " [CS]" };
554         string name = debug_name+suffixes[type];
555         glObjectLabel(GL_SHADER, stage_id, name.size(), name.c_str());
556 #else
557         (void)stage_id; (void)type;
558 #endif
559 }
560
561 } // namespace GL
562 } // namespace Msp