You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

277 regels
9.4 KiB

  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
  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 "SpuGatheringCollisionDispatcher.h"
  14. #include "SpuCollisionTaskProcess.h"
  15. #include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h"
  16. #include "BulletCollision/CollisionDispatch/btEmptyCollisionAlgorithm.h"
  17. #include "SpuContactManifoldCollisionAlgorithm.h"
  18. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  19. #include "BulletCollision/CollisionShapes/btCollisionShape.h"
  20. #include "LinearMath/btQuickprof.h"
  21. #include "BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuCollisionShapes.h"
  22. SpuGatheringCollisionDispatcher::SpuGatheringCollisionDispatcher(class btThreadSupportInterface* threadInterface, unsigned int maxNumOutstandingTasks,btCollisionConfiguration* collisionConfiguration)
  23. :btCollisionDispatcher(collisionConfiguration),
  24. m_spuCollisionTaskProcess(0),
  25. m_threadInterface(threadInterface),
  26. m_maxNumOutstandingTasks(maxNumOutstandingTasks)
  27. {
  28. }
  29. bool SpuGatheringCollisionDispatcher::supportsDispatchPairOnSpu(int proxyType0,int proxyType1)
  30. {
  31. bool supported0 = (
  32. (proxyType0 == BOX_SHAPE_PROXYTYPE) ||
  33. (proxyType0 == TRIANGLE_SHAPE_PROXYTYPE) ||
  34. (proxyType0 == SPHERE_SHAPE_PROXYTYPE) ||
  35. (proxyType0 == CAPSULE_SHAPE_PROXYTYPE) ||
  36. (proxyType0 == CYLINDER_SHAPE_PROXYTYPE) ||
  37. // (proxyType0 == CONE_SHAPE_PROXYTYPE) ||
  38. (proxyType0 == TRIANGLE_MESH_SHAPE_PROXYTYPE) ||
  39. (proxyType0 == CONVEX_HULL_SHAPE_PROXYTYPE)||
  40. (proxyType0 == STATIC_PLANE_PROXYTYPE)||
  41. (proxyType0 == COMPOUND_SHAPE_PROXYTYPE)
  42. );
  43. bool supported1 = (
  44. (proxyType1 == BOX_SHAPE_PROXYTYPE) ||
  45. (proxyType1 == TRIANGLE_SHAPE_PROXYTYPE) ||
  46. (proxyType1 == SPHERE_SHAPE_PROXYTYPE) ||
  47. (proxyType1 == CAPSULE_SHAPE_PROXYTYPE) ||
  48. (proxyType1 == CYLINDER_SHAPE_PROXYTYPE) ||
  49. // (proxyType1 == CONE_SHAPE_PROXYTYPE) ||
  50. (proxyType1 == TRIANGLE_MESH_SHAPE_PROXYTYPE) ||
  51. (proxyType1 == CONVEX_HULL_SHAPE_PROXYTYPE) ||
  52. (proxyType1 == STATIC_PLANE_PROXYTYPE) ||
  53. (proxyType1 == COMPOUND_SHAPE_PROXYTYPE)
  54. );
  55. return supported0 && supported1;
  56. }
  57. SpuGatheringCollisionDispatcher::~SpuGatheringCollisionDispatcher()
  58. {
  59. if (m_spuCollisionTaskProcess)
  60. delete m_spuCollisionTaskProcess;
  61. }
  62. #include "stdio.h"
  63. ///interface for iterating all overlapping collision pairs, no matter how those pairs are stored (array, set, map etc)
  64. ///this is useful for the collision dispatcher.
  65. class btSpuCollisionPairCallback : public btOverlapCallback
  66. {
  67. const btDispatcherInfo& m_dispatchInfo;
  68. SpuGatheringCollisionDispatcher* m_dispatcher;
  69. public:
  70. btSpuCollisionPairCallback(const btDispatcherInfo& dispatchInfo, SpuGatheringCollisionDispatcher* dispatcher)
  71. :m_dispatchInfo(dispatchInfo),
  72. m_dispatcher(dispatcher)
  73. {
  74. }
  75. virtual bool processOverlap(btBroadphasePair& collisionPair)
  76. {
  77. //PPU version
  78. //(*m_dispatcher->getNearCallback())(collisionPair,*m_dispatcher,m_dispatchInfo);
  79. //only support discrete collision detection for now, we could fallback on PPU/unoptimized version for TOI/CCD
  80. btAssert(m_dispatchInfo.m_dispatchFunc == btDispatcherInfo::DISPATCH_DISCRETE);
  81. //by default, Bullet will use this near callback
  82. {
  83. ///userInfo is used to determine if the SPU has to handle this case or not (skip PPU tasks)
  84. if (!collisionPair.m_internalTmpValue)
  85. {
  86. collisionPair.m_internalTmpValue = 1;
  87. }
  88. if (!collisionPair.m_algorithm)
  89. {
  90. btCollisionObject* colObj0 = (btCollisionObject*)collisionPair.m_pProxy0->m_clientObject;
  91. btCollisionObject* colObj1 = (btCollisionObject*)collisionPair.m_pProxy1->m_clientObject;
  92. btCollisionAlgorithmConstructionInfo ci;
  93. ci.m_dispatcher1 = m_dispatcher;
  94. ci.m_manifold = 0;
  95. if (m_dispatcher->needsCollision(colObj0,colObj1))
  96. {
  97. int proxyType0 = colObj0->getCollisionShape()->getShapeType();
  98. int proxyType1 = colObj1->getCollisionShape()->getShapeType();
  99. bool supportsSpuDispatch = m_dispatcher->supportsDispatchPairOnSpu(proxyType0,proxyType1)
  100. && ((colObj0->getCollisionFlags() & btCollisionObject::CF_DISABLE_SPU_COLLISION_PROCESSING) == 0)
  101. && ((colObj1->getCollisionFlags() & btCollisionObject::CF_DISABLE_SPU_COLLISION_PROCESSING) == 0);
  102. if (proxyType0 == COMPOUND_SHAPE_PROXYTYPE)
  103. {
  104. btCompoundShape* compound = (btCompoundShape*)colObj0->getCollisionShape();
  105. if (compound->getNumChildShapes()>MAX_SPU_COMPOUND_SUBSHAPES)
  106. {
  107. //printf("PPU fallback, compound->getNumChildShapes(%d)>%d\n",compound->getNumChildShapes(),MAX_SPU_COMPOUND_SUBSHAPES);
  108. supportsSpuDispatch = false;
  109. }
  110. }
  111. if (proxyType1 == COMPOUND_SHAPE_PROXYTYPE)
  112. {
  113. btCompoundShape* compound = (btCompoundShape*)colObj1->getCollisionShape();
  114. if (compound->getNumChildShapes()>MAX_SPU_COMPOUND_SUBSHAPES)
  115. {
  116. //printf("PPU fallback, compound->getNumChildShapes(%d)>%d\n",compound->getNumChildShapes(),MAX_SPU_COMPOUND_SUBSHAPES);
  117. supportsSpuDispatch = false;
  118. }
  119. }
  120. if (supportsSpuDispatch)
  121. {
  122. int so = sizeof(SpuContactManifoldCollisionAlgorithm);
  123. #ifdef ALLOCATE_SEPARATELY
  124. void* mem = btAlignedAlloc(so,16);//m_dispatcher->allocateCollisionAlgorithm(so);
  125. #else
  126. void* mem = m_dispatcher->allocateCollisionAlgorithm(so);
  127. #endif
  128. collisionPair.m_algorithm = new(mem) SpuContactManifoldCollisionAlgorithm(ci,colObj0,colObj1);
  129. collisionPair.m_internalTmpValue = 2;
  130. } else
  131. {
  132. collisionPair.m_algorithm = m_dispatcher->findAlgorithm(colObj0,colObj1);
  133. collisionPair.m_internalTmpValue = 3;
  134. }
  135. }
  136. }
  137. }
  138. return false;
  139. }
  140. };
  141. void SpuGatheringCollisionDispatcher::dispatchAllCollisionPairs(btOverlappingPairCache* pairCache,const btDispatcherInfo& dispatchInfo, btDispatcher* dispatcher)
  142. {
  143. if (dispatchInfo.m_enableSPU)
  144. {
  145. m_maxNumOutstandingTasks = m_threadInterface->getNumTasks();
  146. {
  147. BT_PROFILE("processAllOverlappingPairs");
  148. if (!m_spuCollisionTaskProcess)
  149. m_spuCollisionTaskProcess = new SpuCollisionTaskProcess(m_threadInterface,m_maxNumOutstandingTasks);
  150. m_spuCollisionTaskProcess->setNumTasks(m_maxNumOutstandingTasks);
  151. // printf("m_maxNumOutstandingTasks =%d\n",m_maxNumOutstandingTasks);
  152. m_spuCollisionTaskProcess->initialize2(dispatchInfo.m_useEpa);
  153. ///modified version of btCollisionDispatcher::dispatchAllCollisionPairs:
  154. {
  155. btSpuCollisionPairCallback collisionCallback(dispatchInfo,this);
  156. pairCache->processAllOverlappingPairs(&collisionCallback,dispatcher);
  157. }
  158. }
  159. //send one big batch
  160. int numTotalPairs = pairCache->getNumOverlappingPairs();
  161. if (numTotalPairs)
  162. {
  163. btBroadphasePair* pairPtr = pairCache->getOverlappingPairArrayPtr();
  164. int i;
  165. {
  166. int pairRange = SPU_BATCHSIZE_BROADPHASE_PAIRS;
  167. if (numTotalPairs < (m_spuCollisionTaskProcess->getNumTasks()*SPU_BATCHSIZE_BROADPHASE_PAIRS))
  168. {
  169. pairRange = (numTotalPairs/m_spuCollisionTaskProcess->getNumTasks())+1;
  170. }
  171. BT_PROFILE("addWorkToTask");
  172. for (i=0;i<numTotalPairs;)
  173. {
  174. //Performance Hint: tweak this number during benchmarking
  175. int endIndex = (i+pairRange) < numTotalPairs ? i+pairRange : numTotalPairs;
  176. m_spuCollisionTaskProcess->addWorkToTask(pairPtr,i,endIndex);
  177. i = endIndex;
  178. }
  179. }
  180. {
  181. BT_PROFILE("PPU fallback");
  182. //handle PPU fallback pairs
  183. for (i=0;i<numTotalPairs;i++)
  184. {
  185. btBroadphasePair& collisionPair = pairPtr[i];
  186. if (collisionPair.m_internalTmpValue == 3)
  187. {
  188. if (collisionPair.m_algorithm)
  189. {
  190. btCollisionObject* colObj0 = (btCollisionObject*)collisionPair.m_pProxy0->m_clientObject;
  191. btCollisionObject* colObj1 = (btCollisionObject*)collisionPair.m_pProxy1->m_clientObject;
  192. if (dispatcher->needsCollision(colObj0,colObj1))
  193. {
  194. btManifoldResult contactPointResult(colObj0,colObj1);
  195. if (dispatchInfo.m_dispatchFunc == btDispatcherInfo::DISPATCH_DISCRETE)
  196. {
  197. //discrete collision detection query
  198. collisionPair.m_algorithm->processCollision(colObj0,colObj1,dispatchInfo,&contactPointResult);
  199. } else
  200. {
  201. //continuous collision detection query, time of impact (toi)
  202. btScalar toi = collisionPair.m_algorithm->calculateTimeOfImpact(colObj0,colObj1,dispatchInfo,&contactPointResult);
  203. if (dispatchInfo.m_timeOfImpact > toi)
  204. dispatchInfo.m_timeOfImpact = toi;
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. {
  213. BT_PROFILE("flush2");
  214. //make sure all SPU work is done
  215. m_spuCollisionTaskProcess->flush2();
  216. }
  217. } else
  218. {
  219. ///PPU fallback
  220. ///!Need to make sure to clear all 'algorithms' when switching between SPU and PPU
  221. btCollisionDispatcher::dispatchAllCollisionPairs(pairCache,dispatchInfo,dispatcher);
  222. }
  223. }