Adaptive raster culling #41
No files matched your search
+11
-4
@@ -109,6 +109,7 @@ public class OcclusionCuller {
|
|||||||
|
|
||||||
// When non-null, receives the queue entry ((packedLocalXYZ << 32) | latticeIndex) of every cell reported
|
// When non-null, receives the queue entry ((packedLocalXYZ << 32) | latticeIndex) of every cell reported
|
||||||
// visible, in traversal order; the count is published to SectionLattice.visibleCount at the end of the search.
|
// visible, in traversal order; the count is published to SectionLattice.visibleCount at the end of the search.
|
||||||
|
// The count is kept either way, for the raster budget.
|
||||||
// Used as the root set of the shadow search.
|
// Used as the root set of the shadow search.
|
||||||
private long[] visibleCells;
|
private long[] visibleCells;
|
||||||
private int visibleCount;
|
private int visibleCount;
|
||||||
@@ -204,7 +205,7 @@ public class OcclusionCuller {
|
|||||||
this.process(visitor, viewport, searchDistance, useOcclusionCulling, allowFrustumClamping, frame);
|
this.process(visitor, viewport, searchDistance, useOcclusionCulling, allowFrustumClamping, frame);
|
||||||
|
|
||||||
if (this.rasterActive) {
|
if (this.rasterActive) {
|
||||||
this.rasterBudget.endFrame();
|
this.rasterBudget.endFrame(this.visibleCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (recordVisible) {
|
if (recordVisible) {
|
||||||
@@ -327,8 +328,12 @@ public class OcclusionCuller {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (visible && visibleCells != null) {
|
if (visible) {
|
||||||
visibleCells[visibleCount++] = entry;
|
if (visibleCells != null) {
|
||||||
|
visibleCells[visibleCount] = entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
visibleCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
int connections;
|
int connections;
|
||||||
@@ -659,9 +664,11 @@ public class OcclusionCuller {
|
|||||||
int xyz = this.lattice.packXyz(origin.x(), origin.y(), origin.z());
|
int xyz = this.lattice.packXyz(origin.x(), origin.y(), origin.z());
|
||||||
|
|
||||||
if (this.visibleCells != null) {
|
if (this.visibleCells != null) {
|
||||||
this.visibleCells[this.visibleCount++] = ((long) xyz << 32) | (idx & 0xFFFFFFFFL);
|
this.visibleCells[this.visibleCount] = ((long) xyz << 32) | (idx & 0xFFFFFFFFL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.visibleCount++;
|
||||||
|
|
||||||
int outgoing;
|
int outgoing;
|
||||||
|
|
||||||
if (useOcclusionCulling) {
|
if (useOcclusionCulling) {
|
||||||
|
|||||||
+34
-5
@@ -14,8 +14,13 @@ package org.embeddedt.embeddium.impl.render.chunk.occlusion;
|
|||||||
*
|
*
|
||||||
* <p>Representative of the inside, that is: an occluder beyond the limit is never drawn, so whatever it hides is
|
* <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
|
* 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
|
* camera stood there. So every {@link #PROBE_PERIOD} frames one frame runs unbudgeted, and when that pass pays
|
||||||
* enough to pay for itself the limit jumps straight to it.
|
* for itself the limit jumps straight to it.
|
||||||
|
*
|
||||||
|
* <p>Yield cannot say whether it pays: a hidden section also stops the search behind it, so one cull removes a
|
||||||
|
* single section from the frame on gentle hills and dozens on cave-riddled terrain, at a similar yield. The probe
|
||||||
|
* instead compares the frame's visible count against the average of the frames before it, which, the camera
|
||||||
|
* having barely moved, is what the pass removed, and charges that against the tests it added.
|
||||||
*/
|
*/
|
||||||
public final class RasterBudget {
|
public final class RasterBudget {
|
||||||
/** Smallest squared chunk distance the limit can take. */
|
/** Smallest squared chunk distance the limit can take. */
|
||||||
@@ -33,6 +38,9 @@ public final class RasterBudget {
|
|||||||
/** Every this many frames one runs unbudgeted, to find occlusion the limit cannot reach. */
|
/** Every this many frames one runs unbudgeted, to find occlusion the limit cannot reach. */
|
||||||
public static final int PROBE_PERIOD = 64;
|
public static final int PROBE_PERIOD = 64;
|
||||||
|
|
||||||
|
/** A probe pays when it removes at least one section from the frame for every this many tests it adds. */
|
||||||
|
public static final int TESTS_PER_REMOVED = 2;
|
||||||
|
|
||||||
private static final int EMA_SHIFT = 2;
|
private static final int EMA_SHIFT = 2;
|
||||||
private static final int Q = 16;
|
private static final int Q = 16;
|
||||||
|
|
||||||
@@ -53,6 +61,10 @@ public final class RasterBudget {
|
|||||||
private int frames;
|
private int frames;
|
||||||
private boolean probing;
|
private boolean probing;
|
||||||
|
|
||||||
|
/** Moving averages, in 1/16ths, of the visible count and the tests of non-probe frames; -1 before the first. */
|
||||||
|
private int visibleEma = -1;
|
||||||
|
private int testedEma;
|
||||||
|
|
||||||
private int farReached;
|
private int farReached;
|
||||||
private int farthest;
|
private int farthest;
|
||||||
private int tested;
|
private int tested;
|
||||||
@@ -61,7 +73,8 @@ public final class RasterBudget {
|
|||||||
/** @param ceiling squared chunk distance no reachable section exceeds */
|
/** @param ceiling squared chunk distance no reachable section exceeds */
|
||||||
public void beginFrame(int ceiling) {
|
public void beginFrame(int ceiling) {
|
||||||
this.ceiling = Math.max(FLOOR, ceiling);
|
this.ceiling = Math.max(FLOOR, ceiling);
|
||||||
this.probing = this.pinned == 0 && this.limit < this.ceiling && ++this.frames % PROBE_PERIOD == 0;
|
this.probing = this.pinned == 0 && this.limit < this.ceiling && ++this.frames % PROBE_PERIOD == 0
|
||||||
|
&& this.visibleEma >= 0;
|
||||||
this.frameLimit = this.probing ? UNBOUNDED : this.limit();
|
this.frameLimit = this.probing ? UNBOUNDED : this.limit();
|
||||||
this.testable = this.frameLimit >= this.ceiling ? UNBOUNDED
|
this.testable = this.frameLimit >= this.ceiling ? UNBOUNDED
|
||||||
: Math.min(this.ceiling, saturatingDouble(saturatingDouble(this.frameLimit)));
|
: Math.min(this.ceiling, saturatingDouble(saturatingDouble(this.frameLimit)));
|
||||||
@@ -91,13 +104,27 @@ public final class RasterBudget {
|
|||||||
this.culled++;
|
this.culled++;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void endFrame() {
|
/** @param visible sections the search found visible this frame */
|
||||||
|
public void endFrame(int visible) {
|
||||||
|
if (!this.probing) {
|
||||||
|
if (this.visibleEma < 0) {
|
||||||
|
this.visibleEma = visible << 4;
|
||||||
|
this.testedEma = this.tested << 4;
|
||||||
|
} else {
|
||||||
|
this.visibleEma += ((visible << 4) - this.visibleEma) >> EMA_SHIFT;
|
||||||
|
this.testedEma += ((this.tested << 4) - this.testedEma) >> EMA_SHIFT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (this.tested < MIN_SAMPLE) return;
|
if (this.tested < MIN_SAMPLE) return;
|
||||||
|
|
||||||
int yield = (int) (((long) this.culled << Q) / this.tested);
|
int yield = (int) (((long) this.culled << Q) / this.tested);
|
||||||
|
|
||||||
if (this.probing) {
|
if (this.probing) {
|
||||||
if (yield >= HIGH_YIELD) {
|
int removed = (this.visibleEma >> 4) - visible;
|
||||||
|
int added = this.tested - (this.testedEma >> 4);
|
||||||
|
|
||||||
|
if (removed > 0 && (long) removed * TESTS_PER_REMOVED >= added) {
|
||||||
this.limit = this.ceiling;
|
this.limit = this.ceiling;
|
||||||
this.yieldEma = yield;
|
this.yieldEma = yield;
|
||||||
}
|
}
|
||||||
@@ -132,6 +159,8 @@ public final class RasterBudget {
|
|||||||
public void copyFrom(RasterBudget other) {
|
public void copyFrom(RasterBudget other) {
|
||||||
this.limit = other.limit;
|
this.limit = other.limit;
|
||||||
this.yieldEma = other.yieldEma;
|
this.yieldEma = other.yieldEma;
|
||||||
|
this.visibleEma = other.visibleEma;
|
||||||
|
this.testedEma = other.testedEma;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int limit() {
|
public int limit() {
|
||||||
|
|||||||
+58
-3
@@ -23,6 +23,15 @@ class RasterBudgetTest {
|
|||||||
|
|
||||||
/** As {@link #frame(RasterBudget, int, double)}, with the sections spread only out to {@code farthest}. */
|
/** As {@link #frame(RasterBudget, int, double)}, with the sections spread only out to {@code farthest}. */
|
||||||
private static void frame(RasterBudget budget, int far, int farthest, double culledFraction) {
|
private static void frame(RasterBudget budget, int far, int farthest, double culledFraction) {
|
||||||
|
frame(budget, far, farthest, culledFraction, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** As {@link #frame(RasterBudget, int, double)}, where each cull also hides {@code removedPerCull - 1} sections behind it. */
|
||||||
|
private static void frame(RasterBudget budget, int far, double culledFraction, int removedPerCull) {
|
||||||
|
frame(budget, far, FARTHEST, culledFraction, removedPerCull);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void frame(RasterBudget budget, int far, int farthest, double culledFraction, int removedPerCull) {
|
||||||
budget.beginFrame(CEILING);
|
budget.beginFrame(CEILING);
|
||||||
int admitted = 0;
|
int admitted = 0;
|
||||||
|
|
||||||
@@ -37,7 +46,7 @@ class RasterBudgetTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
budget.endFrame();
|
budget.endFrame(Math.max(0, far - budget.culled() * removedPerCull));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A frame where every admitted test beyond {@code hiddenPast} culls and none nearer does: a cliff at that distance. */
|
/** A frame where every admitted test beyond {@code hiddenPast} culls and none nearer does: a cliff at that distance. */
|
||||||
@@ -52,7 +61,7 @@ class RasterBudgetTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
budget.endFrame();
|
budget.endFrame(far - budget.culled());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The {@code i}th of {@code far} squared distances spread evenly from just past the near range to {@code farthest}. */
|
/** The {@code i}th of {@code far} squared distances spread evenly from just past the near range to {@code farthest}. */
|
||||||
@@ -187,7 +196,7 @@ class RasterBudgetTest {
|
|||||||
assertFalse(budget.shouldTest(RasterBudget.FLOOR * 4 + 1));
|
assertFalse(budget.shouldTest(RasterBudget.FLOOR * 4 + 1));
|
||||||
}
|
}
|
||||||
assertTrue(budget.shouldTest(RasterBudget.FLOOR * 4));
|
assertTrue(budget.shouldTest(RasterBudget.FLOOR * 4));
|
||||||
budget.endFrame();
|
budget.endFrame(0);
|
||||||
|
|
||||||
budget.pin(RasterBudget.UNBOUNDED);
|
budget.pin(RasterBudget.UNBOUNDED);
|
||||||
budget.beginFrame(CEILING);
|
budget.beginFrame(CEILING);
|
||||||
@@ -261,4 +270,50 @@ class RasterBudgetTest {
|
|||||||
assertFalse(budget.wasProbe());
|
assertFalse(budget.wasProbe());
|
||||||
assertFalse(budget.wasFullPass());
|
assertFalse(budget.wasFullPass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void probeFromTheFloorStartsAFullPassThatRemovesEnough() {
|
||||||
|
var budget = new RasterBudget();
|
||||||
|
|
||||||
|
// below the yield that doubles the limit, but each cull hides a cave system
|
||||||
|
for (int i = 1; i < RasterBudget.PROBE_PERIOD; i++) {
|
||||||
|
frame(budget, 4000, 0.1, 20);
|
||||||
|
assertEquals(RasterBudget.FLOOR, budget.limit(), "frame " + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
frame(budget, 4000, 0.1, 20);
|
||||||
|
assertTrue(budget.wasProbe());
|
||||||
|
assertEquals(CEILING, budget.limit());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void probeIgnoresAHighYieldThatRemovesTooLittleForItsTests() {
|
||||||
|
var budget = new RasterBudget();
|
||||||
|
// gentle hills: the far quarter of what the probe tests culls, but each cull hides only itself
|
||||||
|
int hiddenPast = (FARTHEST * 3) / 4;
|
||||||
|
|
||||||
|
for (int i = 1; i < RasterBudget.PROBE_PERIOD; i++) {
|
||||||
|
frameHiddenBeyond(budget, 4000, hiddenPast);
|
||||||
|
}
|
||||||
|
|
||||||
|
frameHiddenBeyond(budget, 4000, hiddenPast);
|
||||||
|
assertTrue(budget.wasProbe());
|
||||||
|
assertTrue(budget.culled() * 100 >= budget.tested() * 15, "the probe's yield alone would have paid");
|
||||||
|
assertEquals(RasterBudget.FLOOR, budget.limit());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fullPassIsNotProbed() {
|
||||||
|
var budget = new RasterBudget();
|
||||||
|
|
||||||
|
for (int i = 0; i < 16; i++) {
|
||||||
|
frame(budget, 4000, 0.9);
|
||||||
|
}
|
||||||
|
assertEquals(CEILING, budget.limit());
|
||||||
|
|
||||||
|
for (int i = 0; i < 2 * RasterBudget.PROBE_PERIOD; i++) {
|
||||||
|
frame(budget, 4000, 0.9);
|
||||||
|
assertFalse(budget.wasProbe(), "frame " + i);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user