Resolve multisampling depth in shader

This commit is contained in:
Magnus Norddahl 2016-09-03 04:12:00 +02:00
parent a246b58673
commit 09bec67821
4 changed files with 54 additions and 29 deletions

View file

@ -127,8 +127,7 @@ void FGLRenderer::RenderScreenQuad()
void FGLRenderer::PostProcessScene() void FGLRenderer::PostProcessScene()
{ {
if (FGLRenderBuffers::IsEnabled()) mBuffers->BlitSceneToTexture(); mBuffers->BlitSceneToTexture();
AmbientOccludeScene();
UpdateCameraExposure(); UpdateCameraExposure();
BloomScene(); BloomScene();
TonemapScene(); TonemapScene();
@ -156,6 +155,7 @@ void FGLRenderer::AmbientOccludeScene()
const float blurAmount = gl_ssao_blur_amount; const float blurAmount = gl_ssao_blur_amount;
int blurSampleCount = gl_ssao_blur_samples; int blurSampleCount = gl_ssao_blur_samples;
float aoStrength = gl_ssao_strength; float aoStrength = gl_ssao_strength;
bool multisample = gl_multisample > 1;
//float tanHalfFovy = tan(fovy * (M_PI / 360.0f)); //float tanHalfFovy = tan(fovy * (M_PI / 360.0f));
float tanHalfFovy = 1.0f / 1.33333302f; // 1.0f / gl_RenderState.mProjectionMatrix.get()[5]; float tanHalfFovy = 1.0f / 1.33333302f; // 1.0f / gl_RenderState.mProjectionMatrix.get()[5];
@ -168,12 +168,15 @@ void FGLRenderer::AmbientOccludeScene()
glBindFramebuffer(GL_FRAMEBUFFER, mBuffers->AmbientFB0); glBindFramebuffer(GL_FRAMEBUFFER, mBuffers->AmbientFB0);
glViewport(0, 0, mBuffers->AmbientWidth, mBuffers->AmbientHeight); glViewport(0, 0, mBuffers->AmbientWidth, mBuffers->AmbientHeight);
mBuffers->BindSceneDepthTexture(0); mBuffers->BindSceneDepthTexture(0);
mLinearDepthShader->Bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
mLinearDepthShader->DepthTexture.Set(0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
mLinearDepthShader->LinearizeDepthA.Set(1.0f / GetZFar() - 1.0f / GetZNear()); mLinearDepthShader->Bind(multisample);
mLinearDepthShader->LinearizeDepthB.Set(MAX(1.0f / GetZNear(), 1.e-8f)); mLinearDepthShader->DepthTexture[multisample].Set(0);
mLinearDepthShader->InverseDepthRangeA.Set(1.0f); if (multisample) mLinearDepthShader->SampleCount[multisample].Set(gl_multisample);
mLinearDepthShader->InverseDepthRangeB.Set(0.0f); mLinearDepthShader->LinearizeDepthA[multisample].Set(1.0f / GetZFar() - 1.0f / GetZNear());
mLinearDepthShader->LinearizeDepthB[multisample].Set(MAX(1.0f / GetZNear(), 1.e-8f));
mLinearDepthShader->InverseDepthRangeA[multisample].Set(1.0f);
mLinearDepthShader->InverseDepthRangeB[multisample].Set(0.0f);
RenderScreenQuad(); RenderScreenQuad();
// Apply ambient occlusion // Apply ambient occlusion
@ -218,7 +221,7 @@ void FGLRenderer::AmbientOccludeScene()
RenderScreenQuad(); RenderScreenQuad();
// Add SSAO back to scene texture: // Add SSAO back to scene texture:
mBuffers->BindCurrentFB(); mBuffers->BindSceneFB();
glViewport(mSceneViewport.left, mSceneViewport.top, mSceneViewport.width, mSceneViewport.height); glViewport(mSceneViewport.left, mSceneViewport.top, mSceneViewport.width, mSceneViewport.height);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD); glBlendEquation(GL_FUNC_ADD);

View file

@ -49,22 +49,24 @@
#include "gl/system/gl_cvars.h" #include "gl/system/gl_cvars.h"
#include "gl/shaders/gl_ambientshader.h" #include "gl/shaders/gl_ambientshader.h"
void FLinearDepthShader::Bind() void FLinearDepthShader::Bind(bool multisample)
{ {
if (!mShader) auto &shader = mShader[multisample];
if (!shader)
{ {
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330); shader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330);
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/lineardepth.fp", "", 330); shader.Compile(FShaderProgram::Fragment, "shaders/glsl/lineardepth.fp", multisample ? "#define MULTISAMPLE\n" : "", 330);
mShader.SetFragDataLocation(0, "FragColor"); shader.SetFragDataLocation(0, "FragColor");
mShader.Link("shaders/glsl/lineardepth"); shader.Link("shaders/glsl/lineardepth");
mShader.SetAttribLocation(0, "PositionInProjection"); shader.SetAttribLocation(0, "PositionInProjection");
DepthTexture.Init(mShader, "DepthTexture"); DepthTexture[multisample].Init(shader, "DepthTexture");
LinearizeDepthA.Init(mShader, "LinearizeDepthA"); SampleCount[multisample].Init(shader, "SampleCount");
LinearizeDepthB.Init(mShader, "LinearizeDepthB"); LinearizeDepthA[multisample].Init(shader, "LinearizeDepthA");
InverseDepthRangeA.Init(mShader, "InverseDepthRangeA"); LinearizeDepthB[multisample].Init(shader, "LinearizeDepthB");
InverseDepthRangeB.Init(mShader, "InverseDepthRangeB"); InverseDepthRangeA[multisample].Init(shader, "InverseDepthRangeA");
InverseDepthRangeB[multisample].Init(shader, "InverseDepthRangeB");
} }
mShader.Bind(); shader.Bind();
} }
void FSSAOShader::Bind() void FSSAOShader::Bind()

View file

@ -6,16 +6,17 @@
class FLinearDepthShader class FLinearDepthShader
{ {
public: public:
void Bind(); void Bind(bool multisample);
FBufferedUniformSampler DepthTexture; FBufferedUniformSampler DepthTexture[2];
FBufferedUniform1f LinearizeDepthA; FBufferedUniform1i SampleCount[2];
FBufferedUniform1f LinearizeDepthB; FBufferedUniform1f LinearizeDepthA[2];
FBufferedUniform1f InverseDepthRangeA; FBufferedUniform1f LinearizeDepthB[2];
FBufferedUniform1f InverseDepthRangeB; FBufferedUniform1f InverseDepthRangeA[2];
FBufferedUniform1f InverseDepthRangeB[2];
private: private:
FShaderProgram mShader; FShaderProgram mShader[2];
}; };
class FSSAOShader class FSSAOShader

View file

@ -2,7 +2,12 @@
in vec2 TexCoord; in vec2 TexCoord;
out vec4 FragColor; out vec4 FragColor;
#if defined(MULTISAMPLE)
uniform sampler2DMS DepthTexture;
uniform int SampleCount;
#else
uniform sampler2D DepthTexture; uniform sampler2D DepthTexture;
#endif
uniform float LinearizeDepthA; uniform float LinearizeDepthA;
uniform float LinearizeDepthB; uniform float LinearizeDepthB;
uniform float InverseDepthRangeA; uniform float InverseDepthRangeA;
@ -10,7 +15,21 @@ uniform float InverseDepthRangeB;
void main() void main()
{ {
#if defined(MULTISAMPLE)
ivec2 texSize = textureSize(DepthTexture);
ivec2 ipos = ivec2(TexCoord * 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));
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, TexCoord).x;
#endif
float normalizedDepth = clamp(InverseDepthRangeA * depth + InverseDepthRangeB, 0.0, 1.0); float normalizedDepth = clamp(InverseDepthRangeA * depth + InverseDepthRangeB, 0.0, 1.0);
FragColor = vec4(1.0 / (normalizedDepth * LinearizeDepthA + LinearizeDepthB), 0.0, 0.0, 1.0); FragColor = vec4(1.0 / (normalizedDepth * LinearizeDepthA + LinearizeDepthB), 0.0, 0.0, 1.0);
} }