#include "main_timer_sync.h"
#include "core/os/os.h"
#include "servers/display_server.h"
void MainFrameTime::clamp_process_step(double min_process_step, double max_process_step) {
if (process_step < min_process_step) {
process_step = min_process_step;
} else if (process_step > max_process_step) {
process_step = max_process_step;
}
}
void MainTimerSync::DeltaSmoother::update_refresh_rate_estimator(int64_t p_delta) {
if (_estimate_locked) {
return;
}
_estimator_total_delta += p_delta;
_estimator_delta_readings++;
const int NUM_READINGS = 60;
if (_estimator_delta_readings < NUM_READINGS) {
return;
}
p_delta = _estimator_total_delta / NUM_READINGS;
_estimator_delta_readings = 0;
_estimator_total_delta = 0;
int fps = Math::round(1000000.0 / p_delta);
if (_estimated_fps == 0) {
if (fps >= 50) {
_estimated_fps = fps;
#ifdef GODOT_DEBUG_DELTA_SMOOTHER
print_line("initial guess (average measured) refresh rate: " + itos(fps));
#endif
} else {
return;
}
}
if (fps == _estimated_fps) {
_hits_at_estimated++;
if (_estimate_complete && _hits_at_estimated == 20) {
_estimate_locked = true;
#ifdef GODOT_DEBUG_DELTA_SMOOTHER
print_line("estimate LOCKED at " + itos(_estimated_fps) + " fps");
#endif
return;
}
if ((!_estimate_complete) && (_hits_at_estimated > 2)) {
if (_estimated_fps) {
_estimate_complete = true;
_vsync_delta = 1000000 / _estimated_fps;
#ifdef GODOT_DEBUG_DELTA_SMOOTHER
print_line("estimate complete. vsync_delta " + itos(_vsync_delta) + ", fps " + itos(_estimated_fps));
#endif
}
}
#ifdef GODOT_DEBUG_DELTA_SMOOTHER
if ((_hits_at_estimated % (400 / NUM_READINGS)) == 0) {
String sz = "hits at estimated : " + itos(_hits_at_estimated) + ", above : " + itos(_hits_above_estimated) + "( " + itos(_hits_one_above_estimated) + " ), below : " + itos(_hits_below_estimated) + " (" + itos(_hits_one_below_estimated) + " )";
print_line(sz);
}
#endif
return;
}
const int SIGNIFICANCE_UP = 1;
const int SIGNIFICANCE_DOWN = 2;
if (fps < _estimated_fps) {
if (fps == (_estimated_fps - 1)) {
_hits_one_below_estimated++;
if ((_hits_one_below_estimated > _hits_at_estimated) && (_hits_one_below_estimated > SIGNIFICANCE_DOWN)) {
_estimated_fps--;
made_new_estimate();
}
return;
} else {
_hits_below_estimated++;
bool established = _estimate_complete && (_hits_at_estimated > 10);
if (!established) {
if (((_hits_below_estimated / 8) > _hits_at_estimated) && (_hits_below_estimated > SIGNIFICANCE_DOWN)) {
_estimated_fps--;
made_new_estimate();
}
}
return;
}
}
if (fps == (_estimated_fps + 1)) {
_hits_one_above_estimated++;
if ((_hits_one_above_estimated > _hits_at_estimated) && (_hits_one_above_estimated > SIGNIFICANCE_UP)) {
_estimated_fps++;
made_new_estimate();
}
return;
} else {
_hits_above_estimated++;
if ((_hits_above_estimated > _hits_at_estimated) && (_hits_above_estimated > SIGNIFICANCE_UP)) {
int change = fps - _estimated_fps;
change /= 2;
change = MAX(1, change);
_estimated_fps += change;
made_new_estimate();
}
return;
}
}
bool MainTimerSync::DeltaSmoother::fps_allows_smoothing(int64_t p_delta) {
_measurement_time += p_delta;
_measurement_frame_count++;
if (_measurement_frame_count == _measurement_end_frame) {
if (_estimate_complete) {
int64_t time_passed = _measurement_time - _measurement_start_time;
time_passed /= MEASURE_FPS_OVER_NUM_FRAMES;
if (time_passed) {
double fps = 1000000.0 / time_passed;
double ratio = fps / (double)_estimated_fps;
if ((ratio > 0.95) && (ratio < 1.05)) {
_measurement_allows_smoothing = true;
} else {
_measurement_allows_smoothing = false;
}
}
}
_measurement_start_time = _measurement_time;
_measurement_end_frame += MEASURE_FPS_OVER_NUM_FRAMES;
}
return _measurement_allows_smoothing;
}
int64_t MainTimerSync::DeltaSmoother::smooth_delta(int64_t p_delta) {
if (!OS::get_singleton()->is_delta_smoothing_enabled() || Engine::get_singleton()->is_editor_hint()) {
return p_delta;
}
DisplayServer::VSyncMode vsync_mode = DisplayServer::get_singleton()->window_get_vsync_mode(DisplayServer::MAIN_WINDOW_ID);
if (vsync_mode != DisplayServer::VSYNC_ENABLED) {
return p_delta;
}
if (p_delta > 1000000) {
return p_delta;
}
if (!fps_allows_smoothing(p_delta)) {
return p_delta;
}
if (p_delta < 1000) {
return p_delta;
}
update_refresh_rate_estimator(p_delta);
if (!_estimate_complete) {
return p_delta;
}
_leftover_time += p_delta;
int64_t units = _leftover_time / _vsync_delta;
units = MAX(units, 1);
_leftover_time -= units * _vsync_delta;
return units * _vsync_delta;
}
double MainTimerSync::get_physics_jitter_fix() {
return Engine::get_singleton()->get_physics_jitter_fix();
}
int MainTimerSync::get_average_physics_steps(double &p_min, double &p_max) {
p_min = typical_physics_steps[0];
p_max = p_min + 1;
for (int i = 1; i < CONTROL_STEPS; ++i) {
const double typical_lower = typical_physics_steps[i];
const double current_min = typical_lower / (i + 1);
if (current_min > p_max) {
return i;
} else if (current_min > p_min) {
p_min = current_min;
}
const double current_max = (typical_lower + 1) / (i + 1);
if (current_max < p_min) {
return i;
} else if (current_max < p_max) {
p_max = current_max;
}
}
return CONTROL_STEPS;
}
MainFrameTime MainTimerSync::advance_core(double p_physics_step, int p_physics_ticks_per_second, double p_process_step) {
MainFrameTime ret;
ret.process_step = p_process_step;
time_accum += ret.process_step;
ret.physics_steps = std::floor(time_accum * p_physics_ticks_per_second);
int min_typical_steps = typical_physics_steps[0];
int max_typical_steps = min_typical_steps + 1;
bool update_typical = false;
for (int i = 0; i < CONTROL_STEPS - 1; ++i) {
int steps_left_to_match_typical = typical_physics_steps[i + 1] - accumulated_physics_steps[i];
if (steps_left_to_match_typical > max_typical_steps ||
steps_left_to_match_typical + 1 < min_typical_steps) {
update_typical = true;
break;
}
if (steps_left_to_match_typical > min_typical_steps) {
min_typical_steps = steps_left_to_match_typical;
}
if (steps_left_to_match_typical + 1 < max_typical_steps) {
max_typical_steps = steps_left_to_match_typical + 1;
}
}
#ifdef DEBUG_ENABLED
if (max_typical_steps < 0) {
WARN_PRINT_ONCE("`max_typical_steps` is negative. This could hint at an engine bug or system timer misconfiguration.");
}
#endif
if (ret.physics_steps < min_typical_steps) {
const int max_possible_steps = std::floor((time_accum)*p_physics_ticks_per_second + get_physics_jitter_fix());
if (max_possible_steps < min_typical_steps) {
ret.physics_steps = max_possible_steps;
update_typical = true;
} else {
ret.physics_steps = min_typical_steps;
}
} else if (ret.physics_steps > max_typical_steps) {
const int min_possible_steps = std::floor((time_accum)*p_physics_ticks_per_second - get_physics_jitter_fix());
if (min_possible_steps > max_typical_steps) {
ret.physics_steps = min_possible_steps;
update_typical = true;
} else {
ret.physics_steps = max_typical_steps;
}
}
if (ret.physics_steps < 0) {
ret.physics_steps = 0;
}
time_accum -= ret.physics_steps * p_physics_step;
for (int i = CONTROL_STEPS - 2; i >= 0; --i) {
accumulated_physics_steps[i + 1] = accumulated_physics_steps[i] + ret.physics_steps;
}
accumulated_physics_steps[0] = ret.physics_steps;
if (update_typical) {
for (int i = CONTROL_STEPS - 1; i >= 0; --i) {
if (typical_physics_steps[i] > accumulated_physics_steps[i]) {
typical_physics_steps[i] = accumulated_physics_steps[i];
} else if (typical_physics_steps[i] < accumulated_physics_steps[i] - 1) {
typical_physics_steps[i] = accumulated_physics_steps[i] - 1;
}
}
}
return ret;
}
MainFrameTime MainTimerSync::advance_checked(double p_physics_step, int p_physics_ticks_per_second, double p_process_step) {
if (fixed_fps != -1) {
p_process_step = 1.0 / fixed_fps;
}
float min_output_step = p_process_step / 8;
min_output_step = MAX(min_output_step, 1E-6);
p_process_step += time_deficit;
MainFrameTime ret = advance_core(p_physics_step, p_physics_ticks_per_second, p_process_step);
const double process_minus_accum = ret.process_step - time_accum;
{
double min_average_physics_steps, max_average_physics_steps;
int consistent_steps = get_average_physics_steps(min_average_physics_steps, max_average_physics_steps);
if (consistent_steps > 3) {
ret.clamp_process_step(min_average_physics_steps * p_physics_step, max_average_physics_steps * p_physics_step);
}
}
double max_clock_deviation = get_physics_jitter_fix() * p_physics_step;
ret.clamp_process_step(p_process_step - max_clock_deviation, p_process_step + max_clock_deviation);
ret.clamp_process_step(process_minus_accum, process_minus_accum + p_physics_step);
if (ret.process_step < min_output_step) {
ret.process_step = min_output_step;
}
time_accum = ret.process_step - process_minus_accum;
#ifdef DEBUG_ENABLED
if (time_accum < -1E-7) {
WARN_PRINT_ONCE("Intermediate value of `time_accum` is negative. This could hint at an engine bug or system timer misconfiguration.");
}
#endif
if (time_accum > p_physics_step) {
const int extra_physics_steps = std::floor(time_accum * p_physics_ticks_per_second);
time_accum -= extra_physics_steps * p_physics_step;
ret.physics_steps += extra_physics_steps;
}
#ifdef DEBUG_ENABLED
if (time_accum < -1E-7) {
WARN_PRINT_ONCE("Final value of `time_accum` is negative. It should always be between 0 and `p_physics_step`. This hints at an engine bug.");
}
if (time_accum > p_physics_step + 1E-7) {
WARN_PRINT_ONCE("Final value of `time_accum` is larger than `p_physics_step`. It should always be between 0 and `p_physics_step`. This hints at an engine bug.");
}
#endif
time_deficit = p_process_step - ret.process_step;
ret.interpolation_fraction = time_accum / p_physics_step;
return ret;
}
double MainTimerSync::get_cpu_process_step() {
uint64_t cpu_ticks_elapsed = current_cpu_ticks_usec - last_cpu_ticks_usec;
last_cpu_ticks_usec = current_cpu_ticks_usec;
cpu_ticks_elapsed = _delta_smoother.smooth_delta(cpu_ticks_elapsed);
return cpu_ticks_elapsed / 1000000.0;
}
MainTimerSync::MainTimerSync() {
for (int i = CONTROL_STEPS - 1; i >= 0; --i) {
typical_physics_steps[i] = i;
accumulated_physics_steps[i] = i;
}
}
void MainTimerSync::init(uint64_t p_cpu_ticks_usec) {
current_cpu_ticks_usec = last_cpu_ticks_usec = p_cpu_ticks_usec;
}
void MainTimerSync::set_cpu_ticks_usec(uint64_t p_cpu_ticks_usec) {
current_cpu_ticks_usec = p_cpu_ticks_usec;
}
void MainTimerSync::set_fixed_fps(int p_fixed_fps) {
fixed_fps = p_fixed_fps;
}
MainFrameTime MainTimerSync::advance(double p_physics_step, int p_physics_ticks_per_second) {
double cpu_process_step = get_cpu_process_step();
return advance_checked(p_physics_step, p_physics_ticks_per_second, cpu_process_step);
}