Bläddra i källkod

Optimizing the bluring in renderer

Panagiotis Christopoulos Charitos 16 år sedan
förälder
incheckning
7274631afe

+ 2 - 1
shaders/Pps.glsl

@@ -51,7 +51,8 @@ EdgeAA
 
 vec3 EdgeAA()
 {
-	const vec2 pixelsize = vec2( 1.0/(R_W*R_Q), 1.0/(R_H*R_Q) );
+	ivec2 texSize = textureSize(msNormalFai,0);
+	vec2 pixelsize = vec2( 1.0/(float(texSize.x)), 1.0/(float(texSize.y)) );
 	const vec2 kernel[8] = vec2[]( vec2(-1.0,1.0), vec2(1.0,-1.0), vec2(-1.0,-1.0), vec2(1.0,1.0), vec2(-1.0,0.0), vec2(1.0,0.0), vec2(0.0,-1.0), vec2(0.0,1.0) );
 	const float weight = 1.0;
 

+ 6 - 6
shaders/PpsHdr.glsl

@@ -25,12 +25,12 @@ void main()
 	return;*/
 
 	#if defined( _PPS_HDR_PASS_0_ ) || defined( _PPS_HDR_PASS_1_ )
-		const float super_offset = 2.5;
+		const float super_offset = 1.5;
 
-		#if defined( _PPS_HDR_PASS0_ )
-			float offset = 1.0 / float(textureSize(fai,0).x) * super_offset;
+		#if defined( _PPS_HDR_PASS_0_ )
+			float offset = 1.0 / IS_FAI_WIDTH * super_offset;
 		#else
-			float offset = 1.0 / float(textureSize(fai,0).y) * super_offset;
+			float offset = 1.0 / PASS0_HEIGHT * super_offset;
 		#endif
 
 		const int KERNEL_SIZE = 9;
@@ -51,8 +51,8 @@ void main()
 		gl_FragData[0].rgb = color / KERNEL_SIZE;
 
 	#else // _PPS_HDR_PASS_2_
-		vec3 color = MedianFilterRGB( fai, texCoords );
-		//vec3 color = texture2D( fai, texCoords ).rgb;
+		//vec3 color = MedianFilterRGB( fai, texCoords );
+		vec3 color = texture2D( fai, texCoords ).rgb;
 
 		float Y = dot(vec3(0.30, 0.59, 0.11), color);
 		const float exposure = 4.0;

+ 3 - 3
shaders/PpsSsao.glsl

@@ -18,16 +18,16 @@ const float strength = 0.07;
 const float offset = 18.0;
 const float falloff = 0.000002;
 const float rad = 0.006;
-const int SAMPLES = 16; // 10 is good
+const int SAMPLES = 10; // 10 is good
 const float invSamples = 1.0/float(SAMPLES);
 
 void main(void)
 {
 	// these are the random vectors inside a unit sphere
-	vec3 pSphere[16] = vec3[](vec3(0.53812504, 0.18565957, -0.43192),vec3(0.13790712, 0.24864247, 0.44301823),vec3(0.33715037, 0.56794053, -0.005789503),vec3(-0.6999805, -0.04511441, -0.0019965635),vec3(0.06896307, -0.15983082, -0.85477847),vec3(0.056099437, 0.006954967, -0.1843352),vec3(-0.014653638, 0.14027752, 0.0762037),vec3(0.010019933, -0.1924225, -0.034443386),vec3(-0.35775623, -0.5301969, -0.43581226),vec3(-0.3169221, 0.106360726, 0.015860917),vec3(0.010350345, -0.58698344, 0.0046293875),vec3(-0.08972908, -0.49408212, 0.3287904),vec3(0.7119986, -0.0154690035, -0.09183723),vec3(-0.053382345, 0.059675813, -0.5411899),vec3(0.035267662, -0.063188605, 0.54602677),vec3(-0.47761092, 0.2847911, -0.0271716));
+	//vec3 pSphere[16] = vec3[](vec3(0.53812504, 0.18565957, -0.43192),vec3(0.13790712, 0.24864247, 0.44301823),vec3(0.33715037, 0.56794053, -0.005789503),vec3(-0.6999805, -0.04511441, -0.0019965635),vec3(0.06896307, -0.15983082, -0.85477847),vec3(0.056099437, 0.006954967, -0.1843352),vec3(-0.014653638, 0.14027752, 0.0762037),vec3(0.010019933, -0.1924225, -0.034443386),vec3(-0.35775623, -0.5301969, -0.43581226),vec3(-0.3169221, 0.106360726, 0.015860917),vec3(0.010350345, -0.58698344, 0.0046293875),vec3(-0.08972908, -0.49408212, 0.3287904),vec3(0.7119986, -0.0154690035, -0.09183723),vec3(-0.053382345, 0.059675813, -0.5411899),vec3(0.035267662, -0.063188605, 0.54602677),vec3(-0.47761092, 0.2847911, -0.0271716));
 	//const vec3 pSphere[8] = vec3[](vec3(0.24710192, 0.6445882, 0.033550154),vec3(0.00991752, -0.21947019, 0.7196721),vec3(0.25109035, -0.1787317, -0.011580509),vec3(-0.08781511, 0.44514698, 0.56647956),vec3(-0.011737816, -0.0643377, 0.16030222),vec3(0.035941467, 0.04990871, -0.46533614),vec3(-0.058801126, 0.7347013, -0.25399926),vec3(-0.24799341, -0.022052078, -0.13399573));
 	//const vec3 pSphere[12] = vec3[](vec3(-0.13657719, 0.30651027, 0.16118456),vec3(-0.14714938, 0.33245975, -0.113095455),vec3(0.030659059, 0.27887347, -0.7332209),vec3(0.009913514, -0.89884496, 0.07381549),vec3(0.040318526, 0.40091, 0.6847858),vec3(0.22311053, -0.3039437, -0.19340435),vec3(0.36235332, 0.21894878, -0.05407306),vec3(-0.15198798, -0.38409665, -0.46785462),vec3(-0.013492276, -0.5345803, 0.11307949),vec3(-0.4972847, 0.037064247, -0.4381323),vec3(-0.024175806, -0.008928787, 0.17719103),vec3(0.694014, -0.122672155, 0.33098832));
-	//const vec3 pSphere[10] = vec3[](vec3(-0.010735935, 0.01647018, 0.0062425877),vec3(-0.06533369, 0.3647007, -0.13746321),vec3(-0.6539235, -0.016726388, -0.53000957),vec3(0.40958285, 0.0052428036, -0.5591124),vec3(-0.1465366, 0.09899267, 0.15571679),vec3(-0.44122112, -0.5458797, 0.04912532),vec3(0.03755566, -0.10961345, -0.33040273),vec3(0.019100213, 0.29652783, 0.066237666),vec3(0.8765323, 0.011236004, 0.28265962),vec3(0.29264435, -0.40794238, 0.15964167));
+	const vec3 pSphere[10] = vec3[](vec3(-0.010735935, 0.01647018, 0.0062425877),vec3(-0.06533369, 0.3647007, -0.13746321),vec3(-0.6539235, -0.016726388, -0.53000957),vec3(0.40958285, 0.0052428036, -0.5591124),vec3(-0.1465366, 0.09899267, 0.15571679),vec3(-0.44122112, -0.5458797, 0.04912532),vec3(0.03755566, -0.10961345, -0.33040273),vec3(0.019100213, 0.29652783, 0.066237666),vec3(0.8765323, 0.011236004, 0.28265962),vec3(0.29264435, -0.40794238, 0.15964167));
 	// grab a normal for reflecting the sample rays later on
 	vec3 fres = normalize((texture2D(noiseMap,texCoords*offset).xyz*2.0) - vec3(1.0));
 

+ 2 - 2
shaders/PpsSsaoBlur.glsl

@@ -13,9 +13,9 @@ uniform sampler2D tex;
 void main()
 {
 	#if defined( _PPS_SSAO_PASS_0_ )
-		float offset = 1.0 / float(textureSize(tex,0).x);
+		float offset = 1.0 / PASS0_FAI_WIDTH;
 	#else
-		float offset = 1.0 / float(textureSize(tex,0).y);
+		float offset = 1.0 / PASS1_FAI_HEIGHT;
 	#endif
 	const int KERNEL_SIZE = 7;
 	float kernel[KERNEL_SIZE] = float[]( 0.0 * offset, 

+ 9 - 4
src/Main.cpp

@@ -155,6 +155,8 @@ void initPhysics()
 					btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
 					btRigidBody* body = new btRigidBody(rbInfo);
 
+
+
 					//body->setActivationState(ISLAND_SLEEPING);
 
 					dynamicsWorld->addRigidBody(body);
@@ -164,7 +166,10 @@ void initPhysics()
 		}
 	}
 
-	dynamicsWorld->setDebugDrawer(&debugDrawer);
+
+
+
+	//dynamicsWorld->setDebugDrawer(&debugDrawer);
 }
 
 #include "ShaderPrePreprocessor.h"
@@ -338,16 +343,16 @@ int main( int /*argc*/, char* /*argv*/[] )
 		if( I::keys[SDLK_F11] ) app->togleFullScreen();
 		if( I::keys[SDLK_F12] == 1 ) R::takeScreenshot("gfx/screenshot.jpg");
 
-		char str[128];
+		/*char str[128];
 		sprintf( str, "capt/%06d.jpg", R::framesNum );
-		R::takeScreenshot(str);
+		R::takeScreenshot(str);*/
 
 		// std stuff follow
 		SDL_GL_SwapBuffers();
 		R::printLastError();
 		if( 1 )
 		{
-			//if( R::framesNum == 10 ) R::takeScreenshot("gfx/screenshot.tga");
+			if( R::framesNum == 50 ) R::takeScreenshot("gfx/screenshot.tga");
 			app->waitForNextFrame();
 		}
 		else

+ 1 - 0
src/Renderer/Dbg.cpp

@@ -16,6 +16,7 @@ extern btDiscreteDynamicsWorld* dynamicsWorld;
 
 void renderscene( int pass )
 {
+	return;
 	btScalar m[16];
 	btMatrix3x3 rot;
 	rot.setIdentity();

+ 18 - 13
src/Renderer/PpsHdr.cpp

@@ -20,8 +20,8 @@ bool enabled = true;
 
 static Fbo pass0Fbo, pass1Fbo, pass2Fbo; // yet another FBO and another, damn
 
-float renderingQuality = 0.25; // 1/4 of the image
-static uint wwidth, wheight; // render width and height
+float renderingQuality = 0.5; // 1/4 of the image
+static uint w, h; // render width and height
 
 // hdr images
 Texture pass0Fai; // for vertical blur pass
@@ -53,8 +53,8 @@ static void initFbos( Fbo& fbo, Texture& fai, int internalFormat )
 	fbo.setNumOfColorAttachements(1);
 
 	// create the texes
-	fai.createEmpty2D( wwidth, wheight, internalFormat, GL_RGB );
-	fai.texParameter( GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+	fai.createEmpty2D( w, h, internalFormat, GL_RGB );
+	fai.texParameter( GL_TEXTURE_MAG_FILTER, GL_NEAREST );
 	fai.texParameter( GL_TEXTURE_MIN_FILTER, GL_NEAREST );
 
 	// attach
@@ -74,22 +74,27 @@ static void initFbos( Fbo& fbo, Texture& fai, int internalFormat )
 //=====================================================================================================================================
 void init()
 {
-	wwidth = R::Pps::Hdr::renderingQuality * R::w;
-	wheight = R::Pps::Hdr::renderingQuality * R::h;
+	w = R::Pps::Hdr::renderingQuality * R::w;
+	h = R::Pps::Hdr::renderingQuality * R::h;
 
 	initFbos( pass0Fbo, pass0Fai, GL_RGB );
 	initFbos( pass1Fbo, pass1Fai, GL_RGB );
 	initFbos( pass2Fbo, fai, GL_RGB );
 
+	fai.texParameter( GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+
 	// init shaders
-	pass0SProg.customLoad( "shaders/PpsHdr.glsl", "#define _PPS_HDR_PASS_0_\n" );
-	pass0SProg.uniLocs.fai = pass0SProg.uniLocs.fai;
+	if( !pass0SProg.customLoad( "shaders/PpsHdr.glsl", ("#define _PPS_HDR_PASS_0_\n#define IS_FAI_WIDTH " + Util::floatToStr(R::w) + "\n").c_str() ) )
+		FATAL( "See prev error" );
+	pass0SProg.uniLocs.fai = pass0SProg.getUniVar("fai").getLoc();
 
-	pass1SProg.customLoad( "shaders/PpsHdr.glsl", "#define _PPS_HDR_PASS_1_\n" );
-	pass1SProg.uniLocs.fai = pass1SProg.uniLocs.fai;
+	if( !pass1SProg.customLoad( "shaders/PpsHdr.glsl", ("#define _PPS_HDR_PASS_1_\n#define PASS0_HEIGHT " + Util::floatToStr(h) + "\n").c_str() ) )
+		FATAL( "See prev error" );
+	pass1SProg.uniLocs.fai = pass1SProg.getUniVar("fai").getLoc();
 
-	pass2SProg.customLoad( "shaders/PpsHdr.glsl", "#define _PPS_HDR_PASS_2_\n" );
-	pass2SProg.uniLocs.fai = pass2SProg.uniLocs.fai;
+	if( !pass2SProg.customLoad( "shaders/PpsHdr.glsl", "#define _PPS_HDR_PASS_2_\n" ) )
+		FATAL( "See prev error" );
+	pass2SProg.uniLocs.fai = pass2SProg.getUniVar("fai").getLoc();
 }
 
 
@@ -98,7 +103,7 @@ void init()
 //=====================================================================================================================================
 void runPass( const Camera& /*cam*/ )
 {
-	R::setViewport( 0, 0, wwidth, wheight );
+	R::setViewport( 0, 0, w, h );
 
 	glDisable( GL_BLEND );
 	glDisable( GL_DEPTH_TEST );

+ 24 - 17
src/Renderer/PpsSsao.cpp

@@ -14,17 +14,18 @@ namespace Pps {
 namespace Ssao {
 
 
-/*
-=======================================================================================================================================
-VARS                                                                                                                                  =
-=======================================================================================================================================
-*/
+//=====================================================================================================================================
+// VARS                                                                                                                               =
+//=====================================================================================================================================
+bool enabled = true;
+
 static Fbo pass0Fbo, pass1Fbo, pass2Fbo;
 
-float renderingQuality = 1.0; // the renderingQuality of the SSAO fai. Chose low so it can blend
-bool enabled = true;
+float renderingQuality = 0.9; // the renderingQuality of the SSAO fai. Chose low so it can blend
+float bluringQuality = 0.75;
 
-static uint wwidth, wheight; // window width and height
+static uint w, h;
+static uint bw, bh;
 
 Texture pass0Fai, pass1Fai, fai;
 
@@ -46,7 +47,7 @@ static void initBlurFbos()
 	pass1Fbo.setNumOfColorAttachements(1);
 
 	// create the texes
-	pass1Fai.createEmpty2D( wwidth, wheight, GL_ALPHA8, GL_ALPHA );
+	pass1Fai.createEmpty2D( bw, bh, GL_ALPHA8, GL_ALPHA );
 	pass1Fai.texParameter( GL_TEXTURE_MAG_FILTER, GL_NEAREST );
 	pass1Fai.texParameter( GL_TEXTURE_MIN_FILTER, GL_NEAREST );
 
@@ -70,7 +71,7 @@ static void initBlurFbos()
 	pass2Fbo.setNumOfColorAttachements(1);
 
 	// create the texes
-	fai.createEmpty2D( wwidth, wheight, GL_ALPHA8, GL_ALPHA );
+	fai.createEmpty2D( bw, bh, GL_ALPHA8, GL_ALPHA );
 	fai.texParameter( GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 	fai.texParameter( GL_TEXTURE_MIN_FILTER, GL_NEAREST );
 
@@ -93,9 +94,10 @@ init
 */
 void init()
 {
-	if( renderingQuality<0.0 || renderingQuality>1.0 ) ERROR("Incorect R::pps:ssao::rendering_quality");
-	wwidth = R::Pps::Ssao::renderingQuality * R::w;
-	wheight = R::Pps::Ssao::renderingQuality * R::h;
+	w = R::Pps::Ssao::renderingQuality * R::w;
+	h = R::Pps::Ssao::renderingQuality * R::h;
+	bw = w * bluringQuality;
+	bh = h * bluringQuality;
 
 	// create FBO
 	pass0Fbo.create();
@@ -105,7 +107,7 @@ void init()
 	pass0Fbo.setNumOfColorAttachements(1);
 
 	// create the texes
-	pass0Fai.createEmpty2D( wwidth, wheight, GL_ALPHA8, GL_ALPHA );
+	pass0Fai.createEmpty2D( w, h, GL_ALPHA8, GL_ALPHA );
 	pass0Fai.texParameter( GL_TEXTURE_MAG_FILTER, GL_NEAREST );
 	pass0Fai.texParameter( GL_TEXTURE_MIN_FILTER, GL_NEAREST );
 
@@ -138,8 +140,8 @@ void init()
 
 	// blur FBO
 	initBlurFbos();
-	blurSProg.customLoad( "shaders/PpsSsaoBlur.glsl", "#define _PPS_SSAO_PASS_0_\n" );
-	blurSProg2.customLoad( "shaders/PpsSsaoBlur.glsl", "#define _PPS_SSAO_PASS_1_\n" );
+	blurSProg.customLoad( "shaders/PpsSsaoBlur.glsl", ("#define _PPS_SSAO_PASS_0_\n#define PASS0_FAI_WIDTH " + Util::floatToStr(w) + "\n").c_str() );
+	blurSProg2.customLoad( "shaders/PpsSsaoBlur.glsl", ("#define _PPS_SSAO_PASS_1_\n#define PASS1_FAI_HEIGHT " + Util::floatToStr(bh) + "\n").c_str() );
 }
 
 
@@ -152,7 +154,7 @@ void runPass( const Camera& cam )
 {
 	pass0Fbo.bind();
 
-	R::setViewport( 0, 0, wwidth, wheight );
+	R::setViewport( 0, 0, w, h );
 
 	glDisable( GL_BLEND );
 	glDisable( GL_DEPTH_TEST );
@@ -165,6 +167,11 @@ void runPass( const Camera& cam )
 	ssaoSProg.locTexUnit( ssaoSProg.getUniVar("msNormalFai").getLoc(), R::Ms::normalFai, 2 );
 	R::DrawQuad( 0 ); // Draw quad
 
+	/*glBindFramebuffer( GL_READ_FRAMEBUFFER, pass0Fbo.getGlId() );
+	glBindFramebuffer( GL_DRAW_FRAMEBUFFER, pass1Fbo.getGlId() );
+	glBlitFramebuffer( 0, 0, w, h, 0, 0, bw, bh, GL_COLOR_BUFFER_BIT, GL_NEAREST);*/
+
+	R::setViewport( 0, 0, bw, bh );
 
 	// second pass. blur
 	pass1Fbo.bind();

+ 3 - 0
src/Util/App.cpp

@@ -12,6 +12,9 @@ App::App()
 
 	windowW = 1280;
 	windowH = 720;
+	/*windowW = 1440;
+	windowH = 900;*/
+
 
 	timerTick = 1000/40; // in ms. 1000/Hz
 	uint time = 0;