I have been trying to implement the simple SSAO described here : http://www.gamedev.n...h-to-ssao-r2753 inside a DirectX demo, but so far I have not been able to make it work. (The demo is based on Humus Framework).
Edit : I forgot to state that this demo uses inverted z (far plane is z = 0, near plane is z = 1). I don't know if it is relevant with the problem I am encountering though.
Here are the steps that I take in order to prepare the textures used for the SSAO :
During my deferred rendering pass, I compute a position in View Space in the vertex shader, VPosition :
Out.Position = mul(ViewProj, In.Position);
Out.VPosition = mul(View, In.Position);
and in the Pixel Shader, I compute a normal from it :
Out.VNormal = normalize(cross(ddx(In.VPosition.xyz), ddy(In.VPosition.xyz)));
Out.Position = In.VPosition;
Position and VNormal are 2 render targets using the format DXGI_FORMAT_R11G11B10_FLOAT.
In the SSAO pass, those 2 render targets are fed as input textures to the shader. In order to understand what is going wrong, I have kept only the smallest step from the algorithm. So here, I am computing the diff vector between the point located at (x+1,y) and the current point, and I look at the dot product between this diff and the normal.
I would expect that every point on a plane surface should have 0 for this dot product, except if they are at the edge.
Texture2D <float4> Base;
Texture2D <float3> Positions;
Texture2D <float3> VNormals;
SamplerState Filter;
float4 main(PsIn In) : SV_Target
{
float4 base = Base.Sample(Sam, In.texCoord);
const float2 vec[4] = {float2(1,0),float2(-1,0),
float2(0,1),float2(0,-1)};
float3 p = Positions.Sample(Filter, In.texCoord);
float3 n = VNormals.Sample(Filter, In.texCoord);
n = normalize(n);
float2 coord1 = vec[0];
float3 diff = Positions.Sample(Filter, In.texCoord + coord1) - p;
const float3 v = normalize(diff);
base.rgb = float3(dot(n,v),dot(n,v),dot(n,v));
return base;
}
However, as you can see on this screen, this dot product is not at all what I expected it to be, so in turn it is not surprising that the SSAO doesn't work at all. Any help would be appreciated !
If it can be helpful, here is the VNormals texture.