1
0

Noise3D generator is now using linear upscaling

Measured 30% performance increase.

git-svn-id: http://mc-server.googlecode.com/svn/trunk@1482 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com
2013-05-14 21:11:40 +00:00
parent 07cfe8ee35
commit 464dcc3764
8 changed files with 241 additions and 56 deletions

View File

@@ -340,8 +340,8 @@ void cBioGenDistortedVoronoi::GenBiomes(int a_ChunkX, int a_ChunkZ, cChunkDef::B
Distort(BaseX + x * 4, BaseZ + z * 4, DistortX[4 * x][4 * z], DistortZ[4 * x][4 * z]);
}
ArrayLinearUpscale2DInPlace(&DistortX[0][0], cChunkDef::Width + 1, cChunkDef::Width + 1, 4, 4);
ArrayLinearUpscale2DInPlace(&DistortZ[0][0], cChunkDef::Width + 1, cChunkDef::Width + 1, 4, 4);
LinearUpscale2DArrayInPlace(&DistortX[0][0], cChunkDef::Width + 1, cChunkDef::Width + 1, 4, 4);
LinearUpscale2DArrayInPlace(&DistortZ[0][0], cChunkDef::Width + 1, cChunkDef::Width + 1, 4, 4);
for (int z = 0; z < cChunkDef::Width; z++)
{
@@ -447,8 +447,8 @@ void cBioGenMultiStepMap::DecideOceanLandMushroom(int a_ChunkX, int a_ChunkZ, cC
{
Distort(BaseX + x * 4, BaseZ + z * 4, DistortX[4 * x][4 * z], DistortZ[4 * x][4 * z], DistortSize);
}
ArrayLinearUpscale2DInPlace(&DistortX[0][0], cChunkDef::Width + 1, cChunkDef::Width + 1, 4, 4);
ArrayLinearUpscale2DInPlace(&DistortZ[0][0], cChunkDef::Width + 1, cChunkDef::Width + 1, 4, 4);
LinearUpscale2DArrayInPlace(&DistortX[0][0], cChunkDef::Width + 1, cChunkDef::Width + 1, 4, 4);
LinearUpscale2DArrayInPlace(&DistortZ[0][0], cChunkDef::Width + 1, cChunkDef::Width + 1, 4, 4);
// Prepare a 9x9 area of neighboring cell seeds
// (assuming that 7x7 cell area is larger than a chunk being generated)
@@ -621,8 +621,8 @@ void cBioGenMultiStepMap::BuildTemperatureHumidityMaps(int a_ChunkX, int a_Chunk
HumidityMap[x + 17 * z] = NoiseH;
} // for x
} // for z
ArrayLinearUpscale2DInPlace(TemperatureMap, 17, 17, 8, 8);
ArrayLinearUpscale2DInPlace(HumidityMap, 17, 17, 8, 8);
LinearUpscale2DArrayInPlace(TemperatureMap, 17, 17, 8, 8);
LinearUpscale2DArrayInPlace(HumidityMap, 17, 17, 8, 8);
// Re-map into integral values in [0 .. 255] range:
for (int idx = 0; idx < ARRAYCOUNT(a_TemperatureMap); idx++)

View File

@@ -442,7 +442,7 @@ void cCompoGenNether::ComposeTerrain(cChunkDesc & a_ChunkDesc)
m_Noise2.IntNoise3DInt(BaseX + INTERPOL_X * x, 0, BaseZ + INTERPOL_Z * z) /
256;
} // for x, z - FloorLo[]
ArrayLinearUpscale2DInPlace(FloorLo, 17, 17, INTERPOL_X, INTERPOL_Z);
LinearUpscale2DArrayInPlace(FloorLo, 17, 17, INTERPOL_X, INTERPOL_Z);
// Interpolate segments:
for (int Segment = 0; Segment < MaxHeight; Segment += SEGMENT_HEIGHT)
@@ -455,7 +455,7 @@ void cCompoGenNether::ComposeTerrain(cChunkDesc & a_ChunkDesc)
m_Noise2.IntNoise3DInt(BaseX + INTERPOL_Z * x, Segment + SEGMENT_HEIGHT, BaseZ + INTERPOL_Z * z) /
256;
} // for x, z - FloorLo[]
ArrayLinearUpscale2DInPlace(FloorHi, 17, 17, INTERPOL_X, INTERPOL_Z);
LinearUpscale2DArrayInPlace(FloorHi, 17, 17, INTERPOL_X, INTERPOL_Z);
// Interpolate between FloorLo and FloorHi:
for (int z = 0; z < 16; z++) for (int x = 0; x < 16; x++)

View File

@@ -158,7 +158,7 @@ void cDistortedHeightmap::GenerateHeightArray(void)
CurFloor[idx + x * INTERPOL_X] = (NOISE_DATATYPE)GetHeightmapAt(DistX, DistZ) + (NOISE_DATATYPE)0.5;
} // for x
} // for z
ArrayLinearUpscale2DInPlace(CurFloor, 17, 17, INTERPOL_X, INTERPOL_Z);
LinearUpscale2DArrayInPlace(CurFloor, 17, 17, INTERPOL_X, INTERPOL_Z);
} // for y
// Finish the 3D linear interpolation by interpolating between each XZ-floors on the Y axis
@@ -381,8 +381,8 @@ void cDistortedHeightmap::UpdateDistortAmps(void)
GetDistortAmpsAt(Biomes, x, z, m_DistortAmpX[x + 17 * z], m_DistortAmpZ[x + 17 * z]);
}
}
ArrayLinearUpscale2DInPlace(m_DistortAmpX, 17, 17, STEPX, STEPZ);
ArrayLinearUpscale2DInPlace(m_DistortAmpZ, 17, 17, STEPX, STEPZ);
LinearUpscale2DArrayInPlace(m_DistortAmpX, 17, 17, STEPX, STEPZ);
LinearUpscale2DArrayInPlace(m_DistortAmpZ, 17, 17, STEPX, STEPZ);
}

View File

@@ -266,7 +266,7 @@ void cHeiGenBiomal::GenHeightMap(int a_ChunkX, int a_ChunkZ, cChunkDef::HeightMa
Height[x + 17 * z] = GetHeightAt(x, z, a_ChunkX, a_ChunkZ, Biomes);
}
}
ArrayLinearUpscale2DInPlace(Height, 17, 17, STEPX, STEPZ);
LinearUpscale2DArrayInPlace(Height, 17, 17, STEPX, STEPZ);
// Copy into the heightmap
for (int z = 0; z < cChunkDef::Width; z++)

View File

@@ -31,7 +31,7 @@ void Debug3DNoise(NOISE_DATATYPE * a_Noise, int a_SizeX, int a_SizeY, int a_Size
unsigned char buf[BUF_SIZE];
for (int x = 0; x < a_SizeX; x++)
{
buf[x] = (unsigned char)(std::min(256, std::max(0, (int)(128 + 32 * a_Noise[idx++]))));
buf[x] = (unsigned char)(std::min(255, std::max(0, (int)(128 + 32 * a_Noise[idx++]))));
}
f1.Write(buf, a_SizeX);
} // for y
@@ -52,7 +52,7 @@ void Debug3DNoise(NOISE_DATATYPE * a_Noise, int a_SizeX, int a_SizeY, int a_Size
unsigned char buf[BUF_SIZE];
for (int x = 0; x < a_SizeX; x++)
{
buf[x] = (unsigned char)(std::min(256, std::max(0, (int)(128 + 32 * a_Noise[idx++]))));
buf[x] = (unsigned char)(std::min(255, std::max(0, (int)(128 + 32 * a_Noise[idx++]))));
}
f2.Write(buf, a_SizeX);
} // for z
@@ -69,6 +69,37 @@ void Debug3DNoise(NOISE_DATATYPE * a_Noise, int a_SizeX, int a_SizeY, int a_Size
/*
// Perform an automatic test of upscaling upon program start (use breakpoints to debug):
class Test
{
public:
Test(void)
{
DoTest1();
}
void DoTest1(void)
{
float In[3 * 3 * 3];
for (int i = 0; i < ARRAYCOUNT(In); i++)
{
In[i] = (float)(i % 5);
}
Debug3DNoise(In, 3, 3, 3, "Upscale in");
float Out[17 * 33 * 35];
LinearUpscale3DArray(In, 3, 3, 3, Out, 8, 16, 17);
Debug3DNoise(Out, 17, 33, 35, "Upscale test");
}
} gTest;
//*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cNoise3DGenerator:
@@ -190,15 +221,15 @@ void cNoise3DGenerator::GenerateBiomes(int a_ChunkX, int a_ChunkZ, cChunkDef::Bi
void cNoise3DGenerator::DoGenerate(int a_ChunkX, int a_ChunkZ, cChunkDesc & a_ChunkDesc)
{
NOISE_DATATYPE Noise[cChunkDef::Width * cChunkDef::Height * cChunkDef::Width];
NOISE_DATATYPE Noise[17 * 257 * 17];
GenerateNoiseArray(a_ChunkX, a_ChunkZ, Noise);
// Output noise into chunk:
int idx = 0;
for (int z = 0; z < cChunkDef::Width; z++)
{
for (int y = 0; y < cChunkDef::Height; y++)
{
int idx = z * 17 * 257 + y * 17;
for (int x = 0; x < cChunkDef::Width; x++)
{
NOISE_DATATYPE n = Noise[idx++];
@@ -226,8 +257,8 @@ void cNoise3DGenerator::DoGenerate(int a_ChunkX, int a_ChunkZ, cChunkDesc & a_Ch
void cNoise3DGenerator::GenerateNoiseArray(int a_ChunkX, int a_ChunkZ, NOISE_DATATYPE * a_OutNoise)
{
NOISE_DATATYPE NoiseO[DIMX * DIMY * DIMZ]; // Output for the Perlin noise
NOISE_DATATYPE NoiseW[DIMX * DIMY * DIMZ]; // Workspace that the noise calculation can use and trash
NOISE_DATATYPE NoiseO[DIM_X * DIM_Y * DIM_Z]; // Output for the Perlin noise
NOISE_DATATYPE NoiseW[DIM_X * DIM_Y * DIM_Z]; // Workspace that the noise calculation can use and trash
// Our noise array has different layout, XZY, instead of regular chunk's XYZ, that's why the coords are "renamed"
NOISE_DATATYPE StartX = ((NOISE_DATATYPE)(a_ChunkX * cChunkDef::Width)) / m_FrequencyX;
@@ -237,51 +268,42 @@ void cNoise3DGenerator::GenerateNoiseArray(int a_ChunkX, int a_ChunkZ, NOISE_DAT
NOISE_DATATYPE StartY = 0;
NOISE_DATATYPE EndY = ((NOISE_DATATYPE)256) / m_FrequencyY;
m_Perlin.Generate3D(NoiseO, DIMX, DIMY, DIMZ, StartX, EndX, StartY, EndY, StartZ, EndZ, NoiseW);
m_Perlin.Generate3D(NoiseO, DIM_X, DIM_Y, DIM_Z, StartX, EndX, StartY, EndY, StartZ, EndZ, NoiseW);
// DEBUG: Debug3DNoise(NoiseO, DIMX, DIMY, DIMZ, Printf("Chunk_%d_%d_orig", a_ChunkX, a_ChunkZ));
// Linearly interpolate the Perlin noise into full-blown chunk dimensions:
LinearInterpolate3DArray(
NoiseO, DIMX, DIMY, DIMZ,
a_OutNoise, cChunkDef::Width, cChunkDef::Height, cChunkDef::Width
);
// DEBUG: Debug3DNoise(a_OutNoise, cChunkDef::Width, cChunkDef::Height, cChunkDef::Width, Printf("Chunk_%d_%d_lerp", a_ChunkX, a_ChunkZ));
// Modify the noise to account for the wanted elevation:
// DEBUG: Debug3DNoise(NoiseO, DIM_X, DIM_Y, DIM_Z, Printf("Chunk_%d_%d_orig", a_ChunkX, a_ChunkZ));
// Precalculate a "height" array:
NOISE_DATATYPE Test1 = 0;
NOISE_DATATYPE HeightS[DIMX * DIMZ]; // Output for the cubic noise heightmap ("source")
NOISE_DATATYPE Test2 = 0;
NOISE_DATATYPE Height[cChunkDef::Width * cChunkDef::Width]; // Lerp-ed heightmap [x + Width * z]
m_Cubic.Generate2D(HeightS, DIMX, DIMZ, StartX / 25, EndX / 25, StartZ / 25, EndZ / 25);
LinearInterpolate2DArray(
HeightS, DIMX, DIMZ,
Height, cChunkDef::Width, cChunkDef::Width
);
NOISE_DATATYPE Height[DIM_X * DIM_Z]; // Output for the cubic noise heightmap ("source")
m_Cubic.Generate2D(Height, DIM_X, DIM_Z, StartX / 25, EndX / 25, StartZ / 25, EndZ / 25);
for (int i = 0; i < ARRAYCOUNT(Height); i++)
{
Height[i] = abs(Height[i]) * m_HeightAmplification + 1;
}
// Modify noise by height data
for (int y = 0; y < cChunkDef::Height; y++)
// Modify the noise by height data:
for (int y = 0; y < DIM_Y; y++)
{
NOISE_DATATYPE AddHeight = (y - m_MidPoint) / 20;
NOISE_DATATYPE AddHeight = (y * UPSCALE_Y - m_MidPoint) / 20;
AddHeight *= AddHeight * AddHeight;
for (int z = 0; z < cChunkDef::Width; z++)
for (int z = 0; z < DIM_Z; z++)
{
NOISE_DATATYPE * CurRow = &(a_OutNoise[y * cChunkDef::Width + z * cChunkDef::Width * cChunkDef::Height]);
for (int x = 0; x < cChunkDef::Width; x++)
NOISE_DATATYPE * CurRow = &(NoiseO[y * DIM_X + z * DIM_X * DIM_Y]);
for (int x = 0; x < DIM_X; x++)
{
CurRow[x] += AddHeight / Height[x + cChunkDef::Width * z];
CurRow[x] += AddHeight / Height[x + DIM_X * z];
}
}
}
// DEBUG: Debug3DNoise(NoiseO, DIM_X, DIM_Y, DIM_Z, Printf("Chunk_%d_%d_hei", a_ChunkX, a_ChunkZ));
// DEBUG: Debug3DNoise(a_OutNoise, cChunkDef::Width, cChunkDef::Height, cChunkDef::Width, Printf("Chunk_%d_%d", a_ChunkX, a_ChunkZ);
// Upscale the Perlin noise into full-blown chunk dimensions:
LinearUpscale3DArray(
NoiseO, DIM_X, DIM_Y, DIM_Z,
a_OutNoise, UPSCALE_X, UPSCALE_Y, UPSCALE_Z
);
// DEBUG: Debug3DNoise(a_OutNoise, 17, 257, 17, Printf("Chunk_%d_%d_lerp", a_ChunkX, a_ChunkZ));
}
@@ -444,7 +466,7 @@ void cNoise3DComposable::GenerateNoiseArrayIfNeeded(int a_ChunkX, int a_ChunkZ)
}
}
// Linear-interpolate this XZ floor:
ArrayLinearUpscale2DInPlace(CurFloor, 17, 17, UPSCALE_X, UPSCALE_Z);
LinearUpscale2DArrayInPlace(CurFloor, 17, 17, UPSCALE_X, UPSCALE_Z);
}
// Finish the 3D linear interpolation by interpolating between each XZ-floors on the Y axis

View File

@@ -29,10 +29,15 @@ public:
virtual void DoGenerate(int a_ChunkX, int a_ChunkZ, cChunkDesc & a_ChunkDesc) override;
protected:
// Linear interpolation dimensions:
static const int DIMX = 5;
static const int DIMY = 65;
static const int DIMZ = 5;
// Linear interpolation step sizes, must be divisors of cChunkDef::Width and cChunkDef::Height, respectively:
static const int UPSCALE_X = 8;
static const int UPSCALE_Y = 4;
static const int UPSCALE_Z = 8;
// Linear interpolation buffer dimensions, calculated from the step sizes:
static const int DIM_X = 1 + cChunkDef::Width / UPSCALE_X;
static const int DIM_Y = 1 + cChunkDef::Height / UPSCALE_Y;
static const int DIM_Z = 1 + cChunkDef::Width / UPSCALE_Z;
cPerlinNoise m_Perlin; // The base 3D noise source for the actual composition
cCubicNoise m_Cubic; // The noise used for heightmap directing

View File

@@ -559,7 +559,7 @@ void cStructGenDirectOverhangs::GenStructures(cChunkDesc & a_ChunkDesc)
m_Noise2.IntNoise3DInt(BaseX + INTERPOL_X * x, BaseY, BaseZ + INTERPOL_Z * z) /
256;
} // for x, z - FloorLo[]
ArrayLinearUpscale2DInPlace(FloorLo, 17, 17, INTERPOL_X, INTERPOL_Z);
LinearUpscale2DArrayInPlace(FloorLo, 17, 17, INTERPOL_X, INTERPOL_Z);
// Interpolate segments:
for (int Segment = BaseY; Segment < MaxHeight; Segment += SEGMENT_HEIGHT)
@@ -572,7 +572,7 @@ void cStructGenDirectOverhangs::GenStructures(cChunkDesc & a_ChunkDesc)
m_Noise2.IntNoise3DInt(BaseX + INTERPOL_Z * x, Segment + SEGMENT_HEIGHT, BaseZ + INTERPOL_Z * z) /
256;
} // for x, z - FloorLo[]
ArrayLinearUpscale2DInPlace(FloorHi, 17, 17, INTERPOL_X, INTERPOL_Z);
LinearUpscale2DArrayInPlace(FloorHi, 17, 17, INTERPOL_X, INTERPOL_Z);
// Interpolate between FloorLo and FloorHi:
for (int z = 0; z < 16; z++) for (int x = 0; x < 16; x++)