mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-21 21:02:15 +00:00
FPS pacer work (#2086)
This commit is contained in:
1 parent
6f054a09a9
commit
2845c9e078
44 files changed
+3304
-491
No files matched your search
@@ -261,11 +261,6 @@ public class AngelicaConfig {
|
||||
@Config.RangeDouble(min = 16D, max = 64D)
|
||||
public static double mobSpawnerRenderDistance;
|
||||
|
||||
@Config.Comment("Switches to an alternate FPS limiter that gives more stable frametimes, in exchange for slightly " +
|
||||
"more latency. Will never introduce more than one frame of latency, and has a lower impact at higher framerates.")
|
||||
@Config.DefaultBoolean(false)
|
||||
public static boolean sleepBeforeSwap;
|
||||
|
||||
@Config.Comment("Allows unicode languages to use an odd gui scale")
|
||||
@Config.DefaultBoolean(true)
|
||||
public static boolean removeUnicodeEvenScaling;
|
||||
|
||||
@@ -528,6 +528,15 @@ public enum Mixins implements IMixins {
|
||||
"angelica.zoom.MixinMouseFilter"
|
||||
)),
|
||||
|
||||
ANGELICA_FPS_REDUCER(new MixinBuilder("Reduce frame rate and volume while unfocused, minimized, or idle")
|
||||
.setPhase(Phase.EARLY)
|
||||
.addClientMixins(
|
||||
"angelica.fpsreducer.MixinGameSettings_FpsReducerVolume",
|
||||
"angelica.fpsreducer.MixinGuiScreen_FpsReducerInput",
|
||||
"angelica.fpsreducer.MixinMinecraft_FpsReducerInput",
|
||||
"angelica.fpsreducer.MixinSoundManagerLoader_FpsReducerVolume"
|
||||
)),
|
||||
|
||||
HUD_CACHING(new MixinBuilder()
|
||||
.setPhase(Phase.EARLY)
|
||||
.setApplyIf(() -> AngelicaConfig.enableHudCaching)
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.gtnewhorizons.angelica.glsm.profiling.Tracy;
|
||||
import com.gtnewhorizons.angelica.glsm.profiling.TracyBackend;
|
||||
import com.gtnewhorizons.angelica.glsm.streaming.TessellatorStreamingDrawer;
|
||||
import com.gtnewhorizons.angelica.rendering.AngelicaRenderQueue;
|
||||
import com.gtnewhorizons.angelica.rendering.FramePacer;
|
||||
import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasWorldRenderer;
|
||||
import com.gtnewhorizons.angelica.rendering.celeritas.TerrainDrawStats;
|
||||
import com.gtnewhorizons.angelica.rendering.culling.GpuCulling;
|
||||
@@ -121,6 +122,17 @@ public final class TracyFramePlots {
|
||||
|
||||
private TracyFramePlots() {}
|
||||
|
||||
private static final long P_FRAME_GATE_US = Tracy.plotHandle("frameGateUs");
|
||||
private static final long P_PACER_GPU_WAIT_US = Tracy.plotHandle("pacer.gpuWaitUs");
|
||||
private static final long P_PACER_SLACK_US = Tracy.plotHandle("pacer.slackUs");
|
||||
private static final long P_PACER_SPIN_US = Tracy.plotHandle("pacer.spinUs");
|
||||
private static final long P_PACER_CEILING_HZ = Tracy.plotHandle("pacer.ceilingHz");
|
||||
private static final long P_PACER_MARGIN_US = Tracy.plotHandle("pacer.marginUs");
|
||||
private static final long P_PACER_LOCKED = Tracy.plotHandle("pacer.locked");
|
||||
private static final long P_PACER_PROBING = Tracy.plotHandle("pacer.probing");
|
||||
private static final long P_PACER_PRESENT_INTERVAL_US = Tracy.plotHandle("pacer.presentIntervalUs");
|
||||
private static final long P_PACER_IDLE_OVERSHOOT_US = Tracy.plotHandle("pacer.idleOvershootUs");
|
||||
|
||||
public static void onFrame(Minecraft mc) {
|
||||
if (!Tracy.ENABLED) return;
|
||||
|
||||
@@ -132,6 +144,18 @@ public final class TracyFramePlots {
|
||||
final Runtime runtime = Runtime.getRuntime();
|
||||
Tracy.plotInt(P_HEAP_USED, runtime.totalMemory() - runtime.freeMemory());
|
||||
}
|
||||
|
||||
Tracy.plotInt(P_FRAME_GATE_US, FramePacer.gateDurationNanos() / 1000L);
|
||||
Tracy.plotInt(P_PACER_GPU_WAIT_US, FramePacer.gpuWaitNanos() / 1000L);
|
||||
Tracy.plotInt(P_PACER_SLACK_US, FramePacer.slackNanos() / 1000L);
|
||||
Tracy.plotInt(P_PACER_SPIN_US, FramePacer.spinNanos() / 1000L);
|
||||
Tracy.plotInt(P_PACER_MARGIN_US, FramePacer.marginNanos() / 1000L);
|
||||
Tracy.plotInt(P_PACER_PRESENT_INTERVAL_US, FramePacer.presentIntervalNanos() / 1000L);
|
||||
Tracy.plotInt(P_PACER_IDLE_OVERSHOOT_US, FramePacer.idleOvershootNanos() / 1000L);
|
||||
Tracy.plotInt(P_PACER_CEILING_HZ, FramePacer.effectiveCapHz());
|
||||
Tracy.plotInt(P_PACER_LOCKED, FramePacer.locked() ? 1 : 0);
|
||||
Tracy.plotInt(P_PACER_PROBING, FramePacer.probing() ? 1 : 0);
|
||||
|
||||
final int chunksUpdated = WorldRenderer.chunksUpdated;
|
||||
Tracy.plotInt(P_CHUNK_UPDATES, chunksUpdated - lastChunkUpdates);
|
||||
lastChunkUpdates = chunksUpdated;
|
||||
|
||||
@@ -63,6 +63,7 @@ import com.gtnewhorizons.angelica.mixins.interfaces.IGameSettingsExt;
|
||||
import com.gtnewhorizons.angelica.render.CloudRenderer;
|
||||
import com.gtnewhorizons.angelica.render.EmissiveTextureAutoloader;
|
||||
import com.gtnewhorizons.angelica.rendering.AngelicaBlockSafetyRegistry;
|
||||
import com.gtnewhorizons.angelica.rendering.FpsReducer;
|
||||
import com.gtnewhorizons.angelica.rendering.FramePacer;
|
||||
import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasDebugScreenHandler;
|
||||
import com.gtnewhorizons.angelica.rendering.celeritas.CeleritasSetup;
|
||||
@@ -187,6 +188,7 @@ public final class ClientProxy extends CommonProxy {
|
||||
if (AngelicaConfig.enableZoom) {
|
||||
Zoom.init();
|
||||
}
|
||||
FpsReducer.init();
|
||||
AngelicaConfig.applyGpuCullingMode();
|
||||
if (AngelicaConfig.enableDynamicLights) {
|
||||
EntityLightConfig.init(new java.io.File(mc.mcDataDir, "config"));
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
package com.gtnewhorizons.angelica.rendering;
|
||||
|
||||
import com.gtnewhorizons.angelica.rendering.ReducerStateMachine.State;
|
||||
|
||||
import com.gtnewhorizons.angelica.AngelicaMod;
|
||||
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import com.gtnewhorizons.angelica.proxy.ClientProxy;
|
||||
import cpw.mods.fml.client.registry.ClientRegistry;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent;
|
||||
import me.jellysquid.mods.sodium.client.gui.SodiumGameOptions;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.audio.SoundCategory;
|
||||
import net.minecraft.client.audio.SoundHandler;
|
||||
import net.minecraft.client.gui.GuiMainMenu;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraft.client.settings.KeyBinding;
|
||||
import net.minecraftforge.client.event.GuiScreenEvent;
|
||||
import net.minecraftforge.client.event.RenderGameOverlayEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.opengl.Display;
|
||||
|
||||
public final class FpsReducer {
|
||||
|
||||
private static final ReducerStateMachine MACHINE = new ReducerStateMachine(System::nanoTime);
|
||||
private static final FpsReducer INSTANCE = new FpsReducer();
|
||||
|
||||
private static KeyBinding forceKey;
|
||||
private static KeyBinding disableKey;
|
||||
|
||||
private static State applied;
|
||||
private static int appliedVolumePercent = 100;
|
||||
private static boolean configDirty;
|
||||
private static String hudText;
|
||||
private static long lastActiveMillis = Minecraft.getSystemTime();
|
||||
private static volatile int volumePercent = 100;
|
||||
|
||||
private FpsReducer() {}
|
||||
|
||||
public static void init() {
|
||||
forceKey = new KeyBinding("angelica.keybind.fps_reducer_force", 0, "key.categories.misc");
|
||||
disableKey = new KeyBinding("angelica.keybind.fps_reducer_disable", 0, "key.categories.misc");
|
||||
ClientRegistry.registerKeyBinding(forceKey);
|
||||
ClientRegistry.registerKeyBinding(disableKey);
|
||||
FMLCommonHandler.instance().bus().register(INSTANCE);
|
||||
MinecraftForge.EVENT_BUS.register(INSTANCE);
|
||||
}
|
||||
|
||||
public static void evaluateFrame() {
|
||||
final SodiumGameOptions.ReducerSettings s = ClientProxy.options().reducer;
|
||||
final boolean created = s.enabled && Display.isCreated();
|
||||
final boolean visible = !created || Display.isVisible();
|
||||
final boolean active = !created || Display.isActive();
|
||||
final boolean changed = MACHINE.evaluate(s.enabled, visible, active, s.idleTimeoutMinutes * 60_000_000_000L);
|
||||
if (changed || configDirty) {
|
||||
applyTransition(s);
|
||||
}
|
||||
}
|
||||
|
||||
private static void applyTransition(SodiumGameOptions.ReducerSettings s) {
|
||||
final State prev = applied;
|
||||
final State next = MACHINE.state();
|
||||
applied = next;
|
||||
configDirty = false;
|
||||
|
||||
if (prev != null && prev != next) {
|
||||
AngelicaMod.LOGGER.info("FPS reducer: {} -> {}", prev, next);
|
||||
}
|
||||
|
||||
final int percent = ReducerStateMachine.stateVolume(next, s.unfocusedVolume, s.minimizedVolume, s.idleVolume);
|
||||
if (percent != appliedVolumePercent) {
|
||||
appliedVolumePercent = percent;
|
||||
applyVolume(percent);
|
||||
}
|
||||
|
||||
if (next == State.FORCED) {
|
||||
hudText = I18n.format("angelica.fps_reducer.hud.forced");
|
||||
} else if (MACHINE.keybindDisabled()) {
|
||||
hudText = I18n.format("angelica.fps_reducer.hud.disabled");
|
||||
} else {
|
||||
hudText = null;
|
||||
}
|
||||
|
||||
GLStateManager.setPresentSuppressed(next == State.MINIMIZED);
|
||||
}
|
||||
|
||||
public static int effectiveCap(int userLimit, boolean inMenu) {
|
||||
final SodiumGameOptions.ReducerSettings s = ClientProxy.options().reducer;
|
||||
final int stateCap = ReducerStateMachine.stateCap(MACHINE.state(), s.unfocusedFpsLimit, s.idleFpsLimit);
|
||||
return ReducerStateMachine.mergeCap(userLimit, stateCap, inMenu, s.limitMenuFrameRate);
|
||||
}
|
||||
|
||||
public static boolean skipRender() {
|
||||
return MACHINE.state() == State.MINIMIZED;
|
||||
}
|
||||
|
||||
public static void onRenderSkipped() {
|
||||
final Minecraft mc = Minecraft.getMinecraft();
|
||||
if (Display.isActive()) {
|
||||
lastActiveMillis = Minecraft.getSystemTime();
|
||||
return;
|
||||
}
|
||||
if (mc.theWorld == null || mc.currentScreen != null || !mc.gameSettings.pauseOnLostFocus) return;
|
||||
if (mc.gameSettings.touchscreen && Mouse.isButtonDown(1)) return;
|
||||
if (Minecraft.getSystemTime() - lastActiveMillis > 500L) {
|
||||
mc.displayInGameMenu();
|
||||
}
|
||||
}
|
||||
|
||||
public static float scaleVolume(float user) {
|
||||
return ReducerStateMachine.masterVolume(user, volumePercent);
|
||||
}
|
||||
|
||||
private static void applyVolume(int percent) {
|
||||
volumePercent = percent;
|
||||
final Minecraft mc = Minecraft.getMinecraft();
|
||||
final SoundHandler handler = mc.getSoundHandler();
|
||||
if (handler == null || mc.gameSettings == null) return;
|
||||
final float user = mc.gameSettings.getSoundLevel(SoundCategory.MASTER);
|
||||
handler.setSoundLevel(SoundCategory.MASTER, scaleVolume(user));
|
||||
}
|
||||
|
||||
public static void markConfigChanged() {
|
||||
configDirty = true;
|
||||
}
|
||||
|
||||
public static String debugTag() {
|
||||
return applied == null ? null : applied.debugTag();
|
||||
}
|
||||
|
||||
public static void onInput() {
|
||||
MACHINE.onInput();
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onClientTick(TickEvent.ClientTickEvent event) {
|
||||
if (event.phase != TickEvent.Phase.END) return;
|
||||
|
||||
if (forceKey != null) {
|
||||
while (forceKey.isPressed()) {
|
||||
MACHINE.toggleForced();
|
||||
configDirty = true;
|
||||
}
|
||||
}
|
||||
if (disableKey != null) {
|
||||
while (disableKey.isPressed()) {
|
||||
MACHINE.toggleKeybindDisabled();
|
||||
configDirty = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void drawHud(boolean requireNoWorld) {
|
||||
final Minecraft mc = Minecraft.getMinecraft();
|
||||
if (mc.gameSettings.showDebugInfo) return;
|
||||
if (requireNoWorld && mc.theWorld != null) return;
|
||||
mc.fontRenderer.drawStringWithShadow(hudText, 2, 2, 0xFFFFFF);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onRenderOverlay(RenderGameOverlayEvent.Post event) {
|
||||
if (event.type != RenderGameOverlayEvent.ElementType.ALL || hudText == null) return;
|
||||
drawHud(false);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onDrawScreen(GuiScreenEvent.DrawScreenEvent.Post event) {
|
||||
if (hudText == null || !(event.gui instanceof GuiMainMenu)) return;
|
||||
drawHud(true);
|
||||
}
|
||||
}
|
||||
@@ -2,163 +2,199 @@ package com.gtnewhorizons.angelica.rendering;
|
||||
|
||||
import com.gtnewhorizons.angelica.AngelicaMod;
|
||||
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.RenderBackend.GateSample;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.RenderBackend;
|
||||
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;
|
||||
import java.util.function.LongConsumer;
|
||||
|
||||
import static com.gtnewhorizons.angelica.glsm.backend.BackendManager.RENDER_BACKEND;
|
||||
|
||||
public final class FramePacer {
|
||||
|
||||
public interface IdleWork {
|
||||
void run(long deadlineNanos);
|
||||
interface Backend {
|
||||
VSyncMode effectiveVSyncMode();
|
||||
long refreshPeriodNanos();
|
||||
boolean vsyncHonored();
|
||||
int displayGeneration();
|
||||
void awaitPresent();
|
||||
void readGateSample(GateSample out);
|
||||
void pumpDisplayMessages();
|
||||
void parkNanos(long nanos);
|
||||
long nanoTime();
|
||||
}
|
||||
|
||||
private static final int MIN_PLAUSIBLE_REFRESH_HZ = 20;
|
||||
private static final int MAX_PLAUSIBLE_REFRESH_HZ = 1000;
|
||||
private static final long IDLE_WORK_MIN_BUDGET_NANOS = 500_000L;
|
||||
private static final long GATE_BLOCK_MIN_NANOS = 100_000L;
|
||||
private static final int GATE_STREAK_FRAMES = 60;
|
||||
static final int GATE_SETTLE_FRAMES = 120;
|
||||
static final int PACING_HEADROOM_PERCENT = 5;
|
||||
private static final class RenderBackendAdapter implements Backend {
|
||||
@Override public VSyncMode effectiveVSyncMode() { return RENDER_BACKEND.getEffectiveVSyncMode(); }
|
||||
@Override public long refreshPeriodNanos() { return RENDER_BACKEND.refreshPeriodNanos(); }
|
||||
@Override public boolean vsyncHonored() { return RENDER_BACKEND.vsyncHonored(); }
|
||||
@Override public int displayGeneration() { return RENDER_BACKEND.displayGeneration(); }
|
||||
@Override public void awaitPresent() { RENDER_BACKEND.awaitPresent(); }
|
||||
@Override public void readGateSample(GateSample out) { RENDER_BACKEND.readGateSample(out); }
|
||||
@Override public void pumpDisplayMessages() { GLStateManager.pumpDisplayMessages(); }
|
||||
@Override public void parkNanos(long nanos) { RENDER_BACKEND.parkNanos(nanos); }
|
||||
@Override public long nanoTime() { return System.nanoTime(); }
|
||||
}
|
||||
|
||||
private static final Tracy.ZoneId Z_PACER_WAIT = Tracy.zoneId("pacerBackpressure", Tracy.COLOR_SWAP);
|
||||
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 final Tracy.ZoneId Z_PACER_BEGIN = Tracy.zoneId("pacerBegin", Tracy.COLOR_CLIENT);
|
||||
private static final Tracy.ZoneId Z_PACER_PRESENT_SLEEP = Tracy.zoneId("pacerPresentSleep", Tracy.COLOR_SWAP);
|
||||
|
||||
private static long deadline;
|
||||
private static long lastFrameTime;
|
||||
|
||||
private static Backend BACKEND = new RenderBackendAdapter();
|
||||
|
||||
private static void parkNanos(long nanos) {
|
||||
BACKEND.parkNanos(nanos);
|
||||
}
|
||||
|
||||
private static long nanoTime() {
|
||||
return BACKEND.nanoTime();
|
||||
}
|
||||
|
||||
private static PacerSleeper sleeper = new PacerSleeper(FramePacer::nanoTime, FramePacer::parkNanos, true);
|
||||
static PacerCore core = new PacerCore(FramePacer::nanoTime, sleeper);
|
||||
private static final GateSample sample = new GateSample();
|
||||
|
||||
private static boolean warnedUncapped;
|
||||
private static boolean pacedLastFrame;
|
||||
private static boolean statsActive;
|
||||
private static int lastDisplayGeneration;
|
||||
|
||||
private static VSyncMode lastVSyncMode = VSyncMode.ON;
|
||||
private static int lastCapHz;
|
||||
private static int lastRefreshHz;
|
||||
private static VSyncMode currentMode = VSyncMode.ON;
|
||||
private static int currentRefreshHz;
|
||||
private static int currentCapHz;
|
||||
private static int currentEffectiveCapHz;
|
||||
|
||||
private static IdleWork idleWork;
|
||||
private static PacerSleeper sleeper = newSleeper();
|
||||
private static int gateStreak;
|
||||
private static Boolean settledBlocking;
|
||||
private static boolean candidateBlocking;
|
||||
private static int candidateFrames;
|
||||
private static Boolean loggedBlocking;
|
||||
private static VSyncMode loggedMode;
|
||||
private static int loggedRefreshHz;
|
||||
private static int loggedCapHz;
|
||||
|
||||
private static long lastSeenGateEnd;
|
||||
private static int loggedEffectiveCapHz;
|
||||
private static boolean loggedLocked;
|
||||
private static boolean loggedProbing;
|
||||
|
||||
private FramePacer() {}
|
||||
|
||||
private static PacerSleeper newSleeper() {
|
||||
return new PacerSleeper(System::nanoTime, LockSupport::parkNanos, true);
|
||||
public static void setIdleWork(LongConsumer work) {
|
||||
core.setIdleWork(work);
|
||||
}
|
||||
|
||||
public static void setIdleWork(IdleWork work) {
|
||||
idleWork = work;
|
||||
public static void invalidate() {
|
||||
core.invalidate();
|
||||
}
|
||||
|
||||
public static boolean pacedLastFrame() {
|
||||
return pacedLastFrame;
|
||||
return core.paced();
|
||||
}
|
||||
|
||||
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) {
|
||||
private 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()));
|
||||
return configLine(currentMode, currentRefreshHz, currentCapHz, currentEffectiveCapHz);
|
||||
}
|
||||
|
||||
public static int plausibleRefreshHz(int hz) {
|
||||
return hz >= MIN_PLAUSIBLE_REFRESH_HZ && hz <= MAX_PLAUSIBLE_REFRESH_HZ ? hz : 0;
|
||||
}
|
||||
|
||||
public static int withHeadroom(int hz, int percent) {
|
||||
if (hz <= 0) return 0;
|
||||
return (int) Math.max(hz + 1L, Math.round(hz * (100 + percent) / 100.0));
|
||||
}
|
||||
|
||||
public static int pacingCeilingHz(VSyncMode mode, int capHz, int refreshHz, boolean gateActive) {
|
||||
public static long endFrame(int capHz, Runnable renderAheadWait) {
|
||||
final VSyncMode mode = BACKEND.effectiveVSyncMode();
|
||||
final int cap = Math.max(capHz, 0);
|
||||
final int backstop = backstopHz(mode, refreshHz, gateActive);
|
||||
if (cap == 0) return backstop;
|
||||
if (backstop == 0) return cap;
|
||||
return Math.min(cap, backstop);
|
||||
}
|
||||
final long refreshPeriodNanos = BACKEND.refreshPeriodNanos();
|
||||
final boolean honored = BACKEND.vsyncHonored();
|
||||
final int refreshHz = refreshHz(mode, cap, refreshPeriodNanos);
|
||||
|
||||
static int backstopHz(VSyncMode mode, int refreshHz, boolean gateActive) {
|
||||
if (!mode.tearFree()) return 0;
|
||||
final int refresh = plausibleRefreshHz(refreshHz);
|
||||
if (mode == VSyncMode.ON && gateActive) return withHeadroom(refresh, PACING_HEADROOM_PERCENT);
|
||||
return refresh;
|
||||
}
|
||||
core.setRefreshPeriodNanos(refreshPeriodNanos);
|
||||
core.setTearFree(mode.tearFree());
|
||||
core.setVsyncOn(mode == VSyncMode.ON && honored);
|
||||
core.setCap(cap);
|
||||
|
||||
static boolean observeGate(long gateNanos, long thresholdNanos) {
|
||||
if (gateNanos > thresholdNanos) gateStreak = GATE_STREAK_FRAMES;
|
||||
else if (gateStreak > 0) gateStreak--;
|
||||
return gateStreak > 0;
|
||||
}
|
||||
|
||||
static boolean gateActive() {
|
||||
return gateStreak > 0;
|
||||
}
|
||||
|
||||
static Boolean settleGateBlocking(boolean observed) {
|
||||
if (settledBlocking != null && observed == settledBlocking) {
|
||||
candidateFrames = 0;
|
||||
} else if (candidateFrames == 0 || observed != candidateBlocking) {
|
||||
candidateBlocking = observed;
|
||||
candidateFrames = 1;
|
||||
} else if (++candidateFrames >= GATE_SETTLE_FRAMES) {
|
||||
settledBlocking = observed;
|
||||
candidateFrames = 0;
|
||||
final int generation = BACKEND.displayGeneration();
|
||||
if (generation != lastDisplayGeneration) {
|
||||
lastDisplayGeneration = generation;
|
||||
core.invalidate();
|
||||
}
|
||||
return settledBlocking;
|
||||
}
|
||||
|
||||
static boolean vsyncNotHonoured(VSyncMode mode, boolean gateBlocking) {
|
||||
return mode == VSyncMode.ON && !gateBlocking;
|
||||
}
|
||||
|
||||
static boolean pacingStatusChanged(boolean blocking, VSyncMode mode, int refreshHz, int capHz) {
|
||||
final Boolean settled = settleGateBlocking(blocking);
|
||||
if (settled == null) return false;
|
||||
if (settled.equals(loggedBlocking) && mode == loggedMode && refreshHz == loggedRefreshHz && capHz == loggedCapHz) {
|
||||
return false;
|
||||
if (mode == VSyncMode.ON && honored) {
|
||||
BACKEND.awaitPresent();
|
||||
}
|
||||
BACKEND.readGateSample(sample);
|
||||
core.onGateSample(sample.durationNanos, sample.endNanos);
|
||||
core.onGpuWait(sample.gpuWaitNanos);
|
||||
|
||||
Tracy.beginZone(Z_PACER_WAIT);
|
||||
try {
|
||||
if (renderAheadWait != null) {
|
||||
final long waitStart = nanoTime();
|
||||
renderAheadWait.run();
|
||||
core.onGpuWait(nanoTime() - waitStart);
|
||||
}
|
||||
} finally {
|
||||
Tracy.endZone();
|
||||
}
|
||||
|
||||
Tracy.beginZone(Z_PACER_BEGIN);
|
||||
try {
|
||||
core.beginFrame(nanoTime());
|
||||
} finally {
|
||||
Tracy.endZone();
|
||||
}
|
||||
|
||||
BACKEND.pumpDisplayMessages();
|
||||
core.markFrameStart(nanoTime());
|
||||
|
||||
final int effectiveCapHz = core.effectiveCapHz();
|
||||
final boolean locked = core.locked();
|
||||
final boolean probing = core.probing();
|
||||
|
||||
currentMode = mode;
|
||||
currentRefreshHz = refreshHz;
|
||||
currentCapHz = cap;
|
||||
currentEffectiveCapHz = effectiveCapHz;
|
||||
|
||||
maybeLog(mode, refreshHz, cap, effectiveCapHz, locked, probing);
|
||||
|
||||
return core.lastPresentIntervalNanos();
|
||||
}
|
||||
|
||||
public static void beforePresent() {
|
||||
final long firstClearBlockNanos = GLStateManager.takeFirstClearBlockNanos();
|
||||
Tracy.beginZone(Z_PACER_PRESENT_SLEEP);
|
||||
try {
|
||||
core.beforePresent(nanoTime(), firstClearBlockNanos);
|
||||
} finally {
|
||||
Tracy.endZone();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void maybeLog(VSyncMode mode, int refreshHz, int capHz, int effectiveCapHz, boolean locked, boolean probing) {
|
||||
if (mode == loggedMode && refreshHz == loggedRefreshHz && capHz == loggedCapHz
|
||||
&& effectiveCapHz == loggedEffectiveCapHz && locked == loggedLocked && probing == loggedProbing) {
|
||||
return;
|
||||
}
|
||||
loggedBlocking = settled;
|
||||
loggedMode = mode;
|
||||
loggedRefreshHz = refreshHz;
|
||||
loggedCapHz = capHz;
|
||||
return true;
|
||||
loggedEffectiveCapHz = effectiveCapHz;
|
||||
loggedLocked = locked;
|
||||
loggedProbing = probing;
|
||||
|
||||
final String line = pacingLine(mode, refreshHz, capHz, effectiveCapHz, locked, probing);
|
||||
final boolean wallClock = mode == VSyncMode.ON && !locked && !probing;
|
||||
if (wallClock) AngelicaMod.LOGGER.warn(line);
|
||||
else AngelicaMod.LOGGER.info(line);
|
||||
Tracy.message(line, wallClock ? TracyBackend.SEVERITY_WARNING : TracyBackend.SEVERITY_INFO);
|
||||
}
|
||||
|
||||
static String pacingLine(VSyncMode mode, int refreshHz, int capHz, boolean gateBlocking) {
|
||||
static String pacingLine(VSyncMode mode, int refreshHz, int capHz, int effectiveCapHz, boolean locked, boolean probing) {
|
||||
final StringBuilder sb = new StringBuilder("Frame pacing: ");
|
||||
sb.append(switch (mode) {
|
||||
case OFF -> "vsync off";
|
||||
@@ -166,141 +202,68 @@ public final class FramePacer {
|
||||
case ON -> "vsync";
|
||||
});
|
||||
if (mode.tearFree() && refreshHz > 0) sb.append(' ').append(refreshHz).append("Hz");
|
||||
if (capHz > 0) sb.append(", cap ").append(capHz);
|
||||
if (vsyncNotHonoured(mode, gateBlocking)) sb.append(", driver not blocking, paced by wall clock");
|
||||
else if (gateBlocking) sb.append(", hardware paced");
|
||||
if (mode == VSyncMode.ON) {
|
||||
if (locked) sb.append(", hardware paced");
|
||||
else if (probing) sb.append(", probing");
|
||||
else sb.append(", driver not blocking, paced by wall clock");
|
||||
}
|
||||
if (capHz > 0) {
|
||||
sb.append(", cap ").append(capHz);
|
||||
if (effectiveCapHz > 0 && effectiveCapHz != capHz) sb.append(" -> ").append(effectiveCapHz);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static long gateBlockThresholdNanos(long targetNanos) {
|
||||
return Math.max(GATE_BLOCK_MIN_NANOS, targetNanos / 32);
|
||||
}
|
||||
|
||||
public static long nextDeadline(long previousDeadline, long now, long targetNanos) {
|
||||
if (previousDeadline == 0L || now - previousDeadline > 2 * targetNanos) return now + targetNanos;
|
||||
return previousDeadline + targetNanos;
|
||||
}
|
||||
|
||||
public static long endFrame(int capHz, Runnable renderAheadWait) {
|
||||
lastVSyncMode = GLStateManager.getEffectiveVSyncMode();
|
||||
lastCapHz = Math.max(capHz, 0);
|
||||
lastRefreshHz = refreshHz();
|
||||
return pace(pacingCeilingHz(lastVSyncMode, lastCapHz, lastRefreshHz, gateActive()), renderAheadWait);
|
||||
}
|
||||
|
||||
public static long pace(int ceilingHz, Runnable renderAheadWait) {
|
||||
long now = System.nanoTime();
|
||||
long phaseEnd;
|
||||
|
||||
final long targetNanos = ceilingHz > 0 ? 1_000_000_000L / ceilingHz : 0L;
|
||||
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();
|
||||
Tracy.plotInt(P_FRAME_GATE_US, frameGateNanos / 1000);
|
||||
|
||||
if (gateEnd != lastSeenGateEnd) {
|
||||
lastSeenGateEnd = gateEnd;
|
||||
if (frameGateNanos > thresholdNanos && GLStateManager.gateAnchorsNextFrameStart()) {
|
||||
deadline = gateEnd;
|
||||
}
|
||||
}
|
||||
deadline = nextDeadline(deadline, now, targetNanos);
|
||||
|
||||
final IdleWork work = idleWork;
|
||||
if (work != null && deadline - now > IDLE_WORK_MIN_BUDGET_NANOS) {
|
||||
Tracy.beginZone(Z_PACER_IDLE);
|
||||
try {
|
||||
work.run(deadline);
|
||||
} finally {
|
||||
Tracy.endZone();
|
||||
}
|
||||
phaseEnd = System.nanoTime();
|
||||
now = phaseEnd;
|
||||
}
|
||||
|
||||
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);
|
||||
} finally {
|
||||
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;
|
||||
}
|
||||
|
||||
Tracy.beginZone(Z_PACER_WAIT);
|
||||
try {
|
||||
if (renderAheadWait != null) renderAheadWait.run();
|
||||
} finally {
|
||||
Tracy.endZone();
|
||||
}
|
||||
phaseEnd = System.nanoTime();
|
||||
final long waitNanos = phaseEnd - now;
|
||||
now = phaseEnd;
|
||||
if (ceilingHz > 0 && waitNanos > thresholdNanos) deadline = now;
|
||||
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);
|
||||
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;
|
||||
lastFrameTime = now;
|
||||
return period;
|
||||
}
|
||||
|
||||
private static int refreshHz() {
|
||||
final int reported = GLStateManager.getDisplayRefreshRateHz();
|
||||
final int hz = plausibleRefreshHz(reported);
|
||||
|
||||
if (hz == 0 && lastCapHz == 0 && lastVSyncMode.tearFree() && !warnedUncapped) {
|
||||
private static int refreshHz(VSyncMode mode, int capHz, long refreshPeriodNanos) {
|
||||
final int hz = RenderBackend.hzFromPeriod(refreshPeriodNanos);
|
||||
if (hz == 0 && capHz == 0 && mode.tearFree() && !warnedUncapped) {
|
||||
warnedUncapped = true;
|
||||
AngelicaMod.LOGGER.warn("Display refresh rate reported as {}Hz, so the frame rate cannot be bounded if the driver ignores vsync. Set Max Framerate to bound it.", reported);
|
||||
AngelicaMod.LOGGER.warn("Display refresh rate is unknown, so the frame rate cannot be bounded if the driver ignores vsync. Set Max Framerate to bound it.");
|
||||
}
|
||||
return hz;
|
||||
}
|
||||
|
||||
public static String debugIndicator(VSyncMode mode, int refreshHz, int capHz, boolean gateActive) {
|
||||
static String debugIndicator(VSyncMode mode, int refreshHz, int capHz, int effectiveCapHz, boolean locked, boolean probing) {
|
||||
final int cap = Math.max(capHz, 0);
|
||||
final boolean showCap = cap > 0 && cap == pacingCeilingHz(mode, cap, refreshHz, gateActive);
|
||||
final boolean showCap = cap > 0;
|
||||
if (!mode.tearFree() && !showCap) return null;
|
||||
|
||||
final StringBuilder sb = new StringBuilder(" [");
|
||||
if (mode.tearFree()) {
|
||||
sb.append(mode == VSyncMode.MAILBOX ? "mailbox" : "vsync");
|
||||
final int refresh = plausibleRefreshHz(refreshHz);
|
||||
if (refresh > 0) sb.append(' ').append(refresh);
|
||||
if (showCap) sb.append(", ");
|
||||
if (refreshHz > 0) sb.append(' ').append(refreshHz);
|
||||
if (mode == VSyncMode.ON) {
|
||||
if (probing) sb.append(", probing");
|
||||
else if (!locked) sb.append(", wall clock");
|
||||
}
|
||||
if (showCap) sb.append(", cap ").append(cap).append(" -> ").append(effectiveCapHz);
|
||||
} else {
|
||||
sb.append("cap ").append(cap);
|
||||
}
|
||||
if (showCap) sb.append("cap ").append(cap);
|
||||
return sb.append(']').toString();
|
||||
}
|
||||
|
||||
public static long gateDurationNanos() { return sample.durationNanos; }
|
||||
|
||||
public static long gpuWaitNanos() { return core.lastGpuWaitNanos(); }
|
||||
|
||||
public static long slackNanos() { return core.slackNanos(); }
|
||||
|
||||
public static long spinNanos() { return sleeper.lastFrameSpinNanos; }
|
||||
|
||||
public static long marginNanos() { return core.marginNanos(); }
|
||||
|
||||
public static long presentIntervalNanos() { return core.lastPresentIntervalNanos(); }
|
||||
|
||||
public static long idleOvershootNanos() { return core.lastIdleOvershootNanos(); }
|
||||
|
||||
public static int effectiveCapHz() { return core.effectiveCapHz(); }
|
||||
|
||||
public static boolean locked() { return core.locked(); }
|
||||
|
||||
public static boolean probing() { return core.probing(); }
|
||||
|
||||
public static String debugIndicator() {
|
||||
return debugIndicator(lastVSyncMode, lastRefreshHz, lastCapHz, gateActive());
|
||||
return debugIndicator(currentMode, currentRefreshHz, currentCapHz, currentEffectiveCapHz, core.locked(), core.probing());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,374 @@
|
||||
package com.gtnewhorizons.angelica.rendering;
|
||||
|
||||
import com.google.common.primitives.Longs;
|
||||
import com.gtnewhorizons.angelica.glsm.backend.RenderBackend;
|
||||
|
||||
import java.util.function.LongConsumer;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
final class PacerCore {
|
||||
|
||||
private static final long NANOS_PER_SECOND = 1_000_000_000L;
|
||||
private static final long MARGIN_BASE_NANOS = 500_000L;
|
||||
static final long MARGIN_MIN_NANOS = 1_000_000L;
|
||||
static final long MARGIN_CEILING_NANOS = 4_000_000L;
|
||||
private static final long GATE_THRESHOLD_FLOOR_NANOS = 100_000L;
|
||||
private static final long IDLE_MIN_BUDGET_NANOS = 500_000L;
|
||||
private static final long PRESENT_SLOP_NANOS = 50_000L;
|
||||
|
||||
private static final int PROBE_LEAD_PERIODS = 2;
|
||||
static final int PROBE_LEAD_DIVISOR = 8;
|
||||
static final int LOCK_PAIRS = 3;
|
||||
static final int LOCK_FRAMES = 600;
|
||||
static final int LOCK_WINDOW = 16;
|
||||
private static final int CAP_EQ_DIVISOR = 32;
|
||||
private static final int SNAP_TOL_DIVISOR = 8;
|
||||
static final int CATCHUP_DIVISOR = 8;
|
||||
private static final int GATE_THRESHOLD_DIVISOR = 32;
|
||||
private static final int LOCK_TOL_DIVISOR = 8;
|
||||
private static final int MARGIN_MAX_DIVISOR = 4;
|
||||
private static final int STALL_MULTIPLE = 2;
|
||||
private static final int TARGET_RESET_PERIODS = 2;
|
||||
|
||||
private static final int CPU_SLOTS = 16;
|
||||
private static final int OVERRUN_SLOTS = 128;
|
||||
|
||||
private final LongSupplier clock;
|
||||
private final PacerSleeper sleeper;
|
||||
private LongConsumer idleWork;
|
||||
|
||||
private long refreshPeriodNanos;
|
||||
private boolean tearFree = true;
|
||||
private boolean vsyncOn = true;
|
||||
private int capHz;
|
||||
|
||||
private long cadenceNanos;
|
||||
private long targetNanos;
|
||||
private boolean haveTarget;
|
||||
private long startDeadlineNanos;
|
||||
private long pEffPrevNanos;
|
||||
private boolean havePEffPrev;
|
||||
private long frameStartNanos;
|
||||
private long lastPresentCallNanos;
|
||||
private boolean havePresent;
|
||||
private long lastPresentIntervalNanos;
|
||||
|
||||
private final long[] cpuRing = new long[CPU_SLOTS];
|
||||
private final long[] residualRing = new long[OVERRUN_SLOTS];
|
||||
private final long[] idleRing = new long[OVERRUN_SLOTS];
|
||||
private int cpuSlot;
|
||||
private int residualSlot;
|
||||
private int idleSlot;
|
||||
|
||||
private boolean locked;
|
||||
private boolean probing;
|
||||
private boolean probeExhausted;
|
||||
private long probeLeadNanos;
|
||||
private boolean haveCandidate;
|
||||
private long candidateEndNanos;
|
||||
private int framesSinceCandidate;
|
||||
private int consistentPairs;
|
||||
private int inconsistentPairs;
|
||||
|
||||
private boolean pendingSample;
|
||||
private long pendingDurationNanos;
|
||||
private long pendingEndNanos;
|
||||
private long pendingGpuWaitNanos;
|
||||
private long lastGateEndNanos;
|
||||
private boolean haveGateEnd;
|
||||
|
||||
private long cpuHighNanos;
|
||||
private long marginNanos;
|
||||
private long slackNanos;
|
||||
private long lastGpuWaitNanos;
|
||||
private long lastIdleOvershootNanos;
|
||||
|
||||
private boolean frameDriverPaced;
|
||||
private boolean frameProbing;
|
||||
private boolean frameAnchor;
|
||||
private long frameWakeLateNanos;
|
||||
|
||||
PacerCore(LongSupplier clock, PacerSleeper sleeper) {
|
||||
this.clock = clock;
|
||||
this.sleeper = sleeper;
|
||||
}
|
||||
|
||||
void setIdleWork(LongConsumer work) {
|
||||
this.idleWork = work;
|
||||
}
|
||||
|
||||
void invalidate() {
|
||||
dropLock();
|
||||
}
|
||||
|
||||
void setRefreshPeriodNanos(long periodNanos) {
|
||||
final long p = Math.max(periodNanos, 0L);
|
||||
if (p == refreshPeriodNanos) return;
|
||||
refreshPeriodNanos = p;
|
||||
dropLock();
|
||||
}
|
||||
|
||||
void setTearFree(boolean value) {
|
||||
if (value == tearFree) return;
|
||||
tearFree = value;
|
||||
dropLock();
|
||||
}
|
||||
|
||||
void setVsyncOn(boolean value) {
|
||||
if (value == vsyncOn) return;
|
||||
vsyncOn = value;
|
||||
dropLock();
|
||||
}
|
||||
|
||||
void setCap(int hz) {
|
||||
final int cap = Math.max(hz, 0);
|
||||
if (cap == capHz) return;
|
||||
capHz = cap;
|
||||
armProbe();
|
||||
}
|
||||
|
||||
void onGpuWait(long nanos) {
|
||||
if (nanos > 0L) pendingGpuWaitNanos += nanos;
|
||||
}
|
||||
|
||||
void onGateSample(long durationNanos, long endNanos) {
|
||||
if (durationNanos <= 0L) return;
|
||||
pendingSample = true;
|
||||
pendingDurationNanos = durationNanos;
|
||||
pendingEndNanos = endNanos;
|
||||
}
|
||||
|
||||
long beginFrame(long now) {
|
||||
final boolean sample = pendingSample && (!haveGateEnd || pendingEndNanos > lastGateEndNanos);
|
||||
final long sampleEnd = sample ? pendingEndNanos : 0L;
|
||||
final long sampleDuration = sample ? pendingDurationNanos : 0L;
|
||||
if (sample) {
|
||||
lastGateEndNanos = pendingEndNanos;
|
||||
haveGateEnd = true;
|
||||
}
|
||||
pendingSample = false;
|
||||
|
||||
if (sample && (!havePresent || sampleEnd > lastPresentCallNanos)) {
|
||||
pEffPrevNanos = sampleEnd;
|
||||
havePEffPrev = true;
|
||||
} else if (havePresent) {
|
||||
pEffPrevNanos = lastPresentCallNanos;
|
||||
havePEffPrev = true;
|
||||
}
|
||||
lastGpuWaitNanos = pendingGpuWaitNanos;
|
||||
pendingGpuWaitNanos = 0L;
|
||||
|
||||
final long c = cadencePeriodNanos(capHz, refreshPeriodNanos, tearFree);
|
||||
final boolean candidate = sample && vsyncOn && tearFree && refreshPeriodNanos > 0L
|
||||
&& sampleDuration > gateThresholdNanos(c);
|
||||
updateLock(candidate, sampleEnd);
|
||||
|
||||
cadenceNanos = c;
|
||||
frameWakeLateNanos = 0L;
|
||||
if (c == 0L) {
|
||||
probing = false;
|
||||
frameProbing = false;
|
||||
frameDriverPaced = false;
|
||||
frameAnchor = false;
|
||||
haveTarget = false;
|
||||
armProbe();
|
||||
startDeadlineNanos = now;
|
||||
slackNanos = 0L;
|
||||
marginNanos = 0L;
|
||||
lastIdleOvershootNanos = 0L;
|
||||
return now;
|
||||
}
|
||||
|
||||
probing = vsyncOn && !locked && !probeExhausted && c == refreshPeriodNanos;
|
||||
final long step = probing ? c - c / PROBE_LEAD_DIVISOR : c;
|
||||
if (probing) {
|
||||
probeLeadNanos += c - step;
|
||||
probeExhausted = probeLeadNanos >= PROBE_LEAD_PERIODS * c;
|
||||
}
|
||||
|
||||
frameAnchor = !haveTarget;
|
||||
if (frameAnchor) {
|
||||
targetNanos = now;
|
||||
haveTarget = true;
|
||||
} else if (locked && candidate) {
|
||||
long m = (now - sampleEnd) / c + 1L;
|
||||
if (m < 1L) m = 1L;
|
||||
targetNanos = sampleEnd + m * c;
|
||||
} else {
|
||||
long advanced = targetNanos + step;
|
||||
if (havePEffPrev) {
|
||||
final long floor = pEffPrevNanos + c - c / CATCHUP_DIVISOR;
|
||||
if (floor > advanced) advanced = floor;
|
||||
}
|
||||
targetNanos = advanced;
|
||||
}
|
||||
if (targetNanos - now > TARGET_RESET_PERIODS * c) targetNanos = now + c;
|
||||
|
||||
frameDriverPaced = driverPaced();
|
||||
frameProbing = probing;
|
||||
cpuHighNanos = Math.max(0L, Longs.max(cpuRing));
|
||||
marginNanos = clampMargin(MARGIN_BASE_NANOS + Longs.max(residualRing), c);
|
||||
final long justInTime = targetNanos - cpuHighNanos - marginNanos;
|
||||
startDeadlineNanos = frameDriverPaced || frameAnchor ? now : justInTime;
|
||||
|
||||
final long idleDeadline = frameDriverPaced || frameAnchor ? justInTime : startDeadlineNanos;
|
||||
final LongConsumer work = idleWork;
|
||||
lastIdleOvershootNanos = 0L;
|
||||
if (work != null && idleDeadline - now - Longs.max(idleRing) > IDLE_MIN_BUDGET_NANOS) {
|
||||
work.accept(idleDeadline);
|
||||
final long idleEnd = clock.getAsLong();
|
||||
lastIdleOvershootNanos = Math.max(0L, idleEnd - idleDeadline);
|
||||
idleRing[idleSlot] = lastIdleOvershootNanos;
|
||||
idleSlot = (idleSlot + 1) % OVERRUN_SLOTS;
|
||||
now = idleEnd;
|
||||
}
|
||||
|
||||
slackNanos = startDeadlineNanos - now;
|
||||
if (now < startDeadlineNanos) {
|
||||
now = sleeper.sleepUntil(startDeadlineNanos, now);
|
||||
if (now > startDeadlineNanos) frameWakeLateNanos = now - startDeadlineNanos;
|
||||
}
|
||||
return now;
|
||||
}
|
||||
|
||||
void markFrameStart(long now) {
|
||||
frameStartNanos = now;
|
||||
}
|
||||
|
||||
long beforePresent(long now, long firstClearBlockNanos) {
|
||||
final long c = cadenceNanos;
|
||||
if (c > 0L) {
|
||||
final long cpu = now - frameStartNanos - firstClearBlockNanos;
|
||||
if (cpu <= c * STALL_MULTIPLE) {
|
||||
cpuRing[cpuSlot] = Math.min(cpu, c);
|
||||
cpuSlot = (cpuSlot + 1) % CPU_SLOTS;
|
||||
residualRing[residualSlot] = cpu - cpuHighNanos - (startDeadlineNanos - frameStartNanos);
|
||||
residualSlot = (residualSlot + 1) % OVERRUN_SLOTS;
|
||||
} else {
|
||||
armProbe();
|
||||
}
|
||||
|
||||
if (!frameDriverPaced && !frameProbing && !frameAnchor) {
|
||||
if (targetNanos - now > PRESENT_SLOP_NANOS) {
|
||||
now = sleeper.sleepUntil(targetNanos, now);
|
||||
final long late = now - targetNanos;
|
||||
if (late > frameWakeLateNanos) frameWakeLateNanos = late;
|
||||
}
|
||||
}
|
||||
sleeper.noteFrame(slackNanos, frameWakeLateNanos);
|
||||
}
|
||||
|
||||
lastPresentIntervalNanos = havePresent ? now - lastPresentCallNanos : 0L;
|
||||
lastPresentCallNanos = now;
|
||||
havePresent = true;
|
||||
return now;
|
||||
}
|
||||
|
||||
boolean paced() {
|
||||
return cadenceNanos > 0L;
|
||||
}
|
||||
|
||||
boolean locked() {
|
||||
return locked;
|
||||
}
|
||||
|
||||
boolean probing() {
|
||||
return probing;
|
||||
}
|
||||
|
||||
private boolean driverPaced() {
|
||||
return locked && cadenceNanos > 0L && cadenceNanos == refreshPeriodNanos;
|
||||
}
|
||||
|
||||
|
||||
int effectiveCapHz() {
|
||||
return cadenceNanos == 0L ? 0 : RenderBackend.hzFromPeriod(cadenceNanos);
|
||||
}
|
||||
|
||||
long marginNanos() {
|
||||
return marginNanos;
|
||||
}
|
||||
|
||||
|
||||
long slackNanos() {
|
||||
return slackNanos;
|
||||
}
|
||||
|
||||
long lastGpuWaitNanos() {
|
||||
return lastGpuWaitNanos;
|
||||
}
|
||||
|
||||
long lastIdleOvershootNanos() {
|
||||
return lastIdleOvershootNanos;
|
||||
}
|
||||
|
||||
long lastPresentIntervalNanos() {
|
||||
return lastPresentIntervalNanos;
|
||||
}
|
||||
|
||||
static long cadencePeriodNanos(int capHz, long refreshPeriodNanos, boolean tearFree) {
|
||||
final long cap = Math.max(capHz, 0);
|
||||
if (tearFree && refreshPeriodNanos > 0L) {
|
||||
if (cap == 0L || cap * refreshPeriodNanos * CAP_EQ_DIVISOR >= NANOS_PER_SECOND * (CAP_EQ_DIVISOR - 1)) {
|
||||
return refreshPeriodNanos;
|
||||
}
|
||||
final long capPeriod = NANOS_PER_SECOND / cap;
|
||||
long k = (capPeriod + refreshPeriodNanos / 2) / refreshPeriodNanos;
|
||||
if (k < 1L) k = 1L;
|
||||
final long snapped = k * refreshPeriodNanos;
|
||||
return Math.abs(snapped - capPeriod) <= capPeriod / SNAP_TOL_DIVISOR ? snapped : capPeriod;
|
||||
}
|
||||
return cap > 0L ? NANOS_PER_SECOND / cap : 0L;
|
||||
}
|
||||
|
||||
static long gateThresholdNanos(long cadenceNanos) {
|
||||
return Math.max(GATE_THRESHOLD_FLOOR_NANOS, cadenceNanos / GATE_THRESHOLD_DIVISOR);
|
||||
}
|
||||
|
||||
static long clampMargin(long value, long cadenceNanos) {
|
||||
final long ceiling = Math.min(cadenceNanos / MARGIN_MAX_DIVISOR, MARGIN_CEILING_NANOS);
|
||||
return Math.min(Math.max(value, MARGIN_MIN_NANOS), ceiling);
|
||||
}
|
||||
|
||||
private void updateLock(boolean candidate, long sampleEnd) {
|
||||
if (!candidate) {
|
||||
if (framesSinceCandidate < Integer.MAX_VALUE) framesSinceCandidate++;
|
||||
if (locked && framesSinceCandidate >= LOCK_FRAMES) dropLock();
|
||||
return;
|
||||
}
|
||||
if (haveCandidate && framesSinceCandidate <= LOCK_WINDOW) {
|
||||
final long delta = sampleEnd - candidateEndNanos;
|
||||
final long k = (delta + refreshPeriodNanos / 2) / refreshPeriodNanos;
|
||||
if (k >= 1L && Math.abs(delta - k * refreshPeriodNanos) <= refreshPeriodNanos / LOCK_TOL_DIVISOR) {
|
||||
inconsistentPairs = 0;
|
||||
if (++consistentPairs >= LOCK_PAIRS) locked = true;
|
||||
} else {
|
||||
consistentPairs = 0;
|
||||
if (++inconsistentPairs >= LOCK_WINDOW) {
|
||||
dropLock();
|
||||
probeExhausted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
candidateEndNanos = sampleEnd;
|
||||
haveCandidate = true;
|
||||
framesSinceCandidate = 0;
|
||||
}
|
||||
|
||||
private void armProbe() {
|
||||
probeLeadNanos = 0L;
|
||||
probeExhausted = false;
|
||||
}
|
||||
|
||||
private void dropLock() {
|
||||
armProbe();
|
||||
locked = false;
|
||||
haveCandidate = false;
|
||||
candidateEndNanos = 0L;
|
||||
framesSinceCandidate = 0;
|
||||
consistentPairs = 0;
|
||||
inconsistentPairs = 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -8,6 +8,7 @@ final class PacerSleeper {
|
||||
private static final long MIN_PARK_NANOS = 1_000_000L;
|
||||
private static final long MAX_OVERSHOOT_NANOS = 4_000_000L;
|
||||
static final int SLOTS = 10;
|
||||
static final long LATE_TOL_NANOS = 250_000L;
|
||||
|
||||
static final long MIN_SPIN_FLOOR_NANOS = 20_000L;
|
||||
private static final long MAX_SPIN_FLOOR_NANOS = 500_000L;
|
||||
@@ -20,8 +21,9 @@ final class PacerSleeper {
|
||||
private long spinFloorNanos = MIN_SPIN_FLOOR_NANOS;
|
||||
private int offset;
|
||||
|
||||
long lastSpinNanos;
|
||||
int lastParks;
|
||||
long lastFrameSpinNanos;
|
||||
private int frameParks;
|
||||
private long frameSpinNanos;
|
||||
|
||||
private long frames;
|
||||
private long slackSum;
|
||||
@@ -35,13 +37,8 @@ final class PacerSleeper {
|
||||
this.spinWait = spinWait;
|
||||
}
|
||||
|
||||
long spinFloorNanos() {
|
||||
return spinFloorNanos;
|
||||
}
|
||||
|
||||
long sleepUntil(long deadlineNanos, long now) {
|
||||
resetLastFrame();
|
||||
|
||||
long spinFloor = spinFloorNanos;
|
||||
while (true) {
|
||||
final long remaining = deadlineNanos - now;
|
||||
@@ -59,7 +56,7 @@ final class PacerSleeper {
|
||||
now = clock.getAsLong();
|
||||
record(Math.min(Math.max(0L, now - before - request), MAX_OVERSHOOT_NANOS));
|
||||
spinFloor = spinFloorNanos;
|
||||
lastParks++;
|
||||
frameParks++;
|
||||
}
|
||||
|
||||
if (spinWait) {
|
||||
@@ -69,27 +66,28 @@ final class PacerSleeper {
|
||||
else Thread.onSpinWait();
|
||||
now = clock.getAsLong();
|
||||
}
|
||||
lastSpinNanos = now - spinStart;
|
||||
frameSpinNanos += now - spinStart;
|
||||
}
|
||||
return now;
|
||||
}
|
||||
|
||||
void noteFrame(long slackNanos, long wakeLateNanos, boolean slept) {
|
||||
if (!slept) resetLastFrame();
|
||||
|
||||
void noteFrame(long slackNanos, long wakeLateNanos) {
|
||||
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;
|
||||
parkTotal += frameParks;
|
||||
frameParks = 0;
|
||||
lastFrameSpinNanos = frameSpinNanos;
|
||||
frameSpinNanos = 0L;
|
||||
if (wakeLateNanos > LATE_TOL_NANOS) {
|
||||
lateFrames++;
|
||||
if (wakeLateNanos > lateMax) lateMax = wakeLateNanos;
|
||||
}
|
||||
}
|
||||
|
||||
void resetStats() {
|
||||
frameParks = 0;
|
||||
frameSpinNanos = 0L;
|
||||
lastFrameSpinNanos = 0L;
|
||||
frames = 0L;
|
||||
slackSum = 0L;
|
||||
parkTotal = 0L;
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.gtnewhorizons.angelica.rendering;
|
||||
|
||||
import me.jellysquid.mods.sodium.client.gui.FrameRateOptions;
|
||||
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
final class ReducerStateMachine {
|
||||
|
||||
enum State {
|
||||
DISABLED(null),
|
||||
FORCED(" [reducer: forced]"),
|
||||
MINIMIZED(" [reducer: minimized]"),
|
||||
UNFOCUSED(" [reducer: unfocused]"),
|
||||
IDLE(" [reducer: idle]"),
|
||||
FOCUSED(null);
|
||||
|
||||
private final String debugTag;
|
||||
|
||||
State(String debugTag) {
|
||||
this.debugTag = debugTag;
|
||||
}
|
||||
|
||||
String debugTag() {
|
||||
return debugTag;
|
||||
}
|
||||
}
|
||||
|
||||
static final int MAX_FPS_SETTING = FrameRateOptions.MAX_FRAMERATE;
|
||||
static final int MINIMIZED_LOOP_HZ = 20;
|
||||
static final int MENU_CAP_HZ = 30;
|
||||
static final float MIN_VOLUME_MULT = 0.01f;
|
||||
|
||||
private final LongSupplier clock;
|
||||
private State state = State.FOCUSED;
|
||||
private long lastInputNanos;
|
||||
private boolean forced;
|
||||
private boolean keybindDisabled;
|
||||
|
||||
ReducerStateMachine(LongSupplier clock) {
|
||||
this.clock = clock;
|
||||
this.lastInputNanos = clock.getAsLong();
|
||||
}
|
||||
|
||||
void onInput() {
|
||||
lastInputNanos = clock.getAsLong();
|
||||
}
|
||||
|
||||
void toggleForced() {
|
||||
forced = !forced;
|
||||
onInput();
|
||||
}
|
||||
|
||||
void toggleKeybindDisabled() {
|
||||
keybindDisabled = !keybindDisabled;
|
||||
onInput();
|
||||
}
|
||||
|
||||
boolean keybindDisabled() {
|
||||
return keybindDisabled;
|
||||
}
|
||||
|
||||
State state() {
|
||||
return state;
|
||||
}
|
||||
|
||||
boolean evaluate(boolean enabled, boolean visible, boolean active, long idleTimeoutNanos) {
|
||||
final State next;
|
||||
if (!enabled || keybindDisabled) {
|
||||
next = State.DISABLED;
|
||||
} else if (forced) {
|
||||
next = State.FORCED;
|
||||
} else if (!visible) {
|
||||
next = State.MINIMIZED;
|
||||
} else if (!active) {
|
||||
next = State.UNFOCUSED;
|
||||
} else if (idleTimeoutNanos > 0 && clock.getAsLong() - lastInputNanos >= idleTimeoutNanos) {
|
||||
next = State.IDLE;
|
||||
} else {
|
||||
next = State.FOCUSED;
|
||||
}
|
||||
|
||||
final boolean changed = next != state;
|
||||
state = next;
|
||||
return changed;
|
||||
}
|
||||
|
||||
static int stateCap(State state, int unfocusedFps, int idleFps) {
|
||||
return switch (state) {
|
||||
case FORCED, UNFOCUSED -> uncapIfMax(unfocusedFps);
|
||||
case MINIMIZED -> MINIMIZED_LOOP_HZ;
|
||||
case IDLE -> uncapIfMax(idleFps);
|
||||
default -> 0;
|
||||
};
|
||||
}
|
||||
|
||||
static int stateVolume(State state, int unfocusedVol, int minimizedVol, int idleVol) {
|
||||
return switch (state) {
|
||||
case FORCED, UNFOCUSED -> unfocusedVol;
|
||||
case MINIMIZED -> minimizedVol;
|
||||
case IDLE -> idleVol;
|
||||
default -> 100;
|
||||
};
|
||||
}
|
||||
|
||||
static int mergeCap(int userLimit, int stateCap, boolean inMenu, boolean limitMenu) {
|
||||
int cap = uncapIfMax(userLimit);
|
||||
if (inMenu && limitMenu) {
|
||||
cap = min0(cap, MENU_CAP_HZ);
|
||||
}
|
||||
return min0(cap, stateCap);
|
||||
}
|
||||
|
||||
static float masterVolume(float user, int percent) {
|
||||
return percent >= 100 ? user : user * Math.max(MIN_VOLUME_MULT, percent / 100f);
|
||||
}
|
||||
|
||||
private static int uncapIfMax(int value) {
|
||||
return value >= MAX_FPS_SETTING ? 0 : value;
|
||||
}
|
||||
|
||||
private static int min0(int a, int b) {
|
||||
if (a == 0) return b;
|
||||
if (b == 0) return a;
|
||||
return Math.min(a, b);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,9 @@ import java.util.Locale;
|
||||
|
||||
public record FrameRateOptions(Option<VSyncMode> vsync, Option<Integer> maxFramerate) {
|
||||
|
||||
public static final int MIN_FRAMERATE = 5;
|
||||
public static final int MAX_FRAMERATE = 260;
|
||||
|
||||
public static FrameRateOptions create(MinecraftOptionsStorage vanillaOpts, SodiumOptionsStorage sodiumOpts) {
|
||||
final VSyncMode[] allowed = selectableModes();
|
||||
final String[] names = modeNames(VSyncMode.values());
|
||||
@@ -40,7 +43,7 @@ public record FrameRateOptions(Option<VSyncMode> vsync, Option<Integer> maxFrame
|
||||
final OptionImpl<GameSettings, Integer> maxFramerate = OptionImpl.createBuilder(int.class, vanillaOpts)
|
||||
.setName(I18n.format("options.framerateLimit"))
|
||||
.setTooltip(I18n.format("sodium.options.fps_limit.tooltip"))
|
||||
.setControl(option -> new SliderControl(option, 5, 260, 1, ControlValueFormatter.fpsLimit()))
|
||||
.setControl(option -> new SliderControl(option, MIN_FRAMERATE, MAX_FRAMERATE, 1, ControlValueFormatter.fpsLimit()))
|
||||
.setBinding((opts, value) -> opts.limitFramerate = value, opts -> opts.limitFramerate)
|
||||
.build();
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package me.jellysquid.mods.sodium.client.gui;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.IntSupplier;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@@ -15,6 +16,7 @@ import com.cardinalstar.cubicchunks.api.compat.CubicChunksVideoSettings;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.gtnewhorizons.angelica.compat.ModStatus;
|
||||
import com.gtnewhorizons.angelica.config.AngelicaConfig;
|
||||
import com.gtnewhorizons.angelica.rendering.FpsReducer;
|
||||
import com.gtnewhorizons.angelica.rendering.celeritas.MultiDrawModeResolver;
|
||||
import com.gtnewhorizons.angelica.rendering.culling.GpuCulling;
|
||||
import com.gtnewhorizons.angelica.config.GpuCullingMode;
|
||||
@@ -56,6 +58,13 @@ public class SodiumGameOptionPages {
|
||||
private static final int MIN_RENDER_AHEAD = 0;
|
||||
private static final int MAX_RENDER_AHEAD = 9;
|
||||
|
||||
private static <T> BiConsumer<SodiumGameOptions, T> reducerBinding(BiConsumer<SodiumGameOptions, T> setter) {
|
||||
return (opts, value) -> {
|
||||
setter.accept(opts, value);
|
||||
FpsReducer.markConfigChanged();
|
||||
};
|
||||
}
|
||||
|
||||
public static OptionPage general() {
|
||||
final List<OptionGroup> groups = new ArrayList<>();
|
||||
final OptionGroup.Builder firstGroupBuilder = OptionGroup.createBuilder();
|
||||
@@ -586,6 +595,91 @@ public class SodiumGameOptionPages {
|
||||
return new OptionPage(I18n.format("sodium.options.pages.performance"), ImmutableList.copyOf(groups));
|
||||
}
|
||||
|
||||
public static OptionPage fpsReducer() {
|
||||
final List<OptionGroup> groups = new ArrayList<>();
|
||||
|
||||
final FrameRateOptions frameRate = FrameRateOptions.create(vanillaOpts, sodiumOpts);
|
||||
groups.add(OptionGroup.createBuilder()
|
||||
.add(frameRate.vsync())
|
||||
.add(frameRate.maxFramerate())
|
||||
.build());
|
||||
|
||||
groups.add(OptionGroup.createBuilder()
|
||||
.add(OptionImpl.createBuilder(boolean.class, sodiumOpts)
|
||||
.setName(I18n.format("sodium.options.fps_reducer_enabled.name"))
|
||||
.setTooltip(I18n.format("sodium.options.fps_reducer_enabled.tooltip"))
|
||||
.setControl(TickBoxControl::new)
|
||||
.setBinding(reducerBinding((opts, value) -> opts.reducer.enabled = value),
|
||||
options -> options.reducer.enabled)
|
||||
.setImpact(OptionImpact.LOW)
|
||||
.build())
|
||||
.add(OptionImpl.createBuilder(int.class, sodiumOpts)
|
||||
.setName(I18n.format("sodium.options.unfocused_fps_limit.name"))
|
||||
.setTooltip(I18n.format("sodium.options.unfocused_fps_limit.tooltip"))
|
||||
.setControl(option -> new SliderControl(option, FrameRateOptions.MIN_FRAMERATE, FrameRateOptions.MAX_FRAMERATE, 1, ControlValueFormatter.fpsLimit()))
|
||||
.setBinding(reducerBinding((opts, value) -> opts.reducer.unfocusedFpsLimit = value),
|
||||
options -> options.reducer.unfocusedFpsLimit)
|
||||
.setImpact(OptionImpact.LOW)
|
||||
.build())
|
||||
.add(OptionImpl.createBuilder(int.class, sodiumOpts)
|
||||
.setName(I18n.format("sodium.options.unfocused_volume.name"))
|
||||
.setTooltip(I18n.format("sodium.options.unfocused_volume.tooltip"))
|
||||
.setControl(option -> new SliderControl(option, 0, 100, 1, ControlValueFormatter.percentage()))
|
||||
.setBinding(reducerBinding((opts, value) -> opts.reducer.unfocusedVolume = value),
|
||||
options -> options.reducer.unfocusedVolume)
|
||||
.setImpact(OptionImpact.LOW)
|
||||
.build())
|
||||
.add(OptionImpl.createBuilder(int.class, sodiumOpts)
|
||||
.setName(I18n.format("sodium.options.minimized_volume.name"))
|
||||
.setTooltip(I18n.format("sodium.options.minimized_volume.tooltip"))
|
||||
.setControl(option -> new SliderControl(option, 0, 100, 1, ControlValueFormatter.percentage()))
|
||||
.setBinding(reducerBinding((opts, value) -> opts.reducer.minimizedVolume = value),
|
||||
options -> options.reducer.minimizedVolume)
|
||||
.setImpact(OptionImpact.LOW)
|
||||
.build())
|
||||
.build());
|
||||
|
||||
groups.add(OptionGroup.createBuilder()
|
||||
.add(OptionImpl.createBuilder(int.class, sodiumOpts)
|
||||
.setName(I18n.format("sodium.options.idle_timeout.name"))
|
||||
.setTooltip(I18n.format("sodium.options.idle_timeout.tooltip"))
|
||||
.setControl(option -> new SliderControl(option, 0, 30, 1, ControlValueFormatter.quantityOrDisabled("sodium.options.idle_timeout.value", "sodium.options.idle_timeout.off")))
|
||||
.setBinding(reducerBinding((opts, value) -> opts.reducer.idleTimeoutMinutes = value),
|
||||
options -> options.reducer.idleTimeoutMinutes)
|
||||
.setImpact(OptionImpact.LOW)
|
||||
.build())
|
||||
.add(OptionImpl.createBuilder(int.class, sodiumOpts)
|
||||
.setName(I18n.format("sodium.options.idle_fps_limit.name"))
|
||||
.setTooltip(I18n.format("sodium.options.idle_fps_limit.tooltip"))
|
||||
.setControl(option -> new SliderControl(option, FrameRateOptions.MIN_FRAMERATE, FrameRateOptions.MAX_FRAMERATE, 1, ControlValueFormatter.fpsLimit()))
|
||||
.setBinding(reducerBinding((opts, value) -> opts.reducer.idleFpsLimit = value),
|
||||
options -> options.reducer.idleFpsLimit)
|
||||
.setImpact(OptionImpact.LOW)
|
||||
.build())
|
||||
.add(OptionImpl.createBuilder(int.class, sodiumOpts)
|
||||
.setName(I18n.format("sodium.options.idle_volume.name"))
|
||||
.setTooltip(I18n.format("sodium.options.idle_volume.tooltip"))
|
||||
.setControl(option -> new SliderControl(option, 0, 100, 1, ControlValueFormatter.percentage()))
|
||||
.setBinding(reducerBinding((opts, value) -> opts.reducer.idleVolume = value),
|
||||
options -> options.reducer.idleVolume)
|
||||
.setImpact(OptionImpact.LOW)
|
||||
.build())
|
||||
.build());
|
||||
|
||||
groups.add(OptionGroup.createBuilder()
|
||||
.add(OptionImpl.createBuilder(boolean.class, sodiumOpts)
|
||||
.setName(I18n.format("sodium.options.limit_menu_fps.name"))
|
||||
.setTooltip(I18n.format("sodium.options.limit_menu_fps.tooltip"))
|
||||
.setControl(TickBoxControl::new)
|
||||
.setBinding(reducerBinding((opts, value) -> opts.reducer.limitMenuFrameRate = value),
|
||||
options -> options.reducer.limitMenuFrameRate)
|
||||
.setImpact(OptionImpact.LOW)
|
||||
.build())
|
||||
.build());
|
||||
|
||||
return new OptionPage(I18n.format("sodium.options.pages.fps_reducer"), ImmutableList.copyOf(groups));
|
||||
}
|
||||
|
||||
public static OptionImpl<GameSettings, Integer> anisotropicFilteringSlider(MinecraftOptionsStorage storage,
|
||||
Supplier<TextureFilterMode> mode, IntSupplier mipmapLevels) {
|
||||
final int min = SodiumGameOptions.minAnisotropyLevel();
|
||||
|
||||
@@ -38,6 +38,7 @@ public class SodiumGameOptions {
|
||||
public final QualitySettings quality = new QualitySettings();
|
||||
public final AdvancedSettings advanced = new AdvancedSettings();
|
||||
public final PerformanceSettings performance = new PerformanceSettings();
|
||||
public final ReducerSettings reducer = new ReducerSettings();
|
||||
private Path configPath;
|
||||
|
||||
public static void recordAtlasMipmapClamp(int requestedMipmapLevels, int actualMipmapLevels) {
|
||||
@@ -242,4 +243,15 @@ public class SodiumGameOptions {
|
||||
public boolean useCeleritasSmoothLighting = true;
|
||||
public TextureFilterMode textureFilterMode = TextureFilterMode.RGSS;
|
||||
}
|
||||
|
||||
public static class ReducerSettings {
|
||||
public boolean enabled = false;
|
||||
public int unfocusedFpsLimit = 10;
|
||||
public int unfocusedVolume = 100;
|
||||
public int minimizedVolume = 100;
|
||||
public int idleTimeoutMinutes = 5;
|
||||
public int idleFpsLimit = 30;
|
||||
public int idleVolume = 100;
|
||||
public boolean limitMenuFrameRate = true;
|
||||
}
|
||||
}
|
||||
@@ -61,6 +61,7 @@ public class SodiumOptionsGUI extends ScrollableGuiScreen {
|
||||
this.pages.add(SodiumGameOptionPages.quality());
|
||||
this.pages.add(SodiumGameOptionPages.advanced());
|
||||
this.pages.add(SodiumGameOptionPages.performance());
|
||||
this.pages.add(SodiumGameOptionPages.fpsReducer());
|
||||
this.pages.add(SodiumGameOptionPages.appearance());
|
||||
this.pages.add(SodiumGameOptionPages.text());
|
||||
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package me.jellysquid.mods.sodium.client.gui.options.control;
|
||||
|
||||
import me.jellysquid.mods.sodium.client.gui.FrameRateOptions;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
|
||||
import java.util.Locale;
|
||||
@@ -10,7 +11,7 @@ public interface ControlValueFormatter {
|
||||
}
|
||||
|
||||
static ControlValueFormatter fpsLimit() {
|
||||
return (v) -> (v == 260) ? I18n.format("options.framerateLimit.max") : I18n.format("options.framerate", v);
|
||||
return (v) -> (v == FrameRateOptions.MAX_FRAMERATE) ? I18n.format("options.framerateLimit.max") : I18n.format("options.framerate", v);
|
||||
}
|
||||
static ControlValueFormatter droppedItemLimitLimit() {
|
||||
return (v) -> (v == 2048) ? I18n.format("options.droppedItems.max") : I18n.format("options.droppedItems", v);
|
||||
|
||||
Reference in new issue
Block a user