skip testing empty sections in raster culling #39

Merged
embeddedt merged 1 commits from rdh/celeritas:raster-2 into stonecutter 2026-09-05 11:47:25 -04:00
5 changed files with 18 additions and 310 deletions

No files matched your search

@@ -71,8 +71,8 @@ public final class RasterStatsReport {
int n = YAW_STEPS; int n = YAW_STEPS;
System.out.printf("world=%s visible/frame=%d total=%.3fms test=%.3fms occlude=%.3fms%n", worldType, visible / n, System.out.printf("world=%s visible/frame=%d total=%.3fms test=%.3fms occlude=%.3fms%n", worldType, visible / n,
total / 1e6 / n, RasterOccluder.STAT_TEST_NANOS / 1e6 / n, RasterOccluder.STAT_OCCLUDE_NANOS / 1e6 / n); total / 1e6 / n, RasterOccluder.STAT_TEST_NANOS / 1e6 / n, RasterOccluder.STAT_OCCLUDE_NANOS / 1e6 / n);
System.out.printf("sections tested=%d occluded=%d regionSkip=%d vertexHit=%d%n", RasterOccluder.STAT_SECTIONS / n, System.out.printf("sections tested=%d occluded=%d emptySkip=%d vertexHit=%d%n", RasterOccluder.STAT_SECTIONS / n,
RasterOccluder.STAT_OCCLUDED_SECTIONS / n, RasterOccluder.STAT_REGION_SKIP / n, AbstractRasterizer.STAT_T_VERTEX / n); RasterOccluder.STAT_OCCLUDED_SECTIONS / n, RasterOccluder.STAT_EMPTY_SKIP / n, AbstractRasterizer.STAT_T_VERTEX / n);
System.out.printf("draw calls=%d polygons=%d tiles=%d | test calls=%d polygons=%d tiles=%d | eventRows=%d%n", System.out.printf("draw calls=%d polygons=%d tiles=%d | test calls=%d polygons=%d tiles=%d | eventRows=%d%n",
AbstractRasterizer.STAT_DRAW_CALLS / n, AbstractRasterizer.STAT_DRAW_QUADS / n, AbstractRasterizer.STAT_DRAW_CALLS / n, AbstractRasterizer.STAT_DRAW_QUADS / n,
(AbstractRasterizer.STAT_D_INNER + AbstractRasterizer.STAT_D_COVER) / n, (AbstractRasterizer.STAT_D_INNER + AbstractRasterizer.STAT_D_COVER) / n,
@@ -92,7 +92,7 @@ public final class RasterStatsReport {
private static void reset() { private static void reset() {
AbstractRasterizer.STAT_D_NOOP = AbstractRasterizer.STAT_D_RANGE0 = AbstractRasterizer.STAT_D_RANGE1 = AbstractRasterizer.STAT_D_RANGE2 = AbstractRasterizer.STAT_D_RANGE3 = 0; AbstractRasterizer.STAT_D_NOOP = AbstractRasterizer.STAT_D_RANGE0 = AbstractRasterizer.STAT_D_RANGE1 = AbstractRasterizer.STAT_D_RANGE2 = AbstractRasterizer.STAT_D_RANGE3 = 0;
RasterOccluder.STAT_CENTER_HIT = RasterOccluder.STAT_NEAR = RasterOccluder.STAT_AIR_TESTS = RasterOccluder.STAT_AIR_VISIBLE = RasterOccluder.STAT_REGION_SKIP = AbstractRasterizer.STAT_T_VERTEX = 0; RasterOccluder.STAT_CENTER_HIT = RasterOccluder.STAT_NEAR = RasterOccluder.STAT_AIR_TESTS = RasterOccluder.STAT_AIR_VISIBLE = RasterOccluder.STAT_EMPTY_SKIP = AbstractRasterizer.STAT_T_VERTEX = 0;
AbstractRasterizer.STAT_T_EMPTY = AbstractRasterizer.STAT_T_INNER = AbstractRasterizer.STAT_T_COVER = AbstractRasterizer.STAT_T_HIDDEN = AbstractRasterizer.STAT_T_BOX_HIDDEN = 0; AbstractRasterizer.STAT_T_EMPTY = AbstractRasterizer.STAT_T_INNER = AbstractRasterizer.STAT_T_COVER = AbstractRasterizer.STAT_T_HIDDEN = AbstractRasterizer.STAT_T_BOX_HIDDEN = 0;
AbstractRasterizer.STAT_D_INNER = AbstractRasterizer.STAT_D_COVER = 0; AbstractRasterizer.STAT_D_INNER = AbstractRasterizer.STAT_D_COVER = 0;
RasterOccluder.STAT_TEST_NANOS = RasterOccluder.STAT_OCCLUDE_NANOS = RasterOccluder.STAT_SECTIONS = RasterOccluder.STAT_OCCLUDED_SECTIONS = 0; RasterOccluder.STAT_TEST_NANOS = RasterOccluder.STAT_OCCLUDE_NANOS = RasterOccluder.STAT_SECTIONS = RasterOccluder.STAT_OCCLUDED_SECTIONS = 0;
@@ -165,13 +165,6 @@ public abstract class AbstractRasterizer {
/** One bit per tile, set once the tile is fully covered, so scans can skip covered runs 64 tiles at a time. */ /** One bit per tile, set once the tile is fully covered, so scans can skip covered runs 64 tiles at a time. */
long[] fullBits; long[] fullBits;
/**
* One bit per cell of 8x8 tiles in each cell row, set once anything is drawn in the cell. A box whose
* tiles lie in untouched cells alone cannot be occluded, and {@link #touchedVersion} changes whenever a
* cell is first touched, so a caller can cache that verdict and revalidate it with one compare.
*/
int[] touchedCellRows;
int touchedVersion;
final EventFiller[] EVENT_FILLERS = new EventFiller[0x1000]; final EventFiller[] EVENT_FILLERS = new EventFiller[0x1000];
/** Bounds of current triangle - pixel coordinates. */ /** Bounds of current triangle - pixel coordinates. */
@@ -714,125 +707,12 @@ public abstract class AbstractRasterizer {
eventData = new int[pixelHeight * 2]; eventData = new int[pixelHeight * 2];
tiles = new long[tileCount]; tiles = new long[tileCount];
fullBits = new long[(tileCount + 63) >> 6]; fullBits = new long[(tileCount + 63) >> 6];
touchedCellRows = new int[(tileHeight + CELL_TILE_MASK) >> CELL_TILE_SHIFT];
} }
/** Marks every pixel clear. */ /** Marks every pixel clear. */
final void clear() { final void clear() {
Arrays.fill(tiles, 0L); Arrays.fill(tiles, 0L);
Arrays.fill(fullBits, 0L); Arrays.fill(fullBits, 0L);
Arrays.fill(touchedCellRows, 0);
touchedVersion++;
}
/** Cells of the touched map are this many tiles square; 256 tiles across fit one int of cell bits. */
static final int CELL_TILE_SHIFT = 3;
static final int CELL_TILE_MASK = (1 << CELL_TILE_SHIFT) - 1;
/** Bits of the cells spanning tile columns {@code first..last} inclusive. */
private static int cellMask(int first, int last) {
return (-1 >>> (31 - (last >> CELL_TILE_SHIFT))) & (-1 << (first >> CELL_TILE_SHIFT));
}
/** Records that the tiles {@code first..last} of tile row {@code tileY} may have been drawn into. */
private void markTouched(int tileY, int first, int last) {
final int[] rows = touchedCellRows;
final int cellY = tileY >> CELL_TILE_SHIFT;
final int mask = cellMask(first, last);
final int row = rows[cellY];
if ((row & mask) != mask) {
rows[cellY] = row | mask;
touchedVersion++;
}
}
/** Changes whenever a cell of the touched map is first drawn into; see {@link #isTileBoxUntouched}. */
public final int touchedVersion() {
return touchedVersion;
}
/** True if nothing has been drawn in any cell overlapping the given inclusive tile bounds. */
public final boolean isTileBoxUntouched(int packedTileBounds) {
final int[] rows = touchedCellRows;
final int mask = cellMask(packedTileBounds & 0xFF, (packedTileBounds >>> 16) & 0xFF);
final int lastCellY = ((packedTileBounds >>> 24) & 0xFF) >> CELL_TILE_SHIFT;
for (int cellY = ((packedTileBounds >>> 8) & 0xFF) >> CELL_TILE_SHIFT; cellY <= lastCellY; cellY++) {
if ((rows[cellY] & mask) != 0) {
return false;
}
}
return true;
}
/** Result of {@link #boxTileBounds} for a box that reaches the near plane or lies wholly off screen. */
public static final long NO_TILE_BOUNDS = -1L;
/** Set in a {@link #boxTileBounds} result when every point of the box projects on screen. */
public static final long TILE_BOUNDS_ON_SCREEN = 1L << 32;
/**
* Inclusive tile bounds of the screen rectangle around a camera-relative box, packed as x0, y0, x1, y1 in
* successive bytes of the low word, grown by a pixel and clamped to the screen. This rectangle contains the
* projection of every point in the box, so every polygon a box inside it is tested with. The result carries
* {@link #TILE_BOUNDS_ON_SCREEN} when no clamping was needed, so that a box inside it is on screen too.
* {@link #NO_TILE_BOUNDS} if any corner is behind the near plane or the whole box is off screen.
*/
final long boxTileBounds(float fx, float fy, float fz, float ex, float ey, float ez) {
final float bx = m00 * fx + m01 * fy + m02 * fz + m03;
final float by = m10 * fx + m11 * fy + m12 * fz + m13;
final float bz = m20 * fx + m21 * fy + m22 * fz + m23;
final float bw = m30 * fx + m31 * fy + m32 * fz + m33;
final float dxx = m00 * ex, dxy = m10 * ex, dxz = m20 * ex, dxw = m30 * ex;
final float dyx = m01 * ey, dyy = m11 * ey, dyz = m21 * ey, dyw = m31 * ey;
final float dzx = m02 * ez, dzy = m12 * ez, dzz = m22 * ez, dzw = m32 * ez;
float minX = Float.POSITIVE_INFINITY, maxX = Float.NEGATIVE_INFINITY;
float minY = Float.POSITIVE_INFINITY, maxY = Float.NEGATIVE_INFINITY;
for (int corner = 0; corner < 8; corner++) {
float x = bx, y = by, z = bz, w = bw;
if ((corner & 4) != 0) { x += dxx; y += dxy; z += dxz; w += dxw; }
if ((corner & 2) != 0) { x += dyx; y += dyy; z += dyz; w += dyw; }
if ((corner & 1) != 0) { x += dzx; y += dzy; z += dzz; w += dzw; }
// same acceptance as needsNearClip
if (!(w > 0 && z > 0 && z <= w)) {
return NO_TILE_BOUNDS;
}
final float iw = 1f / w;
final float sx = x * iw;
final float sy = y * iw;
if (sx < minX) minX = sx;
if (sx > maxX) maxX = sx;
if (sy < minY) minY = sy;
if (sy > maxY) maxY = sy;
}
// same acceptance as isProjectedPointClear, so that every point of the box passes it
final long onScreen = (minX >= -1f && maxX < 1f && minY >= -1f && maxY < 1f) ? TILE_BOUNDS_ON_SCREEN : 0L;
// to pixels, with a pixel of margin over the rounding of the polygon path and the test dilation
final float px0 = (minX + 1f) * halfPixelWidth - 1f;
final float px1 = (maxX + 1f) * halfPixelWidth + 1f;
final float py0 = (minY + 1f) * halfPixelHeight - 1f;
final float py1 = (maxY + 1f) * halfPixelHeight + 1f;
if (!(px1 >= 0f && py1 >= 0f && px0 <= lastPixelX && py0 <= lastPixelY)) {
return NO_TILE_BOUNDS;
}
final int tx0 = Math.max(0, (int) px0) >> TILE_AXIS_SHIFT;
final int ty0 = Math.max(0, (int) py0) >> TILE_AXIS_SHIFT;
final int tx1 = Math.min(lastPixelX, (int) px1) >> TILE_AXIS_SHIFT;
final int ty1 = Math.min(lastPixelY, (int) py1) >> TILE_AXIS_SHIFT;
return onScreen | (tx0 | (ty0 << 8) | (tx1 << 16) | (ty1 << 24));
} }
final int tileIndexFromPixelXY(int x, int y) { final int tileIndexFromPixelXY(int x, int y) {
@@ -846,37 +726,6 @@ public abstract class AbstractRasterizer {
m10 = m.a10f(); m11 = m.a11f(); m12 = m.a12f(); m13 = m.a13f(); m10 = m.a10f(); m11 = m.a11f(); m12 = m.a12f(); m13 = m.a13f();
m20 = m.a20f(); m21 = m.a21f(); m22 = m.a22f(); m23 = m.a23f(); m20 = m.a20f(); m21 = m.a21f(); m22 = m.a22f(); m23 = m.a23f();
m30 = m.a30f(); m31 = m.a31f(); m32 = m.a32f(); m33 = m.a33f(); m30 = m.a30f(); m31 = m.a31f(); m32 = m.a32f(); m33 = m.a33f();
final float[] step = sectionStep;
step[0] = 16f * m00; step[1] = 16f * m10; step[2] = 16f * m30;
step[3] = 16f * m01; step[4] = 16f * m11; step[5] = 16f * m31;
step[6] = 16f * m02; step[7] = 16f * m12; step[8] = 16f * m32;
}
/**
* Clip-space change in x, y and w per section along each world axis, as three triples. Clip space is
* affine in world space, so a section centre is a base point plus whole steps, and whether it lies on
* screen is a comparison of x and y against w with no divide.
*/
final float[] sectionStep = new float[9];
/** Writes the clip-space x, y and w of a camera-relative point. */
final void clipPoint(float fx, float fy, float fz, float[] out, int offset) {
out[offset] = m00 * fx + m01 * fy + m02 * fz + m03;
out[offset + 1] = m10 * fx + m11 * fy + m12 * fz + m13;
out[offset + 2] = m30 * fx + m31 * fy + m32 * fz + m33;
}
/**
* True if the section centre {@code i, j, k} sections from the given clip-space base point is on screen
* by the same acceptance as {@link #isProjectedPointClear}. The base must be in front of the near plane.
*/
final boolean isSectionCenterOnScreen(float[] base, int offset, int i, int j, int k) {
final float[] step = sectionStep;
final float x = base[offset] + i * step[0] + j * step[3] + k * step[6];
final float y = base[offset + 1] + i * step[1] + j * step[4] + k * step[7];
final float w = base[offset + 2] + i * step[2] + j * step[5] + k * step[8];
return w > 0 && x >= -w && x < w && y >= -w && y < w;
} }
/** @param x region origin relative to the camera, in {@link Constants#CAMERA_PRECISION_BITS} fixed point */ /** @param x region origin relative to the camera, in {@link Constants#CAMERA_PRECISION_BITS} fixed point */
@@ -1099,13 +948,6 @@ public abstract class AbstractRasterizer {
innerLast = Math.min(last, rowBase + bandInnerLast); innerLast = Math.min(last, rowBase + bandInnerLast);
outerFirst = rowBase + bandOuterFirst; outerFirst = rowBase + bandOuterFirst;
outerLast = Math.min(last, rowBase + bandOuterLast); outerLast = Math.min(last, rowBase + bandOuterLast);
final int firstDrawn = Math.max(tileIndex, outerFirst) - rowBase;
final int lastDrawn = outerLast - rowBase;
if (firstDrawn <= lastDrawn) {
markTouched(tileY, firstDrawn, lastDrawn);
}
} }
// outside the band's widest row the polygon covers nothing // outside the band's widest row the polygon covers nothing
@@ -1021,42 +1021,6 @@ public abstract class BoxOccluder {
raster.setRegionOffset(offsetX, offsetY, offsetZ); raster.setRegionOffset(offsetX, offsetY, offsetZ);
} }
/**
* Screen tile bounds of a world-space box for the current scene, as {@link AbstractRasterizer#boxTileBounds}
* packs them. Does not disturb the region prepared by {@link #prepareRegion}.
*/
public final long boxTileBounds(int originX, int originY, int originZ, int sizeX, int sizeY, int sizeZ) {
final float scale = 1f / CAMERA_PRECISION_UNITY;
final float fx = (int) (((long) originX << CAMERA_PRECISION_BITS) - viewX) * scale;
final float fy = (int) (((long) originY << CAMERA_PRECISION_BITS) - viewY) * scale;
final float fz = (int) (((long) originZ << CAMERA_PRECISION_BITS) - viewZ) * scale;
return raster.boxTileBounds(fx, fy, fz, sizeX, sizeY, sizeZ);
}
/** Writes the clip-space x, y and w of a world-space point for the current scene; see {@link AbstractRasterizer#clipPoint}. */
public final void clipPoint(int x, int y, int z, float[] out, int offset) {
final float scale = 1f / CAMERA_PRECISION_UNITY;
final float fx = (int) (((long) x << CAMERA_PRECISION_BITS) - viewX) * scale;
final float fy = (int) (((long) y << CAMERA_PRECISION_BITS) - viewY) * scale;
final float fz = (int) (((long) z << CAMERA_PRECISION_BITS) - viewZ) * scale;
raster.clipPoint(fx, fy, fz, out, offset);
}
/** See {@link AbstractRasterizer#isSectionCenterOnScreen}. */
public final boolean isSectionCenterOnScreen(float[] base, int offset, int i, int j, int k) {
return raster.isSectionCenterOnScreen(base, offset, i, j, k);
}
/** See {@link AbstractRasterizer#touchedVersion}. */
public final int touchedVersion() {
return raster.touchedVersion();
}
/** See {@link AbstractRasterizer#isTileBoxUntouched}. */
public final boolean isTileBoxUntouched(int packedTileBounds) {
return raster.isTileBoxUntouched(packedTileBounds);
}
/** /**
* Check if needs redrawn and prep for redraw if so. * Check if needs redrawn and prep for redraw if so.
* When false, regions should be drawn only if their occluder version is not current. * When false, regions should be drawn only if their occluder version is not current.
@@ -1,7 +1,6 @@
package org.embeddedt.embeddium.impl.render.chunk.occlusion; package org.embeddedt.embeddium.impl.render.chunk.occlusion;
import grondag.bitraster.AbstractRasterizer; import grondag.bitraster.AbstractRasterizer;
import grondag.bitraster.PackedBox;
import org.embeddedt.embeddium.impl.common.util.MathUtil; import org.embeddedt.embeddium.impl.common.util.MathUtil;
import org.embeddedt.embeddium.impl.render.chunk.LocalSectionIndex; import org.embeddedt.embeddium.impl.render.chunk.LocalSectionIndex;
import org.embeddedt.embeddium.impl.render.chunk.PackedSectionMetadata; import org.embeddedt.embeddium.impl.render.chunk.PackedSectionMetadata;
@@ -9,7 +8,6 @@ import org.embeddedt.embeddium.impl.render.chunk.lists.RenderVisualsService;
import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion; import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion;
import org.embeddedt.embeddium.impl.render.viewport.CameraTransform; import org.embeddedt.embeddium.impl.render.viewport.CameraTransform;
import org.embeddedt.embeddium.impl.render.viewport.Viewport; import org.embeddedt.embeddium.impl.render.viewport.Viewport;
import org.embeddedt.embeddium.impl.render.viewport.frustum.Frustum;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.joml.Vector3ic; import org.joml.Vector3ic;
@@ -193,7 +191,7 @@ public class OcclusionCuller {
this.rasterActive = this.rasterOccluder != null && useOcclusionCulling && viewport.getVpMatrix() != null; this.rasterActive = this.rasterOccluder != null && useOcclusionCulling && viewport.getVpMatrix() != null;
if (this.rasterActive) { if (this.rasterActive) {
this.rasterOccluder.prepareScene(frame, viewport, searchDistance, numRegions); this.rasterOccluder.prepareScene(frame, viewport, searchDistance);
} }
this.process(visitor, viewport, searchDistance, useOcclusionCulling, allowFrustumClamping, frame); this.process(visitor, viewport, searchDistance, useOcclusionCulling, allowFrustumClamping, frame);
@@ -306,7 +304,7 @@ public class OcclusionCuller {
if (this.rasterActive && visible) { if (this.rasterActive && visible) {
RasterOccluder.SectionVisibility result = this.rasterTest(occluderBounds, occluderData, RasterOccluder.SectionVisibility result = this.rasterTest(occluderBounds, occluderData,
idx, chunkX, chunkY, chunkZ, camX, camY, camZ, idx, chunkX, chunkY, chunkZ, camX, camY, camZ,
compactMeta, PackedSectionMetadata.hasOccluderData(sm), regionId, regionX, regionY, regionZ); compactMeta, PackedSectionMetadata.hasOccluderData(sm));
visible = result == RasterOccluder.SectionVisibility.VISIBLE; visible = result == RasterOccluder.SectionVisibility.VISIBLE;
traverse = result != RasterOccluder.SectionVisibility.HIDDEN; traverse = result != RasterOccluder.SectionVisibility.HIDDEN;
} }
@@ -363,15 +361,16 @@ public class OcclusionCuller {
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) {
int regionId, int regionX, int regionY, int regionZ) {
var occluder = this.rasterOccluder; var occluder = this.rasterOccluder;
// A section with nothing to draw is visible wherever nothing has been drawn. One with geometry is // A section with nothing to draw is never tested. Such sections are most of what the search reaches on the
// tested against the bounds of what it draws, which may lie off screen even though the section is // surface and nearly all of them pass, so the test costs more than the traversal it prunes. Traversal
// in the frustum, and that cull is worth keeping; such sections are rare above the horizon anyway. // continues through them and whatever lies behind is tested on its own, as Canvas does. One with geometry
if (!hasOccluderData && occluder.isSectionUntouched(regionId, regionX, regionY, regionZ, chunkX, chunkY, chunkZ)) { // is tested against the bounds of what it draws, which may lie off screen even though the section is in
if (AbstractRasterizer.STATS) RasterOccluder.STAT_REGION_SKIP++; // the frustum, and that cull is worth keeping.
if (!hasOccluderData) {
if (AbstractRasterizer.STATS) RasterOccluder.STAT_EMPTY_SKIP++;
return RasterOccluder.SectionVisibility.VISIBLE; return RasterOccluder.SectionVisibility.VISIBLE;
} }
@@ -383,12 +382,10 @@ public class OcclusionCuller {
boolean hasGeometry = boolean hasGeometry =
(PackedSectionMetadata.getCompactVisualsFlags(meta) & (1 << RenderVisualsService.HAS_BLOCK_GEOMETRY)) != 0; (PackedSectionMetadata.getCompactVisualsFlags(meta) & (1 << RenderVisualsService.HAS_BLOCK_GEOMETRY)) != 0;
// the flag spares the many sections without data the loads below, which rarely hit cache RasterOccluder.SectionVisibility result = occluder.testSection(chunkX << 4, chunkY << 4, chunkZ << 4,
int bounds = hasOccluderData ? occluderBounds[idx] : PackedBox.FULL_BOX; squaredChunkDist, occluderBounds[idx]);
RasterOccluder.SectionVisibility result = occluder.testSection(chunkX << 4, chunkY << 4, chunkZ << 4, squaredChunkDist, bounds); if (result == RasterOccluder.SectionVisibility.VISIBLE && hasGeometry) {
if (result == RasterOccluder.SectionVisibility.VISIBLE && hasGeometry && hasOccluderData) {
occluder.occludeSection(occluderData[idx]); occluder.occludeSection(occluderData[idx]);
} }
@@ -6,48 +6,17 @@ import grondag.bitraster.Constants;
import grondag.bitraster.Matrix4L; import grondag.bitraster.Matrix4L;
import grondag.bitraster.PackedBox; import grondag.bitraster.PackedBox;
import grondag.bitraster.PerspectiveRasterizer; import grondag.bitraster.PerspectiveRasterizer;
import org.embeddedt.embeddium.impl.render.chunk.region.RenderRegion;
import org.embeddedt.embeddium.impl.render.viewport.Viewport; import org.embeddedt.embeddium.impl.render.viewport.Viewport;
import org.joml.Matrix4fc; import org.joml.Matrix4fc;
import java.util.Arrays;
public final class RasterOccluder extends BoxOccluder { public final class RasterOccluder extends BoxOccluder {
/** Sections this close are drawn but never tested, since they're almost always visible and most costly to test */ /** Sections this close are drawn but never tested, since they're almost always visible and most costly to test */
public static final int NEAR_SQUARED_CHUNK_DIST = 3; public static final int NEAR_SQUARED_CHUNK_DIST = 3;
private int backtrackCount; private int backtrackCount;
/**
* Per-region screen bounds for the current scene, so that sections of a region whose screen area has
* had nothing drawn in it yet are visible without a test of their own. Most of the sections a surface
* camera reaches are sky, and nothing ever gets drawn above the horizon, so this resolves most of them.
*
* <p>{@code regionScene} stamps the scene the bounds were computed for. {@code regionVersion} holds the
* touched-map version at which the bounds were last found untouched, or {@link #REGION_TOUCHED} once they
* were not: drawing only ever adds coverage, so that verdict is final for the scene. A region that is not
* wholly on screen keeps the clip-space position of its first section centre in {@code regionClip}, from
* which any section centre's on-screen test is a few multiplies.
*/
private int[] regionScene = new int[0];
private int[] regionTiles = new int[0];
private int[] regionVersion = new int[0];
private byte[] regionState = new byte[0];
private float[] regionClip = new float[0];
private int scene;
/** {@code regionVersion} values that never match a touched-map version. */
private static final int REGION_TOUCHED = -1, REGION_UNVERIFIED = -2;
/** The region reaches the near plane or lies wholly off screen; its sections are all tested. */
private static final byte REGION_NO_BOUNDS = 0;
/** The region straddles a screen edge; each section centre is checked against the screen. */
private static final byte REGION_PARTLY_ON_SCREEN = 1;
/** Every point of the region is on screen. */
private static final byte REGION_ON_SCREEN = 2;
/** Work counters for benchmarks; only maintained when {@link AbstractRasterizer#STATS} is set. */ /** Work counters for benchmarks; only maintained when {@link AbstractRasterizer#STATS} is set. */
public static long STAT_CENTER_HIT, STAT_AIR_TESTS, STAT_AIR_VISIBLE, STAT_NEAR, STAT_REGION_SKIP; public static long STAT_CENTER_HIT, STAT_AIR_TESTS, STAT_AIR_VISIBLE, STAT_NEAR, STAT_EMPTY_SKIP;
public static long STAT_TEST_NANOS, STAT_OCCLUDE_NANOS, STAT_SECTIONS, STAT_OCCLUDED_SECTIONS; public static long STAT_TEST_NANOS, STAT_OCCLUDE_NANOS, STAT_SECTIONS, STAT_OCCLUDED_SECTIONS;
public enum SectionVisibility { public enum SectionVisibility {
@@ -79,7 +48,7 @@ public final class RasterOccluder extends BoxOccluder {
* camera translation, so it is supplied as the projection with an identity model matrix; * camera translation, so it is supplied as the projection with an identity model matrix;
* {@link #prepareRegion} applies the camera-relative offset per section. * {@link #prepareRegion} applies the camera-relative offset per section.
*/ */
public void prepareScene(int viewVersion, Viewport viewport, float searchDistance, int numRegions) { public void prepareScene(int viewVersion, Viewport viewport, float searchDistance) {
Matrix4fc vpMatrix = viewport.getVpMatrix(); Matrix4fc vpMatrix = viewport.getVpMatrix();
if (vpMatrix == null) { if (vpMatrix == null) {
@@ -89,16 +58,6 @@ public final class RasterOccluder extends BoxOccluder {
var transform = viewport.getTransform(); var transform = viewport.getTransform();
this.backtrackCount = 0; this.backtrackCount = 0;
this.scene++;
if (this.regionScene.length < numRegions) {
int capacity = Math.max(numRegions, this.regionScene.length * 2);
this.regionScene = Arrays.copyOf(this.regionScene, capacity);
this.regionTiles = Arrays.copyOf(this.regionTiles, capacity);
this.regionVersion = Arrays.copyOf(this.regionVersion, capacity);
this.regionState = Arrays.copyOf(this.regionState, capacity);
this.regionClip = Arrays.copyOf(this.regionClip, capacity * 3);
}
chooseBufferSize(vpMatrix, searchDistance); chooseBufferSize(vpMatrix, searchDistance);
invalidate(); invalidate();
@@ -188,60 +147,6 @@ public final class RasterOccluder extends BoxOccluder {
*/ */
private static final float PIXELS_PER_BLOCK = 1.0f; private static final float PIXELS_PER_BLOCK = 1.0f;
/**
* True if nothing has been drawn where the region projects and the given section's centre is on screen,
* in which case the section is visible: its centre pixel lies inside the region's screen rectangle, and
* an untouched pixel on screen is exactly what the centre test looks for. Costs one compare per section
* once the region's bounds are known, plus a few multiplies when the region is not wholly on screen, and
* a re-check of a handful of cell rows whenever something new has been drawn since.
*/
public boolean isSectionUntouched(int regionId, int originX, int originY, int originZ, int chunkX, int chunkY, int chunkZ) {
final int[] regionVersion = this.regionVersion;
if (this.regionScene[regionId] != this.scene) {
this.regionScene[regionId] = this.scene;
long bounds = boxTileBounds(originX, originY, originZ,
RenderRegion.REGION_BLOCK_WIDTH, RenderRegion.REGION_BLOCK_HEIGHT, RenderRegion.REGION_BLOCK_LENGTH);
this.regionTiles[regionId] = (int) bounds;
regionVersion[regionId] = REGION_UNVERIFIED;
if (bounds == AbstractRasterizer.NO_TILE_BOUNDS) {
this.regionState[regionId] = REGION_NO_BOUNDS;
} else if ((bounds & AbstractRasterizer.TILE_BOUNDS_ON_SCREEN) != 0) {
this.regionState[regionId] = REGION_ON_SCREEN;
} else {
this.regionState[regionId] = REGION_PARTLY_ON_SCREEN;
clipPoint(originX + 8, originY + 8, originZ + 8, this.regionClip, regionId * 3);
}
}
final byte state = this.regionState[regionId];
if (state == REGION_NO_BOUNDS) {
return false;
}
final int version = regionVersion[regionId];
final int current = touchedVersion();
if (version != current) {
if (version == REGION_TOUCHED) {
return false;
}
if (!isTileBoxUntouched(this.regionTiles[regionId])) {
regionVersion[regionId] = REGION_TOUCHED;
return false;
}
regionVersion[regionId] = current;
}
return state == REGION_ON_SCREEN
|| isSectionCenterOnScreen(this.regionClip, regionId * 3,
chunkX - (originX >> 4), chunkY - (originY >> 4), chunkZ - (originZ >> 4));
}
/** /**
* tests the tight bounds of what the section draws first. a section whose geometry is hidden * tests the tight bounds of what the section draws first. a section whose geometry is hidden
* may still have unoccluded empty space, so a second test decides whether traversal continues. * may still have unoccluded empty space, so a second test decides whether traversal continues.