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/compiler/translator/OutputHLSL.cpp b/src/compiler/translator/OutputHLSL.cpp
index 98f7437..3badefd 100644
--- a/src/compiler/translator/OutputHLSL.cpp
+++ b/src/compiler/translator/OutputHLSL.cpp
@@ -882,19 +882,17 @@
       case EOpInitialize:
         if (visit == PreVisit)
         {
-            // GLSL allows to write things like "float x = x;" where a new variable x is defined
-            // and the value of an existing variable x is assigned. HLSL uses C semantics (the
-            // new variable is created before the assignment is evaluated), so we need to convert
-            // this to "float t = x, x = t;".
-
             TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
             ASSERT(symbolNode);
             TIntermTyped *expression = node->getRight();
 
             // Global initializers must be constant at this point.
-            ASSERT(symbolNode->getQualifier() != EvqGlobal ||
-                   (expression->getQualifier() == EvqConst &&
-                    expression->getAsConstantUnion() != nullptr));
+            ASSERT(symbolNode->getQualifier() != EvqGlobal || canWriteAsHLSLLiteral(expression));
+
+            // GLSL allows to write things like "float x = x;" where a new variable x is defined
+            // and the value of an existing variable x is assigned. HLSL uses C semantics (the
+            // new variable is created before the assignment is evaluated), so we need to convert
+            // this to "float t = x, x = t;".
             if (writeSameSymbolInitializer(out, symbolNode, expression))
             {
                 // Skip initializing the rest of the expression
@@ -2654,22 +2652,8 @@
 {
     // We support writing constant unions and constructors that only take constant unions as
     // parameters as HLSL literals.
-    if (expression->getAsConstantUnion())
-    {
-        return true;
-    }
-    if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
-        !expression->getAsAggregate()->isConstructor())
-    {
-        return false;
-    }
-    TIntermAggregate *constructor = expression->getAsAggregate();
-    for (TIntermNode *&node : *constructor->getSequence())
-    {
-        if (!node->getAsConstantUnion())
-            return false;
-    }
-    return true;
+    return expression->getAsConstantUnion() ||
+           expression->isConstructorWithOnlyConstantUnionParameters();
 }
 
 bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,