2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
17 #include "SpuCollisionShapes.h"
19 ///not supported on IBM SDK, until we fix the alignment of btVector3
20 #if defined (__CELLOS_LV2__) && defined (__SPU__)
21 #include <spu_intrinsics.h>
22 static inline vec_float4 vec_dot3( vec_float4 vec0, vec_float4 vec1 )
25 result = spu_mul( vec0, vec1 );
26 result = spu_madd( spu_rlqwbyte( vec0, 4 ), spu_rlqwbyte( vec1, 4 ), result );
27 return spu_madd( spu_rlqwbyte( vec0, 8 ), spu_rlqwbyte( vec1, 8 ), result );
32 void computeAabb (btVector3& aabbMin, btVector3& aabbMax, btConvexInternalShape* convexShape, ppu_address_t convexShapePtr, int shapeType, const btTransform& xform)
34 //calculate the aabb, given the types...
37 case CYLINDER_SHAPE_PROXYTYPE:
39 case BOX_SHAPE_PROXYTYPE:
41 btScalar margin=convexShape->getMarginNV();
42 btVector3 halfExtents = convexShape->getImplicitShapeDimensions();
43 halfExtents += btVector3(margin,margin,margin);
44 const btTransform& t = xform;
45 btMatrix3x3 abs_b = t.getBasis().absolute();
46 btVector3 center = t.getOrigin();
47 btVector3 extent = halfExtents.dot3( abs_b[0], abs_b[1], abs_b[2] );
49 aabbMin = center - extent;
50 aabbMax = center + extent;
53 case CAPSULE_SHAPE_PROXYTYPE:
55 btScalar margin=convexShape->getMarginNV();
56 btVector3 halfExtents = convexShape->getImplicitShapeDimensions();
57 //add the radius to y-axis to get full height
58 btScalar radius = halfExtents[0];
59 halfExtents[1] += radius;
60 halfExtents += btVector3(margin,margin,margin);
62 int capsuleUpAxis = convexShape->getUpAxis();
63 btScalar halfHeight = convexShape->getHalfHeight();
64 btScalar radius = convexShape->getRadius();
65 halfExtents[capsuleUpAxis] = radius + halfHeight;
67 const btTransform& t = xform;
68 btMatrix3x3 abs_b = t.getBasis().absolute();
69 btVector3 center = t.getOrigin();
70 btVector3 extent = halfExtents.dot3( abs_b[0], abs_b[1], abs_b[2] );
72 aabbMin = center - extent;
73 aabbMax = center + extent;
76 case SPHERE_SHAPE_PROXYTYPE:
78 btScalar radius = convexShape->getImplicitShapeDimensions().getX();// * convexShape->getLocalScaling().getX();
79 btScalar margin = radius + convexShape->getMarginNV();
80 const btTransform& t = xform;
81 const btVector3& center = t.getOrigin();
82 btVector3 extent(margin,margin,margin);
83 aabbMin = center - extent;
84 aabbMax = center + extent;
87 case CONVEX_HULL_SHAPE_PROXYTYPE:
89 ATTRIBUTE_ALIGNED16(char convexHullShape0[sizeof(btConvexHullShape)]);
90 cellDmaGet(&convexHullShape0, convexShapePtr , sizeof(btConvexHullShape), DMA_TAG(1), 0, 0);
91 cellDmaWaitTagStatusAll(DMA_MASK(1));
92 btConvexHullShape* localPtr = (btConvexHullShape*)&convexHullShape0;
93 const btTransform& t = xform;
94 btScalar margin = convexShape->getMarginNV();
95 localPtr->getNonvirtualAabb(t,aabbMin,aabbMax,margin);
96 //spu_printf("SPU convex aabbMin=%f,%f,%f=\n",aabbMin.getX(),aabbMin.getY(),aabbMin.getZ());
97 //spu_printf("SPU convex aabbMax=%f,%f,%f=\n",aabbMax.getX(),aabbMax.getY(),aabbMax.getZ());
102 // spu_printf("SPU: unsupported shapetype %d in AABB calculation\n");
107 void dmaBvhShapeData (bvhMeshShape_LocalStoreMemory* bvhMeshShape, btBvhTriangleMeshShape* triMeshShape)
109 register int dmaSize;
110 register ppu_address_t dmaPpuAddress2;
112 dmaSize = sizeof(btTriangleIndexVertexArray);
113 dmaPpuAddress2 = reinterpret_cast<ppu_address_t>(triMeshShape->getMeshInterface());
114 // spu_printf("trimeshShape->getMeshInterface() == %llx\n",dmaPpuAddress2);
116 cellDmaGet(&bvhMeshShape->gTriangleMeshInterfaceStorage, dmaPpuAddress2 , dmaSize, DMA_TAG(1), 0, 0);
117 bvhMeshShape->gTriangleMeshInterfacePtr = &bvhMeshShape->gTriangleMeshInterfaceStorage;
119 bvhMeshShape->gTriangleMeshInterfacePtr = (btTriangleIndexVertexArray*)cellDmaGetReadOnly(&bvhMeshShape->gTriangleMeshInterfaceStorage, dmaPpuAddress2 , dmaSize, DMA_TAG(1), 0, 0);
122 //cellDmaWaitTagStatusAll(DMA_MASK(1));
124 ///now DMA over the BVH
126 dmaSize = sizeof(btOptimizedBvh);
127 dmaPpuAddress2 = reinterpret_cast<ppu_address_t>(triMeshShape->getOptimizedBvh());
128 //spu_printf("trimeshShape->getOptimizedBvh() == %llx\n",dmaPpuAddress2);
129 cellDmaGet(&bvhMeshShape->gOptimizedBvh, dmaPpuAddress2 , dmaSize, DMA_TAG(2), 0, 0);
130 //cellDmaWaitTagStatusAll(DMA_MASK(2));
131 cellDmaWaitTagStatusAll(DMA_MASK(1) | DMA_MASK(2));
134 void dmaBvhIndexedMesh (btIndexedMesh* IndexMesh, IndexedMeshArray& indexArray, int index, uint32_t dmaTag)
136 cellDmaGet(IndexMesh, (ppu_address_t)&indexArray[index] , sizeof(btIndexedMesh), DMA_TAG(dmaTag), 0, 0);
140 void dmaBvhSubTreeHeaders (btBvhSubtreeInfo* subTreeHeaders, ppu_address_t subTreePtr, int batchSize, uint32_t dmaTag)
142 cellDmaGet(subTreeHeaders, subTreePtr, batchSize * sizeof(btBvhSubtreeInfo), DMA_TAG(dmaTag), 0, 0);
145 void dmaBvhSubTreeNodes (btQuantizedBvhNode* nodes, const btBvhSubtreeInfo& subtree, QuantizedNodeArray& nodeArray, int dmaTag)
147 cellDmaGet(nodes, reinterpret_cast<ppu_address_t>(&nodeArray[subtree.m_rootNodeIndex]) , subtree.m_subtreeSize* sizeof(btQuantizedBvhNode), DMA_TAG(2), 0, 0);
150 ///getShapeTypeSize could easily be optimized, but it is not likely a bottleneck
151 int getShapeTypeSize(int shapeType)
157 case CYLINDER_SHAPE_PROXYTYPE:
159 int shapeSize = sizeof(btCylinderShape);
160 btAssert(shapeSize < MAX_SHAPE_SIZE);
163 case BOX_SHAPE_PROXYTYPE:
165 int shapeSize = sizeof(btBoxShape);
166 btAssert(shapeSize < MAX_SHAPE_SIZE);
169 case SPHERE_SHAPE_PROXYTYPE:
171 int shapeSize = sizeof(btSphereShape);
172 btAssert(shapeSize < MAX_SHAPE_SIZE);
175 case TRIANGLE_MESH_SHAPE_PROXYTYPE:
177 int shapeSize = sizeof(btBvhTriangleMeshShape);
178 btAssert(shapeSize < MAX_SHAPE_SIZE);
181 case CAPSULE_SHAPE_PROXYTYPE:
183 int shapeSize = sizeof(btCapsuleShape);
184 btAssert(shapeSize < MAX_SHAPE_SIZE);
188 case CONVEX_HULL_SHAPE_PROXYTYPE:
190 int shapeSize = sizeof(btConvexHullShape);
191 btAssert(shapeSize < MAX_SHAPE_SIZE);
195 case COMPOUND_SHAPE_PROXYTYPE:
197 int shapeSize = sizeof(btCompoundShape);
198 btAssert(shapeSize < MAX_SHAPE_SIZE);
201 case STATIC_PLANE_PROXYTYPE:
203 int shapeSize = sizeof(btStaticPlaneShape);
204 btAssert(shapeSize < MAX_SHAPE_SIZE);
210 //unsupported shapetype, please add here
215 void dmaConvexVertexData (SpuConvexPolyhedronVertexData* convexVertexData, btConvexHullShape* convexShapeSPU)
217 convexVertexData->gNumConvexPoints = convexShapeSPU->getNumPoints();
218 if (convexVertexData->gNumConvexPoints>MAX_NUM_SPU_CONVEX_POINTS)
221 // spu_printf("SPU: Error: MAX_NUM_SPU_CONVEX_POINTS(%d) exceeded: %d\n",MAX_NUM_SPU_CONVEX_POINTS,convexVertexData->gNumConvexPoints);
225 register int dmaSize = convexVertexData->gNumConvexPoints*sizeof(btVector3);
226 ppu_address_t pointsPPU = (ppu_address_t) convexShapeSPU->getUnscaledPoints();
227 cellDmaGet(&convexVertexData->g_convexPointBuffer[0], pointsPPU , dmaSize, DMA_TAG(2), 0, 0);
230 void dmaCollisionShape (void* collisionShapeLocation, ppu_address_t collisionShapePtr, uint32_t dmaTag, int shapeType)
232 register int dmaSize = getShapeTypeSize(shapeType);
233 cellDmaGet(collisionShapeLocation, collisionShapePtr , dmaSize, DMA_TAG(dmaTag), 0, 0);
234 //cellDmaGetReadOnly(collisionShapeLocation, collisionShapePtr , dmaSize, DMA_TAG(dmaTag), 0, 0);
235 //cellDmaWaitTagStatusAll(DMA_MASK(dmaTag));
238 void dmaCompoundShapeInfo (CompoundShape_LocalStoreMemory* compoundShapeLocation, btCompoundShape* spuCompoundShape, uint32_t dmaTag)
240 register int dmaSize;
241 register ppu_address_t dmaPpuAddress2;
242 int childShapeCount = spuCompoundShape->getNumChildShapes();
243 dmaSize = childShapeCount * sizeof(btCompoundShapeChild);
244 dmaPpuAddress2 = (ppu_address_t)spuCompoundShape->getChildList();
245 cellDmaGet(&compoundShapeLocation->gSubshapes[0], dmaPpuAddress2, dmaSize, DMA_TAG(dmaTag), 0, 0);
248 void dmaCompoundSubShapes (CompoundShape_LocalStoreMemory* compoundShapeLocation, btCompoundShape* spuCompoundShape, uint32_t dmaTag)
250 int childShapeCount = spuCompoundShape->getNumChildShapes();
252 // DMA all the subshapes
253 for ( i = 0; i < childShapeCount; ++i)
255 btCompoundShapeChild& childShape = compoundShapeLocation->gSubshapes[i];
256 dmaCollisionShape (&compoundShapeLocation->gSubshapeShape[i],(ppu_address_t)childShape.m_childShape, dmaTag, childShape.m_childShapeType);
261 void spuWalkStacklessQuantizedTree(btNodeOverlapCallback* nodeCallback,unsigned short int* quantizedQueryAabbMin,unsigned short int* quantizedQueryAabbMax,const btQuantizedBvhNode* rootNode,int startNodeIndex,int endNodeIndex)
264 int curIndex = startNodeIndex;
265 int walkIterations = 0;
267 int subTreeSize = endNodeIndex - startNodeIndex;
272 unsigned int aabbOverlap, isLeafNode;
274 while (curIndex < endNodeIndex)
276 //catch bugs in tree data
277 btAssert (walkIterations < subTreeSize);
280 aabbOverlap = spuTestQuantizedAabbAgainstQuantizedAabb(quantizedQueryAabbMin,quantizedQueryAabbMax,rootNode->m_quantizedAabbMin,rootNode->m_quantizedAabbMax);
281 isLeafNode = rootNode->isLeafNode();
283 if (isLeafNode && aabbOverlap)
285 //printf("overlap with node %d\n",rootNode->getTriangleIndex());
286 nodeCallback->processNode(0,rootNode->getTriangleIndex());
287 // spu_printf("SPU: overlap detected with triangleIndex:%d\n",rootNode->getTriangleIndex());
290 if (aabbOverlap || isLeafNode)
296 escapeIndex = rootNode->getEscapeIndex();
297 rootNode += escapeIndex;
298 curIndex += escapeIndex;