Debian Codegraph @

win32/plugins/dfxvideo/winsrc/fps.c

raw
1 /***************************************************************************
2 fps.c - description
3 -------------------
4 begin : Sun Oct 28 2001
5 copyright : (C) 2001 by Pete Bernert
6 email : [email protected]
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. See also the license.txt file for *
15 * additional informations. *
16 * *
17 ***************************************************************************/
18
19 #define _IN_FPS
20
21 #include "externals.h"
22 #include "fps.h"
23 #include "gpu.h"
24
25 ////////////////////////////////////////////////////////////////////////
26 // FPS stuff
27 ////////////////////////////////////////////////////////////////////////
28
29 LARGE_INTEGER CPUFrequency, PerformanceCounter;
30
31 float fFrameRateHz=0;
32 DWORD dwFrameRateTicks=16;
33 float fFrameRate;
34 int iFrameLimit;
35 int UseFrameLimit=0;
36 int UseFrameSkip=0;
37
38 ////////////////////////////////////////////////////////////////////////
39 // FPS skipping / limit
40 ////////////////////////////////////////////////////////////////////////
41
42 BOOL bInitCap = TRUE;
43 float fps_skip = 0;
44 float fps_cur = 0;
45
46 ////////////////////////////////////////////////////////////////////////
47
48 #define MAXLACE 16
49
50 void CheckFrameRate(void)
51 {
52 if(UseFrameSkip) // skipping mode?
53 {
54 if(!(dwActFixes&0x80)) // not old skipping mode?
55 {
56 dwLaceCnt++; // -> store cnt of vsync between frames
57 if(dwLaceCnt>=MAXLACE && UseFrameLimit) // -> if there are many laces without screen toggling,
58 { // do std frame limitation
59 if(dwLaceCnt==MAXLACE) bInitCap=TRUE;
60 FrameCap();
61 }
62 }
63 else if(UseFrameLimit) FrameCap();
64 calcfps(); // -> calc fps display in skipping mode
65 }
66 else // non-skipping mode:
67 {
68 if(UseFrameLimit) FrameCap(); // -> do it
69 if(ulKeybits&KEY_SHOWFPS) calcfps(); // -> and calc fps display
70 }
71 }
72
73 ////////////////////////////////////////////////////////////////////////
74 // WIN VERSION
75 ////////////////////////////////////////////////////////////////////////
76
77 BOOL IsPerformanceCounter = FALSE;
78
79 void FrameCap (void) // frame limit func
80 {
81 static DWORD curticks, lastticks, _ticks_since_last_update;
82 static DWORD TicksToWait = 0;
83 static LARGE_INTEGER CurrentTime;
84 static LARGE_INTEGER LastTime;
85 static BOOL SkipNextWait = FALSE;
86 BOOL Waiting = TRUE;
87
88 //---------------------------------------------------------
89 if(bInitCap)
90 {
91 bInitCap=FALSE;
92 if (IsPerformanceCounter)
93 QueryPerformanceCounter(&LastTime);
94 lastticks = timeGetTime();
95 TicksToWait=0;
96 return;
97 }
98 //---------------------------------------------------------
99
100 if (IsPerformanceCounter)
101 {
102 QueryPerformanceCounter(&CurrentTime);
103 _ticks_since_last_update = CurrentTime.LowPart - LastTime.LowPart;
104
105 //---------------------------------------------------------
106 curticks = timeGetTime();
107 if(_ticks_since_last_update>(CPUFrequency.LowPart>>1))
108 {
109 if(curticks < lastticks)
110 _ticks_since_last_update = dwFrameRateTicks+TicksToWait+1;
111 else _ticks_since_last_update = (CPUFrequency.LowPart * (curticks - lastticks))/1000;
112 }
113 //---------------------------------------------------------
114
115 if ((_ticks_since_last_update > TicksToWait) ||
116 (CurrentTime.LowPart < LastTime.LowPart))
117 {
118 LastTime.HighPart = CurrentTime.HighPart;
119 LastTime.LowPart = CurrentTime.LowPart;
120
121 lastticks=curticks;
122
123 if((_ticks_since_last_update-TicksToWait) > dwFrameRateTicks)
124 TicksToWait=0;
125 else TicksToWait=dwFrameRateTicks-(_ticks_since_last_update-TicksToWait);
126 }
127 else
128 {
129 while (Waiting)
130 {
131 QueryPerformanceCounter(&CurrentTime);
132 _ticks_since_last_update = CurrentTime.LowPart - LastTime.LowPart;
133
134 //---------------------------------------------------------
135 curticks = timeGetTime();
136 if(_ticks_since_last_update>(CPUFrequency.LowPart>>1))
137 {
138 if(curticks < lastticks)
139 _ticks_since_last_update = TicksToWait+1;
140 else _ticks_since_last_update = (CPUFrequency.LowPart * (curticks - lastticks))/1000;
141 }
142 //---------------------------------------------------------
143
144 if ((_ticks_since_last_update > TicksToWait) ||
145 (CurrentTime.LowPart < LastTime.LowPart))
146 {
147 Waiting = FALSE;
148
149 lastticks=curticks;
150
151 LastTime.HighPart = CurrentTime.HighPart;
152 LastTime.LowPart = CurrentTime.LowPart;
153 TicksToWait = dwFrameRateTicks;
154 }
155 }
156 }
157 }
158 else
159 {
160 curticks = timeGetTime();
161 _ticks_since_last_update = curticks - lastticks;
162
163 if ((_ticks_since_last_update > TicksToWait) ||
164 (curticks < lastticks))
165 {
166 lastticks = curticks;
167
168 if((_ticks_since_last_update-TicksToWait) > dwFrameRateTicks)
169 TicksToWait=0;
170 else TicksToWait=dwFrameRateTicks-(_ticks_since_last_update-TicksToWait);
171 }
172 else
173 {
174 while (Waiting)
175 {
176 curticks = timeGetTime();
177 _ticks_since_last_update = curticks - lastticks;
178 if ((_ticks_since_last_update > TicksToWait) ||
179 (curticks < lastticks))
180 {
181 Waiting = FALSE;
182 lastticks = curticks;
183 TicksToWait = dwFrameRateTicks;
184 }
185 }
186 }
187 }
188 }
189
190 ////////////////////////////////////////////////////////////////////////
191
192 #define MAXSKIP 120
193
194 void FrameSkip(void)
195 {
196 static int iNumSkips=0,iAdditionalSkip=0; // number of additional frames to skip
197 static DWORD dwLastLace=0; // helper var for frame limitation
198 static DWORD curticks, lastticks, _ticks_since_last_update;
199 static LARGE_INTEGER CurrentTime;
200 static LARGE_INTEGER LastTime;
201
202 if(!dwLaceCnt) return; // important: if no updatelace happened, we ignore it completely
203
204 if(iNumSkips) // we are in pure skipping mode?
205 {
206 dwLastLace+=dwLaceCnt; // -> calc frame limit helper (number of laces)
207 bSkipNextFrame = TRUE; // -> we skip next frame as well
208 iNumSkips--; // -> ok, one done
209 }
210 else // ok, no additional skipping has to be done...
211 { // we check now, if some limitation is needed, or a new skipping has to get started
212 DWORD dwWaitTime;
213
214 if(bInitCap || bSkipNextFrame) // first time or we skipped before?
215 {
216 if(UseFrameLimit && !bInitCap) // frame limit wanted and not first time called?
217 {
218 DWORD dwT=_ticks_since_last_update; // -> that's the time of the last drawn frame
219 dwLastLace+=dwLaceCnt; // -> and that's the number of updatelace since the start of the last drawn frame
220
221 if (IsPerformanceCounter) // -> now we calc the time of the last drawn frame + the time we spent skipping
222 {
223 QueryPerformanceCounter(&CurrentTime);
224 _ticks_since_last_update= dwT+CurrentTime.LowPart - LastTime.LowPart;
225 }
226 else
227 {
228 curticks = timeGetTime();
229 _ticks_since_last_update= dwT+curticks - lastticks;
230 }
231
232 dwWaitTime=dwLastLace*dwFrameRateTicks; // -> and now we calc the time the real psx would have needed
233
234 if(_ticks_since_last_update<dwWaitTime) // -> we were too fast?
235 {
236 if((dwWaitTime-_ticks_since_last_update)> // -> some more security, to prevent
237 (60*dwFrameRateTicks)) // wrong waiting times
238 _ticks_since_last_update=dwWaitTime;
239
240 while(_ticks_since_last_update<dwWaitTime) // -> loop until we have reached the real psx time
241 { // (that's the additional limitation, yup)
242 if (IsPerformanceCounter)
243 {
244 QueryPerformanceCounter(&CurrentTime);
245 _ticks_since_last_update = dwT+CurrentTime.LowPart - LastTime.LowPart;
246 }
247 else
248 {
249 curticks = timeGetTime();
250 _ticks_since_last_update = dwT+curticks - lastticks;
251 }
252 }
253 }
254 else // we were still too slow ?!!?
255 {
256 if(iAdditionalSkip<MAXSKIP) // -> well, somewhen we really have to stop skipping on very slow systems
257 {
258 iAdditionalSkip++; // -> inc our watchdog var
259 dwLaceCnt=0; // -> reset lace count
260 if (IsPerformanceCounter) // -> ok, start time of the next frame
261 QueryPerformanceCounter(&LastTime);
262 lastticks = timeGetTime();
263 return; // -> done, we will skip next frame to get more speed (SkipNextFrame still TRUE)
264 }
265 }
266 }
267
268 bInitCap=FALSE; // -> ok, we have inited the frameskip func
269 iAdditionalSkip=0; // -> init additional skip
270 bSkipNextFrame=FALSE; // -> we don't skip the next frame
271 if (IsPerformanceCounter) // -> we store the start time of the next frame
272 QueryPerformanceCounter(&LastTime);
273 lastticks = timeGetTime();
274 dwLaceCnt=0; // -> and we start to count the laces
275 dwLastLace=0;
276 _ticks_since_last_update=0;
277 return; // -> done, the next frame will get drawn
278 }
279
280 bSkipNextFrame=FALSE; // init the frame skip signal to 'no skipping' first
281
282 if (IsPerformanceCounter) // get the current time (we are now at the end of one drawn frame)
283 {
284 QueryPerformanceCounter(&CurrentTime);
285 _ticks_since_last_update = CurrentTime.LowPart - LastTime.LowPart;
286 }
287 else
288 {
289 curticks = timeGetTime();
290 _ticks_since_last_update = curticks - lastticks;
291 }
292
293 dwLastLace=dwLaceCnt; // store curr count (frame limitation helper)
294 dwWaitTime=dwLaceCnt*dwFrameRateTicks; // calc the 'real psx lace time'
295
296 if(_ticks_since_last_update>dwWaitTime) // hey, we needed way too long for that frame...
297 {
298 if(UseFrameLimit) // if limitation, we skip just next frame,
299 { // and decide after, if we need to do more
300 iNumSkips=0;
301 }
302 else
303 {
304 iNumSkips=_ticks_since_last_update/dwWaitTime; // -> calc number of frames to skip to catch up
305 iNumSkips--; // -> since we already skip next frame, one down
306 if(iNumSkips>MAXSKIP) iNumSkips=MAXSKIP; // -> well, somewhere we have to draw a line
307 }
308 bSkipNextFrame = TRUE; // -> signal for skipping the next frame
309 }
310 else // we were faster than real psx? fine :)
311 if(UseFrameLimit) // frame limit used? so we wait til the 'real psx time' has been reached
312 {
313 if(dwLaceCnt>MAXLACE) // -> security check
314 _ticks_since_last_update=dwWaitTime;
315
316 while(_ticks_since_last_update<dwWaitTime) // just do a waiting loop...
317 {
318 if (IsPerformanceCounter)
319 {
320 QueryPerformanceCounter(&CurrentTime);
321 _ticks_since_last_update = CurrentTime.LowPart - LastTime.LowPart;
322 }
323 else
324 {
325 curticks = timeGetTime();
326 _ticks_since_last_update = curticks - lastticks;
327 }
328 }
329 }
330
331 if (IsPerformanceCounter) // ok, start time of the next frame
332 QueryPerformanceCounter(&LastTime);
333 lastticks = timeGetTime();
334 }
335
336 dwLaceCnt=0; // init lace counter
337 }
338
339 ////////////////////////////////////////////////////////////////////////
340
341 void calcfps(void) // fps calculations
342 {
343 static DWORD curticks,_ticks_since_last_update,lastticks;
344 static long fps_cnt = 0;
345 static DWORD fps_tck = 1;
346 static LARGE_INTEGER CurrentTime;
347 static LARGE_INTEGER LastTime;
348 static long fpsskip_cnt = 0;
349 static DWORD fpsskip_tck = 1;
350
351 if(IsPerformanceCounter)
352 {
353 QueryPerformanceCounter(&CurrentTime);
354 _ticks_since_last_update=CurrentTime.LowPart-LastTime.LowPart;
355
356 //--------------------------------------------------//
357 curticks = timeGetTime();
358 if(_ticks_since_last_update>(CPUFrequency.LowPart>>1))
359 _ticks_since_last_update = (CPUFrequency.LowPart * (curticks - lastticks))/1000;
360 lastticks=curticks;
361 //--------------------------------------------------//
362
363 if(UseFrameSkip && !UseFrameLimit && _ticks_since_last_update)
364 fps_skip=min(fps_skip,(((float)CPUFrequency.LowPart) / ((float)_ticks_since_last_update) +1.0f));
365
366 LastTime.HighPart = CurrentTime.HighPart;
367 LastTime.LowPart = CurrentTime.LowPart;
368 }
369 else
370 {
371 curticks = timeGetTime();
372 _ticks_since_last_update=curticks-lastticks;
373
374 if(UseFrameSkip && !UseFrameLimit && _ticks_since_last_update)
375 fps_skip=min(fps_skip,((float)1000/(float)_ticks_since_last_update+1.0f));
376
377 lastticks = curticks;
378 }
379
380 if(UseFrameSkip && UseFrameLimit)
381 {
382 fpsskip_tck += _ticks_since_last_update;
383
384 if(++fpsskip_cnt==2)
385 {
386 if(IsPerformanceCounter)
387 fps_skip = ((float)CPUFrequency.LowPart) / ((float)fpsskip_tck) *2.0f;
388 else
389 fps_skip = (float)2000/(float)fpsskip_tck;
390
391 fps_skip +=6.0f;
392
393 fpsskip_cnt = 0;
394 fpsskip_tck = 1;
395 }
396 }
397
398 fps_tck += _ticks_since_last_update;
399
400 if(++fps_cnt==10)
401 {
402 if(IsPerformanceCounter)
403 fps_cur = ((float)CPUFrequency.LowPart) / ((float)fps_tck) *10.0f;
404 else
405 fps_cur = (float)10000/(float)fps_tck;
406
407 fps_cnt = 0;
408 fps_tck = 1;
409
410 if(UseFrameLimit && fps_cur>fFrameRateHz) // optical adjust ;) avoids flickering fps display
411 fps_cur=fFrameRateHz;
412 }
413 }
414
415 ////////////////////////////////////////////////////////////////////////
416 // PC FPS skipping / limit
417 ////////////////////////////////////////////////////////////////////////
418
419 void PCFrameCap (void)
420 {
421 static DWORD curticks, lastticks, _ticks_since_last_update;
422 static DWORD TicksToWait = 0;
423 static LARGE_INTEGER CurrentTime;
424 static LARGE_INTEGER LastTime;
425 BOOL Waiting = TRUE;
426
427 while (Waiting)
428 {
429 if (IsPerformanceCounter)
430 {
431 QueryPerformanceCounter(&CurrentTime);
432 _ticks_since_last_update = CurrentTime.LowPart - LastTime.LowPart;
433
434 //------------------------------------------------//
435 curticks = timeGetTime();
436 if(_ticks_since_last_update>(CPUFrequency.LowPart>>1))
437 {
438 if(curticks < lastticks)
439 _ticks_since_last_update = TicksToWait+1;
440 else _ticks_since_last_update = (CPUFrequency.LowPart * (curticks - lastticks))/1000;
441 }
442 //------------------------------------------------//
443
444 if ((_ticks_since_last_update > TicksToWait) ||
445 (CurrentTime.LowPart < LastTime.LowPart))
446 {
447 Waiting = FALSE;
448 lastticks=curticks;
449 LastTime.HighPart = CurrentTime.HighPart;
450 LastTime.LowPart = CurrentTime.LowPart;
451 TicksToWait = (unsigned long)(CPUFrequency.LowPart / fFrameRateHz);
452 }
453 }
454 else
455 {
456 curticks = timeGetTime();
457 _ticks_since_last_update = curticks - lastticks;
458 if ((_ticks_since_last_update > TicksToWait) ||
459 (curticks < lastticks))
460 {
461 Waiting = FALSE;
462 lastticks = curticks;
463 TicksToWait = (1000 / (DWORD)fFrameRateHz);
464 }
465 }
466 }
467 }
468
469 ////////////////////////////////////////////////////////////////////////
470
471 void PCcalcfps(void)
472 {
473 static DWORD curticks,_ticks_since_last_update,lastticks;
474 static long fps_cnt = 0;
475 static float fps_acc = 0;
476 static LARGE_INTEGER CurrentTime;
477 static LARGE_INTEGER LastTime;
478 float CurrentFPS=0;
479
480 if(IsPerformanceCounter)
481 {
482 QueryPerformanceCounter(&CurrentTime);
483 _ticks_since_last_update=CurrentTime.LowPart-LastTime.LowPart;
484
485 //--------------------------------------------------//
486 curticks = timeGetTime();
487 if(_ticks_since_last_update>(CPUFrequency.LowPart>>1))
488 _ticks_since_last_update = (CPUFrequency.LowPart * (curticks - lastticks))/1000;
489 lastticks=curticks;
490 //--------------------------------------------------//
491
492 if(_ticks_since_last_update)
493 {
494 CurrentFPS = ((float)CPUFrequency.LowPart) / ((float)_ticks_since_last_update);
495 }
496 else CurrentFPS = 0;
497 LastTime.HighPart = CurrentTime.HighPart;
498 LastTime.LowPart = CurrentTime.LowPart;
499 }
500 else
501 {
502 curticks = timeGetTime();
503 if(_ticks_since_last_update=curticks-lastticks)
504 CurrentFPS=(float)1000/(float)_ticks_since_last_update;
505 else CurrentFPS = 0;
506 lastticks = curticks;
507 }
508
509 fps_acc += CurrentFPS;
510
511 if(++fps_cnt==10)
512 {
513 fps_cur = fps_acc / 10;
514 fps_acc = 0;
515 fps_cnt = 0;
516 }
517
518 fps_skip=CurrentFPS+1.0f;
519 }
520
521 ////////////////////////////////////////////////////////////////////////
522
523 void SetAutoFrameCap(void)
524 {
525 if(iFrameLimit==1)
526 {
527 fFrameRateHz = fFrameRate;
528 if(IsPerformanceCounter)
529 dwFrameRateTicks=(DWORD)(CPUFrequency.LowPart / fFrameRateHz);
530 else dwFrameRateTicks=(1000 / (DWORD)fFrameRateHz);
531 return;
532 }
533
534 if(dwActFixes&32)
535 {
536 if (PSXDisplay.Interlaced)
537 fFrameRateHz = PSXDisplay.PAL?50.0f:60.0f;
538 else fFrameRateHz = PSXDisplay.PAL?25.0f:30.0f;
539 }
540 else
541 {
542 fFrameRateHz = PSXDisplay.PAL?50.0f:59.94f;
543 if(IsPerformanceCounter)
544 dwFrameRateTicks=(DWORD)(CPUFrequency.LowPart / fFrameRateHz);
545 else dwFrameRateTicks=(1000 / (DWORD)fFrameRateHz);
546 }
547 }
548
549 ////////////////////////////////////////////////////////////////////////
550
551 void SetFPSHandler(void)
552 {
553 if (QueryPerformanceFrequency (&CPUFrequency)) // timer mode
554 IsPerformanceCounter = TRUE;
555 else IsPerformanceCounter = FALSE;
556 }
557
558 ////////////////////////////////////////////////////////////////////////
559
560 void InitFPS(void)
561 {
562 bInitCap = TRUE;
563
564 if(fFrameRateHz==0) fFrameRateHz=fFrameRate; // set user framerate
565
566 if(IsPerformanceCounter)
567 dwFrameRateTicks=(DWORD)(CPUFrequency.LowPart / fFrameRateHz);
568 else dwFrameRateTicks=(1000 / (DWORD)fFrameRateHz);
569 }
570
571 ////////////////////////////////////////////////////////////////////////