From c35db25269ea9549b996cd18c65b05a9f8d43387 Mon Sep 17 00:00:00 2001 From: faketruth Date: Mon, 26 Dec 2011 02:13:40 +0000 Subject: Made some functions in cChunk and cNoise inline, this should significantly increase chunk generation speed git-svn-id: http://mc-server.googlecode.com/svn/trunk@115 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- VC2010/MCServer.vcxproj | 1 + VC2010/MCServer.vcxproj.filters | 3 +++ source/cChunk.cpp | 7 ------ source/cChunk.h | 7 +++++- source/cEntity.cpp | 17 +++---------- source/cEntity.h | 2 +- source/cNoise.cpp | 54 +++++++++-------------------------------- source/cNoise.h | 36 +++++++++++++++++++++------ source/cNoise.inc | 54 +++++++++++++++++++++++++++++++++++++++++ 9 files changed, 108 insertions(+), 73 deletions(-) create mode 100644 source/cNoise.inc diff --git a/VC2010/MCServer.vcxproj b/VC2010/MCServer.vcxproj index 04f73eafa..42fd90443 100644 --- a/VC2010/MCServer.vcxproj +++ b/VC2010/MCServer.vcxproj @@ -514,6 +514,7 @@ + diff --git a/VC2010/MCServer.vcxproj.filters b/VC2010/MCServer.vcxproj.filters index a7a523f6c..f2a5cedae 100644 --- a/VC2010/MCServer.vcxproj.filters +++ b/VC2010/MCServer.vcxproj.filters @@ -1302,6 +1302,9 @@ LuaBindings + + cNoise + diff --git a/source/cChunk.cpp b/source/cChunk.cpp index 86107e1ef..f30602010 100644 --- a/source/cChunk.cpp +++ b/source/cChunk.cpp @@ -448,13 +448,6 @@ void cChunk::CreateBlockEntities() } } -unsigned int cChunk::MakeIndex(int x, int y, int z ) -{ - if( x < 16 && x > -1 && y < 128 && y > -1 && z < 16 && z > -1 ) - return y + (z * 128) + (x * 128 * 16); - return 0; -} - char cChunk::GetLight(char* a_Buffer, int a_BlockIdx) { if( a_BlockIdx > -1 && a_BlockIdx < c_NumBlocks ) diff --git a/source/cChunk.h b/source/cChunk.h index 0145908f5..b18a763b2 100644 --- a/source/cChunk.h +++ b/source/cChunk.h @@ -111,7 +111,12 @@ private: void CreateBlockEntities(); - unsigned int MakeIndex(int x, int y, int z ); + inline unsigned int cChunk::MakeIndex(int x, int y, int z ) + { + if( x < 16 && x > -1 && y < 128 && y > -1 && z < 16 && z > -1 ) + return y + (z * 128) + (x * 128 * 16); + return 0; + } bool m_bCalculateLighting; bool m_bCalculateHeightmap; diff --git a/source/cEntity.cpp b/source/cEntity.cpp index f739a3243..ce7117498 100644 --- a/source/cEntity.cpp +++ b/source/cEntity.cpp @@ -52,17 +52,6 @@ void cEntity::Initialize( cWorld* a_World ) m_World->AddEntity( this ); MoveToCorrectChunk(true); - - /* -Not needed because itīs covered by the MoveToCorrectChunk function - cWorld::BlockToChunk( (int)m_Pos->x, (int)m_Pos->y, (int)m_Pos->z, m_ChunkX, m_ChunkY, m_ChunkZ ); - cChunk* Chunk = m_World->GetChunk( m_ChunkX, m_ChunkY, m_ChunkZ ); - if( Chunk ) - { - //LOG("Adding entity %i to chunk %i %i %i", m_UniqueID, Chunk->GetPosX(), Chunk->GetPosY(), Chunk->GetPosZ() ); - Chunk->AddEntity( *this ); - } - */ } void cEntity::WrapRotation() @@ -73,17 +62,17 @@ void cEntity::WrapRotation() while(m_Rot->y < -180.f) m_Rot->y+=360.f; } -void cEntity::MoveToCorrectChunk(bool a_IgnoreOldChunk) +void cEntity::MoveToCorrectChunk(bool a_bIgnoreOldChunk) { if( !m_World ) return; // Entity needs a world to move to a chunk int ChunkX = 0, ChunkY = 0, ChunkZ = 0; cWorld::BlockToChunk( (int)m_Pos->x, (int)m_Pos->y, (int)m_Pos->z, ChunkX, ChunkY, ChunkZ ); - if(a_IgnoreOldChunk || m_ChunkX != ChunkX || m_ChunkY != ChunkY || m_ChunkZ != ChunkZ) + if(a_bIgnoreOldChunk || m_ChunkX != ChunkX || m_ChunkY != ChunkY || m_ChunkZ != ChunkZ) { LOG("From %i %i To %i %i", m_ChunkX, m_ChunkZ, ChunkX, ChunkZ ); cChunk* Chunk = 0; - if(!a_IgnoreOldChunk) + if(!a_bIgnoreOldChunk) Chunk = m_World->GetChunkUnreliable( m_ChunkX, m_ChunkY, m_ChunkZ ); typedef std::list< cClientHandle* > ClientList; diff --git a/source/cEntity.h b/source/cEntity.h index 49689f735..af86c2606 100644 --- a/source/cEntity.h +++ b/source/cEntity.h @@ -86,7 +86,7 @@ public: //tolua_export void WrapRotation(); protected: - void MoveToCorrectChunk(bool a_IgnoreOldChunk = false); + void MoveToCorrectChunk(bool a_bIgnoreOldChunk = false); friend class cReferenceManager; void AddReference( cEntity*& a_EntityPtr ); diff --git a/source/cNoise.cpp b/source/cNoise.cpp index 1bdc52698..82d999fdd 100644 --- a/source/cNoise.cpp +++ b/source/cNoise.cpp @@ -1,7 +1,9 @@ #include "cNoise.h" #include +#if NOISE_USE_SSE #include //_mm_mul_epi32 +#endif #define FAST_FLOOR( x ) ( (x) < 0 ? ((int)x)-1 : ((int)x) ) @@ -15,29 +17,7 @@ cNoise::~cNoise() { } -/**************** - * Random value generator - **/ -float cNoise::IntNoise( int a_X ) const -{ - int x = ((a_X*m_Seed)<<13) ^ a_X; - return ( 1.0f - ( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f); -} - -float cNoise::IntNoise2D( int a_X, int a_Y ) const -{ - int n = a_X + a_Y * 57 + m_Seed*57*57; - n = (n<<13) ^ n; - return ( 1.0f - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f); -} - -float cNoise::IntNoise3D( int a_X, int a_Y, int a_Z ) const -{ - int n = a_X + a_Y * 57 + a_Z * 57*57 + m_Seed*57*57*57; - n = (n<<13) ^ n; - return ( 1.0f - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f); -} - +#if NOISE_USE_SSE /**************** * SSE Random value generator **/ @@ -71,6 +51,7 @@ __m128 cNoise::SSE_IntNoise2D( int a_X1, int a_Y1, int a_X2, int a_Y2, int a_X3, return Result4; } +#endif /*************** @@ -166,6 +147,7 @@ float cNoise::CubicNoise2D( float a_X, float a_Y ) const return CubicInterpolate( interp1, interp2, interp3, interp4, FracY ); } +#if NOISE_USE_SSE float cNoise::SSE_CubicNoise2D( float a_X, float a_Y ) const { const int BaseX = FAST_FLOOR( a_X ); @@ -185,6 +167,7 @@ float cNoise::SSE_CubicNoise2D( float a_X, float a_Y ) const const float FracY = (a_Y) - BaseY; return CubicInterpolate( AllInterp.p[0], AllInterp.p[1], AllInterp.p[2], AllInterp.p[3], FracY ); } +#endif /****************** * Interpolated (and 1 smoothed) noise in 3-dimensions @@ -287,16 +270,8 @@ float cNoise::CubicNoise3D( float a_X, float a_Y, float a_Z ) const /****************** * Private **/ -float cNoise::CubicInterpolate( float a_A, float a_B, float a_C, float a_D, float a_Pct ) const -{ - float P = (a_D - a_C) - (a_A - a_B); - float Q = (a_A - a_B) - P; - float R = a_C - a_A; - float S = a_B; - - return P*(a_Pct*a_Pct*a_Pct) + Q*(a_Pct*a_Pct) + R*a_Pct + S; -} +#if NOISE_USE_SSE __m128 cNoise::CubicInterpolate4( const __m128 & a_A, const __m128 & a_B, const __m128 & a_C, const __m128 & a_D, float a_Pct ) const { const __m128 P = _mm_sub_ps( _mm_sub_ps( a_D, a_C ), _mm_sub_ps( a_A, a_B ) ); @@ -309,15 +284,8 @@ __m128 cNoise::CubicInterpolate4( const __m128 & a_A, const __m128 & a_B, const return _mm_add_ps( _mm_add_ps( _mm_add_ps( _mm_mul_ps(P, Pct3), _mm_mul_ps( Q, Pct2 ) ), _mm_mul_ps( R, Pct ) ), a_B ); } +#endif -float cNoise::CosineInterpolate( float a_A, float a_B, float a_Pct ) const -{ - const float ft = a_Pct * 3.1415927f; - const float f = (1.f - cosf(ft)) * 0.5f; - return a_A*(1-f) + a_B*f; -} - -float cNoise::LinearInterpolate( float a_A, float a_B, float a_Pct ) const -{ - return a_A*(1.f-a_Pct) + a_B*a_Pct; -} \ No newline at end of file +#if NOISE_USE_INLINE +# include "cNoise.inc" +#endif \ No newline at end of file diff --git a/source/cNoise.h b/source/cNoise.h index 9511ab6e4..d4da677f3 100644 --- a/source/cNoise.h +++ b/source/cNoise.h @@ -1,6 +1,19 @@ #pragma once -#include +// Some settings +#define NOISE_USE_INLINE 1 +#define NOISE_USE_SSE 0 + +// Do not touch +#if NOISE_USE_INLINE +# define __NOISE_INLINE__ inline +#else +# define __NOISE_INLINE__ +#endif + +#if NOISE_USE_SSE +# include +#endif class cNoise { @@ -8,10 +21,13 @@ public: cNoise( unsigned int a_Seed ); ~cNoise(); - float IntNoise( int a_X ) const; - float IntNoise2D( int a_X, int a_Y ) const; +#if NOISE_USE_SSE __m128 SSE_IntNoise2D( int a_X1, int a_Y1, int a_X2, int a_Y2, int a_X3, int a_Y3, int a_X4, int a_Y4 ) const; - float IntNoise3D( int a_X, int a_Y, int a_Z ) const; +#endif + + __NOISE_INLINE__ float IntNoise( int a_X ) const; + __NOISE_INLINE__ float IntNoise2D( int a_X, int a_Y ) const; + __NOISE_INLINE__ float IntNoise3D( int a_X, int a_Y, int a_Z ) const; float LinearNoise1D( float a_X ) const; float CosineNoise1D( float a_X ) const; @@ -28,11 +44,17 @@ public: void SetSeed( unsigned int a_Seed ) { m_Seed = a_Seed; } private: - float CubicInterpolate( float a_A, float a_B, float a_C, float a_D, float a_Pct ) const; + __NOISE_INLINE__ float CubicInterpolate( float a_A, float a_B, float a_C, float a_D, float a_Pct ) const; + __NOISE_INLINE__ float CosineInterpolate( float a_A, float a_B, float a_Pct ) const; + __NOISE_INLINE__ float LinearInterpolate( float a_A, float a_B, float a_Pct ) const; + +#if NOISE_USE_SSE __m128 CubicInterpolate4( const __m128 & a_A, const __m128 & a_B, const __m128 & a_C, const __m128 & a_D, float a_Pct ) const; - float CosineInterpolate( float a_A, float a_B, float a_Pct ) const; - float LinearInterpolate( float a_A, float a_B, float a_Pct ) const; +#endif unsigned int m_Seed; }; +#if NOISE_USE_INLINE +# include "cNoise.inc" +#endif diff --git a/source/cNoise.inc b/source/cNoise.inc new file mode 100644 index 000000000..a63bb0c2f --- /dev/null +++ b/source/cNoise.inc @@ -0,0 +1,54 @@ +#ifndef __C_NOISE_INC__ +#define __C_NOISE_INC__ + +#include + +/**************** + * Random value generator + **/ +float cNoise::IntNoise( int a_X ) const +{ + int x = ((a_X*m_Seed)<<13) ^ a_X; + return ( 1.0f - ( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f); +} + +float cNoise::IntNoise2D( int a_X, int a_Y ) const +{ + int n = a_X + a_Y * 57 + m_Seed*57*57; + n = (n<<13) ^ n; + return ( 1.0f - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f); +} + +float cNoise::IntNoise3D( int a_X, int a_Y, int a_Z ) const +{ + int n = a_X + a_Y * 57 + a_Z * 57*57 + m_Seed*57*57*57; + n = (n<<13) ^ n; + return ( 1.0f - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f); +} + +/**************** + * Interpolation functions + **/ +float cNoise::CubicInterpolate( float a_A, float a_B, float a_C, float a_D, float a_Pct ) const +{ + float P = (a_D - a_C) - (a_A - a_B); + float Q = (a_A - a_B) - P; + float R = a_C - a_A; + float S = a_B; + + return P*(a_Pct*a_Pct*a_Pct) + Q*(a_Pct*a_Pct) + R*a_Pct + S; +} + +float cNoise::CosineInterpolate( float a_A, float a_B, float a_Pct ) const +{ + const float ft = a_Pct * 3.1415927f; + const float f = (1.f - cosf(ft)) * 0.5f; + return a_A*(1-f) + a_B*f; +} + +float cNoise::LinearInterpolate( float a_A, float a_B, float a_Pct ) const +{ + return a_A*(1.f-a_Pct) + a_B*a_Pct; +} + +#endif \ No newline at end of file -- cgit v1.2.3