]> git.tdb.fi Git - libs/gl.git/commitdiff
Recognize increment/decrement operators as modifying the target variable
authorMikko Rasa <tdb@tdb.fi>
Mon, 8 Nov 2021 13:21:18 +0000 (15:21 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 8 Nov 2021 13:21:50 +0000 (15:21 +0200)
source/glsl/reflect.cpp
source/glsl/reflect.h
source/glsl/spirv.cpp

index 6027acde36862363a6597bebe60db488fa666351..811679f3295c320b30d2caaf8bb92d7ac164f992 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/core/algorithm.h>
+#include <msp/core/raii.h>
 #include "reflect.h"
 
 using namespace std;
@@ -387,16 +388,44 @@ void DependencyCollector::visit(FunctionDeclaration &func)
 }
 
 
-set<VariableDeclaration *> AssignmentCollector::apply(Node &node)
+set<Node *> AssignmentCollector::apply(Node &node)
 {
        node.visit(*this);
        return assigned_variables;
 }
 
+void AssignmentCollector::visit(VariableReference &var)
+{
+       if(assignment_target)
+               assigned_variables.insert(var.declaration);
+}
+
+void AssignmentCollector::visit(InterfaceBlockReference &iface)
+{
+       if(assignment_target)
+               assigned_variables.insert(iface.declaration);
+}
+
+void AssignmentCollector::visit(UnaryExpression &unary)
+{
+       SetFlag set_assignment(assignment_target, (unary.oper->token[1]=='+' || unary.oper->token[1]=='-'));
+       TraversingVisitor::visit(unary);
+}
+
+void AssignmentCollector::visit(BinaryExpression &binary)
+{
+       binary.left->visit(*this);
+       SetFlag clear_assignment(assignment_target, false);
+       binary.right->visit(*this);
+}
+
 void AssignmentCollector::visit(Assignment &assign)
 {
-       if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(assign.target.declaration))
-               assigned_variables.insert(var);
+       {
+               SetFlag set_assignment(assignment_target);
+               assign.left->visit(*this);
+       }
+       assign.right->visit(*this);
 }
 
 } // namespace SL
index 5b4170a9c766e460d34da7157fe84f1d68dbbeec..7c4d26c47fe6714321d2d50588e877053c3bcb5b 100644 (file)
@@ -107,12 +107,17 @@ private:
 class AssignmentCollector: private TraversingVisitor
 {
 private:
-       std::set<VariableDeclaration *> assigned_variables;
+       bool assignment_target = false;
+       std::set<Node *> assigned_variables;
 
 public:
-       std::set<VariableDeclaration *> apply(Node &);
+       std::set<Node *> apply(Node &);
 
 private:
+       virtual void visit(VariableReference &);
+       virtual void visit(InterfaceBlockReference &);
+       virtual void visit(UnaryExpression &);
+       virtual void visit(BinaryExpression &);
        virtual void visit(Assignment &);
 };
 
index 7bec551b166fd4b3303c23591c24886f1baac9b6..2dcc285c624a2806dab80d3a6e487783138f732f 100644 (file)
@@ -1873,8 +1873,9 @@ void SpirVGenerator::visit(Iteration &iter)
        if(iter.init_statement)
                iter.init_statement->visit(*this);
 
-       for(VariableDeclaration *v: AssignmentCollector().apply(iter))
-               variable_load_ids.erase(v);
+       for(Node *n: AssignmentCollector().apply(iter))
+               if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(n))
+                       variable_load_ids.erase(var);
 
        Id header_id = next_id++;
        Id continue_id = next_id++;