Fix buffer bugs affecting transform feedback.

Running dEQP-GLES3.functional.transform_feedback would trigger a few
assertion failures because of some regressions that crept into our
code.

Among the regressions is assuming we can't map a buffer that has no
underlying storage object. This is valid since we sometimes allow
setData without creating a native storage.

Additionally we should free our existing data store on a new call
to SetData, to prevent old but "new" data revisions from complicating
our storage.

BUG=angle:679

Change-Id: Icd96a8a55a2b957d29382d8b8bbd7e8a7ab0cf65
Reviewed-on: https://chromium-review.googlesource.com/204342
Tested-by: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libGLESv2/renderer/d3d11/BufferStorage11.cpp b/src/libGLESv2/renderer/d3d11/BufferStorage11.cpp
index eb82aac..08c0182 100644
--- a/src/libGLESv2/renderer/d3d11/BufferStorage11.cpp
+++ b/src/libGLESv2/renderer/d3d11/BufferStorage11.cpp
@@ -166,10 +166,7 @@
 
 BufferStorage11::~BufferStorage11()
 {
-    for (auto it = mTypedBuffers.begin(); it != mTypedBuffers.end(); it++)
-    {
-        SafeDelete(it->second);
-    }
+    clear();
 }
 
 BufferStorage11 *BufferStorage11::makeBufferStorage11(BufferStorage *bufferStorage)
@@ -296,6 +293,13 @@
 
 void BufferStorage11::clear()
 {
+    for (auto it = mTypedBuffers.begin(); it != mTypedBuffers.end(); it++)
+    {
+        SafeDelete(it->second);
+    }
+
+    mTypedBuffers.clear();
+
     mSize = 0;
     mResolvedDataRevision = 0;
 }
@@ -499,15 +503,17 @@
     ASSERT(!mMappedStorage);
 
     TypedBufferStorage11 *latestStorage = getLatestStorage();
-    ASSERT(latestStorage);
-
-    if (latestStorage->getUsage() == BUFFER_USAGE_PIXEL_PACK ||
-        latestStorage->getUsage() == BUFFER_USAGE_STAGING)
+    if (latestStorage &&
+        (latestStorage->getUsage() == BUFFER_USAGE_PIXEL_PACK ||
+         latestStorage->getUsage() == BUFFER_USAGE_STAGING))
     {
+        // Latest storage is mappable.
         mMappedStorage = latestStorage;
     }
     else
     {
+        // Fall back to using the staging buffer if the latest storage does
+        // not exist or is not CPU-accessible.
         mMappedStorage = getStagingBuffer();
     }
 
@@ -517,6 +523,12 @@
         return NULL;
     }
 
+    if ((access & GL_MAP_WRITE_BIT) > 0)
+    {
+        // Update the data revision immediately, since the data might be changed at any time
+        mMappedStorage->setDataRevision(mMappedStorage->getDataRevision() + 1);
+    }
+
     return mMappedStorage->map(access);
 }