Smarter translucency sorting #42

Merged
embeddedt merged 24 commits from rdh/celeritas:ts into stonecutter 2026-09-16 11:25:42 -04:00
3 changed files with 53 additions and 4 deletions
Showing only changes of commit 8a61753c87 - Show all commits

No files matched your search

@@ -8,6 +8,7 @@ import org.embeddedt.embeddium.impl.render.chunk.occlusion.VisibilityEncoding;
import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion;
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
import org.embeddedt.embeddium.impl.util.task.CancellationToken;
import org.embeddedt.embeddium.impl.render.chunk.sorting.PartitionTree;
import org.embeddedt.embeddium.impl.render.chunk.sorting.SortState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -73,6 +74,9 @@ public class RenderSection extends AbstractSection {
@Setter
private boolean needsDynamicTranslucencySorting;
@Getter
private boolean treeSorted;
// Pending Update State
// The in-flight build job for this section, if one exists. Serves two purposes:
@@ -194,6 +198,16 @@ public class RenderSection extends AbstractSection {
this.translucencySortStates = Map.copyOf(sortStates);
this.highestSortingIndex = highestSortingIndex;
this.needsDynamicTranslucencySorting = !sortStates.isEmpty();
this.treeSorted = !sortStates.isEmpty() && sortStates.values().stream().allMatch(state -> state instanceof PartitionTree);
}
public boolean crossesAnyCutPlane(double lastX, double lastY, double lastZ, double camX, double camY, double camZ) {
for (SortState.Resortable state : this.translucencySortStates.values()) {
if (((PartitionTree) state).crossesCutPlane(lastX, lastY, lastZ, camX, camY, camZ)) {
return true;
}
}
return false;
}
public @Nullable CancellationToken getBuildCancellationToken() {
@@ -300,6 +300,16 @@ public abstract class RenderSectionManager {
continue;
}
if (section.isTreeSorted()) {
boolean crossed = section.crossesAnyCutPlane(
section.lastCameraX - section.getOriginX(), section.lastCameraY - section.getOriginY(), section.lastCameraZ - section.getOriginZ(),
cameraPosition.x - section.getOriginX(), cameraPosition.y - section.getOriginY(), cameraPosition.z - section.getOriginZ()
);
if (!crossed) {
continue;
}
} else {
double dx = cameraPosition.x - section.lastCameraX;
double dy = cameraPosition.y - section.lastCameraY;
double dz = cameraPosition.z - section.lastCameraZ;
@@ -314,7 +324,11 @@ public abstract class RenderSectionManager {
camSectionY != PositionUtil.posToSectionCoord(section.lastCameraY) ||
camSectionZ != PositionUtil.posToSectionCoord(section.lastCameraZ);
if (cameraChangedSection || section.isAlignedWithSectionOnGrid(camSectionX, camSectionY, camSectionZ)) {
if (!cameraChangedSection && !section.isAlignedWithSectionOnGrid(camSectionX, camSectionY, camSectionZ)) {
continue;
}
}
section.setPendingUpdate(update);
// Inject it into the appropriate list
rebuildLists.get(update).add(section);
@@ -325,7 +339,6 @@ public abstract class RenderSectionManager {
}
}
}
}
/**
* {@return true if the renderer should respect per-frame queue limits and not try to update as many chunks as
@@ -618,6 +631,10 @@ public abstract class RenderSectionManager {
}
render.setTranslucencySortStates(sortStates.isEmpty() ? Collections.emptyMap() : sortStates, highestIndex);
render.lastCameraX = this.cameraPosition.x;
render.lastCameraY = this.cameraPosition.y;
render.lastCameraZ = this.cameraPosition.z;
}
// Section MetadataSink: mirrors a section's packed metadata into the graph-search lattice on every
@@ -1024,11 +1041,13 @@ public abstract class RenderSectionManager {
var rebuildLists = this.getCurrentRenderListManager().getRebuildLists();
list.add(String.format("Chunk Queues: U=%02d (P0=%03d | P1=%03d | P2=%03d)",
list.add(String.format("Chunk Queues: U=%02d (P0=%03d | P1=%03d | P2=%03d) S=%03d/%03d",
this.buildResults.size(),
rebuildLists.getUpdateCount(ChunkUpdateType.IMPORTANT_REBUILD),
rebuildLists.getUpdateCount(ChunkUpdateType.REBUILD),
rebuildLists.getUpdateCount(ChunkUpdateType.INITIAL_BUILD)
rebuildLists.getUpdateCount(ChunkUpdateType.INITIAL_BUILD),
rebuildLists.getUpdateCount(ChunkUpdateType.IMPORTANT_SORT),
rebuildLists.getUpdateCount(ChunkUpdateType.SORT)
));
var debugStats = renderListManager.getDebugStatistics();
@@ -237,6 +237,22 @@ public final class PartitionTree implements SortState.Resortable {
return pos + quads.length;
}
public boolean crossesCutPlane(double lastX, double lastY, double lastZ, double camX, double camY, double camZ) {
return crossesOnAxis(0, lastX, camX) || crossesOnAxis(1, lastY, camY) || crossesOnAxis(2, lastZ, camZ);
}
private boolean crossesOnAxis(int axis, double from, double to) {
double lo = Math.min(from, to), hi = Math.max(from, to);
for (float cut : cutPlanes[axis]) {
if (cut > lo && cut <= hi) {
return true;
}
}
return false;
}
@Override
public int debugIndex() {
return 2;