Aggressively optimize multidraw command emission #32

Merged
embeddedt merged 5 commits from command-emit-opts into stonecutter 2026-08-11 18:46:23 -04:00
3 changed files with 69 additions and 55 deletions
Showing only changes of commit 52d5989258 - Show all commits

No files matched your search

@@ -54,13 +54,10 @@ public class SectionRenderDataStorage {
this.indexAllocations[localSectionIndex] = indexAllocation; this.indexAllocations[localSectionIndex] = indexAllocation;
this.numAllocations++; this.numAllocations++;
var pMeshData = this.getDataPointer(localSectionIndex);
int vertexOffset = allocation.getOffset(); int vertexOffset = allocation.getOffset();
int indexOffset = indexAllocation != null ? indexAllocation.getOffset() * 4 : 0; int indexOffset = indexAllocation != null ? indexAllocation.getOffset() * 4 : 0;
int sliceMask = this.storageStrategy.writeMeshes(pMeshData, vertexOffset, indexOffset, ranges, this.primitiveType); this.storageStrategy.writeMeshesAndSliceMask(this.pMeshDataArray, localSectionIndex, vertexOffset, indexOffset, ranges, this.primitiveType);
this.storageStrategy.setSliceMask(this.pMeshDataArray, localSectionIndex, sliceMask);
} }
public void removeMeshes(int localSectionIndex) { public void removeMeshes(int localSectionIndex) {
@@ -68,8 +65,7 @@ public class SectionRenderDataStorage {
this.allocations[localSectionIndex].delete(); this.allocations[localSectionIndex].delete();
this.allocations[localSectionIndex] = null; this.allocations[localSectionIndex] = null;
this.storageStrategy.setSliceMask(this.pMeshDataArray, localSectionIndex, 0); this.storageStrategy.clearRow(this.pMeshDataArray, localSectionIndex);
this.storageStrategy.clear(this.getDataPointer(localSectionIndex));
this.numAllocations--; this.numAllocations--;
} }
@@ -130,8 +126,8 @@ public class SectionRenderDataStorage {
* statically, so that they can hoist the base and stride and index the array themselves rather than dispatching * statically, so that they can hoist the base and stride and index the array themselves rather than dispatching
* through {@link #getDataPointer} once per section. * through {@link #getDataPointer} once per section.
*/ */
public long getBasePointer() { public long getRowBasePointer() {
return this.storageStrategy.getBasePointer(this.pMeshDataArray); return this.storageStrategy.getRowBasePointer(this.pMeshDataArray);
} }
public int getSliceMask(int sectionIndex) { public int getSliceMask(int sectionIndex) {
@@ -24,16 +24,6 @@ import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL;
public class SectionRenderDataUnsafe { public class SectionRenderDataUnsafe {
private static final int NUM_FACINGS = ModelQuadFacing.COUNT; // 6 directions + UNASSIGNED private static final int NUM_FACINGS = ModelQuadFacing.COUNT; // 6 directions + UNASSIGNED
private static final long OFFSET_SLICE_MASK = 0;
private static int getSliceMask(long ptr) {
return LWJGL.memGetInt(ptr + OFFSET_SLICE_MASK);
}
private static void setSliceMask(long ptr, int value) {
LWJGL.memPutInt(ptr + OFFSET_SLICE_MASK, value);
}
/** /**
* {@return the number of index buffer elements needed to draw a span of {@code vertexSpan} vertices} * {@return the number of index buffer elements needed to draw a span of {@code vertexSpan} vertices}
*/ */
@@ -108,7 +98,17 @@ public class SectionRenderDataUnsafe {
} }
@Override @Override
public int writeMeshes(long ptr, int vertexOffset, int indexOffset, public int getSliceMask(long heap, int index) {
return LWJGL.memGetInt(this.heapPointer(heap, index));
}
@Override
protected void setSliceMask(long heap, int index, int value) {
LWJGL.memPutInt(this.heapPointer(heap, index), value);
}
@Override
protected int writeMeshes(long ptr, int vertexOffset, int indexOffset,
Map<ModelQuadFacing, VertexRange> ranges, ChunkPrimitiveType primitiveType) { Map<ModelQuadFacing, VertexRange> ranges, ChunkPrimitiveType primitiveType) {
int sliceMask = 0; int sliceMask = 0;
@@ -175,8 +175,8 @@ public class SectionRenderDataUnsafe {
} }
@Override @Override
public long getBasePointer(long heap) { protected long getHeaderSize() {
return heap + DATA_OFFSET; return DATA_OFFSET;
} }
@Override @Override
@@ -185,7 +185,7 @@ public class SectionRenderDataUnsafe {
} }
@Override @Override
public void setSliceMask(long heap, int index, int value) { protected void setSliceMask(long heap, int index, int value) {
LWJGL.memPutByte(heap + index, (byte) value); LWJGL.memPutByte(heap + index, (byte) value);
} }
@@ -214,7 +214,7 @@ public class SectionRenderDataUnsafe {
} }
@Override @Override
public int writeMeshes(long ptr, int vertexOffset, int indexOffset, protected int writeMeshes(long ptr, int vertexOffset, int indexOffset,
Map<ModelQuadFacing, VertexRange> ranges, ChunkPrimitiveType primitiveType) { Map<ModelQuadFacing, VertexRange> ranges, ChunkPrimitiveType primitiveType) {
int sliceMask = 0; int sliceMask = 0;
@@ -254,22 +254,6 @@ public class SectionRenderDataUnsafe {
} }
} }
@Override
public long allocateHeap(int count) {
long size = DATA_OFFSET + (count * this.getStride());
long pointer = LWJGL.nmemAlignedAlloc(64, size);
if (pointer != 0) {
LWJGL.memSet(pointer, 0, size);
}
return pointer;
}
@Override
public void freeHeap(long pointer) {
LWJGL.nmemAlignedFree(pointer);
}
}; };
public abstract long getStride(); public abstract long getStride();
@@ -303,7 +287,7 @@ public class SectionRenderDataUnsafe {
* @param indexOffset the section's base offset into its index buffer, in bytes; layouts which draw from the * @param indexOffset the section's base offset into its index buffer, in bytes; layouts which draw from the
* shared index buffer ignore this * shared index buffer ignore this
*/ */
public abstract int writeMeshes(long ptr, int vertexOffset, int indexOffset, protected abstract int writeMeshes(long ptr, int vertexOffset, int indexOffset,
Map<ModelQuadFacing, VertexRange> ranges, ChunkPrimitiveType primitiveType); Map<ModelQuadFacing, VertexRange> ranges, ChunkPrimitiveType primitiveType);
/** /**
@@ -319,24 +303,34 @@ public class SectionRenderDataUnsafe {
*/ */
public abstract void rebase(long ptr, int vertexOffset, int indexOffset, ChunkPrimitiveType primitiveType); public abstract void rebase(long ptr, int vertexOffset, int indexOffset, ChunkPrimitiveType primitiveType);
public int getSliceMask(long heap, int index) { public abstract int getSliceMask(long heap, int index);
return SectionRenderDataUnsafe.getSliceMask(this.heapPointer(heap, index));
protected abstract void setSliceMask(long heap, int index, int value);
/**
* {@return the number of leading bytes reserved before the row array, or zero if rows start at the heap base}
*/
protected long getHeaderSize() {
return 0;
} }
public void setSliceMask(long heap, int index, int value) { public final long getRowBasePointer(long heap) {
SectionRenderDataUnsafe.setSliceMask(this.heapPointer(heap, index), value); return heap + this.getHeaderSize();
} }
public long getBasePointer(long heap) { public final long allocateHeap(int count) {
return heap; long size = this.getHeaderSize() + (count * this.getStride());
long pointer = LWJGL.nmemAlignedAlloc(64, size);
if (pointer != 0) {
LWJGL.memSet(pointer, 0, size);
} }
public long allocateHeap(int count) { return pointer;
return LWJGL.nmemCalloc(count, this.getStride());
} }
public void freeHeap(long pointer) { public final void freeHeap(long pointer) {
LWJGL.nmemFree(pointer); LWJGL.nmemAlignedFree(pointer);
} }
public final void clear(long pointer) { public final void clear(long pointer) {
@@ -344,7 +338,31 @@ public class SectionRenderDataUnsafe {
} }
public final long heapPointer(long ptr, int index) { public final long heapPointer(long ptr, int index) {
return this.getBasePointer(ptr) + (index * this.getStride()); return this.getRowBasePointer(ptr) + (index * this.getStride());
}
/**
* Populates section {@code index}'s row from a freshly uploaded mesh and records its slice mask, keeping the
* two in sync so callers can't forget the latter. {@code ranges} must be contiguous in facing order starting
* at zero, as produced by the chunk build buffers.
*
* @param vertexOffset the section's base offset into the vertex arena, in vertices
* @param indexOffset the section's base offset into its index buffer, in bytes; layouts which draw from the
* shared index buffer ignore this
*/
public final void writeMeshesAndSliceMask(long heap, int index, int vertexOffset, int indexOffset,
Map<ModelQuadFacing, VertexRange> ranges, ChunkPrimitiveType primitiveType) {
int sliceMask = this.writeMeshes(this.heapPointer(heap, index), vertexOffset, indexOffset, ranges, primitiveType);
this.setSliceMask(heap, index, sliceMask);
}
/**
* Clears section {@code index}'s row and resets its slice mask to zero, keeping the two in sync.
*/
public final void clearRow(long heap, int index) {
this.setSliceMask(heap, index, 0);
this.clear(this.heapPointer(heap, index));
} }
private static int vertexCountOf(Map<ModelQuadFacing, VertexRange> ranges, int facing) { private static int vertexCountOf(Map<ModelQuadFacing, VertexRange> ranges, int facing) {
@@ -245,7 +245,7 @@ public final class BatchAssembler {
byte[] sections, int sectionCount, boolean reverse, byte[] sections, int sectionCount, boolean reverse,
ChunkPrimitiveType primitiveType, ChunkPrimitiveType primitiveType,
int firstFacing, int lastFacing) { int firstFacing, int lastFacing) {
final long pBase = storage.getBasePointer(); final long pBase = storage.getRowBasePointer();
final long stride = SectionRenderDataUnsafe.Strategy.COMPACT.getStride(); final long stride = SectionRenderDataUnsafe.Strategy.COMPACT.getStride();
int cursor = reverse ? sectionCount - 1 : 0; int cursor = reverse ? sectionCount - 1 : 0;
@@ -269,7 +269,7 @@ public final class BatchAssembler {
byte[] sections, int sectionCount, boolean reverse, byte[] sections, int sectionCount, boolean reverse,
ChunkPrimitiveType primitiveType, ChunkPrimitiveType primitiveType,
long runs, int runCount) { long runs, int runCount) {
final long pBase = storage.getBasePointer(); final long pBase = storage.getRowBasePointer();
final long stride = SectionRenderDataUnsafe.Strategy.COMPACT.getStride(); final long stride = SectionRenderDataUnsafe.Strategy.COMPACT.getStride();
int cursor = reverse ? sectionCount - 1 : 0; int cursor = reverse ? sectionCount - 1 : 0;
@@ -302,7 +302,7 @@ public final class BatchAssembler {
byte[] sections, int sectionCount, boolean reverse, byte[] sections, int sectionCount, boolean reverse,
ChunkPrimitiveType primitiveType, ChunkPrimitiveType primitiveType,
CameraTransform camera) { CameraTransform camera) {
final long pBase = storage.getBasePointer(); final long pBase = storage.getRowBasePointer();
final long stride = SectionRenderDataUnsafe.Strategy.COMPACT.getStride(); final long stride = SectionRenderDataUnsafe.Strategy.COMPACT.getStride();
final int originX = region.getChunkX(); final int originX = region.getChunkX();
@@ -350,7 +350,7 @@ public final class BatchAssembler {
SectionRenderDataStorage storage, SectionRenderDataStorage storage,
byte[] sections, int sectionCount, boolean reverse, byte[] sections, int sectionCount, boolean reverse,
ChunkPrimitiveType primitiveType) { ChunkPrimitiveType primitiveType) {
final long pBase = storage.getBasePointer(); final long pBase = storage.getRowBasePointer();
final long stride = SectionRenderDataUnsafe.Strategy.FULL.getStride(); final long stride = SectionRenderDataUnsafe.Strategy.FULL.getStride();
int cursor = reverse ? sectionCount - 1 : 0; int cursor = reverse ? sectionCount - 1 : 0;