Jump to content
  • Advertisement
Sign in to follow this  
Manpreet Singh

Can't get ssao to work properly

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Edit: Forgot to mention, I'm using slimdx with wpf ( if it matters )

Hi folks,

I'm trying to get a simple SSAO shader to work, using this site as reference http://www.john-chap...ontent.php?id=8

And naturally I've run into problems again.

I'm getting this output ( the first one is with a smaller radius, and the next one with a larger radius, see shader)



Unfortunately, I can't use PIX since it crashes my application before it even starts. I realize that it may be harder to debug without showing the entire code, so first, here's my main SSAO shader. This is almost identical to the one being used in the aforementioned site except for the fact that I'm using a position buffer instead of reconstructing from depth.

float3 currentPosition = PositionTex.Sample(SSAOSampler,input.TexC).xyz; //Current view space fragment position from Position buffer

float3 currentNormal = normalize(NormalTex.Sample(SSAOSampler,input.TexC).xyz); //Current view space fragment normal

float2 NoiseScale = 0.0f;

NoiseScale.xy = BufferSize/(SSAO_noiseSize);

float3 randomVec = (RandomTex.Sample(SSAOSampler, input.TexC * NoiseScale).xyz * 2.0f) - 1.0f; //Random noise texture for kernel rotation

float3 tangent = normalize(randomVec - (currentNormal * dot(randomVec, currentNormal)));

float3 bitangent = cross(currentNormal, tangent);

float3x3 tbn = float3x3(tangent, bitangent, currentNormal);

float occ = 0.0f;

float Radius = 50.0f/BufferSize;

for(int i = 0 ; i < SSAO_kernelSize ; i++ )

float3 vec = AO_SampleVectors.xyz;

float3 sample = mul(vec,tbn);

sample = sample * Radius + currentPosition;

float4 offset = float4(sample, 1.0f);

offset = mul(offset,Projection);

offset.xy /= offset.w;

offset.x = offset.x * 0.5 + 0.5;

offset.y = -offset.y * 0.5 + 0.5;

float sampleDepth = PositionTex.Sample(SSAOSampler, offset.xy).z;

float range_check = abs(currentPosition.z - sampleDepth) < Radius ? 1.0 : 0.0;

occ += (sampleDepth <= sample.z ? 1.0 : 0.0) * range_check ;



float4 Color = {1.0f,1.0f,1.0f,1.0f};

Color.rgb = 1 - occ;

return Color;

To get the normal buffer and position buffers

Vertex shader

matrix viewproj = mul(View, Projection);

input.pos = mul(input.pos,rotation); //Note : rotation matrix = world matrix in this case, just so you don't confuse it

input.norm = normalize(mul(input.norm,rotation));

output.pos1 = mul(input.pos,View); // pos1 is the view space position

output.pos = mul(input.pos,viewproj);

output.uv = input.uv;

output.norm = normalize(mul(input.norm,View)); // norm is the view space normal

return output;


Pixel shader

output.Position = input.pos1;

output.Normal = normalize(input.norm);

return output;

I'll post pics of my position and normal buffer if those are required.

Thanks! Edited by Manpreet Singh

Share this post

Link to post
Share on other sites
Me again.

Sorry about not providing enough information, wrote that in a bit of a hurry.

Ok so I tried messing around some more, and I *think* this has got to do with my rendertarget size?

I'm just making the position and normal buffers with sizes 2048x2048 each, whereas my backbuffer is always resized to the application window(maximum size 1600 x 1200).

Should the render targets be the same size as that of the backbuffer?

Off topic : When viewing a view-space normal buffer, is it normal(no pun) for the scene to change color depending on the view direction? I look at the scene head on, everything is blue, when I turn left, everything facing me starts turning pinkish.

Share this post

Link to post
Share on other sites

My normals were getting scored incorrectly. So after fixing that I got this


Oh and by the way, I'm not doing any kind of blurring yet, so you see the noise.
Am I on the right track or is there still something horribly wrong here?

(mentioning again that I'm using a R32G32B32A32 buffer to store my normals and an identical buffer for view space positions, not reconstructing from depth
Normals : http://i.imgur.com/a4Ofm.png
Position : http://i.imgur.com/RQTwE.png)

Thanks for reading!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!