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