From 723eb746f660d8ee63b63fff7a515ad27fb4264f Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Tue, 30 Aug 2016 01:09:21 +0200 Subject: [PATCH] Add ssao random texture --- src/gl/renderer/gl_postprocess.cpp | 10 ++++++++++ src/gl/shaders/gl_ambientshader.cpp | 11 ++++++++++- src/gl/shaders/gl_ambientshader.h | 2 ++ wadsrc/static/shaders/glsl/ssao.fp | 16 +++++++++------- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/gl/renderer/gl_postprocess.cpp b/src/gl/renderer/gl_postprocess.cpp index 9df28ce5b..4fb83c642 100644 --- a/src/gl/renderer/gl_postprocess.cpp +++ b/src/gl/renderer/gl_postprocess.cpp @@ -100,6 +100,7 @@ CVAR(Float, gl_lens_kcube, 0.1f, 0) CVAR(Float, gl_lens_chromatic, 1.12f, 0) CVAR(Bool, gl_ssao, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) +CVAR(Float, gl_ssao_strength, 0.7, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) CVAR(Bool, gl_ssao_debug, false, 0) CVAR(Float, gl_ssao_bias, 0.5f, 0) CVAR(Float, gl_ssao_radius, 100.0f, 0) @@ -148,11 +149,13 @@ void FGLRenderer::AmbientOccludeScene() FGLDebug::PushGroup("AmbientOccludeScene"); FGLPostProcessState savedState; + savedState.SaveTextureBinding1(); float bias = gl_ssao_bias; float aoRadius = gl_ssao_radius; const float blurAmount = gl_ssao_blur_amount; int blurSampleCount = gl_ssao_blur_samples; + float aoStrength = gl_ssao_strength; //float tanHalfFovy = tan(fovy * (M_PI / 360.0f)); float tanHalfFovy = 1.0f / 1.33333302f; //gl_RenderState.mProjectionMatrix.get()[5]; @@ -178,8 +181,14 @@ void FGLRenderer::AmbientOccludeScene() glBindTexture(GL_TEXTURE_2D, mBuffers->AmbientTexture0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, mBuffers->AmbientRandomTexture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glActiveTexture(GL_TEXTURE0); mSSAOShader->Bind(); mSSAOShader->DepthTexture.Set(0); + mSSAOShader->RandomTexture.Set(1); mSSAOShader->UVToViewA.Set(2.0f * invFocalLenX, -2.0f * invFocalLenY); mSSAOShader->UVToViewB.Set(-invFocalLenX, invFocalLenY); mSSAOShader->InvFullResolution.Set(1.0f / mBuffers->AmbientWidth, 1.0f / mBuffers->AmbientHeight); @@ -187,6 +196,7 @@ void FGLRenderer::AmbientOccludeScene() mSSAOShader->NegInvR2.Set(-1.0f / r2); mSSAOShader->RadiusToScreen.Set(aoRadius * 0.5 / tanHalfFovy * mBuffers->AmbientHeight); mSSAOShader->AOMultiplier.Set(1.0f / (1.0f - nDotVBias)); + mSSAOShader->AOStrength.Set(aoStrength); RenderScreenQuad(); // Blur SSAO texture diff --git a/src/gl/shaders/gl_ambientshader.cpp b/src/gl/shaders/gl_ambientshader.cpp index d9d4a3425..55682a91e 100644 --- a/src/gl/shaders/gl_ambientshader.cpp +++ b/src/gl/shaders/gl_ambientshader.cpp @@ -71,12 +71,20 @@ void FSSAOShader::Bind() { if (!mShader) { + const char *defines = R"( + #define USE_RANDOM_TEXTURE + #define RANDOM_TEXTURE_WIDTH 4.0 + #define NUM_DIRECTIONS 8.0 + #define NUM_STEPS 4.0 + )"; + mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330); - mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/ssao.fp", "", 330); + mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/ssao.fp", defines, 330); mShader.SetFragDataLocation(0, "FragColor"); mShader.Link("shaders/glsl/ssao"); mShader.SetAttribLocation(0, "PositionInProjection"); DepthTexture.Init(mShader, "DepthTexture"); + RandomTexture.Init(mShader, "RandomTexture"); UVToViewA.Init(mShader, "UVToViewA"); UVToViewB.Init(mShader, "UVToViewB"); InvFullResolution.Init(mShader, "InvFullResolution"); @@ -84,6 +92,7 @@ void FSSAOShader::Bind() NegInvR2.Init(mShader, "NegInvR2"); RadiusToScreen.Init(mShader, "RadiusToScreen"); AOMultiplier.Init(mShader, "AOMultiplier"); + AOStrength.Init(mShader, "AOStrength"); } mShader.Bind(); } diff --git a/src/gl/shaders/gl_ambientshader.h b/src/gl/shaders/gl_ambientshader.h index 44f916596..fdff178ec 100644 --- a/src/gl/shaders/gl_ambientshader.h +++ b/src/gl/shaders/gl_ambientshader.h @@ -24,6 +24,7 @@ public: void Bind(); FBufferedUniformSampler DepthTexture; + FBufferedUniformSampler RandomTexture; FBufferedUniform2f UVToViewA; FBufferedUniform2f UVToViewB; FBufferedUniform2f InvFullResolution; @@ -31,6 +32,7 @@ public: FBufferedUniform1f NegInvR2; FBufferedUniform1f RadiusToScreen; FBufferedUniform1f AOMultiplier; + FBufferedUniform1f AOStrength; private: FShaderProgram mShader; diff --git a/wadsrc/static/shaders/glsl/ssao.fp b/wadsrc/static/shaders/glsl/ssao.fp index c9f8534d9..f143e913f 100644 --- a/wadsrc/static/shaders/glsl/ssao.fp +++ b/wadsrc/static/shaders/glsl/ssao.fp @@ -11,15 +11,14 @@ uniform float NegInvR2; uniform float RadiusToScreen; uniform float AOMultiplier; +uniform float AOStrength; + uniform sampler2D DepthTexture; -#if USE_RANDOM_TEXTURE +#if defined(USE_RANDOM_TEXTURE) uniform sampler2D RandomTexture; #endif -#define NUM_DIRECTIONS 8.0 -#define NUM_STEPS 4.0 - #define PI 3.14159265358979323846 // Calculate eye space position for the specified texture coordinate @@ -54,13 +53,12 @@ vec2 RotateDirection(vec2 dir, vec2 cossin) vec4 GetJitter() { -#if !USE_RANDOM_TEXTURE +#if !defined(USE_RANDOM_TEXTURE) return vec4(1,0,1,1); //vec3 rand = noise3(TexCoord.x + TexCoord.y); //float angle = 2.0 * PI * rand.x / NUM_DIRECTIONS; //return vec4(cos(angle), sin(angle), rand.y, rand.z); #else - #define RANDOM_TEXTURE_WIDTH 4.0 return texture(RandomTexture, gl_FragCoord.xy / RANDOM_TEXTURE_WIDTH); #endif } @@ -109,7 +107,11 @@ void main() { vec3 viewPosition = FetchViewPos(TexCoord); vec3 viewNormal = ReconstructNormal(viewPosition); - float occlusion = ComputeAO(viewPosition, viewNormal); + float occlusion = ComputeAO(viewPosition, viewNormal) * AOStrength + (1.0 - AOStrength); + + // GZDoom does not use linear buffers at the moment, apply some gamma to get it closer to correct + occlusion = occlusion * occlusion; + //FragColor = vec4(viewPosition.x * 0.001 + 0.5, viewPosition.y * 0.001 + 0.5, viewPosition.z * 0.001, 1.0); //FragColor = vec4(viewNormal.x * 0.5 + 0.5, viewNormal.y * 0.5 + 0.5, viewNormal.z * 0.5 + 0.5, 1.0); //FragColor = vec4(occlusion, viewPosition.z, 0.0, 1.0);