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