Browse Source

Some shadow optimizations

Panagiotis Christopoulos Charitos 10 years ago
parent
commit
10a7165058
4 changed files with 26 additions and 15 deletions
  1. 1 1
      shaders/FsCommonFrag.glsl
  2. 5 1
      shaders/IsLp.frag.glsl
  3. 18 11
      shaders/LightFunctions.glsl
  4. 2 2
      src/resource/ImageLoader.cpp

+ 1 - 1
shaders/FsCommonFrag.glsl

@@ -219,7 +219,7 @@ vec3 computeLightColor(vec3 diffCol)
 		if(shadowmapLayerIdx < 128.0)
 		{
 			shadow = computeShadowFactorSpot(light.texProjectionMat,
-				fragPos, shadowmapLayerIdx);
+				fragPos, shadowmapLayerIdx, 1);
 		}
 #endif
 

+ 5 - 1
shaders/IsLp.frag.glsl

@@ -99,6 +99,10 @@ void main()
 	uint pointLightsCount = (cluster >> 8u) & 0xFFu;
 	uint spotLightsCount = cluster & 0xFFu;
 
+	// Shadowpass sample count
+	uint shadowSampleCount = computeShadowSampleCount(SHADOW_SAMPLE_COUNT,
+		fragPos.z);
+
 	// Point lights
 	for(uint i = 0U; i < pointLightsCount; ++i)
 	{
@@ -137,7 +141,7 @@ void main()
 		if(shadowmapLayerIdx < 128.0)
 		{
 			shadow = computeShadowFactorSpot(light.texProjectionMat,
-				fragPos, shadowmapLayerIdx);
+				fragPos, shadowmapLayerIdx, shadowSampleCount);
 		}
 
 		out_color += (diffC + specC)

+ 18 - 11
shaders/LightFunctions.glsl

@@ -13,6 +13,8 @@
 const float ATTENUATION_BOOST = 0.05;
 const float OMNI_LIGHT_FRUSTUM_NEAR_PLANE = 0.1 / 4.0;
 
+const uint SHADOW_SAMPLE_COUNT = 16;
+
 //==============================================================================
 /// Calculate the cluster split
 uint calcClusterSplit(float zVspace)
@@ -87,16 +89,28 @@ float computeSpotFactor(
 	return spotFactor;
 }
 
+//==============================================================================
+uint computeShadowSampleCount(const uint COUNT, float zVSpace)
+{
+	const float MAX_DISTANCE = 5.0;
+
+	float z = max(zVSpace, -MAX_DISTANCE);
+	float sampleCountf = float(COUNT) + z * (float(COUNT) / MAX_DISTANCE);
+	sampleCountf = max(sampleCountf, 1.0);
+	uint sampleCount = uint(sampleCountf);
+
+	return sampleCount;
+}
+
 //==============================================================================
 float computeShadowFactorSpot(mat4 lightProjectionMat, vec3 fragPos,
-	float layer)
+	float layer, uint sampleCount)
 {
 	vec4 texCoords4 = lightProjectionMat * vec4(fragPos, 1.0);
 	vec3 texCoords3 = texCoords4.xyz / texCoords4.w;
 
 #if POISSON == 1
-	const uint COUNT = 16;
-	const vec2 poissonDisk[COUNT] = vec2[](
+	const vec2 poissonDisk[SHADOW_SAMPLE_COUNT] = vec2[](
 		vec2(0.751688, 0.619709) * 2.0 - 1.0,
 		vec2(0.604741, 0.778485) * 2.0 - 1.0,
 		vec2(0.936216, 0.463094) * 2.0 - 1.0,
@@ -114,13 +128,6 @@ float computeShadowFactorSpot(mat4 lightProjectionMat, vec3 fragPos,
 		vec2(0.409305, 0.177022) * 2.0 - 1.0,
 		vec2(0.158647, 0.239097) * 2.0 - 1.0);
 
-	// Compute number of samples
-	const float MAX_DISTANCE = 5.0;
-	float z = clamp(fragPos.z, -MAX_DISTANCE, -EPSILON);
-	float sampleCountf = float(COUNT) + z * (float(COUNT) / MAX_DISTANCE);
-	sampleCountf = max(sampleCountf, 1.0);
-	sampleCountf = round(sampleCountf);
-	uint sampleCount = uint(sampleCountf);
 
 	float shadowFactor = 0.0;
 
@@ -134,7 +141,7 @@ float computeShadowFactorSpot(mat4 lightProjectionMat, vec3 fragPos,
 		shadowFactor += texture(u_spotMapArr, tcoord);
 	}
 
-	return shadowFactor / sampleCountf;
+	return shadowFactor / float(sampleCount);
 #else
 	vec4 tcoord = vec4(texCoords3.x, texCoords3.y, layer, texCoords3.z);
 	float shadowFactor = texture(u_spotMapArr, tcoord);

+ 2 - 2
src/resource/ImageLoader.cpp

@@ -334,7 +334,7 @@ static ANKI_USE_RESULT Error loadAnkiTexture(
 	if((header.m_compressionFormats & preferredCompression)
 		== ImageLoader::DataCompression::NONE)
 	{
-		ANKI_LOGI("File does not contain the requested compression");
+		ANKI_LOGW("File does not contain the requested compression");
 
 		// Fallback
 		preferredCompression = ImageLoader::DataCompression::RAW;
@@ -342,7 +342,7 @@ static ANKI_USE_RESULT Error loadAnkiTexture(
 		if((header.m_compressionFormats & preferredCompression)
 			== ImageLoader::DataCompression::NONE)
 		{
-			ANKI_LOGI("File does not contain raw compression");
+			ANKI_LOGE("File does not contain raw compression");
 			return ErrorCode::USER_DATA;
 		}
 	}