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