Fix VertexBuffer11::getSpaceRequired for instanced attribs.

The computation could copy more data than needed. Although benign
it could cause performance degredation compared to the D3D9 back
end.

Change-Id: I3cf1dc79085c33d44040fd55153e63a4e5e63cb1
Reviewed-on: https://chromium-review.googlesource.com/210640
Tested-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Nicolas Capens <capn@chromium.org>
diff --git a/src/common/mathutil.h b/src/common/mathutil.h
index 58f9b4c..02b44c2 100644
--- a/src/common/mathutil.h
+++ b/src/common/mathutil.h
@@ -518,6 +518,12 @@
     return value + alignment - 1 - (value - 1) % alignment;
 }
 
+inline unsigned int UnsignedCeilDivide(unsigned int value, unsigned int divisor)
+{
+    unsigned int divided = value / divisor;
+    return (divided + ((value % divisor == 0) ? 0 : 1));
+}
+
 template <class T>
 inline bool IsUnsignedAdditionSafe(T lhs, T rhs)
 {
diff --git a/src/libGLESv2/renderer/d3d/d3d11/VertexBuffer11.cpp b/src/libGLESv2/renderer/d3d/d3d11/VertexBuffer11.cpp
index 2f47ec0..1908146 100644
--- a/src/libGLESv2/renderer/d3d/d3d11/VertexBuffer11.cpp
+++ b/src/libGLESv2/renderer/d3d/d3d11/VertexBuffer11.cpp
@@ -136,15 +136,8 @@
         }
         else
         {
-            if (static_cast<unsigned int>(instances) < std::numeric_limits<unsigned int>::max() - (attrib.divisor - 1))
-            {
-                // Round up
-                elementCount = rx::roundUp(static_cast<unsigned int>(instances), attrib.divisor);
-            }
-            else
-            {
-                elementCount = instances / attrib.divisor;
-            }
+            // Round up to divisor, if possible
+            elementCount = rx::UnsignedCeilDivide(static_cast<unsigned int>(instances), attrib.divisor);
         }
 
         gl::VertexFormat vertexFormat(attrib);
diff --git a/src/libGLESv2/renderer/d3d/d3d9/VertexBuffer9.cpp b/src/libGLESv2/renderer/d3d/d3d9/VertexBuffer9.cpp
index d260640..7ad7c95 100644
--- a/src/libGLESv2/renderer/d3d/d3d9/VertexBuffer9.cpp
+++ b/src/libGLESv2/renderer/d3d/d3d9/VertexBuffer9.cpp
@@ -214,15 +214,8 @@
         }
         else
         {
-            if (static_cast<unsigned int>(instances) < std::numeric_limits<unsigned int>::max() - (attrib.divisor - 1))
-            {
-                // Round up
-                elementCount = (static_cast<unsigned int>(instances) + (attrib.divisor - 1)) / attrib.divisor;
-            }
-            else
-            {
-                elementCount = static_cast<unsigned int>(instances) / attrib.divisor;
-            }
+            // Round up to divisor, if possible
+            elementCount = rx::UnsignedCeilDivide(static_cast<unsigned int>(instances), attrib.divisor);
         }
 
         if (elementSize <= std::numeric_limits<unsigned int>::max() / elementCount)