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(w3ddevice): stack lights iterator in getStaticDiffuse

Olcay Seyganc2086a3aedb0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 96c0bfb1d..a5ac2a9bf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@ found and fixed — EA's own, not port damage.**
- 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.
- The new renderer stopped redoing work between two pieces of ground that look the same. The map's terrain goes out as hundreds of tiles a frame under the same camera and the same settings, and for every tile the game worked the camera's arithmetic out again, rebuilt its description of the surface and of each texture's filtering, and compared the lot with the tile before. It now notices nothing was changed and goes straight to drawing. The fog of war is sent to the card only where it moved since the last frame, instead of all of it every frame. Two brutal computer players on Twilight Flame at 1920x1080 went from 2.46ms a frame to 2.24ms, and every picture is the one it was.
- The command bar and the observer's panel stopped reading their own pages from scratch every frame. The bar's page alone is 38 KB, and the game copied it, lowercased it and hunted through it for the blanks four times a frame before it wrote in a single number. Each page is read once now, and a frame only fills in the numbers. The same two computer players on Twilight Flame went from 2.03ms a frame to 1.84ms watched as an observer, and from 2.75ms to 2.50ms from a player's seat. Every page still comes out letter for letter as it did: two matches filled 270,000 of them both ways and found no difference.
+- Ground painted with a third texture, the dirt over grass and the rock over sand, cost a trip to the memory manager for every corner of every such patch on screen, every frame, only to light that corner. Nearly a tenth of the game's time went to taking and releasing the memory manager's lock for it. Lighting a corner asks for nothing now: the same Twilight Flame match went from 1.88ms a frame to 1.71ms as an observer and from 2.56ms to 2.26ms from a player's seat, and eight views over five maps came out the same as before.
- 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.
- Big fights no longer drag the picture down into the twenties. Every moving tank and soldier, and every puff of smoke, goes to the graphics card through one shared scratch area, and it was so small that a big fight on Bombardment Beach filled it about fifty times a frame. Each refill made the processor wait for the card, 8 to 12ms of every frame, while the card sat two thirds idle. The area is made full size once now and refills about seven times a frame, in 0.02ms. The same 16,000-frame battle, fast-forwarded, finishes in 226 seconds where it took between 263 and 325, and the seconds that fell under 50 frames a second went from 125 or more to 4.
- Every animation runs on a clock instead of counting frames.
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DScene.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DScene.h
index 7babed60f..ee3c539f9 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DScene.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DScene.h
@@ -89,6 +89,7 @@ public:
void removeDynamicLight(W3DDynamicLight * obj);
RefRenderObjListIterator * createLightsIterator(void);
void destroyLightsIterator(RefRenderObjListIterator * it);
+ RefRenderObjListClass *getLightList(void) {return &LightList;}; ///< for a stack iterator where a heap one per call costs
RefRenderObjListClass *getDynamicLights(void) {return &m_dynamicLightList;};
W3DDynamicLight *getADynamicLight(void);
void setGlobalLight(LightClass *pLight,Int lightIndex=0);
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp
index d98be7fa7..a474886c8 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp
@@ -2254,12 +2254,10 @@ Int BaseHeightMapRenderObjClass::getStaticDiffuse(Int x, Int y)
RTS3DScene *pMyScene = (RTS3DScene *)Scene;
if (pMyScene) {
- RefRenderObjListIterator *it = pMyScene->createLightsIterator();
- doTheLight(&vertex, lightRay, &normalAtTexel, it, 1.0f);
- if (it) {
- pMyScene->destroyLightsIterator(it);
- it = NULL;
- }
+ // Stack iterator: this runs per vertex of every extra-blend tile each frame,
+ // and a heap one cost two allocator critical sections per call.
+ RefRenderObjListIterator it(pMyScene->getLightList());
+ doTheLight(&vertex, lightRay, &normalAtTexel, &it, 1.0f);
} else {
doTheLight(&vertex, lightRay, &normalAtTexel, NULL, 1.0f);
}