{
stage = &s;
features = feat;
- if(supports_stage(s.type))
- {
- s.content.visit(*this);
- NodeRemover().apply(s, nodes_to_remove);
- if(!stage->required_features.glsl_version)
- stage->required_features.glsl_version = Version(1, (stage->required_features.target_api==OPENGL_ES ? 0 : 10));
- }
- else
- unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
+ if(!stage->required_features.glsl_version)
+ stage->required_features.glsl_version = Version(1, (stage->required_features.target_api==OPENGL_ES ? 0 : 10));
+
+ apply();
}
void FeatureConverter::unsupported(const string &reason)
stage->diagnostics.push_back(diagnostic);
}
-void FeatureConverter::visit(Block &block)
-{
- for(auto i=block.body.begin(); i!=block.body.end(); ++i)
- {
- if(&block==&stage->content)
- uniform_insert_point = i;
- (*i)->visit(*this);
- }
-}
-
-void FeatureConverter::visit(RefPtr<Expression> &expr)
-{
- r_replaced_reference = 0;
- expr->visit(*this);
- if(r_replaced_reference)
- expr = r_replaced_reference;
- r_replaced_reference = 0;
-}
-
bool FeatureConverter::check_version(const Version &feature_version) const
{
if(features.glsl_version<feature_version)
return true;
}
-bool FeatureConverter::supports_stage(Stage::Type st) const
+
+void StructuralFeatureConverter::apply()
+{
+ if(supports_stage(stage->type))
+ {
+ stage->content.visit(*this);
+ NodeRemover().apply(*stage, nodes_to_remove);
+ }
+ else
+ unsupported(format("Stage %s is not supported", Stage::get_stage_name(stage->type)));
+}
+
+void StructuralFeatureConverter::visit(Block &block)
+{
+ for(auto i=block.body.begin(); i!=block.body.end(); ++i)
+ {
+ if(&block==&stage->content)
+ uniform_insert_point = i;
+ (*i)->visit(*this);
+ }
+}
+
+void StructuralFeatureConverter::visit(RefPtr<Expression> &expr)
+{
+ r_replaced_reference = 0;
+ expr->visit(*this);
+ if(r_replaced_reference)
+ expr = r_replaced_reference;
+ r_replaced_reference = 0;
+}
+
+bool StructuralFeatureConverter::supports_stage(Stage::Type st) const
{
if(st==Stage::GEOMETRY)
{
return true;
}
-bool FeatureConverter::supports_unified_interface_syntax() const
+bool StructuralFeatureConverter::supports_unified_interface_syntax() const
{
if(features.target_api==OPENGL_ES)
return check_version(Version(3, 0));
return check_version(Version(1, 30));
}
-void FeatureConverter::visit(VariableReference &var)
+void StructuralFeatureConverter::visit(VariableReference &var)
{
if(var.declaration==frag_out && !supports_unified_interface_syntax())
{
}
}
-void FeatureConverter::visit(InterfaceBlockReference &iface)
+void StructuralFeatureConverter::visit(InterfaceBlockReference &iface)
{
r_flattened_interface = nodes_to_remove.count(iface.declaration);
}
-void FeatureConverter::visit(MemberAccess &memacc)
+void StructuralFeatureConverter::visit(MemberAccess &memacc)
{
r_flattened_interface = false;
visit(memacc.left);
}
}
-void FeatureConverter::visit(Assignment &assign)
+void StructuralFeatureConverter::visit(Assignment &assign)
{
TraversingVisitor::visit(assign);
if(assign.target.declaration==frag_out && !supports_unified_interface_syntax())
assign.target.declaration = 0;
}
-bool FeatureConverter::supports_unified_sampling_functions() const
+bool StructuralFeatureConverter::supports_unified_sampling_functions() const
{
if(features.target_api==OPENGL_ES)
return check_version(Version(3, 0));
return check_version(Version(1, 30));
}
-void FeatureConverter::visit(FunctionCall &call)
+void StructuralFeatureConverter::visit(FunctionCall &call)
{
if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
{
TraversingVisitor::visit(call);
}
-bool FeatureConverter::supports_interface_layouts() const
+void StructuralFeatureConverter::visit(VariableDeclaration &var)
+{
+ if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
+ if(stage->type==Stage::FRAGMENT && var.interface=="out")
+ {
+ frag_out = &var;
+ nodes_to_remove.insert(&var);
+ }
+
+ TraversingVisitor::visit(var);
+}
+
+bool StructuralFeatureConverter::supports_interface_blocks(const string &iface) const
+{
+ if(features.target_api==OPENGL_ES)
+ {
+ if(iface=="uniform")
+ return check_version(Version(3, 0));
+ else
+ return check_version(Version(3, 20));
+ }
+ else if(check_version(Version(1, 50)))
+ return true;
+ else if(iface=="uniform")
+ return check_extension(&Features::arb_uniform_buffer_object);
+ else
+ return false;
+}
+
+void StructuralFeatureConverter::visit(InterfaceBlock &iface)
+{
+ bool push_constant = false;
+ if(iface.layout)
+ {
+ auto i = find_member(iface.layout->qualifiers, string("push_constant"), &Layout::Qualifier::name);
+ push_constant = (i!=iface.layout->qualifiers.end());
+ }
+
+ if((!supports_interface_blocks(iface.interface) || push_constant) && iface.type_declaration)
+ {
+ if(!iface.instance_name.empty())
+ unsupported("ARB_uniform_buffer_object required for interface block instances");
+ else if(iface.struct_declaration)
+ {
+ for(const RefPtr<Statement> &s: iface.struct_declaration->members.body)
+ if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(s.get()))
+ var->interface = iface.interface;
+ stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
+ nodes_to_remove.insert(&iface);
+ nodes_to_remove.insert(iface.struct_declaration);
+ }
+ else
+ /* If the interface block is an array, it should have an instance
+ name too, so this should never be reached */
+ throw logic_error("Unexpected interface block configuration");
+ }
+}
+
+
+void QualifierConverter::apply()
+{
+ stage->content.visit(*this);
+}
+
+bool QualifierConverter::supports_interface_layouts() const
{
if(features.target_api==OPENGL_ES)
return check_version(Version(3, 0));
return false;
}
-bool FeatureConverter::supports_stage_interface_layouts() const
+bool QualifierConverter::supports_stage_interface_layouts() const
{
if(features.target_api==OPENGL_ES)
return check_version(Version(3, 10));
return check_extension(&Features::arb_separate_shader_objects);
}
-bool FeatureConverter::supports_centroid_sampling() const
+bool QualifierConverter::supports_centroid_sampling() const
{
if(features.target_api==OPENGL_ES)
return check_version(Version(3, 0));
return check_extension(&Features::ext_gpu_shader4);
}
-bool FeatureConverter::supports_sample_sampling() const
+bool QualifierConverter::supports_sample_sampling() const
{
if(features.target_api==OPENGL_ES)
return check_version(Version(3, 20));
return check_extension(&Features::arb_gpu_shader5);
}
-bool FeatureConverter::supports_uniform_location() const
+bool QualifierConverter::supports_uniform_location() const
{
if(features.target_api==OPENGL_ES)
return check_version(Version(3, 10));
return check_extension(&Features::arb_explicit_uniform_location);
}
-bool FeatureConverter::supports_binding() const
+bool QualifierConverter::supports_binding() const
{
if(features.target_api==OPENGL_ES)
return check_version(Version(3, 10));
return check_version(Version(4, 20));
}
-void FeatureConverter::visit(VariableDeclaration &var)
+void QualifierConverter::visit(VariableDeclaration &var)
{
if(var.layout)
{
var.sampling = string();
}
- if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
- {
- if(stage->type==Stage::FRAGMENT && var.interface=="out")
- {
- frag_out = &var;
- nodes_to_remove.insert(&var);
- }
- }
-
if(var.name=="gl_ClipDistance")
if(const Literal *literal_size = dynamic_cast<const Literal *>(var.array_size.get()))
stage->n_clip_distances = literal_size->value.value<int>();
TraversingVisitor::visit(var);
}
-bool FeatureConverter::supports_interface_blocks(const string &iface) const
-{
- if(features.target_api==OPENGL_ES)
- {
- if(iface=="uniform")
- return check_version(Version(3, 0));
- else
- return check_version(Version(3, 20));
- }
- else if(check_version(Version(1, 50)))
- return true;
- else if(iface=="uniform")
- return check_extension(&Features::arb_uniform_buffer_object);
- else
- return false;
-}
-
-bool FeatureConverter::supports_interface_block_location() const
+bool QualifierConverter::supports_interface_block_location() const
{
if(features.target_api==OPENGL_ES)
return check_version(Version(3, 20));
return check_extension(&Features::arb_enhanced_layouts);
}
-void FeatureConverter::visit(InterfaceBlock &iface)
+void QualifierConverter::visit(InterfaceBlock &iface)
{
- bool push_constant = false;
if(iface.layout)
{
for(auto i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
i = iface.layout->qualifiers.erase(i);
}
else
- {
- if(i->name=="push_constant")
- push_constant = true;
++i;
- }
}
if(iface.layout->qualifiers.empty())
iface.layout = 0;
}
-
- if((!supports_interface_blocks(iface.interface) || push_constant) && iface.type_declaration)
- {
- if(!iface.instance_name.empty())
- unsupported("ARB_uniform_buffer_object required for interface block instances");
- else if(iface.struct_declaration)
- {
- for(const RefPtr<Statement> &s: iface.struct_declaration->members.body)
- if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(s.get()))
- var->interface = iface.interface;
- stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
- nodes_to_remove.insert(&iface);
- nodes_to_remove.insert(iface.struct_declaration);
- }
- else
- /* If the interface block is an array, it should have an instance
- name too, so this should never be reached */
- throw logic_error("Unexpected interface block configuration");
- }
}
} // namespace SL
virtual void visit(VariableDeclaration &);
};
+/** Base class for feature converters. */
+class FeatureConverter: protected TraversingVisitor
+{
+protected:
+ Stage *stage = 0;
+ Features features;
+
+ FeatureConverter() = default;
+
+public:
+ void apply(Stage &, const Features &);
+protected:
+ virtual void apply() = 0;
+
+ void unsupported(const std::string &);
+
+ bool check_version(const Version &) const;
+ bool check_extension(bool Features::*) const;
+};
+
/** Converts structures of the syntax tree to match a particular set of
features. */
-class FeatureConverter: private TraversingVisitor
+class StructuralFeatureConverter: public FeatureConverter
{
private:
- Stage *stage = 0;
- Features features;
VariableDeclaration *frag_out = 0;
NodeList<Statement>::iterator uniform_insert_point;
std::set<Node *> nodes_to_remove;
bool r_flattened_interface = false;
public:
- virtual void apply(Stage &, const Features &);
-
+ void apply(Stage &s, const Features &f) { FeatureConverter::apply(s, f); }
private:
- void unsupported(const std::string &);
+ virtual void apply();
virtual void visit(Block &);
virtual void visit(RefPtr<Expression> &);
- bool check_version(const Version &) const;
- bool check_extension(bool Features::*) const;
bool supports_stage(Stage::Type) const;
bool supports_unified_interface_syntax() const;
virtual void visit(VariableReference &);
virtual void visit(Assignment &);
bool supports_unified_sampling_functions() const;
virtual void visit(FunctionCall &);
+ virtual void visit(VariableDeclaration &);
+ bool supports_interface_blocks(const std::string &) const;
+ virtual void visit(InterfaceBlock &);
+};
+
+/** Converts qualifiers on variables and blocksto match a particular set of
+features. */
+class QualifierConverter: private FeatureConverter
+{
+public:
+ void apply(Stage &s, const Features &f) { FeatureConverter::apply(s, f); }
+private:
+ virtual void apply();
+
bool supports_interface_layouts() const;
bool supports_stage_interface_layouts() const;
bool supports_centroid_sampling() const;
bool supports_uniform_location() const;
bool supports_binding() const;
virtual void visit(VariableDeclaration &);
- bool supports_interface_blocks(const std::string &) const;
bool supports_interface_block_location() const;
virtual void visit(InterfaceBlock &);
};