Skip to content

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.

perf(gameengine): draw again beside a long tick, no cd poll in a match

Olcay Seygana0222574701e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 57fa80f10..0933a3390 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -284,7 +284,7 @@ found and fixed, EA's own, not port damage.**
- Mission timers keep EA's clock. A map's countdown still ticks 30 times a second, because the missions read a running timer's number and act on it: the second American mission declares victory when its "Win" timer drops below 1700, and on the first 60-step builds that came after 38 seconds instead of 10. The cutscene cues, the random music pick in the campaigns and the generals' challenges, the shell map's camera loop and the lightning on Eastern Everglades and Rocky Rampage were off the same way, 30 maps in all, and run on their original timing again. Custom maps get the same.
- Normal speed is 60 steps a second. Turbo mode, the one other speed a match can have, runs 120 of the same steps a second (see Turbo mode above). Replays still play as fast as you ask.
- On a screen faster than 60 Hz the armies glide. The battle moves in 60 steps a second, so a 144 or 240 Hz monitor showed every step two to four times over and a tank crossing the map moved in small hops. Now each picture draws every unit part of the way between its last two steps. The picture runs one step, 16.7 ms, behind the battle; the battle itself does not change, and a replay drawn with it, without it and with no picture at all ends on the same checksum. A 60 Hz screen gets nothing it did not have. `-nosmoothmotion` turns it off for a run, `-smoothmotion` turns it on whatever the screen.
-- The battle's arithmetic runs on a thread of its own, beside the drawing, on Windows. The game used to work out a step and then draw, one after the other, so a heavy step stalled the picture; now the screen keeps drawing while the next step is worked out, which pays most in the late game, where the steps are heaviest. In a late 4-player match, uncapped, frames slower than 16.7 ms (one 60 Hz refresh) fell from 23% to 0.8%, the slowest one frame in a hundred fell from 24.0 to 16.5 ms live and from 16.0-16.5 to 10.6-12.5 ms in a replay, and the average frame went from 15.2 to 11.8 ms. The battle still takes 60 steps a second, no more and no fewer. A match too heavy for its clock used to pay for the thread: the catch-up steps after the picture ignored the step already run beside it, so each frame ran one step more than without the thread. Eight Hard AIs on Twilight Flame at minute 20, run at four times normal speed so they cannot keep up: one frame in a thousand took 26.8 ms with the thread and 24.0 without. Both now spend the same budget, and with the thread that frame is 21.5 ms and the average frame 11.9 ms against 13.9 without, at 12% more steps a second. No menu or saved setting turns the thread off; `-logicthread 0` stays on the command line to chase a desync, and Mac and Linux keep the old order for now. Network games now draw beside the step as well. Two seats at 240 ms of ping and 5% packet loss, with 400 units fighting: one frame in a thousand went from 18.5-21.5 ms to 10.3-12.5, one in a hundred from 13.0-14.8 to 8.3-9.0, and frames over 16.7 ms from 44-95 a match to 6-9. An order reaches the battle and the screen as fast as it did, and a seat with the thread off still agrees with one that has it on. A seat whose drawing takes longer than a step keeps the old order, so a slow graphics card holds the room no lower than before. Network machines compare checksums every 10 steps where they used to compare every step, so nine steps in ten skip a pass over the whole world, and a desync is caught at most 10 steps late.
+- The battle's arithmetic runs on a thread of its own, beside the drawing, on Windows. The game used to work out a step and then draw, one after the other, so a heavy step stalled the picture; now the screen keeps drawing while the next step is worked out, which pays most in the late game, where the steps are heaviest. In a late 4-player match, uncapped, frames slower than 16.7 ms (one 60 Hz refresh) fell from 23% to 0.8%, the slowest one frame in a hundred fell from 24.0 to 16.5 ms live and from 16.0-16.5 to 10.6-12.5 ms in a replay, and the average frame went from 15.2 to 11.8 ms. The battle still takes 60 steps a second, no more and no fewer. A match too heavy for its clock used to pay for the thread: the catch-up steps after the picture ignored the step already run beside it, so each frame ran one step more than without the thread. Eight Hard AIs on Twilight Flame at minute 20, run at four times normal speed so they cannot keep up: one frame in a thousand took 26.8 ms with the thread and 24.0 without. Both now spend the same budget, and with the thread that frame is 21.5 ms and the average frame 11.9 ms against 13.9 without, at 12% more steps a second. No menu or saved setting turns the thread off; `-logicthread 0` stays on the command line to chase a desync, and Mac and Linux keep the old order for now. Network games now draw beside the step as well. Two seats at 240 ms of ping and 5% packet loss, with 400 units fighting: one frame in a thousand went from 18.5-21.5 ms to 10.3-12.5, one in a hundred from 13.0-14.8 to 8.3-9.0, and frames over 16.7 ms from 44-95 a match to 6-9. An order reaches the battle and the screen as fast as it did, and a seat with the thread off still agrees with one that has it on. A seat whose drawing takes longer than a step keeps the old order, so a slow graphics card holds the room no lower than before. Network machines compare checksums every 10 steps where they used to compare every step, so nine steps in ten skip a pass over the whole world, and a desync is caught at most 10 steps late. A step too long for one refresh no longer freezes the screen in single player: the game waits for it as long as a picture can still arrive on time, then draws one more, timed to finish as the step does. Eight Hard AIs and 2000 objects at minute 17, two copies racing on one machine so both met the same noise: one frame in a thousand went from 64.0 to 19.0 ms, frames over 16.7 ms from all of them to 1.6%, and the battle took 29.1 steps a second against 29.7. Those extra pictures move the camera and the effects; units still move once a step, so this smooths a heavy match without making it faster. The game also stopped asking the disc drive for its disc every five seconds during a match, which cost 7 to 11 ms a time on a machine with an empty drive.
- That thread went off after 3.3.0, whose crash reports traced the random crashes in the middle of a match to the drawing and the battle writing to the same memory at the same moment. It is back because those collisions are closed. A model or animation the battle loads for the first time now waits for the picture to finish, the trees a unit pushes through move between frames, the team colours and the list of every object stay put while a picture is drawn, and the shadows thrown on units stop asking the battle where things stand while it is moving them. The test that decided it is a 4-against-4 match of 18000 steps, drawn with the thread on, drawn with it off and run with no picture at all, ending on the same checksum each time with the counter that flags the drawing reading the battle at zero.
- Opening the pause menu no longer makes boats snap to a different angle or parked aircraft sink into the carrier deck. Boats keep their rocking angle and aircraft remain above the deck while the game is paused.
- Replays and games saved in the middle of a match from earlier versions will not load in this one. The "Mission Start" saves a campaign makes between missions do, and every campaign mission can be picked from the campaign menu. A player on an earlier version cannot join a match with a player on this version, whichever of them hosts, so a LAN party needs everyone on the same build.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h b/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h
index dcff298c5..962612f20 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h
@@ -109,6 +109,13 @@ UnsignedInt GameEngine_pictureMilliseconds( void );
/** Whether the catch-up loop may start another logic tick this pass. */
Bool GameEngine_mayStartAnotherCatchupTick( Int ticksSoFar, Int maxTicks, Real elapsedMsInLoop );
+/** Whether one more tick, about as long as the last, leaves this pass's picture inside a 60 Hz frame.
+ A pass already past it is late whatever runs next, so it pays its debt under the budget above. */
+Bool GameEngine_catchupTickFitsPicture( Real passSoFarMS, Real lastTickMS );
+
+/** Milliseconds since this pass, or its last extra picture, began. */
+Real GameEngine_msSincePassStart( void );
+
/** A rate sampled every half second, averaged, and the whole number written on screen for it. */
struct RateReading
{
@@ -186,6 +193,9 @@ public:
* 0 outside a paced single-player pass that planOverlappedTick planned (fast mode, a network game). */
Real msUntilNextTickDue( void );
+ /// That same pass: true when the clock paces it, so a picture beside a late tick costs no network turn.
+ Bool isPassPacedByClock( void );
+
/** What the pacer decided for one pass, see planLogicPass. */
struct LogicPassPlan
{
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
index 77f8f22fb..2871f0f1d 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
@@ -1666,6 +1666,14 @@ Bool GameEngine_mayStartAnotherCatchupTick( Int ticksSoFar, Int maxTicks, Real e
return elapsedMsInLoop < LOGIC_CATCHUP_BUDGET_MS;
}
+/* The debt survives on the accumulator and a pass under 16.7 ms earns less than a tick, so frames that
+ fit drain it on their own; only a pass over the line can build it, and that one is still paid. */
+Bool GameEngine_catchupTickFitsPicture( Real passSoFarMS, Real lastTickMS )
+{
+ const Real PICTURE_MS = 16.7f;
+ return passSoFarMS >= PICTURE_MS || passSoFarMS + lastTickMS * 1.25f < PICTURE_MS;
+}
+
/* R1, smooth motion: when the last logic tick was due and how long a tick lasts, so the renderer can
show models between their last two logic states (W3DSmoothMotion.h). Read, never written, by the
client; nothing here reaches the logic. Set by noteLogicTickDue after each pass's ticks. */
@@ -3090,6 +3098,17 @@ Real GameEngine::msUntilNextTickDue( void )
return 1000.0f / m_maxFPS - s_logicAccumMs - (Real)( Clock_Milliseconds() - s_planTimeMs );
}
+Bool GameEngine::isPassPacedByClock( void )
+{
+ return s_passPlanned && !s_passPlan.fastMode && !s_passPlan.networkPaced && m_maxFPS > 0;
+}
+
+Real GameEngine_msSincePassStart( void )
+{
+ const Int64 now = Clock_Ticks();
+ return engineElapsedMS( thePassStart, now );
+}
+
void GameEngine::update( void )
{
USE_PERF_TIMER(GameEngine_update)
@@ -3223,12 +3242,19 @@ void GameEngine::update( void )
if (overlapped)
LogicThread::takeOverlapTimes( tickBesideRenderMS, waitForTickMS );
const UnsignedInt frameBeforeTicks = overlapped ? s_frameBeforeOverlap : TheGameLogic->getFrame();
+ Real lastTickMS = tickBesideRenderMS;
for( ;; )
{
// TheGameLogic->UPDATE(), on the logic thread at -logicthread 1; the first of an overlapped
// pass has run already, beside the render
if (logicTicksThisPass > 0 || !overlapped)
+ {
+ Int64 tTickStart, tTickEnd;
+ tTickStart = Clock_Ticks();
LogicThread::runTick();
+ tTickEnd = Clock_Ticks();
+ lastTickMS = engineElapsedMS( tTickStart, tTickEnd );
+ }
++logicTicksThisPass;
// -maxframes' frame ends the burst, its CRC logged as it finishes (noteFrameLimit)
@@ -3247,6 +3273,11 @@ void GameEngine::update( void )
if (!GameEngine_mayStartAnotherCatchupTick( logicTicksThisPass, maxTicksThisPass,
tickBesideRenderMS + engineElapsedMS( tCatchupStart, tCatchupNow ) ))
break;
+ /* A match that frames fine and is only a little behind held a frame over 16.7 ms for a tick the
+ next pass could run as well: 43% of the passes on Twilight Flame's minute 20. A network
+ game runs ahead by what it can, or the room falls to this machine's rate. */
+ if (!networkPaced && !GameEngine_catchupTickFitsPicture( engineElapsedMS( thePassStart, tCatchupNow ), lastTickMS ))
+ break;
if (networkPaced)
{
/* The frame just run may have ended the match, and clearGameData takes the network
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/CDManager.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/CDManager.cpp
index 43c363c89..67c1901cb 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/CDManager.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/CDManager.cpp
@@ -192,7 +192,11 @@ void CDManager::init( void )
void CDManager::update( void )
{
- // Every so often, check to make sure the CD is still in the drive
+ /* Every so often, check to make sure the CD is still in the drive. Not in a match: asking an empty
+ optical drive for its volume took 7 to 11 ms on the game thread every five seconds, the 10 ms in
+ the p99.9 of the pass's sync part, and all a disc pulled mid-match costs is the music on it. */
+ if (TheGameLogic->isInGame() && !TheGameLogic->isInShellGame())
+ return;
if ((TheGameLogic->getFrame() % 300) == 299) {
refreshDrives();
}
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/LogicThread.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/LogicThread.cpp
index 851f33e43..d014e32cc 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/LogicThread.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/LogicThread.cpp
@@ -112,6 +112,7 @@ namespace
std::exception_ptr s_overlapError;
LogicClock::time_point s_overlapStart;
Real s_overlapTickMS = 0.0f;
+ Real s_overlapSyncMS = 0.0f; ///< how far into the pass this tick started
Real s_overlapWaitMS = 0.0f;
Int s_overlappedTicks = 0;
Real s_overlapWaitTotalMS = 0.0f;
@@ -319,6 +320,7 @@ void LogicThread::parkRender( void )
if( !s_renderBeside )
return;
s_parkWanted = TRUE;
+ s_doneCv.notify_one(); // the main thread may be in tickOutlastsNextPicture
s_parkCv.wait( lock, []{ return !s_renderBeside; } );
}
@@ -354,6 +356,7 @@ static void startOverlap( void )
TheGameClient->setFrame( TheGameLogic->getFrame() );
TheGameLogic->growObjectLookupForTick();
jitter();
+ s_overlapSyncMS = GameEngine_msSincePassStart();
s_overlapStart = LogicClock::now();
// open before the tick wakes, so nothing the main thread does beside it reads as outside the window
LogicThread::g_windowOpen.store( 1, std::memory_order_relaxed );
@@ -413,6 +416,30 @@ void LogicThread::OverlapWindow::open( Bool mayOverlap )
g_windowOpen.store( 1, std::memory_order_relaxed );
}
+//-------------------------------------------------------------------------------------------------
+/* Behind the clock the next tick is due the whole time, so the rule in closeOrDrawAgain never draws
+ again and the eye waits on the tick: sync + a 13 ms tick is an 18 ms frame on the 2000 object crowd
+ with 4 ms of picture in it. Wait for the tick only as long as the sync point and a picture still
+ fit in a 60 Hz frame after it, then draw once more, timed to end as the last tick's length says this
+ one does, since a picture still going when the tick ends holds the next tick up. A picture that
+ fills the frame by itself (vsync, a slow card) gains nothing from a second one, and under vsync that
+ second one would cost a whole refresh of game speed, so there the tick is simply waited for. */
+static Bool tickOutlastsNextPicture( Real pictureMS )
+{
+ if( !TheGameEngine->isPassPacedByClock() )
+ return FALSE;
+ const Real PICTURE_BUDGET_MS = 15.0f; // 16.7 less the flush and the sync point's slower passes
+ if( pictureMS >= PICTURE_BUDGET_MS )
+ return FALSE;
+ const Real latestStartMS = PICTURE_BUDGET_MS - s_overlapSyncMS - pictureMS;
+ const Real tickLeftMS = s_overlapTickMS - elapsedMS( s_overlapStart, LogicClock::now() );
+ const Real startMS = tickLeftMS <= latestStartMS ? latestStartMS : std::min( latestStartMS, tickLeftMS - pictureMS );
+ // whole microseconds: a float duration added to the steady clock rounds away the wait
+ const std::chrono::microseconds wait( (long long)( std::max( startMS, 0.0f ) * 1000.0f ) );
+ std::unique_lock<std::mutex> lock( s_lock );
+ return !s_doneCv.wait_for( lock, wait, []{ return s_tickDone || s_parkWanted; } );
+}
+
//-------------------------------------------------------------------------------------------------
Bool LogicThread::OverlapWindow::closeOrDrawAgain( Real pictureMS )
{
@@ -426,7 +453,7 @@ Bool LogicThread::OverlapWindow::closeOrDrawAgain( Real pictureMS )
}
// the plan's rule, wait only once the next tick is due, with the next picture's length taken off
// so a picture never holds the next tick up; the last picture stands in for the next one
- if( running && TheGameEngine->msUntilNextTickDue() > pictureMS )
+ if( running && ( TheGameEngine->msUntilNextTickDue() > pictureMS || tickOutlastsNextPicture( pictureMS ) ) )
{
++s_extraPictures;
return TRUE;
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 4190d5610..72942bb0f 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -1606,6 +1606,12 @@ TEST(logic_tick_is_wall_clock_paced_not_render_paced)
CHECK_EQ( GameEngine_logicCatchupMaxFrames( 15 ), 2 );
CHECK_EQ( GameEngine_logicCatchupMaxFrames( 0 ), 1 );
+ /* A tick that fits leaves the picture on time, one that does not waits for the next pass, and a
+ picture already late pays the debt, or a 25 ms render would slow the match to its own rate. */
+ CHECK( GameEngine_catchupTickFitsPicture( 8.0f, 4.0f ) );
+ CHECK( !GameEngine_catchupTickFitsPicture( 10.0f, 7.0f ) );
+ CHECK( GameEngine_catchupTickFitsPicture( 25.0f, 7.0f ) );
+
/* A 300fps renderer (3.33ms/frame) at 30fps logic: 900 render frames span
3 seconds, which must yield 90 logic frames, not 900. */
Real accum = 0.0f;