Skip to content

Commit

Permalink
Various updates to shared code
Browse files Browse the repository at this point in the history
  • Loading branch information
papadanku committed Jul 5, 2024
1 parent 6712cb2 commit fca48de
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 66 deletions.
4 changes: 4 additions & 0 deletions shaders/cAutoExposure.fx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "shared/cMacros.fxh"
#include "shared/cGraphics.fxh"

#define INCLUDE_CCAMERA_OPTIONS_EXPOSURE
#include "shared/cCamera.fxh"

#define INCLUDE_CTONEMAP_OPTIONS_TONEMAP
#include "shared/cTonemap.fxh"

/*
Expand Down
4 changes: 4 additions & 0 deletions shaders/cBloom.fx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

#include "shared/cBuffers.fxh"
#include "shared/cGraphics.fxh"

#define INCLUDE_CCAMERA_OPTIONS_EXPOSURE
#include "shared/cCamera.fxh"

#define INCLUDE_CTONEMAP_OPTIONS_TONEMAP
#include "shared/cTonemap.fxh"

/*
Expand Down
45 changes: 44 additions & 1 deletion shaders/cNoiseBlur.fx
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
#include "shared/cGraphics.fxh"
#include "shared/cProcedural.fxh"
#include "shared/cCamera.fxh"

/*
MIT License
Copyright (C) 2015 Keijiro Takahashi
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/*
[Shader Options]
Expand All @@ -12,6 +36,21 @@ uniform float _Radius <
ui_max = 1.0;
> = 0.5;

uniform float _Falloff <
ui_label = "Radius Falloff";
ui_type = "drag";
> = 0.5;

uniform bool _EnableFalloff <
ui_label = "Enable Radius Falloff";
ui_type = "radio";
> = true;

uniform bool _InvertFalloff <
ui_label = "Invert Radius Falloff";
ui_type = "radio";
> = false;


/*
[Pixel Shaders]
Expand All @@ -35,6 +74,10 @@ float4 PS_NoiseBlur(VS2PS_Quad Input) : SV_TARGET0
float Height = saturate(1.0 - saturate(pow(abs(Input.Tex0.y), 1.0)));
float AspectRatio = ScreenSize.y * (1.0 / ScreenSize.x);

// Compute optional radius falloff
float FalloffFactor = _EnableFalloff ? GetVignette(Input.Tex0, AspectRatio, _Falloff) : 1.0;
FalloffFactor = _InvertFalloff ? FalloffFactor : 1.0 - FalloffFactor;

float4 Weight = 0.0;
[unroll] for(int i = 1; i < 4; ++i)
{
Expand All @@ -45,7 +88,7 @@ float4 PS_NoiseBlur(VS2PS_Quad Input) : SV_TARGET0
sincos(Shift, AngleShift.x, AngleShift.y);
AngleShift *= float(i);

float2 SampleOffset = mul(AngleShift, RotationMatrix);
float2 SampleOffset = mul(AngleShift, RotationMatrix) * FalloffFactor;
SampleOffset *= _Radius;
SampleOffset.x *= AspectRatio;
OutputColor += tex2D(CShade_SampleColorTex, Input.Tex0 + (SampleOffset * 0.01));
Expand Down
29 changes: 2 additions & 27 deletions shaders/kVignette.fx
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
#include "shared/cGraphics.fxh"

/*
MIT License
Copyright (C) 2015 Keijiro Takahashi
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "shared/cCamera.fxh"

/*
[Shader Options]
Expand All @@ -39,10 +17,7 @@ uniform float _Falloff <
float4 PS_Vignette(VS2PS_Quad Input) : SV_TARGET0
{
const float AspectRatio = float(BUFFER_WIDTH) / float(BUFFER_HEIGHT);
Input.Tex0 = (Input.Tex0 * 2.0 - 1.0) * AspectRatio;
float Radius = length(Input.Tex0) * _Falloff;
float Radius_2_1 = (Radius * Radius) + 1.0;
return 1.0 / (Radius_2_1 * Radius_2_1);
return GetVignette(Input.Tex0, AspectRatio, _Falloff);
}

technique CShade_KinoVignette
Expand Down
95 changes: 64 additions & 31 deletions shaders/shared/cCamera.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,73 @@

// AutoExposure(): https://john-chapman.github.io/2017/08/23/dynamic-local-exposure.html

uniform float _CShadeExposureBias <
ui_category = "Output: AutoExposure";
ui_label = "Exposure Bias";
ui_type = "slider";
ui_step = 0.001;
ui_min = 0.0;
ui_max = 8.0;
> = 1.0;

uniform float _CShadeExposureSmoothingSpeed <
ui_category = "Output: AutoExposure";
ui_label = "Smoothing Speed";
ui_type = "slider";
ui_min = 0.1;
ui_max = 1.0;
> = 0.5;

float4 CreateExposureTex(float3 Color, float FrameTime)
{
float3 Luma = max(Color.r, max(Color.g, Color.b));
#if defined(INCLUDE_CCAMERA_OPTIONS_EXPOSURE)
uniform float _CShadeExposureBias <
ui_category = "Output: AutoExposure";
ui_label = "Exposure Bias";
ui_type = "slider";
ui_step = 0.001;
ui_min = 0.0;
ui_max = 8.0;
> = 1.0;

// .rgb = Output the highest brightness out of red/green/blue component
// .a = Output the weight for temporal blending
float Delay = 1e-3 * FrameTime;
return float4(log(max(Luma, 1e-2)), saturate(Delay * _CShadeExposureSmoothingSpeed));
}
uniform float _CShadeExposureSmoothingSpeed <
ui_category = "Output: AutoExposure";
ui_label = "Smoothing Speed";
ui_type = "slider";
ui_min = 0.1;
ui_max = 1.0;
> = 0.5;

float4 CreateExposureTex(float3 Color, float FrameTime)
{
float3 Luma = max(Color.r, max(Color.g, Color.b));

// .rgb = Output the highest brightness out of red/green/blue component
// .a = Output the weight for temporal blending
float Delay = 1e-3 * FrameTime;
return float4(log(max(Luma, 1e-2)), saturate(Delay * _CShadeExposureSmoothingSpeed));
}

float3 ApplyAutoExposure(float3 Color, float Luma)
{
float LumaAverage = exp(Luma);
float Ev100 = log2(LumaAverage * 100.0 / 12.5);
Ev100 -= _CShadeExposureBias; // optional manual bias
float Exposure = 1.0 / (1.2 * exp2(Ev100));
return Color * Exposure;
}
#endif

/*
MIT License
Copyright (C) 2015 Keijiro Takahashi
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

float3 ApplyAutoExposure(float3 Color, float Luma)
float GetVignette(float2 Tex, float AspectRatio, float Falloff)
{
float LumaAverage = exp(Luma);
float Ev100 = log2(LumaAverage * 100.0 / 12.5);
Ev100 -= _CShadeExposureBias; // optional manual bias
float Exposure = 1.0 / (1.2 * exp2(Ev100));
return Color * Exposure;
Tex = (Tex * 2.0 - 1.0) * AspectRatio;
float Radius = length(Tex) * Falloff;
float Radius_2_1 = (Radius * Radius) + 1.0;
return 1.0 / (Radius_2_1 * Radius_2_1);
}

#endif
16 changes: 9 additions & 7 deletions shaders/shared/cTonemap.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,15 @@
return 0.5 * (D * SDR - sqrt(((D*D - 4.0*C*E) * SDR + 4.0*A*E-2.0*B*D) * SDR + B*B) - B) / (A - C * SDR);
}

uniform int _CShadeTonemapOperator <
ui_category = "Output: Tonemapping";
ui_label = "Tonemap Operator";
ui_tooltip = "Select a tonemap operator for the output";
ui_type = "combo";
ui_items = "None\0Reinhard\0Reinhard Squared\0Standard\0Exponential\0ACES Filmic Curve\0";
> = 5;
#if defined(INCLUDE_CTONEMAP_OPTIONS_TONEMAP)
uniform int _CShadeTonemapOperator <
ui_category = "Output: Tonemapping";
ui_label = "Tonemap Operator";
ui_tooltip = "Select a tonemap operator for the output";
ui_type = "combo";
ui_items = "None\0Reinhard\0Reinhard Squared\0Standard\0Exponential\0ACES Filmic Curve\0";
> = 5;
#endif

float3 ApplyTonemap(float3 HDR)
{
Expand Down

0 comments on commit fca48de

Please sign in to comment.