Browse Source

Add more debug drawing capabilities and fix a couple of bugs

Panagiotis Christopoulos Charitos 6 years ago
parent
commit
56e023e131

BIN
engine_data/GiProbe.ankitex


+ 1 - 1
shaders/Pack.glsl

@@ -159,7 +159,7 @@ void writeGBuffer(GbufferInfo g, out Vec4 rt0, out Vec4 rt1, out Vec4 rt2, out V
 // Read from G-buffer
 Vec3 readNormalFromGBuffer(texture2D rt2, sampler sampl, Vec2 uv)
 {
-	return signedOctDecode(textureLod(rt2, sampl, uv, 0.0).rga);
+	return signedOctDecode(textureLod(rt2, sampl, uv, 0.0).gba);
 }
 
 // Read the roughness from G-buffer

+ 1 - 1
src/anki/collision/GjkEpa.cpp

@@ -47,7 +47,7 @@ static Vec4 crossAba(const Vec4& a, const Vec4& b)
 static void support(const GjkContext& ctx, GjkSupport& support)
 {
 	support.m_v0 = ctx.m_shape0Callback(ctx.m_shape0, ctx.m_dir);
-	support.m_v1 = ctx.m_shape0Callback(ctx.m_shape1, -ctx.m_dir);
+	support.m_v1 = ctx.m_shape1Callback(ctx.m_shape1, -ctx.m_dir);
 	support.m_v = support.m_v0 - support.m_v1;
 }
 

+ 21 - 1
src/anki/renderer/Dbg.cpp

@@ -93,7 +93,7 @@ void Dbg::run(RenderPassWorkContext& rgraphCtx, const RenderingContext& ctx)
 	{
 		const RenderableQueueElement& el = ctx.m_renderQueue->m_renderables[i];
 		Array<void*, 1> a = {{const_cast<void*>(el.m_userData)}};
-		// el.m_callback(dctx, a);
+		el.m_callback(dctx, a);
 	}
 
 	// Draw probes
@@ -133,6 +133,26 @@ void Dbg::run(RenderPassWorkContext& rgraphCtx, const RenderingContext& ctx)
 			el.m_debugDrawCallback(dctx, a);
 		}
 	}
+
+	// Reflection probes
+	if(threadId == 0)
+	{
+		for(const ReflectionProbeQueueElement& el : ctx.m_renderQueue->m_reflectionProbes)
+		{
+			Array<void*, 1> a = {{const_cast<void*>(el.m_debugDrawCallbackUserData)}};
+			el.m_debugDrawCallback(dctx, a);
+		}
+	}
+
+	// GI probes
+	if(threadId == 0)
+	{
+		for(const GlobalIlluminationProbeQueueElement& el : ctx.m_renderQueue->m_giProbes)
+		{
+			Array<void*, 1> a = {{const_cast<void*>(el.m_debugDrawCallbackUserData)}};
+			el.m_debugDrawCallback(dctx, a);
+		}
+	}
 }
 
 void Dbg::populateRenderGraph(RenderingContext& ctx)

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

@@ -281,7 +281,7 @@ void ProbeReflections::prepareProbes(RenderingContext& ctx,
 			{
 				// Probe will be updated next frame
 				foundProbeToUpdateNextFrame = true;
-				probe.m_feedbackCallback(true, probe.m_userData);
+				probe.m_feedbackCallback(true, probe.m_feedbackCallbackUserData);
 				continue;
 			}
 			else if(!canUpdateThisFrame)
@@ -318,7 +318,7 @@ void ProbeReflections::prepareProbes(RenderingContext& ctx,
 		// Don't gather renderables next frame
 		if(probe.m_renderQueues[0] != nullptr)
 		{
-			probe.m_feedbackCallback(false, probe.m_userData);
+			probe.m_feedbackCallback(false, probe.m_feedbackCallbackUserData);
 		}
 	}
 

+ 3 - 2
src/anki/renderer/RenderQueue.h

@@ -185,8 +185,9 @@ class ReflectionProbeQueueElement final
 public:
 	U64 m_uuid;
 	ReflectionProbeQueueElementFeedbackCallback m_feedbackCallback;
-	RenderQueueDrawCallback m_drawCallback;
-	void* m_userData;
+	void* m_feedbackCallbackUserData;
+	RenderQueueDrawCallback m_debugDrawCallback;
+	const void* m_debugDrawCallbackUserData;
 	Array<RenderQueue*, 6> m_renderQueues;
 	Vec3 m_worldPosition;
 	Vec3 m_aabbMin;

+ 9 - 5
src/anki/scene/DecalNode.cpp

@@ -104,15 +104,16 @@ void DecalNode::drawCallback(RenderQueueDrawContext& ctx, ConstWeakArray<void*>
 		const DecalNode& self = *static_cast<const DecalNode*>(userData[i]);
 		const DecalComponent& decalComp = self.getComponent<DecalComponent>();
 
-		Mat3 rot = decalComp.getBoundingVolume().getRotation().getRotationPart();
+		const Mat3 rot = decalComp.getBoundingVolume().getRotation().getRotationPart();
 		const Vec4 tsl = decalComp.getBoundingVolume().getCenter().xyz1();
 		const Vec3 scale = decalComp.getBoundingVolume().getExtend().xyz();
 
-		rot(0, 0) *= scale.x();
-		rot(1, 1) *= scale.y();
-		rot(2, 2) *= scale.z();
+		Mat3 nonUniScale = Mat3::getZero();
+		nonUniScale(0, 0) = scale.x();
+		nonUniScale(1, 1) = scale.y();
+		nonUniScale(2, 2) = scale.z();
 
-		mvps[i] = ctx.m_viewProjectionMatrix * Mat4(tsl, rot, 1.0f);
+		mvps[i] = ctx.m_viewProjectionMatrix * Mat4(tsl, rot * nonUniScale, 1.0f);
 		positions[i] = tsl.xyz();
 	}
 
@@ -146,6 +147,9 @@ void DecalNode::drawCallback(RenderQueueDrawContext& ctx, ConstWeakArray<void*>
 		*ctx.m_stagingGpuAllocator,
 		ctx.m_commandBuffer);
 
+	ctx.m_frameAllocator.deleteArray(positions, userData.getSize());
+	ctx.m_frameAllocator.deleteArray(mvps, userData.getSize());
+
 	// Restore state
 	if(!enableDepthTest)
 	{

+ 28 - 8
src/anki/scene/GlobalIlluminationProbeNode.cpp

@@ -9,6 +9,8 @@
 #include <anki/scene/components/MoveComponent.h>
 #include <anki/scene/components/SpatialComponent.h>
 #include <anki/scene/components/GlobalIlluminationProbeComponent.h>
+#include <anki/resource/ResourceManager.h>
+#include <anki/resource/TextureResource.h>
 
 namespace anki
 {
@@ -124,7 +126,9 @@ Error GlobalIlluminationProbeNode::init()
 	SpatialComponent* spatialc = newComponent<SpatialComponent>(this, &m_spatialAabb);
 	spatialc->setUpdateOctreeBounds(false);
 
+	// Misc
 	ANKI_CHECK(m_dbgDrawer.init(&getResourceManager()));
+	ANKI_CHECK(getResourceManager().loadResource("engine_data/GiProbe.ankitex", m_dbgTex));
 
 	return Error::NONE;
 }
@@ -205,6 +209,7 @@ Error GlobalIlluminationProbeNode::frameUpdate(Second prevUpdateTime, Second crn
 void GlobalIlluminationProbeNode::debugDrawCallback(RenderQueueDrawContext& ctx, ConstWeakArray<void*> userData)
 {
 	Mat4* const mvps = ctx.m_frameAllocator.newArray<Mat4>(userData.getSize());
+	Vec3* const positions = ctx.m_frameAllocator.newArray<Vec3>(userData.getSize());
 	for(U i = 0; i < userData.getSize(); ++i)
 	{
 		const GlobalIlluminationProbeNode& self = *static_cast<const GlobalIlluminationProbeNode*>(userData[i]);
@@ -220,6 +225,7 @@ void GlobalIlluminationProbeNode::debugDrawCallback(RenderQueueDrawContext& ctx,
 		rot(2, 2) *= scale.z();
 
 		mvps[i] = ctx.m_viewProjectionMatrix * Mat4(tsl.xyz1(), rot, 1.0f);
+		positions[i] = tsl.xyz();
 	}
 
 	const Bool enableDepthTest = ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DEPTH_TEST_ON);
@@ -232,14 +238,28 @@ void GlobalIlluminationProbeNode::debugDrawCallback(RenderQueueDrawContext& ctx,
 		ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::ALWAYS);
 	}
 
-	static_cast<const GlobalIlluminationProbeNode*>(userData[0])
-		->m_dbgDrawer.drawCubes(ConstWeakArray<Mat4>(mvps, userData.getSize()),
-			Vec4(0.729f, 0.635f, 0.196f, 1.0f),
-			1.0f,
-			ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DITHERED_DEPTH_TEST_ON),
-			2.0f,
-			*ctx.m_stagingGpuAllocator,
-			ctx.m_commandBuffer);
+	const GlobalIlluminationProbeNode& self = *static_cast<const GlobalIlluminationProbeNode*>(userData[0]);
+	self.m_dbgDrawer.drawCubes(ConstWeakArray<Mat4>(mvps, userData.getSize()),
+		Vec4(0.729f, 0.635f, 0.196f, 1.0f),
+		1.0f,
+		ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DITHERED_DEPTH_TEST_ON),
+		2.0f,
+		*ctx.m_stagingGpuAllocator,
+		ctx.m_commandBuffer);
+
+	self.m_dbgDrawer.drawBillboardTextures(ctx.m_projectionMatrix,
+		ctx.m_viewMatrix,
+		ConstWeakArray<Vec3>(positions, userData.getSize()),
+		Vec4(1.0f),
+		ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DITHERED_DEPTH_TEST_ON),
+		self.m_dbgTex->getGrTextureView(),
+		ctx.m_sampler,
+		Vec2(0.75f),
+		*ctx.m_stagingGpuAllocator,
+		ctx.m_commandBuffer);
+
+	ctx.m_frameAllocator.deleteArray(positions, userData.getSize());
+	ctx.m_frameAllocator.deleteArray(mvps, userData.getSize());
 
 	// Restore state
 	if(!enableDepthTest)

+ 2 - 0
src/anki/scene/GlobalIlluminationProbeNode.h

@@ -37,7 +37,9 @@ private:
 	Array<Transform, 6> m_cubeFaceTransforms;
 	Aabb m_spatialAabb = Aabb(Vec3(-1.0f), Vec3(1.0f));
 	Vec4 m_previousPosition = Vec4(0.0f);
+
 	DebugDrawer2 m_dbgDrawer;
+	TextureResourcePtr m_dbgTex;
 
 	void onMoveUpdate(MoveComponent& move);
 	void onShapeUpdateOrProbeNeedsRendering();

+ 8 - 5
src/anki/scene/ModelNode.cpp

@@ -209,17 +209,18 @@ void ModelNode::draw(RenderQueueDrawContext& ctx, ConstWeakArray<void*> userData
 		{
 			const ModelNode& self2 = *static_cast<const ModelNode*>(userData[i]);
 
-			Mat3 rot = self2.m_obb.getRotation().getRotationPart();
+			const Mat3 rot = self2.m_obb.getRotation().getRotationPart();
 			const Vec4 tsl = self2.m_obb.getCenter().xyz1();
 			const Vec3 scale = self2.m_obb.getExtend().xyz();
 
 			// Set non uniform scale. Add a margin to avoid flickering
+			Mat3 nonUniScale = Mat3::getZero();
 			const F32 MARGIN = 1.02;
-			rot(0, 0) *= scale.x() * MARGIN;
-			rot(1, 1) *= scale.y() * MARGIN;
-			rot(2, 2) *= scale.z() * MARGIN;
+			nonUniScale(0, 0) = scale.x() * MARGIN;
+			nonUniScale(1, 1) = scale.y() * MARGIN;
+			nonUniScale(2, 2) = scale.z() * MARGIN;
 
-			mvps[i] = ctx.m_viewProjectionMatrix * Mat4(tsl, rot, 1.0f);
+			mvps[i] = ctx.m_viewProjectionMatrix * Mat4(tsl, rot * nonUniScale, 1.0f);
 		}
 
 		const Bool enableDepthTest = ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DEPTH_TEST_ON);
@@ -240,6 +241,8 @@ void ModelNode::draw(RenderQueueDrawContext& ctx, ConstWeakArray<void*> userData
 			*ctx.m_stagingGpuAllocator,
 			cmdb);
 
+		ctx.m_frameAllocator.deleteArray(mvps, userData.getSize());
+
 		// Restore state
 		if(!enableDepthTest)
 		{

+ 67 - 0
src/anki/scene/ReflectionProbeNode.cpp

@@ -106,6 +106,11 @@ Error ReflectionProbeNode::init(const Vec4& aabbMinLSpace, const Vec4& aabbMaxLS
 	ReflectionProbeComponent* reflc = newComponent<ReflectionProbeComponent>(getSceneGraph().getNewUuid());
 	reflc->setPosition(Vec4(0.0f));
 	reflc->setBoundingBox(aabbMinLSpace, aabbMaxLSpace);
+	reflc->setDrawCallback(drawCallback, this);
+
+	// Misc
+	ANKI_CHECK(m_dbgDrawer.init(&getResourceManager()));
+	ANKI_CHECK(getResourceManager().loadResource("engine_data/Mirror.ankitex", m_dbgTex));
 
 	return Error::NONE;
 }
@@ -159,4 +164,66 @@ Error ReflectionProbeNode::frameUpdate(Second prevUpdateTime, Second crntTime)
 	return Error::NONE;
 }
 
+void ReflectionProbeNode::drawCallback(RenderQueueDrawContext& ctx, ConstWeakArray<void*> userData)
+{
+	Mat4* const mvps = ctx.m_frameAllocator.newArray<Mat4>(userData.getSize());
+	Vec3* const positions = ctx.m_frameAllocator.newArray<Vec3>(userData.getSize());
+	for(U i = 0; i < userData.getSize(); ++i)
+	{
+		const ReflectionProbeNode& self = *static_cast<const ReflectionProbeNode*>(userData[i]);
+		const ReflectionProbeComponent& rComp = self.getComponent<ReflectionProbeComponent>();
+
+		const Vec3 tsl = (rComp.getBoundingBoxMin().xyz() + rComp.getBoundingBoxMax().xyz()) / 2.0f;
+		const Vec3 scale = (tsl - rComp.getBoundingBoxMin().xyz());
+
+		// Set non uniform scale.
+		Mat3 rot = Mat3::getIdentity();
+		rot(0, 0) *= scale.x();
+		rot(1, 1) *= scale.y();
+		rot(2, 2) *= scale.z();
+
+		mvps[i] = ctx.m_viewProjectionMatrix * Mat4(tsl.xyz1(), rot, 1.0f);
+		positions[i] = tsl.xyz();
+	}
+
+	const Bool enableDepthTest = ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DEPTH_TEST_ON);
+	if(enableDepthTest)
+	{
+		ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::LESS);
+	}
+	else
+	{
+		ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::ALWAYS);
+	}
+
+	const ReflectionProbeNode& self = *static_cast<const ReflectionProbeNode*>(userData[0]);
+	self.m_dbgDrawer.drawCubes(ConstWeakArray<Mat4>(mvps, userData.getSize()),
+		Vec4(0.0f, 0.0f, 1.0f, 1.0f),
+		1.0f,
+		ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DITHERED_DEPTH_TEST_ON),
+		2.0f,
+		*ctx.m_stagingGpuAllocator,
+		ctx.m_commandBuffer);
+
+	self.m_dbgDrawer.drawBillboardTextures(ctx.m_projectionMatrix,
+		ctx.m_viewMatrix,
+		ConstWeakArray<Vec3>(positions, userData.getSize()),
+		Vec4(1.0f),
+		ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DITHERED_DEPTH_TEST_ON),
+		self.m_dbgTex->getGrTextureView(),
+		ctx.m_sampler,
+		Vec2(0.75f),
+		*ctx.m_stagingGpuAllocator,
+		ctx.m_commandBuffer);
+
+	ctx.m_frameAllocator.deleteArray(positions, userData.getSize());
+	ctx.m_frameAllocator.deleteArray(mvps, userData.getSize());
+
+	// Restore state
+	if(!enableDepthTest)
+	{
+		ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::LESS);
+	}
+}
+
 } // end namespace anki

+ 6 - 0
src/anki/scene/ReflectionProbeNode.h

@@ -7,6 +7,7 @@
 
 #include <anki/scene/SceneNode.h>
 #include <anki/collision/Aabb.h>
+#include <anki/scene/DebugDrawer.h>
 
 namespace anki
 {
@@ -43,7 +44,12 @@ private:
 	Vec3 m_aabbMaxLSpace = Vec3(-1.0f);
 	Aabb m_spatialAabb;
 
+	DebugDrawer2 m_dbgDrawer;
+	TextureResourcePtr m_dbgTex;
+
 	void onMoveUpdate(MoveComponent& move);
+
+	static void drawCallback(RenderQueueDrawContext& ctx, ConstWeakArray<void*> userData);
 };
 /// @}
 

+ 11 - 7
src/anki/scene/components/ReflectionProbeComponent.h

@@ -57,22 +57,31 @@ public:
 		return m_markedForRendering;
 	}
 
+	void setDrawCallback(RenderQueueDrawCallback callback, const void* userData)
+	{
+		m_drawCallback = callback;
+		m_drawCallbackUserData = userData;
+	}
+
 	void setupReflectionProbeQueueElement(ReflectionProbeQueueElement& el) const
 	{
 		ANKI_ASSERT(m_aabbMin < m_aabbMax);
 		ANKI_ASSERT(m_pos > m_aabbMin && m_pos < m_aabbMax);
 		el.m_feedbackCallback = reflectionProbeQueueElementFeedbackCallback;
-		el.m_userData = const_cast<ReflectionProbeComponent*>(this);
+		el.m_feedbackCallbackUserData = const_cast<ReflectionProbeComponent*>(this);
 		el.m_uuid = m_uuid;
 		el.m_worldPosition = m_pos;
 		el.m_aabbMin = m_aabbMin;
 		el.m_aabbMax = m_aabbMax;
 		el.m_textureArrayIndex = MAX_U32;
-		el.m_drawCallback = debugDrawCallback;
+		el.m_debugDrawCallback = m_drawCallback;
+		el.m_debugDrawCallbackUserData = m_drawCallbackUserData;
 	}
 
 private:
 	U64 m_uuid;
+	RenderQueueDrawCallback m_drawCallback = nullptr;
+	const void* m_drawCallbackUserData = nullptr;
 	Vec3 m_pos = Vec3(0.0f);
 	Vec3 m_aabbMin = Vec3(+1.0f);
 	Vec3 m_aabbMax = Vec3(-1.0f);
@@ -83,11 +92,6 @@ private:
 		ANKI_ASSERT(userData);
 		static_cast<ReflectionProbeComponent*>(userData)->m_markedForRendering = fillRenderQueuesOnNextFrame;
 	}
-
-	static void debugDrawCallback(RenderQueueDrawContext& ctx, ConstWeakArray<void*> userData)
-	{
-		// TODO
-	}
 };
 /// @}