]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/finalize.cpp
Allocate locations to interface variables
[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 void LegacyConverter::visit(VariableDeclaration &var)
386 {
387         if(var.layout)
388         {
389                 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
390                 {
391                         if(i->name=="location")
392                         {
393                                 bool supported = true;
394                                 bool external = false;
395                                 if(var.interface=="in")
396                                 {
397                                         external = (stage->type==Stage::VERTEX);
398                                         supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
399                                 }
400                                 else if(var.interface=="out")
401                                 {
402                                         external = (stage->type==Stage::FRAGMENT);
403                                         supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
404                                         if(external && !supported && !check_extension(&Features::ext_gpu_shader4))
405                                         {
406                                                 external = false;
407                                                 if(i->value!=0)
408                                                         unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
409                                         }
410                                 }
411                                 else if(var.interface=="uniform")
412                                         supported = supports_uniform_location();
413
414                                 if(!supported)
415                                 {
416                                         if(external)
417                                                 stage->locations[var.name] = i->value;
418                                         i = var.layout->qualifiers.erase(i);
419                                 }
420                                 else
421                                         ++i;
422                         }
423                         else
424                                 ++i;
425                 }
426
427                 if(var.layout->qualifiers.empty())
428                         var.layout = 0;
429         }
430
431         if(var.sampling=="centroid")
432         {
433                 if(!supports_centroid_sampling())
434                         var.sampling = string();
435         }
436         else if(var.sampling=="sample")
437         {
438                 if(!supports_sample_sampling())
439                         var.sampling = string();
440         }
441
442         if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
443         {
444                 if(stage->type==Stage::FRAGMENT && var.interface=="out")
445                 {
446                         frag_out = &var;
447                         nodes_to_remove.insert(&var);
448                 }
449         }
450
451         TraversingVisitor::visit(var);
452 }
453
454 bool LegacyConverter::supports_interface_blocks(const string &iface) const
455 {
456         if(features.gl_api==OPENGL_ES2)
457         {
458                 if(iface=="uniform")
459                         return check_version(Version(3, 0));
460                 else
461                         return check_version(Version(3, 20));
462         }
463         else if(check_version(Version(1, 50)))
464                 return true;
465         else if(iface=="uniform")
466                 return check_extension(&Features::arb_uniform_buffer_object);
467         else
468                 return false;
469 }
470
471 bool LegacyConverter::supports_interface_block_location() const
472 {
473         if(features.gl_api==OPENGL_ES2)
474                 return check_version(Version(3, 20));
475         else if(check_version(Version(4, 40)))
476                 return true;
477         else
478                 return check_extension(&Features::arb_enhanced_layouts);
479 }
480
481 void LegacyConverter::visit(InterfaceBlock &iface)
482 {
483         if(iface.layout)
484         {
485                 for(vector<Layout::Qualifier>::const_iterator i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
486                 {
487                         if(i->name=="location" && !supports_interface_block_location())
488                                 i = iface.layout->qualifiers.erase(i);
489                         else
490                                 ++i;
491                 }
492
493                 if(iface.layout->qualifiers.empty())
494                         iface.layout = 0;
495         }
496
497         if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
498         {
499                 if(!iface.instance_name.empty())
500                         unsupported("ARB_uniform_buffer_object required for interface block instances");
501                 else if(iface.struct_declaration)
502                 {
503                         stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
504                         nodes_to_remove.insert(&iface);
505                         nodes_to_remove.insert(iface.struct_declaration);
506                 }
507                 else
508                         /* If the interface block is an array, it should have an instance
509                         name too, so this should never be reached */
510                         throw logic_error("Unexpected interface block configuration");
511         }
512 }
513
514 } // namespace SL
515 } // namespace GL
516 } // namespace Msp