From 9d061cbef6e226e9e232ec0c61fb15d0b80b1e02 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Wed, 6 Mar 2019 18:33:31 +0100 Subject: [PATCH] - implement VkPostprocess::GetOutput --- .../vulkan/renderer/vk_postprocess.cpp | 72 +++++++++++++------ .../vulkan/renderer/vk_postprocess.h | 2 +- .../vulkan/renderer/vk_renderbuffers.cpp | 2 + .../vulkan/renderer/vk_renderbuffers.h | 1 + 4 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/rendering/vulkan/renderer/vk_postprocess.cpp b/src/rendering/vulkan/renderer/vk_postprocess.cpp index bce78e08d..539f94167 100644 --- a/src/rendering/vulkan/renderer/vk_postprocess.cpp +++ b/src/rendering/vulkan/renderer/vk_postprocess.cpp @@ -335,31 +335,59 @@ VulkanDescriptorSet *VkPostprocess::GetInput(VkPPRenderPassSetup *passSetup, con VulkanFramebuffer *VkPostprocess::GetOutput(VkPPRenderPassSetup *passSetup, const PPOutput &output) { -#if 0 - switch (output.Type) + auto fb = GetVulkanFrameBuffer(); + + VulkanImage *image; + VulkanImageView *view; + VkImageLayout *layout; + + if (output.Type == PPTextureType::CurrentPipelineTexture || output.Type == PPTextureType::NextPipelineTexture) { - default: - I_FatalError("Unsupported postprocess output type\n"); - break; + int idx = mCurrentPipelineImage; + if (output.Type == PPTextureType::NextPipelineTexture) + idx = (idx + 1) % VkRenderBuffers::NumPipelineImages; - case PPTextureType::CurrentPipelineTexture: - BindCurrentFB(); - break; - - case PPTextureType::NextPipelineTexture: - BindNextFB(); - break; - - case PPTextureType::PPTexture: - mTextures[step.Output.Texture]->View - break; - - case PPTextureType::SceneColor: - BindSceneFB(false); - break; + image = fb->GetBuffers()->PipelineImage[idx].get(); + view = fb->GetBuffers()->PipelineView[idx].get(); + layout = &fb->GetBuffers()->PipelineLayout[idx]; } -#endif - return nullptr; + else if (output.Type == PPTextureType::PPTexture) + { + image = mTextures[output.Texture]->Image.get(); + view = mTextures[output.Texture]->View.get(); + layout = &mTextures[output.Texture]->Layout; + } + else if (output.Type == PPTextureType::SceneColor) + { + image = fb->GetBuffers()->SceneColor.get(); + view = fb->GetBuffers()->SceneColorView.get(); + layout = &fb->GetBuffers()->SceneColorLayout; + } + else + { + I_FatalError("Unsupported postprocess output type\n"); + return nullptr; + } + + if (*layout != VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) + { + PipelineBarrier barrier; + barrier.addImage(image, *layout, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, VK_ACCESS_SHADER_READ_BIT, VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT); + barrier.execute(fb->GetDrawCommands(), VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT); + *layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + } + + auto &framebuffer = passSetup->Framebuffers[view]; + if (!framebuffer) + { + FramebufferBuilder builder; + builder.setRenderPass(passSetup->RenderPass.get()); + builder.setSize(image->width, image->height); + builder.addAttachment(view); + framebuffer = builder.create(GetVulkanFrameBuffer()->device); + } + + return framebuffer.get(); } VulkanSampler *VkPostprocess::GetSampler(PPFilterMode filter, PPWrapMode wrap) diff --git a/src/rendering/vulkan/renderer/vk_postprocess.h b/src/rendering/vulkan/renderer/vk_postprocess.h index d2486c9c5..acd21919e 100644 --- a/src/rendering/vulkan/renderer/vk_postprocess.h +++ b/src/rendering/vulkan/renderer/vk_postprocess.h @@ -91,7 +91,7 @@ public: //std::unique_ptr DescriptorPool; - //std::unique_ptr Framebuffer; + std::map> Framebuffers; //std::unique_ptr DescriptorSet; private: diff --git a/src/rendering/vulkan/renderer/vk_renderbuffers.cpp b/src/rendering/vulkan/renderer/vk_renderbuffers.cpp index 9c2e68b8f..810040082 100644 --- a/src/rendering/vulkan/renderer/vk_renderbuffers.cpp +++ b/src/rendering/vulkan/renderer/vk_renderbuffers.cpp @@ -111,4 +111,6 @@ void VkRenderBuffers::CreateScene(int width, int height, int samples) barrier.addImage(SceneColor.get(), VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 0, VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT); barrier.addImage(SceneDepthStencil.get(), VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, 0, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT); barrier.execute(fb->GetDrawCommands(), VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT); + + SceneColorLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; } diff --git a/src/rendering/vulkan/renderer/vk_renderbuffers.h b/src/rendering/vulkan/renderer/vk_renderbuffers.h index 390111fdc..51628c2fa 100644 --- a/src/rendering/vulkan/renderer/vk_renderbuffers.h +++ b/src/rendering/vulkan/renderer/vk_renderbuffers.h @@ -22,6 +22,7 @@ public: std::unique_ptr SceneDepthStencilView; std::unique_ptr SceneDepthView; VkFormat SceneDepthStencilFormat = VK_FORMAT_D24_UNORM_S8_UINT; + VkImageLayout SceneColorLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; static const int NumPipelineImages = 2; std::unique_ptr PipelineImage[NumPipelineImages];