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(w3ddevice): rebuild only the terrain that moved
Olcay Seygandb089e4d406b
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/BaseHeightMap.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/BaseHeightMap.hindex aec25ce75..babb880c3 100644--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/BaseHeightMap.h+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/BaseHeightMap.h@@ -127,6 +127,17 @@ public:virtual void adjustTerrainLOD(Int adj);virtual void doPartialUpdate(const IRegion2D &partialRange, WorldHeightMap *htMap, RefRenderObjListIterator *pLightsIterator) = 0;virtual void staticLightingChanged(void);++ /** One terrain cell moved - a building foundation flattening the ground under it, say.+ *+ * This used to go through staticLightingChanged(), which rebuilds and re-lights every drawn+ * vertex on the map: 61 ms on a four-player map, a visible stutter, for one cell. Instead the+ * cell joins a dirty rectangle that the client flushes once a frame through doPartialUpdate,+ * which is the same call WorldBuilder has always used for exactly this. */+ void terrainHeightChanged(Int x, Int y);++ /** The accumulated dirty rectangle, or FALSE if there is none. Clears it. */+ Bool takeDirtyRegion(IRegion2D ®ion);virtual void oversizeTerrain(Int tilesToOversize);virtual void reset(void);@@ -271,6 +282,8 @@ protected:Vector3 m_depthFade; ///<depth based fall off values for r,g,bBool m_disableTextures;Bool m_needFullUpdate; ///< True if lighting changed, and we need to update all instead of what moved.+ IRegion2D m_dirtyRegion; ///< cells changed since the client last rebuilt them, in map coordinates+ Bool m_hasDirtyRegion; ///< ...and whether there are anyBool m_doXNextTime; ///< True if we updated y scroll, and need to do x scroll next frame.Real m_minHeight; ///<minimum value of height samples in heightmapReal m_maxHeight; ///<maximum value of height samples in heightmapdiff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cppindex da6a5fe6e..cfd6b624d 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp@@ -258,6 +258,8 @@ BaseHeightMapRenderObjClass::BaseHeightMapRenderObjClass(void)m_x=0;m_y=0;m_needFullUpdate = false;+ m_hasDirtyRegion = false;+ m_dirtyRegion.lo.x = m_dirtyRegion.lo.y = m_dirtyRegion.hi.x = m_dirtyRegion.hi.y = 0;m_showImpassableAreas = false;m_updating = false;//Set height to the maximum value that can be stored.@@ -2372,6 +2374,54 @@ void BaseHeightMapRenderObjClass::staticLightingChanged( void )}+//=============================================================================+// BaseHeightMapRenderObjClass::terrainHeightChanged+//=============================================================================+/** One cell of the terrain moved. Everything the old whole-map path did *except* the whole-map+part: the scorches and the roads still get their lighting redone, because both sit on the ground+that just moved, and the ground itself joins a dirty rectangle for a partial rebuild. */+//=============================================================================+void BaseHeightMapRenderObjClass::terrainHeightChanged( Int x, Int y )+{+ /* One cell either side. A vertex's lighting and normal are computed from its neighbours, so+ rebuilding only the cell that moved leaves a seam of stale shading around it. */+ const Int PAD = 2;+ if (!m_hasDirtyRegion)+ {+ m_dirtyRegion.lo.x = x - PAD;+ m_dirtyRegion.lo.y = y - PAD;+ m_dirtyRegion.hi.x = x + PAD;+ m_dirtyRegion.hi.y = y + PAD;+ m_hasDirtyRegion = true;+ }+ else+ {+ if (x - PAD < m_dirtyRegion.lo.x) m_dirtyRegion.lo.x = x - PAD;+ if (y - PAD < m_dirtyRegion.lo.y) m_dirtyRegion.lo.y = y - PAD;+ if (x + PAD > m_dirtyRegion.hi.x) m_dirtyRegion.hi.x = x + PAD;+ if (y + PAD > m_dirtyRegion.hi.y) m_dirtyRegion.hi.y = y + PAD;+ }++ // Cause the scorches to get updated with new lighting.+ m_scorchesInBuffer = 0;+ m_curNumScorchVertices = 0;+ m_curNumScorchIndices = 0;+ m_roadBuffer->updateLighting();+}++//=============================================================================+// BaseHeightMapRenderObjClass::takeDirtyRegion+//=============================================================================+Bool BaseHeightMapRenderObjClass::takeDirtyRegion( IRegion2D ®ion )+{+ if (!m_hasDirtyRegion)+ return false;++ region = m_dirtyRegion;+ m_hasDirtyRegion = false;+ return true;+}+//=============================================================================// BaseHeightMapRenderObjClass::setTimeOfDay//=============================================================================diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cppindex 73cb762a5..7d117c767 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp@@ -71,6 +71,7 @@#include "GameClient/Water.h"#include "GameLogic/AIPathfind.h"+#include "GameLogic/GameLogic.h"#include "GameLogic/TerrainLogic.h"#include "W3DDevice/GameClient/TerrainTex.h"#include "W3DDevice/GameClient/W3DDynamicLight.h"@@ -1701,10 +1702,18 @@ void HeightMapRenderObjClass::updateCenter(CameraClass *camera , RefRenderObjLisBaseHeightMapRenderObjClass::updateCenter(camera, pLightsIterator);m_updating = true;- if (m_needFullUpdate)+ if (m_needFullUpdate){m_needFullUpdate = false;+ /* Rebuilding and re-lighting every rendered vertex of the terrain. This is tens of+ milliseconds - a visible stutter - and something as small as one grid cell being lowered+ by a building foundation asks for it (W3DTerrainVisual::setRawMapHeight). Logged with+ its cost so a stutter report can be tied to it instead of guessed at. */+ const DWORD fullUpdateStart = timeGetTime();updateBlock(0, 0, m_x-1, m_y-1, m_map, pLightsIterator);+ DEBUG_LOG(("TERRAIN FULL UPDATE: %d ms at frame %d\n",+ (Int)(timeGetTime() - fullUpdateStart),+ TheGameLogic ? TheGameLogic->getFrame() : 0));m_updating = false;return;}@@ -1915,11 +1924,11 @@ void HeightMapRenderObjClass::updateCenter(CameraClass *camera , RefRenderObjLis//=============================================================================/** Renders (draws) the terrain. *///=============================================================================-//DECLARE_PERF_TIMER(Terrain_Render)+DECLARE_PERF_TIMER(Terrain_Render)void HeightMapRenderObjClass::Render(RenderInfoClass & rinfo){- //USE_PERF_TIMER(Terrain_Render)+ USE_PERF_TIMER(Terrain_Render)Int i,j,devicePasses;W3DShaderManager::ShaderTypes st;diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTerrainVisual.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTerrainVisual.cppindex ed0b0f37f..4364a4944 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTerrainVisual.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTerrainVisual.cpp@@ -946,7 +946,11 @@ void W3DTerrainVisual::setRawMapHeight(const ICoord2D *gridPos, Int height)if (m_logicHeightMap->getHeight(x,y) > height){m_logicHeightMap->setRawHeight(x, y, height);- m_terrainRenderObject->staticLightingChanged(); // OOH! this could benefit from the new Seismic update code+ /* This used to be staticLightingChanged(), which rebuilds and re-lights every drawn+ vertex on the map - 61 ms, measured, a visible stutter - because one cell moved. EA+ had noticed: "OOH! this could benefit from the new Seismic update code". It gets a+ partial rebuild of the cell's neighbourhood instead. */+ m_terrainRenderObject->terrainHeightChanged(x, y);#ifdef DO_SEISMIC_SIMULATIONSdiff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cppindex 76c26b203..15c9b990f 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp@@ -1136,10 +1136,10 @@ void W3DView::updateView(void)UPDATE();}-//DECLARE_PERF_TIMER(W3DView_updateView)+DECLARE_PERF_TIMER(W3DView_updateView)void W3DView::update(void){- //USE_PERF_TIMER(W3DView_updateView)+ USE_PERF_TIMER(W3DView_updateView)Bool recalcCamera = false;Bool didScriptedMovement = false;#ifdef LOG_FRAME_TIMES@@ -1188,15 +1188,29 @@ void W3DView::update(void)if (waypointElapsedMs > 100)waypointElapsedMs = 100; // a hitch should not teleport the camera down the path+ IRegion2D dirtyTerrain;if (TheTerrainRenderObject->doesNeedFullUpdate()) {RefRenderObjListIterator *it = W3DDisplay::m_3DScene->createLightsIterator();TheTerrainRenderObject->updateCenter(m_3DCamera, it);- if (it)+ if (it){W3DDisplay::m_3DScene->destroyLightsIterator(it);it = NULL;}}+ else if (TheTerrainRenderObject->takeDirtyRegion(dirtyTerrain)) {+ /* Ground that moved since the last frame - a building foundation flattening its footprint,+ mostly. Rebuilt here rather than through the whole-map path above, which costs 61 ms and+ is a stutter the player sees. Skipped entirely while a full update is already pending,+ because that redoes everything anyway. */+ RefRenderObjListIterator *it = W3DDisplay::m_3DScene->createLightsIterator();+ TheTerrainRenderObject->doPartialUpdate(dirtyTerrain, TheTerrainRenderObject->getMap(), it);+ if (it)+ {+ W3DDisplay::m_3DScene->destroyLightsIterator(it);+ it = NULL;+ }+ }static Real followFactor = -1;ObjectID cameraLock = getCameraLock();@@ -1644,10 +1658,10 @@ void W3DView::drawView( void )DRAW();}-//DECLARE_PERF_TIMER(W3DView_drawView)+DECLARE_PERF_TIMER(W3DView_drawView)void W3DView::draw( void ){- //USE_PERF_TIMER(W3DView_drawView)+ USE_PERF_TIMER(W3DView_drawView)Bool skipRender = false;Bool doExtraRender = false;CustomScenePassModes customScenePassMode = SCENE_PASS_DEFAULT;