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;
49 add_layout_qualifier(var.layout, Layout::Qualifier("offset", offset));
53 const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var.type_declaration);
54 while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
55 basic = dynamic_cast<const BasicTypeDeclaration *>(basic->base_type);
56 if(basic && basic->kind==BasicTypeDeclaration::MATRIX)
57 add_layout_qualifier(var.layout, Layout::Qualifier("column_major"));
60 offset += mem_reqs.size;
65 void LocationAllocator::apply(Module &module, const Features &f, bool a)
69 for(Stage &s: module.stages)
72 if(features.target_api!=VULKAN)
73 allocate_locations("uniform");
75 for(VariableDeclaration *b: unbound_blocks)
76 bind_uniform(b->layout, b->block_declaration->block_name, features.uniform_binding_range);
77 for(VariableDeclaration *t: unbound_textures)
78 bind_uniform(t->layout, t->name, features.texture_binding_range);
81 void LocationAllocator::apply(Stage &stage)
83 swap(used_locations["in"], used_locations["out"]);
84 used_locations["out"].clear();
86 stage.content.visit(*this);
88 allocate_locations("in");
89 allocate_locations("out");
92 void LocationAllocator::allocate_locations(const string &iface)
94 auto write = unplaced_variables.begin();
96 for(auto i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i)
98 if((*i)->interface!=iface)
106 if((*i)->interface=="uniform")
108 auto j = uniforms.find((*i)->name);
109 if(j!=uniforms.end() && j->second.location>=0)
111 add_layout_qualifier((*i)->layout, Layout::Qualifier("location", j->second.location));
119 set<unsigned> &used = used_locations[(*i)->interface];
121 unsigned size = LocationCounter().apply(**i);
125 for(unsigned j=0; j<size; ++j)
126 if(used.count(next+j))
133 add_layout_qualifier((*i)->layout, Layout::Qualifier("location", next));
134 if((*i)->interface=="uniform")
135 uniforms[(*i)->name].location = next;
137 for(unsigned j=0; j<size; ++j)
142 unplaced_variables.erase(write, unplaced_variables.end());
145 void LocationAllocator::bind_uniform(RefPtr<Layout> &layout, const string &name, unsigned range)
147 auto i = uniforms.find(name);
149 int desc_set = (i!=uniforms.end() ? i->second.desc_set : 0);
150 if(features.target_api==VULKAN && get_layout_value(layout.get(), "set")<0)
151 add_layout_qualifier(layout, Layout::Qualifier("set", desc_set));
153 if(i!=uniforms.end() && i->second.bind_point>=0)
154 add_layout_qualifier(layout, Layout::Qualifier("binding", i->second.bind_point));
157 set<unsigned> &used = used_bindings[desc_set];
159 unsigned bind_point = hash_fold<32>(hash<64>(name))%range;
160 while(used.count(bind_point))
161 bind_point = (bind_point+1)%range;
163 add_layout_qualifier(layout, Layout::Qualifier("binding", bind_point));
164 uniforms[name].bind_point = bind_point;
165 used.insert(bind_point);
169 bool LocationAllocator::visit_uniform(const string &name, RefPtr<Layout> &layout)
172 int bind_point = get_layout_value(layout.get(), "binding");
174 if(features.target_api==VULKAN)
176 desc_set = get_layout_value(layout.get(), "set");
177 if(desc_set<0 && bind_point>=0)
180 add_layout_qualifier(layout, Layout::Qualifier("set", desc_set));
184 uniforms[name].desc_set = desc_set;
189 used_bindings[desc_set].insert(bind_point);
190 uniforms[name].bind_point = bind_point;
193 return bind_point>=0;
196 void LocationAllocator::visit(VariableDeclaration &var)
198 if(!var.name.compare(0, 3, "gl_"))
201 if(!var.interface.empty() && !var.block_declaration)
203 int location = get_layout_value(var.layout.get(), "location");
205 if(location<0 && var.linked_declaration && var.linked_declaration->layout)
207 location = get_layout_value(var.linked_declaration->layout.get(), "location");
209 add_layout_qualifier(var.layout, Layout::Qualifier("location", location));
214 unsigned size = LocationCounter().apply(var);
215 for(unsigned i=0; i<size; ++i)
216 used_locations[var.interface].insert(location+i);
217 if(var.interface=="uniform")
218 uniforms[var.name].location = location;
221 unplaced_variables.push_back(&var);
224 if(var.interface=="uniform")
226 if(var.block_declaration)
228 bool push_constant = has_layout_qualifier(var.layout.get(), "push_constant");
229 if(!push_constant && !visit_uniform(var.block_declaration->block_name, var.layout))
230 unbound_blocks.push_back(&var);
234 const TypeDeclaration *base_type = get_ultimate_base_type(var.type_declaration);
235 if(dynamic_cast<const ImageTypeDeclaration *>(base_type) && !visit_uniform(var.name, var.layout))
236 unbound_textures.push_back(&var);
242 void PrecisionConverter::apply(Stage &s)
245 s.content.visit(*this);
246 NodeRemover().apply(s, nodes_to_remove);
249 void PrecisionConverter::visit(Block &block)
251 for(auto i=block.body.begin(); i!=block.body.end(); ++i)
253 if(&block==&stage->content)
259 void PrecisionConverter::visit(Precision &prec)
261 if(stage->required_features.target_api==OPENGL_ES)
262 have_default.insert(prec.type);
264 nodes_to_remove.insert(&prec);
267 void PrecisionConverter::visit(VariableDeclaration &var)
269 if(stage->required_features.target_api!=OPENGL_ES)
271 var.precision.clear();
275 const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
276 const TypeDeclaration *type = var.type_declaration;
279 if(dynamic_cast<const ImageTypeDeclaration *>(type))
281 default_prec = "lowp";
284 else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
286 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
288 type = basic->base_type;
296 if(!have_default.count(type->name))
298 Precision *prec = new Precision;
299 prec->precision = default_prec;
300 prec->type = type->name;
301 stage->content.body.insert(insert_point, prec);
303 have_default.insert(type->name);
308 void FeatureConverter::apply(Stage &s, const Features &feat)
313 if(!stage->required_features.glsl_version)
314 stage->required_features.glsl_version = Version(1, (stage->required_features.target_api==OPENGL_ES ? 0 : 10));
319 void FeatureConverter::unsupported(const string &reason)
321 Diagnostic diagnostic;
322 diagnostic.severity = Diagnostic::ERR;
323 diagnostic.source = GENERATED_SOURCE;
325 diagnostic.message = reason;
326 stage->diagnostics.push_back(diagnostic);
329 bool FeatureConverter::check_version(const Version &feature_version) const
331 if(features.glsl_version<feature_version)
333 else if(stage->required_features.glsl_version<feature_version)
334 stage->required_features.glsl_version = feature_version;
339 bool FeatureConverter::check_extension(bool Features::*extension) const
341 if(!(features.*extension))
344 stage->required_features.*extension = true;
350 void StructuralFeatureConverter::apply()
352 if(supports_stage(stage->type))
354 stage->content.visit(*this);
355 NodeRemover().apply(*stage, nodes_to_remove);
358 unsupported(format("Stage %s is not supported", Stage::get_stage_name(stage->type)));
361 void StructuralFeatureConverter::visit(Block &block)
363 for(auto i=block.body.begin(); i!=block.body.end(); ++i)
365 if(&block==&stage->content)
366 uniform_insert_point = i;
371 void StructuralFeatureConverter::visit(RefPtr<Expression> &expr)
373 r_replaced_reference = 0;
375 if(r_replaced_reference)
376 expr = r_replaced_reference;
377 r_replaced_reference = 0;
380 bool StructuralFeatureConverter::supports_stage(Stage::Type st) const
382 if(st==Stage::GEOMETRY)
384 if(features.target_api==VULKAN)
386 else if(features.target_api==OPENGL_ES)
387 return check_version(Version(3, 20));
389 return check_version(Version(1, 50));
395 bool StructuralFeatureConverter::supports_unified_interface_syntax() const
397 if(features.target_api==VULKAN)
399 else if(features.target_api==OPENGL_ES)
400 return check_version(Version(3, 0));
402 return check_version(Version(1, 30));
405 void StructuralFeatureConverter::visit(VariableReference &var)
407 if(var.declaration==frag_out && !supports_unified_interface_syntax())
409 var.name = "gl_FragColor";
413 r_flattened_interface = nodes_to_remove.count(var.declaration);
416 void StructuralFeatureConverter::visit(MemberAccess &memacc)
418 r_flattened_interface = false;
420 if(r_flattened_interface)
422 VariableReference *var = new VariableReference;
423 var->name = memacc.member;
424 r_replaced_reference = var;
428 void StructuralFeatureConverter::visit(Assignment &assign)
430 TraversingVisitor::visit(assign);
431 if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
432 assign.target.declaration = 0;
435 bool StructuralFeatureConverter::supports_unified_sampling_functions() const
437 if(features.target_api==VULKAN)
439 else if(features.target_api==OPENGL_ES)
440 return check_version(Version(3, 0));
442 return check_version(Version(1, 30));
445 void StructuralFeatureConverter::visit(FunctionCall &call)
447 if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
449 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
451 const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
452 if(arg_image && !supports_unified_sampling_functions())
454 string suffix = call.name.substr(7);
455 call.name = (arg_image->shadow ? "shadow" : "texture");
457 switch(arg_image->dimensions)
459 case ImageTypeDeclaration::ONE: call.name += "1D"; break;
460 case ImageTypeDeclaration::TWO: call.name += "2D"; break;
461 case ImageTypeDeclaration::THREE: call.name += "3D"; break;
462 case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
467 /* Array textures and the unified sampling function name were
468 both introduced in GLSL 1.30. */
469 if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
470 check_extension(&Features::ext_texture_array);
471 call.name += "Array";
479 TraversingVisitor::visit(call);
482 bool StructuralFeatureConverter::supports_interface_blocks(const string &iface) const
484 if(features.target_api==VULKAN)
486 else if(features.target_api==OPENGL_ES)
489 return check_version(Version(3, 0));
491 return check_version(Version(3, 20));
493 else if(check_version(Version(1, 50)))
495 else if(iface=="uniform")
496 return check_extension(&Features::arb_uniform_buffer_object);
501 void StructuralFeatureConverter::visit(VariableDeclaration &var)
503 if(var.block_declaration)
505 bool push_constant = has_layout_qualifier(var.layout.get(), "push_constant");
506 if(!supports_interface_blocks(var.interface) || (push_constant && features.target_api!=VULKAN))
508 if(var.name.find(' ')==string::npos)
509 unsupported("ARB_uniform_buffer_object required for interface block instances");
512 for(const RefPtr<Statement> &s: var.block_declaration->members.body)
513 if(VariableDeclaration *mem = dynamic_cast<VariableDeclaration *>(s.get()))
514 mem->interface = var.interface;
515 stage->content.body.splice(uniform_insert_point, var.block_declaration->members.body);
516 nodes_to_remove.insert(&var);
517 nodes_to_remove.insert(var.block_declaration);
522 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
523 if(stage->type==Stage::FRAGMENT && var.interface=="out")
526 nodes_to_remove.insert(&var);
529 TraversingVisitor::visit(var);
533 void QualifierConverter::apply()
535 stage->content.visit(*this);
538 bool QualifierConverter::supports_interface_layouts() const
540 if(features.target_api==VULKAN)
542 else if(features.target_api==OPENGL_ES)
543 return check_version(Version(3, 0));
544 else if(check_version(Version(3, 30)))
546 else if(check_version(Version(1, 30)))
547 return check_extension(&Features::arb_explicit_attrib_location);
552 bool QualifierConverter::supports_stage_interface_layouts() const
554 if(features.target_api==VULKAN)
556 else if(features.target_api==OPENGL_ES)
557 return check_version(Version(3, 10));
558 else if(check_version(Version(4, 10)))
561 return check_extension(&Features::arb_separate_shader_objects);
564 bool QualifierConverter::supports_centroid_sampling() const
566 if(features.target_api==VULKAN)
568 else if(features.target_api==OPENGL_ES)
569 return check_version(Version(3, 0));
570 else if(check_version(Version(1, 20)))
573 return check_extension(&Features::ext_gpu_shader4);
576 bool QualifierConverter::supports_sample_sampling() const
578 if(features.target_api==VULKAN)
580 else if(features.target_api==OPENGL_ES)
581 return check_version(Version(3, 20));
582 else if(check_version(Version(4, 0)))
585 return check_extension(&Features::arb_gpu_shader5);
588 bool QualifierConverter::supports_uniform_location() const
590 if(features.target_api==VULKAN)
592 else 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==VULKAN)
604 else if(features.target_api==OPENGL_ES)
605 return check_version(Version(3, 10));
607 return check_version(Version(4, 20));
610 bool QualifierConverter::supports_interface_block_location() const
612 if(features.target_api==VULKAN)
614 else if(features.target_api==OPENGL_ES)
615 return check_version(Version(3, 20));
616 else if(check_version(Version(4, 40)))
619 return check_extension(&Features::arb_enhanced_layouts);
622 void QualifierConverter::visit(VariableDeclaration &var)
626 for(auto i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
628 if(i->name=="location")
630 bool supported = true;
631 bool external = false;
632 if(var.block_declaration)
633 supported = supports_interface_block_location();
634 else if(var.interface=="in")
636 external = (stage->type==Stage::VERTEX);
637 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
639 else if(var.interface=="out")
641 external = (stage->type==Stage::FRAGMENT);
642 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
643 if(external && !supported && !check_extension(&Features::ext_gpu_shader4))
647 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
650 else if(var.interface=="uniform")
651 supported = supports_uniform_location();
656 stage->locations[var.name] = i->value;
657 i = var.layout->qualifiers.erase(i);
662 else if(i->name=="binding" && !supports_binding())
664 if(var.block_declaration)
665 stage->uniform_block_bindings[var.block_declaration->block_name] = i->value;
666 else if(dynamic_cast<const ImageTypeDeclaration *>(get_ultimate_base_type(var.type_declaration)))
667 stage->texture_bindings[var.name] = i->value;
669 i = var.layout->qualifiers.erase(i);
675 if(var.layout->qualifiers.empty())
679 if(var.sampling=="centroid")
681 if(!supports_centroid_sampling())
682 var.sampling = string();
684 else if(var.sampling=="sample")
686 if(!supports_sample_sampling())
687 var.sampling = string();
690 if(var.name=="gl_ClipDistance")
691 if(const Literal *literal_size = dynamic_cast<const Literal *>(var.array_size.get()))
692 stage->n_clip_distances = literal_size->value.value<int>();
694 TraversingVisitor::visit(var);