浏览代码

Some fixes in the reflection shader

Panagiotis Christopoulos Charitos 7 年之前
父节点
当前提交
2eddb3a8a5
共有 2 个文件被更改,包括 11 次插入19 次删除
  1. 9 17
      programs/Reflections.ankiprog
  2. 2 2
      src/anki/renderer/Reflections.cpp

+ 9 - 17
programs/Reflections.ankiprog

@@ -29,7 +29,7 @@ layout(ANKI_IMAGE_BINDING(0, 0)) writeonly uniform image2D u_out;
 
 layout(ANKI_UBO_BINDING(0, 0), std140, row_major) uniform u0_
 {
-	mat4 u_viewProjMat;
+	mat4 u_projMat;
 	mat4 u_invViewProjMat;
 	mat4 u_invProjMat;
 	mat4 u_viewMat;
@@ -46,27 +46,19 @@ vec4 doSslr(vec3 r, vec3 worldPos, vec2 uv)
 	vec3 p0 = (u_viewMat * vec4(worldPos, 1.0)).xyz;
 	r = u_normalMat * r;
 
-	// Compute an end point p1 that is p1 = p0 + t*r. p1 will lie in the near plane.
-	// p1 = p0 + t*r or
-	// p1.x = p0.x + t*r.x (1)
-	// p1.y = p0.y + t*r.y (2) and
-	// p1.z = p0.z + t*r.z (3)
-	// p1.z is known to be -near so we can solve (3) for t
-	// NOTE: u_near is a bit bigger that the real near so that p1 will fall a bit in front of the near plane
-	vec3 p1;
-	{
-		float t = -(u_near + p0.z) / (r.z + EPSILON);
-		p1 = p0 + r * t;	
-	}
+	// Compute an end point p1. This point is supposed to fall in front of the near plane. u_near is a bit bigger than
+	// the actual near.
+	vec3 p1 = p0 + r * (-p0.z - u_near);
 
 	// Project the starting and end points
 	vec2 start = uv;
-	vec4 end4 = u_viewProjMat * vec4(p1, 1.0);
+	vec4 end4 = u_projMat * vec4(p1, 1.0);
 	vec2 end = NDC_TO_UV(end4.xy / end4.w);
 
 	// Compute the step size
 	vec2 dir = end - start;
-	float stepSize = length(dir) / max(dir.x, dir.y);
+	vec2 texelDims = abs(dir * FB_SIZE); // TODO maybe it should be FB_SIZE/2
+	float stepSize = length(dir) / max(texelDims.x, texelDims.y);
 	dir = normalize(dir);
 
 	// Iterate
@@ -102,11 +94,11 @@ vec4 doSslr(vec3 r, vec3 worldPos, vec2 uv)
 
 		// Read depth and get view space Z
 		float depth = textureLod(u_depthRt, newUv, 0.0).r;
-		vec4 newViewPos4 = u_invViewProjMat * vec4(ndc, depth, 1.0);
+		vec4 newViewPos4 = u_invProjMat * vec4(ndc, depth, 1.0);
 		float newViewPosZ = newViewPos4.z / newViewPos4.w;
 
 		// Compare depths
-		float zDiff = intersectionZ - newViewPosZ;
+		float zDiff = newViewPosZ - intersectionZ;
 
 		if(zDiff > 0.1)
 		{

+ 2 - 2
src/anki/renderer/Reflections.cpp

@@ -87,7 +87,7 @@ void Reflections::run(RenderPassWorkContext& rgraphCtx)
 
 	struct Unis
 	{
-		Mat4 m_viewProjMat;
+		Mat4 m_projMat;
 		Mat4 m_invViewProjMat;
 		Mat4 m_invProjMat;
 		Mat4 m_viewMat;
@@ -95,7 +95,7 @@ void Reflections::run(RenderPassWorkContext& rgraphCtx)
 	};
 
 	Unis* unis = allocateAndBindUniforms<Unis*>(sizeof(Unis), cmdb, 0, 0);
-	unis->m_viewProjMat = m_runCtx.m_ctx->m_viewProjMatJitter;
+	unis->m_projMat = m_runCtx.m_ctx->m_projMatJitter;
 	unis->m_invViewProjMat = m_runCtx.m_ctx->m_viewProjMatJitter.getInverse();
 	unis->m_invProjMat = m_runCtx.m_ctx->m_projMatJitter.getInverse();
 	unis->m_viewMat = m_runCtx.m_ctx->m_renderQueue->m_viewMatrix;