EN
games/rob-revisited / src / Revisited / Render / AdaptiveQuality.php · commit f9a1307
<?phpnamespace App\Revisited\Render;/** * Measures the real frame rate and picks the quality level (option "Qualité : Auto"). Fed with the wall-clock time * between two frames; the scene pauses it while nothing representative is drawn (the loading card, a hidden tab, * the menus). Pure logic, no engine: tests feed it frame times. * *   $a = new AdaptiveQuality('medium', auto: true); *   if (($new = $a->frame($seconds)) !== null) { ... switch to $new ... } * * - the rate is the frames over the last WINDOW seconds (hitches > 0.5 s are not frames: a pause, a hidden tab); * - under SLOW_FPS for SLOW_FOR seconds: one level down (high → medium → low → ultra); * - over FAST_FPS for FAST_FOR seconds: one level up, but never back to a level that was already too slow; * - in ultra and still under HOPELESS_FPS for SLOW_FOR seconds: $hopeless (the scene tells the player why); * - auto off (a level forced by the player): it only measures. */final class AdaptiveQuality{    public const SLOW_FPS = 40.0;    public const FAST_FPS = 58.0;    public const HOPELESS_FPS = 20.0;    public const SLOW_FOR = 4.0;    public const FAST_FOR = 20.0;    public const WINDOW = 2.0;    /** seconds ignored after a start, a pause or a change (shaders compiling, meshes uploading) */    public const SETTLE = 1.5;    /** frames per second over the last WINDOW seconds, 0: not measured yet */    public float $fps = 0.0;    /** ultra and still too slow: the computer cannot run the game well (software rendering?) */    public bool $hopeless = false;    /** @var list<float> */    private array $samples = [];    private float $sum = 0.0;    private float $settle = self::SETTLE;    private float $slowFor = 0.0;    private float $fastFor = 0.0;    private float $hopelessFor = 0.0;    /** @var array<string, int> levels left because they were too slow */    private array $failed = [];    public function __construct(public string $level, public bool $auto = true)    {        if (!Quality::valid($level)) {            $this->level = 'medium';        }    }    /** Ignores the next $seconds of frames and forgets the current measure (the timers start again). */    public function pause(float $seconds = self::SETTLE): void    {        $this->samples = [];        $this->sum = 0.0;        $this->settle = max($this->settle, $seconds);        $this->slowFor = $this->fastFor = $this->hopelessFor = 0.0;    }    /**     * One frame took $dt seconds of wall clock.     * @return string|null the new level when it changed     */    public function frame(float $dt): ?string    {        if ($dt <= 0.0) {            return null;        }        if ($dt > 0.5) {            $this->pause(0.5);            return null;        }        if ($this->settle > 0.0) {            $this->settle -= $dt;            return null;        }        $this->samples[] = $dt;        $this->sum += $dt;        while ($this->sum > self::WINDOW && count($this->samples) > 1) {            $this->sum -= array_shift($this->samples);        }        if ($this->sum < self::WINDOW * 0.5) {            return null;        }        $this->fps = count($this->samples) / $this->sum;        $this->slowFor = $this->fps < self::SLOW_FPS ? $this->slowFor + $dt : 0.0;        $this->fastFor = $this->fps > self::FAST_FPS ? $this->fastFor + $dt : 0.0;        $this->hopelessFor = $this->level === 'ultra' && $this->fps < self::HOPELESS_FPS ? $this->hopelessFor + $dt : 0.0;        if ($this->hopelessFor >= self::SLOW_FOR) {            $this->hopeless = true;        }        if (!$this->auto) {            return null;        }        if ($this->slowFor >= self::SLOW_FOR && ($down = Quality::lower($this->level)) !== null) {            $this->failed[$this->level] = ($this->failed[$this->level] ?? 0) + 1;            return $this->change($down);        }        if ($this->fastFor >= self::FAST_FOR && ($up = Quality::higher($this->level)) !== null && ($this->failed[$up] ?? 0) === 0) {            return $this->change($up);        }        return null;    }    /** The player forced a level (auto off) or chose Auto again from $level. */    public function set(string $level, bool $auto): void    {        $this->level = Quality::valid($level) ? $level : $this->level;        $this->auto = $auto;        $this->failed = [];        $this->hopeless = false;        $this->pause();    }    private function change(string $level): string    {        $this->level = $level;        $this->pause(2.0);        return $level;    }}