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 StructOrganizer::visit(StructDeclaration &strct)
17 SetForScope<int> set_offset(offset, 0);
18 TraversingVisitor::visit(strct);
21 void StructOrganizer::visit(VariableDeclaration &var)
25 int *layout_offset = 0;
26 bool has_matrix_order = false;
29 for(Layout::Qualifier &q: var.layout->qualifiers)
31 if(q.name=="offset" && q.has_value)
33 layout_offset = &q.value;
37 else if(q.name=="column_major" || q.name=="row_major")
38 has_matrix_order = true;
42 MemoryRequirementsCalculator::Result mem_reqs = MemoryRequirementsCalculator().apply(var);
43 offset += mem_reqs.alignment-1;
44 offset -= offset%mem_reqs.alignment;
47 *layout_offset = offset;
51 var.layout = new Layout;
53 var.layout->qualifiers.push_back(Layout::Qualifier("offset", offset));
58 const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var.type_declaration);
59 while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
60 basic = dynamic_cast<const BasicTypeDeclaration *>(basic->base_type);
61 if(basic && basic->kind==BasicTypeDeclaration::MATRIX)
62 var.layout->qualifiers.push_back(Layout::Qualifier("column_major"));
65 offset += mem_reqs.size;
70 void LocationAllocator::apply(Module &module, const Features &features)
72 for(Stage &s: module.stages)
74 allocate_locations("uniform");
76 for(InterfaceBlock *b: unbound_blocks)
77 bind_uniform(b->layout, b->block_name, features.uniform_binding_range);
78 for(VariableDeclaration *t: unbound_textures)
79 bind_uniform(t->layout, t->name, features.texture_binding_range);
82 void LocationAllocator::apply(Stage &stage)
84 swap(used_locations["in"], used_locations["out"]);
85 used_locations["out"].clear();
87 stage.content.visit(*this);
89 allocate_locations("in");
90 allocate_locations("out");
93 void LocationAllocator::allocate_locations(const string &iface)
95 auto write = unplaced_variables.begin();
97 for(auto i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i)
99 if((*i)->interface!=iface)
107 if((*i)->interface=="uniform")
109 auto j = uniforms.find((*i)->name);
110 if(j!=uniforms.end() && j->second.location>=0)
112 add_layout_value((*i)->layout, "location", j->second.location);
117 set<unsigned> &used = used_locations[(*i)->interface];
119 unsigned size = LocationCounter().apply(**i);
123 for(unsigned j=0; j<size; ++j)
124 if(used.count(next+j))
131 add_layout_value((*i)->layout, "location", next);
132 if((*i)->interface=="uniform")
133 uniforms[(*i)->name].location = next;
135 for(unsigned j=0; j<size; ++j)
140 unplaced_variables.erase(write, unplaced_variables.end());
143 void LocationAllocator::bind_uniform(RefPtr<Layout> &layout, const string &name, unsigned range)
145 auto i = uniforms.find(name);
146 if(i!=uniforms.end() && i->second.bind_point>=0)
147 add_layout_value(layout, "binding", i->second.bind_point);
150 set<unsigned> &used = used_bindings[0];
152 unsigned bind_point = fold32(hash64(name))%range;
153 while(used.count(bind_point))
154 bind_point = (bind_point+1)%range;
156 add_layout_value(layout, "binding", bind_point);
157 uniforms[name].bind_point = bind_point;
158 used.insert(bind_point);
162 void LocationAllocator::add_layout_value(RefPtr<Layout> &layout, const string &name, unsigned value)
167 layout->qualifiers.push_back(Layout::Qualifier(name, value));
170 void LocationAllocator::visit(VariableDeclaration &var)
172 if(!var.name.compare(0, 3, "gl_"))
175 if(!var.interface.empty())
177 int location = (var.layout ? get_layout_value(*var.layout, "location") : -1);
179 if(location<0 && var.linked_declaration && var.linked_declaration->layout)
181 location = get_layout_value(*var.linked_declaration->layout, "location");
183 add_layout_value(var.layout, "location", location);
188 unsigned size = LocationCounter().apply(var);
189 for(unsigned i=0; i<size; ++i)
190 used_locations[var.interface].insert(location+i);
191 if(var.interface=="uniform")
192 uniforms[var.name].location = location;
195 unplaced_variables.push_back(&var);
198 if(var.interface=="uniform")
200 const TypeDeclaration *type = var.type_declaration;
201 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
202 type = basic->base_type;
203 if(dynamic_cast<const ImageTypeDeclaration *>(type))
205 int bind_point = (var.layout ? get_layout_value(*var.layout, "binding") : -1);
208 used_bindings[0].insert(bind_point);
209 uniforms[var.name].bind_point = bind_point;
212 unbound_textures.push_back(&var);
217 void LocationAllocator::visit(InterfaceBlock &iface)
219 if(!iface.instance_name.compare(0, 3, "gl_"))
222 if(iface.interface=="uniform")
224 bool push_constant = false;
227 auto i = find_member(iface.layout->qualifiers, string("push_constant"), &Layout::Qualifier::name);
228 push_constant = (i!=iface.layout->qualifiers.end());
233 int bind_point = (iface.layout ? get_layout_value(*iface.layout, "binding") : -1);
237 used_bindings[0].insert(bind_point);
238 uniforms[iface.block_name].bind_point = bind_point;
241 unbound_blocks.push_back(&iface);
247 void PrecisionConverter::apply(Stage &s)
250 s.content.visit(*this);
251 NodeRemover().apply(s, nodes_to_remove);
254 void PrecisionConverter::visit(Block &block)
256 for(auto i=block.body.begin(); i!=block.body.end(); ++i)
258 if(&block==&stage->content)
264 void PrecisionConverter::visit(Precision &prec)
266 if(stage->required_features.target_api==OPENGL_ES)
267 have_default.insert(prec.type);
269 nodes_to_remove.insert(&prec);
272 void PrecisionConverter::visit(VariableDeclaration &var)
274 if(stage->required_features.target_api!=OPENGL_ES)
276 var.precision.clear();
280 const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
281 const TypeDeclaration *type = var.type_declaration;
284 if(dynamic_cast<const ImageTypeDeclaration *>(type))
286 default_prec = "lowp";
289 else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
291 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
293 type = basic->base_type;
301 if(!have_default.count(type->name))
303 Precision *prec = new Precision;
304 prec->precision = default_prec;
305 prec->type = type->name;
306 stage->content.body.insert(insert_point, prec);
308 have_default.insert(type->name);
313 void FeatureConverter::apply(Stage &s, const Features &feat)
318 if(!stage->required_features.glsl_version)
319 stage->required_features.glsl_version = Version(1, (stage->required_features.target_api==OPENGL_ES ? 0 : 10));
324 void FeatureConverter::unsupported(const string &reason)
326 Diagnostic diagnostic;
327 diagnostic.severity = Diagnostic::ERR;
328 diagnostic.source = GENERATED_SOURCE;
330 diagnostic.message = reason;
331 stage->diagnostics.push_back(diagnostic);
334 bool FeatureConverter::check_version(const Version &feature_version) const
336 if(features.glsl_version<feature_version)
338 else if(stage->required_features.glsl_version<feature_version)
339 stage->required_features.glsl_version = feature_version;
344 bool FeatureConverter::check_extension(bool Features::*extension) const
346 if(!(features.*extension))
349 stage->required_features.*extension = true;
355 void StructuralFeatureConverter::apply()
357 if(supports_stage(stage->type))
359 stage->content.visit(*this);
360 NodeRemover().apply(*stage, nodes_to_remove);
363 unsupported(format("Stage %s is not supported", Stage::get_stage_name(stage->type)));
366 void StructuralFeatureConverter::visit(Block &block)
368 for(auto i=block.body.begin(); i!=block.body.end(); ++i)
370 if(&block==&stage->content)
371 uniform_insert_point = i;
376 void StructuralFeatureConverter::visit(RefPtr<Expression> &expr)
378 r_replaced_reference = 0;
380 if(r_replaced_reference)
381 expr = r_replaced_reference;
382 r_replaced_reference = 0;
385 bool StructuralFeatureConverter::supports_stage(Stage::Type st) const
387 if(st==Stage::GEOMETRY)
389 if(features.target_api==OPENGL_ES)
390 return check_version(Version(3, 20));
392 return check_version(Version(1, 50));
398 bool StructuralFeatureConverter::supports_unified_interface_syntax() const
400 if(features.target_api==OPENGL_ES)
401 return check_version(Version(3, 0));
403 return check_version(Version(1, 30));
406 void StructuralFeatureConverter::visit(VariableReference &var)
408 if(var.declaration==frag_out && !supports_unified_interface_syntax())
410 var.name = "gl_FragColor";
415 void StructuralFeatureConverter::visit(InterfaceBlockReference &iface)
417 r_flattened_interface = nodes_to_remove.count(iface.declaration);
420 void StructuralFeatureConverter::visit(MemberAccess &memacc)
422 r_flattened_interface = false;
424 if(r_flattened_interface)
426 VariableReference *var = new VariableReference;
427 var->name = memacc.member;
428 r_replaced_reference = var;
432 void StructuralFeatureConverter::visit(Assignment &assign)
434 TraversingVisitor::visit(assign);
435 if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
436 assign.target.declaration = 0;
439 bool StructuralFeatureConverter::supports_unified_sampling_functions() const
441 if(features.target_api==OPENGL_ES)
442 return check_version(Version(3, 0));
444 return check_version(Version(1, 30));
447 void StructuralFeatureConverter::visit(FunctionCall &call)
449 if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
451 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
453 const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
454 if(arg_image && !supports_unified_sampling_functions())
456 string suffix = call.name.substr(7);
457 call.name = (arg_image->shadow ? "shadow" : "texture");
459 switch(arg_image->dimensions)
461 case ImageTypeDeclaration::ONE: call.name += "1D"; break;
462 case ImageTypeDeclaration::TWO: call.name += "2D"; break;
463 case ImageTypeDeclaration::THREE: call.name += "3D"; break;
464 case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
469 /* Array textures and the unified sampling function name were
470 both introduced in GLSL 1.30. */
471 if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
472 check_extension(&Features::ext_texture_array);
473 call.name += "Array";
481 TraversingVisitor::visit(call);
484 void StructuralFeatureConverter::visit(VariableDeclaration &var)
486 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
487 if(stage->type==Stage::FRAGMENT && var.interface=="out")
490 nodes_to_remove.insert(&var);
493 TraversingVisitor::visit(var);
496 bool StructuralFeatureConverter::supports_interface_blocks(const string &iface) const
498 if(features.target_api==OPENGL_ES)
501 return check_version(Version(3, 0));
503 return check_version(Version(3, 20));
505 else if(check_version(Version(1, 50)))
507 else if(iface=="uniform")
508 return check_extension(&Features::arb_uniform_buffer_object);
513 void StructuralFeatureConverter::visit(InterfaceBlock &iface)
515 bool push_constant = false;
518 auto i = find_member(iface.layout->qualifiers, string("push_constant"), &Layout::Qualifier::name);
519 push_constant = (i!=iface.layout->qualifiers.end());
522 if((!supports_interface_blocks(iface.interface) || push_constant) && iface.type_declaration)
524 if(!iface.instance_name.empty())
525 unsupported("ARB_uniform_buffer_object required for interface block instances");
526 else if(iface.struct_declaration)
528 for(const RefPtr<Statement> &s: iface.struct_declaration->members.body)
529 if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(s.get()))
530 var->interface = iface.interface;
531 stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
532 nodes_to_remove.insert(&iface);
533 nodes_to_remove.insert(iface.struct_declaration);
536 /* If the interface block is an array, it should have an instance
537 name too, so this should never be reached */
538 throw logic_error("Unexpected interface block configuration");
543 void QualifierConverter::apply()
545 stage->content.visit(*this);
548 bool QualifierConverter::supports_interface_layouts() const
550 if(features.target_api==OPENGL_ES)
551 return check_version(Version(3, 0));
552 else if(check_version(Version(3, 30)))
554 else if(check_version(Version(1, 30)))
555 return check_extension(&Features::arb_explicit_attrib_location);
560 bool QualifierConverter::supports_stage_interface_layouts() const
562 if(features.target_api==OPENGL_ES)
563 return check_version(Version(3, 10));
564 else if(check_version(Version(4, 10)))
567 return check_extension(&Features::arb_separate_shader_objects);
570 bool QualifierConverter::supports_centroid_sampling() const
572 if(features.target_api==OPENGL_ES)
573 return check_version(Version(3, 0));
574 else if(check_version(Version(1, 20)))
577 return check_extension(&Features::ext_gpu_shader4);
580 bool QualifierConverter::supports_sample_sampling() const
582 if(features.target_api==OPENGL_ES)
583 return check_version(Version(3, 20));
584 else if(check_version(Version(4, 0)))
587 return check_extension(&Features::arb_gpu_shader5);
590 bool QualifierConverter::supports_uniform_location() const
592 if(features.target_api==OPENGL_ES)
593 return check_version(Version(3, 10));
594 else if(check_version(Version(4, 30)))
597 return check_extension(&Features::arb_explicit_uniform_location);
600 bool QualifierConverter::supports_binding() const
602 if(features.target_api==OPENGL_ES)
603 return check_version(Version(3, 10));
605 return check_version(Version(4, 20));
608 void QualifierConverter::visit(VariableDeclaration &var)
612 for(auto i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
614 if(i->name=="location")
616 bool supported = true;
617 bool external = false;
618 if(var.interface=="in")
620 external = (stage->type==Stage::VERTEX);
621 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
623 else if(var.interface=="out")
625 external = (stage->type==Stage::FRAGMENT);
626 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
627 if(external && !supported && !check_extension(&Features::ext_gpu_shader4))
631 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
634 else if(var.interface=="uniform")
635 supported = supports_uniform_location();
640 stage->locations[var.name] = i->value;
641 i = var.layout->qualifiers.erase(i);
646 else if(i->name=="binding" && !supports_binding())
648 const TypeDeclaration *type = var.type_declaration;
649 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
650 type = basic->base_type;
651 if(dynamic_cast<const ImageTypeDeclaration *>(type))
652 stage->texture_bindings[var.name] = i->value;
654 i = var.layout->qualifiers.erase(i);
660 if(var.layout->qualifiers.empty())
664 if(var.sampling=="centroid")
666 if(!supports_centroid_sampling())
667 var.sampling = string();
669 else if(var.sampling=="sample")
671 if(!supports_sample_sampling())
672 var.sampling = string();
675 if(var.name=="gl_ClipDistance")
676 if(const Literal *literal_size = dynamic_cast<const Literal *>(var.array_size.get()))
677 stage->n_clip_distances = literal_size->value.value<int>();
679 TraversingVisitor::visit(var);
682 bool QualifierConverter::supports_interface_block_location() const
684 if(features.target_api==OPENGL_ES)
685 return check_version(Version(3, 20));
686 else if(check_version(Version(4, 40)))
689 return check_extension(&Features::arb_enhanced_layouts);
692 void QualifierConverter::visit(InterfaceBlock &iface)
696 for(auto i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
698 if(i->name=="location" && !supports_interface_block_location())
699 i = iface.layout->qualifiers.erase(i);
700 else if(i->name=="binding" && !supports_binding())
702 stage->uniform_block_bindings[iface.block_name] = i->value;
703 i = iface.layout->qualifiers.erase(i);
709 if(iface.layout->qualifiers.empty())