Only initialize globals if initialization flag is set
This avoids the possibility of initializing globals twice in Chromium.
Now we also never initialize variables redundantly in case we're
writing HLSL output. This was already the intent of the code before,
but a mistake had slipped in and the code didn't actually check the
output type properly.
This also simplifies DeferGlobalInitializers by running it after
SeparateDeclarations.
BUG=chromium:735497
TEST=WebGL conformance tests
Change-Id: I95036a24ac8cf18113755510376a2fca286b3ee6
Reviewed-on: https://chromium-review.googlesource.com/771555
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
diff --git a/src/compiler/translator/Compiler.cpp b/src/compiler/translator/Compiler.cpp
index 9cb79b4..7b3418e 100644
--- a/src/compiler/translator/Compiler.cpp
+++ b/src/compiler/translator/Compiler.cpp
@@ -45,6 +45,7 @@
#include "compiler/translator/ValidateVaryingLocations.h"
#include "compiler/translator/VariablePacker.h"
#include "compiler/translator/VectorizeVectorScalarArithmetic.h"
+#include "compiler/translator/util.h"
#include "third_party/compiler/ArrayBoundsClamper.h"
namespace sh
@@ -571,8 +572,6 @@
&symbolTable, shaderVersion);
}
- DeferGlobalInitializers(root, needToInitializeGlobalsInAST(), &symbolTable);
-
// Split multi declarations and remove calls to array length().
// Note that SimplifyLoopConditions needs to be run before any other AST transformations
// that may need to generate new statements from loop conditions or loop expressions.
@@ -590,7 +589,15 @@
RemoveArrayLengthMethod(root);
- if ((compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && getOutputType())
+ // DeferGlobalInitializers needs to be run before other AST transformations that generate new
+ // statements from expressions. But it's fine to run DeferGlobalInitializers after the above
+ // SplitSequenceOperator and RemoveArrayLengthMethod since they only have an effect on the AST
+ // on ESSL >= 3.00, and the initializers that need to be deferred can only exist in ESSL < 3.00.
+ bool initializeLocalsAndGlobals =
+ (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && !IsOutputHLSL(getOutputType());
+ DeferGlobalInitializers(root, initializeLocalsAndGlobals, &symbolTable);
+
+ if (initializeLocalsAndGlobals)
{
// Initialize uninitialized local variables.
// In some cases initializing can generate extra statements in the parent block, such as