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/VertexArray.cpp b/src/libANGLE/VertexArray.cpp
index 0b8fcb6..cea141d 100644
--- a/src/libANGLE/VertexArray.cpp
+++ b/src/libANGLE/VertexArray.cpp
@@ -14,22 +14,33 @@
 namespace gl
 {
 
-VertexArrayState::VertexArrayState(size_t maxAttribs)
-    : mLabel(), mVertexAttributes(maxAttribs), mMaxEnabledAttribute(0)
+VertexArrayState::VertexArrayState(size_t maxAttribs, size_t maxAttribBindings)
+    : mLabel(), mVertexBindings(maxAttribBindings), mMaxEnabledAttribute(0)
 {
+    ASSERT(maxAttribs <= maxAttribBindings);
+
+    for (size_t i = 0; i < maxAttribs; i++)
+    {
+        mVertexAttributes.emplace_back(static_cast<GLuint>(i));
+    }
 }
 
 VertexArrayState::~VertexArrayState()
 {
-    for (size_t i = 0; i < getMaxAttribs(); i++)
+    for (auto &binding : mVertexBindings)
     {
-        mVertexAttributes[i].buffer.set(nullptr);
+        binding.buffer.set(nullptr);
     }
     mElementArrayBuffer.set(nullptr);
 }
 
-VertexArray::VertexArray(rx::GLImplFactory *factory, GLuint id, size_t maxAttribs)
-    : mId(id), mState(maxAttribs), mVertexArray(factory->createVertexArray(mState))
+VertexArray::VertexArray(rx::GLImplFactory *factory,
+                         GLuint id,
+                         size_t maxAttribs,
+                         size_t maxAttribBindings)
+    : mId(id),
+      mState(maxAttribs, maxAttribBindings),
+      mVertexArray(factory->createVertexArray(mState))
 {
 }
 
@@ -55,11 +66,11 @@
 
 void VertexArray::detachBuffer(GLuint bufferName)
 {
-    for (size_t attribute = 0; attribute < getMaxAttribs(); attribute++)
+    for (auto &binding : mState.mVertexBindings)
     {
-        if (mState.mVertexAttributes[attribute].buffer.id() == bufferName)
+        if (binding.buffer.id() == bufferName)
         {
-            mState.mVertexAttributes[attribute].buffer.set(nullptr);
+            binding.buffer.set(nullptr);
         }
     }
 
@@ -69,31 +80,97 @@
     }
 }
 
-const VertexAttribute &VertexArray::getVertexAttribute(size_t attributeIndex) const
+const VertexAttribute &VertexArray::getVertexAttribute(size_t attribIndex) const
 {
-    ASSERT(attributeIndex < getMaxAttribs());
-    return mState.mVertexAttributes[attributeIndex];
+    ASSERT(attribIndex < getMaxAttribs());
+    return mState.mVertexAttributes[attribIndex];
+}
+
+const VertexBinding &VertexArray::getVertexBinding(size_t bindingIndex) const
+{
+    ASSERT(bindingIndex < getMaxBindings());
+    return mState.mVertexBindings[bindingIndex];
+}
+
+size_t VertexArray::GetAttribIndex(unsigned long dirtyBit)
+{
+    static_assert(gl::MAX_VERTEX_ATTRIBS == gl::MAX_VERTEX_ATTRIB_BINDINGS,
+                  "The stride of vertex attributes should equal to that of vertex bindings.");
+    ASSERT(dirtyBit > gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER);
+    return (dirtyBit - gl::VertexArray::DIRTY_BIT_ATTRIB_0_ENABLED) % gl::MAX_VERTEX_ATTRIBS;
+}
+
+void VertexArray::bindVertexBuffer(size_t bindingIndex,
+                                   Buffer *boundBuffer,
+                                   GLintptr offset,
+                                   GLsizei stride)
+{
+    ASSERT(bindingIndex < getMaxBindings());
+
+    VertexBinding *binding = &mState.mVertexBindings[bindingIndex];
+
+    binding->buffer.set(boundBuffer);
+    binding->offset = offset;
+    binding->stride = stride;
+    mDirtyBits.set(DIRTY_BIT_BINDING_0_BUFFER + bindingIndex);
+}
+
+void VertexArray::setVertexAttribBinding(size_t attribIndex, size_t bindingIndex)
+{
+    ASSERT(attribIndex < getMaxAttribs() && bindingIndex < getMaxBindings());
+
+    // TODO(jiawei.shao@intel.com): Vertex Attrib Bindings
+    ASSERT(attribIndex == bindingIndex);
+    mDirtyBits.set(DIRTY_BIT_ATTRIB_0_BINDING + attribIndex);
+}
+
+void VertexArray::setVertexBindingDivisor(size_t bindingIndex, GLuint divisor)
+{
+    ASSERT(bindingIndex < getMaxBindings());
+
+    mState.mVertexBindings[bindingIndex].divisor = divisor;
+    mDirtyBits.set(DIRTY_BIT_BINDING_0_DIVISOR + bindingIndex);
+}
+
+void VertexArray::setVertexAttribFormat(size_t attribIndex,
+                                        GLint size,
+                                        GLenum type,
+                                        bool normalized,
+                                        bool pureInteger,
+                                        GLintptr relativeOffset)
+{
+    ASSERT(attribIndex < getMaxAttribs());
+
+    VertexAttribute *attrib = &mState.mVertexAttributes[attribIndex];
+
+    attrib->size           = size;
+    attrib->type           = type;
+    attrib->normalized     = normalized;
+    attrib->pureInteger    = pureInteger;
+    attrib->relativeOffset = relativeOffset;
+    mDirtyBits.set(DIRTY_BIT_ATTRIB_0_FORMAT + attribIndex);
 }
 
 void VertexArray::setVertexAttribDivisor(size_t index, GLuint divisor)
 {
     ASSERT(index < getMaxAttribs());
-    mState.mVertexAttributes[index].divisor = divisor;
-    mDirtyBits.set(DIRTY_BIT_ATTRIB_0_DIVISOR + index);
+
+    setVertexAttribBinding(index, index);
+    setVertexBindingDivisor(index, divisor);
 }
 
-void VertexArray::enableAttribute(size_t attributeIndex, bool enabledState)
+void VertexArray::enableAttribute(size_t attribIndex, bool enabledState)
 {
-    ASSERT(attributeIndex < getMaxAttribs());
-    mState.mVertexAttributes[attributeIndex].enabled = enabledState;
-    mDirtyBits.set(DIRTY_BIT_ATTRIB_0_ENABLED + attributeIndex);
+    ASSERT(attribIndex < getMaxAttribs());
+    mState.mVertexAttributes[attribIndex].enabled = enabledState;
+    mDirtyBits.set(DIRTY_BIT_ATTRIB_0_ENABLED + attribIndex);
 
     // Update state cache
     if (enabledState)
     {
-        mState.mMaxEnabledAttribute = std::max(attributeIndex + 1, mState.mMaxEnabledAttribute);
+        mState.mMaxEnabledAttribute = std::max(attribIndex + 1, mState.mMaxEnabledAttribute);
     }
-    else if (mState.mMaxEnabledAttribute == attributeIndex + 1)
+    else if (mState.mMaxEnabledAttribute == attribIndex + 1)
     {
         while (mState.mMaxEnabledAttribute > 0 &&
                !mState.mVertexAttributes[mState.mMaxEnabledAttribute - 1].enabled)
@@ -103,21 +180,31 @@
     }
 }
 
-void VertexArray::setAttributeState(size_t attributeIndex, gl::Buffer *boundBuffer, GLint size, GLenum type,
-                                    bool normalized, bool pureInteger, GLsizei stride, const void *pointer)
+void VertexArray::setAttributeState(size_t attribIndex,
+                                    gl::Buffer *boundBuffer,
+                                    GLint size,
+                                    GLenum type,
+                                    bool normalized,
+                                    bool pureInteger,
+                                    GLsizei stride,
+                                    const void *pointer)
 {
-    ASSERT(attributeIndex < getMaxAttribs());
+    ASSERT(attribIndex < getMaxAttribs());
 
-    VertexAttribute *attrib = &mState.mVertexAttributes[attributeIndex];
+    GLintptr offset = boundBuffer ? reinterpret_cast<GLintptr>(pointer) : 0;
 
-    attrib->buffer.set(boundBuffer);
-    attrib->size = size;
-    attrib->type = type;
-    attrib->normalized = normalized;
-    attrib->pureInteger = pureInteger;
-    attrib->stride = stride;
-    attrib->pointer = pointer;
-    mDirtyBits.set(DIRTY_BIT_ATTRIB_0_POINTER + attributeIndex);
+    setVertexAttribFormat(attribIndex, size, type, normalized, pureInteger, 0);
+    setVertexAttribBinding(attribIndex, attribIndex);
+
+    VertexAttribute &attrib = mState.mVertexAttributes[attribIndex];
+    GLsizei effectiveStride =
+        stride != 0 ? stride : static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib));
+    attrib.pointer                 = pointer;
+    attrib.vertexAttribArrayStride = stride;
+
+    bindVertexBuffer(attribIndex, boundBuffer, offset, effectiveStride);
+
+    mDirtyBits.set(DIRTY_BIT_ATTRIB_0_POINTER + attribIndex);
 }
 
 void VertexArray::setElementArrayBuffer(Buffer *buffer)
@@ -135,4 +222,4 @@
     }
 }
 
-}
+}  // namespace gl