]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/program_backend.cpp
a9328488cedc9bc12b5399bf55d2b032771dd52a
[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, TransientData &transient)
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         transient.textures = compiler.get_texture_bindings();
150         transient.blocks = compiler.get_uniform_block_bindings();
151
152         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
153         rd.n_clip_distances = compiler.get_n_clip_distances();
154 }
155
156 void OpenGLProgram::compile_glsl_stage(const GlslModule &mod, unsigned stage_id)
157 {
158         glCompileShader(stage_id);
159         int status = 0;
160         glGetShaderiv(stage_id, GL_COMPILE_STATUS, &status);
161
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);
168
169         if(!status)
170                 throw compile_error(info_log);
171 #ifdef DEBUG
172         if(!info_log.empty())
173                 IO::print("Shader compile info log:\n%s", info_log);
174 #endif
175 }
176
177 void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
178 {
179         static Require _req(ARB_gl_spirv);
180         static Require _req2(ARB_ES2_compatibility);
181
182         unsigned n_stages = 0;
183         unsigned used_stage_ids[MAX_STAGES];
184         for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
185         {
186                 unsigned stage_id = 0;
187                 switch(e.stage)
188                 {
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");
193                 }
194
195                 used_stage_ids[n_stages++] = stage_id;
196         }
197
198         if(!n_stages)
199                 throw invalid_argument("OpenGLProgram::add_spirv_stages");
200
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);
203
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)
210         {
211                 auto i = spec_values.find(c.name);
212                 if(i!=spec_values.end())
213                 {
214                         spec_id_array.push_back(c.constant_id);
215                         spec_value_array.push_back(i->second);
216                 }
217         }
218
219         auto j = mod.get_entry_points().begin();
220         for(unsigned i=0; i<MAX_STAGES; ++i)
221                 if(stage_ids[i])
222                         glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
223 }
224
225 void OpenGLProgram::finalize(const Module &mod, TransientData &transient)
226 {
227         glLinkProgram(id);
228         int status = 0;
229         glGetProgramiv(id, GL_LINK_STATUS, &status);
230         linked = status;
231
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);
239
240         if(!linked)
241                 throw compile_error(info_log);
242 #ifdef DEBUG
243         if(!info_log.empty())
244                 IO::print("Program link info log:\n%s", info_log);
245 #endif
246
247         if(mod.get_format()==Module::GLSL)
248         {
249                 query_uniforms();
250                 query_attributes();
251                 apply_bindings(transient);
252         }
253 }
254
255 void OpenGLProgram::query_uniforms()
256 {
257         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
258
259         unsigned count = 0;
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)
264         {
265                 char name[128];
266                 int len = 0;
267                 int size;
268                 GLenum type;
269                 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
270                 if(len && strncmp(name, "gl_", 3))
271                 {
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]"))
275                                 name[len-3] = 0;
276
277                         rd.uniforms.emplace_back();
278                         ReflectData::UniformInfo &info = rd.uniforms.back();
279                         info.name = name;
280                         info.tag = name;
281                         info.array_size = size;
282                         info.type = from_gl_type(type);
283                         uniform_names[i] = name;
284                 }
285         }
286
287         sort_member(rd.uniforms, &ReflectData::UniformInfo::tag);
288
289         if(ARB_uniform_buffer_object)
290         {
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);
297         }
298
299         rd.uniform_blocks.emplace_back();
300         ReflectData::UniformBlockInfo &default_block = rd.uniform_blocks.back();
301
302         for(ReflectData::UniformInfo &u: rd.uniforms)
303                 if(!u.block)
304                 {
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);
311
312                         if(is_image(u.type) && u.location>=0)
313                                 glGetUniformiv(id, u.location, &u.binding);
314                 }
315
316         default_block.sort_uniforms();
317         default_block.update_layout_hash();
318         rd.update_layout_hash();
319 }
320
321 void OpenGLProgram::query_uniform_blocks(const vector<ReflectData::UniformInfo *> &uniforms_by_index)
322 {
323         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
324
325         unsigned count = 0;
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)
330         {
331                 char name[128];
332                 int len;
333                 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
334                 rd.uniform_blocks.emplace_back();
335                 ReflectData::UniformBlockInfo &info = rd.uniform_blocks.back();
336                 info.name = name;
337
338                 int value;
339                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
340                 info.data_size = value;
341
342                 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_BINDING, &value);
343                 info.bind_point = value;
344
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]);
348                 for(int j: indices)
349                 {
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;
354                 }
355
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];
361
362                 query_indices.clear();
363                 for(int j: indices)
364                         if(uniforms_by_index[j]->array_size>1)
365                                 query_indices.push_back(j);
366                 if(!query_indices.empty())
367                 {
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];
371                 }
372
373                 query_indices.clear();
374                 for(int j: indices)
375                 {
376                         DataType t = uniforms_by_index[j]->type;
377                         if(is_matrix(t))
378                                 query_indices.push_back(j);
379                 }
380                 if(!query_indices.empty())
381                 {
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];
385                 }
386
387                 info.sort_uniforms();
388                 info.update_layout_hash();
389         }
390 }
391
392 void OpenGLProgram::query_attributes()
393 {
394         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
395
396         unsigned count = 0;
397         glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, reinterpret_cast<int *>(&count));
398         rd.attributes.reserve(count);
399         for(unsigned i=0; i<count; ++i)
400         {
401                 char name[128];
402                 int len = 0;
403                 int size;
404                 GLenum type;
405                 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
406                 if(len && strncmp(name, "gl_", 3))
407                 {
408                         if(len>3 && !strcmp(name+len-3, "[0]"))
409                                 name[len-3] = 0;
410
411                         rd.attributes.emplace_back();
412                         ReflectData::AttributeInfo &info = rd.attributes.back();
413                         info.name = name;
414                         info.location = glGetAttribLocation(id, name);
415                         info.array_size = size;
416                         info.type = from_gl_type(type);
417                 }
418         }
419 }
420
421 void OpenGLProgram::apply_bindings(const TransientData &transient)
422 {
423         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
424
425         for(unsigned i=0; i<rd.uniform_blocks.size(); ++i)
426         {
427                 auto j = transient.blocks.find(rd.uniform_blocks[i].name);
428                 if(j!=transient.blocks.end())
429                 {
430                         glUniformBlockBinding(id, i, j->second);
431                         rd.uniform_blocks[i].bind_point = j->second;
432                 }
433         }
434
435         if(!ARB_separate_shader_objects)
436                 glUseProgram(id);
437         for(const auto &kvp: transient.textures)
438         {
439                 int location = static_cast<const Program *>(this)->get_uniform_location(kvp.first);
440                 if(location>=0)
441                 {
442                         if(ARB_separate_shader_objects)
443                                 glProgramUniform1i(id, location, kvp.second);
444                         else
445                                 glUniform1i(location, kvp.second);
446                 }
447         }
448 }
449
450 void OpenGLProgram::finalize_uniforms()
451 {
452         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
453
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())
456         {
457                 for(const ReflectData::UniformInfo *u: i->uniforms)
458                         if(u->location>=0)
459                         {
460                                 UniformCall::FuncPtr func = 0;
461                                 if(u->type==FLOAT)
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>;
503
504                                 if(func)
505                                         uniform_calls.push_back(UniformCall(u->location, u->array_size, func));
506                         }
507
508                 if(i->data_size<=0)
509                 {
510                         const ReflectData::UniformInfo &last = *i->uniforms.back();
511                         i->data_size = last.location*16+last.array_size*get_type_size(last.type);
512                 }
513         }
514 }
515
516 void OpenGLProgram::set_debug_name(const string &name)
517 {
518 #ifdef DEBUG
519         debug_name = name;
520         if(KHR_debug)
521         {
522                 glObjectLabel(GL_PROGRAM, id, name.size(), name.c_str());
523                 for(unsigned i=0; i<MAX_STAGES; ++i)
524                         if(stage_ids[i])
525                                 set_stage_debug_name(stage_ids[i], static_cast<Stage>(i));
526         }
527 #else
528         (void)name;
529 #endif
530 }
531
532 void OpenGLProgram::set_stage_debug_name(unsigned stage_id, Stage type)
533 {
534 #ifdef DEBUG
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());
538 #else
539         (void)stage_id; (void)type;
540 #endif
541 }
542
543 } // namespace GL
544 } // namespace Msp