2 Bullet Continuous Collision Detection and Physics Library, Copyright (c) 2007 Erwin Coumans
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:
10 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.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
17 #include "SpuSampleTask.h"
18 #include "BulletDynamics/Dynamics/btRigidBody.h"
19 #include "../PlatformDefinitions.h"
20 #include "../SpuFakeDma.h"
21 #include "LinearMath/btMinMax.h"
24 #include <spu_printf.h>
27 #define spu_printf printf
30 #define MAX_NUM_BODIES 8192
32 struct SampleTask_LocalStoreMemory
34 ATTRIBUTE_ALIGNED16(char gLocalRigidBody [sizeof(btRigidBody)+16]);
35 ATTRIBUTE_ALIGNED16(void* gPointerArray[MAX_NUM_BODIES]);
43 void processSampleTask(void* userPtr, void* lsMemory)
45 // BT_PROFILE("processSampleTask");
47 SampleTask_LocalStoreMemory* localMemory = (SampleTask_LocalStoreMemory*)lsMemory;
49 SpuSampleTaskDesc* taskDescPtr = (SpuSampleTaskDesc*)userPtr;
50 SpuSampleTaskDesc& taskDesc = *taskDescPtr;
52 switch (taskDesc.m_sampleCommand)
54 case CMD_SAMPLE_INTEGRATE_BODIES:
56 btTransform predictedTrans;
57 btCollisionObject** eaPtr = (btCollisionObject**)taskDesc.m_mainMemoryPtr;
59 int batchSize = taskDesc.m_sampleValue;
60 if (batchSize>MAX_NUM_BODIES)
62 spu_printf("SPU Error: exceed number of bodies, see MAX_NUM_BODIES in SpuSampleTask.cpp\n");
65 int dmaArraySize = batchSize*sizeof(void*);
67 uint64_t ppuArrayAddress = reinterpret_cast<uint64_t>(eaPtr);
69 // spu_printf("array location is at %llx, batchSize = %d, DMA size = %d\n",ppuArrayAddress,batchSize,dmaArraySize);
73 cellDmaLargeGet((void*)&localMemory->gPointerArray[0], ppuArrayAddress , dmaArraySize, DMA_TAG(1), 0, 0);
74 cellDmaWaitTagStatusAll(DMA_MASK(1));
77 stallingUnalignedDmaSmallGet((void*)&localMemory->gPointerArray[0], ppuArrayAddress , dmaArraySize);
81 for ( int i=0;i<batchSize;i++)
85 void* localPtr = &localMemory->gLocalRigidBody[0];
86 void* shortAdd = localMemory->gPointerArray[i];
87 uint64_t ppuRigidBodyAddress = reinterpret_cast<uint64_t>(shortAdd);
89 // spu_printf("cellDmaGet at CMD_SAMPLE_INTEGRATE_BODIES from %llx to %llx\n",ppuRigidBodyAddress,localPtr);
91 int dmaBodySize = sizeof(btRigidBody);
93 cellDmaGet((void*)localPtr, ppuRigidBodyAddress , dmaBodySize, DMA_TAG(1), 0, 0);
94 cellDmaWaitTagStatusAll(DMA_MASK(1));
97 float timeStep = 1.f/60.f;
99 btRigidBody* body = (btRigidBody*) localPtr;//btRigidBody::upcast(colObj);
102 if (body->isActive() && (!body->isStaticOrKinematicObject()))
104 body->predictIntegratedTransform(timeStep, predictedTrans);
105 body->proceedToTransform( predictedTrans);
106 void* ptr = (void*)localPtr;
107 // spu_printf("cellDmaLargePut from %llx to LS %llx\n",ptr,ppuRigidBodyAddress);
109 cellDmaLargePut(ptr, ppuRigidBodyAddress , dmaBodySize, DMA_TAG(1), 0, 0);
110 cellDmaWaitTagStatusAll(DMA_MASK(1));
120 case CMD_SAMPLE_PREDICT_MOTION_BODIES:
122 btTransform predictedTrans;
123 btCollisionObject** eaPtr = (btCollisionObject**)taskDesc.m_mainMemoryPtr;
125 int batchSize = taskDesc.m_sampleValue;
126 int dmaArraySize = batchSize*sizeof(void*);
128 if (batchSize>MAX_NUM_BODIES)
130 spu_printf("SPU Error: exceed number of bodies, see MAX_NUM_BODIES in SpuSampleTask.cpp\n");
134 uint64_t ppuArrayAddress = reinterpret_cast<uint64_t>(eaPtr);
136 // spu_printf("array location is at %llx, batchSize = %d, DMA size = %d\n",ppuArrayAddress,batchSize,dmaArraySize);
138 if (dmaArraySize>=16)
140 cellDmaLargeGet((void*)&localMemory->gPointerArray[0], ppuArrayAddress , dmaArraySize, DMA_TAG(1), 0, 0);
141 cellDmaWaitTagStatusAll(DMA_MASK(1));
144 stallingUnalignedDmaSmallGet((void*)&localMemory->gPointerArray[0], ppuArrayAddress , dmaArraySize);
148 for ( int i=0;i<batchSize;i++)
152 void* localPtr = &localMemory->gLocalRigidBody[0];
153 void* shortAdd = localMemory->gPointerArray[i];
154 uint64_t ppuRigidBodyAddress = reinterpret_cast<uint64_t>(shortAdd);
156 // spu_printf("cellDmaGet at CMD_SAMPLE_INTEGRATE_BODIES from %llx to %llx\n",ppuRigidBodyAddress,localPtr);
158 int dmaBodySize = sizeof(btRigidBody);
160 cellDmaGet((void*)localPtr, ppuRigidBodyAddress , dmaBodySize, DMA_TAG(1), 0, 0);
161 cellDmaWaitTagStatusAll(DMA_MASK(1));
164 float timeStep = 1.f/60.f;
166 btRigidBody* body = (btRigidBody*) localPtr;//btRigidBody::upcast(colObj);
169 if (!body->isStaticOrKinematicObject())
171 if (body->isActive())
173 body->integrateVelocities( timeStep);
175 body->applyDamping(timeStep);
177 body->predictIntegratedTransform(timeStep,body->getInterpolationWorldTransform());
179 void* ptr = (void*)localPtr;
180 cellDmaLargePut(ptr, ppuRigidBodyAddress , dmaBodySize, DMA_TAG(1), 0, 0);
181 cellDmaWaitTagStatusAll(DMA_MASK(1));
200 #if defined(__CELLOS_LV2__) || defined (LIBSPE2)
202 ATTRIBUTE_ALIGNED16(SampleTask_LocalStoreMemory gLocalStoreMemory);
204 void* createSampleLocalStoreMemory()
206 return &gLocalStoreMemory;
209 void* createSampleLocalStoreMemory()
211 return new SampleTask_LocalStoreMemory;