【Solved】Limited framerate is not stable

I limited the frame rate to 24 because I wanted to create a cinematic look.

globalClock.setMode(ClockObject.M_limited)
globalClock.setFrameRate(24)

However, the frame rate is changing all over the place. For example: 22.9, 23.7, 23.2

self.setFrameRateMeter(True)

When I record the screen with OBS, it is choppy.

Is there any way to stabilize the frame rate?

2 Likes

This code fixed it.
It seems that by default, the accuracy of Windows time is not good. This code increased the accuracy.

from ctypes import windll
windll.winmm.timeBeginPeriod(1)
2 Likes

This is interesting, thank you for sharing. We should investigate using a higher-precision sleeping mechanism in ClockObject on Windows.

1 Like

I discovered there is also the sleep-precision setting you can increase the value of in Config.prc. It will make Panda subtract this amount (in seconds) from the amount of time it sleeps, and then continues to busy-wait until the requested time has been reached.

2 Likes

I could reproduce the problem, the default sleep-precision of 0.01 (10ms) is inadequate and causes frequent oversleeping.

Raising sleep-precision (to over 0.016) will fix your problem, but it’s also not great because it will cause Panda to busy-wait for even longer, consuming unnecessary CPU resources.

So, I checked in a change so that Panda3D 1.11.0 will use a high-precision timer (available as of Windows 10 version 1803), making it possible to lower sleep-precision down to 0.001 without causing oversleeping.

4 Likes

Thanks for pointing this out, this comes timely for me. ‘sleep-precision 0.001’ (1 ms) works very well on MacOS and it reduces CPU consumption drastically. Before, I tried implementing a frame rate limiter with base.setSleep() but it proved impossible to get a consistent framerate.

1 Like