Baked object placement

- Bake rotated block offsets, palette indexes, tiles and canopy bits per object and spin kernel
- Resolve decay, rotation, waterlog and air facts once per palette entry per placement
- Skip the destination read for waterloggable states that cannot become waterlogged
- Move SpinKernel to its own file
This commit is contained in:
Brian Neumann-Fopiano committed 2026-10-21 23:50:55 -04:00
1 parent b526b480a7
commit 943c8d67e5
6 files changed
+979 -536

No files matched your search

@@ -61,6 +61,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.BiConsumer;
@@ -144,6 +145,7 @@ public class IrisObject extends IrisRegistrant {
protected transient AtomicCache<AxisAlignedBB> aabb = new AtomicCache<>();
transient final AtomicCache<KList<IrisBlockVector>> surfaceSupportOffsets = new AtomicCache<>();
transient final AtomicCache<FloatingObjectFootprint> floatingFootprint = new AtomicCache<>();
transient final ConcurrentHashMap<ObjectPlacementBake.Key, ObjectPlacementBake> placementBakes = new ConcurrentHashMap<>();
@Getter
@Setter
private transient TreeGrounding treeGrounding;
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,259 @@
package art.arcane.iris.structure.object;
import art.arcane.iris.generation.block.TileData;
import art.arcane.iris.generation.block.VectorMap;
import art.arcane.iris.generation.decoration.tree.TreeGrounding;
import art.arcane.iris.generation.geometry.IrisBlockVector;
import art.arcane.volmlib.nativelib.terrain.NativeBlockState;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
final class ObjectPlacementBake {
private static final int MAXIMUM_VARIANTS_PER_OBJECT = 32;
private final long blocksRevision;
private final long statesRevision;
private final TreeGrounding grounding;
private final int count;
private final int[] offsetX;
private final int[] offsetY;
private final int[] offsetZ;
private final int[] layerY;
private final short[] paletteIndexes;
private final NativeBlockState[] palette;
private final int[] tileBlocks;
private final TileData[] tileValues;
private final long[] canopy;
private final Key key;
private volatile double[] exact;
private ObjectPlacementBake(Key key, long blocksRevision, long statesRevision, TreeGrounding grounding, int count,
int[] offsetX, int[] offsetY, int[] offsetZ, int[] layerY, short[] paletteIndexes,
NativeBlockState[] palette, int[] tileBlocks, TileData[] tileValues, long[] canopy) {
this.key = key;
this.blocksRevision = blocksRevision;
this.statesRevision = statesRevision;
this.grounding = grounding;
this.count = count;
this.offsetX = offsetX;
this.offsetY = offsetY;
this.offsetZ = offsetZ;
this.layerY = layerY;
this.paletteIndexes = paletteIndexes;
this.palette = palette;
this.tileBlocks = tileBlocks;
this.tileValues = tileValues;
this.canopy = canopy;
}
static ObjectPlacementBake of(IrisObject object, SpinKernel spin, boolean ceilingHang, IrisBlockVector translateOffset) {
Key key = Key.of(spin, ceilingHang, translateOffset);
ConcurrentHashMap<Key, ObjectPlacementBake> variants = object.placementBakes;
ObjectPlacementBake cached = variants.get(key);
if (cached != null && cached.matches(object)) {
return cached;
}
ObjectPlacementBake built = bake(object, key, spin, ceilingHang, translateOffset);
if (built.matches(object)) {
if (variants.size() >= MAXIMUM_VARIANTS_PER_OBJECT) {
variants.clear();
}
variants.put(key, built);
}
return built;
}
private static ObjectPlacementBake bake(IrisObject object, Key key, SpinKernel spin, boolean ceilingHang,
IrisBlockVector translateOffset) {
object.readLock.lock();
try {
VectorMap<NativeBlockState> blocks = object.blocks;
VectorMap<TileData> states = object.states;
long blocksRevision = blocks.modificationRevision();
long statesRevision = states.modificationRevision();
TreeGrounding grounding = object.getTreeGrounding();
boolean hasStates = !states.isEmpty();
IntArrayList x = new IntArrayList();
IntArrayList y = new IntArrayList();
IntArrayList z = new IntArrayList();
IntArrayList layer = new IntArrayList();
IntArrayList indexes = new IntArrayList();
IntArrayList tileBlocks = new IntArrayList();
List<TileData> tileValues = new ArrayList<>();
List<NativeBlockState> palette = new ArrayList<>();
IdentityHashMap<NativeBlockState, Integer> paletteSlots = new IdentityHashMap<>();
IntArrayList canopyBlocks = new IntArrayList();
boolean layerDiffers = false;
IrisBlockVector rotated = new IrisBlockVector(0, 0, 0);
VectorMap<NativeBlockState>.Cursor cursor = blocks.cursor();
int index = 0;
while (cursor.next()) {
IrisBlockVector source = cursor.key();
NativeBlockState state = cursor.value();
if (state == null) {
state = IrisObject.States.air();
}
transform(source, rotated, spin, ceilingHang, translateOffset);
int roundedY = (int) Math.round(rotated.getY());
int floorY = rotated.getBlockY();
x.add((int) Math.round(rotated.getX()));
y.add(roundedY);
z.add((int) Math.round(rotated.getZ()));
layer.add(floorY);
layerDiffers |= floorY != roundedY;
Integer slot = paletteSlots.get(state);
if (slot == null) {
slot = palette.size();
palette.add(state);
paletteSlots.put(state, slot);
}
indexes.add(slot);
if (hasStates) {
TileData tile = states.get(source);
if (tile != null) {
tileBlocks.add(index);
tileValues.add(tile);
}
}
if (grounding != null && grounding.isCanopy(source.getBlockX(), source.getBlockY(), source.getBlockZ())) {
canopyBlocks.add(index);
}
index++;
}
if (palette.size() > Short.MAX_VALUE) {
throw new IllegalStateException("Object " + object.getLoadKey() + " has more than "
+ Short.MAX_VALUE + " distinct block states");
}
short[] paletteIndexes = new short[index];
for (int block = 0; block < index; block++) {
paletteIndexes[block] = (short) indexes.getInt(block);
}
long[] canopy = null;
if (!canopyBlocks.isEmpty()) {
canopy = new long[(index + 63) >>> 6];
for (int block : canopyBlocks) {
canopy[block >>> 6] |= 1L << (block & 63);
}
}
return new ObjectPlacementBake(key, blocksRevision, statesRevision, grounding, index,
x.toIntArray(), y.toIntArray(), z.toIntArray(), layerDiffers ? layer.toIntArray() : null,
paletteIndexes, palette.toArray(new NativeBlockState[0]),
tileBlocks.isEmpty() ? null : tileBlocks.toIntArray(),
tileValues.isEmpty() ? null : tileValues.toArray(new TileData[0]), canopy);
} finally {
object.readLock.unlock();
}
}
private static void transform(IrisBlockVector source, IrisBlockVector target, SpinKernel spin, boolean ceilingHang,
IrisBlockVector translateOffset) {
target.setX(source.getX());
target.setY(source.getY());
target.setZ(source.getZ());
spin.rotate(target);
if (ceilingHang) {
target.setY(-target.getBlockY());
}
if (translateOffset != null) {
target.add(translateOffset);
}
}
boolean matches(IrisObject object) {
return object.blocks.modificationRevision() == blocksRevision
&& object.states.modificationRevision() == statesRevision
&& object.getTreeGrounding() == grounding;
}
int count() {
return count;
}
int offsetX(int index) {
return offsetX[index];
}
int offsetY(int index) {
return offsetY[index];
}
int offsetZ(int index) {
return offsetZ[index];
}
int layerY(int index) {
return layerY == null ? offsetY[index] : layerY[index];
}
int paletteIndex(int index) {
return paletteIndexes[index];
}
NativeBlockState[] palette() {
return palette;
}
int[] tileBlocks() {
return tileBlocks;
}
TileData[] tileValues() {
return tileValues;
}
boolean canopy(int index) {
return canopy != null && (canopy[index >>> 6] & (1L << (index & 63))) != 0L;
}
double[] exact(IrisObject object, SpinKernel spin) {
double[] resolved = exact;
if (resolved != null) {
return resolved;
}
object.readLock.lock();
try {
resolved = new double[count * 3];
IrisBlockVector rotated = new IrisBlockVector(0, 0, 0);
VectorMap<NativeBlockState>.Cursor cursor = object.blocks.cursor();
int index = 0;
while (cursor.next() && index < count) {
transform(cursor.key(), rotated, spin, key.ceilingHang(), key.translateOffset());
resolved[index * 3] = rotated.getX();
resolved[index * 3 + 1] = rotated.getY();
resolved[index * 3 + 2] = rotated.getZ();
index++;
}
if (index != count) {
throw new IllegalStateException("Object " + object.getLoadKey() + " changed while its placement bake was read");
}
} finally {
object.readLock.unlock();
}
exact = resolved;
return resolved;
}
record Key(boolean rotates, int xMode, int yMode, int zMode, long xCos, long xSin, long yCos, long ySin,
long zCos, long zSin, boolean ceilingHang, boolean translating, long translateX, long translateY,
long translateZ) {
static Key of(SpinKernel spin, boolean ceilingHang, IrisBlockVector translateOffset) {
boolean translating = translateOffset != null;
return new Key(spin.rotates, spin.xMode, spin.yMode, spin.zMode,
Double.doubleToLongBits(spin.xCos), Double.doubleToLongBits(spin.xSin),
Double.doubleToLongBits(spin.yCos), Double.doubleToLongBits(spin.ySin),
Double.doubleToLongBits(spin.zCos), Double.doubleToLongBits(spin.zSin), ceilingHang, translating,
translating ? Double.doubleToLongBits(translateOffset.getX()) : 0L,
translating ? Double.doubleToLongBits(translateOffset.getY()) : 0L,
translating ? Double.doubleToLongBits(translateOffset.getZ()) : 0L);
}
IrisBlockVector translateOffset() {
return translating ? new IrisBlockVector(Double.longBitsToDouble(translateX),
Double.longBitsToDouble(translateY), Double.longBitsToDouble(translateZ)) : null;
}
}
}
@@ -0,0 +1,152 @@
package art.arcane.iris.structure.object;
import art.arcane.iris.generation.geometry.IrisBlockVector;
/**
* Precomputed vector rotation for a single placement.
* <p>
* This mirrors {@link IrisObjectRotation#rotate(IrisBlockVector, int, int, int)} branch for branch, with two
* differences: it mutates the vector handed to it instead of cloning, and the per-axis angle plus its cosine
* and sine are resolved once instead of once per block. The angle is a pure function of the axis clamp and the
* spin, both fixed for the placement, and Math.cos/Math.sin are pure, so every produced double is identical to
* the value the per-call form would have produced.
*/
final class SpinKernel {
static final int MODE_NONE = 0;
static final int MODE_FLIP = 1;
static final int MODE_QUARTER = 2;
static final int MODE_THREE_QUARTER = 3;
static final int MODE_ANGLE = 4;
final boolean rotates;
final int xMode;
final int yMode;
final int zMode;
final double xCos;
final double xSin;
final double yCos;
final double ySin;
final double zCos;
final double zSin;
SpinKernel(IrisObjectRotation rotation, int spinx, int spiny, int spinz) {
rotates = rotation.canRotate();
xMode = rotation.canRotateX() ? modeOf(rotation.getXAxis()) : MODE_NONE;
zMode = rotation.canRotateZ() ? modeOf(rotation.getZAxis()) : MODE_NONE;
yMode = rotation.canRotateY() ? modeOf(rotation.getYAxis()) : MODE_NONE;
double xAngle = xMode == MODE_ANGLE ? rotation.getXRotation(spinx) : 0;
double zAngle = zMode == MODE_ANGLE ? rotation.getZRotation(spinz) : 0;
double yAngle = yMode == MODE_ANGLE ? rotation.getYRotation(spiny) : 0;
xCos = Math.cos(xAngle);
xSin = Math.sin(xAngle);
zCos = Math.cos(zAngle);
zSin = Math.sin(zAngle);
yCos = Math.cos(yAngle);
ySin = Math.sin(yAngle);
}
private static int modeOf(IrisAxisRotationClamp clamp) {
if (!clamp.isLocked()) {
return MODE_ANGLE;
}
if (Math.abs(clamp.getMax()) % 360D == 180D) {
return MODE_FLIP;
}
if (clamp.getMax() % 360D == 90D || clamp.getMax() % 360D == -270D) {
return MODE_QUARTER;
}
if (clamp.getMax() == -90D || clamp.getMax() % 360D == 270D) {
return MODE_THREE_QUARTER;
}
return MODE_ANGLE;
}
/**
* Rotates in place. Axis order (X, then Z, then Y) is load bearing.
*/
void rotate(IrisBlockVector v) {
if (!rotates) {
return;
}
switch (xMode) {
case MODE_FLIP -> {
v.setZ(-v.getZ());
v.setY(-v.getY());
}
case MODE_QUARTER -> {
double z = v.getZ();
v.setZ(v.getY());
v.setY(-z);
}
case MODE_THREE_QUARTER -> {
double z = v.getZ();
v.setZ(-v.getY());
v.setY(z);
}
case MODE_ANGLE -> {
double y = xCos * v.getY() - xSin * v.getZ();
double z = xSin * v.getY() + xCos * v.getZ();
v.setY(y);
v.setZ(z);
}
default -> {
}
}
switch (zMode) {
case MODE_FLIP -> {
v.setY(-v.getY());
v.setX(-v.getX());
}
case MODE_QUARTER -> {
double y = v.getY();
v.setY(v.getX());
v.setX(-y);
}
case MODE_THREE_QUARTER -> {
double y = v.getY();
v.setY(-v.getX());
v.setX(y);
}
case MODE_ANGLE -> {
double x = zCos * v.getX() - zSin * v.getY();
double y = zSin * v.getX() + zCos * v.getY();
v.setX(x);
v.setY(y);
}
default -> {
}
}
switch (yMode) {
case MODE_FLIP -> {
v.setX(-v.getX());
v.setZ(-v.getZ());
}
case MODE_QUARTER -> {
double x = v.getX();
v.setX(v.getZ());
v.setZ(-x);
}
case MODE_THREE_QUARTER -> {
double x = v.getX();
v.setX(-v.getZ());
v.setZ(x);
}
case MODE_ANGLE -> {
double x = yCos * v.getX() + ySin * v.getZ();
double z = -ySin * v.getX() + yCos * v.getZ();
v.setX(x);
v.setZ(z);
}
default -> {
}
}
}
}
@@ -791,7 +791,7 @@ public class IrisObjectPlacementRunnerRegressionTest {
object.place(0, ANCHOR_Y, 0, placer, placement, new RNG(2L), data);
assertEquals(1, placer.blockReads);
assertEquals(enabled || authored ? 1 : 0, placer.blockReads);
assertEquals(1, placer.writes().size());
assertSame(submerged && (enabled || authored) ? wet : dry,
placer.writes().get(0).state());
@@ -0,0 +1,76 @@
package art.arcane.iris.structure.object;
import art.arcane.iris.generation.geometry.IrisBlockVector;
import art.arcane.iris.testsupport.KeyedBlockState;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
public class ObjectPlacementBakeTest {
@Test
public void spinsResolvingToTheSameKernelShareOneBake() {
IrisObject object = object();
IrisObjectRotation rotation = new IrisObjectRotation();
ObjectPlacementBake first = ObjectPlacementBake.of(object, new SpinKernel(rotation, 0, 10, 0), false, null);
ObjectPlacementBake second = ObjectPlacementBake.of(object, new SpinKernel(rotation, 3, 80, 9), false, null);
ObjectPlacementBake turned = ObjectPlacementBake.of(object, new SpinKernel(rotation, 0, 100, 0), false, null);
assertSame(first, second);
assertNotSame(first, turned);
assertEquals(3, first.count());
}
@Test
public void bakedOffsetsMatchTheKernelTransformInCursorOrder() {
IrisObject object = object();
IrisObjectRotation rotation = IrisObjectRotation.of(0D, 90D, 0D);
SpinKernel spin = new SpinKernel(rotation, 0, 0, 0);
IrisBlockVector translate = new IrisBlockVector(1.5D, -2D, 0.25D);
ObjectPlacementBake bake = ObjectPlacementBake.of(object, spin, true, translate);
double[] exact = bake.exact(object, spin);
int index = 0;
for (IrisBlockVector source : object.getBlocks().keys()) {
IrisBlockVector expected = source.clone();
spin.rotate(expected);
expected.setY(-expected.getBlockY());
expected.add(translate);
assertEquals((int) Math.round(expected.getX()), bake.offsetX(index));
assertEquals((int) Math.round(expected.getY()), bake.offsetY(index));
assertEquals((int) Math.round(expected.getZ()), bake.offsetZ(index));
assertEquals(expected.getBlockY(), bake.layerY(index));
assertEquals(expected.getX(), exact[index * 3], 0D);
assertEquals(expected.getY(), exact[index * 3 + 1], 0D);
assertEquals(expected.getZ(), exact[index * 3 + 2], 0D);
index++;
}
assertEquals(bake.count(), index);
}
@Test
public void editedObjectsRebake() {
IrisObject object = object();
SpinKernel spin = new SpinKernel(new IrisObjectRotation(), 0, 0, 0);
ObjectPlacementBake before = ObjectPlacementBake.of(object, spin, false, null);
object.getBlocks().put(new IrisBlockVector(1, 1, 1), new KeyedBlockState("minecraft:dirt"));
ObjectPlacementBake after = ObjectPlacementBake.of(object, spin, false, null);
assertNotSame(before, after);
assertEquals(4, after.count());
assertSame(after, ObjectPlacementBake.of(object, spin, false, null));
}
private static IrisObject object() {
IrisObject object = new IrisObject(5, 5, 5);
KeyedBlockState stone = new KeyedBlockState("minecraft:stone");
object.getBlocks().put(new IrisBlockVector(-2, 0, 1), stone);
object.getBlocks().put(new IrisBlockVector(0, -1, 2), stone);
object.getBlocks().put(new IrisBlockVector(2, 2, -2), new KeyedBlockState("minecraft:oak_log[axis=y]"));
return object;
}
}