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 StructOrganizer::StructOrganizer():
19 void StructOrganizer::visit(StructDeclaration &strct)
21 SetForScope<int> set_offset(offset, 0);
22 TraversingVisitor::visit(strct);
25 void StructOrganizer::visit(VariableDeclaration &var)
29 int *layout_offset = 0;
30 bool has_matrix_order = false;
33 vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
34 for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
36 if(i->name=="offset" && i->has_value)
38 layout_offset = &i->value;
42 else if(i->name=="column_major" || i->name=="row_major")
43 has_matrix_order = true;
47 MemoryRequirementsCalculator::Result mem_reqs = MemoryRequirementsCalculator().apply(var);
48 offset += mem_reqs.alignment-1;
49 offset -= offset%mem_reqs.alignment;
52 *layout_offset = offset;
56 var.layout = new Layout;
58 Layout::Qualifier qual;
60 qual.has_value = true;
62 var.layout->qualifiers.push_back(qual);
67 const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var.type_declaration);
68 while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
69 basic = dynamic_cast<const BasicTypeDeclaration *>(basic->base_type);
70 if(basic && basic->kind==BasicTypeDeclaration::MATRIX)
72 Layout::Qualifier qual;
73 qual.name = "column_major";
74 var.layout->qualifiers.push_back(qual);
78 offset += mem_reqs.size;
83 void LocationAllocator::apply(Module &module, const Features &features)
85 for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
87 allocate_locations("uniform");
89 for(vector<InterfaceBlock *>::const_iterator i=unbound_blocks.begin(); i!=unbound_blocks.end(); ++i)
90 bind_uniform((*i)->layout, (*i)->block_name, features.uniform_binding_range);
91 for(vector<VariableDeclaration *>::const_iterator i=unbound_textures.begin(); i!=unbound_textures.end(); ++i)
92 bind_uniform((*i)->layout, (*i)->name, features.texture_binding_range);
95 void LocationAllocator::apply(Stage &stage)
97 swap(used_locations["in"], used_locations["out"]);
98 used_locations["out"].clear();
100 stage.content.visit(*this);
102 allocate_locations("in");
103 allocate_locations("out");
106 void LocationAllocator::allocate_locations(const string &iface)
108 vector<VariableDeclaration *>::iterator write = unplaced_variables.begin();
110 for(vector<VariableDeclaration *>::const_iterator i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i)
112 if((*i)->interface!=iface)
120 if((*i)->interface=="uniform")
122 map<string, Uniform>::const_iterator j = uniforms.find((*i)->name);
123 if(j!=uniforms.end() && j->second.location>=0)
125 add_layout_value((*i)->layout, "location", j->second.location);
130 set<unsigned> &used = used_locations[(*i)->interface];
132 unsigned size = LocationCounter().apply(**i);
136 for(unsigned j=0; j<size; ++j)
137 if(used.count(next+j))
144 add_layout_value((*i)->layout, "location", next);
145 if((*i)->interface=="uniform")
146 uniforms[(*i)->name].location = next;
148 for(unsigned j=0; j<size; ++j)
153 unplaced_variables.erase(write, unplaced_variables.end());
156 void LocationAllocator::bind_uniform(RefPtr<Layout> &layout, const string &name, unsigned range)
158 map<string, Uniform>::const_iterator i = uniforms.find(name);
159 if(i!=uniforms.end() && i->second.bind_point>=0)
160 add_layout_value(layout, "binding", i->second.bind_point);
163 set<unsigned> &used = used_bindings[0];
165 unsigned bind_point = fold32(hash64(name))%range;
166 while(used.count(bind_point))
167 bind_point = (bind_point+1)%range;
169 add_layout_value(layout, "binding", bind_point);
170 uniforms[name].bind_point = bind_point;
171 used.insert(bind_point);
175 void LocationAllocator::add_layout_value(RefPtr<Layout> &layout, const string &name, unsigned value)
180 Layout::Qualifier qual;
182 qual.has_value = true;
184 layout->qualifiers.push_back(qual);
187 void LocationAllocator::visit(VariableDeclaration &var)
189 if(!var.name.compare(0, 3, "gl_"))
192 if(!var.interface.empty())
194 int location = (var.layout ? get_layout_value(*var.layout, "location") : -1);
196 if(location<0 && var.linked_declaration && var.linked_declaration->layout)
198 location = get_layout_value(*var.linked_declaration->layout, "location");
200 add_layout_value(var.layout, "location", location);
205 unsigned size = LocationCounter().apply(var);
206 for(unsigned i=0; i<size; ++i)
207 used_locations[var.interface].insert(location+i);
208 if(var.interface=="uniform")
209 uniforms[var.name].location = location;
212 unplaced_variables.push_back(&var);
215 if(var.interface=="uniform")
217 const TypeDeclaration *type = var.type_declaration;
218 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
219 type = basic->base_type;
220 if(dynamic_cast<const ImageTypeDeclaration *>(type))
222 int bind_point = (var.layout ? get_layout_value(*var.layout, "binding") : -1);
225 used_bindings[0].insert(bind_point);
226 uniforms[var.name].bind_point = bind_point;
229 unbound_textures.push_back(&var);
234 void LocationAllocator::visit(InterfaceBlock &iface)
236 if(!iface.instance_name.compare(0, 3, "gl_"))
239 if(iface.interface=="uniform")
241 int bind_point = (iface.layout ? get_layout_value(*iface.layout, "binding") : -1);
245 used_bindings[0].insert(bind_point);
246 uniforms[iface.block_name].bind_point = bind_point;
249 unbound_blocks.push_back(&iface);
254 PrecisionConverter::PrecisionConverter():
258 void PrecisionConverter::apply(Stage &s)
261 s.content.visit(*this);
262 NodeRemover().apply(s, nodes_to_remove);
265 void PrecisionConverter::visit(Block &block)
267 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
269 if(&block==&stage->content)
275 void PrecisionConverter::visit(Precision &prec)
277 if(stage->required_features.gl_api==OPENGL_ES2)
278 have_default.insert(prec.type);
280 nodes_to_remove.insert(&prec);
283 void PrecisionConverter::visit(VariableDeclaration &var)
285 if(stage->required_features.gl_api!=OPENGL_ES2)
287 var.precision.clear();
291 const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
292 const TypeDeclaration *type = var.type_declaration;
295 if(dynamic_cast<const ImageTypeDeclaration *>(type))
297 default_prec = "lowp";
300 else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
302 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
304 type = basic->base_type;
312 if(!have_default.count(type->name))
314 Precision *prec = new Precision;
315 prec->precision = default_prec;
316 prec->type = type->name;
317 stage->content.body.insert(insert_point, prec);
319 have_default.insert(type->name);
324 LegacyConverter::LegacyConverter():
328 void LegacyConverter::apply(Stage &s, const Features &feat)
332 if(supports_stage(s.type))
334 s.content.visit(*this);
335 NodeRemover().apply(s, nodes_to_remove);
337 if(!stage->required_features.glsl_version)
338 stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
341 unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
344 void LegacyConverter::unsupported(const string &reason)
346 Diagnostic diagnostic;
347 diagnostic.severity = Diagnostic::ERR;
348 diagnostic.source = GENERATED_SOURCE;
350 diagnostic.message = reason;
351 stage->diagnostics.push_back(diagnostic);
354 void LegacyConverter::visit(Block &block)
356 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
358 if(&block==&stage->content)
359 uniform_insert_point = i;
364 bool LegacyConverter::check_version(const Version &feature_version) const
366 if(features.glsl_version<feature_version)
368 else if(stage->required_features.glsl_version<feature_version)
369 stage->required_features.glsl_version = feature_version;
374 bool LegacyConverter::check_extension(bool Features::*extension) const
376 if(!(features.*extension))
379 stage->required_features.*extension = true;
384 bool LegacyConverter::supports_stage(Stage::Type st) const
386 if(st==Stage::GEOMETRY)
388 if(features.gl_api==OPENGL_ES2)
389 return check_version(Version(3, 20));
391 return check_version(Version(1, 50));
397 bool LegacyConverter::supports_unified_interface_syntax() const
399 if(features.gl_api==OPENGL_ES2)
400 return check_version(Version(3, 0));
402 return check_version(Version(1, 30));
405 void LegacyConverter::visit(VariableReference &var)
407 if(var.declaration==frag_out && !supports_unified_interface_syntax())
409 var.name = "gl_FragColor";
414 void LegacyConverter::visit(Assignment &assign)
416 TraversingVisitor::visit(assign);
417 if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
418 assign.target.declaration = 0;
421 bool LegacyConverter::supports_unified_sampling_functions() const
423 if(features.gl_api==OPENGL_ES2)
424 return check_version(Version(3, 0));
426 return check_version(Version(1, 30));
429 void LegacyConverter::visit(FunctionCall &call)
431 if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
433 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
435 const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
436 if(arg_image && !supports_unified_sampling_functions())
438 string suffix = call.name.substr(7);
439 call.name = (arg_image->shadow ? "shadow" : "texture");
441 switch(arg_image->dimensions)
443 case ImageTypeDeclaration::ONE: call.name += "1D"; break;
444 case ImageTypeDeclaration::TWO: call.name += "2D"; break;
445 case ImageTypeDeclaration::THREE: call.name += "3D"; break;
446 case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
451 /* Array textures and the unified sampling function name were
452 both introduced in GLSL 1.30. */
453 if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
454 check_extension(&Features::ext_texture_array);
455 call.name += "Array";
463 TraversingVisitor::visit(call);
466 bool LegacyConverter::supports_interface_layouts() const
468 if(features.gl_api==OPENGL_ES2)
469 return check_version(Version(3, 0));
470 else if(check_version(Version(3, 30)))
472 else if(check_version(Version(1, 30)))
473 return check_extension(&Features::arb_explicit_attrib_location);
478 bool LegacyConverter::supports_stage_interface_layouts() const
480 if(features.gl_api==OPENGL_ES2)
481 return check_version(Version(3, 10));
482 else if(check_version(Version(4, 10)))
485 return check_extension(&Features::arb_separate_shader_objects);
488 bool LegacyConverter::supports_centroid_sampling() const
490 if(features.gl_api==OPENGL_ES2)
491 return check_version(Version(3, 0));
492 else if(check_version(Version(1, 20)))
495 return check_extension(&Features::ext_gpu_shader4);
498 bool LegacyConverter::supports_sample_sampling() const
500 if(features.gl_api==OPENGL_ES2)
501 return check_version(Version(3, 20));
502 else if(check_version(Version(4, 0)))
505 return check_extension(&Features::arb_gpu_shader5);
508 bool LegacyConverter::supports_uniform_location() const
510 if(features.gl_api==OPENGL_ES2)
511 return check_version(Version(3, 10));
512 else if(check_version(Version(4, 30)))
515 return check_extension(&Features::arb_explicit_uniform_location);
518 bool LegacyConverter::supports_binding() const
520 if(features.gl_api==OPENGL_ES2)
521 return check_version(Version(3, 10));
523 return check_version(Version(4, 20));
526 void LegacyConverter::visit(VariableDeclaration &var)
530 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
532 if(i->name=="location")
534 bool supported = true;
535 bool external = false;
536 if(var.interface=="in")
538 external = (stage->type==Stage::VERTEX);
539 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
541 else if(var.interface=="out")
543 external = (stage->type==Stage::FRAGMENT);
544 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
545 if(external && !supported && !check_extension(&Features::ext_gpu_shader4))
549 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
552 else if(var.interface=="uniform")
553 supported = supports_uniform_location();
558 stage->locations[var.name] = i->value;
559 i = var.layout->qualifiers.erase(i);
564 else if(i->name=="binding" && !supports_binding())
566 const TypeDeclaration *type = var.type_declaration;
567 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
568 type = basic->base_type;
569 if(dynamic_cast<const ImageTypeDeclaration *>(type))
570 stage->texture_bindings[var.name] = i->value;
572 i = var.layout->qualifiers.erase(i);
578 if(var.layout->qualifiers.empty())
582 if(var.sampling=="centroid")
584 if(!supports_centroid_sampling())
585 var.sampling = string();
587 else if(var.sampling=="sample")
589 if(!supports_sample_sampling())
590 var.sampling = string();
593 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
595 if(stage->type==Stage::FRAGMENT && var.interface=="out")
598 nodes_to_remove.insert(&var);
602 TraversingVisitor::visit(var);
605 bool LegacyConverter::supports_interface_blocks(const string &iface) const
607 if(features.gl_api==OPENGL_ES2)
610 return check_version(Version(3, 0));
612 return check_version(Version(3, 20));
614 else if(check_version(Version(1, 50)))
616 else if(iface=="uniform")
617 return check_extension(&Features::arb_uniform_buffer_object);
622 bool LegacyConverter::supports_interface_block_location() const
624 if(features.gl_api==OPENGL_ES2)
625 return check_version(Version(3, 20));
626 else if(check_version(Version(4, 40)))
629 return check_extension(&Features::arb_enhanced_layouts);
632 void LegacyConverter::visit(InterfaceBlock &iface)
636 for(vector<Layout::Qualifier>::const_iterator i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
638 if(i->name=="location" && !supports_interface_block_location())
639 i = iface.layout->qualifiers.erase(i);
640 else if(i->name=="binding" && !supports_binding())
642 stage->uniform_block_bindings[iface.block_name] = i->value;
643 i = iface.layout->qualifiers.erase(i);
649 if(iface.layout->qualifiers.empty())
653 if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
655 if(!iface.instance_name.empty())
656 unsupported("ARB_uniform_buffer_object required for interface block instances");
657 else if(iface.struct_declaration)
659 stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
660 nodes_to_remove.insert(&iface);
661 nodes_to_remove.insert(iface.struct_declaration);
664 /* If the interface block is an array, it should have an instance
665 name too, so this should never be reached */
666 throw logic_error("Unexpected interface block configuration");