minor perf improvements

This commit is contained in:
dfsek
2021-01-13 14:03:52 -07:00
parent 54f732176d
commit d1c018690d
7 changed files with 40 additions and 52 deletions
@@ -1616,30 +1616,29 @@ public class FastNoiseLite implements NoiseSampler {
return lerp(xf0, xf1, ys) * 1.4247691104677813f; return lerp(xf0, xf1, ys) * 1.4247691104677813f;
} }
private static long hash(long in) { long murmur64(long h) {
in = (in + 0x7ed55d16) + (in << 12); h ^= h >>> 33;
in = (in ^ 0xc761c23c) ^ (in >> 19); h *= 0xff51afd7ed558ccdL;
in = (in + 0x165667b1) + (in << 5); h ^= h >>> 33;
in = (in + 0xd3a2646c) ^ (in << 9); h *= 0xc4ceb9fe1a85ec53L;
in = (in + 0xfd7046c5) + (in << 3); h ^= h >>> 33;
in = (in ^ 0xb55a4f09) ^ (in >> 16); return h;
return in;
} }
private double singleWhiteNoise(int seed, double x, double y, double z) { private double singleWhiteNoise(int seed, double x, double y, double z) {
long hashX = hash(Double.doubleToRawLongBits(x) ^ seed); long hashX = murmur64(Double.doubleToRawLongBits(x) ^ seed);
long hashZ = hash(Double.doubleToRawLongBits(y) ^ seed); long hashZ = murmur64(Double.doubleToRawLongBits(y) ^ seed);
long hash = (((hashX ^ (hashX >>> 32)) + ((hashZ ^ (hashZ >>> 32)) << 32)) ^ seed) + Double.doubleToRawLongBits(z); long hash = (((hashX ^ (hashX >>> 32)) + ((hashZ ^ (hashZ >>> 32)) << 32)) ^ seed) + Double.doubleToRawLongBits(z);
long base = ((hash(hash)) & 0x000fffffffffffffL) long base = ((murmur64(hash)) & 0x000fffffffffffffL)
+ POSITIVE_POW1; // Sign and exponent + POSITIVE_POW1; // Sign and exponent
return (Double.longBitsToDouble(base) - 1.5) * 2; return (Double.longBitsToDouble(base) - 1.5) * 2;
} }
private double singleWhiteNoise(int seed, double x, double y) { private double singleWhiteNoise(int seed, double x, double y) {
long hashX = hash(Double.doubleToRawLongBits(x) ^ seed); long hashX = murmur64(Double.doubleToRawLongBits(x) ^ seed);
long hashZ = hash(Double.doubleToRawLongBits(y) ^ seed); long hashZ = murmur64(Double.doubleToRawLongBits(y) ^ seed);
long hash = ((hashX ^ (hashX >>> 32)) + ((hashZ ^ (hashZ >>> 32)) << 32)) ^ seed; long hash = ((hashX ^ (hashX >>> 32)) + ((hashZ ^ (hashZ >>> 32)) << 32)) ^ seed;
long base = (hash(hash) & 0x000fffffffffffffL) long base = (murmur64(hash) & 0x000fffffffffffffL)
+ POSITIVE_POW1; // Sign and exponent + POSITIVE_POW1; // Sign and exponent
return (Double.longBitsToDouble(base) - 1.5) * 2; return (Double.longBitsToDouble(base) - 1.5) * 2;
} }
@@ -41,7 +41,7 @@ public class StandardBiomeProvider implements BiomeProvider {
} }
} }
); );
private int resolution = 4; private int resolution = 1;
protected StandardBiomeProvider(BiomePipeline pipeline) { protected StandardBiomeProvider(BiomePipeline pipeline) {
this.pipeline = pipeline; this.pipeline = pipeline;
@@ -21,7 +21,7 @@ public class BiomePipeline {
} }
public BiomeHolder getBiomes(int x, int z) { public BiomeHolder getBiomes(int x, int z) {
BiomeHolder holder = new TerraBiomeHolder(25, new Vector2(x * (init - 1), z * (init - 1))); BiomeHolder holder = new TerraBiomeHolder(init, new Vector2(x * (init - 1), z * (init - 1)));
holder.fill(source); holder.fill(source);
for(Stage stage : stages) holder = stage.apply(holder); for(Stage stage : stages) holder = stage.apply(holder);
return holder; return holder;
@@ -12,6 +12,7 @@ public class TerraBiomeHolder implements BiomeHolder {
private TerraBiome[][] biomes; private TerraBiome[][] biomes;
public TerraBiomeHolder(int width, Vector2 origin) { public TerraBiomeHolder(int width, Vector2 origin) {
if(width % 2 == 0) throw new IllegalArgumentException();
this.width = width; this.width = width;
biomes = new TerraBiome[width][width]; biomes = new TerraBiome[width][width];
this.origin = origin; this.origin = origin;
@@ -48,11 +49,7 @@ public class TerraBiomeHolder implements BiomeHolder {
public void mutate(BiomeMutator mutator) { public void mutate(BiomeMutator mutator) {
for(int x = 0; x < width; x++) { for(int x = 0; x < width; x++) {
for(int z = 0; z < width; z++) { for(int z = 0; z < width; z++) {
BiomeMutator.ViewPoint viewPoint = new BiomeMutator.ViewPoint(new TerraBiome[][] { BiomeMutator.ViewPoint viewPoint = new BiomeMutator.ViewPoint(this, x, z);
{getBiomeRaw(x - 1, z + 1), getBiomeRaw(x, z + 1), getBiomeRaw(x + 1, z + 1)},
{getBiomeRaw(x - 1, z), getBiomeRaw(x, z), getBiomeRaw(x + 1, z)},
{getBiomeRaw(x - 1, z - 1), getBiomeRaw(x, z - 1), getBiomeRaw(x + 1, z - 1)}
});
biomes[x][z] = mutator.mutate(viewPoint, x + origin.getX(), z + origin.getZ()); biomes[x][z] = mutator.mutate(viewPoint, x + origin.getX(), z + origin.getZ());
} }
} }
@@ -67,11 +64,6 @@ public class TerraBiomeHolder implements BiomeHolder {
} }
} }
private TerraBiome getBiomeRaw(int x, int z) {
if(x >= width || z >= width || x < 0 || z < 0) return null;
return biomes[x][z];
}
@Override @Override
public TerraBiome getBiome(int x, int z) { public TerraBiome getBiome(int x, int z) {
if(x >= width || z >= width || x < 0 || z < 0) return null; if(x >= width || z >= width || x < 0 || z < 0) return null;
@@ -1,19 +1,25 @@
package com.dfsek.terra.biome.pipeline.mutator; package com.dfsek.terra.biome.pipeline.mutator;
import com.dfsek.terra.api.world.biome.TerraBiome; import com.dfsek.terra.api.world.biome.TerraBiome;
import com.dfsek.terra.biome.pipeline.BiomeHolder;
public interface BiomeMutator { public interface BiomeMutator {
TerraBiome mutate(ViewPoint viewPoint, double x, double z); TerraBiome mutate(ViewPoint viewPoint, double x, double z);
class ViewPoint { class ViewPoint {
private final TerraBiome[][] biomes; private final BiomeHolder biomes;
private final int offX;
private final int offZ;
public ViewPoint(TerraBiome[][] biomes) { public ViewPoint(BiomeHolder biomes, int offX, int offZ) {
this.biomes = biomes; this.biomes = biomes;
this.offX = offX;
this.offZ = offZ;
} }
public TerraBiome getBiome(int x, int z) { public TerraBiome getBiome(int x, int z) {
return biomes[x + 1][z + 1]; return biomes.getBiome(x + offX, z + offZ);
} }
} }
} }
@@ -146,6 +146,8 @@ public class ConfigPack implements LoaderRegistrar {
selfLoader.load(template, stream); selfLoader.load(template, stream);
load(new ZIPLoader(file), l, main); load(new ZIPLoader(file), l, main);
template.getProviderBuilder().build(0); // Build dummy provider to catch errors at load time.
} }
private void load(Loader loader, long start, TerraPlugin main) throws ConfigException { private void load(Loader loader, long start, TerraPlugin main) throws ConfigException {
+12 -23
View File
@@ -6,7 +6,8 @@ import com.dfsek.terra.api.math.noise.samplers.NoiseSampler;
import com.dfsek.terra.api.platform.world.World; import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.world.biome.Generator; import com.dfsek.terra.api.world.biome.Generator;
import com.dfsek.terra.api.world.biome.TerraBiome; import com.dfsek.terra.api.world.biome.TerraBiome;
import com.dfsek.terra.biome.pipeline.BiomeHolder; import com.dfsek.terra.biome.BiomeProvider;
import com.dfsek.terra.biome.StandardBiomeProvider;
import com.dfsek.terra.biome.pipeline.BiomePipeline; import com.dfsek.terra.biome.pipeline.BiomePipeline;
import com.dfsek.terra.biome.pipeline.expand.FractalExpander; import com.dfsek.terra.biome.pipeline.expand.FractalExpander;
import com.dfsek.terra.biome.pipeline.mutator.BorderMutator; import com.dfsek.terra.biome.pipeline.mutator.BorderMutator;
@@ -60,9 +61,8 @@ public class BiomeTest {
BiomeSource source = new RandomSource(climate, sourceSampler); BiomeSource source = new RandomSource(climate, sourceSampler);
long s = System.nanoTime();
BiomePipeline pipeline = new BiomePipeline.BiomePipelineBuilder(20) BiomeProvider provider = new StandardBiomeProvider.StandardBiomeProviderBuilder((seed) -> new BiomePipeline.BiomePipelineBuilder(3)
.addStage(new ExpanderStage(new FractalExpander(whiteNoise(1)))) .addStage(new ExpanderStage(new FractalExpander(whiteNoise(1))))
.addStage(new MutatorStage(new ReplaceMutator("OCEAN_TEMP", oceanBiomes, whiteNoise(243)))) .addStage(new MutatorStage(new ReplaceMutator("OCEAN_TEMP", oceanBiomes, whiteNoise(243))))
.addStage(new MutatorStage(new ReplaceMutator("LAND_TEMP", landBiomes, whiteNoise(243)))) .addStage(new MutatorStage(new ReplaceMutator("LAND_TEMP", landBiomes, whiteNoise(243))))
@@ -73,21 +73,10 @@ public class BiomeTest {
.addStage(new MutatorStage(new BorderMutator(Sets.newHashSet("OCEAN"), "LAND", whiteNoise(1234), beachBiomes))) .addStage(new MutatorStage(new BorderMutator(Sets.newHashSet("OCEAN"), "LAND", whiteNoise(1234), beachBiomes)))
.addStage(new ExpanderStage(new FractalExpander(whiteNoise(5)))) .addStage(new ExpanderStage(new FractalExpander(whiteNoise(5))))
.addStage(new MutatorStage(new SmoothMutator(whiteNoise(6)))) .addStage(new MutatorStage(new SmoothMutator(whiteNoise(6))))
.build(source); .build(source)).build(0);
BiomeHolder holder = pipeline.getBiomes(0, 0);
BiomeHolder holder2 = pipeline.getBiomes(1, 0);
BiomeHolder holder3 = pipeline.getBiomes(0, 1);
BiomeHolder holder4 = pipeline.getBiomes(1, 1);
long e = System.nanoTime(); int size = 1000;
int size = pipeline.getSize();
int og = size;
size *= 2;
double time = e - s;
time /= 1000000;
System.out.println(time + "ms for " + size + "x" + size);
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
@@ -95,17 +84,17 @@ public class BiomeTest {
System.out.println(size); System.out.println(size);
long s = System.nanoTime();
for(int x = 0; x < size; x++) { for(int x = 0; x < size; x++) {
for(int z = 0; z < size; z++) { for(int z = 0; z < size; z++) {
if(x < og) { image.setRGB(x, z, provider.getBiome(x, z).getColor());
if(z < og) image.setRGB(x, z, holder.getBiome(x, z).getColor());
else image.setRGB(x, z, holder3.getBiome(x, z - og).getColor());
} else {
if(z < og) image.setRGB(x, z, holder2.getBiome(x - og, z).getColor());
else image.setRGB(x, z, holder4.getBiome(x - og, z - og).getColor());
}
} }
} }
long e = System.nanoTime();
double time = e - s;
time /= 1000000;
System.out.println(time + "ms for " + size + "x" + size);
JFrame frame = new JFrame("TerraBiome Viewer"); JFrame frame = new JFrame("TerraBiome Viewer");