From: Mikko Rasa Date: Mon, 12 Apr 2021 15:00:34 +0000 (+0300) Subject: Don't ignore structs entirely in UnusedVariableRemover X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=72a02f2f3f1c454aa670b256262d7bc0541222e3;p=libs%2Fgl.git Don't ignore structs entirely in UnusedVariableRemover Structs may have arrays in them, and array sizes may have references to specialization constants which must be kept. --- diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 68d3f1d7..bdaced5c 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -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) diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index e21dd98f..ea61b671 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -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 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 index 00000000..4a6d9ab6 --- /dev/null +++ b/tests/glsl/specialized_array_in_struct.glsl @@ -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; +} +*/