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 int bind_point = (iface.layout ? get_layout_value(*iface.layout, "binding") : -1);
228 used_bindings[0].insert(bind_point);
229 uniforms[iface.block_name].bind_point = bind_point;
232 unbound_blocks.push_back(&iface);
237 void PrecisionConverter::apply(Stage &s)
240 s.content.visit(*this);
241 NodeRemover().apply(s, nodes_to_remove);
244 void PrecisionConverter::visit(Block &block)
246 for(auto i=block.body.begin(); i!=block.body.end(); ++i)
248 if(&block==&stage->content)
254 void PrecisionConverter::visit(Precision &prec)
256 if(stage->required_features.target_api==OPENGL_ES)
257 have_default.insert(prec.type);
259 nodes_to_remove.insert(&prec);
262 void PrecisionConverter::visit(VariableDeclaration &var)
264 if(stage->required_features.target_api!=OPENGL_ES)
266 var.precision.clear();
270 const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
271 const TypeDeclaration *type = var.type_declaration;
274 if(dynamic_cast<const ImageTypeDeclaration *>(type))
276 default_prec = "lowp";
279 else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
281 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
283 type = basic->base_type;
291 if(!have_default.count(type->name))
293 Precision *prec = new Precision;
294 prec->precision = default_prec;
295 prec->type = type->name;
296 stage->content.body.insert(insert_point, prec);
298 have_default.insert(type->name);
303 void LegacyConverter::apply(Stage &s, const Features &feat)
307 if(supports_stage(s.type))
309 s.content.visit(*this);
310 NodeRemover().apply(s, nodes_to_remove);
312 if(!stage->required_features.glsl_version)
313 stage->required_features.glsl_version = Version(1, (stage->required_features.target_api==OPENGL_ES ? 0 : 10));
316 unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
319 void LegacyConverter::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 void LegacyConverter::visit(Block &block)
331 for(auto i=block.body.begin(); i!=block.body.end(); ++i)
333 if(&block==&stage->content)
334 uniform_insert_point = i;
339 bool LegacyConverter::check_version(const Version &feature_version) const
341 if(features.glsl_version<feature_version)
343 else if(stage->required_features.glsl_version<feature_version)
344 stage->required_features.glsl_version = feature_version;
349 bool LegacyConverter::check_extension(bool Features::*extension) const
351 if(!(features.*extension))
354 stage->required_features.*extension = true;
359 bool LegacyConverter::supports_stage(Stage::Type st) const
361 if(st==Stage::GEOMETRY)
363 if(features.target_api==OPENGL_ES)
364 return check_version(Version(3, 20));
366 return check_version(Version(1, 50));
372 bool LegacyConverter::supports_unified_interface_syntax() const
374 if(features.target_api==OPENGL_ES)
375 return check_version(Version(3, 0));
377 return check_version(Version(1, 30));
380 void LegacyConverter::visit(VariableReference &var)
382 if(var.declaration==frag_out && !supports_unified_interface_syntax())
384 var.name = "gl_FragColor";
389 void LegacyConverter::visit(Assignment &assign)
391 TraversingVisitor::visit(assign);
392 if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
393 assign.target.declaration = 0;
396 bool LegacyConverter::supports_unified_sampling_functions() const
398 if(features.target_api==OPENGL_ES)
399 return check_version(Version(3, 0));
401 return check_version(Version(1, 30));
404 void LegacyConverter::visit(FunctionCall &call)
406 if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
408 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
410 const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
411 if(arg_image && !supports_unified_sampling_functions())
413 string suffix = call.name.substr(7);
414 call.name = (arg_image->shadow ? "shadow" : "texture");
416 switch(arg_image->dimensions)
418 case ImageTypeDeclaration::ONE: call.name += "1D"; break;
419 case ImageTypeDeclaration::TWO: call.name += "2D"; break;
420 case ImageTypeDeclaration::THREE: call.name += "3D"; break;
421 case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
426 /* Array textures and the unified sampling function name were
427 both introduced in GLSL 1.30. */
428 if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
429 check_extension(&Features::ext_texture_array);
430 call.name += "Array";
438 TraversingVisitor::visit(call);
441 bool LegacyConverter::supports_interface_layouts() const
443 if(features.target_api==OPENGL_ES)
444 return check_version(Version(3, 0));
445 else if(check_version(Version(3, 30)))
447 else if(check_version(Version(1, 30)))
448 return check_extension(&Features::arb_explicit_attrib_location);
453 bool LegacyConverter::supports_stage_interface_layouts() const
455 if(features.target_api==OPENGL_ES)
456 return check_version(Version(3, 10));
457 else if(check_version(Version(4, 10)))
460 return check_extension(&Features::arb_separate_shader_objects);
463 bool LegacyConverter::supports_centroid_sampling() const
465 if(features.target_api==OPENGL_ES)
466 return check_version(Version(3, 0));
467 else if(check_version(Version(1, 20)))
470 return check_extension(&Features::ext_gpu_shader4);
473 bool LegacyConverter::supports_sample_sampling() const
475 if(features.target_api==OPENGL_ES)
476 return check_version(Version(3, 20));
477 else if(check_version(Version(4, 0)))
480 return check_extension(&Features::arb_gpu_shader5);
483 bool LegacyConverter::supports_uniform_location() const
485 if(features.target_api==OPENGL_ES)
486 return check_version(Version(3, 10));
487 else if(check_version(Version(4, 30)))
490 return check_extension(&Features::arb_explicit_uniform_location);
493 bool LegacyConverter::supports_binding() const
495 if(features.target_api==OPENGL_ES)
496 return check_version(Version(3, 10));
498 return check_version(Version(4, 20));
501 void LegacyConverter::visit(VariableDeclaration &var)
505 for(auto i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
507 if(i->name=="location")
509 bool supported = true;
510 bool external = false;
511 if(var.interface=="in")
513 external = (stage->type==Stage::VERTEX);
514 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
516 else if(var.interface=="out")
518 external = (stage->type==Stage::FRAGMENT);
519 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
520 if(external && !supported && !check_extension(&Features::ext_gpu_shader4))
524 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
527 else if(var.interface=="uniform")
528 supported = supports_uniform_location();
533 stage->locations[var.name] = i->value;
534 i = var.layout->qualifiers.erase(i);
539 else if(i->name=="binding" && !supports_binding())
541 const TypeDeclaration *type = var.type_declaration;
542 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
543 type = basic->base_type;
544 if(dynamic_cast<const ImageTypeDeclaration *>(type))
545 stage->texture_bindings[var.name] = i->value;
547 i = var.layout->qualifiers.erase(i);
553 if(var.layout->qualifiers.empty())
557 if(var.sampling=="centroid")
559 if(!supports_centroid_sampling())
560 var.sampling = string();
562 else if(var.sampling=="sample")
564 if(!supports_sample_sampling())
565 var.sampling = string();
568 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
570 if(stage->type==Stage::FRAGMENT && var.interface=="out")
573 nodes_to_remove.insert(&var);
577 if(var.name=="gl_ClipDistance")
578 if(const Literal *literal_size = dynamic_cast<const Literal *>(var.array_size.get()))
579 stage->n_clip_distances = literal_size->value.value<int>();
581 TraversingVisitor::visit(var);
584 bool LegacyConverter::supports_interface_blocks(const string &iface) const
586 if(features.target_api==OPENGL_ES)
589 return check_version(Version(3, 0));
591 return check_version(Version(3, 20));
593 else if(check_version(Version(1, 50)))
595 else if(iface=="uniform")
596 return check_extension(&Features::arb_uniform_buffer_object);
601 bool LegacyConverter::supports_interface_block_location() const
603 if(features.target_api==OPENGL_ES)
604 return check_version(Version(3, 20));
605 else if(check_version(Version(4, 40)))
608 return check_extension(&Features::arb_enhanced_layouts);
611 void LegacyConverter::visit(InterfaceBlock &iface)
615 for(auto i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
617 if(i->name=="location" && !supports_interface_block_location())
618 i = iface.layout->qualifiers.erase(i);
619 else if(i->name=="binding" && !supports_binding())
621 stage->uniform_block_bindings[iface.block_name] = i->value;
622 i = iface.layout->qualifiers.erase(i);
628 if(iface.layout->qualifiers.empty())
632 if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
634 if(!iface.instance_name.empty())
635 unsupported("ARB_uniform_buffer_object required for interface block instances");
636 else if(iface.struct_declaration)
638 stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
639 nodes_to_remove.insert(&iface);
640 nodes_to_remove.insert(iface.struct_declaration);
643 /* If the interface block is an array, it should have an instance
644 name too, so this should never be reached */
645 throw logic_error("Unexpected interface block configuration");