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