1 #include <msp/core/algorithm.h>
2 #include <msp/core/raii.h>
3 #include <msp/gl/extensions/arb_explicit_attrib_location.h>
4 #include <msp/gl/extensions/arb_gpu_shader5.h>
5 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
6 #include <msp/gl/extensions/ext_gpu_shader4.h>
7 #include <msp/gl/extensions/ext_texture_array.h>
8 #include "compatibility.h"
16 DefaultPrecisionGenerator::DefaultPrecisionGenerator():
17 stage_type(Stage::SHARED),
21 void DefaultPrecisionGenerator::apply(Stage &stage)
23 SetForScope<Stage::Type> set_stage(stage_type, stage.type);
27 void DefaultPrecisionGenerator::visit(Block &block)
31 SetForScope<bool> set(toplevel, false);
32 BlockModifier::visit(block);
35 TraversingVisitor::visit(block);
38 void DefaultPrecisionGenerator::visit(Precision &prec)
40 have_default.insert(prec.type);
43 void DefaultPrecisionGenerator::visit(VariableDeclaration &var)
45 if(var.type_declaration)
48 string type = var.type;
49 if(!type.compare(0, 3, "vec") || !type.compare(0, 3, "mat"))
51 else if(!type.compare(0, 3, "ivec") || type=="uint")
54 if(!have_default.count(type))
56 Precision *prec = new Precision;
57 if(!type.compare(0, 7, "sampler"))
58 prec->precision = "lowp";
59 else if(stage_type==Stage::FRAGMENT)
60 prec->precision = "mediump";
62 prec->precision = "highp";
64 insert_nodes.push_back(prec);
66 have_default.insert(type);
71 void PrecisionRemover::visit(Precision &)
76 void PrecisionRemover::visit(VariableDeclaration &var)
78 var.precision.clear();
82 LegacyConverter::LegacyConverter():
83 target_api(get_gl_api()),
84 target_version(get_glsl_version()),
88 void LegacyConverter::apply(Stage &s)
90 SetForScope<Stage *> set_stage(stage, &s);
94 bool LegacyConverter::check_version(const Version &feature_version) const
96 if(target_version<feature_version)
98 else if(stage->required_version<feature_version)
99 stage->required_version = feature_version;
104 bool LegacyConverter::check_extension(const Extension &extension) const
109 vector<const Extension *>::iterator i = find(stage->required_extensions, &extension);
110 if(i==stage->required_extensions.end())
111 stage->required_extensions.push_back(&extension);
116 bool LegacyConverter::supports_unified_interface_syntax() const
118 if(target_api==OPENGL_ES2)
119 return check_version(Version(3, 0));
121 return check_version(Version(1, 30));
124 void LegacyConverter::visit(VariableReference &var)
126 if(var.declaration==frag_out && !supports_unified_interface_syntax())
128 var.name = "gl_FragColor";
132 else if(var.declaration)
133 type = var.declaration->type;
138 void LegacyConverter::visit(Assignment &assign)
140 TraversingVisitor::visit(assign);
141 if(assign.target_declaration==frag_out && !supports_unified_interface_syntax())
142 assign.target_declaration = 0;
145 bool LegacyConverter::supports_unified_sampling_functions() const
147 if(target_api==OPENGL_ES2)
148 return check_version(Version(3, 0));
150 return check_version(Version(1, 30));
153 void LegacyConverter::visit(FunctionCall &call)
155 if(call.name=="texture")
159 NodeArray<Expression>::iterator i = call.arguments.begin();
160 if(i!=call.arguments.end())
165 for(; i!=call.arguments.end(); ++i)
169 if(!supports_unified_sampling_functions())
171 if(sampler_type=="sampler1D")
172 call.name = "texture1D";
173 else if(sampler_type=="sampler2D")
174 call.name = "texture2D";
175 else if(sampler_type=="sampler3D")
176 call.name = "texture3D";
177 else if(sampler_type=="samplerCube")
178 call.name = "textureCube";
179 else if(sampler_type=="sampler1DShadow")
180 call.name = "shadow1D";
181 else if(sampler_type=="sampler2DShadow")
182 call.name = "shadow2D";
183 else if(sampler_type=="sampler1DArray")
185 check_extension(EXT_texture_array);
186 call.name = "texture1DArray";
188 else if(sampler_type=="sampler2DArray")
190 check_extension(EXT_texture_array);
191 call.name = "texture2DArray";
193 else if(sampler_type=="sampler1DArrayShadow")
195 check_extension(EXT_texture_array);
196 call.name = "shadow1DArray";
198 else if(sampler_type=="sampler2DArrayShadow")
200 check_extension(EXT_texture_array);
201 call.name = "shadow2DArray";
206 TraversingVisitor::visit(call);
209 bool LegacyConverter::supports_interface_layouts() const
211 if(target_api==OPENGL_ES2)
212 return check_version(Version(3, 0));
213 else if(check_version(Version(3, 30)))
216 return check_extension(ARB_explicit_attrib_location);
219 bool LegacyConverter::supports_centroid_sampling() const
221 if(target_api==OPENGL_ES2)
222 return check_version(Version(3, 0));
223 else if(check_version(Version(1, 20)))
226 return check_extension(EXT_gpu_shader4);
229 bool LegacyConverter::supports_sample_sampling() const
231 if(target_api==OPENGL_ES2)
232 return check_version(Version(3, 20));
233 else if(check_version(Version(4, 0)))
236 return check_extension(ARB_gpu_shader5);
239 void LegacyConverter::visit(VariableDeclaration &var)
241 if(var.layout && !supports_interface_layouts())
243 vector<Layout::Qualifier>::iterator i;
244 for(i=var.layout->qualifiers.begin(); (i!=var.layout->qualifiers.end() && i->identifier!="location"); ++i) ;
245 if(i!=var.layout->qualifiers.end())
247 unsigned location = lexical_cast<unsigned>(i->value);
248 if(stage->type==Stage::VERTEX && var.interface=="in")
250 stage->locations[var.name] = location;
251 var.layout->qualifiers.erase(i);
253 else if(stage->type==Stage::FRAGMENT && var.interface=="out")
256 static Require _req(EXT_gpu_shader4);
257 stage->locations[var.name] = location;
258 var.layout->qualifiers.erase(i);
261 if(var.layout->qualifiers.empty())
266 if(var.sampling=="centroid")
268 if(!supports_centroid_sampling())
269 var.sampling = string();
271 else if(var.sampling=="sample")
273 if(!supports_sample_sampling())
274 var.sampling = string();
277 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
279 if(stage->type==Stage::FRAGMENT && var.interface=="out")
286 TraversingVisitor::visit(var);
289 bool LegacyConverter::supports_interface_blocks(const string &iface) const
291 if(target_api==OPENGL_ES2)
294 return check_version(Version(3, 0));
296 return check_version(Version(3, 20));
298 else if(check_version(Version(1, 50)))
300 else if(iface=="uniform")
301 return check_extension(ARB_uniform_buffer_object);
306 void LegacyConverter::visit(InterfaceBlock &iface)
308 if(!supports_interface_blocks(iface.interface))
309 flatten_block(iface.members);