1 #include <msp/core/algorithm.h>
2 #include <msp/core/raii.h>
3 #include <msp/strings/lexicalcast.h>
4 #include "compatibility.h"
5 #include "glsl_error.h"
13 DefaultPrecisionGenerator::DefaultPrecisionGenerator():
17 void DefaultPrecisionGenerator::apply(Stage &s)
23 void DefaultPrecisionGenerator::visit(Block &block)
25 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
27 if(&block==&stage->content)
33 void DefaultPrecisionGenerator::visit(Precision &prec)
35 have_default.insert(prec.type);
38 void DefaultPrecisionGenerator::visit(VariableDeclaration &var)
40 if(var.type_declaration)
43 string type = var.type;
44 if(!type.compare(0, 3, "vec") || !type.compare(0, 3, "mat"))
46 else if(!type.compare(0, 3, "ivec") || type=="uint")
49 if(!have_default.count(type))
51 Precision *prec = new Precision;
52 if(!type.compare(0, 7, "sampler"))
53 prec->precision = "lowp";
54 else if(stage->type==Stage::FRAGMENT)
55 prec->precision = "mediump";
57 prec->precision = "highp";
59 stage->content.body.insert(insert_point, prec);
61 have_default.insert(type);
66 void PrecisionRemover::apply(Stage &stage)
69 NodeRemover().apply(stage, nodes_to_remove);
72 void PrecisionRemover::visit(Precision &prec)
74 nodes_to_remove.insert(&prec);
77 void PrecisionRemover::visit(VariableDeclaration &var)
79 var.precision.clear();
83 LegacyConverter::LegacyConverter():
87 void LegacyConverter::apply(Stage &s, const Features &feat)
91 if(!supports_stage(s.type))
92 throw unsupported_shader(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
96 void LegacyConverter::visit(Block &block)
98 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
100 if(&block==&stage->content)
101 uniform_insert_point = i;
106 bool LegacyConverter::check_version(const Version &feature_version) const
108 if(features.glsl_version<feature_version)
110 else if(stage->required_features.glsl_version<feature_version)
111 stage->required_features.glsl_version = feature_version;
116 bool LegacyConverter::check_extension(bool Features::*extension) const
118 if(!(features.*extension))
121 stage->required_features.*extension = true;
126 bool LegacyConverter::supports_stage(Stage::Type st) const
128 if(st==Stage::GEOMETRY)
130 if(features.gl_api==OPENGL_ES2)
131 return check_version(Version(3, 20));
133 return check_version(Version(1, 50));
139 bool LegacyConverter::supports_unified_interface_syntax() const
141 if(features.gl_api==OPENGL_ES2)
142 return check_version(Version(3, 0));
144 return check_version(Version(1, 30));
147 void LegacyConverter::visit(VariableReference &var)
149 if(var.declaration==frag_out && !supports_unified_interface_syntax())
151 var.name = "gl_FragColor";
155 else if(var.declaration)
156 type = var.declaration->type;
161 void LegacyConverter::visit(Assignment &assign)
163 TraversingVisitor::visit(assign);
164 if(assign.target_declaration==frag_out && !supports_unified_interface_syntax())
165 assign.target_declaration = 0;
168 bool LegacyConverter::supports_unified_sampling_functions() const
170 if(features.gl_api==OPENGL_ES2)
171 return check_version(Version(3, 0));
173 return check_version(Version(1, 30));
176 void LegacyConverter::visit(FunctionCall &call)
178 if(call.name=="texture")
182 NodeArray<Expression>::iterator i = call.arguments.begin();
183 if(i!=call.arguments.end())
188 for(; i!=call.arguments.end(); ++i)
192 if(!supports_unified_sampling_functions())
194 if(sampler_type=="sampler1D")
195 call.name = "texture1D";
196 else if(sampler_type=="sampler2D")
197 call.name = "texture2D";
198 else if(sampler_type=="sampler3D")
199 call.name = "texture3D";
200 else if(sampler_type=="samplerCube")
201 call.name = "textureCube";
202 else if(sampler_type=="sampler1DShadow")
203 call.name = "shadow1D";
204 else if(sampler_type=="sampler2DShadow")
205 call.name = "shadow2D";
206 else if(sampler_type=="sampler1DArray")
208 check_extension(&Features::ext_texture_array);
209 call.name = "texture1DArray";
211 else if(sampler_type=="sampler2DArray")
213 check_extension(&Features::ext_texture_array);
214 call.name = "texture2DArray";
216 else if(sampler_type=="sampler1DArrayShadow")
218 check_extension(&Features::ext_texture_array);
219 call.name = "shadow1DArray";
221 else if(sampler_type=="sampler2DArrayShadow")
223 check_extension(&Features::ext_texture_array);
224 call.name = "shadow2DArray";
229 TraversingVisitor::visit(call);
232 bool LegacyConverter::supports_interface_layouts() const
234 if(features.gl_api==OPENGL_ES2)
235 return check_version(Version(3, 0));
236 else if(check_version(Version(3, 30)))
239 return check_extension(&Features::arb_explicit_attrib_location);
242 bool LegacyConverter::supports_centroid_sampling() const
244 if(features.gl_api==OPENGL_ES2)
245 return check_version(Version(3, 0));
246 else if(check_version(Version(1, 20)))
249 return check_extension(&Features::ext_gpu_shader4);
252 bool LegacyConverter::supports_sample_sampling() const
254 if(features.gl_api==OPENGL_ES2)
255 return check_version(Version(3, 20));
256 else if(check_version(Version(4, 0)))
259 return check_extension(&Features::arb_gpu_shader5);
262 void LegacyConverter::visit(VariableDeclaration &var)
264 if(var.layout && !supports_interface_layouts())
266 vector<Layout::Qualifier>::iterator i;
267 for(i=var.layout->qualifiers.begin(); (i!=var.layout->qualifiers.end() && i->name!="location"); ++i) ;
268 if(i!=var.layout->qualifiers.end())
270 if(stage->type==Stage::VERTEX && var.interface=="in")
272 stage->locations[var.name] = i->value;
273 var.layout->qualifiers.erase(i);
275 else if(stage->type==Stage::FRAGMENT && var.interface=="out")
277 if(i->value!=0 && !check_extension(&Features::ext_gpu_shader4))
278 throw unsupported_shader("EXT_gpu_shader4 is required");
279 stage->locations[var.name] = i->value;
280 var.layout->qualifiers.erase(i);
283 if(var.layout->qualifiers.empty())
288 if(var.sampling=="centroid")
290 if(!supports_centroid_sampling())
291 var.sampling = string();
293 else if(var.sampling=="sample")
295 if(!supports_sample_sampling())
296 var.sampling = string();
299 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
301 if(stage->type==Stage::FRAGMENT && var.interface=="out")
304 nodes_to_remove.insert(&var);
308 TraversingVisitor::visit(var);
311 bool LegacyConverter::supports_interface_blocks(const string &iface) const
313 if(features.gl_api==OPENGL_ES2)
316 return check_version(Version(3, 0));
318 return check_version(Version(3, 20));
320 else if(check_version(Version(1, 50)))
322 else if(iface=="uniform")
323 return check_extension(&Features::arb_uniform_buffer_object);
328 void LegacyConverter::visit(InterfaceBlock &iface)
330 if(!supports_interface_blocks(iface.interface))
332 stage->content.body.splice(uniform_insert_point, iface.members.body);
333 nodes_to_remove.insert(&iface);