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";
172 void LegacyConverter::visit(Assignment &assign)
174 TraversingVisitor::visit(assign);
175 if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
176 assign.target.declaration = 0;
179 bool LegacyConverter::supports_unified_sampling_functions() const
181 if(features.gl_api==OPENGL_ES2)
182 return check_version(Version(3, 0));
184 return check_version(Version(1, 30));
187 void LegacyConverter::visit(FunctionCall &call)
189 if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
191 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
193 const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
194 if(arg_image && !supports_unified_sampling_functions())
196 string suffix = call.name.substr(7);
197 call.name = (arg_image->shadow ? "shadow" : "texture");
199 switch(arg_image->dimensions)
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;
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";
221 TraversingVisitor::visit(call);
224 bool LegacyConverter::supports_interface_layouts() const
226 if(features.gl_api==OPENGL_ES2)
227 return check_version(Version(3, 0));
228 else if(check_version(Version(3, 30)))
231 return check_extension(&Features::arb_explicit_attrib_location);
234 bool LegacyConverter::supports_centroid_sampling() const
236 if(features.gl_api==OPENGL_ES2)
237 return check_version(Version(3, 0));
238 else if(check_version(Version(1, 20)))
241 return check_extension(&Features::ext_gpu_shader4);
244 bool LegacyConverter::supports_sample_sampling() const
246 if(features.gl_api==OPENGL_ES2)
247 return check_version(Version(3, 20));
248 else if(check_version(Version(4, 0)))
251 return check_extension(&Features::arb_gpu_shader5);
254 void LegacyConverter::visit(VariableDeclaration &var)
258 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
260 if(i->name=="location" && !supports_interface_layouts())
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")
266 if(check_extension(&Features::ext_gpu_shader4))
267 stage->locations[var.name] = i->value;
269 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
272 i = var.layout->qualifiers.erase(i);
278 if(var.layout->qualifiers.empty())
282 if(var.sampling=="centroid")
284 if(!supports_centroid_sampling())
285 var.sampling = string();
287 else if(var.sampling=="sample")
289 if(!supports_sample_sampling())
290 var.sampling = string();
293 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
295 if(stage->type==Stage::FRAGMENT && var.interface=="out")
298 nodes_to_remove.insert(&var);
302 TraversingVisitor::visit(var);
305 bool LegacyConverter::supports_interface_blocks(const string &iface) const
307 if(features.gl_api==OPENGL_ES2)
310 return check_version(Version(3, 0));
312 return check_version(Version(3, 20));
314 else if(check_version(Version(1, 50)))
316 else if(iface=="uniform")
317 return check_extension(&Features::arb_uniform_buffer_object);
322 void LegacyConverter::visit(InterfaceBlock &iface)
324 if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
326 if(!iface.instance_name.empty())
327 unsupported("ARB_uniform_buffer_object required for interface block instances");
328 else if(iface.struct_declaration)
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);
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");