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 61ca849523 - Show all commits
@@ -364,29 +364,29 @@ public abstract class RenderSectionManager {
return; return;
} }
var rebuildLists = this.getCurrentRenderListManager().getRebuildLists().byUpdateType();
var allowImportant = allowImportantRebuilds();
this.cutPlaneIndex.query( this.cutPlaneIndex.query(
this.previousCameraPosition.x, this.previousCameraPosition.y, this.previousCameraPosition.z, this.previousCameraPosition.x, this.previousCameraPosition.y, this.previousCameraPosition.z,
this.cameraPosition.x, this.cameraPosition.y, this.cameraPosition.z, this.cameraPosition.x, this.cameraPosition.y, this.cameraPosition.z,
section -> { section -> {
this.treeTriggersThisSecond++; this.treeTriggersThisSecond++;
this.scheduleTreeSort(section);
ChunkUpdateType update = ChunkUpdateType.getPromotionUpdateType(section.getPendingUpdate(),
(allowImportant && this.shouldPrioritizeRebuild(section)) ? ChunkUpdateType.IMPORTANT_SORT : ChunkUpdateType.SORT);
if (update == null) {
// We wouldn't be able to resort this section anyway
return;
}
section.setPendingUpdate(update);
rebuildLists.get(update).add(section);
} }
); );
} }
private void scheduleTreeSort(RenderSection section) {
ChunkUpdateType update = ChunkUpdateType.getPromotionUpdateType(section.getPendingUpdate(),
(allowImportantRebuilds() && this.shouldPrioritizeRebuild(section)) ? ChunkUpdateType.IMPORTANT_SORT : ChunkUpdateType.SORT);
if (update == null) {
// We wouldn't be able to resort this section anyway
return;
}
section.setPendingUpdate(update);
this.getCurrentRenderListManager().getRebuildLists().byUpdateType().get(update).add(section);
}
/** /**
* {@return true if the renderer should respect per-frame queue limits and not try to update as many chunks as * {@return true if the renderer should respect per-frame queue limits and not try to update as many chunks as
* possible per frame} * possible per frame}
@@ -632,6 +632,10 @@ public abstract class RenderSectionManager {
for (var holder : filtered) { for (var holder : filtered) {
var result = holder.output(); var result = holder.output();
// whether this result belongs to the most recently submitted build
boolean latest = result.buildTime >= result.render.getLastSubmittedFrame();
if (result instanceof ChunkBuildOutput buildResult) { if (result instanceof ChunkBuildOutput buildResult) {
boolean changed = this.updateSectionInfo(result.render, buildResult.info); boolean changed = this.updateSectionInfo(result.render, buildResult.info);
@@ -643,15 +647,12 @@ public abstract class RenderSectionManager {
} }
// We only change the translucency info on full rebuilds, as sorts can keep using the same data // We only change the translucency info on full rebuilds, as sorts can keep using the same data
this.updateTranslucencyInfo(result.render, buildResult.meshes); this.updateTranslucencyInfo(result.render, buildResult.meshes, latest);
} }
var job = result.render.getBuildCancellationToken(); var job = result.render.getBuildCancellationToken();
// Only clear the token if this result belongs to the most recently submitted build. if (job != null && latest) {
// A stale result from an earlier submission must not clear the token for a newer
// in-flight job, which is identified by a higher lastSubmittedFrame.
if (job != null && result.buildTime >= result.render.getLastSubmittedFrame()) {
result.render.setBuildCancellationToken(null); result.render.setBuildCancellationToken(null);
} }
@@ -660,7 +661,11 @@ public abstract class RenderSectionManager {
} }
} }
private void updateTranslucencyInfo(RenderSection render, Map<TerrainRenderPass, BuiltSectionMeshParts> meshes) { /**
* @param latestBuild whether the meshes come from the most recent submission; a stale build keeps its planes
* indexed but leaves the camera bookkeeping to the build still in flight
*/
private void updateTranslucencyInfo(RenderSection render, Map<TerrainRenderPass, BuiltSectionMeshParts> meshes, boolean latestBuild) {
Map<TerrainRenderPass, SortState.Resortable> sortStates = new Reference2ObjectArrayMap<>(); Map<TerrainRenderPass, SortState.Resortable> sortStates = new Reference2ObjectArrayMap<>();
int highestIndex = RenderSection.NO_TRANSLUCENT_GEOMETRY; int highestIndex = RenderSection.NO_TRANSLUCENT_GEOMETRY;
@@ -688,14 +693,28 @@ public abstract class RenderSectionManager {
} }
this.cutPlaneIndex.put(render, PartitionTree.mergeCutPlanes(trees), render.getOriginX(), render.getOriginY(), render.getOriginZ()); this.cutPlaneIndex.put(render, PartitionTree.mergeCutPlanes(trees), render.getOriginX(), render.getOriginY(), render.getOriginZ());
// The build sorted for the camera at submission (see submitRebuildTasks). A crossing since then happened
// before the planes were indexed, so it has to be caught here or the order stays stale until the next one.
int ox = render.getOriginX(), oy = render.getOriginY(), oz = render.getOriginZ();
for (PartitionTree tree : trees) {
if (latestBuild && tree.crossesCutPlane(render.lastCameraX - ox, render.lastCameraY - oy, render.lastCameraZ - oz,
this.cameraPosition.x - ox, this.cameraPosition.y - oy, this.cameraPosition.z - oz)) {
this.treeTriggersThisSecond++;
this.scheduleTreeSort(render);
break;
}
}
} else { } else {
// a rebuild that is no longer tree-sorted must drop its stale planes // a rebuild that is no longer tree-sorted must drop its stale planes
this.cutPlaneIndex.remove(render); this.cutPlaneIndex.remove(render);
} }
render.lastCameraX = this.cameraPosition.x; if (latestBuild) {
render.lastCameraY = this.cameraPosition.y; render.lastCameraX = this.cameraPosition.x;
render.lastCameraZ = this.cameraPosition.z; 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
@@ -809,6 +828,11 @@ public abstract class RenderSectionManager {
if (!type.isSort()) { if (!type.isSort()) {
// Prevent further sorts from being performed on this section // Prevent further sorts from being performed on this section
section.clearTranslucencySortStates(); section.clearTranslucencySortStates();
// the meshing task sorts its translucent geometry for this camera
section.lastCameraX = this.cameraPosition.x;
section.lastCameraY = this.cameraPosition.y;
section.lastCameraZ = this.cameraPosition.z;
} }
} else { } else {
var result = new ChunkJobResult.Success<>(new ChunkBuildOutput(section, RenderSection.EMPTY_DATA, Reference2ReferenceMaps.emptyMap(), frame), -1); var result = new ChunkJobResult.Success<>(new ChunkBuildOutput(section, RenderSection.EMPTY_DATA, Reference2ReferenceMaps.emptyMap(), frame), -1);