From 6bd997d2bf8fcd0e4688e1a24a0d1533ef15c511 Mon Sep 17 00:00:00 2001 From: Steve Williams Date: Mon, 19 Aug 2024 20:02:18 +0100 Subject: [PATCH 1/2] Added support for color space conversion generically over all ImGui operations in pixel shader as required. --- backends/imgui_impl_dx12.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/backends/imgui_impl_dx12.cpp b/backends/imgui_impl_dx12.cpp index 32f1622cd46d..89e8ff1c7e8c 100644 --- a/backends/imgui_impl_dx12.cpp +++ b/backends/imgui_impl_dx12.cpp @@ -53,6 +53,9 @@ #pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below. #endif +// TODO: Pass in configuration parameter in ImGui standard manner. +bool ImGuiConfigFlags_OutputDegamma = false; // TODO: Enable to degamma into linear target as required. + // DirectX data struct ImGui_ImplDX12_RenderBuffers; struct ImGui_ImplDX12_Data @@ -601,7 +604,11 @@ bool ImGui_ImplDX12_CreateDeviceObjects() // Create the pixel shader { - static const char* pixelShader = + + // ADV_SW_PATCH: Added pixel shader variant that degamma's output for use in non _SRGB linear render targets + // that require degamma to be performed in GPU software + + static const char* pixelShader_straight = "struct PS_INPUT\ {\ float4 pos : SV_POSITION;\ @@ -616,7 +623,27 @@ bool ImGui_ImplDX12_CreateDeviceObjects() float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \ return out_col; \ }"; + static const char* pixelShader_linear_target = + "struct PS_INPUT\ + {\ + float4 pos : SV_POSITION;\ + float4 col : COLOR0;\ + float2 uv : TEXCOORD0;\ + };\ + SamplerState sampler0 : register(s0);\ + Texture2D texture0 : register(t0);\ +float3 RemoveSRGBCurve (float3 x)\ +{ return ( abs (x) < 0.04045f ) ?\ + sign (x) * ( abs (x) / 12.92f ) :\ + sign (x) * pow ( ( abs (x) + 0.055f ) / 1.055f, 2.4f ); }\ + float4 main(PS_INPUT input) : SV_Target\ + {\ + float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \ + out_col.rgb = RemoveSRGBCurve(out_col.rgb); \ + return out_col; \ + }"; + auto pixelShader = ImGuiConfigFlags_OutputDegamma ? pixelShader_linear_target : pixelShader_straight; if (FAILED(D3DCompile(pixelShader, strlen(pixelShader), nullptr, nullptr, nullptr, "main", "ps_5_0", 0, 0, &pixelShaderBlob, nullptr))) { vertexShaderBlob->Release(); From b2b1940e2a486c8469e4062d7c275932a685f1d2 Mon Sep 17 00:00:00 2001 From: Steve Williams Date: Tue, 20 Aug 2024 05:56:24 +0100 Subject: [PATCH 2/2] . --- backends/imgui_impl_dx12.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backends/imgui_impl_dx12.cpp b/backends/imgui_impl_dx12.cpp index 89e8ff1c7e8c..1c92562fa2ee 100644 --- a/backends/imgui_impl_dx12.cpp +++ b/backends/imgui_impl_dx12.cpp @@ -54,7 +54,7 @@ #endif // TODO: Pass in configuration parameter in ImGui standard manner. -bool ImGuiConfigFlags_OutputDegamma = false; // TODO: Enable to degamma into linear target as required. +bool ImGuiConfigFlags_ConvertSRGBToLinear = false; // TODO: Enable to degamma into linear target as required. // DirectX data struct ImGui_ImplDX12_RenderBuffers; @@ -643,7 +643,7 @@ float3 RemoveSRGBCurve (float3 x)\ return out_col; \ }"; - auto pixelShader = ImGuiConfigFlags_OutputDegamma ? pixelShader_linear_target : pixelShader_straight; + auto pixelShader = ImGuiConfigFlags_ConvertSRGBToLinear ? pixelShader_linear_target : pixelShader_straight; if (FAILED(D3DCompile(pixelShader, strlen(pixelShader), nullptr, nullptr, nullptr, "main", "ps_5_0", 0, 0, &pixelShaderBlob, nullptr))) { vertexShaderBlob->Release();