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 42 additions and 34 deletions
Showing only changes of commit b69525d7d9 - Show all commits

No files matched your search

@@ -89,7 +89,7 @@ public class MultiDrawBench {
this.syntheticWorld = new SyntheticWorld(this.world, this.renderDistance, SyntheticWorld.DEFAULT_SEED,
this.caveWidth, true, 0.0D, false, false);
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, false);
for (RenderSection section : this.syntheticWorld.getConstructionOrder()) {
this.lattice.attach(section);
@@ -89,7 +89,7 @@ public class ShadowOcclusionCullerBench {
this.numRegions = BenchPlatform.regionManager().getRegionIdsLength();
// hasShadowPass: allocates the second visit-state array the shadow search stamps.
this.lattice = new SectionLattice(SyntheticWorld.MIN_SECTION_Y, SyntheticWorld.MAX_SECTION_Y, true);
this.lattice = new SectionLattice(SyntheticWorld.MIN_SECTION_Y, SyntheticWorld.MAX_SECTION_Y, true, false);
for (RenderSection section : syntheticWorld.getConstructionOrder()) {
this.lattice.attach(section);
@@ -115,6 +115,8 @@ public final class SectionLattice {
// Non-null only when the lattice serves a shadow pass as well.
@Nullable
private final ShadowOcclusionCuller shadowCuller;
// Whether the raster occluder is in use; gates maintenance of occluderData/occluderBounds, which only it reads.
private final boolean rasterOcclusion;
private final int minSectionY, maxSectionY;
@@ -138,17 +140,9 @@ public final class SectionLattice {
long[] sectionMeta;
int[] regionOfCell;
/**
* Per-cell occluder data ({@code BuiltRenderSectionData.occluderBoxes}) mirrored here so the raster test
* does not chase section and build-data pointers for every cell it visits. Null when absent.
*/
int[][] occluderData;
int[] @Nullable [] occluderData;
/**
* The packed bounds word of each cell's occluder data, mirrored densely so the raster test reads one
* array instead of chasing the pointer above into a second cache line; only a draw needs the boxes.
*/
int[] occluderBounds;
int @Nullable [] occluderBounds;
RenderSection[] latticeSection;
@@ -204,10 +198,6 @@ public final class SectionLattice {
* @param hasShadowPass whether the lattice must also support {@link #findShadowVisible}, which costs a second
* visit-state array
*/
public SectionLattice(int minSectionY, int maxSectionY, boolean hasShadowPass) {
this(minSectionY, maxSectionY, hasShadowPass, false);
}
public SectionLattice(int minSectionY, int maxSectionY, boolean hasShadowPass, boolean rasterOcclusion) {
this.minSectionY = minSectionY;
this.maxSectionY = maxSectionY;
@@ -215,6 +205,7 @@ public final class SectionLattice {
this.dimY = (maxSectionY - minSectionY) + 2;
this.culler = new OcclusionCuller(this, minSectionY, maxSectionY, rasterOcclusion);
this.shadowCuller = hasShadowPass ? new ShadowOcclusionCuller(this) : null;
this.rasterOcclusion = rasterOcclusion;
if (this.dimY > MAX_DIM) {
throw new IllegalStateException("World height " + this.dimY + " exceeds occlusion lattice limit " + MAX_DIM);
@@ -285,9 +276,12 @@ public final class SectionLattice {
long previous = this.sectionMeta[idx];
this.sectionMeta[idx] = packedMetadata;
int[] data = PackedSectionMetadata.hasOccluderData(packedMetadata) ? occluderDataOf(this.latticeSection[idx]) : null;
this.occluderData[idx] = data;
this.occluderBounds[idx] = boundsOf(data);
if (this.rasterOcclusion) {
int[] data = PackedSectionMetadata.hasOccluderData(packedMetadata) ? occluderDataOf(this.latticeSection[idx]) : null;
this.occluderData[idx] = data;
this.occluderBounds[idx] = boundsOf(data);
}
return ((previous ^ packedMetadata) & PackedSectionMetadata.GRAPH_INPUT_MASK) != 0;
}
@@ -555,8 +549,8 @@ public final class SectionLattice {
this.sectionMeta = new long[slots];
this.regionOfCell = new int[slots];
this.latticeSection = new RenderSection[slots];
this.occluderData = new int[slots][];
this.occluderBounds = new int[slots];
this.occluderData = this.rasterOcclusion ? new int[slots][] : null;
this.occluderBounds = this.rasterOcclusion ? new int[slots] : null;
// Existing coordinates are no longer valid after resizing; the next
// ensureWindowCovers must rebase and reinstall every attached section.
@@ -575,8 +569,10 @@ public final class SectionLattice {
Arrays.fill(this.sectionMeta, VisibilityEncoding.NULL);
Arrays.fill(this.regionOfCell, -1);
Arrays.fill(this.latticeSection, null);
Arrays.fill(this.occluderData, null);
Arrays.fill(this.occluderBounds, 0);
if (this.rasterOcclusion) {
Arrays.fill(this.occluderData, null);
Arrays.fill(this.occluderBounds, 0);
}
this.baseX = newBaseX;
this.baseZ = newBaseZ;
@@ -656,8 +652,10 @@ public final class SectionLattice {
System.arraycopy(this.sectionMeta, srcOff, this.sectionMeta, destOff, len);
System.arraycopy(this.regionOfCell, srcOff, this.regionOfCell, destOff, len);
System.arraycopy(this.latticeSection, srcOff, this.latticeSection, destOff, len);
System.arraycopy(this.occluderData, srcOff, this.occluderData, destOff, len);
System.arraycopy(this.occluderBounds, srcOff, this.occluderBounds, destOff, len);
if (this.rasterOcclusion) {
System.arraycopy(this.occluderData, srcOff, this.occluderData, destOff, len);
System.arraycopy(this.occluderBounds, srcOff, this.occluderBounds, destOff, len);
}
}
/**
@@ -691,8 +689,10 @@ public final class SectionLattice {
System.arraycopy(this.sectionMeta, srcOff, this.sectionMeta, destOff, runLen);
System.arraycopy(this.regionOfCell, srcOff, this.regionOfCell, destOff, runLen);
System.arraycopy(this.latticeSection, srcOff, this.latticeSection, destOff, runLen);
System.arraycopy(this.occluderData, srcOff, this.occluderData, destOff, runLen);
System.arraycopy(this.occluderBounds, srcOff, this.occluderBounds, destOff, runLen);
if (this.rasterOcclusion) {
System.arraycopy(this.occluderData, srcOff, this.occluderData, destOff, runLen);
System.arraycopy(this.occluderBounds, srcOff, this.occluderBounds, destOff, runLen);
}
}
}
@@ -778,8 +778,10 @@ public final class SectionLattice {
this.sectionMeta[idx] = VisibilityEncoding.NULL;
this.regionOfCell[idx] = -1;
this.latticeSection[idx] = null;
this.occluderData[idx] = null;
this.occluderBounds[idx] = 0;
if (this.rasterOcclusion) {
this.occluderData[idx] = null;
this.occluderBounds[idx] = 0;
}
RenderSection section = this.sections.get(
PositionUtil.packSection(worldX, this.baseY + ly, worldZ));
@@ -855,9 +857,13 @@ public final class SectionLattice {
this.regionOfCell[idx] = section.getRegion().getId();
long packedMetadata = section.getPackedMetadata();
this.sectionMeta[idx] = packedMetadata;
int[] data = PackedSectionMetadata.hasOccluderData(packedMetadata) ? occluderDataOf(section) : null;
this.occluderData[idx] = data;
this.occluderBounds[idx] = boundsOf(data);
if (this.rasterOcclusion) {
int[] data = PackedSectionMetadata.hasOccluderData(packedMetadata) ? occluderDataOf(section) : null;
this.occluderData[idx] = data;
this.occluderBounds[idx] = boundsOf(data);
}
this.installedCount++;
}
@@ -877,8 +883,10 @@ public final class SectionLattice {
this.sectionMeta[idx] = VisibilityEncoding.NULL;
this.regionOfCell[idx] = -1;
this.latticeSection[idx] = null;
this.occluderData[idx] = null;
this.occluderBounds[idx] = 0;
if (this.rasterOcclusion) {
this.occluderData[idx] = null;
this.occluderBounds[idx] = 0;
}
this.installedCount--;
}
}