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