Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsaoSep.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsaoSep.cg	(revision 3302)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsaoSep.cg	(revision 3302)
@@ -0,0 +1,162 @@
+#include "../shaderenv.h"
+#include "common.h"
+
+
+/*************************************************/
+/*     Filter for combining ssao with image      */
+/*************************************************/
+
+
+struct fragment
+{
+	float2 texCoord: TEXCOORD0; 
+	float3 view: TEXCOORD1;
+};
+
+
+struct pixel
+{
+	float4 illum_col: COLOR0;
+};
+
+
+float2 FilterSample(float4 sampleTexCoord, 
+					uniform sampler2D ssaoTex,
+					uniform sampler2D colorsTex,
+					float3 centerPos,
+					float3 bl,
+					float3 br,
+					float3 tl,
+					float3 tr)
+{
+	float2 aoSample = tex2Dlod(ssaoTex, sampleTexCoord);
+		
+	// check spatial discontinuity
+	// note: using the depth from the color texture is not 100% correct as depth was
+	// not scaled with the interpolated view vector depth yet ...
+	float3 samplePos = ReconstructSamplePos(colorsTex, sampleTexCoord.xy, bl, br, tl, tr);
+	//samplePos = ReconstructSamplePos(ssaoTex, sampleTexCoord.xy, bl, br, tl, tr);
+
+	float len = min(SqrLen(centerPos - samplePos), 1e2f);
+	float spatialFactor = 1.0f / max(len, 1e-3f);
+
+	float convergenceFactor = aoSample.y + 1.0f;
+
+	// combine the weights
+	float w = convergenceFactor * convergenceFactor * spatialFactor;// * normalFactor;
+
+	float average = aoSample.x * w;
+
+	return float2(average, w);
+}
+
+
+/** Filter taking into account depth, normal discontinuities 
+   and ssao convergence of a sample (the higher the more reliably
+   has the sample a correct ssao value)
+*/
+float FilterXY(float2 texCoord,
+			   uniform sampler2D ssaoTex,
+			   uniform sampler2D colorsTex,
+			   float3 bl,
+			   float3 br,
+			   float3 tl,
+			   float3 tr,
+			   float2 xyStep)
+{
+	float2 result = float2(0.0f, 0.0f);
+	
+	const float3 centerPos = ReconstructSamplePos(colorsTex, texCoord, bl, br, tl, tr);
+
+	for (int i = -SSAO_FILTER_RADIUS; i < SSAO_FILTER_RADIUS; ++ i)
+	{
+		float4 sampleTexCoord = float4(texCoord + i * xyStep, .0f, .0f);
+		result += FilterSample(sampleTexCoord, ssaoTex, colorsTex, centerPos, bl, br, tl, tr);
+	}
+
+	result.x /= max(result.y, 1e-6f);
+
+	return saturate(result.x);
+}
+
+/** In between step that only filters in one direction
+*/
+pixel FilterSsaoFullRes(fragment IN, 
+						uniform sampler2D colorsTex,
+						uniform sampler2D ssaoTex,
+						uniform float3 bl,
+						uniform float3 br,
+						uniform float3 tl,
+						uniform float3 tr,
+						uniform float2 xyStep
+						 )
+{
+	pixel OUT;
+
+	const float depth = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0)).w;
+
+	OUT.illum_col = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0)).y;
+	// just take unfiltered convergence in current pixel
+	const float convergence = OUT.illum_col.y;
+
+	// filter reaches size 1 pixel when sample size reaches threshold 
+	// afterwards we do not use the filter anymore
+
+	// filter up to a certain convergance value and leave out background (sky) by checking depth
+	if (1)//(convergence < SSAO_CONVERGENCE_THRESHOLD) && (depth < 1e10f))
+	{
+		// the filtered ssao value
+		OUT.illum_col.x = FilterXY(IN.texCoord, ssaoTex, colorsTex, bl, br, tl, tr, xyStep);
+	}
+
+	return OUT;
+}
+
+
+/** Function combining image and indirect illumination buffer using a 
+    depth and convergence aware discontinuity filter.
+*/
+pixel CombineSsaoFullRes(fragment IN, 
+						 uniform sampler2D colorsTex,
+						 uniform sampler2D ssaoTex,
+						 uniform float3 bl,
+						 uniform float3 br,
+						 uniform float3 tl,
+						 uniform float3 tr,
+						 uniform float2 xyStep
+						 )
+{
+	pixel OUT;
+
+	float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
+	float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
+
+	const float depth = col.w;
+	// just take unfiltered convergence in current pixel
+	const float convergence = ao.y;
+
+	// filter reaches size 1 pixel when sample size reaches threshold 
+	// afterwards we do not use the filter anymore
+
+	// filter up to a certain convergance value and leave out background (sky) by checking depth
+	if ((convergence < SSAO_CONVERGENCE_THRESHOLD) && (depth < 1e10f))
+	{
+		// the filtered ssao value
+		ao.x = FilterXY(IN.texCoord, ssaoTex, colorsTex, bl, br, tl, tr, xyStep);
+	}
+
+	// just apply ssao if we are not in the sky
+	if (depth < 1e10f)
+	{
+		OUT.illum_col.xyz = col.xyz * max(2e-2f, 1.0f - ao.x);
+		//OUT.illum_col.xyz = col.xyz * ao.x;
+	}
+	else
+	{
+		OUT.illum_col.xyz = col.xyz;
+	}
+
+	OUT.illum_col.w = col.w;
+
+	return OUT;
+}
