Smarter translucency sorting #42

Merged
embeddedt merged 24 commits from rdh/celeritas:ts into stonecutter 2026-09-16 11:25:42 -04:00
2 changed files with 88 additions and 47 deletions
Showing only changes of commit e6f2061e38 - Show all commits

No files matched your search

@@ -1,5 +1,6 @@
package org.embeddedt.embeddium.impl.render.chunk.compile.sorting; package org.embeddedt.embeddium.impl.render.chunk.compile.sorting;
import org.embeddedt.embeddium.impl.render.chunk.sorting.PartitionTree;
import org.embeddedt.embeddium.impl.render.chunk.sorting.SortState; import org.embeddedt.embeddium.impl.render.chunk.sorting.SortState;
import org.embeddedt.embeddium.impl.render.chunk.sorting.TranslucentSorter; import org.embeddedt.embeddium.impl.render.chunk.sorting.TranslucentSorter;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -43,24 +44,26 @@ public final class QuadPrimitiveType implements ChunkPrimitiveType {
} }
long ptr = LWJGL.memAddress(indexBuffer); long ptr = LWJGL.memAddress(indexBuffer);
int elementsPerPrimitive = this.getIndexBufferElementsPerPrimitive();
boolean triangulating = this.triangulating;
for (int primitiveIndex = 0; primitiveIndex < numPrimitives; primitiveIndex++) { for (int primitiveIndex = 0; primitiveIndex < numPrimitives; primitiveIndex++) {
int indexOffset = primitiveIndex * elementsPerPrimitive; this.writePrimitive(ptr, primitiveIndex, primitiveIndex);
int vertexOffset = primitiveIndex * VERTICES_PER_PRIMITIVE; }
}
LWJGL.memPutInt(ptr + (indexOffset + 0) * 4, vertexOffset + 0); /** writes the indices of the given quad as the primitive at the given position */
LWJGL.memPutInt(ptr + (indexOffset + 1) * 4, vertexOffset + 1); private void writePrimitive(long ptr, int primitiveIndex, int quad) {
LWJGL.memPutInt(ptr + (indexOffset + 2) * 4, vertexOffset + 2); int indexOffset = primitiveIndex * this.getIndexBufferElementsPerPrimitive();
int vertexOffset = quad * VERTICES_PER_PRIMITIVE;
if (triangulating) { LWJGL.memPutInt(ptr + (indexOffset + 0) * 4, vertexOffset + 0);
LWJGL.memPutInt(ptr + (indexOffset + 3) * 4, vertexOffset + 2); LWJGL.memPutInt(ptr + (indexOffset + 1) * 4, vertexOffset + 1);
LWJGL.memPutInt(ptr + (indexOffset + 4) * 4, vertexOffset + 3); LWJGL.memPutInt(ptr + (indexOffset + 2) * 4, vertexOffset + 2);
LWJGL.memPutInt(ptr + (indexOffset + 5) * 4, vertexOffset + 0);
} else { if (this.triangulating) {
LWJGL.memPutInt(ptr + (indexOffset + 3) * 4, vertexOffset + 3); LWJGL.memPutInt(ptr + (indexOffset + 3) * 4, vertexOffset + 2);
} LWJGL.memPutInt(ptr + (indexOffset + 4) * 4, vertexOffset + 3);
LWJGL.memPutInt(ptr + (indexOffset + 5) * 4, vertexOffset + 0);
} else {
LWJGL.memPutInt(ptr + (indexOffset + 3) * 4, vertexOffset + 3);
} }
} }
@@ -71,31 +74,48 @@ public final class QuadPrimitiveType implements ChunkPrimitiveType {
} }
long ptr = LWJGL.memAddress(indexBuffer); long ptr = LWJGL.memAddress(indexBuffer);
int elementsPerPrimitive = this.getIndexBufferElementsPerPrimitive();
boolean triangulating = this.triangulating;
for (int primitiveIndex = 0; primitiveIndex < primitiveMapping.length; primitiveIndex++) { for (int primitiveIndex = 0; primitiveIndex < primitiveMapping.length; primitiveIndex++) {
int indexOffset = primitiveIndex * elementsPerPrimitive; this.writePrimitive(ptr, primitiveIndex, primitiveMapping[primitiveIndex]);
}
}
// Map to the desired primitive /** streams a partition tree's readout straight into the index buffer */
int vertexOffset = primitiveMapping[primitiveIndex] * VERTICES_PER_PRIMITIVE; private final class BufferSink implements PartitionTree.Sink {
private final long ptr;
private int primitiveIndex;
LWJGL.memPutInt(ptr + (indexOffset + 0) * 4, vertexOffset + 0); private BufferSink(long ptr) {
LWJGL.memPutInt(ptr + (indexOffset + 1) * 4, vertexOffset + 1); this.ptr = ptr;
LWJGL.memPutInt(ptr + (indexOffset + 2) * 4, vertexOffset + 2); }
if (triangulating) { @Override
LWJGL.memPutInt(ptr + (indexOffset + 3) * 4, vertexOffset + 2); public void accept(int[] quads) {
LWJGL.memPutInt(ptr + (indexOffset + 4) * 4, vertexOffset + 3); for (int quad : quads) {
LWJGL.memPutInt(ptr + (indexOffset + 5) * 4, vertexOffset + 0); writePrimitive(this.ptr, this.primitiveIndex++, quad);
} else {
LWJGL.memPutInt(ptr + (indexOffset + 3) * 4, vertexOffset + 3);
} }
} }
} }
@Override @Override
public void generateSortedIndexBuffer(ByteBuffer indexBuffer, int quadCount, @Nullable SortState chunkData, float x, float y, float z) { public void generateSortedIndexBuffer(ByteBuffer indexBuffer, int quadCount, @Nullable SortState chunkData, float x, float y, float z) {
if (chunkData instanceof PartitionTree tree) {
int bufferSize = getIndexBufferSize(quadCount);
if (indexBuffer.capacity() != bufferSize) {
throw new IllegalStateException("Given index buffer has length " + indexBuffer.capacity() + " but we expected " + bufferSize);
}
if (quadCount != tree.quadCount()) {
throw new IllegalStateException(String.format("Mismatched SortState (%d) vs given quad count (%d)", tree.quadCount(), quadCount));
}
var sink = new BufferSink(LWJGL.memAddress(indexBuffer));
tree.order(x, y, z, sink);
if (sink.primitiveIndex != quadCount) {
throw new IllegalStateException(String.format("Partition tree wrote %d of %d quads", sink.primitiveIndex, quadCount));
}
return;
}
int[] order = chunkData != null ? TranslucentSorter.order(chunkData, x, y, z) : null; int[] order = chunkData != null ? TranslucentSorter.order(chunkData, x, y, z) : null;
if (order == null) { if (order == null) {
@@ -326,21 +326,32 @@ public final class PartitionTree implements SortState.Resortable {
return cutPlanes[axis]; return cutPlanes[axis];
} }
public int @Nullable [] order(float camX, float camY, float camZ) { /** Receives the quads of one leaf or one plane at a time, farthest first */
int[] order = new int[quadCount]; public interface Sink {
write(root, camX, camY, camZ, order, 0); void accept(int[] quads);
return order;
} }
// returns the position just past the last quad written public int @Nullable [] order(float camX, float camY, float camZ) {
private static int write(Node node, float camX, float camY, float camZ, int[] order, int pos) { var sink = new ArraySink(new int[quadCount]);
order(camX, camY, camZ, sink);
return sink.order;
}
/** The draw order for the camera position, streamed so the caller can write it out without an array in between */
public void order(float camX, float camY, float camZ, Sink sink) {
write(root, camX, camY, camZ, sink);
}
private static void write(Node node, float camX, float camY, float camZ, Sink sink) {
if (node instanceof Leaf leaf) { if (node instanceof Leaf leaf) {
System.arraycopy(leaf.quads, 0, order, pos, leaf.quads.length); sink.accept(leaf.quads);
return pos + leaf.quads.length; return;
} }
if (node instanceof Fixed fixed) { if (node instanceof Fixed fixed) {
return write(fixed.rest, camX, camY, camZ, order, write(fixed.first, camX, camY, camZ, order, pos)); write(fixed.first, camX, camY, camZ, sink);
write(fixed.rest, camX, camY, camZ, sink);
return;
} }
Split split = (Split) node; Split split = (Split) node;
@@ -356,23 +367,33 @@ public final class PartitionTree implements SortState.Resortable {
// slabs below the camera's, farthest first, each followed by the plane above it // slabs below the camera's, farthest first, each followed by the plane above it
for (int i = 0; i < slab; i++) { for (int i = 0; i < slab; i++) {
pos = write(split.slabs[i], camX, camY, camZ, order, pos); write(split.slabs[i], camX, camY, camZ, sink);
pos = writePlane(split.onPlane[i], order, pos); sink.accept(split.onPlane[i]);
} }
// slabs above the camera's, farthest first, each followed by the plane below it // slabs above the camera's, farthest first, each followed by the plane below it
for (int i = split.slabs.length - 1; i > slab; i--) { for (int i = split.slabs.length - 1; i > slab; i--) {
pos = write(split.slabs[i], camX, camY, camZ, order, pos); write(split.slabs[i], camX, camY, camZ, sink);
pos = writePlane(split.onPlane[i - 1], order, pos); sink.accept(split.onPlane[i - 1]);
} }
// the camera's own slab is nearest, drawn last // the camera's own slab is nearest, drawn last
return write(split.slabs[slab], camX, camY, camZ, order, pos); write(split.slabs[slab], camX, camY, camZ, sink);
} }
private static int writePlane(int[] quads, int[] order, int pos) { private static final class ArraySink implements Sink {
System.arraycopy(quads, 0, order, pos, quads.length); private final int[] order;
return pos + quads.length; private int pos;
private ArraySink(int[] order) {
this.order = order;
}
@Override
public void accept(int[] quads) {
System.arraycopy(quads, 0, order, pos, quads.length);
pos += quads.length;
}
} }
public boolean crossesCutPlane(double lastX, double lastY, double lastZ, double camX, double camY, double camZ) { public boolean crossesCutPlane(double lastX, double lastY, double lastZ, double camX, double camY, double camZ) {