diff --git a/src/Engine/Graphics/Vulkan/Command.cpp b/src/Engine/Graphics/Vulkan/Command.cpp index 34f863e..e0c6416 100644 --- a/src/Engine/Graphics/Vulkan/Command.cpp +++ b/src/Engine/Graphics/Vulkan/Command.cpp @@ -29,7 +29,8 @@ Command::Command(PGraphics graphics, PCommandPool pool) : graphics(graphics), po Command::~Command() { vkFreeCommandBuffers(graphics->getDevice(), pool->getHandle(), 1, &handle); - waitSemaphores.clear(); + waitHandles.clear(); + waitFlags.clear(); } void Command::begin() { @@ -114,8 +115,13 @@ void Command::executeCommands(Array commands) { } void Command::waitForSemaphore(VkPipelineStageFlags flags, PSemaphore semaphore) { - bindResource(semaphore->getCurrentSemaphore()); - waitSemaphores.add(semaphore); + // Capture the concrete VkSemaphore NOW. The wrapper's current handle may + // rotate before submit; waiting on a re-resolved handle at submit time can + // target a semaphore that is also signalled in this same submit (or one with + // no outstanding signal), violating VUID-vkQueueSubmit-pWaitSemaphores-03238. + PSemaphoreHandle current = semaphore->getCurrentSemaphore(); + bindResource(current); + waitHandles.add(current->getHandle()); waitFlags.add(flags); } @@ -137,6 +143,13 @@ void Command::checkFence() { } signalSemaphore->resolveSignal(); boundResources.clear(); + // Clear accumulated wait state so a recycled command buffer does not + // re-wait on stale (already-consumed) semaphores from a prior frame. + // A binary semaphore's signal->wait pair is single-use; waiting on a + // handle whose signal was already consumed violates + // VUID-vkQueueSubmit-pWaitSemaphores-03238. + waitHandles.clear(); + waitFlags.clear(); graphics->getDestructionManager()->notifyCommandComplete(); state = State::Init; } diff --git a/src/Engine/Graphics/Vulkan/Command.h b/src/Engine/Graphics/Vulkan/Command.h index 72c20d0..ed9e6b3 100644 --- a/src/Engine/Graphics/Vulkan/Command.h +++ b/src/Engine/Graphics/Vulkan/Command.h @@ -48,7 +48,7 @@ class Command { VkCommandBuffer handle; PRenderPass boundRenderPass; PFramebuffer boundFramebuffer; - Array waitSemaphores; + Array waitHandles; Array waitFlags; Array executingRenders; Array executingComputes; diff --git a/src/Engine/Graphics/Vulkan/Queue.cpp b/src/Engine/Graphics/Vulkan/Queue.cpp index 199b56e..802abb9 100644 --- a/src/Engine/Graphics/Vulkan/Queue.cpp +++ b/src/Engine/Graphics/Vulkan/Queue.cpp @@ -3,6 +3,7 @@ #include "Command.h" #include "Enums.h" #include "Graphics.h" +#include "Graphics/Vulkan/Resources.h" #include using namespace Seele; @@ -25,16 +26,23 @@ void Queue::submitCommandBuffer(PCommand command, const Array& sign VkCommandBuffer cmdHandle = command->handle; Array waitSemaphores; - // wait semaphores get bound to the cmd when they are added - // and unbound when the command completes - for (PSemaphore semaphore : command->waitSemaphores) { - waitSemaphores.add(semaphore->getHandle()); + // Use the handles captured at record time (command->waitHandles). Re-resolving + // the wrapper's current handle here would let the wait target drift if the + // wrapper rotated between record and submit, potentially onto a semaphore that + // is also signalled in this same submit. + for (VkSemaphore waitHandle : command->waitHandles) { + waitSemaphores.add(waitHandle); + std::cout << "Semaphore " << waitHandle << " waited on by cmd " << command->getHandle() << std::endl; + } + + for (VkSemaphore signalSemaphore : signalSemaphores) { + std::cout << "Semaphore " << signalSemaphore << " is signalled by cmd " << command->getHandle() << std::endl; } VkSubmitInfo submitInfo = { .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .pNext = nullptr, - .waitSemaphoreCount = static_cast(command->waitSemaphores.size()), + .waitSemaphoreCount = static_cast(command->waitHandles.size()), .pWaitSemaphores = waitSemaphores.data(), .pWaitDstStageMask = command->waitFlags.data(), .commandBufferCount = 1, @@ -46,7 +54,7 @@ void Queue::submitCommandBuffer(PCommand command, const Array& sign command->fence->submit(); command->state = Command::State::Submit; command->waitFlags.clear(); - command->waitSemaphores.clear(); + command->waitHandles.clear(); if (waitIdleOnSubmit) { command->fence->wait(1000 * 1000ull); diff --git a/src/Engine/Graphics/Vulkan/Window.cpp b/src/Engine/Graphics/Vulkan/Window.cpp index 521d6f9..3d3708e 100644 --- a/src/Engine/Graphics/Vulkan/Window.cpp +++ b/src/Engine/Graphics/Vulkan/Window.cpp @@ -206,7 +206,7 @@ void Seele::Vulkan::Window::beginFrame() { // The image was reacquired, so the previous presentation using this image's // renderDone semaphore has completed and it can be safely reused renderingDoneSemaphores[currentImageIndex]->resolveSignal(); - graphics->getGraphicsCommands()->getCommands()->waitForSemaphore(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, + graphics->getGraphicsCommands()->getCommands()->waitForSemaphore(VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, imageAvailableSemaphores[currentSemaphoreIndex]); static double start = glfwGetTime(); double end = glfwGetTime(); @@ -222,6 +222,7 @@ void Seele::Vulkan::Window::endFrame() { renderingDoneSemaphores[currentImageIndex]->rotateSemaphore(); graphics->getGraphicsCommands()->submitCommands(renderingDoneSemaphores[currentImageIndex]); VkSemaphore renderDoneHandle = renderingDoneSemaphores[currentImageIndex]->getHandle(); + VkResult swapChainResult; VkPresentInfoKHR presentInfo = { .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, .pNext = nullptr, @@ -230,16 +231,15 @@ void Seele::Vulkan::Window::endFrame() { .swapchainCount = 1, .pSwapchains = &swapchain, .pImageIndices = ¤tImageIndex, - .pResults = nullptr, + .pResults = &swapChainResult, }; VkResult r = vkQueuePresentKHR(graphics->getGraphicsCommands()->getQueue()->getHandle(), &presentInfo); - if (r == VK_SUCCESS) { - } else if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) { + std::cout << "Semaphore " << renderDoneHandle << " signals present for image index " << currentImageIndex << std::endl; + if (swapChainResult == VK_ERROR_OUT_OF_DATE_KHR || swapChainResult == VK_SUBOPTIMAL_KHR || r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) { createSwapChain(); } else { VK_CHECK(r); } - renderingDoneSemaphores[currentImageIndex]->encodeSignal(); currentSemaphoreIndex = (currentSemaphoreIndex + 1) % Gfx::numFramesBuffered; currentFrameIndex = currentSemaphoreIndex; // graphics->waitDeviceIdle();