]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/optimize.h
Break up struct variables if they're not used as a whole
[libs/gl.git] / source / glsl / optimize.h
index ae55e5af10f5590f886906355342327a8ddb87be..1eb0464e7daea97d654576c15792a13e764d8c27 100644 (file)
@@ -151,6 +151,51 @@ private:
        virtual void visit(Iteration &);
 };
 
+/**
+Breaks aggregates up into separate variables if only the individual fields are
+accessed and not the aggregate as a whole.
+*/
+class AggregateDismantler: public TraversingVisitor
+{
+private:
+       struct AggregateMember
+       {
+               const VariableDeclaration *declaration = 0;
+               RefPtr<Expression> initializer;
+               std::vector<RefPtr<Expression> *> references;
+       };
+
+       struct Aggregate
+       {
+               VariableDeclaration *declaration = 0;
+               Block *decl_scope = 0;
+               NodeList<Statement>::iterator insert_point;
+               std::vector<AggregateMember> members;
+               bool referenced = false;
+               bool members_referenced = false;
+       };
+
+       NodeList<Statement>::iterator insert_point;
+       std::map<Statement *, Aggregate> aggregates;
+       bool composite_reference = false;
+       Assignment::Target r_reference;
+       Aggregate *r_aggregate_ref = 0;
+
+public:
+       bool apply(Stage &);
+
+private:
+       virtual void visit(Block &);
+       virtual void visit(RefPtr<Expression> &);
+       virtual void visit(VariableReference &);
+       void visit_composite(RefPtr<Expression> &);
+       virtual void visit(MemberAccess &);
+       virtual void visit(BinaryExpression &);
+       virtual void visit(StructDeclaration &) { }
+       virtual void visit(VariableDeclaration &);
+       virtual void visit(InterfaceBlock &) { }
+};
+
 /** Replaces expressions consisting entirely of literals with the results of
 evaluating the expression.*/
 class ConstantFolder: private TraversingVisitor