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/Common.cpp | 7 ++- core/src/Entrypoint/Common.hpp | 1 - core/src/Entrypoint/OpenGL2.cpp | 37 ++++++++--- core/src/Entrypoint/OpenGL3.cpp | 37 ++++++++--- core/src/Entrypoint/OpenGL3.hpp | 1 - core/src/Entrypoint/main.cpp | 135 ++++++++++++++++++++++++++-------------- 6 files changed, 149 insertions(+), 69 deletions(-) (limited to 'core/src') diff --git a/core/src/Entrypoint/Common.cpp b/core/src/Entrypoint/Common.cpp index 62baf9d..c949830 100644 --- a/core/src/Entrypoint/Common.cpp +++ b/core/src/Entrypoint/Common.cpp @@ -1,11 +1,14 @@ #include "Common.hpp" +#include #include +#include + GLFWwindow* RenderingBackend::GetWindow() const { - return mWindow; + return mWindow; } void RenderingBackend::GlfwErrorCallback(int error, const char* message) { - std::cerr << "GLFW Error " << error << ": " << message << "\n"; + std::cerr << "GLFW Error " << error << ": " << message << "\n"; } diff --git a/core/src/Entrypoint/Common.hpp b/core/src/Entrypoint/Common.hpp index 2a4dcbf..216c885 100644 --- a/core/src/Entrypoint/Common.hpp +++ b/core/src/Entrypoint/Common.hpp @@ -1,7 +1,6 @@ #pragma once #include - #include class RenderingBackend { diff --git a/core/src/Entrypoint/OpenGL2.cpp b/core/src/Entrypoint/OpenGL2.cpp index de13a02..216399e 100644 --- a/core/src/Entrypoint/OpenGL2.cpp +++ b/core/src/Entrypoint/OpenGL2.cpp @@ -1,15 +1,17 @@ #include "OpenGL2.hpp" -#include +#if BUILD_CORE_WITH_OPENGL2_BACKEND +# include +# include +# include +# include +# include +# include -#include -#include -#include -#include -#include +# define IMGUI_IMPL_OPENGL_LOADER_GLAD +# include OpenGL2Backend::OpenGL2Backend() { -#if IMGUI_INCLUDE_OPENGL2_BACKEND glfwSetErrorCallback(GlfwErrorCallback); if (!glfwInit()) { throw std::runtime_error("Failed to initialize GLFW."); @@ -31,9 +33,6 @@ OpenGL2Backend::OpenGL2Backend() { ImGui_ImplGlfw_InitForOpenGL(mWindow, true); ImGui_ImplOpenGL2_Init(); -#else - throw std::runtime_error("Backend opengl2 is not available in this build.\n"); -#endif } OpenGL2Backend::~OpenGL2Backend() { @@ -68,3 +67,21 @@ void OpenGL2Backend::EndFrame() { glfwMakeContextCurrent(mWindow); glfwSwapBuffers(mWindow); } + +#else // ^^ BUILD_CORE_WITH_OPENGL2_BACKEND | !BUILD_CORE_WITH_OPENGL2_BACKEND vv +# include + +OpenGL2Backend::OpenGL2Backend() { + throw std::runtime_error("Backend opengl2 is not available in this build.\n"); +} + +OpenGL2Backend::~OpenGL2Backend() { +} + +void OpenGL2Backend::BeginFrame() { +} + +void OpenGL2Backend::EndFrame() { +} + +#endif diff --git a/core/src/Entrypoint/OpenGL3.cpp b/core/src/Entrypoint/OpenGL3.cpp index c1f66d5..7d5cae1 100644 --- a/core/src/Entrypoint/OpenGL3.cpp +++ b/core/src/Entrypoint/OpenGL3.cpp @@ -1,15 +1,17 @@ #include "OpenGL3.hpp" -#include +#if BUILD_CORE_WITH_OPENGL3_BACKEND +# include +# include +# include +# include +# include +# include -#include -#include -#include -#include -#include +# define IMGUI_IMPL_OPENGL_LOADER_GLAD +# include OpenGL3Backend::OpenGL3Backend() { -#if IMGUI_INCLUDE_OPENGL3_BACKEND glfwSetErrorCallback(GlfwErrorCallback); if (!glfwInit()) { throw std::runtime_error("Failed to initialize GLFW."); @@ -47,9 +49,6 @@ OpenGL3Backend::OpenGL3Backend() { ImGui_ImplGlfw_InitForOpenGL(mWindow, true); ImGui_ImplOpenGL3_Init(glslVersion); -#else - throw std::runtime_error("Backend opengl3 is not available in this build.\n"); -#endif } OpenGL3Backend::~OpenGL3Backend() { @@ -83,3 +82,21 @@ void OpenGL3Backend::EndFrame() { glfwSwapBuffers(mWindow); } + +#else // ^^ BUILD_CORE_WITH_OPENGL3_BACKEND | !BUILD_CORE_WITH_OPENGL3_BACKEND vv +# include + +OpenGL3Backend::OpenGL3Backend() { + throw std::runtime_error("Backend opengl3 is not available in this build.\n"); +} + +OpenGL3Backend::~OpenGL3Backend() { +} + +void OpenGL3Backend::BeginFrame() { +} + +void OpenGL3Backend::EndFrame() { +} + +#endif diff --git a/core/src/Entrypoint/OpenGL3.hpp b/core/src/Entrypoint/OpenGL3.hpp index 29086a2..52978fa 100644 --- a/core/src/Entrypoint/OpenGL3.hpp +++ b/core/src/Entrypoint/OpenGL3.hpp @@ -3,7 +3,6 @@ #include "Entrypoint/Common.hpp" #include - #include class OpenGL3Backend : public RenderingBackend { 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