| 1 |
{%MainUnit castletimeutils.pas} |
| 2 |
{ |
| 3 |
Copyright 2000-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Part of CastleTimeUtils unit: TFramesPerSecond. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
TFrameId = QWord; |
| 23 |
|
| 24 |
{ Measure frames per second. |
| 25 |
|
| 26 |
You should never create an instance of this class directly, |
| 27 |
instead just use the available instance in @link(TCastleContainer.Fps). |
| 28 |
The @link(TCastleContainer) instance is, in turn, automatically |
| 29 |
created for each @link(TCastleWindow) or @link(TCastleControl). } |
| 30 |
TFramesPerSecond = class |
| 31 |
private |
| 32 |
FOnlyRenderFps: TFloatTime; |
| 33 |
FRealFps: TFloatTime; |
| 34 |
FSecondsPassed: TFloatTime; |
| 35 |
DoZeroNextSecondsPassed: boolean; |
| 36 |
FUpdateStartTime: TTimerResult; |
| 37 |
LastRecalculateTime: TTimerResult; |
| 38 |
RenderStartTime: TTimerResult; |
| 39 |
{ 0 means "no frame was rendered yet" } |
| 40 |
FramesRendered: QWord; |
| 41 |
{ how much time passed inside frame rendering } |
| 42 |
OnlyRenderTimePassed: TTimerResult; |
| 43 |
FMaxSensibleSecondsPassed: TFloatTime; |
| 44 |
FWasSleeping, FSleeping: boolean; |
| 45 |
public |
| 46 |
const |
| 47 |
DefaultMaxSensibleSecondsPassed = 0.5; |
| 48 |
|
| 49 |
{ Constructor of this class is internal, you should not create instance |
| 50 |
of TFramesPerSecond class yourself. |
| 51 |
Only use the already-created instance in TCastleContainer.Fps. } |
| 52 |
constructor Create; |
| 53 |
|
| 54 |
{ @exclude } |
| 55 |
procedure InternalRenderBegin; |
| 56 |
{ @exclude } |
| 57 |
procedure InternalRenderEnd; |
| 58 |
{ @exclude } |
| 59 |
procedure InternalSleeping; |
| 60 |
{ @exclude } |
| 61 |
procedure InternalUpdateBegin; |
| 62 |
|
| 63 |
{ Rendering speed, measured in frames per second, but accounting only |
| 64 |
time spent inside "render" calls (thus ignoring time spent on |
| 65 |
physics and other logic). |
| 66 |
|
| 67 |
This measures only time spend in @link(TCastleContainer.EventRender) |
| 68 |
method (and it's subordinates, like @link(TCastleUserInterface.Render), |
| 69 |
@link(TCastleScene.LocalRender), |
| 70 |
@link(TCastleWindow.OnRender)). |
| 71 |
It does not take into account time spent on other activities, |
| 72 |
like "update" calls, and it doesn't take into account that frames are possibly |
| 73 |
not rendered all the time (when AutoRedisplay = @false). |
| 74 |
|
| 75 |
See https://castle-engine.io/manual_optimization.php#section_fps |
| 76 |
for a detailed description what FPS mean and how they should be interpreted. |
| 77 |
|
| 78 |
@seealso RealFps } |
| 79 |
property OnlyRenderFps: TFloatTime read FOnlyRenderFps; |
| 80 |
property FrameTime: TFloatTime read FOnlyRenderFps; |
| 81 |
{$ifdef FPC} deprecated 'use OnlyRenderFps'; {$endif} |
| 82 |
|
| 83 |
{ How many frames per second were actually rendered. |
| 84 |
This is the number of @link(TCastleContainer.EventRender) calls |
| 85 |
that actually happened within a real second of time. |
| 86 |
So it's an actual speed of your program. |
| 87 |
Anything can slow this down, not only long rendering, |
| 88 |
but also slow processing of other events (like "update" that does physics). |
| 89 |
|
| 90 |
When @link(TCastleWindow.AutoRedisplay) or |
| 91 |
@link(TCastleControl.AutoRedisplay) is @false, |
| 92 |
this may be very low, since we may not |
| 93 |
render the frames all the time (we may sleep for some time, |
| 94 |
or perform updates without rendering). |
| 95 |
In this case, the RealFps value may be confusing and useless |
| 96 |
(it does not reflect the speed of your application). |
| 97 |
Use the WasSleeping to detect this, and potentially hide the display |
| 98 |
of RealFps from user. |
| 99 |
|
| 100 |
See https://castle-engine.io/manual_optimization.php#section_fps |
| 101 |
for a detailed description what FPS mean and how they should be interpreted. |
| 102 |
|
| 103 |
@seealso OnlyRenderFps } |
| 104 |
property RealFps: TFloatTime read FRealFps; |
| 105 |
property RealTime: TFloatTime read FRealFps; {$ifdef FPC} deprecated 'use RealFps'; {$endif} |
| 106 |
|
| 107 |
{ Some of the frames were not rendered, because the scene and camera |
| 108 |
were not moving. This happens only when |
| 109 |
@link(TCastleWindow.AutoRedisplay) |
| 110 |
or @link(TCastleControl.AutoRedisplay) are @false, |
| 111 |
and it basically indicates that the @link(RealFps) value is not a useful |
| 112 |
indicator of your application speed. |
| 113 |
|
| 114 |
See https://castle-engine.io/manual_optimization.php#section_fps |
| 115 |
for a detailed description what this means. } |
| 116 |
property WasSleeping: boolean read FWasSleeping; |
| 117 |
|
| 118 |
{ Display current FPS (RealFps, OnlyRenderFps, taking into account WasSleeping). } |
| 119 |
function ToString: string; override; |
| 120 |
|
| 121 |
{ How much time passed since the last "update". |
| 122 |
You should use this inside "update" events and methods |
| 123 |
(@link(TCastleContainer.EventUpdate), |
| 124 |
@link(TCastleUserInterface.Update), |
| 125 |
@link(TCastleTransform.Update)...) to scale the movement. |
| 126 |
This way, your animation will work with the same speed |
| 127 |
(objects will travel at the same speed), |
| 128 |
regardless of the system performance (regardless of how often |
| 129 |
the "update" event occurs). |
| 130 |
|
| 131 |
This is calculated as a time between |
| 132 |
start of previous "update" event and start of current "update" event. } |
| 133 |
property SecondsPassed: TFloatTime read FSecondsPassed; |
| 134 |
|
| 135 |
property UpdateSecondsPassed: TFloatTime read FSecondsPassed; |
| 136 |
{$ifdef FPC} deprecated 'use SecondsPassed'; {$endif} |
| 137 |
|
| 138 |
{ Limit the SecondsPassed variable, to avoid increasing time in game |
| 139 |
a lot when a game was hanging or otherwise waiting for some exceptional |
| 140 |
event from OS. |
| 141 |
Used only when non-zero. |
| 142 |
By default it's DefaultMaxSensibleSecondsPassed. } |
| 143 |
property MaxSensibleSecondsPassed: TFloatTime |
| 144 |
read FMaxSensibleSecondsPassed write FMaxSensibleSecondsPassed; |
| 145 |
|
| 146 |
{ Forces SecondsPassed for the next "update" call to be zero. |
| 147 |
|
| 148 |
This is useful if you just came back from some modal state, |
| 149 |
like a modal dialog box (like TCastleWindow.FileDialog or modal boxes |
| 150 |
in CastleMessages -- they already call this method). |
| 151 |
SecondsPassed could be ridicoulously long in such case |
| 152 |
(if our message loop is totally paused, as in TCastleWindow.FileDialog |
| 153 |
on Windows) or not relevant (if we do our message loop, |
| 154 |
but we display something entirely different, like CastleMessages). |
| 155 |
So it's best to pretend that SecondsPassed is 0.0, |
| 156 |
so things such as TCastleSceneCore.Time do not advance wildly |
| 157 |
just because we did a modal dialog. |
| 158 |
|
| 159 |
This forces the SecondsPassed to be zero at the next update event |
| 160 |
(InternalUpdateBegin). } |
| 161 |
procedure ZeroNextSecondsPassed; |
| 162 |
|
| 163 |
{ Time of last Update call. } |
| 164 |
property UpdateStartTime: TTimerResult read FUpdateStartTime; |
| 165 |
|
| 166 |
{ Current frame identifier. |
| 167 |
|
| 168 |
Changed when each container "update" event occurs, |
| 169 |
so this is equal during all @link(TCastleUserInterface.Update), |
| 170 |
@link(TCastleUserInterface.Render), @link(TCastleTransform.Update), |
| 171 |
@link(TCastleTransform.LocalRender) occuring within the same frame. |
| 172 |
You can use this to avoid performing the same job many times |
| 173 |
in a single frame. |
| 174 |
|
| 175 |
Never zero. |
| 176 |
|
| 177 |
It's a class function, so you can access it like |
| 178 |
@code(TFramesPerSecond.FrameId), |
| 179 |
no need to have a TFramesPerSecond instance (which is usually |
| 180 |
accessed from TCastleContainer, like @link(TCastleContainer.Fps), |
| 181 |
@link(TCastleWindow.Fps), @link(TCastleControl.Fps). } |
| 182 |
class function FrameId: TFrameId; |
| 183 |
|
| 184 |
{ Frame identifier within which last render started. |
| 185 |
|
| 186 |
This is usually equal to @link(FrameId) or @link(FrameId) - 1 |
| 187 |
(depending on the order, whether Update or Render happens first, |
| 188 |
which may be platform-dependent). |
| 189 |
Though if you disabled TCastleWindow.AutoRedisplay, it may be something |
| 190 |
even smaller than @link(FrameId) - 1. |
| 191 |
|
| 192 |
Never zero. (This allows to easily treat "frame id = zero" as |
| 193 |
"frame id that never happened" in some logic.) |
| 194 |
} |
| 195 |
class function RenderFrameId: TFrameId; |
| 196 |
end; |
| 197 |
|
| 198 |
{$endif read_interface} |
| 199 |
|
| 200 |
{$ifdef read_implementation} |
| 201 |
|
| 202 |
{ TFramesPerSecond ----------------------------------------------------------- } |
| 203 |
|
| 204 |
var |
| 205 |
FFrameId: TFrameId = 1; |
| 206 |
FRenderFrameId: TFrameId = 1; |
| 207 |
|
| 208 |
constructor TFramesPerSecond.Create; |
| 209 |
const |
| 210 |
DefaultFps = 60.0; |
| 211 |
begin |
| 212 |
inherited; |
| 213 |
|
| 214 |
{ init time measurements to some sensible defaults } |
| 215 |
FOnlyRenderFps := DefaultFps; |
| 216 |
FRealFps := DefaultFps; |
| 217 |
{ for SecondsPassed this initialization is actually not essential, |
| 218 |
since we call ZeroNextSecondsPassed anyway. |
| 219 |
But in case programmer will (incorrectly!) try to use SecondsPassed |
| 220 |
before InternalUpdateBegin call, it's useful to have here some predictable value. } |
| 221 |
FSecondsPassed := 1 / DefaultFps; |
| 222 |
|
| 223 |
{ the default is non-zero now, since all Android games need it } |
| 224 |
FMaxSensibleSecondsPassed := DefaultMaxSensibleSecondsPassed; |
| 225 |
|
| 226 |
ZeroNextSecondsPassed; |
| 227 |
end; |
| 228 |
|
| 229 |
procedure TFramesPerSecond.InternalRenderBegin; |
| 230 |
begin |
| 231 |
RenderStartTime := Timer; |
| 232 |
FRenderFrameId := FFrameId; |
| 233 |
end; |
| 234 |
|
| 235 |
procedure TFramesPerSecond.InternalRenderEnd; |
| 236 |
begin |
| 237 |
Inc(FramesRendered); |
| 238 |
OnlyRenderTimePassed.Value := OnlyRenderTimePassed.Value + Timer.Value - RenderStartTime.Value; |
| 239 |
end; |
| 240 |
|
| 241 |
procedure TFramesPerSecond.InternalSleeping; |
| 242 |
begin |
| 243 |
FSleeping := true; |
| 244 |
end; |
| 245 |
|
| 246 |
function TFramesPerSecond.ToString: string; |
| 247 |
begin |
| 248 |
if (RealFps = 0) and (OnlyRenderFps = 0) then |
| 249 |
Exit('no frames rendered'); |
| 250 |
|
| 251 |
if WasSleeping then |
| 252 |
Result := 'no need to render all frames' |
| 253 |
else |
| 254 |
Result := FormatDot('%f', [RealFps]); |
| 255 |
Result := Result + FormatDot(' (only render: %f)', [OnlyRenderFps]); |
| 256 |
end; |
| 257 |
|
| 258 |
procedure TFramesPerSecond.InternalUpdateBegin; |
| 259 |
|
| 260 |
{ Update RealFps, OnlyRenderFps, WasSleeping } |
| 261 |
procedure UpdateFps; |
| 262 |
const |
| 263 |
TimeToRecalculate = 1.0; { in seconds } |
| 264 |
var |
| 265 |
NowTime: TTimerResult; |
| 266 |
begin |
| 267 |
NowTime := Timer; |
| 268 |
if TimerSeconds(NowTime, LastRecalculateTime) >= TimeToRecalculate then |
| 269 |
begin |
| 270 |
{ update FRealFps, FOnlyRenderFps once for TimeToRecalculate time. |
| 271 |
This way they don't change rapidly. |
| 272 |
|
| 273 |
Previously we used more elaborate hacks for this (resetting |
| 274 |
their times after a longer periods, but keeping some previous |
| 275 |
results), but they were complex and bad: when the game speed |
| 276 |
was changing suddenly, FRealFps, FOnlyRenderFps should also change |
| 277 |
suddenly, not gradually increase / decrease. } |
| 278 |
|
| 279 |
FRealFps := FramesRendered / TimerSeconds(NowTime, LastRecalculateTime); |
| 280 |
FWasSleeping := FSleeping; |
| 281 |
|
| 282 |
if OnlyRenderTimePassed.Value > 0 then |
| 283 |
FOnlyRenderFps := FramesRendered * TimerFrequency / OnlyRenderTimePassed.Value |
| 284 |
else |
| 285 |
FOnlyRenderFps := 0; |
| 286 |
|
| 287 |
LastRecalculateTime := NowTime; |
| 288 |
FramesRendered := 0; |
| 289 |
OnlyRenderTimePassed.Value := 0; |
| 290 |
FSleeping := false; |
| 291 |
end; |
| 292 |
end; |
| 293 |
|
| 294 |
var |
| 295 |
NewUpdateStartTime: TTimerResult; |
| 296 |
begin |
| 297 |
{ update FSecondsPassed, DoZeroNextSecondsPassed, FUpdateStartTime } |
| 298 |
NewUpdateStartTime := Timer; |
| 299 |
|
| 300 |
if DoZeroNextSecondsPassed then |
| 301 |
begin |
| 302 |
FSecondsPassed := 0.0; |
| 303 |
DoZeroNextSecondsPassed := false; |
| 304 |
end else |
| 305 |
begin |
| 306 |
FSecondsPassed := TimerSeconds(NewUpdateStartTime, FUpdateStartTime); |
| 307 |
if MaxSensibleSecondsPassed > 0 then |
| 308 |
FSecondsPassed := Min(FSecondsPassed, MaxSensibleSecondsPassed); |
| 309 |
end; |
| 310 |
|
| 311 |
FUpdateStartTime := NewUpdateStartTime; |
| 312 |
|
| 313 |
Inc(FFrameId); |
| 314 |
|
| 315 |
UpdateFps; |
| 316 |
end; |
| 317 |
|
| 318 |
procedure TFramesPerSecond.ZeroNextSecondsPassed; |
| 319 |
begin |
| 320 |
DoZeroNextSecondsPassed := true; |
| 321 |
end; |
| 322 |
|
| 323 |
class function TFramesPerSecond.FrameId: TFrameId; |
| 324 |
begin |
| 325 |
Result := FFrameId; |
| 326 |
end; |
| 327 |
|
| 328 |
class function TFramesPerSecond.RenderFrameId: TFrameId; |
| 329 |
begin |
| 330 |
Result := FRenderFrameId; |
| 331 |
end; |
| 332 |
|
| 333 |
{$endif read_implementation} |