Skip to content

Commit 387baee

Browse files
SwingSwapchain/VulkanPipeline/VulkanRenderpass/VulkanSwapchain: Fix incomplete cleanup
1 parent 77028d6 commit 387baee

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

src/main/kotlin/graphics/scenery/backends/vulkan/SwingSwapchain.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.lwjgl.system.MemoryUtil.memFree
1111
import org.lwjgl.system.Platform
1212
import org.lwjgl.vulkan.KHRSurface
1313
import org.lwjgl.vulkan.KHRSwapchain
14+
import org.lwjgl.vulkan.VK10.vkQueueWaitIdle
1415
import org.lwjgl.vulkan.VkQueue
1516
import org.lwjgl.vulkan.awt.AWTVK
1617
import java.awt.BorderLayout
@@ -69,8 +70,6 @@ open class SwingSwapchain(override val device: VulkanDevice,
6970
frame.addWindowListener(object : WindowAdapter() {
7071
override fun windowClosing(e: WindowEvent?) {
7172
super.windowClosing(e)
72-
73-
KHRSurface.vkDestroySurfaceKHR(device.instance, surface, null)
7473
}
7574

7675
})
@@ -145,8 +144,12 @@ open class SwingSwapchain(override val device: VulkanDevice,
145144
* Closes the swapchain, deallocating all of its resources.
146145
*/
147146
override fun close() {
148-
logger.debug("Closing swapchain {}", this)
147+
vkQueueWaitIdle(presentQueue)
148+
149+
closeSyncPrimitives()
150+
logger.debug("Closing swapchain {} with handle {}", this, handle.toHexString())
149151
KHRSwapchain.vkDestroySwapchainKHR(device.vulkanDevice, handle, null)
152+
KHRSurface.vkDestroySurfaceKHR(device.instance, surface, null)
150153

151154
(sceneryPanel as? SceneryJPanel)?.remove(0)
152155
presentInfo.free()

src/main/kotlin/graphics/scenery/backends/vulkan/VulkanPipeline.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,21 @@ class VulkanPipeline(val device: VulkanDevice, val renderpass: VulkanRenderpass,
286286
override fun close() {
287287
val removedLayouts = ArrayList<Long>()
288288

289-
pipeline.forEach { _, pipeline ->
290-
vkDestroyPipeline(device.vulkanDevice, pipeline.pipeline, null)
289+
pipeline.forEach { (_, pipeline) ->
290+
if(pipeline.pipeline != 0L) {
291+
vkDestroyPipeline(device.vulkanDevice, pipeline.pipeline, null)
292+
pipeline.pipeline = 0L
293+
}
291294

292-
if(!removedLayouts.contains(pipeline.layout)) {
293-
vkDestroyPipelineLayout(device.vulkanDevice, pipeline.layout, null)
295+
if(!removedLayouts.contains(pipeline.layout) && pipeline.layout != 0L) {
294296
removedLayouts.add(pipeline.layout)
297+
vkDestroyPipelineLayout(device.vulkanDevice, pipeline.layout, null)
298+
pipeline.layout = 0L
295299
}
296300
}
297301

302+
pipeline.clear()
303+
298304
inputAssemblyState.free()
299305
rasterizationState.free()
300306
depthStencilState.free()

src/main/kotlin/graphics/scenery/backends/vulkan/VulkanRenderpass.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ open class VulkanRenderpass(val name: String, var config: RenderConfigReader.Ren
844844
logger.debug("Closing renderpass $name...")
845845
output.forEach { it.value.close() }
846846
configuredPipelines.forEach { it.value.close() }
847+
pipelines.forEach { (_, pipeline) -> pipeline.close() }
847848
pipelines.clear()
848849
UBOs.forEach { it.value.close() }
849850
ownDescriptorSetLayouts.forEach {

src/main/kotlin/graphics/scenery/backends/vulkan/VulkanSwapchain.kt

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -585,10 +585,10 @@ open class VulkanSwapchain(open val device: VulkanDevice,
585585
imageRenderedSemaphores.forEach { device.removeSemaphore(it) }
586586
imageRenderedSemaphores.clear()
587587

588-
fences.forEach { VK10.vkDestroyFence(device.vulkanDevice, it, null) }
588+
fences.forEach { vkDestroyFence(device.vulkanDevice, it, null) }
589589
fences.clear()
590590

591-
imageUseFences.forEach { VK10.vkDestroyFence(device.vulkanDevice, it, null) }
591+
imageUseFences.forEach { vkDestroyFence(device.vulkanDevice, it, null) }
592592
imageUseFences.clear()
593593
}
594594

@@ -599,24 +599,15 @@ open class VulkanSwapchain(open val device: VulkanDevice,
599599
vkQueueWaitIdle(presentQueue)
600600
vkQueueWaitIdle(queue)
601601

602-
logger.debug("Closing swapchain $this")
602+
logger.debug("Closing swapchain {}", this)
603603
KHRSwapchain.vkDestroySwapchainKHR(device.vulkanDevice, handle, null)
604604
vkDestroySurfaceKHR(device.instance, surface, null)
605605

606-
imageAvailableSemaphores.forEach { device.removeSemaphore(it) }
607-
imageAvailableSemaphores.clear()
608-
imageRenderedSemaphores.forEach { device.removeSemaphore(it) }
609-
imageRenderedSemaphores.clear()
610-
611-
fences.forEach { vkDestroyFence(device.vulkanDevice, it, null) }
612-
fences.clear()
613-
614-
imageUseFences.forEach { vkDestroyFence(device.vulkanDevice, it, null) }
615-
imageUseFences.clear()
606+
closeSyncPrimitives()
616607

617608
presentInfo.free()
618-
MemoryUtil.memFree(swapchainImage)
619-
MemoryUtil.memFree(swapchainPointer)
609+
memFree(swapchainImage)
610+
memFree(swapchainPointer)
620611

621612
windowSizeCallback.close()
622613
(window as SceneryWindow.GLFWWindow?)?.let { window ->

0 commit comments

Comments
 (0)