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);
diff --git a/src/libGLESv2/DynamicHLSL.h b/src/libGLESv2/DynamicHLSL.h
index 352a5de..07c2a2f 100644
--- a/src/libGLESv2/DynamicHLSL.h
+++ b/src/libGLESv2/DynamicHLSL.h
@@ -10,6 +10,7 @@
 #define LIBGLESV2_DYNAMIC_HLSL_H_
 
 #include "common/angleutils.h"
+#include "libGLESv2/constants.h"
 
 namespace rx
 {
@@ -27,24 +28,28 @@
 class VertexAttribute;
 struct VertexFormat;
 struct ShaderVariable;
+struct Varying;
 struct Attribute;
+struct PackedVarying;
+
+typedef const PackedVarying *VaryingPacking[IMPLEMENTATION_MAX_VARYING_VECTORS][4];
 
 class DynamicHLSL
 {
   public:
     explicit DynamicHLSL(rx::Renderer *const renderer);
 
-    int packVaryings(InfoLog &infoLog, const ShaderVariable *packing[][4], FragmentShader *fragmentShader,
+    int packVaryings(InfoLog &infoLog, VaryingPacking packing, FragmentShader *fragmentShader,
                      VertexShader *vertexShader, const std::vector<std::string>& transformFeedbackVaryings);
     std::string generateInputLayoutHLSL(const VertexFormat inputLayout[], const Attribute shaderAttributes[]) const;
-    bool generateShaderLinkHLSL(InfoLog &infoLog, int registers, const ShaderVariable *packing[][4],
+    bool generateShaderLinkHLSL(InfoLog &infoLog, int registers, const VaryingPacking packing,
                                 std::string& pixelHLSL, std::string& vertexHLSL,
                                 FragmentShader *fragmentShader, VertexShader *vertexShader,
                                 const std::vector<std::string>& transformFeedbackVaryings,
                                 std::vector<LinkedVarying> *linkedVaryings,
                                 std::map<int, VariableLocation> *programOutputVars) const;
 
-    std::string generateGeometryShaderHLSL(int registers, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const;
+    std::string generateGeometryShaderHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const;
     void getInputLayoutSignature(const VertexFormat inputLayout[], GLenum signature[]) const;
 
     static const std::string VERTEX_ATTRIBUTE_STUB_STRING;
@@ -57,7 +62,7 @@
     std::string generateVaryingHLSL(VertexShader *shader, const std::string &varyingSemantic,
                                     std::vector<LinkedVarying> *linkedVaryings) const;
     void defineOutputVariables(FragmentShader *fragmentShader, std::map<int, VariableLocation> *programOutputVars) const;
-    std::string generatePointSpriteHLSL(int registers, const ShaderVariable *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const;
+    std::string generatePointSpriteHLSL(int registers, FragmentShader *fragmentShader, VertexShader *vertexShader) const;
 
     // Prepend an underscore
     static std::string decorateVariable(const std::string &name);
diff --git a/src/libGLESv2/ProgramBinary.cpp b/src/libGLESv2/ProgramBinary.cpp
index 05c2895..a0742ab 100644
--- a/src/libGLESv2/ProgramBinary.cpp
+++ b/src/libGLESv2/ProgramBinary.cpp
@@ -1013,17 +1013,17 @@
 
 bool ProgramBinary::linkVaryings(InfoLog &infoLog, FragmentShader *fragmentShader, VertexShader *vertexShader)
 {
-    std::vector<gl::Varying> &fragmentVaryings = fragmentShader->getVaryings();
-    std::vector<gl::Varying> &vertexVaryings = vertexShader->getVaryings();
+    std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
+    std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
 
     for (size_t fragVaryingIndex = 0; fragVaryingIndex < fragmentVaryings.size(); fragVaryingIndex++)
     {
-        gl::Varying *input = &fragmentVaryings[fragVaryingIndex];
+        PackedVarying *input = &fragmentVaryings[fragVaryingIndex];
         bool matched = false;
 
         for (size_t vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
         {
-            gl::Varying *output = &vertexVaryings[vertVaryingIndex];
+            PackedVarying *output = &vertexVaryings[vertVaryingIndex];
             if (output->name == input->name)
             {
                 if (!linkValidateVariables(infoLog, output->name, *input, *output))
@@ -1536,7 +1536,7 @@
     mVertexWorkarounds = vertexShader->getD3DWorkarounds();
 
     // Map the varyings to the register file
-    const gl::ShaderVariable *packing[IMPLEMENTATION_MAX_VARYING_VECTORS][4] = {NULL};
+    VaryingPacking packing = { NULL };
     int registers = mDynamicHLSL->packVaryings(infoLog, packing, fragmentShader, vertexShader, transformFeedbackVaryings);
 
     if (registers < 0)
@@ -1602,7 +1602,7 @@
 
         if (usesGeometryShader())
         {
-            std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, packing, fragmentShader, vertexShader);
+            std::string geometryHLSL = mDynamicHLSL->generateGeometryShaderHLSL(registers, fragmentShader, vertexShader);
             mGeometryExecutable = mRenderer->compileToExecutable(infoLog, geometryHLSL.c_str(), rx::SHADER_GEOMETRY,
                                                                  mTransformFeedbackLinkedVaryings,
                                                                  (mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS),
diff --git a/src/libGLESv2/Shader.cpp b/src/libGLESv2/Shader.cpp
index baced15..038ca62 100644
--- a/src/libGLESv2/Shader.cpp
+++ b/src/libGLESv2/Shader.cpp
@@ -125,7 +125,7 @@
     return mActiveInterfaceBlocks;
 }
 
-std::vector<Varying> &Shader::getVaryings()
+std::vector<PackedVarying> &Shader::getVaryings()
 {
     return mVaryings;
 }
@@ -227,7 +227,11 @@
     {
         std::vector<Varying> *activeVaryings;
         ShGetInfoPointer(compiler, SH_ACTIVE_VARYINGS_ARRAY, reinterpret_cast<void**>(&activeVaryings));
-        mVaryings = *activeVaryings;
+
+        for (size_t varyingIndex = 0; varyingIndex < activeVaryings->size(); varyingIndex++)
+        {
+            mVaryings.push_back(PackedVarying((*activeVaryings)[varyingIndex]));
+        }
 
         mUsesMultipleRenderTargets = mHlsl.find("GL_USES_MRT")          != std::string::npos;
         mUsesFragColor             = mHlsl.find("GL_USES_FRAG_COLOR")   != std::string::npos;
@@ -440,7 +444,7 @@
 };
 
 // true if varying x has a higher priority in packing than y
-bool Shader::compareVarying(const ShaderVariable &x, const ShaderVariable &y)
+bool Shader::compareVarying(const PackedVarying &x, const PackedVarying &y)
 {
     if (x.type == y.type)
     {
diff --git a/src/libGLESv2/Shader.h b/src/libGLESv2/Shader.h
index e7df4e9..6b2f9dc 100644
--- a/src/libGLESv2/Shader.h
+++ b/src/libGLESv2/Shader.h
@@ -32,6 +32,23 @@
 {
 class ResourceManager;
 
+struct PackedVarying : public Varying
+{
+    unsigned int registerIndex; // Assigned during link
+
+    PackedVarying(const Varying &varying)
+      : Varying(varying),
+        registerIndex(GL_INVALID_INDEX)
+    {}
+
+    bool registerAssigned() const { return registerIndex != GL_INVALID_INDEX; }
+
+    void resetRegisterAssignment()
+    {
+        registerIndex = GL_INVALID_INDEX;
+    }
+};
+
 class Shader
 {
     friend class DynamicHLSL;
@@ -54,7 +71,7 @@
     void getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer) const;
     const std::vector<Uniform> &getUniforms() const;
     const std::vector<InterfaceBlock> &getInterfaceBlocks() const;
-    std::vector<Varying> &getVaryings();
+    std::vector<PackedVarying> &getVaryings();
 
     virtual void compile() = 0;
     virtual void uncompile();
@@ -82,11 +99,11 @@
 
     void getSourceImpl(const std::string &source, GLsizei bufSize, GLsizei *length, char *buffer) const;
 
-    static bool compareVarying(const ShaderVariable &x, const ShaderVariable &y);
+    static bool compareVarying(const PackedVarying &x, const PackedVarying &y);
 
     const rx::Renderer *const mRenderer;
 
-    std::vector<Varying> mVaryings;
+    std::vector<PackedVarying> mVaryings;
 
     bool mUsesMultipleRenderTargets;
     bool mUsesFragColor;