From 74861d2d73aeb4fe733c67154eab306cceea93ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 31 Oct 2017 21:15:19 +0100 Subject: [PATCH] Vulkan: Make backbuffer transitions part of backbuffer render pass. Optimize depth buffer memory operations. --- Common/Vulkan/VulkanContext.cpp | 14 -------------- Common/Vulkan/VulkanContext.h | 3 --- ext/native/thin3d/VulkanQueueRunner.cpp | 8 ++++---- ext/native/thin3d/VulkanRenderManager.cpp | 7 +------ 4 files changed, 5 insertions(+), 27 deletions(-) diff --git a/Common/Vulkan/VulkanContext.cpp b/Common/Vulkan/VulkanContext.cpp index a49cd58824..3fcfcc49cb 100644 --- a/Common/Vulkan/VulkanContext.cpp +++ b/Common/Vulkan/VulkanContext.cpp @@ -153,20 +153,6 @@ VulkanContext::~VulkanContext() { VulkanFree(); } -void TransitionToPresent(VkCommandBuffer cmd, VkImage image) { - TransitionImageLayout2(cmd, image, VK_IMAGE_ASPECT_COLOR_BIT, - VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, - VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, - VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_MEMORY_READ_BIT); -} - -void TransitionFromPresent(VkCommandBuffer cmd, VkImage image) { - TransitionImageLayout2(cmd, image, VK_IMAGE_ASPECT_COLOR_BIT, - VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, - VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, - VK_ACCESS_MEMORY_READ_BIT, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT); -} - void VulkanContext::BeginFrame() { FrameData *frame = &frame_[curFrame_]; // Process pending deletes. diff --git a/Common/Vulkan/VulkanContext.h b/Common/Vulkan/VulkanContext.h index e8204dba4c..51c46409c9 100644 --- a/Common/Vulkan/VulkanContext.h +++ b/Common/Vulkan/VulkanContext.h @@ -393,9 +393,6 @@ void TransitionImageLayout2(VkCommandBuffer cmd, VkImage image, VkImageAspectFla VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkAccessFlags srcAccessMask, VkAccessFlags dstAccessMask); -void TransitionFromPresent(VkCommandBuffer cmd, VkImage image); -void TransitionToPresent(VkCommandBuffer cmd, VkImage image); - // GLSL compiler void init_glslang(); void finalize_glslang(); diff --git a/ext/native/thin3d/VulkanQueueRunner.cpp b/ext/native/thin3d/VulkanQueueRunner.cpp index f4028a0bfa..d8b358ea79 100644 --- a/ext/native/thin3d/VulkanQueueRunner.cpp +++ b/ext/native/thin3d/VulkanQueueRunner.cpp @@ -54,16 +54,16 @@ void VulkanQueueRunner::InitBackbufferRenderPass() { attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE; attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - attachments[0].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; - attachments[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + attachments[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; // We don't want to preserve the backbuffer between frames so we really don't care. + attachments[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; // We only render once to the backbuffer per frame so we can do this here. attachments[0].flags = 0; attachments[1].format = vulkan_->GetDeviceInfo().preferredDepthStencilFormat; // must use this same format later for the back depth buffer. attachments[1].samples = VK_SAMPLE_COUNT_1_BIT; attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE; + attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; // Don't care about storing backbuffer Z - we clear it anyway. attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE; + attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attachments[1].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; attachments[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; attachments[1].flags = 0; diff --git a/ext/native/thin3d/VulkanRenderManager.cpp b/ext/native/thin3d/VulkanRenderManager.cpp index d6dbd62930..2bcee4cb28 100644 --- a/ext/native/thin3d/VulkanRenderManager.cpp +++ b/ext/native/thin3d/VulkanRenderManager.cpp @@ -302,6 +302,7 @@ VkCommandBuffer VulkanRenderManager::GetInitCmd() { } void VulkanRenderManager::BindFramebufferAsRenderTarget(VKRFramebuffer *fb, VKRRenderPassAction color, VKRRenderPassAction depth, uint32_t clearColor, float clearDepth, uint8_t clearStencil) { + assert(insideFrame_); // Eliminate dupes. if (steps_.size() && steps_.back()->render.framebuffer == fb && steps_.back()->stepType == VKRStepType::RENDER) { if (color != VKRRenderPassAction::CLEAR && depth != VKRRenderPassAction::CLEAR) { @@ -570,9 +571,6 @@ void VulkanRenderManager::BeginSubmitFrame(int frame) { assert(res == VK_SUCCESS); - // TODO: Is it best to do this here, or combine with some other transition, or just do it right before the backbuffer bind-for-render? - TransitionFromPresent(frameData.mainCmd, swapchainImages_[frameData.curSwapchainImage].image); - queueRunner_.SetBackbuffer(framebuffers_[frameData.curSwapchainImage]); frameData.hasBegun = true; @@ -629,8 +627,6 @@ void VulkanRenderManager::EndSubmitFrame(int frame) { frameData.hasBegun = false; insideFrame_ = false; - TransitionToPresent(frameData.mainCmd, swapchainImages_[frameData.curSwapchainImage].image); - Submit(frame, true); VkSwapchainKHR swapchain = vulkan_->GetSwapchain(); @@ -640,7 +636,6 @@ void VulkanRenderManager::EndSubmitFrame(int frame) { present.pImageIndices = &frameData.curSwapchainImage; present.pWaitSemaphores = &renderingCompleteSemaphore_; present.waitSemaphoreCount = 1; - present.pResults = nullptr; VkResult res = vkQueuePresentKHR(vulkan_->GetGraphicsQueue(), &present); // TODO: Deal with the VK_SUBOPTIMAL_WSI and VK_ERROR_OUT_OF_DATE_WSI