diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/FastFrustumClamping.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/FastFrustumClamping.java new file mode 100644 index 000000000..126c0f62c --- /dev/null +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/FastFrustumClamping.java @@ -0,0 +1,100 @@ +package org.embeddedt.embeddium.impl.render.chunk.occlusion; + +// A packed aperture stores xy, xz, and yz lower/upper ranks in six 10-bit lanes, low to high. +final class FastFrustumClamping { + private static final int TABLE_SIZE = 64; + private static final int TARGET_LIMIT = TABLE_SIZE - 2; + private static final int RANK_BITS = 10; + private static final int RANK_MASK = (1 << RANK_BITS) - 1; + private static final double HALF_PI = Math.PI / 2.0; + private static final long[] BOUNDS = new long[TABLE_SIZE * TABLE_SIZE]; + + static final long EMPTY = -1L; + static final long FULL = pack(0, RANK_MASK, 0, RANK_MASK, 0, RANK_MASK); + + static { + for (int x = 0; x < TABLE_SIZE; x++) { + for (int y = 0; y < TABLE_SIZE; y++) { + BOUNDS[index(x, y)] = pack( + rankDown(angle(x + 1, y - 1)), + rankUp(angle(x - 1, y + 1)), + 0, 0, 0, 0 + ); + } + } + } + + // Intersects each projected interval with the target cell's conservative angular bounds. + static long clip(long aperture, int x, int y, int z) { + while (Math.max(x, Math.max(y, z)) > TARGET_LIMIT) { + x >>>= 1; + y >>>= 1; + z >>>= 1; + } + + long xy = BOUNDS[index(x, y)]; + long xz = BOUNDS[index(x, z)]; + long yz = BOUNDS[index(y, z)]; + long result = pack( + Math.max(lower(aperture, 0), lower(xy, 0)), Math.min(upper(aperture, 0), upper(xy, 0)), + Math.max(lower(aperture, 1), lower(xz, 0)), Math.min(upper(aperture, 1), upper(xz, 0)), + Math.max(lower(aperture, 2), lower(yz, 0)), Math.min(upper(aperture, 2), upper(yz, 0))); + + return isEmpty(result) ? EMPTY : result; + } + + // Forms the per-plane hull used when multiple portal paths reach the same cell. + static long hull(long left, long right) { + return pack( + Math.min(lower(left, 0), lower(right, 0)), Math.max(upper(left, 0), upper(right, 0)), + Math.min(lower(left, 1), lower(right, 1)), Math.max(upper(left, 1), upper(right, 1)), + Math.min(lower(left, 2), lower(right, 2)), Math.max(upper(left, 2), upper(right, 2)) + ); + } + + // Rejects a path as soon as one of its three projected intervals has no overlap. + private static boolean isEmpty(long aperture) { + return lower(aperture, 0) > upper(aperture, 0) + || lower(aperture, 1) > upper(aperture, 1) + || lower(aperture, 2) > upper(aperture, 2); + } + + // Maps a projected cell corner to the first-quadrant angle used by the rank table. + private static double angle(int run, int rise) { + return Math.max(0.0, Math.min(HALF_PI, Math.atan2(rise, run))); + } + + // Rounds a lower angular bound down so rank quantization cannot hide a visible path. + private static int rankDown(double angle) { + return Math.max(0, Math.min(RANK_MASK, (int) Math.floor(angle * RANK_MASK / HALF_PI))); + } + + // Rounds an upper angular bound up so rank quantization cannot hide a visible path. + private static int rankUp(double angle) { + return Math.max(0, Math.min(RANK_MASK, (int) Math.ceil(angle * RANK_MASK / HALF_PI))); + } + + private static int index(int x, int y) { + return x * TABLE_SIZE + y; + } + + // Reads one plane's lower endpoint from the packed aperture. + private static int lower(long aperture, int plane) { + return (int) (aperture >>> (plane * 2 * RANK_BITS) & RANK_MASK); + } + + // Reads one plane's upper endpoint from the packed aperture. + private static int upper(long aperture, int plane) { + return (int) (aperture >>> ((plane * 2 + 1) * RANK_BITS) & RANK_MASK); + } + + // Packs all three projected intervals for hot-path min/max operations. + private static long pack(int xyLower, int xyUpper, int xzLower, int xzUpper, int yzLower, int yzUpper) { + return (long) xyLower + | ((long) xyUpper << RANK_BITS) + | ((long) xzLower << (2 * RANK_BITS)) + | ((long) xzUpper << (3 * RANK_BITS)) + | ((long) yzLower << (4 * RANK_BITS)) + | ((long) yzUpper << (5 * RANK_BITS)); + } +} diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java index 9cd81db42..d356eb329 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/render/chunk/occlusion/OcclusionCuller.java @@ -48,6 +48,12 @@ public class OcclusionCuller { // When stepping from a cell in dir, the neighbour is entered from the opposite face. private static final int[] INCOMING = new int[GraphDirection.COUNT]; + private static final long X_OPPOSITE_FACE_PAIRS = (1L << VisibilityEncoding.bit(GraphDirection.WEST, GraphDirection.EAST)) + | (1L << VisibilityEncoding.bit(GraphDirection.EAST, GraphDirection.WEST)); + private static final long Y_OPPOSITE_FACE_PAIRS = (1L << VisibilityEncoding.bit(GraphDirection.DOWN, GraphDirection.UP)) + | (1L << VisibilityEncoding.bit(GraphDirection.UP, GraphDirection.DOWN)); + private static final long Z_OPPOSITE_FACE_PAIRS = (1L << VisibilityEncoding.bit(GraphDirection.NORTH, GraphDirection.SOUTH)) + | (1L << VisibilityEncoding.bit(GraphDirection.SOUTH, GraphDirection.NORTH)); static { for (int dir = 0; dir < GraphDirection.COUNT; dir++) { @@ -65,6 +71,7 @@ public class OcclusionCuller { */ private long[] queue = new long[256]; private int tail; + private long[] apertures = new long[0]; @@ -72,6 +79,7 @@ public class OcclusionCuller { private final RegionCullCache regionCullCache = new RegionCullCache(); private boolean isCameraInUnloadedSection; + private boolean useFastFrustumClamping; public OcclusionCuller(SectionLattice lattice, int minSectionY, int maxSectionY) { this.lattice = lattice; @@ -109,12 +117,13 @@ public class OcclusionCuller { this.regionCullCache.begin(viewport, searchDistance, numRegions); this.isCameraInUnloadedSection = false; + this.useFastFrustumClamping = false; this.init(visitor, viewport, searchDistance, useOcclusionCulling, frame); if (this.isCameraInUnloadedSection) { useOcclusionCulling = false; } - this.process(visitor, viewport, searchDistance, useOcclusionCulling, frame); + this.process(visitor, viewport, searchDistance, useOcclusionCulling, frame, this.useFastFrustumClamping); } /** @@ -129,13 +138,15 @@ public class OcclusionCuller { Viewport viewport, float searchDistance, boolean useOcclusionCulling, - int frame) + int frame, + boolean useFastFrustumClamping) { final long[] visitState = this.lattice.visitState; final long[] sectionMeta = this.lattice.sectionMeta; final int[] regionOfCell = this.lattice.regionOfCell; final int[] delta = this.lattice.delta; final long[] queue = this.queue; + final long[] apertures = this.apertures; final int baseX = this.lattice.baseX, baseY = this.lattice.baseY, baseZ = this.lattice.baseZ; final RegionCullCache cache = this.regionCullCache; @@ -193,55 +204,66 @@ public class OcclusionCuller { int connections; if (useOcclusionCulling) { - // Merge the outgoing paths reachable from every incoming path - // accumulated in this cell. int incoming = (int) (visitState[idx] & DIR_MASK); - connections = VisibilityEncoding.getConnections( - sm & PackedSectionMetadata.VISIBILITY_MASK, incoming); + connections = getAngleRefinedConnections(sm & PackedSectionMetadata.VISIBILITY_MASK, incoming, + getOutwardDirections(chunkX, chunkY, chunkZ, camX, camY, camZ), + transform, chunkX, chunkY, chunkZ); } else { connections = GraphDirectionSet.ALL; + // We can only traverse outwards from the centre of the search. + connections &= getOutwardDirections(chunkX, chunkY, chunkZ, camX, camY, camZ); } - // We can only traverse outwards from the centre of the search. - connections &= getOutwardDirections(chunkX, chunkY, chunkZ, camX, camY, camZ); - - // Unrolled variant of visitNeighbors, with delta, step, incoming values inlined into each branch. - // The unrolling measurably improves performance here. - if ((connections & (1 << GraphDirection.DOWN)) != 0) { - final int n = idx - 1; - long ns = visitState[n]; - if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz - yStep) << 32) | (n & 0xFFFFFFFFL); } - visitState[n] = ns | (1 << GraphDirection.UP); - } - if ((connections & (1 << GraphDirection.UP)) != 0) { - final int n = idx + 1; - long ns = visitState[n]; - if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz + yStep) << 32) | (n & 0xFFFFFFFFL); } - visitState[n] = ns | (1 << GraphDirection.DOWN); - } - if ((connections & (1 << GraphDirection.NORTH)) != 0) { - final int n = idx - strideZ; - long ns = visitState[n]; - if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz - 1) << 32) | (n & 0xFFFFFFFFL); } - visitState[n] = ns | (1 << GraphDirection.SOUTH); - } - if ((connections & (1 << GraphDirection.SOUTH)) != 0) { - final int n = idx + strideZ; - long ns = visitState[n]; - if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz + 1) << 32) | (n & 0xFFFFFFFFL); } - visitState[n] = ns | (1 << GraphDirection.NORTH); - } - if ((connections & (1 << GraphDirection.WEST)) != 0) { - final int n = idx - strideX; - long ns = visitState[n]; - if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz - xStep) << 32) | (n & 0xFFFFFFFFL); } - visitState[n] = ns | (1 << GraphDirection.EAST); - } - if ((connections & (1 << GraphDirection.EAST)) != 0) { - final int n = idx + strideX; - long ns = visitState[n]; - if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz + xStep) << 32) | (n & 0xFFFFFFFFL); } - visitState[n] = ns | (1 << GraphDirection.WEST); + if (useFastFrustumClamping) { + long parentAperture = apertures[idx]; + int relativeX = Math.abs(chunkX - camX); + int relativeY = Math.abs(chunkY - camY); + int relativeZ = Math.abs(chunkZ - camZ); + if ((connections & (1 << GraphDirection.DOWN)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, parentAperture, idx - 1, xyz - yStep, 1 << GraphDirection.UP, frameStamp, relativeX, relativeY + 1, relativeZ, tail); + if ((connections & (1 << GraphDirection.UP)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, parentAperture, idx + 1, xyz + yStep, 1 << GraphDirection.DOWN, frameStamp, relativeX, relativeY + 1, relativeZ, tail); + if ((connections & (1 << GraphDirection.NORTH)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, parentAperture, idx - strideZ, xyz - 1, 1 << GraphDirection.SOUTH, frameStamp, relativeX, relativeY, relativeZ + 1, tail); + if ((connections & (1 << GraphDirection.SOUTH)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, parentAperture, idx + strideZ, xyz + 1, 1 << GraphDirection.NORTH, frameStamp, relativeX, relativeY, relativeZ + 1, tail); + if ((connections & (1 << GraphDirection.WEST)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, parentAperture, idx - strideX, xyz - xStep, 1 << GraphDirection.EAST, frameStamp, relativeX + 1, relativeY, relativeZ, tail); + if ((connections & (1 << GraphDirection.EAST)) != 0) tail = this.visitFfcNode(visitState, apertures, queue, parentAperture, idx + strideX, xyz + xStep, 1 << GraphDirection.WEST, frameStamp, relativeX + 1, relativeY, relativeZ, tail); + } else { + // Unrolled variant of visitNeighbors, with delta, step, incoming values inlined into each branch. + // The unrolling measurably improves performance here. + if ((connections & (1 << GraphDirection.DOWN)) != 0) { + final int n = idx - 1; + long ns = visitState[n]; + if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz - yStep) << 32) | (n & 0xFFFFFFFFL); } + visitState[n] = ns | (1 << GraphDirection.UP); + } + if ((connections & (1 << GraphDirection.UP)) != 0) { + final int n = idx + 1; + long ns = visitState[n]; + if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz + yStep) << 32) | (n & 0xFFFFFFFFL); } + visitState[n] = ns | (1 << GraphDirection.DOWN); + } + if ((connections & (1 << GraphDirection.NORTH)) != 0) { + final int n = idx - strideZ; + long ns = visitState[n]; + if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz - 1) << 32) | (n & 0xFFFFFFFFL); } + visitState[n] = ns | (1 << GraphDirection.SOUTH); + } + if ((connections & (1 << GraphDirection.SOUTH)) != 0) { + final int n = idx + strideZ; + long ns = visitState[n]; + if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz + 1) << 32) | (n & 0xFFFFFFFFL); } + visitState[n] = ns | (1 << GraphDirection.NORTH); + } + if ((connections & (1 << GraphDirection.WEST)) != 0) { + final int n = idx - strideX; + long ns = visitState[n]; + if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz - xStep) << 32) | (n & 0xFFFFFFFFL); } + visitState[n] = ns | (1 << GraphDirection.EAST); + } + if ((connections & (1 << GraphDirection.EAST)) != 0) { + final int n = idx + strideX; + long ns = visitState[n]; + if (ns < frameStamp) { ns = frameStamp; queue[tail++] = ((long) (xyz + xStep) << 32) | (n & 0xFFFFFFFFL); } + visitState[n] = ns | (1 << GraphDirection.WEST); + } } } @@ -277,6 +299,25 @@ public class OcclusionCuller { } } + private int visitFfcNode(long[] visitState, long[] apertures, long[] queue, long parentAperture, int idx, int xyz, int incoming, long frameStamp, + int relativeX, int relativeY, int relativeZ, int tail) { + long aperture = FastFrustumClamping.clip(parentAperture, relativeX, relativeY, relativeZ); + if (aperture == FastFrustumClamping.EMPTY) { + return tail; + } + + long state = visitState[idx]; + if (state < frameStamp) { + apertures[idx] = aperture; + visitState[idx] = frameStamp | incoming; + queue[tail++] = ((long) xyz << 32) | (idx & 0xFFFFFFFFL); + } else if (state != SectionLattice.SENTINEL) { + apertures[idx] = FastFrustumClamping.hull(apertures[idx], aperture); + visitState[idx] = state | incoming; + } + return tail; + } + // Allow movement only away from the camera on each axis. A coordinate // equal to the camera coordinate permits both directions on that axis. private static int getOutwardDirections(int chunkX, int chunkY, int chunkZ, int camX, int camY, int camZ) { @@ -294,6 +335,26 @@ public class OcclusionCuller { return planes; } + static int getAngleRefinedConnections(long visibilityData, int incoming, int outward, CameraTransform camera, int chunkX, int chunkY, int chunkZ) { + return VisibilityEncoding.getConnections(visibilityData & getAngleRefinementMask(camera, chunkX, chunkY, chunkZ), incoming) & outward; + } + + static long getAngleRefinementMask(CameraTransform camera, int chunkX, int chunkY, int chunkZ) { + double centreX = chunkX * 16.0 + 8.0; + double centreY = chunkY * 16.0 + 8.0; + double centreZ = chunkZ * 16.0 + 8.0; + double dx = Math.abs(camera.x - centreX); + double dy = Math.abs(camera.y - centreY); + double dz = Math.abs(camera.z - centreZ); + long mask = VisibilityEncoding.EVERYTHING; + + if (dy > dx || dz > dx) mask &= ~X_OPPOSITE_FACE_PAIRS; + if (dx > dy || dz > dy) mask &= ~Y_OPPOSITE_FACE_PAIRS; + if (dx > dz || dy > dz) mask &= ~Z_OPPOSITE_FACE_PAIRS; + + return mask; + } + // Convert a section coordinate to the origin of its containing render // region, in block coordinates. private static int regionOrigin(int chunkCoord, int shift, int blockSize) { @@ -386,6 +447,14 @@ public class OcclusionCuller { long frameStamp = SectionLattice.frameStamp(frame); visitState[idx] = frameStamp; + this.useFastFrustumClamping = useOcclusionCulling; + if (this.useFastFrustumClamping) { + if (this.apertures.length < visitState.length) { + this.apertures = new long[visitState.length]; + } + this.apertures[idx] = FastFrustumClamping.FULL; + } + // Visit the origin immediately; it is processed inline rather than // enqueued so the BFS starts with its neighbours. int sectionIndex = LocalSectionIndex.pack(origin.x(), origin.y(), origin.z()); @@ -402,7 +471,17 @@ public class OcclusionCuller { } int xyz = this.lattice.packXyz(origin.x(), origin.y(), origin.z()); - this.visitNeighbors(visitState, queue, delta, idx, xyz, outgoing, frameStamp); + if (this.useFastFrustumClamping) { + while (outgoing != 0) { + int dir = Integer.numberOfTrailingZeros(outgoing); + outgoing &= outgoing - 1; + this.tail = this.visitFfcNode(visitState, this.apertures, queue, FastFrustumClamping.FULL, + idx + delta[dir], xyz + XYZ_STEP[dir], INCOMING[dir], frameStamp, + Math.abs(GraphDirection.x(dir)), Math.abs(GraphDirection.y(dir)), Math.abs(GraphDirection.z(dir)), this.tail); + } + } else { + this.visitNeighbors(visitState, queue, delta, idx, xyz, outgoing, frameStamp); + } } /**