SpuLibspe2Support.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
  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. #ifndef BT_SPU_LIBSPE2_SUPPORT_H
  14. #define BT_SPU_LIBSPE2_SUPPORT_H
  15. #include <LinearMath/btScalar.h> //for uint32_t etc.
  16. #ifdef USE_LIBSPE2
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. //#include "SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
  20. #include "PlatformDefinitions.h"
  21. //extern struct SpuGatherAndProcessPairsTaskDesc;
  22. enum
  23. {
  24. Spu_Mailbox_Event_Nothing = 0,
  25. Spu_Mailbox_Event_Task = 1,
  26. Spu_Mailbox_Event_Shutdown = 2,
  27. Spu_Mailbox_Event_ForceDword = 0xFFFFFFFF
  28. };
  29. enum
  30. {
  31. Spu_Status_Free = 0,
  32. Spu_Status_Occupied = 1,
  33. Spu_Status_Startup = 2,
  34. Spu_Status_ForceDword = 0xFFFFFFFF
  35. };
  36. struct btSpuStatus
  37. {
  38. uint32_t m_taskId;
  39. uint32_t m_commandId;
  40. uint32_t m_status;
  41. addr64 m_taskDesc;
  42. addr64 m_lsMemory;
  43. }
  44. __attribute__ ((aligned (128)))
  45. ;
  46. #ifndef __SPU__
  47. #include "bullet/LinearMath/btAlignedObjectArray.h"
  48. #include "SpuCollisionTaskProcess.h"
  49. #include "SpuSampleTaskProcess.h"
  50. #include "btThreadSupportInterface.h"
  51. #include <libspe2.h>
  52. #include <pthread.h>
  53. #include <sched.h>
  54. #define MAX_SPUS 4
  55. typedef struct ppu_pthread_data
  56. {
  57. spe_context_ptr_t context;
  58. pthread_t pthread;
  59. unsigned int entry;
  60. unsigned int flags;
  61. addr64 argp;
  62. addr64 envp;
  63. spe_stop_info_t stopinfo;
  64. } ppu_pthread_data_t;
  65. static void *ppu_pthread_function(void *arg)
  66. {
  67. ppu_pthread_data_t * datap = (ppu_pthread_data_t *)arg;
  68. /*
  69. int rc;
  70. do
  71. {*/
  72. spe_context_run(datap->context, &datap->entry, datap->flags, datap->argp.p, datap->envp.p, &datap->stopinfo);
  73. if (datap->stopinfo.stop_reason == SPE_EXIT)
  74. {
  75. if (datap->stopinfo.result.spe_exit_code != 0)
  76. {
  77. perror("FAILED: SPE returned a non-zero exit status: \n");
  78. exit(1);
  79. }
  80. }
  81. else
  82. {
  83. perror("FAILED: SPE abnormally terminated\n");
  84. exit(1);
  85. }
  86. //} while (rc > 0); // loop until exit or error, and while any stop & signal
  87. pthread_exit(NULL);
  88. }
  89. ///SpuLibspe2Support helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
  90. class SpuLibspe2Support : public btThreadSupportInterface
  91. {
  92. btAlignedObjectArray<btSpuStatus> m_activeSpuStatus;
  93. public:
  94. //Setup and initialize SPU/CELL/Libspe2
  95. SpuLibspe2Support(spe_program_handle_t *speprog,int numThreads);
  96. // SPE program handle ptr.
  97. spe_program_handle_t *program;
  98. // SPE program data
  99. ppu_pthread_data_t data[MAX_SPUS];
  100. //cleanup/shutdown Libspe2
  101. ~SpuLibspe2Support();
  102. ///send messages to SPUs
  103. void sendRequest(uint32_t uiCommand, uint32_t uiArgument0, uint32_t uiArgument1=0);
  104. //check for messages from SPUs
  105. void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
  106. //start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
  107. virtual void startSPU();
  108. //tell the task scheduler we are done with the SPU tasks
  109. virtual void stopSPU();
  110. virtual void setNumTasks(int numTasks)
  111. {
  112. //changing the number of tasks after initialization is not implemented (yet)
  113. }
  114. private:
  115. ///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
  116. void internal_startSPU();
  117. int numThreads;
  118. };
  119. #endif // NOT __SPU__
  120. #endif //USE_LIBSPE2
  121. #endif //BT_SPU_LIBSPE2_SUPPORT_H