bjornbytes / lovr

Lua Virtual Reality Framework
https://lovr.org
MIT License
2k stars 138 forks source link

High Resolution Sleep on Windows #703

Closed bjornbytes closed 1 year ago

bjornbytes commented 1 year ago

There's a new flag CREATE_WAITABLE_TIMER_HIGH_RESOLUTION that can be used with CreateWaitableTimerExW on Windows. This can be used to sleep for a more precise duration compared to Sleep, which only has around 1ms precision and will sometimes sleep for a long time. LÖVR currently uses a 0-millisecond sleep at the end of each frame intended as a "yield", which I don't think would benefit from this. It would only really be useful for Lua code that uses lovr.timer.sleep for short durations.

Available on Windows 10 1803, so there would need to be a fallback to Sleep.

bjornbytes commented 1 year ago

Confirmed that lovr.timer.sleep has 15ms resolution on windows (though it's a global property on some versions of windows so it depends).

Initial test of the high resolution timer is successful and allows for sleeps down to 500us resolution.

Zero-duration sleeps should still use Sleep though because it only yields for a few hundred nanoseconds whereas a zero-duration high-resolution sleep will still wait for 500us.