From a7e5e42a188f9e6ab13706a15e6b50f36f0e00e8 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Sun, 28 Mar 2021 15:13:05 -0700 Subject: Fix backend compiling/loading mechanism --- core/src/Entrypoint/main.cpp | 135 ++++++++++++++++++++++++++++--------------- 1 file changed, 90 insertions(+), 45 deletions(-) (limited to 'core/src/Entrypoint/main.cpp') diff --git a/core/src/Entrypoint/main.cpp b/core/src/Entrypoint/main.cpp index 3adf757..b82214d 100644 --- a/core/src/Entrypoint/main.cpp +++ b/core/src/Entrypoint/main.cpp @@ -13,9 +13,98 @@ #include #include #include +#include #include +#include using namespace std::literals::string_literals; +using namespace std::literals::string_view_literals; + +static std::unique_ptr CreateDefaultBackend() { +#if PLATFORM_WIN32 +# if BUILD_CORE_WITH_DX12_BACKEND + try { + auto backend = std::make_unique(); + return backend; + } catch (const std::exception&) { + } +# elif BUILD_CORE_WITH_DX11_BACKEND + try { + auto backend = std::make_unique(); + return backend; + } catch (const std::exception&) { + } +# elif BUILD_CORE_WITH_VULKAN_BACKEND + try { + auto backend = std::make_unique(); + return backend; + } catch (const std::exception&) { + } +# elif BUILD_CORE_WITH_OPENGL3_BACKEND + try { + auto backend = std::make_unique(); + return backend; + } catch (const std::exception&) { + } +# elif BUILD_CORE_WITH_OPENGL2_BACKEND + try { + auto backend = std::make_unique(); + return backend; + } catch (const std::exception&) { + } +# endif +#elif PLATFORM_MACOS + // We currently only support using metal on macos + backend = std::make_unique(); +#elif PLATFORM_LINUX +# if BUILD_CORE_WITH_VULKAN_BACKEND + try { + auto backend = std::make_unique(); + return backend; + } catch (const std::exception&) { + } +# elif BUILD_CORE_WITH_OPENGL3_BACKEND + try { + auto backend = std::make_unique(); + return backend; + } catch (const std::exception&) { + } +# elif BUILD_CORE_WITH_OPENGL2_BACKEND + try { + auto backend = std::make_unique(); + return backend; + } catch (const std::exception&) { + } +# endif +#endif + + return nullptr; +} + +static std::unique_ptr CreateBackend(std::string_view option) { + if (option == "default") { + return CreateDefaultBackend(); + } else if (option == "opengl2") { + return std::make_unique(); + } else if (option == "opengl3") { + return std::make_unique(); + } else if (option == "vulkan") { + return std::make_unique(); + } else if (option == "dx11") { + return std::make_unique(); + } else if (option == "dx12") { + return std::make_unique(); + } else if (option == "metal") { + return std::make_unique(); + } else { + std::string message; + message += "Unknown backend '"; + message += option; + message += "'.\n"; + throw std::runtime_error(message); + return nullptr; + } +} int main(int argc, char* argv[]) { argparse::ArgumentParser parser; @@ -31,59 +120,15 @@ int main(int argc, char* argv[]) { return -1; } - std::unique_ptr backend; auto backendOption = parser.get("--rendering-backend"); - if (backendOption == "default") { - // TODO better api selection mechanism, use lower tier if higher tier is unavilable instead of bailing -#if PLATFORM_WIN32 -# if IMGUI_INCLUDE_DX12_BACKEND - backend = std::make_unique(); -# elif IMGUI_INCLUDE_DX11_BACKEND - backend = std::make_unique(); -# elif IMGUI_INCLUDE_VULKAN_BACKEND - backend = std::make_unique(); -# elif IMGUI_INCLUDE_OPENGL3_BACKEND - backend = std::make_unique(); -# elif IMGUI_INCLUDE_OPENGL2_BACKEND - backend = std::make_unique(); -# endif -#elif PLATFORM_MACOS - backend = std::make_unique(); -#elif PLATFORM_LINUX -# if IMGUI_INCLUDE_VULKAN_BACKEND - backend = std::make_unique(); -// # elif IMGUI_INCLUDE_OPENGL3_BACKEND -// backend = std::make_unique(); -# elif IMGUI_INCLUDE_OPENGL2_BACKEND - backend = std::make_unique(); -# endif -#endif - } else if (backendOption == "opengl2") { - backend = std::make_unique(); - } else if (backendOption == "opengl3") { - backend = std::make_unique(); - } else if (backendOption == "vulkan") { - backend = std::make_unique(); - } else if (backendOption == "dx11") { - backend = std::make_unique(); - } else if (backendOption == "dx12") { - backend = std::make_unique(); - } else if (backendOption == "metal") { - backend = std::make_unique(); - } else { - std::cout << "Unknown backend '" << backendOption << "'.\n"; - return -1; - } + auto backend = CreateBackend(backendOption); auto& io = ImGui::GetIO(); io.Fonts->AddFontFromFileTTF("fonts/NotoSansSC-Regular.otf", 18, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon()); auto window = backend->GetWindow(); - bool showDemo = true; while (!glfwWindowShouldClose(window)) { backend->BeginFrame(); - if (showDemo) - ImGui::ShowDemoWindow(&showDemo); backend->EndFrame(); } -- cgit v1.2.3-70-g09d2