]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/finalize.cpp
Refactor layout legacy conversion code to be more extensible
[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
95                 if(!stage->required_features.glsl_version)
96                         stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
97         }
98         else
99                 unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
100 }
101
102 void LegacyConverter::unsupported(const string &reason)
103 {
104         Diagnostic diagnostic;
105         diagnostic.severity = Diagnostic::ERR;
106         diagnostic.source = GENERATED_SOURCE;
107         diagnostic.line = 0;
108         diagnostic.message = reason;
109         stage->diagnostics.push_back(diagnostic);
110 }
111
112 void LegacyConverter::visit(Block &block)
113 {
114         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
115         {
116                 if(&block==&stage->content)
117                         uniform_insert_point = i;
118                 (*i)->visit(*this);
119         }
120 }
121
122 bool LegacyConverter::check_version(const Version &feature_version) const
123 {
124         if(features.glsl_version<feature_version)
125                 return false;
126         else if(stage->required_features.glsl_version<feature_version)
127                 stage->required_features.glsl_version = feature_version;
128
129         return true;
130 }
131
132 bool LegacyConverter::check_extension(bool Features::*extension) const
133 {
134         if(!(features.*extension))
135                 return false;
136
137         stage->required_features.*extension = true;
138
139         return true;
140 }
141
142 bool LegacyConverter::supports_stage(Stage::Type st) const
143 {
144         if(st==Stage::GEOMETRY)
145         {
146                 if(features.gl_api==OPENGL_ES2)
147                         return check_version(Version(3, 20));
148                 else
149                         return check_version(Version(1, 50));
150         }
151         else
152                 return true;
153 }
154
155 bool LegacyConverter::supports_unified_interface_syntax() const
156 {
157         if(features.gl_api==OPENGL_ES2)
158                 return check_version(Version(3, 0));
159         else
160                 return check_version(Version(1, 30));
161 }
162
163 void LegacyConverter::visit(VariableReference &var)
164 {
165         if(var.declaration==frag_out && !supports_unified_interface_syntax())
166         {
167                 var.name = "gl_FragColor";
168                 var.declaration = 0;
169         }
170 }
171
172 void LegacyConverter::visit(Assignment &assign)
173 {
174         TraversingVisitor::visit(assign);
175         if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
176                 assign.target.declaration = 0;
177 }
178
179 bool LegacyConverter::supports_unified_sampling_functions() const
180 {
181         if(features.gl_api==OPENGL_ES2)
182                 return check_version(Version(3, 0));
183         else
184                 return check_version(Version(1, 30));
185 }
186
187 void LegacyConverter::visit(FunctionCall &call)
188 {
189         if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
190         {
191                 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
192                 {
193                         const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
194                         if(arg_image && !supports_unified_sampling_functions())
195                         {
196                                 string suffix = call.name.substr(7);
197                                 call.name = (arg_image->shadow ? "shadow" : "texture");
198
199                                 switch(arg_image->dimensions)
200                                 {
201                                 case ImageTypeDeclaration::ONE: call.name += "1D"; break;
202                                 case ImageTypeDeclaration::TWO: call.name += "2D"; break;
203                                 case ImageTypeDeclaration::THREE: call.name += "3D"; break;
204                                 case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
205                                 }
206
207                                 if(arg_image->array)
208                                 {
209                                         /* Array textures and the unified sampling function name were
210                                         both introduced in GLSL 1.30. */
211                                         if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
212                                                 check_extension(&Features::ext_texture_array);
213                                         call.name += "Array";
214                                 }
215
216                                 call.name += suffix;
217                         }
218                 }
219         }
220
221         TraversingVisitor::visit(call);
222 }
223
224 bool LegacyConverter::supports_interface_layouts() const
225 {
226         if(features.gl_api==OPENGL_ES2)
227                 return check_version(Version(3, 0));
228         else if(check_version(Version(3, 30)))
229                 return true;
230         else
231                 return check_extension(&Features::arb_explicit_attrib_location);
232 }
233
234 bool LegacyConverter::supports_centroid_sampling() const
235 {
236         if(features.gl_api==OPENGL_ES2)
237                 return check_version(Version(3, 0));
238         else if(check_version(Version(1, 20)))
239                 return true;
240         else
241                 return check_extension(&Features::ext_gpu_shader4);
242 }
243
244 bool LegacyConverter::supports_sample_sampling() const
245 {
246         if(features.gl_api==OPENGL_ES2)
247                 return check_version(Version(3, 20));
248         else if(check_version(Version(4, 0)))
249                 return true;
250         else
251                 return check_extension(&Features::arb_gpu_shader5);
252 }
253
254 void LegacyConverter::visit(VariableDeclaration &var)
255 {
256         if(var.layout)
257         {
258                 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
259                 {
260                         if(i->name=="location" && !supports_interface_layouts())
261                         {
262                                 if(stage->type==Stage::VERTEX && var.interface=="in")
263                                         stage->locations[var.name] = i->value;
264                                 else if(stage->type==Stage::FRAGMENT && var.interface=="out")
265                                 {
266                                         if(check_extension(&Features::ext_gpu_shader4))
267                                                 stage->locations[var.name] = i->value;
268                                         else if(i->value!=0)
269                                                 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
270                                 }
271
272                                 i = var.layout->qualifiers.erase(i);
273                         }
274                         else
275                                 ++i;
276                 }
277
278                 if(var.layout->qualifiers.empty())
279                         var.layout = 0;
280         }
281
282         if(var.sampling=="centroid")
283         {
284                 if(!supports_centroid_sampling())
285                         var.sampling = string();
286         }
287         else if(var.sampling=="sample")
288         {
289                 if(!supports_sample_sampling())
290                         var.sampling = string();
291         }
292
293         if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
294         {
295                 if(stage->type==Stage::FRAGMENT && var.interface=="out")
296                 {
297                         frag_out = &var;
298                         nodes_to_remove.insert(&var);
299                 }
300         }
301
302         TraversingVisitor::visit(var);
303 }
304
305 bool LegacyConverter::supports_interface_blocks(const string &iface) const
306 {
307         if(features.gl_api==OPENGL_ES2)
308         {
309                 if(iface=="uniform")
310                         return check_version(Version(3, 0));
311                 else
312                         return check_version(Version(3, 20));
313         }
314         else if(check_version(Version(1, 50)))
315                 return true;
316         else if(iface=="uniform")
317                 return check_extension(&Features::arb_uniform_buffer_object);
318         else
319                 return false;
320 }
321
322 void LegacyConverter::visit(InterfaceBlock &iface)
323 {
324         if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
325         {
326                 if(!iface.instance_name.empty())
327                         unsupported("ARB_uniform_buffer_object required for interface block instances");
328                 else if(iface.struct_declaration)
329                 {
330                         stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
331                         nodes_to_remove.insert(&iface);
332                         nodes_to_remove.insert(iface.struct_declaration);
333                 }
334                 else
335                         /* If the interface block is an array, it should have an instance
336                         name too, so this should never be reached */
337                         throw logic_error("Unexpected interface block configuration");
338         }
339 }
340
341 } // namespace SL
342 } // namespace GL
343 } // namespace Msp