Split register allocation of varyings from the translator.

The translator gl::Varying struct does not need to know about register
allocation. We can put that entirely in the API library. This makes
exposing Varying variables with the new translator types cleaner.

BUG=angle:466

Change-Id: Ib85ac27de003913a01f76d5f4dc52454530859e6
Reviewed-on: https://chromium-review.googlesource.com/199736
Reviewed-by: Nicolas Capens <nicolascapens@chromium.org>
Reviewed-by: Shannon Woods <shannonwoods@chromium.org>
Tested-by: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libGLESv2/DynamicHLSL.cpp b/src/libGLESv2/DynamicHLSL.cpp
index 8ec3138..20e4a20 100644
--- a/src/libGLESv2/DynamicHLSL.cpp
+++ b/src/libGLESv2/DynamicHLSL.cpp
@@ -89,18 +89,17 @@
 {
 }
 
-static bool packVarying(Varying *varying, const int maxVaryingVectors, const ShaderVariable *packing[][4])
+static bool packVarying(PackedVarying *varying, const int maxVaryingVectors, VaryingPacking packing)
 {
     GLenum transposedType = TransposeMatrixType(varying->type);
 
     // matrices within varying structs are not transposed
     int registers = (varying->isStruct() ? HLSLVariableRegisterCount(*varying) : VariableRowCount(transposedType)) * varying->elementCount();
     int elements = (varying->isStruct() ? 4 : VariableColumnCount(transposedType));
-    bool success = false;
 
-    if (elements == 2 || elements == 3 || elements == 4)
+    if (elements >= 2 && elements <= 4)
     {
-        for (int r = 0; r <= maxVaryingVectors - registers && !success; r++)
+        for (int r = 0; r <= maxVaryingVectors - registers; r++)
         {
             bool available = true;
 
@@ -127,13 +126,13 @@
                     }
                 }
 
-                success = true;
+                return true;
             }
         }
 
-        if (!success && elements == 2)
+        if (elements == 2)
         {
-            for (int r = maxVaryingVectors - registers; r >= 0 && !success; r--)
+            for (int r = maxVaryingVectors - registers; r >= 0; r--)
             {
                 bool available = true;
 
@@ -160,7 +159,7 @@
                         }
                     }
 
-                    success = true;
+                    return true;
                 }
             }
         }
@@ -204,17 +203,17 @@
                 }
             }
 
-            success = true;
+            return true;
         }
     }
     else UNREACHABLE();
 
-    return success;
+    return false;
 }
 
 // Packs varyings into generic varying registers, using the algorithm from [OpenGL ES Shading Language 1.00 rev. 17] appendix A section 7 page 111
 // Returns the number of used varying registers, or -1 if unsuccesful
-int DynamicHLSL::packVaryings(InfoLog &infoLog, const ShaderVariable *packing[][4], FragmentShader *fragmentShader,
+int DynamicHLSL::packVaryings(InfoLog &infoLog, VaryingPacking packing, FragmentShader *fragmentShader,
                               VertexShader *vertexShader, const std::vector<std::string>& transformFeedbackVaryings)
 {
     const int maxVaryingVectors = mRenderer->getMaxVaryingVectors();
@@ -226,7 +225,7 @@
 
     for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
     {
-        Varying *varying = &fragmentShader->mVaryings[varyingIndex];
+        PackedVarying *varying = &fragmentShader->mVaryings[varyingIndex];
         if (packVarying(varying, maxVaryingVectors, packing))
         {
             packedVaryings.insert(varying->name);
@@ -246,7 +245,7 @@
             bool found = false;
             for (unsigned int varyingIndex = 0; varyingIndex < vertexShader->mVaryings.size(); varyingIndex++)
             {
-                Varying *varying = &vertexShader->mVaryings[varyingIndex];
+                PackedVarying *varying = &vertexShader->mVaryings[varyingIndex];
                 if (transformFeedbackVarying == varying->name)
                 {
                     if (!packVarying(varying, maxVaryingVectors, packing))
@@ -289,17 +288,17 @@
 
     for (unsigned int varyingIndex = 0; varyingIndex < shader->mVaryings.size(); varyingIndex++)
     {
-        Varying *varying = &shader->mVaryings[varyingIndex];
-        if (varying->registerAssigned())
+        const PackedVarying &varying = shader->mVaryings[varyingIndex];
+        if (varying.registerAssigned())
         {
-            GLenum transposedType = TransposeMatrixType(varying->type);
-            int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType));
+            GLenum transposedType = TransposeMatrixType(varying.type);
+            int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
 
-            for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
+            for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
             {
                 for (int row = 0; row < variableRows; row++)
                 {
-                    switch (varying->interpolation)
+                    switch (varying.interpolation)
                     {
                       case INTERPOLATION_SMOOTH:   varyingHLSL += "    ";                 break;
                       case INTERPOLATION_FLAT:     varyingHLSL += "    nointerpolation "; break;
@@ -307,16 +306,16 @@
                       default:  UNREACHABLE();
                     }
 
-                    unsigned int semanticIndex = elementIndex * variableRows + varying->registerIndex + row;
+                    unsigned int semanticIndex = elementIndex * variableRows + varying.registerIndex + row;
                     std::string n = Str(semanticIndex);
 
                     std::string typeString;
 
-                    if (varying->isStruct())
+                    if (varying.isStruct())
                     {
                         // matrices within structs are not transposed, so
                         // do not use the special struct prefix "rm"
-                        typeString = decorateVariable(varying->structName);
+                        typeString = decorateVariable(varying.structName);
                     }
                     else
                     {
@@ -330,9 +329,9 @@
 
             if (linkedVaryings)
             {
-                linkedVaryings->push_back(LinkedVarying(varying->name, varying->type, varying->elementCount(),
-                                                        varyingSemantic, varying->registerIndex,
-                                                        variableRows * varying->elementCount()));
+                linkedVaryings->push_back(LinkedVarying(varying.name, varying.type, varying.elementCount(),
+                                                        varyingSemantic, varying.registerIndex,
+                                                        variableRows * varying.elementCount()));
             }
         }
     }
@@ -404,7 +403,7 @@
            "}\n";
 }
 
-bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const ShaderVariable *packing[][4],
+bool DynamicHLSL::generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
                                          std::string& pixelHLSL, std::string& vertexHLSL,
                                          FragmentShader *fragmentShader, VertexShader *vertexShader,
                                          const std::vector<std::string>& transformFeedbackVaryings,
@@ -563,16 +562,16 @@
 
     for (unsigned int vertVaryingIndex = 0; vertVaryingIndex < vertexShader->mVaryings.size(); vertVaryingIndex++)
     {
-        Varying *varying = &vertexShader->mVaryings[vertVaryingIndex];
-        if (varying->registerAssigned())
+        const PackedVarying &varying = vertexShader->mVaryings[vertVaryingIndex];
+        if (varying.registerAssigned())
         {
-            for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
+            for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
             {
-                int variableRows = (varying->isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying->type)));
+                int variableRows = (varying.isStruct() ? 1 : VariableRowCount(TransposeMatrixType(varying.type)));
 
                 for (int row = 0; row < variableRows; row++)
                 {
-                    int r = varying->registerIndex + elementIndex * variableRows + row;
+                    int r = varying.registerIndex + elementIndex * variableRows + row;
                     vertexHLSL += "    output.v" + Str(r);
 
                     bool sharedRegister = false;   // Register used by multiple varyings
@@ -592,7 +591,7 @@
 
                         for (int x = 0; x < 4; x++)
                         {
-                            if (packing[r][x] == &*varying)
+                            if (packing[r][x] == &varying)
                             {
                                 switch(x)
                                 {
@@ -605,9 +604,9 @@
                         }
                     }
 
-                    vertexHLSL += " = _" + varying->name;
+                    vertexHLSL += " = _" + varying.name;
 
-                    if (varying->isArray())
+                    if (varying.isArray())
                     {
                         vertexHLSL += ArrayString(elementIndex);
                     }
@@ -762,19 +761,19 @@
 
     for (unsigned int varyingIndex = 0; varyingIndex < fragmentShader->mVaryings.size(); varyingIndex++)
     {
-        Varying *varying = &fragmentShader->mVaryings[varyingIndex];
-        if (varying->registerAssigned())
+        const PackedVarying &varying = fragmentShader->mVaryings[varyingIndex];
+        if (varying.registerAssigned())
         {
-            for (unsigned int elementIndex = 0; elementIndex < varying->elementCount(); elementIndex++)
+            for (unsigned int elementIndex = 0; elementIndex < varying.elementCount(); elementIndex++)
             {
-                GLenum transposedType = TransposeMatrixType(varying->type);
-                int variableRows = (varying->isStruct() ? 1 : VariableRowCount(transposedType));
+                GLenum transposedType = TransposeMatrixType(varying.type);
+                int variableRows = (varying.isStruct() ? 1 : VariableRowCount(transposedType));
                 for (int row = 0; row < variableRows; row++)
                 {
-                    std::string n = Str(varying->registerIndex + elementIndex * variableRows + row);
-                    pixelHLSL += "    _" + varying->name;
+                    std::string n = Str(varying.registerIndex + elementIndex * variableRows + row);
+                    pixelHLSL += "    _" + varying.name;
 
-                    if (varying->isArray())
+                    if (varying.isArray())
                     {
                         pixelHLSL += ArrayString(elementIndex);
                     }
@@ -784,7 +783,7 @@
                         pixelHLSL += ArrayString(row);
                     }
 
-                    if (varying->isStruct())
+                    if (varying.isStruct())
                     {
                         pixelHLSL += " = input.v" + n + ";\n";   break;
                     }
@@ -870,14 +869,14 @@
     }
 }
 
-std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
+std::string DynamicHLSL::generateGeometryShaderHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const
 {
     // for now we only handle point sprite emulation
     ASSERT(vertexShader->mUsesPointSize && mRenderer->getMajorShaderModel() >= 4);
-    return generatePointSpriteHLSL(registers, packing, fragmentShader, vertexShader);
+    return generatePointSpriteHLSL(registers, fragmentShader, vertexShader);
 }
 
-std::string DynamicHLSL::generatePointSpriteHLSL(int registers, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const
+std::string DynamicHLSL::generatePointSpriteHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const
 {
     ASSERT(registers >= 0);
     ASSERT(vertexShader->mUsesPointSize);