Move shader attributes into Program shared data.

Making the Program own the attribs, and the Impl only see a read-only
copy cleans up the Impl object. It also allows us to more cleanly
isolate certain coded into D3D.

BUG=angleproject:1123

Change-Id: I469051eb066fc56e55282affa2d5398b394ab8d2
Reviewed-on: https://chromium-review.googlesource.com/293826
Tested-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/libANGLE/Program.cpp b/src/libANGLE/Program.cpp
index cea9df1..7c6b8c3 100644
--- a/src/libANGLE/Program.cpp
+++ b/src/libANGLE/Program.cpp
@@ -163,7 +163,6 @@
 
 Program::Program(rx::ImplFactory *factory, ResourceManager *manager, GLuint handle)
     : mProgram(factory->createProgram(mData)),
-      mLinkedAttributes(gl::MAX_VERTEX_ATTRIBS),
       mValidated(false),
       mLinked(false),
       mDeleteStatus(false),
@@ -356,7 +355,7 @@
         }
     }
 
-    mLinkedAttributes.assign(mLinkedAttributes.size(), sh::Attribute());
+    mData.mAttributes.clear();
     mData.mTransformFeedbackVaryingVars.clear();
 
     mProgram->reset();
@@ -408,20 +407,21 @@
     // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
     for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
     {
-        stream.readInt(&mLinkedAttributes[i].type);
-        stream.readString(&mLinkedAttributes[i].name);
         stream.readInt(&mProgram->getSemanticIndexes()[i]);
     }
 
     unsigned int attribCount = stream.readInt<unsigned int>();
+    ASSERT(mData.mAttributes.empty());
     for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
     {
-        GLenum type = stream.readInt<GLenum>();
-        GLenum precision = stream.readInt<GLenum>();
-        std::string name = stream.readString();
-        GLint arraySize = stream.readInt<GLint>();
-        int location = stream.readInt<int>();
-        mProgram->setShaderAttribute(attribIndex, type, precision, name, arraySize, location);
+        sh::Attribute attrib;
+        attrib.type      = stream.readInt<GLenum>();
+        attrib.precision = stream.readInt<GLenum>();
+        attrib.name      = stream.readString();
+        attrib.arraySize = stream.readInt<GLint>();
+        attrib.location  = stream.readInt<int>();
+        attrib.staticUse = stream.readBool();
+        mData.mAttributes.push_back(attrib);
     }
 
     stream.readInt(&mData.mTransformFeedbackBufferMode);
@@ -454,20 +454,18 @@
     // TODO(jmadill): replace MAX_VERTEX_ATTRIBS
     for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
     {
-        stream.writeInt(mLinkedAttributes[i].type);
-        stream.writeString(mLinkedAttributes[i].name);
         stream.writeInt(mProgram->getSemanticIndexes()[i]);
     }
 
-    const auto &shaderAttributes = mProgram->getShaderAttributes();
-    stream.writeInt(shaderAttributes.size());
-    for (const auto &attrib : shaderAttributes)
+    stream.writeInt(mData.mAttributes.size());
+    for (const sh::Attribute &attrib : mData.mAttributes)
     {
         stream.writeInt(attrib.type);
         stream.writeInt(attrib.precision);
         stream.writeString(attrib.name);
         stream.writeInt(attrib.arraySize);
         stream.writeInt(attrib.location);
+        stream.writeInt(attrib.staticUse);
     }
 
     stream.writeInt(mData.mTransformFeedbackBufferMode);
@@ -586,11 +584,11 @@
 
 GLuint Program::getAttributeLocation(const std::string &name)
 {
-    for (size_t index = 0; index < mLinkedAttributes.size(); index++)
+    for (const sh::Attribute &attribute : mData.mAttributes)
     {
-        if (mLinkedAttributes[index].name == name)
+        if (attribute.name == name && attribute.staticUse)
         {
-            return static_cast<GLuint>(index);
+            return attribute.location;
         }
     }
 
@@ -611,45 +609,7 @@
 
 void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
 {
-    if (mLinked)
-    {
-        // Skip over inactive attributes
-        unsigned int activeAttribute = 0;
-        unsigned int attribute;
-        for (attribute = 0; attribute < static_cast<unsigned int>(mLinkedAttributes.size());
-             attribute++)
-        {
-            if (mLinkedAttributes[attribute].name.empty())
-            {
-                continue;
-            }
-
-            if (activeAttribute == index)
-            {
-                break;
-            }
-
-            activeAttribute++;
-        }
-
-        if (bufsize > 0)
-        {
-            const char *string = mLinkedAttributes[attribute].name.c_str();
-
-            strncpy(name, string, bufsize);
-            name[bufsize - 1] = '\0';
-
-            if (length)
-            {
-                *length = static_cast<GLsizei>(strlen(name));
-            }
-        }
-
-        *size = 1;   // Always a single 'type' instance
-
-        *type = mLinkedAttributes[attribute].type;
-    }
-    else
+    if (!mLinked)
     {
         if (bufsize > 0)
         {
@@ -663,22 +623,57 @@
 
         *type = GL_NONE;
         *size = 1;
+        return;
     }
+
+    size_t attributeIndex = 0;
+
+    for (const sh::Attribute &attribute : mData.mAttributes)
+    {
+        // Skip over inactive attributes
+        if (attribute.staticUse)
+        {
+            if (static_cast<size_t>(index) == attributeIndex)
+            {
+                break;
+            }
+            attributeIndex++;
+        }
+    }
+
+    ASSERT(index == attributeIndex && attributeIndex < mData.mAttributes.size());
+    const sh::Attribute &attrib = mData.mAttributes[attributeIndex];
+
+    if (bufsize > 0)
+    {
+        const char *string = attrib.name.c_str();
+
+        strncpy(name, string, bufsize);
+        name[bufsize - 1] = '\0';
+
+        if (length)
+        {
+            *length = static_cast<GLsizei>(strlen(name));
+        }
+    }
+
+    // Always a single 'type' instance
+    *size = 1;
+    *type = attrib.type;
 }
 
 GLint Program::getActiveAttributeCount()
 {
-    int count = 0;
-
-    if (mLinked)
+    if (!mLinked)
     {
-        for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
-        {
-            if (!mLinkedAttributes[attributeIndex].name.empty())
-            {
-                count++;
-            }
-        }
+        return 0;
+    }
+
+    GLint count = 0;
+
+    for (const sh::Attribute &attrib : mData.mAttributes)
+    {
+        count += (attrib.staticUse ? 1 : 0);
     }
 
     return count;
@@ -686,22 +681,22 @@
 
 GLint Program::getActiveAttributeMaxLength()
 {
-    GLint maxLength = 0;
-
-    if (mLinked)
+    if (!mLinked)
     {
-        for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
+        return 0;
+    }
+
+    size_t maxLength = 0;
+
+    for (const sh::Attribute &attrib : mData.mAttributes)
+    {
+        if (attrib.staticUse)
         {
-            if (!mLinkedAttributes[attributeIndex].name.empty())
-            {
-                maxLength = std::max(
-                    static_cast<GLint>(mLinkedAttributes[attributeIndex].name.length() + 1),
-                    maxLength);
-            }
+            maxLength = std::max(attrib.name.length() + 1, maxLength);
         }
     }
 
-    return maxLength;
+    return static_cast<GLint>(maxLength);
 }
 
 GLint Program::getFragDataLocation(const std::string &name) const
@@ -1292,73 +1287,77 @@
                              const Shader *vertexShader)
 {
     unsigned int usedLocations = 0;
-    const std::vector<sh::Attribute> &shaderAttributes = vertexShader->getActiveAttributes();
+    mData.mAttributes          = vertexShader->getActiveAttributes();
     GLuint maxAttribs = data.caps->maxVertexAttributes;
 
     // TODO(jmadill): handle aliasing robustly
-    if (shaderAttributes.size() > maxAttribs)
+    if (mData.mAttributes.size() > maxAttribs)
     {
         infoLog << "Too many vertex attributes.";
         return false;
     }
 
-    // Link attributes that have a binding location
-    for (unsigned int attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
-    {
-        const sh::Attribute &attribute = shaderAttributes[attributeIndex];
+    std::vector<sh::Attribute *> usedAttribMap(data.caps->maxVertexAttributes, nullptr);
 
+    // Link attributes that have a binding location
+    for (sh::Attribute &attribute : mData.mAttributes)
+    {
+        // TODO(jmadill): do staticUse filtering step here, or not at all
         ASSERT(attribute.staticUse);
 
-        const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location;
-
-        mProgram->setShaderAttribute(attributeIndex, attribute);
-
-        if (location != -1)   // Set by glBindAttribLocation or by location layout qualifier
+        int bindingLocation = attributeBindings.getAttributeBinding(attribute.name);
+        if (attribute.location == -1 && bindingLocation != -1)
         {
+            attribute.location = bindingLocation;
+        }
+
+        if (attribute.location != -1)
+        {
+            // Location is set by glBindAttribLocation or by location layout qualifier
             const int rows = VariableRegisterCount(attribute.type);
 
-            if (static_cast<GLuint>(rows + location) > maxAttribs)
+            if (static_cast<GLuint>(rows + attribute.location) > maxAttribs)
             {
                 infoLog << "Active attribute (" << attribute.name << ") at location "
-                        << location << " is too big to fit";
+                        << attribute.location << " is too big to fit";
 
                 return false;
             }
 
             for (int row = 0; row < rows; row++)
             {
-                const int rowLocation = location + row;
-                sh::ShaderVariable *linkedAttribute = &mLinkedAttributes[rowLocation];
+                const int rowLocation               = attribute.location + row;
+                sh::ShaderVariable *linkedAttribute = usedAttribMap[rowLocation];
 
                 // In GLSL 3.00, attribute aliasing produces a link error
                 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
-                // TODO(jmadill): fix aliasing on ES2
-                // if (mProgram->getShaderVersion() >= 300)
+                if (linkedAttribute)
                 {
-                    if (!linkedAttribute->name.empty())
+                    // TODO(jmadill): fix aliasing on ES2
+                    // if (mProgram->getShaderVersion() >= 300)
                     {
                         infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
                                 << linkedAttribute->name << "' at location " << rowLocation;
                         return false;
                     }
                 }
+                else
+                {
+                    usedAttribMap[rowLocation] = &attribute;
+                }
 
-                *linkedAttribute = attribute;
                 usedLocations |= 1 << rowLocation;
             }
         }
     }
 
     // Link attributes that don't have a binding location
-    for (unsigned int attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
+    for (sh::Attribute &attribute : mData.mAttributes)
     {
-        const sh::Attribute &attribute = shaderAttributes[attributeIndex];
-
         ASSERT(attribute.staticUse);
 
-        const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location;
-
-        if (location == -1)   // Not set by glBindAttribLocation or by location layout qualifier
+        // Not set by glBindAttribLocation or by location layout qualifier
+        if (attribute.location == -1)
         {
             int rows = VariableRegisterCount(attribute.type);
             int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, maxAttribs);
@@ -1366,17 +1365,21 @@
             if (availableIndex == -1 || static_cast<GLuint>(availableIndex + rows) > maxAttribs)
             {
                 infoLog << "Too many active attributes (" << attribute.name << ")";
-                return false;   // Fail to link
+                return false;
             }
 
-            mLinkedAttributes[availableIndex] = attribute;
+            attribute.location = availableIndex;
         }
     }
 
-    for (GLuint attributeIndex = 0; attributeIndex < maxAttribs;)
+    // TODO(jmadill): make semantic index D3D-only
+    for (const sh::Attribute &attribute : mData.mAttributes)
     {
-        int index = vertexShader->getSemanticIndex(mLinkedAttributes[attributeIndex].name);
-        int rows  = VariableRegisterCount(mLinkedAttributes[attributeIndex].type);
+        ASSERT(attribute.staticUse);
+
+        unsigned int attributeIndex = attribute.location;
+        int index                   = vertexShader->getSemanticIndex(attribute.name);
+        int rows                    = VariableRegisterCount(attribute.type);
 
         for (int r = 0; r < rows; r++)
         {
diff --git a/src/libANGLE/Program.h b/src/libANGLE/Program.h
index f3a847a..0c1e3ef 100644
--- a/src/libANGLE/Program.h
+++ b/src/libANGLE/Program.h
@@ -180,6 +180,7 @@
             ASSERT(uniformBlockIndex < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS);
             return mUniformBlockBindings[uniformBlockIndex];
         }
+        const std::vector<sh::Attribute> &getAttributes() const { return mAttributes; }
 
       private:
         friend class Program;
@@ -193,6 +194,8 @@
 
         GLuint mUniformBlockBindings[IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS];
 
+        std::vector<sh::Attribute> mAttributes;
+
         // TODO(jmadill): move more state into Data.
     };
 
@@ -228,7 +231,7 @@
     void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name);
     GLint getActiveAttributeCount();
     GLint getActiveAttributeMaxLength();
-    const std::vector<sh::Attribute> &getLinkedAttributes() const { return mLinkedAttributes; }
+    const std::vector<sh::Attribute> &getAttributes() const { return mData.mAttributes; }
 
     GLint getFragDataLocation(const std::string &name) const;
 
@@ -335,8 +338,6 @@
     Data mData;
     rx::ProgramImpl *mProgram;
 
-    std::vector<sh::Attribute> mLinkedAttributes;
-
     std::map<int, VariableLocation> mOutputVariables;
 
     bool mValidated;
diff --git a/src/libANGLE/renderer/ProgramImpl.cpp b/src/libANGLE/renderer/ProgramImpl.cpp
index b0fd8f9..03d3c7c 100644
--- a/src/libANGLE/renderer/ProgramImpl.cpp
+++ b/src/libANGLE/renderer/ProgramImpl.cpp
@@ -135,26 +135,4 @@
     SafeDeleteContainer(mUniformBlocks);
 }
 
-void ProgramImpl::setShaderAttribute(size_t index, const sh::Attribute &attrib)
-{
-    if (mShaderAttributes.size() <= index)
-    {
-        mShaderAttributes.resize(index + 1);
-    }
-    mShaderAttributes[index] = attrib;
-}
-
-void ProgramImpl::setShaderAttribute(size_t index, GLenum type, GLenum precision, const std::string &name, GLint size, int location)
-{
-    if (mShaderAttributes.size() <= index)
-    {
-        mShaderAttributes.resize(index + 1);
-    }
-    mShaderAttributes[index].type = type;
-    mShaderAttributes[index].precision = precision;
-    mShaderAttributes[index].name = name;
-    mShaderAttributes[index].arraySize = size;
-    mShaderAttributes[index].location = location;
-}
-
 }
diff --git a/src/libANGLE/renderer/ProgramImpl.h b/src/libANGLE/renderer/ProgramImpl.h
index e1bb357..d2f7041 100644
--- a/src/libANGLE/renderer/ProgramImpl.h
+++ b/src/libANGLE/renderer/ProgramImpl.h
@@ -85,7 +85,6 @@
     const std::vector<gl::LinkedUniform*> &getUniforms() const { return mUniforms; }
     const std::map<GLuint, gl::VariableLocation> &getUniformIndices() const { return mUniformIndex; }
     const std::vector<gl::UniformBlock*> &getUniformBlocks() const { return mUniformBlocks; }
-    const std::vector<sh::Attribute> getShaderAttributes() { return mShaderAttributes; }
     const SemanticIndexArray &getSemanticIndexes() const { return mSemanticIndex; }
 
     std::vector<gl::LinkedUniform*> &getUniforms() { return mUniforms; }
@@ -101,9 +100,6 @@
     GLuint getUniformIndex(const std::string &name) const;
     GLuint getUniformBlockIndex(const std::string &name) const;
 
-    void setShaderAttribute(size_t index, const sh::Attribute &attrib);
-    void setShaderAttribute(size_t index, GLenum type, GLenum precision, const std::string &name, GLint size, int location);
-
     virtual void reset();
 
   protected:
@@ -117,9 +113,6 @@
     std::vector<gl::UniformBlock*> mUniformBlocks;
 
     SemanticIndexArray mSemanticIndex;
-
-  private:
-    std::vector<sh::Attribute> mShaderAttributes;
 };
 
 }
diff --git a/src/libANGLE/renderer/d3d/ProgramD3D.cpp b/src/libANGLE/renderer/d3d/ProgramD3D.cpp
index c41dc78..e4f7d54 100644
--- a/src/libANGLE/renderer/d3d/ProgramD3D.cpp
+++ b/src/libANGLE/renderer/d3d/ProgramD3D.cpp
@@ -1003,7 +1003,8 @@
     }
 
     // Generate new dynamic layout with attribute conversions
-    std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(mVertexHLSL, inputLayout, getShaderAttributes());
+    std::string finalVertexHLSL = mDynamicHLSL->generateVertexShaderForInputLayout(
+        mVertexHLSL, inputLayout, mData.getAttributes());
 
     // Generate new vertex executable
     ShaderExecutableD3D *vertexExecutable = NULL;
diff --git a/src/libANGLE/renderer/d3d/ProgramD3D.h b/src/libANGLE/renderer/d3d/ProgramD3D.h
index 17f7c3d..7207d8c 100644
--- a/src/libANGLE/renderer/d3d/ProgramD3D.h
+++ b/src/libANGLE/renderer/d3d/ProgramD3D.h
@@ -124,6 +124,7 @@
     void sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes,
                                 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
                                 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const;
+    const SemanticIndexArray &getAttributesByLayout() const { return mAttributesByLayout; }
 
     void updateCachedInputLayout(const gl::Program *program, const gl::State &state);
     const gl::InputLayout &getCachedInputLayout() const { return mCachedInputLayout; }
@@ -245,7 +246,7 @@
 
     int mShaderVersion;
 
-    int mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS];
+    SemanticIndexArray mAttributesByLayout;
 
     unsigned int mSerial;
 
diff --git a/src/libANGLE/renderer/d3d/d3d11/InputLayoutCache.cpp b/src/libANGLE/renderer/d3d/d3d11/InputLayoutCache.cpp
index 67c59a3..bae230c 100644
--- a/src/libANGLE/renderer/d3d/d3d11/InputLayoutCache.cpp
+++ b/src/libANGLE/renderer/d3d/d3d11/InputLayoutCache.cpp
@@ -46,20 +46,20 @@
     return inputLayout;
 }
 
-GLenum GetNextGLSLAttributeType(const std::vector<sh::Attribute> &linkedAttributes, int index)
+GLenum GetGLSLAttributeType(const std::vector<sh::Attribute> &shaderAttributes, int index)
 {
     // Count matrices differently
-    int subIndex = 0;
-    for (const sh::Attribute &attrib : linkedAttributes)
+    for (const sh::Attribute &attrib : shaderAttributes)
     {
-        if (attrib.type == GL_NONE)
+        if (attrib.location == -1)
         {
             continue;
         }
 
         GLenum transposedType = gl::TransposeMatrixType(attrib.type);
-        subIndex += gl::VariableRowCount(transposedType);
-        if (subIndex > index)
+        int rows              = gl::VariableRowCount(transposedType);
+
+        if (index >= attrib.location && index < attrib.location + rows)
         {
             return transposedType;
         }
@@ -185,6 +185,8 @@
     bool instancedPointSpritesActive = programUsesInstancedPointSprites && (mode == GL_POINTS);
     bool indexedPointSpriteEmulationActive = instancedPointSpritesActive && (sourceInfo != nullptr);
 
+    const auto &semanticToLocation = programD3D->getAttributesByLayout();
+
     if (!mDevice || !mDeviceContext)
     {
         return gl::Error(GL_OUT_OF_MEMORY, "Internal input layout cache is not initialized.");
@@ -200,7 +202,7 @@
     unsigned int firstInstancedElement = gl::MAX_VERTEX_ATTRIBS;
     unsigned int nextAvailableInputSlot = 0;
 
-    const std::vector<sh::Attribute> &linkedAttributes = program->getLinkedAttributes();
+    const std::vector<sh::Attribute> &shaderAttributes = program->getAttributes();
 
     for (unsigned int i = 0; i < unsortedAttributes.size(); i++)
     {
@@ -232,7 +234,8 @@
 
             // Record the type of the associated vertex shader vector in our key
             // This will prevent mismatched vertex shaders from using the same input layout
-            GLenum glslElementType = GetNextGLSLAttributeType(linkedAttributes, inputElementCount);
+            GLenum glslElementType = GetGLSLAttributeType(
+                shaderAttributes, semanticToLocation[sortedSemanticIndices[i]]);
 
             layout.addAttributeData(glslElementType,
                                     sortedSemanticIndices[i],
diff --git a/src/libANGLE/renderer/gl/ProgramGL.cpp b/src/libANGLE/renderer/gl/ProgramGL.cpp
index 56fed09..1a070b6 100644
--- a/src/libANGLE/renderer/gl/ProgramGL.cpp
+++ b/src/libANGLE/renderer/gl/ProgramGL.cpp
@@ -169,34 +169,33 @@
         }
     }
 
-    // Query the attribute information
-    GLint activeAttributeMaxLength = 0;
-    mFunctions->getProgramiv(mProgramID, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &activeAttributeMaxLength);
-
-    std::vector<GLchar> attributeNameBuffer(activeAttributeMaxLength);
-
-    GLint attributeCount = 0;
-    mFunctions->getProgramiv(mProgramID, GL_ACTIVE_ATTRIBUTES, &attributeCount);
-    for (GLint i = 0; i < attributeCount; i++)
+    for (const sh::Attribute &attribute : mData.getAttributes())
     {
-        GLsizei attributeNameLength = 0;
-        GLint attributeSize = 0;
-        GLenum attributeType = GL_NONE;
-        mFunctions->getActiveAttrib(mProgramID, i, static_cast<GLsizei>(attributeNameBuffer.size()),
-                                    &attributeNameLength, &attributeSize, &attributeType,
-                                    &attributeNameBuffer[0]);
+        if (!attribute.staticUse)
+            continue;
 
-        std::string attributeName(&attributeNameBuffer[0], attributeNameLength);
+        GLint realLocation = mFunctions->getAttribLocation(mProgramID, attribute.name.c_str());
 
-        GLint location = mFunctions->getAttribLocation(mProgramID, attributeName.c_str());
-
-        // TODO: determine attribute precision
-        setShaderAttribute(static_cast<size_t>(i), attributeType, GL_NONE, attributeName, attributeSize, location);
-
-        int attributeRegisterCount = gl::VariableRegisterCount(attributeType);
-        for (int offset = 0; offset < attributeRegisterCount; offset++)
+        // Some drivers optimize attributes more aggressively.
+        if (realLocation == -1)
         {
-            mActiveAttributesMask.set(location + offset);
+            continue;
+        }
+
+        // TODO(jmadill): Fix this
+        ASSERT(attribute.location == realLocation);
+
+        int registerCount = gl::VariableRegisterCount(attribute.type);
+
+        if (mAttributeRealLocations.size() < attribute.location + registerCount + 1)
+        {
+            mAttributeRealLocations.resize(attribute.location + registerCount + 1, -1);
+        }
+
+        for (int offset = 0; offset < registerCount; ++offset)
+        {
+            mActiveAttributesMask.set(attribute.location + offset);
+            mAttributeRealLocations[attribute.location + offset] = realLocation + offset;
         }
     }
 
@@ -378,6 +377,7 @@
     mSamplerUniformMap.clear();
     mSamplerBindings.clear();
     mActiveAttributesMask.reset();
+    mAttributeRealLocations.clear();
 }
 
 GLuint ProgramGL::getProgramID() const
diff --git a/src/libANGLE/renderer/gl/ProgramGL.h b/src/libANGLE/renderer/gl/ProgramGL.h
index ec4eaa6..c900c09 100644
--- a/src/libANGLE/renderer/gl/ProgramGL.h
+++ b/src/libANGLE/renderer/gl/ProgramGL.h
@@ -101,6 +101,9 @@
     // An array of the samplers that are used by the program
     std::vector<SamplerBindingGL> mSamplerBindings;
 
+    // Map from GL-layer attribute location to native location.
+    std::vector<GLint> mAttributeRealLocations;
+
     // Array of attribute locations used by this program
     gl::AttributesMask mActiveAttributesMask;