main_timer_sync.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*************************************************************************/
  2. /* main_timer_sync.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "main_timer_sync.h"
  31. void MainFrameTime::clamp_process_step(double min_process_step, double max_process_step) {
  32. if (process_step < min_process_step) {
  33. process_step = min_process_step;
  34. } else if (process_step > max_process_step) {
  35. process_step = max_process_step;
  36. }
  37. }
  38. /////////////////////////////////
  39. // returns the fraction of p_physics_step 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. double 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(double &p_min, double &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 double typical_lower = typical_physics_steps[i];
  52. const double current_min = typical_lower / (i + 1);
  53. if (current_min > p_max) {
  54. return i; // bail out if further restrictions would void the interval
  55. } else if (current_min > p_min) {
  56. p_min = current_min;
  57. }
  58. const double current_max = (typical_lower + 1) / (i + 1);
  59. if (current_max < p_min) {
  60. return i;
  61. } else if (current_max < p_max) {
  62. p_max = current_max;
  63. }
  64. }
  65. return CONTROL_STEPS;
  66. }
  67. // advance physics clock by p_process_step, return appropriate number of steps to simulate
  68. MainFrameTime MainTimerSync::advance_core(double p_physics_step, int p_physics_ticks_per_second, double p_process_step) {
  69. MainFrameTime ret;
  70. ret.process_step = p_process_step;
  71. // simple determination of number of physics iteration
  72. time_accum += ret.process_step;
  73. ret.physics_steps = floor(time_accum * p_physics_ticks_per_second);
  74. int min_typical_steps = typical_physics_steps[0];
  75. int max_typical_steps = min_typical_steps + 1;
  76. // given the past recorded steps and typical steps to match, calculate bounds for this
  77. // step to be typical
  78. bool update_typical = false;
  79. for (int i = 0; i < CONTROL_STEPS - 1; ++i) {
  80. int steps_left_to_match_typical = typical_physics_steps[i + 1] - accumulated_physics_steps[i];
  81. if (steps_left_to_match_typical > max_typical_steps ||
  82. steps_left_to_match_typical + 1 < min_typical_steps) {
  83. update_typical = true;
  84. break;
  85. }
  86. if (steps_left_to_match_typical > min_typical_steps) {
  87. min_typical_steps = steps_left_to_match_typical;
  88. }
  89. if (steps_left_to_match_typical + 1 < max_typical_steps) {
  90. max_typical_steps = steps_left_to_match_typical + 1;
  91. }
  92. }
  93. #ifdef DEBUG_ENABLED
  94. if (max_typical_steps < 0) {
  95. WARN_PRINT_ONCE("`max_typical_steps` is negative. This could hint at an engine bug or system timer misconfiguration.");
  96. }
  97. #endif
  98. // try to keep it consistent with previous iterations
  99. if (ret.physics_steps < min_typical_steps) {
  100. const int max_possible_steps = floor((time_accum)*p_physics_ticks_per_second + get_physics_jitter_fix());
  101. if (max_possible_steps < min_typical_steps) {
  102. ret.physics_steps = max_possible_steps;
  103. update_typical = true;
  104. } else {
  105. ret.physics_steps = min_typical_steps;
  106. }
  107. } else if (ret.physics_steps > max_typical_steps) {
  108. const int min_possible_steps = floor((time_accum)*p_physics_ticks_per_second - get_physics_jitter_fix());
  109. if (min_possible_steps > max_typical_steps) {
  110. ret.physics_steps = min_possible_steps;
  111. update_typical = true;
  112. } else {
  113. ret.physics_steps = max_typical_steps;
  114. }
  115. }
  116. if (ret.physics_steps < 0) {
  117. ret.physics_steps = 0;
  118. }
  119. time_accum -= ret.physics_steps * p_physics_step;
  120. // keep track of accumulated step counts
  121. for (int i = CONTROL_STEPS - 2; i >= 0; --i) {
  122. accumulated_physics_steps[i + 1] = accumulated_physics_steps[i] + ret.physics_steps;
  123. }
  124. accumulated_physics_steps[0] = ret.physics_steps;
  125. if (update_typical) {
  126. for (int i = CONTROL_STEPS - 1; i >= 0; --i) {
  127. if (typical_physics_steps[i] > accumulated_physics_steps[i]) {
  128. typical_physics_steps[i] = accumulated_physics_steps[i];
  129. } else if (typical_physics_steps[i] < accumulated_physics_steps[i] - 1) {
  130. typical_physics_steps[i] = accumulated_physics_steps[i] - 1;
  131. }
  132. }
  133. }
  134. return ret;
  135. }
  136. // calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero
  137. MainFrameTime MainTimerSync::advance_checked(double p_physics_step, int p_physics_ticks_per_second, double p_process_step) {
  138. if (fixed_fps != -1) {
  139. p_process_step = 1.0 / fixed_fps;
  140. }
  141. float min_output_step = p_process_step / 8;
  142. min_output_step = MAX(min_output_step, 1E-6);
  143. // compensate for last deficit
  144. p_process_step += time_deficit;
  145. MainFrameTime ret = advance_core(p_physics_step, p_physics_ticks_per_second, p_process_step);
  146. // we will do some clamping on ret.process_step and need to sync those changes to time_accum,
  147. // that's easiest if we just remember their fixed difference now
  148. const double process_minus_accum = ret.process_step - time_accum;
  149. // first, least important clamping: keep ret.process_step consistent with typical_physics_steps.
  150. // this smoothes out the process steps and culls small but quick variations.
  151. {
  152. double min_average_physics_steps, max_average_physics_steps;
  153. int consistent_steps = get_average_physics_steps(min_average_physics_steps, max_average_physics_steps);
  154. if (consistent_steps > 3) {
  155. ret.clamp_process_step(min_average_physics_steps * p_physics_step, max_average_physics_steps * p_physics_step);
  156. }
  157. }
  158. // second clamping: keep abs(time_deficit) < jitter_fix * frame_slise
  159. double max_clock_deviation = get_physics_jitter_fix() * p_physics_step;
  160. ret.clamp_process_step(p_process_step - max_clock_deviation, p_process_step + max_clock_deviation);
  161. // last clamping: make sure time_accum is between 0 and p_physics_step for consistency between physics and process
  162. ret.clamp_process_step(process_minus_accum, process_minus_accum + p_physics_step);
  163. // all the operations above may have turned ret.p_process_step negative or zero, keep a minimal value
  164. if (ret.process_step < min_output_step) {
  165. ret.process_step = min_output_step;
  166. }
  167. // restore time_accum
  168. time_accum = ret.process_step - process_minus_accum;
  169. // forcing ret.process_step to be positive may trigger a violation of the
  170. // promise that time_accum is between 0 and p_physics_step
  171. #ifdef DEBUG_ENABLED
  172. if (time_accum < -1E-7) {
  173. WARN_PRINT_ONCE("Intermediate value of `time_accum` is negative. This could hint at an engine bug or system timer misconfiguration.");
  174. }
  175. #endif
  176. if (time_accum > p_physics_step) {
  177. const int extra_physics_steps = floor(time_accum * p_physics_ticks_per_second);
  178. time_accum -= extra_physics_steps * p_physics_step;
  179. ret.physics_steps += extra_physics_steps;
  180. }
  181. #ifdef DEBUG_ENABLED
  182. if (time_accum < -1E-7) {
  183. 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.");
  184. }
  185. if (time_accum > p_physics_step + 1E-7) {
  186. 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.");
  187. }
  188. #endif
  189. // track deficit
  190. time_deficit = p_process_step - ret.process_step;
  191. // p_physics_step is 1.0 / iterations_per_sec
  192. // i.e. the time in seconds taken by a physics tick
  193. ret.interpolation_fraction = time_accum / p_physics_step;
  194. return ret;
  195. }
  196. // determine wall clock step since last iteration
  197. double MainTimerSync::get_cpu_process_step() {
  198. uint64_t cpu_ticks_elapsed = current_cpu_ticks_usec - last_cpu_ticks_usec;
  199. last_cpu_ticks_usec = current_cpu_ticks_usec;
  200. return cpu_ticks_elapsed / 1000000.0;
  201. }
  202. MainTimerSync::MainTimerSync() {
  203. for (int i = CONTROL_STEPS - 1; i >= 0; --i) {
  204. typical_physics_steps[i] = i;
  205. accumulated_physics_steps[i] = i;
  206. }
  207. }
  208. // start the clock
  209. void MainTimerSync::init(uint64_t p_cpu_ticks_usec) {
  210. current_cpu_ticks_usec = last_cpu_ticks_usec = p_cpu_ticks_usec;
  211. }
  212. // set measured wall clock time
  213. void MainTimerSync::set_cpu_ticks_usec(uint64_t p_cpu_ticks_usec) {
  214. current_cpu_ticks_usec = p_cpu_ticks_usec;
  215. }
  216. void MainTimerSync::set_fixed_fps(int p_fixed_fps) {
  217. fixed_fps = p_fixed_fps;
  218. }
  219. // advance one physics frame, return timesteps to take
  220. MainFrameTime MainTimerSync::advance(double p_physics_step, int p_physics_ticks_per_second) {
  221. double cpu_process_step = get_cpu_process_step();
  222. return advance_checked(p_physics_step, p_physics_ticks_per_second, cpu_process_step);
  223. }