mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 02:20:57 +00:00
Some refactoring
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
package com.dfsek.terra.addons.noise.samplers;
|
||||
|
||||
import com.dfsek.terra.api.noise.DerivativeNoiseSampler;
|
||||
import com.dfsek.terra.api.noise.NoiseSampler;
|
||||
|
||||
import com.dfsek.terra.api.util.cache.DoubleSeededVector2Key;
|
||||
import com.dfsek.terra.api.util.cache.DoubleSeededVector3Key;
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair.Mutable;
|
||||
@@ -11,135 +12,63 @@ import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.github.benmanes.caffeine.cache.LoadingCache;
|
||||
import com.github.benmanes.caffeine.cache.Scheduler;
|
||||
|
||||
import static com.dfsek.terra.api.util.CacheUtils.CACHE_EXECUTOR;
|
||||
import static com.dfsek.terra.api.util.cache.CacheUtils.CACHE_EXECUTOR;
|
||||
|
||||
|
||||
public class CacheSampler implements NoiseSampler {
|
||||
|
||||
private final NoiseSampler sampler;
|
||||
private final ThreadLocal<Mutable<DoubleSeededVector2, LoadingCache<DoubleSeededVector2, Double>>> cache2D;
|
||||
private final ThreadLocal<Mutable<DoubleSeededVector3, LoadingCache<DoubleSeededVector3, Double>>> cache3D;
|
||||
private final ThreadLocal<Mutable<DoubleSeededVector2Key, LoadingCache<DoubleSeededVector2Key, Double>>> cache2D;
|
||||
private final ThreadLocal<Mutable<DoubleSeededVector3Key, LoadingCache<DoubleSeededVector3Key, Double>>> cache3D;
|
||||
|
||||
public CacheSampler(NoiseSampler sampler, int dimensions) {
|
||||
this.sampler = sampler;
|
||||
if (dimensions == 2) {
|
||||
LoadingCache<DoubleSeededVector2, Double> cache = Caffeine
|
||||
LoadingCache<DoubleSeededVector2Key, Double> cache = Caffeine
|
||||
.newBuilder()
|
||||
.executor(CACHE_EXECUTOR)
|
||||
.scheduler(Scheduler.systemScheduler())
|
||||
.initialCapacity(256)
|
||||
.maximumSize(256)
|
||||
.build(this::sampleNoise);
|
||||
this.cache2D = ThreadLocal.withInitial(() -> Pair.of(new DoubleSeededVector2(0, 0, 0), cache).mutable());
|
||||
this.cache2D = ThreadLocal.withInitial(() -> Pair.of(new DoubleSeededVector2Key(0, 0, 0), cache).mutable());
|
||||
this.cache3D = null;
|
||||
} else {
|
||||
LoadingCache<DoubleSeededVector3, Double> cache = Caffeine
|
||||
LoadingCache<DoubleSeededVector3Key, Double> cache = Caffeine
|
||||
.newBuilder()
|
||||
.executor(CACHE_EXECUTOR)
|
||||
.scheduler(Scheduler.systemScheduler())
|
||||
.initialCapacity(981504)
|
||||
.maximumSize(981504)
|
||||
.build(this::sampleNoise);
|
||||
this.cache3D = ThreadLocal.withInitial(() -> Pair.of(new DoubleSeededVector3(0, 0, 0, 0), cache).mutable());
|
||||
this.cache3D = ThreadLocal.withInitial(() -> Pair.of(new DoubleSeededVector3Key(0, 0, 0, 0), cache).mutable());
|
||||
this.cache2D = null;
|
||||
}
|
||||
}
|
||||
|
||||
private Double sampleNoise(DoubleSeededVector2 vec) {
|
||||
this.cache2D.get().setLeft(new DoubleSeededVector2(0, 0, 0));
|
||||
private Double sampleNoise(DoubleSeededVector2Key vec) {
|
||||
this.cache2D.get().setLeft(new DoubleSeededVector2Key(0, 0, 0));
|
||||
return this.sampler.noise(vec.seed, vec.x, vec.z);
|
||||
}
|
||||
|
||||
private Double sampleNoise(DoubleSeededVector3 vec) {
|
||||
this.cache3D.get().setLeft(new DoubleSeededVector3(0, 0, 0, 0));
|
||||
return this.sampler.noise(vec.seed, vec.x, vec.z);
|
||||
private Double sampleNoise(DoubleSeededVector3Key vec) {
|
||||
this.cache3D.get().setLeft(new DoubleSeededVector3Key(0, 0, 0, 0));
|
||||
return this.sampler.noise(vec.seed, vec.x, vec.y, vec.z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double noise(long seed, double x, double y) {
|
||||
Mutable<DoubleSeededVector2, LoadingCache<DoubleSeededVector2, Double>> cachePair = cache2D.get();
|
||||
DoubleSeededVector2 mutableKey = cachePair.getLeft();
|
||||
Mutable<DoubleSeededVector2Key, LoadingCache<DoubleSeededVector2Key, Double>> cachePair = cache2D.get();
|
||||
DoubleSeededVector2Key mutableKey = cachePair.getLeft();
|
||||
mutableKey.set(x, y, seed);
|
||||
return cachePair.getRight().get(mutableKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double noise(long seed, double x, double y, double z) {
|
||||
Mutable<DoubleSeededVector3, LoadingCache<DoubleSeededVector3, Double>> cachePair = cache3D.get();
|
||||
DoubleSeededVector3 mutableKey = cachePair.getLeft();
|
||||
Mutable<DoubleSeededVector3Key, LoadingCache<DoubleSeededVector3Key, Double>> cachePair = cache3D.get();
|
||||
DoubleSeededVector3Key mutableKey = cachePair.getLeft();
|
||||
mutableKey.set(x, y, z, seed);
|
||||
return cachePair.getRight().get(mutableKey);
|
||||
}
|
||||
|
||||
private static class DoubleSeededVector3 {
|
||||
double x;
|
||||
double y;
|
||||
double z;
|
||||
long seed;
|
||||
|
||||
public DoubleSeededVector3(double x, double y, double z, long seed) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.seed = seed;
|
||||
}
|
||||
|
||||
public void set(double x, double y, double z, long seed) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.seed = seed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof DoubleSeededVector3 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 = (int) Double.doubleToLongBits(x);
|
||||
code = 31 * code + (int) Double.doubleToLongBits(y);
|
||||
code = 31 * code + (int) Double.doubleToLongBits(z);
|
||||
return 31 * code + (Long.hashCode(seed));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class DoubleSeededVector2 {
|
||||
|
||||
double x;
|
||||
double z;
|
||||
long seed;
|
||||
|
||||
public DoubleSeededVector2(double x, double z, long seed) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
this.seed = seed;
|
||||
}
|
||||
|
||||
public void set(double x, double z, long seed) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
this.seed = seed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof DoubleSeededVector2 that) {
|
||||
return this.z == that.z && this.x == that.x && this.seed == that.seed;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int code = (int) Double.doubleToLongBits(x);
|
||||
code = 31 * code + (int) Double.doubleToLongBits(z);
|
||||
return 31 * code + (Long.hashCode(seed));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,10 @@ import com.dfsek.terra.api.config.Loader;
|
||||
import com.dfsek.terra.api.properties.Properties;
|
||||
import com.dfsek.terra.api.util.generic.Lazy;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Scheduler;
|
||||
|
||||
import static com.dfsek.terra.api.util.cache.CacheUtils.CACHE_EXECUTOR;
|
||||
|
||||
|
||||
/*
|
||||
* Cache prevents configs from loading the same image multiple times into memory
|
||||
@@ -26,8 +30,9 @@ record ImageCache(LoadingCache<String, Image> cache) implements Properties {
|
||||
ImageLibraryPackConfigTemplate config = pack.getContext().get(ImageLibraryPackConfigTemplate.class);
|
||||
ImageCache images;
|
||||
if(!pack.getContext().has(ImageCache.class)) {
|
||||
var cacheBuilder = Caffeine.newBuilder();
|
||||
if(config.unloadOnTimeout()) cacheBuilder.expireAfterAccess(config.getCacheTimeout(), TimeUnit.SECONDS);
|
||||
var cacheBuilder = Caffeine.newBuilder().executor(CACHE_EXECUTOR).scheduler(Scheduler.systemScheduler());
|
||||
if(config.unloadOnTimeout()) cacheBuilder.expireAfterAccess(config.getCacheTimeout(), TimeUnit.SECONDS) .executor(CACHE_EXECUTOR)
|
||||
.scheduler(Scheduler.systemScheduler());
|
||||
images = new ImageCache(cacheBuilder.build(s -> loadImage(s, files)));
|
||||
pack.getContext().put(images);
|
||||
} else images = pack.getContext().get(ImageCache.class);
|
||||
|
||||
Reference in New Issue
Block a user