Add ssao random texture

This commit is contained in:
Magnus Norddahl 2016-08-30 01:09:21 +02:00
parent 9076d46261
commit 723eb746f6
4 changed files with 31 additions and 8 deletions

View file

@ -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

View file

@ -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();
}

View file

@ -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;

View file

@ -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);