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