mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-03 19:51:49 +00:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e3b595a712 | ||
|
|
ecc4413d19 | ||
|
|
c7a2af98a5 |
No files matched your search
@@ -18,27 +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 shareGenerator;
|
||||
|
||||
public PaletteLayerRuntime(IrisBiomePaletteLayer layer, PackRuntime runtime) {
|
||||
this.runtime = runtime;
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -48,10 +58,16 @@ public final class PaletteLayerRuntime {
|
||||
}
|
||||
|
||||
public CNG layerGenerator(long seed) {
|
||||
return layerGenerators.get(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);
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ package art.arcane.iris.generation.noise;
|
||||
import art.arcane.iris.generation.image.IrisImageMap;
|
||||
|
||||
import art.arcane.iris.pack.loading.IrisData;
|
||||
import art.arcane.iris.pack.loading.ResourceLoader;
|
||||
import art.arcane.iris.generation.runtime.Engine;
|
||||
import art.arcane.iris.generation.runtime.PackRuntime;
|
||||
import art.arcane.iris.world.history.GenerationKernelRegistry;
|
||||
@@ -42,7 +43,9 @@ import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Snippet("style")
|
||||
@@ -99,6 +102,46 @@ public class IrisGeneratorStyle {
|
||||
return createNoCache(rng, runtime, 1, 0, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this style can share an expression generator across caller seeds within a pack runtime.
|
||||
*/
|
||||
public boolean canShareExpressionGenerator(IrisData data) {
|
||||
return expression != null && canShareExpressionGenerator(data, new HashSet<>());
|
||||
}
|
||||
|
||||
private boolean canShareExpressionGenerator(IrisData data, Set<String> activeExpressions) {
|
||||
if (getClass() != IrisGeneratorStyle.class || expression == null || data == null
|
||||
|| fracture != null || cellularFrequency != 0D || cacheSize != 0
|
||||
|| !activeExpressions.add(expression)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
ResourceLoader<IrisExpression> loader = data.getExpressionLoader();
|
||||
IrisExpression source = loader == null ? null : loader.load(expression);
|
||||
if (source == null || source.getClass() != IrisExpression.class
|
||||
|| source.getVariables() == null || source.getFunctions() == null) {
|
||||
return false;
|
||||
}
|
||||
for (IrisExpressionLoad variable : source.getVariables()) {
|
||||
if (variable.getEngineValue() != null || variable.getEngineStreamValue() != null
|
||||
|| (variable.getStyleValue() != null
|
||||
&& !variable.getStyleValue().canShareExpressionGenerator(data, activeExpressions))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (IrisExpressionFunction function : source.getFunctions()) {
|
||||
if (function.getEngineStreamValue() != null || function.getStyleValue() == null
|
||||
|| !function.getStyleValue().canShareExpressionGenerator(data, activeExpressions)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} finally {
|
||||
activeExpressions.remove(expression);
|
||||
}
|
||||
}
|
||||
|
||||
public CNG createScaledGenerator(RNG rng, PackRuntime runtime, double scaleFactor) {
|
||||
return createNoCache(rng, runtime).bake().scale(scaleFactor).bake();
|
||||
}
|
||||
|
||||
+74
@@ -2,14 +2,20 @@ package art.arcane.iris.generation.biome;
|
||||
|
||||
import art.arcane.iris.generation.runtime.PackRuntime;
|
||||
import art.arcane.iris.testsupport.MockPackData;
|
||||
import art.arcane.iris.generation.noise.IrisExpression;
|
||||
import art.arcane.iris.generation.noise.IrisExpressionFunction;
|
||||
import art.arcane.iris.generation.noise.IrisGeneratorStyle;
|
||||
import art.arcane.iris.generation.noise.NoiseStyle;
|
||||
|
||||
import art.arcane.iris.pack.loading.IrisData;
|
||||
import art.arcane.iris.pack.loading.ResourceLoader;
|
||||
import art.arcane.iris.generation.runtime.Engine;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import art.arcane.volmlib.util.noise.CNG;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -17,6 +23,9 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -28,6 +37,71 @@ public final class IrisBiomePaletteLayerGeneratorCacheTest {
|
||||
private static final int SIGNATURE = 7;
|
||||
private static final long LAYER_SIGNATURE = 2L;
|
||||
|
||||
@Test
|
||||
public void coordinateOnlyLayerSharesOneGeneratorWithoutChangingItsSamples() {
|
||||
IrisGeneratorStyle color = new IrisGeneratorStyle().setExpression("color");
|
||||
PackRuntime runtime = runtimeWithExpressions(Map.of(
|
||||
"material", new IrisExpression()
|
||||
.setExpression("if(y >= 25 + 19 * cos((x-y)/43), 0.90, color(abs(sin((x-y)/19 + z/33)), 0, 0))")
|
||||
.setFunctions(new KList<>(new IrisExpressionFunction().setName("color").setArgs(3).setStyleValue(color))),
|
||||
"color", new IrisExpression().setExpression("if(x < 0.03, 0.70, if(x < 0.07, 0.40, 0.10))")));
|
||||
IrisGeneratorStyle style = new IrisGeneratorStyle().setExpression("material");
|
||||
IrisBiomePaletteLayer layer = new IrisBiomePaletteLayer().zero().setStyle(style);
|
||||
CNG shared = layer.getLayerGenerator(new RNG(17L), 0, runtime);
|
||||
List<String> blocks = List.of("end_stone", "blackstone", "obsidian", "air");
|
||||
|
||||
for (int depth = 0; depth < 79; depth++) {
|
||||
RNG caller = new RNG(17L + depth * 41L);
|
||||
long originalSeed = caller.getSeed() + depth + LAYER_SIGNATURE;
|
||||
CNG original = style.createNoCache(new RNG(originalSeed), runtime);
|
||||
assertSame(shared, layer.getLayerGenerator(caller, depth, runtime));
|
||||
for (int x = -32; x < 32; x += 3) {
|
||||
double z = 41.5D - x;
|
||||
assertEquals(original.fit(blocks, x, depth, z), shared.fit(blocks, x, depth, z));
|
||||
assertEquals(original.noise(x, depth, z), shared.noise(x, depth, z), 0D);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedSeededNoiseKeepsDistinctLayerGenerators() {
|
||||
IrisGeneratorStyle style = new IrisGeneratorStyle().setExpression("seeded");
|
||||
PackRuntime runtime = runtimeWithExpressions(Map.of(
|
||||
"seeded", new IrisExpression().setExpression("noise(x, z)")
|
||||
.setFunctions(new KList<>(new IrisExpressionFunction()
|
||||
.setName("noise").setStyleValue(NoiseStyle.SIMPLEX.style())))));
|
||||
IrisBiomePaletteLayer layer = new IrisBiomePaletteLayer().zero().setStyle(style);
|
||||
CNG first = layer.getLayerGenerator(new RNG(17L), 0, runtime);
|
||||
CNG second = layer.getLayerGenerator(new RNG(29L), 0, runtime);
|
||||
|
||||
assertNotSame(first, second);
|
||||
assertEquals(style.createNoCache(new RNG(17L + LAYER_SIGNATURE), runtime).noise(-23D, 7D, 11D),
|
||||
first.noise(-23D, 7D, 11D), 0D);
|
||||
assertEquals(style.createNoCache(new RNG(29L + LAYER_SIGNATURE), runtime).noise(-23D, 7D, 11D),
|
||||
second.noise(-23D, 7D, 11D), 0D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void recursiveAndMissingExpressionReferencesDoNotQualifyForSharing() {
|
||||
IrisGeneratorStyle recursive = new IrisGeneratorStyle().setExpression("recursive");
|
||||
IrisGeneratorStyle missing = new IrisGeneratorStyle().setExpression("missing");
|
||||
PackRuntime runtime = runtimeWithExpressions(Map.of(
|
||||
"recursive", new IrisExpression().setExpression("again(x, z)")
|
||||
.setFunctions(new KList<>(new IrisExpressionFunction()
|
||||
.setName("again").setStyleValue(recursive)))));
|
||||
|
||||
assertFalse(recursive.canShareExpressionGenerator(runtime.data()));
|
||||
assertFalse(missing.canShareExpressionGenerator(runtime.data()));
|
||||
}
|
||||
|
||||
private static PackRuntime runtimeWithExpressions(Map<String, IrisExpression> contents) {
|
||||
IrisData data = MockPackData.create();
|
||||
ResourceLoader<IrisExpression> expressions = mock();
|
||||
when(data.getExpressionLoader()).thenReturn(expressions);
|
||||
when(expressions.load(any(String.class))).thenAnswer(call -> contents.get(call.getArgument(0)));
|
||||
return data.standalonePackRuntime();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void callerSeedsDoNotShareAFirstWinnerGenerator() {
|
||||
GeneratorFixture fixture = new GeneratorFixture();
|
||||
|
||||
@@ -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