I'm trying to implement
Inigo Quilez' version of Screen Space Ambient Occlusion in ATI's RenderMonkey and I've run into a problem...
Here's my code:
Depth-out shader
VS:
float4x4 matWorldViewProjection;
struct VS_INPUT
{
float4 Position: POSITION0;
};
struct VS_OUTPUT
{
float4 Position: POSITION0;
float4 Pos : TEXCOORD0;
};
VS_OUTPUT vs_main(VS_INPUT Input)
{
VS_OUTPUT Output;
Output.Position = mul(Input.Position, matWorldViewProjection);
Output.Pos = Output.Position;
return Output;
}
PS:
struct PS_INPUT
{
float4 Pos : TEXCOORD0;
};
float4 ps_main(PS_INPUT Input) : COLOR0
{
float depth = Input.Pos.z / Input.Pos.w;
return float4(depth, 1.0f, 1.0f, 1.0f);
}
SSAO-Shader
VS:
struct VS_INPUT
{
float4 Position : POSITION0;
};
struct VS_OUTPUT
{
float4 Position : POSITION;
float2 TexCoord : TEXCOORD0;
float4 Pos : TEXCOORD1;
};
VS_OUTPUT vs_main(VS_INPUT Input)
{
VS_OUTPUT Output;
Output.Position = float4(sign(Input.Position.xy), 0.0, 1.0);
Output.TexCoord.x = (Output.Position.x + 1.0) * 0.5;
Output.TexCoord.y = 1.0 - ((Output.Position.y + 1.0) * 0.5);
Output.Pos = Output.Position;
return Output;
}
PS:
sampler2D DepthTexture;
sampler2D RandomTexture;
float4x4 matProjectionInverse;
float fRange;
int nNumSamples;
float2 fRandTextureSize;
struct PS_INPUT
{
float2 TexCoord : TEXCOORD0;
float4 Pos : TEXCOORD1;
};
float4 ps_main(PS_INPUT Input) : COLOR0
{
float4 samples[16] =
{
float4(0.355512, -0.709318, -0.102371, 0.0),
float4(0.534186, 0.71511, -0.115167, 0.0),
float4(-0.87866, 0.157139, -0.115167, 0.0),
float4(0.140679, -0.475516, -0.0639818, 0.0),
float4(-0.0796121, 0.158842, -0.677075, 0.0),
float4(-0.0759516, -0.101676, -0.483625, 0.0),
float4(0.12493, -0.0223423, -0.483625, 0.0),
float4(-0.0720074, 0.243395, -0.967251, 0.0),
float4(-0.207641, 0.414286, 0.187755, 0.0),
float4(-0.277332, -0.371262, 0.187755, 0.0),
float4(0.63864, -0.114214, 0.262857, 0.0),
float4(-0.184051, 0.622119, 0.262857, 0.0),
float4(0.110007, -0.219486, 0.435574, 0.0),
float4(0.235085, 0.314707, 0.696918, 0.0),
float4(-0.290012, 0.0518654, 0.522688, 0.0),
float4(0.0975089, -0.329594, 0.609803, 0.0)
};
float depth = tex2D(DepthTexture, Input.TexCoord).r;
float4 eyePos = mul(float4(Input.Pos.x, Input.Pos.y, depth, 1.0f), matProjectionInverse);
eyePos.xyz = eyePos.xyz / eyePos.www;
eyePos.w = 1.0f;
float4 randomPlane = tex2D(RandomTexture, Input.Pos.xy * fRandTextureSize.xy);
randomPlane = randomPlane * 2.0 - float4(1.0, 1.0, 1.0, 1.0);
float occlusion = 0.0;
for (int i = 0; i < nNumSamples; i++)
{
float3 sample = eyePos + fRange * reflect(samples.xyz, randomPlane.xyz);
float3 ss = sample.xyz * float3(0.75, 1.0, 1.0);
float4 sz = tex2Dproj(DepthTexture, float4(ss * 0.5 + ss.z * float3(0.5, 0.5, 0.5), 1.0));
float zDifference = 50.0 * max(sample.z - sz.x, 0.0);
occlusion += 1.0 / (1.0 + zDifference * zDifference);
}
return float4(occlusion / nNumSamples, occlusion / nNumSamples, occlusion / nNumSamples, occlusion / nNumSamples);
}
Right now, all this gives me is a noisy image with all geometry rendered completely in black.
I've tried to figure this out, but I don't see what I'm doing wrong. I'm suspecting that the way I calculate the Eye-Position might be incorrect, but then again I don't see why it would be. I didn't use Quilez' method because I couldn't get it to work in RenderMonkey, but I assumed that wouldn't be a problem.
EDIT: Here's a screenshot:
Any ideas?
Thanks a lot in advance!