mirror of
https://github.com/godotengine/godot.git
synced 2024-11-24 13:12:42 +00:00
Merge pull request #99178 from mrsaturnsan/windows_sleep_precision
Make `delay_usec` more precise on Windows to fix framepacing
This commit is contained in:
commit
449d90b64e
@ -817,10 +817,22 @@ double OS_Windows::get_unix_time() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void OS_Windows::delay_usec(uint32_t p_usec) const {
|
void OS_Windows::delay_usec(uint32_t p_usec) const {
|
||||||
if (p_usec < 1000) {
|
constexpr uint32_t tolerance = 1000 + 20;
|
||||||
Sleep(1);
|
|
||||||
} else {
|
uint64_t t0 = get_ticks_usec();
|
||||||
Sleep(p_usec / 1000);
|
uint64_t target_time = t0 + p_usec;
|
||||||
|
|
||||||
|
// Calculate sleep duration with a tolerance for fine-tuning.
|
||||||
|
if (p_usec > tolerance) {
|
||||||
|
uint32_t coarse_sleep_usec = p_usec - tolerance;
|
||||||
|
if (coarse_sleep_usec >= 1000) {
|
||||||
|
Sleep(coarse_sleep_usec / 1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spin-wait until we reach the precise target time.
|
||||||
|
while (get_ticks_usec() < target_time) {
|
||||||
|
YieldProcessor();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user