Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multithreaded Godot Engine Bullet processing. - 3.2 #40072

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/bullet/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ if env["builtin_bullet"]:
# env_bullet.Append(CPPDEFINES=['BT_DEBUG'])

env_bullet.Append(CPPDEFINES=["BT_USE_OLD_DAMPING_METHOD"])

env_bullet.Append(CPPDEFINES=['BT_THREADSAFE'])

env_thirdparty = env_bullet.Clone()
env_thirdparty.disable_warnings()
env_thirdparty.add_source_files(env.modules_sources, thirdparty_sources)
Expand Down
5 changes: 5 additions & 0 deletions modules/bullet/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
*/

#ifndef _3D_DISABLED
bool init_bt_task_scheduler() {
btITaskScheduler *godot_bullet_task_scheduler = btCreateDefaultTaskScheduler();
btSetTaskScheduler(godot_bullet_task_scheduler);
return true;
}
PhysicsServer *_createBulletPhysicsCallback() {
return memnew(BulletPhysicsServer);
}
Expand Down
25 changes: 14 additions & 11 deletions modules/bullet/space_bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "bullet_types_converter.h"
#include "bullet_utilities.h"
#include "constraint_bullet.h"
#include "core/os/os.h"
#include "core/project_settings.h"
#include "core/ustring.h"
#include "godot_collision_configuration.h"
Expand All @@ -48,6 +49,8 @@
#include <BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h>
#include <BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h>
#include <BulletCollision/NarrowPhaseCollision/btPointCollector.h>
#include <BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolverMt.h>
#include <BulletDynamics/Dynamics/btDiscreteDynamicsWorldMt.h>
#include <BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h>
#include <BulletSoftBody/btSoftRigidDynamicsWorld.h>
#include <btBulletDynamicsCommon.h>
Expand Down Expand Up @@ -597,44 +600,43 @@ btScalar calculateGodotCombinedFriction(const btCollisionObject *body0, const bt
}

void SpaceBullet::create_empty_world(bool p_create_soft_world) {

gjk_epa_pen_solver = bulletnew(btGjkEpaPenetrationDepthSolver);
gjk_simplex_solver = bulletnew(btVoronoiSimplexSolver);

void *world_mem;
if (p_create_soft_world) {
world_mem = malloc(sizeof(btSoftRigidDynamicsWorld));
} else {
world_mem = malloc(sizeof(btDiscreteDynamicsWorld));
world_mem = malloc(sizeof(btDiscreteDynamicsWorldMt));
}

ERR_FAIL_COND_MSG(!world_mem, "Out of memory.");

if (p_create_soft_world) {
collisionConfiguration = bulletnew(GodotSoftCollisionConfiguration(static_cast<btDiscreteDynamicsWorld *>(world_mem)));
} else {
collisionConfiguration = bulletnew(GodotCollisionConfiguration(static_cast<btDiscreteDynamicsWorld *>(world_mem)));
collisionConfiguration = bulletnew(GodotCollisionConfiguration(static_cast<btDiscreteDynamicsWorldMt *>(world_mem)));
}

dispatcher = bulletnew(GodotCollisionDispatcher(collisionConfiguration));
broadphase = bulletnew(btDbvtBroadphase);
solver = bulletnew(btSequentialImpulseConstraintSolver);
int32_t process_count = OS::get_singleton()->get_processor_count() - 2;
solver_pool = bulletnew(btConstraintSolverPoolMt(process_count));
solver_mutithread = bulletnew(btSequentialImpulseConstraintSolverMt);

if (p_create_soft_world) {
dynamicsWorld = new (world_mem) btSoftRigidDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
soft_body_world_info = bulletnew(btSoftBodyWorldInfo);
} else {
dynamicsWorld = new (world_mem) btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
}

dynamicsWorld = new (world_mem) btDiscreteDynamicsWorldMt(dispatcher, broadphase, solver_pool, solver_mutithread, collisionConfiguration);
}
dynamicsWorld->setWorldUserInfo(this);
ghostPairCallback = bulletnew(btGhostPairCallback);
godotFilterCallback = bulletnew(GodotFilterCallback);
gCalculateCombinedRestitutionCallback = &calculateGodotCombinedRestitution;
gCalculateCombinedFrictionCallback = &calculateGodotCombinedFriction;
gContactAddedCallback = &godotContactAddedCallback;

dynamicsWorld->setWorldUserInfo(this);

dynamicsWorld->setInternalTickCallback(onBulletPreTickCallback, this, true);
dynamicsWorld->setInternalTickCallback(onBulletTickCallback, this, false);
dynamicsWorld->getBroadphase()->getOverlappingPairCache()->setInternalGhostPairCallback(ghostPairCallback); // Setup ghost check
Expand All @@ -652,7 +654,6 @@ void SpaceBullet::create_empty_world(bool p_create_soft_world) {
void SpaceBullet::destroy_world() {

/// The world elements (like: Collision Objects, Constraints, Shapes) are managed by godot

dynamicsWorld->getBroadphase()->getOverlappingPairCache()->setInternalGhostPairCallback(NULL);
dynamicsWorld->getPairCache()->setOverlapFilterCallback(NULL);

Expand All @@ -663,8 +664,10 @@ void SpaceBullet::destroy_world() {
dynamicsWorld->~btDiscreteDynamicsWorld();
free(dynamicsWorld);
dynamicsWorld = NULL;

bulletdelete(solver);
bulletdelete(solver_pool);
bulletdelete(solver_mutithread);
bulletdelete(broadphase);
bulletdelete(dispatcher);
bulletdelete(collisionConfiguration);
Expand Down
30 changes: 18 additions & 12 deletions modules/bullet/space_bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@

#include <BulletCollision/BroadphaseCollision/btBroadphaseProxy.h>
#include <BulletCollision/BroadphaseCollision/btOverlappingPairCache.h>
#include <BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolverMt.h>
#include <BulletDynamics/Dynamics/btDiscreteDynamicsWorldMt.h>
#include <LinearMath/btScalar.h>
#include <LinearMath/btThreads.h>
#include <LinearMath/btTransform.h>
#include <LinearMath/btVector3.h>

Expand All @@ -53,7 +56,7 @@ class btCollisionDispatcher;
class btConstraintSolver;
class btDefaultCollisionConfiguration;
class btDynamicsWorld;
class btDiscreteDynamicsWorld;
class btDiscreteDynamicsWorldMt;
class btEmptyShape;
class btGhostPairCallback;
class btSoftRigidDynamicsWorld;
Expand Down Expand Up @@ -92,19 +95,22 @@ class SpaceBullet : public RIDBullet {
friend void onBulletTickCallback(btDynamicsWorld *world, btScalar timeStep);
friend class BulletPhysicsDirectSpaceState;

btBroadphaseInterface *broadphase;
btDefaultCollisionConfiguration *collisionConfiguration;
btCollisionDispatcher *dispatcher;
btConstraintSolver *solver;
btDiscreteDynamicsWorld *dynamicsWorld;
btSoftBodyWorldInfo *soft_body_world_info;
btGhostPairCallback *ghostPairCallback;
GodotFilterCallback *godotFilterCallback;
btBroadphaseInterface *broadphase = nullptr;
btDefaultCollisionConfiguration *collisionConfiguration = nullptr;
btCollisionDispatcher *dispatcher = nullptr;
btConstraintSolver *solver = nullptr;
btSequentialImpulseConstraintSolverMt *solver_mutithread = nullptr;
btConstraintSolverPoolMt *solver_pool = nullptr;
btDiscreteDynamicsWorld *dynamicsWorld = nullptr;
btSoftBodyWorldInfo *soft_body_world_info = nullptr;
btGhostPairCallback *ghostPairCallback = nullptr;
GodotFilterCallback *godotFilterCallback = nullptr;

btGjkEpaPenetrationDepthSolver *gjk_epa_pen_solver;
btVoronoiSimplexSolver *gjk_simplex_solver;

BulletPhysicsDirectSpaceState *direct_access;
btGjkEpaPenetrationDepthSolver *gjk_epa_pen_solver = nullptr;
btVoronoiSimplexSolver *gjk_simplex_solver = nullptr;

BulletPhysicsDirectSpaceState *direct_access = nullptr;
Vector3 gravityDirection;
real_t gravityMagnitude;

Expand Down