SpuSampleTaskProcess.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. //#define __CELLOS_LV2__ 1
  14. #define USE_SAMPLE_PROCESS 1
  15. #ifdef USE_SAMPLE_PROCESS
  16. #include "SpuSampleTaskProcess.h"
  17. #include <stdio.h>
  18. #ifdef __SPU__
  19. void SampleThreadFunc(void* userPtr,void* lsMemory)
  20. {
  21. //do nothing
  22. printf("hello world\n");
  23. }
  24. void* SamplelsMemoryFunc()
  25. {
  26. //don't create local store memory, just return 0
  27. return 0;
  28. }
  29. #else
  30. #include "btThreadSupportInterface.h"
  31. //# include "SPUAssert.h"
  32. #include <string.h>
  33. extern "C" {
  34. extern char SPU_SAMPLE_ELF_SYMBOL[];
  35. }
  36. SpuSampleTaskProcess::SpuSampleTaskProcess(btThreadSupportInterface* threadInterface, int maxNumOutstandingTasks)
  37. :m_threadInterface(threadInterface),
  38. m_maxNumOutstandingTasks(maxNumOutstandingTasks)
  39. {
  40. m_taskBusy.resize(m_maxNumOutstandingTasks);
  41. m_spuSampleTaskDesc.resize(m_maxNumOutstandingTasks);
  42. for (int i = 0; i < m_maxNumOutstandingTasks; i++)
  43. {
  44. m_taskBusy[i] = false;
  45. }
  46. m_numBusyTasks = 0;
  47. m_currentTask = 0;
  48. m_initialized = false;
  49. m_threadInterface->startSPU();
  50. }
  51. SpuSampleTaskProcess::~SpuSampleTaskProcess()
  52. {
  53. m_threadInterface->stopSPU();
  54. }
  55. void SpuSampleTaskProcess::initialize()
  56. {
  57. #ifdef DEBUG_SPU_TASK_SCHEDULING
  58. printf("SpuSampleTaskProcess::initialize()\n");
  59. #endif //DEBUG_SPU_TASK_SCHEDULING
  60. for (int i = 0; i < m_maxNumOutstandingTasks; i++)
  61. {
  62. m_taskBusy[i] = false;
  63. }
  64. m_numBusyTasks = 0;
  65. m_currentTask = 0;
  66. m_initialized = true;
  67. }
  68. void SpuSampleTaskProcess::issueTask(void* sampleMainMemPtr,int sampleValue,int sampleCommand)
  69. {
  70. #ifdef DEBUG_SPU_TASK_SCHEDULING
  71. printf("SpuSampleTaskProcess::issueTask (m_currentTask= %d\)n", m_currentTask);
  72. #endif //DEBUG_SPU_TASK_SCHEDULING
  73. m_taskBusy[m_currentTask] = true;
  74. m_numBusyTasks++;
  75. SpuSampleTaskDesc& taskDesc = m_spuSampleTaskDesc[m_currentTask];
  76. {
  77. // send task description in event message
  78. // no error checking here...
  79. // but, currently, event queue can be no larger than NUM_WORKUNIT_TASKS.
  80. taskDesc.m_mainMemoryPtr = reinterpret_cast<uint64_t>(sampleMainMemPtr);
  81. taskDesc.m_sampleValue = sampleValue;
  82. taskDesc.m_sampleCommand = sampleCommand;
  83. //some bookkeeping to recognize finished tasks
  84. taskDesc.m_taskId = m_currentTask;
  85. }
  86. m_threadInterface->sendRequest(1, (ppu_address_t) &taskDesc, m_currentTask);
  87. // if all tasks busy, wait for spu event to clear the task.
  88. if (m_numBusyTasks >= m_maxNumOutstandingTasks)
  89. {
  90. unsigned int taskId;
  91. unsigned int outputSize;
  92. for (int i=0;i<m_maxNumOutstandingTasks;i++)
  93. {
  94. if (m_taskBusy[i])
  95. {
  96. taskId = i;
  97. break;
  98. }
  99. }
  100. m_threadInterface->waitForResponse(&taskId, &outputSize);
  101. //printf("PPU: after issue, received event: %u %d\n", taskId, outputSize);
  102. postProcess(taskId, outputSize);
  103. m_taskBusy[taskId] = false;
  104. m_numBusyTasks--;
  105. }
  106. // find new task buffer
  107. for (int i = 0; i < m_maxNumOutstandingTasks; i++)
  108. {
  109. if (!m_taskBusy[i])
  110. {
  111. m_currentTask = i;
  112. break;
  113. }
  114. }
  115. }
  116. ///Optional PPU-size post processing for each task
  117. void SpuSampleTaskProcess::postProcess(int taskId, int outputSize)
  118. {
  119. }
  120. void SpuSampleTaskProcess::flush()
  121. {
  122. #ifdef DEBUG_SPU_TASK_SCHEDULING
  123. printf("\nSpuCollisionTaskProcess::flush()\n");
  124. #endif //DEBUG_SPU_TASK_SCHEDULING
  125. // all tasks are issued, wait for all tasks to be complete
  126. while(m_numBusyTasks > 0)
  127. {
  128. // Consolidating SPU code
  129. unsigned int taskId;
  130. unsigned int outputSize;
  131. for (int i=0;i<m_maxNumOutstandingTasks;i++)
  132. {
  133. if (m_taskBusy[i])
  134. {
  135. taskId = i;
  136. break;
  137. }
  138. }
  139. {
  140. m_threadInterface->waitForResponse(&taskId, &outputSize);
  141. }
  142. //printf("PPU: flushing, received event: %u %d\n", taskId, outputSize);
  143. postProcess(taskId, outputSize);
  144. m_taskBusy[taskId] = false;
  145. m_numBusyTasks--;
  146. }
  147. }
  148. #endif
  149. #endif //USE_SAMPLE_PROCESS