From d4c5b982df8812cc1d8addae1dbadf4de38a3b98 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sat, 27 Jul 2024 02:28:00 +0200 Subject: [PATCH] Fix V-Sync rendering Turns out I did not need the monitor API for that. Just reading glfwSwapInterval's documentation did the job and fixing my existing code did the job. --- .../GraphicsSubsystemConfiguration.java | 4 +-- .../classes/api/ApiManagementClass.java | 26 ++++++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystemConfiguration.java b/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystemConfiguration.java index 74e9b9f..a99c702 100644 --- a/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystemConfiguration.java +++ b/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystemConfiguration.java @@ -232,8 +232,8 @@ public final class GraphicsSubsystemConfiguration implements SubsystemConfigurat errorGraphicsError = true; - vsyncMode = VsyncMode.OFF; - maximumFramesPerSecond = 120; + vsyncMode = VsyncMode.ON; + maximumFramesPerSecond = 60; } /** {@inheritDoc} */ diff --git a/graphics/src/main/java/de/staropensource/sosengine/graphics/classes/api/ApiManagementClass.java b/graphics/src/main/java/de/staropensource/sosengine/graphics/classes/api/ApiManagementClass.java index 9bc0db7..0168318 100644 --- a/graphics/src/main/java/de/staropensource/sosengine/graphics/classes/api/ApiManagementClass.java +++ b/graphics/src/main/java/de/staropensource/sosengine/graphics/classes/api/ApiManagementClass.java @@ -26,6 +26,7 @@ import de.staropensource.sosengine.base.utility.Math; import de.staropensource.sosengine.base.utility.Miscellaneous; import de.staropensource.sosengine.graphics.GraphicsSubsystemConfiguration; import de.staropensource.sosengine.graphics.classes.Window; +import de.staropensource.sosengine.graphics.types.window.VsyncMode; import lombok.AccessLevel; import lombok.Getter; import org.jetbrains.annotations.NotNull; @@ -83,6 +84,7 @@ public abstract class ApiManagementClass { * @since v1-alpha2 */ public LinkedHashMap<@NotNull Window, @NotNull Throwable> runRenderLoopContinuously(@NotNull Runnable frameCode) { + // Define variables AtomicReference> output = new AtomicReference<>(new LinkedHashMap<>()); // runRenderLoop output long renderTime; // Amount of time spent rendering long sleepDuration; // Time spent sleeping the thread @@ -97,19 +99,23 @@ public abstract class ApiManagementClass { frameCode.run(); }); - // TODO v-sync is currently broken, monitor api needs to be implemented first - - // Calculate amount of time the thread should spend sleeping - sleepDuration = (long) (1d / GraphicsSubsystemConfiguration.getInstance().getMaximumFramesPerSecond() * 1000d) - renderTime; + if (GraphicsSubsystemConfiguration.getInstance().getVsyncMode() != VsyncMode.OFF) + // V-Sync is enabled, no need for manual busy waiting + sleepDuration = 0L; + else + // Calculate amount of time the thread should spend sleeping + sleepDuration = (long) (1d / GraphicsSubsystemConfiguration.getInstance().getMaximumFramesPerSecond() * 1000d) - renderTime; // Add render and sleep time to list used for calculating the delta time value splitDeltaTime.add(renderTime + sleepDuration); - try { - //noinspection BusyWait // true, true - Thread.sleep(sleepDuration); - } catch (InterruptedException exception) { - logger.crash("Rendering loop got interrupted. This is unsupported behaviour.", exception); - } + // Busy wait unless V-Sync is enabled + if (GraphicsSubsystemConfiguration.getInstance().getVsyncMode() == VsyncMode.OFF) + try { + //noinspection BusyWait // true, true, but there's no other way to do it + Thread.sleep(sleepDuration); + } catch (InterruptedException exception) { + logger.crash("Rendering loop got interrupted. This is unsupported behaviour", exception); + } // Calculate delta time and frame count every second if (System.currentTimeMillis() >= reportDuration + 1000) {