Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2970)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2973)
@@ -21,36 +21,7 @@
 
 
-float3 ToneMap(float imageKey, float whiteLum, float middleGrey, float3 color)
-{
-	float3 outCol;
-	const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
-	
-	// adjust to middle gray
-	const float lum = middleGrey * pixLum / imageKey;
-	 
-	// map to range and calc burnout
-	const float burnedLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum);
-	
-	outCol = color * burnedLum / pixLum;
-
-	/*
-	outCol = color;
-
-	outCol.rgb *= MIDDLE_GRAY /(MIDDLE_GRAY + 0.001f);
-    outCol.rgb *= (1.0f + color.rgb / whiteLum);
-	outCol.rgb /= (1.0f + color.rgb);
-*/
-	//vColor.rgb += 0.6f * vBloom;
-	
-	return outCol;
-}
-
-
 float4 main(v2p IN, 
 			uniform sampler2D colors,
-			uniform sampler2D normals,
-			uniform float imageKey,
-			uniform float whiteLum,
-			uniform float middleGrey
+			uniform sampler2D normals
 			): COLOR
 {
@@ -121,7 +92,4 @@
 	//return (s0 + s1 + s2 + s3) * 0.25f;
 
-	//return float4(w);
-	col.xyz = ToneMap(imageKey, whiteLum, middleGrey, col.xyz);
-
 	return col;
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 2972)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 2973)
@@ -7,24 +7,16 @@
 	float4 pos: WPOS;
 	float4 texCoord: TEXCOORD0; 
+
+	float2 lt: TEXCOORD1; // left top
+	float2 rb: TEXCOORD2; // right bottom
+	float2 rt: TEXCOORD3; // right top
+	float2 lb: TEXCOORD4; // left bottom
 };
 
 
-
-float3 ToneMap(float imageKey, float whiteLum, float middleGrey, float3 color)
+struct pixel
 {
-	float3 outCol;
-	const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
-	
-	// adjust to middle gray
-	const float lum = middleGrey * pixLum / imageKey;
-	 
-	// map to range and calc burnout
-	const float burnedLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum);
-	
-	outCol = color * burnedLum / pixLum;
-
-	return outCol;
-}
-
+	float4 col: COLOR0;
+};
 
 
@@ -37,5 +29,5 @@
     for(int i = 0; i < 16; ++ i)
     {
-        average += tex2D(colors, IN.texCoord + float2(downSampleOffs[i].x, downSampleOffs[i].y) );
+        average += tex2D(colors, IN.texCoord.xy + downSampleOffs[i]);
     }
         
@@ -47,6 +39,5 @@
 
 float4 GreyScaleDownSample(fragment IN,
-						   uniform sampler2D colors,
-						   float2 offs[16]): COLOR
+						   uniform sampler2D colors): COLOR
 {
 
@@ -55,26 +46,53 @@
 	float maximum = -1e20;
 
-	float4 color = 0.0f;
-	
 	// the rgb weights
 	const float3 w = float3(0.299f, 0.587f, 0.114f);
 	//float3 w = float3(0.2125f, 0.7154f, 0.0721f);
 
+	float4 cols[4];
+
+	cols[0] = tex2D(colors, IN.texCoord.xy + IN.lt.xy);
+	cols[1] = tex2D(colors, IN.texCoord.xy + IN.lb.xy);
+	cols[2] = tex2D(colors, IN.texCoord.xy + IN.rt.xy);
+	cols[3] = tex2D(colors, IN.texCoord.xy + IN.rb.xy);
+
+
 	for (int i = 0; i < 4; ++ i)
 	{
-		color = tex2D(colors, IN.texcoord + float2(offs[i].x, offs[i].y));
+		const float intensity = dot(cols[i].rgb, w);
 
-		const float intensity = dot(color.rgb, W);
-
-		maximum = max(maximum, intentsity);
-		average += (0.25f * log(1e-5f + GreyValue));
+		maximum = max(maximum, intensity);
+		average += (0.25f * log(1e-5f + intensity));
 	}
 
 	average = exp(average);
 
+	return tex2D(colors, IN.texCoord.xy);
+
 	// Output the luminance to the render target
 	return float4(average, maximum, 0.0f, 1.0f);
 }
+    
 
-    
-    
+pixel ToneMap(fragment IN,
+			  uniform sampler2D colors,
+			  uniform float imageKey, 
+			  uniform float whiteLum, 
+			  uniform float middleGrey)
+{
+	pixel OUT;
+	float4 color = tex2D(colors, IN.texCoord.xy);
+
+	const float pixLum = 0.2125f * color.x + 0.7154f * color.y + 0.0721f * color.z;
+	
+	// adjust to middle gray
+	const float lum = middleGrey * pixLum / imageKey;
+	 
+	// map to range and calc burnout
+	const float scaleLum = lum * (1.0f + lum / whiteLum * whiteLum) / (1.0f + lum);
+
+	OUT.col = color * scaleLum / pixLum;
+	OUT.col.w = color.w;
+
+	return OUT;
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 2972)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 2973)
@@ -75,6 +75,7 @@
 	void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap);
 
-	void ComputeToneParameters(FrameBufferObject *fbo);
-	void ToneMap(FrameBufferObject *fbo, DirectionalLight *light);
+	void ComputeToneParameters(FrameBufferObject *fbo, DirectionalLight *light, float &imageKey, float &whiteLum, float &middleGrey);
+
+	void ToneMap(FrameBufferObject *fbo, DirectionalLight *light, float imageKey, float whiteLum, float middleGrey);
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2972)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2973)
@@ -110,4 +110,5 @@
 static CGparameter sColorsTexToneParam;
 static CGparameter sIntensityTexDownSampleParam;
+
 
 
@@ -419,8 +420,4 @@
 		cgGLLoadProgram(sCgAntiAliasingProgram);
 
-		sImageKeyParam = cgGetNamedParameter(sCgAntiAliasingProgram, "imageKey");
-		sMiddleGreyParam = cgGetNamedParameter(sCgAntiAliasingProgram, "middleGrey");
-		sWhiteLumParam = cgGetNamedParameter(sCgAntiAliasingProgram, "whiteLum");
-
 		sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");  
 		sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
@@ -465,4 +462,45 @@
 		cerr << "deferred program failed to load" << endl;
 
+	sCgToneProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/tonemap.cg", 
+								RenderState::sCgFragmentProfile,
+								"ToneMap",
+								NULL);
+
+	if (sCgToneProgram != NULL)
+	{
+		cgGLLoadProgram(sCgToneProgram);
+
+		sImageKeyParam = cgGetNamedParameter(sCgToneProgram, "imageKey");
+		sMiddleGreyParam = cgGetNamedParameter(sCgToneProgram, "middleGrey");
+		sWhiteLumParam = cgGetNamedParameter(sCgToneProgram, "whiteLum");
+
+		sColorsTexToneParam = cgGetNamedParameter(sCgToneProgram, "colors");  
+	}
+	else
+		cerr << "tone program failed to load" << endl;
+
+	sCgInitialIntensityProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/tonemap.cg", 
+								RenderState::sCgFragmentProfile,
+								"GreyScaleDownSample",
+								NULL);
+
+	if (sCgInitialIntensityProgram != NULL)
+	{
+		cgGLLoadProgram(sCgInitialIntensityProgram);
+
+		// we need size of texture for scaling
+		sColorsTexInitialParam = cgGetNamedParameter(sCgInitialIntensityProgram, "colors");  
+	}
+	else
+		cerr << "intensity program failed to load" << endl;
+
+
+
 	PrintGLerror("init");
 }
@@ -499,4 +537,6 @@
 	glLoadIdentity();
 
+	colorBufferIdx = 0;
+
 	const float offs = 0.5f;
 	glOrtho(-offs, offs, -offs, offs, 0, 1);
@@ -510,4 +550,11 @@
 	else
 		FirstPass(fbo, light);
+
+
+	float imageKey, whiteLum, middleGrey;
+
+	ComputeToneParameters(fbo, light, imageKey, whiteLum, middleGrey);
+
+	ToneMap(fbo, light, imageKey, whiteLum, middleGrey);
 
 	switch (mShadingMethod)
@@ -575,5 +622,4 @@
 
 	// read the second buffer, write to the first buffer
-	//mNewFbo->Bind();
 	mFbo->Bind();
 	glDrawBuffers(1, mrt + mFboIndex);
@@ -685,26 +731,4 @@
 	ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
 
-	// we assume that we have a floating point rgba texture
-	float *pixels = (float *)colorBuffer->ReadTexture();
-
-	const int w = colorBuffer->GetHeight();
-	const int h = colorBuffer->GetWidth();
-
-	const float imageKey = ToneMapper().CalcImageKey(pixels, w, h);
-	const float whiteLum = 1.0f * ToneMapper().CalcMaxLuminance(pixels, w, h);
-
-	//const float minKey = 0.18f;
-	//const float maxKey = 0.72f;
-
-	//const float minKey = 0.0045f;
-	const float minKey = 0.09f;
-	const float maxKey = 0.5f;
-
-	const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z());
-	const float middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey;
-
-	//cout << "middlegrey: " << middleGrey << endl;
-	delete [] pixels;
-
 	GLuint colorsTex = colorBuffer->GetTexture();
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
@@ -720,8 +744,4 @@
 	cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
 	cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
-
-	cgGLSetParameter1f(sImageKeyParam, imageKey);
-	cgGLSetParameter1f(sWhiteLumParam, whiteLum);
-	cgGLSetParameter1f(sMiddleGreyParam, middleGrey);
 
 
@@ -752,5 +772,5 @@
 void DeferredRenderer::FirstPass(FrameBufferObject *fbo, DirectionalLight *light)
 {
-	GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
+	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
 	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
@@ -758,5 +778,5 @@
 	fbo->Bind();
 
-	colorBufferIdx = 3;
+	colorBufferIdx = 3 - colorBufferIdx;
 	glDrawBuffers(1, mrt + colorBufferIdx);
 
@@ -912,5 +932,5 @@
 void DeferredRenderer::CombineIllum(FrameBufferObject *fbo)
 {
-	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
+	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
 
 	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
@@ -921,5 +941,6 @@
 
 	// overwrite old color texture
-	colorBufferIdx = 0;
+	colorBufferIdx = 3 - colorBufferIdx;
+
 	glDrawBuffers(1, mrt + colorBufferIdx);
 
@@ -967,11 +988,11 @@
 void DeferredRenderer::CombineSsao(FrameBufferObject *fbo)
 {
-	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
+	fbo->Bind();
+
+	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
 	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
 
-	fbo->Bind();
-
 	// overwrite old color texture
-	colorBufferIdx = 0;
+	colorBufferIdx = 3 - colorBufferIdx;
 	glDrawBuffers(1, mrt + colorBufferIdx);
 
@@ -1017,5 +1038,7 @@
 									   ShadowMap *shadowMap)
 {
-	GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
+	fbo->Bind();
+
+	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
 
 	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
@@ -1026,7 +1049,5 @@
 	shadowMap->GetTextureMatrix(shadowMatrix);
 
-	fbo->Bind();
-
-	colorBufferIdx = 3;
+	colorBufferIdx = 3 - colorBufferIdx;
 	glDrawBuffers(1, mrt + colorBufferIdx);
 
@@ -1049,7 +1070,8 @@
 
 	cgGLSetParameter1f(sMaxDepthShadowParam, mScaleFactor);
+
 	//cgGLSetParameter1f(sSampleWidthParam, 10.0f / shadowMap->GetSize());
 	cgGLSetParameter1f(sSampleWidthParam, 2.0f / shadowMap->GetSize());
-	//cgGLSetParameter1f(sSampleWidthParam, 2.0f / 2048);
+	
 	 
 	cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
@@ -1108,10 +1130,44 @@
 
 
-void DeferredRenderer::ComputeToneParameters(FrameBufferObject *fbo)
+void DeferredRenderer::ComputeToneParameters(FrameBufferObject *fbo, 
+											 DirectionalLight *light,
+											 float &imageKey, 
+											 float &whiteLum,
+											 float &middleGrey)
 {
 	ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
 
+	// we assume that we have a floating point rgba texture
+	float *pixels = (float *)colorBuffer->ReadTexture();
+	
+	const int w = colorBuffer->GetHeight();
+	const int h = colorBuffer->GetWidth();
+
+	imageKey = ToneMapper().CalcImageKey(pixels, w, h);
+	whiteLum = 1.0f * ToneMapper().CalcMaxLuminance(pixels, w, h);
+
+	//const float minKey = 0.18f;
+	//const float maxKey = 0.72f;
+
+	//const float minKey = 0.0045f;
+	const float minKey = 0.09f;
+	const float maxKey = 0.5f;
+
+	const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z());
+	middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey;
+
+	//cout << "middlegrey: " << middleGrey << endl;
+	delete [] pixels;
+
+
+	/////////////
+
 	GLuint colorsTex = colorBuffer->GetTexture();
-	
+
+	fbo->Bind();
+
+	colorBufferIdx = 3 - colorBufferIdx;
+	glDrawBuffers(1, mrt + colorBufferIdx);
+
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
@@ -1142,4 +1198,6 @@
 	cgGLDisableTextureParameter(sColorsTexToneParam);
 
+	FrameBufferObject::Release();
+
 	
 	////////////
@@ -1151,5 +1209,5 @@
 	for (int i = 0; i < 32; ++ i)
 	{
-		DownSample(fbo);
+		//DownSample(fbo);
 	}
 
@@ -1160,5 +1218,5 @@
 void DeferredRenderer::DownSample(FrameBufferObject *fbo)
 {
-	GLuint intensityTex;
+	GLuint intensityTex=0;
 	
 	cgGLEnableProfile(RenderState::sCgFragmentProfile);
@@ -1170,29 +1228,17 @@
 
 
-void DeferredRenderer::ToneMap(FrameBufferObject *fbo, DirectionalLight *light)
+void DeferredRenderer::ToneMap(FrameBufferObject *fbo,
+							   DirectionalLight *light,
+							   float imageKey, 
+							   float whiteLum,
+							   float middleGrey)
 {
 	ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
 
-	// we assume that we have a floating point rgba texture
-	float *pixels = (float *)colorBuffer->ReadTexture();
-
-	const int w = colorBuffer->GetHeight();
-	const int h = colorBuffer->GetWidth();
-
-	const float imageKey = ToneMapper().CalcImageKey(pixels, w, h);
-	const float whiteLum = 1.0f * ToneMapper().CalcMaxLuminance(pixels, w, h);
-
-	//const float minKey = 0.18f;
-	//const float maxKey = 0.72f;
-
-	//const float minKey = 0.0045f;
-	const float minKey = 0.09f;
-	const float maxKey = 0.5f;
-
-	const float lightIntensity = DotProd(-light->GetDirection(), Vector3::UNIT_Z());
-	const float middleGrey = lightIntensity * maxKey + (1.0f - lightIntensity) * minKey;
-
-	//cout << "middlegrey: " << middleGrey << endl;
-	delete [] pixels;
+	fbo->Bind();
+
+	colorBufferIdx = 3 - colorBufferIdx;
+	glDrawBuffers(1, mrt + colorBufferIdx);
+
 
 	GLuint colorsTex = colorBuffer->GetTexture();
@@ -1228,6 +1274,7 @@
 
 	cgGLDisableTextureParameter(sColorsTexToneParam);
-
-	PrintGLerror("antialiasing");
+	FrameBufferObject::Release();
+
+	PrintGLerror("ToneMap");
 }
 
