SpuCollisionShapes.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 "SpuCollisionShapes.h"
  14. ///not supported on IBM SDK, until we fix the alignment of btVector3
  15. #if defined (__CELLOS_LV2__) && defined (__SPU__)
  16. #include <spu_intrinsics.h>
  17. static inline vec_float4 vec_dot3( vec_float4 vec0, vec_float4 vec1 )
  18. {
  19. vec_float4 result;
  20. result = spu_mul( vec0, vec1 );
  21. result = spu_madd( spu_rlqwbyte( vec0, 4 ), spu_rlqwbyte( vec1, 4 ), result );
  22. return spu_madd( spu_rlqwbyte( vec0, 8 ), spu_rlqwbyte( vec1, 8 ), result );
  23. }
  24. #endif //__SPU__
  25. void computeAabb (btVector3& aabbMin, btVector3& aabbMax, btConvexInternalShape* convexShape, ppu_address_t convexShapePtr, int shapeType, const btTransform& xform)
  26. {
  27. //calculate the aabb, given the types...
  28. switch (shapeType)
  29. {
  30. case CYLINDER_SHAPE_PROXYTYPE:
  31. /* fall through */
  32. case BOX_SHAPE_PROXYTYPE:
  33. {
  34. btScalar margin=convexShape->getMarginNV();
  35. btVector3 halfExtents = convexShape->getImplicitShapeDimensions();
  36. halfExtents += btVector3(margin,margin,margin);
  37. const btTransform& t = xform;
  38. btMatrix3x3 abs_b = t.getBasis().absolute();
  39. btVector3 center = t.getOrigin();
  40. btVector3 extent = halfExtents.dot3( abs_b[0], abs_b[1], abs_b[2] );
  41. aabbMin = center - extent;
  42. aabbMax = center + extent;
  43. break;
  44. }
  45. case CAPSULE_SHAPE_PROXYTYPE:
  46. {
  47. btScalar margin=convexShape->getMarginNV();
  48. btVector3 halfExtents = convexShape->getImplicitShapeDimensions();
  49. //add the radius to y-axis to get full height
  50. btScalar radius = halfExtents[0];
  51. halfExtents[1] += radius;
  52. halfExtents += btVector3(margin,margin,margin);
  53. #if 0
  54. int capsuleUpAxis = convexShape->getUpAxis();
  55. btScalar halfHeight = convexShape->getHalfHeight();
  56. btScalar radius = convexShape->getRadius();
  57. halfExtents[capsuleUpAxis] = radius + halfHeight;
  58. #endif
  59. const btTransform& t = xform;
  60. btMatrix3x3 abs_b = t.getBasis().absolute();
  61. btVector3 center = t.getOrigin();
  62. btVector3 extent = halfExtents.dot3( abs_b[0], abs_b[1], abs_b[2] );
  63. aabbMin = center - extent;
  64. aabbMax = center + extent;
  65. break;
  66. }
  67. case SPHERE_SHAPE_PROXYTYPE:
  68. {
  69. btScalar radius = convexShape->getImplicitShapeDimensions().getX();// * convexShape->getLocalScaling().getX();
  70. btScalar margin = radius + convexShape->getMarginNV();
  71. const btTransform& t = xform;
  72. const btVector3& center = t.getOrigin();
  73. btVector3 extent(margin,margin,margin);
  74. aabbMin = center - extent;
  75. aabbMax = center + extent;
  76. break;
  77. }
  78. case CONVEX_HULL_SHAPE_PROXYTYPE:
  79. {
  80. ATTRIBUTE_ALIGNED16(char convexHullShape0[sizeof(btConvexHullShape)]);
  81. cellDmaGet(&convexHullShape0, convexShapePtr , sizeof(btConvexHullShape), DMA_TAG(1), 0, 0);
  82. cellDmaWaitTagStatusAll(DMA_MASK(1));
  83. btConvexHullShape* localPtr = (btConvexHullShape*)&convexHullShape0;
  84. const btTransform& t = xform;
  85. btScalar margin = convexShape->getMarginNV();
  86. localPtr->getNonvirtualAabb(t,aabbMin,aabbMax,margin);
  87. //spu_printf("SPU convex aabbMin=%f,%f,%f=\n",aabbMin.getX(),aabbMin.getY(),aabbMin.getZ());
  88. //spu_printf("SPU convex aabbMax=%f,%f,%f=\n",aabbMax.getX(),aabbMax.getY(),aabbMax.getZ());
  89. break;
  90. }
  91. default:
  92. {
  93. // spu_printf("SPU: unsupported shapetype %d in AABB calculation\n");
  94. }
  95. };
  96. }
  97. void dmaBvhShapeData (bvhMeshShape_LocalStoreMemory* bvhMeshShape, btBvhTriangleMeshShape* triMeshShape)
  98. {
  99. register int dmaSize;
  100. register ppu_address_t dmaPpuAddress2;
  101. dmaSize = sizeof(btTriangleIndexVertexArray);
  102. dmaPpuAddress2 = reinterpret_cast<ppu_address_t>(triMeshShape->getMeshInterface());
  103. // spu_printf("trimeshShape->getMeshInterface() == %llx\n",dmaPpuAddress2);
  104. #ifdef __SPU__
  105. cellDmaGet(&bvhMeshShape->gTriangleMeshInterfaceStorage, dmaPpuAddress2 , dmaSize, DMA_TAG(1), 0, 0);
  106. bvhMeshShape->gTriangleMeshInterfacePtr = &bvhMeshShape->gTriangleMeshInterfaceStorage;
  107. #else
  108. bvhMeshShape->gTriangleMeshInterfacePtr = (btTriangleIndexVertexArray*)cellDmaGetReadOnly(&bvhMeshShape->gTriangleMeshInterfaceStorage, dmaPpuAddress2 , dmaSize, DMA_TAG(1), 0, 0);
  109. #endif
  110. //cellDmaWaitTagStatusAll(DMA_MASK(1));
  111. ///now DMA over the BVH
  112. dmaSize = sizeof(btOptimizedBvh);
  113. dmaPpuAddress2 = reinterpret_cast<ppu_address_t>(triMeshShape->getOptimizedBvh());
  114. //spu_printf("trimeshShape->getOptimizedBvh() == %llx\n",dmaPpuAddress2);
  115. cellDmaGet(&bvhMeshShape->gOptimizedBvh, dmaPpuAddress2 , dmaSize, DMA_TAG(2), 0, 0);
  116. //cellDmaWaitTagStatusAll(DMA_MASK(2));
  117. cellDmaWaitTagStatusAll(DMA_MASK(1) | DMA_MASK(2));
  118. }
  119. void dmaBvhIndexedMesh (btIndexedMesh* IndexMesh, IndexedMeshArray& indexArray, int index, uint32_t dmaTag)
  120. {
  121. cellDmaGet(IndexMesh, (ppu_address_t)&indexArray[index] , sizeof(btIndexedMesh), DMA_TAG(dmaTag), 0, 0);
  122. }
  123. void dmaBvhSubTreeHeaders (btBvhSubtreeInfo* subTreeHeaders, ppu_address_t subTreePtr, int batchSize, uint32_t dmaTag)
  124. {
  125. cellDmaGet(subTreeHeaders, subTreePtr, batchSize * sizeof(btBvhSubtreeInfo), DMA_TAG(dmaTag), 0, 0);
  126. }
  127. void dmaBvhSubTreeNodes (btQuantizedBvhNode* nodes, const btBvhSubtreeInfo& subtree, QuantizedNodeArray& nodeArray, int dmaTag)
  128. {
  129. cellDmaGet(nodes, reinterpret_cast<ppu_address_t>(&nodeArray[subtree.m_rootNodeIndex]) , subtree.m_subtreeSize* sizeof(btQuantizedBvhNode), DMA_TAG(2), 0, 0);
  130. }
  131. ///getShapeTypeSize could easily be optimized, but it is not likely a bottleneck
  132. int getShapeTypeSize(int shapeType)
  133. {
  134. switch (shapeType)
  135. {
  136. case CYLINDER_SHAPE_PROXYTYPE:
  137. {
  138. int shapeSize = sizeof(btCylinderShape);
  139. btAssert(shapeSize < MAX_SHAPE_SIZE);
  140. return shapeSize;
  141. }
  142. case BOX_SHAPE_PROXYTYPE:
  143. {
  144. int shapeSize = sizeof(btBoxShape);
  145. btAssert(shapeSize < MAX_SHAPE_SIZE);
  146. return shapeSize;
  147. }
  148. case SPHERE_SHAPE_PROXYTYPE:
  149. {
  150. int shapeSize = sizeof(btSphereShape);
  151. btAssert(shapeSize < MAX_SHAPE_SIZE);
  152. return shapeSize;
  153. }
  154. case TRIANGLE_MESH_SHAPE_PROXYTYPE:
  155. {
  156. int shapeSize = sizeof(btBvhTriangleMeshShape);
  157. btAssert(shapeSize < MAX_SHAPE_SIZE);
  158. return shapeSize;
  159. }
  160. case CAPSULE_SHAPE_PROXYTYPE:
  161. {
  162. int shapeSize = sizeof(btCapsuleShape);
  163. btAssert(shapeSize < MAX_SHAPE_SIZE);
  164. return shapeSize;
  165. }
  166. case CONVEX_HULL_SHAPE_PROXYTYPE:
  167. {
  168. int shapeSize = sizeof(btConvexHullShape);
  169. btAssert(shapeSize < MAX_SHAPE_SIZE);
  170. return shapeSize;
  171. }
  172. case COMPOUND_SHAPE_PROXYTYPE:
  173. {
  174. int shapeSize = sizeof(btCompoundShape);
  175. btAssert(shapeSize < MAX_SHAPE_SIZE);
  176. return shapeSize;
  177. }
  178. case STATIC_PLANE_PROXYTYPE:
  179. {
  180. int shapeSize = sizeof(btStaticPlaneShape);
  181. btAssert(shapeSize < MAX_SHAPE_SIZE);
  182. return shapeSize;
  183. }
  184. default:
  185. btAssert(0);
  186. //unsupported shapetype, please add here
  187. return 0;
  188. }
  189. }
  190. void dmaConvexVertexData (SpuConvexPolyhedronVertexData* convexVertexData, btConvexHullShape* convexShapeSPU)
  191. {
  192. convexVertexData->gNumConvexPoints = convexShapeSPU->getNumPoints();
  193. if (convexVertexData->gNumConvexPoints>MAX_NUM_SPU_CONVEX_POINTS)
  194. {
  195. btAssert(0);
  196. // spu_printf("SPU: Error: MAX_NUM_SPU_CONVEX_POINTS(%d) exceeded: %d\n",MAX_NUM_SPU_CONVEX_POINTS,convexVertexData->gNumConvexPoints);
  197. return;
  198. }
  199. register int dmaSize = convexVertexData->gNumConvexPoints*sizeof(btVector3);
  200. ppu_address_t pointsPPU = (ppu_address_t) convexShapeSPU->getUnscaledPoints();
  201. cellDmaGet(&convexVertexData->g_convexPointBuffer[0], pointsPPU , dmaSize, DMA_TAG(2), 0, 0);
  202. }
  203. void dmaCollisionShape (void* collisionShapeLocation, ppu_address_t collisionShapePtr, uint32_t dmaTag, int shapeType)
  204. {
  205. register int dmaSize = getShapeTypeSize(shapeType);
  206. cellDmaGet(collisionShapeLocation, collisionShapePtr , dmaSize, DMA_TAG(dmaTag), 0, 0);
  207. //cellDmaGetReadOnly(collisionShapeLocation, collisionShapePtr , dmaSize, DMA_TAG(dmaTag), 0, 0);
  208. //cellDmaWaitTagStatusAll(DMA_MASK(dmaTag));
  209. }
  210. void dmaCompoundShapeInfo (CompoundShape_LocalStoreMemory* compoundShapeLocation, btCompoundShape* spuCompoundShape, uint32_t dmaTag)
  211. {
  212. register int dmaSize;
  213. register ppu_address_t dmaPpuAddress2;
  214. int childShapeCount = spuCompoundShape->getNumChildShapes();
  215. dmaSize = childShapeCount * sizeof(btCompoundShapeChild);
  216. dmaPpuAddress2 = (ppu_address_t)spuCompoundShape->getChildList();
  217. cellDmaGet(&compoundShapeLocation->gSubshapes[0], dmaPpuAddress2, dmaSize, DMA_TAG(dmaTag), 0, 0);
  218. }
  219. void dmaCompoundSubShapes (CompoundShape_LocalStoreMemory* compoundShapeLocation, btCompoundShape* spuCompoundShape, uint32_t dmaTag)
  220. {
  221. int childShapeCount = spuCompoundShape->getNumChildShapes();
  222. int i;
  223. // DMA all the subshapes
  224. for ( i = 0; i < childShapeCount; ++i)
  225. {
  226. btCompoundShapeChild& childShape = compoundShapeLocation->gSubshapes[i];
  227. dmaCollisionShape (&compoundShapeLocation->gSubshapeShape[i],(ppu_address_t)childShape.m_childShape, dmaTag, childShape.m_childShapeType);
  228. }
  229. }
  230. void spuWalkStacklessQuantizedTree(btNodeOverlapCallback* nodeCallback,unsigned short int* quantizedQueryAabbMin,unsigned short int* quantizedQueryAabbMax,const btQuantizedBvhNode* rootNode,int startNodeIndex,int endNodeIndex)
  231. {
  232. int curIndex = startNodeIndex;
  233. int walkIterations = 0;
  234. #ifdef BT_DEBUG
  235. int subTreeSize = endNodeIndex - startNodeIndex;
  236. #endif
  237. int escapeIndex;
  238. unsigned int aabbOverlap, isLeafNode;
  239. while (curIndex < endNodeIndex)
  240. {
  241. //catch bugs in tree data
  242. btAssert (walkIterations < subTreeSize);
  243. walkIterations++;
  244. aabbOverlap = spuTestQuantizedAabbAgainstQuantizedAabb(quantizedQueryAabbMin,quantizedQueryAabbMax,rootNode->m_quantizedAabbMin,rootNode->m_quantizedAabbMax);
  245. isLeafNode = rootNode->isLeafNode();
  246. if (isLeafNode && aabbOverlap)
  247. {
  248. //printf("overlap with node %d\n",rootNode->getTriangleIndex());
  249. nodeCallback->processNode(0,rootNode->getTriangleIndex());
  250. // spu_printf("SPU: overlap detected with triangleIndex:%d\n",rootNode->getTriangleIndex());
  251. }
  252. if (aabbOverlap || isLeafNode)
  253. {
  254. rootNode++;
  255. curIndex++;
  256. } else
  257. {
  258. escapeIndex = rootNode->getEscapeIndex();
  259. rootNode += escapeIndex;
  260. curIndex += escapeIndex;
  261. }
  262. }
  263. }