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.

fix(ww3d2): stop dereferencing pointers the D3D call never filled in

Olcay Seygan792ad10559db
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 30157c33a..33fdb4625 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -379,6 +379,7 @@ found and fixed — EA's own, not port damage.**
- A replay that never recorded which seat was yours plays instead of crashing before the map loads.
- Alt-tabbing away with an arrow key held no longer leaves the camera sliding that way when you come back. The key release goes to whatever you switched to, so the game never heard it; losing focus now ends the scroll, the middle-button pan and the ALT pitch drag along with it.
- Locking Windows mid-battle no longer eats the game. Nothing that renders runs while the screen is locked, and the particle bookkeeping had been filed under rendering, so every explosion while you were away was created and never cleared up. A hundred rocket buggies firing at the ground for a minute is enough to come back to a game that has run out of memory or simply stopped responding.
+- Tank tracks no longer crash the game. The renderer has to borrow the buffer it writes them into, and when that request failed, which is what a graphics device pulled out from under the game does, the tracks were written to address zero anyway. Two more of the same shape went with it: the screenshot path and the texture size lookup both used a pointer that the failing call had never filled in. Three of those texture lookups also leaked the surface they asked about, once per procedural texture, for the life of the process.
- Double-clicking a menu button opens the screen once. The button stays live while the old screen animates out, so the second click started the same transition again and the screen opened twice, one copy left drawn over whatever came next and doing nothing sensible when clicked.
- Double-tapping 0 jumps the camera to that group.
- The mouse wheel no longer cancels a camera move already in progress.
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTerrainTracks.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTerrainTracks.cpp
index dbdb36887..59b118d9e 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTerrainTracks.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTerrainTracks.cpp
@@ -606,7 +606,11 @@ void TerrainTracksRenderObjClassSystem::ReAcquireResources(void)
DX8IndexBufferClass::WriteLockClass lockIdxBuffer(m_indexBuffer);
UnsignedShort *ib=lockIdxBuffer.Get_Index_Array();
- for (i=0; i<(m_maxTankTrackEdges-1); i++)
+ // same failure as the vertex lock in flush(): write nothing rather than write through null
+ if (ib == NULL)
+ DEBUG_LOG(("TerrainTracks: could not lock the index buffer - tracks will not draw until the detail setting changes\n"));
+
+ for (i=0; ib != NULL && i<(m_maxTankTrackEdges-1); i++)
{
ib[3]=ib[0]=i*2;
ib[1]=i*2+1;
@@ -830,6 +834,19 @@ Try improving the fit to vertical surfaces like cliffs.
{
DX8VertexBufferClass::WriteLockClass lockVtxBuffer(m_vertexBuffer);
VertexFormatXYZDUV1 *verts = (VertexFormatXYZDUV1*)lockVtxBuffer.Get_Vertex_Array();
+
+ //
+ // The lock can fail - a device that has gone out from under us is how it happens - and
+ // everything below writes through this pointer without ever looking at it. Nothing has been
+ // written when it fails, so there is nothing to draw either: drop this flush whole and let
+ // the next frame lay the tracks down again.
+ //
+ if (verts == NULL)
+ {
+ m_edgesToFlush = 0;
+ return;
+ }
+
trackStartIndex=0;
mod=m_usedModules;
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
index 6de881feb..c8fade018 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
@@ -1871,7 +1871,9 @@ void DX8Wrapper::Clear(bool clear_color, bool clear_z_stencil, const Vector3 &co
_PresentParameters.AutoDepthStencilFormat == D3DFMT_D24S8 ||
_PresentParameters.AutoDepthStencilFormat == D3DFMT_D24X4S4);*/
bool has_stencil=false;
- IDirect3DSurface8* depthbuffer;
+ // GetDepthStencilSurface leaves this alone when it fails - a lost device is how - and the test
+ // below then reads whatever was on the stack and calls GetDesc through it.
+ IDirect3DSurface8* depthbuffer = NULL;
_Get_D3D_Device8()->GetDepthStencilSurface(&depthbuffer);
number_of_DX8_calls++;
@@ -3223,7 +3225,7 @@ SurfaceClass * DX8Wrapper::_Get_DX8_Back_Buffer(unsigned int num)
{
DX8_THREAD_ASSERT();
- IDirect3DSurface8 * bb;
+ IDirect3DSurface8 * bb = NULL; // left untouched when the call fails; the test below is not
SurfaceClass *surf=NULL;
DX8CALL(GetBackBuffer(num,D3DBACKBUFFER_TYPE_MONO,&bb));
if (bb)
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/texture.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/texture.cpp
index 1e16a79b8..f7068d95a 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/texture.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/texture.cpp
@@ -74,6 +74,34 @@ static unsigned unused_texture_id;
static unsigned TexturesAppliedPerFrame;
const unsigned MAX_TEXTURES_APPLIED_PER_FRAME=2;
+/**
+ * Describe a texture's top mip level. Returns false, with the descriptor zeroed, when there is
+ * nothing to describe.
+ *
+ * Five places wanted this and all five wrote it out by hand with the same two faults.
+ * GetSurfaceLevel leaves the out pointer untouched when it fails - a device that has gone away
+ * makes that routine here, and it is the same hole that let a failed texture creation reach
+ * TextureLoadTaskClass::Begin_Load as a success - and every one of them called GetDesc through an
+ * uninitialized pointer. Three of them then never released the reference GetSurfaceLevel had
+ * taken, so a procedural texture pinned its own surface for the life of the process.
+ */
+static bool Describe_Texture_Level_0(IDirect3DTexture8 *texture, D3DSURFACE_DESC &desc)
+{
+ ::ZeroMemory(&desc, sizeof(D3DSURFACE_DESC));
+
+ if (texture == NULL)
+ return false;
+
+ IDirect3DSurface8 *surface = NULL;
+ DX8_ErrorCode(texture->GetSurfaceLevel(0, &surface));
+ if (surface == NULL)
+ return false;
+
+ DX8_ErrorCode(surface->GetDesc(&desc));
+ surface->Release();
+ return true;
+}
+
/*!
* KM General base constructor for texture classes
@@ -818,15 +846,14 @@ TextureClass::TextureClass(IDirect3DBaseTexture8* d3d_texture)
IsReducible=false;
Set_D3D_Base_Texture(d3d_texture);
- IDirect3DSurface8* surface;
- DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(0,&surface));
D3DSURFACE_DESC d3d_desc;
- ::ZeroMemory(&d3d_desc, sizeof(D3DSURFACE_DESC));
- DX8_ErrorCode(surface->GetDesc(&d3d_desc));
- Width=d3d_desc.Width;
- Height=d3d_desc.Height;
- TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
- switch (TextureFormat)
+ if (Describe_Texture_Level_0(Peek_D3D_Texture(), d3d_desc))
+ {
+ Width=d3d_desc.Width;
+ Height=d3d_desc.Height;
+ TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
+ }
+ switch (TextureFormat)
{
case WW3D_FORMAT_DXT1:
case WW3D_FORMAT_DXT2:
@@ -909,18 +936,13 @@ void TextureClass::Apply_New_Surface
if (disable_auto_invalidation) InactivationTime = 0;
WWASSERT(d3d_texture);
- IDirect3DSurface8* surface;
- DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(0,&surface));
D3DSURFACE_DESC d3d_desc;
- ::ZeroMemory(&d3d_desc, sizeof(D3DSURFACE_DESC));
- DX8_ErrorCode(surface->GetDesc(&d3d_desc));
- if (initialized)
+ if (initialized && Describe_Texture_Level_0(Peek_D3D_Texture(), d3d_desc))
{
TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
Width=d3d_desc.Width;
Height=d3d_desc.Height;
}
- surface->Release();
}
@@ -1290,18 +1312,13 @@ void ZTextureClass::Apply_New_Surface
if (disable_auto_invalidation) InactivationTime = 0;
WWASSERT(Peek_D3D_Texture());
- IDirect3DSurface8* surface;
- DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(0,&surface));
D3DSURFACE_DESC d3d_desc;
- ::ZeroMemory(&d3d_desc, sizeof(D3DSURFACE_DESC));
- DX8_ErrorCode(surface->GetDesc(&d3d_desc));
- if (initialized)
+ if (initialized && Describe_Texture_Level_0(Peek_D3D_Texture(), d3d_desc))
{
DepthStencilTextureFormat=D3DFormat_To_WW3DZFormat(d3d_desc.Format);
Width=d3d_desc.Width;
Height=d3d_desc.Height;
}
- surface->Release();
}
//**********************************************************************************************
@@ -1563,15 +1580,14 @@ CubeTextureClass::CubeTextureClass(IDirect3DBaseTexture8* d3d_texture)
IsReducible=false;
Peek_Texture()->AddRef();
- IDirect3DSurface8* surface;
- DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(0,&surface));
D3DSURFACE_DESC d3d_desc;
- ::ZeroMemory(&d3d_desc, sizeof(D3DSURFACE_DESC));
- DX8_ErrorCode(surface->GetDesc(&d3d_desc));
- Width=d3d_desc.Width;
- Height=d3d_desc.Height;
- TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
- switch (TextureFormat)
+ if (Describe_Texture_Level_0(Peek_D3D_Texture(), d3d_desc))
+ {
+ Width=d3d_desc.Width;
+ Height=d3d_desc.Height;
+ TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
+ }
+ switch (TextureFormat)
{
case WW3D_FORMAT_DXT1:
case WW3D_FORMAT_DXT2:
@@ -1848,15 +1864,14 @@ CubeTextureClass::CubeTextureClass(IDirect3DBaseTexture8* d3d_texture)
IsReducible=false;
Peek_Texture()->AddRef();
- IDirect3DSurface8* surface;
- DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(0,&surface));
D3DSURFACE_DESC d3d_desc;
- ::ZeroMemory(&d3d_desc, sizeof(D3DSURFACE_DESC));
- DX8_ErrorCode(surface->GetDesc(&d3d_desc));
- Width=d3d_desc.Width;
- Height=d3d_desc.Height;
- TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
- switch (TextureFormat)
+ if (Describe_Texture_Level_0(Peek_D3D_Texture(), d3d_desc))
+ {
+ Width=d3d_desc.Width;
+ Height=d3d_desc.Height;
+ TextureFormat=D3DFormat_To_WW3DFormat(d3d_desc.Format);
+ }
+ switch (TextureFormat)
{
case WW3D_FORMAT_DXT1:
case WW3D_FORMAT_DXT2:
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ww3d.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ww3d.cpp
index 1708146a9..2e11dede3 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ww3d.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ww3d.cpp
@@ -1350,8 +1350,11 @@ void WW3D::Make_Screen_Shot( const char * filename_base , const float gamma, con
// Lock front buffer and copy
- IDirect3DSurface8 *fb;
- fb=DX8Wrapper::_Get_DX8_Front_Buffer();
+ // _Get_DX8_Front_Buffer hands back NULL when the surface it needs cannot be created, which is
+ // what a device that has gone away does; there is no picture to take then.
+ IDirect3DSurface8 *fb=DX8Wrapper::_Get_DX8_Front_Buffer();
+ if (fb == NULL)
+ return;
D3DSURFACE_DESC desc;
fb->GetDesc(&desc);
@@ -1685,8 +1688,11 @@ void WW3D::Update_Movie_Capture( void )
// Lock front buffer and copy
- IDirect3DSurface8 *fb;
- fb=DX8Wrapper::_Get_DX8_Front_Buffer();
+ // _Get_DX8_Front_Buffer hands back NULL when the surface it needs cannot be created, which is
+ // what a device that has gone away does; there is no picture to take then.
+ IDirect3DSurface8 *fb=DX8Wrapper::_Get_DX8_Front_Buffer();
+ if (fb == NULL)
+ return;
D3DSURFACE_DESC desc;
fb->GetDesc(&desc);