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): preload match assets during the loading screen

Olcay Seygan8e68f7b12657
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 107c1b570..8f9a30921 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -875,6 +875,8 @@ found and fixed — EA's own, not port damage.**
- The rest of the same mob. A mob is a leader and ten rioters, and a rioter only reconsiders where it is standing once every sixteen frames - but it was woken on all sixteen anyway, just to be sent back to sleep, and every mob on the map is eleven of those. EA left a note in the code asking for exactly this and never came back to it. The rioters sleep properly now, and one whose mob is dead stops being woken at all. Eight mobs on the field is eighty rioters: where all eighty were woken every frame, five are. Measured against the number in the complaint rather than a comfortable one: a hundred mobs is 1,100 men and costs 4.3 milliseconds of a 33 millisecond frame, two hundred and fifty is 2,750 men and 7.1, four hundred is 4,400 men and 12.6, and not one frame of any of those three ran over budget. Four hundred mobs is past anything a match will produce; what the rioters cost there is a fifth of a millisecond.
+- The first soldier out of your barracks cost eighteen milliseconds of a thirty-three millisecond frame. A unit's models were read off the disc the moment one of its kind first appeared, and that read landed on whatever frame the unit happened to walk out on. So did the first war factory, the first supply centre, the first of everything. That is why the opening of a match was the part that jerked: the opening of a match is nothing but things appearing for the first time. Everything a match can put on the field is now read during the loading screen, which costs about three seconds there and takes the hitches out of the game. EA wrote that loading pass and left it switched off; the code carries their own comment saying it exists "so that we don't have big pauses when building those objects".
+
## Sound, video, and getting it to start at all
- Audio is real, through the audio library the retail game ships with.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
index 2e54e43a6..c9b32eba7 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
@@ -200,7 +200,6 @@ public:
Real m_stealthFriendlyOpacity;
UnsignedInt m_defaultOcclusionDelay; ///<time to delay building occlusion after object is created.
- Bool m_preloadAssets;
Bool m_preloadEverything; ///< Preload everything, everywhere (for debugging only)
Bool m_preloadReport; ///< dump a log of all W3D assets that are being preloaded.
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
index 9f8ce0b5b..6e7c9416c 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
@@ -730,17 +730,7 @@ Int parseBuildMapCache(char *args[], int)
}
-#if defined(_DEBUG) || defined(_INTERNAL) || defined(_ALLOW_DEBUG_CHEATS_IN_RELEASE)
-Int parsePreload( char *args[], int num )
-{
- if( TheWritableGlobalData )
- TheWritableGlobalData->m_preloadAssets = TRUE;
- return 1;
-}
-#endif
-
-
-#if defined(_DEBUG) || defined(_INTERNAL)
+#if defined(_DEBUG) || defined(_INTERNAL)
Int parseDisplayDebug(char *args[], int)
{
if (TheWritableGlobalData)
@@ -764,10 +754,7 @@ Int parseFile(char *args[], int num)
Int parsePreloadEverything( char *args[], int num )
{
if( TheWritableGlobalData )
- {
- TheWritableGlobalData->m_preloadAssets = TRUE;
TheWritableGlobalData->m_preloadEverything = TRUE;
- }
return 1;
}
@@ -2049,8 +2036,6 @@ static CommandLineParam params[] =
{ "-displayDebug", parseDisplayDebug },
{ "-file", parseFile },
-// { "-preload", parsePreload },
-
{ "-preloadEverything", parsePreloadEverything },
{ "-logAssets", parseLogAssets },
{ "-netMinPlayers", parseNetMinPlayers },
@@ -2132,9 +2117,6 @@ static CommandLineParam params[] =
//-allAdvice feature
//{ "-allAdvice", parseAllAdvice },
-#if defined(_DEBUG) || defined(_INTERNAL) || defined(_ALLOW_DEBUG_CHEATS_IN_RELEASE)
- { "-preload", parsePreload },
-#endif
};
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
index e972c1dc4..15a59728b 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
@@ -1334,6 +1334,16 @@ static Bool theFrameTimesStarted = FALSE;
void GameEngine_noteFrameTime( Real ms, UnsignedInt logicFrame )
{
+ /* The histogram deliberately ignores the opening second of a match, and the opening second of a
+ match is exactly where a player feels the game stutter. So every long pass gets a line of its
+ own from the first frame onwards, whether or not the histogram has started. Read it beside
+ SLOW LOGIC FRAME: a long pass with no logic line under it was spent on the client side. */
+ const Real SLOW_PASS_MS = 25.0f;
+ if( ms > SLOW_PASS_MS && TheGameLogic && TheGameLogic->isInGame() && !TheGameLogic->isInShellGame() )
+ {
+ DEBUG_LOG(("SLOW PASS frame %d: %.1f ms\n", logicFrame, ms));
+ }
+
if( !theFrameTimesStarted )
return;
theFrameTimes.note( ms, logicFrame );
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
index e01ae4a1e..a1a31a7e5 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
@@ -924,7 +924,6 @@ GlobalData::GlobalData()
m_stealthFriendlyOpacity = 0.5f;
m_defaultOcclusionDelay = LOGICFRAMES_PER_SECOND * 3; //default to 3 seconds
- m_preloadAssets = FALSE;
m_preloadEverything = FALSE;
m_preloadReport = FALSE;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp
index a23298f67..fd3591f96 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp
@@ -1279,11 +1279,15 @@ void GameClient::preloadAssets( TimeOfDay timeOfDay )
//
AsciiString side;
const ThingTemplate *tTemplate;
+ Int preloaded = 0, considered = 0;
+ Int64 preloadStart, preloadEnd, preloadFreq = 0;
+ QueryPerformanceCounter( (LARGE_INTEGER *)&preloadStart );
for( tTemplate = TheThingFactory->firstTemplate();
tTemplate;
tTemplate = tTemplate->friend_getNextTemplate() )
{
-
+ ++considered;
+
// if this isn't one of the objects that can be preloaded ignore it
if( tTemplate->isKindOf( KINDOF_PRELOAD ) == FALSE && !TheGlobalData->m_preloadEverything )
continue;
@@ -1292,6 +1296,7 @@ void GameClient::preloadAssets( TimeOfDay timeOfDay )
draw = TheThingFactory->newDrawable( tTemplate );
if( draw )
{
+ ++preloaded;
// preload the assets
draw->preloadAssets( timeOfDay );
@@ -1302,6 +1307,10 @@ void GameClient::preloadAssets( TimeOfDay timeOfDay )
} // end if
} // end for
+ QueryPerformanceCounter( (LARGE_INTEGER *)&preloadEnd );
+ QueryPerformanceFrequency( (LARGE_INTEGER *)&preloadFreq );
+ DEBUG_LOG(("PRELOAD: %d of %d templates in %.0f ms\n", preloaded, considered,
+ preloadFreq ? (Real)((double)(preloadEnd - preloadStart) * 1000.0 / (double)preloadFreq) : 0.0f));
GlobalMemoryStatus(&after);
DEBUG_LOG(("Preloading memory dwAvailPageFile %d --> %d : %d\n",
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/ObjectCreationList.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/ObjectCreationList.cpp
index 6b81868c6..01cc3814b 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/ObjectCreationList.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/ObjectCreationList.cpp
@@ -1442,8 +1442,8 @@ protected:
GenericObjectCreationNugget* debrisNugget = (GenericObjectCreationNugget*)instance;
for (const char* debrisName = ini->getNextToken(); debrisName; debrisName = ini->getNextTokenOrNull())
{
- if (TheGlobalData->m_preloadAssets)
- debrisModelNamesGlobalHack.push_back(debrisName);
+ // the preload always runs now, so this list is always the one it reads debris off
+ debrisModelNamesGlobalHack.push_back(debrisName);
debrisNugget->m_names.push_back(AsciiString(debrisName));
// (the for-increment already advances; advancing here too dropped every second name)
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
index 88f51cabe..444f9017b 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
@@ -2068,7 +2068,13 @@ void GameLogic::startNewGame( Bool loadingSaveGame )
// will build and various damage states for all the structures on the map so that we
// don't have big pauses when building those objects or switching to those states
//
- if( TheGlobalData->m_preloadAssets )
+ // EA wrote that and left it behind a flag that no shipping build could set, so the game read
+ // a unit's models on whichever logic frame the first one of its kind appeared: 18ms of a 33ms
+ // frame for the first Ranger out of a barracks, and the opening of a match is nothing but
+ // first appearances. It is not a flag any more. The shell map is the one exception - it
+ // builds none of this and would only pay the loading second for nothing.
+ //
+ if( !isInShellGame() )
{
if (TheGlobalData->m_preloadEverything)
{
@@ -3795,6 +3801,7 @@ enum { MODULE_PROFILE_MAX = 96 };
static NameKeyType theModuleKindKey[ MODULE_PROFILE_MAX ];
static Int64 theModuleKindTicks[ MODULE_PROFILE_MAX ];
static Int theModuleKindCount[ MODULE_PROFILE_MAX ];
+static Int theModuleKindQueries[ MODULE_PROFILE_MAX ];
static Int theModuleKindsUsed = 0;
static void resetModuleProfile( void )
@@ -3806,7 +3813,7 @@ static void resetModuleProfile( void )
theModuleKindsUsed = 0;
}
-static void addModuleProfile( NameKeyType key, Int64 ticks )
+static void addModuleProfile( NameKeyType key, Int64 ticks, Int queries )
{
for( Int at = 0; at < theModuleKindsUsed; at++ )
{
@@ -3814,6 +3821,7 @@ static void addModuleProfile( NameKeyType key, Int64 ticks )
{
theModuleKindTicks[ at ] += ticks;
theModuleKindCount[ at ]++;
+ theModuleKindQueries[ at ] += queries;
return;
}
}
@@ -3824,9 +3832,54 @@ static void addModuleProfile( NameKeyType key, Int64 ticks )
theModuleKindKey[ theModuleKindsUsed ] = key;
theModuleKindTicks[ theModuleKindsUsed ] = ticks;
theModuleKindCount[ theModuleKindsUsed ] = 1;
+ theModuleKindQueries[ theModuleKindsUsed ] = queries;
theModuleKindsUsed++;
}
+//
+// Who asks the partition manager for a range query. THREADING-ROADMAP counted 117 of them a logic
+// frame and could name 17 - the units looking for something to shoot. A sampling profile cannot
+// name the other hundred, because they all end up inside the same getClosestObjects. Attributing
+// each one to the module that was updating when it happened does name them, and the count is a
+// static int read either side of a call that already carries two QueryPerformanceCounter reads.
+//
+static const char *getModuleQueryReport( void )
+{
+ static char report[ 256 ];
+ Bool taken[ MODULE_PROFILE_MAX ];
+
+ Int used = snprintf( report, ARRAY_SIZE(report), "queries by module:" );
+ if( used < 0 || used >= (Int)ARRAY_SIZE(report) )
+ return report;
+
+ for( Int at = 0; at < theModuleKindsUsed; at++ )
+ taken[ at ] = FALSE;
+
+ for( Int rank = 0; rank < 5; rank++ )
+ {
+ Int best = -1;
+ for( Int at = 0; at < theModuleKindsUsed; at++ )
+ if( !taken[ at ] && theModuleKindQueries[ at ] > 0
+ && ( best < 0 || theModuleKindQueries[ at ] > theModuleKindQueries[ best ] ) )
+ best = at;
+
+ if( best < 0 )
+ break;
+
+ const Int room = (Int)ARRAY_SIZE(report) - used;
+ const Int wrote = snprintf( report + used, room, " %s %dq/%dx",
+ TheNameKeyGenerator->keyToName( theModuleKindKey[ best ] ).str(),
+ theModuleKindQueries[ best ], theModuleKindCount[ best ] );
+ if( wrote < 0 || wrote >= room )
+ break;
+ used += wrote;
+
+ taken[ best ] = TRUE;
+ }
+
+ return report;
+}
+
static const char *getModuleProfileReport( void )
{
static char report[ 512 ];
@@ -4421,6 +4474,7 @@ void GameLogic::update( void )
const NameKeyType profModuleKey = u->getModuleNameKey();
const ThingTemplate *profModuleThing = u->friend_getObject() ? u->friend_getObject()->getTemplate() : NULL;
const ObjectID profModuleObjID = u->friend_getObject() ? u->friend_getObject()->getID() : INVALID_ID;
+ const Int profModuleQueriesBefore = PartitionManager::getQueryCountThisFrame();
Int64 profModuleStart;
QueryPerformanceCounter( (LARGE_INTEGER *)&profModuleStart );
#endif
@@ -4433,7 +4487,8 @@ void GameLogic::update( void )
QueryPerformanceCounter( (LARGE_INTEGER *)&profModuleEnd );
theModuleUpdateCount++;
const Int64 profModuleTicks = profModuleEnd - profModuleStart;
- addModuleProfile( profModuleKey, profModuleTicks );
+ addModuleProfile( profModuleKey, profModuleTicks,
+ PartitionManager::getQueryCountThisFrame() - profModuleQueriesBefore );
if( profModuleTicks > theWorstModuleTicks )
{
theWorstModuleTicks = profModuleTicks;
@@ -4560,7 +4615,7 @@ void GameLogic::update( void )
const Real stores = logicElapsedMS( tCommandList, tStores );
const Real victory = logicElapsedMS( tStores, tVictory );
const Real disabled = logicElapsedMS( tVictory, tFrameEnd );
- DEBUG_LOG(("SLOW LOGIC FRAME %d: %.1fms | scripts %.1f | objects %.1f | ai %.1f (pathfind %.1f, players %.1f) | partition %.1f (%d queries, %d gathers, %d target scans, %d objects) | rest %.1f (destroy %.1f, cmdlist %.1f, stores %.1f, victory %.1f, disabled %.1f)\n sc: %s\n ob: %s\n pf: %s\n ai: %s\n",
+ DEBUG_LOG(("SLOW LOGIC FRAME %d: %.1fms | scripts %.1f | objects %.1f | ai %.1f (pathfind %.1f, players %.1f) | partition %.1f (%d queries, %d gathers, %d target scans, %d objects) | rest %.1f (destroy %.1f, cmdlist %.1f, stores %.1f, victory %.1f, disabled %.1f)\n sc: %s\n ob: %s\n qy: %s\n pf: %s\n ai: %s\n",
now, total, scripts, objects, ai, AI::getLastPathfindMS(), AI::getLastPlayerUpdateMS(), partition,
PartitionManager::getQueryCountThisFrame(), PartitionManager::getGatherCountThisFrame(),
AI::getEnemyScanCountThisFrame(),
@@ -4568,6 +4623,7 @@ void GameLogic::update( void )
destroy, cmdlist, stores, victory, disabled,
TheScriptEngine->getProfileReport(),
getModuleProfileReport(),
+ getModuleQueryReport(),
Pathfinder::getProfileReport(),
AIPlayer::getProfileReport()));
}
@@ -4908,7 +4964,23 @@ UnsignedInt GameLogic::getCRC( Int mode, AsciiString deepCRCFileName )
// ------------------------------------------------------------------------------------------------
void GameLogic::sendObjectCreated( Object *obj )
{
+ /* The first drawable of a template loads that template's models, and with the assets on demand
+ that load lands on whichever logic frame the unit happens to appear on: the first Ranger out
+ of a barracks cost 18ms of a 33ms frame. The preload in startNewGame is what keeps this
+ quiet; a line here is one that got past it, which is the only way anybody would notice. */
+ Int64 firstDrawStart, firstDrawEnd, firstDrawFreq = 0;
+ QueryPerformanceCounter( (LARGE_INTEGER *)&firstDrawStart );
Drawable *draw = TheThingFactory->newDrawable(obj->getTemplate());
+ QueryPerformanceCounter( (LARGE_INTEGER *)&firstDrawEnd );
+ QueryPerformanceFrequency( (LARGE_INTEGER *)&firstDrawFreq );
+ {
+ const Real ASSET_LOAD_WORTH_LOGGING_MS = 3.0f;
+ const Real drawMS = firstDrawFreq
+ ? (Real)((double)(firstDrawEnd - firstDrawStart) * 1000.0 / (double)firstDrawFreq) : 0.0f;
+ if( drawMS > ASSET_LOAD_WORTH_LOGGING_MS )
+ DEBUG_LOG(("SLOW DRAWABLE frame %d: %s took %.1f ms to build\n", m_frame,
+ obj->getTemplate()->getName().str(), drawMS));
+ }
/// @todo COLIN ... shouldn't we have a check here for existing drawable!!!!!