1
0

SpuDoubleBuffer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_DOUBLE_BUFFER_H
  14. #define BT_DOUBLE_BUFFER_H
  15. #include "SpuFakeDma.h"
  16. #include "bullet/LinearMath/btScalar.h"
  17. ///DoubleBuffer
  18. template<class T, int size>
  19. class DoubleBuffer
  20. {
  21. #if defined(__SPU__) || defined(USE_LIBSPE2)
  22. ATTRIBUTE_ALIGNED128( T m_buffer0[size] ) ;
  23. ATTRIBUTE_ALIGNED128( T m_buffer1[size] ) ;
  24. #else
  25. T m_buffer0[size];
  26. T m_buffer1[size];
  27. #endif
  28. T *m_frontBuffer;
  29. T *m_backBuffer;
  30. unsigned int m_dmaTag;
  31. bool m_dmaPending;
  32. public:
  33. bool isPending() const { return m_dmaPending;}
  34. DoubleBuffer();
  35. void init ();
  36. // dma get and put commands
  37. void backBufferDmaGet(uint64_t ea, unsigned int numBytes, unsigned int tag);
  38. void backBufferDmaPut(uint64_t ea, unsigned int numBytes, unsigned int tag);
  39. // gets pointer to a buffer
  40. T *getFront();
  41. T *getBack();
  42. // if back buffer dma was started, wait for it to complete
  43. // then move back to front and vice versa
  44. T *swapBuffers();
  45. };
  46. template<class T, int size>
  47. DoubleBuffer<T,size>::DoubleBuffer()
  48. {
  49. init ();
  50. }
  51. template<class T, int size>
  52. void DoubleBuffer<T,size>::init()
  53. {
  54. this->m_dmaPending = false;
  55. this->m_frontBuffer = &this->m_buffer0[0];
  56. this->m_backBuffer = &this->m_buffer1[0];
  57. }
  58. template<class T, int size>
  59. void
  60. DoubleBuffer<T,size>::backBufferDmaGet(uint64_t ea, unsigned int numBytes, unsigned int tag)
  61. {
  62. m_dmaPending = true;
  63. m_dmaTag = tag;
  64. if (numBytes)
  65. {
  66. m_backBuffer = (T*)cellDmaLargeGetReadOnly(m_backBuffer, ea, numBytes, tag, 0, 0);
  67. }
  68. }
  69. template<class T, int size>
  70. void
  71. DoubleBuffer<T,size>::backBufferDmaPut(uint64_t ea, unsigned int numBytes, unsigned int tag)
  72. {
  73. m_dmaPending = true;
  74. m_dmaTag = tag;
  75. cellDmaLargePut(m_backBuffer, ea, numBytes, tag, 0, 0);
  76. }
  77. template<class T, int size>
  78. T *
  79. DoubleBuffer<T,size>::getFront()
  80. {
  81. return m_frontBuffer;
  82. }
  83. template<class T, int size>
  84. T *
  85. DoubleBuffer<T,size>::getBack()
  86. {
  87. return m_backBuffer;
  88. }
  89. template<class T, int size>
  90. T *
  91. DoubleBuffer<T,size>::swapBuffers()
  92. {
  93. if (m_dmaPending)
  94. {
  95. cellDmaWaitTagStatusAll(1<<m_dmaTag);
  96. m_dmaPending = false;
  97. }
  98. T *tmp = m_backBuffer;
  99. m_backBuffer = m_frontBuffer;
  100. m_frontBuffer = tmp;
  101. return m_frontBuffer;
  102. }
  103. #endif