Vulkan: Fix issue in DynamicBuffer

- We weren't keeping track of mSize correctly, causing some very
specific index buffer bindings to fail. The size returned when allocating
the buffer can be a bit different than the size requested, and that extra
space between mSize and the allocated size was not meant to be used,
causing errors in the validation layers.

Bug: angleproject:2580
Change-Id: I47eb7b8de6f4f657de14385b77ba6a459add599b
Reviewed-on: https://chromium-review.googlesource.com/1118607
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Luc Ferron <lucferron@chromium.org>
diff --git a/src/libANGLE/renderer/vulkan/BufferVk.cpp b/src/libANGLE/renderer/vulkan/BufferVk.cpp
index 53d7ad7..e6544e7 100644
--- a/src/libANGLE/renderer/vulkan/BufferVk.cpp
+++ b/src/libANGLE/renderer/vulkan/BufferVk.cpp
@@ -18,7 +18,7 @@
 namespace rx
 {
 
-BufferVk::BufferVk(const gl::BufferState &state) : BufferImpl(state), mCurrentRequiredSize(0)
+BufferVk::BufferVk(const gl::BufferState &state) : BufferImpl(state)
 {
 }
 
@@ -49,7 +49,7 @@
     ContextVk *contextVk = vk::GetImpl(context);
     VkDevice device      = contextVk->getDevice();
 
-    if (size > mCurrentRequiredSize)
+    if (size > static_cast<size_t>(mState.getSize()))
     {
         // Release and re-create the memory and buffer.
         release(contextVk->getRenderer());
@@ -76,7 +76,7 @@
         const VkMemoryPropertyFlags memoryPropertyFlags =
             (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
         ANGLE_TRY(vk::AllocateBufferMemory(contextVk->getRenderer(), memoryPropertyFlags, &mBuffer,
-                                           &mBufferMemory, &mCurrentRequiredSize));
+                                           &mBufferMemory));
     }
 
     if (data)
diff --git a/src/libANGLE/renderer/vulkan/BufferVk.h b/src/libANGLE/renderer/vulkan/BufferVk.h
index a1228a3..06c8254 100644
--- a/src/libANGLE/renderer/vulkan/BufferVk.h
+++ b/src/libANGLE/renderer/vulkan/BufferVk.h
@@ -64,7 +64,6 @@
 
     vk::Buffer mBuffer;
     vk::DeviceMemory mBufferMemory;
-    size_t mCurrentRequiredSize;
 };
 
 }  // namespace rx
diff --git a/src/libANGLE/renderer/vulkan/ProgramVk.cpp b/src/libANGLE/renderer/vulkan/ProgramVk.cpp
index 23fd105..8b9a1cb 100644
--- a/src/libANGLE/renderer/vulkan/ProgramVk.cpp
+++ b/src/libANGLE/renderer/vulkan/ProgramVk.cpp
@@ -422,9 +422,8 @@
             // Assume host visible/coherent memory available.
             VkMemoryPropertyFlags flags =
                 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
-            size_t requiredSize = 0;
             ANGLE_TRY(AllocateBufferMemory(renderer, flags, &mEmptyUniformBlockStorage.buffer,
-                                           &mEmptyUniformBlockStorage.memory, &requiredSize));
+                                           &mEmptyUniformBlockStorage.memory));
         }
 
         // Ensure the descriptor set range includes the uniform buffers at position 0.
diff --git a/src/libANGLE/renderer/vulkan/vk_helpers.cpp b/src/libANGLE/renderer/vulkan/vk_helpers.cpp
index f33af5e..af7f980 100644
--- a/src/libANGLE/renderer/vulkan/vk_helpers.cpp
+++ b/src/libANGLE/renderer/vulkan/vk_helpers.cpp
@@ -149,11 +149,13 @@
 
         mRetainedBuffers.emplace_back(std::move(mBuffer), std::move(mMemory));
 
+        mSize = std::max(sizeToAllocate, mMinSize);
+
         VkBufferCreateInfo createInfo;
         createInfo.sType                 = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
         createInfo.pNext                 = nullptr;
         createInfo.flags                 = 0;
-        createInfo.size                  = std::max(sizeToAllocate, mMinSize);
+        createInfo.size                  = mSize;
         createInfo.usage                 = mUsage;
         createInfo.sharingMode           = VK_SHARING_MODE_EXCLUSIVE;
         createInfo.queueFamilyIndexCount = 0;
@@ -161,7 +163,7 @@
         ANGLE_TRY(mBuffer.init(device, createInfo));
 
         ANGLE_TRY(AllocateBufferMemory(renderer, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &mBuffer,
-                                       &mMemory, &mSize));
+                                       &mMemory));
 
         ANGLE_TRY(mMemory.map(device, 0, mSize, 0, &mMappedMemory));
         mNextAllocationOffset        = 0;
@@ -508,7 +510,6 @@
 ImageHelper::ImageHelper()
     : mFormat(nullptr),
       mSamples(0),
-      mAllocatedMemorySize(0),
       mCurrentLayout(VK_IMAGE_LAYOUT_UNDEFINED),
       mLayerCount(0)
 {
@@ -520,7 +521,6 @@
       mExtents(other.mExtents),
       mFormat(other.mFormat),
       mSamples(other.mSamples),
-      mAllocatedMemorySize(other.mAllocatedMemorySize),
       mCurrentLayout(other.mCurrentLayout),
       mLayerCount(other.mLayerCount)
 {
@@ -594,8 +594,7 @@
                               VkMemoryPropertyFlags flags)
 {
     // TODO(jmadill): Memory sub-allocation. http://anglebug.com/2162
-    ANGLE_TRY(AllocateImageMemory(device, memoryProperties, flags, &mImage, &mDeviceMemory,
-                                  &mAllocatedMemorySize));
+    ANGLE_TRY(AllocateImageMemory(device, memoryProperties, flags, &mImage, &mDeviceMemory));
     return NoError();
 }
 
@@ -747,11 +746,6 @@
     return mSamples;
 }
 
-size_t ImageHelper::getAllocatedMemorySize() const
-{
-    return mAllocatedMemorySize;
-}
-
 void ImageHelper::changeLayoutWithStages(VkImageAspectFlags aspectMask,
                                          VkImageLayout newLayout,
                                          VkPipelineStageFlags srcStageMask,
diff --git a/src/libANGLE/renderer/vulkan/vk_helpers.h b/src/libANGLE/renderer/vulkan/vk_helpers.h
index 8787619..b758641 100644
--- a/src/libANGLE/renderer/vulkan/vk_helpers.h
+++ b/src/libANGLE/renderer/vulkan/vk_helpers.h
@@ -248,7 +248,6 @@
     gl::Extents mExtents;
     const Format *mFormat;
     GLint mSamples;
-    size_t mAllocatedMemorySize;
 
     // Current state.
     VkImageLayout mCurrentLayout;
diff --git a/src/libANGLE/renderer/vulkan/vk_utils.cpp b/src/libANGLE/renderer/vulkan/vk_utils.cpp
index a718d35..5660059 100644
--- a/src/libANGLE/renderer/vulkan/vk_utils.cpp
+++ b/src/libANGLE/renderer/vulkan/vk_utils.cpp
@@ -131,16 +131,12 @@
                                       const vk::MemoryProperties &memoryProperties,
                                       VkMemoryPropertyFlags memoryPropertyFlags,
                                       T *bufferOrImage,
-                                      vk::DeviceMemory *deviceMemoryOut,
-                                      size_t *requiredSizeOut)
+                                      vk::DeviceMemory *deviceMemoryOut)
 {
     // Call driver to determine memory requirements.
     VkMemoryRequirements memoryRequirements;
     bufferOrImage->getMemoryRequirements(device, &memoryRequirements);
 
-    // The requirements size is not always equal to the specified API size.
-    *requiredSizeOut = static_cast<size_t>(memoryRequirements.size);
-
     ANGLE_TRY(FindAndAllocateCompatibleMemory(device, memoryProperties, memoryPropertyFlags,
                                               memoryRequirements, deviceMemoryOut));
     ANGLE_TRY(bufferOrImage->bindMemory(device, *deviceMemoryOut));
@@ -1106,9 +1102,8 @@
         (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
 
     ANGLE_TRY(mBuffer.init(contextVk->getDevice(), createInfo));
-    ANGLE_TRY(
-        AllocateBufferMemory(contextVk->getRenderer(), flags, &mBuffer, &mDeviceMemory, &mSize));
-
+    ANGLE_TRY(AllocateBufferMemory(contextVk->getRenderer(), flags, &mBuffer, &mDeviceMemory));
+    mSize = static_cast<size_t>(size);
     return vk::NoError();
 }
 
@@ -1121,25 +1116,23 @@
 Error AllocateBufferMemory(RendererVk *renderer,
                            VkMemoryPropertyFlags memoryPropertyFlags,
                            Buffer *buffer,
-                           DeviceMemory *deviceMemoryOut,
-                           size_t *requiredSizeOut)
+                           DeviceMemory *deviceMemoryOut)
 {
     VkDevice device                              = renderer->getDevice();
     const vk::MemoryProperties &memoryProperties = renderer->getMemoryProperties();
 
     return AllocateBufferOrImageMemory(device, memoryProperties, memoryPropertyFlags, buffer,
-                                       deviceMemoryOut, requiredSizeOut);
+                                       deviceMemoryOut);
 }
 
 Error AllocateImageMemory(VkDevice device,
                           const MemoryProperties &memoryProperties,
                           VkMemoryPropertyFlags memoryPropertyFlags,
                           Image *image,
-                          DeviceMemory *deviceMemoryOut,
-                          size_t *requiredSizeOut)
+                          DeviceMemory *deviceMemoryOut)
 {
     return AllocateBufferOrImageMemory(device, memoryProperties, memoryPropertyFlags, image,
-                                       deviceMemoryOut, requiredSizeOut);
+                                       deviceMemoryOut);
 }
 
 // GarbageObject implementation.
diff --git a/src/libANGLE/renderer/vulkan/vk_utils.h b/src/libANGLE/renderer/vulkan/vk_utils.h
index 55f1aa0..ab73d0d 100644
--- a/src/libANGLE/renderer/vulkan/vk_utils.h
+++ b/src/libANGLE/renderer/vulkan/vk_utils.h
@@ -657,8 +657,7 @@
 Error AllocateBufferMemory(RendererVk *renderer,
                            VkMemoryPropertyFlags memoryPropertyFlags,
                            Buffer *buffer,
-                           DeviceMemory *deviceMemoryOut,
-                           size_t *requiredSizeOut);
+                           DeviceMemory *deviceMemoryOut);
 
 struct BufferAndMemory final : angle::NonCopyable
 {
@@ -675,8 +674,7 @@
                           const MemoryProperties &memoryProperties,
                           VkMemoryPropertyFlags memoryPropertyFlags,
                           Image *image,
-                          DeviceMemory *deviceMemoryOut,
-                          size_t *requiredSizeOut);
+                          DeviceMemory *deviceMemoryOut);
 
 using ShaderAndSerial = ObjectAndSerial<ShaderModule>;
 
diff --git a/src/tests/deqp_support/deqp_gles2_test_expectations.txt b/src/tests/deqp_support/deqp_gles2_test_expectations.txt
index 49be2d0..514b722 100644
--- a/src/tests/deqp_support/deqp_gles2_test_expectations.txt
+++ b/src/tests/deqp_support/deqp_gles2_test_expectations.txt
@@ -262,9 +262,6 @@
 1028 WIN LINUX MAC : dEQP-GLES2.functional.fbo.completeness.renderable.texture.depth.srgb8 = FAIL
 
 // Vulkan failures
-2580 VULKAN : dEQP-GLES2.functional.buffer.write.recreate_store.different_size = SKIP
-2580 VULKAN : dEQP-GLES2.functional.buffer.write.recreate_store.random_* = SKIP
-2580 VULKAN : dEQP-GLES2.functional.buffer.write.random.* = SKIP
 2494 VULKAN : dEQP-GLES2.functional.shaders.struct.uniform.sampler_in_function_arg_* = SKIP
 2494 VULKAN : dEQP-GLES2.functional.shaders.struct.uniform.sampler_in_array_function_arg_* = SKIP
 2592 VULKAN : dEQP-GLES2.functional.shaders.builtin_variable.depth_range* = SKIP