From 3f0bd4596ea36661ff05e950b665aff6b497c71f Mon Sep 17 00:00:00 2001 From: Philip Rideout Date: Mon, 25 Mar 2019 14:06:54 -0700 Subject: [PATCH] Add HwFence support to the Vulkan backend. There are two changes involved here: 1) Recreate (do not recycle) the submission fences at every frame. 2) Add shared ownership semantics to existing fences to allow DriverAPI clients to "own" fences. The first change was motivated by FrameSkipper, which might have a fence list that is longer than the number of swap chain contexts. The second change was implemented simply by creating a tiny wrapper struct for VkFence with a proper constructor and destructor, then leveraging shared_ptr. --- filament/backend/src/vulkan/VulkanContext.cpp | 50 ++++++++++--------- filament/backend/src/vulkan/VulkanContext.h | 15 +++++- filament/backend/src/vulkan/VulkanDriver.cpp | 26 +++++++--- filament/backend/src/vulkan/VulkanHandles.h | 5 ++ 4 files changed, 66 insertions(+), 30 deletions(-) diff --git a/filament/backend/src/vulkan/VulkanContext.cpp b/filament/backend/src/vulkan/VulkanContext.cpp index f5b3e263da..fbc4e8d79e 100644 --- a/filament/backend/src/vulkan/VulkanContext.cpp +++ b/filament/backend/src/vulkan/VulkanContext.cpp @@ -35,6 +35,18 @@ namespace filament { namespace driver { +VulkanCmdFence::VulkanCmdFence(VkDevice device, bool signaled) : device(device) { + VkFenceCreateInfo fenceCreateInfo { .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO }; + if (signaled) { + fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; + } + vkCreateFence(device, &fenceCreateInfo, VKALLOC, &fence); +} + + VulkanCmdFence::~VulkanCmdFence() { + vkDestroyFence(device, fence, VKALLOC); +} + void selectPhysicalDevice(VulkanContext& context) { uint32_t physicalDeviceCount = 0; VkResult result = vkEnumeratePhysicalDevices(context.instance, &physicalDeviceCount, nullptr); @@ -210,10 +222,8 @@ void createVirtualDevice(VulkanContext& context) { .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 1 }; - const VkFenceCreateInfo fenceCreateInfo { .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO }; const VkCommandBufferBeginInfo binfo { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO }; vkAllocateCommandBuffers(context.device, &allocateInfo, &context.work.cmdbuffer); - vkCreateFence(context.device, &fenceCreateInfo, VKALLOC, &context.work.fence); vkBeginCommandBuffer(context.work.cmdbuffer, &binfo); context.work.submitted = false; } @@ -374,16 +384,6 @@ void createSwapChain(VulkanContext& context, VulkanSurfaceContext& surfaceContex for (uint32_t i = 0; i < allocateInfo.commandBufferCount; ++i) { surfaceContext.swapContexts[i].commands.cmdbuffer = cmdbufs[i]; } - - // Create fences. - VkFenceCreateInfo fenceCreateInfo = {}; - fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; - fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; - for (uint32_t i = 0; i < allocateInfo.commandBufferCount; i++) { - result = vkCreateFence(context.device, &fenceCreateInfo, VKALLOC, - &surfaceContext.swapContexts[i].commands.fence); - ASSERT_POSTCONDITION(result == VK_SUCCESS, "vkCreateFence error."); - } } uint32_t selectMemoryType(VulkanContext& context, uint32_t flags, VkFlags reqs) { @@ -422,7 +422,7 @@ void waitForIdle(VulkanContext& context) { for (auto& swapContext : surfaceContext.swapContexts) { assert(nfences < 4); if (swapContext.commands.submitted && swapContext.commands.fence) { - fences[nfences++] = swapContext.commands.fence; + fences[nfences++] = swapContext.commands.fence->fence; swapContext.commands.submitted = false; } } @@ -449,12 +449,16 @@ void acquireSwapCommandBuffer(VulkanContext& context) { SwapContext& swap = getSwapContext(context); // Ensure that the previous submission of this command buffer has finished. - result = vkWaitForFences(context.device, 1, &swap.commands.fence, VK_FALSE, UINT64_MAX); - ASSERT_POSTCONDITION(result == VK_SUCCESS, "vkWaitForFences error."); + if (swap.commands.fence) { + ASSERT_POSTCONDITION(result == VK_SUCCESS, "vkWaitForFences error."); + result = vkWaitForFences(context.device, 1, &swap.commands.fence->fence, VK_FALSE, + UINT64_MAX); + ASSERT_POSTCONDITION(result == VK_SUCCESS, "vkWaitForFences error."); + } + + swap.commands.fence.reset(new VulkanCmdFence(context.device)); // Restart the command buffer. - result = vkResetFences(context.device, 1, &swap.commands.fence); - ASSERT_POSTCONDITION(result == VK_SUCCESS, "vkResetFences error."); VkCommandBuffer cmdbuffer = swap.commands.cmdbuffer; VkResult error = vkResetCommandBuffer(cmdbuffer, 0); ASSERT_POSTCONDITION(not error, "vkResetCommandBuffer error."); @@ -483,13 +487,13 @@ void flushCommandBuffer(VulkanContext& context) { .commandBufferCount = 1, .pCommandBuffers = &context.currentCommands->cmdbuffer, }; - error = vkQueueSubmit(context.graphicsQueue, 1, &submitInfo, sc.commands.fence); + error = vkQueueSubmit(context.graphicsQueue, 1, &submitInfo, sc.commands.fence->fence); ASSERT_POSTCONDITION(!error, "vkQueueSubmit error."); // Restart the command buffer. - error = vkWaitForFences(context.device, 1, &sc.commands.fence, VK_FALSE, UINT64_MAX); + error = vkWaitForFences(context.device, 1, &sc.commands.fence->fence, VK_FALSE, UINT64_MAX); ASSERT_POSTCONDITION(!error, "vkWaitForFences error."); - error = vkResetFences(context.device, 1, &sc.commands.fence); + error = vkResetFences(context.device, 1, &sc.commands.fence->fence); ASSERT_POSTCONDITION(!error, "vkResetFences error."); error = vkResetCommandBuffer(context.currentCommands->cmdbuffer, 0); ASSERT_POSTCONDITION(!error, "vkResetCommandBuffer error."); @@ -522,11 +526,11 @@ VkCommandBuffer acquireWorkCommandBuffer(VulkanContext& context) { const VkCommandBufferBeginInfo binfo { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO }; if (work.submitted) { work.submitted = false; - vkWaitForFences(context.device, 1, &work.fence, VK_FALSE, UINT64_MAX); - vkResetFences(context.device, 1, &work.fence); + vkWaitForFences(context.device, 1, &work.fence->fence, VK_FALSE, UINT64_MAX); vkResetCommandBuffer(work.cmdbuffer, 0); vkBeginCommandBuffer(work.cmdbuffer, &binfo); } + work.fence.reset(new VulkanCmdFence(context.device)); return work.cmdbuffer; } @@ -541,7 +545,7 @@ void flushWorkCommandBuffer(VulkanContext& context) { .pCommandBuffers = &work.cmdbuffer, }; vkEndCommandBuffer(work.cmdbuffer); - vkQueueSubmit(context.graphicsQueue, 1, &submitInfo, work.fence); + vkQueueSubmit(context.graphicsQueue, 1, &submitInfo, work.fence->fence); work.submitted = true; } diff --git a/filament/backend/src/vulkan/VulkanContext.h b/filament/backend/src/vulkan/VulkanContext.h index 1ba757bfe6..6f4b72e9af 100644 --- a/filament/backend/src/vulkan/VulkanContext.h +++ b/filament/backend/src/vulkan/VulkanContext.h @@ -28,6 +28,7 @@ #include "vk_mem_alloc.h" +#include #include namespace filament { @@ -39,9 +40,21 @@ constexpr VkAllocationCallbacks* VKALLOC = nullptr; struct VulkanSurfaceContext; +// This wrapper exists so that we can use shared_ptr to implement shared ownership for low-level +// Vulkan fences. +struct VulkanCmdFence { + VulkanCmdFence(VkDevice device, bool signaled = false); + ~VulkanCmdFence(); + const VkDevice device; + VkFence fence; +}; + + // The submission fence has shared ownership semantics because it is potentially wrapped by a +// DriverApi fence object and should not be destroyed until both the DriverAPI object is freed and +// we're done waiting on the most recent submission of the given command buffer. struct VulkanCommandBuffer { VkCommandBuffer cmdbuffer; - VkFence fence; + std::shared_ptr fence; VulkanDisposer::Set resources; bool submitted; }; diff --git a/filament/backend/src/vulkan/VulkanDriver.cpp b/filament/backend/src/vulkan/VulkanDriver.cpp index cb41feeb51..994de2d8d6 100644 --- a/filament/backend/src/vulkan/VulkanDriver.cpp +++ b/filament/backend/src/vulkan/VulkanDriver.cpp @@ -198,7 +198,7 @@ void VulkanDriver::terminate() { VulkanCommandBuffer& work = mContext.work; VkDevice device = mContext.device; vkFreeCommandBuffers(device, mContext.commandPool, 1, &work.cmdbuffer); - vkDestroyFence(device, work.fence, VKALLOC); + work.fence.reset(); mStagePool.reset(); mBinder.destroyCache(); @@ -408,6 +408,16 @@ void VulkanDriver::destroyRenderTarget(Handle rth) { } void VulkanDriver::createFenceR(Handle fh, int) { + // We prefer the fence to be created inside a frame, otherwise there's no command buffer. + assert(mContext.currentCommands != nullptr && "Fences should be created within a frame."); + + // As a fallback in release builds, trigger the fence based on the work command buffer. + if (mContext.currentCommands == nullptr) { + construct_handle(mHandleMap, fh, mContext.work); + return; + } + + construct_handle(mHandleMap, fh, *mContext.currentCommands); } void VulkanDriver::createSwapChainR(Handle sch, void* nativeWindow, @@ -465,7 +475,7 @@ Handle VulkanDriver::createRenderTargetS() noexcept { } Handle VulkanDriver::createFenceS() noexcept { - return {}; + return alloc_handle(); } Handle VulkanDriver::createSwapChainS() noexcept { @@ -500,7 +510,7 @@ void VulkanDriver::destroySwapChain(Handle sch) { mDisposer.release(swapContext.commands.resources); vkFreeCommandBuffers(mContext.device, mContext.commandPool, 1, &swapContext.commands.cmdbuffer); - vkDestroyFence(mContext.device, swapContext.commands.fence, VKALLOC); + swapContext.commands.fence.reset(); vkDestroyImageView(mContext.device, swapContext.attachment.view, VKALLOC); swapContext.commands.fence = VK_NULL_HANDLE; swapContext.attachment.view = VK_NULL_HANDLE; @@ -534,10 +544,13 @@ void VulkanDriver::updateStreams(CommandStream* driver) { } void VulkanDriver::destroyFence(Handle fh) { + destruct_handle(mHandleMap, fh); } FenceStatus VulkanDriver::wait(Handle fh, uint64_t timeout) { - return FenceStatus::ERROR; + VkFence fence = handle_cast(mHandleMap, fh)->fence->fence; + VkResult result = vkWaitForFences(mContext.device, 1, &fence, VK_FALSE, timeout); + return result == VK_SUCCESS ? FenceStatus::CONDITION_SATISFIED : FenceStatus::TIMEOUT_EXPIRED; } // We create all textures using VK_IMAGE_TILING_OPTIMAL, so our definition of "supported" is that @@ -565,7 +578,7 @@ bool VulkanDriver::isRenderTargetFormatSupported(TextureFormat format) { } bool VulkanDriver::isFrameTimeSupported() { - return false; + return true; } void VulkanDriver::updateVertexBuffer(Handle vbh, size_t index, @@ -809,7 +822,8 @@ void VulkanDriver::commit(Handle sch) { .signalSemaphoreCount = 1u, .pSignalSemaphores = &surfaceContext.renderingFinished, }; - result = vkQueueSubmit(mContext.graphicsQueue, 1, &submitInfo, swapContext.commands.fence); + result = vkQueueSubmit(mContext.graphicsQueue, 1, &submitInfo, + swapContext.commands.fence->fence); ASSERT_POSTCONDITION(result == VK_SUCCESS, "vkQueueSubmit error."); swapContext.commands.submitted = true; diff --git a/filament/backend/src/vulkan/VulkanHandles.h b/filament/backend/src/vulkan/VulkanHandles.h index 9cca70828c..fad2157594 100644 --- a/filament/backend/src/vulkan/VulkanHandles.h +++ b/filament/backend/src/vulkan/VulkanHandles.h @@ -162,6 +162,11 @@ struct VulkanRenderPrimitive : public HwRenderPrimitive { std::vector offsets; }; +struct VulkanFence : public HwFence { + VulkanFence(const VulkanCommandBuffer& commands) : fence(commands.fence) {} + std::shared_ptr fence; +}; + } // namespace filament } // namespace driver