ES31: Refactor VertexArray for Vertex Attrib Binding

OpenGL ES3.1 feature Vertex Attrib Binding requires vertex arrays should
be split into two arrays:
1. an array of vertex buffer binding points, each of which specifies:
  - a bound buffer object,
  - a starting offset for vertex attribute data in that buffer object,
  - a stride used by all attributes using that binding point,
  - a frequency divisor used by all attributes using that binding point.
2. an array of generic vertex attribute format information records, each
of which specifies:
  - a reference to one of the new buffer binding points above,
  - a component count and format, and a normalization flag for the
    attribute data,
  - the offset of the attribute data relative to the base offset of each
    vertex found at the associated binding point.

Current ANGLE implementation simply uses a struct to represent a vertex
attribute object, which does not meet the requirements above.

This patch aims to be the the basis of the implementation of all ES3.1
Vertex Attrib Binding APIs by refactoring the struct VertexAttribute and
the class VertexArray to fit the new data layout and ensuring all current
functionality is retained.

BUG=angleproject:1593

TEST=angle_unittests, angle_end2end_tests, gpu_unittests

Change-Id: Ieb41f1bf503f815fd0476d2ea045dcb863465254
Reviewed-on: https://chromium-review.googlesource.com/418880
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/renderer/gl/VertexArrayGL.cpp b/src/libANGLE/renderer/gl/VertexArrayGL.cpp
index 776b641..c03647f 100644
--- a/src/libANGLE/renderer/gl/VertexArrayGL.cpp
+++ b/src/libANGLE/renderer/gl/VertexArrayGL.cpp
@@ -26,11 +26,11 @@
 {
 namespace
 {
-bool AttributeNeedsStreaming(const VertexAttribute &attribute)
+// Warning: you should ensure binding really matches attrib.bindingIndex before using this function.
+bool AttributeNeedsStreaming(const VertexAttribute &attrib, const VertexBinding &binding)
 {
-    return (attribute.enabled && attribute.buffer.get() == nullptr);
+    return (attrib.enabled && binding.buffer.get() == nullptr);
 }
-
 }  // anonymous namespace
 
 VertexArrayGL::VertexArrayGL(const VertexArrayState &state,
@@ -41,6 +41,7 @@
       mStateManager(stateManager),
       mVertexArrayID(0),
       mAppliedElementArrayBuffer(),
+      mAppliedBindings(state.getMaxBindings()),
       mStreamingElementArrayBufferSize(0),
       mStreamingElementArrayBuffer(0),
       mStreamingArrayBufferSize(0),
@@ -50,10 +51,12 @@
     ASSERT(mStateManager);
     mFunctions->genVertexArrays(1, &mVertexArrayID);
 
-    // Set the cached vertex attribute array size
-    GLint maxVertexAttribs = 0;
-    mFunctions->getIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs);
-    mAppliedAttributes.resize(maxVertexAttribs);
+    // Set the cached vertex attribute array and vertex attribute binding array size
+    GLuint maxVertexAttribs = static_cast<GLuint>(state.getMaxAttribs());
+    for (GLuint i = 0; i < maxVertexAttribs; i++)
+    {
+        mAppliedAttributes.emplace_back(i);
+    }
 }
 
 VertexArrayGL::~VertexArrayGL()
@@ -70,9 +73,9 @@
     mStreamingArrayBuffer = 0;
 
     mAppliedElementArrayBuffer.set(nullptr);
-    for (size_t idx = 0; idx < mAppliedAttributes.size(); idx++)
+    for (auto &binding : mAppliedBindings)
     {
-        mAppliedAttributes[idx].buffer.set(nullptr);
+        binding.buffer.set(nullptr);
     }
 }
 
@@ -247,17 +250,19 @@
     ASSERT(mAttributesNeedStreaming.any());
 
     const auto &attribs = mData.getVertexAttributes();
+    const auto &bindings = mData.getVertexBindings();
     for (auto idx : angle::IterateBitSet(mAttributesNeedStreaming & activeAttributesMask))
     {
         const auto &attrib = attribs[idx];
-        ASSERT(AttributeNeedsStreaming(attrib));
+        const auto &binding = bindings[attrib.bindingIndex];
+        ASSERT(AttributeNeedsStreaming(attrib, binding));
 
         // If streaming is going to be required, compute the size of the required buffer
         // and how much slack space at the beginning of the buffer will be required by determining
         // the attribute with the largest data size.
         size_t typeSize = ComputeVertexAttributeTypeSize(attrib);
-        *outStreamingDataSize += typeSize * ComputeVertexAttributeElementCount(
-                                                attrib, indexRange.vertexCount(), instanceCount);
+        *outStreamingDataSize += typeSize * ComputeVertexBindingElementCount(
+                                                binding, indexRange.vertexCount(), instanceCount);
         *outMaxAttributeDataSize = std::max(*outMaxAttributeDataSize, typeSize);
     }
 }
@@ -308,21 +313,25 @@
         size_t curBufferOffset = bufferEmptySpace;
 
         const auto &attribs = mData.getVertexAttributes();
+        const auto &bindings = mData.getVertexBindings();
         for (auto idx : angle::IterateBitSet(mAttributesNeedStreaming & activeAttributesMask))
         {
             const auto &attrib = attribs[idx];
-            ASSERT(AttributeNeedsStreaming(attrib));
+            const auto &binding = bindings[attrib.bindingIndex];
+            ASSERT(AttributeNeedsStreaming(attrib, binding));
 
             const size_t streamedVertexCount =
-                ComputeVertexAttributeElementCount(attrib, indexRange.vertexCount(), instanceCount);
+                ComputeVertexBindingElementCount(binding, indexRange.vertexCount(), instanceCount);
 
-            const size_t sourceStride = ComputeVertexAttributeStride(attrib);
+            const size_t sourceStride = ComputeVertexAttributeStride(attrib, binding);
             const size_t destStride   = ComputeVertexAttributeTypeSize(attrib);
 
             // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
             // a non-instanced draw call
-            const size_t firstIndex = attrib.divisor == 0 ? indexRange.start : 0;
+            const size_t firstIndex = binding.divisor == 0 ? indexRange.start : 0;
 
+            // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
+            // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
             const uint8_t *inputPointer = reinterpret_cast<const uint8_t *>(attrib.pointer);
 
             // Pack the data when copying it, user could have supplied a very large stride that
@@ -398,8 +407,9 @@
 
 void VertexArrayGL::updateNeedsStreaming(size_t attribIndex)
 {
-    const VertexAttribute &attrib = mData.getVertexAttribute(attribIndex);
-    mAttributesNeedStreaming.set(attribIndex, AttributeNeedsStreaming(attrib));
+    const auto &attrib  = mData.getVertexAttribute(attribIndex);
+    const auto &binding = mData.getBindingFromAttribIndex(attribIndex);
+    mAttributesNeedStreaming.set(attribIndex, AttributeNeedsStreaming(attrib, binding));
 }
 
 void VertexArrayGL::updateAttribEnabled(size_t attribIndex)
@@ -427,7 +437,12 @@
 void VertexArrayGL::updateAttribPointer(size_t attribIndex)
 {
     const VertexAttribute &attrib = mData.getVertexAttribute(attribIndex);
-    if (mAppliedAttributes[attribIndex] == attrib)
+    ASSERT(attribIndex == attrib.bindingIndex);
+
+    GLuint bindingIndex          = attrib.bindingIndex;
+    const VertexBinding &binding = mData.getVertexBinding(bindingIndex);
+
+    if (mAppliedAttributes[attribIndex] == attrib && mAppliedBindings[bindingIndex] == binding)
     {
         return;
     }
@@ -441,7 +456,7 @@
     }
 
     mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
-    const Buffer *arrayBuffer = attrib.buffer.get();
+    const Buffer *arrayBuffer = binding.buffer.get();
     if (arrayBuffer != nullptr)
     {
         const BufferGL *arrayBufferGL = GetImplAs<BufferGL>(arrayBuffer);
@@ -451,24 +466,56 @@
     {
         mStateManager->bindBuffer(GL_ARRAY_BUFFER, 0);
     }
-    mAppliedAttributes[attribIndex].buffer = attrib.buffer;
+
+    mAppliedBindings[bindingIndex].buffer = binding.buffer;
+
+    const GLvoid *inputPointer = nullptr;
+    if (arrayBuffer != nullptr)
+    {
+        inputPointer = static_cast<const GLvoid *>(
+            reinterpret_cast<const uint8_t *>(binding.offset + attrib.relativeOffset));
+    }
+    else
+    {
+        // Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
+        // https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding
+        inputPointer = attrib.pointer;
+    }
 
     if (attrib.pureInteger)
     {
         mFunctions->vertexAttribIPointer(static_cast<GLuint>(attribIndex), attrib.size, attrib.type,
-                                         attrib.stride, attrib.pointer);
+                                         binding.stride, inputPointer);
     }
     else
     {
         mFunctions->vertexAttribPointer(static_cast<GLuint>(attribIndex), attrib.size, attrib.type,
-                                        attrib.normalized, attrib.stride, attrib.pointer);
+                                        attrib.normalized, binding.stride, inputPointer);
     }
-    mAppliedAttributes[attribIndex].size        = attrib.size;
-    mAppliedAttributes[attribIndex].type        = attrib.type;
-    mAppliedAttributes[attribIndex].normalized  = attrib.normalized;
-    mAppliedAttributes[attribIndex].pureInteger = attrib.pureInteger;
-    mAppliedAttributes[attribIndex].stride      = attrib.stride;
-    mAppliedAttributes[attribIndex].pointer     = attrib.pointer;
+    mAppliedAttributes[attribIndex].size                    = attrib.size;
+    mAppliedAttributes[attribIndex].type                    = attrib.type;
+    mAppliedAttributes[attribIndex].normalized              = attrib.normalized;
+    mAppliedAttributes[attribIndex].pureInteger             = attrib.pureInteger;
+    mAppliedAttributes[attribIndex].pointer                 = attrib.pointer;
+    mAppliedAttributes[attribIndex].relativeOffset          = attrib.relativeOffset;
+    mAppliedAttributes[attribIndex].vertexAttribArrayStride = attrib.vertexAttribArrayStride;
+
+    mAppliedBindings[bindingIndex].stride = binding.stride;
+    mAppliedBindings[bindingIndex].offset = binding.offset;
+}
+
+void VertexArrayGL::updateAttribDivisor(size_t attribIndex)
+{
+    ASSERT(attribIndex == mData.getBindingIndexFromAttribIndex(attribIndex));
+
+    const VertexBinding &binding = mData.getVertexBinding(attribIndex);
+    if (mAppliedBindings[attribIndex].divisor != binding.divisor)
+    {
+        mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
+        mFunctions->vertexAttribDivisor(static_cast<GLuint>(attribIndex), binding.divisor);
+
+        mAppliedBindings[attribIndex].divisor = binding.divisor;
+    }
 }
 
 void VertexArrayGL::syncState(const VertexArray::DirtyBits &dirtyBits)
@@ -478,34 +525,30 @@
         if (dirtyBit == VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER)
         {
             // TODO(jmadill): Element array buffer bindings
+            continue;
         }
-        else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_ENABLED &&
-                 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_ENABLED)
+
+        size_t index = VertexArray::GetAttribIndex(dirtyBit);
+        if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_ENABLED &&
+            dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_ENABLED)
         {
-            size_t attribIndex =
-                static_cast<size_t>(dirtyBit) - VertexArray::DIRTY_BIT_ATTRIB_0_ENABLED;
-            updateAttribEnabled(attribIndex);
+            updateAttribEnabled(index);
         }
         else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_POINTER &&
                  dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_POINTER)
         {
-            size_t attribIndex =
-                static_cast<size_t>(dirtyBit) - VertexArray::DIRTY_BIT_ATTRIB_0_POINTER;
-            updateAttribPointer(attribIndex);
+            updateAttribPointer(index);
         }
-        else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_DIVISOR &&
-                 dirtyBit < VertexArray::DIRTY_BIT_ATTRIB_MAX_DIVISOR)
+        else if (dirtyBit >= VertexArray::DIRTY_BIT_ATTRIB_0_FORMAT &&
+                 dirtyBit < VertexArray::DIRTY_BIT_BINDING_MAX_BUFFER)
         {
-            size_t attribIndex =
-                static_cast<size_t>(dirtyBit) - VertexArray::DIRTY_BIT_ATTRIB_0_DIVISOR;
-            const VertexAttribute &attrib = mData.getVertexAttribute(attribIndex);
-
-            if (mAppliedAttributes[attribIndex].divisor != attrib.divisor)
-            {
-                mStateManager->bindVertexArray(mVertexArrayID, getAppliedElementArrayBufferID());
-                mFunctions->vertexAttribDivisor(static_cast<GLuint>(attribIndex), attrib.divisor);
-                mAppliedAttributes[attribIndex].divisor = attrib.divisor;
-            }
+            // TODO(jiawei.shao@intel.com): Vertex Attrib Bindings
+            ASSERT(index == mData.getBindingIndexFromAttribIndex(index));
+        }
+        else if (dirtyBit >= VertexArray::DIRTY_BIT_BINDING_0_DIVISOR &&
+                 dirtyBit < VertexArray::DIRTY_BIT_BINDING_MAX_DIVISOR)
+        {
+            updateAttribDivisor(index);
         }
         else
             UNREACHABLE();