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