diff --git a/kraken/KRPipeline.cpp b/kraken/KRPipeline.cpp index 0a50886..0bcb69a 100644 --- a/kraken/KRPipeline.cpp +++ b/kraken/KRPipeline.cpp @@ -36,6 +36,7 @@ #include "KRSpotLight.h" #include "KRPointLight.h" #include "KRContext.h" +#include "KRRenderPass.h" const char *KRPipeline::KRENGINE_UNIFORM_NAMES[] = { @@ -109,7 +110,7 @@ const char *KRPipeline::KRENGINE_UNIFORM_NAMES[] = { "fade_color", // KRENGINE_UNIFORM_FADE_COLOR }; -KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const char* szKey, const std::vector& shaders, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat) +KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, KRRenderPass& renderPass, const char* szKey, const std::vector& shaders, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat) : KRContextObject(context) , m_iProgram(0) // not used for Vulkan { @@ -333,7 +334,7 @@ KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const char* szKey pipelineInfo.pColorBlendState = &colorBlending; pipelineInfo.pDynamicState = nullptr; pipelineInfo.layout = m_pipelineLayout; - pipelineInfo.renderPass = surface.getRenderPass(); + pipelineInfo.renderPass = renderPass.m_renderPass; pipelineInfo.subpass = 0; pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; pipelineInfo.basePipelineIndex = -1; diff --git a/kraken/KRPipeline.h b/kraken/KRPipeline.h index 7328c58..f6f90f3 100644 --- a/kraken/KRPipeline.h +++ b/kraken/KRPipeline.h @@ -43,11 +43,12 @@ class KRShader; class KRSurface; +class KRRenderPass; class KRPipeline : public KRContextObject { public: - KRPipeline(KRContext& context, KRSurface& surface, const char* szKey, const std::vector& shaders, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat); + KRPipeline(KRContext& context, KRSurface& surface, KRRenderPass& renderPass, const char* szKey, const std::vector& shaders, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat); KRPipeline(KRContext &context, char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource); virtual ~KRPipeline(); const char *getKey() const; diff --git a/kraken/KRPipelineManager.cpp b/kraken/KRPipelineManager.cpp index ab378bf..a8a0657 100644 --- a/kraken/KRPipelineManager.cpp +++ b/kraken/KRPipelineManager.cpp @@ -62,7 +62,7 @@ KRPipelineManager::~KRPipelineManager() { #endif // ANDROID } -KRPipeline* KRPipelineManager::getPipeline(KRSurface& surface, const std::string& shader_name, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat) +KRPipeline* KRPipelineManager::getPipeline(KRSurface& surface, KRRenderPass& renderPass, const std::string& shader_name, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat) { std::pair > key; key.first = shader_name; @@ -80,7 +80,7 @@ KRPipeline* KRPipelineManager::getPipeline(KRSurface& surface, const std::string std::vector shaders; shaders.push_back(m_pContext->getShaderManager()->get(shader_name + ".vert", "spv")); shaders.push_back(m_pContext->getShaderManager()->get(shader_name + ".frag", "spv")); - KRPipeline* pipeline = new KRPipeline(*m_pContext, surface, shader_name.c_str(), shaders, vertexAttributes, modelFormat); + KRPipeline* pipeline = new KRPipeline(*m_pContext, surface, renderPass, shader_name.c_str(), shaders, vertexAttributes, modelFormat); m_pipelines[key] = pipeline; diff --git a/kraken/KRPipelineManager.h b/kraken/KRPipelineManager.h index 4ad692b..f4642c4 100644 --- a/kraken/KRPipelineManager.h +++ b/kraken/KRPipelineManager.h @@ -55,7 +55,7 @@ public: virtual ~KRPipelineManager(); KRPipeline* get(const char* szKey); - KRPipeline *getPipeline(KRSurface& surface, const std::string& shader_name, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat); + KRPipeline *getPipeline(KRSurface& surface, KRRenderPass& renderPass, const std::string& shader_name, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat); KRPipeline *getPipeline(const std::string &pipeline_name, KRCamera *pCamera, const std::vector &point_lights, const std::vector &directional_lights, const std::vector&spot_lights, int bone_count, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, bool bReflectionMap, bool bReflectionCubeMap, bool bLightMap, bool bDiffuseMapScale,bool bSpecMapScale, bool bNormalMapScale, bool bReflectionMapScale, bool bDiffuseMapOffset, bool bSpecMapOffset, bool bNormalMapOffset, bool bReflectionMapOffset, bool bAlphaTest, bool bAlphaBlend, KRNode::RenderPass renderPass, bool bRimColor = false); bool selectPipeline(KRCamera &camera, KRPipeline *pPipeline, const KRViewport &viewport, const Matrix4 &matModel, const std::vector &point_lights, const std::vector &directional_lights, const std::vector&spot_lights, int bone_count, const KRNode::RenderPass &renderPass, const Vector3 &rim_color, float rim_power, const Vector4 &fade_color); diff --git a/kraken/KRPresentationThread.cpp b/kraken/KRPresentationThread.cpp index 3b3b75e..1db19b5 100644 --- a/kraken/KRPresentationThread.cpp +++ b/kraken/KRPresentationThread.cpp @@ -30,6 +30,7 @@ // #include "KRPresentationThread.h" +#include "KRRenderPass.h" KRPresentationThread::KRPresentationThread(KRContext& context) : KRContextObject(context) @@ -153,32 +154,21 @@ void KRPresentationThread::renderFrame() // TODO - Add error handling... } - 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; - renderPassInfo.renderPass = surface.getRenderPass(); - renderPassInfo.framebuffer = surface.m_swapChainFramebuffers[frameIndex % surface.m_swapChainFramebuffers.size()]; - renderPassInfo.renderArea.offset = { 0, 0 }; - renderPassInfo.renderArea.extent = surface.m_swapChainExtent; - renderPassInfo.clearValueCount = static_cast(clearValues.size()); - renderPassInfo.pClearValues = clearValues.data(); - - vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); + KRRenderPass& forwardOpaquePass = surface.getForwardOpaquePass(); + forwardOpaquePass.begin(commandBuffer, surface, frameIndex); KRMeshManager::KRVBOData& testVertices = getContext().getMeshManager()->KRENGINE_VBO_DATA_2D_SQUARE_VERTICES; bool haveMesh = testVertices.isVBOReady(); if (haveMesh) { - KRPipeline* testPipeline = m_pContext->getPipelineManager()->getPipeline(surface, "vulkan_test", testVertices.getVertexAttributes(), KRMesh::model_format_t::KRENGINE_MODEL_FORMAT_STRIP); + KRPipeline* testPipeline = m_pContext->getPipelineManager()->getPipeline(surface, forwardOpaquePass, "vulkan_test", testVertices.getVertexAttributes(), KRMesh::model_format_t::KRENGINE_MODEL_FORMAT_STRIP); testPipeline->bind(commandBuffer); testVertices.bind(commandBuffer); vkCmdDraw(commandBuffer, 4, 1, 0, 0); } - vkCmdEndRenderPass(commandBuffer); + forwardOpaquePass.end(commandBuffer); + if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) { m_activeState = PresentThreadState::error; // TODO - Add error handling... diff --git a/kraken/KRRenderPass.cpp b/kraken/KRRenderPass.cpp index ac75310..973535f 100644 --- a/kraken/KRRenderPass.cpp +++ b/kraken/KRRenderPass.cpp @@ -30,6 +30,7 @@ // #include "KRRenderPass.h" +#include "KRSurface.h"" KRRenderPass::KRRenderPass(KRContext& context) : KRContextObject(context) @@ -114,4 +115,27 @@ void KRRenderPass::destroy(KRDevice &device) vkDestroyRenderPass(device.m_logicalDevice, m_renderPass, nullptr); m_renderPass = VK_NULL_HANDLE; } -} \ No newline at end of file +} + +void KRRenderPass::begin(VkCommandBuffer& commandBuffer, KRSurface& surface, uint64_t frameIndex) +{ + 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; + renderPassInfo.renderPass = m_renderPass; + renderPassInfo.framebuffer = surface.m_swapChainFramebuffers[frameIndex % surface.m_swapChainFramebuffers.size()]; + renderPassInfo.renderArea.offset = { 0, 0 }; + renderPassInfo.renderArea.extent = surface.m_swapChainExtent; + renderPassInfo.clearValueCount = static_cast(clearValues.size()); + renderPassInfo.pClearValues = clearValues.data(); + + vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); +} + +void KRRenderPass::end(VkCommandBuffer& commandBuffer) +{ + vkCmdEndRenderPass(commandBuffer); +} diff --git a/kraken/KRRenderPass.h b/kraken/KRRenderPass.h index e406942..8e29be3 100644 --- a/kraken/KRRenderPass.h +++ b/kraken/KRRenderPass.h @@ -35,6 +35,8 @@ #include "KREngine-common.h" #include "KRContext.h" +class KRSurface; + class KRRenderPass : public KRContextObject { public: @@ -43,6 +45,9 @@ public: void create(KRDevice& device, VkFormat swapChainImageFormat, VkFormat depthImageFormat); void destroy(KRDevice& device); + void begin(VkCommandBuffer &commandBuffer, KRSurface& surface, uint64_t frameIndex); + void end(VkCommandBuffer& commandBuffer); + // private: VkRenderPass m_renderPass; }; diff --git a/kraken/KRSurface.cpp b/kraken/KRSurface.cpp index 16f3041..fefc3c9 100644 --- a/kraken/KRSurface.cpp +++ b/kraken/KRSurface.cpp @@ -323,7 +323,7 @@ KrResult KRSurface::createSwapChain() VkFramebufferCreateInfo framebufferInfo{}; framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; - framebufferInfo.renderPass = getRenderPass(); + framebufferInfo.renderPass = m_forwardOpaquePass->m_renderPass; framebufferInfo.attachmentCount = static_cast(attachments.size()); framebufferInfo.pAttachments = attachments.data(); framebufferInfo.width = m_swapChainExtent.width; @@ -391,11 +391,6 @@ void KRSurface::createRenderPasses() m_forwardOpaquePass->create(*device, m_swapChainImageFormat, m_depthImageFormat); } -VkRenderPass& KRSurface::getRenderPass() -{ - return m_forwardOpaquePass->m_renderPass; -} - std::unique_ptr& KRSurface::getDevice() { return m_pContext->getDeviceManager()->getDevice(m_deviceHandle); @@ -415,3 +410,9 @@ VkFormat KRSurface::getDepthFormat() const { return m_depthImageFormat; } + + +KRRenderPass& KRSurface::getForwardOpaquePass() +{ + return *m_forwardOpaquePass; +} diff --git a/kraken/KRSurface.h b/kraken/KRSurface.h index e31b14e..525ac4f 100644 --- a/kraken/KRSurface.h +++ b/kraken/KRSurface.h @@ -59,7 +59,7 @@ public: KrResult initialize(); KrResult recreateSwapChain(); void createRenderPasses(); - VkRenderPass& getRenderPass(); + KRRenderPass& getForwardOpaquePass(); #ifdef WIN32 HWND m_hWnd;