anegostudios / VintageStory-Issues

Vintage Story's public issue tracker for reporting bugs, crashes and the like
47 stars 16 forks source link

MaxFPS slider (& logic) doesn't work as intended #1872

Open beruzebabu opened 2 years ago

beruzebabu commented 2 years ago

Game Version: 1.16.5 Platform: Windows Modded: No SP/MP: Singleplayer

Description

Using the MaxFPS slider at higher settings than 60 (without Vsync since this disables it) locks FPS to ~60.

How to reproduce

  1. Have a Windows PC without HPET enabled
  2. Set MaxFPS above 60
  3. Observe as FPS doesn't actually go higher than 60

Expected behavior

FPS is capped to the MaxFPS setting instead of 60

Screenshots

Before: 20220420165804_1 After: 20220420170032_1

Fix

I recently got the game and upon starting a new world and exploring, I noticed my FPS wasn't super smooth. After checking the settings I saw my FPS was capped to 60 even though I had configured it at a max of 100. After a bit of digging I found out that the game's rendering loop uses Thread.Sleep(num) to limit FPS. On my system however, the minimum amount of time Thread.Sleep sleeps for is ~15-16ms thus restricting the FPS limiter to a max limit of ~60 FPS. Since using native calls to modify the timer on the OS level seemed dirty and impractical to me, I tried looking for a different solution and here's what I came up with:

        double num = (double)ClientSettings.MaxFPS / 1.6;
        if (ClientSettings.VsyncMode != 1 && num > 10.0 && num < 150.0)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            while (stopwatch.Elapsed.TotalMilliseconds < 1000.0 / num - this.frameStopWatch.Elapsed.TotalMilliseconds)
            {
                Thread.Sleep(0);
            }
        }

Since Thread.Sleep(0) is used, this will still yield if other work is available, if not it will loop thus consuming CPU cycles and being a 'busy wait'. Thus this uses slightly more CPU cycles than using just Thread.Sleep, but now I can enjoy a much smoother (and configurable) experience.

beruzebabu commented 2 years ago

1133 is related