Graphics: Attempt to provide more consistent frame timings
We now actively track how far behind / in front of an ideal timestep we are during each frame, and try to catch up / delay approximate this timing. Therefore we use more precise timers and sleep functions (nanosleep if available). We also delay **before** the final buffer swap so the frame displays at more consistent points in time. Not only should this provide a somewhat more consistent looking map scrolling at lower frame rates, it also guarantees that we don't fall out of sync eg. with the Audio during longer cutscenes. 'Graphics.frameReset()' now finally has a function, in that it resets the ideal timestep approximation, which I beliefe was also its job in the original RMXP engine. I'm not sure how well this will work when the frame rate is set to the monitor refresh rate and vsync is turned on. Very likely unnecessary frame skips will occur here and there due to imprecise timers. In the future we should probably check if the frame rate is equal to or higher than the monitor rate, and disable frame skip accordingly. These changes currently break the F2 FPS display (it shows a value that's slightly too high).
This commit is contained in:
parent
906f9fae17
commit
58bd60a70f
|
@ -394,7 +394,7 @@ void EventThread::notifyFrame()
|
|||
|
||||
static uint64_t freq = SDL_GetPerformanceFrequency();
|
||||
|
||||
int32_t currFPS = freq / diff;
|
||||
double currFPS = (double) freq / diff;
|
||||
fps.acc += currFPS;
|
||||
++fps.accDiv;
|
||||
|
||||
|
|
|
@ -99,7 +99,7 @@ private:
|
|||
bool displaying;
|
||||
bool immInitFlag;
|
||||
bool immFiniFlag;
|
||||
uint64_t acc;
|
||||
double acc;
|
||||
uint32_t accDiv;
|
||||
} fps;
|
||||
};
|
||||
|
|
113
src/graphics.cpp
113
src/graphics.cpp
|
@ -38,6 +38,8 @@
|
|||
#include "SDL_video.h"
|
||||
#include "SDL_timer.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
struct PingPong
|
||||
{
|
||||
TEXFBO rt[2];
|
||||
|
@ -244,32 +246,105 @@ private:
|
|||
|
||||
struct FPSLimiter
|
||||
{
|
||||
unsigned lastTickCount;
|
||||
unsigned mspf; /* ms per frame */
|
||||
uint64_t lastTickCount;
|
||||
|
||||
FPSLimiter(unsigned desiredFPS)
|
||||
: lastTickCount(SDL_GetTicks())
|
||||
/* ticks per frame */
|
||||
int64_t tpf;
|
||||
|
||||
/* Ticks per second */
|
||||
const uint64_t tickFreq;
|
||||
|
||||
/* Ticks per milisecond */
|
||||
const uint64_t tickFreqMS;
|
||||
|
||||
/* Ticks per nanosecond */
|
||||
const double tickFreqNS;
|
||||
|
||||
/* Data for frame timing adjustment */
|
||||
struct
|
||||
{
|
||||
/* Last tick count */
|
||||
uint64_t last;
|
||||
|
||||
/* How far behind/in front we are for ideal frame timing */
|
||||
int64_t idealDiff;
|
||||
|
||||
bool resetFlag;
|
||||
} adj;
|
||||
|
||||
FPSLimiter(uint16_t desiredFPS)
|
||||
: lastTickCount(SDL_GetPerformanceCounter()),
|
||||
tickFreq(SDL_GetPerformanceFrequency()),
|
||||
tickFreqMS(tickFreq / 1000),
|
||||
tickFreqNS(tickFreq / 1000000000)
|
||||
{
|
||||
setDesiredFPS(desiredFPS);
|
||||
|
||||
adj.last = SDL_GetPerformanceCounter();
|
||||
adj.idealDiff = 0;
|
||||
adj.resetFlag = false;
|
||||
}
|
||||
|
||||
void setDesiredFPS(unsigned value)
|
||||
void setDesiredFPS(uint16_t value)
|
||||
{
|
||||
mspf = 1000 / value;
|
||||
tpf = tickFreq / value;
|
||||
}
|
||||
|
||||
void delay()
|
||||
{
|
||||
unsigned tmpTicks = SDL_GetTicks();
|
||||
unsigned tickDelta = tmpTicks - lastTickCount;
|
||||
lastTickCount = tmpTicks;
|
||||
int64_t tickDelta = SDL_GetPerformanceCounter() - lastTickCount;
|
||||
int64_t toDelay = tpf - tickDelta;
|
||||
|
||||
/* Compensate for the last delta
|
||||
* to the ideal timestep */
|
||||
toDelay -= adj.idealDiff;
|
||||
|
||||
int toDelay = mspf - tickDelta;
|
||||
if (toDelay < 0)
|
||||
toDelay = 0;
|
||||
|
||||
SDL_Delay(toDelay);
|
||||
lastTickCount = SDL_GetTicks();
|
||||
delayTicks(toDelay);
|
||||
|
||||
uint64_t now = lastTickCount = SDL_GetPerformanceCounter();
|
||||
int64_t diff = now - adj.last;
|
||||
adj.last = now;
|
||||
|
||||
/* Recalculate our temporal position
|
||||
* relative to the ideal timestep */
|
||||
adj.idealDiff = diff - tpf + adj.idealDiff;
|
||||
|
||||
if (adj.resetFlag)
|
||||
{
|
||||
adj.idealDiff = 0;
|
||||
adj.resetFlag = false;
|
||||
}
|
||||
}
|
||||
|
||||
void resetFrameAdjust()
|
||||
{
|
||||
adj.resetFlag = true;
|
||||
}
|
||||
|
||||
/* If we're more than a full frame's worth
|
||||
* of ticks behind the ideal timestep,
|
||||
* there's no choice but to skip frame(s)
|
||||
* to catch up */
|
||||
bool frameSkipRequired() const
|
||||
{
|
||||
return adj.idealDiff > tpf;
|
||||
}
|
||||
|
||||
private:
|
||||
void delayTicks(uint64_t ticks)
|
||||
{
|
||||
#ifdef HAVE_NANOSLEEP
|
||||
struct timespec req;
|
||||
req.tv_sec = 0;
|
||||
req.tv_nsec = ticks / tickFreqNS;
|
||||
while (nanosleep(&req, &req) == -1)
|
||||
;
|
||||
#else
|
||||
SDL_Delay(ticks / tickFreqMS);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -408,8 +483,8 @@ struct GraphicsPrivate
|
|||
|
||||
void swapGLBuffer()
|
||||
{
|
||||
SDL_GL_SwapWindow(threadData->window);
|
||||
fpsLimiter.delay();
|
||||
SDL_GL_SwapWindow(threadData->window);
|
||||
|
||||
++frameCount;
|
||||
|
||||
|
@ -471,6 +546,16 @@ void Graphics::update()
|
|||
if (p->frozen)
|
||||
return;
|
||||
|
||||
if (p->fpsLimiter.frameSkipRequired())
|
||||
{
|
||||
/* Skip frame */
|
||||
p->fpsLimiter.delay();
|
||||
++p->frameCount;
|
||||
p->threadData->ethread->notifyFrame();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
p->checkResize();
|
||||
p->redrawScreen();
|
||||
|
||||
|
@ -567,7 +652,7 @@ void Graphics::transition(int duration,
|
|||
|
||||
void Graphics::frameReset()
|
||||
{
|
||||
|
||||
p->fpsLimiter.resetFrameAdjust();
|
||||
}
|
||||
|
||||
#undef RET_IF_DISP
|
||||
|
|
Loading…
Reference in New Issue