Sped up hydrology sampling

- Bounds sampling answers a column from its region, land or sea type, or noise-picked biome when every biome it can still become shares the group bounds
- Base terrain heights are kept in a shared per-column memo for hydrology batches, terrain3D anchors and scalar sampling
- Hydrology height batches skip columns whose base height is already known
- The per-batch base height map and its thread local are removed
- The four inferred biome streams share one context region stream
- BakedStream exposes a baked-plane lookup without falling back to the source
- Tests cover region constants, the base height memo and bit-exact heights with and without region constants
This commit is contained in:
Brian Neumann-Fopiano committed 2026-10-30 20:54:55 -04:00
1 parent c700138b66
commit b2990104d2
10 files changed
+635 -78

No files matched your search

@@ -22,11 +22,13 @@ import org.junit.rules.TemporaryFolder;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.SplittableRandom;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Answers.CALLS_REAL_METHODS;
import static org.mockito.ArgumentMatchers.any;
@@ -81,6 +83,24 @@ public class IrisComplexBakeParityTest {
}
}
@Test
public void regionConstantBoundsEqualFullBiomeBoundsBitForBit() throws Exception {
try (Statics ignored = new Statics(); Fixture shortcut = new Fixture(pack()); Fixture full = new Fixture(pack())) {
Field regionBounds = IrisComplex.class.getDeclaredField("regionBounds");
regionBounds.setAccessible(true);
assertNotNull(regionBounds.get(shortcut.complex));
regionBounds.set(full.complex, null);
SplittableRandom random = new SplittableRandom(0x5EEDL);
for (int column = 0; column < 2048; column++) {
int x = random.nextInt(-6400, 6400);
int z = random.nextInt(-6400, 6400);
assertEquals("column " + x + "," + z,
Double.doubleToRawLongBits(full.complex.getBaseTerrainHeightStream().getDouble(x, z)),
Double.doubleToRawLongBits(shortcut.complex.getBaseTerrainHeightStream().getDouble(x, z)));
}
}
}
@Test
public void batchedChunksEvaluateExactlyTheScalarGeneratorSet() throws Exception {
try (Statics ignored = new Statics(); Fixture batched = new Fixture(pack()); Fixture scalar = new Fixture(pack())) {
@@ -243,7 +243,7 @@ public class IrisComplexBoundsBiomeCacheTest {
constructor.setAccessible(true);
Object cache = constructor.newInstance(4096);
Method get = type.getDeclaredMethod("get", double.class, double.class);
Method put = type.getDeclaredMethod("put", double.class, double.class, IrisBiome.class);
Method put = type.getDeclaredMethod("put", double.class, double.class, Object.class);
get.setAccessible(true);
put.setAccessible(true);
IrisBiome first = new IrisBiome();
@@ -266,7 +266,7 @@ public class IrisComplexBoundsBiomeCacheTest {
}
Field values = type.getDeclaredField("values");
values.setAccessible(true);
assertEquals(4096, ((IrisBiome[]) values.get(cache)).length);
assertEquals(4096, ((Object[]) values.get(cache)).length);
}
private static ProceduralStream<IrisBiome> constant(IrisBiome biome) {
@@ -295,7 +295,7 @@ public class IrisComplexBoundsBiomeCacheTest {
constructor.setAccessible(true);
cache = constructor.newInstance();
sample = IrisComplex.class.getDeclaredMethod("sampleBoundsRaw", cacheClass, Engine.class,
IrisInterpolator.class, IrisGenerator[].class, double.class, double.class);
IrisInterpolator.class, int.class, IrisGenerator[].class, double.class, double.class);
sample.setAccessible(true);
boundsConstructor = Class.forName("art.arcane.iris.generation.runtime.IrisComplex$GeneratorBounds")
.getDeclaredConstructor(double.class, double.class);
@@ -313,7 +313,7 @@ public class IrisComplexBoundsBiomeCacheTest {
private NoiseBounds sample(Object gridCache, IrisInterpolator interpolator, double x, double z)
throws ReflectiveOperationException {
return (NoiseBounds) sample.invoke(complex, gridCache, null, interpolator, new IrisGenerator[0], x, z);
return (NoiseBounds) sample.invoke(complex, gridCache, null, interpolator, 0, new IrisGenerator[0], x, z);
}
private Object newGridCache() throws ReflectiveOperationException {
@@ -0,0 +1,136 @@
package art.arcane.iris.generation.runtime;
import art.arcane.iris.generation.biome.IrisBiome;
import art.arcane.iris.generation.terrain.InferredType;
import art.arcane.iris.generation.terrain.IrisRegion;
import art.arcane.volmlib.util.interpolation.NoiseBounds;
import org.junit.Test;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
public class RegionBoundsTest {
private static final NoiseBounds FLAT = new NoiseBounds(0D, 0D);
private static final NoiseBounds HILLS = new NoiseBounds(4D, 30D);
@Test
public void groupsEveryReachableBiomeAgreesOnAreConstantForTheRegion() {
IrisBiome plains = new IrisBiome();
IrisBiome forest = new IrisBiome();
IrisBiome ocean = new IrisBiome();
IrisRegion region = new IrisRegion();
Map<IrisBiome, NoiseBounds[]> bounds = new IdentityHashMap<>();
bounds.put(plains, new NoiseBounds[]{FLAT, HILLS});
bounds.put(forest, new NoiseBounds[]{new NoiseBounds(0D, 0D), new NoiseBounds(2D, 30D)});
bounds.put(ocean, new NoiseBounds[]{FLAT, HILLS});
RegionBounds compiled = RegionBounds.compile(List.of(region), 2,
new Fixed(Map.of(InferredType.LAND, List.of(plains, forest), InferredType.SEA, List.of(ocean)), Map.of()),
(group, biome) -> bounds.get(biome)[group]);
RegionBounds.Constants constants = compiled.of(region);
assertBits(FLAT, constants.any(0));
assertBits(FLAT, constants.typed(InferredType.LAND, 0));
assertBits(FLAT, constants.typed(InferredType.SEA, 0));
assertNull(constants.any(1));
assertNull(constants.typed(InferredType.LAND, 1));
assertSame(HILLS, constants.typed(InferredType.SEA, 1));
assertSame(HILLS, compiled.pick(plains, 1));
assertBits(new NoiseBounds(2D, 30D), compiled.pick(forest, 1));
}
@Test
public void picksAreConstantOnlyWhenEveryDescendantAgrees() {
IrisBiome steppe = new IrisBiome();
IrisBiome meadow = new IrisBiome();
IrisBiome mesa = new IrisBiome();
IrisBiome canyon = new IrisBiome();
IrisRegion region = new IrisRegion();
Map<IrisBiome, NoiseBounds> bounds = new IdentityHashMap<>();
bounds.put(steppe, FLAT);
bounds.put(meadow, new NoiseBounds(0D, 0D));
bounds.put(mesa, FLAT);
bounds.put(canyon, HILLS);
RegionBounds compiled = RegionBounds.compile(List.of(region), 1,
new Fixed(Map.of(InferredType.LAND, List.of(steppe, mesa), InferredType.SEA, List.of()),
descendants(steppe, List.of(steppe, meadow), mesa, List.of(mesa, canyon))),
(group, biome) -> bounds.get(biome));
assertNull(compiled.of(region).typed(InferredType.LAND, 0));
assertSame(FLAT, compiled.pick(steppe, 0));
assertNull(compiled.pick(mesa, 0));
assertNull(compiled.pick(canyon, 0));
}
@Test
public void landAndSeaConstantsOnlyMergeWhenTheirBitsMatch() {
IrisBiome beach = new IrisBiome();
IrisBiome reef = new IrisBiome();
IrisRegion region = new IrisRegion();
Map<IrisBiome, NoiseBounds> bounds = new IdentityHashMap<>();
bounds.put(beach, new NoiseBounds(0D, 0D));
bounds.put(reef, new NoiseBounds(-0D, 0D));
RegionBounds.Constants constants = RegionBounds.compile(List.of(region), 1,
new Fixed(Map.of(InferredType.LAND, List.of(beach), InferredType.SEA, List.of(reef)), Map.of()),
(group, biome) -> bounds.get(biome)).of(region);
assertNull(constants.any(0));
assertSame(bounds.get(beach), constants.typed(InferredType.LAND, 0));
assertSame(bounds.get(reef), constants.typed(InferredType.SEA, 0));
}
@Test
public void emptyBiomeListsAndUnknownRegionsHaveNoConstant() {
IrisRegion region = new IrisRegion();
IrisBiome only = new IrisBiome();
RegionBounds compiled = RegionBounds.compile(List.of(region), 1,
new Fixed(Map.of(InferredType.LAND, List.of(only), InferredType.SEA, List.of()), Map.of()),
(group, biome) -> FLAT);
RegionBounds.Constants constants = compiled.of(region);
assertSame(FLAT, constants.typed(InferredType.LAND, 0));
assertNull(constants.typed(InferredType.SEA, 0));
assertNull(constants.any(0));
assertNull(constants.typed(InferredType.SHORE, 0));
assertNull(constants.typed(InferredType.CAVE, 0));
assertNull(compiled.of(new IrisRegion()));
assertNull(compiled.of(null));
assertNull(compiled.pick(new IrisBiome(), 0));
assertNull(compiled.pick(null, 0));
}
private static Map<IrisBiome, List<IrisBiome>> descendants(IrisBiome first, List<IrisBiome> firstDescendants,
IrisBiome second, List<IrisBiome> secondDescendants) {
Map<IrisBiome, List<IrisBiome>> descendants = new IdentityHashMap<>();
descendants.put(first, firstDescendants);
descendants.put(second, secondDescendants);
return descendants;
}
private static void assertBits(NoiseBounds expected, NoiseBounds actual) {
assertEquals(Double.doubleToRawLongBits(expected.min()), Double.doubleToRawLongBits(actual.min()));
assertEquals(Double.doubleToRawLongBits(expected.max()), Double.doubleToRawLongBits(actual.max()));
}
private record Fixed(Map<InferredType, List<IrisBiome>> picks, Map<IrisBiome, List<IrisBiome>> descendants)
implements RegionBounds.Reach {
@Override
public List<IrisBiome> picks(IrisRegion region, InferredType type) {
return picks.get(type);
}
@Override
public Collection<IrisBiome> descendants(IrisBiome biome) {
List<IrisBiome> known = descendants.get(biome);
return known == null ? List.of(biome) : known;
}
}
}
@@ -0,0 +1,87 @@
package art.arcane.iris.generation.runtime;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class SharedBaseHeightsTest {
@Test
public void storedColumnsComeBackWithTheirExactBits() {
SharedBaseHeights cache = new SharedBaseHeights(1 << 10);
double height = Math.nextUp(71.25D);
assertTrue(Double.isNaN(cache.get(12, -40)));
cache.put(12, -40, height);
assertEquals(Double.doubleToRawLongBits(height), Double.doubleToRawLongBits(cache.get(12, -40)));
assertTrue(Double.isNaN(cache.get(-40, 12)));
}
@Test
public void originMissesUntilItIsStored() {
SharedBaseHeights cache = new SharedBaseHeights(2);
assertTrue(Double.isNaN(cache.get(0, 0)));
cache.put(0, 0, -0D);
assertEquals(Double.doubleToRawLongBits(-0D), Double.doubleToRawLongBits(cache.get(0, 0)));
}
@Test
public void nonFiniteHeightsAreNeverStored() {
SharedBaseHeights cache = new SharedBaseHeights(2);
cache.put(3, 4, 64D);
cache.put(3, 4, Double.NaN);
cache.put(3, 4, Double.POSITIVE_INFINITY);
assertEquals(64D, cache.get(3, 4), 0D);
}
@Test
public void fullCoordinateDomainKeepsDistinctColumns() {
int[] coordinates = {0, 1, -1, 1 << 21, -(1 << 21), 30_000_000, -30_000_000,
Integer.MIN_VALUE, Integer.MAX_VALUE};
for (int x : coordinates) {
for (int z : coordinates) {
SharedBaseHeights cache = new SharedBaseHeights(2);
cache.put(x, z, 12D);
for (int otherX : coordinates) {
for (int otherZ : coordinates) {
if (x == otherX && z == otherZ) {
assertEquals(12D, cache.get(otherX, otherZ), 0D);
} else {
assertTrue(Double.isNaN(cache.get(otherX, otherZ)));
}
}
}
}
}
}
@Test
public void aCollidingColumnReplacesTheOldOne() {
SharedBaseHeights cache = new SharedBaseHeights(2);
cache.put(5, 9, 40D);
cache.put(6, 9, 41D);
cache.put(7, 9, 42D);
int stored = 0;
for (int x = 5; x <= 7; x++) {
double value = cache.get(x, 9);
if (!Double.isNaN(value)) {
assertEquals(35D + x, value, 0D);
stored++;
}
}
assertTrue(stored >= 1 && stored <= 2);
}
@Test
public void capacityFollowsTheHeapWithinBounds() {
assertEquals(SharedBaseHeights.MINIMUM_CAPACITY, SharedBaseHeights.capacityFor(0L));
assertEquals(SharedBaseHeights.MAXIMUM_CAPACITY, SharedBaseHeights.capacityFor(Long.MAX_VALUE));
int capacity = SharedBaseHeights.capacityFor(6L << 30);
assertEquals(1, Integer.bitCount(capacity));
assertTrue(capacity >= SharedBaseHeights.MINIMUM_CAPACITY && capacity <= SharedBaseHeights.MAXIMUM_CAPACITY);
}
}
@@ -146,7 +146,7 @@ public class SharedBoundsBiomesTest {
int xi = (int) ((state & Long.MAX_VALUE) % coordinates.length);
int zi = (int) ((state >>> 10) % coordinates.length);
cache.put(coordinates[xi], coordinates[zi], biomes[xi][zi]);
IrisBiome actual = cache.get(coordinates[xi], coordinates[zi]);
Object actual = cache.get(coordinates[xi], coordinates[zi]);
if (actual != null) {
hits.incrementAndGet();
if (actual != biomes[xi][zi]) {