2
0
Эх сурвалжийг харах

Added ability to record debug output of a unit test (#598)

Jorrit Rouwe 2 жил өмнө
parent
commit
39bbde4aea

+ 48 - 14
UnitTests/PhysicsTestContext.cpp

@@ -9,6 +9,10 @@
 #include <Jolt/Physics/Collision/Shape/SphereShape.h>
 #include <Jolt/Core/JobSystemThreadPool.h>
 #include <Jolt/Core/TempAllocator.h>
+#include <Jolt/Core/StreamWrapper.h>
+#ifdef JPH_DEBUG_RENDERER
+	#include <Jolt/Renderer/DebugRendererRecorder.h>
+#endif
 
 PhysicsTestContext::PhysicsTestContext(float inDeltaTime, int inCollisionSteps, int inWorkerThreads, uint inMaxBodies, uint inMaxBodyPairs, uint inMaxContactConstraints) :
 #ifdef JPH_DISABLE_TEMP_ALLOCATOR
@@ -27,6 +31,11 @@ PhysicsTestContext::PhysicsTestContext(float inDeltaTime, int inCollisionSteps,
 
 PhysicsTestContext::~PhysicsTestContext()
 {
+#ifdef JPH_DEBUG_RENDERER
+	delete mDebugRenderer;
+	delete mStreamWrapper;
+	delete mStream;
+#endif // JPH_DEBUG_RENDERER
 	delete mSystem;
 	delete mJobSystem;
 	delete mTempAllocator;
@@ -77,6 +86,23 @@ Body &PhysicsTestContext::CreateSphere(RVec3Arg inPosition, float inRadius, EMot
 	return CreateBody(new SphereShapeSettings(inRadius), inPosition, Quat::sIdentity(), inMotionType, inMotionQuality, inLayer, inActivation);
 }
 
+EPhysicsUpdateError PhysicsTestContext::SimulateSingleStep()
+{
+	EPhysicsUpdateError errors = mSystem->Update(mDeltaTime, mCollisionSteps, mTempAllocator, mJobSystem);
+#ifndef JPH_DISABLE_TEMP_ALLOCATOR
+	JPH_ASSERT(static_cast<TempAllocatorImpl *>(mTempAllocator)->IsEmpty());
+#endif // JPH_DISABLE_TEMP_ALLOCATOR
+#ifdef JPH_DEBUG_RENDERER
+	if (mDebugRenderer != nullptr)
+	{
+		mSystem->DrawBodies(BodyManager::DrawSettings(), mDebugRenderer);
+		mSystem->DrawConstraints(mDebugRenderer);
+		mDebugRenderer->EndFrame();
+	}
+#endif // JPH_DEBUG_RENDERER
+	return errors;
+}
+
 EPhysicsUpdateError PhysicsTestContext::Simulate(float inTotalTime, function<void()> inPreStepCallback)
 {
 	EPhysicsUpdateError errors = EPhysicsUpdateError::None;
@@ -84,25 +110,13 @@ EPhysicsUpdateError PhysicsTestContext::Simulate(float inTotalTime, function<voi
 	const int cNumSteps = int(round(inTotalTime / mDeltaTime));
 	for (int s = 0; s < cNumSteps; ++s)
 	{
-		inPreStepCallback();
-		errors |= mSystem->Update(mDeltaTime, mCollisionSteps, mTempAllocator, mJobSystem);
-	#ifndef JPH_DISABLE_TEMP_ALLOCATOR
-		JPH_ASSERT(static_cast<TempAllocatorImpl *>(mTempAllocator)->IsEmpty());
-	#endif // JPH_DISABLE_TEMP_ALLOCATOR
+		inPreStepCallback();		
+		errors |= SimulateSingleStep();
 	}
 
 	return errors;
 }
 
-EPhysicsUpdateError PhysicsTestContext::SimulateSingleStep()
-{
-	EPhysicsUpdateError errors = mSystem->Update(mDeltaTime, mCollisionSteps, mTempAllocator, mJobSystem);
-#ifndef JPH_DISABLE_TEMP_ALLOCATOR
-	JPH_ASSERT(static_cast<TempAllocatorImpl *>(mTempAllocator)->IsEmpty());
-#endif // JPH_DISABLE_TEMP_ALLOCATOR
-	return errors;
-}
-
 RVec3 PhysicsTestContext::PredictPosition(RVec3Arg inPosition, Vec3Arg inVelocity, Vec3Arg inAcceleration, float inTotalTime) const
 {
 	// Integrate position using a Symplectic Euler step (just like the PhysicsSystem)
@@ -137,3 +151,23 @@ Quat PhysicsTestContext::PredictOrientation(QuatArg inRotation, Vec3Arg inAngula
 	}
 	return rot;
 }
+
+#ifdef JPH_DEBUG_RENDERER
+
+void PhysicsTestContext::RecordDebugOutput(const char *inFileName)
+{
+	mStream = new ofstream;
+	mStream->open(inFileName, ofstream::out | ofstream::binary | ofstream::trunc);
+	if (mStream->is_open())
+	{
+		mStreamWrapper = new StreamOutWrapper(*mStream);
+		mDebugRenderer = new DebugRendererRecorder(*mStreamWrapper);
+	}
+	else
+	{
+		delete mStream;
+		mStream = nullptr;
+	}
+}
+
+#endif // JPH_DEBUG_RENDERER

+ 16 - 0
UnitTests/PhysicsTestContext.h

@@ -9,9 +9,15 @@
 #include <Jolt/Physics/Constraints/TwoBodyConstraint.h>
 #include "Layers.h"
 
+JPH_SUPPRESS_WARNINGS_STD_BEGIN
+#include <fstream>
+JPH_SUPPRESS_WARNINGS_STD_END
+
 namespace JPH {
 	class TempAllocator;
 	class JobSystem;
+	class DebugRendererRecorder;
+	class StreamOutWrapper;
 };
 
 // Helper class used in test cases for creating and manipulating physics objects
@@ -82,6 +88,11 @@ public:
 		return mDeltaTime / mCollisionSteps;
 	}
 
+#ifdef JPH_DEBUG_RENDERER
+	// Write the debug output to a file to be able to replay it with JoltViewer
+	void				RecordDebugOutput(const char *inFileName);
+#endif // JPH_DEBUG_RENDERER
+
 private:
 	TempAllocator *		mTempAllocator;
 	JobSystem *			mJobSystem;
@@ -89,6 +100,11 @@ private:
 	ObjectVsBroadPhaseLayerFilterImpl mObjectVsBroadPhaseLayerFilter;
 	ObjectLayerPairFilterImpl mObjectVsObjectLayerFilter;
 	PhysicsSystem *		mSystem;
+#ifdef JPH_DEBUG_RENDERER
+	DebugRendererRecorder *mDebugRenderer = nullptr;
+	ofstream *			mStream = nullptr;
+	StreamOutWrapper *	mStreamWrapper = nullptr;
+#endif // JPH_DEBUG_RENDERER
 	float				mDeltaTime;
 	int					mCollisionSteps;
 };