Fix deferring global array initialization
The initial implementation of DeferGlobalInitializers did not take
HLSL corner cases into account. In particular, in case there was a
const-qualified array variable with an initializer that contained
elements that weren't constant folded, initialization would not be
deferred and the global scope of HLSL output would contain a call to
angle_construct_into_*().
On the other hand, deferring global initializers was also done in
cases where it wasn't necessary. Initializers of non-const qualified
array variables that could be written as HLSL literals by HLSL output
were unnecessarily deferred.
This patch fixes both of these issues: Now all global initializers are
potential candidates for deferral instead of just those where the
symbol has the EvqGlobal qualifier, and initializers that are
constructors taking only constant unions as parameters are not
unnecessarily deferred.
BUG=angleproject:1205
BUG=541551
TEST=angle_end2end_tests
Change-Id: I4027059e0e5f39c8a5a48b5c97a3fceaac6b6f8a
Reviewed-on: https://chromium-review.googlesource.com/339201
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
diff --git a/src/tests/gl_tests/GLSLTest.cpp b/src/tests/gl_tests/GLSLTest.cpp
index fd3f18a..e0b1d94 100644
--- a/src/tests/gl_tests/GLSLTest.cpp
+++ b/src/tests/gl_tests/GLSLTest.cpp
@@ -1577,6 +1577,39 @@
EXPECT_NE(0u, program);
}
+// This test was written specifically to stress DeferGlobalInitializers AST transformation.
+// Test a shader where a global constant array is initialized with an expression containing array
+// indexing. This initializer is tricky to constant fold, so if it's not constant folded it needs to
+// be handled in a way that doesn't generate statements in the global scope in HLSL output.
+// Also includes multiple array initializers in one declaration, where only the second one has
+// array indexing. This makes sure that the qualifier for the declaration is set correctly if
+// transformations are applied to the declaration also in the case of ESSL output.
+TEST_P(GLSLTest_ES3, InitGlobalArrayWithArrayIndexing)
+{
+ const std::string vertexShaderSource =
+ "#version 300 es\n"
+ "precision highp float;\n"
+ "in vec4 a_vec;\n"
+ "void main()\n"
+ "{\n"
+ " gl_Position = vec4(a_vec);\n"
+ "}";
+
+ const std::string fragmentShaderSource =
+ "#version 300 es\n"
+ "precision highp float;\n"
+ "out vec4 my_FragColor;\n"
+ "const highp float f[2] = float[2](0.1, 0.2);\n"
+ "const highp float[2] g = float[2](0.3, 0.4), h = float[2](0.5, f[1]);\n"
+ "void main()\n"
+ "{\n"
+ " my_FragColor = vec4(h[1]);\n"
+ "}";
+
+ GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
+ EXPECT_NE(0u, program);
+}
+
// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
ANGLE_INSTANTIATE_TEST(GLSLTest,
ES2_D3D9(),