Skip to content

Commit

Permalink
Median brightness for smoother falloff
Browse files Browse the repository at this point in the history
  • Loading branch information
papadanku committed Jan 2, 2022
1 parent 5a1e99e commit 1efc2ff
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
13 changes: 9 additions & 4 deletions shaders/cBloom.fx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ uniform float _Saturation <
ui_type = "drag";
ui_min = 0.0;
ui_label = "Saturation";
> = 1.0;
> = 4.0;

uniform float3 _ColorShift <
ui_type = "color";
Expand All @@ -38,7 +38,7 @@ uniform float _Intensity <
ui_type = "drag";
ui_min = 0.0;
ui_label = "Color Intensity";
> = 1.0;
> = 8.0;

texture2D _RenderColor : COLOR;

Expand Down Expand Up @@ -319,20 +319,25 @@ float4 UpsamplePS(sampler2D Source, float4 TexCoord[3])
return Output / 16.0;
}

float Med3(float x, float y, float z)
{
return max(min(x, y), min(max(x, y), z));
}

void PrefilterPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
{
const float Knee = mad(_Threshold, _Smooth, 1e-5f);
const float3 Curve = float3(_Threshold - Knee, Knee * 2.0, 0.25 / Knee);
float4 Color = tex2D(_SampleColor, TexCoord);

// Under-threshold
float Brightness = max(Color.r, max(Color.g, Color.b));
float Brightness = Med3(Color.r, Color.g, Color.b);
float ResponseCurve = clamp(Brightness - Curve.x, 0.0, Curve.y);
ResponseCurve = Curve.z * ResponseCurve * ResponseCurve;

// Combine and apply the brightness response curve
Color = Color * max(ResponseCurve, Brightness - _Threshold) / max(Brightness, 1e-10);
Brightness = max(max(Color.r, Color.g), Color.b);
Brightness = Med3(Color.r, Color.g, Color.b);
OutputColor0 = saturate(lerp(Brightness, Color.rgb, _Saturation)) * _ColorShift;

// Set alpha to 1.0 so we can see the complete results in ReShade's statistics
Expand Down
6 changes: 3 additions & 3 deletions shaders/cInterpolation.fx
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ void VerticalBlurPS1(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0,
OutputColor0 = GaussianBlur(_SampleData1, TexCoord, Offsets);
}

float4 Median(float4 A, float4 B, float4 C)
float4 Med3(float4 x, float4 y, float4 z)
{
return max(min(A, B), min(max(A, B), C));
return max(min(x, y), min(max(x, y), z));
}

void InterpolatePS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
Expand All @@ -321,7 +321,7 @@ void InterpolatePS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, o
float4 FrameP = tex2D(_SampleFrame1, TexCoord);
float4 FrameC = tex2D(_SampleFrame0, TexCoord);
float4 FrameA = lerp(FrameC, FrameP, 64.0 / 256.0);
OutputColor0 = Median(FrameA, FrameF, FrameB);
OutputColor0 = Med3(FrameA, FrameF, FrameB);
}

void CopyPS2(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
Expand Down
10 changes: 8 additions & 2 deletions shaders/cThreshold.fx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ void PostProcessVS(in uint ID : SV_VERTEXID, inout float4 Position : SV_POSITION
Position = float4(TexCoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
}

float Med3(float x, float y, float z)
{
return max(min(x, y), min(max(x, y), z));
}

/* [Pixel Shaders] */

void ThresholdPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out float4 OutputColor0 : SV_TARGET0)
Expand All @@ -55,12 +60,13 @@ void ThresholdPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, out
float4 Color = tex2D(_SampleColor, TexCoord);

// Under-threshold
float Brightness = max(Color.r, max(Color.g, Color.b));
float Brightness = Med3(Color.r, Color.g, Color.b);
float ResponseCurve = clamp(Brightness - Curve.x, 0.0, Curve.y);
ResponseCurve = Curve.z * ResponseCurve * ResponseCurve;

// Combine and apply the brightness response curve
Color *= max(ResponseCurve, Brightness - _Threshold) / max(Brightness, 1e-10);
Color = Color * max(ResponseCurve, Brightness - _Threshold) / max(Brightness, 1e-10);
Brightness = Med3(Color.r, Color.g, Color.b);
OutputColor0 = saturate(lerp(Brightness, Color, _Saturation) * _Intensity);
}

Expand Down
20 changes: 5 additions & 15 deletions shaders/kDatamosh.fx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ uniform float _Constraint <
ui_label = "Constraint";
ui_tooltip = "Higher = Smoother flow";
ui_min = 0.0;
> = 0.1;
> = 1.0;

uniform float _BlendFactor <
ui_category = "Motion Vectors";
Expand All @@ -97,7 +97,7 @@ uniform float _BlendFactor <
ui_tooltip = "Higher = Less temporal noise";
ui_min = 0.0;
ui_max = 1.0;
> = 0.5;
> = 0.25;

#ifndef LINEAR_SAMPLING
#define LINEAR_SAMPLING 0
Expand Down Expand Up @@ -275,7 +275,7 @@ void OpticalFlowPS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, o

for(float Level = MaxLevel; Level > 0.0; Level--)
{
const float Lambda = max(ldexp(_Constraint * 1e-3, Level - MaxLevel), 1e-7);
const float Lambda = max(ldexp(_Constraint * 1e-5, Level - MaxLevel), 1e-7);
float2 SampleIxy = tex2Dlod(_SampleDerivatives, float4(TexCoord, 0.0, Level)).xy;

float2 SampleFrames;
Expand Down Expand Up @@ -328,18 +328,8 @@ void AccumulatePS(float4 Position : SV_POSITION, float2 TexCoord : TEXCOORD0, ou
float ResetAccumulation = saturate(Random.z * 0.5 + Quality);

// - Reset if the amount of motion is larger than the block size.
if(MotionVectorLength > _BlockSize)
{
OutputColor0.rgb = ResetAccumulation;
OutputColor0.a = 0.0;
}
else
{
// This should work given law of addition
OutputColor0.rgb = UpdateAccumulation;
OutputColor0.a = 1.0;
}

OutputColor0.rgb = MotionVectorLength > _BlockSize ? ResetAccumulation : UpdateAccumulation;
OutputColor0.a = MotionVectorLength > _BlockSize ? 0.0 : 1.0;
OutputColor1 = float4(tex2D(_SampleFrame0, TexCoord).rgb, 0.0);
}

Expand Down

0 comments on commit 1efc2ff

Please sign in to comment.