123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- #include "timer_sync.h"
- void MainFrameTime::clamp_idle(float min_idle_step, float max_idle_step) {
- if (idle_step < min_idle_step) {
- idle_step = min_idle_step;
- } else if (idle_step > max_idle_step) {
- idle_step = max_idle_step;
- }
- }
- float MainTimerSync::get_physics_jitter_fix() {
- return Engine::get_singleton()->get_physics_jitter_fix();
- }
- 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);
- if (current_min > p_max)
- return i;
- else if (current_min > p_min)
- p_min = current_min;
- const float 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(float p_frame_slice, int p_iterations_per_second, float p_idle_step) {
- MainFrameTime ret;
- ret.idle_step = p_idle_step;
-
- time_accum += ret.idle_step;
- ret.physics_steps = floor(time_accum * p_iterations_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;
- }
-
- if (ret.physics_steps < min_typical_steps) {
- const int max_possible_steps = floor((time_accum)*p_iterations_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 = floor((time_accum)*p_iterations_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;
- }
- }
- time_accum -= ret.physics_steps * p_frame_slice;
-
- 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(float p_frame_slice, int p_iterations_per_second, float p_idle_step) {
- if (fixed_fps != -1)
- p_idle_step = 1.0 / fixed_fps;
-
- p_idle_step += time_deficit;
- MainFrameTime ret = advance_core(p_frame_slice, p_iterations_per_second, p_idle_step);
-
-
- const double idle_minus_accum = ret.idle_step - time_accum;
-
-
- {
- 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) {
- ret.clamp_idle(min_average_physics_steps * p_frame_slice, max_average_physics_steps * p_frame_slice);
- }
- }
-
- float max_clock_deviation = get_physics_jitter_fix() * p_frame_slice;
- ret.clamp_idle(p_idle_step - max_clock_deviation, p_idle_step + max_clock_deviation);
-
- ret.clamp_idle(idle_minus_accum, idle_minus_accum + p_frame_slice);
-
- time_accum = ret.idle_step - idle_minus_accum;
-
- time_deficit = p_idle_step - ret.idle_step;
- return ret;
- }
- float MainTimerSync::get_cpu_idle_step() {
- 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;
- }
- MainTimerSync::MainTimerSync() :
- last_cpu_ticks_usec(0),
- current_cpu_ticks_usec(0),
- time_accum(0),
- time_deficit(0),
- fixed_fps(0) {
- 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(float p_frame_slice, int p_iterations_per_second) {
- float cpu_idle_step = get_cpu_idle_step();
- return advance_checked(p_frame_slice, p_iterations_per_second, cpu_idle_step);
- }
|