forked from Dynamitos/Seele
Fixing semaphore waiting logic
This commit is contained in:
1 parent
bcb9a7b148
commit
6a6294c67d
4 files changed
+36
-15
No files matched your search
@@ -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<Gfx::OComputeCommand> 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;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ class Command {
|
||||
VkCommandBuffer handle;
|
||||
PRenderPass boundRenderPass;
|
||||
PFramebuffer boundFramebuffer;
|
||||
Array<PSemaphore> waitSemaphores;
|
||||
Array<VkSemaphore> waitHandles;
|
||||
Array<VkPipelineStageFlags> waitFlags;
|
||||
Array<ORenderCommand> executingRenders;
|
||||
Array<OComputeCommand> executingComputes;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "Command.h"
|
||||
#include "Enums.h"
|
||||
#include "Graphics.h"
|
||||
#include "Graphics/Vulkan/Resources.h"
|
||||
#include <mutex>
|
||||
|
||||
using namespace Seele;
|
||||
@@ -25,16 +26,23 @@ void Queue::submitCommandBuffer(PCommand command, const Array<VkSemaphore>& sign
|
||||
VkCommandBuffer cmdHandle = command->handle;
|
||||
|
||||
Array<VkSemaphore> 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<uint32>(command->waitSemaphores.size()),
|
||||
.waitSemaphoreCount = static_cast<uint32>(command->waitHandles.size()),
|
||||
.pWaitSemaphores = waitSemaphores.data(),
|
||||
.pWaitDstStageMask = command->waitFlags.data(),
|
||||
.commandBufferCount = 1,
|
||||
@@ -46,7 +54,7 @@ void Queue::submitCommandBuffer(PCommand command, const Array<VkSemaphore>& 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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user