[OpenGL] Merge "shared" files back to imgui_hud.cpp

pull/131/head
jackun 4 years ago
parent b40fb95a04
commit f35e49defa
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3

@ -1,11 +1,19 @@
#include <cstdlib>
#include <functional>
#include <thread>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <imgui.h>
#include "font_default.h" #include "font_default.h"
#include "cpu.h" #include "cpu.h"
#include "file_utils.h" #include "file_utils.h"
#include "imgui_hud_shared.h"
#include "imgui_hud.h" #include "imgui_hud.h"
#include "notify.h"
#ifdef HAVE_DBUS
#include "dbus_info.h"
#endif
#include <glad/glad.h> #include <glad/glad.h>
@ -46,6 +54,29 @@ static state state;
static uint32_t vendorID; static uint32_t vendorID;
static std::string deviceName; static std::string deviceName;
static notify_thread notifier;
static bool cfg_inited = false;
static ImVec2 window_size;
static bool inited = false;
overlay_params params {};
// seems to quit by itself though
static std::unique_ptr<notify_thread, std::function<void(notify_thread *)>>
stop_it(&notifier, [](notify_thread *n){ stop_notifier(*n); });
void imgui_init()
{
if (cfg_inited)
return;
parse_overlay_config(&params, getenv("MANGOHUD_CONFIG"));
notifier.params = &params;
start_notifier(notifier);
window_size = ImVec2(params.width, params.height);
init_system_info();
cfg_inited = true;
init_cpu_stats(params);
}
//static //static
void imgui_create(void *ctx) void imgui_create(void *ctx)
{ {
@ -91,7 +122,7 @@ void imgui_create(void *ctx)
ImGui::GetIO().IniFilename = NULL; ImGui::GetIO().IniFilename = NULL;
ImGui::GetIO().DisplaySize = ImVec2(last_vp[2], last_vp[3]); ImGui::GetIO().DisplaySize = ImVec2(last_vp[2], last_vp[3]);
VARIANT(ImGui_ImplOpenGL3_Init)(); ImGui_ImplOpenGL3_Init();
// Make a dummy GL call (we don't actually need the result) // Make a dummy GL call (we don't actually need the result)
// IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by this code. // IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by this code.
// Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above. // Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above.
@ -118,34 +149,8 @@ void imgui_create(void *ctx)
// Restore global context or ours might clash with apps that use Dear ImGui // Restore global context or ours might clash with apps that use Dear ImGui
ImGui::SetCurrentContext(saved_ctx); ImGui::SetCurrentContext(saved_ctx);
} }
/*
#ifdef IMGUI_GLX
void VARIANT(imgui_create)(void *ctx)
{
if (inited)
return;
if (!ctx) void imgui_shutdown()
return;
imgui_create(ctx);
}
#endif
#ifdef IMGUI_EGL
void VARIANT(imgui_create)(void *ctx)
{
if (inited)
return;
if (!ctx)
return;
imgui_create(ctx);
}
#endif*/
void VARIANT(imgui_shutdown)()
{ {
#ifndef NDEBUG #ifndef NDEBUG
std::cerr << __func__ << std::endl; std::cerr << __func__ << std::endl;
@ -153,26 +158,26 @@ void VARIANT(imgui_shutdown)()
if (state.imgui_ctx) { if (state.imgui_ctx) {
ImGui::SetCurrentContext(state.imgui_ctx); ImGui::SetCurrentContext(state.imgui_ctx);
VARIANT(ImGui_ImplOpenGL3_Shutdown)(); ImGui_ImplOpenGL3_Shutdown();
ImGui::DestroyContext(state.imgui_ctx); ImGui::DestroyContext(state.imgui_ctx);
state.imgui_ctx = nullptr; state.imgui_ctx = nullptr;
} }
inited = false; inited = false;
} }
void VARIANT(imgui_set_context)(void *ctx) void imgui_set_context(void *ctx)
{ {
if (!ctx) { if (!ctx) {
VARIANT(imgui_shutdown)(); imgui_shutdown();
return; return;
} }
#ifndef NDEBUG #ifndef NDEBUG
std::cerr << __func__ << ": " << ctx << std::endl; std::cerr << __func__ << ": " << ctx << std::endl;
#endif #endif
VARIANT(imgui_create)(ctx); imgui_create(ctx);
} }
void VARIANT(imgui_render)(unsigned int width, unsigned int height) void imgui_render(unsigned int width, unsigned int height)
{ {
if (!state.imgui_ctx) if (!state.imgui_ctx)
return; return;
@ -184,7 +189,7 @@ void VARIANT(imgui_render)(unsigned int width, unsigned int height)
ImGui::SetCurrentContext(state.imgui_ctx); ImGui::SetCurrentContext(state.imgui_ctx);
ImGui::GetIO().DisplaySize = ImVec2(width, height); ImGui::GetIO().DisplaySize = ImVec2(width, height);
VARIANT(ImGui_ImplOpenGL3_NewFrame)(); ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
{ {
std::lock_guard<std::mutex> lk(notifier.mutex); std::lock_guard<std::mutex> lk(notifier.mutex);
@ -194,7 +199,7 @@ void VARIANT(imgui_render)(unsigned int width, unsigned int height)
ImGui::PopStyleVar(3); ImGui::PopStyleVar(3);
ImGui::Render(); ImGui::Render();
VARIANT(ImGui_ImplOpenGL3_RenderDrawData)(ImGui::GetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
ImGui::SetCurrentContext(saved_ctx); ImGui::SetCurrentContext(saved_ctx);
} }

@ -1,13 +1,15 @@
#pragma once #pragma once
#include "overlay.h"
#include "imgui_impl_opengl3.h" #include "imgui_impl_opengl3.h"
namespace MangoHud { namespace GL { namespace MangoHud { namespace GL {
void VARIANT(imgui_init)(); extern overlay_params params;
void VARIANT(imgui_create)(void *ctx); void imgui_init();
void VARIANT(imgui_shutdown)(); void imgui_create(void *ctx);
void VARIANT(imgui_set_context)(void *ctx); void imgui_shutdown();
void VARIANT(imgui_render)(unsigned int width, unsigned int height); void imgui_set_context(void *ctx);
void imgui_render(unsigned int width, unsigned int height);
}} // namespace }} // namespace

@ -1,36 +0,0 @@
#include <cstdlib>
#include <functional>
#include <thread>
#include <iostream>
#include "imgui_hud_shared.h"
#ifdef HAVE_DBUS
#include "dbus_info.h"
#endif
namespace MangoHud { namespace GL {
notify_thread notifier;
static bool cfg_inited = false;
ImVec2 window_size;
bool inited = false;
overlay_params params {};
// seems to quit by itself though
static std::unique_ptr<notify_thread, std::function<void(notify_thread *)>>
stop_it(&notifier, [](notify_thread *n){ stop_notifier(*n); });
void imgui_init()
{
if (cfg_inited)
return;
parse_overlay_config(&params, getenv("MANGOHUD_CONFIG"));
notifier.params = &params;
start_notifier(notifier);
window_size = ImVec2(params.width, params.height);
init_system_info();
cfg_inited = true;
init_cpu_stats(params);
}
}} // namespaces

@ -1,15 +0,0 @@
#pragma once
#include <imgui.h>
#include "overlay.h"
#include "notify.h"
namespace MangoHud { namespace GL {
extern notify_thread notifier;
extern ImVec2 window_size;
extern bool inited;
extern overlay_params params;
void imgui_init();
}} // namespaces

@ -400,7 +400,7 @@ void GetOpenGLVersion(int& major, int& minor, bool& isGLES)
//} //}
} }
bool VARIANT(ImGui_ImplOpenGL3_Init)(const char* glsl_version) bool ImGui_ImplOpenGL3_Init(const char* glsl_version)
{ {
GLint major = 0, minor = 0; GLint major = 0, minor = 0;
GetOpenGLVersion(major, minor, g_IsGLES); GetOpenGLVersion(major, minor, g_IsGLES);
@ -458,18 +458,18 @@ bool VARIANT(ImGui_ImplOpenGL3_Init)(const char* glsl_version)
return true; return true;
} }
void VARIANT(ImGui_ImplOpenGL3_Shutdown)() void ImGui_ImplOpenGL3_Shutdown()
{ {
ImGui_ImplOpenGL3_DestroyDeviceObjects(); ImGui_ImplOpenGL3_DestroyDeviceObjects();
} }
void VARIANT(ImGui_ImplOpenGL3_NewFrame)() void ImGui_ImplOpenGL3_NewFrame()
{ {
if (!g_ShaderHandle) if (!g_ShaderHandle)
ImGui_ImplOpenGL3_CreateDeviceObjects(); ImGui_ImplOpenGL3_CreateDeviceObjects();
} }
static void VARIANT(ImGui_ImplOpenGL3_SetupRenderState)(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object) static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object)
{ {
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill
glEnable(GL_BLEND); glEnable(GL_BLEND);
@ -523,7 +523,7 @@ static void VARIANT(ImGui_ImplOpenGL3_SetupRenderState)(ImDrawData* draw_data, i
// OpenGL3 Render function. // OpenGL3 Render function.
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop) // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so. // Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so.
void VARIANT(ImGui_ImplOpenGL3_RenderDrawData)(ImDrawData* draw_data) void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data)
{ {
// Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x); int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
@ -584,7 +584,7 @@ void VARIANT(ImGui_ImplOpenGL3_RenderDrawData)(ImDrawData* draw_data)
if (g_GlVersion >= 3000) if (g_GlVersion >= 3000)
glGenVertexArrays(1, &vertex_array_object); glGenVertexArrays(1, &vertex_array_object);
VARIANT(ImGui_ImplOpenGL3_SetupRenderState)(draw_data, fb_width, fb_height, vertex_array_object); ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
// Will project scissor/clipping rectangles into framebuffer space // Will project scissor/clipping rectangles into framebuffer space
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
@ -607,7 +607,7 @@ void VARIANT(ImGui_ImplOpenGL3_RenderDrawData)(ImDrawData* draw_data)
// User callback, registered via ImDrawList::AddCallback() // User callback, registered via ImDrawList::AddCallback()
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.) // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
VARIANT(ImGui_ImplOpenGL3_SetupRenderState)(draw_data, fb_width, fb_height, vertex_array_object); ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object);
else else
pcmd->UserCallback(cmd_list, pcmd); pcmd->UserCallback(cmd_list, pcmd);
} }

@ -25,33 +25,13 @@
namespace MangoHud { namespace MangoHud {
/*
#if defined(IMGUI_GLX) && defined(IMGUI_EGL)
#error Both IMGUI_GLX and IMGUI_EGL can not be defined at the same time!
#elif !defined(IMGUI_GLX) && !defined(IMGUI_EGL)
#error Define IMGUI_GLX or IMGUI_EGL!
#endif
#undef VARIANT
#ifdef IMGUI_GLX
#define VARIANT(x) x##_GLX
#endif
#ifdef IMGUI_EGL
#define VARIANT(x) x##_EGL
#endif
*/
// FIXME might not be needed, GLX/EGL _can_ maybe live together
#define VARIANT(x) x
void GetOpenGLVersion(int& major, int& minor, bool& isGLES); void GetOpenGLVersion(int& major, int& minor, bool& isGLES);
// Backend API // Backend API
IMGUI_IMPL_API bool VARIANT(ImGui_ImplOpenGL3_Init)(const char* glsl_version = nullptr); IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = nullptr);
IMGUI_IMPL_API void VARIANT(ImGui_ImplOpenGL3_Shutdown)(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown();
IMGUI_IMPL_API void VARIANT(ImGui_ImplOpenGL3_NewFrame)(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame();
IMGUI_IMPL_API void VARIANT(ImGui_ImplOpenGL3_RenderDrawData)(ImDrawData* draw_data); IMGUI_IMPL_API void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data);
// (Optional) Called by Init/NewFrame/Shutdown // (Optional) Called by Init/NewFrame/Shutdown
//IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateFontsTexture(); //IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateFontsTexture();

@ -8,7 +8,6 @@
#include <chrono> #include <chrono>
#include <iomanip> #include <iomanip>
#include "imgui_hud_shared.h"
#include "imgui_hud.h" #include "imgui_hud.h"
using namespace MangoHud::GL; using namespace MangoHud::GL;
@ -59,12 +58,12 @@ EXPORT_C_(unsigned int) eglSwapBuffers( void* dpy, void* surf)
//std::cerr << __func__ << "\n"; //std::cerr << __func__ << "\n";
VARIANT(imgui_create)(surf); imgui_create(surf);
int width=0, height=0; int width=0, height=0;
if (pfn_eglQuerySurface(dpy, surf, 0x3056, &height) && if (pfn_eglQuerySurface(dpy, surf, 0x3056, &height) &&
pfn_eglQuerySurface(dpy, surf, 0x3057, &width)) pfn_eglQuerySurface(dpy, surf, 0x3057, &width))
VARIANT(imgui_render)(width, height); imgui_render(width, height);
//std::cerr << "\t" << width << " x " << height << "\n"; //std::cerr << "\t" << width << " x " << height << "\n";

@ -11,7 +11,6 @@
#include <chrono> #include <chrono>
#include <iomanip> #include <iomanip>
#include "imgui_hud_shared.h"
#include "imgui_hud.h" #include "imgui_hud.h"
using namespace MangoHud::GL; using namespace MangoHud::GL;
@ -66,7 +65,7 @@ EXPORT_C_(int) glXMakeCurrent(void* dpy, void* drawable, void* ctx) {
int ret = glx.MakeCurrent(dpy, drawable, ctx); int ret = glx.MakeCurrent(dpy, drawable, ctx);
if (ret) if (ret)
VARIANT(imgui_set_context)(ctx); imgui_set_context(ctx);
if (params.gl_vsync >= -1) { if (params.gl_vsync >= -1) {
if (glx.SwapIntervalEXT) if (glx.SwapIntervalEXT)
@ -82,7 +81,7 @@ EXPORT_C_(int) glXMakeCurrent(void* dpy, void* drawable, void* ctx) {
EXPORT_C_(void) glXSwapBuffers(void* dpy, void* drawable) { EXPORT_C_(void) glXSwapBuffers(void* dpy, void* drawable) {
glx.Load(); glx.Load();
VARIANT(imgui_create)(glx.GetCurrentContext()); imgui_create(glx.GetCurrentContext());
unsigned int width = -1, height = -1; unsigned int width = -1, height = -1;
@ -105,7 +104,7 @@ EXPORT_C_(void) glXSwapBuffers(void* dpy, void* drawable) {
width = vp[2]; width = vp[2];
height = vp[3];*/ height = vp[3];*/
VARIANT(imgui_render)(width, height); imgui_render(width, height);
glx.SwapBuffers(dpy, drawable); glx.SwapBuffers(dpy, drawable);
if (fps_limit_stats.targetFrameTime > 0){ if (fps_limit_stats.targetFrameTime > 0){
fps_limit_stats.frameStart = os_time_get_nano(); fps_limit_stats.frameStart = os_time_get_nano();

@ -56,7 +56,6 @@ vklayer_files = files(
) )
opengl_files = files( opengl_files = files(
'gl/imgui_hud_shared.cpp',
'gl/glad.c', 'gl/glad.c',
'gl/imgui_impl_opengl3.cpp', 'gl/imgui_impl_opengl3.cpp',
'gl/imgui_hud.cpp', 'gl/imgui_hud.cpp',

Loading…
Cancel
Save