raster fixes + cheap opts #40

Merged
embeddedt merged 4 commits from rdh/celeritas:raster-3 into stonecutter 2026-09-05 16:55:36 -04:00
7 changed files with 70 additions and 8 deletions

No files matched your search

@@ -122,6 +122,9 @@ public class OcclusionCuller {
private boolean isCameraInUnloadedSection; private boolean isCameraInUnloadedSection;
private boolean isMultiRootSearch; private boolean isMultiRootSearch;
// Lattice index of the camera section when it is the search root, else -1. It is visited inline, never tested.
private int cameraSectionIndex;
public OcclusionCuller(SectionLattice lattice, int minSectionY, int maxSectionY, boolean rasterOcclusion) { public OcclusionCuller(SectionLattice lattice, int minSectionY, int maxSectionY, boolean rasterOcclusion) {
this.lattice = lattice; this.lattice = lattice;
this.minSectionY = minSectionY; this.minSectionY = minSectionY;
@@ -180,6 +183,7 @@ public class OcclusionCuller {
this.isCameraInUnloadedSection = false; this.isCameraInUnloadedSection = false;
this.isMultiRootSearch = false; this.isMultiRootSearch = false;
this.cameraSectionIndex = -1;
this.init(visitor, viewport, searchDistance, useOcclusionCulling, frame); this.init(visitor, viewport, searchDistance, useOcclusionCulling, frame);
if (this.isCameraInUnloadedSection) { if (this.isCameraInUnloadedSection) {
useOcclusionCulling = false; useOcclusionCulling = false;
@@ -192,6 +196,7 @@ public class OcclusionCuller {
if (this.rasterActive) { if (this.rasterActive) {
this.rasterOccluder.prepareScene(frame, viewport, searchDistance); this.rasterOccluder.prepareScene(frame, viewport, searchDistance);
this.occludeCameraSection(viewport);
} }
this.process(visitor, viewport, searchDistance, useOcclusionCulling, allowFrustumClamping, frame); this.process(visitor, viewport, searchDistance, useOcclusionCulling, allowFrustumClamping, frame);
@@ -359,6 +364,25 @@ public class OcclusionCuller {
this.visibleCount = visibleCount; this.visibleCount = visibleCount;
} }
private void occludeCameraSection(Viewport viewport) {
int idx = this.cameraSectionIndex;
if (idx < 0) {
return;
}
long sm = this.lattice.sectionMeta[idx];
if (!PackedSectionMetadata.hasOccluderData(sm)
|| (PackedSectionMetadata.getVisualsFlags(sm) & (1 << RenderVisualsService.HAS_BLOCK_GEOMETRY)) == 0) {
return;
}
var origin = viewport.getChunkCoord();
this.rasterOccluder.occludeSectionAt(origin.x() << 4, origin.y() << 4, origin.z() << 4,
this.lattice.occluderData[idx]);
}
private RasterOccluder.SectionVisibility rasterTest(int[] occluderBounds, int[][] occluderData, private RasterOccluder.SectionVisibility rasterTest(int[] occluderBounds, int[][] occluderData,
int idx, int chunkX, int chunkY, int chunkZ, int camX, int camY, int camZ, int idx, int chunkX, int chunkY, int chunkZ, int camX, int camY, int camZ,
int meta, boolean hasOccluderData) { int meta, boolean hasOccluderData) {
@@ -573,6 +597,8 @@ public class OcclusionCuller {
var origin = viewport.getChunkCoord(); var origin = viewport.getChunkCoord();
int idx = this.lattice.indexOf(origin.x(), origin.y(), origin.z()); int idx = this.lattice.indexOf(origin.x(), origin.y(), origin.z());
this.cameraSectionIndex = idx;
// The camera section is loaded and, after ensureWindowCovers, installed // The camera section is loaded and, after ensureWindowCovers, installed
// in the lattice interior. // in the lattice interior.
long frameStamp = SectionLattice.frameStamp(frame); long frameStamp = SectionLattice.frameStamp(frame);
@@ -176,6 +176,12 @@ public final class RasterOccluder extends BoxOccluder {
} }
} }
/** Draws a section's occluder boxes without a preceding {@link #testSection}, for a section the search never tests. */
public void occludeSectionAt(int originX, int originY, int originZ, int[] data) {
prepareRegion(originX, originY, originZ, PackedBox.rangeFromSquareChunkDist(0), 0);
occludeSection(data);
}
/** Draws the section's occluder boxes for the region prepared by the last {@link #testSection}. */ /** Draws the section's occluder boxes for the region prepared by the last {@link #testSection}. */
public void occludeSection(int[] data) { public void occludeSection(int[] data) {
if (!AbstractRasterizer.STATS) { if (!AbstractRasterizer.STATS) {
@@ -202,7 +208,7 @@ public final class RasterOccluder extends BoxOccluder {
return SectionVisibility.VISIBLE; return SectionVisibility.VISIBLE;
} }
// tests are dilated by a pixel inside the rasterizer, so no world-space fuzz is needed // no world-space fuzz: RenderableBounds already carries the model extent
if (isBoxVisible(bounds, 0)) { if (isBoxVisible(bounds, 0)) {
return SectionVisibility.VISIBLE; return SectionVisibility.VISIBLE;
} }
@@ -24,7 +24,10 @@ final class RenderableBounds {
return PackedBox.FULL_BOX; return PackedBox.FULL_BOX;
} }
return PackedBox.pack(this.minX, this.minY, this.minZ, return PackedBox.pack(
this.maxX + 1, this.maxY + 1, this.maxZ + 1, PackedBox.RANGE_EXTREME); Math.max(0, this.minX - 1), Math.max(0, this.minY - 1), Math.max(0, this.minZ - 1),
Math.min(16, this.maxX + 2), Math.min(16, this.maxY + 2), Math.min(16, this.maxZ + 2),
PackedBox.RANGE_EXTREME
);
} }
} }
@@ -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 it.unimi.dsi.fastutil.longs.Long2ReferenceMap; 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.common.util.MathUtil; import org.embeddedt.embeddium.impl.common.util.MathUtil;
@@ -285,7 +286,7 @@ public final class SectionLattice {
// new boxes even when the packed metadata is otherwise unchanged. // new boxes even when the packed metadata is otherwise unchanged.
occluderChanged = this.occluderData[idx] != data; occluderChanged = this.occluderData[idx] != data;
this.occluderData[idx] = data; this.occluderData[idx] = data;
this.occluderBounds[idx] = boundsOf(data); this.occluderBounds[idx] = boundsOf(packedMetadata, data);
} }
return occluderChanged || ((previous ^ packedMetadata) & PackedSectionMetadata.GRAPH_INPUT_MASK) != 0; return occluderChanged || ((previous ^ packedMetadata) & PackedSectionMetadata.GRAPH_INPUT_MASK) != 0;
@@ -839,8 +840,18 @@ public final class SectionLattice {
return built != null ? built.occluderBoxes : null; return built != null ? built.occluderBoxes : null;
} }
private static int boundsOf(int @Nullable [] occluderData) { // A section awaiting a remesh may draw outside the bounds of the mesh it last built, so it takes the whole
return occluderData != null ? SectionVisibilityBuilder.bounds(occluderData) : 0; // section until the new one lands. Sorts are excluded, as they do not change geometry.
private static int boundsOf(long packedMetadata, int @Nullable [] occluderData) {
if (occluderData == null) {
return 0;
}
var pendingUpdate = PackedSectionMetadata.getPendingUpdate(packedMetadata);
return pendingUpdate != null && !pendingUpdate.isSort()
? PackedBox.FULL_BOX
: SectionVisibilityBuilder.bounds(occluderData);
} }
private void install(RenderSection section) { private void install(RenderSection section) {
@@ -866,7 +877,7 @@ public final class SectionLattice {
if (this.rasterOcclusion) { if (this.rasterOcclusion) {
int[] data = PackedSectionMetadata.hasOccluderData(packedMetadata) ? occluderDataOf(section) : null; int[] data = PackedSectionMetadata.hasOccluderData(packedMetadata) ? occluderDataOf(section) : null;
this.occluderData[idx] = data; this.occluderData[idx] = data;
this.occluderBounds[idx] = boundsOf(data); this.occluderBounds[idx] = boundsOf(packedMetadata, data);
} }
this.installedCount++; this.installedCount++;
@@ -71,6 +71,11 @@ public class SectionVisibilityBuilder {
this.renderableBounds.mark(x & 15, y & 15, z & 15); this.renderableBounds.mark(x & 15, y & 15, z & 15);
} }
public void markSectionRenderable() {
this.renderableBounds.mark(0, 0, 0);
this.renderableBounds.mark(SECTION_AXIS_MASK, SECTION_AXIS_MASK, SECTION_AXIS_MASK);
}
public int @Nullable [] computeOccluderBoxes() { public int @Nullable [] computeOccluderBoxes() {
return dataFrom(this.blocks, this.renderableBounds); return dataFrom(this.blocks, this.renderableBounds);
} }
@@ -57,6 +57,11 @@ public class BoxFinder {
clearSectionBits(); clearSectionBits();
voxelCount = voxelCount(source, 0); voxelCount = voxelCount(source, 0);
if (voxelCount == 0) {
return;
}
markBoxSlices(); markBoxSlices();
markBoxNeighborSlices(); markBoxNeighborSlices();
buildSortedBoxes(); buildSortedBoxes();
@@ -233,7 +233,13 @@ public class ChunkBuilderMeshingTask extends ChunkBuilderTask<ChunkBuildOutput>
} }
} }
MeshAppenderRenderer.renderMeshAppenders(renderContext.getMeshAppenders(), context.localSlice(), renderContext.getOrigin(), buffers); var meshAppenders = renderContext.getMeshAppenders();
if (this.rasterOcclusion && !meshAppenders.isEmpty()) {
occluder.markSectionRenderable();
}
MeshAppenderRenderer.renderMeshAppenders(meshAppenders, context.localSlice(), renderContext.getOrigin(), buffers);
} catch (ReportedException ex) { } catch (ReportedException ex) {
// Propagate existing crashes (add context) // Propagate existing crashes (add context)
throw fillCrashInfo(ex.getReport(), slice, blockPos); throw fillCrashInfo(ex.getReport(), slice, blockPos);