Browse Source

Minor tweaks in shaders

Panagiotis Christopoulos Charitos 8 years ago
parent
commit
d66a7a65c0
3 changed files with 47 additions and 15 deletions
  1. 6 9
      programs/Is.ankiprog
  2. 14 1
      programs/TemporalAAResolve.ankiprog
  3. 27 5
      programs/VolumetricFog.ankiprog

+ 6 - 9
programs/Is.ankiprog

@@ -198,9 +198,6 @@ void main()
 
 	uint idxOffset = u_clusters[clusterIdx];
 
-	// Shadowpass sample count
-	uint shadowSampleCount = computeShadowSampleCount(SHADOW_SAMPLE_COUNT, fragPos.z);
-
 	// Decals
 	uint count = u_lightIndices[idxOffset++];
 	while(count-- != 0)
@@ -210,13 +207,13 @@ void main()
 		appendDecalColors(decal, fragPos, diffCol, roughness);
 	}
 
+	// Ambient and emissive color
+	vec3 outC = diffCol * emission;
+
 	// Don't allow zero a2 because we may end up with division with zero
 	float a2 = roughness * 0.9 + 0.1;
 	a2 *= a2;
 
-	// Ambient and emissive color
-	vec3 outC = diffCol * emission;
-
 	// Point lights
 	count = u_lightIndices[idxOffset++];
 	while(count-- != 0)
@@ -250,7 +247,7 @@ void main()
 		if(shadowmapLayerIdx >= 0.0)
 		{
 			float shadow = computeShadowFactorSpot(
-				light.texProjectionMat, fragPos, shadowmapLayerIdx, shadowSampleCount, u_spotMapArr);
+				light.texProjectionMat, fragPos, shadowmapLayerIdx, 1, u_spotMapArr);
 			lambert *= shadow;
 		}
 
@@ -264,10 +261,10 @@ void main()
 	vec3 worldNormal = u_invViewRotation * normal;
 	vec3 worldR = reflect(worldEye, worldNormal);
 
-	float reflLod = float(IR_MIPMAP_COUNT) * roughness;
+	float reflLod = float(IR_MIPMAP_COUNT) * a2;
 
 	float ndotv = dot(normal, viewDir);
-	vec2 envBRDF = texture(u_integrationLut, vec2(roughness, ndotv)).xy;
+	vec2 envBRDF = texture(u_integrationLut, vec2(a2, ndotv)).xy;
 	vec3 specIndirectTerm = specCol * envBRDF.x + envBRDF.y;
 
 	vec3 specIndirect, diffIndirect;

+ 14 - 1
programs/TemporalAAResolve.ankiprog

@@ -18,7 +18,9 @@ http://www.anki3d.org/LICENSE
 #include "shaders/Pack.glsl"
 #include "shaders/Tonemapping.glsl"
 
-#define YCBCR 1
+#define VARIANCE_CLIPPING 1
+const float VARIANCE_CLIPPING_GAMMA = 1.75;
+#define YCBCR 0
 const float BLEND_FACTOR = 1.0 / 16.0; // Keep it low to have a better result
 
 layout(location = 0) in vec2 in_uv;
@@ -60,8 +62,19 @@ void main()
 	vec3 near2 = sampleOffset(u_inputRt, in_uv, -1, 0);
 	vec3 near3 = sampleOffset(u_inputRt, in_uv, 0, -1);
 
+#if VARIANCE_CLIPPING
+	vec3 m1 = crntCol + near0 + near1 + near2 + near3;
+	vec3 m2 = crntCol * crntCol + near0 * near0 + near1 * near1 + near2 * near2 + near3 * near3;
+
+	vec3 mu = m1 / 5.0;
+	vec3 sigma = sqrt(m2 / 5.0 - mu * mu);
+
+	vec3 boxMin = mu - VARIANCE_CLIPPING_GAMMA * sigma;
+	vec3 boxMax = mu + VARIANCE_CLIPPING_GAMMA * sigma;
+#else
 	vec3 boxMin = min(crntCol, min(near0, min(near1, min(near2, near3))));
 	vec3 boxMax = max(crntCol, max(near0, max(near1, max(near2, near3))));
+#endif
 
 	historyCol = clamp(historyCol, boxMin, boxMax);
 

+ 27 - 5
programs/VolumetricFog.ankiprog

@@ -58,6 +58,7 @@ layout(location = 0) out vec3 out_color;
 
 const uint MAX_SAMPLES_PER_CLUSTER = 4u;
 const float DIST_BETWEEN_SAMPLES = 0.25;
+const float HISTORY_FEEDBACK = 1.0 / 16.0;
 
 // Return the diffuse color without taking into account the diffuse term of the particles.
 vec3 computeLightColor(vec3 fragPos, uint plightCount, uint plightIdx, uint slightCount, uint slightIdx)
@@ -108,16 +109,30 @@ vec3 computeLightColor(vec3 fragPos, uint plightCount, uint plightIdx, uint slig
 	return outColor;
 }
 
+vec3 readHistory(vec3 ndc, out float historyFeedback)
+{
+	vec4 v4 = u_prevViewProjMatMulInvViewProjMat * vec4(ndc, 1.0);
+	v4.xy /= v4.w;
+
+	vec2 oldUv = NDC_TO_UV(v4.xy);
+	vec3 history = textureLod(u_historyRt, oldUv, 0.0).rgb;
+
+	// Compute the history blend. If clip falls outside NDC then it's 1.0 (use only current fog term) and if it's
+	// inside NDC then use the HISTORY_FEEDBACK value
+	vec2 posNdc = abs(v4.xy);
+	historyFeedback = max(posNdc.x, posNdc.y);
+	historyFeedback = min(floor(historyFeedback), 1.0 - HISTORY_FEEDBACK);
+	historyFeedback += HISTORY_FEEDBACK;
+
+	return history;
+}
+
 void main()
 {
 	float depth = textureLod(u_msDepthRt, in_uv, 0.0).r;
 
 	vec3 ndc = UV_TO_NDC(vec3(in_uv, depth));
 
-	vec4 v4 = u_prevViewProjMatMulInvViewProjMat * vec4(ndc, 1.0);
-	vec2 oldUv = NDC_TO_UV(v4.xy / v4.w);
-	vec3 history = textureLod(u_historyRt, oldUv, 0.0).rgb;
-
 	vec3 farPos;
 	farPos.z = u_unprojectionParams.z / (u_unprojectionParams.w + depth);
 	farPos.xy = ndc.xy * u_unprojectionParams.xy * farPos.z;
@@ -181,8 +196,15 @@ void main()
 
 	newCol *= u_fogParticleColor;
 
+	// Read history
+	float historyFeedback;
+	vec3 history = readHistory(ndc, historyFeedback);
+	
+	// Fix ghosting
 	history = max(history, newCol);
-	out_color = mix(history, newCol, 1.0 / 16.0);
+
+	// Blend
+	out_color = mix(history, newCol, historyFeedback);
 }
 			]]></source>
 		</shader>