Skip to content

Commit

Permalink
vulkan swapchain acquire semaphore fix #802
Browse files Browse the repository at this point in the history
  • Loading branch information
turanszkij committed Feb 15, 2024
1 parent 865a204 commit 8d89e30
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
27 changes: 18 additions & 9 deletions WickedEngine/wiGraphicsDevice_Vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,8 @@ namespace vulkan_internal
VkSurfaceKHR surface = VK_NULL_HANDLE;

uint32_t swapChainImageIndex = 0;
VkSemaphore swapchainAcquireSemaphore = VK_NULL_HANDLE;
uint32_t swapChainAcquireSemaphoreIndex = 0;
wi::vector<VkSemaphore> swapchainAcquireSemaphores;
VkSemaphore swapchainReleaseSemaphore = VK_NULL_HANDLE;

ColorSpace colorSpace = ColorSpace::SRGB;
Expand All @@ -1004,6 +1005,7 @@ namespace vulkan_internal
for (size_t i = 0; i < swapChainImages.size(); ++i)
{
allocationhandler->destroyer_imageviews.push_back(std::make_pair(swapChainImageViews[i], framecount));
allocationhandler->destroyer_semaphores.push_back(std::make_pair(swapchainAcquireSemaphores[i], framecount));
}

#ifdef SDL2
Expand All @@ -1015,7 +1017,6 @@ namespace vulkan_internal
allocationhandler->destroyer_swapchains.push_back(std::make_pair(swapChain, framecount));
allocationhandler->destroyer_surfaces.push_back(std::make_pair(surface, framecount));
}
allocationhandler->destroyer_semaphores.push_back(std::make_pair(swapchainAcquireSemaphore, framecount));
allocationhandler->destroyer_semaphores.push_back(std::make_pair(swapchainReleaseSemaphore, framecount));

allocationhandler->destroylocker.unlock();
Expand Down Expand Up @@ -1279,10 +1280,13 @@ namespace vulkan_internal
VkSemaphoreCreateInfo semaphoreInfo = {};
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;

if (internal_state->swapchainAcquireSemaphore == VK_NULL_HANDLE)
if (internal_state->swapchainAcquireSemaphores.empty())
{
res = vkCreateSemaphore(device, &semaphoreInfo, nullptr, &internal_state->swapchainAcquireSemaphore);
assert(res == VK_SUCCESS);
for (size_t i = 0; i < internal_state->swapChainImages.size(); ++i)
{
res = vkCreateSemaphore(device, &semaphoreInfo, nullptr, &internal_state->swapchainAcquireSemaphores.emplace_back());
assert(res == VK_SUCCESS);
}
}

if (internal_state->swapchainReleaseSemaphore == VK_NULL_HANDLE)
Expand Down Expand Up @@ -7053,7 +7057,7 @@ using namespace vulkan_internal;

VkSemaphoreSubmitInfo& waitSemaphore = queue.submit_waitSemaphoreInfos.emplace_back();
waitSemaphore.sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO;
waitSemaphore.semaphore = internal_state->swapchainAcquireSemaphore;
waitSemaphore.semaphore = internal_state->swapchainAcquireSemaphores[internal_state->swapChainAcquireSemaphoreIndex];
waitSemaphore.value = 0; // not a timeline semaphore
waitSemaphore.stageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT;

Expand Down Expand Up @@ -7447,12 +7451,14 @@ using namespace vulkan_internal;
commandlist.renderpass_barriers_end.clear();
auto internal_state = to_internal(swapchain);

internal_state->swapChainAcquireSemaphoreIndex = (internal_state->swapChainAcquireSemaphoreIndex + 1) % internal_state->swapchainAcquireSemaphores.size();

internal_state->locker.lock();
VkResult res = vkAcquireNextImageKHR(
device,
internal_state->swapChain,
UINT64_MAX,
internal_state->swapchainAcquireSemaphore,
internal_state->swapchainAcquireSemaphores[internal_state->swapChainAcquireSemaphoreIndex],
VK_NULL_HANDLE,
&internal_state->swapChainImageIndex
);
Expand All @@ -7470,9 +7476,12 @@ using namespace vulkan_internal;
// https://www.khronos.org/blog/resolving-longstanding-issues-with-wsi
{
std::scoped_lock lock(allocationhandler->destroylocker);
allocationhandler->destroyer_semaphores.emplace_back(internal_state->swapchainAcquireSemaphore, allocationhandler->framecount);
for (auto& x : internal_state->swapchainAcquireSemaphores)
{
allocationhandler->destroyer_semaphores.emplace_back(x, allocationhandler->framecount);
}
}
internal_state->swapchainAcquireSemaphore = VK_NULL_HANDLE;
internal_state->swapchainAcquireSemaphores.clear();
if (CreateSwapChainInternal(internal_state, physicalDevice, device, allocationhandler))
{
RenderPassBegin(swapchain, cmd);
Expand Down
16 changes: 13 additions & 3 deletions WickedEngine/wiRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13648,9 +13648,19 @@ void Postprocess_RTShadow(
device->EventEnd(cmd);
}

device->Barrier(GPUBarrier::Image(&output, output.desc.layout, ResourceState::COPY_DST), cmd);
device->CopyResource(&output, &res.temporal[temporal_output], cmd);
device->Barrier(GPUBarrier::Image(&output, ResourceState::COPY_DST, output.desc.layout), cmd);
{
GPUBarrier barriers[] = {
GPUBarrier::Image(&output, output.desc.layout, ResourceState::COPY_DST),
GPUBarrier::Image(&res.temporal[temporal_output], output.desc.layout, ResourceState::COPY_SRC),
};
device->Barrier(barriers, arraysize(barriers), cmd);
device->CopyResource(&output, &res.temporal[temporal_output], cmd);
for (auto& x : barriers)
{
std::swap(x.image.layout_before, x.image.layout_after);
}
device->Barrier(barriers, arraysize(barriers), cmd);
}

wi::profiler::EndRange(prof_range);
device->EventEnd(cmd);
Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace wi::version
// minor features, major updates, breaking compatibility changes
const int minor = 71;
// minor bug fixes, alterations, refactors, updates
const int revision = 378;
const int revision = 379;

const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);

Expand Down

0 comments on commit 8d89e30

Please sign in to comment.