Skip to content
Open
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions Shaders/smolbbsoop/smolbbsoop_Global.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,41 @@
ui_tooltip = "Input HDR Peak Brightness. Setting the correct value ensures the conversion is as accurate as possible";
> = 1000;

//============================================================================================
// Color Spaces Conversion Matrices and Functions
// From https://github.com/clshortfuse/renodx/tree/main/src/shaders
//============================================================================================

static const float3x3 BT709_TO_XYZ_MAT = float3x3(
0.4123907993f, 0.3575843394f, 0.1804807884f,
0.2126390059f, 0.7151686788f, 0.0721923154f,
0.0193308187f, 0.1191947798f, 0.9505321522f);

static const float3x3 XYZ_TO_BT709_MAT = float3x3(
3.2409699419f, -1.5373831776f, -0.4986107603f,
-0.9692436363f, 1.8759675015f, 0.0415550574f,
0.0556300797f, -0.2039769589f, 1.0569715142f);

static const float3x3 BT2020_TO_XYZ_MAT = float3x3(
0.6369580483f, 0.1446169036f, 0.1688809752f,
0.2627002120f, 0.6779980715f, 0.0593017165f,
0.0000000000f, 0.0280726930f, 1.0609850577f);

static const float3x3 XYZ_TO_BT2020_MAT = float3x3(
1.7166511880f, -0.3556707838f, -0.2533662814f,
-0.6666843518f, 1.6164812366f, 0.0157685458f,
0.0176398574f, -0.0427706133f, 0.9421031212f);

//static const float3x3 BT709_TO_BT2020_MAT = mul(XYZ_TO_BT2020_MAT, BT709_TO_XYZ_MAT);
//static const float3x3 BT2020_TO_BT709_MAT = mul(XYZ_TO_BT709_MAT, BT2020_TO_XYZ_MAT);

float3 Bt709FromBt2020(float3 bt2020){
return mul(mul(XYZ_TO_BT709_MAT, BT2020_TO_XYZ_MAT), bt2020);
}
float3 Bt2020FromBt709(float3 bt709){
return mul(mul(XYZ_TO_BT2020_MAT, BT709_TO_XYZ_MAT), bt709);
}

//==============================================================
// Functions (After)
//==============================================================
Expand Down
9 changes: 5 additions & 4 deletions Shaders/smolbbsoop_HDR_Converter.fx
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
float4 ConvertBufferBefore(float4 pos : SV_Position, float2 texcoord : TEXCOORD) : SV_Target
{
float3 HDRColour = tex2D(ReShade::BackBuffer, texcoord).rgb;
float3 LinearColour = PQToLinear(HDRColour.rgb);
float3 LinearBt2020Colour = PQToLinear(HDRColour.rgb);
float3 LinearBt709Colour = Bt2020FromBt709(LinearBt2020Colour);

// Apply tonemap in linear space, then convert to sRGB
float3 TonemappedLinear = Reinhard(LinearColour);
float3 TonemappedLinear = Reinhard(LinearBt709Colour);
float3 sRGBColour = LinearTosRGB(TonemappedLinear);

return float4(sRGBColour, 1.0);
Expand All @@ -121,8 +122,8 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Convert to linear, then inverse tonemap
float3 LinearColour = sRGBToLinear(sRGBColour);
float3 InvTonemappedLinear = InvReinhard(LinearColour);

float3 HDRColour = LinearToPQ(InvTonemappedLinear);
float3 LinearBt2020Colour = Bt709FromBt2020(InvTonemappedLinear);
float3 HDRColour = LinearToPQ(LinearBt2020Colour);

return float4(HDRColour, 1.0);
}
Expand Down