Skip to content

Commit

Permalink
Fixes to SDR and HDR10
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxG2D authored Jul 3, 2024
1 parent 625ecaa commit 1bff11c
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 41 deletions.
21 changes: 13 additions & 8 deletions Shaders/HDRBloom.fx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// Defines
#ifndef REMOVE_SDR_VALUES
#define REMOVE_SDR_VALUES 1
#define REMOVE_SDR_VALUES 0
#endif

#ifndef ADDITIONAL_BLUR_PASS
Expand Down Expand Up @@ -226,12 +226,13 @@ float4 PreProcessPS(float4 pixel : SV_POSITION, float2 texcoord : TEXCOORD0) : S
// HDR10 BT.2020 PQ
if (inColorSpace == 2)
{
color.rgb = clamp(color.rgb, -FLT16_MAX, FLT16_MAX);
//color.rgb = BT2020_2_BT709(color.rgb);
color.rgb = PQToLinear(color.rgb);
color.rgb = BT2020_2_BT709(color.rgb);
}

#if LINEAR_CONVERSION
color.rgb = sRGB_to_linear(color.rgb);
color.rgb = sRGBToLinear(color.rgb);
#endif

// Inv Tonemapping
Expand All @@ -253,7 +254,6 @@ float4 PreProcessPS(float4 pixel : SV_POSITION, float2 texcoord : TEXCOORD0) : S
}
#endif


// Bloom Brightness
color.rgb *= UI_BLOOM_BRIGHTNESS;

Expand Down Expand Up @@ -373,13 +373,18 @@ float4 BlendBloomPS(float4 pixel : SV_POSITION, float2 texcoord : TEXCOORD0) : S
//bloom /= 3.0;

uint inColorSpace = UI_IN_COLOR_SPACE;

// HDR10 BT.2020 PQ

if (inColorSpace == 2)
{
bloom.rgb = BT709_2_BT2020(bloom.rgb);
bloom.rgb = LinearToPQ(bloom.rgb);
bloom.rgb = fixNAN(bloom.rgb);
//bloom.rgb = DisplayMapColor(bloom.rgb, luminance, HDR_MAX_NITS);
//bloom.rgb = BT709_2_BT2020(bloom.rgb);
bloom.rgb = LinearToPQ(bloom.rgb);
}



if (UI_BLOOM_BLENDING_TYPE == Overlay)
{
finalcolor.rgb = UI_BLOOM_SHOW_DEBUG
Expand All @@ -397,7 +402,7 @@ float4 BlendBloomPS(float4 pixel : SV_POSITION, float2 texcoord : TEXCOORD0) : S
// SDR Clamp
if (inColorSpace == 0)
{
finalcolor = saturate(finalcolor);
finalcolor = clamp(finalcolor, 0.0, 1.0);
}
return finalcolor;

Expand Down
59 changes: 39 additions & 20 deletions Shaders/HDRMotionBlur.fx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@
#define VELOCITY_SCALE 50.0
#define HALF_SAMPLES (UI_BLUR_SAMPLES_MAX / 2)

/**
uniform float HDR_MAX_NITS
<
ui_label = "HDR display peak brightness (max nits)";
ui_tooltip = "Set it equal or higher the Auto HDR max brightness to avoid double tonemapping";
ui_category = "HDR tonemapping";
ui_type = "drag";
ui_min = sRGB_max_nits;
ui_max = 10000.f;
ui_step = 1.f;
> = 750.f;
**/

// UI
uniform int README
<
Expand Down Expand Up @@ -235,29 +248,32 @@ float4 BlurPS(float4 p : SV_Position, float2 texcoord : TEXCOORD) : SV_Target
float SampleDistVector = dot(SampleDist, 1.0);
float4 SummedSamples = 0;
float4 Sampled = 0;
//Sampled = tex2D(SamplerColor, texcoord);
float4 Color = tex2D(SamplerColor, texcoord);
float2 NoiseOffset = 0;
if (abs(SampleDistVector) > 0.001)
{
NoiseOffset = BlueNoise(texcoord - SampleDist * (0 - HALF_SAMPLES)) * 0.001);
}

}
// Blur Loop
for (int s = 0; s < UI_BLUR_SAMPLES_MAX; s++)
{
Sampled = tex2D(SamplerColor, texcoord - SampleDist * (s - HALF_SAMPLES) + (NoiseOffset * UI_BLUR_BLUE_NOISE));
// HDR10 BT.2020 PQ
if (inColorSpace == 2)
{
Sampled.rgb = PQToLinear(Sampled.rgb);
Sampled.rgb = BT2020_2_BT709(Sampled.rgb);
}

#if LINEAR_CONVERSION
Sampled.rgb = sRGB_to_linear(Sampled.rgb);
#endif

[branch]
if (inColorSpace == 2)
{
Sampled.rgb = clamp(Sampled.rgb, -FLT16_MAX, FLT16_MAX);
//Sampled.rgb = BT2020_2_BT709(Sampled.rgb);
Sampled.rgb = PQToLinear(Sampled.rgb);
//Sampled.rgb = min(max(Sampled.rgb, -PQMaxWhitePoint),PQMaxWhitePoint);
}

#if LINEAR_CONVERSION
Sampled.rgb = sRGBToLinear(Sampled.rgb);
#endif

SummedSamples += Sampled / UI_BLUR_SAMPLES_MAX;
Color.rgb = max(Color.rgb, Sampled.rgb);
}
Expand Down Expand Up @@ -285,21 +301,24 @@ float4 BlurPS(float4 p : SV_Position, float2 texcoord : TEXCOORD) : SV_Target

[branch]
#if LINEAR_CONVERSION
Finalcolor.rgb = linear_to_sRGB(Finalcolor.rgb);
Finalcolor.rgb = LinearTosRGB(Finalcolor.rgb);
#endif

// HDR10 BT.2020 PQ
if (inColorSpace == 2)
{
Finalcolor.rgb = fixNAN(Finalcolor.rgb);
//Finalcolor.rgb = DisplayMapColor(Finalcolor.rgb, luminance, HDR_MAX_NITS);
//Finalcolor.rgb = BT709_2_BT2020(Finalcolor.rgb);
Finalcolor.rgb = LinearToPQ(Finalcolor.rgb);
}

// SDR
if (inColorSpace == 0)
{
Finalcolor *= 1.0 / max(dot(SummedSamples.rgb, lumCoeffsRGB), 1.0);
clamp(Finalcolor, 0.0, 1.0);
}
// HDR10 BT.2020 PQ
if (inColorSpace == 2)
{
Finalcolor.rgb = BT709_2_BT2020(Finalcolor.rgb);
Finalcolor.rgb = LinearToPQ(Finalcolor.rgb);
}

#if DEPTH_ENABLE
Finalcolor = UI_SHOW_DEPTH
Expand Down
73 changes: 60 additions & 13 deletions Shaders/HDRShadersFunctions.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#endif

#define PI 3.1415927410125732421875f
#define THRESHOLD_ANGLE (PI / 4.0) // Adjust this threshold as needed

#define UINT_MAX 4294967295
#define INT_MAX 2147483647
Expand Down Expand Up @@ -197,6 +196,11 @@ float SafeDivide(float a, float b)
//CONVERSIONS - LUMA
/////////////////////////////////////////////

float Luminance(float3 color, float3 lumCoeff)
{
return dot(color, lumCoeff);
}

float sRGBToLinear(float color)
{
const float a = 0.055f;
Expand Down Expand Up @@ -242,7 +246,7 @@ float3 LinearTosRGB(float3 Color)

float3 LinearToPQ(float3 linearCol)
{
linearCol /= PQMaxWhitePoint;
linearCol /= HDR10_max_nits;

float3 colToPow = pow(linearCol, PQ_constant_N);
float3 numerator = PQ_constant_C1 + PQ_constant_C2 * colToPow;
Expand All @@ -257,9 +261,10 @@ float3 PQToLinear(float3 ST2084)
float3 colToPow = pow(ST2084, 1.0f / PQ_constant_M);
float3 numerator = max(colToPow - PQ_constant_C1, 0.f);
float3 denominator = PQ_constant_C2 - (PQ_constant_C3 * colToPow);
//denominator = max(denominator, 1e-10f);
float3 linearColor = pow(numerator / denominator, 1.f / PQ_constant_N);

linearColor *= PQMaxWhitePoint;
linearColor *= HDR10_max_nits;

return linearColor;
}
Expand All @@ -275,6 +280,14 @@ float LumaCompress(float val, float MaxValue, float ShoulderStart, float Pow)
return val <= ShoulderStart ? val : v2;
}

float3 DisplayMapColor (float3 color, float luma, float HdrMaxNits)
{
luma = Luminance(color, lumCoeffHDR);
float maxOutputLuminance = HdrMaxNits / sRGB_max_nits;
float compressedHDRLuminance = LumaCompress(luma, maxOutputLuminance, maxOutputLuminance, 1);
return color * compressedHDRLuminance / luma;
}

/////////////////////////////////////////////
//CONVERSIONS - CHROMA
/////////////////////////////////////////////
Expand All @@ -291,6 +304,10 @@ static const float3x3 XYZ_2_AP1_MAT = float3x3(
1.6410233797, -0.3248032942, -0.2364246952,
-0.6636628587, 1.6153315917, 0.0167563477,
0.0117218943, -0.0082844420, 0.9883948585);
static const float3x3 AP1_2_XYZ_MAT = float3x3(
0.6624541811, 0.1340042065, 0.1561876870,
0.2722287168, 0.6740817658, 0.0536895174,
-0.0055746495, 0.0040607335, 1.0103391003);
static const float3x3 D65_2_D60_CAT = float3x3(
1.01303, 0.00610531, -0.014971,
0.00769823, 0.998165, -0.00503203,
Expand All @@ -299,10 +316,6 @@ static const float3x3 D60_2_D65_CAT = float3x3(
0.987224, -0.00611327, 0.0159533,
-0.00759836, 1.00186, 0.00533002,
0.00307257, -0.00509595, 1.08168);
static const float3x3 AP1_2_XYZ_MAT = float3x3(
0.6624541811, 0.1340042065, 0.1561876870,
0.2722287168, 0.6740817658, 0.0536895174,
-0.0055746495, 0.0040607335, 1.0103391003);
static const float3 AP1_RGB2Y = float3(
0.2722287168,
0.6740817658,
Expand Down Expand Up @@ -348,16 +361,55 @@ static const float3x3 BT2020_2_AP1_MAT = float3x3(
//CONVERSIONS - CHROMA (FUNCTIONS)
/////////////////////////////////////////////

float3 XYZ_2_sRGB_MAT(float3 color)
{
return mul(XYZ_2_sRGB_MAT, color);
}
float3 sRGB_2_XYZ_MAT(float3 color)
{
return mul(sRGB_2_XYZ_MAT, color);
}


float3 XYZ_2_AP1_MAT(float3 color)
{
return mul(XYZ_2_AP1_MAT, color);
}
float3 AP1_2_XYZ_MAT(float3 color)
{
return mul(AP1_2_XYZ_MAT, color);
}


float3 BT709_2_BT2020(float3 color)
{
return mul(BT709_2_BT2020, color);
}

float3 BT2020_2_BT709(float3 color)
{
return mul(BT2020_2_BT709, color);
}


float3 XYZ_2_BT2020_MAT(float3 color)
{
return mul(XYZ_2_BT2020_MAT, color);
}
float3 BT2020_2_XYZ_MAT(float3 color)
{
return mul(BT2020_2_XYZ_MAT, color);
}


float3 AP1_2_BT2020_MAT(float3 color)
{
return mul(AP1_2_BT2020_MAT, color);
}
float3 BT2020_2_AP1_MAT(float3 color)
{
return mul(BT2020_2_AP1_MAT, color);
}

/////////////////////////////////////////////
//OKLAB
/////////////////////////////////////////////
Expand Down Expand Up @@ -691,11 +743,6 @@ float3 YUVtoRGB(float3 yuv)
//SATURATION - FUNCTIONS
/////////////////////////////////////////////

float Luminance(float3 color, float3 lumCoeff)
{
return dot(color, lumCoeff);
}

float3 LumaSaturation(float3 color, float amount)
{
float luminanceHDR = Luminance(color, lumCoeffHDR);
Expand Down

0 comments on commit 1bff11c

Please sign in to comment.