Reused cave density columns

- Cave passes reuse validated lattice columns in local scratch
- Generation releases retained columns after each carve
- Regression tests cover density parity and cache lifetime
This commit is contained in:
Brian Neumann-Fopiano committed 2026-10-13 12:01:59 -04:00
1 parent 2e3a41f262
commit 54feb80295
4 files changed
+175 -7

No files matched your search

@@ -40,6 +40,7 @@ final class CaveCarveScratch {
final double[] planeThresholdLimit = new double[256];
final boolean[] planeCarve = new boolean[256];
final double[] adaptivePlaneDensity = new double[81];
final CaveDensityGrid.Column[] densityColumns = new CaveDensityGrid.Column[16];
double[] verticalDensityLower = new double[9];
double[] verticalDensityUpper = new double[9];
int verticalDensityLowerY = Integer.MIN_VALUE;
@@ -79,8 +80,9 @@ final class CaveCarveScratch {
int adaptiveGeometryAxisCells = -1;
boolean fullWeightsInitialized;
void releaseSections() {
void releaseReferences() {
Arrays.fill(sectionMatter, null);
Arrays.fill(sectionSlices, null);
Arrays.fill(densityColumns, null);
}
}
@@ -37,7 +37,7 @@ final class CaveDensityGrid {
if (cached == null) {
cached = columns.putIfAbsent(key, new Column(owner, latticeX, latticeZ, height));
}
return cached.owner == owner && cached.latticeX == latticeX && cached.latticeZ == latticeZ ? cached : null;
return cached.matches(owner, latticeX, latticeZ) ? cached : null;
}
void clear() {
@@ -57,6 +57,10 @@ final class CaveDensityGrid {
segments = new AtomicReferenceArray<>(Math.ceilDiv(Math.max(1, height), SEGMENT_SIZE));
}
boolean matches(Object owner, int latticeX, int latticeZ) {
return this.owner == owner && this.latticeX == latticeX && this.latticeZ == latticeZ;
}
double get(int y) {
int segment = y >> SEGMENT_SHIFT;
if (segment >= segments.length()) {
@@ -449,7 +449,7 @@ public class IrisCaveCarver3D {
return carved;
} finally {
scratchCache.get().releaseSections();
scratchCache.get().releaseReferences();
carving.set(nested);
if (nested) {
scratchCache.set(previousScratch);
@@ -2466,8 +2466,19 @@ public class IrisCaveCarver3D {
private double latticeDensity(CaveCarveScratch scratch, int x, int y, int z) {
int latticeMask = (1 << CaveDensityGrid.LATTICE_SHIFT) - 1;
CaveDensityGrid.Column column = ((x | z) & latticeMask) != 0 ? null : densityGrid.column(this, densityGridOwner,
densityMaximumY + 1, x >> CaveDensityGrid.LATTICE_SHIFT, z >> CaveDensityGrid.LATTICE_SHIFT);
if (((x | z) & latticeMask) != 0) {
return samplePlaneDensity(scratch, x, y, z);
}
int latticeX = x >> CaveDensityGrid.LATTICE_SHIFT;
int latticeZ = z >> CaveDensityGrid.LATTICE_SHIFT;
int slot = ((latticeX & 3) << 2) | (latticeZ & 3);
CaveDensityGrid.Column column = parallelDensity ? scratch.densityColumns[slot] : null;
if (column == null || !column.matches(this, latticeX, latticeZ)) {
column = densityGrid.column(this, densityGridOwner, densityMaximumY + 1, latticeX, latticeZ);
if (parallelDensity) {
scratch.densityColumns[slot] = column;
}
}
if (column == null) {
return samplePlaneDensity(scratch, x, y, z);
}