euv_core/game/clock/
impl.rs1use super::*;
2
3impl Default for FrameClock {
5 fn default() -> Self {
11 Self::from_fixed_step_ns(CLOCK_DEFAULT_FIXED_STEP_NS, CLOCK_DEFAULT_MAX_SUBSTEPS)
12 }
13}
14
15impl FrameClock {
17 #[must_use]
28 pub fn new(fixed_dt: f32, max_substeps: u32) -> Self {
29 let safe_dt: f32 = if fixed_dt.is_finite() {
30 fixed_dt.max(0.000_1)
31 } else {
32 1.0 / 60.0
33 };
34 let step_ns: u64 = ((safe_dt as f64) * CLOCK_NS_PER_SEC_F64)
35 .round()
36 .max(CLOCK_MIN_FIXED_STEP_NS as f64) as u64;
37 Self::from_fixed_step_ns(step_ns, max_substeps)
38 }
39
40 #[must_use]
51 pub fn from_fixed_step_ns(fixed_step_ns: u64, max_substeps: u32) -> Self {
52 let safe_ns: u64 = fixed_step_ns.max(CLOCK_MIN_FIXED_STEP_NS);
53 Self {
54 fixed_dt: ((safe_ns as f64) / CLOCK_NS_PER_SEC_F64) as f32,
55 fixed_step_ns: safe_ns,
56 max_substeps: max_substeps.max(1),
57 accumulator_ns: 0,
58 accumulator: 0.0,
59 delta_ns: 0,
60 delta_secs: 0.0,
61 elapsed_ns: 0,
62 elapsed_secs: 0.0,
63 alpha: 0.0,
64 fps: 0.0,
65 frame_count: 0,
66 fixed_step_count: 0,
67 }
68 }
69
70 #[must_use]
81 pub fn for_sim_hz(sim_hz: u32, max_substeps: u32) -> Self {
82 let clamped_hz: u64 = (sim_hz.clamp(1, 1000)) as u64;
83 let step_ns: u64 = (CLOCK_NS_PER_SEC + clamped_hz / 2) / clamped_hz;
84 Self::from_fixed_step_ns(step_ns, max_substeps)
85 }
86
87 #[must_use]
97 pub fn hysteresis_window_ns(&self) -> u64 {
98 let safe_fixed_ns: u64 = self.get_fixed_step_ns().max(CLOCK_MIN_FIXED_STEP_NS);
99 (safe_fixed_ns / CLOCK_HYSTERESIS_DIVISOR).max(1)
100 }
101
102 pub fn tick_ns(&mut self, raw_dt_ns: u64) -> u32 {
113 let mut safe_fixed_ns: u64 = self.get_fixed_step_ns().max(CLOCK_MIN_FIXED_STEP_NS);
114 let expected_dt_f32: f32 = ((safe_fixed_ns as f64) / CLOCK_NS_PER_SEC_F64) as f32;
115 if (self.get_fixed_dt() - expected_dt_f32).abs() > 1e-5 && self.get_fixed_dt().is_finite() {
116 let safe_dt: f32 = self.get_fixed_dt().max(0.000_1);
117 safe_fixed_ns = ((safe_dt as f64) * CLOCK_NS_PER_SEC_F64)
118 .round()
119 .max(CLOCK_MIN_FIXED_STEP_NS as f64) as u64;
120 self.set_fixed_step_ns(safe_fixed_ns);
121 }
122 let dt_ns: u64 = raw_dt_ns.min(CLOCK_MAX_FRAME_DT_NS);
123 let dt_secs: f32 = ((dt_ns as f64) / CLOCK_NS_PER_SEC_F64) as f32;
124 self.set_delta_ns(dt_ns);
125 self.set_delta_secs(dt_secs);
126 let next_elapsed_ns: u64 = self.get_elapsed_ns().saturating_add(dt_ns);
127 self.set_elapsed_ns(next_elapsed_ns);
128 self.set_elapsed_secs((next_elapsed_ns as f64) / CLOCK_NS_PER_SEC_F64);
129 self.set_frame_count(self.get_frame_count().saturating_add(1));
130 if dt_ns > 0 {
131 let inst_fps: f32 = (CLOCK_NS_PER_SEC_F64 / (dt_ns as f64)) as f32;
132 let prev_fps: f32 = self.get_fps();
133 let smoothed: f32 = if prev_fps <= 0.0 {
134 inst_fps
135 } else {
136 let ema_alpha: f32 =
137 (1.0 - (-dt_secs / CLOCK_FPS_EMA_TAU_SECS).exp()).clamp(0.0, 1.0);
138 prev_fps + (inst_fps - prev_fps) * ema_alpha
139 };
140 self.set_fps(smoothed);
141 }
142 let snap_window_ns: u64 = (safe_fixed_ns / CLOCK_HYSTERESIS_DIVISOR).max(1);
143 let mut acc_ns: u64 = self.get_accumulator_ns().saturating_add(dt_ns);
144 let rem_before: u64 = acc_ns % safe_fixed_ns;
145 if rem_before > 0 && safe_fixed_ns - rem_before <= snap_window_ns {
146 acc_ns = acc_ns.saturating_add(safe_fixed_ns - rem_before);
147 }
148 let max_steps: u32 = self.get_max_substeps().max(1);
149 let mut steps: u32 = 0;
150 while acc_ns >= safe_fixed_ns && steps < max_steps {
151 acc_ns -= safe_fixed_ns;
152 steps += 1;
153 }
154 if (steps == max_steps && acc_ns >= safe_fixed_ns)
155 || (steps > 0 && acc_ns <= snap_window_ns)
156 {
157 acc_ns = 0;
158 }
159 self.set_accumulator_ns(acc_ns);
160 self.set_accumulator(((acc_ns as f64) / CLOCK_NS_PER_SEC_F64) as f32);
161 self.set_alpha(((acc_ns as f64) / (safe_fixed_ns as f64)).clamp(0.0, 1.0) as f32);
162 self.set_fixed_step_count(self.get_fixed_step_count().saturating_add(steps as u64));
163 steps
164 }
165
166 pub fn tick(&mut self, raw_dt_secs: f32) -> u32 {
177 let dt_ns: u64 = if raw_dt_secs.is_finite() && raw_dt_secs > 0.0 {
178 ((raw_dt_secs.clamp(0.0, 0.25) as f64) * CLOCK_NS_PER_SEC_F64).round() as u64
179 } else {
180 0
181 };
182 self.tick_ns(dt_ns)
183 }
184}
185
186impl Default for FixedTimestep3DSession {
188 fn default() -> Self {
194 Self::new(60, CLOCK_DEFAULT_MAX_SUBSTEPS, 60.0)
195 }
196}
197
198impl FixedTimestep3DSession {
200 #[must_use]
212 pub fn new(sim_hz: u32, max_substeps: u32, present_hz: f32) -> Self {
213 let safe_present_hz: f32 = if present_hz.is_finite() {
214 present_hz.clamp(1.0, 1000.0)
215 } else {
216 60.0
217 };
218 Self {
219 clock: FrameClock::for_sim_hz(sim_hz, max_substeps),
220 present_hz: safe_present_hz,
221 sim_steps_total: 0,
222 present_frames_total: 0,
223 interpolated_only_frames: 0,
224 clamped_stall_frames: 0,
225 last_alpha: 0.0,
226 last_steps: 0,
227 }
228 }
229
230 pub fn set_present_cadence_hz(&mut self, present_hz: f32) {
237 if present_hz.is_finite() {
238 self.set_present_hz(present_hz.clamp(1.0, 1000.0));
239 }
240 }
241
242 pub fn advance_present_ns<F>(&mut self, present_dt_ns: u64, mut step_sim: F) -> SimPresentFrame
254 where
255 F: FnMut(f32),
256 {
257 let fixed_ns: u64 = self
258 .get_clock()
259 .get_fixed_step_ns()
260 .max(CLOCK_MIN_FIXED_STEP_NS);
261 let max_substeps: u32 = self.get_clock().get_max_substeps().max(1);
262 let pre_acc_ns: u64 = self.get_clock().get_accumulator_ns();
263 let clamped_dt_ns: u64 = present_dt_ns.min(CLOCK_MAX_FRAME_DT_NS);
264 let uncapped_steps: u64 = pre_acc_ns.saturating_add(clamped_dt_ns) / fixed_ns;
265 let hit_clamp: bool =
266 present_dt_ns > CLOCK_MAX_FRAME_DT_NS || uncapped_steps > (max_substeps as u64);
267 let steps: u32 = self.get_mut_clock().tick_ns(present_dt_ns);
268 let fixed_dt: f32 = self.get_clock().get_fixed_dt();
269 let alpha: f32 = self.get_clock().get_alpha();
270 let present_dt_secs: f32 = self.get_clock().get_delta_secs();
271 for _ in 0..steps {
272 step_sim(fixed_dt);
273 }
274 let interpolated_only: bool = steps == 0;
275 self.set_sim_steps_total(self.get_sim_steps_total().saturating_add(steps as u64));
276 self.set_present_frames_total(self.get_present_frames_total().saturating_add(1));
277 if interpolated_only {
278 self.set_interpolated_only_frames(
279 self.get_interpolated_only_frames().saturating_add(1),
280 );
281 }
282 if hit_clamp {
283 self.set_clamped_stall_frames(self.get_clamped_stall_frames().saturating_add(1));
284 }
285 self.set_last_alpha(alpha);
286 self.set_last_steps(steps);
287 SimPresentFrame::new(
288 steps,
289 alpha,
290 fixed_dt,
291 present_dt_secs,
292 interpolated_only,
293 hit_clamp,
294 )
295 }
296
297 pub fn advance_present<F>(&mut self, present_dt_secs: f32, step_sim: F) -> SimPresentFrame
309 where
310 F: FnMut(f32),
311 {
312 let dt_ns: u64 = if present_dt_secs.is_finite() && present_dt_secs > 0.0 {
313 ((present_dt_secs.min(1.0) as f64) * CLOCK_NS_PER_SEC_F64).round() as u64
314 } else {
315 0
316 };
317 self.advance_present_ns(dt_ns, step_sim)
318 }
319
320 #[must_use]
332 pub fn interpolate_scalar(prev: f32, next: f32, alpha: f32) -> f32 {
333 let t: f32 = if alpha.is_finite() {
334 alpha.clamp(0.0, 1.0)
335 } else {
336 1.0
337 };
338 prev + (next - prev) * t
339 }
340}