btCollisionDispatcherMt.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  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_batchUpdating = false;
  27. m_grainSize = grainSize; // iterations per task
  28. }
  29. btPersistentManifold* btCollisionDispatcherMt::getNewManifold(const btCollisionObject* body0, const btCollisionObject* body1)
  30. {
  31. //optional relative contact breaking threshold, turned on by default (use setDispatcherFlags to switch off feature for improved performance)
  32. btScalar contactBreakingThreshold = (m_dispatcherFlags & btCollisionDispatcher::CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD) ? btMin(body0->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold), body1->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold))
  33. : gContactBreakingThreshold;
  34. btScalar contactProcessingThreshold = btMin(body0->getContactProcessingThreshold(), body1->getContactProcessingThreshold());
  35. void* mem = m_persistentManifoldPoolAllocator->allocate(sizeof(btPersistentManifold));
  36. if (NULL == mem)
  37. {
  38. //we got a pool memory overflow, by default we fallback to dynamically allocate memory. If we require a contiguous contact pool then assert.
  39. if ((m_dispatcherFlags & CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION) == 0)
  40. {
  41. mem = btAlignedAlloc(sizeof(btPersistentManifold), 16);
  42. }
  43. else
  44. {
  45. btAssert(0);
  46. //make sure to increase the m_defaultMaxPersistentManifoldPoolSize in the btDefaultCollisionConstructionInfo/btDefaultCollisionConfiguration
  47. return 0;
  48. }
  49. }
  50. btPersistentManifold* manifold = new (mem) btPersistentManifold(body0, body1, 0, contactBreakingThreshold, contactProcessingThreshold);
  51. if (!m_batchUpdating)
  52. {
  53. // batch updater will update manifold pointers array after finishing, so
  54. // only need to update array when not batch-updating
  55. //btAssert( !btThreadsAreRunning() );
  56. manifold->m_index1a = m_manifoldsPtr.size();
  57. m_manifoldsPtr.push_back(manifold);
  58. }
  59. else
  60. {
  61. m_batchManifoldsPtr[btGetCurrentThreadIndex()].push_back(manifold);
  62. }
  63. return manifold;
  64. }
  65. void btCollisionDispatcherMt::releaseManifold(btPersistentManifold* manifold)
  66. {
  67. clearManifold(manifold);
  68. //btAssert( !btThreadsAreRunning() );
  69. if (!m_batchUpdating)
  70. {
  71. // batch updater will update manifold pointers array after finishing, so
  72. // only need to update array when not batch-updating
  73. int findIndex = manifold->m_index1a;
  74. btAssert(findIndex < m_manifoldsPtr.size());
  75. m_manifoldsPtr.swap(findIndex, m_manifoldsPtr.size() - 1);
  76. m_manifoldsPtr[findIndex]->m_index1a = findIndex;
  77. m_manifoldsPtr.pop_back();
  78. }
  79. manifold->~btPersistentManifold();
  80. if (m_persistentManifoldPoolAllocator->validPtr(manifold))
  81. {
  82. m_persistentManifoldPoolAllocator->freeMemory(manifold);
  83. }
  84. else
  85. {
  86. btAlignedFree(manifold);
  87. }
  88. }
  89. struct CollisionDispatcherUpdater : public btIParallelForBody
  90. {
  91. btBroadphasePair* mPairArray;
  92. btNearCallback mCallback;
  93. btCollisionDispatcher* mDispatcher;
  94. const btDispatcherInfo* mInfo;
  95. CollisionDispatcherUpdater()
  96. {
  97. mPairArray = NULL;
  98. mCallback = NULL;
  99. mDispatcher = NULL;
  100. mInfo = NULL;
  101. }
  102. void forLoop(int iBegin, int iEnd) const
  103. {
  104. for (int i = iBegin; i < iEnd; ++i)
  105. {
  106. btBroadphasePair* pair = &mPairArray[i];
  107. mCallback(*pair, *mDispatcher, *mInfo);
  108. }
  109. }
  110. };
  111. void btCollisionDispatcherMt::dispatchAllCollisionPairs(btOverlappingPairCache* pairCache, const btDispatcherInfo& info, btDispatcher* dispatcher)
  112. {
  113. const int pairCount = pairCache->getNumOverlappingPairs();
  114. if (pairCount == 0)
  115. {
  116. return;
  117. }
  118. CollisionDispatcherUpdater updater;
  119. updater.mCallback = getNearCallback();
  120. updater.mPairArray = pairCache->getOverlappingPairArrayPtr();
  121. updater.mDispatcher = this;
  122. updater.mInfo = &info;
  123. m_batchUpdating = true;
  124. btParallelFor(0, pairCount, m_grainSize, updater);
  125. m_batchUpdating = false;
  126. // merge new manifolds, if any
  127. for (int i = 0; i < m_batchManifoldsPtr.size(); ++i)
  128. {
  129. btAlignedObjectArray<btPersistentManifold*>& batchManifoldsPtr = m_batchManifoldsPtr[i];
  130. for (int j = 0; j < batchManifoldsPtr.size(); ++j)
  131. {
  132. m_manifoldsPtr.push_back(batchManifoldsPtr[j]);
  133. }
  134. batchManifoldsPtr.resizeNoInitialize(0);
  135. }
  136. // update the indices (used when releasing manifolds)
  137. for (int i = 0; i < m_manifoldsPtr.size(); ++i)
  138. {
  139. m_manifoldsPtr[i]->m_index1a = i;
  140. }
  141. }