From ecf7298c68c8a3a861bced9118a273b620d16d7b Mon Sep 17 00:00:00 2001 From: Samuel Skiff Date: Wed, 24 Aug 2022 11:54:18 -0500 Subject: [PATCH] Bloom Updates --- .../postFX/scripts/Bloom/BloomPostFX.tscript | 53 +++++++++++++------ .../postFX/scripts/Bloom/bloomStrengthP.hlsl | 14 +++-- .../postFX/scripts/Bloom/bloomThresholdP.hlsl | 3 +- .../postFX/scripts/Bloom/downSampleP.hlsl | 53 +++++++++---------- .../core/postFX/scripts/Bloom/upSampleP.hlsl | 46 ++++------------ .../scripts/default.postfxpreset.tscript | 8 +-- 6 files changed, 85 insertions(+), 92 deletions(-) diff --git a/Templates/BaseGame/game/core/postFX/scripts/Bloom/BloomPostFX.tscript b/Templates/BaseGame/game/core/postFX/scripts/Bloom/BloomPostFX.tscript index de27b826f..cccc9f288 100644 --- a/Templates/BaseGame/game/core/postFX/scripts/Bloom/BloomPostFX.tscript +++ b/Templates/BaseGame/game/core/postFX/scripts/Bloom/BloomPostFX.tscript @@ -20,13 +20,20 @@ // IN THE SOFTWARE. //----------------------------------------------------------------------------- -$PostFX::BloomPostFX::threshold = 0.75; +// Inspired by bloom described in paper listed here: +// http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare + +$PostFX::BloomPostFX::threshold = 0.65; $PostFX::BloomPostFX::intensity = 0.5; -$PostFX::BloomPostFX::radius = 8.0; +$PostFX::BloomPostFX::radius = 4.0; $PostFX::BloomPostFX::dirtEnabled = true; $PostFX::BloomPostFX::dirtScale = 2048.0; -$PostFX::BloomPostFX::dirtIntensity = 7.0; +$PostFX::BloomPostFX::dirtIntensity = 2.0; +$PostFX::BloomPostFX::dirtEdgeMinDist = 0.125; +$PostFX::BloomPostFX::dirtEdgeMaxDist = 0.75; +$PostFX::BloomPostFX::dirtEdgeMinVal = 0.05; +$PostFX::BloomPostFX::dirtImage = "core/postFX/images/lensDirt.png"; singleton ShaderData( PFX_BloomThreshold_Shader ) { @@ -112,6 +119,11 @@ function BloomPostFX::setShaderConsts( %this ) %dirtScale = $PostFX::BloomPostFX::dirtScale; %dirtIntensity = $PostFX::BloomPostFX::dirtIntensity; %final.setShaderConst("$dirtParams", %dirtScale SPC %dirtScale SPC %dirtIntensity); + + %edgeMin = $PostFX::BloomPostFX::dirtEdgeMinDist; + %edgeMax = $PostFX::BloomPostFX::dirtEdgeMaxDist; + %edgeVal = $PostFX::BloomPostFX::dirtEdgeMinVal; + %final.setShaderConst("$edgeParams", %edgeMin SPC %edgeMax SPC %edgeVal); } function BloomPostFX::preProcess( %this ) @@ -129,15 +141,12 @@ function BloomPostFX::preProcess( %this ) } } - if($PostFX::BloomPostFX::dirtImage $= "") - { - $PostFX::BloomPostFX::dirtImage = "core/postFX/images/lensDirt.png"; - } - - if($PostFX::BloomPostFX::dirtImage !$= "") + if(%this.dirtImage !$= $PostFX::BloomPostFX::dirtImage) { + %this.dirtImage = $PostFX::BloomPostFX::dirtImage; + %final = %this->bloomFinal; - %final.setTexture(1, $PostFX::BloomPostFX::dirtImage); + %final.setTexture(1, %this.dirtImage); } } @@ -153,7 +162,8 @@ function BloomPostFX::SetupBlurFX( %this ) stateBlock = BloomPostFX_SampleStateBlock; texture[0] = "#threshold"; target = "#bloom_0"; - targetFormat = "GFXFormatR16G16B16A16F"; + targetScale = "0.5 0.5"; + targetFormat = %this.selTexFormat; }; %textureName = "#bloom_0"; @@ -169,7 +179,7 @@ function BloomPostFX::SetupBlurFX( %this ) texture[0] = %textureName; target = "#" @ %mipName; targetScale = "0.5 0.5"; - targetFormat = "GFXFormatR16G16B16A16F"; + targetFormat = %this.selTexFormat; }; %blurFX.add(%mipFX); @@ -179,7 +189,7 @@ function BloomPostFX::SetupBlurFX( %this ) for (%idx = %this.mipsCount; %idx > 0; %idx--) { %nxt = "#bloom_" @ (%idx - 1); - %mipName = "upsample_" @ (%idx - 1); + %mipName = "upSample_" @ (%idx - 1); echo(%mipName SPC %textureName SPC %nxt); %mipFX = new PostEffect() @@ -191,6 +201,8 @@ function BloomPostFX::SetupBlurFX( %this ) texture[0] = %nxt; texture[1] = %textureName; target = "#" @ %mipName; + targetScale = "1.0 1.0"; + targetFormat = %this.selTexFormat; }; %blurFX.add(%mipFX); @@ -233,6 +245,9 @@ function BloomPostFX::populatePostFXSettings(%this) PostEffectEditorInspector.addField("$PostFX::BloomPostFX::dirtEnabled", "Enable Dirt", "bool", "", $PostFX::BloomPostFX::dirtEnabled, ""); PostEffectEditorInspector.addField("$PostFX::BloomPostFX::dirtScale", "Scale", "float", "", $PostFX::BloomPostFX::dirtScale, ""); PostEffectEditorInspector.addField("$PostFX::BloomPostFX::dirtIntensity", "Intensity", "float", "", $PostFX::BloomPostFX::dirtIntensity, ""); + PostEffectEditorInspector.addField("$PostFX::BloomPostFX::dirtEdgeMinDist", "Min Dist", "range", "", $PostFX::BloomPostFX::dirtEdgeMinDist, "0 1 10"); + PostEffectEditorInspector.addField("$PostFX::BloomPostFX::dirtEdgeMaxDist", "Max Dist", "range", "", $PostFX::BloomPostFX::dirtEdgeMaxDist, "0 1 10"); + PostEffectEditorInspector.addField("$PostFX::BloomPostFX::dirtEdgeMinVal", "Min Value", "range", "", $PostFX::BloomPostFX::dirtEdgeMinVal, "0 1 10"); PostEffectEditorInspector.addField("$PostFX::BloomPostFX::dirtImage", "Dirt Image", "image", "", $PostFX::BloomPostFX::dirtImage, ""); PostEffectEditorInspector.endGroup(); } @@ -269,12 +284,18 @@ function BloomPostFX::savePresetSettings(%this) PostFXManager::savePresetSetting("$PostFX::BloomPostFX::dirtScale"); PostFXManager::savePresetSetting("$PostFX::BloomPostFX::dirtIntensity"); PostFXManager::savePresetSetting("$PostFX::BloomPostFX::dirtImage"); + + PostFXManager::savePresetSetting("$PostFX::BloomPostFX::dirtEdgeMinDist"); + PostFXManager::savePresetSetting("$PostFX::BloomPostFX::dirtEdgeMaxDist"); + PostFXManager::savePresetSetting("$PostFX::BloomPostFX::dirtEdgeMinVal"); } //Our actual postFX singleton PostEffect( BloomPostFX ) { - mipsCount = 4; + mipsCount = 5; + selTexFormat = "GFXFormatR16G16B16A16F"; + enabled = false; allowReflectPass = false; @@ -287,8 +308,6 @@ singleton PostEffect( BloomPostFX ) texture[0] = "$backBuffer"; target = "#threshold"; targetFormat = "GFXFormatR16G16B16A16F"; - targetClear = PFXTargetClear_OnDraw; - targetClearColor = "0 0 0 0"; new PostEffect() { @@ -296,7 +315,7 @@ singleton PostEffect( BloomPostFX ) allowReflectPass = false; shader = PFX_BloomStrength_Shader; stateBlock = BloomPostFX_Add_SampleStateBlock; - texture[0] = "#upsample_0"; + texture[0] = "#upSample_0"; target = "$backBuffer"; }; }; diff --git a/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomStrengthP.hlsl b/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomStrengthP.hlsl index df4b7ed62..8cf4f99eb 100644 --- a/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomStrengthP.hlsl +++ b/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomStrengthP.hlsl @@ -28,21 +28,25 @@ uniform float strength; // XY: Dirt Texture Size/Scale // Z: Dirt Effect Strength uniform float3 dirtParams; +// XY: Edge Min & Max Distance +// Z: Edge Min Value +uniform float3 edgeParams; uniform float2 oneOverTargetSize; float4 main(PFXVertToPix IN) : TORQUE_TARGET0 { #if defined(USE_DIRT) - float3 dirt = TORQUE_TEX2D(dirtTex, IN.uv0 / (dirtParams.xy * oneOverTargetSize)).rgb * dirtParams.z; + float edge = distance(IN.uv0, float2(0.5f, 0.5f)); + edge = max(smoothstep(edgeParams.x, edgeParams.y, edge), edgeParams.z); + float3 dirt = TORQUE_TEX2D(dirtTex, IN.uv0 / (dirtParams.xy * oneOverTargetSize)).rgb * dirtParams.z * edge; #endif - float4 upSample = TORQUE_TEX2D(inputTex, IN.uv0); + float4 upSample = TORQUE_TEX2D(inputTex, IN.uv0) * strength; #if defined(USE_DIRT) - upSample.rgb += upSample.rgb * dirt; + upSample.rgb += upSample.rgb * dirt; + //upSample.rgb = dirt; #endif - upSample.rgb *= strength; - return upSample; } diff --git a/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomThresholdP.hlsl b/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomThresholdP.hlsl index f1e1376d9..92c4f5943 100644 --- a/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomThresholdP.hlsl +++ b/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomThresholdP.hlsl @@ -29,8 +29,7 @@ float4 main(PFXVertToPix IN) : TORQUE_TARGET0 { float4 screenColor = TORQUE_TEX2D(inputTex, IN.uv0); float brightness = max(screenColor.r, max(screenColor.g, screenColor.b)); - float contribution = max(brightness - threshold, 0); + float contribution = pow(brightness, threshold * 10.0f); contribution /= max(brightness, 0.0001f); - clip(contribution > 0.0001f ? 1 : -1); return screenColor * contribution; } diff --git a/Templates/BaseGame/game/core/postFX/scripts/Bloom/downSampleP.hlsl b/Templates/BaseGame/game/core/postFX/scripts/Bloom/downSampleP.hlsl index c8b282c94..dd8e7fce3 100644 --- a/Templates/BaseGame/game/core/postFX/scripts/Bloom/downSampleP.hlsl +++ b/Templates/BaseGame/game/core/postFX/scripts/Bloom/downSampleP.hlsl @@ -22,40 +22,37 @@ #include "core/rendering/shaders/postFX/postFx.hlsl" -//----------------------------------------------------------------------------- -// Data -//----------------------------------------------------------------------------- +static const int KERNEL_SAMPLES = 9; +static const float3 KERNEL[9] = { + float3( 0.0000f, 0.0000f, 0.2500f), + float3( 1.0000f, 0.0000f, 0.1250f), + float3( 0.0000f, 1.0000f, 0.1250f), + float3(-1.0000f, 0.0000f, 0.1250f), + float3( 0.0000f,-1.0000f, 0.1250f), + float3( 1.0000f, 1.0000f, 0.0625f), + float3( 1.0000f,-1.0000f, 0.0625f), + float3(-1.0000f,-1.0000f, 0.0625f), + float3(-1.0000f, 1.0000f, 0.0625f) +}; TORQUE_UNIFORM_SAMPLER2D(inputTex, 0); uniform float2 oneOverTargetSize; -//----------------------------------------------------------------------------- -// Main -//----------------------------------------------------------------------------- float4 main(PFXVertToPix IN) : TORQUE_TARGET0 { - float x = oneOverTargetSize.x; - float y = oneOverTargetSize.y; - float4 a = TORQUE_TEX2D(inputTex, IN.uv0 + float2(-x*2.0f, y*2.0f)); - float4 b = TORQUE_TEX2D(inputTex, IN.uv0 + float2( 0.0f , y*2.0f)); - float4 c = TORQUE_TEX2D(inputTex, IN.uv0 + float2( x*2.0f, y*2.0f)); - - float4 d = TORQUE_TEX2D(inputTex, IN.uv0 + float2(-x*2.0f, 0.0f)); - float4 e = TORQUE_TEX2D(inputTex, IN.uv0); - float4 f = TORQUE_TEX2D(inputTex, IN.uv0 + float2( x*2.0f, 0.0f)); - - float4 g = TORQUE_TEX2D(inputTex, IN.uv0 + float2(-x*2.0f,-y*2.0f)); - float4 h = TORQUE_TEX2D(inputTex, IN.uv0 + float2( 0.0f ,-y*2.0f)); - float4 i = TORQUE_TEX2D(inputTex, IN.uv0 + float2( x*2.0f,-y*2.0f)); - - float4 j = TORQUE_TEX2D(inputTex, IN.uv0 + float2(-x, y)); - float4 k = TORQUE_TEX2D(inputTex, IN.uv0 + float2( x, y)); - float4 l = TORQUE_TEX2D(inputTex, IN.uv0 + float2(-x,-y)); - float4 m = TORQUE_TEX2D(inputTex, IN.uv0 + float2( x,-y)); + float4 downSample = float4(0, 0, 0, 0); + + [unroll] + for (int i=0; i