]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/finalize.cpp
Fix flattening of GLSL interface blocks
[libs/gl.git] / source / glsl / finalize.cpp
index eee8a5198efa8b2037848bcc7a34bd8cd0d333ff..f005d87dcbf82749e33eb1ba8c55fdbece642b76 100644 (file)
@@ -12,10 +12,6 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-StructOrganizer::StructOrganizer():
-       offset(-1)
-{ }
-
 void StructOrganizer::visit(StructDeclaration &strct)
 {
        SetForScope<int> set_offset(offset, 0);
@@ -238,10 +234,6 @@ void LocationAllocator::visit(InterfaceBlock &iface)
 }
 
 
-PrecisionConverter::PrecisionConverter():
-       stage(0)
-{ }
-
 void PrecisionConverter::apply(Stage &s)
 {
        stage = &s;
@@ -261,7 +253,7 @@ void PrecisionConverter::visit(Block &block)
 
 void PrecisionConverter::visit(Precision &prec)
 {
-       if(stage->required_features.gl_api==OPENGL_ES2)
+       if(stage->required_features.target_api==OPENGL_ES)
                have_default.insert(prec.type);
        else
                nodes_to_remove.insert(&prec);
@@ -269,7 +261,7 @@ void PrecisionConverter::visit(Precision &prec)
 
 void PrecisionConverter::visit(VariableDeclaration &var)
 {
-       if(stage->required_features.gl_api!=OPENGL_ES2)
+       if(stage->required_features.target_api!=OPENGL_ES)
        {
                var.precision.clear();
                return;
@@ -308,10 +300,6 @@ void PrecisionConverter::visit(VariableDeclaration &var)
 }
 
 
-LegacyConverter::LegacyConverter():
-       frag_out(0)
-{ }
-
 void LegacyConverter::apply(Stage &s, const Features &feat)
 {
        stage = &s;
@@ -322,7 +310,7 @@ void LegacyConverter::apply(Stage &s, const Features &feat)
                NodeRemover().apply(s, nodes_to_remove);
 
                if(!stage->required_features.glsl_version)
-                       stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
+                       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)));
@@ -348,6 +336,15 @@ void LegacyConverter::visit(Block &block)
        }
 }
 
+void LegacyConverter::visit(RefPtr<Expression> &expr)
+{
+       r_replaced_reference = 0;
+       expr->visit(*this);
+       if(r_replaced_reference)
+               expr = r_replaced_reference;
+       r_replaced_reference = 0;
+}
+
 bool LegacyConverter::check_version(const Version &feature_version) const
 {
        if(features.glsl_version<feature_version)
@@ -372,7 +369,7 @@ bool LegacyConverter::supports_stage(Stage::Type st) const
 {
        if(st==Stage::GEOMETRY)
        {
-               if(features.gl_api==OPENGL_ES2)
+               if(features.target_api==OPENGL_ES)
                        return check_version(Version(3, 20));
                else
                        return check_version(Version(1, 50));
@@ -383,7 +380,7 @@ bool LegacyConverter::supports_stage(Stage::Type st) const
 
 bool LegacyConverter::supports_unified_interface_syntax() const
 {
-       if(features.gl_api==OPENGL_ES2)
+       if(features.target_api==OPENGL_ES)
                return check_version(Version(3, 0));
        else
                return check_version(Version(1, 30));
@@ -398,6 +395,23 @@ void LegacyConverter::visit(VariableReference &var)
        }
 }
 
+void LegacyConverter::visit(InterfaceBlockReference &iface)
+{
+       r_flattened_interface = nodes_to_remove.count(iface.declaration);
+}
+
+void LegacyConverter::visit(MemberAccess &memacc)
+{
+       r_flattened_interface = false;
+       visit(memacc.left);
+       if(r_flattened_interface)
+       {
+               VariableReference *var = new VariableReference;
+               var->name = memacc.member;
+               r_replaced_reference = var;
+       }
+}
+
 void LegacyConverter::visit(Assignment &assign)
 {
        TraversingVisitor::visit(assign);
@@ -407,7 +421,7 @@ void LegacyConverter::visit(Assignment &assign)
 
 bool LegacyConverter::supports_unified_sampling_functions() const
 {
-       if(features.gl_api==OPENGL_ES2)
+       if(features.target_api==OPENGL_ES)
                return check_version(Version(3, 0));
        else
                return check_version(Version(1, 30));
@@ -452,7 +466,7 @@ void LegacyConverter::visit(FunctionCall &call)
 
 bool LegacyConverter::supports_interface_layouts() const
 {
-       if(features.gl_api==OPENGL_ES2)
+       if(features.target_api==OPENGL_ES)
                return check_version(Version(3, 0));
        else if(check_version(Version(3, 30)))
                return true;
@@ -464,7 +478,7 @@ bool LegacyConverter::supports_interface_layouts() const
 
 bool LegacyConverter::supports_stage_interface_layouts() const
 {
-       if(features.gl_api==OPENGL_ES2)
+       if(features.target_api==OPENGL_ES)
                return check_version(Version(3, 10));
        else if(check_version(Version(4, 10)))
                return true;
@@ -474,7 +488,7 @@ bool LegacyConverter::supports_stage_interface_layouts() const
 
 bool LegacyConverter::supports_centroid_sampling() const
 {
-       if(features.gl_api==OPENGL_ES2)
+       if(features.target_api==OPENGL_ES)
                return check_version(Version(3, 0));
        else if(check_version(Version(1, 20)))
                return true;
@@ -484,7 +498,7 @@ bool LegacyConverter::supports_centroid_sampling() const
 
 bool LegacyConverter::supports_sample_sampling() const
 {
-       if(features.gl_api==OPENGL_ES2)
+       if(features.target_api==OPENGL_ES)
                return check_version(Version(3, 20));
        else if(check_version(Version(4, 0)))
                return true;
@@ -494,7 +508,7 @@ bool LegacyConverter::supports_sample_sampling() const
 
 bool LegacyConverter::supports_uniform_location() const
 {
-       if(features.gl_api==OPENGL_ES2)
+       if(features.target_api==OPENGL_ES)
                return check_version(Version(3, 10));
        else if(check_version(Version(4, 30)))
                return true;
@@ -504,7 +518,7 @@ bool LegacyConverter::supports_uniform_location() const
 
 bool LegacyConverter::supports_binding() const
 {
-       if(features.gl_api==OPENGL_ES2)
+       if(features.target_api==OPENGL_ES)
                return check_version(Version(3, 10));
        else
                return check_version(Version(4, 20));
@@ -586,12 +600,16 @@ void LegacyConverter::visit(VariableDeclaration &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 LegacyConverter::supports_interface_blocks(const string &iface) const
 {
-       if(features.gl_api==OPENGL_ES2)
+       if(features.target_api==OPENGL_ES)
        {
                if(iface=="uniform")
                        return check_version(Version(3, 0));
@@ -608,7 +626,7 @@ bool LegacyConverter::supports_interface_blocks(const string &iface) const
 
 bool LegacyConverter::supports_interface_block_location() const
 {
-       if(features.gl_api==OPENGL_ES2)
+       if(features.target_api==OPENGL_ES)
                return check_version(Version(3, 20));
        else if(check_version(Version(4, 40)))
                return true;
@@ -643,6 +661,9 @@ void LegacyConverter::visit(InterfaceBlock &iface)
                        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);