VulkanUnitTests: Free / Destroy all Vulkan objects.

Fixes a whole lot of leak-detector warnings.

Bug: b/133399620
Change-Id: I4f35150712dd2035a84c16d06ca75a4eba2c3c69
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/31836
Tested-by: Ben Clayton <bclayton@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
diff --git a/tests/VulkanUnitTests/Device.cpp b/tests/VulkanUnitTests/Device.cpp
index 098e736..18ebe18 100644
--- a/tests/VulkanUnitTests/Device.cpp
+++ b/tests/VulkanUnitTests/Device.cpp
@@ -171,6 +171,11 @@
 	return VK_SUCCESS;
 }
 
+void Device::DestroyBuffer(VkBuffer buffer) const
+{
+	driver->vkDestroyBuffer(device, buffer, nullptr);
+}
+
 VkResult Device::CreateShaderModule(
 		const std::vector<uint32_t>& spirv, VkShaderModule* out) const
 {
@@ -184,6 +189,11 @@
 	return driver->vkCreateShaderModule(device, &info, 0, out);
 }
 
+void Device::DestroyShaderModule(VkShaderModule shaderModule) const
+{
+	driver->vkDestroyShaderModule(device, shaderModule, nullptr);
+}
+
 VkResult Device::CreateDescriptorSetLayout(
 		const std::vector<VkDescriptorSetLayoutBinding>& bindings,
 		VkDescriptorSetLayout* out) const
@@ -199,6 +209,11 @@
 	return driver->vkCreateDescriptorSetLayout(device, &info, 0, out);
 }
 
+void Device::DestroyDescriptorSetLayout(VkDescriptorSetLayout descriptorSetLayout) const
+{
+	driver->vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
+}
+
 VkResult Device::CreatePipelineLayout(
 		VkDescriptorSetLayout layout, VkPipelineLayout* out) const
 {
@@ -215,6 +230,11 @@
 	return driver->vkCreatePipelineLayout(device, &info, 0, out);
 }
 
+void Device::DestroyPipelineLayout(VkPipelineLayout pipelineLayout) const
+{
+	driver->vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
+}
+
 VkResult Device::CreateComputePipeline(
 		VkShaderModule module, VkPipelineLayout pipelineLayout,
 		VkPipeline* out) const
@@ -241,6 +261,11 @@
 	return driver->vkCreateComputePipelines(device, 0, 1, &info, 0, out);
 }
 
+void Device::DestroyPipeline(VkPipeline pipeline) const
+{
+	driver->vkDestroyPipeline(device, pipeline, nullptr);
+}
+
 VkResult Device::CreateStorageBufferDescriptorPool(uint32_t descriptorCount,
 		VkDescriptorPool* out) const
 {
@@ -261,6 +286,11 @@
 	return driver->vkCreateDescriptorPool(device, &info, 0, out);
 }
 
+void Device::DestroyDescriptorPool(VkDescriptorPool descriptorPool) const
+{
+	driver->vkDestroyDescriptorPool(device, descriptorPool, nullptr);
+}
+
 VkResult Device::AllocateDescriptorSet(
 		VkDescriptorPool pool, VkDescriptorSetLayout layout,
 		VkDescriptorSet* out) const
@@ -331,6 +361,11 @@
     return VK_ERROR_OUT_OF_DEVICE_MEMORY; // TODO: Change to something not made up?
 }
 
+void Device::FreeMemory(VkDeviceMemory memory) const
+{
+	driver->vkFreeMemory(device, memory, nullptr);
+}
+
 VkResult Device::MapMemory(VkDeviceMemory memory, VkDeviceSize offset,
 		VkDeviceSize size, VkMemoryMapFlags flags, void **ppData) const
 {
@@ -353,6 +388,11 @@
     return driver->vkCreateCommandPool(device, &info, 0, out);
 }
 
+void Device::DestroyCommandPool(VkCommandPool commandPool) const
+{
+    return driver->vkDestroyCommandPool(device, commandPool, nullptr);
+}
+
 VkResult Device::AllocateCommandBuffer(
 		VkCommandPool pool, VkCommandBuffer* out) const
 {
@@ -366,6 +406,11 @@
     return driver->vkAllocateCommandBuffers(device, &info, out);
 }
 
+void Device::FreeCommandBuffer(VkCommandPool pool, VkCommandBuffer buffer)
+{
+	driver->vkFreeCommandBuffers(device, pool, 1, &buffer);
+}
+
 VkResult Device::BeginCommandBuffer(
 		VkCommandBufferUsageFlags usage, VkCommandBuffer commandBuffer) const
 {
diff --git a/tests/VulkanUnitTests/Device.hpp b/tests/VulkanUnitTests/Device.hpp
index 37b9108..ec582a7 100644
--- a/tests/VulkanUnitTests/Device.hpp
+++ b/tests/VulkanUnitTests/Device.hpp
@@ -46,32 +46,50 @@
 	VkResult CreateStorageBuffer(VkDeviceMemory memory, VkDeviceSize size,
 			VkDeviceSize offset, VkBuffer *out) const;
 
+	// DestroyBuffer destroys a VkBuffer.
+	void DestroyBuffer(VkBuffer buffer) const;
+
 	// CreateShaderModule creates a new shader module with the given SPIR-V
 	// code.
 	VkResult CreateShaderModule(const std::vector<uint32_t> &spirv,
 			VkShaderModule *out) const;
 
+	// DestroyShaderModule destroys a VkShaderModule.
+	void DestroyShaderModule(VkShaderModule shaderModule) const;
+
 	// CreateDescriptorSetLayout creates a new descriptor set layout with the
 	// given bindings.
 	VkResult CreateDescriptorSetLayout(
 			const std::vector<VkDescriptorSetLayoutBinding> &bindings,
 			VkDescriptorSetLayout *out) const;
 
+	// DestroyDescriptorSetLayout destroys a VkDescriptorSetLayout.
+	void DestroyDescriptorSetLayout(VkDescriptorSetLayout descriptorSetLayout) const;
+
 	// CreatePipelineLayout creates a new single set descriptor set layout.
 	VkResult CreatePipelineLayout(VkDescriptorSetLayout layout,
 			VkPipelineLayout *out) const;
 
+	// DestroyPipelineLayout destroys a VkPipelineLayout.
+	void DestroyPipelineLayout(VkPipelineLayout pipelineLayout) const;
+
 	// CreateComputePipeline creates a new compute pipeline with the entry point
 	// "main".
 	VkResult CreateComputePipeline(VkShaderModule module,
 			VkPipelineLayout pipelineLayout,
 			VkPipeline *out) const;
 
+	// DestroyPipeline destroys a graphics or compute pipeline.
+	void DestroyPipeline(VkPipeline pipeline) const;
+
 	// CreateStorageBufferDescriptorPool creates a new descriptor pool that can
 	// hold descriptorCount storage buffers.
 	VkResult CreateStorageBufferDescriptorPool(uint32_t descriptorCount,
 			VkDescriptorPool *out) const;
 
+	// DestroyDescriptorPool destroys the VkDescriptorPool.
+	void DestroyDescriptorPool(VkDescriptorPool descriptorPool) const;
+
 	// AllocateDescriptorSet allocates a single descriptor set with the given
 	// layout from pool.
 	VkResult AllocateDescriptorSet(VkDescriptorPool pool,
@@ -89,6 +107,9 @@
 	// VK_ERROR_OUT_OF_DEVICE_MEMORY is returned.
 	VkResult AllocateMemory(size_t size, VkMemoryPropertyFlags flags, VkDeviceMemory* out) const;
 
+	// FreeMemory frees the VkDeviceMemory.
+	void FreeMemory(VkDeviceMemory memory) const;
+
 	// MapMemory wraps vkMapMemory, supplying the first VkDevice parameter.
 	VkResult MapMemory(VkDeviceMemory memory, VkDeviceSize offset,
 			VkDeviceSize size, VkMemoryMapFlags flags, void **ppData) const;
@@ -99,9 +120,15 @@
 	// CreateCommandPool creates a new command pool.
 	VkResult CreateCommandPool(VkCommandPool* out) const;
 
+	// DestroyCommandPool destroys a VkCommandPool.
+	void DestroyCommandPool(VkCommandPool commandPool) const;
+
 	// AllocateCommandBuffer creates a new command buffer with a primary level.
 	VkResult AllocateCommandBuffer(VkCommandPool pool, VkCommandBuffer* out) const;
 
+	// FreeCommandBuffer frees the VkCommandBuffer.
+	void FreeCommandBuffer(VkCommandPool pool, VkCommandBuffer buffer);
+
 	// BeginCommandBuffer begins writing to commandBuffer.
 	VkResult BeginCommandBuffer(VkCommandBufferUsageFlags usage, VkCommandBuffer commandBuffer) const;
 
diff --git a/tests/VulkanUnitTests/VkInstanceFuncs.hpp b/tests/VulkanUnitTests/VkInstanceFuncs.hpp
index bcd054f..a8ba8f5 100644
--- a/tests/VulkanUnitTests/VkInstanceFuncs.hpp
+++ b/tests/VulkanUnitTests/VkInstanceFuncs.hpp
@@ -42,9 +42,19 @@
             VkPipelineLayout*);
 VK_INSTANCE(vkCreateShaderModule, VkResult, VkDevice, const VkShaderModuleCreateInfo*, const VkAllocationCallbacks*,
             VkShaderModule*);
+VK_INSTANCE(vkDestroyBuffer, void, VkDevice, VkBuffer, const VkAllocationCallbacks*);
+VK_INSTANCE(vkDestroyCommandPool, void, VkDevice, VkCommandPool, const VkAllocationCallbacks*);
+VK_INSTANCE(vkDestroyDescriptorPool, void, VkDevice, VkDescriptorPool, const VkAllocationCallbacks*);
+VK_INSTANCE(vkDestroyDescriptorSetLayout, void, VkDevice, VkDescriptorSetLayout, const VkAllocationCallbacks*);
 VK_INSTANCE(vkDestroyDevice, VkResult, VkDevice, const VkAllocationCallbacks*);
+VK_INSTANCE(vkDestroyInstance, void, VkInstance, const VkAllocationCallbacks*);
+VK_INSTANCE(vkDestroyPipeline, void, VkDevice, VkPipeline, const VkAllocationCallbacks*);
+VK_INSTANCE(vkDestroyPipelineLayout, void, VkDevice, VkPipelineLayout, const VkAllocationCallbacks*);
+VK_INSTANCE(vkDestroyShaderModule, void, VkDevice, VkShaderModule, const VkAllocationCallbacks*);
 VK_INSTANCE(vkEndCommandBuffer, VkResult, VkCommandBuffer);
 VK_INSTANCE(vkEnumeratePhysicalDevices, VkResult, VkInstance, uint32_t*, VkPhysicalDevice*);
+VK_INSTANCE(vkFreeCommandBuffers, void, VkDevice, VkCommandPool, uint32_t, const VkCommandBuffer*);
+VK_INSTANCE(vkFreeMemory, void, VkDevice, VkDeviceMemory, const VkAllocationCallbacks*);
 VK_INSTANCE(vkGetDeviceQueue, void, VkDevice, uint32_t, uint32_t, VkQueue*);
 VK_INSTANCE(vkGetPhysicalDeviceMemoryProperties, void, VkPhysicalDevice, VkPhysicalDeviceMemoryProperties*);
 VK_INSTANCE(vkGetPhysicalDeviceProperties, void, VkPhysicalDevice, VkPhysicalDeviceProperties*);
diff --git a/tests/VulkanUnitTests/unittests.cpp b/tests/VulkanUnitTests/unittests.cpp
index d2bfcc5..8a986b1 100644
--- a/tests/VulkanUnitTests/unittests.cpp
+++ b/tests/VulkanUnitTests/unittests.cpp
@@ -103,6 +103,8 @@
     EXPECT_EQ(physicalDeviceProperties.deviceType, VK_PHYSICAL_DEVICE_TYPE_CPU);

 

     EXPECT_EQ(strncmp(physicalDeviceProperties.deviceName, "SwiftShader Device", VK_MAX_PHYSICAL_DEVICE_NAME_SIZE), 0);

+

+    driver.vkDestroyInstance(instance, nullptr);

 }

 

 std::vector<uint32_t> compileSpirv(const char* assembly)

@@ -357,6 +359,19 @@
 

     device->UnmapMemory(memory);

     buffers = nullptr;

+

+    device->FreeCommandBuffer(commandPool, commandBuffer);

+    device->FreeMemory(memory);

+    device->DestroyPipeline(pipeline);

+    device->DestroyCommandPool(commandPool);

+    device->DestroyPipelineLayout(pipelineLayout);

+    device->DestroyDescriptorSetLayout(descriptorSetLayout);

+    device->DestroyDescriptorPool(descriptorPool);

+    device->DestroyBuffer(bufferIn);

+    device->DestroyBuffer(bufferOut);

+    device->DestroyShaderModule(shaderModule);

+    device.reset(nullptr);

+    driver.vkDestroyInstance(instance, nullptr);

 }

 

 INSTANTIATE_TEST_CASE_P(ComputeParams, SwiftShaderVulkanBufferToBufferComputeTest, testing::Values(