Use the mipmap trick to speed up ssao.

This commit is contained in:
vlj 2014-05-12 00:03:42 +02:00
parent 567974aeed
commit 11b7d20ad8
4 changed files with 18 additions and 24 deletions

View File

@ -26,9 +26,10 @@ in vec2 uv;
out float AO;
const float sigma = 1.;
const float tau = 7.;
const float beta = 0.0001;
const float epsilon = .00001;
const float radius = .1;
const float radius = 1.;
const float k = 1.;
#define SAMPLES 16
@ -38,13 +39,6 @@ const float invSamples = 1. / SAMPLES;
vec3 DecodeNormal(vec2 n);
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
vec3 rand(vec2 co)
{
float noiseX = clamp(fract(sin(dot(co ,vec2(12.9898,78.233))) * 43758.5453),0.0,1.0)*2.0-1.0;
float noiseY = clamp(fract(sin(dot(co ,vec2(12.9898,78.233)*2.0)) * 43758.5453),0.0,1.0)*2.0-1.0;
return vec3(noiseX, noiseY, length(texture(noise_texture, co * pow(3.14159265359, 2.)).xyz));
}
void main(void)
{
vec4 cur = texture(ntex, uv);
@ -59,22 +53,23 @@ void main(void)
float len = dot(vec3(1.0), abs(cur.xyz));
if (len < 0.2 || curdepth > 0.9955) discard;
float randAngle = rand(uv).x;
vec2 Xaxis = vec2(cos(randAngle), sin(randAngle));
Xaxis = normalize(Xaxis);
vec2 Yaxis = vec2(sin(randAngle), -cos(randAngle));
Yaxis = normalize(Yaxis);
int x = int(1024. * uv.x), y = int(1024. * uv.y);
float r = radius / FragPos.z;
float phi = 30. * (x ^ y) + 10. * x * y;
float bl = 0.0;
for(int i = 0; i < SAMPLES; ++i) {
vec2 occluder_uv = samplePoints[i].x * Xaxis + samplePoints[i].y * Yaxis;
occluder_uv *= samplePoints[i].w * radius;
float alpha = (i + .5) * invSamples;
float theta = 2. * 3.14 * tau * alpha + phi;
float h = r * alpha;
vec2 occluder_uv = h * vec2(cos(theta), sin(theta));
occluder_uv += uv;
if (occluder_uv.x < 0. || occluder_uv.x > 1. || occluder_uv.y < 0. || occluder_uv.y > 1.) continue;
float occluderFragmentDepth = texture(dtex, occluder_uv).x;
float m = round(log2(h) + 3.);
float occluderFragmentDepth = textureLod(dtex, occluder_uv, m).x;
vec4 OccluderPos = getPosFromUVDepth(vec3(occluder_uv, occluderFragmentDepth), InverseProjectionMatrix);
vec3 vi = (OccluderPos - FragPos).xyz;

View File

@ -423,11 +423,11 @@ void PostProcessing::renderSSAO()
glUseProgram(FullScreenShader::SSAOShader::Program);
glBindVertexArray(FullScreenShader::SSAOShader::vao);
setTexture(0, irr_driver->getRenderTargetTexture(RTT_NORMAL_AND_DEPTH), GL_LINEAR, GL_LINEAR);
setTexture(1, irr_driver->getDepthStencilTexture(), GL_LINEAR, GL_LINEAR);
setTexture(2, getTextureGLuint(noise_tex), GL_LINEAR, GL_LINEAR);
setTexture(0, irr_driver->getDepthStencilTexture(), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
setTexture(1, getTextureGLuint(noise_tex), GL_LINEAR, GL_LINEAR);
FullScreenShader::SSAOShader::setUniforms(0, 1, 2);
FullScreenShader::SSAOShader::setUniforms(0, 1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

View File

@ -2624,13 +2624,12 @@ namespace FullScreenShader
}*/
}
void SSAOShader::setUniforms(unsigned TU_ntex, unsigned TU_dtex, unsigned TU_noise)
void SSAOShader::setUniforms(unsigned TU_dtex, unsigned TU_noise)
{
if (UserConfigParams::m_ubo_disabled)
bypassUBO(Program);
glUniform4fv(FullScreenShader::SSAOShader::uniform_samplePoints, 16, FullScreenShader::SSAOShader::SSAOSamples);
glUniform1i(FullScreenShader::SSAOShader::uniform_ntex, TU_ntex);
glUniform1i(FullScreenShader::SSAOShader::uniform_dtex, TU_dtex);
glUniform1i(FullScreenShader::SSAOShader::uniform_noise_texture, TU_noise);
}

View File

@ -712,7 +712,7 @@ public:
static float SSAOSamples[64];
static void init();
static void setUniforms(unsigned TU_ntex, unsigned TU_dtex, unsigned TU_noise);
static void setUniforms(unsigned TU_dtex, unsigned TU_noise);
};
class FogShader