Debian Codegraph @

win32/plugins/peopsxgl/winsrc/fps.c

raw
1 /***************************************************************************
2 fps.c - description
3 -------------------
4 begin : Sun Mar 08 2009
5 copyright : (C) 1999-2009 by Pete Bernert
6 web : www.pbernert.com
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 //*************************************************************************//
20 // History of changes:
21 //
22 // 2009/03/08 - Pete
23 // - generic cleanup for the Peops release
24 //
25 //*************************************************************************//
26
27 #include "stdafx.h"
28
29 #define _IN_FPS
30
31 #include "externals.h"
32
33 ////////////////////////////////////////////////////////////////////////
34 // FPS stuff
35 ////////////////////////////////////////////////////////////////////////
36
37 BOOL bIsPerformanceCounter=FALSE;
38 float fFrameRateHz=0;
39 DWORD dwFrameRateTicks=16;
40 float fFrameRate;
41 int iFrameLimit;
42 BOOL bUseFrameLimit=FALSE;
43 BOOL bUseFrameSkip=0;
44 DWORD dwLaceCnt=0;
45
46 LARGE_INTEGER liCPUFrequency;
47
48 ////////////////////////////////////////////////////////////////////////
49 // FPS skipping / limit
50 ////////////////////////////////////////////////////////////////////////
51
52 BOOL bInitCap = TRUE;
53 float fps_skip = 0;
54 float fps_cur = 0;
55
56 void FrameCap (void)
57 {
58 static DWORD curticks, lastticks, _ticks_since_last_update;
59 static DWORD TicksToWait = 0;
60 static LARGE_INTEGER CurrentTime;
61 static LARGE_INTEGER LastTime;
62 static BOOL SkipNextWait = FALSE;
63 BOOL Waiting = TRUE;
64
65 //---------------------------------------------------------
66 // init some static vars...
67 // bInitCap is TRUE on startup and everytime the user
68 // is toggling the frame limit
69 //---------------------------------------------------------
70
71 if(bInitCap)
72 {
73 bInitCap=FALSE;
74 if (bIsPerformanceCounter)
75 QueryPerformanceCounter(&LastTime);
76 lastticks = timeGetTime();
77 TicksToWait=0;
78 return;
79 }
80
81 //---------------------------------------------------------
82
83 if(bIsPerformanceCounter)
84 {
85 QueryPerformanceCounter(&CurrentTime);
86 _ticks_since_last_update = CurrentTime.LowPart - LastTime.LowPart;
87
88 //---------------------------------------------------------
89 // check if diff > 1/2 sec, if yes: take mm timer value
90 //---------------------------------------------------------
91
92 curticks = timeGetTime();
93 if(_ticks_since_last_update>(liCPUFrequency.LowPart>>1))
94 {
95 if(curticks < lastticks)
96 _ticks_since_last_update = dwFrameRateTicks+TicksToWait+1;
97 else _ticks_since_last_update = (liCPUFrequency.LowPart * (curticks - lastticks))/1000;
98 }
99
100 //---------------------------------------------------------
101
102 if ((_ticks_since_last_update > TicksToWait) ||
103 (CurrentTime.LowPart < LastTime.LowPart))
104 {
105 LastTime.HighPart = CurrentTime.HighPart;
106 LastTime.LowPart = CurrentTime.LowPart;
107
108 lastticks=curticks;
109
110 if((_ticks_since_last_update-TicksToWait) > dwFrameRateTicks)
111 TicksToWait=0;
112 else TicksToWait=dwFrameRateTicks-(_ticks_since_last_update-TicksToWait);
113 }
114 else
115 {
116 while (Waiting)
117 {
118 QueryPerformanceCounter(&CurrentTime);
119 _ticks_since_last_update = CurrentTime.LowPart - LastTime.LowPart;
120
121 //---------------------------------------------------------
122 // check if diff > 1/2 sec, if yes: take mm timer value
123 //---------------------------------------------------------
124 curticks = timeGetTime();
125 if(_ticks_since_last_update>(liCPUFrequency.LowPart>>1))
126 {
127 if(curticks < lastticks)
128 _ticks_since_last_update = TicksToWait+1;
129 else _ticks_since_last_update = (liCPUFrequency.LowPart * (curticks - lastticks))/1000;
130 }
131 //---------------------------------------------------------
132
133 if ((_ticks_since_last_update > TicksToWait) ||
134 (CurrentTime.LowPart < LastTime.LowPart))
135 {
136 Waiting = FALSE;
137
138 lastticks=curticks;
139
140 LastTime.HighPart = CurrentTime.HighPart;
141 LastTime.LowPart = CurrentTime.LowPart;
142 TicksToWait = dwFrameRateTicks;
143 }
144 }
145 }
146 }
147 else
148 {
149 curticks = timeGetTime();
150 _ticks_since_last_update = curticks - lastticks;
151
152 if ((_ticks_since_last_update > TicksToWait) ||
153 (curticks < lastticks))
154 {
155 lastticks = curticks;
156
157 if((_ticks_since_last_update-TicksToWait) > dwFrameRateTicks)
158 TicksToWait=0;
159 else TicksToWait=dwFrameRateTicks-(_ticks_since_last_update-TicksToWait);
160 }
161 else
162 {
163 while (Waiting)
164 {
165 curticks = timeGetTime();
166 _ticks_since_last_update = curticks - lastticks;
167 if ((_ticks_since_last_update > TicksToWait) ||
168 (curticks < lastticks))
169 {
170 Waiting = FALSE;
171 lastticks = curticks;
172 TicksToWait = dwFrameRateTicks;
173 }
174 }
175 }
176 }
177 }
178
179 ////////////////////////////////////////////////////////////////////////
180
181 #define MAXSKIP 120
182 #define MAXLACE 16
183
184 void FrameSkip(void)
185 {
186 static int iNumSkips=0,iAdditionalSkip=0; // number of additional frames to skip
187 static DWORD dwLastLace=0; // helper var for frame limitation
188 static DWORD curticks, lastticks, _ticks_since_last_update;
189 static LARGE_INTEGER CurrentTime;
190 static LARGE_INTEGER LastTime;
191
192 if(!dwLaceCnt) return; // important: if no updatelace happened, we ignore it completely
193
194 if(iNumSkips) // we are in skipping mode?
195 {
196 dwLastLace+=dwLaceCnt; // -> calc frame limit helper (number of laces)
197 bSkipNextFrame = TRUE; // -> we skip next frame
198 iNumSkips--; // -> ok, one done
199 }
200 else // ok, no additional skipping has to be done...
201 { // we check now, if some limitation is needed, or a new skipping has to get started
202 DWORD dwWaitTime;
203
204 if(bInitCap || bSkipNextFrame) // first time or we skipped before?
205 {
206 if(bUseFrameLimit && !bInitCap) // frame limit wanted and not first time called?
207 {
208 DWORD dwT=_ticks_since_last_update; // -> that's the time of the last drawn frame
209 dwLastLace+=dwLaceCnt; // -> and that's the number of updatelace since the start of the last drawn frame
210
211 if(bIsPerformanceCounter) // -> now we calc the time of the last drawn frame + the time we spent skipping
212 {
213 QueryPerformanceCounter(&CurrentTime);
214 _ticks_since_last_update= dwT+CurrentTime.LowPart - LastTime.LowPart;
215 }
216 else
217 {
218 curticks = timeGetTime();
219 _ticks_since_last_update= dwT+curticks - lastticks;
220 }
221
222 dwWaitTime=dwLastLace*dwFrameRateTicks; // -> and now we calc the time the real psx would have needed
223
224 if(_ticks_since_last_update<dwWaitTime) // -> we were too fast?
225 {
226 if((dwWaitTime-_ticks_since_last_update)> // -> some more security, to prevent
227 (60*dwFrameRateTicks)) // wrong waiting times
228 _ticks_since_last_update=dwWaitTime;
229
230 while(_ticks_since_last_update<dwWaitTime) // -> loop until we have reached the real psx time
231 { // (that's the additional limitation, yup)
232 if(bIsPerformanceCounter)
233 {
234 QueryPerformanceCounter(&CurrentTime);
235 _ticks_since_last_update = dwT+CurrentTime.LowPart - LastTime.LowPart;
236 }
237 else
238 {
239 curticks = timeGetTime();
240 _ticks_since_last_update = dwT+curticks - lastticks;
241 }
242 }
243 }
244 else // we were still too slow ?!!?
245 {
246 if(iAdditionalSkip<MAXSKIP) // -> well, somewhen we really have to stop skipping on very slow systems
247 {
248 iAdditionalSkip++; // -> inc our watchdog var
249 dwLaceCnt=0; // -> reset lace count
250 if(bIsPerformanceCounter) // -> ok, start time of the next frame
251 QueryPerformanceCounter(&LastTime);
252 lastticks = timeGetTime();
253 return; // -> done, we will skip next frame to get more speed
254 }
255 }
256 }
257
258 bInitCap=FALSE; // -> ok, we have inited the frameskip func
259 iAdditionalSkip=0; // -> init additional skip
260 bSkipNextFrame=FALSE; // -> we don't skip the next frame
261 if(bIsPerformanceCounter) // -> we store the start time of the next frame
262 QueryPerformanceCounter(&LastTime);
263 lastticks = timeGetTime();
264 dwLaceCnt=0; // -> and we start to count the laces
265 dwLastLace=0;
266 _ticks_since_last_update=0;
267 return; // -> done, the next frame will get drawn
268 }
269
270 bSkipNextFrame=FALSE; // init the frame skip signal to 'no skipping' first
271
272 if(bIsPerformanceCounter) // get the current time (we are now at the end of one drawn frame)
273 {
274 QueryPerformanceCounter(&CurrentTime);
275 _ticks_since_last_update = CurrentTime.LowPart - LastTime.LowPart;
276 }
277 else
278 {
279 curticks = timeGetTime();
280 _ticks_since_last_update = curticks - lastticks;
281 }
282
283 dwLastLace=dwLaceCnt; // store curr count (frame limitation helper)
284 dwWaitTime=dwLaceCnt*dwFrameRateTicks; // calc the 'real psx lace time'
285
286 if(_ticks_since_last_update>dwWaitTime) // hey, we needed way too long for that frame...
287 {
288 if(bUseFrameLimit) // if limitation, we skip just next frame,
289 { // and decide after, if we need to do more
290 iNumSkips=0;
291 }
292 else
293 {
294 iNumSkips=_ticks_since_last_update/dwWaitTime; // -> calc number of frames to skip to catch up
295 iNumSkips--; // -> since we already skip next frame, one down
296 if(iNumSkips>MAXSKIP) iNumSkips=MAXSKIP; // -> well, somewhere we have to draw a line
297 }
298 bSkipNextFrame = TRUE; // -> signal for skipping the next frame
299 }
300 else // we were faster than real psx? fine :)
301 if(bUseFrameLimit) // frame limit used? so we wait til the 'real psx time' has been reached
302 {
303 if(dwLaceCnt>MAXLACE) // -> security check
304 _ticks_since_last_update=dwWaitTime;
305
306 while(_ticks_since_last_update<dwWaitTime) // just do a waiting loop...
307 {
308 if(bIsPerformanceCounter)
309 {
310 QueryPerformanceCounter(&CurrentTime);
311 _ticks_since_last_update = CurrentTime.LowPart - LastTime.LowPart;
312 }
313 else
314 {
315 curticks = timeGetTime();
316 _ticks_since_last_update = curticks - lastticks;
317 }
318 }
319 }
320
321 if(bIsPerformanceCounter) // ok, start time of the next frame
322 QueryPerformanceCounter(&LastTime);
323 lastticks = timeGetTime();
324 }
325
326 dwLaceCnt=0; // init lace counter
327 }
328
329 ////////////////////////////////////////////////////////////////////////
330
331 void calcfps(void)
332 {
333 static DWORD curticks,_ticks_since_last_update,lastticks;
334 static long fps_cnt = 0;
335 static DWORD fps_tck = 1;
336 static LARGE_INTEGER CurrentTime;
337 static LARGE_INTEGER LastTime;
338 static long fpsskip_cnt = 0;
339 static DWORD fpsskip_tck = 1;
340
341 if(bIsPerformanceCounter)
342 {
343 QueryPerformanceCounter(&CurrentTime);
344 _ticks_since_last_update=CurrentTime.LowPart-LastTime.LowPart;
345
346 //--------------------------------------------------//
347 curticks = timeGetTime();
348 if(_ticks_since_last_update>(liCPUFrequency.LowPart>>1))
349 _ticks_since_last_update = (liCPUFrequency.LowPart * (curticks - lastticks))/1000;
350 lastticks=curticks;
351 //--------------------------------------------------//
352
353 if(bUseFrameSkip && !bUseFrameLimit && _ticks_since_last_update)
354 fps_skip=min(fps_skip,(((float)liCPUFrequency.LowPart) / ((float)_ticks_since_last_update) +1.0f));
355
356 LastTime.HighPart = CurrentTime.HighPart;
357 LastTime.LowPart = CurrentTime.LowPart;
358 }
359 else
360 {
361 curticks = timeGetTime();
362 _ticks_since_last_update=curticks-lastticks;
363
364 if(bUseFrameSkip && !bUseFrameLimit && _ticks_since_last_update)
365 fps_skip=min(fps_skip,((float)1000/(float)_ticks_since_last_update+1.0f));
366
367 lastticks = curticks;
368 }
369
370 if(bUseFrameSkip && bUseFrameLimit)
371 {
372 fpsskip_tck += _ticks_since_last_update;
373
374 if(++fpsskip_cnt==2)
375 {
376 if(bIsPerformanceCounter)
377 fps_skip = ((float)liCPUFrequency.LowPart) / ((float)fpsskip_tck) *2.0f;
378 else
379 fps_skip = (float)2000/(float)fpsskip_tck;
380
381 fps_skip +=6.0f;
382
383 fpsskip_cnt = 0;
384 fpsskip_tck = 1;
385 }
386 }
387
388 fps_tck += _ticks_since_last_update;
389
390 if(++fps_cnt==10)
391 {
392 if(bIsPerformanceCounter)
393 fps_cur = ((float)liCPUFrequency.LowPart) / ((float)fps_tck) *10.0f;
394 else
395 fps_cur = (float)10000/(float)fps_tck;
396
397 fps_cnt = 0;
398 fps_tck = 1;
399
400 if(bUseFrameLimit && fps_cur>fFrameRateHz) // optical adjust ;) avoids flickering fps display
401 fps_cur=fFrameRateHz;
402 }
403 }
404
405 ////////////////////////////////////////////////////////////////////////
406 // PC FPS skipping / limit
407 ////////////////////////////////////////////////////////////////////////
408
409 void PCFrameCap(void)
410 {
411 static DWORD curticks, lastticks, _ticks_since_last_update;
412 static DWORD TicksToWait = 0;
413 static LARGE_INTEGER CurrentTime;
414 static LARGE_INTEGER LastTime;
415 BOOL Waiting = TRUE;
416
417 while (Waiting)
418 {
419 if(bIsPerformanceCounter)
420 {
421 QueryPerformanceCounter(&CurrentTime);
422 _ticks_since_last_update = CurrentTime.LowPart - LastTime.LowPart;
423
424 //------------------------------------------------//
425 curticks = timeGetTime();
426 if(_ticks_since_last_update>(liCPUFrequency.LowPart>>1))
427 {
428 if(curticks < lastticks)
429 _ticks_since_last_update = TicksToWait+1;
430 else _ticks_since_last_update = (liCPUFrequency.LowPart * (curticks - lastticks))/1000;
431 }
432 //------------------------------------------------//
433
434 if ((_ticks_since_last_update > TicksToWait) ||
435 (CurrentTime.LowPart < LastTime.LowPart))
436 {
437 Waiting = FALSE;
438
439 lastticks=curticks;
440
441 LastTime.HighPart = CurrentTime.HighPart;
442 LastTime.LowPart = CurrentTime.LowPart;
443 TicksToWait = (liCPUFrequency.LowPart / fFrameRateHz);
444 }
445 }
446 else
447 {
448 curticks = timeGetTime();
449 _ticks_since_last_update = curticks - lastticks;
450 if ((_ticks_since_last_update > TicksToWait) ||
451 (curticks < lastticks))
452 {
453 Waiting = FALSE;
454 lastticks = curticks;
455 TicksToWait = (1000 / (DWORD)fFrameRateHz);
456 }
457 }
458 }
459 }
460
461 ////////////////////////////////////////////////////////////////////////
462
463 void PCcalcfps(void)
464 {
465 static DWORD curticks,_ticks_since_last_update,lastticks;
466 static long fps_cnt = 0;
467 static float fps_acc = 0;
468 static LARGE_INTEGER CurrentTime;
469 static LARGE_INTEGER LastTime;
470 float CurrentFPS=0;
471
472 if(bIsPerformanceCounter)
473 {
474 QueryPerformanceCounter(&CurrentTime);
475 _ticks_since_last_update=CurrentTime.LowPart-LastTime.LowPart;
476
477 //--------------------------------------------------//
478 curticks = timeGetTime();
479 if(_ticks_since_last_update>(liCPUFrequency.LowPart>>1))
480 _ticks_since_last_update = (liCPUFrequency.LowPart * (curticks - lastticks))/1000;
481 lastticks=curticks;
482 //--------------------------------------------------//
483
484 if(_ticks_since_last_update)
485 {
486 CurrentFPS = ((float)liCPUFrequency.LowPart) / ((float)_ticks_since_last_update);
487 }
488 else CurrentFPS = 0;
489 LastTime.HighPart = CurrentTime.HighPart;
490 LastTime.LowPart = CurrentTime.LowPart;
491 }
492 else
493 {
494 curticks = timeGetTime();
495 if(_ticks_since_last_update=curticks-lastticks)
496 CurrentFPS=(float)1000/(float)_ticks_since_last_update;
497 else CurrentFPS = 0;
498 lastticks = curticks;
499 }
500
501 fps_acc += CurrentFPS;
502
503 if(++fps_cnt==10)
504 {
505 fps_cur = fps_acc / 10;
506 fps_acc = 0;
507 fps_cnt = 0;
508 }
509
510 fps_skip=CurrentFPS+1.0f;
511 }
512
513 ////////////////////////////////////////////////////////////////////////
514
515 void SetAutoFrameCap(void)
516 {
517 if(iFrameLimit==1)
518 {
519 fFrameRateHz = fFrameRate;
520 if(bIsPerformanceCounter)
521 dwFrameRateTicks=(liCPUFrequency.LowPart / fFrameRateHz);
522 else dwFrameRateTicks=(1000 / (DWORD)fFrameRateHz);
523 return;
524 }
525
526 if(dwActFixes&128)
527 {
528 if (PSXDisplay.Interlaced)
529 fFrameRateHz = PSXDisplay.PAL?50.0f:60.0f;
530 else fFrameRateHz = PSXDisplay.PAL?25.0f:30.0f;
531 }
532 else
533 {
534 //fFrameRateHz = PSXDisplay.PAL?50.0f:59.94f;
535
536 if(PSXDisplay.PAL)
537 {
538 if (STATUSREG&GPUSTATUS_INTERLACED)
539 fFrameRateHz=33868800.0f/677343.75f; // 50.00238
540 else fFrameRateHz=33868800.0f/680595.00f; // 49.76351
541 }
542 else
543 {
544 if (STATUSREG&GPUSTATUS_INTERLACED)
545 fFrameRateHz=33868800.0f/565031.25f; // 59.94146
546 else fFrameRateHz=33868800.0f/566107.50f; // 59.82750
547 }
548
549 if(bIsPerformanceCounter)
550 dwFrameRateTicks=(liCPUFrequency.LowPart / fFrameRateHz);
551 else dwFrameRateTicks=(1000 / (DWORD)fFrameRateHz);
552 }
553 }
554
555 ////////////////////////////////////////////////////////////////////////
556
557 void InitFrameCap(void) // inits cpu frequency info (on gpu startup)
558 {
559 if (QueryPerformanceFrequency (&liCPUFrequency))
560 bIsPerformanceCounter = TRUE;
561 else bIsPerformanceCounter = FALSE;
562 }
563
564 ////////////////////////////////////////////////////////////////////////
565
566 void ReInitFrameCap(void)
567 {
568 BOOL bOldPerformanceCounter=bIsPerformanceCounter; // store curr timer mode
569
570 if(dwActFixes&0x10000) // check game fix... high performance counters are bad on some mb chipsets
571 bIsPerformanceCounter=FALSE;
572 else
573 {
574 if (QueryPerformanceFrequency (&liCPUFrequency))
575 bIsPerformanceCounter = TRUE;
576 else bIsPerformanceCounter = FALSE;
577 }
578
579 if(bOldPerformanceCounter!=bIsPerformanceCounter) // changed?
580 {
581 bInitCap = TRUE;
582 SetAutoFrameCap();
583 }
584 }
585
586 ////////////////////////////////////////////////////////////////////////
587
588 void SetFrameRateConfig(void)
589 {
590 if(fFrameRateHz==0)
591 {
592 if(iFrameLimit==2) fFrameRateHz=59.94f; // auto framerate? set some init val (no pal/ntsc known yet)
593 else fFrameRateHz=fFrameRate; // else set user framerate
594 }
595
596 if(bIsPerformanceCounter)
597 dwFrameRateTicks=(liCPUFrequency.LowPart / fFrameRateHz);
598 else dwFrameRateTicks=(1000 / (DWORD)fFrameRateHz);
599 }
600
601 void CheckFrameRate(void) // called in updatelace (on every emulated psx vsync)
602 {
603 if(bUseFrameSkip)
604 {
605 if(!(dwActFixes&0x100))
606 {
607 dwLaceCnt++; // -> and store cnt of vsync between frames
608 if(dwLaceCnt>=MAXLACE && bUseFrameLimit)
609 {
610 if(dwLaceCnt==MAXLACE) bInitCap=TRUE;
611 FrameCap();
612 }
613 }
614 else if(bUseFrameLimit) FrameCap();
615 calcfps(); // -> calc fps display in skipping mode
616 }
617 else // -> non-skipping mode:
618 {
619 if(bUseFrameLimit) FrameCap();
620 if(ulKeybits&KEY_SHOWFPS) calcfps();
621 }
622 }
623
624 ////////////////////////////////////////////////////////////////////////
625
626 void CALLBACK GPUsetframelimit(unsigned long option) // new EPSXE interface func: main emu can enable/disable fps limitation this way
627 {
628 bInitCap = TRUE;
629
630 if(option==1) // emu says: limit
631 {
632 bUseFrameLimit=TRUE;bUseFrameSkip=FALSE;iFrameLimit=2;
633 SetAutoFrameCap();
634 }
635 else // emu says: no limit
636 {
637 bUseFrameLimit=FALSE;
638 }
639 }
640
641 ////////////////////////////////////////////////////////////////////////