Browse Source

Add fade in probe visualization

Panagiotis Christopoulos Charitos 1 year ago
parent
commit
e8591bfa71
3 changed files with 86 additions and 57 deletions
  1. 46 55
      AnKi/Renderer/GBuffer.cpp
  2. 24 0
      AnKi/Shaders/Functions.hlsl
  3. 16 2
      AnKi/Shaders/GBufferVisualizeProbe.ankiprog

+ 46 - 55
AnKi/Renderer/GBuffer.cpp

@@ -206,71 +206,62 @@ void GBuffer::populateRenderGraph(RenderingContext& ctx)
 			cmdb.setDepthCompareOperation(CompareOperation::kLessEqual);
 			getRenderer().getRenderableDrawer().drawMdi(args, cmdb);
 
-			// Visualize GI probes
-			if(g_visualizeGiProbes.get())
 			{
-				cmdb.bindShaderProgram(m_visualizeGiProbeGrProg.get());
-				cmdb.bindSrv(0, 0, GpuSceneArrays::GlobalIlluminationProbe::getSingleton().getBufferView());
-
-				for(const auto& probe : SceneGraph::getSingleton().getComponentArrays().getGlobalIlluminationProbes())
+				struct Consts
 				{
-					struct Consts
-					{
-						Mat4 m_viewProjMat;
-						Mat4 m_invViewProjMat;
+					Mat4 m_viewProjMat;
+					Mat4 m_invViewProjMat;
 
-						Vec2 m_viewportSize;
-						U32 m_probeIdx;
-						F32 m_sphereRadius;
+					Vec2 m_viewportSize;
+					U32 m_probeIdx;
+					F32 m_sphereRadius;
 
-						Vec3 m_cameraPos;
-						F32 m_padding;
-					};
+					Vec3 m_cameraPos;
+					F32 m_pixelShift;
+				};
 
-					Consts* consts = allocateAndBindConstants<Consts>(cmdb, 0, 0);
-
-					consts->m_viewProjMat = ctx.m_matrices.m_viewProjectionJitter;
-					consts->m_invViewProjMat = ctx.m_matrices.m_invertedViewProjectionJitter;
-					consts->m_viewportSize = Vec2(getRenderer().getInternalResolution());
-					consts->m_probeIdx = probe.getGpuSceneAllocation().getIndex();
-					consts->m_sphereRadius = 0.5f;
-					consts->m_cameraPos = ctx.m_matrices.m_cameraTransform.getTranslationPart().xyz();
+				// Visualize GI probes
+				if(g_visualizeGiProbes.get())
+				{
+					cmdb.bindShaderProgram(m_visualizeGiProbeGrProg.get());
+					cmdb.bindSrv(0, 0, GpuSceneArrays::GlobalIlluminationProbe::getSingleton().getBufferView());
 
-					cmdb.draw(PrimitiveTopology::kTriangles, 6, probe.getCellCount());
+					for(const auto& probe : SceneGraph::getSingleton().getComponentArrays().getGlobalIlluminationProbes())
+					{
+						Consts* consts = allocateAndBindConstants<Consts>(cmdb, 0, 0);
+
+						consts->m_viewProjMat = ctx.m_matrices.m_viewProjectionJitter;
+						consts->m_invViewProjMat = ctx.m_matrices.m_invertedViewProjectionJitter;
+						consts->m_viewportSize = Vec2(getRenderer().getInternalResolution());
+						consts->m_probeIdx = probe.getGpuSceneAllocation().getIndex();
+						consts->m_sphereRadius = 0.5f;
+						consts->m_cameraPos = ctx.m_matrices.m_cameraTransform.getTranslationPart().xyz();
+						consts->m_pixelShift = (getRenderer().getFrameCount() & 1) ? 1.0f : 0.0f;
+
+						cmdb.draw(PrimitiveTopology::kTriangles, 6, probe.getCellCount());
+					}
 				}
-			}
-
-			// Visualize refl probes
-			if(g_visualizeReflectionProbes.get())
-			{
-				cmdb.bindShaderProgram(m_visualizeReflProbeGrProg.get());
-				cmdb.bindSrv(0, 0, GpuSceneArrays::ReflectionProbe::getSingleton().getBufferView());
 
-				for(const auto& probe : SceneGraph::getSingleton().getComponentArrays().getReflectionProbes())
+				// Visualize refl probes
+				if(g_visualizeReflectionProbes.get())
 				{
-					struct Consts
-					{
-						Mat4 m_viewProjMat;
-						Mat4 m_invViewProjMat;
-
-						Vec2 m_viewportSize;
-						U32 m_probeIdx;
-						F32 m_sphereRadius;
-
-						Vec3 m_cameraPos;
-						F32 m_padding;
-					};
+					cmdb.bindShaderProgram(m_visualizeReflProbeGrProg.get());
+					cmdb.bindSrv(0, 0, GpuSceneArrays::ReflectionProbe::getSingleton().getBufferView());
 
-					Consts* consts = allocateAndBindConstants<Consts>(cmdb, 0, 0);
-
-					consts->m_viewProjMat = ctx.m_matrices.m_viewProjectionJitter;
-					consts->m_invViewProjMat = ctx.m_matrices.m_invertedViewProjectionJitter;
-					consts->m_viewportSize = Vec2(getRenderer().getInternalResolution());
-					consts->m_probeIdx = probe.getGpuSceneAllocation().getIndex();
-					consts->m_sphereRadius = 0.5f;
-					consts->m_cameraPos = ctx.m_matrices.m_cameraTransform.getTranslationPart().xyz();
-
-					cmdb.draw(PrimitiveTopology::kTriangles, 6);
+					for(const auto& probe : SceneGraph::getSingleton().getComponentArrays().getReflectionProbes())
+					{
+						Consts* consts = allocateAndBindConstants<Consts>(cmdb, 0, 0);
+
+						consts->m_viewProjMat = ctx.m_matrices.m_viewProjectionJitter;
+						consts->m_invViewProjMat = ctx.m_matrices.m_invertedViewProjectionJitter;
+						consts->m_viewportSize = Vec2(getRenderer().getInternalResolution());
+						consts->m_probeIdx = probe.getGpuSceneAllocation().getIndex();
+						consts->m_sphereRadius = 0.5f;
+						consts->m_cameraPos = ctx.m_matrices.m_cameraTransform.getTranslationPart().xyz();
+						consts->m_pixelShift = (getRenderer().getFrameCount() & 1) ? 1.0f : 0.0f;
+
+						cmdb.draw(PrimitiveTopology::kTriangles, 6);
+					}
 				}
 			}
 		});

+ 24 - 0
AnKi/Shaders/Functions.hlsl

@@ -762,3 +762,27 @@ void unflatten3dArrayIndex(const U32 sizeA, const U32 sizeB, const U32 sizeC, co
 	b = (flatIdx / sizeC) % sizeB;
 	c = flatIdx % sizeC;
 }
+
+Bool dither2x2(Vec2 svPosition, F32 factor)
+{
+	const U32 ditherMatrix[4] = {0, 3, 2, 1};
+	const F32 axisSize = 2.0;
+
+	const U32 x = U32(fmod(svPosition.x, axisSize));
+	const U32 y = U32(fmod(svPosition.y, axisSize));
+	const U32 index = x + y * U32(axisSize);
+	const F32 limit = (F32(ditherMatrix[index]) + 1.0) / (1.0 + axisSize * axisSize);
+	return (factor < limit) ? true : false;
+}
+
+Bool dither4x4(Vec2 svPosition, F32 factor)
+{
+	const U32 ditherMatrix[16] = {0, 12, 3, 15, 8, 4, 11, 7, 2, 14, 1, 13, 10, 6, 9, 5};
+	const F32 axisSize = 4.0;
+
+	const U32 x = U32(fmod(svPosition.x, axisSize));
+	const U32 y = U32(fmod(svPosition.y, axisSize));
+	const U32 index = x + y * U32(axisSize);
+	const F32 limit = (F32(ditherMatrix[index]) + 1.0) / (1.0 + axisSize * axisSize);
+	return (factor < limit) ? true : false;
+}

+ 16 - 2
AnKi/Shaders/GBufferVisualizeProbe.ankiprog

@@ -26,7 +26,7 @@ struct Consts
 	F32 m_sphereRadius;
 
 	Vec3 m_cameraPos;
-	F32 m_padding;
+	F32 m_pixelShift;
 };
 
 ConstantBuffer<Consts> g_consts : register(b0);
@@ -147,9 +147,23 @@ PixelOut main(VertOut input)
 		discard;
 	}
 
-	const F32 t = min(t0, t1);
+	// Compute a fade close to the camera
+	const F32 fullBrightDist = g_consts.m_sphereRadius * 4.0;
+	const F32 zeroBrightDist = g_consts.m_sphereRadius * 2.0;
+	F32 fade = length(g_consts.m_cameraPos - input.m_sphereCenter) - zeroBrightDist;
+	fade = saturate(fade / (fullBrightDist - zeroBrightDist));
+	fade = sqrt(fade);
 
+	// Dither close to the camera
+	const Bool skip = dither4x4(input.m_svPosition.xy + Vec2(g_consts.m_pixelShift, 0.0), fade);
+	if(skip)
+	{
+		discard;
+	}
+
+	const F32 t = min(t0, t1);
 	const Vec3 collisionPoint = g_consts.m_cameraPos + rayDir * t;
+
 	const Vec4 p = mul(g_consts.m_viewProjMat, Vec4(collisionPoint, 1.0));
 	output.m_svDepth = p.z / p.w;