mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-03 19:51:49 +00:00
Size palette layer caches
- Hold every depth seed of a layer's thickness - Widen lock-free recent slots to cover consecutive depth seeds - Share one generator for seeded palette layer styles - Test cached depth generators against fresh builds
This commit is contained in:
2 files changed
+158
-7
No files matched your search
@@ -18,30 +18,37 @@
|
||||
|
||||
package art.arcane.iris.generation.biome;
|
||||
|
||||
import art.arcane.iris.generation.noise.IrisGeneratorStyle;
|
||||
import art.arcane.iris.generation.runtime.PackRuntime;
|
||||
import art.arcane.iris.generation.runtime.SeedTable;
|
||||
import art.arcane.volmlib.util.noise.CNG;
|
||||
|
||||
/**
|
||||
* What one engine derives from an {@link IrisBiomePaletteLayer}: the block selection and depth generators of the
|
||||
* seeds that sample it.
|
||||
* seeds that sample it. A column walk asks for one layer seed per depth, so the layer table holds the layer's whole
|
||||
* thickness for a few parent seeds instead of rebuilding a generator on every depth step.
|
||||
*/
|
||||
public final class PaletteLayerRuntime {
|
||||
private static final int LAYER_SEED_CAPACITY = 32;
|
||||
private static final int LAYER_RECENT_SLOTS = 32;
|
||||
private static final int MAX_LAYER_RECENT_SLOTS = 4096;
|
||||
private static final int PARENT_SEEDS_PER_LAYER = 4;
|
||||
private static final int HEIGHT_SEED_CAPACITY = 32;
|
||||
|
||||
private final PackRuntime runtime;
|
||||
private final SeedTable<CNG> layerGenerators;
|
||||
private final SeedTable<CNG> heightGenerators;
|
||||
private final boolean shareExpressionGenerator;
|
||||
private final boolean shareGenerator;
|
||||
|
||||
public PaletteLayerRuntime(IrisBiomePaletteLayer layer, PackRuntime runtime) {
|
||||
this.runtime = runtime;
|
||||
this.shareExpressionGenerator = layer.getStyle() != null
|
||||
&& layer.getStyle().canShareExpressionGenerator(runtime.data());
|
||||
this.layerGenerators = new SeedTable<>(LAYER_SEED_CAPACITY, LAYER_RECENT_SLOTS,
|
||||
seed -> layer.createLayerGenerator(seed, runtime));
|
||||
IrisGeneratorStyle style = layer.getStyle();
|
||||
// A seeded style rolls from its own seed and the world seed, never the caller's, so every depth builds the same noise.
|
||||
this.shareGenerator = style != null
|
||||
&& (style.isSeeded() || style.canShareExpressionGenerator(runtime.data()));
|
||||
int depthSeeds = Math.max(1, Math.max(layer.getMinHeight(), layer.getMaxHeight()));
|
||||
this.layerGenerators = new SeedTable<>(Math.max(LAYER_SEED_CAPACITY, depthSeeds * PARENT_SEEDS_PER_LAYER),
|
||||
recentSlots(depthSeeds), seed -> layer.createLayerGenerator(seed, runtime));
|
||||
this.heightGenerators = new SeedTable<>(HEIGHT_SEED_CAPACITY, IrisBiomePaletteLayer::createHeightGenerator);
|
||||
}
|
||||
|
||||
@@ -51,10 +58,16 @@ public final class PaletteLayerRuntime {
|
||||
}
|
||||
|
||||
public CNG layerGenerator(long seed) {
|
||||
return layerGenerators.get(shareExpressionGenerator ? 0L : seed);
|
||||
return layerGenerators.get(shareGenerator ? 0L : seed);
|
||||
}
|
||||
|
||||
public CNG heightGenerator(long seed) {
|
||||
return heightGenerators.get(seed);
|
||||
}
|
||||
|
||||
/** Consecutive depth seeds land in distinct slots when the slot count covers the thickness. */
|
||||
private static int recentSlots(int depthSeeds) {
|
||||
int covering = Integer.highestOneBit(Math.min(depthSeeds, MAX_LAYER_RECENT_SLOTS) - 1) << 1;
|
||||
return Math.clamp(covering, LAYER_RECENT_SLOTS, MAX_LAYER_RECENT_SLOTS);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package art.arcane.iris.generation.biome;
|
||||
|
||||
import art.arcane.iris.generation.noise.IrisGeneratorStyle;
|
||||
import art.arcane.iris.generation.noise.NoiseStyle;
|
||||
import art.arcane.iris.generation.runtime.PackRuntime;
|
||||
import art.arcane.iris.testsupport.MockPackData;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import art.arcane.volmlib.util.noise.CNG;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.ThreadMXBean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public final class PaletteLayerDepthCacheTest {
|
||||
private static final long[] PARENT_SEEDS = {0L, 17L, -9_223_372_036_854_775_807L, 8_675_309L, 1_234_567_890_123L};
|
||||
private static final int[] THICKNESSES = {1, 8, 33, 96, 300};
|
||||
private static final int LAYER_INDEX = 3;
|
||||
private static final int COLUMNS = 256;
|
||||
private static final int BENCH_THICKNESS = 96;
|
||||
private static final int BENCH_CHUNKS = 8;
|
||||
|
||||
@Test
|
||||
public void deepLayerBuildsEachDepthGeneratorOnce() {
|
||||
CountingStyle style = new CountingStyle(NoiseStyle.SIMPLEX);
|
||||
IrisBiomePaletteLayer layer = layer(style, BENCH_THICKNESS);
|
||||
PackRuntime runtime = MockPackData.create().standalonePackRuntime();
|
||||
|
||||
walkChunks(layer, runtime, new RNG(17L), BENCH_THICKNESS, 2);
|
||||
|
||||
assertEquals(BENCH_THICKNESS, style.builds.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void seededLayerBuildsOneGeneratorForEveryDepthAndParent() {
|
||||
CountingStyle style = new CountingStyle(NoiseStyle.SIMPLEX);
|
||||
style.setSeed(4_242L);
|
||||
IrisBiomePaletteLayer layer = layer(style, BENCH_THICKNESS);
|
||||
PackRuntime runtime = MockPackData.create().standalonePackRuntime();
|
||||
|
||||
for (long parentSeed : PARENT_SEEDS) {
|
||||
walkChunks(layer, runtime, new RNG(parentSeed), BENCH_THICKNESS, 1);
|
||||
}
|
||||
|
||||
assertEquals(1, style.builds.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cachedDepthGeneratorsMatchFreshBuilds() {
|
||||
assertMatchesFreshBuilds(new IrisGeneratorStyle(NoiseStyle.SIMPLEX));
|
||||
assertMatchesFreshBuilds(new IrisGeneratorStyle(NoiseStyle.CELLULAR).setFracture(
|
||||
new IrisGeneratorStyle(NoiseStyle.PERLIN).zoomed(3D)).zoomed(0.5D));
|
||||
assertMatchesFreshBuilds(new IrisGeneratorStyle(NoiseStyle.SIMPLEX).setSeed(77L));
|
||||
assertMatchesFreshBuilds(new IrisGeneratorStyle(NoiseStyle.PERLIN).setSeed(-3L)
|
||||
.setFracture(new IrisGeneratorStyle(NoiseStyle.SIMPLEX)));
|
||||
}
|
||||
|
||||
/** Prints builds and thread CPU time for a deep-layer chunk walk; compare runs on the same machine. */
|
||||
@Test
|
||||
public void reportDeepLayerWalkCost() {
|
||||
ThreadMXBean threads = ManagementFactory.getThreadMXBean();
|
||||
for (int round = 0; round < 3; round++) {
|
||||
for (boolean fractured : new boolean[]{false, true}) {
|
||||
CountingStyle style = new CountingStyle(fractured ? NoiseStyle.CELLULAR : NoiseStyle.SIMPLEX);
|
||||
if (fractured) {
|
||||
style.setFracture(new IrisGeneratorStyle(NoiseStyle.PERLIN).zoomed(3D));
|
||||
}
|
||||
IrisBiomePaletteLayer layer = layer(style, BENCH_THICKNESS);
|
||||
PackRuntime runtime = MockPackData.create().standalonePackRuntime();
|
||||
long start = threads.getCurrentThreadCpuTime();
|
||||
double sink = walkChunks(layer, runtime, new RNG(17L), BENCH_THICKNESS, BENCH_CHUNKS);
|
||||
long cpu = threads.getCurrentThreadCpuTime() - start;
|
||||
System.out.printf("palette depth walk %s: thickness=%d chunks=%d builds=%d cpu=%.1fms (%.2fms/chunk) sink=%.3f%n",
|
||||
fractured ? "cellular+fracture" : "simplex", BENCH_THICKNESS, BENCH_CHUNKS, style.builds.get(),
|
||||
cpu / 1e6D, cpu / 1e6D / BENCH_CHUNKS, sink);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertMatchesFreshBuilds(IrisGeneratorStyle style) {
|
||||
for (int thickness : THICKNESSES) {
|
||||
IrisBiomePaletteLayer layer = layer(style, thickness);
|
||||
PackRuntime runtime = MockPackData.create().standalonePackRuntime();
|
||||
int blockCount = layer.getBlockData(runtime.data()).size();
|
||||
for (int pass = 0; pass < 2; pass++) {
|
||||
for (long parentSeed : PARENT_SEEDS) {
|
||||
RNG parent = new RNG(parentSeed);
|
||||
for (int depth = 0; depth < thickness; depth++) {
|
||||
int signature = LAYER_INDEX + depth;
|
||||
long seed = parentSeed + signature + thickness + thickness + blockCount;
|
||||
CNG fresh = style.createNoCache(new RNG(seed), runtime);
|
||||
CNG cached = layer.getLayerGenerator(parent, signature, runtime);
|
||||
for (int sample = 0; sample < 6; sample++) {
|
||||
double x = sample * 37.25D - 91D + depth;
|
||||
double z = 13.5D - sample * 19D - depth;
|
||||
assertEquals(fresh.noise(x, depth, z), cached.noise(x, depth, z), 0D);
|
||||
assertEquals(fresh.noise(x, z), cached.noise(x, z), 0D);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static double walkChunks(IrisBiomePaletteLayer layer, PackRuntime runtime, RNG parent, int thickness, int chunks) {
|
||||
double sink = 0D;
|
||||
for (int chunk = 0; chunk < chunks; chunk++) {
|
||||
for (int column = 0; column < COLUMNS; column++) {
|
||||
double x = chunk * 16 + (column & 15);
|
||||
double z = column >> 4;
|
||||
for (int depth = 0; depth < thickness; depth++) {
|
||||
sink += layer.getLayerGenerator(parent, LAYER_INDEX + depth, runtime).noise(x + depth, depth, z - depth);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sink;
|
||||
}
|
||||
|
||||
private static IrisBiomePaletteLayer layer(IrisGeneratorStyle style, int thickness) {
|
||||
return new IrisBiomePaletteLayer().zero().setStyle(style).setMinHeight(thickness).setMaxHeight(thickness);
|
||||
}
|
||||
|
||||
private static final class CountingStyle extends IrisGeneratorStyle {
|
||||
private final AtomicInteger builds = new AtomicInteger();
|
||||
|
||||
private CountingStyle(NoiseStyle style) {
|
||||
super(style);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CNG createNoCache(RNG rng, PackRuntime runtime) {
|
||||
builds.incrementAndGet();
|
||||
return super.createNoCache(rng, runtime);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user