Smarter translucency sorting #42

Merged
embeddedt merged 24 commits from rdh/celeritas:ts into stonecutter 2026-09-16 11:25:42 -04:00
Showing only changes of commit b3a509d36f - Show all commits
@@ -1,5 +1,7 @@
package org.embeddedt.embeddium.impl.render.chunk.sorting;
import it.unimi.dsi.fastutil.ints.IntArrayFIFOQueue;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import org.embeddedt.embeddium.impl.model.quad.properties.ModelQuadFacing;
import org.embeddedt.embeddium.impl.util.sorting.MergeSort;
import org.jetbrains.annotations.Nullable;
@@ -216,6 +218,53 @@ public final class TranslucentSorter {
/** returns null if the relation has a cycle */
public static int @Nullable [] topologicalOrder(QuadSet quads) {
return null;
int n = quads.count();
IntArrayList[] successors = new IntArrayList[n];
int[] predecessors = new int[n];
// build the graph up
for (int quadIdx = 0; quadIdx < n; quadIdx++) {
for (int otherIdx = 0; otherIdx < n; otherIdx++) {
if (quadIdx != otherIdx && quads.isSeenThrough(quadIdx, otherIdx)) {
if (successors[quadIdx] == null) {
successors[quadIdx] = new IntArrayList();
}
successors[quadIdx].add(otherIdx);
predecessors[otherIdx]++;
}
}
}
// kahn's algorithm: emit quads with no predecessors and remove them from the graph
// until either all quads are emitted or a cycle is detected
IntArrayFIFOQueue ready = new IntArrayFIFOQueue(n);
for (int quadIdx = 0; quadIdx < n; quadIdx++) {
if (predecessors[quadIdx] == 0) {
ready.enqueue(quadIdx);
}
}
int[] out = new int[n];
int o = 0;
while (!ready.isEmpty()) {
int current = ready.dequeueInt();
out[o++] = current;
IntArrayList next = successors[current];
if (next == null) continue;
for (int i = 0; i < next.size(); i++) {
int successor = next.getInt(i);
if (--predecessors[successor] == 0) {
ready.enqueue(successor);
}
}
}
return o == n ? out : null;
}
}