rasterized occlusion culling #38

Merged
embeddedt merged 20 commits from rdh/celeritas:raster into stonecutter 2026-09-04 22:15:05 -04:00
3 changed files with 88 additions and 2 deletions
Showing only changes of commit 9a662d7577 - Show all commits

No files matched your search

@@ -2,7 +2,9 @@ package org.embeddedt.embeddium.impl.render.chunk.data;
import org.embeddedt.embeddium.impl.render.chunk.lists.RenderVisualsService; import org.embeddedt.embeddium.impl.render.chunk.lists.RenderVisualsService;
import org.jetbrains.annotations.MustBeInvokedByOverriders; import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
/** /**
@@ -13,6 +15,8 @@ public class BuiltRenderSectionData {
public boolean hasBlockGeometry; public boolean hasBlockGeometry;
public long visibilityData; public long visibilityData;
public int @Nullable [] occluderBoxes;
public int getVisualBitmaskForSection() { public int getVisualBitmaskForSection() {
return this.hasBlockGeometry ? (1 << RenderVisualsService.HAS_BLOCK_GEOMETRY) : 0; return this.hasBlockGeometry ? (1 << RenderVisualsService.HAS_BLOCK_GEOMETRY) : 0;
} }
@@ -26,11 +30,12 @@ public class BuiltRenderSectionData {
public boolean equals(Object o) { public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
BuiltRenderSectionData that = (BuiltRenderSectionData) o; BuiltRenderSectionData that = (BuiltRenderSectionData) o;
return hasBlockGeometry == that.hasBlockGeometry && visibilityData == that.visibilityData; return hasBlockGeometry == that.hasBlockGeometry && visibilityData == that.visibilityData
&& Arrays.equals(occluderBoxes, that.occluderBoxes);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(hasBlockGeometry, visibilityData); return (Objects.hash(hasBlockGeometry, visibilityData) * 31) + Arrays.hashCode(occluderBoxes);
} }
} }
@@ -2,7 +2,11 @@ package org.embeddedt.embeddium.impl.render.chunk.occlusion;
import it.unimi.dsi.fastutil.ints.IntArrayFIFOQueue; import it.unimi.dsi.fastutil.ints.IntArrayFIFOQueue;
import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntArrayList;
import org.embeddedt.embeddium.impl.render.chunk.occlusion.geometry.BoxFinder;
import org.embeddedt.embeddium.impl.render.chunk.occlusion.geometry.OccluderBoxes;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.BitSet; import java.util.BitSet;
/** /**
@@ -26,6 +30,8 @@ public class SectionVisibilityBuilder {
private final BitSet blocks; private final BitSet blocks;
private boolean floodFilled;
private static int[] buildFloodfillIndices() { private static int[] buildFloodfillIndices() {
IntArrayList indicesList = new IntArrayList(TOTAL_BLOCKS - (SECTION_AXIS_SIZE - 2) * (SECTION_AXIS_SIZE - 2) * (SECTION_AXIS_SIZE - 2)); IntArrayList indicesList = new IntArrayList(TOTAL_BLOCKS - (SECTION_AXIS_SIZE - 2) * (SECTION_AXIS_SIZE - 2) * (SECTION_AXIS_SIZE - 2));
for (int x = 0; x < SECTION_AXIS_SIZE; x++) { for (int x = 0; x < SECTION_AXIS_SIZE; x++) {
@@ -62,7 +68,20 @@ public class SectionVisibilityBuilder {
this.blocks.set(getIndex(x & 15, y & 15, z & 15)); this.blocks.set(getIndex(x & 15, y & 15, z & 15));
} }
public int @Nullable [] computeOccluderBoxes() {
if (this.floodFilled) {
throw new IllegalStateException("opacity was consumed by computeVisibilityEncoding()");
}
if (this.blocks.isEmpty()) {
return null;
}
return OccluderBoxes.from(Arrays.copyOf(this.blocks.toLongArray(), BoxFinder.INTERIOR_CACHE_WORDS));
}
private long computeWithFloodFill() { private long computeWithFloodFill() {
this.floodFilled = true;
long resultEncoding = 0; long resultEncoding = 0;
var blocks = this.blocks; var blocks = this.blocks;
BitSet escapedFaces = new BitSet(GraphDirection.COUNT); BitSet escapedFaces = new BitSet(GraphDirection.COUNT);
@@ -0,0 +1,62 @@
package org.embeddedt.embeddium.impl.render.chunk.occlusion.geometry;
import grondag.bitraster.PackedBox;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
public final class OccluderBoxes {
/** Below this a box costs a rasterizer draw for negligible screen coverage. */
private static final int MIN_VOLUME = 8;
/** {@link BoxFinder} is far too large to allocate per section; build threads are pooled */
private static final ThreadLocal<BoxFinder> FINDER = ThreadLocal.withInitial(() -> new BoxFinder(new AreaFinder()));
private OccluderBoxes() {
}
public static int @Nullable [] from(long[] opacityBits) {
BoxFinder finder = FINDER.get();
finder.findBoxes(opacityBits, 0);
var found = finder.boxes;
int count = found.size();
if (count == 0) {
return null;
}
int[] boxes = new int[count];
int kept = 0;
for (int range = PackedBox.RANGE_EXTREME; range >= PackedBox.RANGE_NEAR; range--) {
for (int i = 0; i < count; i++) {
int box = found.getInt(i);
if (PackedBox.range(box) == range && volume(box) >= MIN_VOLUME) {
boxes[kept++] = toWorldAxes(box);
}
}
}
if (kept == 0) {
return null;
}
return kept == count ? boxes : Arrays.copyOf(boxes, kept);
}
private static int toWorldAxes(int box) {
return PackedBox.pack(
PackedBox.x0(box), PackedBox.z0(box), PackedBox.y0(box),
PackedBox.x1(box), PackedBox.z1(box), PackedBox.y1(box),
PackedBox.range(box)
);
}
private static int volume(int box) {
return (PackedBox.x1(box) - PackedBox.x0(box))
* (PackedBox.y1(box) - PackedBox.y0(box))
* (PackedBox.z1(box) - PackedBox.z0(box));
}
}