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): stop laying the command bar page out every frame

Olcay Seygane47d7b1db48b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 41949684e..3140d4dee 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@ found and fixed — EA's own, not port damage.**
- A slow moment costs you a dropped frame, not a slow game.
- Explosions no longer catch the picture. The first time each kind of fire, smoke or blast reached the screen, the game stopped for 25 to 60ms to prepare it, and the opening volley of a fight cost 300ms at once. That work is kept on disk and ships with the game, so it happens while the map loads, in about 3ms, and the worst frame of the same battle went from 141ms to 38ms.
- The price, build time and power figures on the command bar buttons stopped costing you frames. All the buttons shared one piece of text, so every figure on the bar was lettered from scratch every frame, about 33 of them. Each now keeps its own: a battle that averaged 14ms a frame averages 11.7ms, frames slower than 60 a second went from one in six to one in a hundred, and the old renderer gets about 0.8ms back too. The money readout was lettered again every frame as well, for a number that had not changed; it is lettered when the number moves. Over a whole battle the game now letters 163 pieces of text where it lettered 68,227.
+- The new command bar stopped eating half the frame. The clock and the frame counter in the top right corner sat inside the bar's own page, so every tick of them had the whole bar laid out again from scratch, 15ms a time and thirty times a second. The corner box is a page of its own now, the scoreboard counts again twice a second instead of every frame, and a click on the battlefield no longer lays the bar out twice. In the same four-player battle, almost half the frames used to miss 60 a second; now fewer than one in four hundred do, and the game draws more than twice as many frames.
- A screen full of fire and smoke is cheaper to draw. Thirty Inferno Cannons shelling twenty Crusaders used to send the graphics card 459 separate batches of see-through effects a frame, mostly the same texture again and again, and the game looked up the same seven card settings from scratch for every one of them. Matching effects go out together now, and settings that have not changed are not looked up again: 177 batches, and the same battle went from 11.6ms a frame to 8.6ms. It is at 6.7ms now, since the game stopped preparing everything it draws a second time for a copy of the picture nobody sees. Nothing on screen changes, the effects are drawn in exactly the order they were.
- The same battle with the particle limit turned up costs 7.6ms a frame where it cost 9.1ms, and the old renderer went from 9.3ms to 6.3ms. Both were keeping a count of every kind of draw the game makes, and both did it by writing out and reading back a description of each one, on every draw, for a report no player ever opens. They glance at one draw in sixteen now. The new renderer also stopped re-sending the graphics card settings it had just sent.
- Raise the particle limit as far as you like and every puff gets drawn. Past about sixteen thousand smoke and fire sprites on screen the game quietly stopped drawing the rest: a test with 68,000 of them showed barely a fifth. A test with 101,000 now draws all of them at 8.3ms a frame, and in 1,200 frames not one fell below 60 a second. The first version that drew them all took 22.4ms; the rest came from sorting and copying them far less and from sharing the work across every core of the processor.
diff --git a/GeneralsMD/Code/Data/Window/Html/ControlBar.html b/GeneralsMD/Code/Data/Window/Html/ControlBar.html
index 2757708dd..ce9a24a74 100644
--- a/GeneralsMD/Code/Data/Window/Html/ControlBar.html
+++ b/GeneralsMD/Code/Data/Window/Html/ControlBar.html
@@ -46,11 +46,10 @@
{{signal0.drop}} to {{signal2.drop}} how far below its place each button
still is while they rise into the column
url(Name.svg) an SVG file under Window/Html, fitted whole into the box and centred
- {{net.clock}} ... the network box: net.clock, net.game, net.real, net.hz, net.fps,
- net.renderer and net.frame always; net.ready, net.runahead, net.room,
- net.in and net.out with {{net.online}} "online" in a network game;
- net.units and net.cap with {{net.capped}} "capped" under a unit limit
- {{blink}} "lit" for half of every second, for anything that blinks
+ {{blink}} "lit" for half of every second while the general has a promotion to
+ spend, the one thing that blinks: a value that changes lays the whole
+ page out again. The network box in the top right corner is
+ Window/Html/Net.html for the same reason
{{panelN.x}} .y .w .h N 0 to 2, left, centre and right: where the plates would have gone,
moving with the bar when it slides or minimises; .w and .h are 0 and
{{panelN.shown}} is "hidden" when a minimised bar has stood it down
@@ -326,16 +325,6 @@
.signal.attack .icon { background-image: url(Icons/SignalAttack.svg); }
.signal.defend .icon { background-image: url(Icons/SignalDefend.svg); }
.signal.look .icon { background-image: url(Icons/SignalLook.svg); }
-
- /* the network box: everything the corner readout had, flush in the screen's top right corner */
- #net { right: 0; top: 0; padding: 3px 6px 3px 8px; border-right: 0; font-size: 7px; color: #c9d2d9; text-align: right; }
- #net .line { line-height: 10px; white-space: nowrap; }
- #net .big { font-size: 9px; font-weight: bold; color: #e8d27a; }
- #net .dim { color: #7d8a93; }
- #net .onlineonly { display: none; }
- #net.online .onlineonly { display: block; }
- #net .cappedonly { display: none; }
- #net .cappedonly.capped { display: block; }
</style>
</head>
<body>
@@ -421,13 +410,6 @@
<div class="bar" style="top: 10px"></div>
</div>
- <div id="net" class="piece panel {{net.online}}">
- <div class="line"><span class="big">{{net.clock}}</span> {{net.game}} <span class="dim">({{net.real}})</span></div>
- <div class="line">{{net.hz}}hz {{net.fps}}fps <span class="dim">{{net.renderer}} frame {{net.frame}}</span></div>
- <div class="line onlineonly">{{text:GUI:BarNetReady}} {{net.ready}}/{{net.runahead}} {{text:GUI:BarNetRoom}} {{net.room}}fps <span class="dim">&#8595;{{net.in}} &#8593;{{net.out}} KB/s</span></div>
- <div class="line cappedonly {{net.capped}}">{{net.units}}/{{net.cap}} {{text:GUI:BarNetUnits}}</div>
- </div>
-
</div>
</body>
</html>
diff --git a/GeneralsMD/Code/Data/Window/Html/Net.html b/GeneralsMD/Code/Data/Window/Html/Net.html
new file mode 100644
index 000000000..ee386590f
--- /dev/null
+++ b/GeneralsMD/Code/Data/Window/Html/Net.html
@@ -0,0 +1,53 @@
+<!DOCTYPE html>
+<!--
+ The network box in the screen's top right corner, drawn with Window/Html/ControlBar.html and only
+ while that page is up. It is a page of its own because its clock and rates change several times a
+ second, and a page is laid out again from its text every time any value in it changes: inside the
+ command bar's page each of those changes laid out the whole bar, fifteen milliseconds a time.
+
+ The game fills in, every frame (GameClient/HtmlTemplate.h):
+ {{side}} "america", "china" or "gla": the side whose bar is on screen
+ {{net.clock}} ... net.clock, net.game, net.real, net.hz, net.fps, net.renderer and
+ net.frame always, net.frame as of the last rate reading; net.ready,
+ net.runahead, net.room, net.in and net.out with {{net.online}} "online"
+ in a network game; net.units and net.cap with {{net.capped}} "capped"
+ under a unit limit
+ All in the page's pixels, a pixel of an 800x600 screen.
+
+ Edit the copy under Run/Window/Html and the next match reads it.
+-->
+<html>
+<head>
+<meta charset="utf-8">
+<title>Network box</title>
+<style>
+ html, body { margin: 0; padding: 0; }
+ body { background-color: #3b4a36; font-size: 8px; color: #dfe5ea; }
+
+ /* ControlBar.html's panel: a dark body under a steel rail, lit along the top and left */
+ .panel { background-color: rgba(10, 16, 20, 0.94); border-top: 1px solid #aab4bf; border-left: 1px solid #8a949f;
+ border-right: 1px solid #1a2026; }
+ .china .panel { background-color: rgba(24, 9, 7, 0.94); border-top-color: #d98a72; border-left-color: #b8604b; }
+ .gla .panel { background-color: rgba(21, 18, 8, 0.94); border-top-color: #cbbf82; border-left-color: #a99d62; }
+
+ #net { position: absolute; right: 0; top: 0; padding: 3px 6px 3px 8px; border-right: 0; font-size: 7px; color: #c9d2d9; text-align: right; }
+ #net .line { line-height: 10px; white-space: nowrap; }
+ #net .big { font-size: 9px; font-weight: bold; color: #e8d27a; }
+ #net .dim { color: #7d8a93; }
+ #net .onlineonly { display: none; }
+ #net.online .onlineonly { display: block; }
+ #net .cappedonly { display: none; }
+ #net .cappedonly.capped { display: block; }
+</style>
+</head>
+<body>
+<div class="{{side}}">
+ <div id="net" class="panel {{net.online}}">
+ <div class="line"><span class="big">{{net.clock}}</span> {{net.game}} <span class="dim">({{net.real}})</span></div>
+ <div class="line">{{net.hz}}hz {{net.fps}}fps <span class="dim">{{net.renderer}} frame {{net.frame}}</span></div>
+ <div class="line onlineonly">{{text:GUI:BarNetReady}} {{net.ready}}/{{net.runahead}} {{text:GUI:BarNetRoom}} {{net.room}}fps <span class="dim">&#8595;{{net.in}} &#8593;{{net.out}} KB/s</span></div>
+ <div class="line cappedonly {{net.capped}}">{{net.units}}/{{net.cap}} {{text:GUI:BarNetUnits}}</div>
+ </div>
+</div>
+</body>
+</html>
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index 7ecf5b540..3b63e3c92 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -1211,6 +1211,8 @@ protected:
///< the run of cells, a column, with its left edge at 'left' and its first cell's top edge at 'bottomY'
void drawProductionStripColumn( Int left, Int bottomY );
void drawQueueTray( void ); ///< the playing strip as a row in Window/Html/Queue.html's tray
+ void drawNetPage( void ); ///< the network box, Window/Html/Net.html, with the command bar's page
+ std::string scoreboardHtml( void ); ///< Window/Html/Scoreboard.html filled in for this frame
const Image *productionStripTray( void ); ///< the bar's tray, mirrored, kept until the bar changes side
void stripTrayMetrics( ICoord2D *tray, ICoord2D *cameo, ICoord2D *hole, Int *step ); ///< that tray's size, its cameo hole, and the column step
void drawStripSeconds( Int which, Int x, Int y, Int w, Int h, Int seconds ); ///< countdown written inside a cameo
@@ -1227,6 +1229,8 @@ protected:
HtmlOverlay * m_scoreboardOverlay;
Bool m_scoreboardPageLoaded; ///< read once a match, like the spectator's page
std::string m_scoreboardPage;
+ std::string m_scoreboardHtml; ///< the page filled in, kept for NET_WORTH_REFRESH_FRAMES
+ UnsignedInt m_scoreboardHtmlFrame; ///< the logic frame it was filled in on
// The last half minute of every player's money earned, one reading a game second, oldest first, so
// a seat can say what it earns now beside its average over the match. The reading's own second
// goes with it: a pass that runs several logic frames at once skips seconds.
@@ -1246,6 +1250,10 @@ protected:
Bool m_controlBarPageLoaded;
Bool m_controlBarPageShown; ///< drawn this frame, so its buttons can be clicked
std::string m_controlBarPage;
+ Bool m_controlBarPageHovered; ///< the pointer was on something the page drew, last frame
+ HtmlOverlay * m_netOverlay;
+ Bool m_netPageLoaded;
+ std::string m_netPage;
HtmlOverlay * m_tooltipOverlay;
Bool m_tooltipPageLoaded;
std::string m_tooltipPage;
@@ -1329,7 +1337,7 @@ protected:
void drawBlindSpots( void ); ///< shade the ground a placed or selected defence cannot shoot into
DisplayString * m_hudDisplayString; ///< the ShowHudOverlay line (fps / clock)
- HtmlValues m_hudValues; ///< that line's readings one by one, for the command bar page's network box
+ HtmlValues m_hudValues; ///< that line's readings one by one, for Window/Html/Net.html
DisplayString * m_peaceTimeDisplayString; ///< the peace time clock at the top of the screen
DisplayString * m_peaceTimeLabelDisplayString; ///< the word written over that clock
DisplayString * m_peaceCountdownDisplayString; ///< the big digit of its last ten seconds
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index c4da2c5a4..3cbfdb700 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -1218,9 +1218,13 @@ InGameUI::InGameUI()
m_scoreboardOpen = FALSE;
m_scoreboardOverlay = NULL;
m_scoreboardPageLoaded = FALSE;
+ m_scoreboardHtmlFrame = 0;
m_earnedReadingCount = 0;
m_controlBarOverlay = NULL;
m_controlBarPageLoaded = FALSE;
+ m_controlBarPageHovered = FALSE;
+ m_netOverlay = NULL;
+ m_netPageLoaded = FALSE;
m_promotionOverlay = NULL;
m_promotionFrontOverlay = NULL;
for( Int grid = 0; grid < CELL_GRID_COUNT; grid++ )
@@ -1362,6 +1366,8 @@ InGameUI::~InGameUI()
m_scoreboardOverlay = NULL;
delete m_controlBarOverlay;
m_controlBarOverlay = NULL;
+ delete m_netOverlay;
+ m_netOverlay = NULL;
delete m_promotionOverlay;
m_promotionOverlay = NULL;
delete m_promotionFrontOverlay;
@@ -3762,8 +3768,10 @@ void InGameUI::reset( void )
m_isQuitMenuVisible = FALSE;
m_scoreboardOpen = FALSE;
m_scoreboardPageLoaded = FALSE;
+ m_scoreboardHtml.clear();
m_earnedReadingCount = 0; // a new match and a loaded save both come through here
m_controlBarPageLoaded = FALSE;
+ m_netPageLoaded = FALSE;
m_tooltipPageLoaded = FALSE;
m_promotionPageLoaded = FALSE;
m_quitMenuPageLoaded = FALSE;
@@ -9304,7 +9312,8 @@ void InGameUI::drawHudOverlay( void )
m_hudValues[ "net.hz" ] = std::to_string( m_hudLogicHz.shown );
m_hudValues[ "net.fps" ] = std::to_string( m_hudFps.shown );
m_hudValues[ "net.renderer" ] = WideCharStringToMultiByte( TheDisplay->getRendererName() );
- m_hudValues[ "net.frame" ] = std::to_string( logicFrame );
+ // as of the last rate reading, twice a second: every frame laid the box out thirty times a second
+ m_hudValues[ "net.frame" ] = std::to_string( m_hudLastSampleLogicFrame );
if( TheNetwork != NULL )
{
enum { BYTES_PER_KILOBYTE = 1024 };
@@ -10443,6 +10452,23 @@ void InGameUI::drawScoreboard( void )
if( m_scoreboardOverlay == NULL )
m_scoreboardOverlay = new HtmlOverlay( m_superweaponNormalFont );
+ // counted again twice a second, as the spectator page is: the money moves every frame, and every
+ // change lays the whole page out again
+ const UnsignedInt frame = TheGameLogic->getFrame();
+ if( m_scoreboardHtml.empty() || frame < m_scoreboardHtmlFrame || frame >= m_scoreboardHtmlFrame + NET_WORTH_REFRESH_FRAMES )
+ {
+ m_scoreboardHtml = scoreboardHtml();
+ m_scoreboardHtmlFrame = frame;
+ }
+ m_scoreboardOverlay->setPage( m_scoreboardHtml );
+ m_scoreboardOverlay->draw();
+}
+
+//-------------------------------------------------------------------------------------------------
+/** Window/Html/Scoreboard.html filled in with every seat as it stands this frame. */
+//-------------------------------------------------------------------------------------------------
+std::string InGameUI::scoreboardHtml( void )
+{
Player *local = ThePlayerList->getLocalPlayer();
const Bool watching = localPlayerWatching();
@@ -10516,8 +10542,7 @@ void InGameUI::drawScoreboard( void )
HtmlValues values;
values[ "side" ] = spectatorSide();
values[ "clock" ] = spectatorClock( TheGameLogic->getFrame() );
- m_scoreboardOverlay->setPage( HtmlTemplate_expand( m_scoreboardPage, values, lists, lookupGameText ) );
- m_scoreboardOverlay->draw();
+ return HtmlTemplate_expand( m_scoreboardPage, values, lists, lookupGameText );
}
static const char *const CONTROL_BAR_PAGE = "Window\\Html\\ControlBar.html";
@@ -11151,6 +11176,28 @@ static void putFrontWindow( GameWindow *parent, GameWinDrawFunc draw )
front->winBringToTop();
}
+static const char *const NET_PAGE = "Window\\Html\\Net.html";
+
+/** The network box in the screen's top right corner, drawHudOverlay's readings on a page of their
+ * own: they change several times a second, and each change lays its page out again. */
+void InGameUI::drawNetPage( void )
+{
+ if( !m_netPageLoaded )
+ {
+ m_netPageLoaded = TRUE;
+ readHtmlPage( NET_PAGE, m_netPage );
+ }
+ if( m_netPage.empty() )
+ return;
+ if( m_netOverlay == NULL )
+ m_netOverlay = new HtmlOverlay( m_superweaponNormalFont );
+
+ HtmlValues values = m_hudValues;
+ values[ "side" ] = spectatorSide();
+ m_netOverlay->setPage( HtmlTemplate_expand( m_netPage, values, HtmlLists(), lookupGameText ) );
+ m_netOverlay->draw();
+}
+
void InGameUI::drawCellGridFront( Int grid )
{
HtmlOverlay *&overlay = m_cellFrontOverlay[ grid ];
@@ -11203,10 +11250,10 @@ Bool InGameUI::drawControlBarPage( const IRegion2D *panels, const Bool *shown, I
values[ "promotion" ] = !watching && TheControlBar->isGeneralStarFlashing() ? "ready" : "";
values[ "watching" ] = watching ? "watching" : "";
// the page's clicks are whole, down and up at once, so a key pressed in under the pointer goes by
- // the mouse's own left button
- values[ "held" ] = TheMouse->getMouseStatus()->leftState != MBS_Up ? "held" : "";
- values.insert( m_hudValues.begin(), m_hudValues.end() );
- values[ "blink" ] = TheGameLogic->getFrame() % LOGICFRAMES_PER_SECOND > LOGICFRAMES_PER_SECOND / 2 ? "lit" : "";
+ // the mouse's own left button. Only over the page: a changed value lays the whole page out again,
+ // fifteen milliseconds, and every click on the battlefield did that twice
+ values[ "held" ] = m_controlBarPageHovered && TheMouse->getMouseStatus()->leftState != MBS_Up ? "held" : "";
+ values[ "blink" ] = !values[ "promotion" ].empty() && TheGameLogic->getFrame() % LOGICFRAMES_PER_SECOND > LOGICFRAMES_PER_SECOND / 2 ? "lit" : "";
for( Int panel = 0; panel < panelCount; panel++ )
putPageRect( values, "panel" + std::to_string( panel ), panels[ panel ], shown[ panel ] );
@@ -11423,8 +11470,9 @@ Bool InGameUI::drawControlBarPage( const IRegion2D *panels, const Bool *shown, I
standDownPromotionScreen();
m_controlBarOverlay->setPage( HtmlTemplate_expand( m_controlBarPage, values, lists, lookupGameText ) );
- m_controlBarOverlay->hover( TheMouse->getMouseStatus()->pos );
+ m_controlBarPageHovered = m_controlBarOverlay->hover( TheMouse->getMouseStatus()->pos );
m_controlBarOverlay->draw();
+ drawNetPage();
std::vector< IRegion2D > solids;
m_controlBarOverlay->rectsOf( ".solid", solids );