Hi,
I implemented SSAO based on some tutorials and it does work correctly. However, it is very slow (resulting in low FPS) if the geometry is near (< 0.5 meters) the camera (e.g. facing a wall in first person view). Can anyone explain why? And is there a solution to this?
float main(PixelInputType input) : SV_TARGET
{
uint3 TexCoords = uint3(input.position.xy, 0);
float4 Position = positionTexture.Load(TexCoords);
if (Position.w != -1.0f){
float3 normal = normalTexture.Load(TexCoords).xyz;
float3 rvec = float3(NoiseTexture.Load(TexCoords & NoiseTextureCoordMask), 0.0f);
if (abs(normal.x) > 0.75f){
rvec = float3(0, rvec.x, rvec.y);
}
else if (abs(normal.y) > 0.75f){
rvec = float3(rvec.x, 0, rvec.y);
}
float3 tangent = normalize(rvec - normal * dot(rvec, normal));
float3 bitangent = cross(normal, tangent);
float3x3 tbn = float3x3(tangent, bitangent, normal); //rows
float Occlusion = 0.0;
for (uint i = 0; i < KernelSize; ++i) {
float3 lKernelData = KernelData[i].xyz;
float3 RotatedKernel = mul(lKernelData, tbn);
if (dot(RotatedKernel, normal) > 0.1f){
float3 Sample = RotatedKernel*Radius + Position.xyz;
float4 offset = mul(float4(Sample, 1.0), ViewProjectionMatrix);
offset.xyz /= offset.w;
if (offset.z > 0){
offset.xy = offset.xy * 0.5 + 0.5;
offset.y = 1 - offset.y;
float4 SamplePos = positionTexture.Sample(SampleTypePoint, offset.xy);
if (SamplePos.w != -1.0f){
float SquaredDifference = dot(Position.xyz - SamplePos.xyz, Position.xyz - SamplePos.xyz);
// range check & accumulate:
Occlusion += ((dot(SamplePos.xyz, SamplePos.xyz) <= dot(Sample.xyz, Sample.xyz) && SquaredDifference < Radius*Radius && SquaredDifference != 0) ? 1.0 : 0.0);
}
}
}
}
return min(1.0f,(Occlusion / KernelSize)*1.25);
}
else{
return 0.0f;
}
}
My position is relative to the camera, so I do depth calculations based on the position. Position.w != -1 is true if it is actual geometry and not the sky box.
Is it slow due to cache misses because the shader looks up texels far from the one rendered? That's what I would guess, but I'm not sure.
Best regards,
Magogan