]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/finalize.cpp
Use default member initializers for simple types
[libs/gl.git] / source / glsl / finalize.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/core/hash.h>
3 #include <msp/core/raii.h>
4 #include <msp/strings/lexicalcast.h>
5 #include "finalize.h"
6 #include "glsl_error.h"
7 #include "reflect.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13 namespace SL {
14
15 void StructOrganizer::visit(StructDeclaration &strct)
16 {
17         SetForScope<int> set_offset(offset, 0);
18         TraversingVisitor::visit(strct);
19 }
20
21 void StructOrganizer::visit(VariableDeclaration &var)
22 {
23         if(offset>=0)
24         {
25                 int *layout_offset = 0;
26                 bool has_matrix_order = false;
27                 if(var.layout)
28                 {
29                         for(Layout::Qualifier &q: var.layout->qualifiers)
30                         {
31                                 if(q.name=="offset" && q.has_value)
32                                 {
33                                         layout_offset = &q.value;
34                                         if(q.value>=offset)
35                                                 offset = q.value;
36                                 }
37                                 else if(q.name=="column_major" || q.name=="row_major")
38                                         has_matrix_order = true;
39                         }
40                 }
41
42                 MemoryRequirementsCalculator::Result mem_reqs = MemoryRequirementsCalculator().apply(var);
43                 offset += mem_reqs.alignment-1;
44                 offset -= offset%mem_reqs.alignment;
45
46                 if(layout_offset)
47                         *layout_offset = offset;
48                 else
49                 {
50                         if(!var.layout)
51                                 var.layout = new Layout;
52
53                         var.layout->qualifiers.push_back(Layout::Qualifier("offset", offset));
54                 }
55
56                 if(!has_matrix_order)
57                 {
58                         const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var.type_declaration);
59                         while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
60                                 basic = dynamic_cast<const BasicTypeDeclaration *>(basic->base_type);
61                         if(basic && basic->kind==BasicTypeDeclaration::MATRIX)
62                                 var.layout->qualifiers.push_back(Layout::Qualifier("column_major"));
63                 }
64
65                 offset += mem_reqs.size;
66         }
67 }
68
69
70 void LocationAllocator::apply(Module &module, const Features &features)
71 {
72         for(Stage &s: module.stages)
73                 apply(s);
74         allocate_locations("uniform");
75
76         for(InterfaceBlock *b: unbound_blocks)
77                 bind_uniform(b->layout, b->block_name, features.uniform_binding_range);
78         for(VariableDeclaration *t: unbound_textures)
79                 bind_uniform(t->layout, t->name, features.texture_binding_range);
80 }
81
82 void LocationAllocator::apply(Stage &stage)
83 {
84         swap(used_locations["in"], used_locations["out"]);
85         used_locations["out"].clear();
86
87         stage.content.visit(*this);
88
89         allocate_locations("in");
90         allocate_locations("out");
91 }
92
93 void LocationAllocator::allocate_locations(const string &iface)
94 {
95         auto write = unplaced_variables.begin();
96         unsigned next = 0;
97         for(auto i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i)
98         {
99                 if((*i)->interface!=iface)
100                 {
101                         if(write!=i)
102                                 *write = *i;
103                         ++write;
104                         continue;
105                 }
106
107                 if((*i)->interface=="uniform")
108                 {
109                         auto j = uniforms.find((*i)->name);
110                         if(j!=uniforms.end() && j->second.location>=0)
111                         {
112                                 add_layout_value((*i)->layout, "location", j->second.location);
113                                 continue;
114                         }
115                 }
116
117                 set<unsigned> &used = used_locations[(*i)->interface];
118
119                 unsigned size = LocationCounter().apply(**i);
120                 while(1)
121                 {
122                         int blocking = -1;
123                         for(unsigned j=0; j<size; ++j)
124                                 if(used.count(next+j))
125                                         blocking = next+j;
126                         if(blocking<0)
127                                 break;
128                         next = blocking+1;
129                 }
130
131                 add_layout_value((*i)->layout, "location", next);
132                 if((*i)->interface=="uniform")
133                         uniforms[(*i)->name].location = next;
134
135                 for(unsigned j=0; j<size; ++j)
136                         used.insert(next+j);
137                 next += size;
138         }
139
140         unplaced_variables.erase(write, unplaced_variables.end());
141 }
142
143 void LocationAllocator::bind_uniform(RefPtr<Layout> &layout, const string &name, unsigned range)
144 {
145         auto i = uniforms.find(name);
146         if(i!=uniforms.end() && i->second.bind_point>=0)
147                 add_layout_value(layout, "binding", i->second.bind_point);
148         else
149         {
150                 set<unsigned> &used = used_bindings[0];
151
152                 unsigned bind_point = fold32(hash64(name))%range;
153                 while(used.count(bind_point))
154                         bind_point = (bind_point+1)%range;
155
156                 add_layout_value(layout, "binding", bind_point);
157                 uniforms[name].bind_point = bind_point;
158                 used.insert(bind_point);
159         }
160 }
161
162 void LocationAllocator::add_layout_value(RefPtr<Layout> &layout, const string &name, unsigned value)
163 {
164         if(!layout)
165                 layout = new Layout;
166
167         layout->qualifiers.push_back(Layout::Qualifier(name, value));
168 }
169
170 void LocationAllocator::visit(VariableDeclaration &var)
171 {
172         if(!var.name.compare(0, 3, "gl_"))
173                 return;
174
175         if(!var.interface.empty())
176         {
177                 int location = (var.layout ? get_layout_value(*var.layout, "location") : -1);
178
179                 if(location<0 && var.linked_declaration && var.linked_declaration->layout)
180                 {
181                         location = get_layout_value(*var.linked_declaration->layout, "location");
182                         if(location>=0)
183                                 add_layout_value(var.layout, "location", location);
184                 }
185
186                 if(location>=0)
187                 {
188                         unsigned size = LocationCounter().apply(var);
189                         for(unsigned i=0; i<size; ++i)
190                                 used_locations[var.interface].insert(location+i);
191                         if(var.interface=="uniform")
192                                 uniforms[var.name].location = location;
193                 }
194                 else
195                         unplaced_variables.push_back(&var);
196         }
197
198         if(var.interface=="uniform")
199         {
200                 const TypeDeclaration *type = var.type_declaration;
201                 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
202                         type = basic->base_type;
203                 if(dynamic_cast<const ImageTypeDeclaration *>(type))
204                 {
205                         int bind_point = (var.layout ? get_layout_value(*var.layout, "binding") : -1);
206                         if(bind_point>=0)
207                         {
208                                 used_bindings[0].insert(bind_point);
209                                 uniforms[var.name].bind_point = bind_point;
210                         }
211                         else
212                                 unbound_textures.push_back(&var);
213                 }
214         }
215 }
216
217 void LocationAllocator::visit(InterfaceBlock &iface)
218 {
219         if(!iface.instance_name.compare(0, 3, "gl_"))
220                 return;
221
222         if(iface.interface=="uniform")
223         {
224                 int bind_point = (iface.layout ? get_layout_value(*iface.layout, "binding") : -1);
225
226                 if(bind_point>=0)
227                 {
228                         used_bindings[0].insert(bind_point);
229                         uniforms[iface.block_name].bind_point = bind_point;
230                 }
231                 else
232                         unbound_blocks.push_back(&iface);
233         }
234 }
235
236
237 void PrecisionConverter::apply(Stage &s)
238 {
239         stage = &s;
240         s.content.visit(*this);
241         NodeRemover().apply(s, nodes_to_remove);
242 }
243
244 void PrecisionConverter::visit(Block &block)
245 {
246         for(auto i=block.body.begin(); i!=block.body.end(); ++i)
247         {
248                 if(&block==&stage->content)
249                         insert_point = i;
250                 (*i)->visit(*this);
251         }
252 }
253
254 void PrecisionConverter::visit(Precision &prec)
255 {
256         if(stage->required_features.target_api==OPENGL_ES)
257                 have_default.insert(prec.type);
258         else
259                 nodes_to_remove.insert(&prec);
260 }
261
262 void PrecisionConverter::visit(VariableDeclaration &var)
263 {
264         if(stage->required_features.target_api!=OPENGL_ES)
265         {
266                 var.precision.clear();
267                 return;
268         }
269
270         const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
271         const TypeDeclaration *type = var.type_declaration;
272         while(type)
273         {
274                 if(dynamic_cast<const ImageTypeDeclaration *>(type))
275                 {
276                         default_prec = "lowp";
277                         break;
278                 }
279                 else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
280                 {
281                         if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
282                                 break;
283                         type = basic->base_type;
284                 }
285                 else
286                         return;
287         }
288         if(!type)
289                 return;
290
291         if(!have_default.count(type->name))
292         {
293                 Precision *prec = new Precision;
294                 prec->precision = default_prec;
295                 prec->type = type->name;
296                 stage->content.body.insert(insert_point, prec);
297
298                 have_default.insert(type->name);
299         }
300 }
301
302
303 void LegacyConverter::apply(Stage &s, const Features &feat)
304 {
305         stage = &s;
306         features = feat;
307         if(supports_stage(s.type))
308         {
309                 s.content.visit(*this);
310                 NodeRemover().apply(s, nodes_to_remove);
311
312                 if(!stage->required_features.glsl_version)
313                         stage->required_features.glsl_version = Version(1, (stage->required_features.target_api==OPENGL_ES ? 0 : 10));
314         }
315         else
316                 unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
317 }
318
319 void LegacyConverter::unsupported(const string &reason)
320 {
321         Diagnostic diagnostic;
322         diagnostic.severity = Diagnostic::ERR;
323         diagnostic.source = GENERATED_SOURCE;
324         diagnostic.line = 0;
325         diagnostic.message = reason;
326         stage->diagnostics.push_back(diagnostic);
327 }
328
329 void LegacyConverter::visit(Block &block)
330 {
331         for(auto i=block.body.begin(); i!=block.body.end(); ++i)
332         {
333                 if(&block==&stage->content)
334                         uniform_insert_point = i;
335                 (*i)->visit(*this);
336         }
337 }
338
339 bool LegacyConverter::check_version(const Version &feature_version) const
340 {
341         if(features.glsl_version<feature_version)
342                 return false;
343         else if(stage->required_features.glsl_version<feature_version)
344                 stage->required_features.glsl_version = feature_version;
345
346         return true;
347 }
348
349 bool LegacyConverter::check_extension(bool Features::*extension) const
350 {
351         if(!(features.*extension))
352                 return false;
353
354         stage->required_features.*extension = true;
355
356         return true;
357 }
358
359 bool LegacyConverter::supports_stage(Stage::Type st) const
360 {
361         if(st==Stage::GEOMETRY)
362         {
363                 if(features.target_api==OPENGL_ES)
364                         return check_version(Version(3, 20));
365                 else
366                         return check_version(Version(1, 50));
367         }
368         else
369                 return true;
370 }
371
372 bool LegacyConverter::supports_unified_interface_syntax() const
373 {
374         if(features.target_api==OPENGL_ES)
375                 return check_version(Version(3, 0));
376         else
377                 return check_version(Version(1, 30));
378 }
379
380 void LegacyConverter::visit(VariableReference &var)
381 {
382         if(var.declaration==frag_out && !supports_unified_interface_syntax())
383         {
384                 var.name = "gl_FragColor";
385                 var.declaration = 0;
386         }
387 }
388
389 void LegacyConverter::visit(Assignment &assign)
390 {
391         TraversingVisitor::visit(assign);
392         if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
393                 assign.target.declaration = 0;
394 }
395
396 bool LegacyConverter::supports_unified_sampling_functions() const
397 {
398         if(features.target_api==OPENGL_ES)
399                 return check_version(Version(3, 0));
400         else
401                 return check_version(Version(1, 30));
402 }
403
404 void LegacyConverter::visit(FunctionCall &call)
405 {
406         if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
407         {
408                 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
409                 {
410                         const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
411                         if(arg_image && !supports_unified_sampling_functions())
412                         {
413                                 string suffix = call.name.substr(7);
414                                 call.name = (arg_image->shadow ? "shadow" : "texture");
415
416                                 switch(arg_image->dimensions)
417                                 {
418                                 case ImageTypeDeclaration::ONE: call.name += "1D"; break;
419                                 case ImageTypeDeclaration::TWO: call.name += "2D"; break;
420                                 case ImageTypeDeclaration::THREE: call.name += "3D"; break;
421                                 case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
422                                 }
423
424                                 if(arg_image->array)
425                                 {
426                                         /* Array textures and the unified sampling function name were
427                                         both introduced in GLSL 1.30. */
428                                         if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
429                                                 check_extension(&Features::ext_texture_array);
430                                         call.name += "Array";
431                                 }
432
433                                 call.name += suffix;
434                         }
435                 }
436         }
437
438         TraversingVisitor::visit(call);
439 }
440
441 bool LegacyConverter::supports_interface_layouts() const
442 {
443         if(features.target_api==OPENGL_ES)
444                 return check_version(Version(3, 0));
445         else if(check_version(Version(3, 30)))
446                 return true;
447         else if(check_version(Version(1, 30)))
448                 return check_extension(&Features::arb_explicit_attrib_location);
449         else
450                 return false;
451 }
452
453 bool LegacyConverter::supports_stage_interface_layouts() const
454 {
455         if(features.target_api==OPENGL_ES)
456                 return check_version(Version(3, 10));
457         else if(check_version(Version(4, 10)))
458                 return true;
459         else
460                 return check_extension(&Features::arb_separate_shader_objects);
461 }
462
463 bool LegacyConverter::supports_centroid_sampling() const
464 {
465         if(features.target_api==OPENGL_ES)
466                 return check_version(Version(3, 0));
467         else if(check_version(Version(1, 20)))
468                 return true;
469         else
470                 return check_extension(&Features::ext_gpu_shader4);
471 }
472
473 bool LegacyConverter::supports_sample_sampling() const
474 {
475         if(features.target_api==OPENGL_ES)
476                 return check_version(Version(3, 20));
477         else if(check_version(Version(4, 0)))
478                 return true;
479         else
480                 return check_extension(&Features::arb_gpu_shader5);
481 }
482
483 bool LegacyConverter::supports_uniform_location() const
484 {
485         if(features.target_api==OPENGL_ES)
486                 return check_version(Version(3, 10));
487         else if(check_version(Version(4, 30)))
488                 return true;
489         else
490                 return check_extension(&Features::arb_explicit_uniform_location);
491 }
492
493 bool LegacyConverter::supports_binding() const
494 {
495         if(features.target_api==OPENGL_ES)
496                 return check_version(Version(3, 10));
497         else
498                 return check_version(Version(4, 20));
499 }
500
501 void LegacyConverter::visit(VariableDeclaration &var)
502 {
503         if(var.layout)
504         {
505                 for(auto i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
506                 {
507                         if(i->name=="location")
508                         {
509                                 bool supported = true;
510                                 bool external = false;
511                                 if(var.interface=="in")
512                                 {
513                                         external = (stage->type==Stage::VERTEX);
514                                         supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
515                                 }
516                                 else if(var.interface=="out")
517                                 {
518                                         external = (stage->type==Stage::FRAGMENT);
519                                         supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
520                                         if(external && !supported && !check_extension(&Features::ext_gpu_shader4))
521                                         {
522                                                 external = false;
523                                                 if(i->value!=0)
524                                                         unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
525                                         }
526                                 }
527                                 else if(var.interface=="uniform")
528                                         supported = supports_uniform_location();
529
530                                 if(!supported)
531                                 {
532                                         if(external)
533                                                 stage->locations[var.name] = i->value;
534                                         i = var.layout->qualifiers.erase(i);
535                                 }
536                                 else
537                                         ++i;
538                         }
539                         else if(i->name=="binding" && !supports_binding())
540                         {
541                                 const TypeDeclaration *type = var.type_declaration;
542                                 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
543                                         type = basic->base_type;
544                                 if(dynamic_cast<const ImageTypeDeclaration *>(type))
545                                         stage->texture_bindings[var.name] = i->value;
546
547                                 i = var.layout->qualifiers.erase(i);
548                         }
549                         else
550                                 ++i;
551                 }
552
553                 if(var.layout->qualifiers.empty())
554                         var.layout = 0;
555         }
556
557         if(var.sampling=="centroid")
558         {
559                 if(!supports_centroid_sampling())
560                         var.sampling = string();
561         }
562         else if(var.sampling=="sample")
563         {
564                 if(!supports_sample_sampling())
565                         var.sampling = string();
566         }
567
568         if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
569         {
570                 if(stage->type==Stage::FRAGMENT && var.interface=="out")
571                 {
572                         frag_out = &var;
573                         nodes_to_remove.insert(&var);
574                 }
575         }
576
577         TraversingVisitor::visit(var);
578 }
579
580 bool LegacyConverter::supports_interface_blocks(const string &iface) const
581 {
582         if(features.target_api==OPENGL_ES)
583         {
584                 if(iface=="uniform")
585                         return check_version(Version(3, 0));
586                 else
587                         return check_version(Version(3, 20));
588         }
589         else if(check_version(Version(1, 50)))
590                 return true;
591         else if(iface=="uniform")
592                 return check_extension(&Features::arb_uniform_buffer_object);
593         else
594                 return false;
595 }
596
597 bool LegacyConverter::supports_interface_block_location() const
598 {
599         if(features.target_api==OPENGL_ES)
600                 return check_version(Version(3, 20));
601         else if(check_version(Version(4, 40)))
602                 return true;
603         else
604                 return check_extension(&Features::arb_enhanced_layouts);
605 }
606
607 void LegacyConverter::visit(InterfaceBlock &iface)
608 {
609         if(iface.layout)
610         {
611                 for(auto i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
612                 {
613                         if(i->name=="location" && !supports_interface_block_location())
614                                 i = iface.layout->qualifiers.erase(i);
615                         else if(i->name=="binding" && !supports_binding())
616                         {
617                                 stage->uniform_block_bindings[iface.block_name] = i->value;
618                                 i = iface.layout->qualifiers.erase(i);
619                         }
620                         else
621                                 ++i;
622                 }
623
624                 if(iface.layout->qualifiers.empty())
625                         iface.layout = 0;
626         }
627
628         if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
629         {
630                 if(!iface.instance_name.empty())
631                         unsupported("ARB_uniform_buffer_object required for interface block instances");
632                 else if(iface.struct_declaration)
633                 {
634                         stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
635                         nodes_to_remove.insert(&iface);
636                         nodes_to_remove.insert(iface.struct_declaration);
637                 }
638                 else
639                         /* If the interface block is an array, it should have an instance
640                         name too, so this should never be reached */
641                         throw logic_error("Unexpected interface block configuration");
642         }
643 }
644
645 } // namespace SL
646 } // namespace GL
647 } // namespace Msp