From da24e134d85c40d977e5c7cc79b2499c82d46030 Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Mon, 6 May 2024 11:35:37 +0200 Subject: [PATCH] Null backend always available --- CMakeLists.txt | 9 +++--- CMakePresets.json | 28 +++++++++-------- hello_imgui_cmake/hello_imgui_build_lib.cmake | 30 +++++++++++-------- .../backend_impls/abstract_runner.cpp | 6 +--- .../null_window_helper.h | 3 -- .../internal/backend_impls/rendering_null.cpp | 3 -- .../internal/backend_impls/rendering_null.h | 3 -- .../internal/backend_impls/runner_factory.cpp | 10 ++----- .../internal/backend_impls/runner_null.h | 3 -- 9 files changed, 42 insertions(+), 53 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d813987e..4ffba3d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,17 +82,18 @@ option(HELLOIMGUI_ADD_APP_WITH_INSTALL "Add cmake install() instructions with he # Platform backends: option(HELLOIMGUI_USE_GLFW3 "Use Glfw3 as a platform backend" OFF) option(HELLOIMGUI_USE_SDL2 "Use Sdl2 as a platform backend" OFF) -option(HELLOIMGUI_USE_NULL "Null platform backend" OFF) # for testing and remote rendering # Rendering backends option(HELLOIMGUI_HAS_OPENGL3 "Use OpenGL3 as a rendering backend" OFF) option(HELLOIMGUI_HAS_METAL "Use Metal as a rendering backend" OFF) option(HELLOIMGUI_HAS_VULKAN "Use Vulkan as a rendering backend" OFF) option(HELLOIMGUI_HAS_DIRECTX11 "Use DirectX11 as a rendering backend" OFF) option(HELLOIMGUI_HAS_DIRECTX12 "Use DirectX12 as a rendering backend" OFF) -option(HELLOIMGUI_HAS_NULL "Null rendering backend" OFF) # for testing and remote rendering -# Null backend: useful for testing, or for remote rendering (will set both rendering and platform backends to null) -option(HELLOIMGUI_NULL_BACKEND "Use Null rendering/platform backend" OFF) +# Headless mode: by default, HelloImGui's cmake tooling will always check that there is at least one +# rendering backend and one platform backend that is not a "Null" backend. +# If you set HELLOIMGUI_HEADLESS, you can disable this check, and compile HelloImGui, +# using only the Null rendering/platform backends. +option(HELLOIMGUI_HEADLESS "Allow headless mode (will use Null rendering/platform backend)" OFF) # # do not remove this line (used by the script that generates the documentation) #------------------------------------------------------------------------------ diff --git a/CMakePresets.json b/CMakePresets.json index 80848259..28eb806a 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -3,7 +3,7 @@ "configurePresets": [ { "name": "build_default_glfw", - "description": "Build with default settings (opengl3 and glfw). You need to create a build directory and run from it", + "description": "Build with default settings (opengl3 and glfw)", "cacheVariables": { "HELLOIMGUI_USE_GLFW3": "ON", "HELLOIMGUI_HAS_OPENGL3": "ON" @@ -11,7 +11,7 @@ }, { "name": "build_default_sdl", - "description": "Build with default settings (opengl3 and sdl). You need to create a build directory and run from it", + "description": "Build with default settings (opengl3 and sdl)", "cacheVariables": { "HELLOIMGUI_USE_SDL2": "ON", "HELLOIMGUI_HAS_OPENGL3": "ON" @@ -19,22 +19,20 @@ }, { "name": "build_all_backends", - "description": "Build with all platform and renderer backends. You need to create a build directory and run from it", + "description": "Build with all platform and renderer backends", "cacheVariables": { "HELLOIMGUI_USE_SDL2": "ON", "HELLOIMGUI_USE_GLFW3": "ON", - "HELLOIMGUI_USE_NULL": "ON", "HELLOIMGUI_HAS_OPENGL3": "ON", "HELLOIMGUI_HAS_METAL": "ON", "HELLOIMGUI_HAS_VULKAN": "ON", "HELLOIMGUI_HAS_DIRECTX11": "ON", - "HELLOIMGUI_HAS_DIRECTX12": "ON", - "HELLOIMGUI_HAS_NULL": "ON" + "HELLOIMGUI_HAS_DIRECTX12": "ON" } }, { "name": "build_vcpkg_default", - "description": "Build with vcpkg default settings (opengl3 + sdl and glfw). You need to create a build directory and run from it", + "description": "Build with vcpkg default settings (opengl3 + sdl and glfw)", "cacheVariables": { "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake", "HELLOIMGUI_USE_GLFW3": "ON", @@ -45,8 +43,7 @@ }, { "name": "build_use_vcpkg_opengl3", - "description": "Build with vcpkg default settings (opengl3 + sdl and glfw). Will build inside build_use_vcpkg_opengl3/", - "binaryDir": "${sourceDir}/build_use_vcpkg_opengl3", + "description": "Build with vcpkg default settings (opengl3 + sdl and glfw)", "cacheVariables": { "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake", "HELLOIMGUI_USE_GLFW3": "ON", @@ -57,8 +54,7 @@ }, { "name": "build_use_vcpkg_metal", - "description": "Build with vcpkg (for metal + sdl and glfw). Will build inside build_use_vcpkg_metal/", - "binaryDir": "${sourceDir}/build_use_vcpkg_metal", + "description": "Build with vcpkg (for metal + sdl and glfw)", "cacheVariables": { "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake", "HELLOIMGUI_USE_GLFW3": "ON", @@ -69,8 +65,7 @@ }, { "name": "build_use_vcpkg_vulkan", - "description": "Build with vcpkg (for vulkan + sdl and glfw). Will build inside build_use_vcpkg_vulkan/", - "binaryDir": "${sourceDir}/build_use_vcpkg_vulkan", + "description": "Build with vcpkg (for vulkan + sdl and glfw)", "cacheVariables": { "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake", "HELLOIMGUI_USE_GLFW3": "ON", @@ -78,6 +73,13 @@ "HELLOIMGUI_HAS_VULKAN": "ON", "HELLOIMGUI_USE_IMGUI_CMAKE_PACKAGE": "ON" } + }, + { + "name": "build_headless", + "description": "Build with headless backend. No GUI will be shown", + "cacheVariables": { + "HELLOIMGUI_HEADLESS": "ON" + } } ] } \ No newline at end of file diff --git a/hello_imgui_cmake/hello_imgui_build_lib.cmake b/hello_imgui_cmake/hello_imgui_build_lib.cmake index d4e47fb4..0316b399 100644 --- a/hello_imgui_cmake/hello_imgui_build_lib.cmake +++ b/hello_imgui_cmake/hello_imgui_build_lib.cmake @@ -34,7 +34,6 @@ function(him_back_available_platform_backends out_var) set(${out_var} HELLOIMGUI_USE_SDL2 HELLOIMGUI_USE_GLFW3 - HELLOIMGUI_USE_NULL PARENT_SCOPE) endfunction() @@ -45,15 +44,10 @@ function(him_back_available_rendering_backends out_var) HELLOIMGUI_HAS_VULKAN HELLOIMGUI_HAS_DIRECTX11 HELLOIMGUI_HAS_DIRECTX12 - HELLOIMGUI_HAS_NULL PARENT_SCOPE) endfunction() function(him_back_parse_legacy_combinations) - if(HELLOIMGUI_NULL_BACKEND) - set(HELLOIMGUI_USE_NULL ON CACHE BOOL "" FORCE) - set(HELLOIMGUI_HAS_NULL ON CACHE BOOL "" FORCE) - endif() if(HELLOIMGUI_USE_SDL_OPENGL3) message(WARNING " HELLOIMGUI_USE_SDL_OPENGL3 is deprecated, @@ -163,6 +157,10 @@ function(him_back_check_if_no_rendering_backend_selected out_var) # will set out endfunction() function(him_back_check_if_no_backend_option_chosen out_var) + if (HELLOIMGUI_HEADLESS) + set(${out_var} OFF PARENT_SCOPE) + return() + endif() him_back_check_if_no_platform_backend_selected(no_platform_selected) him_back_check_if_no_rendering_backend_selected(no_rendering_selected) if(no_platform_selected AND no_rendering_selected) @@ -173,6 +171,10 @@ function(him_back_check_if_no_backend_option_chosen out_var) endfunction() function(him_back_check_at_least_one_rendering_one_platform_backend out_var) + if (HELLOIMGUI_HEADLESS) + set(${out_var} ON PARENT_SCOPE) + return() + endif() him_back_check_if_no_platform_backend_selected(no_platform_selected) him_back_check_if_no_rendering_backend_selected(no_rendering_selected) if(no_platform_selected OR no_rendering_selected) @@ -190,6 +192,11 @@ function(him_back_describe_active_rendering_backends out_description) set(result "${result} ${backend}") endif() endforeach() + + if (HELLOIMGUI_HEADLESS) + set(result "${result} null-headless") + endif() + set(${out_description} ${result} PARENT_SCOPE) endfunction() @@ -201,6 +208,11 @@ function(him_back_describe_active_platform_backends out_description) set(result "${result} ${backend}") endif() endforeach() + + if (HELLOIMGUI_HEADLESS) + set(result "${result} null-headless") + endif() + set(${out_description} ${result} PARENT_SCOPE) endfunction() @@ -1212,9 +1224,6 @@ function(him_main_add_hello_imgui_library) if (HELLOIMGUI_USE_GLFW3) him_use_glfw3_backend(${HELLOIMGUI_TARGET}) endif() - if (HELLOIMGUI_USE_NULL) - target_compile_definitions(${HELLOIMGUI_TARGET} PUBLIC HELLOIMGUI_USE_NULL) - endif() if (HELLOIMGUI_HAS_OPENGL3) him_has_opengl3(${HELLOIMGUI_TARGET}) @@ -1231,9 +1240,6 @@ function(him_main_add_hello_imgui_library) if (HELLOIMGUI_HAS_DIRECTX12) him_has_directx12(${HELLOIMGUI_TARGET}) endif() - if (HELLOIMGUI_HAS_NULL) - target_compile_definitions(${HELLOIMGUI_TARGET} PUBLIC HELLOIMGUI_HAS_NULL) - endif() if (HELLOIMGUI_WITH_NETIMGUI) him_with_netimgui() diff --git a/src/hello_imgui/internal/backend_impls/abstract_runner.cpp b/src/hello_imgui/internal/backend_impls/abstract_runner.cpp index 54ee8500..dd959ba8 100644 --- a/src/hello_imgui/internal/backend_impls/abstract_runner.cpp +++ b/src/hello_imgui/internal/backend_impls/abstract_runner.cpp @@ -649,11 +649,7 @@ void AbstractRunner::InitRenderBackendCallbacks() } else if (params.rendererBackendType == RendererBackendType::Null) { - #ifdef HELLOIMGUI_USE_NULL - mRenderingBackendCallbacks = CreateBackendCallbacks_Null(); - #else - IM_ASSERT(false && "Null backend is not available!"); - #endif + mRenderingBackendCallbacks = CreateBackendCallbacks_Null(); } else { diff --git a/src/hello_imgui/internal/backend_impls/backend_window_helper/null_window_helper.h b/src/hello_imgui/internal/backend_impls/backend_window_helper/null_window_helper.h index c5f7e31c..c9805975 100644 --- a/src/hello_imgui/internal/backend_impls/backend_window_helper/null_window_helper.h +++ b/src/hello_imgui/internal/backend_impls/backend_window_helper/null_window_helper.h @@ -1,5 +1,4 @@ #pragma once -#ifdef HELLOIMGUI_USE_NULL #include "backend_window_helper.h" #include "hello_imgui/internal/backend_impls/null_config.h" @@ -51,5 +50,3 @@ namespace HelloImGui { namespace BackendApi }; }} // namespace HelloImGui { namespace BackendApi - -#endif // #ifdef HELLOIMGUI_USE_NULL diff --git a/src/hello_imgui/internal/backend_impls/rendering_null.cpp b/src/hello_imgui/internal/backend_impls/rendering_null.cpp index b0d28c2d..6eca6da3 100644 --- a/src/hello_imgui/internal/backend_impls/rendering_null.cpp +++ b/src/hello_imgui/internal/backend_impls/rendering_null.cpp @@ -1,4 +1,3 @@ -#ifdef HELLOIMGUI_HAS_NULL #include "rendering_null.h" @@ -25,5 +24,3 @@ namespace HelloImGui } } - -#endif \ No newline at end of file diff --git a/src/hello_imgui/internal/backend_impls/rendering_null.h b/src/hello_imgui/internal/backend_impls/rendering_null.h index c0527fbc..ceb6ab27 100644 --- a/src/hello_imgui/internal/backend_impls/rendering_null.h +++ b/src/hello_imgui/internal/backend_impls/rendering_null.h @@ -1,5 +1,4 @@ #pragma once -#ifdef HELLOIMGUI_HAS_NULL #include "hello_imgui/internal/backend_impls/rendering_callbacks.h" @@ -7,5 +6,3 @@ namespace HelloImGui { RenderingCallbacksPtr CreateBackendCallbacks_Null(); } - -#endif \ No newline at end of file diff --git a/src/hello_imgui/internal/backend_impls/runner_factory.cpp b/src/hello_imgui/internal/backend_impls/runner_factory.cpp index 24b915dd..c746db69 100644 --- a/src/hello_imgui/internal/backend_impls/runner_factory.cpp +++ b/src/hello_imgui/internal/backend_impls/runner_factory.cpp @@ -16,7 +16,7 @@ void ChooseBackendTypesIfSelectedAsFirstAvailable(RunnerParams* runnerParams) runnerParams->platformBackendType = PlatformBackendType::Glfw; #elif defined(HELLOIMGUI_USE_SDL2) runnerParams->platformBackendType = PlatformBackendType::Sdl; - #elif defined(HELLOIMGUI_USE_NULL) + #else runnerParams->platformBackendType = PlatformBackendType::Null; #endif } @@ -33,7 +33,7 @@ void ChooseBackendTypesIfSelectedAsFirstAvailable(RunnerParams* runnerParams) runnerParams->rendererBackendType = RendererBackendType::DirectX11; #elif defined(HELLOIMGUI_HAS_DIRECTX12) runnerParams->rendererBackendType = RendererBackendType::DirectX12; - #elif defined(HELLOIMGUI_HAS_NULL) + #else runnerParams->rendererBackendType = RendererBackendType::Null; #endif } @@ -75,11 +75,7 @@ std::unique_ptr FactorRunner(RunnerParams& params) } else if (params.platformBackendType == PlatformBackendType::Null) { - #ifdef HELLOIMGUI_USE_NULL - return std::make_unique(params); - #else - return nullptr; - #endif + return std::make_unique(params); } else return nullptr; diff --git a/src/hello_imgui/internal/backend_impls/runner_null.h b/src/hello_imgui/internal/backend_impls/runner_null.h index 4e8314c6..1d8fb1db 100644 --- a/src/hello_imgui/internal/backend_impls/runner_null.h +++ b/src/hello_imgui/internal/backend_impls/runner_null.h @@ -1,5 +1,4 @@ #pragma once -#ifdef HELLOIMGUI_USE_NULL #include "hello_imgui/internal/backend_impls/abstract_runner.h" #include "hello_imgui/internal/backend_impls/backend_window_helper/null_window_helper.h" #include "hello_imgui/internal/backend_impls/null_config.h" @@ -43,5 +42,3 @@ namespace HelloImGui }; } // namespace HelloImGui - -#endif // #ifdef HELLOIMGUI_USE_NULL