]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/finalize.cpp
Add legacy conversion for binding layout qualifiers
[libs/gl.git] / source / glsl / finalize.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/core/raii.h>
3 #include <msp/strings/lexicalcast.h>
4 #include "finalize.h"
5 #include "glsl_error.h"
6 #include "reflect.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12 namespace SL {
13
14 void LocationAllocator::apply(Module &module)
15 {
16         for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
17                 apply(*i);
18         allocate_locations("uniform");
19 }
20
21 void LocationAllocator::apply(Stage &stage)
22 {
23         swap(used_locations["in"], used_locations["out"]);
24         used_locations["out"].clear();
25
26         stage.content.visit(*this);
27
28         allocate_locations("in");
29         allocate_locations("out");
30 }
31
32 void LocationAllocator::allocate_locations(const string &iface)
33 {
34         vector<VariableDeclaration *>::iterator write = unplaced_variables.begin();
35         unsigned next = 0;
36         for(vector<VariableDeclaration *>::const_iterator i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i)
37         {
38                 if((*i)->interface!=iface)
39                 {
40                         if(write!=i)
41                                 *write = *i;
42                         ++write;
43                         continue;
44                 }
45
46                 if((*i)->interface=="uniform")
47                 {
48                         map<string, unsigned>::const_iterator j = uniform_locations.find((*i)->name);
49                         if(j!=uniform_locations.end())
50                         {
51                                 add_location((*i)->layout, j->second);
52                                 continue;
53                         }
54                 }
55
56                 set<unsigned> &used = used_locations[(*i)->interface];
57
58                 unsigned size = LocationCounter().apply(**i);
59                 while(1)
60                 {
61                         int blocking = -1;
62                         for(unsigned j=0; j<size; ++j)
63                                 if(used.count(next+j))
64                                         blocking = next+j;
65                         if(blocking<0)
66                                 break;
67                         next = blocking+1;
68                 }
69
70                 add_location((*i)->layout, next);
71                 if((*i)->interface=="uniform")
72                         uniform_locations[(*i)->name] = next;
73
74                 for(unsigned j=0; j<size; ++j)
75                         used.insert(next+j);
76                 next += size;
77         }
78
79         unplaced_variables.erase(write, unplaced_variables.end());
80 }
81
82 void LocationAllocator::add_location(RefPtr<Layout> &layout, unsigned location)
83 {
84         if(!layout)
85                 layout = new Layout;
86
87         Layout::Qualifier qual;
88         qual.name = "location";
89         qual.has_value = true;
90         qual.value = location;
91         layout->qualifiers.push_back(qual);
92 }
93
94 void LocationAllocator::visit(VariableDeclaration &var)
95 {
96         if(!var.interface.empty() && var.name.compare(0, 3, "gl_"))
97         {
98                 int location = (var.layout ? get_layout_value(*var.layout, "location") : -1);
99
100                 if(location<0 && var.linked_declaration && var.linked_declaration->layout)
101                 {
102                         location = get_layout_value(*var.linked_declaration->layout, "location");
103                         if(location>=0)
104                                 add_location(var.layout, location);
105                 }
106
107                 if(location>=0)
108                 {
109                         unsigned size = LocationCounter().apply(var);
110                         for(unsigned i=0; i<size; ++i)
111                                 used_locations[var.interface].insert(location+i);
112                         if(var.interface=="uniform")
113                                 uniform_locations[var.name] = location;
114                 }
115                 else
116                         unplaced_variables.push_back(&var);
117         }
118 }
119
120
121 PrecisionConverter::PrecisionConverter():
122         stage(0)
123 { }
124
125 void PrecisionConverter::apply(Stage &s)
126 {
127         stage = &s;
128         s.content.visit(*this);
129         NodeRemover().apply(s, nodes_to_remove);
130 }
131
132 void PrecisionConverter::visit(Block &block)
133 {
134         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
135         {
136                 if(&block==&stage->content)
137                         insert_point = i;
138                 (*i)->visit(*this);
139         }
140 }
141
142 void PrecisionConverter::visit(Precision &prec)
143 {
144         if(stage->required_features.gl_api==OPENGL_ES2)
145                 have_default.insert(prec.type);
146         else
147                 nodes_to_remove.insert(&prec);
148 }
149
150 void PrecisionConverter::visit(VariableDeclaration &var)
151 {
152         if(stage->required_features.gl_api!=OPENGL_ES2)
153         {
154                 var.precision.clear();
155                 return;
156         }
157
158         const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
159         const TypeDeclaration *type = var.type_declaration;
160         while(type)
161         {
162                 if(dynamic_cast<const ImageTypeDeclaration *>(type))
163                 {
164                         default_prec = "lowp";
165                         break;
166                 }
167                 else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
168                 {
169                         if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
170                                 break;
171                         type = basic->base_type;
172                 }
173                 else
174                         return;
175         }
176         if(!type)
177                 return;
178
179         if(!have_default.count(type->name))
180         {
181                 Precision *prec = new Precision;
182                 prec->precision = default_prec;
183                 prec->type = type->name;
184                 stage->content.body.insert(insert_point, prec);
185
186                 have_default.insert(type->name);
187         }
188 }
189
190
191 LegacyConverter::LegacyConverter():
192         frag_out(0)
193 { }
194
195 void LegacyConverter::apply(Stage &s, const Features &feat)
196 {
197         stage = &s;
198         features = feat;
199         if(supports_stage(s.type))
200         {
201                 s.content.visit(*this);
202                 NodeRemover().apply(s, nodes_to_remove);
203
204                 if(!stage->required_features.glsl_version)
205                         stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
206         }
207         else
208                 unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
209 }
210
211 void LegacyConverter::unsupported(const string &reason)
212 {
213         Diagnostic diagnostic;
214         diagnostic.severity = Diagnostic::ERR;
215         diagnostic.source = GENERATED_SOURCE;
216         diagnostic.line = 0;
217         diagnostic.message = reason;
218         stage->diagnostics.push_back(diagnostic);
219 }
220
221 void LegacyConverter::visit(Block &block)
222 {
223         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
224         {
225                 if(&block==&stage->content)
226                         uniform_insert_point = i;
227                 (*i)->visit(*this);
228         }
229 }
230
231 bool LegacyConverter::check_version(const Version &feature_version) const
232 {
233         if(features.glsl_version<feature_version)
234                 return false;
235         else if(stage->required_features.glsl_version<feature_version)
236                 stage->required_features.glsl_version = feature_version;
237
238         return true;
239 }
240
241 bool LegacyConverter::check_extension(bool Features::*extension) const
242 {
243         if(!(features.*extension))
244                 return false;
245
246         stage->required_features.*extension = true;
247
248         return true;
249 }
250
251 bool LegacyConverter::supports_stage(Stage::Type st) const
252 {
253         if(st==Stage::GEOMETRY)
254         {
255                 if(features.gl_api==OPENGL_ES2)
256                         return check_version(Version(3, 20));
257                 else
258                         return check_version(Version(1, 50));
259         }
260         else
261                 return true;
262 }
263
264 bool LegacyConverter::supports_unified_interface_syntax() const
265 {
266         if(features.gl_api==OPENGL_ES2)
267                 return check_version(Version(3, 0));
268         else
269                 return check_version(Version(1, 30));
270 }
271
272 void LegacyConverter::visit(VariableReference &var)
273 {
274         if(var.declaration==frag_out && !supports_unified_interface_syntax())
275         {
276                 var.name = "gl_FragColor";
277                 var.declaration = 0;
278         }
279 }
280
281 void LegacyConverter::visit(Assignment &assign)
282 {
283         TraversingVisitor::visit(assign);
284         if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
285                 assign.target.declaration = 0;
286 }
287
288 bool LegacyConverter::supports_unified_sampling_functions() const
289 {
290         if(features.gl_api==OPENGL_ES2)
291                 return check_version(Version(3, 0));
292         else
293                 return check_version(Version(1, 30));
294 }
295
296 void LegacyConverter::visit(FunctionCall &call)
297 {
298         if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
299         {
300                 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
301                 {
302                         const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
303                         if(arg_image && !supports_unified_sampling_functions())
304                         {
305                                 string suffix = call.name.substr(7);
306                                 call.name = (arg_image->shadow ? "shadow" : "texture");
307
308                                 switch(arg_image->dimensions)
309                                 {
310                                 case ImageTypeDeclaration::ONE: call.name += "1D"; break;
311                                 case ImageTypeDeclaration::TWO: call.name += "2D"; break;
312                                 case ImageTypeDeclaration::THREE: call.name += "3D"; break;
313                                 case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
314                                 }
315
316                                 if(arg_image->array)
317                                 {
318                                         /* Array textures and the unified sampling function name were
319                                         both introduced in GLSL 1.30. */
320                                         if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
321                                                 check_extension(&Features::ext_texture_array);
322                                         call.name += "Array";
323                                 }
324
325                                 call.name += suffix;
326                         }
327                 }
328         }
329
330         TraversingVisitor::visit(call);
331 }
332
333 bool LegacyConverter::supports_interface_layouts() const
334 {
335         if(features.gl_api==OPENGL_ES2)
336                 return check_version(Version(3, 0));
337         else if(check_version(Version(3, 30)))
338                 return true;
339         else if(check_version(Version(1, 30)))
340                 return check_extension(&Features::arb_explicit_attrib_location);
341         else
342                 return false;
343 }
344
345 bool LegacyConverter::supports_stage_interface_layouts() const
346 {
347         if(features.gl_api==OPENGL_ES2)
348                 return check_version(Version(3, 10));
349         else if(check_version(Version(4, 10)))
350                 return true;
351         else
352                 return check_extension(&Features::arb_separate_shader_objects);
353 }
354
355 bool LegacyConverter::supports_centroid_sampling() const
356 {
357         if(features.gl_api==OPENGL_ES2)
358                 return check_version(Version(3, 0));
359         else if(check_version(Version(1, 20)))
360                 return true;
361         else
362                 return check_extension(&Features::ext_gpu_shader4);
363 }
364
365 bool LegacyConverter::supports_sample_sampling() const
366 {
367         if(features.gl_api==OPENGL_ES2)
368                 return check_version(Version(3, 20));
369         else if(check_version(Version(4, 0)))
370                 return true;
371         else
372                 return check_extension(&Features::arb_gpu_shader5);
373 }
374
375 bool LegacyConverter::supports_uniform_location() const
376 {
377         if(features.gl_api==OPENGL_ES2)
378                 return check_version(Version(3, 10));
379         else if(check_version(Version(4, 30)))
380                 return true;
381         else
382                 return check_extension(&Features::arb_explicit_uniform_location);
383 }
384
385 bool LegacyConverter::supports_binding() const
386 {
387         if(features.gl_api==OPENGL_ES2)
388                 return check_version(Version(3, 10));
389         else
390                 return check_version(Version(4, 20));
391 }
392
393 void LegacyConverter::visit(VariableDeclaration &var)
394 {
395         if(var.layout)
396         {
397                 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
398                 {
399                         if(i->name=="location")
400                         {
401                                 bool supported = true;
402                                 bool external = false;
403                                 if(var.interface=="in")
404                                 {
405                                         external = (stage->type==Stage::VERTEX);
406                                         supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
407                                 }
408                                 else if(var.interface=="out")
409                                 {
410                                         external = (stage->type==Stage::FRAGMENT);
411                                         supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
412                                         if(external && !supported && !check_extension(&Features::ext_gpu_shader4))
413                                         {
414                                                 external = false;
415                                                 if(i->value!=0)
416                                                         unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
417                                         }
418                                 }
419                                 else if(var.interface=="uniform")
420                                         supported = supports_uniform_location();
421
422                                 if(!supported)
423                                 {
424                                         if(external)
425                                                 stage->locations[var.name] = i->value;
426                                         i = var.layout->qualifiers.erase(i);
427                                 }
428                                 else
429                                         ++i;
430                         }
431                         else if(i->name=="binding" && !supports_binding())
432                         {
433                                 const TypeDeclaration *type = var.type_declaration;
434                                 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
435                                         type = basic->base_type;
436                                 if(dynamic_cast<const ImageTypeDeclaration *>(type))
437                                         stage->texture_bindings[var.name] = i->value;
438
439                                 i = var.layout->qualifiers.erase(i);
440                         }
441                         else
442                                 ++i;
443                 }
444
445                 if(var.layout->qualifiers.empty())
446                         var.layout = 0;
447         }
448
449         if(var.sampling=="centroid")
450         {
451                 if(!supports_centroid_sampling())
452                         var.sampling = string();
453         }
454         else if(var.sampling=="sample")
455         {
456                 if(!supports_sample_sampling())
457                         var.sampling = string();
458         }
459
460         if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
461         {
462                 if(stage->type==Stage::FRAGMENT && var.interface=="out")
463                 {
464                         frag_out = &var;
465                         nodes_to_remove.insert(&var);
466                 }
467         }
468
469         TraversingVisitor::visit(var);
470 }
471
472 bool LegacyConverter::supports_interface_blocks(const string &iface) const
473 {
474         if(features.gl_api==OPENGL_ES2)
475         {
476                 if(iface=="uniform")
477                         return check_version(Version(3, 0));
478                 else
479                         return check_version(Version(3, 20));
480         }
481         else if(check_version(Version(1, 50)))
482                 return true;
483         else if(iface=="uniform")
484                 return check_extension(&Features::arb_uniform_buffer_object);
485         else
486                 return false;
487 }
488
489 bool LegacyConverter::supports_interface_block_location() const
490 {
491         if(features.gl_api==OPENGL_ES2)
492                 return check_version(Version(3, 20));
493         else if(check_version(Version(4, 40)))
494                 return true;
495         else
496                 return check_extension(&Features::arb_enhanced_layouts);
497 }
498
499 void LegacyConverter::visit(InterfaceBlock &iface)
500 {
501         if(iface.layout)
502         {
503                 for(vector<Layout::Qualifier>::const_iterator i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
504                 {
505                         if(i->name=="location" && !supports_interface_block_location())
506                                 i = iface.layout->qualifiers.erase(i);
507                         else if(i->name=="binding" && !supports_binding())
508                         {
509                                 stage->uniform_block_bindings[iface.block_name] = i->value;
510                                 i = iface.layout->qualifiers.erase(i);
511                         }
512                         else
513                                 ++i;
514                 }
515
516                 if(iface.layout->qualifiers.empty())
517                         iface.layout = 0;
518         }
519
520         if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
521         {
522                 if(!iface.instance_name.empty())
523                         unsupported("ARB_uniform_buffer_object required for interface block instances");
524                 else if(iface.struct_declaration)
525                 {
526                         stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
527                         nodes_to_remove.insert(&iface);
528                         nodes_to_remove.insert(iface.struct_declaration);
529                 }
530                 else
531                         /* If the interface block is an array, it should have an instance
532                         name too, so this should never be reached */
533                         throw logic_error("Unexpected interface block configuration");
534         }
535 }
536
537 } // namespace SL
538 } // namespace GL
539 } // namespace Msp