Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pthom/hello_imgui
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Apr 13, 2024
2 parents 1dd3e7a + aa78fab commit 0a893fe
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,17 @@ 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)
# </Backends> # do not remove this line (used by the script that generates the documentation)


Expand Down
11 changes: 10 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@
"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_DIRECTX12": "ON",
"HELLOIMGUI_HAS_NULL": "ON"
}
},
{
"name": "build_null_backend",
"description": "Build with null backend.",
"cacheVariables": {
"HELLOIMGUI_NULL_BACKEND": "ON"
}
},
{
Expand Down
12 changes: 12 additions & 0 deletions hello_imgui_cmake/hello_imgui_build_lib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function(him_back_available_platform_backends out_var)
set(${out_var}
HELLOIMGUI_USE_SDL2
HELLOIMGUI_USE_GLFW3
HELLOIMGUI_USE_NULL
PARENT_SCOPE)
endfunction()

Expand All @@ -44,10 +45,15 @@ 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,
Expand Down Expand Up @@ -1156,6 +1162,9 @@ 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})
Expand All @@ -1172,6 +1181,9 @@ 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()

him_add_apple_options()
him_add_linux_options()
Expand Down
41 changes: 28 additions & 13 deletions src/hello_imgui/impl/hello_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,42 @@ namespace ChronoShenanigans

}

float FrameRate(float durationForMean)
static std::deque<float> gFrameTimes;

void _UpdateFrameRateStats()
{
static std::deque<float> times;
float now = ChronoShenanigans::ClockSeconds();
times.push_back(now);
if (times.size() <= 1)
gFrameTimes.push_back(now);

size_t maxFrameCount = 300;
while (gFrameTimes.size() > maxFrameCount)
gFrameTimes.pop_front();
};

float FrameRate(float durationForMean)
{
if (gFrameTimes.size() <= 1)
return 0.f;

while (true)
float lastFrameTime = gFrameTimes.back();
int lastFrameIdx = (int)gFrameTimes.size() - 1;

// Go back in frame times to find the first frame that is not too old
int i = (int)gFrameTimes.size() - 1;
while (i > 0)
{
float firstTime = times.front();
float age = now - firstTime;
if ((age > durationForMean) && (times.size() >= 3))
times.pop_front();
else
if (lastFrameTime - gFrameTimes[i] > durationForMean)
break;
--i;
}
if (i == lastFrameIdx)
return 0.f;
// printf("i=%d, lastFrameIdx=%d\n", i, lastFrameIdx);

float totalTime = times.back() - times.front();
int nbFrames = (int)times.size();
float fps = 1.f / (totalTime / (float) (nbFrames - 1));
// Compute the mean frame rate
float totalTime = lastFrameTime - gFrameTimes[i];
int nbFrames = lastFrameIdx - i;
float fps = (float)nbFrames / totalTime;
return fps;
}

Expand Down
37 changes: 27 additions & 10 deletions src/hello_imgui/internal/backend_impls/abstract_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "rendering_vulkan.h"
#include "rendering_dx11.h"
#include "rendering_dx12.h"
#include "rendering_null.h"

//
// NOTE: AbstractRunner should *not* care in any case of:
Expand Down Expand Up @@ -578,6 +579,14 @@ void AbstractRunner::InitRenderBackendCallbacks()
IM_ASSERT(false && "DirectX12 backend is not available!");
#endif
}
else if (params.rendererBackendType == RendererBackendType::Null)
{
#ifdef HELLOIMGUI_USE_NULL
mRenderingBackendCallbacks = CreateBackendCallbacks_Null();
#else
IM_ASSERT(false && "Null backend is not available!");
#endif
}
else
{
fprintf(stderr, "Missing rendering backend! %s\n", gMissingBackendErrorMessage.c_str());
Expand Down Expand Up @@ -737,6 +746,8 @@ void AbstractRunner::RenderGui()
}


void _UpdateFrameRateStats(); // See hello_imgui.cpp

void AbstractRunner::CreateFramesAndRender()
{
// Notes:
Expand All @@ -760,11 +771,17 @@ void AbstractRunner::CreateFramesAndRender()
// any user callback (which may call python functions)
// -

// `true_gil` is a synonym for "true" (whenever using Python or not using Python)
// (it is here only to make the code more readable)
constexpr bool true_gil = true;
// `foldable_region` is a synonym for "true" (whenever using Python or not using Python)
// (it is here only to make the code more readable, and to enable to collapse blocks of code)
constexpr bool foldable_region = true;

if (foldable_region) // Update frame rate stats
{
_UpdateFrameRateStats();
// printf("Render frame %i, fps=%.1f\n", mIdxFrame, HelloImGui::FrameRate());
}

if (true_gil) // basic layout checks
if (foldable_region) // basic layout checks
{ // SCOPED_RELEASE_GIL_ON_MAIN_THREAD start
SCOPED_RELEASE_GIL_ON_MAIN_THREAD;

Expand All @@ -780,7 +797,7 @@ void AbstractRunner::CreateFramesAndRender()
#endif
} // SCOPED_RELEASE_GIL_ON_MAIN_THREAD end

if (true) // Register tests
if (foldable_region) // Register tests
{
#ifdef HELLOIMGUI_WITH_TEST_ENGINE
// This block calls a user callback, so it cannot be inside SCOPED_RELEASE_GIL_ON_MAIN_THREAD
Expand All @@ -795,7 +812,7 @@ void AbstractRunner::CreateFramesAndRender()
#endif
}

if (true_gil) // handle window size and position on first frames
if (foldable_region) // handle window size and position on first frames
{ // SCOPED_RELEASE_GIL_ON_MAIN_THREAD start
SCOPED_RELEASE_GIL_ON_MAIN_THREAD;

Expand Down Expand Up @@ -860,7 +877,7 @@ void AbstractRunner::CreateFramesAndRender()
}
} // SCOPED_RELEASE_GIL_ON_MAIN_THREAD end

if(true_gil) // Handle idling
if(foldable_region) // Handle idling
{ // SCOPED_RELEASE_GIL_ON_MAIN_THREAD start
SCOPED_RELEASE_GIL_ON_MAIN_THREAD;

Expand All @@ -884,7 +901,7 @@ void AbstractRunner::CreateFramesAndRender()
#endif
} // SCOPED_RELEASE_GIL_ON_MAIN_THREAD end

if (true_gil) // Load additional fonts during execution
if (foldable_region) // Load additional fonts during execution
{
if (params.callbacks.LoadAdditionalFonts != nullptr)
{
Expand All @@ -904,7 +921,7 @@ void AbstractRunner::CreateFramesAndRender()
if (params.callbacks.PreNewFrame)
params.callbacks.PreNewFrame();

if (true_gil) // New Frame / Rendering and Platform Backend (not ImGui)
if (foldable_region) // New Frame / Rendering and Platform Backend (not ImGui)
{ // SCOPED_RELEASE_GIL_ON_MAIN_THREAD start
SCOPED_RELEASE_GIL_ON_MAIN_THREAD;

Expand Down Expand Up @@ -958,7 +975,7 @@ void AbstractRunner::CreateFramesAndRender()
if (params.callbacks.BeforeImGuiRender)
params.callbacks.BeforeImGuiRender();

if (true_gil) // Render and Swap
if (foldable_region) // Render and Swap
{ // SCOPED_RELEASE_GIL_ON_MAIN_THREAD start
SCOPED_RELEASE_GIL_ON_MAIN_THREAD;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once
#ifdef HELLOIMGUI_USE_NULL

#include "backend_window_helper.h"
#include "hello_imgui/internal/backend_impls/null_config.h"
#include <thread>

#ifdef _WIN32
#ifdef CreateWindow
#undef CreateWindow
#endif
#endif


namespace HelloImGui { namespace BackendApi
{
class NullWindowHelper: public IBackendWindowHelper
{
// Note: this is a fake class, it has no member
// It is only a class in order to enforce a consistent API between backends.
public:
WindowPointer CreateWindow(AppWindowParams &appWindowParams, const BackendOptions& backendOptions) override {
return nullptr;
}

std::vector<ScreenBounds> GetMonitorsWorkAreas() override { return NullConfig::GetMonitorsWorkAreas();}

bool IsWindowIconified(WindowPointer window) override { return false; }
void RaiseWindow(WindowPointer window) override {}

ScreenBounds GetWindowBounds(WindowPointer window) override { return {}; }
void SetWindowBounds(WindowPointer window, ScreenBounds windowBounds) override {}

void WaitForEventTimeout(double timeout_seconds) override {
std::this_thread::sleep_for(std::chrono::milliseconds((int)(timeout_seconds * 1000)));
}

ImVec2 GetWindowScaleFactor(WindowPointer window) override { return NullConfig::GetWindowScaleFactor(); }
float GetWindowSizeDpiScaleFactor(WindowPointer window) override { return NullConfig::GetWindowSizeDpiScaleFactor(); }

void HideWindow(WindowPointer window) override {}
void ShowWindow(WindowPointer window) override {}
bool IsWindowHidden(WindowPointer window) override { return false; }

};
}} // namespace HelloImGui { namespace BackendApi

#endif // #ifdef HELLOIMGUI_USE_NULL
29 changes: 29 additions & 0 deletions src/hello_imgui/internal/backend_impls/null_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "hello_imgui/screen_bounds.h"
#include "imgui.h"
#include <vector>

namespace HelloImGui
{
namespace NullConfig
{
ScreenBounds GetScreenBounds()
{
ScreenBounds bounds;
bounds.size = {1920, 1080};
bounds.position = {0, 0};
return {bounds};
}

std::vector<ScreenBounds> GetMonitorsWorkAreas()
{
return {GetScreenBounds()};
}

ImVec2 GetWindowScaleFactor() { return {1.f, 1.f}; }
float GetWindowSizeDpiScaleFactor() { return 1.f; }

}

}
29 changes: 29 additions & 0 deletions src/hello_imgui/internal/backend_impls/rendering_null.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifdef HELLOIMGUI_HAS_NULL
#include "rendering_null.h"


namespace HelloImGui
{
RenderingCallbacksPtr CreateBackendCallbacks_Null()
{
auto callbacks = std::make_shared<RenderingCallbacks>();

callbacks->Impl_NewFrame_3D = [] {};

callbacks->Impl_RenderDrawData_To_3D = [] {};

callbacks->Impl_ScreenshotRgb_3D = []() { return ImageBuffer{}; };

callbacks->Impl_Frame_3D_ClearColor = [](ImVec4) {};

callbacks->Impl_Shutdown_3D = [] {};

callbacks->Impl_CreateFontTexture = [] {};
callbacks->Impl_DestroyFontTexture = [] {};

return callbacks;
}

}

#endif
11 changes: 11 additions & 0 deletions src/hello_imgui/internal/backend_impls/rendering_null.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#ifdef HELLOIMGUI_HAS_NULL

#include "hello_imgui/internal/backend_impls/rendering_callbacks.h"

namespace HelloImGui
{
RenderingCallbacksPtr CreateBackendCallbacks_Null();
}

#endif
Loading

0 comments on commit 0a893fe

Please sign in to comment.