1 #include <msp/core/algorithm.h>
2 #include <msp/core/raii.h>
3 #include <msp/strings/lexicalcast.h>
5 #include "glsl_error.h"
14 void LocationAllocator::apply(Module &module)
16 for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
18 allocate_locations("uniform");
21 void LocationAllocator::apply(Stage &stage)
23 swap(used_locations["in"], used_locations["out"]);
24 used_locations["out"].clear();
26 stage.content.visit(*this);
28 allocate_locations("in");
29 allocate_locations("out");
32 void LocationAllocator::allocate_locations(const string &iface)
34 vector<VariableDeclaration *>::iterator write = unplaced_variables.begin();
36 for(vector<VariableDeclaration *>::const_iterator i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i)
38 if((*i)->interface!=iface)
46 if((*i)->interface=="uniform")
48 map<string, unsigned>::const_iterator j = uniform_locations.find((*i)->name);
49 if(j!=uniform_locations.end())
51 add_location((*i)->layout, j->second);
56 set<unsigned> &used = used_locations[(*i)->interface];
58 unsigned size = LocationCounter().apply(**i);
62 for(unsigned j=0; j<size; ++j)
63 if(used.count(next+j))
70 add_location((*i)->layout, next);
71 if((*i)->interface=="uniform")
72 uniform_locations[(*i)->name] = next;
74 for(unsigned j=0; j<size; ++j)
79 unplaced_variables.erase(write, unplaced_variables.end());
82 void LocationAllocator::add_location(RefPtr<Layout> &layout, unsigned location)
87 Layout::Qualifier qual;
88 qual.name = "location";
89 qual.has_value = true;
90 qual.value = location;
91 layout->qualifiers.push_back(qual);
94 void LocationAllocator::visit(VariableDeclaration &var)
96 if(!var.interface.empty() && var.name.compare(0, 3, "gl_"))
98 int location = (var.layout ? get_layout_value(*var.layout, "location") : -1);
100 if(location<0 && var.linked_declaration && var.linked_declaration->layout)
102 location = get_layout_value(*var.linked_declaration->layout, "location");
104 add_location(var.layout, location);
109 unsigned size = LocationCounter().apply(var);
110 for(unsigned i=0; i<size; ++i)
111 used_locations[var.interface].insert(location+i);
112 if(var.interface=="uniform")
113 uniform_locations[var.name] = location;
116 unplaced_variables.push_back(&var);
121 PrecisionConverter::PrecisionConverter():
125 void PrecisionConverter::apply(Stage &s)
128 s.content.visit(*this);
129 NodeRemover().apply(s, nodes_to_remove);
132 void PrecisionConverter::visit(Block &block)
134 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
136 if(&block==&stage->content)
142 void PrecisionConverter::visit(Precision &prec)
144 if(stage->required_features.gl_api==OPENGL_ES2)
145 have_default.insert(prec.type);
147 nodes_to_remove.insert(&prec);
150 void PrecisionConverter::visit(VariableDeclaration &var)
152 if(stage->required_features.gl_api!=OPENGL_ES2)
154 var.precision.clear();
158 const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
159 const TypeDeclaration *type = var.type_declaration;
162 if(dynamic_cast<const ImageTypeDeclaration *>(type))
164 default_prec = "lowp";
167 else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
169 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
171 type = basic->base_type;
179 if(!have_default.count(type->name))
181 Precision *prec = new Precision;
182 prec->precision = default_prec;
183 prec->type = type->name;
184 stage->content.body.insert(insert_point, prec);
186 have_default.insert(type->name);
191 LegacyConverter::LegacyConverter():
195 void LegacyConverter::apply(Stage &s, const Features &feat)
199 if(supports_stage(s.type))
201 s.content.visit(*this);
202 NodeRemover().apply(s, nodes_to_remove);
204 if(!stage->required_features.glsl_version)
205 stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
208 unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
211 void LegacyConverter::unsupported(const string &reason)
213 Diagnostic diagnostic;
214 diagnostic.severity = Diagnostic::ERR;
215 diagnostic.source = GENERATED_SOURCE;
217 diagnostic.message = reason;
218 stage->diagnostics.push_back(diagnostic);
221 void LegacyConverter::visit(Block &block)
223 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
225 if(&block==&stage->content)
226 uniform_insert_point = i;
231 bool LegacyConverter::check_version(const Version &feature_version) const
233 if(features.glsl_version<feature_version)
235 else if(stage->required_features.glsl_version<feature_version)
236 stage->required_features.glsl_version = feature_version;
241 bool LegacyConverter::check_extension(bool Features::*extension) const
243 if(!(features.*extension))
246 stage->required_features.*extension = true;
251 bool LegacyConverter::supports_stage(Stage::Type st) const
253 if(st==Stage::GEOMETRY)
255 if(features.gl_api==OPENGL_ES2)
256 return check_version(Version(3, 20));
258 return check_version(Version(1, 50));
264 bool LegacyConverter::supports_unified_interface_syntax() const
266 if(features.gl_api==OPENGL_ES2)
267 return check_version(Version(3, 0));
269 return check_version(Version(1, 30));
272 void LegacyConverter::visit(VariableReference &var)
274 if(var.declaration==frag_out && !supports_unified_interface_syntax())
276 var.name = "gl_FragColor";
281 void LegacyConverter::visit(Assignment &assign)
283 TraversingVisitor::visit(assign);
284 if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
285 assign.target.declaration = 0;
288 bool LegacyConverter::supports_unified_sampling_functions() const
290 if(features.gl_api==OPENGL_ES2)
291 return check_version(Version(3, 0));
293 return check_version(Version(1, 30));
296 void LegacyConverter::visit(FunctionCall &call)
298 if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
300 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
302 const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
303 if(arg_image && !supports_unified_sampling_functions())
305 string suffix = call.name.substr(7);
306 call.name = (arg_image->shadow ? "shadow" : "texture");
308 switch(arg_image->dimensions)
310 case ImageTypeDeclaration::ONE: call.name += "1D"; break;
311 case ImageTypeDeclaration::TWO: call.name += "2D"; break;
312 case ImageTypeDeclaration::THREE: call.name += "3D"; break;
313 case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
318 /* Array textures and the unified sampling function name were
319 both introduced in GLSL 1.30. */
320 if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
321 check_extension(&Features::ext_texture_array);
322 call.name += "Array";
330 TraversingVisitor::visit(call);
333 bool LegacyConverter::supports_interface_layouts() const
335 if(features.gl_api==OPENGL_ES2)
336 return check_version(Version(3, 0));
337 else if(check_version(Version(3, 30)))
339 else if(check_version(Version(1, 30)))
340 return check_extension(&Features::arb_explicit_attrib_location);
345 bool LegacyConverter::supports_stage_interface_layouts() const
347 if(features.gl_api==OPENGL_ES2)
348 return check_version(Version(3, 10));
349 else if(check_version(Version(4, 10)))
352 return check_extension(&Features::arb_separate_shader_objects);
355 bool LegacyConverter::supports_centroid_sampling() const
357 if(features.gl_api==OPENGL_ES2)
358 return check_version(Version(3, 0));
359 else if(check_version(Version(1, 20)))
362 return check_extension(&Features::ext_gpu_shader4);
365 bool LegacyConverter::supports_sample_sampling() const
367 if(features.gl_api==OPENGL_ES2)
368 return check_version(Version(3, 20));
369 else if(check_version(Version(4, 0)))
372 return check_extension(&Features::arb_gpu_shader5);
375 bool LegacyConverter::supports_uniform_location() const
377 if(features.gl_api==OPENGL_ES2)
378 return check_version(Version(3, 10));
379 else if(check_version(Version(4, 30)))
382 return check_extension(&Features::arb_explicit_uniform_location);
385 void LegacyConverter::visit(VariableDeclaration &var)
389 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
391 if(i->name=="location")
393 bool supported = true;
394 bool external = false;
395 if(var.interface=="in")
397 external = (stage->type==Stage::VERTEX);
398 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
400 else if(var.interface=="out")
402 external = (stage->type==Stage::FRAGMENT);
403 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
404 if(external && !supported && !check_extension(&Features::ext_gpu_shader4))
408 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
411 else if(var.interface=="uniform")
412 supported = supports_uniform_location();
417 stage->locations[var.name] = i->value;
418 i = var.layout->qualifiers.erase(i);
427 if(var.layout->qualifiers.empty())
431 if(var.sampling=="centroid")
433 if(!supports_centroid_sampling())
434 var.sampling = string();
436 else if(var.sampling=="sample")
438 if(!supports_sample_sampling())
439 var.sampling = string();
442 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
444 if(stage->type==Stage::FRAGMENT && var.interface=="out")
447 nodes_to_remove.insert(&var);
451 TraversingVisitor::visit(var);
454 bool LegacyConverter::supports_interface_blocks(const string &iface) const
456 if(features.gl_api==OPENGL_ES2)
459 return check_version(Version(3, 0));
461 return check_version(Version(3, 20));
463 else if(check_version(Version(1, 50)))
465 else if(iface=="uniform")
466 return check_extension(&Features::arb_uniform_buffer_object);
471 bool LegacyConverter::supports_interface_block_location() const
473 if(features.gl_api==OPENGL_ES2)
474 return check_version(Version(3, 20));
475 else if(check_version(Version(4, 40)))
478 return check_extension(&Features::arb_enhanced_layouts);
481 void LegacyConverter::visit(InterfaceBlock &iface)
485 for(vector<Layout::Qualifier>::const_iterator i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
487 if(i->name=="location" && !supports_interface_block_location())
488 i = iface.layout->qualifiers.erase(i);
493 if(iface.layout->qualifiers.empty())
497 if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
499 if(!iface.instance_name.empty())
500 unsupported("ARB_uniform_buffer_object required for interface block instances");
501 else if(iface.struct_declaration)
503 stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
504 nodes_to_remove.insert(&iface);
505 nodes_to_remove.insert(iface.struct_declaration);
508 /* If the interface block is an array, it should have an instance
509 name too, so this should never be reached */
510 throw logic_error("Unexpected interface block configuration");