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