Add Bullet and Chipmunk to dali-toolkit
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / BulletDynamics / Dynamics / btSimulationIslandManagerMt.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  https://bulletphysics.org
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 #ifndef BT_SIMULATION_ISLAND_MANAGER_MT_H
17 #define BT_SIMULATION_ISLAND_MANAGER_MT_H
18
19 #include "BulletCollision/CollisionDispatch/btSimulationIslandManager.h"
20
21 class btTypedConstraint;
22 class btConstraintSolver;
23 struct btContactSolverInfo;
24 class btIDebugDraw;
25
26 ///
27 /// SimulationIslandManagerMt -- Multithread capable version of SimulationIslandManager
28 ///                       Splits the world up into islands which can be solved in parallel.
29 ///                       In order to solve islands in parallel, an IslandDispatch function
30 ///                       must be provided which will dispatch calls to multiple threads.
31 ///                       The amount of parallelism that can be achieved depends on the number
32 ///                       of islands. If only a single island exists, then no parallelism is
33 ///                       possible.
34 ///
35 class btSimulationIslandManagerMt : public btSimulationIslandManager
36 {
37 public:
38         struct Island
39         {
40                 // a simulation island consisting of bodies, manifolds and constraints,
41                 // to be passed into a constraint solver.
42                 btAlignedObjectArray<btCollisionObject*> bodyArray;
43                 btAlignedObjectArray<btPersistentManifold*> manifoldArray;
44                 btAlignedObjectArray<btTypedConstraint*> constraintArray;
45                 int id;  // island id
46                 bool isSleeping;
47
48                 void append(const Island& other);  // add bodies, manifolds, constraints to my own
49         };
50         struct SolverParams
51         {
52                 btConstraintSolver* m_solverPool;
53                 btConstraintSolver* m_solverMt;
54                 btContactSolverInfo* m_solverInfo;
55                 btIDebugDraw* m_debugDrawer;
56                 btDispatcher* m_dispatcher;
57         };
58         static void solveIsland(btConstraintSolver* solver, Island& island, const SolverParams& solverParams);
59
60         typedef void (*IslandDispatchFunc)(btAlignedObjectArray<Island*>* islands, const SolverParams& solverParams);
61         static void serialIslandDispatch(btAlignedObjectArray<Island*>* islandsPtr, const SolverParams& solverParams);
62         static void parallelIslandDispatch(btAlignedObjectArray<Island*>* islandsPtr, const SolverParams& solverParams);
63
64 protected:
65         btAlignedObjectArray<Island*> m_allocatedIslands;    // owner of all Islands
66         btAlignedObjectArray<Island*> m_activeIslands;       // islands actively in use
67         btAlignedObjectArray<Island*> m_freeIslands;         // islands ready to be reused
68         btAlignedObjectArray<Island*> m_lookupIslandFromId;  // big lookup table to map islandId to Island pointer
69         Island* m_batchIsland;
70         int m_minimumSolverBatchSize;
71         int m_batchIslandMinBodyCount;
72         IslandDispatchFunc m_islandDispatch;
73
74         Island* getIsland(int id);
75         virtual Island* allocateIsland(int id, int numBodies);
76         virtual void initIslandPools();
77         virtual void addBodiesToIslands(btCollisionWorld* collisionWorld);
78         virtual void addManifoldsToIslands(btDispatcher* dispatcher);
79         virtual void addConstraintsToIslands(btAlignedObjectArray<btTypedConstraint*>& constraints);
80         virtual void mergeIslands();
81
82 public:
83         btSimulationIslandManagerMt();
84         virtual ~btSimulationIslandManagerMt();
85
86         virtual void buildAndProcessIslands(btDispatcher* dispatcher,
87                                                                                 btCollisionWorld* collisionWorld,
88                                                                                 btAlignedObjectArray<btTypedConstraint*>& constraints,
89                                                                                 const SolverParams& solverParams);
90
91         virtual void buildIslands(btDispatcher* dispatcher, btCollisionWorld* colWorld);
92
93         int getMinimumSolverBatchSize() const
94         {
95                 return m_minimumSolverBatchSize;
96         }
97         void setMinimumSolverBatchSize(int sz)
98         {
99                 m_minimumSolverBatchSize = sz;
100         }
101         IslandDispatchFunc getIslandDispatchFunction() const
102         {
103                 return m_islandDispatch;
104         }
105         // allow users to set their own dispatch function for multithreaded dispatch
106         void setIslandDispatchFunction(IslandDispatchFunc func)
107         {
108                 m_islandDispatch = func;
109         }
110 };
111
112 #endif  //BT_SIMULATION_ISLAND_MANAGER_H