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

Added support for color space conversion in DX12 shader #7904

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion backends/imgui_impl_dx12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_ConvertSRGBToLinear = false; // TODO: Enable to degamma into linear target as required.

// DirectX data
struct ImGui_ImplDX12_RenderBuffers;
struct ImGui_ImplDX12_Data
Expand Down Expand Up @@ -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;\
Expand All @@ -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_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();
Expand Down