From: <axl...@us...> - 2010-04-28 02:05:32
|
Revision: 707 http://hgengine.svn.sourceforge.net/hgengine/?rev=707&view=rev Author: axlecrusher Date: 2010-04-28 02:05:26 +0000 (Wed, 28 Apr 2010) Log Message: ----------- preallocate matrix space in one giant chunk. Modified Paths: -------------- Mercury2/src/MercuryMatrix.cpp Mercury2/src/MercuryMatrix.h Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2010-04-27 21:39:01 UTC (rev 706) +++ Mercury2/src/MercuryMatrix.cpp 2010-04-28 02:05:26 UTC (rev 707) @@ -1,13 +1,52 @@ #include "MercuryMatrix.h" #include <MercuryLog.h> +MercuryMatrixMemory& MercuryMatrixMemory::Instance() +{ + static MercuryMatrixMemory* mmm = NULL; + + if (mmm==NULL) + { + mmm = new MercuryMatrixMemory(); + mmm->Init(); + } + + return *mmm; +} + +void MercuryMatrixMemory::Init() +{ + MSemaphoreLock lock(&m_lock); + for (unsigned int i = 0; i < rows;i++) + m_free.push_back( m_data+i ); +} + +FloatRow* MercuryMatrixMemory::GetNewMatrix() +{ + MatrixArray* m = (MatrixArray*)0xdeadbeef; + MSemaphoreLock lock(&m_lock); + if ( m_free.begin() != m_free.end() ) + { + m = m_free.front(); + m_free.pop_front(); + } + return (FloatRow*)m; +} + +void MercuryMatrixMemory::FreeMatrix(FloatRow* m) +{ + MSemaphoreLock lock(&m_lock); + m_free.push_back((MatrixArray*)m); +} +/* VC_ALIGN(16) float base_matrix_identity[16] CC_ALIGN(16) = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }; - +*/ MercuryMatrix::MercuryMatrix() + :m_matrix(0) {/* #ifdef USE_SSE m_matrix[0] = _mm_load1_ps( &base_matrix_identity[0] ); @@ -19,9 +58,24 @@ #endif */ // *this = Identity(); + m_matrix = MercuryMatrixMemory::Instance().GetNewMatrix(); LoadIdentity(); } +MercuryMatrix::MercuryMatrix(const MercuryMatrix& m) + :m_matrix(0) +{ + m_matrix = MercuryMatrixMemory::Instance().GetNewMatrix(); + Copy16f(m_matrix, m.m_matrix); +} + +MercuryMatrix::~MercuryMatrix() +{ + if (m_matrix) + MercuryMatrixMemory::Instance().FreeMatrix( m_matrix ); + m_matrix = NULL; +} + const MercuryMatrix& MercuryMatrix::operator=(const MercuryMatrix& m) { Copy16f(m_matrix, m.m_matrix); @@ -44,7 +98,8 @@ const MercuryMatrix& MercuryMatrix::Identity() { - static bool bSetIdentity = false; + return IdentityMatrix; +/* static bool bSetIdentity = false; if (!bSetIdentity) { @@ -60,11 +115,14 @@ } return IdentityMatrix; + */ } void MercuryMatrix::LoadIdentity() { - Copy16f(m_matrix, base_matrix_identity ); + for (uint8_t x=0;x<4;++x) + for (uint8_t y=0;y<4;++y) + m_matrix[x][y] = (x==y)?1.0f:0.0f; } void MercuryMatrix::Translate(float x, float y, float z) Modified: Mercury2/src/MercuryMatrix.h =================================================================== --- Mercury2/src/MercuryMatrix.h 2010-04-27 21:39:01 UTC (rev 706) +++ Mercury2/src/MercuryMatrix.h 2010-04-28 02:05:26 UTC (rev 707) @@ -7,25 +7,49 @@ #include <MercuryVertex.h> #include <MQuaternion.h> +#include <list> +#include <MSemaphore.h> + +///Memory holder for matrices +class MercuryMatrixMemory +{ + /* Allocates all matrix space as one contigous block + to try to take advantage of data prefetching. Some matrix data should get a + free ride into the CPU cache. */ + public: + void Init(); + static MercuryMatrixMemory& Instance(); + FloatRow* GetNewMatrix(); + void FreeMatrix(FloatRow* m); + private: + typedef FloatRow MatrixArray[4]; //64kb + static const unsigned int rows = 1024; //1024 matrices * 64bytes each = 64kb + std::list< MatrixArray* > m_free; + MatrixArray m_data[rows]; + MSemaphore m_lock; +}; + ///General Purpose 4x4 row-major matrix - VC_ALIGN(16) class MercuryMatrix +class MercuryMatrix { private: ///[row][column] (The internal matrix) // float m_matrix[4][4]; - FloatRow m_matrix[4]; +// FloatRow m_matrix[4]; + FloatRow* m_matrix; static MercuryMatrix IdentityMatrix; public: MercuryMatrix(); - inline MercuryMatrix(const MercuryMatrix& m) { *this = m; } - inline float* operator[](unsigned int i) { return (float*)&m_matrix[i]; } + MercuryMatrix(const MercuryMatrix& m); + ~MercuryMatrix(); + inline float* operator[](unsigned int i) { return m_matrix[i]; } ///Allow typecasting to float * for use in APIs - inline const float* operator[](unsigned int i) const { return (float*)&m_matrix[i]; } + inline const float* operator[](unsigned int i) const { return m_matrix[i]; } const MercuryMatrix& operator=(const MercuryMatrix& m); const MercuryMatrix& operator=(const float* m); - inline float* Ptr() { return (float*)&m_matrix; } - inline const float* Ptr() const { return (float*)&m_matrix; } + inline float* Ptr() { return (float*)m_matrix; } + inline const float* Ptr() const { return (const float*)m_matrix; } MercuryMatrix operator*(const MercuryMatrix& m) const; MercuryMatrix& operator*=(const MercuryMatrix& m); @@ -55,13 +79,8 @@ void LoadIdentity(); void Print() const; -} CC_ALIGN(16); +}; -//namespace MercuryMath -//{ -//} - - #endif /* This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |