sRGB to Linear RGB (approximation)

  • Posts: 56
8 months 3 weeks ago - 3 months 2 weeks ago #1 by crabshank
A very simple shader that removes the inherent gamma correction in sRGB and approximates the linear RGB (where gamma=1) that a camera captures:
#include "ReShade.fxh"

float4 LinearisePass(float4 vpos : SV_Position, float2 texcoord : TexCoord) : SV_Target
{

float4 c0=tex2D(ReShade::BackBuffer, texcoord);

c0.rgb=(c0.rgb > 0.0404482362771082)?pow(abs((c0.rgb+0.055)/1.055),2.4):c0.rgb/12.92;
	
return c0;
}

float4 sRGBPass(float4 vpos : SV_Position, float2 texcoord : TexCoord) : SV_Target
{

float4 c0=tex2D(ReShade::BackBuffer, texcoord);

c0.rgb=( c0.rgb > 0.00313066844250063 )?1.055 * pow(abs(c0.rgb),1/2.4) - 0.055:12.92 *c0.rgb;

return c0;
}

technique sRGB_to_Linear
{
	pass
	{
		VertexShader = PostProcessVS;
		PixelShader = LinearisePass;
	}
}

technique Linear_to_sRGB
{
	pass
	{
		VertexShader = PostProcessVS;
		PixelShader = sRGBPass;
	}
}





Then re-grade as you see fit, I suggest applying gamma (choosing your own value or algorithm could lead to better results) first.

The image above uses the inverse setting.

Video shader here

I also have a version that uses an implicit method, but the gamma function is less accurate (straight gamma of 2.2) here
The following user(s) said Thank You: Wicked Sick, sajittarius, jas01, Deathmedic, Cul

Please Log in or Create an account to join the conversation.

  • Posts: 56
6 months 2 weeks ago #2 by crabshank
UPDATE: Included both transforms (to Linear and inverse).

Please Log in or Create an account to join the conversation.

  • Posts: 80
6 months 2 weeks ago #3 by sajittarius
Would it be possible to make a version of this with an adjustable gamma slider? I tried it in Star Wars Jedi: Fallen Order with HDR on and its amazing but could use a tiny tweak to the gamma values.

This shader works in HDR, whilst many other gamma ones (LevelsPlus, Tonemap, etc) do not, so i could see it helping in a lot of games where the HDR seems washed out.
The following user(s) said Thank You: crabshank

Please Log in or Create an account to join the conversation.

  • Posts: 56
6 months 2 weeks ago #4 by crabshank
UPDATE: Separated transforms into separate techniques.

@sajittarius using my grey gamma shader after this should help.
The following user(s) said Thank You: sajittarius

Please Log in or Create an account to join the conversation.

  • Posts: 80
6 months 1 week ago #5 by sajittarius
i think the grey gamma shader was what i was looking for in the first place (it works great for what i want just by tweaking that shader)

thank you :)

Please Log in or Create an account to join the conversation.