btCollisionDispatcherMt.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "btCollisionDispatcherMt.h"
  14. #include "LinearMath/btQuickprof.h"
  15. #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
  16. #include "BulletCollision/CollisionShapes/btCollisionShape.h"
  17. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  18. #include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h"
  19. #include "LinearMath/btPoolAllocator.h"
  20. #include "BulletCollision/CollisionDispatch/btCollisionConfiguration.h"
  21. #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
  22. btCollisionDispatcherMt::btCollisionDispatcherMt(btCollisionConfiguration* config, int grainSize)
  23. : btCollisionDispatcher(config)
  24. {
  25. m_batchManifoldsPtr.resize(btGetTaskScheduler()->getNumThreads());
  26. m_batchReleasePtr.resize(btGetTaskScheduler()->getNumThreads());
  27. m_batchUpdating = false;
  28. m_grainSize = grainSize; // iterations per task
  29. }
  30. btPersistentManifold* btCollisionDispatcherMt::getNewManifold(const btCollisionObject* body0, const btCollisionObject* body1)
  31. {
  32. //optional relative contact breaking threshold, turned on by default (use setDispatcherFlags to switch off feature for improved performance)
  33. btScalar contactBreakingThreshold = (m_dispatcherFlags & btCollisionDispatcher::CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD) ? btMin(body0->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold), body1->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold))
  34. : gContactBreakingThreshold;
  35. btScalar contactProcessingThreshold = btMin(body0->getContactProcessingThreshold(), body1->getContactProcessingThreshold());
  36. void* mem = m_persistentManifoldPoolAllocator->allocate(sizeof(btPersistentManifold));
  37. if (NULL == mem)
  38. {
  39. //we got a pool memory overflow, by default we fallback to dynamically allocate memory. If we require a contiguous contact pool then assert.
  40. if ((m_dispatcherFlags & CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION) == 0)
  41. {
  42. mem = btAlignedAlloc(sizeof(btPersistentManifold), 16);
  43. }
  44. else
  45. {
  46. btAssert(0);
  47. //make sure to increase the m_defaultMaxPersistentManifoldPoolSize in the btDefaultCollisionConstructionInfo/btDefaultCollisionConfiguration
  48. return 0;
  49. }
  50. }
  51. btPersistentManifold* manifold = new (mem) btPersistentManifold(body0, body1, 0, contactBreakingThreshold, contactProcessingThreshold);
  52. if (!m_batchUpdating)
  53. {
  54. // batch updater will update manifold pointers array after finishing, so
  55. // only need to update array when not batch-updating
  56. //btAssert( !btThreadsAreRunning() );
  57. manifold->m_index1a = m_manifoldsPtr.size();
  58. m_manifoldsPtr.push_back(manifold);
  59. }
  60. else
  61. {
  62. m_batchManifoldsPtr[btGetCurrentThreadIndex()].push_back(manifold);
  63. }
  64. return manifold;
  65. }
  66. void btCollisionDispatcherMt::releaseManifold(btPersistentManifold* manifold)
  67. {
  68. //btAssert( !btThreadsAreRunning() );
  69. if (!m_batchUpdating)
  70. {
  71. clearManifold(manifold);
  72. // batch updater will update manifold pointers array after finishing, so
  73. // only need to update array when not batch-updating
  74. int findIndex = manifold->m_index1a;
  75. btAssert(findIndex < m_manifoldsPtr.size());
  76. m_manifoldsPtr.swap(findIndex, m_manifoldsPtr.size() - 1);
  77. m_manifoldsPtr[findIndex]->m_index1a = findIndex;
  78. m_manifoldsPtr.pop_back();
  79. } else {
  80. m_batchReleasePtr[btGetCurrentThreadIndex()].push_back(manifold);
  81. return;
  82. }
  83. manifold->~btPersistentManifold();
  84. if (m_persistentManifoldPoolAllocator->validPtr(manifold))
  85. {
  86. m_persistentManifoldPoolAllocator->freeMemory(manifold);
  87. }
  88. else
  89. {
  90. btAlignedFree(manifold);
  91. }
  92. }
  93. struct CollisionDispatcherUpdater : public btIParallelForBody
  94. {
  95. btBroadphasePair* mPairArray;
  96. btNearCallback mCallback;
  97. btCollisionDispatcher* mDispatcher;
  98. const btDispatcherInfo* mInfo;
  99. CollisionDispatcherUpdater()
  100. {
  101. mPairArray = NULL;
  102. mCallback = NULL;
  103. mDispatcher = NULL;
  104. mInfo = NULL;
  105. }
  106. void forLoop(int iBegin, int iEnd) const
  107. {
  108. for (int i = iBegin; i < iEnd; ++i)
  109. {
  110. btBroadphasePair* pair = &mPairArray[i];
  111. mCallback(*pair, *mDispatcher, *mInfo);
  112. }
  113. }
  114. };
  115. void btCollisionDispatcherMt::dispatchAllCollisionPairs(btOverlappingPairCache* pairCache, const btDispatcherInfo& info, btDispatcher* dispatcher)
  116. {
  117. const int pairCount = pairCache->getNumOverlappingPairs();
  118. if (pairCount == 0)
  119. {
  120. return;
  121. }
  122. CollisionDispatcherUpdater updater;
  123. updater.mCallback = getNearCallback();
  124. updater.mPairArray = pairCache->getOverlappingPairArrayPtr();
  125. updater.mDispatcher = this;
  126. updater.mInfo = &info;
  127. m_batchUpdating = true;
  128. btParallelFor(0, pairCount, m_grainSize, updater);
  129. m_batchUpdating = false;
  130. // merge new manifolds, if any
  131. for (int i = 0; i < m_batchManifoldsPtr.size(); ++i)
  132. {
  133. btAlignedObjectArray<btPersistentManifold*>& batchManifoldsPtr = m_batchManifoldsPtr[i];
  134. for (int j = 0; j < batchManifoldsPtr.size(); ++j)
  135. {
  136. m_manifoldsPtr.push_back(batchManifoldsPtr[j]);
  137. }
  138. batchManifoldsPtr.resizeNoInitialize(0);
  139. }
  140. // remove batched remove manifolds.
  141. for (int i = 0; i < m_batchReleasePtr.size(); ++i)
  142. {
  143. btAlignedObjectArray<btPersistentManifold*>& batchManifoldsPtr = m_batchReleasePtr[i];
  144. for (int j = 0; j < batchManifoldsPtr.size(); ++j)
  145. {
  146. releaseManifold(batchManifoldsPtr[j]);
  147. }
  148. batchManifoldsPtr.resizeNoInitialize(0);
  149. }
  150. // update the indices (used when releasing manifolds)
  151. for (int i = 0; i < m_manifoldsPtr.size(); ++i)
  152. {
  153. m_manifoldsPtr[i]->m_index1a = i;
  154. }
  155. }