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
fix(ww3d2): Direct3D 11 no longer shares the window or freezes movies
Olcay Seygan4467fb0b612d
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ef68efdb2..c14d5f650 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 touchedthe bugs inside it in twenty-two years. This build compiles, runs and plays.-**121 changes. ~580 engine source files ported. 14 automated test suites. Around 60 original bugs+**122 changes. ~580 engine source files ported. 14 automated test suites. Around 60 original bugsfound and fixed — EA's own, not port damage.**---@@ -638,6 +638,8 @@ found and fixed — EA's own, not port damage.**## It does not crash+- Starting a match with the Direct3D 11 picture on took the game down. Both devices were presenting into the same window, the old one lost itself, and the fog texture then locked a surface that was never made.+- The opening movies froze on their first frame with that picture on. A movie writes a new image every frame into a texture the copy had already taken.- Two blocks of 2003 assembly destroyed registers and took down the main menu.- Quitting faulted twice every time; it now takes about half a second.- A long chat message or an unusual map name could kill the process.diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShroud.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShroud.cppindex 3eca91419..b760c3d17 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShroud.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShroud.cpp@@ -181,6 +181,11 @@ void W3DShroud::init(WorldHeightMap *pMap, Real worldCellSizeX, Real worldCellSim_pSrcTexture = DX8Wrapper::_Create_DX8_Surface(srcWidth,srcHeight, WW3D_FORMAT_R5G6B5);DEBUG_ASSERTCRASH( m_pSrcTexture != NULL, ("Failed to Allocate Shroud Src Surface"));+ if (m_pSrcTexture == NULL)+ {+ DEBUG_LOG(("W3DShroud::init - no src surface (device missing or lost)\n"));+ return;+ }D3DLOCKED_RECT rect;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cppindex bd9db9df6..463da9f1c 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp@@ -207,6 +207,14 @@ DX11BufferTwinClass * Direct3D11_Twin_Index_Buffer(unsigned byte_count, bool dynbyte_count);}+void Direct3D11_Mark_Surface_Dirty(struct IDirect3DSurface9 * surface)+{+ if (!Active || surface == NULL) {+ return;+ }+ DX11Texture_Mark_Dirty(surface);+}+void Direct3D11_Mirror_Texture(unsigned stage, struct IDirect3DBaseTexture9 * texture){if (!Active) {diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.hindex f6a92e85e..bf197a5bc 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h@@ -84,6 +84,10 @@ DX11BufferTwinClass * Direct3D11_Twin_Index_Buffer(unsigned byte_count, bool dyn// first bind and kept on it, so nothing in the loaders has to know this exists.void Direct3D11_Mirror_Texture(unsigned stage, struct IDirect3DBaseTexture9 * texture);+// The CPU has just written this surface. The next bind of its texture fills the Direct3D 11 copy+// again. A no-op when the backend is not running.+void Direct3D11_Mark_Surface_Dirty(struct IDirect3DSurface9 * surface);+// Send the draws into the copy of whatever texture this surface belongs to. A null surface, or one// with no texture behind it, is the back buffer.void Direct3D11_Mirror_Render_Target(struct IDirect3DSurface9 * surface);diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.cppindex 3c8eafef9..bfb318706 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.cpp@@ -40,6 +40,11 @@ static const GUID DX11_TEXTURE_REFUSED =// {2E2E9C23-1B2A-4C7E-9E2F-1D0B7E9A5C01}static const GUID DX11_TEXTURE_TARGET ={ 0x2e2e9c23, 0x1b2a, 0x4c7e, { 0x9e, 0x2f, 0x1d, 0x0b, 0x7e, 0x9a, 0x5c, 0x01 } };+// Set when the CPU has written the D3D9 texture since the copy was last filled. Movies do this+// every frame; the loaders do not.+// {2E2E9C24-1B2A-4C7E-9E2F-1D0B7E9A5C01}+static const GUID DX11_TEXTURE_DIRTY =+ { 0x2e2e9c24, 0x1b2a, 0x4c7e, { 0x9e, 0x2f, 0x1d, 0x0b, 0x7e, 0x9a, 0x5c, 0x01 } };// Why the first texture that could not be copied could not be copied. A count of refusals says// how much of the picture is missing its texture; this says what to fix.@@ -296,6 +301,61 @@ static ID3D11ShaderResourceView * build(ID3D11Device * device, ID3D11DeviceContereturn view;}+static void clear_dirty(IDirect3DBaseTexture9 * texture)+{+ const unsigned char clear = 0;+ texture->SetPrivateData(DX11_TEXTURE_DIRTY, &clear, sizeof(clear), 0);+}++static bool texture_is_dirty(IDirect3DBaseTexture9 * texture)+{+ unsigned char dirty = 0;+ DWORD size = sizeof(dirty);+ return SUCCEEDED(texture->GetPrivateData(DX11_TEXTURE_DIRTY, &dirty, &size)) && dirty != 0;+}++// Put the D3D9 texture's current pixels into a copy that already exists. Default-pool and render+// target copies are filled another way and cannot be locked, so they are left alone.+static bool refresh_copy(ID3D11DeviceContext * context, ID3D11ShaderResourceView * view,+ IDirect3DBaseTexture9 * texture)+{+ IDirect3DTexture9 * two_dimensional = NULL;+ if (FAILED(texture->QueryInterface(IID_IDirect3DTexture9, (void **)&two_dimensional))) {+ return false;+ }++ D3DSURFACE_DESC description;+ if (FAILED(two_dimensional->GetLevelDesc(0, &description))) {+ two_dimensional->Release();+ return false;+ }+ if ((description.Usage & D3DUSAGE_RENDERTARGET) != 0 || description.Pool == D3DPOOL_DEFAULT) {+ two_dimensional->Release();+ return false;+ }++ ID3D11Resource * resource = NULL;+ view->GetResource(&resource);+ if (resource == NULL) {+ two_dimensional->Release();+ return false;+ }++ ID3D11Texture2D * copy = NULL;+ resource->QueryInterface(__uuidof(ID3D11Texture2D), (void **)©);+ resource->Release();+ if (copy == NULL) {+ two_dimensional->Release();+ return false;+ }++ const bool uploaded = upload_levels(context, copy, two_dimensional, description.Format,+ two_dimensional->GetLevelCount());+ copy->Release();+ two_dimensional->Release();+ return uploaded;+}+ID3D11ShaderResourceView * DX11Texture_Mirror(ID3D11Device * device, ID3D11DeviceContext * context,IDirect3DBaseTexture9 * texture){@@ -305,6 +365,10 @@ ID3D11ShaderResourceView * DX11Texture_Mirror(ID3D11Device * device, ID3D11Devic// GetPrivateData on an IUnknown adds a reference for the caller and the texture keeps its// own, so this one is handed straight back.view->Release();+ if (texture_is_dirty(texture)) {+ refresh_copy(context, view, texture);+ clear_dirty(texture);+ }++Reused;return view;}@@ -325,10 +389,28 @@ ID3D11ShaderResourceView * DX11Texture_Mirror(ID3D11Device * device, ID3D11Devictexture->SetPrivateData(DX11_TEXTURE_VIEW, view, sizeof(view), D3DSPD_IUNKNOWN);view->Release();+ clear_dirty(texture);++Mirrored;return view;}+void DX11Texture_Mark_Dirty(IDirect3DSurface9 * surface)+{+ if (surface == NULL) {+ return;+ }++ IDirect3DTexture9 * texture = NULL;+ if (FAILED(surface->GetContainer(IID_IDirect3DTexture9, (void **)&texture))+ || texture == NULL) {+ return;+ }++ const unsigned char dirty = 1;+ texture->SetPrivateData(DX11_TEXTURE_DIRTY, &dirty, sizeof(dirty), 0);+ texture->Release();+}+bool DX11Texture_Update(ID3D11Device * device, ID3D11DeviceContext * context,IDirect3DSurface9 * destination, IDirect3DSurface9 * source, const RECT * source_rectangle,const POINT * destination_point)diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.hindex a2aa8ac5a..28e117581 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11texture.h@@ -20,10 +20,11 @@** The Direct3D 11 copy of a texture the engine already built in Direct3D 9.**** Textures are not buffers. A vertex buffer is write-only and had to have its write moved, but a-** managed texture can be read back level by level, and every path that produces one - the .dds and-** .tga loaders, the procedural surfaces, the shroud - has finished writing before anything binds-** it. So the copy is made at the moment of the first bind and from the D3D9 texture itself, which-** costs one read of each level once and needs no hook in any of the loaders.+** managed texture can be read back level by level, and the loaders have finished writing before+** anything binds it. So the copy is made at the moment of the first bind and from the D3D9+** texture itself, which costs one read of each level once and needs no hook in any of the loaders.+** Movies rewrite the same texture every frame after that; Unlock marks it dirty and the next bind+** fills the copy again.**** The copy is kept on the D3D9 texture as private data, so it is released when that texture is,** and a texture that cannot be mirrored - a render target, a format D3D11 does not name - is@@ -53,6 +54,10 @@ bool DX11Texture_Update(ID3D11Device * device, ID3D11DeviceContext * context,IDirect3DSurface9 * destination, IDirect3DSurface9 * source,const struct tagRECT * source_rectangle, const struct tagPOINT * destination_point);+// The CPU has just written this surface. The next bind of its texture recopies the pixels; a+// surface with no texture behind it is ignored.+void DX11Texture_Mark_Dirty(IDirect3DSurface9 * surface);+// The render target view over the copy of the texture this surface is a level of, built on the// first call and kept on that texture with the shader resource view. Null when the surface has no// texture behind it, which is the device's own back buffer.diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cppindex 2604fb43b..bf6b29130 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp@@ -1961,7 +1961,16 @@ void DX8Wrapper::End_Scene(bool flip_frames)HRESULT hr;{WWPROFILE("DX8Device::Present()");- hr=_Get_D3D_Device()->Present(NULL, NULL, NULL, NULL);+ //+ // -dx11present owns the window. Presenting both swap chains into it loses the Direct3D 9+ // device (DEVICELOST on the next call, then a null shroud surface when a match starts).+ // Direct3D11_End_Scene has already shown the frame.+ //+ if (Direct3D11_Present_Is_Enabled()) {+ hr = D3D_OK;+ } else {+ hr=_Get_D3D_Device()->Present(NULL, NULL, NULL, NULL);+ }}number_of_DX8_calls++;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/surfaceclass.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/surfaceclass.cppindex db49e8fb6..43c1ef845 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/surfaceclass.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/surfaceclass.cpp@@ -51,6 +51,7 @@#include "surfaceclass.h"#include "formconv.h"#include "dx8wrapper.h"+#include "dx11runtime.h"#include "vector2i.h"#include "colorspace.h"#include "bound.h"@@ -269,6 +270,7 @@ void * SurfaceClass::Lock(int * pitch)void SurfaceClass::Unlock(void){DX8_ErrorCode(D3DSurface->UnlockRect());+ Direct3D11_Mark_Surface_Dirty(D3DSurface);}/***********************************************************************************************diff --git a/GeneralsMD/Code/Tests/test_dx11texture.cpp b/GeneralsMD/Code/Tests/test_dx11texture.cppindex 8c7100700..1a38dc551 100644--- a/GeneralsMD/Code/Tests/test_dx11texture.cpp+++ b/GeneralsMD/Code/Tests/test_dx11texture.cpp@@ -242,6 +242,50 @@ TEST(dx11texture_a_second_bind_finds_the_copy)texture->Release();}+// A movie writes a new image into the same texture every frame. Without a dirty mark the copy+// made on the first bind is what every later frame draws.+TEST(dx11texture_a_cpu_write_reaches_the_copy)+{+ Direct3D9Fixture d3d9;+ if (!d3d9.Create()) {+ printf("skip: no Direct3D 9 device on this machine\n");+ return;+ }++ DX11DeviceClass d3d11;+ CHECK(d3d11.Create_Offscreen());++ IDirect3DTexture9 * texture = filled_texture(d3d9.Get(), D3DFMT_A8R8G8B8, 0x80);+ CHECK(texture != NULL);++ ID3D11ShaderResourceView * view =+ DX11Texture_Mirror(d3d11.Get_Device(), d3d11.Get_Context(), texture);+ CHECK(view != NULL);++ D3DLOCKED_RECT locked;+ CHECK(SUCCEEDED(texture->LockRect(0, &locked, NULL, 0)));+ for (unsigned row = 0; row < TEXTURE_SIZE; ++row) {+ memset((unsigned char *)locked.pBits + row * locked.Pitch, 0x20, TEXTURE_SIZE * 4);+ }+ texture->UnlockRect(0);++ IDirect3DSurface9 * surface = NULL;+ CHECK(SUCCEEDED(texture->GetSurfaceLevel(0, &surface)));+ DX11Texture_Mark_Dirty(surface);+ surface->Release();++ view = DX11Texture_Mirror(d3d11.Get_Device(), d3d11.Get_Context(), texture);+ CHECK(view != NULL);++ unsigned char pixels[TEXTURE_SIZE * TEXTURE_SIZE * 4];+ CHECK(read_back(d3d11, view, pixels, TEXTURE_SIZE * 4));+ for (unsigned i = 0; i < TEXTURE_SIZE * TEXTURE_SIZE * 4; ++i) {+ CHECK_EQ(pixels[i], (unsigned char)0x20);+ }++ texture->Release();+}+// X8R8G8B8 has no alpha channel and the byte where DXGI reads one holds whatever the loader left.// Left alone, a texture whose unused byte is zero is a texture that draws nothing at all.TEST(dx11texture_an_alphaless_format_comes_out_opaque)