Smarter translucency sorting #42
No files matched your search
+42
-22
@@ -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,18 +44,21 @@ 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;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** writes the indices of the given quad as the primitive at the given position */
|
||||||
|
private void writePrimitive(long ptr, int primitiveIndex, int quad) {
|
||||||
|
int indexOffset = primitiveIndex * this.getIndexBufferElementsPerPrimitive();
|
||||||
|
int vertexOffset = quad * VERTICES_PER_PRIMITIVE;
|
||||||
|
|
||||||
LWJGL.memPutInt(ptr + (indexOffset + 0) * 4, vertexOffset + 0);
|
LWJGL.memPutInt(ptr + (indexOffset + 0) * 4, vertexOffset + 0);
|
||||||
LWJGL.memPutInt(ptr + (indexOffset + 1) * 4, vertexOffset + 1);
|
LWJGL.memPutInt(ptr + (indexOffset + 1) * 4, vertexOffset + 1);
|
||||||
LWJGL.memPutInt(ptr + (indexOffset + 2) * 4, vertexOffset + 2);
|
LWJGL.memPutInt(ptr + (indexOffset + 2) * 4, vertexOffset + 2);
|
||||||
|
|
||||||
if (triangulating) {
|
if (this.triangulating) {
|
||||||
LWJGL.memPutInt(ptr + (indexOffset + 3) * 4, vertexOffset + 2);
|
LWJGL.memPutInt(ptr + (indexOffset + 3) * 4, vertexOffset + 2);
|
||||||
LWJGL.memPutInt(ptr + (indexOffset + 4) * 4, vertexOffset + 3);
|
LWJGL.memPutInt(ptr + (indexOffset + 4) * 4, vertexOffset + 3);
|
||||||
LWJGL.memPutInt(ptr + (indexOffset + 5) * 4, vertexOffset + 0);
|
LWJGL.memPutInt(ptr + (indexOffset + 5) * 4, vertexOffset + 0);
|
||||||
@@ -62,7 +66,6 @@ public final class QuadPrimitiveType implements ChunkPrimitiveType {
|
|||||||
LWJGL.memPutInt(ptr + (indexOffset + 3) * 4, vertexOffset + 3);
|
LWJGL.memPutInt(ptr + (indexOffset + 3) * 4, vertexOffset + 3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void generateIndexBuffer(ByteBuffer indexBuffer, int[] primitiveMapping) {
|
private void generateIndexBuffer(ByteBuffer indexBuffer, int[] primitiveMapping) {
|
||||||
int bufferSize = getIndexBufferSize(primitiveMapping.length);
|
int bufferSize = getIndexBufferSize(primitiveMapping.length);
|
||||||
@@ -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) {
|
||||||
|
|||||||
+37
-16
@@ -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 {
|
||||||
|
private final int[] order;
|
||||||
|
private int pos;
|
||||||
|
|
||||||
|
private ArraySink(int[] order) {
|
||||||
|
this.order = order;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(int[] quads) {
|
||||||
System.arraycopy(quads, 0, order, pos, quads.length);
|
System.arraycopy(quads, 0, order, pos, quads.length);
|
||||||
return 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) {
|
||||||
|
|||||||
Reference in new issue
Block a user