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 var.layout->qualifiers.push_back(Layout::Qualifier("offset", offset));
63 const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var.type_declaration);
64 while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
65 basic = dynamic_cast<const BasicTypeDeclaration *>(basic->base_type);
66 if(basic && basic->kind==BasicTypeDeclaration::MATRIX)
67 var.layout->qualifiers.push_back(Layout::Qualifier("column_major"));
70 offset += mem_reqs.size;
75 void LocationAllocator::apply(Module &module, const Features &features)
77 for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
79 allocate_locations("uniform");
81 for(vector<InterfaceBlock *>::const_iterator i=unbound_blocks.begin(); i!=unbound_blocks.end(); ++i)
82 bind_uniform((*i)->layout, (*i)->block_name, features.uniform_binding_range);
83 for(vector<VariableDeclaration *>::const_iterator i=unbound_textures.begin(); i!=unbound_textures.end(); ++i)
84 bind_uniform((*i)->layout, (*i)->name, features.texture_binding_range);
87 void LocationAllocator::apply(Stage &stage)
89 swap(used_locations["in"], used_locations["out"]);
90 used_locations["out"].clear();
92 stage.content.visit(*this);
94 allocate_locations("in");
95 allocate_locations("out");
98 void LocationAllocator::allocate_locations(const string &iface)
100 vector<VariableDeclaration *>::iterator write = unplaced_variables.begin();
102 for(vector<VariableDeclaration *>::const_iterator i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i)
104 if((*i)->interface!=iface)
112 if((*i)->interface=="uniform")
114 map<string, Uniform>::const_iterator j = uniforms.find((*i)->name);
115 if(j!=uniforms.end() && j->second.location>=0)
117 add_layout_value((*i)->layout, "location", j->second.location);
122 set<unsigned> &used = used_locations[(*i)->interface];
124 unsigned size = LocationCounter().apply(**i);
128 for(unsigned j=0; j<size; ++j)
129 if(used.count(next+j))
136 add_layout_value((*i)->layout, "location", next);
137 if((*i)->interface=="uniform")
138 uniforms[(*i)->name].location = next;
140 for(unsigned j=0; j<size; ++j)
145 unplaced_variables.erase(write, unplaced_variables.end());
148 void LocationAllocator::bind_uniform(RefPtr<Layout> &layout, const string &name, unsigned range)
150 map<string, Uniform>::const_iterator i = uniforms.find(name);
151 if(i!=uniforms.end() && i->second.bind_point>=0)
152 add_layout_value(layout, "binding", i->second.bind_point);
155 set<unsigned> &used = used_bindings[0];
157 unsigned bind_point = fold32(hash64(name))%range;
158 while(used.count(bind_point))
159 bind_point = (bind_point+1)%range;
161 add_layout_value(layout, "binding", bind_point);
162 uniforms[name].bind_point = bind_point;
163 used.insert(bind_point);
167 void LocationAllocator::add_layout_value(RefPtr<Layout> &layout, const string &name, unsigned value)
172 layout->qualifiers.push_back(Layout::Qualifier(name, value));
175 void LocationAllocator::visit(VariableDeclaration &var)
177 if(!var.name.compare(0, 3, "gl_"))
180 if(!var.interface.empty())
182 int location = (var.layout ? get_layout_value(*var.layout, "location") : -1);
184 if(location<0 && var.linked_declaration && var.linked_declaration->layout)
186 location = get_layout_value(*var.linked_declaration->layout, "location");
188 add_layout_value(var.layout, "location", location);
193 unsigned size = LocationCounter().apply(var);
194 for(unsigned i=0; i<size; ++i)
195 used_locations[var.interface].insert(location+i);
196 if(var.interface=="uniform")
197 uniforms[var.name].location = location;
200 unplaced_variables.push_back(&var);
203 if(var.interface=="uniform")
205 const TypeDeclaration *type = var.type_declaration;
206 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
207 type = basic->base_type;
208 if(dynamic_cast<const ImageTypeDeclaration *>(type))
210 int bind_point = (var.layout ? get_layout_value(*var.layout, "binding") : -1);
213 used_bindings[0].insert(bind_point);
214 uniforms[var.name].bind_point = bind_point;
217 unbound_textures.push_back(&var);
222 void LocationAllocator::visit(InterfaceBlock &iface)
224 if(!iface.instance_name.compare(0, 3, "gl_"))
227 if(iface.interface=="uniform")
229 int bind_point = (iface.layout ? get_layout_value(*iface.layout, "binding") : -1);
233 used_bindings[0].insert(bind_point);
234 uniforms[iface.block_name].bind_point = bind_point;
237 unbound_blocks.push_back(&iface);
242 PrecisionConverter::PrecisionConverter():
246 void PrecisionConverter::apply(Stage &s)
249 s.content.visit(*this);
250 NodeRemover().apply(s, nodes_to_remove);
253 void PrecisionConverter::visit(Block &block)
255 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
257 if(&block==&stage->content)
263 void PrecisionConverter::visit(Precision &prec)
265 if(stage->required_features.gl_api==OPENGL_ES2)
266 have_default.insert(prec.type);
268 nodes_to_remove.insert(&prec);
271 void PrecisionConverter::visit(VariableDeclaration &var)
273 if(stage->required_features.gl_api!=OPENGL_ES2)
275 var.precision.clear();
279 const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
280 const TypeDeclaration *type = var.type_declaration;
283 if(dynamic_cast<const ImageTypeDeclaration *>(type))
285 default_prec = "lowp";
288 else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
290 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
292 type = basic->base_type;
300 if(!have_default.count(type->name))
302 Precision *prec = new Precision;
303 prec->precision = default_prec;
304 prec->type = type->name;
305 stage->content.body.insert(insert_point, prec);
307 have_default.insert(type->name);
312 LegacyConverter::LegacyConverter():
316 void LegacyConverter::apply(Stage &s, const Features &feat)
320 if(supports_stage(s.type))
322 s.content.visit(*this);
323 NodeRemover().apply(s, nodes_to_remove);
325 if(!stage->required_features.glsl_version)
326 stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
329 unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
332 void LegacyConverter::unsupported(const string &reason)
334 Diagnostic diagnostic;
335 diagnostic.severity = Diagnostic::ERR;
336 diagnostic.source = GENERATED_SOURCE;
338 diagnostic.message = reason;
339 stage->diagnostics.push_back(diagnostic);
342 void LegacyConverter::visit(Block &block)
344 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
346 if(&block==&stage->content)
347 uniform_insert_point = i;
352 bool LegacyConverter::check_version(const Version &feature_version) const
354 if(features.glsl_version<feature_version)
356 else if(stage->required_features.glsl_version<feature_version)
357 stage->required_features.glsl_version = feature_version;
362 bool LegacyConverter::check_extension(bool Features::*extension) const
364 if(!(features.*extension))
367 stage->required_features.*extension = true;
372 bool LegacyConverter::supports_stage(Stage::Type st) const
374 if(st==Stage::GEOMETRY)
376 if(features.gl_api==OPENGL_ES2)
377 return check_version(Version(3, 20));
379 return check_version(Version(1, 50));
385 bool LegacyConverter::supports_unified_interface_syntax() const
387 if(features.gl_api==OPENGL_ES2)
388 return check_version(Version(3, 0));
390 return check_version(Version(1, 30));
393 void LegacyConverter::visit(VariableReference &var)
395 if(var.declaration==frag_out && !supports_unified_interface_syntax())
397 var.name = "gl_FragColor";
402 void LegacyConverter::visit(Assignment &assign)
404 TraversingVisitor::visit(assign);
405 if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
406 assign.target.declaration = 0;
409 bool LegacyConverter::supports_unified_sampling_functions() const
411 if(features.gl_api==OPENGL_ES2)
412 return check_version(Version(3, 0));
414 return check_version(Version(1, 30));
417 void LegacyConverter::visit(FunctionCall &call)
419 if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
421 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
423 const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
424 if(arg_image && !supports_unified_sampling_functions())
426 string suffix = call.name.substr(7);
427 call.name = (arg_image->shadow ? "shadow" : "texture");
429 switch(arg_image->dimensions)
431 case ImageTypeDeclaration::ONE: call.name += "1D"; break;
432 case ImageTypeDeclaration::TWO: call.name += "2D"; break;
433 case ImageTypeDeclaration::THREE: call.name += "3D"; break;
434 case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
439 /* Array textures and the unified sampling function name were
440 both introduced in GLSL 1.30. */
441 if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
442 check_extension(&Features::ext_texture_array);
443 call.name += "Array";
451 TraversingVisitor::visit(call);
454 bool LegacyConverter::supports_interface_layouts() const
456 if(features.gl_api==OPENGL_ES2)
457 return check_version(Version(3, 0));
458 else if(check_version(Version(3, 30)))
460 else if(check_version(Version(1, 30)))
461 return check_extension(&Features::arb_explicit_attrib_location);
466 bool LegacyConverter::supports_stage_interface_layouts() const
468 if(features.gl_api==OPENGL_ES2)
469 return check_version(Version(3, 10));
470 else if(check_version(Version(4, 10)))
473 return check_extension(&Features::arb_separate_shader_objects);
476 bool LegacyConverter::supports_centroid_sampling() const
478 if(features.gl_api==OPENGL_ES2)
479 return check_version(Version(3, 0));
480 else if(check_version(Version(1, 20)))
483 return check_extension(&Features::ext_gpu_shader4);
486 bool LegacyConverter::supports_sample_sampling() const
488 if(features.gl_api==OPENGL_ES2)
489 return check_version(Version(3, 20));
490 else if(check_version(Version(4, 0)))
493 return check_extension(&Features::arb_gpu_shader5);
496 bool LegacyConverter::supports_uniform_location() const
498 if(features.gl_api==OPENGL_ES2)
499 return check_version(Version(3, 10));
500 else if(check_version(Version(4, 30)))
503 return check_extension(&Features::arb_explicit_uniform_location);
506 bool LegacyConverter::supports_binding() const
508 if(features.gl_api==OPENGL_ES2)
509 return check_version(Version(3, 10));
511 return check_version(Version(4, 20));
514 void LegacyConverter::visit(VariableDeclaration &var)
518 for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
520 if(i->name=="location")
522 bool supported = true;
523 bool external = false;
524 if(var.interface=="in")
526 external = (stage->type==Stage::VERTEX);
527 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
529 else if(var.interface=="out")
531 external = (stage->type==Stage::FRAGMENT);
532 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
533 if(external && !supported && !check_extension(&Features::ext_gpu_shader4))
537 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
540 else if(var.interface=="uniform")
541 supported = supports_uniform_location();
546 stage->locations[var.name] = i->value;
547 i = var.layout->qualifiers.erase(i);
552 else if(i->name=="binding" && !supports_binding())
554 const TypeDeclaration *type = var.type_declaration;
555 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
556 type = basic->base_type;
557 if(dynamic_cast<const ImageTypeDeclaration *>(type))
558 stage->texture_bindings[var.name] = i->value;
560 i = var.layout->qualifiers.erase(i);
566 if(var.layout->qualifiers.empty())
570 if(var.sampling=="centroid")
572 if(!supports_centroid_sampling())
573 var.sampling = string();
575 else if(var.sampling=="sample")
577 if(!supports_sample_sampling())
578 var.sampling = string();
581 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
583 if(stage->type==Stage::FRAGMENT && var.interface=="out")
586 nodes_to_remove.insert(&var);
590 TraversingVisitor::visit(var);
593 bool LegacyConverter::supports_interface_blocks(const string &iface) const
595 if(features.gl_api==OPENGL_ES2)
598 return check_version(Version(3, 0));
600 return check_version(Version(3, 20));
602 else if(check_version(Version(1, 50)))
604 else if(iface=="uniform")
605 return check_extension(&Features::arb_uniform_buffer_object);
610 bool LegacyConverter::supports_interface_block_location() const
612 if(features.gl_api==OPENGL_ES2)
613 return check_version(Version(3, 20));
614 else if(check_version(Version(4, 40)))
617 return check_extension(&Features::arb_enhanced_layouts);
620 void LegacyConverter::visit(InterfaceBlock &iface)
624 for(vector<Layout::Qualifier>::const_iterator i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
626 if(i->name=="location" && !supports_interface_block_location())
627 i = iface.layout->qualifiers.erase(i);
628 else if(i->name=="binding" && !supports_binding())
630 stage->uniform_block_bindings[iface.block_name] = i->value;
631 i = iface.layout->qualifiers.erase(i);
637 if(iface.layout->qualifiers.empty())
641 if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
643 if(!iface.instance_name.empty())
644 unsupported("ARB_uniform_buffer_object required for interface block instances");
645 else if(iface.struct_declaration)
647 stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
648 nodes_to_remove.insert(&iface);
649 nodes_to_remove.insert(iface.struct_declaration);
652 /* If the interface block is an array, it should have an instance
653 name too, so this should never be reached */
654 throw logic_error("Unexpected interface block configuration");