Skip to content

Commit

Permalink
Work / possible multiple renderer backends
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Feb 2, 2024
1 parent 6264f1c commit 3cbfbb3
Show file tree
Hide file tree
Showing 21 changed files with 709 additions and 438 deletions.
34 changes: 0 additions & 34 deletions hello_imgui_cmake/hello_imgui_build_lib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -711,39 +711,6 @@ function(him_has_directx12 target)
endfunction()


###################################################################################################
# Check only one rendering backend is selected: API = him_check_only_one_backend_selected
###################################################################################################
function(him_check_only_one_rendering_backend_selected)
# Only one of HELLOIMGUI_HAS_OPENGL, HELLOIMGUI_HAS_METAL, HELLOIMGUI_HAS_VULKAN can be ON
# count selected rendering backends
set(selected_backends 0)
if (HELLOIMGUI_HAS_OPENGL)
math(EXPR selected_backends "${selected_backends} + 1")
endif()
if (HELLOIMGUI_HAS_METAL)
math(EXPR selected_backends "${selected_backends} + 1")
endif()
if (HELLOIMGUI_HAS_VULKAN)
math(EXPR selected_backends "${selected_backends} + 1")
endif()
if (HELLOIMGUI_HAS_DIRECTX11)
math(EXPR selected_backends "${selected_backends} + 1")
endif()
if (HELLOIMGUI_HAS_DIRECTX12)
math(EXPR selected_backends "${selected_backends} + 1")
endif()
# selected_backends should be 1
if (selected_backends EQUAL 0)
message(FATAL_ERROR "HelloImGui: no rendering backend selected")
endif()
if (NOT selected_backends EQUAL 1)
message(FATAL_ERROR "HelloImGui: only one of HELLOIMGUI_HAS_OPENGL, HELLOIMGUI_HAS_METAL, HELLOIMGUI_HAS_VULKAN, HELLOIMGUI_HAS_DIRECTX12 can be ON")
endif()
endfunction()



###################################################################################################
# SDL platform backend: API = him_use_sdl2_backend
###################################################################################################
Expand Down Expand Up @@ -1095,7 +1062,6 @@ function(him_main_add_hello_imgui_library)
target_compile_definitions(${HELLOIMGUI_TARGET} PUBLIC HELLOIMGUI_USE_SDL_DIRECTX12)
endif ()

him_check_only_one_rendering_backend_selected()
him_add_apple_options()
him_add_linux_options()
him_add_windows_options()
Expand Down
46 changes: 43 additions & 3 deletions src/hello_imgui/doc_params.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,17 @@ struct RunnerParams
// These pointers will be filled when the application starts
BackendPointers backendPointers;

// `backendType`: _enum BackendType, default=BackendType::FirstAvailable_
// `backendType`: _enum BackendType, default=PlatformBackendType::FirstAvailable_
// Select the wanted platform backend type between `Sdl`, `Glfw`.
// if `FirstAvailable`, Glfw will be preferred over Sdl when both are available.
// Only useful when multiple backend are compiled and available.
BackendType backendType = BackendType::FirstAvailable;
PlatformBackendType backendType = PlatformBackendType::FirstAvailable;

// `rendererBackendType`: _enum RendererBackendType, default=RendererBackendType::FirstAvailable_
// Select the wanted rendering backend type between `OpenGL3`, `Metal`, `Vulkan`, `DirectX11`, `DirectX12`.
// if `FirstAvailable`, it will be selected in the order of preference mentioned above.
// Only useful when multiple rendering backend are compiled and available.
RendererBackendType rendererBackendType = RendererBackendType::FirstAvailable;

// `rendererBackendOptions`: _see renderer_backend_options.h_
// Options for the renderer backend
Expand Down Expand Up @@ -181,6 +188,39 @@ struct RunnerParams
};
```

### Backend selection


```cpp

// You can select the platform backend type (SDL, GLFW) and the rendering backend type
// via RunnerParams.backendType and RunnerParams.rendererBackendType.

// Platform backend type (SDL, GLFW)
// They are listed in the order of preference when FirstAvailable is selected.
enum class PlatformBackendType
{
FirstAvailable,
Glfw,
Sdl,
};

using BackendType = PlatformBackendType; // for backward compatibility

// Rendering backend type (OpenGL3, Metal, Vulkan, DirectX11, DirectX12)
// They are listed in the order of preference when FirstAvailable is selected.
enum class RendererBackendType
{
FirstAvailable,
OpenGL3,
Metal,
Vulkan,
DirectX11,
DirectX12,
};

```


# Fps Idling

Expand All @@ -203,7 +243,7 @@ struct FpsIdling
float fpsIdle = 9.f;

// `enableIdling`: _bool, default=true_.
// Set this to false to disable idling
// Disable idling by setting this to false.
// (this can be changed dynamically during execution)
bool enableIdling = true;

Expand Down
7 changes: 7 additions & 0 deletions src/hello_imgui/doc_params.src.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ See [runner_params.h](https://github.com/pthom/hello_imgui/blob/master/src/hello
@import "runner_params.h" {md_id=RunnerParams}
```

### Backend selection


```cpp
@import "runner_params.h" {md_id=BackendType}
```
# Fps Idling
Expand Down
8 changes: 8 additions & 0 deletions src/hello_imgui/hello_imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ float FrameRate(float durationForMean = 0.5f);
// of ImGuiTestEngine that was initialized by HelloImGui
// (iif ImGui Test Engine is active).
ImGuiTestEngine* GetImGuiTestEngine();

// `GetBackendDescription()`: returns a string with the backend info
// Could be for example:
// "Glfw - OpenGL3"
// "Glfw - Metal"
// "Sdl - Vulkan"
std::string GetBackendDescription();

// @@md


Expand Down
35 changes: 35 additions & 0 deletions src/hello_imgui/impl/hello_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,41 @@ float FrameRate(float durationForMean)
return fps;
}

std::string PlatformBackendTypeToString(PlatformBackendType platformBackendType)
{
if (platformBackendType == PlatformBackendType::Glfw)
return "Glfw";
else if (platformBackendType == PlatformBackendType::Sdl)
return "Sdl";
else
return "Unknown platform backend";
}

std::string RendererBackendTypeToString(RendererBackendType rendererBackendType)
{
if (rendererBackendType == RendererBackendType::OpenGL3)
return "OpenGL3";
else if (rendererBackendType == RendererBackendType::Vulkan)
return "Vulkan";
else if (rendererBackendType == RendererBackendType::Metal)
return "Metal";
else if (rendererBackendType == RendererBackendType::DirectX11)
return "DirectX11";
else if (rendererBackendType == RendererBackendType::DirectX12)
return "DirectX12";
else
return "Unknown renderer backend";
}

std::string GetBackendDescription()
{
const auto& params = GetRunnerParams();
std::string platformBackend = PlatformBackendTypeToString(params->platformBackendType);
std::string rendererBackend = RendererBackendTypeToString(params->rendererBackendType);
return platformBackend + " - " + rendererBackend;
}


#ifdef HELLOIMGUI_WITH_TEST_ENGINE
extern ImGuiTestEngine *GHImGuiTestEngine;
ImGuiTestEngine* GetImGuiTestEngine() { return GHImGuiTestEngine; }
Expand Down
Loading

0 comments on commit 3cbfbb3

Please sign in to comment.