- simplify depthblur.fp into a single function

This commit is contained in:
Magnus Norddahl 2019-04-16 08:59:29 +02:00
parent 073f151761
commit 9d29a460de
2 changed files with 41 additions and 48 deletions

View File

@ -697,11 +697,6 @@ void PPAmbientOcclusion::Render(PPRenderState *renderstate, float m5, int sceneW
ssaoUniforms.Scale = sceneScale; ssaoUniforms.Scale = sceneScale;
ssaoUniforms.Offset = sceneOffset; ssaoUniforms.Offset = sceneOffset;
DepthBlurUniforms blurUniforms;
blurUniforms.BlurSharpness = blurSharpness;
blurUniforms.InvFullResolution = { 1.0f / AmbientWidth, 1.0f / AmbientHeight };
blurUniforms.PowExponent = gl_ssao_exponent;
AmbientCombineUniforms combineUniforms; AmbientCombineUniforms combineUniforms;
combineUniforms.SampleCount = gl_multisample; combineUniforms.SampleCount = gl_multisample;
combineUniforms.Scale = screen->SceneScale(); combineUniforms.Scale = screen->SceneScale();
@ -740,6 +735,11 @@ void PPAmbientOcclusion::Render(PPRenderState *renderstate, float m5, int sceneW
// Blur SSAO texture // Blur SSAO texture
if (gl_ssao_debug < 2) if (gl_ssao_debug < 2)
{ {
DepthBlurUniforms blurUniforms;
blurUniforms.BlurSharpness = blurSharpness;
blurUniforms.PowExponent = gl_ssao_exponent;
blurUniforms.InvFullResolution = { 1.0f / AmbientWidth, 0.0f };
renderstate->Clear(); renderstate->Clear();
renderstate->Shader = &BlurHorizontal; renderstate->Shader = &BlurHorizontal;
renderstate->Uniforms.Set(blurUniforms); renderstate->Uniforms.Set(blurUniforms);
@ -749,9 +749,15 @@ void PPAmbientOcclusion::Render(PPRenderState *renderstate, float m5, int sceneW
renderstate->SetNoBlend(); renderstate->SetNoBlend();
renderstate->Draw(); renderstate->Draw();
blurUniforms.InvFullResolution = { 0.0f, 1.0f / AmbientHeight };
renderstate->Clear();
renderstate->Shader = &BlurVertical; renderstate->Shader = &BlurVertical;
renderstate->Uniforms.Set(blurUniforms);
renderstate->Viewport = ambientViewport;
renderstate->SetInputTexture(0, &Ambient1); renderstate->SetInputTexture(0, &Ambient1);
renderstate->SetOutputTexture(&Ambient0); renderstate->SetOutputTexture(&Ambient0);
renderstate->SetNoBlend();
renderstate->Draw(); renderstate->Draw();
} }

View File

@ -6,61 +6,48 @@ layout(binding=0) uniform sampler2D AODepthTexture;
#define KERNEL_RADIUS 3.0 #define KERNEL_RADIUS 3.0
float CrossBilateralWeight(float r, float sampleDepth, float centerDepth) void main()
{ {
const float blurSigma = KERNEL_RADIUS * 0.5; const float blurSigma = KERNEL_RADIUS * 0.5;
const float blurFalloff = 1.0 / (2.0 * blurSigma * blurSigma); const float blurFalloff = 1.0 / (2.0 * blurSigma * blurSigma);
float deltaZ = (sampleDepth - centerDepth) * BlurSharpness; vec2 centerSample = texture(AODepthTexture, TexCoord).xy;
return exp2(-r * r * blurFalloff - deltaZ * deltaZ); float centerDepth = centerSample.y;
} float totalAO = centerSample.x;
void ProcessSample(float ao, float z, float r, float centerDepth, inout float totalAO, inout float totalW)
{
float w = CrossBilateralWeight(r, z, centerDepth);
totalAO += w * ao;
totalW += w;
}
void ProcessRadius(vec2 deltaUV, float centerDepth, inout float totalAO, inout float totalW)
{
for (float r = 1; r <= KERNEL_RADIUS; r += 1.0)
{
vec2 uv = r * deltaUV + TexCoord;
vec2 aoZ = texture(AODepthTexture, uv).xy;
ProcessSample(aoZ.x, aoZ.y, r, centerDepth, totalAO, totalW);
}
}
vec2 ComputeBlur(vec2 deltaUV)
{
vec2 aoZ = texture(AODepthTexture, TexCoord).xy;
float totalAO = aoZ.x;
float totalW = 1.0; float totalW = 1.0;
ProcessRadius(deltaUV, aoZ.y, totalAO, totalW); for (float r = 1.0; r <= KERNEL_RADIUS; r += 1.0)
ProcessRadius(-deltaUV, aoZ.y, totalAO, totalW); {
vec4 blurSample = texture(AODepthTexture, TexCoord - InvFullResolution * r);
float ao = blurSample.x;
float z = blurSample.y;
return vec2(totalAO / totalW, aoZ.y); float deltaZ = (z - centerDepth) * BlurSharpness;
} float w = exp2(-r * r * blurFalloff - deltaZ * deltaZ);
vec2 BlurX() totalAO += w * ao;
{ totalW += w;
return ComputeBlur(vec2(InvFullResolution.x, 0.0)); }
}
float BlurY() for (float r = 1.0; r <= KERNEL_RADIUS; r += 1.0)
{ {
return pow(clamp(ComputeBlur(vec2(0.0, InvFullResolution.y)).x, 0.0, 1.0), PowExponent); vec4 blurSample = texture(AODepthTexture, InvFullResolution * r + TexCoord);
} float ao = blurSample.x;
float z = blurSample.y;
float deltaZ = (z - centerDepth) * BlurSharpness;
float w = exp2(-r * r * blurFalloff - deltaZ * deltaZ);
totalAO += w * ao;
totalW += w;
}
float fragAO = totalAO / totalW;
void main()
{
#if defined(BLUR_HORIZONTAL) #if defined(BLUR_HORIZONTAL)
FragColor = vec4(BlurX(), 0.0, 1.0); FragColor = vec4(fragAO, centerDepth, 0.0, 1.0);
#else #else
FragColor = vec4(BlurY(), 0.0, 0.0, 1.0); FragColor = vec4(pow(clamp(fragAO, 0.0, 1.0), PowExponent), 0.0, 0.0, 1.0);
#endif #endif
} }