Defer dependent HLSL global var inits.

Some global initializers depend on other globals, for instance a
varying or attribute value. Since we use a static proxy variable for
these varyings, we need to initialize the global static after we
initialize the proxy in the shader preamble. This fixes a long-
standing compiler bug.

We should also add a WebGL test for this.

BUG=angle:878

Change-Id: I71db103a6b8c24fb862e0d8b32293da9bc2e8103
Reviewed-on: https://chromium-review.googlesource.com/243581
Tested-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/tests/angle_tests/GLSLTest.cpp b/tests/angle_tests/GLSLTest.cpp
index f6b2258..1277576 100644
--- a/tests/angle_tests/GLSLTest.cpp
+++ b/tests/angle_tests/GLSLTest.cpp
@@ -873,4 +873,35 @@
     {
         glDeleteShader(shader);
     }
-}
\ No newline at end of file
+}
+
+// Tests that using a global static initialized from a varying works as expected.
+// See: https://code.google.com/p/angleproject/issues/detail?id=878
+TYPED_TEST(GLSLTest, GlobalStaticAndVarying)
+{
+    const std::string &vertexShaderSource =
+        "attribute vec4 a_position;\n"
+        "varying float v;\n"
+        "void main() {\n"
+        "  gl_Position = a_position;\n"
+        "  v = 1.0;\n"
+        "}\n";
+
+    const std::string &fragmentShaderSource =
+        "precision highp float;\n"
+        "varying float v;\n"
+        "float x = v;"
+        "float global_v = x;"
+        "void main() {\n"
+        "  gl_FragColor = vec4(global_v, 0.0, 0.0, 1.0);\n"
+        "}\n";
+
+    GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
+    ASSERT_NE(0u, program);
+
+    drawQuad(program, "a_position", 0.5f);
+    swapBuffers();
+
+    ASSERT_GL_NO_ERROR();
+    EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
+}