Reland 'Remove IndexRange retrieving in validation'

This change adds GL_KHR_robust_buffer_access_behavior support.
The old change is in https://chromium-review.googlesource.com/c/angle/angle/+/607413

BUG=755897, angleproject:1393, angleproject:1463

Change-Id: I04a1132c3ae8d3a766194df61c4ff7bf0b084f03
Reviewed-on: https://chromium-review.googlesource.com/640750
Commit-Queue: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libANGLE/renderer/d3d/VertexDataManager.cpp b/src/libANGLE/renderer/d3d/VertexDataManager.cpp
index f5c6cf0..04081e0 100644
--- a/src/libANGLE/renderer/d3d/VertexDataManager.cpp
+++ b/src/libANGLE/renderer/d3d/VertexDataManager.cpp
@@ -410,7 +410,7 @@
     for (auto attribIndex : dynamicAttribsMask)
     {
         const auto &dynamicAttrib = (*translatedAttribs)[attribIndex];
-        ANGLE_TRY(reserveSpaceForAttrib(dynamicAttrib, count, instances));
+        ANGLE_TRY(reserveSpaceForAttrib(dynamicAttrib, start, count, instances));
     }
 
     // Store dynamic attributes
@@ -445,6 +445,7 @@
 }
 
 gl::Error VertexDataManager::reserveSpaceForAttrib(const TranslatedAttribute &translatedAttrib,
+                                                   GLint start,
                                                    GLsizei count,
                                                    GLsizei instances) const
 {
@@ -460,10 +461,23 @@
 
     size_t totalCount = gl::ComputeVertexBindingElementCount(
         binding.getDivisor(), static_cast<size_t>(count), static_cast<size_t>(instances));
-    ASSERT(!bufferD3D ||
-           ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize())) >=
-               static_cast<int>(totalCount));
+    // TODO(jiajia.qin@intel.com): force the index buffer to clamp any out of range indices instead
+    // of invalid operation here.
+    if (bufferD3D)
+    {
+        // Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
+        // a non-instanced draw call
+        GLint firstVertexIndex = binding.getDivisor() > 0 ? 0 : start;
+        int64_t maxVertexCount =
+            static_cast<int64_t>(firstVertexIndex) + static_cast<int64_t>(totalCount);
+        int elementsInBuffer =
+            ElementsInBuffer(attrib, binding, static_cast<unsigned int>(bufferD3D->getSize()));
 
+        if (maxVertexCount > elementsInBuffer)
+        {
+            return gl::InvalidOperation() << "Vertex buffer is not big enough for the draw call.";
+        }
+    }
     return mStreamingBuffer->reserveVertexSpace(attrib, binding, static_cast<GLsizei>(totalCount),
                                                 instances);
 }