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 74 additions and 60 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.numAllocations++;
var pMeshData = this.getDataPointer(localSectionIndex);
int vertexOffset = allocation.getOffset();
int indexOffset = indexAllocation != null ? indexAllocation.getOffset() * 4 : 0;
int sliceMask = this.storageStrategy.writeMeshes(pMeshData, vertexOffset, indexOffset, ranges, this.primitiveType);
this.storageStrategy.setSliceMask(this.pMeshDataArray, localSectionIndex, sliceMask);
this.storageStrategy.writeMeshesAndSliceMask(this.pMeshDataArray, localSectionIndex, vertexOffset, indexOffset, ranges, this.primitiveType);
}
public void removeMeshes(int localSectionIndex) {
@@ -68,8 +65,7 @@ public class SectionRenderDataStorage {
this.allocations[localSectionIndex].delete();
this.allocations[localSectionIndex] = null;
this.storageStrategy.setSliceMask(this.pMeshDataArray, localSectionIndex, 0);
this.storageStrategy.clear(this.getDataPointer(localSectionIndex));
this.storageStrategy.clearRow(this.pMeshDataArray, localSectionIndex);
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
* through {@link #getDataPointer} once per section.
*/
public long getBasePointer() {
return this.storageStrategy.getBasePointer(this.pMeshDataArray);
public long getRowBasePointer() {
return this.storageStrategy.getRowBasePointer(this.pMeshDataArray);
}
public int getSliceMask(int sectionIndex) {
@@ -24,16 +24,6 @@ import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL;
public class SectionRenderDataUnsafe {
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}
*/
@@ -108,8 +98,18 @@ public class SectionRenderDataUnsafe {
}
@Override
public int writeMeshes(long ptr, int vertexOffset, int indexOffset,
Map<ModelQuadFacing, VertexRange> ranges, ChunkPrimitiveType primitiveType) {
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) {
int sliceMask = 0;
for (int facing = 0; facing < NUM_FACINGS; facing++) {
@@ -175,8 +175,8 @@ public class SectionRenderDataUnsafe {
}
@Override
public long getBasePointer(long heap) {
return heap + DATA_OFFSET;
protected long getHeaderSize() {
return DATA_OFFSET;
}
@Override
@@ -185,7 +185,7 @@ public class SectionRenderDataUnsafe {
}
@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);
}
@@ -214,8 +214,8 @@ public class SectionRenderDataUnsafe {
}
@Override
public int writeMeshes(long ptr, int vertexOffset, int indexOffset,
Map<ModelQuadFacing, VertexRange> ranges, ChunkPrimitiveType primitiveType) {
protected int writeMeshes(long ptr, int vertexOffset, int indexOffset,
Map<ModelQuadFacing, VertexRange> ranges, ChunkPrimitiveType primitiveType) {
int sliceMask = 0;
for (int facing = 0; facing < NUM_FACINGS; facing++) {
@@ -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();
@@ -303,8 +287,8 @@ public class SectionRenderDataUnsafe {
* @param indexOffset the section's base offset into its index buffer, in bytes; layouts which draw from the
* shared index buffer ignore this
*/
public abstract int writeMeshes(long ptr, int vertexOffset, int indexOffset,
Map<ModelQuadFacing, VertexRange> ranges, ChunkPrimitiveType primitiveType);
protected abstract int writeMeshes(long ptr, int vertexOffset, int indexOffset,
Map<ModelQuadFacing, VertexRange> ranges, ChunkPrimitiveType primitiveType);
/**
* Rewrites every facing's index offset against a newly allocated index buffer, preserving element counts.
@@ -319,24 +303,34 @@ public class SectionRenderDataUnsafe {
*/
public abstract void rebase(long ptr, int vertexOffset, int indexOffset, ChunkPrimitiveType primitiveType);
public int getSliceMask(long heap, int index) {
return SectionRenderDataUnsafe.getSliceMask(this.heapPointer(heap, index));
public abstract int getSliceMask(long heap, int 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) {
SectionRenderDataUnsafe.setSliceMask(this.heapPointer(heap, index), value);
public final long getRowBasePointer(long heap) {
return heap + this.getHeaderSize();
}
public long getBasePointer(long heap) {
return heap;
public final long allocateHeap(int count) {
long size = this.getHeaderSize() + (count * this.getStride());
long pointer = LWJGL.nmemAlignedAlloc(64, size);
if (pointer != 0) {
LWJGL.memSet(pointer, 0, size);
}
return pointer;
}
public long allocateHeap(int count) {
return LWJGL.nmemCalloc(count, this.getStride());
}
public void freeHeap(long pointer) {
LWJGL.nmemFree(pointer);
public final void freeHeap(long pointer) {
LWJGL.nmemAlignedFree(pointer);
}
public final void clear(long pointer) {
@@ -344,7 +338,31 @@ public class SectionRenderDataUnsafe {
}
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) {
@@ -245,7 +245,7 @@ public final class BatchAssembler {
byte[] sections, int sectionCount, boolean reverse,
ChunkPrimitiveType primitiveType,
int firstFacing, int lastFacing) {
final long pBase = storage.getBasePointer();
final long pBase = storage.getRowBasePointer();
final long stride = SectionRenderDataUnsafe.Strategy.COMPACT.getStride();
int cursor = reverse ? sectionCount - 1 : 0;
@@ -269,7 +269,7 @@ public final class BatchAssembler {
byte[] sections, int sectionCount, boolean reverse,
ChunkPrimitiveType primitiveType,
long runs, int runCount) {
final long pBase = storage.getBasePointer();
final long pBase = storage.getRowBasePointer();
final long stride = SectionRenderDataUnsafe.Strategy.COMPACT.getStride();
int cursor = reverse ? sectionCount - 1 : 0;
@@ -302,7 +302,7 @@ public final class BatchAssembler {
byte[] sections, int sectionCount, boolean reverse,
ChunkPrimitiveType primitiveType,
CameraTransform camera) {
final long pBase = storage.getBasePointer();
final long pBase = storage.getRowBasePointer();
final long stride = SectionRenderDataUnsafe.Strategy.COMPACT.getStride();
final int originX = region.getChunkX();
@@ -350,7 +350,7 @@ public final class BatchAssembler {
SectionRenderDataStorage storage,
byte[] sections, int sectionCount, boolean reverse,
ChunkPrimitiveType primitiveType) {
final long pBase = storage.getBasePointer();
final long pBase = storage.getRowBasePointer();
final long stride = SectionRenderDataUnsafe.Strategy.FULL.getStride();
int cursor = reverse ? sectionCount - 1 : 0;