rasterized occlusion culling #38

Merged
embeddedt merged 20 commits from rdh/celeritas:raster into stonecutter 2026-09-04 22:15:05 -04:00
4 changed files with 31 additions and 23 deletions
Showing only changes of commit 2545927343 - Show all commits

No files matched your search

@@ -15,10 +15,10 @@ import org.jetbrains.annotations.Nullable;
*
* <p>Bit layout:
* <pre>
* 63 53 52 51 49 48 46 45 0
* +--------------------+--+-----------+----------+-------------------+
* | spare |IF| pending | visuals | visibility graph |
* +--------------------+--+-----------+----------+-------------------+
* 63 54 53 52 51 49 48 46 45 0
* +--------------------+--+--+-----------+----------+-------------------+
* | spare |OD|IF| pending | visuals | visibility graph |
* +--------------------+--+--+-----------+----------+-------------------+
* </pre>
* <ul>
* <li>Bits {@code 0..45}: {@link VisibilityEncoding} entries. Entry
@@ -30,7 +30,8 @@ import org.jetbrains.annotations.Nullable;
* <li>Bits {@code 49..51}: pending update type; zero means none, otherwise
* {@code ChunkUpdateType.ordinal() + 1}.</li>
* <li>Bit {@code 52}: a build is in flight.</li>
* <li>Bits {@code 53..63}: reserved.</li>
* <li>Bit {@code 53}: the section's built data has occluder boxes.</li>
* <li>Bits {@code 54..63}: reserved.</li>
* </ul>
*
* <p>The visibility graph uses only bits {@code 0..45}, but
@@ -68,10 +69,13 @@ public final class PackedSectionMetadata {
private static final long PENDING_UPDATE_MASK = 0b111L;
// Bit 52: a build cancellation token is currently attached.
// Bit 53 is reserved for SectionLattice.OCCLUDER_DATA_FLAG on the lattice's copy of the word.
private static final int BUILD_IN_FLIGHT_BIT = 52;
private static final long BUILD_IN_FLIGHT_FLAG = 1L << BUILD_IN_FLIGHT_BIT;
// Bit 53: the section's built data has occluder boxes.
private static final int HAS_OCCLUDER_DATA_BIT = 53;
private static final long HAS_OCCLUDER_DATA_FLAG = 1L << HAS_OCCLUDER_DATA_BIT;
/** Returns only the visibility graph field. */
public static long getVisibilityData(long packed) {
return packed & VISIBILITY_MASK;
@@ -126,6 +130,18 @@ public final class PackedSectionMetadata {
return (packed & BUILD_IN_FLIGHT_FLAG) != 0;
}
/** Returns whether the section's built data has occluder boxes. */
public static boolean hasOccluderData(long packed) {
return (packed & HAS_OCCLUDER_DATA_FLAG) != 0;
}
/**
* Replaces the has-occluder-data flag and preserves every other field.
*/
public static long withHasOccluderData(long packed, boolean hasOccluderData) {
return hasOccluderData ? (packed | HAS_OCCLUDER_DATA_FLAG) : (packed & ~HAS_OCCLUDER_DATA_FLAG);
}
/*
* Compact collector metadata: includes only the info the VisibleChunkCollector cares about.
*
@@ -143,9 +143,10 @@ public class RenderSection extends AbstractSection {
public void updateCachedContextDataFlags() {
int flags = this.contextData != null ? this.contextData.getVisualBitmaskForSection() : 0;
long visibilityData = this.contextData != null ? this.contextData.visibilityData : VisibilityEncoding.NULL;
boolean hasOccluderData = this.contextData != null && this.contextData.occluderBoxes != null;
this.writeMetadata(PackedSectionMetadata.withVisibilityData(
PackedSectionMetadata.withVisualsFlags(this.packedMetadata, flags), visibilityData));
this.writeMetadata(PackedSectionMetadata.withHasOccluderData(PackedSectionMetadata.withVisibilityData(
PackedSectionMetadata.withVisualsFlags(this.packedMetadata, flags), visibilityData), hasOccluderData));
}
// The sole writer of packedMetadata: stores the new word and notifies the mirror.
@@ -303,7 +303,7 @@ public class OcclusionCuller {
if (this.rasterActive && visible) {
RasterOccluder.SectionVisibility result = this.rasterTest(idx, chunkX, chunkY, chunkZ, camX, camY, camZ,
compactMeta, (sm & SectionLattice.OCCLUDER_DATA_FLAG) != 0, regionId, regionX, regionY, regionZ);
compactMeta, PackedSectionMetadata.hasOccluderData(sm), regionId, regionX, regionY, regionZ);
visible = result == RasterOccluder.SectionVisibility.VISIBLE;
traverse = result != RasterOccluder.SectionVisibility.HIDDEN;
}
@@ -150,12 +150,6 @@ public final class SectionLattice {
*/
int[] occluderBounds;
/**
* Set in a cell's {@link #sectionMeta} word when it has occluder data, so the raster test can skip the
* pointer load for the many cells, most of them sky, that have none. Bit 53 is outside every field of
* {@link PackedSectionMetadata}, and every reader masks the fields it wants.
*/
static final long OCCLUDER_DATA_FLAG = 1L << 53;
RenderSection[] latticeSection;
// Queue entries ((packedLocalXYZ << 32) | latticeIndex) of the cells the last main search reported visible, in
@@ -290,8 +284,8 @@ public final class SectionLattice {
}
long previous = this.sectionMeta[idx];
int[] data = occluderDataOf(this.latticeSection[idx]);
this.sectionMeta[idx] = withOccluderFlag(packedMetadata, data);
this.sectionMeta[idx] = packedMetadata;
int[] data = PackedSectionMetadata.hasOccluderData(packedMetadata) ? occluderDataOf(this.latticeSection[idx]) : null;
this.occluderData[idx] = data;
this.occluderBounds[idx] = boundsOf(data);
@@ -842,10 +836,6 @@ public final class SectionLattice {
return occluderData != null ? OccluderBoxBuilder.bounds(occluderData) : 0;
}
private static long withOccluderFlag(long packedMetadata, int @Nullable [] occluderData) {
return occluderData != null ? packedMetadata | OCCLUDER_DATA_FLAG : packedMetadata;
}
private void install(RenderSection section) {
if (this.visitState == null) {
return;
@@ -863,8 +853,9 @@ public final class SectionLattice {
}
this.latticeSection[idx] = section;
this.regionOfCell[idx] = section.getRegion().getId();
int[] data = occluderDataOf(section);
this.sectionMeta[idx] = withOccluderFlag(section.getPackedMetadata(), data);
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);
this.installedCount++;