From 1b9417220971f271daaf2d4cc7e2a41d34edaeb0 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 16 Oct 2021 16:49:44 +0300 Subject: [PATCH] Save possible outer reference when entering subscript Otherwise it gets corrupted and the outer variable may be incorrectly removed if nothing else refers to it. --- source/glsl/optimize.cpp | 1 + tests/glsl/nested_member_access.glsl | 46 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/glsl/nested_member_access.glsl diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 1e974c9f..08d92599 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -1384,6 +1384,7 @@ void UnusedVariableRemover::visit(BinaryExpression &binary) { SetFlag clear_assignment(assignment_target, false); SetFlag clear_composite(composite_reference, false); + SetForScope clear_reference(r_reference, Assignment::Target()); binary.right->visit(*this); } diff --git a/tests/glsl/nested_member_access.glsl b/tests/glsl/nested_member_access.glsl new file mode 100644 index 00000000..60b3a492 --- /dev/null +++ b/tests/glsl/nested_member_access.glsl @@ -0,0 +1,46 @@ +uniform Lighting +{ + vec3 light_color[4]; +}; +uniform Params +{ + int light_index; +}; + +#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 = vec4(light_color[light_index], 1.0); +} + +/* Expected output: vertex +layout(location=0) in vec4 position; +void main() +{ + gl_Position = position; +} +*/ + +/* Expected output: fragment +layout(binding=5) uniform Lighting +{ + vec3 light_color[4]; +}; +layout(binding=80) uniform Params +{ + int light_index; +}; +layout(location=0) out vec4 frag_color; +void main() +{ + frag_color = vec4(light_color[light_index], 1.0); +} +*/ -- 2.45.2