]> 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_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>
15 #include "device.h"
16 #include "error.h"
17 #include "program.h"
18 #include "program_backend.h"
19 #include "glsl/compiler.h"
20
21 using namespace std;
22
23 namespace {
24
25 template<typename T, void (*&func)(GLint, GLsizei, const T *)>
26 void uniform_wrapper(unsigned index, unsigned count, const void *data)
27 {
28         func(index, count, static_cast<const T *>(data));
29 }
30
31 template<typename T, void (*&func)(GLint, GLsizei, GLboolean, const T *)>
32 void uniform_matrix_wrapper(unsigned index, unsigned count, const void *data)
33 {
34         func(index, count, false, static_cast<const T *>(data));
35 }
36
37 }
38
39 namespace Msp {
40 namespace GL {
41
42 OpenGLProgram::OpenGLProgram()
43 {
44         static Require _req(ARB_shader_objects);
45
46         id = glCreateProgram();
47 }
48
49 OpenGLProgram::OpenGLProgram(OpenGLProgram &&other):
50         id(other.id),
51         linked(other.linked),
52         uniform_calls(move(other.uniform_calls)),
53         debug_name(move(other.debug_name))
54 {
55         move(other.stage_ids, other.stage_ids+MAX_STAGES, stage_ids);
56         other.id = 0;
57         fill(other.stage_ids, other.stage_ids+MAX_STAGES, 0);
58 }
59
60 OpenGLProgram::~OpenGLProgram()
61 {
62         for(unsigned i=0; i<MAX_STAGES; ++i)
63                 if(stage_ids[i])
64                         glDeleteShader(stage_ids[i]);
65         glDeleteProgram(id);
66 }
67
68 bool OpenGLProgram::has_stages() const
69 {
70         for(unsigned i=0; i<MAX_STAGES; ++i)
71                 if(stage_ids[i])
72                         return true;
73         return false;
74 }
75
76 unsigned OpenGLProgram::add_stage(Stage type)
77 {
78         GLenum gl_type;
79         switch(type)
80         {
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");
85         }
86
87         if(stage_ids[type])
88                 throw invalid_operation("OpenGLProgram::add_stage");
89
90         unsigned stage_id = glCreateShader(gl_type);
91         stage_ids[type] = stage_id;
92         glAttachShader(id, stage_id);
93
94 #ifdef DEBUG
95         if(!debug_name.empty() && KHR_debug)
96                 set_stage_debug_name(stage_id, type);
97 #endif
98
99         return stage_id;
100 }
101
102 void OpenGLProgram::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values)
103 {
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);
108 #ifdef DEBUG
109         string diagnostics = compiler.get_diagnostics();
110         if(!diagnostics.empty())
111                 IO::print("Program diagnostics:\n%s\n", diagnostics);
112 #endif
113
114         vector<SL::Stage::Type> stages = compiler.get_stages();
115         if(stages.empty())
116                 throw invalid_argument("OpenGLProgram::add_glsl_stages");
117
118         for(SL::Stage::Type st: stages)
119         {
120                 unsigned stage_id = 0;
121                 switch(st)
122                 {
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");
127                 }
128
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);
133
134                 if(st==SL::Stage::VERTEX)
135                 {
136                         for(const auto &kvp: compiler.get_vertex_attributes())
137                                 glBindAttribLocation(id, kvp.second, kvp.first.c_str());
138                 }
139
140                 if(st==SL::Stage::FRAGMENT && EXT_gpu_shader4)
141                 {
142                         for(const auto &kvp: compiler.get_fragment_outputs())
143                                 glBindFragDataLocation(id, kvp.second, kvp.first.c_str());
144                 }
145
146                 compile_glsl_stage(mod, stage_id);
147         }
148
149         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
150         rd.n_clip_distances = compiler.get_n_clip_distances();
151
152         link(mod);
153         query_uniforms();
154         query_attributes();
155
156         const map<string, unsigned> &block_bindings = compiler.get_uniform_block_bindings();
157         if(!block_bindings.empty())
158         {
159                 for(unsigned i=0; i<rd.uniform_blocks.size(); ++i)
160                 {
161                         auto j = block_bindings.find(rd.uniform_blocks[i].name);
162                         if(j!=block_bindings.end())
163                         {
164                                 glUniformBlockBinding(id, i, j->second);
165                                 rd.uniform_blocks[i].bind_point = j->second;
166                         }
167                 }
168         }
169
170         const map<string, unsigned> &tex_bindings = compiler.get_texture_bindings();
171         if(!tex_bindings.empty())
172         {
173                 if(!ARB_separate_shader_objects)
174                         glUseProgram(id);
175                 for(const auto &kvp: tex_bindings)
176                 {
177                         int location = static_cast<const Program *>(this)->get_uniform_location(kvp.first);
178                         if(location>=0)
179                         {
180                                 if(ARB_separate_shader_objects)
181                                         glProgramUniform1i(id, location, kvp.second);
182                                 else
183                                         glUniform1i(location, kvp.second);
184                         }
185                 }
186         }
187 }
188
189 void OpenGLProgram::compile_glsl_stage(const GlslModule &mod, unsigned stage_id)
190 {
191         glCompileShader(stage_id);
192         int status = 0;
193         glGetShaderiv(stage_id, GL_COMPILE_STATUS, &status);
194
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);
201
202         if(!status)
203                 throw compile_error(info_log);
204 #ifdef DEBUG
205         if(!info_log.empty())
206                 IO::print("Shader compile info log:\n%s", info_log);
207 #endif
208 }
209
210 void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
211 {
212         static Require _req(ARB_gl_spirv);
213         static Require _req2(ARB_ES2_compatibility);
214
215         unsigned n_stages = 0;
216         unsigned used_stage_ids[MAX_STAGES];
217         for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
218         {
219                 unsigned stage_id = 0;
220                 switch(e.stage)
221                 {
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");
226                 }
227
228                 used_stage_ids[n_stages++] = stage_id;
229         }
230
231         if(!n_stages)
232                 throw invalid_argument("OpenGLProgram::add_spirv_stages");
233
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);
236
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)
243         {
244                 auto i = spec_values.find(c.name);
245                 if(i!=spec_values.end())
246                 {
247                         spec_id_array.push_back(c.constant_id);
248                         spec_value_array.push_back(i->second);
249                 }
250         }
251
252         auto j = mod.get_entry_points().begin();
253         for(unsigned i=0; i<MAX_STAGES; ++i)
254                 if(stage_ids[i])
255                         glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
256
257         link(mod);
258 }
259
260 void OpenGLProgram::link(const Module &mod)
261 {
262         glLinkProgram(id);
263         int status = 0;
264         glGetProgramiv(id, GL_LINK_STATUS, &status);
265         linked = status;
266
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);
274
275         if(!linked)
276                 throw compile_error(info_log);
277 #ifdef DEBUG
278         if(!info_log.empty())
279                 IO::print("Program link info log:\n%s", info_log);
280 #endif
281 }
282
283 void OpenGLProgram::query_uniforms()
284 {
285         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
286
287         unsigned count = 0;
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)
292         {
293                 char name[128];
294                 int len = 0;
295                 int size;
296                 GLenum type;
297                 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
298                 if(len && strncmp(name, "gl_", 3))
299                 {
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]"))
303                                 name[len-3] = 0;
304
305                         rd.uniforms.emplace_back();
306                         ReflectData::UniformInfo &info = rd.uniforms.back();
307                         info.name = name;
308                         info.tag = name;
309                         info.array_size = size;
310                         info.type = from_gl_type(type);
311                         uniform_names[i] = name;
312                 }
313         }
314
315         sort_member(rd.uniforms, &ReflectData::UniformInfo::tag);
316
317         if(ARB_uniform_buffer_object)
318         {
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);
325         }
326
327         rd.uniform_blocks.emplace_back();
328         ReflectData::UniformBlockInfo &default_block = rd.uniform_blocks.back();
329
330         for(ReflectData::UniformInfo &u: rd.uniforms)
331                 if(!u.block)
332                 {
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);
339
340                         if(is_image(u.type) && u.location>=0)
341                                 glGetUniformiv(id, u.location, &u.binding);
342                 }
343
344         default_block.sort_uniforms();
345         default_block.update_layout_hash();
346         rd.update_layout_hash();
347 }
348
349 void OpenGLProgram::query_uniform_blocks(const vector<ReflectData::UniformInfo *> &uniforms_by_index)
350 {
351         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
352
353         unsigned count = 0;
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)
358         {
359                 char name[128];
360                 int len;
361                 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
362                 rd.uniform_blocks.emplace_back();
363                 ReflectData::UniformBlockInfo &info = rd.uniform_blocks.back();
364                 info.name = name;
365
366                 int value;
367                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
368                 info.data_size = value;
369
370                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_BINDING, &value);
371                 info.bind_point = value;
372
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]);
376                 for(int j: indices)
377                 {
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;
382                 }
383
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];
389
390                 query_indices.clear();
391                 for(int j: indices)
392                         if(uniforms_by_index[j]->array_size>1)
393                                 query_indices.push_back(j);
394                 if(!query_indices.empty())
395                 {
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];
399                 }
400
401                 query_indices.clear();
402                 for(int j: indices)
403                 {
404                         DataType t = uniforms_by_index[j]->type;
405                         if(is_matrix(t))
406                                 query_indices.push_back(j);
407                 }
408                 if(!query_indices.empty())
409                 {
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];
413                 }
414
415                 info.sort_uniforms();
416                 info.update_layout_hash();
417         }
418 }
419
420 void OpenGLProgram::query_attributes()
421 {
422         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
423
424         unsigned count = 0;
425         glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, reinterpret_cast<int *>(&count));
426         rd.attributes.reserve(count);
427         for(unsigned i=0; i<count; ++i)
428         {
429                 char name[128];
430                 int len = 0;
431                 int size;
432                 GLenum type;
433                 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
434                 if(len && strncmp(name, "gl_", 3))
435                 {
436                         if(len>3 && !strcmp(name+len-3, "[0]"))
437                                 name[len-3] = 0;
438
439                         rd.attributes.emplace_back();
440                         ReflectData::AttributeInfo &info = rd.attributes.back();
441                         info.name = name;
442                         info.location = glGetAttribLocation(id, name);
443                         info.array_size = size;
444                         info.type = from_gl_type(type);
445                 }
446         }
447
448         sort_member(reflect_data.attributes, &ReflectData::AttributeInfo::name);
449 }
450
451 void OpenGLProgram::finalize_uniforms()
452 {
453         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
454
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())
457         {
458                 for(const ReflectData::UniformInfo *u: i->uniforms)
459                         if(u->location>=0)
460                         {
461                                 UniformCall::FuncPtr func = 0;
462                                 if(u->type==FLOAT)
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>;
504
505                                 if(func)
506                                         uniform_calls.push_back(UniformCall(u->location, u->array_size, func));
507                         }
508
509                 if(i->data_size<=0)
510                 {
511                         const ReflectData::UniformInfo &last = *i->uniforms.back();
512                         i->data_size = last.location*16+last.array_size*get_type_size(last.type);
513                 }
514         }
515 }
516
517 void OpenGLProgram::set_debug_name(const string &name)
518 {
519 #ifdef DEBUG
520         debug_name = name;
521         if(KHR_debug)
522         {
523                 glObjectLabel(GL_PROGRAM, id, name.size(), name.c_str());
524                 for(unsigned i=0; i<MAX_STAGES; ++i)
525                         if(stage_ids[i])
526                                 set_stage_debug_name(stage_ids[i], static_cast<Stage>(i));
527         }
528 #else
529         (void)name;
530 #endif
531 }
532
533 void OpenGLProgram::set_stage_debug_name(unsigned stage_id, Stage type)
534 {
535 #ifdef DEBUG
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());
539 #else
540         (void)stage_id; (void)type;
541 #endif
542 }
543
544 } // namespace GL
545 } // namespace Msp