diff --git a/src/d_main.c b/src/d_main.c index 5f31f53c9..d54b6e4dc 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -350,6 +350,8 @@ static void D_Renderview(void) // added comment : there is a wipe eatch change of the gamestate gamestate_t wipegamestate = GS_LEVEL; +static void D_LimitFps(void); + static void D_MakeIntermissionBG(void) { if (!lastdraw) @@ -645,6 +647,10 @@ static boolean D_Display(void) CON_Drawer(); // Ha, i LIED! + // be sure to limit our fps *before* drawing + // so we can properly "pace" our frames + D_LimitFps(); + if (cv_lua_profile.value > 0) { LUA_RenderTimers(); @@ -658,6 +664,78 @@ static boolean D_Display(void) return ranwipe; } +static precise_t sleeptime = 0; + +static boolean D_UseNewFramelimiter(void) +{ + //if (!cv_newframelimiter.value) return false; + return (rendermode != render_none && !dedicated && (R_GetFramerateCap() >= 45) && R_UsingFrameInterpolation()); +} + +static void D_LimitFps(void) +{ + precise_t curtime, start; + static precise_t lasttime = 0; + + // timedemo runs uncapped! + if (singletics) + return; + + if (!D_UseNewFramelimiter()) + return; + + const UINT32 framecap = R_GetFramerateCap(); + + // uncapped obviously does not need to be... capped... + if (framecap == 0) + return; + + // dont need to sleep if we vsync and our framecap is set to refresh rate or higher + if (cv_vidwait.value && (cv_fpscap.value == 0 || framecap > I_GetRefreshRate())) + return; + + start = I_GetPreciseTime(); + + const UINT64 precision = I_GetPrecisePrecision(); + + // frametarget in precise_t ticks (ex. 60fps = ~16,666,666) + const precise_t frametarget = precision / framecap; + + lasttime += frametarget; + + // just sync this up if we missed the target + if ((INT64)(start - lasttime) >= 0) + lasttime = start; + + while (1) + { + // check the current time against the "time of the old frame" + curtime = I_GetPreciseTime(); + + // done waiting! + // continue as normal + if ((INT64)(curtime - lasttime) >= 0) + { + // time we spent :chaosleep:ing or doing absolutely nothing + // for frameskip calculations + sleeptime = curtime - start; + return; + } + + // if we have more than 1ms to wait + // sleep for a bit to not trash the cpu lul + precise_t remaining = lasttime - curtime; + + if (remaining > (precise_t)(precision / 1000)) + { + // sleep for 90% of the time we gotta wait for the next frame + // since we dont wanna oversleep :chaosleep: + I_SleepDuration(9 * remaining / 10); + } + } + // the rest we just be waitin until we meet our frametime target +} + // ========================================================================= // D_SRB2Loop // ========================================================================= @@ -709,13 +787,13 @@ void D_SRB2Loop(void) for (;;) { - // capbudget is the minimum precise_t duration of a single loop iteration - precise_t capbudget; precise_t elapsed; precise_t enterprecise, finishprecise; boolean ranwipe = false; + sleeptime = 0; + enterprecise = I_GetPreciseTime(); I_HandleInterrupt(); @@ -723,10 +801,6 @@ void D_SRB2Loop(void) memset(&g_dc, 0, sizeof(g_dc)); Z_Frame_Reset(); - // Casting the return value of a function is bad practice (apparently) - const UINT32 framecap = R_GetFramerateCap(); - capbudget = (framecap == 0) ? 0 : (precise_t)((double)precision / (double)framecap + 0.5); // + 0.5 instead of round - I_UpdateTime(cv_timescale.value); if (lastwipetic) @@ -747,11 +821,9 @@ void D_SRB2Loop(void) } #ifdef DEBUGFILE - if (!realtics) - if (debugload) - debugload--; + if (!realtics && debugload) + debugload--; #endif - interp = !dedicated && R_UsingFrameInterpolation(); doDisplay = false; @@ -785,6 +857,7 @@ void D_SRB2Loop(void) { P_RunChaseCameras(); } + R_UpdateViewInterpolation(); } @@ -830,7 +903,6 @@ void D_SRB2Loop(void) M_SaveFrame(); if (takescreenshot) M_DoScreenShot(); - #ifndef DEDICATED if (!dedicated && renderisnewtic) { @@ -852,9 +924,9 @@ void D_SRB2Loop(void) // Fully completed frame made. finishprecise = I_GetPreciseTime(); - // Use the time before sleep for frameskip calculations: + // Use the time without sleep for frameskip calculations: // post-sleep time is literally being intentionally wasted - elapsed = finishprecise - enterprecise; + elapsed = (finishprecise - enterprecise) - sleeptime; deltasecs = (double)elapsed / (double)precision; deltatics = deltasecs * (double)NEWTICRATE; @@ -882,8 +954,12 @@ void D_SRB2Loop(void) frameskip = 0; } - if (!singletics) + // old fps limiter + if (!singletics && !D_UseNewFramelimiter()) { + const UINT32 framecap = R_GetFramerateCap(); + const precise_t capbudget = (framecap == 0) ? 0 : (precise_t)((double)precision / (double)framecap + 0.5); // + 0.5 instead of round + // in the case of "match refresh rate" + vsync, don't sleep at all const boolean vsync_with_match_refresh = cv_vidwait.value && cv_fpscap.value == 0; @@ -891,13 +967,13 @@ void D_SRB2Loop(void) { I_SleepDuration(capbudget - elapsed); } - } - // Capture the time once more to get the real delta time. - finishprecise = I_GetPreciseTime(); - elapsed = finishprecise - enterprecise; - deltasecs = (double)elapsed / (double)precision; - deltatics = deltasecs * (double)NEWTICRATE; + // Capture the time once more to get the real delta time. + finishprecise = I_GetPreciseTime(); + elapsed = finishprecise - enterprecise; + deltasecs = (double)elapsed / (double)precision; + deltatics = deltasecs * (double)NEWTICRATE; + } } }