Skip to content

Commit

Permalink
Gamma correction config var
Browse files Browse the repository at this point in the history
  • Loading branch information
metallicafan212 committed Jul 4, 2023
1 parent 61c12b7 commit 1f4471e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Inc/ICBINDx11Drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ class UICBINDx11RenderDevice : public URenderDevice
// Metallicafan212: TODO! Float to provide super/min'd resolution scaling
FLOAT ResolutionScale;

// Metallicafan212: Gamma correction value for the final output
FLOAT Gamma;


// Metallicafan212: Versions to check on lock if they changed
INT LastAASamples;
Expand Down
14 changes: 10 additions & 4 deletions Shaders/MSAAResolve.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ shared cbuffer CommonBuffer : register (b0)
float CubicB : packoffset(c12.z);
float CubicC : packoffset(c12.w);
int FilterType : packoffset(c13.x);
float3 Pad3 : packoffset(c13.y);
float Gamma : packoffset(c13.y);
float2 Pad3 : packoffset(c13.z);
float4 SampleOffsets[8] : packoffset(c14);
};

Expand Down Expand Up @@ -302,7 +303,10 @@ float4 PxShader(PSInput input) : SV_TARGET
sum = lerp(sum, ZeroBased, a);//lerp(ZeroBased, sum, a);
*/

return float4(sum, 1.0f);
// Metallicafan212: Gamma correct it
float OverGamma = 1.0f / Gamma;
return float4(pow(sum, float3(OverGamma, OverGamma, OverGamma)), 1.0f);

}

// Metallicafan212: Resolve the MSAA texture to this current pixel
Expand Down Expand Up @@ -420,8 +424,10 @@ void CSMain( uint3 dispatchThreadID : SV_DispatchThreadID )

float3 output = sum / max(totalWeight, 0.00001f);
output = max(output, 0.0f);

Out[dispatchThreadID.xy] = float4(output, 1.0f);

// Metallicafan212: Gamma correct it
float OverGamma = 1.0f / Gamma;
Out[dispatchThreadID.xy] = float4(pow(output, float3(OverGamma, OverGamma, OverGamma)), 1.0f);

//return float4(output, 1.0f);

Expand Down
15 changes: 12 additions & 3 deletions Shaders/ResScaling.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ shared cbuffer CommonBuffer : register (b0)

// Metallicafan212: The info we use for this specific shader
float ResolutionScale : packoffset(c12.x);
float3 Pad3 : packoffset(c12.y);
float Gamma : packoffset(c12.y);
float2 Pad3 : packoffset(c12.z);
};

// Metallicafan212: HACK!!!! This includes this twice to define the final color function, as HLSL cannot do out of order compiling
Expand Down Expand Up @@ -69,8 +70,12 @@ float4 PxShader(PSInput input) : SV_TARGET
float2 fUV = frac(UV);
UV = iUV + fUV * fUV * (3.0 - 2.0 * fUV);
UV = (UV - 0.5) / SW;

return float4(Screen.SampleBias(ScreenState, UV, 0.0f).xyz, 1.0f);//float4(Screen.SampleBias(ScreenState, input.uv, 0.0f).xyz, 1.0f);

float3 TexColor = Screen.SampleBias(ScreenState, UV, 0.0f).xyz;

// Metallicafan212: Gamma correct it
float OverGamma = 1.0f / Gamma;
return float4(pow(TexColor, float3(OverGamma, OverGamma, OverGamma)), 1.0f);
}

// Metallicafan212: Compute shader version (slow!!!!)
Expand Down Expand Up @@ -188,5 +193,9 @@ void CSMain( uint3 pixelID : SV_DispatchThreadID )
Result.xyz /= 2.0f;
*/

// Metallicafan212: Gamma correct it
float OverGamma = 1.0f / Gamma;
Result = float4(pow(Result, float3(OverGamma, OverGamma, OverGamma)), 1.0f);

Out[pixelID.xy] = Result;
}
4 changes: 3 additions & 1 deletion Src/ResScalingShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ struct FResScaleVars : FShaderVarCommon
{
// Metallicafan212: The info we use for this specific shader
FLOAT ResolutionScale;
FLOAT Pad3[3];
FLOAT Gamma;
FLOAT Pad3[2];
};

// Metallicafan212: This file defines the generic (no texture) rendering shader
Expand Down Expand Up @@ -91,6 +92,7 @@ void FD3DResScalingShader::WriteConstantBuffer(void* InMem)
FResScaleVars* SDef = (FResScaleVars*)InMem;

SDef->ResolutionScale = ParentDevice->ResolutionScale;
SDef->Gamma = ParentDevice->Gamma;

unguard;
}
2 changes: 2 additions & 0 deletions Src/UnConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ void UICBINDx11RenderDevice::StaticConstructor()
// Metallicafan212: TODO! Should this be 1.2f by default? I'll leave it all alone in case there's some user out there that will complain lol
AddFloatProp(CPP_PROP(ResolutionScale), 1.0f);

AddFloatProp(CPP_PROP(Gamma), 1.0f);

unguard;
}

Expand Down
6 changes: 5 additions & 1 deletion Src/UnICBINDDx11Drv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,9 @@ void UICBINDx11RenderDevice::Unlock(UBOOL Blit)
m_D3DDeviceContext->ResolveSubresource(m_BackBuffTex, 0, m_ScreenBuffTex, 0, DXGI_FORMAT_B8G8R8A8_UNORM);
}
}
else if (ResolutionScale != 1.0f)
// Metallicafan212: Always use the resolution scaling shader, so we can do final effects on the screen
else
//else if (ResolutionScale != 1.0f)
{
#if USE_RES_COMPUTE
// Metallicafan212: Use a compute shader instead!!!
Expand Down Expand Up @@ -1902,10 +1904,12 @@ void UICBINDx11RenderDevice::Unlock(UBOOL Blit)
RestoreRenderTarget();
#endif
}
/*
else
{
m_D3DDeviceContext->CopyResource(m_BackBuffTex, m_ScreenBuffTex);
}
*/

constexpr DXGI_PRESENT_PARAMETERS Parm{ 0, nullptr, nullptr, nullptr };
HRESULT hr = m_D3DSwapChain->Present1(bVSync ? 1 : 0, (bAllowTearing && !bFullscreen && !bVSync ? DXGI_PRESENT_ALLOW_TEARING : 0), &Parm);//m_D3DSwapChain->Present(0, 0);
Expand Down
6 changes: 5 additions & 1 deletion Src/UnMSAAShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ struct FMSAAVars : FShaderVarCommon
FLOAT CubicB;
FLOAT CubicC;
INT FilterType;
FLOAT Pad3[3];
FLOAT Gamma;
FLOAT Pad3[2];
FPlane SampleOffsets[8];
};

Expand Down Expand Up @@ -109,6 +110,9 @@ void FD3DMSAAShader::WriteConstantBuffer(void* InMem)
SDef->CubicC = ParentDevice->MSAACubicC;
SDef->FilterType = ParentDevice->MSAAFilterType;

// Metallicafan212: Copy the gamma over
SDef->Gamma = ParentDevice->Gamma;

// Metallicafan212: Sample offsets
switch (ParentDevice->NumAASamples)
{
Expand Down

0 comments on commit 1f4471e

Please sign in to comment.