From 9bdfb0feea5515454661abbd29742b9d235b0792 Mon Sep 17 00:00:00 2001 From: Kearwood Gilbert Date: Tue, 1 Mar 2022 23:53:42 -0800 Subject: [PATCH] Attached depth buffer to KRSurface and enabled depth testing in render pass --- kraken/KRPipeline.cpp | 14 ++++++++++- kraken/KRPresentationThread.cpp | 8 ++++--- kraken/KRSurface.cpp | 42 ++++++++++++++++++++++++++------- kraken/KRSurface.h | 1 + 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/kraken/KRPipeline.cpp b/kraken/KRPipeline.cpp index c102cd2..0a50886 100644 --- a/kraken/KRPipeline.cpp +++ b/kraken/KRPipeline.cpp @@ -308,6 +308,18 @@ KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const char* szKey // failed! TODO - Error handling } + VkPipelineDepthStencilStateCreateInfo depthStencil{}; + depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; + depthStencil.depthTestEnable = VK_TRUE; + depthStencil.depthWriteEnable = VK_TRUE; + depthStencil.depthCompareOp = VK_COMPARE_OP_LESS; + depthStencil.depthBoundsTestEnable = VK_FALSE; + depthStencil.minDepthBounds = 0.0f; + depthStencil.maxDepthBounds = 1.0f; + depthStencil.stencilTestEnable = VK_FALSE; + depthStencil.front = {}; + depthStencil.back = {}; + VkGraphicsPipelineCreateInfo pipelineInfo{}; pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineInfo.stageCount = stage_count; @@ -317,7 +329,7 @@ KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const char* szKey pipelineInfo.pViewportState = &viewportState; pipelineInfo.pRasterizationState = &rasterizer; pipelineInfo.pMultisampleState = &multisampling; - pipelineInfo.pDepthStencilState = nullptr; + pipelineInfo.pDepthStencilState = &depthStencil; pipelineInfo.pColorBlendState = &colorBlending; pipelineInfo.pDynamicState = nullptr; pipelineInfo.layout = m_pipelineLayout; diff --git a/kraken/KRPresentationThread.cpp b/kraken/KRPresentationThread.cpp index b353011..d962fd9 100644 --- a/kraken/KRPresentationThread.cpp +++ b/kraken/KRPresentationThread.cpp @@ -150,7 +150,9 @@ void KRPresentationThread::renderFrame() // TODO - Add error handling... } - VkClearValue clearColor = { {{0.0f, 0.0f, 0.0f, 1.0f}} }; + std::array clearValues{}; + clearValues[0].color = { {0.0f, 0.0f, 0.0f, 1.0f} }; + clearValues[1].depthStencil = { 1.0f, 0 }; VkRenderPassBeginInfo renderPassInfo{}; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; @@ -158,8 +160,8 @@ void KRPresentationThread::renderFrame() renderPassInfo.framebuffer = surface.m_swapChainFramebuffers[frameIndex % surface.m_swapChainFramebuffers.size()]; renderPassInfo.renderArea.offset = { 0, 0 }; renderPassInfo.renderArea.extent = surface.m_swapChainExtent; - renderPassInfo.clearValueCount = 1; - renderPassInfo.pClearValues = &clearColor; + renderPassInfo.clearValueCount = static_cast(clearValues.size()); + renderPassInfo.pClearValues = clearValues.data(); vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); diff --git a/kraken/KRSurface.cpp b/kraken/KRSurface.cpp index cd5589a..cc489db 100644 --- a/kraken/KRSurface.cpp +++ b/kraken/KRSurface.cpp @@ -317,13 +317,16 @@ KrResult KRSurface::createSwapChain() m_swapChainFramebuffers.resize(m_swapChainImageViews.size()); for (size_t i = 0; i < m_swapChainImageViews.size(); i++) { - VkImageView attachments[] = { m_swapChainImageViews[i] }; + std::array attachments = { + m_swapChainImageViews[i], + m_depthImageView + }; VkFramebufferCreateInfo framebufferInfo{}; framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; framebufferInfo.renderPass = getRenderPass(); - framebufferInfo.attachmentCount = 1; - framebufferInfo.pAttachments = attachments; + framebufferInfo.attachmentCount = static_cast(attachments.size()); + framebufferInfo.pAttachments = attachments.data(); framebufferInfo.width = m_swapChainExtent.width; framebufferInfo.height = m_swapChainExtent.height; framebufferInfo.layers = 1; @@ -399,6 +402,21 @@ void KRSurface::createRenderPasses() colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; + + VkAttachmentDescription depthAttachment{}; + depthAttachment.format = m_depthImageFormat; + depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT; + depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + + VkAttachmentReference depthAttachmentRef{}; + depthAttachmentRef.attachment = 1; + depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + VkAttachmentReference colorAttachmentRef{}; colorAttachmentRef.attachment = 0; colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; @@ -407,19 +425,22 @@ void KRSurface::createRenderPasses() subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; subpass.colorAttachmentCount = 1; subpass.pColorAttachments = &colorAttachmentRef; + subpass.pDepthStencilAttachment = &depthAttachmentRef; VkSubpassDependency dependency{}; dependency.srcSubpass = VK_SUBPASS_EXTERNAL; dependency.dstSubpass = 0; - dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT; dependency.srcAccessMask = 0; - dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; + dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT; + dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; + + std::array attachments = { colorAttachment, depthAttachment }; VkRenderPassCreateInfo renderPassInfo{}; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; - renderPassInfo.attachmentCount = 1; - renderPassInfo.pAttachments = &colorAttachment; + renderPassInfo.attachmentCount = static_cast(attachments.size()); + renderPassInfo.pAttachments = attachments.data(); renderPassInfo.subpassCount = 1; renderPassInfo.pSubpasses = &subpass; renderPassInfo.dependencyCount = 1; @@ -451,3 +472,8 @@ uint32_t KRSurface::getHeight() const { return m_swapChainExtent.height; } + +VkFormat KRSurface::getDepthFormat() const +{ + return m_depthImageFormat; +} diff --git a/kraken/KRSurface.h b/kraken/KRSurface.h index 91a779f..a6ed1bb 100644 --- a/kraken/KRSurface.h +++ b/kraken/KRSurface.h @@ -49,6 +49,7 @@ public: void destroy(); uint32_t getWidth() const; uint32_t getHeight() const; + VkFormat getDepthFormat() const; KRSurface(const KRSurface&) = delete; KRSurface& operator=(const KRSurface&) = delete;