Smarter translucency sorting #42

Merged
embeddedt merged 24 commits from rdh/celeritas:ts into stonecutter 2026-09-16 11:25:42 -04:00
5 changed files with 68 additions and 29 deletions
Showing only changes of commit 6418e3e5f0 - Show all commits

No files matched your search

@@ -15,7 +15,7 @@ import org.jetbrains.annotations.Nullable;
* *
* <p>Bit layout: * <p>Bit layout:
* <pre> * <pre>
* 63 54 53 52 51 49 48 46 45 0 * 63 55 54 53 52 50 49 46 45 0
* +--------------------+--+--+-----------+----------+-------------------+ * +--------------------+--+--+-----------+----------+-------------------+
* | spare |OD|IF| pending | visuals | visibility graph | * | spare |OD|IF| pending | visuals | visibility graph |
* +--------------------+--+--+-----------+----------+-------------------+ * +--------------------+--+--+-----------+----------+-------------------+
@@ -26,12 +26,12 @@ import org.jetbrains.annotations.Nullable;
* another. Each eight-bit row uses its six low bits; the two padding * another. Each eight-bit row uses its six low bits; the two padding
* positions are not part of the visibility field (the final row's * positions are not part of the visibility field (the final row's
* padding is reused by the visual flags).</li> * padding is reused by the visual flags).</li>
* <li>Bits {@code 46..48}: {@link RenderVisualsService} visual flags.</li> * <li>Bits {@code 46..49}: {@link RenderVisualsService} visual flags.</li>
* <li>Bits {@code 49..51}: pending update type; zero means none, otherwise * <li>Bits {@code 50..52}: pending update type; zero means none, otherwise
* {@code ChunkUpdateType.ordinal() + 1}.</li> * {@code ChunkUpdateType.ordinal() + 1}.</li>
* <li>Bit {@code 52}: a build is in flight.</li> * <li>Bit {@code 53}: a build is in flight.</li>
* <li>Bit {@code 53}: the section's built data has occluder boxes.</li> * <li>Bit {@code 54}: the section's built data has occluder boxes.</li>
* <li>Bits {@code 54..63}: reserved.</li> * <li>Bits {@code 55..63}: reserved.</li>
* </ul> * </ul>
* *
* <p>The visibility graph uses only bits {@code 0..45}, but * <p>The visibility graph uses only bits {@code 0..45}, but
@@ -54,20 +54,20 @@ public final class PackedSectionMetadata {
private static final int VISIBILITY_BITS = 46; private static final int VISIBILITY_BITS = 46;
public static final long VISIBILITY_MASK = (1L << VISIBILITY_BITS) - 1; public static final long VISIBILITY_MASK = (1L << VISIBILITY_BITS) - 1;
// Bits 46..48: RenderVisualsService.HAS_* flags. // Bits 46..49: RenderVisualsService flags.
private static final int VISUALS_FLAGS_SHIFT = 46; private static final int VISUALS_FLAGS_SHIFT = 46;
private static final long VISUALS_FLAGS_MASK = 0b111L; private static final long VISUALS_FLAGS_MASK = 0b1111L;
// Bits 49..51: 0 means no pending update; otherwise ordinal + 1. // Bits 50..52: 0 means no pending update; otherwise ordinal + 1.
private static final int PENDING_UPDATE_SHIFT = 49; private static final int PENDING_UPDATE_SHIFT = 50;
private static final long PENDING_UPDATE_MASK = 0b111L; private static final long PENDING_UPDATE_MASK = 0b111L;
// Bit 52: a build cancellation token is currently attached. // Bit 53: a build cancellation token is currently attached.
private static final int BUILD_IN_FLIGHT_BIT = 52; private static final int BUILD_IN_FLIGHT_BIT = 53;
private static final long BUILD_IN_FLIGHT_FLAG = 1L << BUILD_IN_FLIGHT_BIT; private static final long BUILD_IN_FLIGHT_FLAG = 1L << BUILD_IN_FLIGHT_BIT;
// Bit 53: the section's built data has occluder boxes. // Bit 54: the section's built data has occluder boxes.
private static final int HAS_OCCLUDER_DATA_BIT = 53; private static final int HAS_OCCLUDER_DATA_BIT = 54;
private static final long HAS_OCCLUDER_DATA_FLAG = 1L << HAS_OCCLUDER_DATA_BIT; private static final long HAS_OCCLUDER_DATA_FLAG = 1L << HAS_OCCLUDER_DATA_BIT;
/** /**
@@ -90,14 +90,14 @@ public final class PackedSectionMetadata {
return (packed & ~VISIBILITY_MASK) | (visibilityData & VISIBILITY_MASK); return (packed & ~VISIBILITY_MASK) | (visibilityData & VISIBILITY_MASK);
} }
/** Returns the three visual-presence flags. */ /** Returns the four visual flags. */
public static int getVisualsFlags(long packed) { public static int getVisualsFlags(long packed) {
return (int) ((packed >>> VISUALS_FLAGS_SHIFT) & VISUALS_FLAGS_MASK); return (int) ((packed >>> VISUALS_FLAGS_SHIFT) & VISUALS_FLAGS_MASK);
} }
/** /**
* Replaces the visual-presence flags and preserves every other field. * Replaces the visual flags and preserves every other field.
* Only the three low bits of {@code flags} are stored. * Only the four low bits of {@code flags} are stored.
*/ */
public static long withVisualsFlags(long packed, int flags) { public static long withVisualsFlags(long packed, int flags) {
return (packed & ~(VISUALS_FLAGS_MASK << VISUALS_FLAGS_SHIFT)) return (packed & ~(VISUALS_FLAGS_MASK << VISUALS_FLAGS_SHIFT))
@@ -145,7 +145,7 @@ public final class PackedSectionMetadata {
/* /*
* Compact collector metadata: includes only the info the VisibleChunkCollector cares about. * Compact collector metadata: includes only the info the VisibleChunkCollector cares about.
* *
* 6 5 3 2 0 * 7 6 4 3 0
* +--+-------+--------+ * +--+-------+--------+
* |IF|pending|visuals | * |IF|pending|visuals |
* +--+-------+--------+ * +--+-------+--------+
@@ -70,12 +70,15 @@ public class RenderSection extends AbstractSection {
@Getter @Getter
private int highestSortingIndex = NO_TRANSLUCENT_GEOMETRY; private int highestSortingIndex = NO_TRANSLUCENT_GEOMETRY;
@Getter public enum SortMode {
@Setter NONE,
private boolean needsDynamicTranslucencySorting; TREE,
DYNAMIC
}
@Getter @Getter
private boolean treeSorted; @NotNull
private SortMode sortMode = SortMode.NONE;
// Pending Update State // Pending Update State
@@ -154,7 +157,13 @@ public class RenderSection extends AbstractSection {
} }
public void updateCachedContextDataFlags() { public void updateCachedContextDataFlags() {
int flags = this.contextData != null ? this.contextData.getVisualBitmaskForSection() : 0; int flags = 0;
if (this.contextData != null) {
flags = this.contextData.getVisualBitmaskForSection();
if (this.sortMode == SortMode.DYNAMIC) {
flags |= 1 << RenderVisualsService.NEEDS_DYNAMIC_SORT;
}
}
long visibilityData = this.contextData != null ? this.contextData.visibilityData : VisibilityEncoding.NULL; long visibilityData = this.contextData != null ? this.contextData.visibilityData : VisibilityEncoding.NULL;
boolean hasOccluderData = this.contextData != null && this.contextData.occluderBoxes != null; boolean hasOccluderData = this.contextData != null && this.contextData.occluderBoxes != null;
@@ -197,8 +206,23 @@ public class RenderSection extends AbstractSection {
public void setTranslucencySortStates(@NotNull Map<TerrainRenderPass, SortState.Resortable> sortStates, int highestSortingIndex) { public void setTranslucencySortStates(@NotNull Map<TerrainRenderPass, SortState.Resortable> sortStates, int highestSortingIndex) {
this.translucencySortStates = Map.copyOf(sortStates); this.translucencySortStates = Map.copyOf(sortStates);
this.highestSortingIndex = highestSortingIndex; this.highestSortingIndex = highestSortingIndex;
this.needsDynamicTranslucencySorting = !sortStates.isEmpty(); if (sortStates.isEmpty()) {
this.treeSorted = !sortStates.isEmpty() && sortStates.values().stream().allMatch(state -> state instanceof PartitionTree); this.sortMode = SortMode.NONE;
} else if (sortStates.values().stream().allMatch(state -> state instanceof PartitionTree)) {
this.sortMode = SortMode.TREE;
} else {
this.sortMode = SortMode.DYNAMIC;
}
this.updateCachedContextDataFlags();
}
public void clearTranslucencySortStates() {
this.translucencySortStates = Collections.emptyMap();
this.sortMode = SortMode.NONE;
}
public boolean isTreeSorted() {
return this.sortMode == SortMode.TREE;
} }
public @Nullable CancellationToken getBuildCancellationToken() { public @Nullable CancellationToken getBuildCancellationToken() {
@@ -297,7 +297,7 @@ public abstract class RenderSectionManager {
if (!region.hasSectionsInPass(translucentPass)) { if (!region.hasSectionsInPass(translucentPass)) {
continue; continue;
} }
ByteIterator sectionIterator = entry.sectionsWithGeometryIterator(); ByteIterator sectionIterator = entry.sectionsNeedingDynamicSortIterator();
if (sectionIterator == null) { if (sectionIterator == null) {
continue; continue;
} }
@@ -305,7 +305,7 @@ public abstract class RenderSectionManager {
var section = region.getSection(sectionIterator.nextByteAsInt()); var section = region.getSection(sectionIterator.nextByteAsInt());
// tree-sorted sections are retriggered by scheduleTreeSortedUpdates() instead // tree-sorted sections are retriggered by scheduleTreeSortedUpdates() instead
if (section == null || !section.isNeedsDynamicTranslucencySorting() || section.isTreeSorted()) { if (section == null || section.getSortMode() != RenderSection.SortMode.DYNAMIC) {
continue; continue;
} }
@@ -789,7 +789,7 @@ 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.setNeedsDynamicTranslucencySorting(false); section.clearTranslucencySortStates();
} }
} 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);
@@ -806,7 +806,7 @@ public abstract class RenderSectionManager {
protected abstract @Nullable ChunkBuilderTask<ChunkBuildOutput> createRebuildTask(RenderSection render, int frame); protected abstract @Nullable ChunkBuilderTask<ChunkBuildOutput> createRebuildTask(RenderSection render, int frame);
public ChunkBuilderSortTask createSortTask(RenderSection render, int frame) { public ChunkBuilderSortTask createSortTask(RenderSection render, int frame) {
if(!render.isNeedsDynamicTranslucencySorting()) if(render.getTranslucencySortStates().isEmpty())
return null; return null;
return new ChunkBuilderSortTask(render, (float)cameraPosition.x, (float)cameraPosition.y, (float)cameraPosition.z, frame, render.getTranslucencySortStates(), this.renderPassConfiguration); return new ChunkBuilderSortTask(render, (float)cameraPosition.x, (float)cameraPosition.y, (float)cameraPosition.z, frame, render.getTranslucencySortStates(), this.renderPassConfiguration);
} }
@@ -19,6 +19,9 @@ public class ChunkRenderList {
private final byte[] sectionsWithEntities = new byte[RenderRegion.REGION_SIZE]; private final byte[] sectionsWithEntities = new byte[RenderRegion.REGION_SIZE];
private int sectionsWithEntitiesCount = 0; private int sectionsWithEntitiesCount = 0;
private final byte[] sectionsNeedingDynamicSort = new byte[RenderRegion.REGION_SIZE];
private int sectionsNeedingDynamicSortCount = 0;
private int size; private int size;
public ChunkRenderList(RenderRegion region) { public ChunkRenderList(RenderRegion region) {
@@ -40,6 +43,17 @@ public class ChunkRenderList {
this.sectionsWithEntities[this.sectionsWithEntitiesCount] = (byte) index; this.sectionsWithEntities[this.sectionsWithEntitiesCount] = (byte) index;
this.sectionsWithEntitiesCount += (flags >>> RenderVisualsService.HAS_BLOCK_ENTITIES) & 1; this.sectionsWithEntitiesCount += (flags >>> RenderVisualsService.HAS_BLOCK_ENTITIES) & 1;
this.sectionsNeedingDynamicSort[this.sectionsNeedingDynamicSortCount] = (byte) index;
this.sectionsNeedingDynamicSortCount += (flags >>> RenderVisualsService.NEEDS_DYNAMIC_SORT) & 1;
}
public @Nullable ByteIterator sectionsNeedingDynamicSortIterator() {
if (this.sectionsNeedingDynamicSortCount == 0) {
return null;
}
return new ByteArrayIterator(this.sectionsNeedingDynamicSort, this.sectionsNeedingDynamicSortCount);
} }
public @Nullable ByteIterator sectionsWithGeometryIterator() { public @Nullable ByteIterator sectionsWithGeometryIterator() {
@@ -4,4 +4,5 @@ public class RenderVisualsService {
public static final int HAS_BLOCK_GEOMETRY = 0; public static final int HAS_BLOCK_GEOMETRY = 0;
public static final int HAS_SPRITES = 1; public static final int HAS_SPRITES = 1;
public static final int HAS_BLOCK_ENTITIES = 2; public static final int HAS_BLOCK_ENTITIES = 2;
public static final int NEEDS_DYNAMIC_SORT = 3;
} }