From 387baeed63aaf47d2ea67545422fdcc84b853f60 Mon Sep 17 00:00:00 2001 From: Ulrik Guenther Date: Thu, 12 Oct 2023 10:07:54 +0200 Subject: [PATCH] SwingSwapchain/VulkanPipeline/VulkanRenderpass/VulkanSwapchain: Fix incomplete cleanup --- .../scenery/backends/vulkan/SwingSwapchain.kt | 9 +++++--- .../scenery/backends/vulkan/VulkanPipeline.kt | 14 +++++++++---- .../backends/vulkan/VulkanRenderpass.kt | 1 + .../backends/vulkan/VulkanSwapchain.kt | 21 ++++++------------- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/graphics/scenery/backends/vulkan/SwingSwapchain.kt b/src/main/kotlin/graphics/scenery/backends/vulkan/SwingSwapchain.kt index f669fd8477..9d0c601e15 100644 --- a/src/main/kotlin/graphics/scenery/backends/vulkan/SwingSwapchain.kt +++ b/src/main/kotlin/graphics/scenery/backends/vulkan/SwingSwapchain.kt @@ -11,6 +11,7 @@ import org.lwjgl.system.MemoryUtil.memFree import org.lwjgl.system.Platform import org.lwjgl.vulkan.KHRSurface import org.lwjgl.vulkan.KHRSwapchain +import org.lwjgl.vulkan.VK10.vkQueueWaitIdle import org.lwjgl.vulkan.VkQueue import org.lwjgl.vulkan.awt.AWTVK import java.awt.BorderLayout @@ -69,8 +70,6 @@ open class SwingSwapchain(override val device: VulkanDevice, frame.addWindowListener(object : WindowAdapter() { override fun windowClosing(e: WindowEvent?) { super.windowClosing(e) - - KHRSurface.vkDestroySurfaceKHR(device.instance, surface, null) } }) @@ -145,8 +144,12 @@ open class SwingSwapchain(override val device: VulkanDevice, * Closes the swapchain, deallocating all of its resources. */ override fun close() { - logger.debug("Closing swapchain {}", this) + vkQueueWaitIdle(presentQueue) + + closeSyncPrimitives() + logger.debug("Closing swapchain {} with handle {}", this, handle.toHexString()) KHRSwapchain.vkDestroySwapchainKHR(device.vulkanDevice, handle, null) + KHRSurface.vkDestroySurfaceKHR(device.instance, surface, null) (sceneryPanel as? SceneryJPanel)?.remove(0) presentInfo.free() diff --git a/src/main/kotlin/graphics/scenery/backends/vulkan/VulkanPipeline.kt b/src/main/kotlin/graphics/scenery/backends/vulkan/VulkanPipeline.kt index 494a5b35b4..3b28f3a2e2 100644 --- a/src/main/kotlin/graphics/scenery/backends/vulkan/VulkanPipeline.kt +++ b/src/main/kotlin/graphics/scenery/backends/vulkan/VulkanPipeline.kt @@ -286,15 +286,21 @@ class VulkanPipeline(val device: VulkanDevice, val renderpass: VulkanRenderpass, override fun close() { val removedLayouts = ArrayList() - pipeline.forEach { _, pipeline -> - vkDestroyPipeline(device.vulkanDevice, pipeline.pipeline, null) + pipeline.forEach { (_, pipeline) -> + if(pipeline.pipeline != 0L) { + vkDestroyPipeline(device.vulkanDevice, pipeline.pipeline, null) + pipeline.pipeline = 0L + } - if(!removedLayouts.contains(pipeline.layout)) { - vkDestroyPipelineLayout(device.vulkanDevice, pipeline.layout, null) + if(!removedLayouts.contains(pipeline.layout) && pipeline.layout != 0L) { removedLayouts.add(pipeline.layout) + vkDestroyPipelineLayout(device.vulkanDevice, pipeline.layout, null) + pipeline.layout = 0L } } + pipeline.clear() + inputAssemblyState.free() rasterizationState.free() depthStencilState.free() diff --git a/src/main/kotlin/graphics/scenery/backends/vulkan/VulkanRenderpass.kt b/src/main/kotlin/graphics/scenery/backends/vulkan/VulkanRenderpass.kt index 8420d14701..c15ab039d5 100644 --- a/src/main/kotlin/graphics/scenery/backends/vulkan/VulkanRenderpass.kt +++ b/src/main/kotlin/graphics/scenery/backends/vulkan/VulkanRenderpass.kt @@ -844,6 +844,7 @@ open class VulkanRenderpass(val name: String, var config: RenderConfigReader.Ren logger.debug("Closing renderpass $name...") output.forEach { it.value.close() } configuredPipelines.forEach { it.value.close() } + pipelines.forEach { (_, pipeline) -> pipeline.close() } pipelines.clear() UBOs.forEach { it.value.close() } ownDescriptorSetLayouts.forEach { diff --git a/src/main/kotlin/graphics/scenery/backends/vulkan/VulkanSwapchain.kt b/src/main/kotlin/graphics/scenery/backends/vulkan/VulkanSwapchain.kt index 7ea2c342f7..ca037523a9 100644 --- a/src/main/kotlin/graphics/scenery/backends/vulkan/VulkanSwapchain.kt +++ b/src/main/kotlin/graphics/scenery/backends/vulkan/VulkanSwapchain.kt @@ -585,10 +585,10 @@ open class VulkanSwapchain(open val device: VulkanDevice, imageRenderedSemaphores.forEach { device.removeSemaphore(it) } imageRenderedSemaphores.clear() - fences.forEach { VK10.vkDestroyFence(device.vulkanDevice, it, null) } + fences.forEach { vkDestroyFence(device.vulkanDevice, it, null) } fences.clear() - imageUseFences.forEach { VK10.vkDestroyFence(device.vulkanDevice, it, null) } + imageUseFences.forEach { vkDestroyFence(device.vulkanDevice, it, null) } imageUseFences.clear() } @@ -599,24 +599,15 @@ open class VulkanSwapchain(open val device: VulkanDevice, vkQueueWaitIdle(presentQueue) vkQueueWaitIdle(queue) - logger.debug("Closing swapchain $this") + logger.debug("Closing swapchain {}", this) KHRSwapchain.vkDestroySwapchainKHR(device.vulkanDevice, handle, null) vkDestroySurfaceKHR(device.instance, surface, null) - imageAvailableSemaphores.forEach { device.removeSemaphore(it) } - imageAvailableSemaphores.clear() - imageRenderedSemaphores.forEach { device.removeSemaphore(it) } - imageRenderedSemaphores.clear() - - fences.forEach { vkDestroyFence(device.vulkanDevice, it, null) } - fences.clear() - - imageUseFences.forEach { vkDestroyFence(device.vulkanDevice, it, null) } - imageUseFences.clear() + closeSyncPrimitives() presentInfo.free() - MemoryUtil.memFree(swapchainImage) - MemoryUtil.memFree(swapchainPointer) + memFree(swapchainImage) + memFree(swapchainPointer) windowSizeCallback.close() (window as SceneryWindow.GLFWWindow?)?.let { window ->