Adaptive raster culling #41

Merged
embeddedt merged 9 commits from adaptive-raster-culling into stonecutter 2026-09-17 10:10:54 -04:00
3 changed files with 132 additions and 23 deletions
Showing only changes of commit 7aea4244b6 - Show all commits

No files matched your search

@@ -1,6 +1,7 @@
package org.embeddedt.embeddium.impl.render.chunk.occlusion.bench;
import org.embeddedt.embeddium.impl.render.chunk.RenderSection;
import org.embeddedt.embeddium.impl.render.chunk.occlusion.RasterBudget;
import org.embeddedt.embeddium.impl.render.chunk.occlusion.SectionLattice;
import org.embeddedt.embeddium.impl.render.viewport.Viewport;
@@ -8,8 +9,8 @@ public final class RasterCullReport {
private static final int RENDER_DISTANCE = Integer.getInteger("report.renderDistance", 32);
private static final float FOV = Float.parseFloat(System.getProperty("report.fov", "70"));
private static final int YAW_STEPS = 8;
/** Frames the adaptive budget is given to settle before a placement is measured. */
private static final int WARM_FRAMES = 48;
/** Frames the adaptive budget is given to settle before a placement is measured; covers a probe. */
private static final int WARM_FRAMES = RasterBudget.PROBE_PERIOD + 32;
private static final int TIMED_ROUNDS = 5;
private static final WorldType[] WORLDS = worlds();
@@ -112,7 +113,7 @@ public final class RasterCullReport {
System.out.printf("%-8s %-8s %10d %10d %7.1f%% %10d %7.1f%% %6s %5.1f%% %7.3f %7.3f %7.3f %9d %10s %10s%n", world, placement.name(),
meanBaseline, meanOccluded, percentDelta(meanBaseline, meanOccluded),
meanBudgeted, percentDelta(meanBaseline, meanBudgeted),
budget.wasFullPass() ? "full" : Integer.toString(budget.limit()), budget.yield() * 100.0f,
describe(budget), budget.yield() * 100.0f,
nanos[0] / 1e6 / YAW_STEPS, nanos[1] / 1e6 / YAW_STEPS, nanos[2] / 1e6 / YAW_STEPS,
backtracks / YAW_STEPS, rasterLattice.rasterBufferSize(), budgetedLattice.rasterBufferSize());
}
@@ -140,15 +141,21 @@ public final class RasterCullReport {
return baseline == 0 ? 0.0 : ((value - baseline) * 100.0) / baseline;
}
private static String describe(RasterBudget budget) {
return budget.wasProbe() ? "probe" : budget.wasFullPass() ? "full" : Integer.toString(budget.limit());
}
/**
* Settles the controller on the placement with the least occlusion, then jumps to the one with the most and
* counts frames until the budget stops changing. This is the latency a player sees walking into a cave.
* Settles the controller, from the floor, on the placement with the least occlusion, then jumps to the one with
* the most and counts frames until the budget stops changing, over two probe periods. This is the latency a
* player sees walking into a cave. The trace lists each frame at which the budget's state changed.
*/
private static void rampReport(WorldType world, SectionLattice lattice, float searchDistance, int numRegions, int frame) {
Placement[] placements = placements();
Placement from = placements[placements.length - 1];
Placement to = placements[0];
var budget = lattice.rasterBudget();
budget.copyFrom(new RasterBudget());
for (int warm = 0; warm < WARM_FRAMES; warm++) {
Viewport viewport = Cameras.viewport(8.5, from.y(), 8.5, (360.0f / YAW_STEPS) * (warm % YAW_STEPS),
@@ -161,15 +168,18 @@ public final class RasterCullReport {
var trace = new StringBuilder();
int settledAt = -1;
int previous = startBudget;
String previousState = null;
for (int i = 0; i < WARM_FRAMES; i++) {
for (int i = 0; i < 2 * RasterBudget.PROBE_PERIOD; i++) {
Viewport viewport = Cameras.viewport(8.5, to.y(), 8.5, (360.0f / YAW_STEPS) * (i % YAW_STEPS),
to.pitch(), RENDER_DISTANCE, FOV);
lattice.ensureWindowCovers(viewport.getChunkCoord(), searchDistance);
lattice.findVisible(new CountingVisitor(), viewport, searchDistance, numRegions, true, true, ++frame);
if (i < 16) {
trace.append(' ').append(budget.wasFullPass() ? "full" : Integer.toString(budget.limit()));
String state = describe(budget);
if (!state.equals(previousState)) {
trace.append(' ').append(i).append(':').append(state);
}
if (budget.limit() != previous) {
@@ -179,10 +189,11 @@ public final class RasterCullReport {
}
previous = budget.limit();
previousState = state;
}
System.out.printf("%-8s ramp %s -> %s: limit %d -> %s, settled after %d frames, trace:%s%n", world, from.name(), to.name(),
startBudget, budget.wasFullPass() ? "full" : Integer.toString(budget.limit()), settledAt, trace);
startBudget, describe(budget), settledAt, trace);
}
/**
@@ -258,7 +269,7 @@ public final class RasterCullReport {
}
System.out.printf("%5d %6s %5.1f%% %9d %9d %9d %8.3f %8.3f %8.3f%n", i,
budget.wasFullPass() ? "full" : Integer.toString(budget.limit()), budget.yield() * 100.0f,
describe(budget), budget.yield() * 100.0f,
visible[0], visible[1], visible[2], ms[0], ms[1], ms[2]);
}
@@ -11,6 +11,11 @@ package org.embeddedt.embeddium.impl.render.chunk.occlusion;
*
* <p>Limiting by distance rather than by count matters: the search runs roughly front to back, so everything
* inside the limit is drawn before anything it could hide, and the yield measured there is representative.
*
* <p>Representative of the inside, that is: an occluder beyond the limit is never drawn, so whatever it hides is
* never seen to be hidden, and a cliff a few chunks off would hold the limit at the floor for as long as the
* camera stood there. So every {@link #PROBE_PERIOD} frames one frame runs unbudgeted, and when that pass yields
* enough to pay for itself the limit jumps straight to it.
*/
public final class RasterBudget {
/** Smallest squared chunk distance the limit can take. */
@@ -25,6 +30,9 @@ public final class RasterBudget {
/** The first this many far sections are tested regardless of the limit, so enclosed spaces get a full pass. */
public static final int CHEAP_PASS = 32;
/** Every this many frames one runs unbudgeted, to find occlusion the limit cannot reach. */
public static final int PROBE_PERIOD = 64;
private static final int EMA_SHIFT = 2;
private static final int Q = 16;
@@ -34,12 +42,17 @@ public final class RasterBudget {
private int yieldEma;
private int ceiling = UNBOUNDED;
private int frameLimit = FLOOR;
/** Nothing beyond this is tested this frame; twice the limit's distance, or unbounded on a full pass. */
private int testable = UNBOUNDED;
/** Non-zero holds the limit fixed; for benchmarks. */
private int pinned;
private int frames;
private boolean probing;
private int farReached;
private int farthest;
private int tested;
@@ -48,8 +61,10 @@ public final class RasterBudget {
/** @param ceiling squared chunk distance no reachable section exceeds */
public void beginFrame(int ceiling) {
this.ceiling = Math.max(FLOOR, ceiling);
int limit = this.limit();
this.testable = limit >= this.ceiling ? UNBOUNDED : Math.min(this.ceiling, saturatingDouble(saturatingDouble(limit)));
this.probing = this.pinned == 0 && this.limit < this.ceiling && ++this.frames % PROBE_PERIOD == 0;
this.frameLimit = this.probing ? UNBOUNDED : this.limit();
this.testable = this.frameLimit >= this.ceiling ? UNBOUNDED
: Math.min(this.ceiling, saturatingDouble(saturatingDouble(this.frameLimit)));
this.farReached = 0;
this.farthest = 0;
this.tested = 0;
@@ -64,7 +79,7 @@ public final class RasterBudget {
this.farthest = squaredChunkDist;
}
if (squaredChunkDist > this.testable || (squaredChunkDist > this.limit() && this.farReached > CHEAP_PASS)) {
if (squaredChunkDist > this.testable || (squaredChunkDist > this.frameLimit && this.farReached > CHEAP_PASS)) {
return false;
}
@@ -77,12 +92,22 @@ public final class RasterBudget {
}
public void endFrame() {
if (this.tested >= MIN_SAMPLE) {
int yield = (int) (((long) this.culled << Q) / this.tested);
this.yieldEma += (yield - this.yieldEma) >> EMA_SHIFT;
if (this.tested < MIN_SAMPLE) return;
int yield = (int) (((long) this.culled << Q) / this.tested);
if (this.probing) {
if (yield >= HIGH_YIELD) {
this.limit = this.ceiling;
this.yieldEma = yield;
}
return;
}
if (this.pinned != 0 || this.tested < MIN_SAMPLE) {
this.yieldEma += (yield - this.yieldEma) >> EMA_SHIFT;
if (this.pinned != 0) {
return;
}
@@ -139,10 +164,14 @@ public final class RasterBudget {
return this.tested == this.farReached;
}
public boolean wasProbe() {
return this.probing;
}
@Override
public String toString() {
return String.format("limit=%s yield=%.1f%% far=%d farthest=%d tested=%d culled=%d",
this.limit() == UNBOUNDED ? "max" : Integer.toString(this.limit()),
return String.format("limit=%s%s yield=%.1f%% far=%d farthest=%d tested=%d culled=%d",
this.limit() == UNBOUNDED ? "max" : Integer.toString(this.limit()), this.probing ? " (probe)" : "",
this.yield() * 100.0f, this.farReached, this.farthest, this.tested, this.culled);
}
@@ -27,8 +27,7 @@ class RasterBudgetTest {
int admitted = 0;
for (int i = 0; i < far; i++) {
int squaredDist = RasterOccluder.NEAR_SQUARED_CHUNK_DIST + 1
+ (int) ((long) i * (farthest - RasterOccluder.NEAR_SQUARED_CHUNK_DIST - 1) / Math.max(1, far - 1));
int squaredDist = spread(i, far, farthest);
if (budget.shouldTest(squaredDist)) {
admitted++;
@@ -41,6 +40,27 @@ class RasterBudgetTest {
budget.endFrame();
}
/** A frame where every admitted test beyond {@code hiddenPast} culls and none nearer does: a cliff at that distance. */
private static void frameHiddenBeyond(RasterBudget budget, int far, int hiddenPast) {
budget.beginFrame(CEILING);
for (int i = 0; i < far; i++) {
int squaredDist = spread(i, far, FARTHEST);
if (budget.shouldTest(squaredDist) && squaredDist > hiddenPast) {
budget.recordCulled();
}
}
budget.endFrame();
}
/** The {@code i}th of {@code far} squared distances spread evenly from just past the near range to {@code farthest}. */
private static int spread(int i, int far, int farthest) {
return RasterOccluder.NEAR_SQUARED_CHUNK_DIST + 1
+ (int) ((long) i * (farthest - RasterOccluder.NEAR_SQUARED_CHUNK_DIST - 1) / Math.max(1, far - 1));
}
@Test
void startsAtFloorAndTestsOnlyTheNearShell() {
var budget = new RasterBudget();
@@ -175,7 +195,7 @@ class RasterBudgetTest {
}
@Test
void pinnedLimitNeverMoves() {
void pinnedLimitNeverMovesAndNeverProbes() {
var budget = new RasterBudget();
budget.pin(RasterBudget.UNBOUNDED);
@@ -186,10 +206,59 @@ class RasterBudgetTest {
budget.pin(RasterBudget.FLOOR);
for (int i = 0; i < 20; i++) {
for (int i = 0; i < RasterBudget.PROBE_PERIOD + 2; i++) {
frame(budget, 4000, 1.0);
assertEquals(RasterBudget.FLOOR, budget.limit());
assertFalse(budget.wasFullPass());
assertFalse(budget.wasProbe());
}
}
@Test
void probeFindsOcclusionBeyondTheFloorsReach() {
var budget = new RasterBudget();
// hidden only past twice the floor's distance, which is as far as the floor ever tests
int hiddenPast = RasterBudget.FLOOR * 4;
for (int i = 1; i < RasterBudget.PROBE_PERIOD; i++) {
frameHiddenBeyond(budget, 4000, hiddenPast);
assertEquals(RasterBudget.FLOOR, budget.limit(), "frame " + i);
assertFalse(budget.wasProbe());
}
frameHiddenBeyond(budget, 4000, hiddenPast);
assertTrue(budget.wasProbe());
assertTrue(budget.wasFullPass());
assertEquals(CEILING, budget.limit(), "the probe should start a full pass at once");
// and the full pass measures the same yield, so it stays
for (int i = 0; i < 8; i++) {
frameHiddenBeyond(budget, 4000, hiddenPast);
assertFalse(budget.wasProbe());
assertTrue(budget.wasFullPass());
assertEquals(CEILING, budget.limit());
}
}
@Test
void probeThatFindsNothingChangesNothing() {
var budget = new RasterBudget();
for (int i = 1; i < RasterBudget.PROBE_PERIOD; i++) {
frame(budget, 4000, 0.03);
}
int limit = budget.limit();
float yield = budget.yield();
frame(budget, 4000, 0.03);
assertTrue(budget.wasProbe());
assertTrue(budget.wasFullPass());
assertEquals(limit, budget.limit());
assertEquals(yield, budget.yield(), "a probe's yield is not blended into the average");
frame(budget, 4000, 0.03);
assertFalse(budget.wasProbe());
assertFalse(budget.wasFullPass());
}
}