SpuDoubleBuffer.h

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2007 Erwin Coumans  http://bulletphysics.com
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 
00017 #ifndef BT_DOUBLE_BUFFER_H
00018 #define BT_DOUBLE_BUFFER_H
00019 
00020 #include "SpuFakeDma.h"
00021 #include "LinearMath/btScalar.h"
00022 
00023 
00025 template<class T, int size>
00026 class DoubleBuffer
00027 {
00028 #if defined(__SPU__) || defined(USE_LIBSPE2)
00029         ATTRIBUTE_ALIGNED128( T m_buffer0[size] ) ;
00030         ATTRIBUTE_ALIGNED128( T m_buffer1[size] ) ;
00031 #else
00032         T m_buffer0[size];
00033         T m_buffer1[size];
00034 #endif
00035         
00036         T *m_frontBuffer;
00037         T *m_backBuffer;
00038 
00039         unsigned int m_dmaTag;
00040         bool m_dmaPending;
00041 public:
00042         bool    isPending() const { return m_dmaPending;}
00043         DoubleBuffer();
00044 
00045         void init ();
00046 
00047         // dma get and put commands
00048         void backBufferDmaGet(uint64_t ea, unsigned int numBytes, unsigned int tag);
00049         void backBufferDmaPut(uint64_t ea, unsigned int numBytes, unsigned int tag);
00050 
00051         // gets pointer to a buffer
00052         T *getFront();
00053         T *getBack();
00054 
00055         // if back buffer dma was started, wait for it to complete
00056         // then move back to front and vice versa
00057         T *swapBuffers();
00058 };
00059 
00060 template<class T, int size>
00061 DoubleBuffer<T,size>::DoubleBuffer()
00062 {
00063         init ();
00064 }
00065 
00066 template<class T, int size>
00067 void DoubleBuffer<T,size>::init()
00068 {
00069         this->m_dmaPending = false;
00070         this->m_frontBuffer = &this->m_buffer0[0];
00071         this->m_backBuffer = &this->m_buffer1[0];
00072 }
00073 
00074 template<class T, int size>
00075 void
00076 DoubleBuffer<T,size>::backBufferDmaGet(uint64_t ea, unsigned int numBytes, unsigned int tag)
00077 {
00078         m_dmaPending = true;
00079         m_dmaTag = tag;
00080         if (numBytes)
00081         {
00082                 m_backBuffer = (T*)cellDmaLargeGetReadOnly(m_backBuffer, ea, numBytes, tag, 0, 0);
00083         }
00084 }
00085 
00086 template<class T, int size>
00087 void
00088 DoubleBuffer<T,size>::backBufferDmaPut(uint64_t ea, unsigned int numBytes, unsigned int tag)
00089 {
00090         m_dmaPending = true;
00091         m_dmaTag = tag;
00092         cellDmaLargePut(m_backBuffer, ea, numBytes, tag, 0, 0);
00093 }
00094 
00095 template<class T, int size>
00096 T *
00097 DoubleBuffer<T,size>::getFront()
00098 {
00099         return m_frontBuffer;
00100 }
00101 
00102 template<class T, int size>
00103 T *
00104 DoubleBuffer<T,size>::getBack()
00105 {
00106         return m_backBuffer;
00107 }
00108 
00109 template<class T, int size>
00110 T *
00111 DoubleBuffer<T,size>::swapBuffers()
00112 {
00113         if (m_dmaPending)
00114         {
00115                 cellDmaWaitTagStatusAll(1<<m_dmaTag);
00116                 m_dmaPending = false;
00117         }
00118 
00119         T *tmp = m_backBuffer;
00120         m_backBuffer = m_frontBuffer;
00121         m_frontBuffer = tmp;
00122 
00123         return m_frontBuffer;
00124 }
00125 
00126 #endif