Browse Source

Optimizing a bit

Panagiotis Christopoulos Charitos 12 years ago
parent
commit
d2dd4b6217

+ 22 - 19
shaders/IsLp.glsl

@@ -153,14 +153,15 @@ float calcLambertTerm(in vec3 normal, in vec3 frag2LightVec)
 //==============================================================================
 /// Performs phong lighting using the MS FAIs and a few other things
 vec3 calcPhong(in vec3 fragPosVspace, in vec3 diffuse, 
-	in vec2 specularAll, in vec3 normal, in Light light, in vec3 rayDir)
+	in float specColor, in float specPower, in vec3 normal, in Light light, 
+	in vec3 rayDir)
 {
 	// Specular
 	vec3 eyeVec = normalize(fragPosVspace);
 	vec3 h = normalize(rayDir - eyeVec);
-	float specIntensity = pow(max(0.0, dot(normal, h)), specularAll.g);
+	float specIntensity = pow(max(0.0, dot(normal, h)), specPower);
 	vec3 specCol = 
-		light.specularColorTexId.rgb * (specIntensity * specularAll.r);
+		light.specularColorTexId.rgb * (specIntensity * specColor);
 	
 	// Do a mad optimization
 	// diffCol = diffuse * light.diffuseColorShadowmapId.rgb
@@ -223,20 +224,21 @@ void main()
 	// get frag pos in view space
 	vec3 fragPosVspace = getFragPosVSpace();
 
-	// Decode MS
-#if USE_MRT
-	vec3 normal = readAndUnpackNormal(msFai1, vTexCoords);
-	vec4 diffuseAndSpec = texture(msFai0, vTexCoords);
-#else
-	uvec2 msAll = texture(msFai0, vTexCoords).rg;
+	// Decode GBuffer
+	vec3 normal;
+	vec3 diffColor;
+	float specColor;
+	float specPower;
 
-	vec3 normal = unpackNormal(unpackHalf2x16(msAll[1]));
-	vec4 diffuseAndSpec = unpackUnorm4x8(msAll[0]);
+	readGBuffer(
+		msFai0,
+#if USE_MRT
+		msFai1,
 #endif
-	vec2 specularAll = unpackSpecular(diffuseAndSpec.a);
+		vTexCoords, diffColor, normal, specColor, specPower);
 
 	// Ambient color
-	fColor = diffuseAndSpec.rgb * sceneAmbientColor.rgb;
+	fColor = diffColor * sceneAmbientColor.rgb;
 
 	// Point lights
 	uint pointLightsCount = tiles[vInstanceId].lightsCount[0];
@@ -254,8 +256,8 @@ void main()
 
 		float lambert = calcLambertTerm(normal, ray);
 
-		fColor += calcPhong(fragPosVspace, diffuseAndSpec.rgb, 
-			specularAll, normal, light, ray) * (att * lambert);
+		fColor += calcPhong(fragPosVspace, diffColor, 
+			specColor, specPower, normal, light, ray) * (att * lambert);
 	}
 
 	// Spot lights
@@ -278,8 +280,8 @@ void main()
 
 		float spot = calcSpotFactor(light, ray);
 
-		vec3 col = calcPhong(fragPosVspace, diffuseAndSpec.rgb, 
-			specularAll, normal, light.lightBase, ray);
+		vec3 col = calcPhong(fragPosVspace, diffColor, 
+			specColor, specPower, normal, light.lightBase, ray);
 
 		fColor += col * (att * lambert * spot);
 	}
@@ -315,8 +317,9 @@ void main()
 			float shadow = calcShadowFactor(light, 
 				fragPosVspace, shadowMapArr, shadowmapLayerId);
 
-			vec3 col = calcPhong(fragPosVspace, diffuseAndSpec.rgb, 
-				specularAll, normal, light.spotLightBase.lightBase, ray);
+			vec3 col = calcPhong(fragPosVspace, diffColor, 
+				specColor, specPower, normal, light.spotLightBase.lightBase, 
+				ray);
 
 			fColor += col * (midFactor * shadow);
 		}

+ 10 - 27
shaders/MsCommonFrag.glsl

@@ -1,6 +1,9 @@
 #define DEFAULT_FLOAT_PRECISION mediump
 #pragma anki include "shaders/CommonFrag.glsl"
 
+#pragma anki include "shaders/Pack.glsl"
+#pragma anki include "shaders/MsBsCommon.glsl"
+
 //==============================================================================
 // Variables                                                                   =
 //==============================================================================
@@ -41,9 +44,6 @@ layout(location = 0) out uvec2 fMsFai0;
 // Functions                                                                   =
 //==============================================================================
 
-#pragma anki include "shaders/Pack.glsl"
-#pragma anki include "shaders/MsBsCommon.glsl"
-
 /// @param[in] normal The fragment's normal in view space
 /// @param[in] tangent The tangent
 /// @param[in] tangent Extra stuff for the tangent
@@ -150,34 +150,17 @@ vec3 readRgbFromTexture(in sampler2D tex, in highp vec2 texCoords)
 #if defined(PASS_COLOR)
 #	define writeFais_DEFINED
 void writeFais(
-	in vec3 diffCol, // from 0 to 1
+	in vec3 diffColor, // from 0 to 1
 	in vec3 normal, 
-	in float specularComponent, // Streangth and shininess
+	in vec2 specularComponent, // Streangth and shininess
 	in float blurring)
 {
+	writeGBuffer(
+		diffColor, normal, specularComponent.x, specularComponent.y,
+		fMsFai0
 #if USE_MRT
-	// Diffuse color and specular
-	fMsFai0 = vec4(diffCol, specularComponent);
-	// Normal
-	packAndWriteNormal(normal, fMsFai1);
-#else
-	// Diffuse color and specular
-	fMsFai0[0] = packUnorm4x8(vec4(diffCol, specularComponent));
-	// Normal
-	fMsFai0[1] = packHalf2x16(packNormal(normal));
-#endif
-}
+		, fMsFai1
 #endif
-
-/// Write the data to FAIs
-#if defined(PASS_COLOR)
-#	define writeFaisPackSpecular_DEFINED
-void writeFaisPackSpecular(
-	in vec3 diffCol, // Normalized
-	in vec3 normal, 
-	in vec2 specular, // Streangth and shininess
-	in float blurring)
-{
-	writeFais(diffCol, normal, packSpecular(specular), blurring);
+		);
 }
 #endif

+ 0 - 11
shaders/MsCommonVert.glsl

@@ -19,8 +19,6 @@ out mediump vec3 vNormal;
 out mediump vec3 vTangent;
 out mediump float vTangentW;
 out mediump vec3 vVertPosViewSpace; ///< For env mapping. AKA view vector
-/// Calculate it per vertex instead of per fragment
-flat out float vSpecularComponent; 
 #endif
 /// @}
 
@@ -61,12 +59,3 @@ void setVertPosViewSpace(in mat4 modelViewMat)
 }
 #endif
 
-//==============================================================================
-#if defined(PASS_COLOR)
-#define prepackSpecular_DEFINED
-void prepackSpecular(in vec2 specular)
-{
-	vSpecularComponent = float(packSpecular(specular));
-}
-#endif
-

+ 69 - 0
shaders/Pack.glsl

@@ -132,3 +132,72 @@ vec4 unpackUnorm4x8(in uint u)
 	return value * (1.0 / 255.0);
 }
 #endif
+
+// Populate the G buffer
+void writeGBuffer(
+	in vec3 diffColor, in vec3 normal, in float specColor, in float specPower,
+	out vec4 fai0
+#if USE_MRT
+	,out vec4 fai1
+#endif
+	)
+{
+	vec3 unorm = normal * 0.5 + 0.5;
+#if USE_MRT
+	fai0 = vec4(diffColor, specColor);
+	fai1 = vec4(unorm.xyz, specPower / MAX_SPECULARITY);
+#else
+	fai0.x = packUnorm4x8(vec4(diffColor, specColor));
+	fai0.y = packUnorm4x8(vec4(unorm, specPower / MAX_SPECULARITY));
+#endif
+}
+
+// Read from the G buffer
+void readGBuffer(
+#if USE_MRT
+	in sampler2D fai0, in sampler2D fai1,
+#else
+	in highp usampler2D fai0,
+#endif
+	in vec2 texCoord,
+	out vec3 diffColor, out vec3 normal, out float specColor, 
+	out float specPower)
+{
+#if USE_MRT
+	vec4 comp = texture(fai0, texCoord);
+	diffColor = comp.rgb;
+	specColor = comp.a;
+
+	comp = texture(fai1, texCoord);
+	normal = normalize(comp.xyz * 2.0 - 1.0);
+	specPower = comp.w * MAX_SPECULARITY;
+#else
+	uvec2 all_ = texture(fai0, texCoord).rg;
+
+	vec4 v = unpackUnorm4x8(all_[0]);
+	diffColor = v.rgb;
+	specColor = v.a * MAX_SPECULARITY;
+
+	v = unpackUnorm4x8(all_[1]);
+	normal = normalize(v.xyz * 2.0 - 1.0);
+	specPower = v.xyz;
+#endif
+}
+
+// Read only normal from G buffer
+void readNormalFromGBuffer(
+#if USE_MRT
+	in sampler2D fai1,
+#else
+	in highp usampler2D fai0,
+#endif
+	in vec2 texCoord,
+	out vec3 normal)
+{
+#if USE_MRT
+	normal = normalize(readAndUnpackNormal(fai1, texCoord).xyz);
+#else
+	vec4 v = unpackUnorm4x8(texture(fai0, texCoord).g);
+	normal = normalize(v.xyz * 2.0 - 1.0);
+#endif
+}

+ 2 - 6
shaders/PpsSsao.glsl

@@ -49,12 +49,8 @@ uniform sampler2D noiseMap;
 // Get normal
 vec3 getNormal(in vec2 uv)
 {
-#if USE_MRT
-	vec3 normal = readAndUnpackNormal(msGFai, uv);
-#else
-	uvec2 msAll = texture(msGFai, uv).rg;
-	vec3 normal = unpackNormal(unpackHalf2x16(msAll[1]));
-#endif
+	vec3 normal;
+	readNormalFromGBuffer(msGFai, uv, normal);
 	return normal;
 }
 

+ 1 - 1
src/core/Logger.cpp

@@ -29,7 +29,7 @@ void Logger::write(const char* file, int line, const char* func,
 void Logger::writeFormated(const char* file, int line, const char* func,
 	LoggerMessageType type, const char* fmt, ...)
 {
-	char buffer[1024 * 10];
+	char buffer[1024 * 20];
 	va_list args;
 
 	va_start(args, fmt);

+ 0 - 4
tools/2anki/CMakeLists.txt

@@ -1,4 +0,0 @@
-INCLUDE_DIRECTORIES("../../extern/assimp/include")
-	
-ADD_EXECUTABLE(2anki Main.cpp)
-TARGET_LINK_LIBRARIES(2anki ankiassimp) 

+ 1 - 1
tools/CMakeLists.txt

@@ -1 +1 @@
-ADD_SUBDIRECTORY(2anki)
+ADD_SUBDIRECTORY(scene)

+ 4 - 0
tools/scene/CMakeLists.txt

@@ -0,0 +1,4 @@
+INCLUDE_DIRECTORIES("../../extern/assimp/include")
+	
+ADD_EXECUTABLE(ankisceneimp Main.cpp)
+TARGET_LINK_LIBRARIES(ankisceneimp ankiassimp) 

+ 27 - 16
tools/2anki/Main.cpp → tools/scene/Main.cpp

@@ -119,11 +119,11 @@ static aiMatrix4x4 toAnkiMatrix(const aiMatrix4x4& in)
 //==============================================================================
 static void parseConfig(int argc, char** argv)
 {
-	static const char* usage = R"(Usage: 2anki in_file out_dir [options]
+	static const char* usage = R"(Usage: %s in_file out_dir [options]
 Options:
--rpath <string>   : Append a string to the meshes and materials
--texrpath         : Append a string to the textures paths
--flipyz           : Flip y with z (For blender exports)
+-rpath <string>    : Append a string to the meshes and materials
+-texrpath <string> : Append a string to the textures paths
+-flipyz            : Flip y with z (For blender exports)
 )";
 
 	// Parse config
@@ -173,10 +173,20 @@ Options:
 		}
 	}
 
+	if(config.rpath.empty())
+	{
+		config.rpath = config.outDir;
+	}
+
+	if(config.texpath.empty())
+	{
+		config.texpath = config.outDir;
+	}
+
 	return;
 
 error:
-	printf("%s", usage);
+	printf(usage, argv[0]);
 	exit(0);
 }
 
@@ -231,7 +241,7 @@ static void exportMesh(
 	uint32_t vertsCount = mesh.mNumVertices;
 
 	// Open file
-	file.open(config.outDir + name + ".mesh",
+	file.open(config.outDir + name + ".ankimesh",
 		std::ios::out | std::ios::binary);
 
 	// Write magic word
@@ -496,7 +506,7 @@ static void exportMaterial(
 		;
 
 	std::fstream file;
-	file.open(config.outDir + name + ".mtl", std::ios::out);
+	file.open(config.outDir + name + ".ankimtl", std::ios::out);
 
 	// Chose the correct template
 	std::string str;
@@ -596,7 +606,7 @@ static void exportModel(
 	LOGI("Exporting model %s\n", name.c_str());
 
 	std::fstream file;
-	file.open(config.outDir + name + ".mdl", std::ios::out);
+	file.open(config.outDir + name + ".ankimdl", std::ios::out);
 
 	file << xmlHeader << '\n';
 	file << "<model>\n";
@@ -612,7 +622,7 @@ static void exportModel(
 
 		// Write mesh
 		file << "\t\t\t<mesh>" << config.rpath 
-			<< mesh.mName.C_Str() << ".mesh</mesh>\n";
+			<< mesh.mName.C_Str() << ".ankimesh</mesh>\n";
 
 		// Write material
 		const aiMaterial& mtl = *scene.mMaterials[mesh.mMaterialIndex];
@@ -620,7 +630,7 @@ static void exportModel(
 		mtl.Get(AI_MATKEY_NAME, mtlname);
 
 		file << "\t\t\t<material>" << config.rpath 
-			<< mtlname.C_Str() << ".mtl</material>\n";
+			<< mtlname.C_Str() << ".ankimtl</material>\n";
 
 		// end
 		file << "\t\t</modelPatch>\n";
@@ -856,16 +866,16 @@ static void exportScene(const aiScene& scene)
 		{
 			std::ofstream file;
 			file.open(
-				config.outDir + modelName + ".mdl");
+				config.outDir + modelName + ".ankimdl");
 
 			file << xmlHeader << "\n"
 				<< "<model>\n"
 				<< "\t<modelPatches>\n"
 				<< "\t\t<modelPatch>\n"
-				<< "\t\t\t<mesh>" << config.outDir << meshName 
-				<< ".mesh</mesh>\n"
-				<< "\t\t\t<material>" << config.outDir << mtlName 
-				<< ".mtl</material>\n"
+				<< "\t\t\t<mesh>" << config.rpath << meshName 
+				<< ".ankimesh</mesh>\n"
+				<< "\t\t\t<material>" << config.rpath << mtlName 
+				<< ".ankimtl</material>\n"
 				<< "\t\t</modelPatch>\n"
 				<< "\t</modelPatches>\n"
 				<< "</model>\n";
@@ -878,7 +888,8 @@ static void exportScene(const aiScene& scene)
 		// Write the scene file
 		file << "\t<modelNode>\n"
 			<< "\t\t<name>" << nodeName << "</name>\n"
-			<< "\t\t<model>" << config.outDir << modelName << ".mdl</model>\n"
+			<< "\t\t<model>" << config.rpath << modelName 
+			<< ".ankimdl</model>\n"
 			<< "\t\t<instancesCount>" 
 			<< mesh.transforms.size() << "</instancesCount>\n";
 

+ 2 - 9
tools/2anki/diffNormTemplateMtl.h → tools/scene/diffNormTemplateMtl.h

@@ -9,8 +9,8 @@ R"(<?xml version="1.0" encoding="UTF-8" ?>
 		<inputs>
 			<input><type>mat4</type><name>modelViewProjectionMat</name><value></value><instanced>%instanced%</instanced></input>
 			<input><type>mat3</type><name>normalMat</name><value></value><instanced>%instanced%</instanced></input>
-			<input><type>vec2</type><name>specular</name><value>1.0 90.0</value></input>
 
+			<input><type>vec2</type><name>specular</name><value>1.0 90.0</value></input>
 			<input><type>sampler2D</type><name>diffuseMap</name><value>%diffuseMap%</value></input>
 			<input><type>sampler2D</type><name>normalMap</name><value>%normalMap%</value></input>
 		</inputs>
@@ -22,13 +22,6 @@ R"(<?xml version="1.0" encoding="UTF-8" ?>
 			</includes>
 
 			<operations>
-				<operation>
-					<id>0</id>
-					<returnType>void</returnType>
-					<function>prepackSpecular</function>
-					<arguments><argument>specular</argument></arguments>
-				</operation>
-
 				<operation>
 					<id>1</id>
 					<returnType>void</returnType>
@@ -74,7 +67,7 @@ R"(<?xml version="1.0" encoding="UTF-8" ?>
 					<arguments>
 						<argument>operationOut0</argument>
 						<argument>operationOut1</argument>
-						<argument>vSpecularComponent</argument>
+						<argument>specular</argument>
 						<argument>0.0</argument>
 					</arguments>
 				</operation>

+ 2 - 8
tools/2anki/diffTemplateMtl.h → tools/scene/diffTemplateMtl.h

@@ -9,6 +9,7 @@ R"(<?xml version="1.0" encoding="UTF-8" ?>
 		<inputs>
 			<input><type>mat4</type><name>modelViewProjectionMat</name><value></value><instanced>%instanced%</instanced></input>
 			<input><type>mat3</type><name>normalMat</name><value></value><instanced>%instanced%</instanced></input>
+
 			<input><type>vec2</type><name>specular</name><value>1.0 90.0</value></input>
 			<input><type>sampler2D</type><name>diffuseMap</name><value>%diffuseMap%</value></input>
 		</inputs>
@@ -20,13 +21,6 @@ R"(<?xml version="1.0" encoding="UTF-8" ?>
 			</includes>
 
 			<operations>
-				<operation>
-					<id>0</id>
-					<returnType>void</returnType>
-					<function>prepackSpecular</function>
-					<arguments><argument>specular</argument></arguments>
-				</operation>
-
 				<operation>
 					<id>1</id>
 					<returnType>void</returnType>
@@ -68,7 +62,7 @@ R"(<?xml version="1.0" encoding="UTF-8" ?>
 					<arguments>
 						<argument>operationOut0</argument>
 						<argument>operationOut1</argument>
-						<argument>vSpecularComponent</argument>
+						<argument>specular</argument>
 						<argument>0.0</argument>
 					</arguments>
 				</operation>