Can't get ssao to work properly

Started by
1 comment, last by FrogJohnson 11 years, 9 months ago
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)

http://i.imgur.com/EjqFa.png

http://i.imgur.com/7S2u9.png

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 ;

}

occ/=SSAO_kernelSize;

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

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

http://i.imgur.com/XoAYv.png
http://i.imgur.com/hbaY3.png

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!

This topic is closed to new replies.

Advertisement