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);
94 NodeRemover().apply(s, nodes_to_remove);
96 if(!stage->required_features.glsl_version)
97 stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
100 unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
103 void LegacyConverter::unsupported(const string &reason)
105 Diagnostic diagnostic;
106 diagnostic.severity = Diagnostic::ERR;
107 diagnostic.source = GENERATED_SOURCE;
109 diagnostic.message = reason;
110 stage->diagnostics.push_back(diagnostic);
113 void LegacyConverter::visit(Block &block)
115 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
117 if(&block==&stage->content)
118 uniform_insert_point = i;
123 bool LegacyConverter::check_version(const Version &feature_version) const
125 if(features.glsl_version<feature_version)
127 else if(stage->required_features.glsl_version<feature_version)
128 stage->required_features.glsl_version = feature_version;
133 bool LegacyConverter::check_extension(bool Features::*extension) const
135 if(!(features.*extension))
138 stage->required_features.*extension = true;
143 bool LegacyConverter::supports_stage(Stage::Type st) const
145 if(st==Stage::GEOMETRY)
147 if(features.gl_api==OPENGL_ES2)
148 return check_version(Version(3, 20));
150 return check_version(Version(1, 50));
156 bool LegacyConverter::supports_unified_interface_syntax() const
158 if(features.gl_api==OPENGL_ES2)
159 return check_version(Version(3, 0));
161 return check_version(Version(1, 30));
164 void LegacyConverter::visit(VariableReference &var)
166 if(var.declaration==frag_out && !supports_unified_interface_syntax())
168 var.name = "gl_FragColor";
173 void LegacyConverter::visit(Assignment &assign)
175 TraversingVisitor::visit(assign);
176 if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
177 assign.target.declaration = 0;
180 bool LegacyConverter::supports_unified_sampling_functions() const
182 if(features.gl_api==OPENGL_ES2)
183 return check_version(Version(3, 0));
185 return check_version(Version(1, 30));
188 void LegacyConverter::visit(FunctionCall &call)
190 if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
192 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
194 const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
195 if(arg_image && !supports_unified_sampling_functions())
197 string suffix = call.name.substr(7);
198 call.name = (arg_image->shadow ? "shadow" : "texture");
200 switch(arg_image->dimensions)
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;
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";
222 TraversingVisitor::visit(call);
225 bool LegacyConverter::supports_interface_layouts() const
227 if(features.gl_api==OPENGL_ES2)
228 return check_version(Version(3, 0));
229 else if(check_version(Version(3, 30)))
231 else if(check_version(Version(1, 30)))
232 return check_extension(&Features::arb_explicit_attrib_location);
237 bool LegacyConverter::supports_centroid_sampling() const
239 if(features.gl_api==OPENGL_ES2)
240 return check_version(Version(3, 0));
241 else if(check_version(Version(1, 20)))
244 return check_extension(&Features::ext_gpu_shader4);
247 bool LegacyConverter::supports_sample_sampling() const
249 if(features.gl_api==OPENGL_ES2)
250 return check_version(Version(3, 20));
251 else if(check_version(Version(4, 0)))
254 return check_extension(&Features::arb_gpu_shader5);
257 void LegacyConverter::visit(VariableDeclaration &var)
261 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
263 if(i->name=="location" && !supports_interface_layouts())
265 if(stage->type==Stage::VERTEX && var.interface=="in")
266 stage->locations[var.name] = i->value;
267 else if(stage->type==Stage::FRAGMENT && var.interface=="out")
269 if(check_extension(&Features::ext_gpu_shader4))
270 stage->locations[var.name] = i->value;
272 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
275 i = var.layout->qualifiers.erase(i);
281 if(var.layout->qualifiers.empty())
285 if(var.sampling=="centroid")
287 if(!supports_centroid_sampling())
288 var.sampling = string();
290 else if(var.sampling=="sample")
292 if(!supports_sample_sampling())
293 var.sampling = string();
296 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
298 if(stage->type==Stage::FRAGMENT && var.interface=="out")
301 nodes_to_remove.insert(&var);
305 TraversingVisitor::visit(var);
308 bool LegacyConverter::supports_interface_blocks(const string &iface) const
310 if(features.gl_api==OPENGL_ES2)
313 return check_version(Version(3, 0));
315 return check_version(Version(3, 20));
317 else if(check_version(Version(1, 50)))
319 else if(iface=="uniform")
320 return check_extension(&Features::arb_uniform_buffer_object);
325 void LegacyConverter::visit(InterfaceBlock &iface)
327 if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
329 if(!iface.instance_name.empty())
330 unsupported("ARB_uniform_buffer_object required for interface block instances");
331 else if(iface.struct_declaration)
333 stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
334 nodes_to_remove.insert(&iface);
335 nodes_to_remove.insert(iface.struct_declaration);
338 /* If the interface block is an array, it should have an instance
339 name too, so this should never be reached */
340 throw logic_error("Unexpected interface block configuration");