From: Mikko Rasa Date: Mon, 8 Nov 2021 00:01:26 +0000 (+0200) Subject: Process loop initialization outside the body in UnusedVariableRemover X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=241cf36a6d7735706804bb3c517529bbe078f1ee Process loop initialization outside the body in UnusedVariableRemover If it's processed inside the scope of the loop, assignments to the iteration variable may get discarded. --- diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 3cb10e86..8008465e 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -1574,8 +1574,14 @@ void UnusedVariableRemover::visit(Iteration &iter) vector saved_refs; swap(loop_ext_refs, saved_refs); { + if(iter.init_statement) + iter.init_statement->visit(*this); SetForScope set_loop(in_loop, in_loop+1); - TraversingVisitor::visit(iter); + if(iter.condition) + iter.condition->visit(*this); + iter.body.visit(*this); + if(iter.loop_expression) + iter.loop_expression->visit(*this); } swap(loop_ext_refs, saved_refs); diff --git a/tests/glsl/complex_loop_increment.glsl b/tests/glsl/complex_loop_increment.glsl new file mode 100644 index 00000000..fa0ea86e --- /dev/null +++ b/tests/glsl/complex_loop_increment.glsl @@ -0,0 +1,30 @@ +#pragma MSP stage(vertex) +void main() +{ + int n = 0; + for(int i=10; i!=1; ) + { + if((i&1)==0) + i /= 2; + else + i = 3*i+1; + ++n; + } + gl_Position = vec4(n); +} + +/* Expected output: vertex +void main() +{ + int n = 0; + for(int i = 10; i!=1;) + { + if((i&1)==0) + i /= 2; + else + i = 3*i+1; + ++n; + } + gl_Position = vec4(0.0); +} +*/