2018-05-16 17:19:33 +00:00
|
|
|
/*************************************************************************/
|
2019-02-12 13:23:35 +00:00
|
|
|
/* main_timer_sync.cpp */
|
2018-05-16 17:19:33 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-05-16 17:19:33 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2019-02-12 13:23:35 +00:00
|
|
|
#include "main_timer_sync.h"
|
2018-05-15 20:12:35 +00:00
|
|
|
|
2020-12-22 09:50:29 +00:00
|
|
|
void MainFrameTime::clamp_process_step(float min_process_step, float 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;
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
|
2020-12-22 09:50:29 +00:00
|
|
|
// returns the fraction of p_physics_step required for the timer to overshoot
|
2018-05-15 20:12:35 +00:00
|
|
|
// before advance_core considers changing the physics_steps return from
|
|
|
|
// the typical values as defined by typical_physics_steps
|
|
|
|
float MainTimerSync::get_physics_jitter_fix() {
|
|
|
|
return Engine::get_singleton()->get_physics_jitter_fix();
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets our best bet for the average number of physics steps per render frame
|
|
|
|
// return value: number of frames back this data is consistent
|
|
|
|
int MainTimerSync::get_average_physics_steps(float &p_min, float &p_max) {
|
|
|
|
p_min = typical_physics_steps[0];
|
|
|
|
p_max = p_min + 1;
|
|
|
|
|
|
|
|
for (int i = 1; i < CONTROL_STEPS; ++i) {
|
|
|
|
const float typical_lower = typical_physics_steps[i];
|
|
|
|
const float current_min = typical_lower / (i + 1);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (current_min > p_max) {
|
2018-05-15 20:12:35 +00:00
|
|
|
return i; // bail out of further restrictions would void the interval
|
2020-05-14 14:41:43 +00:00
|
|
|
} else if (current_min > p_min) {
|
2018-05-15 20:12:35 +00:00
|
|
|
p_min = current_min;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-15 20:12:35 +00:00
|
|
|
const float current_max = (typical_lower + 1) / (i + 1);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (current_max < p_min) {
|
2018-05-15 20:12:35 +00:00
|
|
|
return i;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else if (current_max < p_max) {
|
2018-05-15 20:12:35 +00:00
|
|
|
p_max = current_max;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return CONTROL_STEPS;
|
|
|
|
}
|
|
|
|
|
2020-12-22 09:50:29 +00:00
|
|
|
// advance physics clock by p_process_step, return appropriate number of steps to simulate
|
|
|
|
MainFrameTime MainTimerSync::advance_core(float p_physics_step, int p_physics_fps, float p_process_step) {
|
2018-05-15 20:12:35 +00:00
|
|
|
MainFrameTime ret;
|
|
|
|
|
2020-12-22 09:50:29 +00:00
|
|
|
ret.process_step = p_process_step;
|
2018-05-15 20:12:35 +00:00
|
|
|
|
|
|
|
// simple determination of number of physics iteration
|
2020-12-22 09:50:29 +00:00
|
|
|
time_accum += ret.process_step;
|
|
|
|
ret.physics_steps = floor(time_accum * p_physics_fps);
|
2018-05-15 20:12:35 +00:00
|
|
|
|
|
|
|
int min_typical_steps = typical_physics_steps[0];
|
|
|
|
int max_typical_steps = min_typical_steps + 1;
|
|
|
|
|
2018-09-13 01:38:39 +00:00
|
|
|
// given the past recorded steps and typical steps to match, calculate bounds for this
|
2018-05-15 20:12:35 +00:00
|
|
|
// step to be typical
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (steps_left_to_match_typical > min_typical_steps) {
|
2018-05-15 20:12:35 +00:00
|
|
|
min_typical_steps = steps_left_to_match_typical;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (steps_left_to_match_typical + 1 < max_typical_steps) {
|
2018-05-15 20:12:35 +00:00
|
|
|
max_typical_steps = steps_left_to_match_typical + 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// try to keep it consistent with previous iterations
|
|
|
|
if (ret.physics_steps < min_typical_steps) {
|
2020-12-22 09:50:29 +00:00
|
|
|
const int max_possible_steps = floor((time_accum)*p_physics_fps + get_physics_jitter_fix());
|
2018-05-15 20:12:35 +00:00
|
|
|
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) {
|
2020-12-22 09:50:29 +00:00
|
|
|
const int min_possible_steps = floor((time_accum)*p_physics_fps - get_physics_jitter_fix());
|
2018-05-15 20:12:35 +00:00
|
|
|
if (min_possible_steps > max_typical_steps) {
|
|
|
|
ret.physics_steps = min_possible_steps;
|
|
|
|
update_typical = true;
|
|
|
|
} else {
|
|
|
|
ret.physics_steps = max_typical_steps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 09:50:29 +00:00
|
|
|
time_accum -= ret.physics_steps * p_physics_step;
|
2018-05-15 20:12:35 +00:00
|
|
|
|
|
|
|
// keep track of accumulated step counts
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero
|
2020-12-22 09:50:29 +00:00
|
|
|
MainFrameTime MainTimerSync::advance_checked(float p_physics_step, int p_physics_fps, float p_process_step) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (fixed_fps != -1) {
|
2020-12-22 09:50:29 +00:00
|
|
|
p_process_step = 1.0 / fixed_fps;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-15 20:12:35 +00:00
|
|
|
|
|
|
|
// compensate for last deficit
|
2020-12-22 09:50:29 +00:00
|
|
|
p_process_step += time_deficit;
|
2018-05-15 20:12:35 +00:00
|
|
|
|
2020-12-22 09:50:29 +00:00
|
|
|
MainFrameTime ret = advance_core(p_physics_step, p_physics_fps, p_process_step);
|
2018-05-15 20:12:35 +00:00
|
|
|
|
2020-12-22 09:50:29 +00:00
|
|
|
// we will do some clamping on ret.process_step and need to sync those changes to time_accum,
|
2018-05-15 20:12:35 +00:00
|
|
|
// that's easiest if we just remember their fixed difference now
|
2020-12-22 09:50:29 +00:00
|
|
|
const double process_minus_accum = ret.process_step - time_accum;
|
2018-05-15 20:12:35 +00:00
|
|
|
|
2020-12-22 09:50:29 +00:00
|
|
|
// first, least important clamping: keep ret.process_step consistent with typical_physics_steps.
|
|
|
|
// this smoothes out the process steps and culls small but quick variations.
|
2018-05-15 20:12:35 +00:00
|
|
|
{
|
|
|
|
float 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) {
|
2020-12-22 09:50:29 +00:00
|
|
|
ret.clamp_process_step(min_average_physics_steps * p_physics_step, max_average_physics_steps * p_physics_step);
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// second clamping: keep abs(time_deficit) < jitter_fix * frame_slise
|
2020-12-22 09:50:29 +00:00
|
|
|
float 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);
|
2018-05-15 20:12:35 +00:00
|
|
|
|
2020-12-22 09:50:29 +00:00
|
|
|
// last clamping: make sure time_accum is between 0 and p_physics_step for consistency between physics and process
|
|
|
|
ret.clamp_process_step(process_minus_accum, process_minus_accum + p_physics_step);
|
2018-05-15 20:12:35 +00:00
|
|
|
|
|
|
|
// restore time_accum
|
2020-12-22 09:50:29 +00:00
|
|
|
time_accum = ret.process_step - process_minus_accum;
|
2018-05-15 20:12:35 +00:00
|
|
|
|
|
|
|
// track deficit
|
2020-12-22 09:50:29 +00:00
|
|
|
time_deficit = p_process_step - ret.process_step;
|
2018-05-15 20:12:35 +00:00
|
|
|
|
2020-12-22 09:50:29 +00:00
|
|
|
// p_physics_step is 1.0 / iterations_per_sec
|
2019-07-01 16:42:02 +00:00
|
|
|
// i.e. the time in seconds taken by a physics tick
|
2020-12-22 09:50:29 +00:00
|
|
|
ret.interpolation_fraction = time_accum / p_physics_step;
|
2019-07-01 16:42:02 +00:00
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine wall clock step since last iteration
|
2020-12-22 09:50:29 +00:00
|
|
|
float MainTimerSync::get_cpu_process_step() {
|
2018-05-15 20:12:35 +00:00
|
|
|
uint64_t cpu_ticks_elapsed = current_cpu_ticks_usec - last_cpu_ticks_usec;
|
|
|
|
last_cpu_ticks_usec = current_cpu_ticks_usec;
|
|
|
|
|
|
|
|
return cpu_ticks_elapsed / 1000000.0;
|
|
|
|
}
|
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
MainTimerSync::MainTimerSync() {
|
2018-05-15 20:12:35 +00:00
|
|
|
for (int i = CONTROL_STEPS - 1; i >= 0; --i) {
|
|
|
|
typical_physics_steps[i] = i;
|
|
|
|
accumulated_physics_steps[i] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start the clock
|
|
|
|
void MainTimerSync::init(uint64_t p_cpu_ticks_usec) {
|
|
|
|
current_cpu_ticks_usec = last_cpu_ticks_usec = p_cpu_ticks_usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set measured wall clock time
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-12-22 09:50:29 +00:00
|
|
|
// advance one physics frame, return timesteps to take
|
|
|
|
MainFrameTime MainTimerSync::advance(float p_physics_step, int p_physics_fps) {
|
|
|
|
float cpu_process_step = get_cpu_process_step();
|
2018-05-15 20:12:35 +00:00
|
|
|
|
2020-12-22 09:50:29 +00:00
|
|
|
return advance_checked(p_physics_step, p_physics_fps, cpu_process_step);
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|