2020-04-12 13:21:11 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <functional>
|
|
|
|
#include <thread>
|
2020-04-01 08:25:55 +00:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
2020-04-12 13:21:11 +00:00
|
|
|
#include <imgui.h>
|
2020-04-01 08:25:55 +00:00
|
|
|
#include "font_default.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "file_utils.h"
|
2020-04-01 22:30:47 +00:00
|
|
|
#include "imgui_hud.h"
|
2020-04-12 13:21:11 +00:00
|
|
|
#include "notify.h"
|
2020-06-21 11:14:42 +00:00
|
|
|
#include "blacklist.h"
|
2020-04-12 13:21:11 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_DBUS
|
|
|
|
#include "dbus_info.h"
|
|
|
|
#endif
|
2020-04-01 08:25:55 +00:00
|
|
|
|
2020-04-09 18:24:37 +00:00
|
|
|
#include <glad/glad.h>
|
2020-04-01 22:30:47 +00:00
|
|
|
|
|
|
|
namespace MangoHud { namespace GL {
|
2020-04-01 08:25:55 +00:00
|
|
|
|
|
|
|
struct GLVec
|
|
|
|
{
|
|
|
|
GLint v[4];
|
|
|
|
|
|
|
|
GLint operator[] (size_t i)
|
|
|
|
{
|
|
|
|
return v[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator== (const GLVec& r)
|
|
|
|
{
|
|
|
|
return v[0] == r.v[0]
|
|
|
|
&& v[1] == r.v[1]
|
|
|
|
&& v[2] == r.v[2]
|
|
|
|
&& v[3] == r.v[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!= (const GLVec& r)
|
|
|
|
{
|
|
|
|
return !(*this == r);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct state {
|
|
|
|
ImGuiContext *imgui_ctx = nullptr;
|
|
|
|
ImFont* font = nullptr;
|
|
|
|
ImFont* font1 = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static GLVec last_vp {}, last_sb {};
|
|
|
|
static swapchain_stats sw_stats {};
|
|
|
|
static state state;
|
|
|
|
static uint32_t vendorID;
|
|
|
|
static std::string deviceName;
|
|
|
|
|
2020-04-12 13:21:11 +00:00
|
|
|
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(¬ifier, [](notify_thread *n){ stop_notifier(*n); });
|
|
|
|
|
|
|
|
void imgui_init()
|
|
|
|
{
|
|
|
|
if (cfg_inited)
|
|
|
|
return;
|
2020-06-21 11:14:42 +00:00
|
|
|
is_blacklisted(true);
|
2020-04-12 13:21:11 +00:00
|
|
|
parse_overlay_config(¶ms, getenv("MANGOHUD_CONFIG"));
|
|
|
|
notifier.params = ¶ms;
|
|
|
|
start_notifier(notifier);
|
|
|
|
window_size = ImVec2(params.width, params.height);
|
|
|
|
init_system_info();
|
|
|
|
cfg_inited = true;
|
|
|
|
init_cpu_stats(params);
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:24:37 +00:00
|
|
|
//static
|
|
|
|
void imgui_create(void *ctx)
|
2020-04-01 08:25:55 +00:00
|
|
|
{
|
|
|
|
if (inited)
|
|
|
|
return;
|
|
|
|
inited = true;
|
|
|
|
|
|
|
|
if (!ctx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
imgui_init();
|
|
|
|
|
2020-04-09 18:24:37 +00:00
|
|
|
gladLoadGL();
|
|
|
|
|
2020-04-12 13:16:13 +00:00
|
|
|
GetOpenGLVersion(sw_stats.version_gl.major,
|
|
|
|
sw_stats.version_gl.minor,
|
|
|
|
sw_stats.version_gl.is_gles);
|
2020-04-01 08:25:55 +00:00
|
|
|
|
|
|
|
deviceName = (char*)glGetString(GL_RENDERER);
|
2020-04-28 15:05:25 +00:00
|
|
|
sw_stats.deviceName = deviceName;
|
2020-04-01 08:25:55 +00:00
|
|
|
if (deviceName.find("Radeon") != std::string::npos
|
|
|
|
|| deviceName.find("AMD") != std::string::npos){
|
|
|
|
vendorID = 0x1002;
|
|
|
|
} else {
|
|
|
|
vendorID = 0x10de;
|
|
|
|
}
|
|
|
|
init_gpu_stats(vendorID, params);
|
2020-05-09 23:43:08 +00:00
|
|
|
get_device_name(vendorID, deviceID, sw_stats);
|
2020-04-01 08:25:55 +00:00
|
|
|
// Setup Dear ImGui context
|
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
ImGuiContext *saved_ctx = ImGui::GetCurrentContext();
|
|
|
|
state.imgui_ctx = ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
|
|
|
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
|
|
|
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
|
|
|
|
|
|
|
// Setup Dear ImGui style
|
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
//ImGui::StyleColorsClassic();
|
|
|
|
imgui_custom_style(params);
|
|
|
|
|
|
|
|
glGetIntegerv (GL_VIEWPORT, last_vp.v);
|
|
|
|
glGetIntegerv (GL_SCISSOR_BOX, last_sb.v);
|
|
|
|
|
|
|
|
ImGui::GetIO().IniFilename = NULL;
|
|
|
|
ImGui::GetIO().DisplaySize = ImVec2(last_vp[2], last_vp[3]);
|
|
|
|
|
2020-04-12 13:21:11 +00:00
|
|
|
ImGui_ImplOpenGL3_Init();
|
2020-04-01 08:25:55 +00:00
|
|
|
// 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.
|
|
|
|
// Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above.
|
|
|
|
GLint current_texture;
|
|
|
|
glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
|
|
|
|
|
2020-05-24 13:42:35 +00:00
|
|
|
create_fonts(params, state.font, state.font1);
|
2020-04-01 08:25:55 +00:00
|
|
|
sw_stats.font1 = state.font1;
|
|
|
|
|
|
|
|
// Restore global context or ours might clash with apps that use Dear ImGui
|
|
|
|
ImGui::SetCurrentContext(saved_ctx);
|
|
|
|
}
|
2020-04-01 22:30:47 +00:00
|
|
|
|
2020-04-12 13:21:11 +00:00
|
|
|
void imgui_shutdown()
|
2020-04-01 08:25:55 +00:00
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
|
|
|
std::cerr << __func__ << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (state.imgui_ctx) {
|
|
|
|
ImGui::SetCurrentContext(state.imgui_ctx);
|
2020-04-12 13:21:11 +00:00
|
|
|
ImGui_ImplOpenGL3_Shutdown();
|
2020-04-01 08:25:55 +00:00
|
|
|
ImGui::DestroyContext(state.imgui_ctx);
|
|
|
|
state.imgui_ctx = nullptr;
|
|
|
|
}
|
|
|
|
inited = false;
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:21:11 +00:00
|
|
|
void imgui_set_context(void *ctx)
|
2020-04-01 08:25:55 +00:00
|
|
|
{
|
|
|
|
if (!ctx) {
|
2020-04-12 13:21:11 +00:00
|
|
|
imgui_shutdown();
|
2020-04-01 08:25:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifndef NDEBUG
|
|
|
|
std::cerr << __func__ << ": " << ctx << std::endl;
|
|
|
|
#endif
|
2020-04-12 13:21:11 +00:00
|
|
|
imgui_create(ctx);
|
2020-04-01 08:25:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 13:21:11 +00:00
|
|
|
void imgui_render(unsigned int width, unsigned int height)
|
2020-04-01 08:25:55 +00:00
|
|
|
{
|
|
|
|
if (!state.imgui_ctx)
|
|
|
|
return;
|
|
|
|
|
2020-06-21 11:44:12 +00:00
|
|
|
check_keybinds(sw_stats, params, vendorID);
|
2020-04-01 08:25:55 +00:00
|
|
|
update_hud_info(sw_stats, params, vendorID);
|
|
|
|
|
|
|
|
ImGuiContext *saved_ctx = ImGui::GetCurrentContext();
|
|
|
|
ImGui::SetCurrentContext(state.imgui_ctx);
|
|
|
|
ImGui::GetIO().DisplaySize = ImVec2(width, height);
|
|
|
|
|
2020-04-12 13:21:11 +00:00
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
2020-04-01 08:25:55 +00:00
|
|
|
ImGui::NewFrame();
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(notifier.mutex);
|
2020-05-16 11:35:50 +00:00
|
|
|
position_layer(sw_stats, params, window_size);
|
2020-04-01 08:25:55 +00:00
|
|
|
render_imgui(sw_stats, params, window_size, false);
|
|
|
|
}
|
|
|
|
ImGui::PopStyleVar(3);
|
|
|
|
|
|
|
|
ImGui::Render();
|
2020-04-12 13:21:11 +00:00
|
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
2020-04-01 08:25:55 +00:00
|
|
|
ImGui::SetCurrentContext(saved_ctx);
|
|
|
|
}
|
2020-04-01 22:30:47 +00:00
|
|
|
|
|
|
|
}} // namespaces
|