Преглед изворни кода

vulkan: properly destroy semaphores and pipelines when GSG closes

rdb пре 7 година
родитељ
комит
41e819f341

+ 26 - 0
panda/src/vulkandisplay/vulkanGraphicsStateGuardian.cxx

@@ -307,6 +307,16 @@ VulkanGraphicsStateGuardian(GraphicsEngine *engine, VulkanGraphicsPipe *pipe,
  */
 VulkanGraphicsStateGuardian::
 ~VulkanGraphicsStateGuardian() {
+  // Remove all the pipeline states.
+  for (const auto &item : _pipeline_map) {
+    vkDestroyPipeline(_device, item.second, nullptr);
+  }
+
+  // And all the semaphores that were generated on this device.
+  for (VkSemaphore semaphore : _semaphores) {
+    vkDestroySemaphore(_device, semaphore, nullptr);
+  }
+
   // Remove the things we created in the constructor, in reverse order.
   vkDestroyBuffer(_device, _color_vertex_buffer, nullptr);
   vkDestroyDescriptorPool(_device, _descriptor_pool, nullptr);
@@ -2271,6 +2281,22 @@ create_buffer(VkDeviceSize size, VkBuffer &buffer, VkDeviceMemory &memory,
   return true;
 }
 
+/**
+ * Creates a new semaphore on this device.
+ */
+VkSemaphore VulkanGraphicsStateGuardian::
+create_semaphore() {
+  VkSemaphoreCreateInfo semaphore_info = {};
+  semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
+
+  VkSemaphore semaphore;
+  VkResult
+  err = vkCreateSemaphore(_device, &semaphore_info, nullptr, &semaphore);
+  nassertr_always(err == VK_SUCCESS, VK_NULL_HANDLE);
+  _semaphores.push_back(semaphore);
+  return semaphore;
+}
+
 /**
  * Returns a VkPipeline for the given RenderState+GeomVertexFormat combination.
  */

+ 5 - 0
panda/src/vulkandisplay/vulkanGraphicsStateGuardian.h

@@ -127,6 +127,8 @@ private:
   bool create_buffer(VkDeviceSize size, VkBuffer &buffer, VkDeviceMemory &memory,
                      int usage_flags, VkMemoryPropertyFlagBits flags);
 
+  VkSemaphore create_semaphore();
+
   /**
    * Stores whatever is used to key a cached pipeline into the pipeline map.
    * This allows us to map Panda states to Vulkan pipelines effectively.
@@ -203,6 +205,9 @@ private:
   VkSemaphore _wait_semaphore;
   VkSemaphore _signal_semaphore;
 
+  // Remembers semaphores created on this device.
+  pvector<VkSemaphore> _semaphores;
+
   // Palette for flat colors.
   VkBuffer _color_vertex_buffer;
   int _next_palette_index;

+ 4 - 9
panda/src/vulkandisplay/vulkanGraphicsWindow.cxx

@@ -993,18 +993,13 @@ create_swapchain() {
   // Create a semaphore for signalling the availability of an image.
   // It will be signalled in end_flip() and waited upon before submitting the
   // command buffers that use that image for rendering to.
-  VkSemaphoreCreateInfo semaphore_info = {};
-  semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
-
-  err = vkCreateSemaphore(vkgsg->_device, &semaphore_info,
-                          nullptr, &_image_available);
-  nassertr(err == VK_SUCCESS, false);
+  _image_available = vkgsg->create_semaphore();
+  nassertr(_image_available != VK_NULL_HANDLE, false);
 
   // Now create another one that is signalled when we are finished rendering,
   // to indicate that it is safe to present the image.
-  err = vkCreateSemaphore(vkgsg->_device, &semaphore_info,
-                          nullptr, &_render_complete);
-  nassertr(err == VK_SUCCESS, false);
+  _render_complete = vkgsg->create_semaphore();
+  nassertr(_render_complete != VK_NULL_HANDLE, false);
 
   // We need to acquire an image before we continue rendering.
   _image_index = 0;