/**************************************************************************/1/* main_timer_sync.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/config/engine.h"3334// Uncomment this define to get more debugging logs for the delta smoothing.35// #define GODOT_DEBUG_DELTA_SMOOTHER3637struct MainFrameTime {38double process_step; // delta time to advance during process()39int physics_steps; // number of times to iterate the physics engine40double interpolation_fraction; // fraction through the current physics tick4142void clamp_process_step(double min_process_step, double max_process_step);43};4445class MainTimerSync {46class DeltaSmoother {47public:48// pass the recorded delta, returns a smoothed delta49int64_t smooth_delta(int64_t p_delta);5051private:52void update_refresh_rate_estimator(int64_t p_delta);53bool fps_allows_smoothing(int64_t p_delta);5455// estimated vsync delta (monitor refresh rate)56int64_t _vsync_delta = 16666;5758// keep track of accumulated time so we know how many vsyncs to advance by59int64_t _leftover_time = 0;6061// keep a rough measurement of the FPS as we run.62// If this drifts a long way below or above the refresh rate, the machine63// is struggling to keep up, and we can switch off smoothing. This64// also deals with the case that the user has overridden the vsync in the GPU settings,65// in which case we don't want to try smoothing.66static const int MEASURE_FPS_OVER_NUM_FRAMES = 64;6768int64_t _measurement_time = 0;69int64_t _measurement_frame_count = 0;70int64_t _measurement_end_frame = MEASURE_FPS_OVER_NUM_FRAMES;71int64_t _measurement_start_time = 0;72bool _measurement_allows_smoothing = true;7374// we can estimate the fps by growing it on condition75// that a large proportion of frames are higher than the current estimate.76int32_t _estimated_fps = 0;77int32_t _hits_at_estimated = 0;78int32_t _hits_above_estimated = 0;79int32_t _hits_below_estimated = 0;80int32_t _hits_one_above_estimated = 0;81int32_t _hits_one_below_estimated = 0;82bool _estimate_complete = false;83bool _estimate_locked = false;8485// data for averaging the delta over a second or so86// to prevent spurious values87int64_t _estimator_total_delta = 0;88int32_t _estimator_delta_readings = 0;8990void made_new_estimate() {91_hits_above_estimated = 0;92_hits_at_estimated = 0;93_hits_below_estimated = 0;94_hits_one_above_estimated = 0;95_hits_one_below_estimated = 0;9697_estimate_complete = false;9899#ifdef GODOT_DEBUG_DELTA_SMOOTHER100print_line("estimated fps " + itos(_estimated_fps));101#endif102}103104} _delta_smoother;105106// wall clock time measured on the main thread107uint64_t last_cpu_ticks_usec = 0;108uint64_t current_cpu_ticks_usec = 0;109110// logical game time since last physics timestep111double time_accum = 0;112113// current difference between wall clock time and reported sum of process_steps114double time_deficit = 0;115116// number of frames back for keeping accumulated physics steps roughly constant.117// value of 12 chosen because that is what is required to make 144 Hz monitors118// behave well with 60 Hz physics updates. The only worse commonly available refresh119// would be 85, requiring CONTROL_STEPS = 17.120static const int CONTROL_STEPS = 12;121122// sum of physics steps done over the last (i+1) frames123int accumulated_physics_steps[CONTROL_STEPS];124125// typical value for accumulated_physics_steps[i] is either this or this plus one126int typical_physics_steps[CONTROL_STEPS];127128int fixed_fps = 0;129130protected:131// returns the fraction of p_physics_step required for the timer to overshoot132// before advance_core considers changing the physics_steps return from133// the typical values as defined by typical_physics_steps134double get_physics_jitter_fix();135136// gets our best bet for the average number of physics steps per render frame137// return value: number of frames back this data is consistent138int get_average_physics_steps(double &p_min, double &p_max);139140// advance physics clock by p_process_step, return appropriate number of steps to simulate141MainFrameTime advance_core(double p_physics_step, int p_physics_ticks_per_second, double p_process_step);142143// calls advance_core, keeps track of deficit it adds to animaption_step, make sure the deficit sum stays close to zero144MainFrameTime advance_checked(double p_physics_step, int p_physics_ticks_per_second, double p_process_step);145146// determine wall clock step since last iteration147double get_cpu_process_step();148149public:150MainTimerSync();151152// start the clock153void init(uint64_t p_cpu_ticks_usec);154// set measured wall clock time155void set_cpu_ticks_usec(uint64_t p_cpu_ticks_usec);156//set fixed fps157void set_fixed_fps(int p_fixed_fps);158159// advance one frame, return timesteps to take160MainFrameTime advance(double p_physics_step, int p_physics_ticks_per_second);161};162163164