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 for(Layout::Qualifier &q: var.layout->qualifiers)
35 if(q.name=="offset" && q.has_value)
37 layout_offset = &q.value;
41 else if(q.name=="column_major" || q.name=="row_major")
42 has_matrix_order = true;
46 MemoryRequirementsCalculator::Result mem_reqs = MemoryRequirementsCalculator().apply(var);
47 offset += mem_reqs.alignment-1;
48 offset -= offset%mem_reqs.alignment;
51 *layout_offset = offset;
55 var.layout = new Layout;
57 var.layout->qualifiers.push_back(Layout::Qualifier("offset", offset));
62 const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(var.type_declaration);
63 while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
64 basic = dynamic_cast<const BasicTypeDeclaration *>(basic->base_type);
65 if(basic && basic->kind==BasicTypeDeclaration::MATRIX)
66 var.layout->qualifiers.push_back(Layout::Qualifier("column_major"));
69 offset += mem_reqs.size;
74 void LocationAllocator::apply(Module &module, const Features &features)
76 for(Stage &s: module.stages)
78 allocate_locations("uniform");
80 for(InterfaceBlock *b: unbound_blocks)
81 bind_uniform(b->layout, b->block_name, features.uniform_binding_range);
82 for(VariableDeclaration *t: unbound_textures)
83 bind_uniform(t->layout, t->name, features.texture_binding_range);
86 void LocationAllocator::apply(Stage &stage)
88 swap(used_locations["in"], used_locations["out"]);
89 used_locations["out"].clear();
91 stage.content.visit(*this);
93 allocate_locations("in");
94 allocate_locations("out");
97 void LocationAllocator::allocate_locations(const string &iface)
99 auto write = unplaced_variables.begin();
101 for(auto i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i)
103 if((*i)->interface!=iface)
111 if((*i)->interface=="uniform")
113 auto j = uniforms.find((*i)->name);
114 if(j!=uniforms.end() && j->second.location>=0)
116 add_layout_value((*i)->layout, "location", j->second.location);
121 set<unsigned> &used = used_locations[(*i)->interface];
123 unsigned size = LocationCounter().apply(**i);
127 for(unsigned j=0; j<size; ++j)
128 if(used.count(next+j))
135 add_layout_value((*i)->layout, "location", next);
136 if((*i)->interface=="uniform")
137 uniforms[(*i)->name].location = next;
139 for(unsigned j=0; j<size; ++j)
144 unplaced_variables.erase(write, unplaced_variables.end());
147 void LocationAllocator::bind_uniform(RefPtr<Layout> &layout, const string &name, unsigned range)
149 auto i = uniforms.find(name);
150 if(i!=uniforms.end() && i->second.bind_point>=0)
151 add_layout_value(layout, "binding", i->second.bind_point);
154 set<unsigned> &used = used_bindings[0];
156 unsigned bind_point = fold32(hash64(name))%range;
157 while(used.count(bind_point))
158 bind_point = (bind_point+1)%range;
160 add_layout_value(layout, "binding", bind_point);
161 uniforms[name].bind_point = bind_point;
162 used.insert(bind_point);
166 void LocationAllocator::add_layout_value(RefPtr<Layout> &layout, const string &name, unsigned value)
171 layout->qualifiers.push_back(Layout::Qualifier(name, value));
174 void LocationAllocator::visit(VariableDeclaration &var)
176 if(!var.name.compare(0, 3, "gl_"))
179 if(!var.interface.empty())
181 int location = (var.layout ? get_layout_value(*var.layout, "location") : -1);
183 if(location<0 && var.linked_declaration && var.linked_declaration->layout)
185 location = get_layout_value(*var.linked_declaration->layout, "location");
187 add_layout_value(var.layout, "location", location);
192 unsigned size = LocationCounter().apply(var);
193 for(unsigned i=0; i<size; ++i)
194 used_locations[var.interface].insert(location+i);
195 if(var.interface=="uniform")
196 uniforms[var.name].location = location;
199 unplaced_variables.push_back(&var);
202 if(var.interface=="uniform")
204 const TypeDeclaration *type = var.type_declaration;
205 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
206 type = basic->base_type;
207 if(dynamic_cast<const ImageTypeDeclaration *>(type))
209 int bind_point = (var.layout ? get_layout_value(*var.layout, "binding") : -1);
212 used_bindings[0].insert(bind_point);
213 uniforms[var.name].bind_point = bind_point;
216 unbound_textures.push_back(&var);
221 void LocationAllocator::visit(InterfaceBlock &iface)
223 if(!iface.instance_name.compare(0, 3, "gl_"))
226 if(iface.interface=="uniform")
228 int bind_point = (iface.layout ? get_layout_value(*iface.layout, "binding") : -1);
232 used_bindings[0].insert(bind_point);
233 uniforms[iface.block_name].bind_point = bind_point;
236 unbound_blocks.push_back(&iface);
241 PrecisionConverter::PrecisionConverter():
245 void PrecisionConverter::apply(Stage &s)
248 s.content.visit(*this);
249 NodeRemover().apply(s, nodes_to_remove);
252 void PrecisionConverter::visit(Block &block)
254 for(auto i=block.body.begin(); i!=block.body.end(); ++i)
256 if(&block==&stage->content)
262 void PrecisionConverter::visit(Precision &prec)
264 if(stage->required_features.gl_api==OPENGL_ES2)
265 have_default.insert(prec.type);
267 nodes_to_remove.insert(&prec);
270 void PrecisionConverter::visit(VariableDeclaration &var)
272 if(stage->required_features.gl_api!=OPENGL_ES2)
274 var.precision.clear();
278 const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
279 const TypeDeclaration *type = var.type_declaration;
282 if(dynamic_cast<const ImageTypeDeclaration *>(type))
284 default_prec = "lowp";
287 else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
289 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
291 type = basic->base_type;
299 if(!have_default.count(type->name))
301 Precision *prec = new Precision;
302 prec->precision = default_prec;
303 prec->type = type->name;
304 stage->content.body.insert(insert_point, prec);
306 have_default.insert(type->name);
311 LegacyConverter::LegacyConverter():
315 void LegacyConverter::apply(Stage &s, const Features &feat)
319 if(supports_stage(s.type))
321 s.content.visit(*this);
322 NodeRemover().apply(s, nodes_to_remove);
324 if(!stage->required_features.glsl_version)
325 stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
328 unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
331 void LegacyConverter::unsupported(const string &reason)
333 Diagnostic diagnostic;
334 diagnostic.severity = Diagnostic::ERR;
335 diagnostic.source = GENERATED_SOURCE;
337 diagnostic.message = reason;
338 stage->diagnostics.push_back(diagnostic);
341 void LegacyConverter::visit(Block &block)
343 for(auto i=block.body.begin(); i!=block.body.end(); ++i)
345 if(&block==&stage->content)
346 uniform_insert_point = i;
351 bool LegacyConverter::check_version(const Version &feature_version) const
353 if(features.glsl_version<feature_version)
355 else if(stage->required_features.glsl_version<feature_version)
356 stage->required_features.glsl_version = feature_version;
361 bool LegacyConverter::check_extension(bool Features::*extension) const
363 if(!(features.*extension))
366 stage->required_features.*extension = true;
371 bool LegacyConverter::supports_stage(Stage::Type st) const
373 if(st==Stage::GEOMETRY)
375 if(features.gl_api==OPENGL_ES2)
376 return check_version(Version(3, 20));
378 return check_version(Version(1, 50));
384 bool LegacyConverter::supports_unified_interface_syntax() const
386 if(features.gl_api==OPENGL_ES2)
387 return check_version(Version(3, 0));
389 return check_version(Version(1, 30));
392 void LegacyConverter::visit(VariableReference &var)
394 if(var.declaration==frag_out && !supports_unified_interface_syntax())
396 var.name = "gl_FragColor";
401 void LegacyConverter::visit(Assignment &assign)
403 TraversingVisitor::visit(assign);
404 if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
405 assign.target.declaration = 0;
408 bool LegacyConverter::supports_unified_sampling_functions() const
410 if(features.gl_api==OPENGL_ES2)
411 return check_version(Version(3, 0));
413 return check_version(Version(1, 30));
416 void LegacyConverter::visit(FunctionCall &call)
418 if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
420 if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
422 const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
423 if(arg_image && !supports_unified_sampling_functions())
425 string suffix = call.name.substr(7);
426 call.name = (arg_image->shadow ? "shadow" : "texture");
428 switch(arg_image->dimensions)
430 case ImageTypeDeclaration::ONE: call.name += "1D"; break;
431 case ImageTypeDeclaration::TWO: call.name += "2D"; break;
432 case ImageTypeDeclaration::THREE: call.name += "3D"; break;
433 case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
438 /* Array textures and the unified sampling function name were
439 both introduced in GLSL 1.30. */
440 if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
441 check_extension(&Features::ext_texture_array);
442 call.name += "Array";
450 TraversingVisitor::visit(call);
453 bool LegacyConverter::supports_interface_layouts() const
455 if(features.gl_api==OPENGL_ES2)
456 return check_version(Version(3, 0));
457 else if(check_version(Version(3, 30)))
459 else if(check_version(Version(1, 30)))
460 return check_extension(&Features::arb_explicit_attrib_location);
465 bool LegacyConverter::supports_stage_interface_layouts() const
467 if(features.gl_api==OPENGL_ES2)
468 return check_version(Version(3, 10));
469 else if(check_version(Version(4, 10)))
472 return check_extension(&Features::arb_separate_shader_objects);
475 bool LegacyConverter::supports_centroid_sampling() const
477 if(features.gl_api==OPENGL_ES2)
478 return check_version(Version(3, 0));
479 else if(check_version(Version(1, 20)))
482 return check_extension(&Features::ext_gpu_shader4);
485 bool LegacyConverter::supports_sample_sampling() const
487 if(features.gl_api==OPENGL_ES2)
488 return check_version(Version(3, 20));
489 else if(check_version(Version(4, 0)))
492 return check_extension(&Features::arb_gpu_shader5);
495 bool LegacyConverter::supports_uniform_location() const
497 if(features.gl_api==OPENGL_ES2)
498 return check_version(Version(3, 10));
499 else if(check_version(Version(4, 30)))
502 return check_extension(&Features::arb_explicit_uniform_location);
505 bool LegacyConverter::supports_binding() const
507 if(features.gl_api==OPENGL_ES2)
508 return check_version(Version(3, 10));
510 return check_version(Version(4, 20));
513 void LegacyConverter::visit(VariableDeclaration &var)
517 for(auto i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
519 if(i->name=="location")
521 bool supported = true;
522 bool external = false;
523 if(var.interface=="in")
525 external = (stage->type==Stage::VERTEX);
526 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
528 else if(var.interface=="out")
530 external = (stage->type==Stage::FRAGMENT);
531 supported = (external ? supports_interface_layouts() : supports_stage_interface_layouts());
532 if(external && !supported && !check_extension(&Features::ext_gpu_shader4))
536 unsupported("EXT_gpu_shader4 required for multiple fragment shader outputs");
539 else if(var.interface=="uniform")
540 supported = supports_uniform_location();
545 stage->locations[var.name] = i->value;
546 i = var.layout->qualifiers.erase(i);
551 else if(i->name=="binding" && !supports_binding())
553 const TypeDeclaration *type = var.type_declaration;
554 while(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
555 type = basic->base_type;
556 if(dynamic_cast<const ImageTypeDeclaration *>(type))
557 stage->texture_bindings[var.name] = i->value;
559 i = var.layout->qualifiers.erase(i);
565 if(var.layout->qualifiers.empty())
569 if(var.sampling=="centroid")
571 if(!supports_centroid_sampling())
572 var.sampling = string();
574 else if(var.sampling=="sample")
576 if(!supports_sample_sampling())
577 var.sampling = string();
580 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
582 if(stage->type==Stage::FRAGMENT && var.interface=="out")
585 nodes_to_remove.insert(&var);
589 TraversingVisitor::visit(var);
592 bool LegacyConverter::supports_interface_blocks(const string &iface) const
594 if(features.gl_api==OPENGL_ES2)
597 return check_version(Version(3, 0));
599 return check_version(Version(3, 20));
601 else if(check_version(Version(1, 50)))
603 else if(iface=="uniform")
604 return check_extension(&Features::arb_uniform_buffer_object);
609 bool LegacyConverter::supports_interface_block_location() const
611 if(features.gl_api==OPENGL_ES2)
612 return check_version(Version(3, 20));
613 else if(check_version(Version(4, 40)))
616 return check_extension(&Features::arb_enhanced_layouts);
619 void LegacyConverter::visit(InterfaceBlock &iface)
623 for(auto i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
625 if(i->name=="location" && !supports_interface_block_location())
626 i = iface.layout->qualifiers.erase(i);
627 else if(i->name=="binding" && !supports_binding())
629 stage->uniform_block_bindings[iface.block_name] = i->value;
630 i = iface.layout->qualifiers.erase(i);
636 if(iface.layout->qualifiers.empty())
640 if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
642 if(!iface.instance_name.empty())
643 unsupported("ARB_uniform_buffer_object required for interface block instances");
644 else if(iface.struct_declaration)
646 stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
647 nodes_to_remove.insert(&iface);
648 nodes_to_remove.insert(iface.struct_declaration);
651 /* If the interface block is an array, it should have an instance
652 name too, so this should never be reached */
653 throw logic_error("Unexpected interface block configuration");