D3D11: map index buffers only when needed.

Before this patch, index buffers where always mapped in case we needed the
index data for the indexed point sprites workaround.
This patch makes it so the index buffer is only mapped in this case, when
we need to stream index data or when we need to fill the static copies of
the index buffers.

This make the memory usage of
http://alteredqualia.com/xg/examples/mammoth.html go down from 41MB to
28MB.

BUG=angleproject:516

Change-Id: I937506d06fd6f074ef2120469dbd235e20245fca
Reviewed-on: https://chromium-review.googlesource.com/283626
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Tested-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/libANGLE/renderer/d3d/IndexDataManager.cpp b/src/libANGLE/renderer/d3d/IndexDataManager.cpp
index 0d1db5a..5328a03 100644
--- a/src/libANGLE/renderer/d3d/IndexDataManager.cpp
+++ b/src/libANGLE/renderer/d3d/IndexDataManager.cpp
@@ -133,9 +133,13 @@
     const gl::Type &srcTypeInfo = gl::GetTypeInfo(srcType);
     const gl::Type &dstTypeInfo = gl::GetTypeInfo(dstType);
 
+    BufferD3D *buffer = glBuffer ? GetImplAs<BufferD3D>(glBuffer) : nullptr;
+
     translated->indexType = dstType;
     if (sourceData)
     {
+        sourceData->srcBuffer = buffer;
+        sourceData->srcIndices = indices;
         sourceData->srcIndexType = srcType;
         sourceData->srcCount = count;
     }
@@ -144,16 +148,10 @@
     if (glBuffer == nullptr)
     {
         translated->storage = nullptr;
-        if (sourceData)
-        {
-            sourceData->srcIndices = indices;
-        }
         return streamIndexData(indices, count, srcType, dstType, translated);
     }
 
     // Case 2: the indices are already in a buffer
-    BufferD3D *buffer = GetImplAs<BufferD3D>(glBuffer);
-
     unsigned int offset = static_cast<unsigned int>(reinterpret_cast<uintptr_t>(indices));
     ASSERT(srcTypeInfo.bytes * static_cast<unsigned int>(count) + offset <= buffer->getSize());
 
@@ -166,21 +164,6 @@
       default: UNREACHABLE(); offsetAligned = false;
     }
 
-    // TODO(cwallez) avoid doing this in all cases as it prevents optimizations
-    // of the sysmem buffer copy
-    const uint8_t *bufferData = nullptr;
-    gl::Error error = buffer->getData(&bufferData);
-    if (error.isError())
-    {
-        return error;
-    }
-    ASSERT(bufferData != nullptr);
-
-    if (sourceData)
-    {
-        sourceData->srcIndices = bufferData + offset;
-    }
-
     // Case 2a: the buffer can be used directly
     if (offsetAligned && buffer->supportsDirectBinding() &&
         dstType == srcType && !primitiveRestartWorkaround)
@@ -213,7 +196,15 @@
 
     if (staticBuffer == nullptr || !offsetAligned)
     {
-        gl::Error error = streamIndexData(bufferData, count, srcType, dstType, translated);
+        const uint8_t *bufferData = nullptr;
+        gl::Error error = buffer->getData(&bufferData);
+        if (error.isError())
+        {
+            return error;
+        }
+        ASSERT(bufferData != nullptr);
+
+        error = streamIndexData(bufferData, count, srcType, dstType, translated);
         if (error.isError())
         {
             return error;
@@ -223,9 +214,17 @@
     {
         if (!staticBufferInitialized)
         {
+            const uint8_t *bufferData = nullptr;
+            gl::Error error = buffer->getData(&bufferData);
+            if (error.isError())
+            {
+                return error;
+            }
+            ASSERT(bufferData != nullptr);
+
             unsigned int convertCount = buffer->getSize() >> srcTypeInfo.bytesShift;
-            gl::Error error = StreamInIndexBuffer(staticBuffer, bufferData, convertCount,
-                                                  srcType, dstType, nullptr);
+            error = StreamInIndexBuffer(staticBuffer, bufferData, convertCount,
+                                        srcType, dstType, nullptr);
             if (error.isError())
             {
                 return error;