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): build an html page in 2 ms instead of 14
Olcay Seygan2c5efa88ddad
diff --git a/.gitattributes b/.gitattributesindex fd43213f3..e7ce68df5 100644--- a/.gitattributes+++ b/.gitattributes@@ -4,6 +4,9 @@# the same.*.sh text eol=lf+# litehtml's sources are LF, and a patch for them in CRLF matches none of their lines.+GeneralsMD/Code/Libraries/Source/litehtml-*.patch text eol=lf+# FFmpeg's own files, committed as they came out of its build: no line ending conversion on the# headers, and the DLLs and import libraries are binary.GeneralsMD/Code/Libraries/Source/FFmpeg/dist/** -textdiff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3140d4dee..b3667cb6d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,7 +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.+- 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. When the bar does have to be laid out again, on a hover, a click on it or the scoreboard's twice-a-second count, it takes 2ms where it took 14: the page engine was building an object for every word of the bar's style sheet and every space of its indenting, and reading the same style sheet from scratch each time. All ten pages were laid out both ways and every box and every word came out in the same place, size and colour.- 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/GameEngine/Include/GameClient/HtmlTemplate.h b/GeneralsMD/Code/GameEngine/Include/GameClient/HtmlTemplate.hindex e914275a4..630cde830 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/HtmlTemplate.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/HtmlTemplate.h@@ -53,4 +53,9 @@ extern std::string HtmlTemplate_expand( const std::string &page, const HtmlValue/** The text with & < > " and ' written as entities, so a player's name cannot open a tag. */extern std::string HtmlTemplate_escape( const std::string &text );+/** A filled-in page split for litehtml: the <style> block's text into `styles`, for its CSS parser,+ * and the rest into `body` with every run of white space outside a tag one space, which is all CSS+ * draws of it while no page uses white-space: pre. */+extern void HtmlTemplate_compact( const std::string &page, std::string &body, std::string &styles );+#endif // _HTML_TEMPLATE_H_diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlOverlay.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlOverlay.cppindex 1250bb07b..59c1973d8 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlOverlay.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlOverlay.cpp@@ -30,6 +30,7 @@#include "GameClient/DisplayStringManager.h"#include "GameClient/GameFont.h"#include "GameClient/HtmlOverlay.h"+#include "GameClient/HtmlTemplate.h"#include "GameClient/Image.h"#include "GameNetwork/GameSpy/ThreadUtils.h"@@ -60,6 +61,7 @@ const Int BOLD_WEIGHT = 600; ///< CSS font-weight from which the game'sconst Real ASCENT_SHARE = 0.8f; ///< of the font's height; GameFont only knows the heightconst Real X_HEIGHT_SHARE = 0.5f;const char *const PAGE_FOLDER = "Window\\Html\\";+const char *const EMPTY_PAGE = "<!DOCTYPE html><html></html>"; ///< standards mode, as every page isconst char *const SVG_EXTENSION = ".svg";const char *const SVG_UNITS = "px";const Int RGBA_BYTES = 4;@@ -186,6 +188,10 @@ private:std::map< std::string, SvgRuns > m_svgRuns; ///< by page path and screen sizestd::string m_page;litehtml::document::ptr m_document;+ litehtml::document::ptr m_cssDocument; ///< an empty page the stylesheets are parsed against, for its quirks mode+ litehtml::css m_masterCss; ///< litehtml's own stylesheet, parsed once+ litehtml::css m_pageCss; ///< the page's <style>, parsed while its text stays the same+ std::string m_styles; ///< that textReal m_scale;Int m_screenWidth;Int m_screenHeight;@@ -211,6 +217,7 @@ HtmlOverlayContainer::HtmlOverlayContainer( const AsciiString &defaultFont ) :HtmlOverlayContainer::~HtmlOverlayContainer( void ){m_document = nullptr;+ m_cssDocument = nullptr;freeStrings( TRUE );for( std::map< std::string, NSVGimage * >::iterator image = m_svgImages.begin(); image != m_svgImages.end(); ++image )if( image->second )@@ -231,7 +238,32 @@ void HtmlOverlayContainer::setPage( const std::string &html )m_screenWidth = width;m_screenHeight = height;m_scale = scale;- m_document = litehtml::document::createFromString( litehtml::estring( html, litehtml::encoding::utf_8 ), this );++ // litehtml makes an element for every word and one for every white space character, each with a+ // whole set of CSS properties, so the stylesheet and the indenting came in as thousands of them:+ // eleven milliseconds to build the command bar and three to throw it away+ std::string body;+ std::string styles;+ HtmlTemplate_compact( html, body, styles );++ // the stylesheets were two thirds of building a page, and they are the same text every time:+ // litehtml's own is parsed once, and the page's again only when its text changes+ if( m_cssDocument == nullptr )+ {+ m_cssDocument = litehtml::document::createFromString( litehtml::estring( EMPTY_PAGE, litehtml::encoding::utf_8 ), this, "", "" );+ m_masterCss.parse_css_stylesheet( std::string( litehtml::master_css ), "", m_cssDocument );+ m_masterCss.sort_selectors();+ }+ if( styles != m_styles )+ {+ m_styles = styles;+ m_pageCss = litehtml::css();+ m_pageCss.parse_css_stylesheet( m_styles, "", m_cssDocument );+ m_pageCss.sort_selectors();+ }++ m_document = litehtml::document::createFromString( litehtml::estring( body, litehtml::encoding::utf_8 ), this, "", "",+ &m_masterCss, &m_pageCss );m_document->render( viewportWidth() );}diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlTemplate.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlTemplate.cppindex 160a18beb..9481b9903 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlTemplate.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlTemplate.cpp@@ -200,3 +200,64 @@ std::string HtmlTemplate_expand( const std::string &written, const HtmlValues &vat = end;}}++//-------------------------------------------------------------------------------------------------+static Bool isPageSpace( char character )+{+ return character == ' ' || character == '\t' || character == '\n' || character == '\r' || character == '\f';+}++//-------------------------------------------------------------------------------------------------+void HtmlTemplate_compact( const std::string &page, std::string &body, std::string &styles )+{+ static const std::string STYLE_OPEN = "<style";+ static const std::string STYLE_CLOSE = "</style>";++ body.clear();+ styles.clear();+ body.reserve( page.size() );+ size_t at = 0;+ while( at < page.size() )+ {+ if( page.compare( at, STYLE_OPEN.size(), STYLE_OPEN ) == 0 )+ {+ const size_t open = page.find( '>', at );+ const size_t close = open == std::string::npos ? std::string::npos : page.find( STYLE_CLOSE, open );+ if( close == std::string::npos )+ {+ body.append( page, at, std::string::npos );+ break;+ }+ styles.append( page, open + 1, close - open - 1 );+ at = close + STYLE_CLOSE.size();+ }+ else if( page[ at ] == '<' )+ {+ // a tag goes over whole, the white space in its attribute values with it+ char quote = 0;+ const size_t start = at;+ for( ; at < page.size(); at++ )+ {+ const char character = page[ at ];+ if( quote != 0 )+ quote = character == quote ? 0 : quote;+ else if( character == '"' || character == '\'' )+ quote = character;+ else if( character == '>' )+ {+ at++;+ break;+ }+ }+ body.append( page, start, at - start );+ }+ else if( isPageSpace( page[ at ] ) )+ {+ while( at < page.size() && isPageSpace( page[ at ] ) )+ at++;+ body += ' ';+ }+ else+ body += page[ at++ ];+ }+}diff --git a/GeneralsMD/Code/Libraries/Source/litehtml-parsed-css.patch b/GeneralsMD/Code/Libraries/Source/litehtml-parsed-css.patchnew file mode 100644index 000000000..dc2280f59--- /dev/null+++ b/GeneralsMD/Code/Libraries/Source/litehtml-parsed-css.patch@@ -0,0 +1,53 @@+diff --git a/include/litehtml/document.h b/include/litehtml/document.h+index e01f5b4..ba7353e 100644+--- a/include/litehtml/document.h++++ b/include/litehtml/document.h+@@ -113,7 +113,9 @@ namespace litehtml+ const estring& str,+ document_container* container,+ const string& master_styles = litehtml::master_css,+- const string& user_styles = "");++ const string& user_styles = "",++ const css* master_parsed = nullptr, // ZHR: parsed once by the caller, used in place of master_styles++ const css* user_parsed = nullptr); // ZHR: and of user_styles++ private:+ uint_ptr add_font(const font_description& descr, font_metrics* fm);+diff --git a/src/document.cpp b/src/document.cpp+index abf3987..6a41cf4 100644+--- a/src/document.cpp++++ b/src/document.cpp+@@ -52,7 +52,9 @@ document::ptr document::createFromString(+ const estring& str,+ document_container* container,+ const string& master_styles,+- const string& user_styles )++ const string& user_styles,++ const css* master_parsed,++ const css* user_parsed )+ {+ // Create litehtml::document+ document::ptr doc = make_shared<document>(container);+@@ -79,12 +81,20 @@ document::ptr document::createFromString(+ // Destroy GumboOutput+ gumbo_destroy_output(&kGumboDefaultOptions, output);++- if (master_styles != "")++ if (master_parsed)++ {++ doc->m_master_css = *master_parsed;++ }++ else if (master_styles != "")+ {+ doc->m_master_css.parse_css_stylesheet(master_styles, "", doc);+ doc->m_master_css.sort_selectors();+ }+- if (user_styles != "")++ if (user_parsed)++ {++ doc->m_user_css = *user_parsed;++ }++ else if (user_styles != "")+ {+ doc->m_user_css.parse_css_stylesheet(user_styles, "", doc);+ doc->m_user_css.sort_selectors();diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex b1a058000..c52a0d97c 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -13722,6 +13722,25 @@ TEST(html_template_fills_values_and_repeats_each)CHECK_STR( HtmlTemplate_escape( "a\"b'c" ).c_str(), "a"b'c" );}+// litehtml builds an element for every word and every white space character, so the page it is+// handed has its stylesheet taken out for the CSS parser and each run of white space between tags+// cut to one; a tag's attribute values keep theirs, a '>' inside quotes included.+TEST(a_page_goes_to_litehtml_with_its_style_apart_and_its_spaces_folded)+{+ std::string body;+ std::string styles;+ HtmlTemplate_compact( "<!DOCTYPE html>\r\n<html>\n <head><style>\n .a b { top: 1px; }\n</style></head>\n"+ " <body>\n\t<div class=\"x y\" data-click='a > b'>one two</div>\n <span>3</span> <span>4</span>\n</body></html>",+ body, styles );+ CHECK_STR( styles.c_str(), "\n .a b { top: 1px; }\n" );+ CHECK_STR( body.c_str(), "<!DOCTYPE html> <html> <head></head> <body> <div class=\"x y\" data-click='a > b'>one two</div> "+ "<span>3</span> <span>4</span> </body></html>" );++ HtmlTemplate_compact( "<p>no end<style>.a{}", body, styles );+ CHECK_STR( body.c_str(), "<p>no end<style>.a{}" );+ CHECK_STR( styles.c_str(), "" );+}+// The spectator's page is the shipped one. It switches no options any more - the strips drop-down// that did went with the shelves it switched - so an option: click is a box that does nothing, and// only a watched match would show it.diff --git a/GeneralsMD/Code/Tools/vendor.ps1 b/GeneralsMD/Code/Tools/vendor.ps1index b3a9e53e6..b03117375 100644--- a/GeneralsMD/Code/Tools/vendor.ps1+++ b/GeneralsMD/Code/Tools/vendor.ps1@@ -132,6 +132,24 @@ function Install-Litehtml {Step "litehtml 0.10 -> Libraries\Source\litehtml"}+# --- The fork's one change to litehtml, Libraries\Source\litehtml-parsed-css.patch: createFromString+# takes stylesheets the caller parsed once, which was two thirds of building a page. HtmlOverlay.cpp+# passes them and does not compile without it. A copy that has the patch says so by the parameter's+# name in document.h, so a copy fetched before the patch existed gets it on the next build too.+function Install-LitehtmlPatch {+ $destination = Join-Path $libraries 'Source\litehtml'+ $header = Join-Path $destination 'include\litehtml\document.h'+ if (Select-String -LiteralPath $header -Pattern 'master_parsed' -SimpleMatch -Quiet) { return }+ $patch = Join-Path $libraries 'Source\litehtml-parsed-css.patch'+ # without the ceiling git finds this checkout around the folder and skips every file in the patch+ # as outside it, and says nothing+ $env:GIT_CEILING_DIRECTORIES = Join-Path $libraries 'Source'+ try { git -C $destination -c core.autocrlf=false apply $patch }+ finally { Remove-Item Env:GIT_CEILING_DIRECTORIES }+ if ($LASTEXITCODE -ne 0) { throw "litehtml-parsed-css.patch did not apply to Libraries\Source\litehtml" }+ Step "litehtml-parsed-css.patch -> Libraries\Source\litehtml"+}+# --- nanosvg, the two headers: parses and rasterises the SVG pictures a page names in url(), which# the game then draws pixel by pixel. Same .gitignore dance as litehtml.function Install-Nanosvg {@@ -247,6 +265,7 @@ Install-LzhlInstall-DirectXInstall-GameSpyInstall-Litehtml+Install-LitehtmlPatchInstall-NanosvgInstall-ArtStep 'everything the build needs is in place'