deinitialize

Ensures all created objects are cleaned up correctly and allocated memory is freed

This function ensures that all the objects that were created are cleaned up correctly and nothing is left "open" when the application is closed.

Wait for the device to have finished all operations before starting the clean up.

 	vkDeviceWaitIdle(appManager.device)
 

Destroy the fence used to sync work between the CPU and GPU.

 	vkWaitForFences(appManager.device, static_cast<uint32_t>(appManager.frameFences.size()), appManager.frameFences.data(), true, uint64_t(-1));
 	vkResetFences(appManager.device, static_cast<uint32_t>(appManager.frameFences.size()), appManager.frameFences.data());
 	for (auto& fence : appManager.frameFences)
 	{
 		vkDestroyFence(appManager.device, fence, nullptr);
 	}
 

Destroy the semaphores used for image acquisition and rendering.

 	for (auto& semaphore : appManager.acquireSemaphore)
 	{
 		vkDestroySemaphore(appManager.device, semaphore, nullptr);
 	}
 
 	for (auto& semaphore : appManager.presentSemaphores)
 	{
 		vkDestroySemaphore(appManager.device, semaphore, nullptr);
 	}
 

Free the memory allocated for the descriptor sets.

 	vkFreeDescriptorSets(appManager.device, appManager.descriptorPool, 1, &appManager.staticDescSet);
 	vkFreeDescriptorSets(appManager.device, appManager.descriptorPool, 1, &appManager.dynamicDescSet);
 

Destroy both the descriptor layouts and descriptor pool.

 	vkDestroyDescriptorSetLayout(appManager.device, appManager.staticDescriptorSetLayout, nullptr);
 	vkDestroyDescriptorSetLayout(appManager.device, appManager.dynamicDescriptorSetLayout, nullptr);
 	vkDestroyDescriptorPool(appManager.device, appManager.descriptorPool, nullptr);
 

Destroy the uniform buffer and free the memory.

 	vkDestroyBuffer(appManager.device, appManager.dynamicUniformBufferData.buffer, nullptr);
 	vkFreeMemory(appManager.device, appManager.dynamicUniformBufferData.memory, nullptr);
 

Destroy the pipeline followed by the pipeline layout.

 	vkDestroyPipeline(appManager.device, appManager.pipeline, nullptr);
 	vkDestroyPipelineLayout(appManager.device, appManager.pipelineLayout, nullptr);
 

Destroy the texture image.

 	vkDestroyImage(appManager.device, appManager.texture.image, nullptr);
 	

Destroy the texture image view.

 	vkDestroyImageView(appManager.device, appManager.texture.view, nullptr);
 	

Free the memory allocated for the texture.

 	vkFreeMemory(appManager.device, appManager.texture.memory, nullptr);
 	

Destroy the sampler.

 	vkDestroySampler(appManager.device, appManager.texture.sampler, nullptr);
 

Destroy then free the memory for the vertex buffer.

 	vkDestroyBuffer(appManager.device, appManager.vertexBuffer.buffer, nullptr);
 	vkFreeMemory(appManager.device, appManager.vertexBuffer.memory, nullptr);
 

Iterate through each of the framebuffers and destroy them.

 	for (uint32_t i = 0; i < appManager.frameBuffers.size(); i++)
 	{
 		vkDestroyFramebuffer(appManager.device, appManager.frameBuffers[i], nullptr);
 	}
 

Destroy the two shader modules - vertex and fragment.

 	vkDestroyShaderModule(appManager.device, appManager.shaderStages[0].module, nullptr);
 	vkDestroyShaderModule(appManager.device, appManager.shaderStages[1].module, nullptr);
 

Destroy the render pass.

 	vkDestroyRenderPass(appManager.device, appManager.renderPass, nullptr);
 

Clean up the swapchain image views.

 	for (auto& imagebuffers : appManager.swapChainImages)
 	{
 		vkDestroyImageView(appManager.device, imagebuffers.view, nullptr);
 	}
 

Free the allocated memory in the command buffers.

 	vkFreeCommandBuffers(appManager.device, appManager.commandPool, static_cast<uint32_t>(appManager.commandBuffers.size()), appManager.commandBuffers.data());
 

Destroy the command pool.

 	vkDestroyCommandPool(appManager.device, appManager.commandPool, nullptr);
 

Clean up the swapchain.

 	vkDestroySwapchainKHR(appManager.device, appManager.swapchain, nullptr);
 	

Clean up the surface.

 	vkDestroySurfaceKHR(appManager.instance, appManager.surface, nullptr);
 

Destroy the logical device.

 	vkDestroyDevice(appManager.device, nullptr);