SpuSampleTask.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library, Copyright (c) 2007 Erwin Coumans
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 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.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. #include "SpuSampleTask.h"
  13. #include "bullet/BulletDynamics/Dynamics/btRigidBody.h"
  14. #include "../PlatformDefinitions.h"
  15. #include "../SpuFakeDma.h"
  16. #include "bullet/LinearMath/btMinMax.h"
  17. #ifdef __SPU__
  18. #include <spu_printf.h>
  19. #else
  20. #include <stdio.h>
  21. #define spu_printf printf
  22. #endif
  23. #define MAX_NUM_BODIES 8192
  24. #ifdef WINRT
  25. __declspec(align(16)) struct SampleTask_LocalStoreMemory
  26. #else
  27. struct SampleTask_LocalStoreMemory
  28. #endif
  29. {
  30. ATTRIBUTE_ALIGNED16(char gLocalRigidBody [sizeof(btRigidBody)+16]);
  31. ATTRIBUTE_ALIGNED16(void* gPointerArray[MAX_NUM_BODIES]);
  32. #ifdef WINRT
  33. void* operator new(size_t i)
  34. {
  35. return _aligned_malloc(i, 16);
  36. }
  37. void operator delete(void* p)
  38. {
  39. _aligned_free(p);
  40. }
  41. #endif
  42. };
  43. //-- MAIN METHOD
  44. void processSampleTask(void* userPtr, void* lsMemory)
  45. {
  46. // BT_PROFILE("processSampleTask");
  47. SampleTask_LocalStoreMemory* localMemory = (SampleTask_LocalStoreMemory*)lsMemory;
  48. SpuSampleTaskDesc* taskDescPtr = (SpuSampleTaskDesc*)userPtr;
  49. SpuSampleTaskDesc& taskDesc = *taskDescPtr;
  50. switch (taskDesc.m_sampleCommand)
  51. {
  52. case CMD_SAMPLE_INTEGRATE_BODIES:
  53. {
  54. btTransform predictedTrans;
  55. btCollisionObject** eaPtr = (btCollisionObject**)taskDesc.m_mainMemoryPtr;
  56. int batchSize = taskDesc.m_sampleValue;
  57. if (batchSize>MAX_NUM_BODIES)
  58. {
  59. spu_printf("SPU Error: exceed number of bodies, see MAX_NUM_BODIES in SpuSampleTask.cpp\n");
  60. break;
  61. }
  62. int dmaArraySize = batchSize*sizeof(void*);
  63. uint64_t ppuArrayAddress = reinterpret_cast<uint64_t>(eaPtr);
  64. // spu_printf("array location is at %llx, batchSize = %d, DMA size = %d\n",ppuArrayAddress,batchSize,dmaArraySize);
  65. if (dmaArraySize>=16)
  66. {
  67. cellDmaLargeGet((void*)&localMemory->gPointerArray[0], ppuArrayAddress , dmaArraySize, DMA_TAG(1), 0, 0);
  68. cellDmaWaitTagStatusAll(DMA_MASK(1));
  69. } else
  70. {
  71. stallingUnalignedDmaSmallGet((void*)&localMemory->gPointerArray[0], ppuArrayAddress , dmaArraySize);
  72. }
  73. for ( int i=0;i<batchSize;i++)
  74. {
  75. ///DMA rigid body
  76. void* localPtr = &localMemory->gLocalRigidBody[0];
  77. void* shortAdd = localMemory->gPointerArray[i];
  78. uint64_t ppuRigidBodyAddress = reinterpret_cast<uint64_t>(shortAdd);
  79. // spu_printf("cellDmaGet at CMD_SAMPLE_INTEGRATE_BODIES from %llx to %llx\n",ppuRigidBodyAddress,localPtr);
  80. int dmaBodySize = sizeof(btRigidBody);
  81. cellDmaGet((void*)localPtr, ppuRigidBodyAddress , dmaBodySize, DMA_TAG(1), 0, 0);
  82. cellDmaWaitTagStatusAll(DMA_MASK(1));
  83. float timeStep = 1.f/60.f;
  84. btRigidBody* body = (btRigidBody*) localPtr;//btRigidBody::upcast(colObj);
  85. if (body)
  86. {
  87. if (body->isActive() && (!body->isStaticOrKinematicObject()))
  88. {
  89. body->predictIntegratedTransform(timeStep, predictedTrans);
  90. body->proceedToTransform( predictedTrans);
  91. void* ptr = (void*)localPtr;
  92. // spu_printf("cellDmaLargePut from %llx to LS %llx\n",ptr,ppuRigidBodyAddress);
  93. cellDmaLargePut(ptr, ppuRigidBodyAddress , dmaBodySize, DMA_TAG(1), 0, 0);
  94. cellDmaWaitTagStatusAll(DMA_MASK(1));
  95. }
  96. }
  97. }
  98. break;
  99. }
  100. case CMD_SAMPLE_PREDICT_MOTION_BODIES:
  101. {
  102. btTransform predictedTrans;
  103. btCollisionObject** eaPtr = (btCollisionObject**)taskDesc.m_mainMemoryPtr;
  104. int batchSize = taskDesc.m_sampleValue;
  105. int dmaArraySize = batchSize*sizeof(void*);
  106. if (batchSize>MAX_NUM_BODIES)
  107. {
  108. spu_printf("SPU Error: exceed number of bodies, see MAX_NUM_BODIES in SpuSampleTask.cpp\n");
  109. break;
  110. }
  111. uint64_t ppuArrayAddress = reinterpret_cast<uint64_t>(eaPtr);
  112. // spu_printf("array location is at %llx, batchSize = %d, DMA size = %d\n",ppuArrayAddress,batchSize,dmaArraySize);
  113. if (dmaArraySize>=16)
  114. {
  115. cellDmaLargeGet((void*)&localMemory->gPointerArray[0], ppuArrayAddress , dmaArraySize, DMA_TAG(1), 0, 0);
  116. cellDmaWaitTagStatusAll(DMA_MASK(1));
  117. } else
  118. {
  119. stallingUnalignedDmaSmallGet((void*)&localMemory->gPointerArray[0], ppuArrayAddress , dmaArraySize);
  120. }
  121. for ( int i=0;i<batchSize;i++)
  122. {
  123. ///DMA rigid body
  124. void* localPtr = &localMemory->gLocalRigidBody[0];
  125. void* shortAdd = localMemory->gPointerArray[i];
  126. uint64_t ppuRigidBodyAddress = reinterpret_cast<uint64_t>(shortAdd);
  127. // spu_printf("cellDmaGet at CMD_SAMPLE_INTEGRATE_BODIES from %llx to %llx\n",ppuRigidBodyAddress,localPtr);
  128. int dmaBodySize = sizeof(btRigidBody);
  129. cellDmaGet((void*)localPtr, ppuRigidBodyAddress , dmaBodySize, DMA_TAG(1), 0, 0);
  130. cellDmaWaitTagStatusAll(DMA_MASK(1));
  131. float timeStep = 1.f/60.f;
  132. btRigidBody* body = (btRigidBody*) localPtr;//btRigidBody::upcast(colObj);
  133. if (body)
  134. {
  135. if (!body->isStaticOrKinematicObject())
  136. {
  137. if (body->isActive())
  138. {
  139. body->integrateVelocities( timeStep);
  140. //damping
  141. body->applyDamping(timeStep);
  142. body->predictIntegratedTransform(timeStep,body->getInterpolationWorldTransform());
  143. void* ptr = (void*)localPtr;
  144. cellDmaLargePut(ptr, ppuRigidBodyAddress , dmaBodySize, DMA_TAG(1), 0, 0);
  145. cellDmaWaitTagStatusAll(DMA_MASK(1));
  146. }
  147. }
  148. }
  149. }
  150. break;
  151. }
  152. default:
  153. {
  154. }
  155. };
  156. }
  157. #if defined(__CELLOS_LV2__) || defined (LIBSPE2)
  158. ATTRIBUTE_ALIGNED16(SampleTask_LocalStoreMemory gLocalStoreMemory);
  159. void* createSampleLocalStoreMemory()
  160. {
  161. return &gLocalStoreMemory;
  162. }
  163. #else
  164. void* createSampleLocalStoreMemory()
  165. {
  166. return new SampleTask_LocalStoreMemory;
  167. };
  168. #endif