From: Mikko Rasa Date: Wed, 3 Mar 2021 11:02:55 +0000 (+0200) Subject: Handle all constructs when inlining GLSL functions X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f76e62a74917dbd850912587a5e79240dec9681f;p=libs%2Fgl.git Handle all constructs when inlining GLSL functions We need to look for function calls inside conditional and iteration statements too, and inline calls within condition expressions. --- diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 08455c8a..b7236feb 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -41,18 +41,21 @@ void InlineableFunctionLocator::visit(FunctionDeclaration &func) TraversingVisitor::visit(func); } -void InlineableFunctionLocator::visit(Conditional &) +void InlineableFunctionLocator::visit(Conditional &cond) { + TraversingVisitor::visit(cond); inlineable.erase(current_function); } -void InlineableFunctionLocator::visit(Iteration &) +void InlineableFunctionLocator::visit(Iteration &iter) { + TraversingVisitor::visit(iter); inlineable.erase(current_function); } -void InlineableFunctionLocator::visit(Return &) +void InlineableFunctionLocator::visit(Return &ret) { + TraversingVisitor::visit(ret); if(return_count) inlineable.erase(current_function); ++return_count; @@ -274,6 +277,22 @@ void FunctionInliner::visit(FunctionDeclaration &func) TraversingVisitor::visit(func); } +void FunctionInliner::visit(Conditional &cond) +{ + visit_and_inline(cond.condition); + cond.body.visit(*this); +} + +void FunctionInliner::visit(Iteration &iter) +{ + SetForScope set_block(current_block, &iter.body); + if(iter.init_statement) + iter.init_statement->visit(*this); + /* Skip the condition and loop expression parts because they're executed on + every iteration of the loop */ + iter.body.visit(*this); +} + void FunctionInliner::visit(Return &ret) { if(ret.expression) diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index 390dd64e..e74e6e0e 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -93,6 +93,8 @@ private: virtual void visit(ExpressionStatement &); virtual void visit(VariableDeclaration &); virtual void visit(FunctionDeclaration &); + virtual void visit(Conditional &); + virtual void visit(Iteration &); virtual void visit(Return &); };