From e01097264b254c7876f307f9a95fa996d7f09bfb Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Mon, 20 Apr 2026 10:11:46 -0700 Subject: [PATCH] Fix infinite extent AABB crash w/ kotlin, and ensure proper sizing/padding on VAO/VBO snapshots. (#1687) --- .../recording/commands/AttribLayoutKey.java | 27 ++++++ .../commands/IndexedDrawBatchBuilder.java | 11 +-- .../commands/IndexedDrawCapture.java | 51 +++++----- .../glsm/recording/IndexedDrawReplayTest.java | 4 +- .../commands/AttribLayoutKeyTest.java | 95 +++++++++++++++++++ .../TileEntityRenderBoundsRegistry.java | 6 +- .../AngelicaChunkBuilderMeshingTask.java | 9 +- .../early/rendering/MixinTileEntity.java | 3 +- 8 files changed, 163 insertions(+), 43 deletions(-) create mode 100644 glsm/src/test/java/com/gtnewhorizons/angelica/glsm/recording/commands/AttribLayoutKeyTest.java diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/commands/AttribLayoutKey.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/commands/AttribLayoutKey.java index ea4375607..1b6b67976 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/commands/AttribLayoutKey.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/commands/AttribLayoutKey.java @@ -1,5 +1,7 @@ package com.gtnewhorizons.angelica.glsm.recording.commands; +import com.gtnewhorizons.angelica.glsm.states.VertexAttribState; + import java.util.Arrays; public final class AttribLayoutKey { @@ -7,6 +9,8 @@ public final class AttribLayoutKey { final int[] sizes; final int[] types; final boolean[] normalized; + private final int[] offsets; + private final int stride; private final int hash; public AttribLayoutKey(int[] locations, int[] sizes, int[] types, boolean[] normalized) { @@ -14,6 +18,21 @@ public final class AttribLayoutKey { this.sizes = sizes; this.types = types; this.normalized = normalized; + + final int n = locations.length; + final int[] off = new int[n]; + int base = 0; + int maxAlign = 1; + for (int i = 0; i < n; i++) { + final int typeSize = VertexAttribState.Attrib.glTypeSizeBytes(types[i]); + if (typeSize > maxAlign) maxAlign = typeSize; + base = (base + typeSize - 1) & -typeSize; + off[i] = base; + base += sizes[i] * typeSize; + } + this.offsets = off; + this.stride = (base + maxAlign - 1) & -maxAlign; + int h = Arrays.hashCode(locations); h = 31 * h + Arrays.hashCode(sizes); h = 31 * h + Arrays.hashCode(types); @@ -21,6 +40,14 @@ public final class AttribLayoutKey { this.hash = h; } + public int offset(int i) { + return offsets[i]; + } + + public int stride() { + return stride; + } + @Override public boolean equals(Object o) { if (!(o instanceof AttribLayoutKey k)) return false; diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/commands/IndexedDrawBatchBuilder.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/commands/IndexedDrawBatchBuilder.java index fbfbad8f0..238f6cacb 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/commands/IndexedDrawBatchBuilder.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/commands/IndexedDrawBatchBuilder.java @@ -2,7 +2,6 @@ package com.gtnewhorizons.angelica.glsm.recording.commands; import com.gtnewhorizon.gtnhlib.bytebuf.MemoryUtilities; import com.gtnewhorizons.angelica.glsm.GLStateManager; -import com.gtnewhorizons.angelica.glsm.states.VertexAttribState; import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL15; @@ -81,7 +80,7 @@ public final class IndexedDrawBatchBuilder { long totalVertexBytes = 0; long totalIndexBytes = 0; for (IndexedDrawCapture c : group) { - totalVertexBytes += (long) c.vertexCount * c.tightStride; + totalVertexBytes += (long) c.vertexCount * c.layoutKey.stride(); totalIndexBytes += (long) c.indexCount * 4; // always GL_UNSIGNED_INT } if (totalVertexBytes > Integer.MAX_VALUE || totalIndexBytes > Integer.MAX_VALUE) { @@ -116,7 +115,7 @@ public final class IndexedDrawBatchBuilder { for (int g = 0; g < n; g++) { final IndexedDrawCapture c = group.get(g); - final int vBytes = c.vertexCount * c.tightStride; + final int vBytes = c.vertexCount * c.layoutKey.stride(); GLStateManager.glBufferSubData(GL15.GL_ARRAY_BUFFER, vboBytePos, c.vertexData); final long srcIdxAddr = memAddress0(c.indexData); @@ -148,15 +147,13 @@ public final class IndexedDrawBatchBuilder { GLStateManager.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, eboData, GL15.GL_STATIC_DRAW); final AttribLayoutKey layout = group.get(0).layoutKey; - final int stride = group.get(0).tightStride; - int attribOffset = 0; + final int stride = layout.stride(); for (int i = 0; i < layout.locations.length; i++) { final int loc = layout.locations[i]; final int size = layout.sizes[i]; final int type = layout.types[i]; - GLStateManager.glVertexAttribPointer(loc, size, type, layout.normalized[i], stride, attribOffset); + GLStateManager.glVertexAttribPointer(loc, size, type, layout.normalized[i], stride, layout.offset(i)); GLStateManager.glEnableVertexAttribArray(loc); - attribOffset += size * VertexAttribState.Attrib.glTypeSizeBytes(type); } success = true; diff --git a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/commands/IndexedDrawCapture.java b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/commands/IndexedDrawCapture.java index ba42ddf67..f4f379254 100644 --- a/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/commands/IndexedDrawCapture.java +++ b/glsm/src/main/java/com/gtnewhorizons/angelica/glsm/recording/commands/IndexedDrawCapture.java @@ -26,12 +26,11 @@ public final class IndexedDrawCapture { public final int indexCount; public final ByteBuffer vertexData; public final ByteBuffer indexData; - public final int tightStride; public final BatchedIndexedDrawCmd placeholder; private boolean freed; public IndexedDrawCapture(AttribLayoutKey layoutKey, int drawMode, int vertexCount, int indexCount, - ByteBuffer vertexData, ByteBuffer indexData, int tightStride, + ByteBuffer vertexData, ByteBuffer indexData, BatchedIndexedDrawCmd placeholder) { this.layoutKey = layoutKey; this.drawMode = drawMode; @@ -39,7 +38,6 @@ public final class IndexedDrawCapture { this.indexCount = indexCount; this.vertexData = vertexData; this.indexData = indexData; - this.tightStride = tightStride; this.placeholder = placeholder; } @@ -120,12 +118,6 @@ public final class IndexedDrawCapture { final int[] types = new int[enabledCount]; final boolean[] normalized = new boolean[enabledCount]; int k = 0; - int tightStride = 0; - int expectedSrcOffset = -1; - ByteBuffer sharedSrcBuffer = null; - long sharedSrcBase = 0; - int sharedSrcStride = -1; - boolean tightSrc = true; for (int i = 0; i < VertexAttribState.MAX_ATTRIBS; i++) { final AttribSnapshot.AttribDesc d = snap.get(i); if (d == null) continue; @@ -133,23 +125,27 @@ public final class IndexedDrawCapture { sizes[k] = d.size(); types[k] = d.type(); normalized[k] = d.normalized(); - final int attribBytes = d.size() * d.typeSizeBytes(); - if (tightSrc) { - final int relOffset = (int) (d.offset() - d.readBufferBaseOffset()); - if (k == 0) { - sharedSrcBuffer = d.readBuffer(); - sharedSrcStride = d.effectiveStride(); - sharedSrcBase = MemoryUtilities.memAddress0(sharedSrcBuffer) + relOffset + (long) minVtx * sharedSrcStride; - expectedSrcOffset = relOffset + attribBytes; - } else if (d.readBuffer() != sharedSrcBuffer || d.effectiveStride() != sharedSrcStride || relOffset != expectedSrcOffset) { - tightSrc = false; - } else { - expectedSrcOffset += attribBytes; - } - } - tightStride += attribBytes; k++; } + final AttribLayoutKey layoutKey = new AttribLayoutKey(locations, sizes, types, normalized); + final int tightStride = layoutKey.stride(); + + ByteBuffer sharedSrcBuffer = null; + long sharedSrcBase = 0; + int sharedSrcStride = -1; + boolean tightSrc = true; + for (int a = 0; a < enabledCount; a++) { + final AttribSnapshot.AttribDesc d = snap.get(locations[a]); + final int relOffset = (int) (d.offset() - d.readBufferBaseOffset()); + if (a == 0) { + sharedSrcBuffer = d.readBuffer(); + sharedSrcStride = d.effectiveStride(); + sharedSrcBase = MemoryUtilities.memAddress0(sharedSrcBuffer) + relOffset + (long) minVtx * sharedSrcStride; + if (relOffset != layoutKey.offset(0)) tightSrc = false; + } else if (d.readBuffer() != sharedSrcBuffer || d.effectiveStride() != sharedSrcStride || relOffset != layoutKey.offset(a)) { + tightSrc = false; + } + } if (tightSrc && sharedSrcStride != tightStride) tightSrc = false; vertexData = MemoryUtilities.memAlloc(vertexCount * tightStride); @@ -157,15 +153,13 @@ public final class IndexedDrawCapture { if (tightSrc) { MemoryUtilities.memCopy(sharedSrcBase, dstAddr, (long) vertexCount * tightStride); } else { - int attribOutBase = 0; for (int a = 0; a < enabledCount; a++) { final AttribSnapshot.AttribDesc d = snap.get(locations[a]); final int attribBytes = d.size() * d.typeSizeBytes(); final int srcStride = d.effectiveStride(); final long srcStart = MemoryUtilities.memAddress0(d.readBuffer()) + (d.offset() - d.readBufferBaseOffset()) + (long) minVtx * srcStride; - final long dstStart = dstAddr + attribOutBase; + final long dstStart = dstAddr + layoutKey.offset(a); copyStrided(srcStart, srcStride, dstStart, tightStride, attribBytes, vertexCount); - attribOutBase += attribBytes; } } @@ -184,10 +178,9 @@ public final class IndexedDrawCapture { bakedDrawMode = mode; } - final AttribLayoutKey layoutKey = new AttribLayoutKey(locations, sizes, types, normalized); final BatchedIndexedDrawCmd placeholder = new BatchedIndexedDrawCmd(); - final IndexedDrawCapture out = new IndexedDrawCapture(layoutKey, bakedDrawMode, vertexCount, bakedIndexCount, vertexData, indexData, tightStride, placeholder); + final IndexedDrawCapture out = new IndexedDrawCapture(layoutKey, bakedDrawMode, vertexCount, bakedIndexCount, vertexData, indexData, placeholder); success = true; return out; } finally { diff --git a/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/recording/IndexedDrawReplayTest.java b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/recording/IndexedDrawReplayTest.java index 12af0678a..612df346f 100644 --- a/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/recording/IndexedDrawReplayTest.java +++ b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/recording/IndexedDrawReplayTest.java @@ -179,7 +179,7 @@ class IndexedDrawReplayTest extends DisplayListTestFixture { final ByteBuffer tinyIdx = MemoryUtilities.memAlloc(16); final IndexedDrawCapture huge = new IndexedDrawCapture( otherLayout, GL11.GL_TRIANGLES, - Integer.MAX_VALUE / 4, 1, tinyVtx, tinyIdx, 16, + Integer.MAX_VALUE / 4, 1, tinyVtx, tinyIdx, new BatchedIndexedDrawCmd()); final IndexedDrawBatchBuilder builder = new IndexedDrawBatchBuilder(); @@ -205,7 +205,7 @@ class IndexedDrawReplayTest extends DisplayListTestFixture { final ByteBuffer idx = MemoryUtilities.memAlloc(32); final IndexedDrawCapture cap = new IndexedDrawCapture( new AttribLayoutKey(new int[]{0}, new int[]{2}, new int[]{GL11.GL_FLOAT}, new boolean[]{false}), - GL11.GL_TRIANGLES, 4, 6, vtx, idx, 8, new BatchedIndexedDrawCmd()); + GL11.GL_TRIANGLES, 4, 6, vtx, idx, new BatchedIndexedDrawCmd()); rec.recordIndexedDrawCapture(cap); assertFalse(cap.isFreed()); diff --git a/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/recording/commands/AttribLayoutKeyTest.java b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/recording/commands/AttribLayoutKeyTest.java new file mode 100644 index 000000000..a92712b82 --- /dev/null +++ b/glsm/src/test/java/com/gtnewhorizons/angelica/glsm/recording/commands/AttribLayoutKeyTest.java @@ -0,0 +1,95 @@ +package com.gtnewhorizons.angelica.glsm.recording.commands; + +import org.junit.jupiter.api.Test; +import org.lwjgl.opengl.GL11; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +class AttribLayoutKeyTest { + + private static int[] offsetsOf(AttribLayoutKey k) { + final int[] out = new int[k.locations.length]; + for (int i = 0; i < out.length; i++) out[i] = k.offset(i); + return out; + } + + @Test + void standardPosColorUvNormal_isAlignedAndPacked() { + final AttribLayoutKey k = new AttribLayoutKey( + new int[]{0, 1, 2, 4}, + new int[]{3, 4, 2, 3}, + new int[]{GL11.GL_FLOAT, GL11.GL_UNSIGNED_BYTE, GL11.GL_FLOAT, GL11.GL_BYTE}, + new boolean[]{false, true, false, true}); + assertArrayEquals(new int[]{0, 12, 16, 24}, offsetsOf(k)); + assertEquals(28, k.stride()); + } + + @Test + void posUvNormal3_roundsTailStrideTo24() { + final AttribLayoutKey k = new AttribLayoutKey( + new int[]{0, 2, 4}, + new int[]{3, 2, 3}, + new int[]{GL11.GL_FLOAT, GL11.GL_FLOAT, GL11.GL_BYTE}, + new boolean[]{false, false, true}); + assertArrayEquals(new int[]{0, 12, 20}, offsetsOf(k)); + assertEquals(24, k.stride()); + } + + @Test + void interiorHazardOrder_roundsMidAttribOffset() { + final AttribLayoutKey k = new AttribLayoutKey( + new int[]{0, 4, 2}, + new int[]{3, 3, 2}, + new int[]{GL11.GL_FLOAT, GL11.GL_BYTE, GL11.GL_FLOAT}, + new boolean[]{false, true, false}); + assertArrayEquals(new int[]{0, 12, 16}, offsetsOf(k)); + assertEquals(24, k.stride()); + } + + @Test + void allByteLayout_packsTight() { + final AttribLayoutKey k = new AttribLayoutKey( + new int[]{0, 1, 2}, + new int[]{3, 4, 2}, + new int[]{GL11.GL_BYTE, GL11.GL_UNSIGNED_BYTE, GL11.GL_BYTE}, + new boolean[]{true, true, false}); + assertArrayEquals(new int[]{0, 3, 7}, offsetsOf(k)); + assertEquals(9, k.stride()); + } + + @Test + void shortsFollowingByte_roundToEvenAndStrideToEven() { + final AttribLayoutKey k = new AttribLayoutKey( + new int[]{0, 1}, + new int[]{1, 2}, + new int[]{GL11.GL_BYTE, GL11.GL_SHORT}, + new boolean[]{true, false}); + assertArrayEquals(new int[]{0, 2}, offsetsOf(k)); + assertEquals(6, k.stride()); + } + + @Test + void equalInputs_produceEqualKeys() { + final AttribLayoutKey a = new AttribLayoutKey( + new int[]{0, 2}, new int[]{3, 2}, + new int[]{GL11.GL_FLOAT, GL11.GL_FLOAT}, new boolean[]{false, false}); + final AttribLayoutKey b = new AttribLayoutKey( + new int[]{0, 2}, new int[]{3, 2}, + new int[]{GL11.GL_FLOAT, GL11.GL_FLOAT}, new boolean[]{false, false}); + assertEquals(a, b); + assertEquals(a.hashCode(), b.hashCode()); + assertEquals(a.stride(), b.stride()); + assertArrayEquals(offsetsOf(a), offsetsOf(b)); + } + + @Test + void differentNormalized_producesDifferentKeys() { + final AttribLayoutKey a = new AttribLayoutKey( + new int[]{0}, new int[]{4}, new int[]{GL11.GL_UNSIGNED_BYTE}, new boolean[]{true}); + final AttribLayoutKey b = new AttribLayoutKey( + new int[]{0}, new int[]{4}, new int[]{GL11.GL_UNSIGNED_BYTE}, new boolean[]{false}); + assertNotEquals(a, b); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/TileEntityRenderBoundsRegistry.java b/src/main/java/com/gtnewhorizons/angelica/rendering/TileEntityRenderBoundsRegistry.java index c2cabf6d2..bb401ebd7 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/TileEntityRenderBoundsRegistry.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/TileEntityRenderBoundsRegistry.java @@ -34,11 +34,13 @@ public final class TileEntityRenderBoundsRegistry { } private static boolean probeAndCache(TileEntity te, Class clazz) { - boolean isInfinite = false; + boolean isInfinite; try { final AxisAlignedBB aabb = te.getRenderBoundingBox(); isInfinite = isInfiniteExtentsBox(aabb); - } catch (Exception ignored) {} + } catch (Throwable t) { + isInfinite = true; + } synchronized (classRegistry) { classRegistry.put(clazz, isInfinite ? INFINITE : FINITE); diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaChunkBuilderMeshingTask.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaChunkBuilderMeshingTask.java index 5cdeb740d..48b0fa46f 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaChunkBuilderMeshingTask.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaChunkBuilderMeshingTask.java @@ -144,13 +144,18 @@ public abstract class AngelicaChunkBuilderMeshingTask extends ChunkBuilderTask secMinX + 16 || aabb.maxY > secMinY + 16 || aabb.maxZ > secMinZ + 16; } else { - isGlobal = false; + isGlobal = true; } } (isGlobal ? renderData.globalBlockEntities : renderData.culledBlockEntities).add(tileEntity); diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/rendering/MixinTileEntity.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/rendering/MixinTileEntity.java index 1bf3cd0e3..0998edb79 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/rendering/MixinTileEntity.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/rendering/MixinTileEntity.java @@ -9,6 +9,7 @@ import net.minecraft.init.Blocks; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; @@ -23,7 +24,7 @@ public abstract class MixinTileEntity implements ITileEntityBoundingBoxCache { @Shadow public int yCoord; @Shadow public int zCoord; - @Shadow(remap = false) public static final AxisAlignedBB INFINITE_EXTENT_AABB = null; + @Shadow(remap = false) @Final public static AxisAlignedBB INFINITE_EXTENT_AABB; /** * @author mitchej123