Blender  V3.3
btCollisionDispatcherMt.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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 
17 #include "LinearMath/btQuickprof.h"
18 
20 
27 
29  : btCollisionDispatcher(config)
30 {
31  m_batchManifoldsPtr.resize(btGetTaskScheduler()->getNumThreads());
32  m_batchUpdating = false;
33  m_grainSize = grainSize; // iterations per task
34 }
35 
37 {
38  //optional relative contact breaking threshold, turned on by default (use setDispatcherFlags to switch off feature for improved performance)
39 
40  btScalar contactBreakingThreshold = (m_dispatcherFlags & btCollisionDispatcher::CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD) ? btMin(body0->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold), body1->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold))
42 
43  btScalar contactProcessingThreshold = btMin(body0->getContactProcessingThreshold(), body1->getContactProcessingThreshold());
44 
46  if (NULL == mem)
47  {
48  //we got a pool memory overflow, by default we fallback to dynamically allocate memory. If we require a contiguous contact pool then assert.
50  {
51  mem = btAlignedAlloc(sizeof(btPersistentManifold), 16);
52  }
53  else
54  {
55  btAssert(0);
56  //make sure to increase the m_defaultMaxPersistentManifoldPoolSize in the btDefaultCollisionConstructionInfo/btDefaultCollisionConfiguration
57  return 0;
58  }
59  }
60  btPersistentManifold* manifold = new (mem) btPersistentManifold(body0, body1, 0, contactBreakingThreshold, contactProcessingThreshold);
61  if (!m_batchUpdating)
62  {
63  // batch updater will update manifold pointers array after finishing, so
64  // only need to update array when not batch-updating
65  //btAssert( !btThreadsAreRunning() );
66  manifold->m_index1a = m_manifoldsPtr.size();
67  m_manifoldsPtr.push_back(manifold);
68  }
69  else
70  {
72  }
73 
74  return manifold;
75 }
76 
78 {
79  clearManifold(manifold);
80  //btAssert( !btThreadsAreRunning() );
81  if (!m_batchUpdating)
82  {
83  // batch updater will update manifold pointers array after finishing, so
84  // only need to update array when not batch-updating
85  int findIndex = manifold->m_index1a;
86  btAssert(findIndex < m_manifoldsPtr.size());
87  m_manifoldsPtr.swap(findIndex, m_manifoldsPtr.size() - 1);
88  m_manifoldsPtr[findIndex]->m_index1a = findIndex;
90  }
91 
92  manifold->~btPersistentManifold();
94  {
96  }
97  else
98  {
99  btAlignedFree(manifold);
100  }
101 }
102 
104 {
109 
111  {
112  mPairArray = NULL;
113  mCallback = NULL;
114  mDispatcher = NULL;
115  mInfo = NULL;
116  }
117  void forLoop(int iBegin, int iEnd) const
118  {
119  for (int i = iBegin; i < iEnd; ++i)
120  {
121  btBroadphasePair* pair = &mPairArray[i];
122  mCallback(*pair, *mDispatcher, *mInfo);
123  }
124  }
125 };
126 
128 {
129  const int pairCount = pairCache->getNumOverlappingPairs();
130  if (pairCount == 0)
131  {
132  return;
133  }
135  updater.mCallback = getNearCallback();
136  updater.mPairArray = pairCache->getOverlappingPairArrayPtr();
137  updater.mDispatcher = this;
138  updater.mInfo = &info;
139 
140  m_batchUpdating = true;
141  btParallelFor(0, pairCount, m_grainSize, updater);
142  m_batchUpdating = false;
143 
144  // merge new manifolds, if any
145  for (int i = 0; i < m_batchManifoldsPtr.size(); ++i)
146  {
148 
149  for (int j = 0; j < batchManifoldsPtr.size(); ++j)
150  {
151  m_manifoldsPtr.push_back(batchManifoldsPtr[j]);
152  }
153 
154  batchManifoldsPtr.resizeNoInitialize(0);
155  }
156 
157  // update the indices (used when releasing manifolds)
158  for (int i = 0; i < m_manifoldsPtr.size(); ++i)
159  {
160  m_manifoldsPtr[i]->m_index1a = i;
161  }
162 }
#define btAlignedFree(ptr)
#define btAlignedAlloc(size, alignment)
btBroadphasePair
void(* btNearCallback)(btBroadphasePair &collisionPair, btCollisionDispatcher &dispatcher, const btDispatcherInfo &dispatchInfo)
user can override this nearcallback for collision filtering and more finegrained control over collisi...
btCollisionObject
btScalar gContactBreakingThreshold
SIMD_FORCE_INLINE const T & btMin(const T &a, const T &b)
Definition: btMinMax.h:21
btPersistentManifold()
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
#define btAssert(x)
Definition: btScalar.h:295
btITaskScheduler * btGetTaskScheduler()
Definition: btThreads.cpp:407
unsigned int btGetCurrentThreadIndex()
Definition: btThreads.cpp:290
void btParallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)
Definition: btThreads.cpp:412
SIMD_FORCE_INLINE void resizeNoInitialize(int newsize)
SIMD_FORCE_INLINE int size() const
return the number of elements in the array
void swap(int index0, int index1)
SIMD_FORCE_INLINE void pop_back()
SIMD_FORCE_INLINE void resize(int newsize, const T &fillData=T())
SIMD_FORCE_INLINE void push_back(const T &_Val)
virtual btPersistentManifold * getNewManifold(const btCollisionObject *body0, const btCollisionObject *body1) BT_OVERRIDE
btAlignedObjectArray< btAlignedObjectArray< btPersistentManifold * > > m_batchManifoldsPtr
virtual void dispatchAllCollisionPairs(btOverlappingPairCache *pairCache, const btDispatcherInfo &info, btDispatcher *dispatcher) BT_OVERRIDE
btCollisionDispatcherMt(btCollisionConfiguration *config, int grainSize=40)
virtual void releaseManifold(btPersistentManifold *manifold) BT_OVERRIDE
btNearCallback getNearCallback() const
virtual void clearManifold(btPersistentManifold *manifold)
btAlignedObjectArray< btPersistentManifold * > m_manifoldsPtr
btPoolAllocator * m_persistentManifoldPoolAllocator
virtual int getNumOverlappingPairs() const =0
virtual btBroadphasePair * getOverlappingPairArrayPtr()=0
void freeMemory(void *ptr)
bool validPtr(void *ptr)
void * allocate(int size)
void forLoop(int iBegin, int iEnd) const
btCollisionDispatcher * mDispatcher