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): draw less of the scene into the water mirror

Olcay Seygan12e61fce983f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d4cc221d0..28095a5f9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -679,8 +679,11 @@ found and fixed — EA's own, not port damage.**
on; the water you actually play beside gets it now. The reflection only ever darkens: open water
keeps the exact colour the map painted it, and a black shroud stays black. The water shows only
what the land does: an enemy hidden under the fog of war, or a stealthed unit, used to give itself
- away in the reflection. It is not free: the world is drawn a second time for the mirror, and on
- one 1280x720 view of Golden Oasis a frame went from 4.9ms to 8.7ms.
+ away in the reflection. It is not free, since the world is drawn a second time for the mirror, but
+ the mirror only draws the ground it can see, only the part of the screen the water covers, and
+ only every other frame while the camera holds still. On one 1280x720 view of Golden Oasis, where
+ the river fills the screen, a frame takes 6.9ms against 5.8ms with no reflection; before that
+ work it took 8.3ms. Over the small lakes on Fortress Avalanche the difference is 7.1ms against 6.6ms.
- Bright things can bleed light into the air. Off by default, because the artwork was painted in 2003 for a screen that had no glow at all.
- Two dropdowns on the display page, not two percentages. Glow is off, subtle, normal or strong; what glows is only the brightest, bright things, or most of the picture. The numbers underneath were a strength you had to find by experiment and a brightness that ran backwards, where turning it down put more of the screen in the haze, and nothing on screen told you that.
- Scorch marks follow the texture quality setting like everything else. The terrain and the trees
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DWater.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DWater.h
index c8db1b453..91bfb256f 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DWater.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DWater.h
@@ -175,6 +175,10 @@ protected:
Real m_fBumpScale; ///<scales bump map uv perturbation
TextureClass * m_pReflectionTexture; ///<render target for reflection
Real m_reflectionLevel; ///<height of the water plane the reflection texture was rendered for
+ Vector2 m_reflectionAreaMin; ///<lower left of the screen area the reflection texture covers, -1 to 1
+ Vector2 m_reflectionAreaMax; ///<upper right of the same area
+ Matrix3D m_reflectionCameraTransform; ///<where the camera stood when the reflection texture was last rendered
+ Bool m_reflectionReused; ///<the frame before this one kept the previous reflection texture
RenderObjClass *m_skyBox; ///<box around level
WaterTracksRenderSystem *m_waterTrackSystem; ///<object responsible for rendering water wakes
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
index f03627a31..e2f37f464 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
@@ -2040,6 +2040,24 @@ void HeightMapRenderObjClass::Render(RenderInfoClass & rinfo)
{
static int count = 0;
count++;
+ if (ShaderClass::Is_Backface_Culling_Inverted())
+ { // The draw window reaches well past what the mirror sees, and a mirror frame has
+ // nothing else to spend on a block of ground that could never show in the water.
+ // A block split across the window's wrap seam is not worth the arithmetic and is drawn.
+ const Int cellXMin = getXWithOrigin(i*VERTEX_BUFFER_TILE_LENGTH)+m_map->getDrawOrgX()-m_map->getBorderSizeInline();
+ const Int cellXMax = getXWithOrigin((i+1)*VERTEX_BUFFER_TILE_LENGTH-1)+m_map->getDrawOrgX()+1-m_map->getBorderSizeInline();
+ const Int cellYMin = getYWithOrigin(j*VERTEX_BUFFER_TILE_LENGTH)+m_map->getDrawOrgY()-m_map->getBorderSizeInline();
+ const Int cellYMax = getYWithOrigin((j+1)*VERTEX_BUFFER_TILE_LENGTH-1)+m_map->getDrawOrgY()+1-m_map->getBorderSizeInline();
+ if (cellXMax > cellXMin && cellYMax > cellYMin)
+ {
+ AABoxClass block;
+ block.Init_Min_Max(
+ Vector3((cellXMin-1)*MAP_XY_FACTOR, (cellYMin-1)*MAP_XY_FACTOR, m_minHeight),
+ Vector3((cellXMax+1)*MAP_XY_FACTOR, (cellYMax+1)*MAP_XY_FACTOR, m_maxHeight));
+ if (rinfo.Camera.Cull_Box(block))
+ continue;
+ }
+ }
Int numPolys = VERTEX_BUFFER_TILE_LENGTH*VERTEX_BUFFER_TILE_LENGTH*2;
Int numVertex = (VERTEX_BUFFER_TILE_LENGTH*2)*(VERTEX_BUFFER_TILE_LENGTH*2);
if (HALF_RES_MESH) {
@@ -2136,7 +2154,9 @@ void HeightMapRenderObjClass::Render(RenderInfoClass & rinfo)
if (TheTerrainTracksRenderObjClassSystem)
TheTerrainTracksRenderObjClassSystem->flush();
- if (m_shroud && rinfo.Additional_Pass_Count())
+ // The water a reflection lands on is shrouded already, and shrouding the ground inside the
+ // mirror as well would draw the whole terrain a second time for no visible difference.
+ if (m_shroud && rinfo.Additional_Pass_Count() && !ShaderClass::Is_Backface_Culling_Inverted())
{
rinfo.Peek_Additional_Pass(0)->Install_Materials();
renderTerrainPass(&rinfo.Camera);
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp
index 289da259f..364252eca 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp
@@ -2069,7 +2069,11 @@ void W3DTreeBuffer::drawTrees(CameraClass * camera, RefRenderObjListIterator *pD
if (m_treeTexture==NULL) {
return;
}
- if (m_updateAllKeys) {
+ // The visible flags and the buffers built from them are shared by every pass. A water mirror
+ // culling them against its own frustum left the screen drawing the mirror's trees until the
+ // next cull, and with the mirror skipping frames the trees flickered between the two sets. The
+ // mirror draws what the screen culled, which is what stands near the water it lands on.
+ if (m_updateAllKeys && !ShaderClass::Is_Backface_Culling_Inverted()) {
cull(camera);
}
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
index b2c09be06..19549b2c3 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
@@ -99,6 +99,7 @@
#define PATCH_SCALE (4.0f * MAP_XY_FACTOR) //horizontal scale factor. Adjust this and size to get desired vertex density.
#define SEA_REFLECTION_SIZE 256 //dimensions of reflection texture
#define WATER_REFLECTION_SIZE 512 //dimensions of the reflection texture laid over map water
+#define WATER_REFLECTION_AREA_MARGIN 0.05f //slack around the water's screen area, so filtering at its edge reads inside it
#define WATER_REFLECTION_STRENGTH 0.8f //how far a reflected object darkens the water under it
#define WATER_REFLECTION_SKY_COLOR Vector3(1.0f,1.0f,1.0f) //white, so where nothing stands the water keeps its own colour
@@ -396,6 +397,10 @@ WaterRenderObjClass::WaterRenderObjClass(void)
m_tod=TIME_OF_DAY_AFTERNOON;
m_pReflectionTexture=NULL;
m_reflectionLevel=0;
+ m_reflectionAreaMin.Set(-1.0f, -1.0f);
+ m_reflectionAreaMax.Set(1.0f, 1.0f);
+ m_reflectionCameraTransform.Make_Identity();
+ m_reflectionReused=FALSE;
m_skyBox=NULL;
m_vertexBufferD3D=NULL;
m_indexBufferD3D=NULL;
@@ -1591,13 +1596,35 @@ void WaterRenderObjClass::updateRenderTargetTextures(CameraClass *cam)
return;
if (m_waterType == WATER_TYPE_2_PVSHADER && getClippedWaterPlane(cam, NULL))
+ {
+ m_reflectionAreaMin.Set(-1.0f, -1.0f);
+ m_reflectionAreaMax.Set(1.0f, 1.0f);
renderMirror(cam, m_level); //generate texture containing reflected scene
+ }
if (m_waterType != WATER_TYPE_0_TRANSLUCENT || !m_pReflectionTexture)
return;
+ // A camera that has not moved looks at the same water from the same place, so a reflection one
+ // frame old lines up with it exactly and only what moved in it is a frame late. Such a frame
+ // keeps the last texture, and the one after it draws a new one.
+ const Bool cameraMoved = memcmp(&cam->Get_Transform(), &m_reflectionCameraTransform, sizeof(Matrix3D)) != 0;
+ if (!cameraMoved && !m_reflectionReused)
+ {
+ m_reflectionReused = TRUE;
+ return;
+ }
+ m_reflectionReused = FALSE;
+ m_reflectionCameraTransform = cam->Get_Transform();
+
// One mirror a frame, so the first water area in view decides the plane. A map whose visible
- // water sits at two different heights reflects the second one at the first one's level.
+ // water sits at two different heights reflects the second one at the first one's level. The
+ // mirror only covers the part of the screen the water is drawn on: whatever stands outside that
+ // part of its frustum can never show in the water, and is culled before it costs a draw.
+ Bool waterInView = FALSE;
+ Bool waterBehindCamera = FALSE;
+ Vector2 areaMin(1.0f, 1.0f);
+ Vector2 areaMax(-1.0f, -1.0f);
for (PolygonTrigger *pTrig=PolygonTrigger::getFirstPolygonTrigger(); pTrig; pTrig = pTrig->getNext())
{
if (!pTrig->isWaterArea() || pTrig->getNumPoints() < 3)
@@ -1608,10 +1635,41 @@ void WaterRenderObjClass::updateRenderTargetTextures(CameraClass *cam)
if (cam->Cull_Sphere(SphereClass(Vector3(center.x, center.y, center.z), pTrig->getRadius())))
continue;
- m_reflectionLevel = pTrig->getPoint(0)->z;
- renderMirror(cam, m_reflectionLevel);
+ if (!waterInView)
+ m_reflectionLevel = pTrig->getPoint(0)->z;
+ waterInView = TRUE;
+
+ for (Int pointIndex = 0; pointIndex < pTrig->getNumPoints(); ++pointIndex)
+ {
+ const ICoord3D *point = pTrig->getPoint(pointIndex);
+ Vector3 projected;
+ if (cam->Project(projected, Vector3(point->x, point->y, point->z)) == CameraClass::OUTSIDE_NEAR_CLIP)
+ {
+ waterBehindCamera = TRUE;
+ continue;
+ }
+ areaMin.X = min(areaMin.X, projected.X);
+ areaMin.Y = min(areaMin.Y, projected.Y);
+ areaMax.X = max(areaMax.X, projected.X);
+ areaMax.Y = max(areaMax.Y, projected.Y);
+ }
+ }
+
+ if (!waterInView)
return;
+
+ if (waterBehindCamera)
+ { //a corner the camera cannot project says nothing about where the rest lands
+ areaMin.Set(-1.0f, -1.0f);
+ areaMax.Set(1.0f, 1.0f);
}
+
+ m_reflectionAreaMin.Set(max(areaMin.X - WATER_REFLECTION_AREA_MARGIN, -1.0f), max(areaMin.Y - WATER_REFLECTION_AREA_MARGIN, -1.0f));
+ m_reflectionAreaMax.Set(min(areaMax.X + WATER_REFLECTION_AREA_MARGIN, 1.0f), min(areaMax.Y + WATER_REFLECTION_AREA_MARGIN, 1.0f));
+ if (m_reflectionAreaMin.X >= m_reflectionAreaMax.X || m_reflectionAreaMin.Y >= m_reflectionAreaMax.Y)
+ return; //every corner is off one side of the screen
+
+ renderMirror(cam, m_reflectionLevel);
}
//-------------------------------------------------------------------------------------------------
@@ -1669,6 +1727,15 @@ void WaterRenderObjClass::renderMirror(CameraClass *cam, Real level)
vMin.X=vMin.Y=0.0f;
cam->Set_Viewport(vMin,vMax);
+ // A point on the water plane lands on the same view plane position for both cameras, so the
+ // water's screen area is the same slice of the mirror's view plane, stretched over the texture.
+ Vector2 planeOldMin, planeOldMax;
+ cam->Get_View_Plane(planeOldMin, planeOldMax);
+ const Vector2 planeSize = planeOldMax - planeOldMin;
+ cam->Set_View_Plane(
+ Vector2(planeOldMin.X + planeSize.X * (m_reflectionAreaMin.X + 1.0f) * 0.5f, planeOldMin.Y + planeSize.Y * (m_reflectionAreaMin.Y + 1.0f) * 0.5f),
+ Vector2(planeOldMin.X + planeSize.X * (m_reflectionAreaMax.X + 1.0f) * 0.5f, planeOldMin.Y + planeSize.Y * (m_reflectionAreaMax.Y + 1.0f) * 0.5f));
+
cam->Apply(); //force an update of all the camera dependent parameters like frustum clip planes
//flip the winding order of polygons to draw the reflected back sides.
@@ -1692,6 +1759,7 @@ void WaterRenderObjClass::renderMirror(CameraClass *cam, Real level)
cam->Set_Oblique_Near_Plane(NULL);
cam->Set_Transform(OldCameraMatrix); //restore original non-reflected matrix
cam->Set_Viewport(vOldMin,vOldMax);
+ cam->Set_View_Plane(planeOldMin, planeOldMax);
cam->Apply(); //force an update of all the camera dependent parameters like frustum clip planes
@@ -3184,7 +3252,17 @@ void WaterRenderObjClass::drawReflection(Int triangleCount, Int vertexCount)
clipToTexture._41 = 0.5f;
clipToTexture._42 = 0.5f;
clipToTexture._43 = 1.0f;
- D3DXMATRIX viewToTexture = projection * clipToTexture;
+ // then from the whole screen onto the slice of it the texture was rendered for, still before
+ // the divide, so the offsets are scaled by w
+ const Real areaWidth = m_reflectionAreaMax.X - m_reflectionAreaMin.X;
+ const Real areaHeight = m_reflectionAreaMax.Y - m_reflectionAreaMin.Y;
+ D3DXMATRIX screenToArea;
+ D3DXMatrixIdentity(&screenToArea);
+ screenToArea._11 = 2.0f / areaWidth;
+ screenToArea._31 = -(1.0f + m_reflectionAreaMin.X) / areaWidth;
+ screenToArea._22 = 2.0f / areaHeight;
+ screenToArea._32 = (m_reflectionAreaMax.Y - 1.0f) / areaHeight;
+ D3DXMATRIX viewToTexture = projection * clipToTexture * screenToArea;
DX8Wrapper::_Set_DX8_Transform(D3DTS_TEXTURE0, *(Matrix4x4*)&viewToTexture);
DX8Wrapper::Set_DX8_Texture(0, m_pReflectionTexture->Peek_D3D_Texture());