CommandPool reset and trim

Implemented vkResetCommandPool and vkTrimCommandPool.
The reset command out all command buffers in the command
pool back to the initial state. It also frees up the entire pool.

The trim command is a potential future memory usage optimization.
It is currently a noop.

Passes a few tests in dEQP-VK.api.command_buffers.

Bug b/119827933

Change-Id: Ic974f63a8dfca014462ac904218b9dcc3035fc8a
Reviewed-on: https://swiftshader-review.googlesource.com/c/24370
Tested-by: Alexis Hétu <sugoi@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Reviewed-by: Chris Forbes <chrisforbes@google.com>
diff --git a/src/Vulkan/VkCommandPool.cpp b/src/Vulkan/VkCommandPool.cpp
index 821d4fb..85c5738 100644
--- a/src/Vulkan/VkCommandPool.cpp
+++ b/src/Vulkan/VkCommandPool.cpp
@@ -13,6 +13,7 @@
 // limitations under the License.
 
 #include "VkCommandPool.hpp"
+#include "VkCommandBuffer.hpp"
 #include "VkDestroy.h"
 #include <algorithm>
 
@@ -79,4 +80,28 @@
 	}
 }
 
+VkResult CommandPool::reset(VkCommandPoolResetFlags flags)
+{
+	// According the Vulkan 1.1 spec:
+	// "All command buffers that have been allocated from
+	//  the command pool are put in the initial state."
+	for(auto commandBuffer : *commandBuffers)
+	{
+		Cast(commandBuffer)->reset(flags);
+	}
+
+	// According the Vulkan 1.1 spec:
+	// "Resetting a command pool recycles all of the
+	//  resources from all of the command buffers allocated
+	//  from the command pool back to the command pool."
+	commandBuffers->clear();
+
+	return VK_SUCCESS;
+}
+
+void CommandPool::trim(VkCommandPoolTrimFlags flags)
+{
+	// TODO (b/119827933): Optimize memory usage here
+}
+
 } // namespace vk
diff --git a/src/Vulkan/VkCommandPool.hpp b/src/Vulkan/VkCommandPool.hpp
index 05135c1..fca0a4b 100644
--- a/src/Vulkan/VkCommandPool.hpp
+++ b/src/Vulkan/VkCommandPool.hpp
@@ -32,6 +32,8 @@
 
 	VkResult allocateCommandBuffers(VkCommandBufferLevel level, uint32_t commandBufferCount, VkCommandBuffer* pCommandBuffers);
 	void freeCommandBuffers(uint32_t commandBufferCount, const VkCommandBuffer* pCommandBuffers);
+	VkResult reset(VkCommandPoolResetFlags flags);
+	void trim(VkCommandPoolTrimFlags flags);
 
 private:
 	std::set<VkCommandBuffer>* commandBuffers;
diff --git a/src/Vulkan/libVulkan.cpp b/src/Vulkan/libVulkan.cpp
index 6285653..a8959d0 100644
--- a/src/Vulkan/libVulkan.cpp
+++ b/src/Vulkan/libVulkan.cpp
@@ -1175,9 +1175,10 @@
 
 VKAPI_ATTR VkResult VKAPI_CALL vkResetCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolResetFlags flags)
 {
-	TRACE("()");
-	UNIMPLEMENTED();
-	return VK_SUCCESS;
+	TRACE("(VkDevice device = 0x%X, VkCommandPool commandPool = 0x%X, VkCommandPoolResetFlags flags = %d )",

+		device, commandPool, flags);

+
+	return vk::Cast(commandPool)->reset(flags);
 }
 
 VKAPI_ATTR VkResult VKAPI_CALL vkAllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers)
@@ -1888,8 +1889,10 @@
 
 VKAPI_ATTR void VKAPI_CALL vkTrimCommandPool(VkDevice device, VkCommandPool commandPool, VkCommandPoolTrimFlags flags)
 {
-	TRACE("()");
-	UNIMPLEMENTED();
+	TRACE("(VkDevice device = 0x%X, VkCommandPool commandPool = 0x%X, VkCommandPoolTrimFlags flags = %d)",

+	      device, commandPool, flags);

+

+	vk::Cast(commandPool)->trim(flags);
 }
 
 VKAPI_ATTR void VKAPI_CALL vkGetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2* pQueueInfo, VkQueue* pQueue)