diff --git a/src/gl/imgui_hud.cpp b/src/gl/imgui_hud.cpp index bde2dbac..2eb79787 100644 --- a/src/gl/imgui_hud.cpp +++ b/src/gl/imgui_hud.cpp @@ -116,7 +116,7 @@ void imgui_create(void *ctx) // Setup Dear ImGui style ImGui::StyleColorsDark(); //ImGui::StyleColorsClassic(); - imgui_custom_style(params); + convert_colors(false, sw_stats, params); glGetIntegerv (GL_VIEWPORT, last_vp.v); glGetIntegerv (GL_SCISSOR_BOX, last_sb.v); diff --git a/src/overlay.cpp b/src/overlay.cpp index 52682869..2d5e9466 100644 --- a/src/overlay.cpp +++ b/src/overlay.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -1112,7 +1113,7 @@ void render_benchmark(swapchain_stats& data, struct overlay_params& params, ImVe ImGui::TextColored(ImVec4(1.0, 1.0, 1.0, alpha / params.background_alpha), "%s %.1f", data_.first.c_str(), data_.second); } float max = *max_element(benchmark.fps_data.begin(), benchmark.fps_data.end()); - ImVec4 plotColor = ImGui::ColorConvertU32ToFloat4(params.frametime_color); + ImVec4 plotColor = data.colors.frametime; plotColor.w = alpha / params.background_alpha; ImGui::PushStyleColor(ImGuiCol_PlotLines, plotColor); ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(0.0, 0.0, 0.0, alpha / params.background_alpha)); @@ -1143,7 +1144,7 @@ void render_mango(swapchain_stats& data, struct overlay_params& params, ImVec2& gpu_text = "GPU"; else gpu_text = params.gpu_text.c_str(); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.gpu_color), "%s", gpu_text); + ImGui::TextColored(data.colors.gpu, "%s", gpu_text); ImGui::TableNextCell(); right_aligned_text(ralign_width, "%i", gpu_info.load); ImGui::SameLine(0, 1.0f); @@ -1156,7 +1157,7 @@ void render_mango(swapchain_stats& data, struct overlay_params& params, ImVec2& cpu_text = "CPU"; else cpu_text = params.cpu_text.c_str(); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.cpu_color), "%s", cpu_text); + ImGui::TextColored(data.colors.cpu, "%s", cpu_text); ImGui::TableNextCell(); right_aligned_text(ralign_width, "%d", int(cpuStats.GetCPUDataTotal().percent)); ImGui::SameLine(0, 1.0f); @@ -1164,7 +1165,7 @@ void render_mango(swapchain_stats& data, struct overlay_params& params, ImVec2& } if (params.enabled[OVERLAY_PARAM_ENABLED_fps]){ ImGui::TableNextRow(); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.engine_color), "%s", is_vulkan ? data.engineName.c_str() : "OpenGL"); + ImGui::TextColored(data.colors.engine, "%s", is_vulkan ? data.engineName.c_str() : "OpenGL"); ImGui::TableNextCell(); right_aligned_text(ralign_width, "%.0f", data.fps); ImGui::SameLine(0, 1.0f); @@ -1176,7 +1177,7 @@ void render_mango(swapchain_stats& data, struct overlay_params& params, ImVec2& if (params.enabled[OVERLAY_PARAM_ENABLED_frame_timing]){ ImGui::Dummy(ImVec2(0.0f, params.font_size * params.font_scale / 2)); ImGui::PushFont(data.font1); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.engine_color), "%s", "Frametime"); + ImGui::TextColored(data.colors.engine, "%s", "Frametime"); ImGui::PopFont(); char hash[40]; @@ -1197,6 +1198,7 @@ void render_mango(swapchain_stats& data, struct overlay_params& params, ImVec2& ImGui::End(); window_size = ImVec2(window_size.x, ImGui::GetCursorPosY() + 150.0f); } + void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& window_size, bool is_vulkan) { ImGui::GetIO().FontGlobalScale = params.font_scale; @@ -1230,7 +1232,7 @@ void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& gpu_text = "GPU"; else gpu_text = params.gpu_text.c_str(); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.gpu_color), "%s", gpu_text); + ImGui::TextColored(data.colors.gpu, "%s", gpu_text); ImGui::TableNextCell(); right_aligned_text(ralign_width, "%i", gpu_info.load); ImGui::SameLine(0, 1.0f); @@ -1269,7 +1271,7 @@ void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& cpu_text = "CPU"; else cpu_text = params.cpu_text.c_str(); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.cpu_color), "%s", cpu_text); + ImGui::TextColored(data.colors.cpu, "%s", cpu_text); ImGui::TableNextCell(); right_aligned_text(ralign_width, "%d", int(cpuStats.GetCPUDataTotal().percent)); ImGui::SameLine(0, 1.0f); @@ -1290,10 +1292,10 @@ void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& for (const CPUData &cpuData : cpuStats.GetCPUData()) { ImGui::TableNextRow(); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.cpu_color), "CPU"); + ImGui::TextColored(data.colors.cpu, "CPU"); ImGui::SameLine(0, 1.0f); ImGui::PushFont(data.font1); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.cpu_color),"%i", i); + ImGui::TextColored(data.colors.cpu,"%i", i); ImGui::PopFont(); ImGui::TableNextCell(); right_aligned_text(ralign_width, "%i", int(cpuData.percent)); @@ -1312,11 +1314,11 @@ void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& auto sampling = params.fps_sampling_period; ImGui::TableNextRow(); if (params.enabled[OVERLAY_PARAM_ENABLED_io_read] && !params.enabled[OVERLAY_PARAM_ENABLED_io_write]) - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.io_color), "IO RD"); + ImGui::TextColored(data.colors.io, "IO RD"); else if (params.enabled[OVERLAY_PARAM_ENABLED_io_read] && params.enabled[OVERLAY_PARAM_ENABLED_io_write]) - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.io_color), "IO RW"); + ImGui::TextColored(data.colors.io, "IO RW"); else if (params.enabled[OVERLAY_PARAM_ENABLED_io_write] && !params.enabled[OVERLAY_PARAM_ENABLED_io_read]) - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.io_color), "IO WR"); + ImGui::TextColored(data.colors.io, "IO WR"); if (params.enabled[OVERLAY_PARAM_ENABLED_io_read]){ ImGui::TableNextCell(); @@ -1339,7 +1341,7 @@ void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& } if (params.enabled[OVERLAY_PARAM_ENABLED_vram]){ ImGui::TableNextRow(); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.vram_color), "VRAM"); + ImGui::TextColored(data.colors.vram, "VRAM"); ImGui::TableNextCell(); right_aligned_text(ralign_width, "%.1f", gpu_info.memoryUsed); ImGui::SameLine(0,1.0f); @@ -1357,7 +1359,7 @@ void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& } if (params.enabled[OVERLAY_PARAM_ENABLED_ram]){ ImGui::TableNextRow(); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.ram_color), "RAM"); + ImGui::TextColored(data.colors.ram, "RAM"); ImGui::TableNextCell(); right_aligned_text(ralign_width, "%.1f", memused); ImGui::SameLine(0,1.0f); @@ -1367,7 +1369,7 @@ void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& } if (params.enabled[OVERLAY_PARAM_ENABLED_fps]){ ImGui::TableNextRow(); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.engine_color), "%s", is_vulkan ? data.engineName.c_str() : "OpenGL"); + ImGui::TextColored(data.colors.engine, "%s", is_vulkan ? data.engineName.c_str() : "OpenGL"); ImGui::TableNextCell(); right_aligned_text(ralign_width, "%.0f", data.fps); ImGui::SameLine(0, 1.0f); @@ -1383,29 +1385,28 @@ void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& } if (!params.enabled[OVERLAY_PARAM_ENABLED_fps] && params.enabled[OVERLAY_PARAM_ENABLED_engine_version]){ ImGui::TableNextRow(); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.engine_color), "%s", is_vulkan ? data.engineName.c_str() : "OpenGL"); + ImGui::TextColored(data.colors.engine, "%s", is_vulkan ? data.engineName.c_str() : "OpenGL"); } ImGui::EndTable(); - auto engine_color = ImGui::ColorConvertU32ToFloat4(params.engine_color); if (params.enabled[OVERLAY_PARAM_ENABLED_engine_version]){ ImGui::PushFont(data.font1); ImGui::Dummy(ImVec2(0, 8.0f)); if (is_vulkan) { if ((data.engineName == "DXVK" || data.engineName == "VKD3D")){ - ImGui::TextColored(engine_color, + ImGui::TextColored(data.colors.engine, "%s/%d.%d.%d", data.engineVersion.c_str(), data.version_vk.major, data.version_vk.minor, data.version_vk.patch); } else { - ImGui::TextColored(engine_color, + ImGui::TextColored(data.colors.engine, "%d.%d.%d", data.version_vk.major, data.version_vk.minor, data.version_vk.patch); } } else { - ImGui::TextColored(engine_color, + ImGui::TextColored(data.colors.engine, "%d.%d%s", data.version_gl.major, data.version_gl.minor, data.version_gl.is_gles ? " ES" : ""); } @@ -1415,21 +1416,21 @@ void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& if (params.enabled[OVERLAY_PARAM_ENABLED_gpu_name] && !data.gpuName.empty()){ ImGui::PushFont(data.font1); ImGui::Dummy(ImVec2(0.0,5.0f)); - ImGui::TextColored(engine_color, + ImGui::TextColored(data.colors.engine, "%s", data.gpuName.c_str()); ImGui::PopFont(); } if (params.enabled[OVERLAY_PARAM_ENABLED_vulkan_driver] && !data.driverName.empty()){ ImGui::PushFont(data.font1); ImGui::Dummy(ImVec2(0.0,5.0f)); - ImGui::TextColored(engine_color, + ImGui::TextColored(data.colors.engine, "%s", data.driverName.c_str()); ImGui::PopFont(); } if (params.enabled[OVERLAY_PARAM_ENABLED_arch]){ ImGui::PushFont(data.font1); ImGui::Dummy(ImVec2(0.0,5.0f)); - ImGui::TextColored(engine_color, "%s", "" MANGOHUD_ARCH); + ImGui::TextColored(data.colors.engine, "%s", "" MANGOHUD_ARCH); ImGui::PopFont(); } @@ -1440,7 +1441,7 @@ void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& if (params.enabled[OVERLAY_PARAM_ENABLED_frame_timing]){ ImGui::Dummy(ImVec2(0.0f, params.font_size * params.font_scale / 2)); ImGui::PushFont(data.font1); - ImGui::TextColored(ImGui::ColorConvertU32ToFloat4(params.engine_color), "%s", "Frametime"); + ImGui::TextColored(data.colors.engine, "%s", "Frametime"); ImGui::PopFont(); char hash[40]; @@ -2236,18 +2237,123 @@ static void setup_swapchain_data_pipeline(struct swapchain_data *data) // update_image_descriptor(data, data->font_image_view[0], data->descriptor_set); } -void imgui_custom_style(struct overlay_params& params){ +// Cut from https://github.com/ocornut/imgui/pull/2943 +// Probably move to ImGui +float SRGBToLinear(float in) +{ + if (in <= 0.04045f) + return in / 12.92f; + else + return powf((in + 0.055f) / 1.055f, 2.4f); +} + +float LinearToSRGB(float in) +{ + if (in <= 0.0031308f) + return in * 12.92f; + else + return 1.055f * powf(in, 1.0f / 2.4f) - 0.055f; +} + +ImVec4 SRGBToLinear(ImVec4 col) +{ + col.x = SRGBToLinear(col.x); + col.y = SRGBToLinear(col.y); + col.z = SRGBToLinear(col.z); + // Alpha component is already linear + + return col; +} + +ImVec4 LinearToSRGB(ImVec4 col) +{ + col.x = LinearToSRGB(col.x); + col.y = LinearToSRGB(col.y); + col.z = LinearToSRGB(col.z); + // Alpha component is already linear + + return col; +} + +void convert_colors(bool do_conv, struct swapchain_stats& sw_stats, struct overlay_params& params) +{ + auto convert = [&do_conv](unsigned color) -> ImVec4 { + ImVec4 fc = ImGui::ColorConvertU32ToFloat4(color); + if (do_conv) + return SRGBToLinear(fc); + return fc; + }; + + sw_stats.colors.cpu = convert(params.cpu_color); + sw_stats.colors.gpu = convert(params.gpu_color); + sw_stats.colors.vram = convert(params.vram_color); + sw_stats.colors.ram = convert(params.ram_color); + sw_stats.colors.engine = convert(params.engine_color); + sw_stats.colors.io = convert(params.io_color); + sw_stats.colors.frametime = convert(params.frametime_color); + sw_stats.colors.background = convert(params.background_color); + sw_stats.colors.text = convert(params.text_color); + sw_stats.colors.media_player = convert(params.media_player_color); + ImGuiStyle& style = ImGui::GetStyle(); - style.Colors[ImGuiCol_PlotLines] = ImGui::ColorConvertU32ToFloat4(params.frametime_color); - style.Colors[ImGuiCol_PlotHistogram] = ImGui::ColorConvertU32ToFloat4(params.frametime_color); - style.Colors[ImGuiCol_WindowBg] = ImGui::ColorConvertU32ToFloat4(params.background_color); - style.Colors[ImGuiCol_Text] = ImGui::ColorConvertU32ToFloat4(params.text_color); + style.Colors[ImGuiCol_PlotLines] = convert(params.frametime_color); + style.Colors[ImGuiCol_PlotHistogram] = convert(params.frametime_color); + style.Colors[ImGuiCol_WindowBg] = convert(params.background_color); + style.Colors[ImGuiCol_Text] = convert(params.text_color); style.CellPadding.y = -2; } +// TODO probably needs colorspace check too +static void convert_colors_vk(VkFormat format, struct swapchain_stats& sw_stats, struct overlay_params& params) +{ + bool do_conv = false; + switch (format) { + case VK_FORMAT_R8_SRGB: + case VK_FORMAT_R8G8_SRGB: + case VK_FORMAT_R8G8B8_SRGB: + case VK_FORMAT_B8G8R8_SRGB: + case VK_FORMAT_R8G8B8A8_SRGB: + case VK_FORMAT_B8G8R8A8_SRGB: + case VK_FORMAT_A8B8G8R8_SRGB_PACK32: + case VK_FORMAT_BC1_RGB_SRGB_BLOCK: + case VK_FORMAT_BC1_RGBA_SRGB_BLOCK: + case VK_FORMAT_BC2_SRGB_BLOCK: + case VK_FORMAT_BC3_SRGB_BLOCK: + case VK_FORMAT_BC7_SRGB_BLOCK: + case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK: + case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK: + case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK: + case VK_FORMAT_ASTC_4x4_SRGB_BLOCK: + case VK_FORMAT_ASTC_5x4_SRGB_BLOCK: + case VK_FORMAT_ASTC_5x5_SRGB_BLOCK: + case VK_FORMAT_ASTC_6x5_SRGB_BLOCK: + case VK_FORMAT_ASTC_6x6_SRGB_BLOCK: + case VK_FORMAT_ASTC_8x5_SRGB_BLOCK: + case VK_FORMAT_ASTC_8x6_SRGB_BLOCK: + case VK_FORMAT_ASTC_8x8_SRGB_BLOCK: + case VK_FORMAT_ASTC_10x5_SRGB_BLOCK: + case VK_FORMAT_ASTC_10x6_SRGB_BLOCK: + case VK_FORMAT_ASTC_10x8_SRGB_BLOCK: + case VK_FORMAT_ASTC_10x10_SRGB_BLOCK: + case VK_FORMAT_ASTC_12x10_SRGB_BLOCK: + case VK_FORMAT_ASTC_12x12_SRGB_BLOCK: + case VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG: + case VK_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG: + case VK_FORMAT_PVRTC2_2BPP_SRGB_BLOCK_IMG: + case VK_FORMAT_PVRTC2_4BPP_SRGB_BLOCK_IMG: + do_conv = true; + break; + default: + break; + } + + convert_colors(do_conv, sw_stats, params); +} + static void setup_swapchain_data(struct swapchain_data *data, - const VkSwapchainCreateInfoKHR *pCreateInfo, struct overlay_params& params) + const VkSwapchainCreateInfoKHR *pCreateInfo) { + struct device_data *device_data = data->device; data->width = pCreateInfo->imageExtent.width; data->height = pCreateInfo->imageExtent.height; data->format = pCreateInfo->imageFormat; @@ -2257,9 +2363,7 @@ static void setup_swapchain_data(struct swapchain_data *data, ImGui::GetIO().IniFilename = NULL; ImGui::GetIO().DisplaySize = ImVec2((float)data->width, (float)data->height); - imgui_custom_style(params); - - struct device_data *device_data = data->device; + convert_colors_vk(pCreateInfo->imageFormat, data->sw_stats, device_data->instance->params); /* Render pass */ VkAttachmentDescription attachment_desc = {}; @@ -2459,7 +2563,7 @@ static VkResult overlay_CreateSwapchainKHR( VkResult result = device_data->vtable.CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain); if (result != VK_SUCCESS) return result; struct swapchain_data *swapchain_data = new_swapchain_data(*pSwapchain, device_data); - setup_swapchain_data(swapchain_data, pCreateInfo, device_data->instance->params); + setup_swapchain_data(swapchain_data, pCreateInfo); const VkPhysicalDeviceProperties& prop = device_data->properties; swapchain_data->sw_stats.version_vk.major = VK_VERSION_MAJOR(prop.apiVersion); diff --git a/src/overlay.h b/src/overlay.h index e0598a9d..871c8fcc 100644 --- a/src/overlay.h +++ b/src/overlay.h @@ -46,6 +46,18 @@ struct swapchain_stats { std::string deviceName; std::string gpuName; std::string driverName; + struct { + ImVec4 cpu, + gpu, + vram, + ram, + engine, + io, + frametime, + background, + text, + media_player; + } colors; }; struct fps_limit { @@ -78,9 +90,9 @@ void init_cpu_stats(overlay_params& params); void check_keybinds(struct swapchain_stats& sw_stats, struct overlay_params& params, uint32_t vendorID); void init_system_info(void); void FpsLimiter(struct fps_limit& stats); -void imgui_custom_style(struct overlay_params& params); void get_device_name(int32_t vendorID, int32_t deviceID, struct swapchain_stats& sw_stats); void calculate_benchmark_data(void); void create_fonts(const overlay_params& params, ImFont*& small_font, ImFont*& text_font); +void convert_colors(bool do_conv, struct swapchain_stats& sw_stats, struct overlay_params& params); #endif //MANGOHUD_OVERLAY_H