Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set the color space for ImGui away from UNORM (to SRGB or others) in Vulkan #8271

Open
BenMcAvoy opened this issue Dec 29, 2024 · 1 comment

Comments

@BenMcAvoy
Copy link

BenMcAvoy commented Dec 29, 2024

Version/Branch of Dear ImGui:

Version 1.91.0, Branch: docking (master/docking/etc.)

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_vulkan.cpp

Compiler, OS:

Linux + Clang

Full config/build information:

Dear ImGui 1.91.0 (19100)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=201703
define: __linux__
define: __GNUC__=14
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_glfw
io.BackendRendererName: imgui_impl_vulkan
io.ConfigFlags: 0x00000000
io.ConfigViewportsNoDecoration
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00001C0E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
 RendererHasViewports
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 1338.00,924.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

My Issue/Question:

I cannot seem to figure out how to switch the surface format from UNORM to SRGB or some other format when using Vulkan in ImGui. This means that all my colours for ImGui appear washed out and exploring issues and examples has not made it clearer unfortunately. Thanks so much for any help!

Screenshots/Video:

Surface is in VK_FORMAT_B8G8R8A8_SRGB
image

Surface is VK_FORMAT_B8G8R8A8_UNORM
image

Minimal, Complete and Verifiable Example code:

// If you were to want to use SRGB in your application like so:
// It seems undocumented how to switch colour spaces if it is possible
VkSurfaceFormatKHR App::ChooseSwapSurfaceFormat(
    const std::vector<VkSurfaceFormatKHR> &availableFormats) {
  for (const auto &availableFormat : availableFormats) {
    if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB &&
        availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
      return availableFormat;
    }
  }

  return availableFormats[0];
}

I have explored other issues but they do not seem to have any solutions apart from modifying the backend files, I would like not to resort to this, or is there simply no other way in the current API. Any help is greatly appreciated!

@BenMcAvoy
Copy link
Author

It is far from a solution and is at best a hack, but this at least makes the UI look correct:

  ImGuiStyle &style = ImGui::GetStyle();
  // Go through every colour and convert it to linear
  // This is because ImGui uses linear colours but we are using sRGB
  // This is a simple approximation of the conversion
  for (int i = 0; i < ImGuiCol_COUNT; i++) {
    /*float linear = (srgb <= 0.04045f) ? srgb / 12.92f : pow((srgb + 0.055f)
     * / 1.055f, 2.4f);*/

    ImVec4 &col = style.Colors[i];
    col.x = col.x <= 0.04045f ? col.x / 12.92f
                              : pow((col.x + 0.055f) / 1.055f, 2.4f);
    col.y = col.y <= 0.04045f ? col.y / 12.92f
                              : pow((col.y + 0.055f) / 1.055f, 2.4f);
    col.z = col.z <= 0.04045f ? col.z / 12.92f
                              : pow((col.z + 0.055f) / 1.055f, 2.4f);
  }

I saw there were already some PRs to do with colour space in ImGui Vulkan, specifically #8053 but it seems to be a draft so I'm unsure and they are most likely more experienced than me, is it worth me attempting to make a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants