From fca48de47afafe4b70abdd2423cb8a6779d05357 Mon Sep 17 00:00:00 2001 From: papadanku <115061077+papadanku@users.noreply.github.com> Date: Fri, 5 Jul 2024 16:58:41 -0700 Subject: [PATCH] Various updates to shared code --- shaders/cAutoExposure.fx | 4 ++ shaders/cBloom.fx | 4 ++ shaders/cNoiseBlur.fx | 45 +++++++++++++++++- shaders/kVignette.fx | 29 +---------- shaders/shared/cCamera.fxh | 95 +++++++++++++++++++++++++------------ shaders/shared/cTonemap.fxh | 16 ++++--- 6 files changed, 127 insertions(+), 66 deletions(-) diff --git a/shaders/cAutoExposure.fx b/shaders/cAutoExposure.fx index 055adb2..4e94559 100644 --- a/shaders/cAutoExposure.fx +++ b/shaders/cAutoExposure.fx @@ -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" /* diff --git a/shaders/cBloom.fx b/shaders/cBloom.fx index e6a964c..bb1180a 100644 --- a/shaders/cBloom.fx +++ b/shaders/cBloom.fx @@ -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" /* diff --git a/shaders/cNoiseBlur.fx b/shaders/cNoiseBlur.fx index 721a513..581bc31 100644 --- a/shaders/cNoiseBlur.fx +++ b/shaders/cNoiseBlur.fx @@ -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] @@ -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] @@ -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) { @@ -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)); diff --git a/shaders/kVignette.fx b/shaders/kVignette.fx index 8b3410a..3a91dad 100644 --- a/shaders/kVignette.fx +++ b/shaders/kVignette.fx @@ -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] @@ -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 diff --git a/shaders/shared/cCamera.fxh b/shaders/shared/cCamera.fxh index 9c86c26..a0dc07b 100644 --- a/shaders/shared/cCamera.fxh +++ b/shaders/shared/cCamera.fxh @@ -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 \ No newline at end of file diff --git a/shaders/shared/cTonemap.fxh b/shaders/shared/cTonemap.fxh index 96b8e9f..c22f45a 100644 --- a/shaders/shared/cTonemap.fxh +++ b/shaders/shared/cTonemap.fxh @@ -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) {