Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletMultiThreaded / SpuNarrowPhaseCollisionTask / SpuCollisionShapes.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
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:
10
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.
14 */
15 #ifndef __SPU_COLLISION_SHAPES_H
16 #define __SPU_COLLISION_SHAPES_H
17
18 #include "../SpuDoubleBuffer.h"
19
20 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
21 #include "BulletCollision/CollisionShapes/btConvexInternalShape.h"
22 #include "BulletCollision/CollisionShapes/btCylinderShape.h"
23 #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
24
25 #include "BulletCollision/CollisionShapes/btOptimizedBvh.h"
26 #include "BulletCollision/CollisionShapes/btTriangleIndexVertexArray.h"
27 #include "BulletCollision/CollisionShapes/btSphereShape.h"
28
29 #include "BulletCollision/CollisionShapes/btCapsuleShape.h"
30
31 #include "BulletCollision/CollisionShapes/btConvexShape.h"
32 #include "BulletCollision/CollisionShapes/btBvhTriangleMeshShape.h"
33 #include "BulletCollision/CollisionShapes/btConvexHullShape.h"
34 #include "BulletCollision/CollisionShapes/btCompoundShape.h"
35
36 #define MAX_NUM_SPU_CONVEX_POINTS 128 //@fallback to PPU if a btConvexHullShape has more than MAX_NUM_SPU_CONVEX_POINTS points
37 #define MAX_SPU_COMPOUND_SUBSHAPES 16 //@fallback on PPU if compound has more than MAX_SPU_COMPOUND_SUBSHAPES child shapes
38 #define MAX_SHAPE_SIZE 256 //@todo: assert on this
39
40 ATTRIBUTE_ALIGNED16(struct)     SpuConvexPolyhedronVertexData
41 {
42         void*   gSpuConvexShapePtr;
43         btVector3* gConvexPoints;
44         int gNumConvexPoints;
45         int unused;
46         ATTRIBUTE_ALIGNED16(btVector3 g_convexPointBuffer[MAX_NUM_SPU_CONVEX_POINTS]);
47 };
48
49
50
51 ATTRIBUTE_ALIGNED16(struct) CollisionShape_LocalStoreMemory
52 {
53         ATTRIBUTE_ALIGNED16(char collisionShape[MAX_SHAPE_SIZE]);
54 };
55
56 ATTRIBUTE_ALIGNED16(struct) CompoundShape_LocalStoreMemory
57 {
58         // Compound data
59
60         ATTRIBUTE_ALIGNED16(btCompoundShapeChild gSubshapes[MAX_SPU_COMPOUND_SUBSHAPES]);
61         ATTRIBUTE_ALIGNED16(char gSubshapeShape[MAX_SPU_COMPOUND_SUBSHAPES][MAX_SHAPE_SIZE]);
62 };
63
64 ATTRIBUTE_ALIGNED16(struct) bvhMeshShape_LocalStoreMemory
65 {
66         //ATTRIBUTE_ALIGNED16(btOptimizedBvh    gOptimizedBvh);
67         ATTRIBUTE_ALIGNED16(char gOptimizedBvh[sizeof(btOptimizedBvh)+16]);
68         btOptimizedBvh* getOptimizedBvh()
69         {
70                 return (btOptimizedBvh*) gOptimizedBvh;
71         }
72
73         ATTRIBUTE_ALIGNED16(btTriangleIndexVertexArray  gTriangleMeshInterfaceStorage);
74         btTriangleIndexVertexArray*     gTriangleMeshInterfacePtr;
75         ///only a single mesh part for now, we can add support for multiple parts, but quantized trees don't support this at the moment 
76         ATTRIBUTE_ALIGNED16(btIndexedMesh       gIndexMesh);
77         #define MAX_SPU_SUBTREE_HEADERS 32
78         //1024
79         ATTRIBUTE_ALIGNED16(btBvhSubtreeInfo    gSubtreeHeaders[MAX_SPU_SUBTREE_HEADERS]);
80         ATTRIBUTE_ALIGNED16(btQuantizedBvhNode  gSubtreeNodes[MAX_SUBTREE_SIZE_IN_BYTES/sizeof(btQuantizedBvhNode)]);
81 };
82
83
84 void computeAabb (btVector3& aabbMin, btVector3& aabbMax, btConvexInternalShape* convexShape, ppu_address_t convexShapePtr, int shapeType, const btTransform& xform);
85 void dmaBvhShapeData (bvhMeshShape_LocalStoreMemory* bvhMeshShape, btBvhTriangleMeshShape* triMeshShape);
86 void dmaBvhIndexedMesh (btIndexedMesh* IndexMesh, IndexedMeshArray& indexArray, int index, uint32_t dmaTag);
87 void dmaBvhSubTreeHeaders (btBvhSubtreeInfo* subTreeHeaders, ppu_address_t subTreePtr, int batchSize, uint32_t dmaTag);
88 void dmaBvhSubTreeNodes (btQuantizedBvhNode* nodes, const btBvhSubtreeInfo& subtree, QuantizedNodeArray&        nodeArray, int dmaTag);
89
90 int  getShapeTypeSize(int shapeType);
91 void dmaConvexVertexData (SpuConvexPolyhedronVertexData* convexVertexData, btConvexHullShape* convexShapeSPU);
92 void dmaCollisionShape (void* collisionShapeLocation, ppu_address_t collisionShapePtr, uint32_t dmaTag, int shapeType);
93 void dmaCompoundShapeInfo (CompoundShape_LocalStoreMemory* compoundShapeLocation, btCompoundShape* spuCompoundShape, uint32_t dmaTag);
94 void dmaCompoundSubShapes (CompoundShape_LocalStoreMemory* compoundShapeLocation, btCompoundShape* spuCompoundShape, uint32_t dmaTag);
95
96
97 #define USE_BRANCHFREE_TEST 1
98 #ifdef USE_BRANCHFREE_TEST
99 SIMD_FORCE_INLINE unsigned int spuTestQuantizedAabbAgainstQuantizedAabb(unsigned short int* aabbMin1,unsigned short int* aabbMax1,const unsigned short int* aabbMin2,const unsigned short int* aabbMax2)
100 {               
101 #if defined(__CELLOS_LV2__) && defined (__SPU__)
102         vec_ushort8 vecMin = {aabbMin1[0],aabbMin2[0],aabbMin1[2],aabbMin2[2],aabbMin1[1],aabbMin2[1],0,0};
103         vec_ushort8 vecMax = {aabbMax2[0],aabbMax1[0],aabbMax2[2],aabbMax1[2],aabbMax2[1],aabbMax1[1],0,0};
104         vec_ushort8 isGt = spu_cmpgt(vecMin,vecMax);
105         return spu_extract(spu_gather(isGt),0)==0;
106
107 #else
108         return btSelect((unsigned)((aabbMin1[0] <= aabbMax2[0]) & (aabbMax1[0] >= aabbMin2[0])
109                 & (aabbMin1[2] <= aabbMax2[2]) & (aabbMax1[2] >= aabbMin2[2])
110                 & (aabbMin1[1] <= aabbMax2[1]) & (aabbMax1[1] >= aabbMin2[1])),
111                 1, 0);
112 #endif
113 }
114 #else
115
116 SIMD_FORCE_INLINE unsigned int spuTestQuantizedAabbAgainstQuantizedAabb(const unsigned short int* aabbMin1,const unsigned short int* aabbMax1,const unsigned short int* aabbMin2,const unsigned short int*  aabbMax2)
117 {
118         unsigned int overlap = 1;
119         overlap = (aabbMin1[0] > aabbMax2[0] || aabbMax1[0] < aabbMin2[0]) ? 0 : overlap;
120         overlap = (aabbMin1[2] > aabbMax2[2] || aabbMax1[2] < aabbMin2[2]) ? 0 : overlap;
121         overlap = (aabbMin1[1] > aabbMax2[1] || aabbMax1[1] < aabbMin2[1]) ? 0 : overlap;
122         return overlap;
123 }
124 #endif
125
126 void    spuWalkStacklessQuantizedTree(btNodeOverlapCallback* nodeCallback,unsigned short int* quantizedQueryAabbMin,unsigned short int* quantizedQueryAabbMax,const btQuantizedBvhNode* rootNode,int startNodeIndex,int endNodeIndex);
127
128 #endif