Major occlusion search optimizations #34

Merged
embeddedt merged 11 commits from occlusion-opts into stonecutter 2026-08-18 19:59:03 -04:00
6 changed files with 101 additions and 0 deletions
Showing only changes of commit 7704538a23 - Show all commits

No files matched your search

@@ -54,6 +54,16 @@ public class BoxCuller {
return maxZ < -this.maxDistance || minZ > this.maxDistance;
}
/**
* Camera-relative variant of a "fully inside" test: returns true only when the entire box lies within the
* allowed distance bounds on every axis, meaning no sub-box of it could ever be culled.
*/
public boolean isFullyInsideSodium(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
return minX >= -this.maxDistance && maxX <= this.maxDistance
&& minY >= -this.maxDistance && maxY <= this.maxDistance
&& minZ >= -this.maxDistance && maxZ <= this.maxDistance;
}
@Override
public String toString() {
return "Box Culling active; max distance " + maxDistance;
@@ -33,4 +33,9 @@ public class CullEverythingFrustum extends Frustum implements ViewportProvider,
public boolean testAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
return false;
}
@Override
public int intersectAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
return OUTSIDE;
}
}
@@ -387,6 +387,53 @@ public class AdvancedShadowCullingFrustum extends Frustum implements ViewportPro
return (boxCuller == null || !boxCuller.isCulledSodium(minX, minY, minZ, maxX, maxY, maxZ)) && this.checkCornerVisibility(minX, minY, minZ, maxX, maxY, maxZ) > 0;
}
/**
* Tri-state variant of {@link #checkCornerVisibility}, ported from JOML's {@code FrustumIntersection#intersectAab}.
* Unlike {@link #checkCornerVisibility}, this additionally tests the near corner against each plane so that boxes
* lying entirely within the frustum can be reported as fully inside.
*/
protected int intersectCorners(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
boolean inside = true;
for (int i = 0; i < planeCount; ++i) {
Vector4f plane = this.planes[i];
float px = plane.x();
float py = plane.y();
float pz = plane.z();
float pw = plane.w();
// If the far corner (the one furthest along the plane normal) is behind the plane, the box is fully outside.
if (Math.fma(px, px < 0 ? minX : maxX, Math.fma(py, py < 0 ? minY : maxY, pz * (pz < 0 ? minZ : maxZ))) < -pw) {
return OUTSIDE;
}
// If the near corner is behind the plane, the box straddles it and can be at most partially inside.
if (Math.fma(px, px < 0 ? maxX : minX, Math.fma(py, py < 0 ? maxY : minY, pz * (pz < 0 ? maxZ : minZ))) < -pw) {
inside = false;
}
}
return inside ? FULLY_INSIDE : PARTIALLY_INSIDE;
}
@Override
public int intersectAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
if (boxCuller != null && boxCuller.isCulledSodium(minX, minY, minZ, maxX, maxY, maxZ)) {
return OUTSIDE;
}
int result = intersectCorners(minX, minY, minZ, maxX, maxY, maxZ);
// A box can only be fully inside if it is also fully within the box culler's bounds; otherwise a sub-box could
// be culled and the region must be treated as partial.
if (result == FULLY_INSIDE && boxCuller != null
&& !boxCuller.isFullyInsideSodium(minX, minY, minZ, maxX, maxY, maxZ)) {
return PARTIALLY_INSIDE;
}
return result;
}
private final Vector3d position = new Vector3d();
@Override
@@ -58,4 +58,29 @@ public class ReversedAdvancedShadowCullingFrustum extends AdvancedShadowCullingF
return this.checkCornerVisibility(minX, minY, minZ, maxX, maxY, maxZ) > 0;
}
@Override
public int intersectAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
if (distanceCuller != null && distanceCuller.isCulledSodium(minX, minY, minZ, maxX, maxY, maxZ)) {
return OUTSIDE;
}
if (boxCuller != null && !boxCuller.isCulledSodium(minX, minY, minZ, maxX, maxY, maxZ)) {
// The box overlaps the kept voxel region, so it is visible. It is only fully inside when the entire box
// stays within both the voxel region and the outer distance bound, so no sub-box could be culled.
boolean fullyInside = boxCuller.isFullyInsideSodium(minX, minY, minZ, maxX, maxY, maxZ)
&& (distanceCuller == null || distanceCuller.isFullyInsideSodium(minX, minY, minZ, maxX, maxY, maxZ));
return fullyInside ? FULLY_INSIDE : PARTIALLY_INSIDE;
}
// Outside the voxel region: fall back to the shadow planes, still bounded by the distance culler.
int result = intersectCorners(minX, minY, minZ, maxX, maxY, maxZ);
if (result == FULLY_INSIDE && distanceCuller != null
&& !distanceCuller.isFullyInsideSodium(minX, minY, minZ, maxX, maxY, maxZ)) {
return PARTIALLY_INSIDE;
}
return result;
}
}
@@ -50,4 +50,13 @@ public class BoxCullingFrustum extends Frustum implements ViewportProvider, org.
public boolean testAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
return !boxCuller.isCulledSodium(minX, minY, minZ, maxX, maxY, maxZ);
}
@Override
public int intersectAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
if (boxCuller.isCulledSodium(minX, minY, minZ, maxX, maxY, maxZ)) {
return OUTSIDE;
}
return boxCuller.isFullyInsideSodium(minX, minY, minZ, maxX, maxY, maxZ) ? FULLY_INSIDE : PARTIALLY_INSIDE;
}
}
@@ -37,4 +37,9 @@ public class NonCullingFrustum extends Frustum implements ViewportProvider, org.
public boolean testAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
return true;
}
@Override
public int intersectAab(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
return FULLY_INSIDE;
}
}