1 #include <msp/core/algorithm.h>
2 #include <msp/core/hash.h>
3 #include <msp/core/raii.h>
4 #include <msp/strings/lexicalcast.h>
6 #include "glsl_error.h"
15 void LocationAllocator::apply(Module &module, const Features &features)
17 for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
19 allocate_locations("uniform");
21 for(vector<InterfaceBlock *>::const_iterator i=unbound_blocks.begin(); i!=unbound_blocks.end(); ++i)
22 bind_uniform((*i)->layout, (*i)->block_name, features.uniform_binding_range);
23 for(vector<VariableDeclaration *>::const_iterator i=unbound_textures.begin(); i!=unbound_textures.end(); ++i)
24 bind_uniform((*i)->layout, (*i)->name, features.texture_binding_range);
27 void LocationAllocator::apply(Stage &stage)
29 swap(used_locations["in"], used_locations["out"]);
30 used_locations["out"].clear();
32 stage.content.visit(*this);
34 allocate_locations("in");
35 allocate_locations("out");
38 void LocationAllocator::allocate_locations(const string &iface)
40 vector<VariableDeclaration *>::iterator write = unplaced_variables.begin();
42 for(vector<VariableDeclaration *>::const_iterator i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i)
44 if((*i)->interface!=iface)
52 if((*i)->interface=="uniform")
54 map<string, Uniform>::const_iterator j = uniforms.find((*i)->name);
55 if(j!=uniforms.end() && j->second.location>=0)
57 add_layout_value((*i)->layout, "location", j->second.location);
62 set<unsigned> &used = used_locations[(*i)->interface];
64 unsigned size = LocationCounter().apply(**i);
68 for(unsigned j=0; j<size; ++j)
69 if(used.count(next+j))
76 add_layout_value((*i)->layout, "location", next);
77 if((*i)->interface=="uniform")
78 uniforms[(*i)->name].location = next;
80 for(unsigned j=0; j<size; ++j)
85 unplaced_variables.erase(write, unplaced_variables.end());
88 void LocationAllocator::bind_uniform(RefPtr<Layout> &layout, const string &name, unsigned range)
90 map<string, Uniform>::const_iterator i = uniforms.find(name);
91 if(i!=uniforms.end() && i->second.bind_point>=0)
92 add_layout_value(layout, "binding", i->second.bind_point);
95 set<unsigned> &used = used_bindings[0];
97 unsigned bind_point = fold32(hash64(name))%range;
98 while(used.count(bind_point))
99 bind_point = (bind_point+1)%range;
101 add_layout_value(layout, "binding", bind_point);
102 uniforms[name].bind_point = bind_point;
103 used.insert(bind_point);
107 void LocationAllocator::add_layout_value(RefPtr<Layout> &layout, const string &name, unsigned value)
112 Layout::Qualifier qual;
114 qual.has_value = true;
116 layout->qualifiers.push_back(qual);
119 void LocationAllocator::visit(VariableDeclaration &var)
121 if(!var.name.compare(0, 3, "gl_"))
124 if(!var.interface.empty())
126 int location = (var.layout ? get_layout_value(*var.layout, "location") : -1);
128 if(location<0 && var.linked_declaration && var.linked_declaration->layout)
130 location = get_layout_value(*var.linked_declaration->layout, "location");
132 add_layout_value(var.layout, "location", location);
137 unsigned size = LocationCounter().apply(var);
138 for(unsigned i=0; i<size; ++i)
139 used_locations[var.interface].insert(location+i);
140 if(var.interface=="uniform")
141 uniforms[var.name].location = location;
144 unplaced_variables.push_back(&var);
147 if(var.interface=="uniform")
149 const TypeDeclaration *type = var.type_declaration;
150 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
151 type = basic->base_type;
152 if(dynamic_cast<const ImageTypeDeclaration *>(type))
154 int bind_point = (var.layout ? get_layout_value(*var.layout, "binding") : -1);
157 used_bindings[0].insert(bind_point);
158 uniforms[var.name].bind_point = bind_point;
161 unbound_textures.push_back(&var);
166 void LocationAllocator::visit(InterfaceBlock &iface)
168 if(!iface.instance_name.compare(0, 3, "gl_"))
171 if(iface.interface=="uniform")
173 int bind_point = (iface.layout ? get_layout_value(*iface.layout, "binding") : -1);
177 used_bindings[0].insert(bind_point);
178 uniforms[iface.block_name].bind_point = bind_point;
181 unbound_blocks.push_back(&iface);
186 PrecisionConverter::PrecisionConverter():
190 void PrecisionConverter::apply(Stage &s)
193 s.content.visit(*this);
194 NodeRemover().apply(s, nodes_to_remove);
197 void PrecisionConverter::visit(Block &block)
199 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
201 if(&block==&stage->content)
207 void PrecisionConverter::visit(Precision &prec)
209 if(stage->required_features.gl_api==OPENGL_ES2)
210 have_default.insert(prec.type);
212 nodes_to_remove.insert(&prec);
215 void PrecisionConverter::visit(VariableDeclaration &var)
217 if(stage->required_features.gl_api!=OPENGL_ES2)
219 var.precision.clear();
223 const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
224 const TypeDeclaration *type = var.type_declaration;
227 if(dynamic_cast<const ImageTypeDeclaration *>(type))
229 default_prec = "lowp";
232 else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
234 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
236 type = basic->base_type;
244 if(!have_default.count(type->name))
246 Precision *prec = new Precision;
247 prec->precision = default_prec;
248 prec->type = type->name;
249 stage->content.body.insert(insert_point, prec);
251 have_default.insert(type->name);
256 LegacyConverter::LegacyConverter():
260 void LegacyConverter::apply(Stage &s, const Features &feat)
264 if(supports_stage(s.type))
266 s.content.visit(*this);
267 NodeRemover().apply(s, nodes_to_remove);
269 if(!stage->required_features.glsl_version)
270 stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
273 unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
276 void LegacyConverter::unsupported(const string &reason)
278 Diagnostic diagnostic;
279 diagnostic.severity = Diagnostic::ERR;
280 diagnostic.source = GENERATED_SOURCE;
282 diagnostic.message = reason;
283 stage->diagnostics.push_back(diagnostic);
286 void LegacyConverter::visit(Block &block)
288 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
290 if(&block==&stage->content)
291 uniform_insert_point = i;
296 bool LegacyConverter::check_version(const Version &feature_version) const
298 if(features.glsl_version<feature_version)
300 else if(stage->required_features.glsl_version<feature_version)
301 stage->required_features.glsl_version = feature_version;
306 bool LegacyConverter::check_extension(bool Features::*extension) const
308 if(!(features.*extension))
311 stage->required_features.*extension = true;
316 bool LegacyConverter::supports_stage(Stage::Type st) const
318 if(st==Stage::GEOMETRY)
320 if(features.gl_api==OPENGL_ES2)
321 return check_version(Version(3, 20));
323 return check_version(Version(1, 50));
329 bool LegacyConverter::supports_unified_interface_syntax() const
331 if(features.gl_api==OPENGL_ES2)
332 return check_version(Version(3, 0));
334 return check_version(Version(1, 30));
337 void LegacyConverter::visit(VariableReference &var)
339 if(var.declaration==frag_out && !supports_unified_interface_syntax())
341 var.name = "gl_FragColor";
346 void LegacyConverter::visit(Assignment &assign)
348 TraversingVisitor::visit(assign);
349 if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
350 assign.target.declaration = 0;
353 bool LegacyConverter::supports_unified_sampling_functions() const
355 if(features.gl_api==OPENGL_ES2)
356 return check_version(Version(3, 0));
358 return check_version(Version(1, 30));
361 void LegacyConverter::visit(FunctionCall &call)
363 if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
365 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
367 const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
368 if(arg_image && !supports_unified_sampling_functions())
370 string suffix = call.name.substr(7);
371 call.name = (arg_image->shadow ? "shadow" : "texture");
373 switch(arg_image->dimensions)
375 case ImageTypeDeclaration::ONE: call.name += "1D"; break;
376 case ImageTypeDeclaration::TWO: call.name += "2D"; break;
377 case ImageTypeDeclaration::THREE: call.name += "3D"; break;
378 case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
383 /* Array textures and the unified sampling function name were
384 both introduced in GLSL 1.30. */
385 if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
386 check_extension(&Features::ext_texture_array);
387 call.name += "Array";
395 TraversingVisitor::visit(call);
398 bool LegacyConverter::supports_interface_layouts() const
400 if(features.gl_api==OPENGL_ES2)
401 return check_version(Version(3, 0));
402 else if(check_version(Version(3, 30)))
404 else if(check_version(Version(1, 30)))
405 return check_extension(&Features::arb_explicit_attrib_location);
410 bool LegacyConverter::supports_stage_interface_layouts() const
412 if(features.gl_api==OPENGL_ES2)
413 return check_version(Version(3, 10));
414 else if(check_version(Version(4, 10)))
417 return check_extension(&Features::arb_separate_shader_objects);
420 bool LegacyConverter::supports_centroid_sampling() const
422 if(features.gl_api==OPENGL_ES2)
423 return check_version(Version(3, 0));
424 else if(check_version(Version(1, 20)))
427 return check_extension(&Features::ext_gpu_shader4);
430 bool LegacyConverter::supports_sample_sampling() const
432 if(features.gl_api==OPENGL_ES2)
433 return check_version(Version(3, 20));
434 else if(check_version(Version(4, 0)))
437 return check_extension(&Features::arb_gpu_shader5);
440 bool LegacyConverter::supports_uniform_location() const
442 if(features.gl_api==OPENGL_ES2)
443 return check_version(Version(3, 10));
444 else if(check_version(Version(4, 30)))
447 return check_extension(&Features::arb_explicit_uniform_location);
450 bool LegacyConverter::supports_binding() const
452 if(features.gl_api==OPENGL_ES2)
453 return check_version(Version(3, 10));
455 return check_version(Version(4, 20));
458 void LegacyConverter::visit(VariableDeclaration &var)
462 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
464 if(i->name=="location")
466 bool supported = true;
467 bool external = false;
468 if(var.interface=="in")
470 external = (stage->type==Stage::VERTEX);
471 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
473 else if(var.interface=="out")
475 external = (stage->type==Stage::FRAGMENT);
476 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
477 if(external && !supported && !check_extension(&Features::ext_gpu_shader4))
481 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
484 else if(var.interface=="uniform")
485 supported = supports_uniform_location();
490 stage->locations[var.name] = i->value;
491 i = var.layout->qualifiers.erase(i);
496 else if(i->name=="binding" && !supports_binding())
498 const TypeDeclaration *type = var.type_declaration;
499 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
500 type = basic->base_type;
501 if(dynamic_cast<const ImageTypeDeclaration *>(type))
502 stage->texture_bindings[var.name] = i->value;
504 i = var.layout->qualifiers.erase(i);
510 if(var.layout->qualifiers.empty())
514 if(var.sampling=="centroid")
516 if(!supports_centroid_sampling())
517 var.sampling = string();
519 else if(var.sampling=="sample")
521 if(!supports_sample_sampling())
522 var.sampling = string();
525 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
527 if(stage->type==Stage::FRAGMENT && var.interface=="out")
530 nodes_to_remove.insert(&var);
534 TraversingVisitor::visit(var);
537 bool LegacyConverter::supports_interface_blocks(const string &iface) const
539 if(features.gl_api==OPENGL_ES2)
542 return check_version(Version(3, 0));
544 return check_version(Version(3, 20));
546 else if(check_version(Version(1, 50)))
548 else if(iface=="uniform")
549 return check_extension(&Features::arb_uniform_buffer_object);
554 bool LegacyConverter::supports_interface_block_location() const
556 if(features.gl_api==OPENGL_ES2)
557 return check_version(Version(3, 20));
558 else if(check_version(Version(4, 40)))
561 return check_extension(&Features::arb_enhanced_layouts);
564 void LegacyConverter::visit(InterfaceBlock &iface)
568 for(vector<Layout::Qualifier>::const_iterator i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
570 if(i->name=="location" && !supports_interface_block_location())
571 i = iface.layout->qualifiers.erase(i);
572 else if(i->name=="binding" && !supports_binding())
574 stage->uniform_block_bindings[iface.block_name] = i->value;
575 i = iface.layout->qualifiers.erase(i);
581 if(iface.layout->qualifiers.empty())
585 if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
587 if(!iface.instance_name.empty())
588 unsupported("ARB_uniform_buffer_object required for interface block instances");
589 else if(iface.struct_declaration)
591 stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
592 nodes_to_remove.insert(&iface);
593 nodes_to_remove.insert(iface.struct_declaration);
596 /* If the interface block is an array, it should have an instance
597 name too, so this should never be reached */
598 throw logic_error("Unexpected interface block configuration");