Shared bounds biomes and paged erosion raster

- Bounds starcasts share whole-column base biome samples across threads through a heap-sized lock-free memo
- Erosion raster geometry tracks station owners in paged local arrays and builds its index map in first-visit order
This commit is contained in:
Brian Neumann-Fopiano committed 2026-10-22 10:02:53 -04:00
1 parent af90765a92
commit 1fe26e6288
6 files changed
+568 -46

No files matched your search

@@ -18,6 +18,7 @@ import it.unimi.dsi.fastutil.longs.LongIterator;
import it.unimi.dsi.fastutil.longs.LongArrayFIFOQueue;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import java.util.Arrays;
import java.util.Objects;
public final class ErosionFieldCompiler {
@@ -159,44 +160,7 @@ public final class ErosionFieldCompiler {
IntArrayList stations = geometry.stations;
DoubleArrayList distance = geometry.distance;
if (!geometry.prepared) {
for (int station = 0; station < count; station++) {
int radius = prepared.radii()[station];
int stationX = centerline.x()[station];
int stationZ = centerline.z()[station];
if (samplingBounds != null && !samplingBounds.intersects(stationX, stationZ, radius)) {
continue;
}
int minimumZ = samplingBounds == null ? -radius : Math.max(-radius, samplingBounds.minimumZ() - stationZ);
int maximumZ = samplingBounds == null ? radius : Math.min(radius, samplingBounds.maximumZ() - stationZ);
int minimumX = samplingBounds == null ? -radius : Math.max(-radius, samplingBounds.minimumX() - stationX);
int maximumX = samplingBounds == null ? radius : Math.min(radius, samplingBounds.maximumX() - stationX);
for (int deltaZ = minimumZ; deltaZ <= maximumZ; deltaZ++) {
for (int deltaX = minimumX; deltaX <= maximumX; deltaX++) {
int cellX = stationX + deltaX;
int cellZ = stationZ + deltaZ;
double cellDistance = centerline.distanceToSegment(station, cellX, cellZ, SurfaceDistanceTable.SHARED);
if (cellDistance > radius) {
continue;
}
long key = RiverFootprint.pack(cellX, cellZ);
int index = indices.putIfAbsent(key, stations.size());
if (index < 0) {
stations.add(station);
distance.add(cellDistance);
continue;
}
int existing = stations.getInt(index);
double existingDistance = distance.getDouble(index);
// A cell on a station's cross-section row ties between the two segments meeting
// there; the later station owns it so the row uses the head its own ring bounded.
if (cellDistance < existingDistance - EPSILON
|| Math.abs(cellDistance - existingDistance) <= EPSILON && station > existing) {
stations.set(index, station);
distance.set(index, cellDistance);
}
}
}
}
rasterize(centerline, prepared.radii(), count, samplingBounds, indices, stations, distance);
geometry.prepared = true;
}
int oceanStart = oceanStart(centerline, valley, terminal);
@@ -912,12 +876,90 @@ public final class ErosionFieldCompiler {
return 1D - t * t * (3D - 2D * t);
}
static void rasterize(SurfaceCenterline centerline, int[] radii, int count, SurfaceBounds samplingBounds,
Long2IntOpenHashMap indices, IntArrayList stations, DoubleArrayList distance) {
CellPages pages = new CellPages();
LongArrayList order = new LongArrayList();
for (int station = 0; station < count; station++) {
int radius = radii[station];
int stationX = centerline.x()[station];
int stationZ = centerline.z()[station];
if (samplingBounds != null && !samplingBounds.intersects(stationX, stationZ, radius)) {
continue;
}
int minimumZ = samplingBounds == null ? -radius : Math.max(-radius, samplingBounds.minimumZ() - stationZ);
int maximumZ = samplingBounds == null ? radius : Math.min(radius, samplingBounds.maximumZ() - stationZ);
int minimumX = samplingBounds == null ? -radius : Math.max(-radius, samplingBounds.minimumX() - stationX);
int maximumX = samplingBounds == null ? radius : Math.min(radius, samplingBounds.maximumX() - stationX);
for (int deltaZ = minimumZ; deltaZ <= maximumZ; deltaZ++) {
for (int deltaX = minimumX; deltaX <= maximumX; deltaX++) {
int cellX = stationX + deltaX;
int cellZ = stationZ + deltaZ;
double cellDistance = centerline.distanceToSegment(station, cellX, cellZ, SurfaceDistanceTable.SHARED);
if (cellDistance > radius) {
continue;
}
int[] page = pages.page(cellX, cellZ);
int offset = CellPages.offset(cellX, cellZ);
int index = page[offset];
if (index < 0) {
page[offset] = stations.size();
stations.add(station);
distance.add(cellDistance);
order.add(RiverFootprint.pack(cellX, cellZ));
continue;
}
int existing = stations.getInt(index);
double existingDistance = distance.getDouble(index);
// A cell on a station's cross-section row ties between the two segments meeting
// there; the later station owns it so the row uses the head its own ring bounded.
if (cellDistance < existingDistance - EPSILON
|| Math.abs(cellDistance - existingDistance) <= EPSILON && station > existing) {
stations.set(index, station);
distance.set(index, cellDistance);
}
}
}
}
for (int index = 0; index < order.size(); index++) {
indices.put(order.getLong(index), index);
}
}
private static double side(SurfaceCenterline centerline, int station, int cellX, int cellZ) {
double deltaX = cellX - centerline.x()[station];
double deltaZ = cellZ - centerline.z()[station];
return deltaX * centerline.normalX(station) + deltaZ * centerline.normalZ(station);
}
private static final class CellPages {
private static final int SHIFT = 6;
private static final int MASK = (1 << SHIFT) - 1;
private final Long2ObjectOpenHashMap<int[]> pages = new Long2ObjectOpenHashMap<>();
private long currentKey;
private int[] current;
private int[] page(int x, int z) {
long key = RiverFootprint.pack(x >> SHIFT, z >> SHIFT);
if (current != null && key == currentKey) {
return current;
}
int[] page = pages.get(key);
if (page == null) {
page = new int[1 << (SHIFT * 2)];
Arrays.fill(page, -1);
pages.put(key, page);
}
current = page;
currentKey = key;
return page;
}
private static int offset(int x, int z) {
return ((z & MASK) << SHIFT) | (x & MASK);
}
}
private static final class RasterGeometry {
private final Long2IntOpenHashMap indices = new Long2IntOpenHashMap();
private final IntArrayList stations = new IntArrayList();
@@ -128,8 +128,8 @@ import java.nio.file.Path;
import java.util.function.BiFunction;
@Data
@EqualsAndHashCode(exclude = {"data", "gridBoundsCache", "sharedCornerBounds", "terrainProgram", "bakeCache", "regionSource", "baseBiomeSource", "caveSource", "rockSource", "fluidSource", "childStyleSeed", "inferredByType", "naturalHeightSource", "naturalTrueBiomeSource", "trueBiomeSource", "frozenInterpolators", "frozenGenerators", "inferredBiomeStreams", "hydrologyRuntime", "imageMapRuntime", "terrainEngine", "terrain3D", "hydrologyBanks3D"})
@ToString(exclude = {"data", "gridBoundsCache", "sharedCornerBounds", "terrainProgram", "bakeCache", "regionSource", "baseBiomeSource", "caveSource", "rockSource", "fluidSource", "childStyleSeed", "inferredByType", "naturalHeightSource", "naturalTrueBiomeSource", "trueBiomeSource", "frozenInterpolators", "frozenGenerators", "inferredBiomeStreams", "hydrologyRuntime", "imageMapRuntime", "terrainEngine", "terrain3D", "hydrologyBanks3D"})
@EqualsAndHashCode(exclude = {"data", "gridBoundsCache", "sharedCornerBounds", "sharedBoundsBiomes", "terrainProgram", "bakeCache", "regionSource", "baseBiomeSource", "caveSource", "rockSource", "fluidSource", "childStyleSeed", "inferredByType", "naturalHeightSource", "naturalTrueBiomeSource", "trueBiomeSource", "frozenInterpolators", "frozenGenerators", "inferredBiomeStreams", "hydrologyRuntime", "imageMapRuntime", "terrainEngine", "terrain3D", "hydrologyBanks3D"})
@ToString(exclude = {"data", "gridBoundsCache", "sharedCornerBounds", "sharedBoundsBiomes", "terrainProgram", "bakeCache", "regionSource", "baseBiomeSource", "caveSource", "rockSource", "fluidSource", "childStyleSeed", "inferredByType", "naturalHeightSource", "naturalTrueBiomeSource", "trueBiomeSource", "frozenInterpolators", "frozenGenerators", "inferredBiomeStreams", "hydrologyRuntime", "imageMapRuntime", "terrainEngine", "terrain3D", "hydrologyBanks3D"})
public class IrisComplex implements DataProvider {
private static final NoiseBounds ZERO_NOISE_BOUNDS = new NoiseBounds(0D, 0D);
private static final AtomicLong lastBoundsFailureLog = new AtomicLong(0L);
@@ -163,6 +163,7 @@ public class IrisComplex implements DataProvider {
@Getter(AccessLevel.NONE)
private final int biomeBoundsSamplingStep;
private transient volatile SharedCornerBounds sharedCornerBounds;
private transient volatile SharedBoundsBiomes sharedBoundsBiomes;
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private transient volatile TerrainProgram terrainProgram;
@@ -2054,6 +2055,21 @@ public class IrisComplex implements DataProvider {
return shared;
}
private SharedBoundsBiomes sharedBoundsBiomes(ProceduralStream<IrisBiome> source) {
SharedBoundsBiomes shared = sharedBoundsBiomes;
if (shared != null && shared.source() == source) {
return shared;
}
synchronized (this) {
shared = sharedBoundsBiomes;
if (shared == null || shared.source() != source) {
shared = new SharedBoundsBiomes(source, SharedBoundsBiomes.capacityFor(Runtime.getRuntime().maxMemory()));
sharedBoundsBiomes = shared;
}
return shared;
}
}
private long computePackedBounds(GridBoundsCache cache, Engine engine, IrisInterpolator interpolator, IrisGenerator[] generators, int gx, int gz) {
NoiseBounds bounds = sampleBoundsRaw(cache, engine, interpolator, generators, gx, gz);
return (((long) Float.floatToRawIntBits((float) bounds.min())) << 32) | (Float.floatToRawIntBits((float) bounds.max()) & 0xFFFFFFFFL);
@@ -2688,6 +2704,7 @@ public class IrisComplex implements DataProvider {
private Engine engine;
private IrisGenerator[] generators;
private IdentityHashMap<IrisBiome, GeneratorBounds> cachedBounds;
private SharedBoundsBiomes sharedBiomes;
private boolean integerCoordinates;
private boolean inUse;
@@ -2702,6 +2719,7 @@ public class IrisComplex implements DataProvider {
this.cachedBounds = cachedBounds;
this.integerCoordinates = complex.baseBiomeStream instanceof BakedStream<?>
|| complex.baseBiomeStream instanceof CachedStream2D<?>;
this.sharedBiomes = integerCoordinates ? complex.sharedBoundsBiomes(complex.baseBiomeStream) : null;
this.inUse = true;
if (sampleSource.get() != complex.baseBiomeStream) {
sampleCache.clear();
@@ -2718,6 +2736,7 @@ public class IrisComplex implements DataProvider {
engine = null;
generators = null;
cachedBounds = null;
sharedBiomes = null;
inUse = false;
}
@@ -2728,11 +2747,17 @@ public class IrisComplex implements DataProvider {
double sampleZ = integerCoordinates ? (int) zz : zz;
IrisBiome bx = sampleCache.get(sampleX, sampleZ);
if (bx == null) {
ChunkBakeCache.enterSparse();
try {
bx = complex.baseBiomeStream.get(xx, zz);
} finally {
ChunkBakeCache.exitSparse();
bx = sharedBiomes == null ? null : sharedBiomes.get((int) sampleX, (int) sampleZ);
if (bx == null) {
ChunkBakeCache.enterSparse();
try {
bx = complex.baseBiomeStream.get(xx, zz);
} finally {
ChunkBakeCache.exitSparse();
}
if (sharedBiomes != null) {
sharedBiomes.put((int) sampleX, (int) sampleZ, bx);
}
}
sampleCache.put(sampleX, sampleZ, bx);
}
@@ -0,0 +1,86 @@
package art.arcane.iris.generation.runtime;
import art.arcane.iris.generation.biome.IrisBiome;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.Objects;
final class SharedBoundsBiomes {
static final int MINIMUM_CAPACITY = 1 << 16;
static final int MAXIMUM_CAPACITY = 1 << 21;
static final long BYTES_PER_ENTRY = 16L;
static final long HEAP_SHARE = 256L;
private static final VarHandle INTS = MethodHandles.arrayElementVarHandle(int[].class);
private static final VarHandle LONGS = MethodHandles.arrayElementVarHandle(long[].class);
private static final VarHandle OBJECTS = MethodHandles.arrayElementVarHandle(Object[].class);
private final Object source;
private final long[] keys;
private final Object[] values;
private final int[] stamps;
private final int mask;
SharedBoundsBiomes(Object source, int capacity) {
if (capacity < 2 || Integer.bitCount(capacity) != 1) {
throw new IllegalArgumentException("capacity must be a power of two of at least 2");
}
this.source = Objects.requireNonNull(source, "source");
this.keys = new long[capacity];
this.values = new Object[capacity];
this.stamps = new int[capacity];
this.mask = capacity - 1;
}
static int capacityFor(long maxMemory) {
long entries = Math.max(0L, maxMemory) / HEAP_SHARE / BYTES_PER_ENTRY;
long bounded = Math.max(MINIMUM_CAPACITY, Math.min(MAXIMUM_CAPACITY, entries));
return Integer.highestOneBit((int) bounded);
}
Object source() {
return source;
}
int slot(int x, int z) {
long mixed = key(x, z);
mixed = (mixed ^ (mixed >>> 30)) * 0xBF58476D1CE4E5B9L;
mixed = (mixed ^ (mixed >>> 27)) * 0x94D049BB133111EBL;
return (int) ((mixed ^ (mixed >>> 31)) & mask);
}
IrisBiome get(int x, int z) {
long key = key(x, z);
int slot = slot(x, z);
int before = (int) INTS.getAcquire(stamps, slot);
if (before == 0 || (before & 1) != 0) {
return null;
}
long storedKey = (long) LONGS.getAcquire(keys, slot);
Object value = OBJECTS.getAcquire(values, slot);
VarHandle.loadLoadFence();
int after = (int) INTS.getAcquire(stamps, slot);
if (before != after || storedKey != key) {
return null;
}
return (IrisBiome) value;
}
void put(int x, int z, IrisBiome biome) {
if (biome == null) {
return;
}
int slot = slot(x, z);
int stamp = (int) INTS.getAcquire(stamps, slot);
if ((stamp & 1) != 0 || !INTS.compareAndSet(stamps, slot, stamp, stamp + 1)) {
return;
}
LONGS.setRelease(keys, slot, key(x, z));
OBJECTS.setRelease(values, slot, biome);
INTS.setRelease(stamps, slot, stamp + 2);
}
private static long key(int x, int z) {
return ((long) x << 32) | (z & 0xFFFFFFFFL);
}
}