diff --git a/src/gl/renderer/gl_postprocess.cpp b/src/gl/renderer/gl_postprocess.cpp index 8282bc387..3031d80ba 100644 --- a/src/gl/renderer/gl_postprocess.cpp +++ b/src/gl/renderer/gl_postprocess.cpp @@ -177,6 +177,8 @@ 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->Offset[multisample].Set(mSceneViewport.left / (float)mScreenViewport.width, mSceneViewport.top / (float)mScreenViewport.height); RenderScreenQuad(); // Apply ambient occlusion @@ -236,7 +238,6 @@ void FGLRenderer::AmbientOccludeScene() mSSAOCombineShader->Bind(); mSSAOCombineShader->AODepthTexture.Set(0); RenderScreenQuad(); - glViewport(mScreenViewport.left, mScreenViewport.top, mScreenViewport.width, mScreenViewport.height); FGLDebug::PopGroup(); } diff --git a/src/gl/scene/gl_scene.cpp b/src/gl/scene/gl_scene.cpp index 27772a0ca..a50fe87f8 100644 --- a/src/gl/scene/gl_scene.cpp +++ b/src/gl/scene/gl_scene.cpp @@ -491,6 +491,8 @@ void FGLRenderer::DrawScene(int drawmode) RenderScene(recursion); + AmbientOccludeScene(); + // Handle all portals after rendering the opaque objects but before // doing all translucent stuff recursion++; diff --git a/src/gl/shaders/gl_ambientshader.cpp b/src/gl/shaders/gl_ambientshader.cpp index f3b6eced6..54a651281 100644 --- a/src/gl/shaders/gl_ambientshader.cpp +++ b/src/gl/shaders/gl_ambientshader.cpp @@ -65,6 +65,8 @@ void FLinearDepthShader::Bind(bool multisample) LinearizeDepthB[multisample].Init(shader, "LinearizeDepthB"); InverseDepthRangeA[multisample].Init(shader, "InverseDepthRangeA"); InverseDepthRangeB[multisample].Init(shader, "InverseDepthRangeB"); + Scale[multisample].Init(shader, "Scale"); + Offset[multisample].Init(shader, "Offset"); } shader.Bind(); } diff --git a/src/gl/shaders/gl_ambientshader.h b/src/gl/shaders/gl_ambientshader.h index 5f9ef630b..5b18ea85f 100644 --- a/src/gl/shaders/gl_ambientshader.h +++ b/src/gl/shaders/gl_ambientshader.h @@ -14,6 +14,8 @@ public: FBufferedUniform1f LinearizeDepthB[2]; FBufferedUniform1f InverseDepthRangeA[2]; FBufferedUniform1f InverseDepthRangeB[2]; + FBufferedUniform2f Scale[2]; + FBufferedUniform2f Offset[2]; private: FShaderProgram mShader[2]; diff --git a/wadsrc/static/shaders/glsl/lineardepth.fp b/wadsrc/static/shaders/glsl/lineardepth.fp index 31df9dc32..7e4eee745 100644 --- a/wadsrc/static/shaders/glsl/lineardepth.fp +++ b/wadsrc/static/shaders/glsl/lineardepth.fp @@ -8,28 +8,34 @@ uniform int SampleCount; #else uniform sampler2D DepthTexture; #endif + uniform float LinearizeDepthA; uniform float LinearizeDepthB; uniform float InverseDepthRangeA; uniform float InverseDepthRangeB; +uniform vec2 Scale; +uniform vec2 Offset; void main() { + vec2 uv = Offset + TexCoord * Scale; + #if defined(MULTISAMPLE) ivec2 texSize = textureSize(DepthTexture); - ivec2 ipos = ivec2(TexCoord * vec2(texSize)); + ivec2 ipos = ivec2(uv * vec2(texSize)); float depth = 0.0; for (int i = 0; i < SampleCount; i++) depth += texelFetch(DepthTexture, ipos, i).x; depth /= float(SampleCount); #else /*ivec2 texSize = textureSize(DepthTexture, 0); - ivec2 ipos = ivec2(TexCoord * vec2(texSize)); + 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(DepthTexture, ipos, 0).x;*/ - float depth = texture(DepthTexture, TexCoord).x; + float depth = texture(DepthTexture, uv).x; #endif + float normalizedDepth = clamp(InverseDepthRangeA * depth + InverseDepthRangeB, 0.0, 1.0); FragColor = vec4(1.0 / (normalizedDepth * LinearizeDepthA + LinearizeDepthB), 0.0, 0.0, 1.0); }