1
0
Fork 0

Merge pull request #103773 from Calinou/windows-low-processor-mode-sleep-cpu

Use more efficient sleep approach on Windows when low-processor mode is enabled
This commit is contained in:
Thaddeus Crews 2025-03-09 09:05:27 -05:00
commit 2324805fdc
No known key found for this signature in database
GPG Key ID: 62181B86FE9E5D84
1 changed files with 19 additions and 11 deletions

View File

@ -2340,18 +2340,26 @@ void OS_Windows::add_frame_delay(bool p_can_draw) {
target_ticks += dynamic_delay;
uint64_t current_ticks = get_ticks_usec();
if (target_ticks > current_ticks + delay_resolution) {
uint64_t delay_time = target_ticks - current_ticks - delay_resolution;
// Make sure we always sleep for a multiple of delay_resolution to avoid overshooting.
// Refer to: https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep#remarks
delay_time = (delay_time / delay_resolution) * delay_resolution;
if (delay_time > 0) {
delay_usec(delay_time);
if (!is_in_low_processor_usage_mode()) {
if (target_ticks > current_ticks + delay_resolution) {
uint64_t delay_time = target_ticks - current_ticks - delay_resolution;
// Make sure we always sleep for a multiple of delay_resolution to avoid overshooting.
// Refer to: https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep#remarks
delay_time = (delay_time / delay_resolution) * delay_resolution;
if (delay_time > 0) {
delay_usec(delay_time);
}
}
// Busy wait for the remainder of time.
while (get_ticks_usec() < target_ticks) {
YieldProcessor();
}
} else {
// Use a more relaxed approach for low processor usage mode.
// This has worse frame pacing but is more power efficient.
if (current_ticks < target_ticks) {
delay_usec(target_ticks - current_ticks);
}
}
// Busy wait for the remainder of time.
while (get_ticks_usec() < target_ticks) {
YieldProcessor();
}
current_ticks = get_ticks_usec();