From cc1ef4d62736eb389cb5871dcbaf54399338747b Mon Sep 17 00:00:00 2001 From: Samuel Skiff Date: Mon, 22 Aug 2022 21:09:14 -0500 Subject: [PATCH] Bloom Tweaks --- .../postFX/scripts/Bloom/BloomPostFX.tscript | 200 ++++++++-------- ...bloomCombineP.hlsl => bloomStrengthP.hlsl} | 14 +- .../postFX/scripts/Bloom/bloomThresholdP.hlsl | 6 +- .../core/postFX/scripts/Bloom/upSampleP.hlsl | 213 ++++-------------- .../postFX/scripts/Glow/GlowPostFX.tscript | 2 +- 5 files changed, 166 insertions(+), 269 deletions(-) rename Templates/BaseGame/game/core/postFX/scripts/Bloom/{bloomCombineP.hlsl => bloomStrengthP.hlsl} (79%) diff --git a/Templates/BaseGame/game/core/postFX/scripts/Bloom/BloomPostFX.tscript b/Templates/BaseGame/game/core/postFX/scripts/Bloom/BloomPostFX.tscript index f1cd5e770..ac531c39d 100644 --- a/Templates/BaseGame/game/core/postFX/scripts/Bloom/BloomPostFX.tscript +++ b/Templates/BaseGame/game/core/postFX/scripts/Bloom/BloomPostFX.tscript @@ -20,12 +20,11 @@ // IN THE SOFTWARE. //----------------------------------------------------------------------------- -$PostFX::BloomPostFX::threshold = 1.0; +$PostFX::BloomPostFX::threshold = 0.75; $PostFX::BloomPostFX::intensity = 0.5; $PostFX::BloomPostFX::radius = 8.0; -// Valid Values: 0, 1, 2, 3 -$PostFX::BloomPostFX::quality = 1; +$mipsCount = 4; singleton ShaderData( PFX_BloomThreshold_Shader ) { @@ -37,30 +36,31 @@ singleton ShaderData( PFX_BloomThreshold_Shader ) pixVersion = 3.0; }; -//singleton ShaderData( PFX_BloomDownSample_Shader ) -//{ -// DXVertexShaderFile = $Core::CommonShaderPath @ "/postFX/postFxV.hlsl"; -// DXPixelShaderFile = "./downSampleP.hlsl"; -// -// samplerNames[0] = "$inputTex"; -// -// pixVersion = 3.0; -//}; - -singleton ShaderData( PFX_BloomUpSample_Shader ) +singleton ShaderData( PFX_BloomDownSample_Shader ) { DXVertexShaderFile = $Core::CommonShaderPath @ "/postFX/postFxV.hlsl"; - DXPixelShaderFile = "./upSampleP.hlsl"; + DXPixelShaderFile = "./downSampleP.hlsl"; samplerNames[0] = "$inputTex"; pixVersion = 3.0; }; -singleton ShaderData( PFX_BloomCombine_Shader ) +singleton ShaderData( PFX_BloomUpSample_Shader ) { DXVertexShaderFile = $Core::CommonShaderPath @ "/postFX/postFxV.hlsl"; - DXPixelShaderFile = "./bloomCombineP.hlsl"; + DXPixelShaderFile = "./upSampleP.hlsl"; + + samplerNames[0] = "$nxtTex"; + samplerNames[1] = "$mipTex"; + + pixVersion = 3.0; +}; + +singleton ShaderData( PFX_BloomStrength_Shader ) +{ + DXVertexShaderFile = $Core::CommonShaderPath @ "/postFX/postFxV.hlsl"; + DXPixelShaderFile = "./bloomStrengthP.hlsl"; samplerNames[0] = "$inputTex"; @@ -71,51 +71,108 @@ singleton GFXStateBlockData( BloomPostFX_SampleStateBlock : PFX_DefaultStateBloc { samplersDefined = true; samplerStates[0] = SamplerClampLinear; + samplerStates[1] = SamplerClampLinear; }; -singleton GFXStateBlockData( BloomPostFX_Combine_SampleStateBlock : PFX_DefaultStateBlock ) +singleton GFXStateBlockData( BloomPostFX_Add_SampleStateBlock : PFX_DefaultStateBlock ) { - samplersDefined = true; + alphaDefined = true; + alphaTestEnable = true; + alphaTestRef = 1; + alphaTestFunc = GFXCmpGreaterEqual; + + // Do a one to one blend. + blendDefined = true; + blendEnable = true; + blendSrc = GFXBlendOne; + blendDest = GFXBlendOne; + + samplersDefined = true; samplerStates[0] = SamplerClampLinear; - samplerStates[1] = SamplerClampLinear; - samplerStates[2] = SamplerClampLinear; }; function BloomPostFX::setShaderConsts( %this ) { %this.setShaderConst("$threshold", $PostFX::BloomPostFX::threshold); - %blurOne = %this->blur_one; - %blurOne.setShaderConst("$blurRadius", $PostFX::BloomPostFX::radius); + %blur = %this->bloomBlur; + for (%idx = 0; %idx < $mipsCount; %idx++) + { + %mip = %blur.getObject($mipsCount + %idx); + %mip.setShaderConst("$filterRadius", $PostFX::BloomPostFX::radius); + } - %blurTwo = %this->blur_two; - %blurTwo.setShaderConst("$blurRadius", $PostFX::BloomPostFX::radius); - - %combine = %this->bloom_combine; - %combine.setShaderConst("$intensity", $PostFX::BloomPostFX::intensity); - - %steps = mRound($PostFX::BloomPostFX::radius * 0.333 + 1); - echo(%steps); - - %blurOne.setShaderConst("$numSteps", %steps); - %blurTwo.setShaderConst("$numSteps", %steps); + %final = %this->bloomFinal; + %final.setShaderConst("$strength", $PostFX::BloomPostFX::intensity); } function BloomPostFX::preProcess( %this ) { - if ( $PostFX::BloomPostFX::quality !$= %this.quality ) +} + +// This function sets up s sort of "mip-chain" for the bloom effect +// Not really "optimal" but it works well enough +function SetupBlurFX() +{ + %blurFX = new PostEffect() + { + internalName = "bloomBlur"; + allowReflectPass = false; + shader = PFX_BloomDownSample_Shader; + stateBlock = BloomPostFX_SampleStateBlock; + texture[0] = "#threshold"; + target = "#bloom_0"; + targetFormat = "GFXFormatR16G16B16A16F"; + }; + + %textureName = "#bloom_0"; + for (%idx = 0; %idx < $mipsCount; %idx++) { - %this.quality = mClamp( mRound( $PostFX::BloomPostFX::quality ), 0, 3 ); + %mipName = "bloom_" @ (%idx + 1); + %mipFX = new PostEffect() + { + internalName = %mipName; + allowReflectPass = false; + shader = PFX_BloomDownSample_Shader; + stateBlock = BloomPostFX_SampleStateBlock; + texture[0] = %textureName; + target = "#" @ %mipName; + targetScale = "0.5 0.5"; + targetFormat = "GFXFormatR16G16B16A16F"; + }; + + %blurFX.add(%mipFX); + %textureName = "#" @ %mipName; + } + + for (%idx = $mipsCount; %idx > 0; %idx--) + { + %nxt = "#bloom_" @ (%idx - 1); + %mipName = "upsample_" @ (%idx - 1); + echo(%mipName SPC %textureName SPC %nxt); - %blurOne = %this->blur_one; - %blurTwo = %this->blur_two; - %blurOne.setShaderMacro( "QUALITY", %this.quality ); - %blurTwo.setShaderMacro( "QUALITY", %this.quality ); + %mipFX = new PostEffect() + { + internalName = %mipName; + allowReflectPass = false; + shader = PFX_BloomUpSample_Shader; + stateBlock = BloomPostFX_SampleStateBlock; + texture[0] = %nxt; + texture[1] = %textureName; + target = "#" @ %mipName; + }; + + %blurFX.add(%mipFX); + %textureName = "#" @ %mipName; } + + return %blurFX; } function BloomPostFX::onAdd(%this) { + %this.add(SetupBlurFX()); + //Register the postFX with the manager PostFXManager.registerPostEffect(%this); } @@ -137,7 +194,6 @@ function BloomPostFX::populatePostFXSettings(%this) { PostEffectEditorInspector.startGroup("BloomPostFX - General"); PostEffectEditorInspector.addCallbackField("$PostFX::BloomPostFX::Enabled", "Enabled", "bool", "", $PostFX::BloomPostFX::Enabled, "", "toggleBloomPostFX"); - PostEffectEditorInspector.addField("$PostFX::BloomPostFX::quality", "Quality", "list", "0,1,2,3", $PostFX::BloomPostFX::quality, "0,1,2,3"); PostEffectEditorInspector.addField("$PostFX::BloomPostFX::threshold", "Threshold", "range", "", $PostFX::BloomPostFX::threshold, "0 1 10"); PostEffectEditorInspector.addField("$PostFX::BloomPostFX::intensity", "Intensity", "range", "", $PostFX::BloomPostFX::intensity, "0 2 10"); PostEffectEditorInspector.addField("$PostFX::BloomPostFX::radius", "Radius", "float", "", $PostFX::BloomPostFX::radius, ""); @@ -168,7 +224,6 @@ function BloomPostFX::applyFromPreset(%this) function BloomPostFX::savePresetSettings(%this) { PostFXManager::savePresetSetting("$PostFX::BloomPostFX::Enabled"); - PostFXManager::savePresetSetting("$PostFX::BloomPostFX::quality"); PostFXManager::savePresetSetting("$PostFX::BloomPostFX::threshold"); PostFXManager::savePresetSetting("$PostFX::BloomPostFX::intensity"); PostFXManager::savePresetSetting("$PostFX::BloomPostFX::radius"); @@ -180,66 +235,25 @@ singleton PostEffect( BloomPostFX ) enabled = false; allowReflectPass = false; - renderTime = "PFXAfterDiffuse"; - renderBin = "ObjTranslucentBin"; - renderPriority = 0.9; + renderTime = "PFXBeforeBin"; + renderBin = "EditorBin"; + renderPriority = 9999; shader = PFX_BloomThreshold_Shader; stateBlock = BloomPostFX_SampleStateBlock; texture[0] = "$backBuffer"; - target = "$outTex"; + target = "#threshold"; + targetFormat = "GFXFormatR16G16B16A16F"; targetClear = PFXTargetClear_OnDraw; + targetClearColor = "0 0 0 0"; new PostEffect() { + internalName = "bloomFinal"; allowReflectPass = false; - shader = PFX_PassthruShader; - stateBlock = BloomPostFX_SampleStateBlock; - texture[0] = "$inTex"; - target = "#bloom0"; - targetScale = "0.5 0.5"; - }; - - new PostEffect() - { - allowReflectPass = false; - shader = PFX_PassthruShader; - stateBlock = BloomPostFX_SampleStateBlock; - texture[0] = "#bloom0"; - target = "#bloom1"; - targetScale = "0.25 0.25"; - }; - - new PostEffect() - { - internalName = "blur_one"; - allowReflectPass = false; - shader = PFX_BloomUpSample_Shader; - stateBlock = BloomPostFX_SampleStateBlock; - texture[0] = "#bloom0"; - target = "#blur0"; - }; - - new PostEffect() - { - internalName = "blur_two"; - allowReflectPass = false; - shader = PFX_BloomUpSample_Shader; - stateBlock = BloomPostFX_SampleStateBlock; - texture[0] = "#bloom1"; - target = "#blur1"; - targetScale = "0.5 0.5"; - }; - - new PostEffect() - { - internalName = "bloom_combine"; - allowReflectPass = false; - shader = PFX_BloomCombine_Shader; - stateBlock = BloomPostFX_Combine_SampleStateBlock; - texture[0] = "#blur0"; - texture[1] = "#blur1"; - texture[2] = "$backBuffer"; + shader = PFX_BloomStrength_Shader; + stateBlock = BloomPostFX_Add_SampleStateBlock; + texture[0] = "#upsample_0"; target = "$backBuffer"; }; }; diff --git a/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomCombineP.hlsl b/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomStrengthP.hlsl similarity index 79% rename from Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomCombineP.hlsl rename to Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomStrengthP.hlsl index 0493464ab..35fb2dfbc 100644 --- a/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomCombineP.hlsl +++ b/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomStrengthP.hlsl @@ -22,15 +22,13 @@ #include "core/rendering/shaders/postFX/postFx.hlsl" -TORQUE_UNIFORM_SAMPLER2D(inputTex0, 0); -TORQUE_UNIFORM_SAMPLER2D(inputTex1, 1); -TORQUE_UNIFORM_SAMPLER2D(colorTex, 2); -uniform float intensity; +TORQUE_UNIFORM_SAMPLER2D(inputTex, 0); +uniform float strength; float4 main(PFXVertToPix IN) : TORQUE_TARGET0 { - float4 bloom = (TORQUE_TEX2D(inputTex0, IN.uv0) + TORQUE_TEX2D(inputTex1, IN.uv0)) * 0.5f; - float4 screenColor = TORQUE_TEX2D(colorTex, IN.uv0); - screenColor.rgb += bloom.rgb * intensity; - return screenColor; + float4 upSample = TORQUE_TEX2D(inputTex, IN.uv0); + 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 546bcdc2c..0cccb4928 100644 --- a/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomThresholdP.hlsl +++ b/Templates/BaseGame/game/core/postFX/scripts/Bloom/bloomThresholdP.hlsl @@ -29,6 +29,8 @@ float4 main(PFXVertToPix IN) : TORQUE_TARGET0 { float4 screenColor = TORQUE_TEX2D(inputTex, IN.uv0); float brightness = max(screenColor.r, max(screenColor.g, screenColor.b)); - //clip(brightness - threshold); - return screenColor * saturate(pow(brightness, threshold*8.0f)); + float contribution = max(brightness - threshold, 0); + contribution /= max(brightness, 0.0001f); + clip(contribution > 0.00001f ? 1 : -1); + return screenColor * contribution; } diff --git a/Templates/BaseGame/game/core/postFX/scripts/Bloom/upSampleP.hlsl b/Templates/BaseGame/game/core/postFX/scripts/Bloom/upSampleP.hlsl index d69bb26b0..86dd7b2ca 100644 --- a/Templates/BaseGame/game/core/postFX/scripts/Bloom/upSampleP.hlsl +++ b/Templates/BaseGame/game/core/postFX/scripts/Bloom/upSampleP.hlsl @@ -20,186 +20,69 @@ // IN THE SOFTWARE. //----------------------------------------------------------------------------- -#include "core/rendering/shaders/torque.hlsl" #include "core/rendering/shaders/postFX/postFx.hlsl" -#if QUALITY == 3 - -static const int KERNEL_SAMPLES = 64; -static const float3 KERNEL[64] = { - float3(0.015625, 0.000000, 0.017183), - float3(-0.023043, 0.021109, 0.017183), - float3(0.004098, -0.046696, 0.017183), - float3(0.038027, 0.049600, 0.017183), - float3(-0.076931, -0.013608, 0.017183), - float3(0.079102, -0.050318, 0.017182), - float3(-0.028394, 0.105625, 0.017182), - float3(-0.057613, -0.110931, 0.017181), - float3(0.132092, 0.048240, 0.017180), - float3(-0.144429, 0.059618, 0.017178), - float3(0.072849, -0.155673, 0.017175), - float3(0.056116, 0.178906, 0.017172), - float3(-0.175746, -0.101848, 0.017168), - float3(0.213648, -0.046970, 0.017163), - float3(-0.134796, 0.191733, 0.017157), - float3(-0.032128, -0.247927, 0.017149), - float3(0.203110, 0.171181, 0.017140), - float3(-0.281010, 0.011621, 0.017129), - float3(0.210434, -0.209410, 0.017116), - float3(-0.014435, 0.312166, 0.017101), - float3(-0.210233, -0.251929, 0.017084), - float3(0.340680, 0.045838, 0.017063), - float3(-0.294996, 0.205251, 0.017040), - float3(0.082306, -0.365856, 0.017014), - float3(0.194211, 0.338925, 0.016984), - float3(-0.387031, -0.123474, 0.016951), - float3(0.382974, -0.176944, 0.016913), - float3(-0.168902, 0.403582, 0.016871), - float3(-0.153361, -0.426383, 0.016825), - float3(0.414933, 0.218077, 0.016773), - float3(-0.468376, 0.123462, 0.016717), - float3(0.270419, -0.420563, 0.016654), - float3(0.087335, 0.508175, 0.016586), - float3(-0.420019, -0.325286, 0.016512), - float3(0.545008, -0.045153, 0.016431), - float3(-0.381982, 0.412911, 0.016344), - float3(0.002820, -0.578118, 0.016250), - float3(0.398932, 0.439764, 0.016148), - float3(-0.606775, -0.056236, 0.016038), - float3(0.497850, -0.377850, 0.015921), - float3(-0.114661, 0.630280, 0.015795), - float3(-0.349522, -0.555426, 0.015661), - float3(0.647981, 0.177585, 0.015518), - float3(-0.611659, 0.313893, 0.015367), - float3(0.244418, -0.659276, 0.015206), - float3(0.270999, 0.665703, 0.015037), - float3(-0.663622, -0.314503, 0.014857), - float3(0.716710, -0.220969, 0.014669), - float3(-0.387117, 0.660547, 0.014471), - float3(-0.164025, -0.763837, 0.014263), - float3(0.649650, 0.461481, 0.014045), - float3(-0.806263, 0.100483, 0.013819), - float3(0.536765, -0.630614, 0.013582), - float3(0.030749, 0.843190, 0.013337), - float3(-0.603207, -0.612100, 0.013082), - float3(0.873858, 0.044681, 0.012818), - float3(-0.686583, 0.567289, 0.012545), - float3(0.125226, -0.897556, 0.012264), - float3(0.522814, 0.759288, 0.011975), - float3(-0.913626, -0.210223, 0.011678), - float3(0.829278, -0.469836, 0.011373), - float3(-0.298936, 0.921474, 0.011062), - float3(-0.408505, -0.895610, 0.010745), - float3(0.920579, 0.390557, 0.010422) -}; - -#elif QUALITY == 2 - -static const int KERNEL_SAMPLES = 32; -static const float3 KERNEL[32] = { - float3(0.031250, 0.000000, 0.034485), - float3(-0.046086, 0.042218, 0.034485), - float3(0.008196, -0.093391, 0.034484), - float3(0.076055, 0.099200, 0.034481), - float3(-0.153861, -0.027216, 0.034475), - float3(0.158204, -0.100637, 0.034464), - float3(-0.056788, 0.211250, 0.034446), - float3(-0.115227, -0.221862, 0.034418), - float3(0.264184, 0.096480, 0.034378), - float3(-0.288858, 0.119236, 0.034321), - float3(0.145697, -0.311346, 0.034245), - float3(0.112231, 0.357812, 0.034146), - float3(-0.351492, -0.203697, 0.034019), - float3(0.427296, -0.093940, 0.033859), - float3(-0.269592, 0.383467, 0.033663), - float3(-0.064255, -0.495854, 0.033424), - float3(0.406220, 0.342362, 0.033139), - float3(-0.562020, 0.023241, 0.032802), - float3(0.420867, -0.418819, 0.032408), - float3(-0.028870, 0.624333, 0.031952), - float3(-0.420465, -0.503858, 0.031431), - float3(0.681360, 0.091676, 0.030841), - float3(-0.589992, 0.410501, 0.030178), - float3(0.164611, -0.731713, 0.029439), - float3(0.388423, 0.677849, 0.028625), - float3(-0.774063, -0.246947, 0.027733), - float3(0.765949, -0.353888, 0.026766), - float3(-0.337803, 0.807164, 0.025725), - float3(-0.306722, -0.852766, 0.024613), - float3(0.829865, 0.436154, 0.023437), - float3(-0.936752, 0.246925, 0.022202), - float3(0.540838, -0.841127, 0.020916) -}; - -#elif QUALITY == 1 - -static const int KERNEL_SAMPLES = 16; -static const float3 KERNEL[16] = { - float3(0.062500, 0.000000, 0.069464), - float3(-0.092171, 0.084436, 0.069456), - float3(0.016392, -0.186782, 0.069422), - float3(0.152110, 0.198400, 0.069329), - float3(-0.307723, -0.054432, 0.069134), - float3(0.316408, -0.201273, 0.068781), - float3(-0.113577, 0.422500, 0.068204), - float3(-0.230454, -0.443724, 0.067328), - float3(0.528368, 0.192959, 0.066073), - float3(-0.577716, 0.238473, 0.064362), - float3(0.291394, -0.622692, 0.062123), - float3(0.224463, 0.715623, 0.059300), - float3(-0.702984, -0.407394, 0.055864), - float3(0.854591, -0.187880, 0.051818), - float3(-0.539184, 0.766934, 0.047209), - float3(-0.128511, -0.991708, 0.042133) -}; - -#elif QUALITY == 0 - static const int KERNEL_SAMPLES = 9; static const float3 KERNEL[9] = { - float3( 0, 0, 0.25), - float3( 1, 0, 0.125), - float3( 0, 1, 0.125), - float3(-1, 0, 0.125), - float3( 0,-1, 0.125), - float3( 0.707, 0.707, 0.0625), - float3( 0.707,-0.707, 0.0625), - float3(-0.707,-0.707, 0.0625), - float3(-0.707, 0.707, 0.0625) + float3( 0.0000, 0.0000, 0.2500), + float3( 1.0000, 0.0000, 0.1250), + float3( 0.0000, 1.0000, 0.1250), + float3(-1.0000, 0.0000, 0.1250), + float3( 0.0000,-1.0000, 0.1250), + float3( 0.7070, 0.7070, 0.0625), + float3( 0.7070,-0.7070, 0.0625), + float3(-0.7070,-0.7070, 0.0625), + float3(-0.7070, 0.7070, 0.0625) }; -#endif - -TORQUE_UNIFORM_SAMPLER2D(inputTex, 0); -uniform float blurRadius; -uniform int numSteps; +TORQUE_UNIFORM_SAMPLER2D(nxtTex, 0); +TORQUE_UNIFORM_SAMPLER2D(mipTex, 1); +uniform float filterRadius; uniform float2 oneOverTargetSize; float4 main(PFXVertToPix IN) : TORQUE_TARGET0 { float4 upSample = float4(0, 0, 0, 0); - float radStep = blurRadius / float(numSteps); - - const float alpha = M_2PI_F / float(numSteps); [unroll] for (int i=0; i