]> git.tdb.fi Git - libs/gl.git/commitdiff
Don't ignore structs entirely in UnusedVariableRemover
authorMikko Rasa <tdb@tdb.fi>
Mon, 12 Apr 2021 15:00:34 +0000 (18:00 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 12 Apr 2021 15:00:34 +0000 (18:00 +0300)
Structs may have arrays in them, and array sizes may have references to
specialization constants which must be kept.

source/glsl/optimize.cpp
source/glsl/optimize.h
tests/glsl/specialized_array_in_struct.glsl [new file with mode: 0644]

index 68d3f1d77a9b1233701629b2f27b319bfec43e3d..bdaced5c46029299e531b8f3cdbd5055651bc539 100644 (file)
@@ -1010,6 +1010,7 @@ UnusedVariableRemover::UnusedVariableRemover():
        r_assignment(0),
        assignment_target(false),
        r_side_effects(false),
+       in_struct(false),
        composite_reference(false)
 { }
 
@@ -1233,8 +1234,19 @@ void UnusedVariableRemover::visit(ExpressionStatement &expr)
                unused_nodes.insert(&expr);
 }
 
+void UnusedVariableRemover::visit(StructDeclaration &strct)
+{
+       SetFlag set_struct(in_struct);
+       TraversingVisitor::visit(strct);
+}
+
 void UnusedVariableRemover::visit(VariableDeclaration &var)
 {
+       TraversingVisitor::visit(var);
+
+       if(in_struct)
+               return;
+
        VariableInfo &var_info = variables[&var];
        var_info.interface_block = interface_block;
 
@@ -1250,7 +1262,6 @@ void UnusedVariableRemover::visit(VariableDeclaration &var)
                var_info.initialized = true;
                record_assignment(&var, *var.init_expression);
        }
-       TraversingVisitor::visit(var);
 }
 
 void UnusedVariableRemover::visit(InterfaceBlock &iface)
index e21dd98fd0fcbe63e578e3012cd072d108c5437d..ea61b671d2e1ea2a68b10c4b4682e8cb6a560260 100644 (file)
@@ -300,6 +300,7 @@ private:
        Assignment *r_assignment;
        bool assignment_target;
        bool r_side_effects;
+       bool in_struct;
        bool composite_reference;
        Assignment::Target r_reference;
        std::set<Node *> unused_nodes;
@@ -323,8 +324,7 @@ private:
        virtual void visit(FunctionCall &);
        void record_assignment(const Assignment::Target &, Node &);
        virtual void visit(ExpressionStatement &);
-       // Ignore structs because their members can't be accessed directly.
-       virtual void visit(StructDeclaration &) { }
+       virtual void visit(StructDeclaration &);
        virtual void visit(VariableDeclaration &);
        virtual void visit(InterfaceBlock &);
        void merge_variables(const BlockVariableMap &);
diff --git a/tests/glsl/specialized_array_in_struct.glsl b/tests/glsl/specialized_array_in_struct.glsl
new file mode 100644 (file)
index 0000000..4a6d9ab
--- /dev/null
@@ -0,0 +1,50 @@
+layout(constant_id=auto) const int max_colors = 2;
+struct ColorInfo
+{
+       vec4 color;
+};
+uniform Colors
+{
+       ColorInfo colors[max_colors];
+};
+
+#pragma MSP stage(vertex)
+layout(location=0) in vec4 position;
+void main()
+{
+       gl_Position = position;
+}
+
+#pragma MSP stage(fragment)
+layout(location=0) out vec4 frag_color;
+void main()
+{
+       frag_color = colors[0].color;
+}
+
+// Compile mode: module
+
+/* Expected output: vertex
+layout(location=0) in vec4 position;
+void main()
+{
+       gl_Position = position;
+}
+*/
+
+/* Expected output: fragment
+layout(constant_id=953017667) const int max_colors = 2;
+struct ColorInfo
+{
+       vec4 color;
+};
+layout(binding=23) uniform Colors
+{
+       ColorInfo colors[max_colors];
+};
+layout(location=0) out vec4 frag_color;
+void main()
+{
+       frag_color = colors[0].color;
+}
+*/