Browse Source

Add JobSystemSingleThreaded (#18)

This PR adds bindings for
[JobSystemSingleThreaded](https://jrouwe.github.io/JoltPhysics/class_job_system_single_threaded.html)

After profiling, we detected that `JobSystemThreadPool` was introducing
unnecessary overhead for our use case. In tests,
`JPC_PhysicsSystem_Update` time dropped from roughly ~1ms to ~105µs.
Marcos Casagrande 3 months ago
parent
commit
50e60a3945
3 changed files with 24 additions and 4 deletions
  1. 1 1
      HelloWorld/main.cpp
  2. 7 2
      JoltC/Functions.h
  3. 16 1
      JoltCImpl/JoltC.cpp

+ 1 - 1
HelloWorld/main.cpp

@@ -194,7 +194,7 @@ int main() {
 
 
 		printf("Step %d: Position = (%f, %f, %f), Velocity = (%f, %f, %f)\n", step, position.x, position.y, position.z, velocity.x, velocity.y, velocity.z);
 		printf("Step %d: Position = (%f, %f, %f), Velocity = (%f, %f, %f)\n", step, position.x, position.y, position.z, velocity.x, velocity.y, velocity.z);
 
 
-		JPC_PhysicsSystem_Update(physics_system, cDeltaTime, cCollisionSteps, temp_allocator, job_system);
+		JPC_PhysicsSystem_Update(physics_system, cDeltaTime, cCollisionSteps, temp_allocator, (JPC_JobSystem*) job_system);
 	}
 	}
 
 
 	JPC_BodyInterface_RemoveBody(body_interface, sphere_id);
 	JPC_BodyInterface_RemoveBody(body_interface, sphere_id);

+ 7 - 2
JoltC/Functions.h

@@ -251,9 +251,11 @@ JPC_API JPC_TempAllocatorImpl* JPC_TempAllocatorImpl_new(uint size);
 JPC_API void JPC_TempAllocatorImpl_delete(JPC_TempAllocatorImpl* object);
 JPC_API void JPC_TempAllocatorImpl_delete(JPC_TempAllocatorImpl* object);
 
 
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
-// JobSystemThreadPool
+// JobSystem
 
 
+typedef struct JPC_JobSystem JPC_JobSystem;
 typedef struct JPC_JobSystemThreadPool JPC_JobSystemThreadPool;
 typedef struct JPC_JobSystemThreadPool JPC_JobSystemThreadPool;
+typedef struct JPC_JobSystemSingleThreaded JPC_JobSystemSingleThreaded;
 
 
 JPC_API JPC_JobSystemThreadPool* JPC_JobSystemThreadPool_new2(
 JPC_API JPC_JobSystemThreadPool* JPC_JobSystemThreadPool_new2(
 	uint inMaxJobs,
 	uint inMaxJobs,
@@ -265,6 +267,9 @@ JPC_API JPC_JobSystemThreadPool* JPC_JobSystemThreadPool_new3(
 
 
 JPC_API void JPC_JobSystemThreadPool_delete(JPC_JobSystemThreadPool* object);
 JPC_API void JPC_JobSystemThreadPool_delete(JPC_JobSystemThreadPool* object);
 
 
+JPC_API JPC_JobSystemSingleThreaded* JPC_JobSystemSingleThreaded_new(uint inMaxJobs);
+JPC_API void JPC_JobSystemSingleThreaded_delete(JPC_JobSystemSingleThreaded* object);
+
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
 // CollisionGroup and GroupFilter
 // CollisionGroup and GroupFilter
 
 
@@ -1550,7 +1555,7 @@ JPC_API JPC_PhysicsUpdateError JPC_PhysicsSystem_Update(
 	float inDeltaTime,
 	float inDeltaTime,
 	int inCollisionSteps,
 	int inCollisionSteps,
 	JPC_TempAllocatorImpl *inTempAllocator, // FIXME: un-specialize
 	JPC_TempAllocatorImpl *inTempAllocator, // FIXME: un-specialize
-	JPC_JobSystemThreadPool *inJobSystem); // FIXME: un-specialize
+	JPC_JobSystem* inJobSystem);
 
 
 JPC_API void JPC_PhysicsSystem_AddConstraint(JPC_PhysicsSystem* self, JPC_Constraint* constraint);
 JPC_API void JPC_PhysicsSystem_AddConstraint(JPC_PhysicsSystem* self, JPC_Constraint* constraint);
 JPC_API void JPC_PhysicsSystem_RemoveConstraint(JPC_PhysicsSystem* self, JPC_Constraint* constraint);
 JPC_API void JPC_PhysicsSystem_RemoveConstraint(JPC_PhysicsSystem* self, JPC_Constraint* constraint);

+ 16 - 1
JoltCImpl/JoltC.cpp

@@ -1,6 +1,8 @@
 #include <Jolt/Jolt.h>
 #include <Jolt/Jolt.h>
 
 
 #include <Jolt/Core/Factory.h>
 #include <Jolt/Core/Factory.h>
+#include <Jolt/Core/JobSystem.h>
+#include <Jolt/Core/JobSystemSingleThreaded.h>
 #include <Jolt/Core/JobSystemThreadPool.h>
 #include <Jolt/Core/JobSystemThreadPool.h>
 #include <Jolt/Core/TempAllocator.h>
 #include <Jolt/Core/TempAllocator.h>
 #include <Jolt/Physics/Body/BodyActivationListener.h>
 #include <Jolt/Physics/Body/BodyActivationListener.h>
@@ -113,9 +115,15 @@ OPAQUE_WRAPPER(JPC_NarrowPhaseQuery, JPH::NarrowPhaseQuery)
 OPAQUE_WRAPPER(JPC_TempAllocatorImpl, JPH::TempAllocatorImpl)
 OPAQUE_WRAPPER(JPC_TempAllocatorImpl, JPH::TempAllocatorImpl)
 DESTRUCTOR(JPC_TempAllocatorImpl)
 DESTRUCTOR(JPC_TempAllocatorImpl)
 
 
+OPAQUE_WRAPPER(JPC_JobSystem, JPH::JobSystem)
+DESTRUCTOR(JPC_JobSystem)
+
 OPAQUE_WRAPPER(JPC_JobSystemThreadPool, JPH::JobSystemThreadPool)
 OPAQUE_WRAPPER(JPC_JobSystemThreadPool, JPH::JobSystemThreadPool)
 DESTRUCTOR(JPC_JobSystemThreadPool)
 DESTRUCTOR(JPC_JobSystemThreadPool)
 
 
+OPAQUE_WRAPPER(JPC_JobSystemSingleThreaded, JPH::JobSystemSingleThreaded)
+DESTRUCTOR(JPC_JobSystemSingleThreaded)
+
 OPAQUE_WRAPPER(JPC_Shape, JPH::Shape)
 OPAQUE_WRAPPER(JPC_Shape, JPH::Shape)
 OPAQUE_WRAPPER(JPC_CompoundShape, JPH::CompoundShape)
 OPAQUE_WRAPPER(JPC_CompoundShape, JPH::CompoundShape)
 OPAQUE_WRAPPER(JPC_Body, JPH::Body)
 OPAQUE_WRAPPER(JPC_Body, JPH::Body)
@@ -365,6 +373,13 @@ JPC_API JPC_JobSystemThreadPool* JPC_JobSystemThreadPool_new3(
 	return to_jpc(new JPH::JobSystemThreadPool(inMaxJobs, inMaxBarriers, inNumThreads));
 	return to_jpc(new JPH::JobSystemThreadPool(inMaxJobs, inMaxBarriers, inNumThreads));
 }
 }
 
 
+////////////////////////////////////////////////////////////////////////////////
+// JobSystemSingleThreaded
+
+JPC_API JPC_JobSystemSingleThreaded* JPC_JobSystemSingleThreaded_new(uint inMaxJobs) {
+	return to_jpc(new JPH::JobSystemSingleThreaded(inMaxJobs));
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////////
 // CollisionGroup
 // CollisionGroup
 
 
@@ -2914,7 +2929,7 @@ JPC_API JPC_PhysicsUpdateError JPC_PhysicsSystem_Update(
 	float inDeltaTime,
 	float inDeltaTime,
 	int inCollisionSteps,
 	int inCollisionSteps,
 	JPC_TempAllocatorImpl *inTempAllocator,
 	JPC_TempAllocatorImpl *inTempAllocator,
-	JPC_JobSystemThreadPool *inJobSystem)
+	JPC_JobSystem *inJobSystem)
 {
 {
 	auto res = to_jph(self)->Update(
 	auto res = to_jph(self)->Update(
 		inDeltaTime,
 		inDeltaTime,