diff --git a/src/main/java/net/caffeinemc/mods/sodium/client/data/config/MixinConfig.java b/src/main/java/net/caffeinemc/mods/sodium/client/data/config/MixinConfig.java index 09c945a453..fa06cfb6d9 100644 --- a/src/main/java/net/caffeinemc/mods/sodium/client/data/config/MixinConfig.java +++ b/src/main/java/net/caffeinemc/mods/sodium/client/data/config/MixinConfig.java @@ -69,6 +69,8 @@ private MixinConfig() { this.addMixinRule("features.render.particle", true); + this.addMixinRule("features.render.sync", true); + this.addMixinRule("features.render.world", true); this.addMixinRule("features.render.world.clouds", true); this.addMixinRule("features.render.world.sky", true); diff --git a/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/sync/RenderSystemMixin.java b/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/sync/RenderSystemMixin.java new file mode 100644 index 0000000000..dedc4f255a --- /dev/null +++ b/src/main/java/net/caffeinemc/mods/sodium/mixin/features/render/sync/RenderSystemMixin.java @@ -0,0 +1,29 @@ +package net.caffeinemc.mods.sodium.mixin.features.render.sync; + +import com.mojang.blaze3d.systems.RenderSystem; +import org.lwjgl.glfw.GLFW; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; + +@Mixin(value = RenderSystem.class, remap = false) +public class RenderSystemMixin { + /** + * @author theyareonit + * @reason Improve frame synchronization + */ + @Overwrite + public static void limitDisplayFPS(int fps) { + double frameTime = 1.0 / fps; + double now = GLFW.glfwGetTime(); + double end = (now - (now % frameTime)) + frameTime; // subtracting (now % frameTime) corrects for desync + + for (; now < end; now = GLFW.glfwGetTime()) { + double waitTime = (end - now) - 0.002; // -2ms to account for sleep imprecision on some operating systems + if (waitTime >= 0.001) { // cant sleep less than 1ms without platform-specific code + GLFW.glfwWaitEventsTimeout(waitTime); + } + } + + GLFW.glfwPollEvents(); + } +} \ No newline at end of file diff --git a/src/main/resources/sodium.mixins.json b/src/main/resources/sodium.mixins.json index ed405322d7..918637f6ed 100644 --- a/src/main/resources/sodium.mixins.json +++ b/src/main/resources/sodium.mixins.json @@ -60,6 +60,7 @@ "features.render.model.block.ModelBlockRendererMixin", "features.render.model.item.ItemRendererMixin", "features.render.particle.SingleQuadParticleMixin", + "features.render.sync.RenderSystemMixin", "features.render.world.clouds.LevelRendererMixin", "features.render.world.sky.FogRendererMixin", "features.render.world.sky.ClientLevelMixin",