]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/finalize.cpp
Add missing NodeRemover invocation to LegacyConverter
[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
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11 namespace SL {
12
13 PrecisionConverter::PrecisionConverter():
14         stage(0)
15 { }
16
17 void PrecisionConverter::apply(Stage &s)
18 {
19         stage = &s;
20         s.content.visit(*this);
21         NodeRemover().apply(s, nodes_to_remove);
22 }
23
24 void PrecisionConverter::visit(Block &block)
25 {
26         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
27         {
28                 if(&block==&stage->content)
29                         insert_point = i;
30                 (*i)->visit(*this);
31         }
32 }
33
34 void PrecisionConverter::visit(Precision &prec)
35 {
36         if(stage->required_features.gl_api==OPENGL_ES2)
37                 have_default.insert(prec.type);
38         else
39                 nodes_to_remove.insert(&prec);
40 }
41
42 void PrecisionConverter::visit(VariableDeclaration &var)
43 {
44         if(stage->required_features.gl_api!=OPENGL_ES2)
45         {
46                 var.precision.clear();
47                 return;
48         }
49
50         const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
51         const TypeDeclaration *type = var.type_declaration;
52         while(type)
53         {
54                 if(dynamic_cast<const ImageTypeDeclaration *>(type))
55                 {
56                         default_prec = "lowp";
57                         break;
58                 }
59                 else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
60                 {
61                         if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
62                                 break;
63                         type = basic->base_type;
64                 }
65                 else
66                         return;
67         }
68         if(!type)
69                 return;
70
71         if(!have_default.count(type->name))
72         {
73                 Precision *prec = new Precision;
74                 prec->precision = default_prec;
75                 prec->type = type->name;
76                 stage->content.body.insert(insert_point, prec);
77
78                 have_default.insert(type->name);
79         }
80 }
81
82
83 LegacyConverter::LegacyConverter():
84         frag_out(0)
85 { }
86
87 void LegacyConverter::apply(Stage &s, const Features &feat)
88 {
89         stage = &s;
90         features = feat;
91         if(supports_stage(s.type))
92         {
93                 s.content.visit(*this);
94                 NodeRemover().apply(s, nodes_to_remove);
95
96                 if(!stage->required_features.glsl_version)
97                         stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
98         }
99         else
100                 unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
101 }
102
103 void LegacyConverter::unsupported(const string &reason)
104 {
105         Diagnostic diagnostic;
106         diagnostic.severity = Diagnostic::ERR;
107         diagnostic.source = GENERATED_SOURCE;
108         diagnostic.line = 0;
109         diagnostic.message = reason;
110         stage->diagnostics.push_back(diagnostic);
111 }
112
113 void LegacyConverter::visit(Block &block)
114 {
115         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
116         {
117                 if(&block==&stage->content)
118                         uniform_insert_point = i;
119                 (*i)->visit(*this);
120         }
121 }
122
123 bool LegacyConverter::check_version(const Version &feature_version) const
124 {
125         if(features.glsl_version<feature_version)
126                 return false;
127         else if(stage->required_features.glsl_version<feature_version)
128                 stage->required_features.glsl_version = feature_version;
129
130         return true;
131 }
132
133 bool LegacyConverter::check_extension(bool Features::*extension) const
134 {
135         if(!(features.*extension))
136                 return false;
137
138         stage->required_features.*extension = true;
139
140         return true;
141 }
142
143 bool LegacyConverter::supports_stage(Stage::Type st) const
144 {
145         if(st==Stage::GEOMETRY)
146         {
147                 if(features.gl_api==OPENGL_ES2)
148                         return check_version(Version(3, 20));
149                 else
150                         return check_version(Version(1, 50));
151         }
152         else
153                 return true;
154 }
155
156 bool LegacyConverter::supports_unified_interface_syntax() const
157 {
158         if(features.gl_api==OPENGL_ES2)
159                 return check_version(Version(3, 0));
160         else
161                 return check_version(Version(1, 30));
162 }
163
164 void LegacyConverter::visit(VariableReference &var)
165 {
166         if(var.declaration==frag_out && !supports_unified_interface_syntax())
167         {
168                 var.name = "gl_FragColor";
169                 var.declaration = 0;
170         }
171 }
172
173 void LegacyConverter::visit(Assignment &assign)
174 {
175         TraversingVisitor::visit(assign);
176         if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
177                 assign.target.declaration = 0;
178 }
179
180 bool LegacyConverter::supports_unified_sampling_functions() const
181 {
182         if(features.gl_api==OPENGL_ES2)
183                 return check_version(Version(3, 0));
184         else
185                 return check_version(Version(1, 30));
186 }
187
188 void LegacyConverter::visit(FunctionCall &call)
189 {
190         if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
191         {
192                 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
193                 {
194                         const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
195                         if(arg_image && !supports_unified_sampling_functions())
196                         {
197                                 string suffix = call.name.substr(7);
198                                 call.name = (arg_image->shadow ? "shadow" : "texture");
199
200                                 switch(arg_image->dimensions)
201                                 {
202                                 case ImageTypeDeclaration::ONE: call.name += "1D"; break;
203                                 case ImageTypeDeclaration::TWO: call.name += "2D"; break;
204                                 case ImageTypeDeclaration::THREE: call.name += "3D"; break;
205                                 case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
206                                 }
207
208                                 if(arg_image->array)
209                                 {
210                                         /* Array textures and the unified sampling function name were
211                                         both introduced in GLSL 1.30. */
212                                         if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
213                                                 check_extension(&Features::ext_texture_array);
214                                         call.name += "Array";
215                                 }
216
217                                 call.name += suffix;
218                         }
219                 }
220         }
221
222         TraversingVisitor::visit(call);
223 }
224
225 bool LegacyConverter::supports_interface_layouts() const
226 {
227         if(features.gl_api==OPENGL_ES2)
228                 return check_version(Version(3, 0));
229         else if(check_version(Version(3, 30)))
230                 return true;
231         else
232                 return check_extension(&Features::arb_explicit_attrib_location);
233 }
234
235 bool LegacyConverter::supports_centroid_sampling() const
236 {
237         if(features.gl_api==OPENGL_ES2)
238                 return check_version(Version(3, 0));
239         else if(check_version(Version(1, 20)))
240                 return true;
241         else
242                 return check_extension(&Features::ext_gpu_shader4);
243 }
244
245 bool LegacyConverter::supports_sample_sampling() const
246 {
247         if(features.gl_api==OPENGL_ES2)
248                 return check_version(Version(3, 20));
249         else if(check_version(Version(4, 0)))
250                 return true;
251         else
252                 return check_extension(&Features::arb_gpu_shader5);
253 }
254
255 void LegacyConverter::visit(VariableDeclaration &var)
256 {
257         if(var.layout)
258         {
259                 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
260                 {
261                         if(i->name=="location" && !supports_interface_layouts())
262                         {
263                                 if(stage->type==Stage::VERTEX && var.interface=="in")
264                                         stage->locations[var.name] = i->value;
265                                 else if(stage->type==Stage::FRAGMENT && var.interface=="out")
266                                 {
267                                         if(check_extension(&Features::ext_gpu_shader4))
268                                                 stage->locations[var.name] = i->value;
269                                         else if(i->value!=0)
270                                                 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
271                                 }
272
273                                 i = var.layout->qualifiers.erase(i);
274                         }
275                         else
276                                 ++i;
277                 }
278
279                 if(var.layout->qualifiers.empty())
280                         var.layout = 0;
281         }
282
283         if(var.sampling=="centroid")
284         {
285                 if(!supports_centroid_sampling())
286                         var.sampling = string();
287         }
288         else if(var.sampling=="sample")
289         {
290                 if(!supports_sample_sampling())
291                         var.sampling = string();
292         }
293
294         if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
295         {
296                 if(stage->type==Stage::FRAGMENT && var.interface=="out")
297                 {
298                         frag_out = &var;
299                         nodes_to_remove.insert(&var);
300                 }
301         }
302
303         TraversingVisitor::visit(var);
304 }
305
306 bool LegacyConverter::supports_interface_blocks(const string &iface) const
307 {
308         if(features.gl_api==OPENGL_ES2)
309         {
310                 if(iface=="uniform")
311                         return check_version(Version(3, 0));
312                 else
313                         return check_version(Version(3, 20));
314         }
315         else if(check_version(Version(1, 50)))
316                 return true;
317         else if(iface=="uniform")
318                 return check_extension(&Features::arb_uniform_buffer_object);
319         else
320                 return false;
321 }
322
323 void LegacyConverter::visit(InterfaceBlock &iface)
324 {
325         if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
326         {
327                 if(!iface.instance_name.empty())
328                         unsupported("ARB_uniform_buffer_object required for interface block instances");
329                 else if(iface.struct_declaration)
330                 {
331                         stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
332                         nodes_to_remove.insert(&iface);
333                         nodes_to_remove.insert(iface.struct_declaration);
334                 }
335                 else
336                         /* If the interface block is an array, it should have an instance
337                         name too, so this should never be reached */
338                         throw logic_error("Unexpected interface block configuration");
339         }
340 }
341
342 } // namespace SL
343 } // namespace GL
344 } // namespace Msp