From 1b5a730358983ff3980a3d85fe58711fced9ae94 Mon Sep 17 00:00:00 2001 From: Philip Rideout Date: Thu, 21 Nov 2019 11:32:30 -0800 Subject: [PATCH] VulkanDriver: add depth buffer to swap chain. --- filament/backend/src/vulkan/VulkanContext.cpp | 76 +++++++++++++++++++ filament/backend/src/vulkan/VulkanContext.h | 2 + filament/backend/src/vulkan/VulkanDriver.cpp | 3 + 3 files changed, 81 insertions(+) diff --git a/filament/backend/src/vulkan/VulkanContext.cpp b/filament/backend/src/vulkan/VulkanContext.cpp index 8a50f3973b..eb35643dd6 100644 --- a/filament/backend/src/vulkan/VulkanContext.cpp +++ b/filament/backend/src/vulkan/VulkanContext.cpp @@ -386,6 +386,8 @@ void createSwapChain(VulkanContext& context, VulkanSurfaceContext& surfaceContex for (uint32_t i = 0; i < allocateInfo.commandBufferCount; ++i) { surfaceContext.swapContexts[i].commands.cmdbuffer = cmdbufs[i]; } + + createDepthBuffer(context, surfaceContext, context.depthFormat); } uint32_t selectMemoryType(VulkanContext& context, uint32_t flags, VkFlags reqs) { @@ -554,5 +556,79 @@ void flushWorkCommandBuffer(VulkanContext& context) { work.fence->submitted = true; } +void createDepthBuffer(VulkanContext& context, VulkanSurfaceContext& surfaceContext, + VkFormat depthFormat) { + // Create an appropriately-sized device-only VkImage. + const auto size = surfaceContext.surfaceCapabilities.currentExtent; + VkImage depthImage; + VkImageCreateInfo imageInfo { + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .imageType = VK_IMAGE_TYPE_2D, + .extent = { size.width, size.height, 1 }, + .format = depthFormat, + .mipLevels = 1, + .arrayLayers = 1, + .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, + .samples = VK_SAMPLE_COUNT_1_BIT, + }; + VkResult error = vkCreateImage(context.device, &imageInfo, VKALLOC, &depthImage); + assert(!error && "Unable to create depth image."); + + // Allocate memory for the VkImage and bind it. + VkMemoryRequirements memReqs; + vkGetImageMemoryRequirements(context.device, depthImage, &memReqs); + VkMemoryAllocateInfo allocInfo { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = memReqs.size, + .memoryTypeIndex = selectMemoryType(context, memReqs.memoryTypeBits, + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) + }; + error = vkAllocateMemory(context.device, &allocInfo, nullptr, + &surfaceContext.depth.memory); + assert(!error && "Unable to allocate depth memory."); + error = vkBindImageMemory(context.device, depthImage, surfaceContext.depth.memory, 0); + assert(!error && "Unable to bind depth memory."); + + // Create a VkImageView so that we can attach depth to the framebuffer. + VkImageView depthView; + VkImageViewCreateInfo viewInfo { + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, + .image = depthImage, + .viewType = VK_IMAGE_VIEW_TYPE_2D, + .format = depthFormat, + .subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, + .subresourceRange.levelCount = 1, + .subresourceRange.layerCount = 1, + }; + error = vkCreateImageView(context.device, &viewInfo, VKALLOC, &depthView); + assert(!error && "Unable to create depth view."); + + // Unlike the color attachments (which are double-buffered or triple-buffered), we only need one + // depth attachment in the entire chain. + surfaceContext.depth.view = depthView; + surfaceContext.depth.image = depthImage; + surfaceContext.depth.format = depthFormat; + + // Begin a new command buffer solely for the purpose of transitioning the image layout. + VkCommandBuffer cmdbuffer = acquireWorkCommandBuffer(context); + + // Transition the depth image into an optimal layout. + VkImageMemoryBarrier barrier { + .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, + .newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .image = depthImage, + .subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, + .subresourceRange.levelCount = 1, + .subresourceRange.layerCount = 1, + .dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT + }; + vkCmdPipelineBarrier(cmdbuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, + VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT, 0, 0, nullptr, 0, nullptr, 1, &barrier); + + flushWorkCommandBuffer(context); +} + } // namespace filament } // namespace backend diff --git a/filament/backend/src/vulkan/VulkanContext.h b/filament/backend/src/vulkan/VulkanContext.h index 56e98526c0..e470f39052 100644 --- a/filament/backend/src/vulkan/VulkanContext.h +++ b/filament/backend/src/vulkan/VulkanContext.h @@ -120,6 +120,7 @@ struct VulkanSurfaceContext { uint32_t currentSwapIndex; VkSemaphore imageAvailable; VkSemaphore renderingFinished; + VulkanAttachment depth; }; void selectPhysicalDevice(VulkanContext& context); @@ -137,6 +138,7 @@ VkFormat findSupportedFormat(VulkanContext& context, const std::vector VkImageTiling tiling, VkFormatFeatureFlags features); VkCommandBuffer acquireWorkCommandBuffer(VulkanContext& context); void flushWorkCommandBuffer(VulkanContext& context); +void createDepthBuffer(VulkanContext& context, VulkanSurfaceContext& sc, VkFormat depthFormat); } // namespace filament } // namespace backend diff --git a/filament/backend/src/vulkan/VulkanDriver.cpp b/filament/backend/src/vulkan/VulkanDriver.cpp index 24a5c15f44..c1bfa37863 100644 --- a/filament/backend/src/vulkan/VulkanDriver.cpp +++ b/filament/backend/src/vulkan/VulkanDriver.cpp @@ -535,6 +535,9 @@ void VulkanDriver::destroySwapChain(Handle sch) { vkDestroySemaphore(mContext.device, surfaceContext.imageAvailable, VKALLOC); vkDestroySemaphore(mContext.device, surfaceContext.renderingFinished, VKALLOC); vkDestroySurfaceKHR(mContext.instance, surfaceContext.surface, VKALLOC); + vkDestroyImageView(mContext.device, surfaceContext.depth.view, VKALLOC); + vkDestroyImage(mContext.device, surfaceContext.depth.image, VKALLOC); + vkFreeMemory(mContext.device, surfaceContext.depth.memory, VKALLOC); if (mContext.currentSurface == &surfaceContext) { mContext.currentSurface = nullptr; }