1
0

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
This commit is contained in:
faketruth 2011-12-26 02:13:40 +00:00
parent 4c76ca986c
commit c35db25269
9 changed files with 108 additions and 73 deletions

View File

@ -514,6 +514,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\source\AllToLua.pkg" /> <None Include="..\source\AllToLua.pkg" />
<None Include="..\source\cNoise.inc" />
<None Include="icon.ico" /> <None Include="icon.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1302,6 +1302,9 @@
<Filter>LuaBindings</Filter> <Filter>LuaBindings</Filter>
</None> </None>
<None Include="icon.ico" /> <None Include="icon.ico" />
<None Include="..\source\cNoise.inc">
<Filter>cNoise</Filter>
</None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="MCServer.rc" /> <ResourceCompile Include="MCServer.rc" />

View File

@ -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) char cChunk::GetLight(char* a_Buffer, int a_BlockIdx)
{ {
if( a_BlockIdx > -1 && a_BlockIdx < c_NumBlocks ) if( a_BlockIdx > -1 && a_BlockIdx < c_NumBlocks )

View File

@ -111,7 +111,12 @@ private:
void CreateBlockEntities(); 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_bCalculateLighting;
bool m_bCalculateHeightmap; bool m_bCalculateHeightmap;

View File

@ -52,17 +52,6 @@ void cEntity::Initialize( cWorld* a_World )
m_World->AddEntity( this ); m_World->AddEntity( this );
MoveToCorrectChunk(true); 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() void cEntity::WrapRotation()
@ -73,17 +62,17 @@ void cEntity::WrapRotation()
while(m_Rot->y < -180.f) m_Rot->y+=360.f; 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 if( !m_World ) return; // Entity needs a world to move to a chunk
int ChunkX = 0, ChunkY = 0, ChunkZ = 0; int ChunkX = 0, ChunkY = 0, ChunkZ = 0;
cWorld::BlockToChunk( (int)m_Pos->x, (int)m_Pos->y, (int)m_Pos->z, ChunkX, ChunkY, ChunkZ ); 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 ); LOG("From %i %i To %i %i", m_ChunkX, m_ChunkZ, ChunkX, ChunkZ );
cChunk* Chunk = 0; cChunk* Chunk = 0;
if(!a_IgnoreOldChunk) if(!a_bIgnoreOldChunk)
Chunk = m_World->GetChunkUnreliable( m_ChunkX, m_ChunkY, m_ChunkZ ); Chunk = m_World->GetChunkUnreliable( m_ChunkX, m_ChunkY, m_ChunkZ );
typedef std::list< cClientHandle* > ClientList; typedef std::list< cClientHandle* > ClientList;

View File

@ -86,7 +86,7 @@ public: //tolua_export
void WrapRotation(); void WrapRotation();
protected: protected:
void MoveToCorrectChunk(bool a_IgnoreOldChunk = false); void MoveToCorrectChunk(bool a_bIgnoreOldChunk = false);
friend class cReferenceManager; friend class cReferenceManager;
void AddReference( cEntity*& a_EntityPtr ); void AddReference( cEntity*& a_EntityPtr );

View File

@ -1,7 +1,9 @@
#include "cNoise.h" #include "cNoise.h"
#include <math.h> #include <math.h>
#if NOISE_USE_SSE
#include <smmintrin.h> //_mm_mul_epi32 #include <smmintrin.h> //_mm_mul_epi32
#endif
#define FAST_FLOOR( x ) ( (x) < 0 ? ((int)x)-1 : ((int)x) ) #define FAST_FLOOR( x ) ( (x) < 0 ? ((int)x)-1 : ((int)x) )
@ -15,29 +17,7 @@ cNoise::~cNoise()
{ {
} }
/**************** #if NOISE_USE_SSE
* 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);
}
/**************** /****************
* SSE Random value generator * 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; return Result4;
} }
#endif
/*************** /***************
@ -166,6 +147,7 @@ float cNoise::CubicNoise2D( float a_X, float a_Y ) const
return CubicInterpolate( interp1, interp2, interp3, interp4, FracY ); return CubicInterpolate( interp1, interp2, interp3, interp4, FracY );
} }
#if NOISE_USE_SSE
float cNoise::SSE_CubicNoise2D( float a_X, float a_Y ) const float cNoise::SSE_CubicNoise2D( float a_X, float a_Y ) const
{ {
const int BaseX = FAST_FLOOR( a_X ); 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; const float FracY = (a_Y) - BaseY;
return CubicInterpolate( AllInterp.p[0], AllInterp.p[1], AllInterp.p[2], AllInterp.p[3], FracY ); return CubicInterpolate( AllInterp.p[0], AllInterp.p[1], AllInterp.p[2], AllInterp.p[3], FracY );
} }
#endif
/****************** /******************
* Interpolated (and 1 smoothed) noise in 3-dimensions * 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 * 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 __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 ) ); 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 ); 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 #if NOISE_USE_INLINE
{ # include "cNoise.inc"
const float ft = a_Pct * 3.1415927f; #endif
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;
}

View File

@ -1,6 +1,19 @@
#pragma once #pragma once
// 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 <emmintrin.h> # include <emmintrin.h>
#endif
class cNoise class cNoise
{ {
@ -8,10 +21,13 @@ public:
cNoise( unsigned int a_Seed ); cNoise( unsigned int a_Seed );
~cNoise(); ~cNoise();
float IntNoise( int a_X ) const; #if NOISE_USE_SSE
float IntNoise2D( int a_X, int a_Y ) const;
__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; __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 LinearNoise1D( float a_X ) const;
float CosineNoise1D( 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; } void SetSeed( unsigned int a_Seed ) { m_Seed = a_Seed; }
private: 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; __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; #endif
float LinearInterpolate( float a_A, float a_B, float a_Pct ) const;
unsigned int m_Seed; unsigned int m_Seed;
}; };
#if NOISE_USE_INLINE
# include "cNoise.inc"
#endif

54
source/cNoise.inc Normal file
View File

@ -0,0 +1,54 @@
#ifndef __C_NOISE_INC__
#define __C_NOISE_INC__
#include <math.h>
/****************
* 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