mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-22 13:19:44 +00:00
SDL presenter fixes and frame pacer adjustments (#2025)
This commit is contained in:
1 parent
553dec5b6d
commit
b2f597a73a
17 files changed
+336
-93
No files matched your search
@@ -2,6 +2,7 @@ package com.gtnewhorizons.angelica.debug.flyby;
|
||||
|
||||
import com.gtnewhorizons.angelica.config.SystemProperties;
|
||||
import com.gtnewhorizons.angelica.glsm.profiling.Tracy;
|
||||
import com.gtnewhorizons.angelica.rendering.FramePacer;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent;
|
||||
import net.minecraft.client.Minecraft;
|
||||
@@ -279,6 +280,7 @@ public final class FlybyRunner {
|
||||
this.frameCount = 0;
|
||||
this.lastFrameNs = 0L;
|
||||
this.runStartNs = System.nanoTime();
|
||||
FramePacer.beginStats();
|
||||
if (Tracy.ENABLED) {
|
||||
Tracy.message("flyby start route=" + this.route.id() + " length=" + this.runLength + this.route.lengthUnit() + " speed=" + this.route.speedOr(this.speed) + "b/t ticks=" + this.runTicks + " sdlgpu=" + SystemProperties.USE_SDL_GPU);
|
||||
}
|
||||
@@ -384,6 +386,7 @@ public final class FlybyRunner {
|
||||
|
||||
final String summary = this.summarise(elapsedNs, player);
|
||||
LOGGER.info(summary);
|
||||
LOGGER.info(FramePacer.endStats());
|
||||
if (mc.thePlayer != null) {
|
||||
mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.AQUA + "[Angelica] " + EnumChatFormatting.WHITE + summary));
|
||||
}
|
||||
@@ -438,6 +441,13 @@ public final class FlybyRunner {
|
||||
if (this.isActive()) {
|
||||
LOGGER.info("Flyby cancelled");
|
||||
this.state = State.DONE;
|
||||
Tracy.sectionLeave(this.warmupSection);
|
||||
this.warmupSection = 0L;
|
||||
Tracy.sectionLeave(this.legSection);
|
||||
this.legSection = 0L;
|
||||
Tracy.sectionLeave(this.runSection);
|
||||
this.runSection = 0L;
|
||||
FramePacer.endStats();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.gtnewhorizons.angelica.AngelicaMod;
|
||||
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.VSyncMode;
|
||||
import com.gtnewhorizons.angelica.glsm.profiling.Tracy;
|
||||
import com.gtnewhorizons.angelica.glsm.profiling.TracyBackend;
|
||||
|
||||
import java.util.concurrent.locks.LockSupport;
|
||||
|
||||
@@ -25,16 +26,21 @@ public final class FramePacer {
|
||||
private static final Tracy.ZoneId Z_PACER_IDLE = Tracy.zoneId("pacerIdleWork", Tracy.COLOR_CLIENT);
|
||||
private static final Tracy.ZoneId Z_PACER_SLEEP = Tracy.zoneId("pacerSleep", Tracy.COLOR_SWAP);
|
||||
private static final long P_FRAME_GATE_US = Tracy.plotHandle("frameGateUs");
|
||||
private static final long P_SLACK_US = Tracy.plotHandle("pacer.slackUs");
|
||||
private static final long P_SPIN_US = Tracy.plotHandle("pacer.spinUs");
|
||||
private static final long P_CEILING_HZ = Tracy.plotHandle("pacer.ceilingHz");
|
||||
private static final long P_GATE_ACTIVE = Tracy.plotHandle("pacer.gateActive");
|
||||
|
||||
private static long deadline;
|
||||
private static long lastFrameTime;
|
||||
|
||||
private static boolean warnedUncapped;
|
||||
private static boolean pacedLastFrame;
|
||||
private static boolean statsActive;
|
||||
|
||||
private static VSyncMode lastVSyncMode = VSyncMode.ON;
|
||||
private static int lastCapHz;
|
||||
private static int lastRefreshHz;
|
||||
private static int lastCeilingHz;
|
||||
|
||||
private static IdleWork idleWork;
|
||||
private static PacerSleeper sleeper = newSleeper();
|
||||
@@ -47,7 +53,6 @@ public final class FramePacer {
|
||||
private static int loggedRefreshHz;
|
||||
private static int loggedCapHz;
|
||||
|
||||
private static boolean gateBlocking;
|
||||
private static long lastSeenGateEnd;
|
||||
|
||||
private FramePacer() {}
|
||||
@@ -60,6 +65,35 @@ public final class FramePacer {
|
||||
idleWork = work;
|
||||
}
|
||||
|
||||
public static boolean pacedLastFrame() {
|
||||
return pacedLastFrame;
|
||||
}
|
||||
|
||||
public static void beginStats() {
|
||||
sleeper.resetStats();
|
||||
statsActive = true;
|
||||
}
|
||||
|
||||
public static String endStats() {
|
||||
statsActive = false;
|
||||
final String summary = sleeper.summary(configLine());
|
||||
Tracy.message(summary);
|
||||
return summary;
|
||||
}
|
||||
|
||||
static boolean statsActive() {
|
||||
return statsActive;
|
||||
}
|
||||
|
||||
static String configLine(VSyncMode mode, int refreshHz, int capHz, int ceilingHz) {
|
||||
return "cfg=[" + mode + " refresh=" + refreshHz + "Hz cap=" + capHz + " ceiling=" + ceilingHz + "Hz]";
|
||||
}
|
||||
|
||||
private static String configLine() {
|
||||
return configLine(lastVSyncMode, lastRefreshHz, lastCapHz,
|
||||
pacingCeilingHz(lastVSyncMode, lastCapHz, lastRefreshHz, gateActive()));
|
||||
}
|
||||
|
||||
public static int plausibleRefreshHz(int hz) {
|
||||
return hz >= MIN_PLAUSIBLE_REFRESH_HZ && hz <= MAX_PLAUSIBLE_REFRESH_HZ ? hz : 0;
|
||||
}
|
||||
@@ -155,11 +189,6 @@ public final class FramePacer {
|
||||
}
|
||||
|
||||
public static long pace(int ceilingHz, Runnable renderAheadWait) {
|
||||
if (ceilingHz != lastCeilingHz) {
|
||||
lastCeilingHz = ceilingHz;
|
||||
deadline = 0L;
|
||||
}
|
||||
|
||||
long now = System.nanoTime();
|
||||
long phaseEnd;
|
||||
|
||||
@@ -167,6 +196,9 @@ public final class FramePacer {
|
||||
final long thresholdNanos = gateBlockThresholdNanos(targetNanos);
|
||||
long frameGateNanos = 0L;
|
||||
|
||||
pacedLastFrame = ceilingHz > 0;
|
||||
Tracy.plotInt(P_CEILING_HZ, ceilingHz);
|
||||
|
||||
if (ceilingHz > 0) {
|
||||
frameGateNanos = GLStateManager.lastFrameGateNanos();
|
||||
final long gateEnd = GLStateManager.lastFrameGateEndNanos();
|
||||
@@ -192,8 +224,11 @@ public final class FramePacer {
|
||||
now = phaseEnd;
|
||||
}
|
||||
|
||||
if (now < deadline) {
|
||||
final long preSleep = now;
|
||||
final long slackNanos = deadline - now;
|
||||
Tracy.plotInt(P_SLACK_US, slackNanos / 1000);
|
||||
|
||||
final boolean slept = now < deadline;
|
||||
if (slept) {
|
||||
Tracy.beginZone(Z_PACER_SLEEP);
|
||||
try {
|
||||
now = sleeper.sleepUntil(deadline, now);
|
||||
@@ -201,6 +236,10 @@ public final class FramePacer {
|
||||
Tracy.endZone();
|
||||
}
|
||||
}
|
||||
if (Tracy.ENABLED || statsActive) {
|
||||
sleeper.noteFrame(slackNanos, slept ? now - deadline : 0L, slept);
|
||||
Tracy.plotInt(P_SPIN_US, sleeper.lastSpinNanos / 1000);
|
||||
}
|
||||
} else {
|
||||
deadline = 0L;
|
||||
}
|
||||
@@ -215,15 +254,18 @@ public final class FramePacer {
|
||||
final long waitNanos = phaseEnd - now;
|
||||
now = phaseEnd;
|
||||
if (ceilingHz > 0 && waitNanos > thresholdNanos) deadline = now;
|
||||
gateBlocking = observeGate(ceilingHz > 0 ? Math.max(frameGateNanos, waitNanos) : 0L, thresholdNanos);
|
||||
final boolean gateBlocking = observeGate(ceilingHz > 0 ? Math.max(frameGateNanos, waitNanos) : 0L, thresholdNanos);
|
||||
Tracy.plotInt(P_GATE_ACTIVE, gateBlocking ? 1 : 0);
|
||||
|
||||
GLStateManager.pumpDisplayMessages();
|
||||
|
||||
if (pacingStatusChanged(gateBlocking, lastVSyncMode, lastRefreshHz, lastCapHz)) {
|
||||
final boolean blocking = settledBlocking;
|
||||
final String line = pacingLine(lastVSyncMode, lastRefreshHz, lastCapHz, blocking);
|
||||
if (vsyncNotHonoured(lastVSyncMode, blocking)) AngelicaMod.LOGGER.warn(line);
|
||||
final boolean notHonoured = vsyncNotHonoured(lastVSyncMode, blocking);
|
||||
if (notHonoured) AngelicaMod.LOGGER.warn(line);
|
||||
else AngelicaMod.LOGGER.info(line);
|
||||
Tracy.message(line, notHonoured ? TracyBackend.SEVERITY_WARNING : TracyBackend.SEVERITY_INFO);
|
||||
}
|
||||
|
||||
final long period = lastFrameTime == 0L ? 0L : now - lastFrameTime;
|
||||
|
||||
@@ -5,31 +5,50 @@ import java.util.function.LongSupplier;
|
||||
|
||||
final class PacerSleeper {
|
||||
|
||||
private static final long SPIN_FLOOR_NANOS = 100_000L;
|
||||
private static final long MIN_PARK_NANOS = 1_000_000L;
|
||||
private static final long MAX_OVERSHOOT_NANOS = 4_000_000L;
|
||||
private static final int SLOTS = 10;
|
||||
static final int SLOTS = 10;
|
||||
|
||||
static final long MIN_SPIN_FLOOR_NANOS = 20_000L;
|
||||
private static final long MAX_SPIN_FLOOR_NANOS = 500_000L;
|
||||
|
||||
private final LongSupplier clock;
|
||||
private final LongConsumer parker;
|
||||
private final boolean spinWait;
|
||||
private final long[] overshoots = new long[SLOTS];
|
||||
private long overshootSum;
|
||||
private long spinFloorNanos = MIN_SPIN_FLOOR_NANOS;
|
||||
private int offset;
|
||||
|
||||
long lastSpinNanos;
|
||||
int lastParks;
|
||||
|
||||
private long frames;
|
||||
private long slackSum;
|
||||
private long parkTotal;
|
||||
private long lateFrames;
|
||||
private long lateMax;
|
||||
|
||||
PacerSleeper(LongSupplier clock, LongConsumer parker, boolean spinWait) {
|
||||
this.clock = clock;
|
||||
this.parker = parker;
|
||||
this.spinWait = spinWait;
|
||||
}
|
||||
|
||||
long spinFloorNanos() {
|
||||
return spinFloorNanos;
|
||||
}
|
||||
|
||||
long sleepUntil(long deadlineNanos, long now) {
|
||||
resetLastFrame();
|
||||
|
||||
long spinFloor = spinFloorNanos;
|
||||
while (true) {
|
||||
final long remaining = deadlineNanos - now;
|
||||
if (remaining <= SPIN_FLOOR_NANOS) break;
|
||||
if (remaining <= spinFloor) break;
|
||||
if (Thread.currentThread().isInterrupted()) return clock.getAsLong();
|
||||
|
||||
long request = remaining - avgOvershootNanos() - SPIN_FLOOR_NANOS;
|
||||
long request = remaining - avgOvershootNanos() - spinFloor;
|
||||
if (request <= 0L) {
|
||||
if (remaining <= MIN_PARK_NANOS) break;
|
||||
request = MIN_PARK_NANOS;
|
||||
@@ -39,17 +58,51 @@ final class PacerSleeper {
|
||||
parker.accept(request);
|
||||
now = clock.getAsLong();
|
||||
record(Math.min(Math.max(0L, now - before - request), MAX_OVERSHOOT_NANOS));
|
||||
spinFloor = spinFloorNanos;
|
||||
lastParks++;
|
||||
}
|
||||
|
||||
if (spinWait) {
|
||||
final long spinStart = now;
|
||||
while (now < deadlineNanos) {
|
||||
Thread.yield();
|
||||
if (deadlineNanos - now > spinFloor) Thread.yield();
|
||||
else Thread.onSpinWait();
|
||||
now = clock.getAsLong();
|
||||
}
|
||||
lastSpinNanos = now - spinStart;
|
||||
}
|
||||
return now;
|
||||
}
|
||||
|
||||
void noteFrame(long slackNanos, long wakeLateNanos, boolean slept) {
|
||||
if (!slept) resetLastFrame();
|
||||
|
||||
frames++;
|
||||
slackSum += slackNanos;
|
||||
parkTotal += lastParks;
|
||||
if (slackNanos < 0L && -slackNanos > lateMax) lateMax = -slackNanos;
|
||||
if (slackNanos < 0L || wakeLateNanos > 0L) lateFrames++;
|
||||
}
|
||||
|
||||
private void resetLastFrame() {
|
||||
lastSpinNanos = 0L;
|
||||
lastParks = 0;
|
||||
}
|
||||
|
||||
void resetStats() {
|
||||
frames = 0L;
|
||||
slackSum = 0L;
|
||||
parkTotal = 0L;
|
||||
lateFrames = 0L;
|
||||
lateMax = 0L;
|
||||
}
|
||||
|
||||
String summary(String config) {
|
||||
if (frames == 0L) return "pacer: " + config + " no paced frames";
|
||||
return "pacer: " + config + " frames=" + frames + " parks=" + parkTotal + " slackUs avg=" + slackSum / frames / 1000L + " floorUs=" + spinFloorNanos / 1000L
|
||||
+ " overshootUs est=" + avgOvershootNanos() / 1000L + " lateFrames=" + lateFrames + " lateMaxUs=" + lateMax / 1000L;
|
||||
}
|
||||
|
||||
long avgOvershootNanos() {
|
||||
return overshootSum / SLOTS;
|
||||
}
|
||||
@@ -58,5 +111,12 @@ final class PacerSleeper {
|
||||
overshootSum += nanos - overshoots[offset];
|
||||
overshoots[offset] = nanos;
|
||||
offset = (offset + 1) % SLOTS;
|
||||
|
||||
long max = 0L;
|
||||
for (final long o : overshoots) {
|
||||
if (o > max) max = o;
|
||||
}
|
||||
final long jitter = max - avgOvershootNanos();
|
||||
spinFloorNanos = Math.max(MIN_SPIN_FLOOR_NANOS, Math.min(MAX_SPIN_FLOOR_NANOS, jitter));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user