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