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
3 changed files with 39 additions and 29 deletions
Showing only changes of commit 8864436962 - Show all commits

No files matched your search

@@ -15,14 +15,14 @@ public record DirectMultiDrawEmitter(MultiDrawBatch batch) implements MultiDrawE
@Override @Override
@SuppressWarnings("IntegerMultiplicationImplicitCastToLong") @SuppressWarnings("IntegerMultiplicationImplicitCastToLong")
public void addDrawCommand(int vertexOffset, int elementCount, int indexOffset) { public void addDrawCommand(int vertexOffset, int elementCount, int indexOffset, int commandCount) {
var batch = this.batch; var batch = this.batch;
int size = batch.size; int size = batch.size;
LWJGL.memPutInt(batch.pBaseVertex + (size << 2), vertexOffset); LWJGL.memPutInt(batch.pBaseVertex + (size << 2), vertexOffset);
LWJGL.memPutInt(batch.pElementCount + (size << 2), elementCount); LWJGL.memPutInt(batch.pElementCount + (size << 2), elementCount);
LWJGL.memPutAddress(batch.pElementPointer + (size << 3), indexOffset); LWJGL.memPutAddress(batch.pElementPointer + (size << 3), indexOffset);
batch.size++; batch.size += commandCount;
} }
@Override @Override
@@ -49,12 +49,14 @@ public class IndirectMultiDrawEmitter implements MultiDrawEmitter {
} }
@Override @Override
public void addDrawCommand(int vertexOffset, int elementCount, int indexOffset) { public void addDrawCommand(int vertexOffset, int elementCount, int indexOffset, int commandCount) {
long ptr = this.indirectBuffer + (long)this.numCommands * COMMAND_SIZE; long ptr = this.indirectBuffer + (long)this.numCommands * COMMAND_SIZE;
LWJGL.memPutInt(ptr + 0L, elementCount); LWJGL.memPutInt(ptr + 0L, elementCount);
LWJGL.memPutInt(ptr + 8L, indexOffset / 4); LWJGL.memPutInt(ptr + 8L, indexOffset / 4);
LWJGL.memPutInt(ptr + 12L, vertexOffset); LWJGL.memPutInt(ptr + 12L, vertexOffset);
this.numCommands++;
// A zero count leaves the speculative command slot open to be overwritten by the next candidate.
this.numCommands += commandCount;
} }
@Override @Override
@@ -13,50 +13,58 @@ public interface MultiDrawEmitter {
default void addDrawCommands(long pMeshData, int facingMask, boolean sorted) { default void addDrawCommands(long pMeshData, int facingMask, boolean sorted) {
if (sorted) { if (sorted) {
for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) { for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) {
if ((facingMask & (1 << facing)) != 0) {
this.addDrawCommand( this.addDrawCommand(
SectionRenderDataUnsafe.getVertexOffset(pMeshData, facing), SectionRenderDataUnsafe.getVertexOffset(pMeshData, facing),
SectionRenderDataUnsafe.getElementCount(pMeshData, facing), SectionRenderDataUnsafe.getElementCount(pMeshData, facing),
SectionRenderDataUnsafe.getIndexOffset(pMeshData, facing) SectionRenderDataUnsafe.getIndexOffset(pMeshData, facing),
(facingMask >> facing) & 1
); );
} }
}
return; return;
} }
int vertexOffset = 0; int vertexOffset = 0;
int elementCount = 0; int elementCount = 0;
int runActive = 0;
// the slice mask contains every non-empty facing, so removing visible bits leaves culled non-empty facings
int culledMask = SectionRenderDataUnsafe.getSliceMask(pMeshData) & ~facingMask;
for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) { for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) {
// each bit represents one directional mesh group - extract this group's visibility as 0 or 1
int visible = (facingMask >> facing) & 1;
// we should finish only when an active run reaches a culled non-empty facing
int finishRun = runActive & ((culledMask >> facing) & 1);
this.addDrawCommand(vertexOffset, elementCount, 0, finishRun);
// (finishRun - 1) is -1 when continuing and 0 when finishing, preserving or clearing the count
elementCount &= finishRun - 1;
// finishRun can only be set for an active run, so xor closes that run
runActive ^= finishRun;
int currentVertexOffset = SectionRenderDataUnsafe.getVertexOffset(pMeshData, facing);
int currentElementCount = SectionRenderDataUnsafe.getElementCount(pMeshData, facing); int currentElementCount = SectionRenderDataUnsafe.getElementCount(pMeshData, facing);
if (currentElementCount == 0) { // start a run only when this facing is visible and no run remains active
continue; int startRun = visible & (runActive ^ 1);
// negating startRun expands 0/1 to a zero/all-ones mask, selecting the old or current offset
vertexOffset ^= (vertexOffset ^ currentVertexOffset) & -startRun;
// multiplying by visible conditionally includes this facing's elements
elementCount += currentElementCount * visible;
// a visible facing opens or continues the run
runActive |= visible;
} }
if ((facingMask & (1 << facing)) == 0) { this.addDrawCommand(vertexOffset, elementCount, 0, runActive);
if (elementCount != 0) {
this.addDrawCommand(vertexOffset, elementCount, 0);
elementCount = 0;
} }
continue; void addDrawCommand(int vertexOffset, int elementCount, int indexOffset, int commandCount);
}
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); void executeBatch(CommandList commandList, GlTessellation tessellation, GlPrimitiveType primitiveType);
boolean isEmpty(); boolean isEmpty();
int getIndexBufferSize(); int getIndexBufferSize();