# Problem with simple SSAO implementation

This topic is 2197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone,

I have been trying to implement the simple SSAO described here : [url="http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/a-simple-and-practical-approach-to-ssao-r2753"]http://www.gamedev.n...h-to-ssao-r2753[/url] 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 :

[CODE]
Out.Position = mul(ViewProj, In.Position);
Out.VPosition = mul(View, In.Position);
[/CODE]

and in the Pixel Shader, I compute a normal from it :

[CODE]
Out.VNormal = normalize(cross(ddx(In.VPosition.xyz), ddy(In.VPosition.xyz)));
Out.Position = In.VPosition;[/CODE]

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.

[CODE]
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;
}[/CODE]

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 !

[img]http://img3.imageshack.us/img3/83/ssaodotnv.jpg[/img]

If it can be helpful, here is the VNormals texture.

[img]http://img268.imageshack.us/img268/1262/ssaovnormal.jpg[/img]

##### Share on other sites
Hi!

I think, when you sample the neighbor positions you are jumping too far. Texture coordinates are living in [0,1]x[0,1]. So, you should actually take a step of size 1/width in x-direction and 1/height in y-direction.
So here we go:
[CODE]int2 dim;
Positions.GetDimensions(dim.x, dim.y);
float2 stepSize = 1.0 / dim;
float3 diff = Positions.Sample(Filter, In.texCoord + coord1 * stepSize) - p;[/CODE]

You could also pass 1/width and 1/height to the shader as constants.
However, with that small code piece you can find out whether that’s the problem or not.

If you would use the Load intrinsics you'd have to specify the offset in pixels, though. If you don't need a sampler (i.e. filtering) you should always use Load, since it is faster.

Cheers!

##### Share on other sites
Thanks a lot!

This explains why I would get nothing if I didn't wrap my sampler. I can continue with the implementation with this issue solved.