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