remove ChunkLocalCachingBiomeProvider

This commit is contained in:
dfsek
2022-06-18 02:01:06 -07:00
parent 642372eaa1
commit 7de66fecf8
10 changed files with 71 additions and 200 deletions

View File

@@ -92,17 +92,13 @@ public interface BiomeProvider {
return StreamSupport.stream(getBiomes().spliterator(), false);
}
default CachingBiomeProvider caching(int minY, int maxY) {
return new CachingBiomeProvider(this, minY, maxY);
}
default CachingBiomeProvider caching(WorldProperties worldProperties) {
return caching(worldProperties.getMinHeight(), worldProperties.getMaxHeight());
}
default ChunkLocalCachingBiomeProvider caching(WorldProperties worldProperties, int chunkX, int chunkZ) {
return new ChunkLocalCachingBiomeProvider(this, worldProperties, chunkX, chunkZ);
default CachingBiomeProvider caching() {
if(this instanceof CachingBiomeProvider cachingBiomeProvider) {
return cachingBiomeProvider;
}
return new CachingBiomeProvider(this);
}
default int resolution() {
return 1;

View File

@@ -1,14 +1,11 @@
package com.dfsek.terra.api.world.biome.generation;
import com.dfsek.terra.api.Handle;
import com.dfsek.terra.api.util.Column;
import com.dfsek.terra.api.util.MathUtil;
import com.dfsek.terra.api.util.vector.Vector3;
import com.dfsek.terra.api.world.biome.Biome;
import com.dfsek.terra.api.world.info.WorldProperties;
import java.util.HashMap;
import java.util.Map;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import java.util.Optional;
@@ -19,15 +16,23 @@ import java.util.Optional;
*/
public class CachingBiomeProvider implements BiomeProvider, Handle {
protected final BiomeProvider delegate;
protected final int minY;
protected final int maxY;
private final Map<Long, Biome[]> cache = new HashMap<>();
private final Map<Long, Column<Biome>> columnCache = new HashMap<>();
private final int res;
private final LoadingCache<SeededVector3, Biome> cache;
private final LoadingCache<SeededVector2, Optional<Biome>> baseCache;
protected CachingBiomeProvider(BiomeProvider delegate, int minY, int maxY) {
protected CachingBiomeProvider(BiomeProvider delegate) {
this.delegate = delegate;
this.minY = minY;
this.maxY = maxY;
this.res = delegate.resolution();
this.cache = Caffeine
.newBuilder()
.maximumSize(98304) // 1 full chunk (high res)
.build(vec -> delegate.getBiome(vec.x * res, vec.y * res, vec.z * res, vec.seed));
this.baseCache = Caffeine
.newBuilder()
.maximumSize(256) // 1 full chunk (high res)
.build(vec -> delegate.getBaseBiome(vec.x * res, vec.z * res, vec.seed));
}
@Override
@@ -37,23 +42,12 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
@Override
public Biome getBiome(int x, int y, int z, long seed) {
if(y >= maxY || y < minY) throw new IllegalArgumentException("Y out of range: " + y + " (min: " + minY + ", max: " + maxY + ")");
Biome[] biomes = cache.computeIfAbsent(MathUtil.squash(x, z), key -> new Biome[maxY - minY]);
int yi = y - minY;
if(biomes[yi] == null) {
biomes[yi] = delegate.getBiome(x, y, z, seed);
}
return biomes[yi];
return cache.get(new SeededVector3(x / res, y / res, z / res, seed));
}
@Override
public Optional<Biome> getBaseBiome(int x, int z, long seed) {
return delegate.getBaseBiome(x, z, seed);
}
@Override
public Column<Biome> getColumn(int x, int z, long seed, int min, int max) {
return columnCache.computeIfAbsent(MathUtil.squash(x, z), k -> new BiomeColumn(this, min, max, x, z, seed));
return baseCache.get(new SeededVector2(x / res, z / res, seed));
}
@Override
@@ -65,4 +59,38 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
public int resolution() {
return delegate.resolution();
}
private record SeededVector3(int x, int y, int z, long seed) {
@Override
public boolean equals(Object obj) {
if(obj instanceof SeededVector3 that) {
return this.y == that.y && this.z == that.z && this.x == that.x && this.seed == that.seed;
}
return false;
}
@Override
public int hashCode() {
int code = x;
code = 31 * code + y;
code = 31 * code + z;
return 31 * code + ((int) (seed ^ (seed >>> 32)));
}
}
private record SeededVector2(int x, int z, long seed) {
@Override
public boolean equals(Object obj) {
if(obj instanceof SeededVector2 that) {
return this.z == that.z && this.x == that.x && this.seed == that.seed;
}
return false;
}
@Override
public int hashCode() {
int code = x;
code = 31 * code + z;
return 31 * code + ((int) (seed ^ (seed >>> 32)));
}
}
}

View File

@@ -1,118 +0,0 @@
package com.dfsek.terra.api.world.biome.generation;
import net.jafama.FastMath;
import java.util.Optional;
import com.dfsek.terra.api.util.Column;
import com.dfsek.terra.api.world.biome.Biome;
import com.dfsek.terra.api.world.info.WorldProperties;
/**
* A biome provider implementation that lazily evaluates biomes, and caches them.
* <p>
* This is for use in chunk generators, it makes the assumption that <b>the seed remains the same for the duration of its use!</b>
* <p>
* The cache works the best when all biomes are within one chunk! This is because internally, there are two caches, one constant-size one
* for the chunk, and a slower dynamically-sized cache for out-of-chunk biomes.
*/
public class ChunkLocalCachingBiomeProvider extends CachingBiomeProvider {
private final BiomeChunk[] chunks = new BiomeChunk[9];
private final Column<Biome>[] columnCache; // x + z * 16
private final int chunkX;
private final int chunkZ;
private final int height;
private final int zMul;
private final int yMul;
private final int resolution;
protected ChunkLocalCachingBiomeProvider(BiomeProvider delegate, WorldProperties worldProperties, int chunkX, int chunkZ) {
super(delegate, worldProperties.getMinHeight(), worldProperties.getMaxHeight());
this.height = maxY - minY;
this.chunkX = chunkX;
this.chunkZ = chunkZ;
this.resolution = delegate.resolution();
this.zMul = 16 / resolution;
this.yMul = zMul * zMul;
this.columnCache = new Column[yMul];
}
@Override
public BiomeProvider getHandle() {
return delegate;
}
@Override
public Biome getBiome(int x, int y, int z, long seed) {
int localChunkX = FastMath.floorDiv(x, 16) - this.chunkX + 1;
int localChunkZ = FastMath.floorDiv(z, 16) - this.chunkZ + 1;
if(localChunkX >= 0 && localChunkZ >= 0 && localChunkX <= 2 && localChunkZ <= 2) {
int chunkIndex = localChunkX + localChunkZ * 3;
BiomeChunk chunk = chunks[chunkIndex];
if(chunk == null) {
chunk = new BiomeChunk(height / resolution, yMul);
chunks[chunkIndex] = chunk;
}
int scaledX = FastMath.floorDiv(x & 15, resolution);
int scaledY = FastMath.floorDiv(y - minY, resolution);
int scaledZ = FastMath.floorDiv(z & 15, resolution);
int biomeIndex = scaledX + zMul * scaledZ + yMul * scaledY;
Biome biome = chunk.cache[biomeIndex];
if(biome == null) {
biome = delegate.getBiome(x, y, z, seed);
chunk.cache[biomeIndex] = biome;
}
return biome;
}
return super.getBiome(x, y, z, seed);
}
@Override
public Optional<Biome> getBaseBiome(int x, int z, long seed) {
return delegate.getBaseBiome(x, z, seed);
}
@Override
public Column<Biome> getColumn(int x, int z, long seed, int min, int max) {
int scaledX = (x & 15) / resolution;
int scaledZ = (z & 15) / resolution;
if(FastMath.floorDiv(x, 16) == chunkX && FastMath.floorDiv(z, 16) == chunkZ) {
int index = scaledX + (zMul * scaledZ);
Column<Biome> column = columnCache[index];
if(column == null) {
column = new BiomeColumn(this, min, max, x, z, seed);
columnCache[index] = column;
}
return column;
}
return super.getColumn(x, z, seed, min, max);
}
@Override
public Iterable<Biome> getBiomes() {
return delegate.getBiomes();
}
@Override
public int resolution() {
return resolution;
}
private static class BiomeChunk {
final Biome[] cache; // x + z * 16 + y * 256
private BiomeChunk(int height, int widthSq) {
this.cache = new Biome[widthSq * height];
}
}
}

View File

@@ -17,12 +17,8 @@
package com.dfsek.terra.bukkit.generator;
import com.dfsek.terra.api.world.biome.generation.ChunkLocalCachingBiomeProvider;
import com.dfsek.terra.api.world.info.WorldProperties;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.bukkit.World;
import org.bukkit.generator.BiomeProvider;
import org.bukkit.generator.BlockPopulator;
@@ -51,14 +47,6 @@ public class BukkitChunkGeneratorWrapper extends org.bukkit.generator.ChunkGener
private final BlockState air;
private ChunkGenerator delegate;
private ConfigPack pack;
private final LoadingCache<SeededVector, ChunkLocalCachingBiomeProvider> biomeProviderCache = CacheBuilder.newBuilder()
.maximumSize(128)
.build(new CacheLoader<>() {
@Override
public @NotNull ChunkLocalCachingBiomeProvider load(@NotNull SeededVector key) {
return pack.getBiomeProvider().caching(key.worldProperties, key.x, key.z);
}
});
private record SeededVector(int x, int z, WorldProperties worldProperties) {
@Override
@@ -95,7 +83,7 @@ public class BukkitChunkGeneratorWrapper extends org.bukkit.generator.ChunkGener
@Override
public void generateNoise(@NotNull WorldInfo worldInfo, @NotNull Random random, int x, int z, @NotNull ChunkData chunkData) {
BukkitWorldProperties properties = new BukkitWorldProperties(worldInfo);
delegate.generateChunkData(new BukkitProtoChunk(chunkData), properties, biomeProviderCache.getUnchecked(new SeededVector(x, z, new BukkitWorldProperties(worldInfo))), x, z);
delegate.generateChunkData(new BukkitProtoChunk(chunkData), properties, pack.getBiomeProvider(), x, z);
}
@Override
@@ -106,7 +94,7 @@ public class BukkitChunkGeneratorWrapper extends org.bukkit.generator.ChunkGener
@Override
public void populate(@NotNull WorldInfo worldInfo, @NotNull Random random, int x, int z,
@NotNull LimitedRegion limitedRegion) {
generationStage.populate(new BukkitProtoWorld(limitedRegion, air, biomeProviderCache.getUnchecked(new SeededVector(x, z, new BukkitWorldProperties(worldInfo)))));
generationStage.populate(new BukkitProtoWorld(limitedRegion, air, pack.getBiomeProvider()));
}
})
.collect(Collectors.toList());

View File

@@ -1,7 +1,5 @@
package com.dfsek.terra.bukkit.world;
import com.dfsek.terra.api.world.biome.generation.ChunkLocalCachingBiomeProvider;
import org.bukkit.Location;
import org.bukkit.generator.LimitedRegion;
import org.slf4j.Logger;

View File

@@ -141,7 +141,7 @@ public class NMSChunkGeneratorDelegate extends ChunkGenerator {
public int getBaseHeight(int x, int z, Heightmap.@NotNull Types heightmap, @NotNull LevelHeightAccessor world) {
WorldProperties properties = new NMSWorldProperties(seed, world);
int y = properties.getMaxHeight();
BiomeProvider biomeProvider = pack.getBiomeProvider().caching(properties);
BiomeProvider biomeProvider = pack.getBiomeProvider();
while(y >= getMinY() && !heightmap.isOpaque().test(
((CraftBlockData) delegate.getBlock(properties, x, y - 1, z, biomeProvider).getHandle()).getState())) {
y--;

View File

@@ -121,7 +121,7 @@ public class NMSChunkGeneratorDelegate extends ChunkGenerator {
public int getBaseHeight(int x, int z, @NotNull Types heightmap, @NotNull LevelHeightAccessor world, @NotNull RandomState noiseConfig) {
WorldProperties properties = new NMSWorldProperties(seed, world);
int y = properties.getMaxHeight();
BiomeProvider biomeProvider = pack.getBiomeProvider().caching(properties);
BiomeProvider biomeProvider = pack.getBiomeProvider();
while(y >= getMinY() && !heightmap.isOpaque().test(
((CraftBlockData) delegate.getBlock(properties, x, y - 1, z, biomeProvider).getHandle()).getState())) {
y--;

View File

@@ -1,7 +1,5 @@
package com.dfsek.terra.cli.world;
import com.dfsek.terra.api.world.biome.generation.ChunkLocalCachingBiomeProvider;
import com.google.common.collect.Streams;
import net.jafama.FastMath;
import net.querz.mca.MCAFile;
@@ -87,7 +85,7 @@ public class CLIWorld implements ServerWorld, NBTSerializable<Stream<Pair<Vector
try {
int num = amount.getAndIncrement();
CLIChunk chunk = getChunkAt(finalX, finalZ);
ChunkLocalCachingBiomeProvider cachingBiomeProvider = pack.getBiomeProvider().caching(this, finalX, finalZ);
BiomeProvider cachingBiomeProvider = pack.getBiomeProvider();
chunkGenerator.generateChunkData(chunk, this, cachingBiomeProvider, finalX, finalZ);
CLIProtoWorld protoWorld = new CLIProtoWorld(this, cachingBiomeProvider, finalX, finalZ);
pack.getStages().forEach(stage -> stage.populate(protoWorld));

View File

@@ -95,9 +95,7 @@ public class FabricChunkGeneratorWrapper extends net.minecraft.world.gen.chunk.C
if(chunk instanceof net.minecraft.world.chunk.ProtoChunk) {
ChunkPos pos = chunk.getPos();
((BiomeProviderHolder) chunk)
.terra$setHeldBiomeProvider(pack.getBiomeProvider()
.caching((ProtoWorld) ((StructureAccessorAccessor) structureAccessor).getWorld(), pos.x,
pos.z));
.terra$setHeldBiomeProvider(pack.getBiomeProvider());
}
return super.populateBiomes(biomeRegistry, executor, noiseConfig, blender, structureAccessor, chunk);
}
@@ -142,10 +140,10 @@ public class FabricChunkGeneratorWrapper extends net.minecraft.world.gen.chunk.C
if(chunk instanceof BiomeProviderHolder providerHolder) {
biomeProvider = providerHolder.terra$getHeldBiomeProvider();
if(biomeProvider == null) {
biomeProvider = pack.getBiomeProvider().caching(world, chunk.getPos().x, chunk.getPos().z);
biomeProvider = pack.getBiomeProvider();
}
} else {
biomeProvider = pack.getBiomeProvider().caching(world, chunk.getPos().x, chunk.getPos().z);
biomeProvider = pack.getBiomeProvider();
}
delegate.generateChunkData((ProtoChunk) chunk, world, biomeProvider, chunk.getPos().x, chunk.getPos().z);
@@ -208,7 +206,7 @@ public class FabricChunkGeneratorWrapper extends net.minecraft.world.gen.chunk.C
@Override
public int getHeight(int x, int z, Type heightmap, HeightLimitView height, NoiseConfig noiseConfig) {
WorldProperties properties = FabricAdapter.adapt(height, noiseConfig.getLegacyWorldSeed());
BiomeProvider biomeProvider = pack.getBiomeProvider().caching(properties);
BiomeProvider biomeProvider = pack.getBiomeProvider();
int min = height.getBottomY();
for(int y = height.getTopY() - 1; y >= min; y--) {
if(heightmap
@@ -222,7 +220,7 @@ public class FabricChunkGeneratorWrapper extends net.minecraft.world.gen.chunk.C
public VerticalBlockSample getColumnSample(int x, int z, HeightLimitView height, NoiseConfig noiseConfig) {
BlockState[] array = new BlockState[height.getHeight()];
WorldProperties properties = FabricAdapter.adapt(height, noiseConfig.getLegacyWorldSeed());
BiomeProvider biomeProvider = pack.getBiomeProvider().caching(properties);
BiomeProvider biomeProvider = pack.getBiomeProvider();
for(int y = height.getTopY() - 1; y >= height.getBottomY(); y--) {
array[y - height.getBottomY()] = (BlockState) delegate.getBlock(properties, x, y, z, biomeProvider);
}

View File

@@ -75,26 +75,13 @@ public abstract class ChunkRegionMixin {
@Final
private MultiTickScheduler<Fluid> fluidTickScheduler;
@Shadow
public abstract net.minecraft.server.world.ServerWorld toServerWorld();
@Shadow
public abstract Chunk getChunk(int chunkX, int chunkZ);
private Lazy<BiomeProvider> caching;
@SuppressWarnings("deprecation")
@Inject(at = @At("RETURN"),
method = "<init>(Lnet/minecraft/server/world/ServerWorld;Ljava/util/List;Lnet/minecraft/world/chunk/ChunkStatus;I)V")
public void injectConstructor(net.minecraft.server.world.ServerWorld world, List<net.minecraft.world.chunk.Chunk> list,
ChunkStatus chunkStatus, int i,
CallbackInfo ci) {
this.terra$config = ((ServerWorld) world).getPack();
this.caching = Lazy.lazy(() -> ((TerraBiomeSource) ((ChunkRegion) (Object) this)
.toServerWorld()
.getChunkManager()
.getChunkGenerator()
.getBiomeSource()).getProvider().caching((ProtoWorld) this));
}
@@ -137,11 +124,7 @@ public abstract class ChunkRegionMixin {
}
public BiomeProvider terraWorld$getBiomeProvider() {
BiomeProvider provider = ((BiomeProviderHolder) this).terra$getHeldBiomeProvider();
if(provider != null) {
return provider;
}
return caching.value();
return terra$config.getBiomeProvider();
}
public Entity terraWorld$spawnEntity(double x, double y, double z, EntityType entityType) {