Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
perf(gameengine): stop the catch-up loop freezing the picture
Olcay Seygan405afa92c85d
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h b/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.hindex 27f85eaf1..dc3503417 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h@@ -51,6 +51,27 @@/** The above in logic frames, at a given game speed: 3 at the retail 30fps, 6 at 60. */Int GameEngine_logicCatchupMaxFrames( Int logicFps );+/** How long the catch-up loop may keep starting new logic ticks before it gives up for this pass.++ A frame count cannot tell a cheap tick from an expensive one, and that is the whole difference+ between the two situations the loop is in. Behind because the *renderer* hitched, the ticks are+ 2ms each and paying three of them costs nothing. Behind because eight Merciless AIs are all+ fighting at once, the ticks are 25ms each and paying three of them freezes the picture for 75ms+ on top of the render - which is exactly the 91ms and 113ms frames measured on Twilight Flame.++ So the loop is bounded by a clock as well as by a count: after each tick, another one only+ starts if the loop has spent less than this. Cheap debt still gets paid in full; expensive debt+ is left on the accumulator, where the existing cap stops it spiralling and the match honestly+ runs a touch slow instead of stopping dead. The worst case becomes this plus one tick.++ That is the intent the frame count was already written for - "when the logic frame itself is what+ overran, paying the debt would just queue more of the same work" - measured in the unit that can+ actually see it. */+#define LOGIC_CATCHUP_BUDGET_MS 8.0f++/** Whether the catch-up loop may start another logic tick this pass. */+Bool GameEngine_mayStartAnotherCatchupTick( Int ticksSoFar, Int maxTicks, Real elapsedMsInLoop );+// forward declarationsclass AudioManager;class GameLogic;diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cppindex 1f09beb5e..ff388ea3f 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp@@ -1016,6 +1016,13 @@ Int GameEngine_logicCatchupMaxFrames( Int logicFps )return frames < 1 ? 1 : frames;}+Bool GameEngine_mayStartAnotherCatchupTick( Int ticksSoFar, Int maxTicks, Real elapsedMsInLoop )+{+ if (ticksSoFar >= maxTicks)+ return FALSE;+ return elapsedMsInLoop < LOGIC_CATCHUP_BUDGET_MS;+}+Bool GameEngine_isLogicFrameDue( Real& accumMs, Real elapsedMs, Int logicFps ){if (logicFps <= 0)@@ -1602,13 +1609,26 @@ void GameEngine::update( void )// frame that is itself over budget cannot pull the loop into a spiral.Int logicTicksThisPass = 0;const Int maxTicksThisPass = GameEngine_logicCatchupMaxFrames(m_maxFPS);- do+ /* Bounded by the clock as well as by the count - see LOGIC_CATCHUP_BUDGET_MS. Three+ 25ms ticks back to back with no picture in between is the 113ms freeze; one of them+ plus the render is a dropped frame nobody files a bug about. */+ Int64 tCatchupStart, tCatchupNow;+ QueryPerformanceCounter( (LARGE_INTEGER *)&tCatchupStart );+ for( ;; ){TheGameLogic->UPDATE();++logicTicksThisPass;++ if (!mayCatchUp)+ break;+ QueryPerformanceCounter( (LARGE_INTEGER *)&tCatchupNow );+ if (!GameEngine_mayStartAnotherCatchupTick( logicTicksThisPass, maxTicksThisPass,+ engineElapsedMS( tCatchupStart, tCatchupNow ) ))+ break;+ // Asked last, because it is the one with a side effect: it spends the debt it reports.+ if (!GameEngine_isLogicFrameDue(logicAccumMs, 0.0f, m_maxFPS))+ break;}- while (mayCatchUp && logicTicksThisPass < maxTicksThisPass &&- GameEngine_isLogicFrameDue(logicAccumMs, 0.0f, m_maxFPS));#ifdef DEBUG_LOGGINGQueryPerformanceCounter( (LARGE_INTEGER *)&tLogicEnd );logicMS = engineElapsedMS( tLogicStart, tLogicEnd );diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 1372b196b..c54236f7f 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -751,6 +751,51 @@ TEST(logic_tick_is_wall_clock_paced_not_render_paced)CHECK( due >= 36 && due <= 38 ); // 630ms of wall clock at 60Hz, and no more}+/* The count above cannot tell a cheap logic tick from an expensive one, and that is the whole+ difference between the two situations the catch-up loop is ever in. Measured on Twilight Flame+ with eight Merciless AI: three 25ms ticks back to back with no picture in between is where the+ 91ms and 113ms frames came from. So the loop is bounded by a clock as well. */+extern Bool GameEngine_mayStartAnotherCatchupTick( Int ticksSoFar, Int maxTicks, Real elapsedMsInLoop );++TEST(catchup_stops_paying_debt_when_the_logic_tick_is_what_is_slow)+{+ const Int cap30 = GameEngine_logicCatchupMaxFrames( 30 ); // 3++ // Behind because the renderer hitched: the ticks are cheap, so pay the debt in full.+ CHECK( GameEngine_mayStartAnotherCatchupTick( 1, cap30, 2.0f ) );+ CHECK( GameEngine_mayStartAnotherCatchupTick( 2, cap30, 4.0f ) );++ // Behind because the simulation is what is slow: one 25ms tick has already spent the budget,+ // so the next two do not run and the freeze is one tick long instead of three.+ CHECK( !GameEngine_mayStartAnotherCatchupTick( 1, cap30, 25.0f ) );+ CHECK( !GameEngine_mayStartAnotherCatchupTick( 2, cap30, 50.0f ) );++ // The frame count still caps it, however cheap the ticks are - the anti-spiral is unchanged.+ CHECK( !GameEngine_mayStartAnotherCatchupTick( cap30, cap30, 0.0f ) );+ CHECK( !GameEngine_mayStartAnotherCatchupTick( cap30 + 1, cap30, 0.0f ) );++ // The budget is a strict bound, so exactly-at-budget stops too.+ CHECK( !GameEngine_mayStartAnotherCatchupTick( 1, cap30, LOGIC_CATCHUP_BUDGET_MS ) );+ CHECK( GameEngine_mayStartAnotherCatchupTick( 1, cap30, LOGIC_CATCHUP_BUDGET_MS - 0.1f ) );++ /* What it is worth, in the shape the loop actually runs: a pass that owes three ticks, where+ each tick costs 25ms. Old rule: three ticks, 75ms of frozen picture. New rule: one. */+ Real elapsed = 0.0f;+ Int ticks = 0;+ do { elapsed += 25.0f; ++ticks; }+ while( GameEngine_mayStartAnotherCatchupTick( ticks, cap30, elapsed ) );+ CHECK_EQ( 1, ticks );+ CHECK_NEAR( 25.0f, elapsed, 0.01f );++ // ...and the same pass with 2ms ticks still pays all three, because it costs 6ms to do so.+ elapsed = 0.0f;+ ticks = 0;+ do { elapsed += 2.0f; ++ticks; }+ while( GameEngine_mayStartAnotherCatchupTick( ticks, cap30, elapsed ) );+ CHECK_EQ( cap30, ticks );+ CHECK_NEAR( 6.0f, elapsed, 0.01f );+}+/* AnimateWindowManager.cpp: the same argument one layer up. The menu animationscount fixed steps and were authored against a capped frame rate, so with therenderer uncapped they have to be paced off the wall clock too or a whole