merge adjacent visible unsorted terrain draw commands #31
No files matched your search
+1
-3
@@ -146,8 +146,6 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
|
|||||||
int originY = renderRegion.getChunkY();
|
int originY = renderRegion.getChunkY();
|
||||||
int originZ = renderRegion.getChunkZ();
|
int originZ = renderRegion.getChunkZ();
|
||||||
|
|
||||||
int indexPointerMask = pass.isSorted() ? 0xFFFFFFFF : 0;
|
|
||||||
|
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
int sectionIndex = iterator.nextByteAsInt();
|
int sectionIndex = iterator.nextByteAsInt();
|
||||||
|
|
||||||
@@ -168,7 +166,7 @@ public abstract class DefaultChunkRenderer extends ShaderChunkRenderer {
|
|||||||
slices &= SectionRenderDataUnsafe.getSliceMask(pMeshData);
|
slices &= SectionRenderDataUnsafe.getSliceMask(pMeshData);
|
||||||
|
|
||||||
if (slices != 0) {
|
if (slices != 0) {
|
||||||
emitter.addDrawCommands(pMeshData, slices, indexPointerMask);
|
emitter.addDrawCommands(pMeshData, slices, pass.isSorted());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-16
@@ -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.GlIndexType;
|
||||||
import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType;
|
import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType;
|
||||||
import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation;
|
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 static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL;
|
||||||
|
|
||||||
public record DirectMultiDrawEmitter(MultiDrawBatch batch) implements MultiDrawEmitter {
|
public record DirectMultiDrawEmitter(MultiDrawBatch batch) implements MultiDrawEmitter {
|
||||||
@@ -17,23 +15,14 @@ public record DirectMultiDrawEmitter(MultiDrawBatch batch) implements MultiDrawE
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("IntegerMultiplicationImplicitCastToLong")
|
@SuppressWarnings("IntegerMultiplicationImplicitCastToLong")
|
||||||
public void addDrawCommands(long pMeshData, int mask, int indexPointerMask) {
|
public void addDrawCommand(int vertexOffset, int elementCount, int indexOffset, int commandCount) {
|
||||||
var batch = this.batch;
|
var batch = this.batch;
|
||||||
final var pBaseVertex = batch.pBaseVertex;
|
|
||||||
final var pElementCount = batch.pElementCount;
|
|
||||||
final var pElementPointer = batch.pElementPointer;
|
|
||||||
|
|
||||||
int size = batch.size;
|
int size = batch.size;
|
||||||
|
|
||||||
for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) {
|
LWJGL.memPutInt(batch.pBaseVertex + (size << 2), vertexOffset);
|
||||||
LWJGL.memPutInt(pBaseVertex + (size << 2), SectionRenderDataUnsafe.getVertexOffset(pMeshData, facing));
|
LWJGL.memPutInt(batch.pElementCount + (size << 2), elementCount);
|
||||||
LWJGL.memPutInt(pElementCount + (size << 2), SectionRenderDataUnsafe.getElementCount(pMeshData, facing));
|
LWJGL.memPutAddress(batch.pElementPointer + (size << 3), indexOffset);
|
||||||
LWJGL.memPutAddress(pElementPointer + (size << 3), SectionRenderDataUnsafe.getIndexOffset(pMeshData, facing) & indexPointerMask);
|
batch.size += commandCount;
|
||||||
|
|
||||||
size += (mask >> facing) & 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
batch.size = size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+6
-16
@@ -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.GlIndexType;
|
||||||
import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType;
|
import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType;
|
||||||
import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation;
|
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 static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL;
|
||||||
import org.taumc.celeritas.lwjgl.LWJGLServiceProvider;
|
import org.taumc.celeritas.lwjgl.LWJGLServiceProvider;
|
||||||
|
|
||||||
@@ -51,22 +49,14 @@ public class IndirectMultiDrawEmitter implements MultiDrawEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addDrawCommands(long pMeshData, int facingMask, int indexPointerMask) {
|
public void addDrawCommand(int vertexOffset, int elementCount, int indexOffset, int commandCount) {
|
||||||
int size = this.numCommands;
|
long ptr = this.indirectBuffer + (long)this.numCommands * COMMAND_SIZE;
|
||||||
|
LWJGL.memPutInt(ptr + 0L, elementCount);
|
||||||
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;
|
|
||||||
LWJGL.memPutInt(ptr + 8L, indexOffset / 4);
|
LWJGL.memPutInt(ptr + 8L, indexOffset / 4);
|
||||||
LWJGL.memPutInt(ptr + 12L, SectionRenderDataUnsafe.getVertexOffset(pMeshData, facing)); // baseVertex
|
LWJGL.memPutInt(ptr + 12L, vertexOffset);
|
||||||
|
|
||||||
size += (facingMask >> facing) & 1;
|
// A zero count leaves the speculative command slot open to be overwritten by the next candidate.
|
||||||
}
|
this.numCommands += commandCount;
|
||||||
|
|
||||||
this.numCommands = size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+56
-1
@@ -4,12 +4,67 @@ import org.embeddedt.embeddium.impl.gl.device.CommandList;
|
|||||||
import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType;
|
import org.embeddedt.embeddium.impl.gl.tessellation.GlPrimitiveType;
|
||||||
import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation;
|
import org.embeddedt.embeddium.impl.gl.tessellation.GlTessellation;
|
||||||
import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing;
|
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;
|
import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion;
|
||||||
|
|
||||||
public interface MultiDrawEmitter {
|
public interface MultiDrawEmitter {
|
||||||
int MAX_COMMAND_COUNT = (ModelQuadFacing.COUNT * RenderRegion.REGION_SIZE) + 1;
|
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++) {
|
||||||
|
this.addDrawCommand(
|
||||||
|
SectionRenderDataUnsafe.getVertexOffset(pMeshData, facing),
|
||||||
|
SectionRenderDataUnsafe.getElementCount(pMeshData, facing),
|
||||||
|
SectionRenderDataUnsafe.getIndexOffset(pMeshData, facing),
|
||||||
|
(facingMask >> facing) & 1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int vertexOffset = 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++) {
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// start a run only when this facing is visible and no run remains active
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.addDrawCommand(vertexOffset, elementCount, 0, runActive);
|
||||||
|
}
|
||||||
|
|
||||||
|
void addDrawCommand(int vertexOffset, int elementCount, int indexOffset, int commandCount);
|
||||||
void executeBatch(CommandList commandList, GlTessellation tessellation, GlPrimitiveType primitiveType);
|
void executeBatch(CommandList commandList, GlTessellation tessellation, GlPrimitiveType primitiveType);
|
||||||
boolean isEmpty();
|
boolean isEmpty();
|
||||||
int getIndexBufferSize();
|
int getIndexBufferSize();
|
||||||
|
|||||||
Reference in new issue
Block a user