timer_sync.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*************************************************************************/
  2. /* timer_sync.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "timer_sync.h"
  31. void MainFrameTime::clamp_idle(float min_idle_step, float max_idle_step) {
  32. if (idle_step < min_idle_step) {
  33. idle_step = min_idle_step;
  34. } else if (idle_step > max_idle_step) {
  35. idle_step = max_idle_step;
  36. }
  37. }
  38. /////////////////////////////////
  39. // returns the fraction of p_frame_slice required for the timer to overshoot
  40. // before advance_core considers changing the physics_steps return from
  41. // the typical values as defined by typical_physics_steps
  42. float MainTimerSync::get_physics_jitter_fix() {
  43. return Engine::get_singleton()->get_physics_jitter_fix();
  44. }
  45. // gets our best bet for the average number of physics steps per render frame
  46. // return value: number of frames back this data is consistent
  47. int MainTimerSync::get_average_physics_steps(float &p_min, float &p_max) {
  48. p_min = typical_physics_steps[0];
  49. p_max = p_min + 1;
  50. for (int i = 1; i < CONTROL_STEPS; ++i) {
  51. const float typical_lower = typical_physics_steps[i];
  52. const float current_min = typical_lower / (i + 1);
  53. if (current_min > p_max)
  54. return i; // bail out of further restrictions would void the interval
  55. else if (current_min > p_min)
  56. p_min = current_min;
  57. const float current_max = (typical_lower + 1) / (i + 1);
  58. if (current_max < p_min)
  59. return i;
  60. else if (current_max < p_max)
  61. p_max = current_max;
  62. }
  63. return CONTROL_STEPS;
  64. }
  65. // advance physics clock by p_idle_step, return appropriate number of steps to simulate
  66. MainFrameTime MainTimerSync::advance_core(float p_frame_slice, int p_iterations_per_second, float p_idle_step) {
  67. MainFrameTime ret;
  68. ret.idle_step = p_idle_step;
  69. // simple determination of number of physics iteration
  70. time_accum += ret.idle_step;
  71. ret.physics_steps = floor(time_accum * p_iterations_per_second);
  72. int min_typical_steps = typical_physics_steps[0];
  73. int max_typical_steps = min_typical_steps + 1;
  74. // given the past recorded steps and typical steps to match, calculate bounds for this
  75. // step to be typical
  76. bool update_typical = false;
  77. for (int i = 0; i < CONTROL_STEPS - 1; ++i) {
  78. int steps_left_to_match_typical = typical_physics_steps[i + 1] - accumulated_physics_steps[i];
  79. if (steps_left_to_match_typical > max_typical_steps ||
  80. steps_left_to_match_typical + 1 < min_typical_steps) {
  81. update_typical = true;
  82. break;
  83. }
  84. if (steps_left_to_match_typical > min_typical_steps)
  85. min_typical_steps = steps_left_to_match_typical;
  86. if (steps_left_to_match_typical + 1 < max_typical_steps)
  87. max_typical_steps = steps_left_to_match_typical + 1;
  88. }
  89. // try to keep it consistent with previous iterations
  90. if (ret.physics_steps < min_typical_steps) {
  91. const int max_possible_steps = floor((time_accum)*p_iterations_per_second + get_physics_jitter_fix());
  92. if (max_possible_steps < min_typical_steps) {
  93. ret.physics_steps = max_possible_steps;
  94. update_typical = true;
  95. } else {
  96. ret.physics_steps = min_typical_steps;
  97. }
  98. } else if (ret.physics_steps > max_typical_steps) {
  99. const int min_possible_steps = floor((time_accum)*p_iterations_per_second - get_physics_jitter_fix());
  100. if (min_possible_steps > max_typical_steps) {
  101. ret.physics_steps = min_possible_steps;
  102. update_typical = true;
  103. } else {
  104. ret.physics_steps = max_typical_steps;
  105. }
  106. }
  107. time_accum -= ret.physics_steps * p_frame_slice;
  108. // keep track of accumulated step counts
  109. for (int i = CONTROL_STEPS - 2; i >= 0; --i) {
  110. accumulated_physics_steps[i + 1] = accumulated_physics_steps[i] + ret.physics_steps;
  111. }
  112. accumulated_physics_steps[0] = ret.physics_steps;
  113. if (update_typical) {
  114. for (int i = CONTROL_STEPS - 1; i >= 0; --i) {
  115. if (typical_physics_steps[i] > accumulated_physics_steps[i]) {
  116. typical_physics_steps[i] = accumulated_physics_steps[i];
  117. } else if (typical_physics_steps[i] < accumulated_physics_steps[i] - 1) {
  118. typical_physics_steps[i] = accumulated_physics_steps[i] - 1;
  119. }
  120. }
  121. }
  122. return ret;
  123. }
  124. // calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero
  125. MainFrameTime MainTimerSync::advance_checked(float p_frame_slice, int p_iterations_per_second, float p_idle_step) {
  126. if (fixed_fps != -1)
  127. p_idle_step = 1.0 / fixed_fps;
  128. // compensate for last deficit
  129. p_idle_step += time_deficit;
  130. MainFrameTime ret = advance_core(p_frame_slice, p_iterations_per_second, p_idle_step);
  131. // we will do some clamping on ret.idle_step and need to sync those changes to time_accum,
  132. // that's easiest if we just remember their fixed difference now
  133. const double idle_minus_accum = ret.idle_step - time_accum;
  134. // first, least important clamping: keep ret.idle_step consistent with typical_physics_steps.
  135. // this smoothes out the idle steps and culls small but quick variations.
  136. {
  137. float min_average_physics_steps, max_average_physics_steps;
  138. int consistent_steps = get_average_physics_steps(min_average_physics_steps, max_average_physics_steps);
  139. if (consistent_steps > 3) {
  140. ret.clamp_idle(min_average_physics_steps * p_frame_slice, max_average_physics_steps * p_frame_slice);
  141. }
  142. }
  143. // second clamping: keep abs(time_deficit) < jitter_fix * frame_slise
  144. float max_clock_deviation = get_physics_jitter_fix() * p_frame_slice;
  145. ret.clamp_idle(p_idle_step - max_clock_deviation, p_idle_step + max_clock_deviation);
  146. // last clamping: make sure time_accum is between 0 and p_frame_slice for consistency between physics and idle
  147. ret.clamp_idle(idle_minus_accum, idle_minus_accum + p_frame_slice);
  148. // restore time_accum
  149. time_accum = ret.idle_step - idle_minus_accum;
  150. // track deficit
  151. time_deficit = p_idle_step - ret.idle_step;
  152. return ret;
  153. }
  154. // determine wall clock step since last iteration
  155. float MainTimerSync::get_cpu_idle_step() {
  156. uint64_t cpu_ticks_elapsed = current_cpu_ticks_usec - last_cpu_ticks_usec;
  157. last_cpu_ticks_usec = current_cpu_ticks_usec;
  158. return cpu_ticks_elapsed / 1000000.0;
  159. }
  160. MainTimerSync::MainTimerSync() :
  161. last_cpu_ticks_usec(0),
  162. current_cpu_ticks_usec(0),
  163. time_accum(0),
  164. time_deficit(0),
  165. fixed_fps(0) {
  166. for (int i = CONTROL_STEPS - 1; i >= 0; --i) {
  167. typical_physics_steps[i] = i;
  168. accumulated_physics_steps[i] = i;
  169. }
  170. }
  171. // start the clock
  172. void MainTimerSync::init(uint64_t p_cpu_ticks_usec) {
  173. current_cpu_ticks_usec = last_cpu_ticks_usec = p_cpu_ticks_usec;
  174. }
  175. // set measured wall clock time
  176. void MainTimerSync::set_cpu_ticks_usec(uint64_t p_cpu_ticks_usec) {
  177. current_cpu_ticks_usec = p_cpu_ticks_usec;
  178. }
  179. void MainTimerSync::set_fixed_fps(int p_fixed_fps) {
  180. fixed_fps = p_fixed_fps;
  181. }
  182. // advance one frame, return timesteps to take
  183. MainFrameTime MainTimerSync::advance(float p_frame_slice, int p_iterations_per_second) {
  184. float cpu_idle_step = get_cpu_idle_step();
  185. return advance_checked(p_frame_slice, p_iterations_per_second, cpu_idle_step);
  186. }