1 #include <msp/core/algorithm.h>
2 #include <msp/core/raii.h>
3 #include <msp/strings/lexicalcast.h>
5 #include "glsl_error.h"
13 PrecisionConverter::PrecisionConverter():
17 void PrecisionConverter::apply(Stage &s)
20 s.content.visit(*this);
21 NodeRemover().apply(s, nodes_to_remove);
24 void PrecisionConverter::visit(Block &block)
26 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
28 if(&block==&stage->content)
34 void PrecisionConverter::visit(Precision &prec)
36 if(stage->required_features.gl_api==OPENGL_ES2)
37 have_default.insert(prec.type);
39 nodes_to_remove.insert(&prec);
42 void PrecisionConverter::visit(VariableDeclaration &var)
44 if(stage->required_features.gl_api!=OPENGL_ES2)
46 var.precision.clear();
50 const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
51 const TypeDeclaration *type = var.type_declaration;
54 if(dynamic_cast<const ImageTypeDeclaration *>(type))
56 default_prec = "lowp";
59 else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
61 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
63 type = basic->base_type;
71 if(!have_default.count(type->name))
73 Precision *prec = new Precision;
74 prec->precision = default_prec;
75 prec->type = type->name;
76 stage->content.body.insert(insert_point, prec);
78 have_default.insert(type->name);
83 LegacyConverter::LegacyConverter():
87 void LegacyConverter::apply(Stage &s, const Features &feat)
91 if(supports_stage(s.type))
93 s.content.visit(*this);
95 if(!stage->required_features.glsl_version)
96 stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
99 unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
102 void LegacyConverter::unsupported(const string &reason)
104 Diagnostic diagnostic;
105 diagnostic.severity = Diagnostic::ERR;
106 diagnostic.source = GENERATED_SOURCE;
108 diagnostic.message = reason;
109 stage->diagnostics.push_back(diagnostic);
112 void LegacyConverter::visit(Block &block)
114 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
116 if(&block==&stage->content)
117 uniform_insert_point = i;
122 bool LegacyConverter::check_version(const Version &feature_version) const
124 if(features.glsl_version<feature_version)
126 else if(stage->required_features.glsl_version<feature_version)
127 stage->required_features.glsl_version = feature_version;
132 bool LegacyConverter::check_extension(bool Features::*extension) const
134 if(!(features.*extension))
137 stage->required_features.*extension = true;
142 bool LegacyConverter::supports_stage(Stage::Type st) const
144 if(st==Stage::GEOMETRY)
146 if(features.gl_api==OPENGL_ES2)
147 return check_version(Version(3, 20));
149 return check_version(Version(1, 50));
155 bool LegacyConverter::supports_unified_interface_syntax() const
157 if(features.gl_api==OPENGL_ES2)
158 return check_version(Version(3, 0));
160 return check_version(Version(1, 30));
163 void LegacyConverter::visit(VariableReference &var)
165 if(var.declaration==frag_out && !supports_unified_interface_syntax())
167 var.name = "gl_FragColor";
171 else if(var.declaration)
172 r_type = var.declaration->type;
177 void LegacyConverter::visit(Assignment &assign)
179 TraversingVisitor::visit(assign);
180 if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
181 assign.target.declaration = 0;
184 bool LegacyConverter::supports_unified_sampling_functions() const
186 if(features.gl_api==OPENGL_ES2)
187 return check_version(Version(3, 0));
189 return check_version(Version(1, 30));
192 void LegacyConverter::visit(FunctionCall &call)
194 if(call.name=="texture")
198 NodeArray<Expression>::iterator i = call.arguments.begin();
199 if(i!=call.arguments.end())
202 sampler_type = r_type;
204 for(; i!=call.arguments.end(); ++i)
208 if(!supports_unified_sampling_functions())
210 if(sampler_type=="sampler1D")
211 call.name = "texture1D";
212 else if(sampler_type=="sampler2D")
213 call.name = "texture2D";
214 else if(sampler_type=="sampler3D")
215 call.name = "texture3D";
216 else if(sampler_type=="samplerCube")
217 call.name = "textureCube";
218 else if(sampler_type=="sampler1DShadow")
219 call.name = "shadow1D";
220 else if(sampler_type=="sampler2DShadow")
221 call.name = "shadow2D";
222 else if(sampler_type=="sampler1DArray")
224 check_extension(&Features::ext_texture_array);
225 call.name = "texture1DArray";
227 else if(sampler_type=="sampler2DArray")
229 check_extension(&Features::ext_texture_array);
230 call.name = "texture2DArray";
232 else if(sampler_type=="sampler1DArrayShadow")
234 check_extension(&Features::ext_texture_array);
235 call.name = "shadow1DArray";
237 else if(sampler_type=="sampler2DArrayShadow")
239 check_extension(&Features::ext_texture_array);
240 call.name = "shadow2DArray";
245 TraversingVisitor::visit(call);
248 bool LegacyConverter::supports_interface_layouts() const
250 if(features.gl_api==OPENGL_ES2)
251 return check_version(Version(3, 0));
252 else if(check_version(Version(3, 30)))
255 return check_extension(&Features::arb_explicit_attrib_location);
258 bool LegacyConverter::supports_centroid_sampling() const
260 if(features.gl_api==OPENGL_ES2)
261 return check_version(Version(3, 0));
262 else if(check_version(Version(1, 20)))
265 return check_extension(&Features::ext_gpu_shader4);
268 bool LegacyConverter::supports_sample_sampling() const
270 if(features.gl_api==OPENGL_ES2)
271 return check_version(Version(3, 20));
272 else if(check_version(Version(4, 0)))
275 return check_extension(&Features::arb_gpu_shader5);
278 void LegacyConverter::visit(VariableDeclaration &var)
280 if(var.layout && !supports_interface_layouts())
282 vector<Layout::Qualifier>::iterator i;
283 for(i=var.layout->qualifiers.begin(); (i!=var.layout->qualifiers.end() && i->name!="location"); ++i) ;
284 if(i!=var.layout->qualifiers.end())
286 if(stage->type==Stage::VERTEX && var.interface=="in")
288 stage->locations[var.name] = i->value;
289 var.layout->qualifiers.erase(i);
291 else if(stage->type==Stage::FRAGMENT && var.interface=="out")
293 if(check_extension(&Features::ext_gpu_shader4))
294 stage->locations[var.name] = i->value;
296 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
297 var.layout->qualifiers.erase(i);
300 if(var.layout->qualifiers.empty())
305 if(var.sampling=="centroid")
307 if(!supports_centroid_sampling())
308 var.sampling = string();
310 else if(var.sampling=="sample")
312 if(!supports_sample_sampling())
313 var.sampling = string();
316 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
318 if(stage->type==Stage::FRAGMENT && var.interface=="out")
321 nodes_to_remove.insert(&var);
325 TraversingVisitor::visit(var);
328 bool LegacyConverter::supports_interface_blocks(const string &iface) const
330 if(features.gl_api==OPENGL_ES2)
333 return check_version(Version(3, 0));
335 return check_version(Version(3, 20));
337 else if(check_version(Version(1, 50)))
339 else if(iface=="uniform")
340 return check_extension(&Features::arb_uniform_buffer_object);
345 void LegacyConverter::visit(InterfaceBlock &iface)
347 if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
349 if(!iface.instance_name.empty())
350 unsupported("ARB_uniform_buffer_object required for interface block instances");
351 else if(iface.struct_declaration)
353 stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
354 nodes_to_remove.insert(&iface);
355 nodes_to_remove.insert(iface.struct_declaration);
358 /* If the interface block is an array, it should have an instance
359 name too, so this should never be reached */
360 throw logic_error("Unexpected interface block configuration");