main_timer_sync.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**************************************************************************/
  2. /* main_timer_sync.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef MAIN_TIMER_SYNC_H
  31. #define MAIN_TIMER_SYNC_H
  32. #include "core/config/engine.h"
  33. // Uncomment this define to get more debugging logs for the delta smoothing.
  34. // #define GODOT_DEBUG_DELTA_SMOOTHER
  35. struct MainFrameTime {
  36. double process_step; // delta time to advance during process()
  37. int physics_steps; // number of times to iterate the physics engine
  38. double interpolation_fraction; // fraction through the current physics tick
  39. void clamp_process_step(double min_process_step, double max_process_step);
  40. };
  41. class MainTimerSync {
  42. class DeltaSmoother {
  43. public:
  44. // pass the recorded delta, returns a smoothed delta
  45. int64_t smooth_delta(int64_t p_delta);
  46. private:
  47. void update_refresh_rate_estimator(int64_t p_delta);
  48. bool fps_allows_smoothing(int64_t p_delta);
  49. // estimated vsync delta (monitor refresh rate)
  50. int64_t _vsync_delta = 16666;
  51. // keep track of accumulated time so we know how many vsyncs to advance by
  52. int64_t _leftover_time = 0;
  53. // keep a rough measurement of the FPS as we run.
  54. // If this drifts a long way below or above the refresh rate, the machine
  55. // is struggling to keep up, and we can switch off smoothing. This
  56. // also deals with the case that the user has overridden the vsync in the GPU settings,
  57. // in which case we don't want to try smoothing.
  58. static const int MEASURE_FPS_OVER_NUM_FRAMES = 64;
  59. int64_t _measurement_time = 0;
  60. int64_t _measurement_frame_count = 0;
  61. int64_t _measurement_end_frame = MEASURE_FPS_OVER_NUM_FRAMES;
  62. int64_t _measurement_start_time = 0;
  63. bool _measurement_allows_smoothing = true;
  64. // we can estimate the fps by growing it on condition
  65. // that a large proportion of frames are higher than the current estimate.
  66. int32_t _estimated_fps = 0;
  67. int32_t _hits_at_estimated = 0;
  68. int32_t _hits_above_estimated = 0;
  69. int32_t _hits_below_estimated = 0;
  70. int32_t _hits_one_above_estimated = 0;
  71. int32_t _hits_one_below_estimated = 0;
  72. bool _estimate_complete = false;
  73. bool _estimate_locked = false;
  74. // data for averaging the delta over a second or so
  75. // to prevent spurious values
  76. int64_t _estimator_total_delta = 0;
  77. int32_t _estimator_delta_readings = 0;
  78. void made_new_estimate() {
  79. _hits_above_estimated = 0;
  80. _hits_at_estimated = 0;
  81. _hits_below_estimated = 0;
  82. _hits_one_above_estimated = 0;
  83. _hits_one_below_estimated = 0;
  84. _estimate_complete = false;
  85. #ifdef GODOT_DEBUG_DELTA_SMOOTHER
  86. print_line("estimated fps " + itos(_estimated_fps));
  87. #endif
  88. }
  89. } _delta_smoother;
  90. // wall clock time measured on the main thread
  91. uint64_t last_cpu_ticks_usec = 0;
  92. uint64_t current_cpu_ticks_usec = 0;
  93. // logical game time since last physics timestep
  94. double time_accum = 0;
  95. // current difference between wall clock time and reported sum of process_steps
  96. double time_deficit = 0;
  97. // number of frames back for keeping accumulated physics steps roughly constant.
  98. // value of 12 chosen because that is what is required to make 144 Hz monitors
  99. // behave well with 60 Hz physics updates. The only worse commonly available refresh
  100. // would be 85, requiring CONTROL_STEPS = 17.
  101. static const int CONTROL_STEPS = 12;
  102. // sum of physics steps done over the last (i+1) frames
  103. int accumulated_physics_steps[CONTROL_STEPS];
  104. // typical value for accumulated_physics_steps[i] is either this or this plus one
  105. int typical_physics_steps[CONTROL_STEPS];
  106. int fixed_fps = 0;
  107. protected:
  108. // returns the fraction of p_physics_step required for the timer to overshoot
  109. // before advance_core considers changing the physics_steps return from
  110. // the typical values as defined by typical_physics_steps
  111. double get_physics_jitter_fix();
  112. // gets our best bet for the average number of physics steps per render frame
  113. // return value: number of frames back this data is consistent
  114. int get_average_physics_steps(double &p_min, double &p_max);
  115. // advance physics clock by p_process_step, return appropriate number of steps to simulate
  116. MainFrameTime advance_core(double p_physics_step, int p_physics_ticks_per_second, double p_process_step);
  117. // calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero
  118. MainFrameTime advance_checked(double p_physics_step, int p_physics_ticks_per_second, double p_process_step);
  119. // determine wall clock step since last iteration
  120. double get_cpu_process_step();
  121. public:
  122. MainTimerSync();
  123. // start the clock
  124. void init(uint64_t p_cpu_ticks_usec);
  125. // set measured wall clock time
  126. void set_cpu_ticks_usec(uint64_t p_cpu_ticks_usec);
  127. //set fixed fps
  128. void set_fixed_fps(int p_fixed_fps);
  129. // advance one frame, return timesteps to take
  130. MainFrameTime advance(double p_physics_step, int p_physics_ticks_per_second);
  131. };
  132. #endif // MAIN_TIMER_SYNC_H