Skip to content

Commit

Permalink
SwingSwapchain/VulkanPipeline/VulkanRenderpass/VulkanSwapchain: Fix i…
Browse files Browse the repository at this point in the history
…ncomplete cleanup
  • Loading branch information
skalarproduktraum committed Oct 12, 2023
1 parent 77028d6 commit 387baee
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

})
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 10 additions & 4 deletions src/main/kotlin/graphics/scenery/backends/vulkan/VulkanPipeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,21 @@ class VulkanPipeline(val device: VulkanDevice, val renderpass: VulkanRenderpass,
override fun close() {
val removedLayouts = ArrayList<Long>()

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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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 ->
Expand Down

0 comments on commit 387baee

Please sign in to comment.