Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replace frame limiter #2618

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(value = RenderSystem.class, remap = false)
public class RenderSystemMixin {

@Shadow
private static double lastDrawTime = Double.MIN_VALUE;

/**
* @author theyareonit
* @reason Improve frame synchronization
*/
@Overwrite
public static void limitDisplayFPS(int fps) {
double frametime = 1.0 / fps;
double target = lastDrawTime + frametime;
double now = GLFW.glfwGetTime();

for (; now < target; now = GLFW.glfwGetTime()) {
double waitTime = (target - now) - 0.002; // -2ms to account for inaccuracy of timeouts on some operating systems
if (waitTime >= 0.001) { // pretty sure you can't sleep for less than 1ms on Windows or Linux
IMS212 marked this conversation as resolved.
Show resolved Hide resolved
GLFW.glfwWaitEventsTimeout(waitTime); // could be replaced with Thread.sleep(), but i'm not sure if it'd be as precise
}
}

lastDrawTime = now - (now % frametime); // subtracting (now % frametime) keeps frames in sync
GLFW.glfwPollEvents(); // glfwWaitEventsTimeout won't catch every input if subtracting 2ms from the timeout
}
}
1 change: 1 addition & 0 deletions src/main/resources/sodium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down