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