Improve the low processor mode sleep precision

The Low Processor Usage Mode Sleep Usec setting is now used as a
FPS limiter rather than a constant sleep duration.

This will increase CPU/GPU usage slightly due to the higher
effective FPS, but the increase in overall smoothness is worth it.

If both Force Fps and Low Processor Usage Mode settings are enabled
in the project settings, only the setting that causes the highest
sleep duration will be retained.

This closes #11030.

(cherry picked from commit 1c28b269d8)
This commit is contained in:
Hugo Locurcio 2020-06-15 19:30:39 +02:00 committed by Rémi Verschelde
parent 530307e7ec
commit 087a83fd54

View file

@ -2163,22 +2163,37 @@ bool Main::iteration() {
if (fixed_fps != -1) if (fixed_fps != -1)
return exit; return exit;
if (OS::get_singleton()->is_in_low_processor_usage_mode() || !OS::get_singleton()->can_draw()) const uint32_t frame_delay = Engine::get_singleton()->get_frame_delay();
OS::get_singleton()->delay_usec(OS::get_singleton()->get_low_processor_usage_mode_sleep_usec()); //apply some delay to force idle time if (frame_delay) {
else { // Add fixed frame delay to decrease CPU/GPU usage. This doesn't take
uint32_t frame_delay = Engine::get_singleton()->get_frame_delay(); // the actual frame time into account.
if (frame_delay) // Due to the high fluctuation of the actual sleep duration, it's not recommended
OS::get_singleton()->delay_usec(Engine::get_singleton()->get_frame_delay() * 1000); // to use this as a FPS limiter.
OS::get_singleton()->delay_usec(frame_delay * 1000);
} }
int target_fps = Engine::get_singleton()->get_target_fps(); // Add a dynamic frame delay to decrease CPU/GPU usage. This takes the
// previous frame time into account for a smoother result.
uint64_t dynamic_delay = 0;
if (OS::get_singleton()->is_in_low_processor_usage_mode() || !OS::get_singleton()->can_draw()) {
dynamic_delay = OS::get_singleton()->get_low_processor_usage_mode_sleep_usec();
}
const int target_fps = Engine::get_singleton()->get_target_fps();
if (target_fps > 0 && !Engine::get_singleton()->is_editor_hint()) { if (target_fps > 0 && !Engine::get_singleton()->is_editor_hint()) {
uint64_t time_step = 1000000L / target_fps; // Override the low processor usage mode sleep delay if the target FPS is lower.
target_ticks += time_step; dynamic_delay = MAX(dynamic_delay, (uint64_t)(1000000 / target_fps));
}
if (dynamic_delay > 0) {
target_ticks += dynamic_delay;
uint64_t current_ticks = OS::get_singleton()->get_ticks_usec(); uint64_t current_ticks = OS::get_singleton()->get_ticks_usec();
if (current_ticks < target_ticks) OS::get_singleton()->delay_usec(target_ticks - current_ticks);
if (current_ticks < target_ticks) {
OS::get_singleton()->delay_usec(target_ticks - current_ticks);
}
current_ticks = OS::get_singleton()->get_ticks_usec(); current_ticks = OS::get_singleton()->get_ticks_usec();
target_ticks = MIN(MAX(target_ticks, current_ticks - time_step), current_ticks + time_step); target_ticks = MIN(MAX(target_ticks, current_ticks - dynamic_delay), current_ticks + dynamic_delay);
} }
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED