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