Merged carve field reads

- Batched baked carve field reads, per-thread bake chunk memory, terminal carving roots return early
This commit is contained in:
Brian Neumann-Fopiano committed 2026-10-22 11:29:26 -04:00
commit 9d4c55d11a
9 files changed
+644 -44

No files matched your search

@@ -3,6 +3,7 @@ package art.arcane.iris.generation.bake;
import art.arcane.iris.generation.cache.ConcurrentClockCache;
import art.arcane.iris.generation.stream.ProvisionalSampling;
import java.util.Arrays;
import java.util.Objects;
public final class ChunkBakeCache {
@@ -10,6 +11,7 @@ public final class ChunkBakeCache {
static final int MAXIMUM_CAPACITY = 32_768;
static final long BYTES_PER_ENTRY = 13L * 1_024L;
static final int HEAP_SHARE = 32;
private static final int RECENT_SLOTS = 16;
private static final ThreadLocal<int[]> SPARSE = ThreadLocal.withInitial(() -> new int[1]);
private final PlaneSource source;
@@ -52,17 +54,16 @@ public final class ChunkBakeCache {
public ChunkPlanes.Plane planeIfPresent(BakePlane plane, int chunkX, int chunkZ) {
long key = key(chunkX, chunkZ);
int slot = Recent.slot(chunkX, chunkZ);
Recent last = recent.get();
int current = generation;
ChunkPlanes planes;
if (last.key == key && last.generation == current) {
planes = last.planes;
} else {
ChunkPlanes planes = last.get(key, slot, current);
if (planes == null) {
planes = entries.get(key);
if (planes == null) {
return null;
}
last.remember(key, planes, current);
last.remember(key, slot, planes, current);
}
return planes.planeIfPresent(plane);
}
@@ -75,6 +76,10 @@ public final class ChunkBakeCache {
return remembered(chunkX, chunkZ).complete(plane, source);
}
public void fillColumns(BakePlane plane, int chunkX, int chunkZ, long[] columns, Object target) {
remembered(chunkX, chunkZ).plane(plane).fillColumns(source, chunkX, chunkZ, columns, target);
}
public boolean memoAllowed() {
return SPARSE.get()[0] == 0 && !source.mainThread();
}
@@ -102,7 +107,7 @@ public final class ChunkBakeCache {
private ChunkPlanes remembered(int chunkX, int chunkZ) {
int current = generation;
ChunkPlanes planes = planes(chunkX, chunkZ);
recent.get().remember(key(chunkX, chunkZ), planes, current);
recent.get().remember(key(chunkX, chunkZ), Recent.slot(chunkX, chunkZ), planes, current);
return planes;
}
@@ -123,14 +128,25 @@ public final class ChunkBakeCache {
}
private static final class Recent {
private long key;
private final long[] keys = new long[RECENT_SLOTS];
private final ChunkPlanes[] planes = new ChunkPlanes[RECENT_SLOTS];
private int generation = -1;
private ChunkPlanes planes;
private void remember(long key, ChunkPlanes planes, int generation) {
this.key = key;
this.planes = planes;
this.generation = generation;
private static int slot(int chunkX, int chunkZ) {
return ((chunkZ & 3) << 2) | (chunkX & 3);
}
private ChunkPlanes get(long key, int slot, int generation) {
return this.generation == generation && keys[slot] == key ? planes[slot] : null;
}
private void remember(long key, int slot, ChunkPlanes value, int generation) {
if (this.generation != generation) {
Arrays.fill(planes, null);
this.generation = generation;
}
keys[slot] = key;
planes[slot] = value;
}
}
}
@@ -145,6 +145,55 @@ public final class ChunkPlanes {
return values;
}
void fillColumns(PlaneSource source, int chunkX, int chunkZ, long[] columns, Object target) {
long[] missing = new long[4];
boolean any = false;
if (!complete) {
for (int word = 0; word < 4; word++) {
missing[word] = columns[word] & ~(long) READY.getAcquire(ready, word);
any |= missing[word] != 0L;
}
}
if (!any) {
copyColumns(columns, values, target);
return;
}
long revision = ProvisionalSampling.revision();
source.fill(kind, chunkX, chunkZ, missing, target);
long[] known = new long[4];
for (int word = 0; word < 4; word++) {
known[word] = columns[word] & ~missing[word];
}
copyColumns(known, values, target);
if (revision == ProvisionalSampling.revision()) {
publish(missing, target);
}
}
private void publish(long[] missing, Object computed) {
copyColumns(missing, computed, values);
for (int word = 0; word < 4; word++) {
if (missing[word] != 0L) {
READY.getAndBitwiseOrRelease(ready, word, missing[word]);
}
}
}
private void copyColumns(long[] columns, Object from, Object to) {
for (int word = 0; word < 4; word++) {
long bits = columns[word];
while (bits != 0L) {
int index = (word << 6) | Long.numberOfTrailingZeros(bits);
if (kind.doubles()) {
((double[]) to)[index] = ((double[]) from)[index];
} else {
((Object[]) to)[index] = ((Object[]) from)[index];
}
bits &= bits - 1L;
}
}
}
private Object unpublishedFill(PlaneSource source, int chunkX, int chunkZ) {
long[] missing = missing();
Object computed = kind.doubles() ? new double[256] : new Object[256];
@@ -34,6 +34,8 @@ import art.arcane.volmlib.nativelib.terrain.NativeBlockState;
import art.arcane.iris.generation.block.B;
import art.arcane.iris.generation.context.ChunkContext;
import art.arcane.volmlib.util.stream.ProceduralStream;
import art.arcane.iris.generation.stream.BakedDoubleStream;
import art.arcane.iris.generation.stream.BakedStream;
import art.arcane.iris.generation.stream.ChunkFillableDoubleStream2D;
import art.arcane.iris.generation.simd.SimdSupport;
import art.arcane.volmlib.util.documentation.ChunkCoordinates;
@@ -64,6 +66,12 @@ public class MantleCarvingComponent extends IrisMantleComponent {
private static final int[] KERNEL_DX = new int[KERNEL_SIZE];
private static final int[] KERNEL_DZ = new int[KERNEL_SIZE];
private static final double[] KERNEL_WEIGHT = new double[KERNEL_SIZE];
private static final int NEIGHBOURS = 9;
private static final int[] NEIGHBOUR_DX = new int[NEIGHBOURS];
private static final int[] NEIGHBOUR_DZ = new int[NEIGHBOURS];
private static final long[][] NEIGHBOUR_COLUMNS = new long[NEIGHBOURS][4];
private static final int[] FIELD_NEIGHBOUR = new int[FIELD_SIZE * FIELD_SIZE];
private static final int[] FIELD_COLUMN = new int[FIELD_SIZE * FIELD_SIZE];
private static final ThreadLocal<BlendScratch> BLEND_SCRATCH = ThreadLocal.withInitial(BlendScratch::new);
private final Object profileCarverLock = new Object();
@@ -81,6 +89,22 @@ public class MantleCarvingComponent extends IrisMantleComponent {
kernelIndex++;
}
}
for (int neighbour = 0; neighbour < NEIGHBOURS; neighbour++) {
NEIGHBOUR_DX[neighbour] = (neighbour / 3) - 1;
NEIGHBOUR_DZ[neighbour] = (neighbour % 3) - 1;
}
for (int fieldX = 0; fieldX < FIELD_SIZE; fieldX++) {
int offsetX = fieldX - BLEND_RADIUS;
for (int fieldZ = 0; fieldZ < FIELD_SIZE; fieldZ++) {
int offsetZ = fieldZ - BLEND_RADIUS;
int neighbour = ((Math.floorDiv(offsetX, CHUNK_SIZE) + 1) * 3) + Math.floorDiv(offsetZ, CHUNK_SIZE) + 1;
int column = ((offsetZ & (CHUNK_SIZE - 1)) << 4) | (offsetX & (CHUNK_SIZE - 1));
int fieldIndex = (fieldX * FIELD_SIZE) + fieldZ;
FIELD_NEIGHBOUR[fieldIndex] = neighbour;
FIELD_COLUMN[fieldIndex] = column;
NEIGHBOUR_COLUMNS[neighbour][column >>> 6] |= 1L << (column & 63);
}
}
}
public MantleCarvingComponent(EngineMantle engineMantle) {
@@ -128,7 +152,7 @@ public class MantleCarvingComponent extends IrisMantleComponent {
private void generateLayer(MantleWriter writer, int x, int z, ChunkContext context, BlendScratch blendScratch) {
IrisComplex complex = context.getComplex();
IrisDimensionCarvingResolver.State resolverState = new IrisDimensionCarvingResolver.State();
IrisDimensionCarvingResolver.State resolverState = blendScratch.resolverState;
int[] chunkSurfaceHeights = prepareChunkSurfaceHeights(x, z, context, blendScratch.chunkSurfaceHeights);
PrecisionStopwatch resolveStopwatch = PrecisionStopwatch.start();
List<WeightedProfile> weightedProfiles = resolveWeightedProfiles(x, z, complex, resolverState);
@@ -422,7 +446,7 @@ public class MantleCarvingComponent extends IrisMantleComponent {
private void fillProfileField(IrisCaveProfile[] profileField, int chunkX, int chunkZ, IrisComplex complex, IrisDimensionCarvingResolver.State resolverState, BlendScratch blendScratch) {
int startX = PowerOfTwoCoordinates.chunkToBlock(chunkX) - BLEND_RADIUS;
int startZ = PowerOfTwoCoordinates.chunkToBlock(chunkZ) - BLEND_RADIUS;
prefillProfileFieldSamples(startX, startZ, complex, blendScratch);
prefillProfileFieldSamples(chunkX, chunkZ, startX, startZ, complex, blendScratch);
for (int fieldX = 0; fieldX < FIELD_SIZE; fieldX++) {
int worldX = startX + fieldX;
@@ -519,17 +543,28 @@ public class MantleCarvingComponent extends IrisMantleComponent {
return resolved;
}
private void prefillProfileFieldSamples(int startX, int startZ, IrisComplex complex, BlendScratch blendScratch) {
fillFieldHeights(complex.getNaturalHeightStream(), startX, startZ, blendScratch.fieldSurfaceHeights);
Arrays.fill(blendScratch.fieldFluidHeights, complex.getFluidHeight());
fillFieldFluidPresence(complex, startX, startZ, blendScratch.fieldSurfaceHeights,
blendScratch.fieldFluidHeights, blendScratch.fieldHasFluid);
fillFieldObjects(complex.getRegionStream(), startX, startZ, blendScratch.fieldRegions);
fillFieldObjects(complex.getNaturalTrueBiomeStream(), startX, startZ, blendScratch.fieldSurfaceBiomes);
fillFieldObjects(complex.getCaveBiomeStream(), startX, startZ, blendScratch.fieldCaveBiomes);
private void prefillProfileFieldSamples(int chunkX, int chunkZ, int startX, int startZ, IrisComplex complex, BlendScratch blendScratch) {
fillFieldHeights(complex.getNaturalHeightStream(), chunkX, chunkZ, startX, startZ, blendScratch);
double fluidHeight = complex.getFluidHeight();
Arrays.fill(blendScratch.fieldFluidHeights, fluidHeight);
fillFieldFluidPresence(complex.getFluidStream(), chunkX, chunkZ, startX, startZ, Math.round(fluidHeight), blendScratch);
fillFieldObjects(complex.getRegionStream(), chunkX, chunkZ, startX, startZ, blendScratch.fieldRegions, blendScratch.neighbourObjects);
fillFieldObjects(complex.getNaturalTrueBiomeStream(), chunkX, chunkZ, startX, startZ, blendScratch.fieldSurfaceBiomes, blendScratch.neighbourObjects);
fillFieldObjects(complex.getCaveBiomeStream(), chunkX, chunkZ, startX, startZ, blendScratch.fieldCaveBiomes, blendScratch.neighbourObjects);
}
private <T> void fillFieldObjects(ProceduralStream<T> stream, int startX, int startZ, T[] target) {
@SuppressWarnings("unchecked")
private <T> void fillFieldObjects(ProceduralStream<T> stream, int chunkX, int chunkZ, int startX, int startZ, T[] target, Object[][] neighbourObjects) {
if (stream instanceof BakedStream<?> baked) {
for (int neighbour = 0; neighbour < NEIGHBOURS; neighbour++) {
baked.fillColumns(chunkX + NEIGHBOUR_DX[neighbour], chunkZ + NEIGHBOUR_DZ[neighbour],
NEIGHBOUR_COLUMNS[neighbour], neighbourObjects[neighbour]);
}
for (int fieldIndex = 0; fieldIndex < target.length; fieldIndex++) {
target[fieldIndex] = (T) neighbourObjects[FIELD_NEIGHBOUR[fieldIndex]][FIELD_COLUMN[fieldIndex]];
}
return;
}
for (int fieldX = 0; fieldX < FIELD_SIZE; fieldX++) {
int worldX = startX + fieldX;
for (int fieldZ = 0; fieldZ < FIELD_SIZE; fieldZ++) {
@@ -538,7 +573,19 @@ public class MantleCarvingComponent extends IrisMantleComponent {
}
}
private void fillFieldHeights(ProceduralStream<Double> stream, int startX, int startZ, double[] target) {
private void fillFieldHeights(ProceduralStream<Double> stream, int chunkX, int chunkZ, int startX, int startZ, BlendScratch blendScratch) {
double[] target = blendScratch.fieldSurfaceHeights;
if (stream instanceof BakedDoubleStream baked) {
double[][] neighbourHeights = blendScratch.neighbourHeights;
for (int neighbour = 0; neighbour < NEIGHBOURS; neighbour++) {
baked.fillColumns(chunkX + NEIGHBOUR_DX[neighbour], chunkZ + NEIGHBOUR_DZ[neighbour],
NEIGHBOUR_COLUMNS[neighbour], neighbourHeights[neighbour]);
}
for (int fieldIndex = 0; fieldIndex < target.length; fieldIndex++) {
target[fieldIndex] = neighbourHeights[FIELD_NEIGHBOUR[fieldIndex]][FIELD_COLUMN[fieldIndex]];
}
return;
}
for (int fieldX = 0; fieldX < FIELD_SIZE; fieldX++) {
int worldX = startX + fieldX;
for (int fieldZ = 0; fieldZ < FIELD_SIZE; fieldZ++) {
@@ -547,20 +594,41 @@ public class MantleCarvingComponent extends IrisMantleComponent {
}
}
private void fillFieldFluidPresence(
IrisComplex complex,
int startX,
int startZ,
double[] surfaceHeights,
double[] fluidHeights,
boolean[] target
) {
private void fillFieldFluidPresence(ProceduralStream<NativeBlockState> stream, int chunkX, int chunkZ, int startX, int startZ,
long roundedFluidHeight, BlendScratch blendScratch) {
double[] surfaceHeights = blendScratch.fieldSurfaceHeights;
boolean[] target = blendScratch.fieldHasFluid;
if (stream instanceof BakedStream<?> baked) {
long[][] submerged = blendScratch.neighbourSubmerged;
for (long[] columns : submerged) {
Arrays.fill(columns, 0L);
}
for (int fieldIndex = 0; fieldIndex < target.length; fieldIndex++) {
if (Math.round(surfaceHeights[fieldIndex]) < roundedFluidHeight) {
int column = FIELD_COLUMN[fieldIndex];
submerged[FIELD_NEIGHBOUR[fieldIndex]][column >>> 6] |= 1L << (column & 63);
}
}
Object[][] neighbourObjects = blendScratch.neighbourObjects;
for (int neighbour = 0; neighbour < NEIGHBOURS; neighbour++) {
long[] columns = submerged[neighbour];
if ((columns[0] | columns[1] | columns[2] | columns[3]) != 0L) {
baked.fillColumns(chunkX + NEIGHBOUR_DX[neighbour], chunkZ + NEIGHBOUR_DZ[neighbour],
columns, neighbourObjects[neighbour]);
}
}
for (int fieldIndex = 0; fieldIndex < target.length; fieldIndex++) {
target[fieldIndex] = Math.round(surfaceHeights[fieldIndex]) < roundedFluidHeight
&& B.isFluid((NativeBlockState) neighbourObjects[FIELD_NEIGHBOUR[fieldIndex]][FIELD_COLUMN[fieldIndex]]);
}
return;
}
for (int fieldX = 0; fieldX < FIELD_SIZE; fieldX++) {
int worldX = startX + fieldX;
for (int fieldZ = 0; fieldZ < FIELD_SIZE; fieldZ++) {
int fieldIndex = (fieldX * FIELD_SIZE) + fieldZ;
target[fieldIndex] = B.isFluid(complex.getFluidStream().get(worldX, startZ + fieldZ))
&& Math.round(surfaceHeights[fieldIndex]) < Math.round(fluidHeights[fieldIndex]);
target[fieldIndex] = B.isFluid(stream.get(worldX, startZ + fieldZ))
&& Math.round(surfaceHeights[fieldIndex]) < roundedFluidHeight;
}
}
}
@@ -682,6 +750,10 @@ public class MantleCarvingComponent extends IrisMantleComponent {
private final IrisBiome[] fieldCaveBiomes = new IrisBiome[FIELD_SIZE * FIELD_SIZE];
private final int[] chunkSurfaceHeights = new int[CHUNK_AREA];
private final double[] chunkSurfaceHeightSamples = new double[CHUNK_AREA];
private final double[][] neighbourHeights = new double[NEIGHBOURS][CHUNK_AREA];
private final Object[][] neighbourObjects = new Object[NEIGHBOURS][CHUNK_AREA];
private final long[][] neighbourSubmerged = new long[NEIGHBOURS][4];
private final IrisDimensionCarvingResolver.State resolverState = new IrisDimensionCarvingResolver.State();
private int nextWeightBuffer;
private boolean inUse;
@@ -705,6 +777,9 @@ public class MantleCarvingComponent extends IrisMantleComponent {
Arrays.fill(fieldRegions, null);
Arrays.fill(fieldSurfaceBiomes, null);
Arrays.fill(fieldCaveBiomes, null);
for (Object[] objects : neighbourObjects) {
Arrays.fill(objects, null);
}
columnProfileWeights.clear();
profileOrder.clear();
nextWeightBuffer = 0;
@@ -81,6 +81,21 @@ public final class BakedDoubleStream extends BasicStream<Double> implements Chun
}
}
public void fillColumns(int chunkX, int chunkZ, long[] columns, double[] target) {
if (cache.memoAllowed()) {
cache.fillColumns(plane, chunkX, chunkZ, columns, target);
return;
}
int originX = chunkX << 4;
int originZ = chunkZ << 4;
for (int word = 0; word < 4; word++) {
for (long bits = columns[word]; bits != 0L; bits &= bits - 1L) {
int index = (word << 6) | Long.numberOfTrailingZeros(bits);
target[index] = getDouble(originX + (index & 15), originZ + (index >> 4));
}
}
}
@Override
public void fillChunkRaw(int worldX, int worldZ, Object[] target) {
double[] values = new double[256];
@@ -54,6 +54,21 @@ public final class BakedStream<T> extends BasicStream<T> implements ChunkFillabl
return scalar.fromDouble(d);
}
public void fillColumns(int chunkX, int chunkZ, long[] columns, Object[] target) {
if (cache.memoAllowed()) {
cache.fillColumns(plane, chunkX, chunkZ, columns, target);
return;
}
int originX = chunkX << 4;
int originZ = chunkZ << 4;
for (int word = 0; word < 4; word++) {
for (long bits = columns[word]; bits != 0L; bits &= bits - 1L) {
int index = (word << 6) | Long.numberOfTrailingZeros(bits);
target[index] = get(originX + (index & 15), originZ + (index >> 4));
}
}
}
@Override
public void fillChunkRaw(int worldX, int worldZ, Object[] target) {
int chunkX = worldX >> 4;
@@ -120,7 +120,7 @@ public final class IrisDimensionCarvingResolver {
}
int remainingDepth = clampDepth(rootEntry.getChildRecursionDepth());
if (remainingDepth <= 0) {
if (remainingDepth <= 0 || terminal(engine, data, rootEntry, rootBiome, resolvedState)) {
return rootEntry;
}
@@ -131,13 +131,12 @@ public final class IrisDimensionCarvingResolver {
return columns.entries[slot];
}
Map<String, IrisDimensionCarvingEntry> entryIndex = resolveEntryIndex(engine, resolvedState);
IrisDimensionCarvingEntry current = rootEntry;
int depth = remainingDepth;
boolean cacheable = true;
while (depth > 0) {
cacheable &= fixedChildStyle(current);
IrisDimensionCarvingEntry selected = selectChild(engine, data, current, worldX, worldZ, entryIndex, resolvedState);
IrisDimensionCarvingEntry selected = selectChild(engine, data, current, worldX, worldZ, resolvedState);
if (selected == null || selected == current || selected.getWorldYRange() != null
&& !selected.getWorldYRange().contains(worldY)) {
break;
@@ -164,6 +163,24 @@ public final class IrisDimensionCarvingResolver {
return current;
}
private static boolean terminal(Engine engine, IrisData data, IrisDimensionCarvingEntry entry, IrisBiome biome, State state) {
KList<String> children = entry.getChildren();
if (children == null || children.isEmpty()) {
return true;
}
return selectionPlan(engine, data, entry, biome, state).size() <= 1L;
}
private static IrisRaritySelection<CarvingChoice> selectionPlan(Engine engine, IrisData data, IrisDimensionCarvingEntry parent,
IrisBiome parentBiome, State state) {
IrisRaritySelection<CarvingChoice> selectionPlan = state.selectionPlans.get(parent);
if (selectionPlan == null) {
selectionPlan = buildSelectionPlan(data, parent, parentBiome, resolveEntryIndex(engine, state), state);
state.selectionPlans.put(parent, selectionPlan);
}
return selectionPlan;
}
private static boolean fixedChildStyle(IrisDimensionCarvingEntry entry) {
for (IrisGeneratorStyle style = entry.getChildStyle(); style != null; style = style.getFracture()) {
if (style.getExpression() != null) {
@@ -202,7 +219,6 @@ public final class IrisDimensionCarvingResolver {
IrisDimensionCarvingEntry parent,
int worldX,
int worldZ,
Map<String, IrisDimensionCarvingEntry> entryIndex,
State state
) {
KList<String> children = parent.getChildren();
@@ -215,12 +231,7 @@ public final class IrisDimensionCarvingResolver {
return parent;
}
IrisRaritySelection<CarvingChoice> selectionPlan = state.selectionPlans.get(parent);
if (selectionPlan == null) {
selectionPlan = buildSelectionPlan(data, parent, parentBiome, entryIndex, state);
state.selectionPlans.put(parent, selectionPlan);
}
IrisRaritySelection<CarvingChoice> selectionPlan = selectionPlan(engine, data, parent, parentBiome, state);
if (selectionPlan.size() <= 1L) {
return parent;
}