From 12e476dc81839e572bacc532536efe49676167f3 Mon Sep 17 00:00:00 2001 From: Kearwood Gilbert Date: Thu, 12 Aug 2021 00:45:57 -0700 Subject: [PATCH] Surface handles are now indirect. --- kraken/KRContext.cpp | 19 ++++++++++++++++--- kraken/KRContext.h | 11 ++++++----- kraken/KREngine-common.h | 3 +++ kraken/KRPipeline.cpp | 13 +++++++------ kraken/KRPipeline.h | 2 +- kraken/KRPipelineManager.cpp | 4 ++-- kraken/KRPipelineManager.h | 2 +- 7 files changed, 36 insertions(+), 18 deletions(-) diff --git a/kraken/KRContext.cpp b/kraken/KRContext.cpp index 8f41b38..9fb7528 100755 --- a/kraken/KRContext.cpp +++ b/kraken/KRContext.cpp @@ -60,6 +60,7 @@ KRContext::KRContext(const KrInitializeInfo* initializeInfo) , m_vulkanInstance(VK_NULL_HANDLE) , m_resourceMapSize(initializeInfo->resourceMapSize) , m_topDeviceHandle(0) + , m_topSurfaceHandle(0) { m_resourceMap = (KRResource **)malloc(sizeof(KRResource*) * m_resourceMapSize); memset(m_resourceMap, 0, m_resourceMapSize * sizeof(KRResource*)); @@ -808,6 +809,7 @@ KRContext::destroySurfaces() } m_surfaces.clear(); + m_surfaceHandleMap.clear(); } void @@ -1068,9 +1070,12 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW } } - m_surfaces.insert(std::pair(createWindowSurfaceInfo->surfaceHandle, info)); + KrSurfaceHandle surfaceHandle = ++m_topSurfaceHandle; + m_surfaces.insert(std::pair(surfaceHandle, info)); - m_pPipelineManager->createPipelines(deviceInfo->logicalDevice); // TODO - Support multiple surfaces. Device needs to be passed in. + m_surfaceHandleMap.insert(std::pair(createWindowSurfaceInfo->surfaceHandle, surfaceHandle)); + + m_pPipelineManager->createPipelines(surfaceHandle); return KR_SUCCESS; #else @@ -1087,7 +1092,15 @@ KrResult KRContext::deleteWindowSurface(const KrDeleteWindowSurfaceInfo* deleteW if (m_vulkanInstance == VK_NULL_HANDLE) { return KR_ERROR_VULKAN_REQUIRED; } - auto itr = m_surfaces.find(deleteWindowSurfaceInfo->surfaceHandle); + + auto handleItr = m_surfaceHandleMap.find(deleteWindowSurfaceInfo->surfaceHandle); + if (handleItr == m_surfaceHandleMap.end()) { + return KR_ERROR_NOT_FOUND; + } + KrSurfaceHandle surfaceHandle = (*handleItr).second; + m_surfaceHandleMap.erase(handleItr); + + auto itr = m_surfaces.find(surfaceHandle); if (itr == m_surfaces.end()) { return KR_ERROR_NOT_FOUND; } diff --git a/kraken/KRContext.h b/kraken/KRContext.h index 7511eb1..e4baf6e 100755 --- a/kraken/KRContext.h +++ b/kraken/KRContext.h @@ -129,7 +129,7 @@ public: static void activateStreamerContext(); static void activateRenderContext(); - typedef int KrDeviceHandle; + typedef struct { VkPhysicalDevice device; VkDevice logicalDevice; @@ -141,7 +141,6 @@ public: VkQueue computeQueue; } DeviceInfo; - typedef int KrSurfaceHandle; typedef struct { KrSurfaceHandle surfaceHandle; KrDeviceHandle deviceHandle; @@ -209,9 +208,6 @@ private: unordered_multimap m_resources; - - unordered_map m_surfaces; - std::thread m_presentationThread; void presentationThreadFunc(); std::atomic m_stop; @@ -219,6 +215,11 @@ private: unordered_map m_devices; KrDeviceHandle m_topDeviceHandle; + + unordered_map m_surfaces; + KrDeviceHandle m_topSurfaceHandle; + + unordered_map m_surfaceHandleMap; }; #endif diff --git a/kraken/KREngine-common.h b/kraken/KREngine-common.h index a7a5c1c..6132ed9 100755 --- a/kraken/KREngine-common.h +++ b/kraken/KREngine-common.h @@ -234,6 +234,9 @@ typedef enum { STREAM_LEVEL_IN_HQ } kraken_stream_level; +typedef int KrDeviceHandle; +typedef int KrSurfaceHandle; + #include "KRBehavior.h" #endif diff --git a/kraken/KRPipeline.cpp b/kraken/KRPipeline.cpp index 5ce58e6..94fdd59 100644 --- a/kraken/KRPipeline.cpp +++ b/kraken/KRPipeline.cpp @@ -109,14 +109,15 @@ 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) +KRPipeline::KRPipeline(KRContext& context, KrSurfaceHandle surfaceHandle, 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; - KRContext::SurfaceInfo& surface = m_pContext->GetSurfaceInfo(1); // TODO - Support multiple surfaces + KRContext::SurfaceInfo& surface = m_pContext->GetSurfaceInfo(surfaceHandle); + KRContext::DeviceInfo& device = m_pContext->GetDeviceInfo(surface.deviceHandle); strcpy(m_szKey, szKey); @@ -127,7 +128,7 @@ KRPipeline::KRPipeline(KRContext& context, VkDevice& device, const char* szKey, for (KRShader* shader : shaders) { VkShaderModule shaderModule; - if (!shader->createShaderModule(device, shaderModule)) { + if (!shader->createShaderModule(device.logicalDevice, shaderModule)) { // failed! TODO - Error handling } VkPipelineShaderStageCreateInfo& stageInfo = stages[stage_count++]; @@ -169,7 +170,7 @@ KRPipeline::KRPipeline(KRContext& context, VkDevice& device, const char* szKey, renderPassInfo.subpassCount = 1; renderPassInfo.pSubpasses = &subpass; - if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &m_renderPass) != VK_SUCCESS) { + if (vkCreateRenderPass(device.logicalDevice, &renderPassInfo, nullptr, &m_renderPass) != VK_SUCCESS) { // failed! TODO - Error handling } @@ -254,7 +255,7 @@ KRPipeline::KRPipeline(KRContext& context, VkDevice& device, const char* szKey, pipelineLayoutInfo.pushConstantRangeCount = 0; pipelineLayoutInfo.pPushConstantRanges = nullptr; - if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &m_pipelineLayout) != VK_SUCCESS) { + if (vkCreatePipelineLayout(device.logicalDevice, &pipelineLayoutInfo, nullptr, &m_pipelineLayout) != VK_SUCCESS) { // failed! TODO - Error handling } @@ -276,7 +277,7 @@ KRPipeline::KRPipeline(KRContext& context, VkDevice& device, const char* szKey, pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; pipelineInfo.basePipelineIndex = -1; - if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &m_graphicsPipeline) != VK_SUCCESS) { + if (vkCreateGraphicsPipelines(device.logicalDevice, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &m_graphicsPipeline) != VK_SUCCESS) { // Failed! TODO - Error handling } } diff --git a/kraken/KRPipeline.h b/kraken/KRPipeline.h index 7ae8410..f539c31 100644 --- a/kraken/KRPipeline.h +++ b/kraken/KRPipeline.h @@ -44,7 +44,7 @@ class KRShader; class KRPipeline : public KRContextObject { public: - KRPipeline(KRContext& context, VkDevice& device, const char* szKey, const std::vector& shaders); + KRPipeline(KRContext& context, KrSurfaceHandle surfaceHandle, 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 f816b9f..89df35f 100644 --- a/kraken/KRPipelineManager.cpp +++ b/kraken/KRPipelineManager.cpp @@ -62,7 +62,7 @@ KRPipelineManager::~KRPipelineManager() { #endif // ANDROID } -void KRPipelineManager::createPipelines(VkDevice& device) +void KRPipelineManager::createPipelines(KrSurfaceHandle surface) { { // simple_blit @@ -70,7 +70,7 @@ void KRPipelineManager::createPipelines(VkDevice& device) 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); + KRPipeline* pipeline = new KRPipeline(*m_pContext, surface, 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 b71e337..1923166 100644 --- a/kraken/KRPipelineManager.h +++ b/kraken/KRPipelineManager.h @@ -51,7 +51,7 @@ class KRPipelineManager : public KRContextObject { public: KRPipelineManager(KRContext &context); virtual ~KRPipelineManager(); - void createPipelines(VkDevice& device); + void createPipelines(KrSurfaceHandle surface); 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);