SpuGatheringCollisionDispatcher.cpp 10.0 KB

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