Sign in to follow this  

Can't get ssao to work properly

This topic is 1984 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 [url="http://www.john-chapman.net/content.php?id=8"]http://www.john-chap...ontent.php?id=8[/url]

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)

[url="http://i.imgur.com/EjqFa.png"]http://i.imgur.com/EjqFa.png[/url]

[url="http://i.imgur.com/7S2u9.png"]http://i.imgur.com/7S2u9.png[/url]

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.

[CODE]
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[i].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 ;

}

occ/=SSAO_kernelSize;

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

Color.rgb = 1 - occ;

return Color;

[/CODE]

To get the normal buffer and position buffers

[CODE]

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;

[/CODE]

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
Update.

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

[url="http://i.imgur.com/XoAYv.png"]http://i.imgur.com/XoAYv.png[/url]
[url="http://i.imgur.com/hbaY3.png"]http://i.imgur.com/hbaY3.png[/url]

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 : [url="http://i.imgur.com/a4Ofm.png"]http://i.imgur.com/a4Ofm.png[/url]
Position : [url="http://i.imgur.com/RQTwE.png"]http://i.imgur.com/RQTwE.png[/url])

Thanks for reading!

Share this post


Link to post
Share on other sites

This topic is 1984 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this