From 2922e74f07c87efcb0ce7280fb84eff977dc01ee Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Tue, 2 Feb 2021 16:14:15 +0100 Subject: [PATCH] Update platform creation code --- openVulkanoCpp/Host/GLFW/PlatformGLFW.cpp | 3 ++ openVulkanoCpp/Host/PlatformProducer.cpp | 62 ++++++++++++++++++----- openVulkanoCpp/Host/PlatformProducer.hpp | 23 +++++++++ openVulkanoCpp/Vulkan/Renderer.cpp | 5 +- 4 files changed, 79 insertions(+), 14 deletions(-) diff --git a/openVulkanoCpp/Host/GLFW/PlatformGLFW.cpp b/openVulkanoCpp/Host/GLFW/PlatformGLFW.cpp index 5c5dd40..3a75e15 100644 --- a/openVulkanoCpp/Host/GLFW/PlatformGLFW.cpp +++ b/openVulkanoCpp/Host/GLFW/PlatformGLFW.cpp @@ -6,6 +6,7 @@ #include "PlatformGLFW.hpp" #include "WindowGLFW.hpp" +#include "Base/PlatformEnums.hpp" #include namespace openVulkanoCpp::GLFW @@ -55,4 +56,6 @@ namespace openVulkanoCpp::GLFW windows.emplace_back(std::move(window)); return windowPtr; } + + PlatformProducerRegistration platformRegistration(RenderAPI::Vulkan); } \ No newline at end of file diff --git a/openVulkanoCpp/Host/PlatformProducer.cpp b/openVulkanoCpp/Host/PlatformProducer.cpp index 0605883..9b5c4b9 100644 --- a/openVulkanoCpp/Host/PlatformProducer.cpp +++ b/openVulkanoCpp/Host/PlatformProducer.cpp @@ -5,32 +5,68 @@ */ #include "PlatformProducer.hpp" -#include "GLFW/PlatformGLFW.hpp" #include "Base/Logger.hpp" -#include "Vulkan/Renderer.hpp" #include +#include namespace openVulkanoCpp { + namespace + { + struct PlatformRegistry + { + static PlatformRegistry& GetInstance() + { + static PlatformRegistry instance; + return instance; + } + + std::map> platformMap; + std::map> rendererMap; + + void RegisterPlatform(RenderAPI renderApi, std::function createMethod) + { + platformMap[renderApi] = createMethod; + } + + void RegisterRenderer(RenderAPI renderApi, std::function createMethod) + { + rendererMap[renderApi] = createMethod; + } + }; + } + + void PlatformProducer::RegisterPlatformProducer(RenderAPI renderApi, std::function createMethod) + { + PlatformRegistry::GetInstance().RegisterPlatform(renderApi, createMethod); + } + + void PlatformProducer::RegisterRendererProducer(RenderAPI renderApi, std::function createMethod) + { + PlatformRegistry::GetInstance().RegisterRenderer(renderApi, createMethod); + } + IRenderer* PlatformProducer::CreateRenderManager(RenderAPI renderApi) { - switch (renderApi) + auto& rMap = PlatformRegistry::GetInstance().rendererMap; + auto renderProducer = rMap.find(renderApi); + if (renderProducer == rMap.end()) { - case RenderAPI::Vulkan: return new Vulkan::Renderer(); - default: - Logger::RENDER->error("Unsupported render api requested! Requested {}", renderApi.ToString()); - throw std::runtime_error("Unsupported render api requested!"); + Logger::RENDER->error("Unsupported render api requested! Requested {}", renderApi.ToString()); + throw std::runtime_error("Unsupported render api requested!"); } + return renderProducer->second(); } IPlatform* PlatformProducer::CreatePlatform(RenderAPI renderApi) { - switch (renderApi) + auto& pMap = PlatformRegistry::GetInstance().platformMap; + auto platformProducer = pMap.find(renderApi); + if (platformProducer == pMap.end()) { - case RenderAPI::Vulkan: return new GLFW::PlatformGLFW(); - default: - Logger::MANAGER->error("Unsupported render api requested! Requested {}", renderApi.ToString()); - throw std::runtime_error("Unsupported render api requested!"); + Logger::MANAGER->error("No platform for render api {}!", renderApi.ToString()); + return nullptr; } + return platformProducer->second(); } -} \ No newline at end of file +} diff --git a/openVulkanoCpp/Host/PlatformProducer.hpp b/openVulkanoCpp/Host/PlatformProducer.hpp index 084e9b9..bd7834e 100644 --- a/openVulkanoCpp/Host/PlatformProducer.hpp +++ b/openVulkanoCpp/Host/PlatformProducer.hpp @@ -7,6 +7,7 @@ #pragma once #include "Base/PlatformEnums.hpp" +#include namespace openVulkanoCpp { @@ -35,5 +36,27 @@ namespace openVulkanoCpp * \throws std::runtime_error if the render api is not supported */ static IPlatform* CreatePlatform(RenderAPI renderApi); + + static void RegisterPlatformProducer(RenderAPI renderApi, std::function createMethod); + + static void RegisterRendererProducer(RenderAPI renderApi, std::function createMethod); + }; + + template + struct PlatformProducerRegistration + { + PlatformProducerRegistration(RenderAPI renderApi) + { + PlatformProducer::RegisterPlatformProducer(renderApi, []() -> IPlatform* { new PLATFORM_CLASS(); }); + } + }; + + template + struct RendererProducerRegistration + { + RendererProducerRegistration(RenderAPI renderApi) + { + PlatformProducer::RegisterRendererProducer(renderApi, []() -> IRenderer* { new RENDERER_CLASS(); }); + } }; } diff --git a/openVulkanoCpp/Vulkan/Renderer.cpp b/openVulkanoCpp/Vulkan/Renderer.cpp index 10428d1..419ea58 100644 --- a/openVulkanoCpp/Vulkan/Renderer.cpp +++ b/openVulkanoCpp/Vulkan/Renderer.cpp @@ -8,6 +8,7 @@ #include "Scene/VulkanGeometry.hpp" #include "Scene/VulkanNode.hpp" #include "Scene/VulkanShader.hpp" +#include "Host/PlatformProducer.hpp" #include namespace openVulkanoCpp::Vulkan @@ -170,4 +171,6 @@ namespace openVulkanoCpp::Vulkan } cmdHelper->cmdBuffer.end(); } -} \ No newline at end of file + + RendererProducerRegistration rendererRegistration(RenderAPI::Vulkan); +}