Add work-around for D3D9 shader compiler bug.

With certain selection statements with a vertex input in the
condition and side-effects in the else-block, we'd run in to
a D3D9 compiler bug which would cause incorrect results.

We can work around this bug in D3D9 by selectively rewriting
these statements to use an 'else if' clause instead of 'else'.

BUG=322794

Change-Id: I93c96fb201ff4959c00d9a36321faac7e0343278
Reviewed-on: https://chromium-review.googlesource.com/184681
Reviewed-by: Nicolas Capens <nicolascapens@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Tested-by: Jamie Madill <jmadill@chromium.org>
diff --git a/src/compiler/translator/BaseTypes.h b/src/compiler/translator/BaseTypes.h
index 1f378d5..59c4f28 100644
--- a/src/compiler/translator/BaseTypes.h
+++ b/src/compiler/translator/BaseTypes.h
@@ -294,6 +294,7 @@
 {
     EvqTemporary,     // For temporaries (within a function), read/write
     EvqGlobal,        // For globals read/write
+    EvqInternal,      // For internal use, not visible to the user
     EvqConst,         // User defined constants and non-output parameters in functions
     EvqAttribute,     // Readonly
     EvqVaryingIn,     // readonly, fragment shaders only
diff --git a/src/compiler/translator/Common.h b/src/compiler/translator/Common.h
index f1a67fe..1e4503e 100644
--- a/src/compiler/translator/Common.h
+++ b/src/compiler/translator/Common.h
@@ -11,8 +11,12 @@
 #include <sstream>
 #include <string>
 #include <vector>
+#include <limits>
+#include <stdio.h>
 
 #include "compiler/translator/PoolAlloc.h"
+#include "compiler/translator/compilerdebug.h"
+#include "common/angleutils.h"
 
 struct TSourceLoc {
     int first_file;
@@ -74,4 +78,15 @@
     TMap(const tAllocator& a) : std::map<K, D, CMP, tAllocator>(std::map<K, D, CMP, tAllocator>::key_compare(), a) {}
 };
 
+// Integer to TString conversion
+template <typename T>
+inline TString str(T i)
+{
+    ASSERT(std::numeric_limits<T>::is_integer);
+    char buffer[((8 * sizeof(T)) / 3) + 3];
+    const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
+    snprintf(buffer, sizeof(buffer), formatStr, i);
+    return buffer;
+}
+
 #endif // _COMMON_INCLUDED_
diff --git a/src/compiler/translator/NodeSearch.h b/src/compiler/translator/NodeSearch.h
index 27e471d..b58c7ec 100644
--- a/src/compiler/translator/NodeSearch.h
+++ b/src/compiler/translator/NodeSearch.h
@@ -9,6 +9,8 @@
 #ifndef TRANSLATOR_NODESEARCH_H_
 #define TRANSLATOR_NODESEARCH_H_
 
+#include "compiler/translator/intermediate.h"
+
 namespace sh
 {
 
diff --git a/src/compiler/translator/OutputHLSL.cpp b/src/compiler/translator/OutputHLSL.cpp
index c25ca86..0a7b930 100644
--- a/src/compiler/translator/OutputHLSL.cpp
+++ b/src/compiler/translator/OutputHLSL.cpp
@@ -16,6 +16,7 @@
 #include "compiler/translator/HLSLLayoutEncoder.h"
 #include "compiler/translator/FlagStd140Structs.h"
 #include "compiler/translator/NodeSearch.h"
+#include "compiler/translator/RewriteElseBlocks.h"
 
 #include <algorithm>
 #include <cfloat>
@@ -23,16 +24,6 @@
 
 namespace sh
 {
-// Integer to TString conversion
-template <typename T>
-TString str(T i)
-{
-    ASSERT(std::numeric_limits<T>::is_integer);
-    char buffer[(CHAR_BIT * sizeof(T) / 3) + 3];
-    const char *formatStr = std::numeric_limits<T>::is_signed ? "%d" : "%u";
-    snprintf(buffer, sizeof(buffer), formatStr, i);
-    return buffer;
-}
 
 TString OutputHLSL::TextureFunction::name() const
 {
@@ -173,6 +164,13 @@
     const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(mContext.treeRoot);
     makeFlaggedStructMaps(flaggedStructs);
 
+    // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which
+    // use a vertex attribute as a condition, and some related computation in the else block.
+    if (mOutputType == SH_HLSL9_OUTPUT && mContext.shaderType == SH_VERTEX_SHADER)
+    {
+        RewriteElseBlocks(mContext.treeRoot);
+    }
+
     mContext.treeRoot->traverse(this);   // Output the body first to determine what has to go in the header
     header();
 
@@ -1674,6 +1672,10 @@
             mUsesFragDepth = true;
             out << "gl_Depth";
         }
+        else if (qualifier == EvqInternal)
+        {
+            out << name;
+        }
         else
         {
             out << decorate(name);
diff --git a/src/compiler/translator/RewriteElseBlocks.cpp b/src/compiler/translator/RewriteElseBlocks.cpp
new file mode 100644
index 0000000..48e87cd
--- /dev/null
+++ b/src/compiler/translator/RewriteElseBlocks.cpp
@@ -0,0 +1,98 @@
+//
+// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+// RewriteElseBlocks.cpp: Implementation for tree transform to change
+//   all if-else blocks to if-if blocks.
+//
+
+#include "compiler/translator/RewriteElseBlocks.h"
+#include "compiler/translator/NodeSearch.h"
+#include "compiler/translator/SymbolTable.h"
+
+namespace sh
+{
+
+TIntermSymbol *MakeNewTemporary(const TString &name, TBasicType type)
+{
+    TType variableType(type, EbpHigh, EvqInternal);
+    return new TIntermSymbol(-1, name, variableType);
+}
+
+TIntermBinary *MakeNewBinary(TOperator op, TIntermTyped *left, TIntermTyped *right, const TType &resultType)
+{
+    TIntermBinary *binary = new TIntermBinary(op);
+    binary->setLeft(left);
+    binary->setRight(right);
+    binary->setType(resultType);
+    return binary;
+}
+
+TIntermUnary *MakeNewUnary(TOperator op, TIntermTyped *operand)
+{
+    TIntermUnary *unary = new TIntermUnary(op, operand->getType());
+    unary->setOperand(operand);
+    return unary;
+}
+
+bool ElseBlockRewriter::visitAggregate(Visit visit, TIntermAggregate *node)
+{
+    switch (node->getOp())
+    {
+      case EOpSequence:
+        {
+            for (size_t statementIndex = 0; statementIndex != node->getSequence().size(); statementIndex++)
+            {
+                TIntermNode *statement = node->getSequence()[statementIndex];
+                TIntermSelection *selection = statement->getAsSelectionNode();
+                if (selection && selection->getFalseBlock() != NULL)
+                {
+                    node->getSequence()[statementIndex] = rewriteSelection(selection);
+                    delete selection;
+                }
+            }
+        }
+        break;
+
+      default: break;
+    }
+
+    return true;
+}
+
+TIntermNode *ElseBlockRewriter::rewriteSelection(TIntermSelection *selection)
+{
+    ASSERT(selection->getFalseBlock() != NULL);
+
+    TString temporaryName = "cond_" + str(mTemporaryIndex++);
+    TIntermTyped *typedCondition = selection->getCondition()->getAsTyped();
+    TType resultType(EbtBool, EbpUndefined);
+    TIntermSymbol *conditionSymbolA = MakeNewTemporary(temporaryName, EbtBool);
+    TIntermSymbol *conditionSymbolB = MakeNewTemporary(temporaryName, EbtBool);
+    TIntermSymbol *conditionSymbolC = MakeNewTemporary(temporaryName, EbtBool);
+    TIntermBinary *storeCondition = MakeNewBinary(EOpInitialize, conditionSymbolA,
+                                                  typedCondition, resultType);
+    TIntermUnary *negatedCondition = MakeNewUnary(EOpLogicalNot, conditionSymbolB);
+    TIntermSelection *falseBlock = new TIntermSelection(negatedCondition,
+                                                        selection->getFalseBlock(), NULL);
+    TIntermSelection *newIfElse = new TIntermSelection(conditionSymbolC,
+                                                       selection->getTrueBlock(), falseBlock);
+
+    TIntermAggregate *declaration = new TIntermAggregate(EOpDeclaration);
+    declaration->getSequence().push_back(storeCondition);
+
+    TIntermAggregate *block = new TIntermAggregate(EOpSequence);
+    block->getSequence().push_back(declaration);
+    block->getSequence().push_back(newIfElse);
+
+    return block;
+}
+
+void RewriteElseBlocks(TIntermNode *node)
+{
+    ElseBlockRewriter rewriter;
+    node->traverse(&rewriter);
+}
+
+}
diff --git a/src/compiler/translator/RewriteElseBlocks.h b/src/compiler/translator/RewriteElseBlocks.h
new file mode 100644
index 0000000..1022133
--- /dev/null
+++ b/src/compiler/translator/RewriteElseBlocks.h
@@ -0,0 +1,39 @@
+//
+// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+// RewriteElseBlocks.h: Prototype for tree transform to change
+//   all if-else blocks to if-if blocks.
+//
+
+#ifndef COMPILER_REWRITE_ELSE_BLOCKS_H_
+#define COMPILER_REWRITE_ELSE_BLOCKS_H_
+
+#include "compiler/translator/intermediate.h"
+
+namespace sh
+{
+
+class ElseBlockRewriter : public TIntermTraverser
+{
+  public:
+      ElseBlockRewriter()
+          : TIntermTraverser(false, false, true, false)
+          , mTemporaryIndex(0)
+      {}
+
+  protected:
+    bool visitAggregate(Visit visit, TIntermAggregate *aggregate);
+
+  private:
+    int mTemporaryIndex;
+
+    TIntermNode *rewriteSelection(TIntermSelection *selection);
+};
+
+void RewriteElseBlocks(TIntermNode *node);
+
+}
+
+#endif // COMPILER_REWRITE_ELSE_BLOCKS_H_
diff --git a/src/compiler/translator/intermediate.h b/src/compiler/translator/intermediate.h
index 4ec4109..041eb86 100644
--- a/src/compiler/translator/intermediate.h
+++ b/src/compiler/translator/intermediate.h
@@ -428,7 +428,7 @@
 
 protected:
     TIntermOperator(TOperator o) : TIntermTyped(TType(EbtFloat, EbpUndefined)), op(o) {}
-    TIntermOperator(TOperator o, TType& t) : TIntermTyped(t), op(o) {}   
+    TIntermOperator(TOperator o, const TType& t) : TIntermTyped(t), op(o) {}
     TOperator op;
 };
 
@@ -468,7 +468,7 @@
 //
 class TIntermUnary : public TIntermOperator {
 public:
-    TIntermUnary(TOperator o, TType& t) : TIntermOperator(o, t), operand(0), useEmulatedFunction(false) {}
+    TIntermUnary(TOperator o, const TType& t) : TIntermOperator(o, t), operand(0), useEmulatedFunction(false) {}
     TIntermUnary(TOperator o) : TIntermOperator(o), operand(0), useEmulatedFunction(false) {}
 
     virtual void traverse(TIntermTraverser*);