diff --git a/kraken/KRContext.cpp b/kraken/KRContext.cpp index dc1d8c0..d958979 100755 --- a/kraken/KRContext.cpp +++ b/kraken/KRContext.cpp @@ -932,7 +932,7 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW if (m_vulkanInstance == VK_NULL_HANDLE) { return KR_ERROR_VULKAN_REQUIRED; } - if (m_surfaces.count(createWindowSurfaceInfo->surfaceHandle)) { + if (m_surfaceHandleMap.count(createWindowSurfaceInfo->surfaceHandle)) { return KR_ERROR_DUPLICATE_HANDLE; } @@ -958,33 +958,6 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW m_surfaces.insert(std::pair>(surfaceHandle, std::move(info))); m_surfaceHandleMap.insert(std::pair(createWindowSurfaceInfo->surfaceHandle, surfaceHandle)); - - m_pPipelineManager->createPipelines(surfaceHandle); - - { - KRPipeline* testPipeline = m_pPipelineManager->get("vulkan_test"); - KRSurface& surface = *m_surfaces[surfaceHandle]; - surface.m_swapChainFramebuffers.resize(surface.m_swapChainImageViews.size()); - - for (size_t i = 0; i < surface.m_swapChainImageViews.size(); i++) { - VkImageView attachments[] = { - surface.m_swapChainImageViews[i] - }; - - VkFramebufferCreateInfo framebufferInfo{}; - framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; - framebufferInfo.renderPass = testPipeline->getRenderPass(); - framebufferInfo.attachmentCount = 1; - framebufferInfo.pAttachments = attachments; - framebufferInfo.width = surface.m_swapChainExtent.width; - framebufferInfo.height = surface.m_swapChainExtent.height; - framebufferInfo.layers = 1; - - if (vkCreateFramebuffer(deviceInfo->m_logicalDevice, &framebufferInfo, nullptr, &surface.m_swapChainFramebuffers[i]) != VK_SUCCESS) { - // TODO - Error Handling - } - } - } return KR_SUCCESS; #else diff --git a/kraken/KRPipeline.cpp b/kraken/KRPipeline.cpp index 52762ef..e2d4da9 100644 --- a/kraken/KRPipeline.cpp +++ b/kraken/KRPipeline.cpp @@ -109,15 +109,14 @@ const char *KRPipeline::KRENGINE_UNIFORM_NAMES[] = { "fade_color", // KRENGINE_UNIFORM_FADE_COLOR }; -KRPipeline::KRPipeline(KRContext& context, KrSurfaceHandle surfaceHandle, const char* szKey, const std::vector& shaders) +KRPipeline::KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, VkFormat swapChainImageFormat, uint32_t swapChainWidth, uint32_t swapChainHeight, const char* szKey, const std::vector& shaders) : KRContextObject(context) , m_iProgram(0) // not used for Vulkan { m_pipelineLayout = nullptr; m_graphicsPipeline = nullptr; m_renderPass = nullptr; - KRSurface& surface = m_pContext->GetSurfaceInfo(surfaceHandle); - KRDevice& device = m_pContext->GetDeviceInfo(surface.m_deviceHandle); + KRDevice& device = m_pContext->GetDeviceInfo(deviceHandle); strcpy(m_szKey, szKey); @@ -145,7 +144,7 @@ KRPipeline::KRPipeline(KRContext& context, KrSurfaceHandle surfaceHandle, const } VkAttachmentDescription colorAttachment{}; - colorAttachment.format = surface.m_swapChainImageFormat; + colorAttachment.format = swapChainImageFormat; colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT; colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; @@ -199,14 +198,15 @@ KRPipeline::KRPipeline(KRContext& context, KrSurfaceHandle surfaceHandle, const VkViewport viewport{}; viewport.x = 0.0f; viewport.y = 0.0f; - viewport.width = (float)surface.m_swapChainExtent.width; - viewport.height = (float)surface.m_swapChainExtent.height; + viewport.width = static_cast(swapChainWidth); + viewport.height = static_cast(swapChainHeight); viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; VkRect2D scissor{}; scissor.offset = { 0, 0 }; - scissor.extent = surface.m_swapChainExtent; + scissor.extent.width = swapChainWidth; + scissor.extent.height = swapChainHeight; VkPipelineViewportStateCreateInfo viewportState{}; viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; diff --git a/kraken/KRPipeline.h b/kraken/KRPipeline.h index 232f759..06b2b90 100644 --- a/kraken/KRPipeline.h +++ b/kraken/KRPipeline.h @@ -44,7 +44,7 @@ class KRShader; class KRPipeline : public KRContextObject { public: - KRPipeline(KRContext& context, KrSurfaceHandle surfaceHandle, const char* szKey, const std::vector& shaders); + KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, VkFormat swapChainImageFormat, uint32_t swapChainWidth, uint32_t swapChainHeight, const char* szKey, const std::vector& shaders); 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 2dc8856..c467324 100644 --- a/kraken/KRPipelineManager.cpp +++ b/kraken/KRPipelineManager.cpp @@ -62,7 +62,7 @@ KRPipelineManager::~KRPipelineManager() { #endif // ANDROID } -void KRPipelineManager::createPipelines(KrSurfaceHandle surface) +void KRPipelineManager::createPipelines(KRSurface& surface) { { // vulkan_test @@ -70,7 +70,7 @@ void KRPipelineManager::createPipelines(KrSurfaceHandle surface) std::vector shaders; shaders.push_back(m_pContext->getShaderManager()->get(pipeline_name + ".vert", "spv")); shaders.push_back(m_pContext->getShaderManager()->get(pipeline_name + ".frag", "spv")); - KRPipeline* pipeline = new KRPipeline(*m_pContext, surface, pipeline_name.c_str(), shaders); + KRPipeline* pipeline = new KRPipeline(*m_pContext, surface.m_deviceHandle, surface.m_swapChainImageFormat, surface.m_swapChainExtent.width, surface.m_swapChainExtent.height , pipeline_name.c_str(), shaders); std::pair > key; key.first = pipeline_name; m_pipelines[key] = pipeline; diff --git a/kraken/KRPipelineManager.h b/kraken/KRPipelineManager.h index 88d043f..2427f26 100644 --- a/kraken/KRPipelineManager.h +++ b/kraken/KRPipelineManager.h @@ -35,6 +35,7 @@ #include "KRCamera.h" #include "KRDataBlock.h" #include "KRNode.h" +#include "KRSurface.h" using std::map; using std::vector; @@ -51,7 +52,7 @@ class KRPipelineManager : public KRContextObject { public: KRPipelineManager(KRContext &context); virtual ~KRPipelineManager(); - void createPipelines(KrSurfaceHandle surface); + void createPipelines(KRSurface& surface); KRPipeline* get(const char* szKey); 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); diff --git a/kraken/KRSurface.cpp b/kraken/KRSurface.cpp index 6991214..0ba2780 100644 --- a/kraken/KRSurface.cpp +++ b/kraken/KRSurface.cpp @@ -202,6 +202,29 @@ KrResult KRSurface::initialize() } } + KRPipelineManager* pipelineManager = m_pContext->getPipelineManager(); + pipelineManager->createPipelines(*this); + + KRPipeline* testPipeline = pipelineManager->get("vulkan_test"); + m_swapChainFramebuffers.resize(m_swapChainImageViews.size()); + + for (size_t i = 0; i < m_swapChainImageViews.size(); i++) { + VkImageView attachments[] = { m_swapChainImageViews[i] }; + + VkFramebufferCreateInfo framebufferInfo{}; + framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; + framebufferInfo.renderPass = testPipeline->getRenderPass(); + framebufferInfo.attachmentCount = 1; + framebufferInfo.pAttachments = attachments; + framebufferInfo.width = m_swapChainExtent.width; + framebufferInfo.height = m_swapChainExtent.height; + framebufferInfo.layers = 1; + + if (vkCreateFramebuffer(deviceInfo->m_logicalDevice, &framebufferInfo, nullptr, &m_swapChainFramebuffers[i]) != VK_SUCCESS) { + return KR_ERROR_VULKAN_FRAMEBUFFER; + } + } + return KR_SUCCESS; } diff --git a/kraken/public/kraken.h b/kraken/public/kraken.h index c848d13..bea878f 100644 --- a/kraken/public/kraken.h +++ b/kraken/public/kraken.h @@ -49,6 +49,7 @@ typedef enum { KR_ERROR_VULKAN, KR_ERROR_VULKAN_REQUIRED, KR_ERROR_VULKAN_SWAP_CHAIN, + KR_ERROR_VULKAN_FRAMEBUFFER, KR_ERROR_NO_DEVICE, KR_ERROR_SHADER_COMPILE_FAILED, KR_ERROR_UNEXPECTED = 0x10000000,