Skip to content

Commit

Permalink
ImGui renderer: added option to control srgb->linear color conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Jun 9, 2023
1 parent 66e2439 commit e2b3d90
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
18 changes: 10 additions & 8 deletions Imgui/interface/ImGuiDiligentRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct IShaderResourceVariable;
struct ImGuiDiligentCreateInfo;
enum TEXTURE_FORMAT : Uint16;
enum SURFACE_TRANSFORM : Uint32;
enum IMGUI_COLOR_CONVERSION_MODE : Uint8;

class ImGuiDiligentRenderer
{
Expand Down Expand Up @@ -78,14 +79,15 @@ class ImGuiDiligentRenderer
RefCntAutoPtr<IShaderResourceBinding> m_pSRB;
IShaderResourceVariable* m_pTextureVar = nullptr;

const TEXTURE_FORMAT m_BackBufferFmt;
const TEXTURE_FORMAT m_DepthBufferFmt;
Uint32 m_VertexBufferSize = 0;
Uint32 m_IndexBufferSize = 0;
Uint32 m_RenderSurfaceWidth = 0;
Uint32 m_RenderSurfaceHeight = 0;
SURFACE_TRANSFORM m_SurfacePreTransform = SURFACE_TRANSFORM_IDENTITY;
bool m_BaseVertexSupported = false;
const TEXTURE_FORMAT m_BackBufferFmt;
const TEXTURE_FORMAT m_DepthBufferFmt;
Uint32 m_VertexBufferSize = 0;
Uint32 m_IndexBufferSize = 0;
Uint32 m_RenderSurfaceWidth = 0;
Uint32 m_RenderSurfaceHeight = 0;
SURFACE_TRANSFORM m_SurfacePreTransform = SURFACE_TRANSFORM_IDENTITY;
const IMGUI_COLOR_CONVERSION_MODE m_ColorConversionMode;
bool m_BaseVertexSupported = false;
};

} // namespace Diligent
21 changes: 21 additions & 0 deletions Imgui/interface/ImGuiImplDiligent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ enum SURFACE_TRANSFORM : Uint32;

class ImGuiDiligentRenderer;

/// Conversion mode to apply to imgui colors.
///
/// \remarks Imgui colors are defined in sRGB space.
/// Depending on the use case, they may need
/// to be converted to linear space.
enum IMGUI_COLOR_CONVERSION_MODE : Uint8
{
/// Select the color conversion mode automatically:
/// * Use SRGB_TO_LINEAR mode for sRGB framebuffers
/// * Use NONE mode for non-sRGB framebuffers
IMGUI_COLOR_CONVERSION_MODE_AUTO = 0,

/// Always perform srgb-to-linear conversion.
IMGUI_COLOR_CONVERSION_MODE_SRGB_TO_LINEAR,

/// Do not perform any color conversion.
IMGUI_COLOR_CONVERSION_MODE_NONE
};

struct ImGuiDiligentCreateInfo
{
static constexpr Uint32 DefaultInitialVBSize = 1024;
Expand All @@ -51,6 +70,8 @@ struct ImGuiDiligentCreateInfo
TEXTURE_FORMAT BackBufferFmt = {};
TEXTURE_FORMAT DepthBufferFmt = {};

IMGUI_COLOR_CONVERSION_MODE ColorConversion = IMGUI_COLOR_CONVERSION_MODE_AUTO;

Uint32 InitialVertexBufferSize = DefaultInitialVBSize;
Uint32 InitialIndexBufferSize = DefaultInitialIBSize;

Expand Down
13 changes: 7 additions & 6 deletions Imgui/src/ImGuiDiligentRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,12 @@ fragment PSOut ps_main(VSOut in [[stage_in]],

ImGuiDiligentRenderer::ImGuiDiligentRenderer(const ImGuiDiligentCreateInfo& CI) :
// clang-format off
m_pDevice {CI.pDevice},
m_BackBufferFmt {CI.BackBufferFmt},
m_DepthBufferFmt {CI.DepthBufferFmt},
m_VertexBufferSize{CI.InitialVertexBufferSize},
m_IndexBufferSize {CI.InitialIndexBufferSize}
m_pDevice {CI.pDevice},
m_BackBufferFmt {CI.BackBufferFmt},
m_DepthBufferFmt {CI.DepthBufferFmt},
m_VertexBufferSize {CI.InitialVertexBufferSize},
m_IndexBufferSize {CI.InitialIndexBufferSize},
m_ColorConversionMode{CI.ColorConversion}
// clang-format on
{
//Check base vertex support
Expand Down Expand Up @@ -469,7 +470,7 @@ void ImGuiDiligentRenderer::CreateDeviceObjects()
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_DEFAULT;

const auto IsSRB = GetTextureFormatAttribs(m_BackBufferFmt).ComponentType == COMPONENT_TYPE_UNORM_SRGB;
if (IsSRB)
if ((m_ColorConversionMode == IMGUI_COLOR_CONVERSION_MODE_AUTO && IsSRB) || (m_ColorConversionMode == IMGUI_COLOR_CONVERSION_MODE_SRGB_TO_LINEAR))
{
static constexpr ShaderMacro Macros[] =
{
Expand Down

0 comments on commit e2b3d90

Please sign in to comment.