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!