From 63fb604e988298edf1ad9cf13e1f8a1ad95b747e Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Fri, 9 Sep 2016 18:19:00 +0200 Subject: [PATCH] Fix stripes in the reconstructed normals due to down scaling --- src/gl/renderer/gl_postprocess.cpp | 2 +- wadsrc/static/shaders/glsl/lineardepth.fp | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/gl/renderer/gl_postprocess.cpp b/src/gl/renderer/gl_postprocess.cpp index 06c85a7bc..e00ca7305 100644 --- a/src/gl/renderer/gl_postprocess.cpp +++ b/src/gl/renderer/gl_postprocess.cpp @@ -192,7 +192,7 @@ void FGLRenderer::AmbientOccludeScene() mLinearDepthShader->LinearizeDepthB[multisample].Set(MAX(1.0f / GetZNear(), 1.e-8f)); mLinearDepthShader->InverseDepthRangeA[multisample].Set(1.0f); mLinearDepthShader->InverseDepthRangeB[multisample].Set(0.0f); - mLinearDepthShader->Scale[multisample].Set(mSceneViewport.width / (float)mScreenViewport.width, mSceneViewport.height / (float)mScreenViewport.height); + mLinearDepthShader->Scale[multisample].Set(mBuffers->AmbientWidth * 2.0f / (float)mScreenViewport.width, mBuffers->AmbientHeight * 2.0f / (float)mScreenViewport.height); mLinearDepthShader->Offset[multisample].Set(mSceneViewport.left / (float)mScreenViewport.width, mSceneViewport.top / (float)mScreenViewport.height); RenderScreenQuad(); diff --git a/wadsrc/static/shaders/glsl/lineardepth.fp b/wadsrc/static/shaders/glsl/lineardepth.fp index 61e5e8126..558738bd9 100644 --- a/wadsrc/static/shaders/glsl/lineardepth.fp +++ b/wadsrc/static/shaders/glsl/lineardepth.fp @@ -24,18 +24,21 @@ void main() #if defined(MULTISAMPLE) ivec2 texSize = textureSize(DepthTexture); - ivec2 ipos = ivec2(uv * vec2(texSize)); +#else + ivec2 texSize = textureSize(DepthTexture, 0); +#endif + + // Use floor here because as we downscale the sampling error has to remain uniform to prevent + // noise in the depth values. + ivec2 ipos = ivec2(max(floor(uv * vec2(texSize) - 0.75), vec2(0.0))); + +#if defined(MULTISAMPLE) float depth = 0.0; for (int i = 0; i < SampleCount; i++) depth += texelFetch(ColorTexture, ipos, i).a != 0.0 ? texelFetch(DepthTexture, ipos, i).x : 1.0; depth /= float(SampleCount); #else - /*ivec2 texSize = textureSize(DepthTexture, 0); - ivec2 ipos = ivec2(uv * vec2(texSize)); - if (ipos.x < 0) ipos.x += texSize.x; - if (ipos.y < 0) ipos.y += texSize.y; - float depth = texelFetch(ColorTexture, ipos, 0).a != 0.0 ? texelFetch(DepthTexture, ipos, 0).x : 1.0;*/ - float depth = texture(ColorTexture, uv).a != 0.0 ? texture(DepthTexture, uv).x : 1.0; + float depth = texelFetch(ColorTexture, ipos, 0).a != 0.0 ? texelFetch(DepthTexture, ipos, 0).x : 1.0; #endif float normalizedDepth = clamp(InverseDepthRangeA * depth + InverseDepthRangeB, 0.0, 1.0);