rasterized occlusion culling #38

Merged
embeddedt merged 20 commits from rdh/celeritas:raster into stonecutter 2026-09-04 22:15:05 -04:00
6 changed files with 68 additions and 19 deletions
Showing only changes of commit 74c6cf0ffd - Show all commits

No files matched your search

@@ -45,6 +45,9 @@ public class OcclusionCullerBench {
@Param({ "true" }) @Param({ "true" })
public boolean scatteredAlloc; public boolean scatteredAlloc;
@Param({ "false" })
public boolean rasterOcclusion;
public SyntheticWorld syntheticWorld; public SyntheticWorld syntheticWorld;
public SectionLattice lattice; public SectionLattice lattice;
@@ -62,12 +65,14 @@ public class OcclusionCullerBench {
public void setup() { public void setup() {
BenchPlatform.ensureInitialized(); BenchPlatform.ensureInitialized();
this.syntheticWorld = new SyntheticWorld(this.world, this.renderDistance, this.scatteredAlloc); this.syntheticWorld = new SyntheticWorld(this.world, this.renderDistance, this.scatteredAlloc,
this.rasterOcclusion);
this.searchDistance = this.syntheticWorld.getSearchDistance(); this.searchDistance = this.syntheticWorld.getSearchDistance();
this.numRegions = BenchPlatform.regionManager().getRegionIdsLength(); this.numRegions = BenchPlatform.regionManager().getRegionIdsLength();
this.lattice = new SectionLattice(SyntheticWorld.MIN_SECTION_Y, SyntheticWorld.MAX_SECTION_Y, false); this.lattice = new SectionLattice(SyntheticWorld.MIN_SECTION_Y, SyntheticWorld.MAX_SECTION_Y, false,
this.rasterOcclusion);
for (RenderSection section : this.syntheticWorld.getConstructionOrder()) { for (RenderSection section : this.syntheticWorld.getConstructionOrder()) {
this.lattice.attach(section); this.lattice.attach(section);
@@ -4,7 +4,6 @@ import it.unimi.dsi.fastutil.longs.Long2ReferenceMap;
import it.unimi.dsi.fastutil.longs.Long2ReferenceOpenHashMap; import it.unimi.dsi.fastutil.longs.Long2ReferenceOpenHashMap;
import org.embeddedt.embeddium.impl.render.chunk.ChunkUpdateType; import org.embeddedt.embeddium.impl.render.chunk.ChunkUpdateType;
import org.embeddedt.embeddium.impl.render.chunk.RenderSection; import org.embeddedt.embeddium.impl.render.chunk.RenderSection;
import org.embeddedt.embeddium.impl.render.chunk.occlusion.geometry.OccluderBoxes;
import org.embeddedt.embeddium.impl.render.chunk.bench.voxel.VoxelWorld; import org.embeddedt.embeddium.impl.render.chunk.bench.voxel.VoxelWorld;
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.region.RenderRegionManager; import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegionManager;
@@ -284,7 +283,7 @@ public final class SyntheticWorld {
int index = gridIndex(x, y, z, radius, width); int index = gridIndex(x, y, z, radius, width);
if (occluderBoxes != null) { if (occluderBoxes != null) {
occluderBoxes[index] = OccluderBoxes.from(slice.opacityBits()); occluderBoxes[index] = slice.occlusionData();
} }
visibility[index] = slice.computeVisibility(); visibility[index] = slice.computeVisibility();
@@ -1,5 +1,6 @@
package org.embeddedt.embeddium.impl.render.chunk.occlusion; package org.embeddedt.embeddium.impl.render.chunk.occlusion;
import grondag.bitraster.PackedBox;
import org.embeddedt.embeddium.impl.common.util.MathUtil; import org.embeddedt.embeddium.impl.common.util.MathUtil;
import org.embeddedt.embeddium.impl.render.chunk.LocalSectionIndex; import org.embeddedt.embeddium.impl.render.chunk.LocalSectionIndex;
import org.embeddedt.embeddium.impl.render.chunk.PackedSectionMetadata; import org.embeddedt.embeddium.impl.render.chunk.PackedSectionMetadata;
@@ -297,18 +298,22 @@ public class OcclusionCuller {
visible = aperture != FastFrustumClamping.EMPTY; visible = aperture != FastFrustumClamping.EMPTY;
} }
boolean traverse = visible;
if (this.rasterActive && visible) { if (this.rasterActive && visible) {
visible = this.rasterTest(idx, chunkX, chunkY, chunkZ, camX, camY, camZ, compactMeta); RasterOccluder.SectionVisibility result = this.rasterTest(idx, chunkX, chunkY, chunkZ, camX, camY, camZ, compactMeta);
visible = result == RasterOccluder.SectionVisibility.VISIBLE;
traverse = result != RasterOccluder.SectionVisibility.HIDDEN;
} }
int sectionIndex = LocalSectionIndex.pack(chunkX, chunkY, chunkZ); int sectionIndex = LocalSectionIndex.pack(chunkX, chunkY, chunkZ);
visitor.visit(idx, regionId, sectionIndex, compactMeta, visible); visitor.visit(idx, regionId, sectionIndex, compactMeta, visible);
if (!visible) { if (!traverse) {
continue; continue;
} }
if (visibleCells != null) { if (visible && visibleCells != null) {
visibleCells[visibleCount++] = entry; visibleCells[visibleCount++] = entry;
} }
@@ -351,28 +356,29 @@ public class OcclusionCuller {
this.visibleCount = visibleCount; this.visibleCount = visibleCount;
} }
private boolean rasterTest(int idx, int chunkX, int chunkY, int chunkZ, int camX, int camY, int camZ, int meta) { private RasterOccluder.SectionVisibility rasterTest(int idx, int chunkX, int chunkY, int chunkZ, int camX, int camY, int camZ, int meta) {
int dx = chunkX - camX; int dx = chunkX - camX;
int dy = chunkY - camY; int dy = chunkY - camY;
int dz = chunkZ - camZ; int dz = chunkZ - camZ;
int squaredChunkDist = (dx * dx) + (dy * dy) + (dz * dz); int squaredChunkDist = (dx * dx) + (dy * dy) + (dz * dz);
var occluder = this.rasterOccluder; boolean hasGeometry =
(PackedSectionMetadata.getCompactVisualsFlags(meta) & (1 << RenderVisualsService.HAS_BLOCK_GEOMETRY)) != 0;
if (!occluder.isSectionVisible(chunkX << 4, chunkY << 4, chunkZ << 4, squaredChunkDist)) {
return false;
}
if ((PackedSectionMetadata.getCompactVisualsFlags(meta) & (1 << RenderVisualsService.HAS_BLOCK_GEOMETRY)) != 0) {
var section = this.lattice.latticeSection[idx]; var section = this.lattice.latticeSection[idx];
var built = section != null ? section.getBuiltContext() : null; var built = section != null ? section.getBuiltContext() : null;
int[] data = built != null ? built.occluderBoxes : null;
if (built != null && built.occluderBoxes != null) { int bounds = data != null ? OccluderBoxBuilder.bounds(data) : PackedBox.FULL_BOX;
occluder.drawOccluders(built.occluderBoxes, squaredChunkDist);
} var occluder = this.rasterOccluder;
RasterOccluder.SectionVisibility result = occluder.testSection(chunkX << 4, chunkY << 4, chunkZ << 4, squaredChunkDist, bounds);
if (result == RasterOccluder.SectionVisibility.VISIBLE && hasGeometry && data != null) {
occluder.occlude(data);
} }
return true; return result;
} }
public int rasterBacktrackCount() { public int rasterBacktrackCount() {
@@ -0,0 +1,30 @@
package org.embeddedt.embeddium.impl.render.chunk.occlusion;
import grondag.bitraster.PackedBox;
final class RenderableBounds {
private int minX = 16, minY = 16, minZ = 16;
private int maxX = -1, maxY = -1, maxZ = -1;
void mark(int x, int y, int z) {
if (x < this.minX) this.minX = x;
if (y < this.minY) this.minY = y;
if (z < this.minZ) this.minZ = z;
if (x > this.maxX) this.maxX = x;
if (y > this.maxY) this.maxY = y;
if (z > this.maxZ) this.maxZ = z;
}
boolean isEmpty() {
return this.maxX < 0;
}
int packed() {
if (isEmpty()) {
return PackedBox.FULL_BOX;
}
return PackedBox.pack(this.minX, this.minY, this.minZ,
this.maxX + 1, this.maxY + 1, this.maxZ + 1, PackedBox.RANGE_EXTREME);
}
}
@@ -26,6 +26,7 @@ public class SectionVisibilityBuilder {
private static final int[] INDICES_TO_INITIATE_FLOODFILL = buildFloodfillIndices(); private static final int[] INDICES_TO_INITIATE_FLOODFILL = buildFloodfillIndices();
private final BitSet blocks; private final BitSet blocks;
private final RenderableBounds renderableBounds = new RenderableBounds();
private boolean floodFilled; private boolean floodFilled;
@@ -65,12 +66,16 @@ public class SectionVisibilityBuilder {
this.blocks.set(getIndex(x & 15, y & 15, z & 15)); this.blocks.set(getIndex(x & 15, y & 15, z & 15));
} }
public void markRenderable(int x, int y, int z) {
this.renderableBounds.mark(x & 15, y & 15, z & 15);
}
public int @Nullable [] computeOccluderBoxes() { public int @Nullable [] computeOccluderBoxes() {
if (this.floodFilled) { if (this.floodFilled) {
throw new IllegalStateException("opacity was consumed by computeVisibilityEncoding()"); throw new IllegalStateException("opacity was consumed by computeVisibilityEncoding()");
} }
return OccluderBoxBuilder.boxesFrom(this.blocks); return OccluderBoxBuilder.dataFrom(this.blocks, this.renderableBounds);
} }
private long computeWithFloodFill() { private long computeWithFloodFill() {
@@ -137,6 +137,10 @@ public class ChunkBuilderMeshingTask extends ChunkBuilderTask<ChunkBuildOutput>
blockPos.set(x, y, z); blockPos.set(x, y, z);
modelOffset.set(x & 15, y & 15, z & 15); modelOffset.set(x & 15, y & 15, z & 15);
if (occluderBoxes != null) {
occluderBoxes.markRenderable(x, y, z);
}
//? if shaders && >=1.17 { //? if shaders && >=1.17 {
if (voxelizingLight && blockState.getBlock() instanceof net.minecraft.world.level.block.LightBlock) { if (voxelizingLight && blockState.getBlock() instanceof net.minecraft.world.level.block.LightBlock) {
cache.getSpecialBlockRenderer().voxelizeLightBlock(blockPos, blockState, buffers); cache.getSpecialBlockRenderer().voxelizeLightBlock(blockPos, blockState, buffers);