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 70 additions and 21 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.region.RenderRegion;
import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass; import org.embeddedt.embeddium.impl.render.chunk.terrain.TerrainRenderPass;
import org.embeddedt.embeddium.impl.util.task.CancellationToken; 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.embeddedt.embeddium.impl.render.chunk.sorting.SortState;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -73,6 +74,9 @@ public class RenderSection extends AbstractSection {
@Setter @Setter
private boolean needsDynamicTranslucencySorting; private boolean needsDynamicTranslucencySorting;
@Getter
private boolean treeSorted;
// Pending Update State // Pending Update State
// The in-flight build job for this section, if one exists. Serves two purposes: // 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.translucencySortStates = Map.copyOf(sortStates);
this.highestSortingIndex = highestSortingIndex; this.highestSortingIndex = highestSortingIndex;
this.needsDynamicTranslucencySorting = !sortStates.isEmpty(); 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() { public @Nullable CancellationToken getBuildCancellationToken() {
@@ -300,29 +300,42 @@ public abstract class RenderSectionManager {
continue; continue;
} }
double dx = cameraPosition.x - section.lastCameraX; if (section.isTreeSorted()) {
double dy = cameraPosition.y - section.lastCameraY; boolean crossed = section.crossesAnyCutPlane(
double dz = cameraPosition.z - section.lastCameraZ; section.lastCameraX - section.getOriginX(), section.lastCameraY - section.getOriginY(), section.lastCameraZ - section.getOriginZ(),
double camDelta = (dx * dx) + (dy * dy) + (dz * dz); cameraPosition.x - section.getOriginX(), cameraPosition.y - section.getOriginY(), cameraPosition.z - section.getOriginZ()
);
if (camDelta < 1) { if (!crossed) {
// Didn't move enough, ignore continue;
continue; }
} else {
double dx = cameraPosition.x - section.lastCameraX;
double dy = cameraPosition.y - section.lastCameraY;
double dz = cameraPosition.z - section.lastCameraZ;
double camDelta = (dx * dx) + (dy * dy) + (dz * dz);
if (camDelta < 1) {
// Didn't move enough, ignore
continue;
}
boolean cameraChangedSection = camSectionX != PositionUtil.posToSectionCoord(section.lastCameraX) ||
camSectionY != PositionUtil.posToSectionCoord(section.lastCameraY) ||
camSectionZ != PositionUtil.posToSectionCoord(section.lastCameraZ);
if (!cameraChangedSection && !section.isAlignedWithSectionOnGrid(camSectionX, camSectionY, camSectionZ)) {
continue;
}
} }
boolean cameraChangedSection = camSectionX != PositionUtil.posToSectionCoord(section.lastCameraX) || section.setPendingUpdate(update);
camSectionY != PositionUtil.posToSectionCoord(section.lastCameraY) || // Inject it into the appropriate list
camSectionZ != PositionUtil.posToSectionCoord(section.lastCameraZ); rebuildLists.get(update).add(section);
if (cameraChangedSection || section.isAlignedWithSectionOnGrid(camSectionX, camSectionY, camSectionZ)) { section.lastCameraX = cameraPosition.x;
section.setPendingUpdate(update); section.lastCameraY = cameraPosition.y;
// Inject it into the appropriate list section.lastCameraZ = cameraPosition.z;
rebuildLists.get(update).add(section);
section.lastCameraX = cameraPosition.x;
section.lastCameraY = cameraPosition.y;
section.lastCameraZ = cameraPosition.z;
}
} }
} }
} }
@@ -618,6 +631,10 @@ public abstract class RenderSectionManager {
} }
render.setTranslucencySortStates(sortStates.isEmpty() ? Collections.emptyMap() : sortStates, highestIndex); 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 // 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(); 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(), this.buildResults.size(),
rebuildLists.getUpdateCount(ChunkUpdateType.IMPORTANT_REBUILD), rebuildLists.getUpdateCount(ChunkUpdateType.IMPORTANT_REBUILD),
rebuildLists.getUpdateCount(ChunkUpdateType.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(); var debugStats = renderListManager.getDebugStatistics();
@@ -237,6 +237,22 @@ public final class PartitionTree implements SortState.Resortable {
return pos + quads.length; 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 @Override
public int debugIndex() { public int debugIndex() {
return 2; return 2;