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): skip a posed skin volume too small to see

Olcay Seygan2bd3b69437d4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 171dc9778..1f6c4b68a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,7 +3,7 @@
EA released the 2003 source for preservation. It did not compile, did not run, and nobody had touched
the bugs inside it in twenty-two years. This build compiles, runs and plays.
-**119 changes. ~580 engine source files ported. 14 automated test suites. Around 60 original bugs
+**120 changes. ~580 engine source files ported. 14 automated test suites. Around 60 original bugs
found and fixed — EA's own, not port damage.**
---
@@ -588,6 +588,7 @@ found and fixed — EA's own, not port damage.**
## Soldiers cast real shadows
- Infantry shadows are built from the pose: arms, head, weapon, moving with him.
+- A crowd of infantry no longer rebuilds a posed shadow for every man too small to make out. Zoom in and the pose is still there.
- `UseShadowVolumesForSkins = No` puts the old flat blobs back.
- Scuds, rockets and falling bombs cast a shadow running along the ground.
- A big smoke cloud darkens the ground under it and fades as it does.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/Shadow.h b/GeneralsMD/Code/GameEngine/Include/GameClient/Shadow.h
index da604f84b..096ad72ad 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/Shadow.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/Shadow.h
@@ -65,6 +65,16 @@ static const char* TheShadowNames[] =
#define MAX_SHADOW_LIGHTS 1 //maximum number of shadow casting light sources in scene - support for more than 1 has been dropped from most code.
+// A posed skin volume whose bounding sphere is smaller than this many pixels
+// on screen is not rebuilt or drawn that frame. Zoom in and it comes back.
+enum { SHADOW_SKIN_VOLUME_MIN_SCREEN_RADIUS = 20 };
+
+inline Bool Shadow_skinVolumeLargeEnoughOnScreen( Int dx, Int dy )
+{
+ return ( dx * dx + dy * dy ) >=
+ SHADOW_SKIN_VOLUME_MIN_SCREEN_RADIUS * SHADOW_SKIN_VOLUME_MIN_SCREEN_RADIUS;
+}
+
class RenderObjClass; //forward reference
class RenderCost; //forward reference
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
index 61e972781..1dc7e1166 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
@@ -59,6 +59,7 @@
#include "GameLogic/TerrainLogic.h"
#include "WW3D2/DX8Caps.h"
#include "GameClient/Drawable.h"
+#include "GameClient/View.h"
#include "wwshade/shdmesh.h"
#include "wwshade/shdsubmesh.h"
@@ -369,6 +370,7 @@ protected:
return m_verts[dwVertId];
}
+public:
Bool isSkin (void) const { return m_isSkin;}
Int getNumSourceVerts (void) const { return m_numSourceVerts;}
/**Point the vertex and face normal accessors at externally owned, per-frame data. Used while a
@@ -1982,6 +1984,36 @@ void W3DVolumetricShadow::SetGeometry( W3DShadowGeometry *geometry )
} // end SetGeometry
+static Bool geometryHasSkinMesh( W3DShadowGeometry *geometry )
+{
+ if( geometry == NULL )
+ return FALSE;
+ Int n = geometry->getMeshCount();
+ for( Int i = 0; i < n; ++i )
+ if( geometry->getMesh( i )->isSkin() )
+ return TRUE;
+ return FALSE;
+}
+
+static Bool skinCasterLargeEnoughOnScreen( RenderObjClass *robj )
+{
+ if( robj == NULL || TheTacticalView == NULL )
+ return TRUE;
+
+ const SphereClass &sphere = robj->Get_Bounding_Sphere();
+ Coord3D p;
+ p.x = sphere.Center.X;
+ p.y = sphere.Center.Y;
+ p.z = sphere.Center.Z;
+ ICoord2D s0, s1;
+ if( !TheTacticalView->worldToScreen( &p, &s0 ) )
+ return FALSE;
+ p.x += sphere.Radius;
+ if( !TheTacticalView->worldToScreen( &p, &s1 ) )
+ return TRUE;
+ return Shadow_skinVolumeLargeEnoughOnScreen( s1.x - s0.x, s1.y - s0.y );
+}
+
/**Called once per frame for each object, when necessary it will reconstruct
the shadow volume for this shadow from the silhouette of the geometry
and any light sources
@@ -1998,6 +2030,12 @@ void W3DVolumetricShadow::Update()
if( m_geometry == NULL)
return;
+ // Infantry at max zoom are a few pixels; rebuilding a posed volume for
+ // each of a thousand of them is the frame. The pose is still there when
+ // the caster is large enough to see it.
+ if( geometryHasSkinMesh( m_geometry ) && !skinCasterLargeEnoughOnScreen( m_robj ) )
+ return;
+
//
// for now we will just rebuild a shadow volume every so often, this
// should be changed to be built only when the light angle sufficiently
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index d0a619f69..2c3af6f43 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -10721,6 +10721,21 @@ TEST(build_placement_preview_defaults_are_the_ones_the_game_always_used)
delete scratch;
}
+//-------------------------------------------------------------------------------------------------
+// A posed skin volume smaller than this on screen is not rebuilt that frame. The threshold is
+// the shipped contract: 20 pixels of radius is still drawn, 19 is not.
+//-------------------------------------------------------------------------------------------------
+TEST(skin_volume_screen_lod_keeps_readable_shadows)
+{
+ CHECK( Shadow_skinVolumeLargeEnoughOnScreen( SHADOW_SKIN_VOLUME_MIN_SCREEN_RADIUS, 0 ) );
+ CHECK( Shadow_skinVolumeLargeEnoughOnScreen( 0, SHADOW_SKIN_VOLUME_MIN_SCREEN_RADIUS ) );
+ CHECK( !Shadow_skinVolumeLargeEnoughOnScreen( SHADOW_SKIN_VOLUME_MIN_SCREEN_RADIUS - 1, 0 ) );
+ CHECK( !Shadow_skinVolumeLargeEnoughOnScreen( 0, 0 ) );
+ // 16^2 + 12^2 = 400, which is exactly the 20-pixel radius
+ CHECK( Shadow_skinVolumeLargeEnoughOnScreen( 16, 12 ) );
+ CHECK( !Shadow_skinVolumeLargeEnoughOnScreen( 15, 12 ) );
+}
+
//-------------------------------------------------------------------------------------------------
// A map name with a non-ASCII byte in it reaches isalnum as a negative number, which is undefined
// and is what the CRT asserted on while building the map cache. Every byte over 127 has to encode,