Fixing semaphore waiting logic

This commit is contained in:
Dynamitos committed 2026-10-25 19:18:57 +01:00
1 parent bcb9a7b148
commit 6a6294c67d
4 files changed
+36 -15

No files matched your search

+16 -3
View File
@@ -29,7 +29,8 @@ Command::Command(PGraphics graphics, PCommandPool pool) : graphics(graphics), po
Command::~Command() { Command::~Command() {
vkFreeCommandBuffers(graphics->getDevice(), pool->getHandle(), 1, &handle); vkFreeCommandBuffers(graphics->getDevice(), pool->getHandle(), 1, &handle);
waitSemaphores.clear(); waitHandles.clear();
waitFlags.clear();
} }
void Command::begin() { void Command::begin() {
@@ -114,8 +115,13 @@ void Command::executeCommands(Array<Gfx::OComputeCommand> commands) {
} }
void Command::waitForSemaphore(VkPipelineStageFlags flags, PSemaphore semaphore) { void Command::waitForSemaphore(VkPipelineStageFlags flags, PSemaphore semaphore) {
bindResource(semaphore->getCurrentSemaphore()); // Capture the concrete VkSemaphore NOW. The wrapper's current handle may
waitSemaphores.add(semaphore); // 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); waitFlags.add(flags);
} }
@@ -137,6 +143,13 @@ void Command::checkFence() {
} }
signalSemaphore->resolveSignal(); signalSemaphore->resolveSignal();
boundResources.clear(); 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(); graphics->getDestructionManager()->notifyCommandComplete();
state = State::Init; state = State::Init;
} }
+1 -1
View File
@@ -48,7 +48,7 @@ class Command {
VkCommandBuffer handle; VkCommandBuffer handle;
PRenderPass boundRenderPass; PRenderPass boundRenderPass;
PFramebuffer boundFramebuffer; PFramebuffer boundFramebuffer;
Array<PSemaphore> waitSemaphores; Array<VkSemaphore> waitHandles;
Array<VkPipelineStageFlags> waitFlags; Array<VkPipelineStageFlags> waitFlags;
Array<ORenderCommand> executingRenders; Array<ORenderCommand> executingRenders;
Array<OComputeCommand> executingComputes; Array<OComputeCommand> executingComputes;
+14 -6
View File
@@ -3,6 +3,7 @@
#include "Command.h" #include "Command.h"
#include "Enums.h" #include "Enums.h"
#include "Graphics.h" #include "Graphics.h"
#include "Graphics/Vulkan/Resources.h"
#include <mutex> #include <mutex>
using namespace Seele; using namespace Seele;
@@ -25,16 +26,23 @@ void Queue::submitCommandBuffer(PCommand command, const Array<VkSemaphore>& sign
VkCommandBuffer cmdHandle = command->handle; VkCommandBuffer cmdHandle = command->handle;
Array<VkSemaphore> waitSemaphores; Array<VkSemaphore> waitSemaphores;
// wait semaphores get bound to the cmd when they are added // Use the handles captured at record time (command->waitHandles). Re-resolving
// and unbound when the command completes // the wrapper's current handle here would let the wait target drift if the
for (PSemaphore semaphore : command->waitSemaphores) { // wrapper rotated between record and submit, potentially onto a semaphore that
waitSemaphores.add(semaphore->getHandle()); // 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 = { VkSubmitInfo submitInfo = {
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
.pNext = nullptr, .pNext = nullptr,
.waitSemaphoreCount = static_cast<uint32>(command->waitSemaphores.size()), .waitSemaphoreCount = static_cast<uint32>(command->waitHandles.size()),
.pWaitSemaphores = waitSemaphores.data(), .pWaitSemaphores = waitSemaphores.data(),
.pWaitDstStageMask = command->waitFlags.data(), .pWaitDstStageMask = command->waitFlags.data(),
.commandBufferCount = 1, .commandBufferCount = 1,
@@ -46,7 +54,7 @@ void Queue::submitCommandBuffer(PCommand command, const Array<VkSemaphore>& sign
command->fence->submit(); command->fence->submit();
command->state = Command::State::Submit; command->state = Command::State::Submit;
command->waitFlags.clear(); command->waitFlags.clear();
command->waitSemaphores.clear(); command->waitHandles.clear();
if (waitIdleOnSubmit) { if (waitIdleOnSubmit) {
command->fence->wait(1000 * 1000ull); command->fence->wait(1000 * 1000ull);
+5 -5
View File
@@ -206,7 +206,7 @@ void Seele::Vulkan::Window::beginFrame() {
// The image was reacquired, so the previous presentation using this image's // The image was reacquired, so the previous presentation using this image's
// renderDone semaphore has completed and it can be safely reused // renderDone semaphore has completed and it can be safely reused
renderingDoneSemaphores[currentImageIndex]->resolveSignal(); 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]); imageAvailableSemaphores[currentSemaphoreIndex]);
static double start = glfwGetTime(); static double start = glfwGetTime();
double end = glfwGetTime(); double end = glfwGetTime();
@@ -222,6 +222,7 @@ void Seele::Vulkan::Window::endFrame() {
renderingDoneSemaphores[currentImageIndex]->rotateSemaphore(); renderingDoneSemaphores[currentImageIndex]->rotateSemaphore();
graphics->getGraphicsCommands()->submitCommands(renderingDoneSemaphores[currentImageIndex]); graphics->getGraphicsCommands()->submitCommands(renderingDoneSemaphores[currentImageIndex]);
VkSemaphore renderDoneHandle = renderingDoneSemaphores[currentImageIndex]->getHandle(); VkSemaphore renderDoneHandle = renderingDoneSemaphores[currentImageIndex]->getHandle();
VkResult swapChainResult;
VkPresentInfoKHR presentInfo = { VkPresentInfoKHR presentInfo = {
.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
.pNext = nullptr, .pNext = nullptr,
@@ -230,16 +231,15 @@ void Seele::Vulkan::Window::endFrame() {
.swapchainCount = 1, .swapchainCount = 1,
.pSwapchains = &swapchain, .pSwapchains = &swapchain,
.pImageIndices = &currentImageIndex, .pImageIndices = &currentImageIndex,
.pResults = nullptr, .pResults = &swapChainResult,
}; };
VkResult r = vkQueuePresentKHR(graphics->getGraphicsCommands()->getQueue()->getHandle(), &presentInfo); VkResult r = vkQueuePresentKHR(graphics->getGraphicsCommands()->getQueue()->getHandle(), &presentInfo);
if (r == VK_SUCCESS) { std::cout << "Semaphore " << renderDoneHandle << " signals present for image index " << currentImageIndex << std::endl;
} else if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) { if (swapChainResult == VK_ERROR_OUT_OF_DATE_KHR || swapChainResult == VK_SUBOPTIMAL_KHR || r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) {
createSwapChain(); createSwapChain();
} else { } else {
VK_CHECK(r); VK_CHECK(r);
} }
renderingDoneSemaphores[currentImageIndex]->encodeSignal();
currentSemaphoreIndex = (currentSemaphoreIndex + 1) % Gfx::numFramesBuffered; currentSemaphoreIndex = (currentSemaphoreIndex + 1) % Gfx::numFramesBuffered;
currentFrameIndex = currentSemaphoreIndex; currentFrameIndex = currentSemaphoreIndex;
// graphics->waitDeviceIdle(); // graphics->waitDeviceIdle();