From 29a30037cb593ab5f5190e2ee2de9aab0d6b0d32 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Sun, 17 Mar 2019 20:36:08 +0100 Subject: [PATCH] - delay destruction of VkHardwareTexture resources until current frame has finished rendering --- src/rendering/vulkan/system/vk_buffers.cpp | 2 +- src/rendering/vulkan/system/vk_framebuffer.cpp | 14 ++++++++++++-- src/rendering/vulkan/system/vk_framebuffer.h | 10 +++++++++- src/rendering/vulkan/textures/vk_hwtexture.cpp | 12 ++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/rendering/vulkan/system/vk_buffers.cpp b/src/rendering/vulkan/system/vk_buffers.cpp index e794c0714..57086017a 100644 --- a/src/rendering/vulkan/system/vk_buffers.cpp +++ b/src/rendering/vulkan/system/vk_buffers.cpp @@ -13,7 +13,7 @@ VKBuffer::~VKBuffer() auto fb = GetVulkanFrameBuffer(); if (fb && mBuffer) - fb->mFrameDeleteList.push_back(std::move(mBuffer)); + fb->FrameDeleteList.Buffers.push_back(std::move(mBuffer)); } void VKBuffer::SetData(size_t size, const void *data, bool staticdata) diff --git a/src/rendering/vulkan/system/vk_framebuffer.cpp b/src/rendering/vulkan/system/vk_framebuffer.cpp index 1103ffdbc..20d538e52 100644 --- a/src/rendering/vulkan/system/vk_framebuffer.cpp +++ b/src/rendering/vulkan/system/vk_framebuffer.cpp @@ -99,6 +99,8 @@ VulkanFrameBuffer::~VulkanFrameBuffer() delete mViewpoints; delete mLights; mShadowMap.Reset(); + + DeleteFrameObjects(); } void VulkanFrameBuffer::InitializeState() @@ -205,13 +207,21 @@ void VulkanFrameBuffer::Update() mDrawCommands.reset(); mUploadCommands.reset(); - mFrameDeleteList.clear(); + DeleteFrameObjects(); Finish.Unclock(); Super::Update(); } +void VulkanFrameBuffer::DeleteFrameObjects() +{ + FrameDeleteList.Images.clear(); + FrameDeleteList.ImageViews.clear(); + FrameDeleteList.Buffers.clear(); + FrameDeleteList.Descriptors.clear(); +} + void VulkanFrameBuffer::SubmitCommands(bool finish) { mDrawCommands->end(); @@ -270,7 +280,7 @@ void VulkanFrameBuffer::SubmitCommands(bool finish) vkResetFences(device->device, 1, &mRenderFinishedFence->fence); mDrawCommands.reset(); mUploadCommands.reset(); - mFrameDeleteList.clear(); + DeleteFrameObjects(); } } diff --git a/src/rendering/vulkan/system/vk_framebuffer.h b/src/rendering/vulkan/system/vk_framebuffer.h index fde969d2e..770bcf376 100644 --- a/src/rendering/vulkan/system/vk_framebuffer.h +++ b/src/rendering/vulkan/system/vk_framebuffer.h @@ -50,7 +50,14 @@ public: std::unique_ptr FanToTrisIndexBuffer; - std::vector> mFrameDeleteList; + class DeleteList + { + public: + std::vector> Images; + std::vector> ImageViews; + std::vector> Buffers; + std::vector> Descriptors; + } FrameDeleteList; std::unique_ptr swdrawer; @@ -95,6 +102,7 @@ private: void SubmitCommands(bool finish); void CopyScreenToBuffer(int w, int h, void *data); void UpdateShadowMap(); + void DeleteFrameObjects(); std::unique_ptr mShaderManager; std::unique_ptr mSamplerManager; diff --git a/src/rendering/vulkan/textures/vk_hwtexture.cpp b/src/rendering/vulkan/textures/vk_hwtexture.cpp index 8ecdcbdc6..997a2bf35 100644 --- a/src/rendering/vulkan/textures/vk_hwtexture.cpp +++ b/src/rendering/vulkan/textures/vk_hwtexture.cpp @@ -52,6 +52,18 @@ VkHardwareTexture::~VkHardwareTexture() if (Next) Next->Prev = Prev; if (Prev) Prev->Next = Next; else First = Next; + + auto fb = GetVulkanFrameBuffer(); + if (fb) + { + auto &deleteList = fb->FrameDeleteList; + for (auto &it : mDescriptorSets) + deleteList.Descriptors.push_back(std::move(it.second)); + mDescriptorSets.clear(); + if (mImage) deleteList.Images.push_back(std::move(mImage)); + if (mImageView) deleteList.ImageViews.push_back(std::move(mImageView)); + if (mStagingBuffer) deleteList.Buffers.push_back(std::move(mStagingBuffer)); + } } void VkHardwareTexture::Reset()