]> git.tdb.fi Git - libs/gl.git/commitdiff
Reorder declarations in shaders
authorMikko Rasa <tdb@tdb.fi>
Fri, 2 Dec 2016 10:26:12 +0000 (12:26 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 2 Dec 2016 10:26:12 +0000 (12:26 +0200)
So that structs appear before variables and variables appear before
functions.  This ensures that the declarations stay visible if an
expression is moved to an earlier point while inlining functions.

source/programcompiler.cpp
source/programcompiler.h

index fdb2576eff5afb14c6870c02ddd5e013bf2182b3..7224df1efba672d4afb7ecb12923a0109ae9f6f6 100644 (file)
@@ -165,6 +165,7 @@ void ProgramCompiler::generate(Stage &stage)
        apply<InterfaceGenerator>(stage);
        apply<VariableResolver>(stage);
        apply<VariableRenamer>(stage);
+       apply<DeclarationReorderer>(stage);
        apply<LegacyConverter>(stage);
 }
 
@@ -917,6 +918,45 @@ void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var)
 }
 
 
+ProgramCompiler::DeclarationReorderer::DeclarationReorderer():
+       kind(NO_DECLARATION)
+{ }
+
+void ProgramCompiler::DeclarationReorderer::visit(Block &block)
+{
+       list<RefPtr<Node> >::iterator struct_insert_point = block.body.end();
+       list<RefPtr<Node> >::iterator variable_insert_point = block.body.end();
+
+       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+       {
+               kind = NO_DECLARATION;
+               (*i)->visit(*this);
+
+               bool moved = false;
+               if(kind==STRUCT && struct_insert_point!=block.body.end())
+               {
+                       block.body.insert(struct_insert_point, *i);
+                       moved = true;
+               }
+               else if(kind>STRUCT && struct_insert_point==block.body.end())
+                       struct_insert_point = i;
+
+               if(kind==VARIABLE && variable_insert_point!=block.body.end())
+               {
+                       block.body.insert(variable_insert_point, *i);
+                       moved = true;
+               }
+               else if(kind>VARIABLE && variable_insert_point==block.body.end())
+                       variable_insert_point = i;
+
+               if(moved)
+                       block.body.erase(i++);
+               else
+                       ++i;
+       }
+}
+
+
 ProgramCompiler::ExpressionEvaluator::ExpressionEvaluator():
        variable_values(0),
        result(0.0f),
index 398bb214d10568800d69e757dec8eadad82efda5..8c4034d4e906baae7812ee66ecc1b84abff39448 100644 (file)
@@ -156,6 +156,29 @@ private:
                virtual void visit(ProgramSyntax::VariableDeclaration &);
        };
 
+       struct DeclarationReorderer: Visitor
+       {
+               enum DeclarationKind
+               {
+                       NO_DECLARATION,
+                       LAYOUT,
+                       STRUCT,
+                       VARIABLE,
+                       FUNCTION
+               };
+
+               DeclarationKind kind;
+
+               DeclarationReorderer();
+
+               virtual void visit(ProgramSyntax::Block &);
+               virtual void visit(ProgramSyntax::InterfaceLayout &) { kind = LAYOUT; }
+               virtual void visit(ProgramSyntax::StructDeclaration &) { kind = STRUCT; }
+               virtual void visit(ProgramSyntax::VariableDeclaration &) { kind = VARIABLE; }
+               virtual void visit(ProgramSyntax::InterfaceBlock &) { kind = VARIABLE; }
+               virtual void visit(ProgramSyntax::FunctionDeclaration &) { kind = FUNCTION; }
+       };
+
        struct ExpressionEvaluator: ProgramSyntax::NodeVisitor
        {
                typedef std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Expression *> ValueMap;