Debian Codegraph @

main/main_timer_sync.h

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