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(gameclient): upload fog only when written, keep aura leaders in a list

Olcay Seygan91fe97803a86
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2987c156d..042ea0b2d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -459,6 +459,7 @@ found and fixed, EA's own, not port damage.**
- Faster drawing in big battles on the Direct3D 11 renderer: each unit and building draw costs about a quarter less CPU, smoke, fire and tracers go out in about half the draw calls with the translucent pass a third cheaper, and shadows and aura rings laid on the ground take less than half the CPU they did. The picture is identical to the bit.
- Late in big matches each frame does less busywork: the mouse-over check and the visibility sweep look only at what is on screen.
- Late in a match the interface costs less: clocks and readouts reuse their text textures instead of making new ones every frame, leader aura rings and guard shields are worked out once a game frame, and health bars, rank insignia and supply readouts over a crowded screen cost about a quarter less to draw.
+- The interface spends less time each frame on the fog of war, leaders' aura rings and guard shields, so big late-game battles draw faster.
- Late in a match the picture caught less often. Every ten seconds each computer player looked over its economy, its spending and the enemy army all in one step, 8 to 10ms of work landing on a single picture. Those three now run a fifth of a second apart. With four Hard computer players on Twilight Flame at minutes 24 and 25, pictures slower than 16.7ms went from 29 a minute to 22, and the slowest one in a hundred from 14.7ms to 14.0ms. Two more hitches got smaller in the same match. The map of where units can drive is rebuilt whenever a building goes up or comes down, and that rebuild went from 9.9ms to 7.4ms, with the ones over 8ms halved (90 to 43). A Hard computer steering its Particle Cannon beam now weighs the ground around it in 0.9ms instead of 2.6ms. Neither changes a single move: the match ends on the same checksum.
- The game no longer freezes for seconds when your hard drive is busy. It writes a log of the match as it plays, and once a second the battle itself stopped to push that log to disk. On a drive already busy with something else, Windows can hold such a write back for seconds, and the whole game stood still behind it: a 2v2 against the computer froze for 7.8 seconds on a single step. The log is written on the side now and the battle never waits for it. Three matches played while another program kept the drive busy froze for 6.2 seconds in two of them before; with the change, the slowest step in all three took 48ms.
- The game's log no longer grows without limit. Players sent us logs of half a gigabyte up to 19 GB, enough to fill a disk, and the launcher could not open them to report a crash. Most of it was the network code writing a line for every order and every frame, which only a developer's build writes now: a 100-second two-player network match went from 10.9 MB of log to 175 KB, and a skirmish from 600 KB to 166 KB. Past 64 MB the log keeps its first 8 MB, which say how the game started and on what machine, and its most recent lines, so it stays under 64 MB however long you play.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h b/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h
index e2a8115a2..073b105f0 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/Drawable.h
@@ -982,6 +982,9 @@ private:
};
UnsignedByte m_iconModules = 0; ///< IconModule bits, found the first time the icon pass asks
Bool hasIconModule( IconModule module );
+public:
+ Bool hasProductionModule( void ) { return hasIconModule( ICON_MODULE_PRODUCTION ); } ///< the object's; it has one
+private:
static Bool s_staticImagesInited;
static const Image* s_veterancyImage[LEVEL_COUNT];
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index 670b6ebc5..42543bdac 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -1003,6 +1003,7 @@ public: // ********************************************************************
virtual Bool getInputEnabled( void ) { LOGIC_CENSUS( CENSUS_CLIENT_READ ); return m_inputEnabled; } ///< Get the current input status
virtual void disregardDrawable( Drawable *draw ); ///< Drawable is being destroyed, clean up any UI elements associated with it
+ void noteRankChanged( ObjectID id ); ///< after the tick that moved its grade: it may lead now (updateAuraRings)
virtual void preDraw( void ); ///< Logic which needs to occur before the UI renders
virtual void draw( void ) = 0; ///< Render the in-game user interface
@@ -1568,6 +1569,8 @@ protected:
UnsignedInt m_auraRingsFrame = ~0u; ///< the logic frame the rings were last laid for
const Player *m_auraRingsPlayer = NULL; ///< and the local player they were laid for
Int m_auraRingsShroudIndex = -1; ///< and the player whose fog the screen showed
+ std::vector<ObjectID> m_auraLeaders; ///< everything that gave an aura at its grade when last looked at
+ Bool m_auraLeadersFound = FALSE; ///< m_auraLeaders came from a walk over every drawable
//Floating Text Data
FloatingTextList m_floatingTextList; ///< Our list of floating text
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 8fd296a74..311ee85e0 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -672,7 +672,8 @@ void InGameUI::xfer( Xfer *xfer )
// ------------------------------------------------------------------------------------------------
void InGameUI::loadPostProcess( void )
{
-
+ // a loaded unit takes its grade from its experience, with no promotion to tell noteRankChanged
+ m_auraLeadersFound = FALSE;
} // end loadPostProcess
// ------------------------------------------------------------------------------------------------
@@ -1514,6 +1515,39 @@ Bool InGameUI::isTimeFrozenWithoutTick( void ) const
|| TheScriptEngine->isTimeFrozenDebug() || TheScriptEngine->isTimeFrozenScript();
}
+//-------------------------------------------------------------------------------------------------
+/** The aura kinds an object gives at its grade that have a ring to show, into kinds; 0 for one that
+ * leads nobody. */
+//-------------------------------------------------------------------------------------------------
+static Int ringedAuraKinds( const Object *obj, Int kinds[ RANK_AURA_COUNT ] )
+{
+ const RankLadder *ladder = TheGlobalData->getRankLadder( obj->getTemplate() );
+ if( ladder == NULL )
+ return 0;
+ const Int grade = obj->getRankGrade();
+ Int count = 0;
+ for( Int kind = 0; kind < RANK_AURA_COUNT; ++kind )
+ if( ladder->aura( grade, (RankAura)kind ) > 0.0f && TheGlobalData->m_rankAuraRing[ kind ].valid() )
+ kinds[ count++ ] = kind;
+ return count;
+}
+
+//-------------------------------------------------------------------------------------------------
+/** A promotion or a demotion, after its tick. One that now leads joins the leaders updateAuraRings
+ * looks at; one that no longer does leaves there. */
+//-------------------------------------------------------------------------------------------------
+void InGameUI::noteRankChanged( ObjectID id )
+{
+ // before the first walk, or with no rings drawn at all, the walk finds it
+ if( !m_auraLeadersFound )
+ return;
+ const Object *obj = TheGameLogic->findObjectByID( id );
+ Int kinds[ RANK_AURA_COUNT ];
+ if( obj != NULL && ringedAuraKinds( obj, kinds ) > 0
+ && std::find( m_auraLeaders.begin(), m_auraLeaders.end(), id ) == m_auraLeaders.end() )
+ m_auraLeaders.push_back( id );
+}
+
//-------------------------------------------------------------------------------------------------
/** A leader's auras on the ground: one ring for each kind it gives, under every leader on the map,
* selected or not, friend or enemy, while the player at this machine can see it: out of the fog and
@@ -1548,23 +1582,17 @@ void InGameUI::updateAuraRings()
Wanted wanted[ AURA_RING_MAX ];
Int wantedCount = 0;
- for( Drawable *draw = TheGameClient->firstDrawable(); draw != NULL && local != NULL; draw = draw->getNextDrawable() )
+ // the rings a leader puts down, unless the local player cannot see it
+ auto want = [&]( Drawable *draw, const Object *obj )
{
- const Object *obj = draw->getObject();
- const RankLadder *ladder = obj ? TheGlobalData->getRankLadder( obj->getTemplate() ) : NULL;
- if( ladder == NULL )
- continue;
- // the grade first: few units lead, and the fog asks the partition
- const Int grade = obj->getRankGrade();
Int kinds[ RANK_AURA_COUNT ];
- Int kindCount = 0;
- for( Int kind = 0; kind < RANK_AURA_COUNT; ++kind )
- if( ladder->aura( grade, (RankAura)kind ) > 0.0f && TheGlobalData->m_rankAuraRing[ kind ].valid() )
- kinds[ kindCount++ ] = kind;
+ const Int kindCount = ringedAuraKinds( obj, kinds );
if( kindCount == 0 || obj->isEffectivelyDead() || obj->getContainedBy()
|| draw->isDrawableEffectivelyHidden() || draw->getFullyObscuredByShroud()
|| obj->getShroudedStatus( local->getPlayerIndex() ) != OBJECTSHROUD_CLEAR )
- continue;
+ return;
+ const RankLadder *ladder = TheGlobalData->getRankLadder( obj->getTemplate() );
+ const Int grade = obj->getRankGrade();
for( Int k = 0; k < kindCount && wantedCount < AURA_RING_MAX; ++k )
{
const Int kind = kinds[ k ];
@@ -1574,6 +1602,48 @@ void InGameUI::updateAuraRings()
wanted[ wantedCount ].pos = *draw->getPosition();
++wantedCount;
}
+ };
+
+ // Who leads changes only with a grade, and a grade only with a promotion, which noteRankChanged
+ // hears of after its tick; one walk over every drawable finds those already leading. The walk on
+ // every logic frame cost a third of a millisecond late in a match, nearly all of it on units nowhere
+ // near a leader's grade.
+ if( local != NULL )
+ {
+ if( !m_auraLeadersFound )
+ {
+ m_auraLeaders.clear();
+ for( Drawable *draw = TheGameClient->firstDrawable(); draw != NULL; draw = draw->getNextDrawable() )
+ {
+ Int kinds[ RANK_AURA_COUNT ];
+ if( draw->getObject() && ringedAuraKinds( draw->getObject(), kinds ) > 0 )
+ m_auraLeaders.push_back( draw->getObject()->getID() );
+ }
+ m_auraLeadersFound = TRUE;
+ }
+
+ // a leader gone from the game or back below a leader's grade leaves the list
+ static std::vector<Drawable *> leaders;
+ leaders.clear();
+ for( size_t i = 0; i < m_auraLeaders.size(); )
+ {
+ const Object *obj = TheGameLogic->findObjectByID( m_auraLeaders[ i ] );
+ Int kinds[ RANK_AURA_COUNT ];
+ if( obj == NULL || ringedAuraKinds( obj, kinds ) == 0 )
+ {
+ m_auraLeaders[ i ] = m_auraLeaders.back();
+ m_auraLeaders.pop_back();
+ continue;
+ }
+ if( obj->getDrawable() )
+ leaders.push_back( obj->getDrawable() );
+ ++i;
+ }
+ // in the drawable list's order, newest first, as the walk met them, so the slots and the decals
+ // are made in the order they always were
+ std::sort( leaders.begin(), leaders.end(), []( const Drawable *a, const Drawable *b ) { return a->getID() > b->getID(); } );
+ for( size_t i = 0; i < leaders.size(); ++i )
+ want( leaders[ i ], leaders[ i ]->getObject() );
}
// a ring nobody wants goes; one whose radius changed goes too and comes back at the new size
@@ -2841,6 +2911,8 @@ void InGameUI::reset( void )
}
m_auraRingsFrame = ~0u;
m_auraRingsPlayer = NULL;
+ m_auraLeaders.clear();
+ m_auraLeadersFound = FALSE;
m_isQuitMenuVisible = FALSE;
TheObserverCamera.reset();
@@ -9809,7 +9881,11 @@ static struct
static void gatherProductionList( Object *obj, void *userData )
{
ProductionListGather *g = (ProductionListGather *)userData;
- appendProducerQueue( obj, g->slot, g->count, g->total );
+ // the drawable knows whether its object has a production module, where asking the object walked
+ // every module of everything the player owns
+ Drawable *draw = obj->getDrawable();
+ if( draw == NULL || draw->hasProductionModule() )
+ appendProducerQueue( obj, g->slot, g->count, g->total );
appendStructureUnderConstruction( obj, g->slot, g->count, g->total );
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
index f8994de6d..8bf1c15d8 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
@@ -3544,11 +3544,12 @@ void Object::onRankChanged( VeterancyLevel oldLevel, VeterancyLevel newLevel, In
&& Drawable::getRankInsignia( this ) != NULL )
floatRankGain( ladder->insigniaIndex( newGrade, isKindOf( KINDOF_HERO ) ) );
- // the portrait shows the grade, so a selected unit's bar redraws; the selection is the client's,
- // so the client asks after the tick
+ // the portrait shows the grade, so a selected unit's bar redraws, and the aura rings look for a new
+ // leader; the selection and the rings are the client's, so the client asks after the tick
const ObjectID objID = getID();
LogicThread::callAfterTick( [objID]()
{
+ TheInGameUI->noteRankChanged( objID );
Drawable *draw = TheInGameUI->getFirstSelectedDrawable();
if( draw && draw->getObject() && draw->getObject()->getID() == objID )
TheControlBar->markUIDirty();
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DShroud.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DShroud.h
index 2defbea29..63ce5ba1c 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DShroud.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DShroud.h
@@ -129,6 +129,7 @@ protected:
W3DShroudLevel *m_currentFogData; ///<copy of intermediate logical shroud while it's interpolated.
void interpolateFogLevels(RenderRect *rect); ///<fade current fog levels to actual logic side levels.
void fillBorderShroudData(W3DShroudLevel level, SurfaceClass* pDestSurface); ///<fill the destination texture with a known value
+ Bool m_cellsWritten = TRUE; ///<a cell was written since render last copied them up
};
#endif //__W3DSHROUD_H_
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DInGameUI.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DInGameUI.cpp
index b38fa9c68..3951eeb11 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DInGameUI.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DInGameUI.cpp
@@ -1315,6 +1315,26 @@ static void drawShieldShape( Real centreX, Int top, Real width, Int height, Unsi
}
}
+//-------------------------------------------------------------------------------------------------
+/** Player::iterateObjects callback: the spot over the head of one of the local player's guards, into
+ * the vector of them. */
+//-------------------------------------------------------------------------------------------------
+static void addGuardMarker( Object *obj, void *tops )
+{
+ if( !obj->isLocallyControlled() || obj->isEffectivelyDead() || obj->getContainedBy() )
+ return;
+ const AIUpdateInterface *ai = obj->getAI();
+ if( ai == NULL || ai->getCurrentStateID() != AI_GUARD )
+ return;
+ const Drawable *draw = obj->getDrawable();
+ if( draw == NULL || draw->isDrawableEffectivelyHidden() )
+ return;
+
+ Coord3D top = *obj->getPosition();
+ top.z += obj->getGeometryInfo().getMaxHeightAbovePosition();
+ ( (std::vector<Coord3D> *)tops )->push_back( top );
+}
+
//-------------------------------------------------------------------------------------------------
/** A guarding unit looks like an idle one until something walks into its circle, so each of the
* local player's guards wears a small shield over its head, selected or not. It follows the zoom
@@ -1342,10 +1362,10 @@ void W3DInGameUI::drawGuardMarkers( void )
const Int lift = height / 2; // clear of the health bar's line over the model's top
const Real rim = max( 1.0f, height / 10.0f );
- // Who stands guard, and where, moves only with a tick, so the walk over every object runs once a
- // logic frame; it ran on every pass and was most of what this method cost. Only the camera is
- // this pass's, except under frozen time, whose scripts move units with no new frame.
- // ponytail: walks every object once a logic frame; share one walk with another per-object overlay if it shows
+ // Who stands guard, and where, moves only with a tick, so the walk runs once a logic frame; it ran
+ // on every pass and was most of what this method cost. Only the camera is this pass's, except under
+ // frozen time, whose scripts move units with no new frame.
+ // ponytail: two guards or more walk every object for the order; keep the list order if it shows
const UnsignedInt frame = TheGameLogic->getFrame();
const Player *local = ThePlayerList->getLocalPlayer();
if( frame != m_guardMarkersFrame || local != m_guardMarkersPlayer || isTimeFrozenWithoutTick() )
@@ -1353,20 +1373,15 @@ void W3DInGameUI::drawGuardMarkers( void )
m_guardMarkersFrame = frame;
m_guardMarkersPlayer = local;
m_guardMarkerTops.clear();
- for( Object *obj = TheGameLogic->getFirstObject(); obj; obj = obj->getNextObject() )
+ // Only the local player's own objects wear one, and an observer owns none, so his objects are
+ // asked first. Two shields can overlap, and they go down in the order of the list of every
+ // object as they always did, so with two or more that list is walked for the order.
+ local->iterateObjects( addGuardMarker, &m_guardMarkerTops );
+ if( m_guardMarkerTops.size() > 1 )
{
- if( !obj->isLocallyControlled() || obj->isEffectivelyDead() || obj->getContainedBy() )
- continue;
- const AIUpdateInterface *ai = obj->getAI();
- if( ai == NULL || ai->getCurrentStateID() != AI_GUARD )
- continue;
- const Drawable *draw = obj->getDrawable();
- if( draw == NULL || draw->isDrawableEffectivelyHidden() )
- continue;
-
- Coord3D top = *obj->getPosition();
- top.z += obj->getGeometryInfo().getMaxHeightAbovePosition();
- m_guardMarkerTops.push_back( top );
+ m_guardMarkerTops.clear();
+ for( Object *obj = TheGameLogic->getFirstObject(); obj; obj = obj->getNextObject() )
+ addGuardMarker( obj, &m_guardMarkerTops );
}
}
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShroud.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShroud.cpp
index 2fa41d3ba..5bb85a693 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShroud.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShroud.cpp
@@ -204,6 +204,7 @@ void W3DShroud::init(WorldHeightMap *pMap, Real worldCellSizeX, Real worldCellSi
//clear entire texture to black
memset(m_srcTextureData,0,m_srcTexturePitch*srcHeight);
+ m_cellsWritten = TRUE;
#if defined(_DEBUG) || defined(_INTERNAL)
if (TheGlobalData && TheGlobalData->m_fogOfWarOn)
@@ -319,11 +320,13 @@ void W3DShroud::setShroudLevel(Int x, Int y, W3DShroudLevel level, Bool textureO
if (!m_pSrcTexture)
return;
- if (x < m_numCellsX && y < m_numCellsY)
+ if (x >= 0 && y >= 0 && x < m_numCellsX && y < m_numCellsY)
{
if (level < TheGlobalData->m_shroudAlpha)
level = TheGlobalData->m_shroudAlpha;
+ m_cellsWritten = TRUE;
+
#if defined(_DEBUG) || defined(_INTERNAL)
if (TheGlobalData && TheGlobalData->m_fogOfWarOn)
{
@@ -424,8 +427,9 @@ void W3DShroud::fillShroudData(W3DShroudLevel level)
pixel=( ((bluepixel&0xf8) >> 3) | ((greenpixel&0xfc)<<3) | ((redpixel&0xf8)<<8));
}
+ m_cellsWritten = TRUE;
UnsignedShort *ptr=(UnsignedShort *)m_srcTextureData;
- Int pitch = m_srcTexturePitch >> 1; //2 bytes per pointer increment
+ Int pitch = m_srcTexturePitch >> 1; //2 bytes per pointer increment
for (y=0; y<m_numCellsY; y++)
{
for (x=0; x<m_numCellsX; x++)
@@ -718,15 +722,9 @@ void W3DShroud::render(CameraClass *cam)
m_pDstTexture->Get_Filter().Set_Min_Filter(m_shroudFilter);
}
- //Update video memory texture with sysmem copy
- SurfaceClass* pDestSurface;
- {
- pDestSurface=m_pDstTexture->Get_Surface_Level(0);
- }
-
RenderRect srcRect;
RenderPoint dstPoint={1,1}; //first row/column is reserved for border.
-
+
srcRect.left=visStartX;
srcRect.top=visStartY;
srcRect.right=visEndX;
@@ -735,13 +733,27 @@ void W3DShroud::render(CameraClass *cam)
#ifdef DO_FOG_INTERPOLATION
//interpolate current shroud state to the final one
interpolateFogLevels(&srcRect);
+ m_cellsWritten = TRUE;
#endif
+ // Nothing goes up while no cell was written since the last copy: the fog was a D3D9 surface update
+ // and a Direct3D 11 compare of every row on every pass, and an observer's never changes. A written
+ // pass still copies every cell, so the Direct3D 11 row compare sends only the rows that changed.
+ if (!m_cellsWritten && !m_clearDstTexture)
+ return;
+ m_cellsWritten = FALSE;
+
+ //Update video memory texture with sysmem copy
+ SurfaceClass* pDestSurface;
+ {
+ pDestSurface=m_pDstTexture->Get_Surface_Level(0);
+ }
+
if (m_clearDstTexture)
{ //we need to clear unused parts of the destination texture to a known
//color in order to keep map border in the state we want.
m_clearDstTexture=FALSE;
-
+
fillBorderShroudData(m_boderShroudLevel, pDestSurface);
}