merge adjacent visible unsorted terrain draw commands #31

Closed
rdh wants to merge 2 commits from rdh/celeritas:merge-unsorted-draws into stonecutter
pull from: rdh/celeritas:merge-unsorted-draws
4 changed files with 59 additions and 37 deletions
Showing only changes of commit c8db29e29e - Show all commits

No files matched your search

@@ -146,8 +146,6 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
int originY = renderRegion.getChunkY();
int originZ = renderRegion.getChunkZ();
int indexPointerMask = pass.isSorted() ? 0xFFFFFFFF : 0;
while (iterator.hasNext()) {
int sectionIndex = iterator.nextByteAsInt();
@@ -168,7 +166,7 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
slices &= SectionRenderDataUnsafe.getSliceMask(pMeshData);
if (slices != 0) {
emitter.addDrawCommands(pMeshData, slices, indexPointerMask);
emitter.addDrawCommands(pMeshData, slices, pass.isSorted());
}
}
}
@@ -6,8 +6,6 @@ import org.embeddedt.embeddium.impl.gl.device.MultiDrawBatch;
import org.embeddedt.embeddium.impl.gl.tessellation.GlIndexType;
import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType;
import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation;
import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing;
import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataUnsafe;
import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL;
public record DirectMultiDrawEmitter(MultiDrawBatch batch) implements MultiDrawEmitter {
@@ -17,23 +15,14 @@ public record DirectMultiDrawEmitter(MultiDrawBatch batch) implements MultiDrawE
@Override
@SuppressWarnings("IntegerMultiplicationImplicitCastToLong")
public void addDrawCommands(long pMeshData, int mask, int indexPointerMask) {
public void addDrawCommand(int vertexOffset, int elementCount, int indexOffset) {
var batch = this.batch;
final var pBaseVertex = batch.pBaseVertex;
final var pElementCount = batch.pElementCount;
final var pElementPointer = batch.pElementPointer;
int size = batch.size;
for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) {
LWJGL.memPutInt(pBaseVertex + (size << 2), SectionRenderDataUnsafe.getVertexOffset(pMeshData, facing));
LWJGL.memPutInt(pElementCount + (size << 2), SectionRenderDataUnsafe.getElementCount(pMeshData, facing));
LWJGL.memPutAddress(pElementPointer + (size << 3), SectionRenderDataUnsafe.getIndexOffset(pMeshData, facing) & indexPointerMask);
size += (mask >> facing) & 1;
}
batch.size = size;
LWJGL.memPutInt(batch.pBaseVertex + (size << 2), vertexOffset);
LWJGL.memPutInt(batch.pElementCount + (size << 2), elementCount);
LWJGL.memPutAddress(batch.pElementPointer + (size << 3), indexOffset);
batch.size++;
}
@Override
@@ -8,8 +8,6 @@ import org.embeddedt.embeddium.impl.gl.device.DrawCommandList;
import org.embeddedt.embeddium.impl.gl.tessellation.GlIndexType;
import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType;
import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation;
import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing;
import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataUnsafe;
import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL;
import org.taumc.celeritas.lwjgl.LWJGLServiceProvider;
@@ -51,22 +49,12 @@ public class IndirectMultiDrawEmitter implements MultiDrawEmitter {
}
@Override
public void addDrawCommands(long pMeshData, int facingMask, int indexPointerMask) {
int size = this.numCommands;
long basePtr = this.indirectBuffer;
for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) {
long ptr = basePtr + (long)size * COMMAND_SIZE;
LWJGL.memPutInt(ptr + 0L, SectionRenderDataUnsafe.getElementCount(pMeshData, facing)); // count
int indexOffset = SectionRenderDataUnsafe.getIndexOffset(pMeshData, facing) & indexPointerMask;
public void addDrawCommand(int vertexOffset, int elementCount, int indexOffset) {
long ptr = this.indirectBuffer + (long)this.numCommands * COMMAND_SIZE;
LWJGL.memPutInt(ptr + 0L, elementCount);
LWJGL.memPutInt(ptr + 8L, indexOffset / 4);
LWJGL.memPutInt(ptr + 12L, SectionRenderDataUnsafe.getVertexOffset(pMeshData, facing)); // baseVertex
size += (facingMask >> facing) & 1;
}
this.numCommands = size;
LWJGL.memPutInt(ptr + 12L, vertexOffset);
this.numCommands++;
}
@Override
@@ -4,12 +4,59 @@ import org.embeddedt.embeddium.impl.gl.device.CommandList;
import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType;
import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation;
import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing;
import org.embeddedt.embeddium.impl.render.chunk.data.SectionRenderDataUnsafe;
import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion;
public interface MultiDrawEmitter {
int MAX_COMMAND_COUNT = (ModelQuadFacing.COUNT * RenderRegion.REGION_SIZE) + 1;
void addDrawCommands(long pMeshData, int facingMask, int indexPointerMask);
default void addDrawCommands(long pMeshData, int facingMask, boolean sorted) {
if (sorted) {
for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) {
if ((facingMask & (1 << facing)) != 0) {
this.addDrawCommand(
SectionRenderDataUnsafe.getVertexOffset(pMeshData, facing),
SectionRenderDataUnsafe.getElementCount(pMeshData, facing),
SectionRenderDataUnsafe.getIndexOffset(pMeshData, facing)
);
}
}
return;
}
int vertexOffset = 0;
int elementCount = 0;
for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) {
int currentElementCount = SectionRenderDataUnsafe.getElementCount(pMeshData, facing);
if (currentElementCount == 0) {
continue;
}
if ((facingMask & (1 << facing)) == 0) {
if (elementCount != 0) {
this.addDrawCommand(vertexOffset, elementCount, 0);
elementCount = 0;
}
continue;
}
if (elementCount == 0) {
vertexOffset = SectionRenderDataUnsafe.getVertexOffset(pMeshData, facing);
}
elementCount += currentElementCount;
}
if (elementCount != 0) {
this.addDrawCommand(vertexOffset, elementCount, 0);
}
}
void addDrawCommand(int vertexOffset, int elementCount, int indexOffset);
void executeBatch(CommandList commandList, GlTessellation tessellation, GlPrimitiveType primitiveType);
boolean isEmpty();
int getIndexBufferSize();