diff --git a/kraken/KRContext.cpp b/kraken/KRContext.cpp index d0fc9ff..fd8c880 100755 --- a/kraken/KRContext.cpp +++ b/kraken/KRContext.cpp @@ -1162,6 +1162,8 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW } } + m_pPipelineManager->createPipelines(info.logicalDevice); // TODO - Support multiple surfaces. Device needs to be passed in. + return KR_SUCCESS; #else // Not implemented for this platform @@ -1214,4 +1216,15 @@ void KRContext::presentationThreadFunc() void KRContext::renderFrame() { +} + +KRContext::SurfaceInfo& KRContext::GetSurfaceInfo(size_t index) +{ + assert(index < m_surfaces.size()); + return m_surfaces[index]; +} + +size_t KRContext::GetSurfaceCount() const +{ + return m_surfaces.size(); } \ No newline at end of file diff --git a/kraken/KRContext.h b/kraken/KRContext.h index bb87ab2..e02b5a2 100755 --- a/kraken/KRContext.h +++ b/kraken/KRContext.h @@ -128,6 +128,28 @@ public: static void activateStreamerContext(); static void activateRenderContext(); + + typedef struct { + KrSurfaceHandle surfaceHandle; + VkSurfaceKHR surface; + VkPhysicalDevice device; + VkDevice logicalDevice; + VkPhysicalDeviceProperties deviceProperties; + VkPhysicalDeviceFeatures deviceFeatures; + VkQueue graphicsQueue; + VkQueue presentQueue; + VkSwapchainKHR swapChain; + std::vector swapChainImages; + VkFormat swapChainImageFormat; + VkExtent2D swapChainExtent; + std::vector swapChainImageViews; +#ifdef WIN32 + HWND hWnd; +#endif + } SurfaceInfo; + + SurfaceInfo& GetSurfaceInfo(size_t index); + size_t GetSurfaceCount() const; #if TARGET_OS_MAC static void attachToView(void *view); @@ -177,24 +199,7 @@ private: void destroySurfaces(); unordered_multimap m_resources; - typedef struct { - KrSurfaceHandle surfaceHandle; - VkSurfaceKHR surface; - VkPhysicalDevice device; - VkDevice logicalDevice; - VkPhysicalDeviceProperties deviceProperties; - VkPhysicalDeviceFeatures deviceFeatures; - VkQueue graphicsQueue; - VkQueue presentQueue; - VkSwapchainKHR swapChain; - std::vector swapChainImages; - VkFormat swapChainImageFormat; - VkExtent2D swapChainExtent; - std::vector swapChainImageViews; -#ifdef WIN32 - HWND hWnd; -#endif - } SurfaceInfo; + unordered_map m_surfaces; std::thread m_presentationThread; void presentationThreadFunc(); diff --git a/kraken/KRPipeline.cpp b/kraken/KRPipeline.cpp index 4e747cf..a3e7c99 100644 --- a/kraken/KRPipeline.cpp +++ b/kraken/KRPipeline.cpp @@ -108,6 +108,41 @@ const char *KRPipeline::KRENGINE_UNIFORM_NAMES[] = { "fade_color", // KRENGINE_UNIFORM_FADE_COLOR }; +KRPipeline::KRPipeline(KRContext& context, VkDevice& device, const char* szKey, const std::vector& shaders) + : KRContextObject(context) + , m_iProgram(0) // not used for Vulkan +{ + strcpy(m_szKey, szKey); + + const int kMaxStages = 4; + VkPipelineShaderStageCreateInfo stages[kMaxStages]; + std::vector stageNames; + memset(static_cast(stages), 0, sizeof(VkPipelineShaderStageCreateInfo) * kMaxStages); + size_t stage_count = 0; + + for (KRShader* shader : shaders) { + VkShaderModule shaderModule; + if (!shader->createShaderModule(device, shaderModule)) { + // failed! TODO - Error handling + } + VkPipelineShaderStageCreateInfo& stageInfo = stages[stage_count++]; + stageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; + if (shader->getSubExtension().compare("vert") == 0) { + stageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT; + } else if (shader->getSubExtension().compare("frag") == 0) { + stageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT; + } else { + // failed! TODO - Error handling + } + stageInfo.module = shaderModule; + const std::string& stageName = stageNames.emplace_back(shader->getName()); + + + stageInfo.pName = stageName.c_str(); + } + // TODO - WIP, need to complete +} + KRPipeline::KRPipeline(KRContext &context, char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource) : KRContextObject(context) { strcpy(m_szKey, szKey); diff --git a/kraken/KRPipeline.h b/kraken/KRPipeline.h index 5d3b1ae..4ba8e31 100644 --- a/kraken/KRPipeline.h +++ b/kraken/KRPipeline.h @@ -40,8 +40,11 @@ #include "KRNode.h" #include "KRViewport.h" +class KRShader; + class KRPipeline : public KRContextObject { public: + KRPipeline(KRContext& context, VkDevice& device, 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; @@ -119,18 +122,6 @@ public: KRENGINE_UNIFORM_FADE_COLOR, KRENGINE_NUM_UNIFORMS }; - /* - typedef enum { - KRENGINE_UNIFORM_TYPE_UNKNOWN, - KRENGINE_UNIFORM_TYPE_FLOAT, - KRENGINE_UNIFORM_TYPE_INT, - KRENGINE_UNIFORM_TYPE_VECTOR2, - KRENGINE_UNIFORM_TYPE_VECTOR3, - KRENGINE_UNIFORM_TYPE_VECTOR4, - KRENGINE_UNIFORM_TYPE_MAT4 - } uniform_type_t; - uniform_type_t m_uniform_type[KRENGINE_NUM_UNIFORMS]; - */ static const char *KRENGINE_UNIFORM_NAMES[]; GLint m_uniforms[KRENGINE_NUM_UNIFORMS]; diff --git a/kraken/KRPipelineManager.cpp b/kraken/KRPipelineManager.cpp index a648a8d..f816b9f 100644 --- a/kraken/KRPipelineManager.cpp +++ b/kraken/KRPipelineManager.cpp @@ -62,6 +62,21 @@ KRPipelineManager::~KRPipelineManager() { #endif // ANDROID } +void KRPipelineManager::createPipelines(VkDevice& device) +{ + { + // simple_blit + std::string pipeline_name = "simple_blit"; + 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, device, pipeline_name.c_str(), shaders); + std::pair > key; + key.first = pipeline_name; + m_pipelines[key] = pipeline; + } +} + KRPipeline *KRPipelineManager::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) { diff --git a/kraken/KRPipelineManager.h b/kraken/KRPipelineManager.h index 16bb01f..b71e337 100644 --- a/kraken/KRPipelineManager.h +++ b/kraken/KRPipelineManager.h @@ -51,6 +51,7 @@ class KRPipelineManager : public KRContextObject { public: KRPipelineManager(KRContext &context); virtual ~KRPipelineManager(); + void createPipelines(VkDevice& device); 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/KRShader.cpp b/kraken/KRShader.cpp index d9ac35d..fae4d41 100644 --- a/kraken/KRShader.cpp +++ b/kraken/KRShader.cpp @@ -35,6 +35,7 @@ KRShader::KRShader(KRContext &context, std::string name, std::string extension) { m_pData = new KRDataBlock(); m_extension = extension; + m_subExtension = KRResource::GetFileExtension(name); } KRShader::KRShader(KRContext &context, std::string name, std::string extension, KRDataBlock *data) : KRResource(context, name) @@ -53,6 +54,11 @@ std::string KRShader::getExtension() return m_extension; } +std::string& KRShader::getSubExtension() +{ + return m_subExtension; +} + bool KRShader::save(KRDataBlock &data) { data.append(*m_pData); @@ -73,8 +79,9 @@ bool KRShader::createShaderModule(VkDevice& device, VkShaderModule& module) createInfo.codeSize = m_pData->getSize(); createInfo.pCode = reinterpret_cast(m_pData->getStart()); - VkShaderModule shaderModule; - if (vkCreateShaderModule(device, &createInfo, nullptr, &module) != VK_SUCCESS) { + VkResult result = vkCreateShaderModule(device, &createInfo, nullptr, &module); + + if(result != VK_SUCCESS) { success = false; } m_pData->unlock(); diff --git a/kraken/KRShader.h b/kraken/KRShader.h index edeba84..80e4d11 100644 --- a/kraken/KRShader.h +++ b/kraken/KRShader.h @@ -45,6 +45,7 @@ public: virtual ~KRShader(); virtual std::string getExtension(); + std::string& getSubExtension(); bool createShaderModule(VkDevice& device, VkShaderModule& module); @@ -55,6 +56,7 @@ public: private: std::string m_extension; + std::string m_subExtension; KRDataBlock *m_pData; }; diff --git a/kraken/KRShaderManager.cpp b/kraken/KRShaderManager.cpp index 0964bae..05b74eb 100644 --- a/kraken/KRShaderManager.cpp +++ b/kraken/KRShaderManager.cpp @@ -257,6 +257,8 @@ bool KRShaderManager::compileAll(KRBundle* outputBundle, KRUnknown* logResource) glslang::TShader::ForbidIncluder includer; glslang::TShader vertShader(EShLangVertex); glslang::TShader fragShader(EShLangFragment); + vertShader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_5); + fragShader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_5); glslang::TProgram program; // this must be declared after the TShader's to ensure it is deallocated before the TShader's if (vertSource) { vertSource->getData()->lock(); diff --git a/standard_assets/shaders/sprite.frag b/standard_assets/shaders/sprite.frag index 50008f1..fd92352 100644 --- a/standard_assets/shaders/sprite.frag +++ b/standard_assets/shaders/sprite.frag @@ -38,8 +38,6 @@ layout (binding = 1) uniform sampler2D diffuseTexture; layout (location = 0) in vec2 textureCoordinate; layout (location = 0) out vec4 colorOut; -in mediump vec2 texCoord; - void main() { - colorOut = vec4(vec3(texture(diffuseTexture, texCoord)), 1.0) * material.alpha; + colorOut = vec4(vec3(texture(diffuseTexture, textureCoordinate)), 1.0) * material.alpha; }