From 3689390c56a5085b30eb390e95932f0b0d9c412c Mon Sep 17 00:00:00 2001 From: PancakeTAS Date: Thu, 3 Jul 2025 09:19:22 +0200 Subject: [PATCH] fix semaphore deletion --- lsfg-vk-gen/include/context.hpp | 2 ++ lsfg-vk-gen/src/context.cpp | 18 +++++++++++++++--- lsfg-vk-gen/src/lsfg.cpp | 6 ++++-- src/context.cpp | 12 +++++++++--- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lsfg-vk-gen/include/context.hpp b/lsfg-vk-gen/include/context.hpp index 41fa127..e29d07f 100644 --- a/lsfg-vk-gen/include/context.hpp +++ b/lsfg-vk-gen/include/context.hpp @@ -4,6 +4,7 @@ #include "core/commandbuffer.hpp" #include "core/commandpool.hpp" #include "core/descriptorpool.hpp" +#include "core/fence.hpp" #include "core/image.hpp" #include "core/semaphore.hpp" #include "shaderchains/alpha.hpp" @@ -67,6 +68,7 @@ namespace LSFG { std::vector> outSemaphores; std::array cmdBuffers1; std::vector> cmdBuffers2; + std::array>, 8> doneFences; uint64_t fc{0}; Shaderchains::Downsample downsampleChain; diff --git a/lsfg-vk-gen/src/context.cpp b/lsfg-vk-gen/src/context.cpp index 6e26566..db11e72 100644 --- a/lsfg-vk-gen/src/context.cpp +++ b/lsfg-vk-gen/src/context.cpp @@ -1,4 +1,5 @@ #include "context.hpp" +#include "core/fence.hpp" #include "core/semaphore.hpp" #include "lsfg.hpp" @@ -28,8 +29,10 @@ Context::Context(const Core::Device& device, uint32_t width, uint32_t height, in this->cmdPool = Core::CommandPool(device); // prepare vectors - for (size_t i = 0; i < 8; i++) + for (size_t i = 0; i < 8; i++) { this->internalSemaphores.at(i).resize(outN.size()); + this->doneFences.at(i).resize(outN.size()); + } for (size_t i = 0; i < outN.size(); i++) { this->outSemaphores.emplace_back(); this->cmdBuffers2.emplace_back(); @@ -114,6 +117,14 @@ Context::Context(const Core::Device& device, uint32_t width, uint32_t height, in void Context::present(const Core::Device& device, int inSem, const std::vector& outSem) { + auto& doneFences = this->doneFences.at(this->fc % 8); + for (auto& fenceOptional : doneFences) { + if (!fenceOptional.has_value()) + continue; + if (!fenceOptional->wait(device, UINT64_MAX)) + throw vulkan_error(VK_ERROR_DEVICE_LOST, "Fence wait timed out"); + } + auto& inSemaphore = this->inSemaphores.at(this->fc % 8); inSemaphore = Core::Semaphore(device, inSem); auto& internalSemaphores = this->internalSemaphores.at(this->fc % 8); @@ -134,10 +145,11 @@ void Context::present(const Core::Device& device, int inSem, { inSemaphore }, std::nullopt, internalSemaphores, std::nullopt); - for (size_t pass = 0; pass < outSem.size(); pass++) { auto& outSemaphore = this->outSemaphores.at(pass).at(this->fc % 8); outSemaphore = Core::Semaphore(device, outSem.at(pass)); + auto& outFenceOptional = this->doneFences.at(fc % 8).at(pass); + outFenceOptional.emplace(Core::Fence(device)); auto& cmdBuffer2 = this->cmdBuffers2.at(pass).at(this->fc % 8); cmdBuffer2 = Core::CommandBuffer(device, this->cmdPool); @@ -158,7 +170,7 @@ void Context::present(const Core::Device& device, int inSem, cmdBuffer2.end(); - cmdBuffer2.submit(device.getComputeQueue(), std::nullopt, + cmdBuffer2.submit(device.getComputeQueue(), outFenceOptional, { internalSemaphores.at(pass) }, std::nullopt, { outSemaphore }, std::nullopt); } diff --git a/lsfg-vk-gen/src/lsfg.cpp b/lsfg-vk-gen/src/lsfg.cpp index 7519d5a..816c4ff 100644 --- a/lsfg-vk-gen/src/lsfg.cpp +++ b/lsfg-vk-gen/src/lsfg.cpp @@ -58,15 +58,17 @@ void LSFG::deleteContext(int32_t id) { if (it == contexts.end()) throw LSFG::vulkan_error(VK_ERROR_DEVICE_LOST, "No such context"); + vkDeviceWaitIdle(device->handle()); contexts.erase(it); } void LSFG::finalize() { - if (!instance.has_value() && !device.has_value()) + if (!instance.has_value() || !device.has_value()) return; Globals::uninitializeGlobals(); - instance.reset(); + vkDeviceWaitIdle(device->handle()); device.reset(); + instance.reset(); } diff --git a/src/context.cpp b/src/context.cpp index e5c0412..56ca13b 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -63,7 +63,7 @@ VkResult LsContext::present(const Hooks::DeviceInfo& info, const void* pNext, Vk // 1. copy swapchain image to frame_0/frame_1 int preCopySemaphoreFd{}; pass.preCopySemaphores.at(0) = Mini::Semaphore(info.device, &preCopySemaphoreFd); - // pass.preCopySemaphores.at(1) = Mini::Semaphore(info.device); + pass.preCopySemaphores.at(1) = Mini::Semaphore(info.device); pass.preCopyBuf = Mini::CommandBuffer(info.device, this->cmdPool); pass.preCopyBuf.begin(); @@ -75,9 +75,15 @@ VkResult LsContext::present(const Hooks::DeviceInfo& info, const void* pNext, Vk true, false); pass.preCopyBuf.end(); + + std::vector gameRenderSemaphores2 = gameRenderSemaphores; + if (this->frameIdx > 0) + gameRenderSemaphores2.emplace_back(this->passInfos.at((this->frameIdx - 1) % 8) + .preCopySemaphores.at(1).handle()); pass.preCopyBuf.submit(info.queue.second, - gameRenderSemaphores, - { pass.preCopySemaphores.at(0).handle() }); + gameRenderSemaphores2, + { pass.preCopySemaphores.at(0).handle(), + pass.preCopySemaphores.at(1).handle() }); // 2. render intermediary frames std::vector renderSemaphoreFds(info.frameGen);