]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/program_backend.cpp
Rearrange shader reflection calls
[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("OpenGLProgram::add_stage");
73         }
74
75         if(stage_ids[type])
76                 throw invalid_operation("OpenGLProgram::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("OpenGLProgram::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("OpenGLProgram::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         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
141         rd.n_clip_distances = compiler.get_n_clip_distances();
142 }
143
144 void OpenGLProgram::compile_glsl_stage(const GlslModule &mod, unsigned stage_id)
145 {
146         glCompileShader(stage_id);
147         int status = 0;
148         glGetShaderiv(stage_id, GL_COMPILE_STATUS, &status);
149
150         int info_log_len = 0;
151         glGetShaderiv(stage_id, GL_INFO_LOG_LENGTH, &info_log_len);
152         string info_log(info_log_len+1, 0);
153         glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]);
154         info_log.erase(info_log_len);
155         info_log = mod.get_source_map().translate_errors(info_log);
156
157         if(!status)
158                 throw compile_error(info_log);
159 #ifdef DEBUG
160         if(!info_log.empty())
161                 IO::print("Shader compile info log:\n%s", info_log);
162 #endif
163 }
164
165 void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values, TransientData &transient)
166 {
167         static Require _req(ARB_gl_spirv);
168         static Require _req2(ARB_ES2_compatibility);
169
170         unsigned n_stages = 0;
171         unsigned used_stage_ids[MAX_STAGES];
172         for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
173         {
174                 unsigned stage_id = 0;
175                 switch(e.stage)
176                 {
177                 case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break;
178                 case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
179                 case SpirVModule::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
180                 default: throw invalid_operation("OpenGLProgram::add_spirv_stages");
181                 }
182
183                 used_stage_ids[n_stages++] = stage_id;
184         }
185
186         if(!n_stages)
187                 throw invalid_argument("OpenGLProgram::add_spirv_stages");
188
189         const vector<uint32_t> &code = mod.get_code();
190         glShaderBinary(n_stages, used_stage_ids, GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
191
192         const vector<SpirVModule::Constant> &spec_consts = mod.get_spec_constants();
193         vector<unsigned> spec_id_array;
194         vector<unsigned> spec_value_array;
195         spec_id_array.reserve(spec_consts.size());
196         spec_value_array.reserve(spec_consts.size());
197         for(const SpirVModule::Constant &c: spec_consts)
198         {
199                 auto i = spec_values.find(c.name);
200                 if(i!=spec_values.end())
201                 {
202                         spec_id_array.push_back(c.constant_id);
203                         spec_value_array.push_back(i->second);
204                         transient.spec_values[c.constant_id] = i->second;
205                 }
206         }
207
208         auto j = mod.get_entry_points().begin();
209         for(unsigned i=0; i<MAX_STAGES; ++i)
210                 if(stage_ids[i])
211                         glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
212 }
213
214 void OpenGLProgram::finalize(const Module &mod, TransientData &transient)
215 {
216         glLinkProgram(id);
217         int status = 0;
218         glGetProgramiv(id, GL_LINK_STATUS, &status);
219         linked = status;
220
221         int info_log_len = 0;
222         glGetProgramiv(id, GL_INFO_LOG_LENGTH, &info_log_len);
223         string info_log(info_log_len+1, 0);
224         glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]);
225         info_log.erase(info_log_len);
226         if(mod.get_format()==Module::GLSL)
227                 info_log = static_cast<const GlslModule &>(mod).get_source_map().translate_errors(info_log);
228
229         if(!linked)
230                 throw compile_error(info_log);
231 #ifdef DEBUG
232         if(!info_log.empty())
233                 IO::print("Program link info log:\n%s", info_log);
234 #endif
235
236         if(mod.get_format()==Module::GLSL)
237         {
238                 query_uniforms();
239                 query_attributes();
240                 apply_bindings(transient);
241         }
242 }
243
244 void OpenGLProgram::query_uniforms()
245 {
246         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
247
248         unsigned count = 0;
249         glGetProgramiv(id, GL_ACTIVE_UNIFORMS, reinterpret_cast<int *>(&count));
250         rd.uniforms.reserve(count);
251         vector<string> uniform_names(count);
252         for(unsigned i=0; i<count; ++i)
253         {
254                 char name[128];
255                 int len = 0;
256                 int size;
257                 GLenum type;
258                 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
259                 if(len && strncmp(name, "gl_", 3))
260                 {
261                         /* Some implementations report the first element of a uniform array,
262                         others report just the name of the array itself. */
263                         if(len>3 && !strcmp(name+len-3, "[0]"))
264                                 name[len-3] = 0;
265
266                         rd.uniforms.push_back(ReflectData::UniformInfo());
267                         ReflectData::UniformInfo &info = rd.uniforms.back();
268                         info.name = name;
269                         info.tag = name;
270                         info.array_size = size;
271                         info.type = from_gl_type(type);
272                         uniform_names[i] = name;
273                 }
274         }
275
276         sort_member(rd.uniforms, &ReflectData::UniformInfo::tag);
277
278         if(ARB_uniform_buffer_object)
279         {
280                 vector<ReflectData::UniformInfo *> uniforms_by_index(count);
281                 for(unsigned i=0; i<count; ++i)
282                         if(!uniform_names[i].empty())
283                                 // The element is already known to be present
284                                 uniforms_by_index[i] = &*lower_bound_member(rd.uniforms, Tag(uniform_names[i]), &ReflectData::UniformInfo::tag);
285                 query_uniform_blocks(uniforms_by_index);
286         }
287
288         rd.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
289         ReflectData::UniformBlockInfo &default_block = rd.uniform_blocks.back();
290
291         for(ReflectData::UniformInfo &u: rd.uniforms)
292                 if(!u.block)
293                 {
294                         u.location = glGetUniformLocation(id, u.name.c_str());
295                         u.block = &default_block;
296                         u.array_stride = get_type_size(u.type);
297                         if(is_matrix(u.type))
298                                 u.matrix_stride = get_type_size(get_matrix_column_type(u.type));
299                         default_block.uniforms.push_back(&u);
300
301                         if(u.location>=0)
302                         {
303                                 UniformCall::FuncPtr func = 0;
304                                 if(is_image(u.type))
305                                         glGetUniformiv(id, u.location, &u.binding);
306                                 else if(u.type==FLOAT)
307                                         func = &uniform_wrapper<float, glUniform1fv>;
308                                 else if(u.type==FLOAT_VEC2)
309                                         func = &uniform_wrapper<float, glUniform2fv>;
310                                 else if(u.type==FLOAT_VEC3)
311                                         func = &uniform_wrapper<float, glUniform3fv>;
312                                 else if(u.type==FLOAT_VEC4)
313                                         func = &uniform_wrapper<float, glUniform4fv>;
314                                 else if(u.type==INT)
315                                         func = &uniform_wrapper<int, glUniform1iv>;
316                                 else if(u.type==INT_VEC2)
317                                         func = &uniform_wrapper<int, glUniform2iv>;
318                                 else if(u.type==INT_VEC3)
319                                         func = &uniform_wrapper<int, glUniform3iv>;
320                                 else if(u.type==INT_VEC4)
321                                         func = &uniform_wrapper<int, glUniform4iv>;
322                                 else if(u.type==UNSIGNED_INT)
323                                         func = &uniform_wrapper<unsigned, glUniform1uiv>;
324                                 else if(u.type==UINT_VEC2)
325                                         func = &uniform_wrapper<unsigned, glUniform2uiv>;
326                                 else if(u.type==UINT_VEC3)
327                                         func = &uniform_wrapper<unsigned, glUniform3uiv>;
328                                 else if(u.type==UINT_VEC4)
329                                         func = &uniform_wrapper<unsigned, glUniform4uiv>;
330                                 else if(u.type==FLOAT_MAT2)
331                                         func = &uniform_matrix_wrapper<float, glUniformMatrix2fv>;
332                                 else if(u.type==FLOAT_MAT3)
333                                         func = &uniform_matrix_wrapper<float, glUniformMatrix3fv>;
334                                 else if(u.type==FLOAT_MAT4)
335                                         func = &uniform_matrix_wrapper<float, glUniformMatrix4fv>;
336                                 else if(u.type==FLOAT_MAT2x3)
337                                         func = &uniform_matrix_wrapper<float, glUniformMatrix2x3fv>;
338                                 else if(u.type==FLOAT_MAT3x2)
339                                         func = &uniform_matrix_wrapper<float, glUniformMatrix3x2fv>;
340                                 else if(u.type==FLOAT_MAT2x4)
341                                         func = &uniform_matrix_wrapper<float, glUniformMatrix2x4fv>;
342                                 else if(u.type==FLOAT_MAT4x2)
343                                         func = &uniform_matrix_wrapper<float, glUniformMatrix4x2fv>;
344                                 else if(u.type==FLOAT_MAT3x4)
345                                         func = &uniform_matrix_wrapper<float, glUniformMatrix3x4fv>;
346                                 else if(u.type==FLOAT_MAT4x3)
347                                         func = &uniform_matrix_wrapper<float, glUniformMatrix4x3fv>;
348
349                                 if(func)
350                                         uniform_calls.push_back(UniformCall(u.location, u.array_size, func));
351                         }
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.push_back(ReflectData::UniformBlockInfo());
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.push_back(ReflectData::AttributeInfo());
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
459 void OpenGLProgram::apply_bindings(const TransientData &transient)
460 {
461         ReflectData &rd = static_cast<Program *>(this)->reflect_data;
462
463         for(unsigned i=0; i<rd.uniform_blocks.size(); ++i)
464         {
465                 auto j = transient.blocks.find(rd.uniform_blocks[i].name);
466                 if(j!=transient.blocks.end())
467                 {
468                         glUniformBlockBinding(id, i, j->second);
469                         rd.uniform_blocks[i].bind_point = j->second;
470                 }
471         }
472
473         if(!ARB_separate_shader_objects)
474                 glUseProgram(id);
475         for(const auto &kvp: transient.textures)
476         {
477                 int location = static_cast<const Program *>(this)->get_uniform_location(kvp.first);
478                 if(location>=0)
479                 {
480                         if(ARB_separate_shader_objects)
481                                 glProgramUniform1i(id, location, kvp.second);
482                         else
483                                 glUniform1i(location, kvp.second);
484                 }
485         }
486 }
487
488 void OpenGLProgram::set_debug_name(const string &name)
489 {
490 #ifdef DEBUG
491         debug_name = name;
492         if(KHR_debug)
493         {
494                 glObjectLabel(GL_PROGRAM, id, name.size(), name.c_str());
495                 for(unsigned i=0; i<MAX_STAGES; ++i)
496                         if(stage_ids[i])
497                                 set_stage_debug_name(stage_ids[i], static_cast<Stage>(i));
498         }
499 #else
500         (void)name;
501 #endif
502 }
503
504 void OpenGLProgram::set_stage_debug_name(unsigned stage_id, Stage type)
505 {
506 #ifdef DEBUG
507         static const char *const suffixes[] = { " [VS]", " [GS]", " [FS]" };
508         string name = debug_name+suffixes[type];
509         glObjectLabel(GL_SHADER, stage_id, name.size(), name.c_str());
510 #else
511         (void)stage_id; (void)type;
512 #endif
513 }
514
515 } // namespace GL
516 } // namespace Msp