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
build(cmake): drop d3d8to9 from the build and prove the phase
Olcay Seygan6e2635aaa809
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8f9a30921..4835b0afa 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -887,7 +887,13 @@ found and fixed — EA's own, not port damage.**- The videos play: the intro, the sizzle reel, the mission briefings, the general portraits.- The pointer is on screen over them. It used to appear only once the main menu did, so clicking through the logos was done blind.- Escape skips the opening logo, not just everything after it. The key has always skipped movies; the gate it asks was raised only once the logo had played itself out, so the one film you see on every single launch was the one you could not get past.-- About 5,600 graphics calls are translated to a modern path, none of them touched.+- The graphics go straight to a modern path. About 5,600 calls used to be translated on the way out+ by a small library shipped alongside the game; the game now speaks that path itself and the extra+ library is gone from the download. The picture is the picture it was, and the frame it draws in a+ fixed-seed match is within half a percent of the old one, pixel for pixel.+- Vertical sync works in a window. The option has always been in the menu, and in a window the old+ path ignored it and ran the picture as fast as it could, which is what a screen tears from. It is+ honoured now, so the picture holds to your monitor's refresh rate unless you turn it off.- No disc, no registry keys, no retail installer — a normal install works.- The startup screen is this build's own, so you can see which one you launched before the menu loads.- The zip installs itself and takes itself back off. `install.bat` asks where the game is, offering@@ -906,7 +912,7 @@ found and fixed — EA's own, not port damage.**- The game no longer needs a Visual C++ redistributable installed. The exe asked Windows for twosupport libraries by name at load, so a machine that had never installed one failed exactly theway a missing `mss32.dll` did: a dialog naming a file, nothing written to any log, nothing to send- back to anyone. Both libraries are compiled into the exe and into the graphics translator now. The+ back to anyone. Both libraries are compiled into the exe now. Thedownload is half a megabyte larger and there is nothing left to install alongside it.- Over a build that is already there it updates rather than installs, and says which number it ismoving you from and to. Running an older package over a newer install stops instead, since thediff --git a/GeneralsMD/Code/CMakeLists.txt b/GeneralsMD/Code/CMakeLists.txtindex dfbd94899..c66b55b9c 100644--- a/GeneralsMD/Code/CMakeLists.txt+++ b/GeneralsMD/Code/CMakeLists.txt@@ -263,18 +263,12 @@ set_source_files_properties(${WW}/WW3D2/dx8webbrowser.cpp PROPERTIES COMPILE_OPTadd_executable(eip_sampler ${CMAKE_CURRENT_SOURCE_DIR}/Tools/ProfileSampler/eip_sampler.cpp)set_target_properties(eip_sampler PROPERTIES CXX_STANDARD 17)-# Phase 1 checkpoint: vendored DX8 SDK + a d3d8.dll create a real device. Our d3d8to9-# d3d8.dll is copied next to the exe (see below, after d3d8to9 is defined) so the test-# exercises the game's real path: Direct3D 9 by default, Direct3D 9On12 / Direct3D 12 with -d3d12.-add_executable(dx8_smoke ${WW}/WW3D2/test_dx8_smoke.cpp)-target_include_directories(dx8_smoke PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/DirectX/Include)-target_link_directories(dx8_smoke PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/DirectX/Lib)-target_link_libraries(dx8_smoke PRIVATE d3d8 user32)--# RENDERER-ROADMAP.md phase 1 checkpoint: the same proof without the translating-# dll. d3d9.h and d3d9.lib come from the Windows SDK, so this target must not see-# Libraries/DirectX/Include - d3d8.h would shadow nothing here, but keeping the two-# header sets apart is what makes the native path native.+# RENDERER-ROADMAP.md phase 1 checkpoint: a real device, with no translating dll+# anywhere. It replaces dx8_smoke, which proved the same thing about the vendored+# DirectX 8 SDK and the d3d8.dll the renderer used to load; that path is gone, and+# test_dx8_smoke.cpp stays in the tree unbuilt as the record of what it measured.+# d3d9.h and d3d9.lib come from the Windows SDK, so this target never sees+# Libraries/DirectX/Include.add_executable(dx9_smoke ${WW}/WW3D2/test_dx9_smoke.cpp)target_link_libraries(dx9_smoke PRIVATE d3dx9runtime user32)@@ -368,11 +362,12 @@ set(GS_BUILD_TESTS OFF CACHE BOOL "" FORCE)set(GS_INCLUDE_VOICE OFF CACHE BOOL "" FORCE)add_subdirectory(${LIBSRC}/GameSpy ${CMAKE_CURRENT_BINARY_DIR}/gamespy)-# d3d8to9 (vendored, github.com/crosire/d3d8to9): builds our own d3d8.dll that-# translates every D3D8 call to Direct3D 9. It sits next to generals.exe, the-# loader prefers the application directory over system32, and the whole-# renderer runs on the DX9 runtime without touching the ~5600 D3D8 call sites.-add_subdirectory(${LIBSRC}/d3d8to9 ${CMAKE_CURRENT_BINARY_DIR}/d3d8to9)+# d3d8to9 is no longer built. It was a d3d8.dll next to generals.exe that translated+# every D3D8 call to Direct3D 9, which is how the renderer ran on a modern runtime+# without touching the ~5600 D3D8 call sites. The renderer creates its own+# IDirect3DDevice9 now (RENDERER-ROADMAP.md phase 1), so nothing loads that dll. The+# vendored source stays in Libraries/Source/d3d8to9: WW3D2/d3d8shadertranslate.cpp is+# lifted from it, and it is the reference for how a D3D8 call was being answered.# ===========================================================================# Phase 3 - GameEngine. ~580 files of game logic; no DX8, no device code.@@ -522,9 +517,6 @@ add_custom_command(TARGET generals POST_BUILD# and without it the crash log's stack is addresses with no names.COMMAND ${CMAKE_COMMAND} -E copy_if_different$<TARGET_PDB_FILE:generals> ${CMAKE_CURRENT_SOURCE_DIR}/../Run/- # d3d8to9's d3d8.dll next to the exe = the renderer runs on Direct3D 9.- COMMAND ${CMAKE_COMMAND} -E copy_if_different- $<TARGET_FILE:d3d8to9> ${CMAKE_CURRENT_SOURCE_DIR}/../Run/# The fork's key bindings and their labels. Run/ is gitignored, so these two# used to be hand-made files that silently went stale every time a command was# added - the tracked copies under Code/Data are the masters and this puts them@@ -568,7 +560,6 @@ add_custom_command(TARGET generals POST_BUILDCOMMAND ${CMAKE_COMMAND} -E copy_directory${CMAKE_CURRENT_SOURCE_DIR}/Data/Scenarios${CMAKE_CURRENT_SOURCE_DIR}/../Run/Scenarios)-add_dependencies(generals d3d8to9)# ===========================================================================# Tests. Run with: ctest --test-dir build -C Release --output-on-failure@@ -640,17 +631,13 @@ target_compile_definitions(test_gameengine PRIVATE# too so one command covers everything.add_test(NAME wwmath_selfcheck COMMAND wwmath_selfcheck)add_test(NAME compression_selfcheck COMMAND compression_selfcheck)-# dx8_smoke runs through our d3d8to9 d3d8.dll (copied next to it): once on its default,-# Direct3D 9, once opted in to Direct3D 9On12 / Direct3D 12 via -d3d12 (each run fails if-# it did not land on its runtime)-add_custom_command(TARGET dx8_smoke POST_BUILD- COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:d3d8to9> $<TARGET_FILE_DIR:dx8_smoke>)-add_dependencies(dx8_smoke d3d8to9)-add_test(NAME dx8_smoke COMMAND dx8_smoke)-add_test(NAME dx8_smoke_d3d12 COMMAND dx8_smoke -d3d12)-# dx9_smoke deliberately does not get d3d8.dll copied next to it: it calls-# Direct3DCreate9 itself and a translating dll would hide whether that worked.+# dx9_smoke creates a device the way the renderer does, with nothing next to it to+# translate anything, and puts the D3D8 calls that have no D3D9 counterpart through their+# replacements. "-msaa 4" adds the multisampled back buffer and the plain render target+# with a depth buffer of its own that DX8Wrapper::_Get_Non_MultiSampled_Depth_Buffer pairs+# for the screen filters.add_test(NAME dx9_smoke COMMAND dx9_smoke)+add_test(NAME dx9_smoke_msaa COMMAND dx9_smoke -msaa 4)# bink_smoke validates the hand-reconstructed Bink ABI against the real# BINKW32.DLL the game ships - it loads that DLL by path, so it does not link@@ -659,9 +646,6 @@ add_executable(bink_smoke ${TESTS}/bink_smoke.c)target_include_directories(bink_smoke PRIVATE ${WW}/Bink/include)add_test(NAME bink_smoke COMMAND bink_smokeWORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../Run)-# "-msaa": a multisampled back buffer, and the plain render target + non-multisampled depth-# buffer pairing that DX8Wrapper::_Get_Non_MultiSampled_Depth_Buffer hands the screen filters.-add_test(NAME dx8_smoke_msaa COMMAND dx8_smoke -msaa 4)# Tools/bigfile.py reads and writes the game's BIG archives (that is how the shipped INIs# come out for editing). Its selfcheck round-trips synthetic files, so it needs no gamediff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cppindex ab25eea7a..5a06cac08 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp@@ -977,16 +977,13 @@ void W3DDisplay::init( void )} // end if- extern bool DX8Wrapper_IsWindowed; // dx8wrapper.cpp- // which runtime the device really landed on - d3d8.dll in the exe directory is our d3d8to9,- // so normally Direct3D 9, or Direct3D 9On12 (Direct3D 12) when the player started with -d3d12- // and is fullscreen (d3d8to9 keeps windowed devices on Direct3D 9 - its 9On12 windowed- // present is blank above ~640x480 on at least one machine)+ // Which runtime the device really landed on. The renderer creates an IDirect3DDevice9 itself+ // now, so there is one answer here and no translating dll to name; -d3d12 was d3d8to9's opt-in+ // and does nothing until RENDERER-ROADMAP.md's phase 5 puts a real Direct3D 12 backend behind+ // the same seam.DEBUG_LOG(("W3DDisplay::init - renderer runtime: %s\n",- GetModuleHandleA("d3d9on12.dll") ? (DX8Wrapper_IsWindowed ? "Direct3D 9 (d3d8to9; -d3d12 given, but windowed devices stay on Direct3D 9)"- : "Direct3D 12 (d3d8to9 -> Direct3D 9On12)")- : GetModuleHandleA("d3d9.dll") ? "Direct3D 9 (d3d8to9)"- : "Direct3D 8 (system d3d8.dll)"));+ GetModuleHandleA("d3d9.dll") ? "Direct3D 9 (native)"+ : "no Direct3D 9 runtime loaded"));// multisampling is opt-in with "-msaa" / "-msaa N" and silently degrades to whatever the// device supports, so log what was actually grantedDEBUG_LOG(("W3DDisplay::init - multisampling: %ux\n", DX8Wrapper::Get_MultiSample_Level()));diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/test_dx9_smoke.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/test_dx9_smoke.cppindex 19f0e3dd1..b33da02ff 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/test_dx9_smoke.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/test_dx9_smoke.cpp@@ -142,8 +142,43 @@ static bool channel_is_near(BYTE actual, BYTE expected)return difference <= (int)CHANNEL_TOLERANCE && difference >= -(int)CHANNEL_TOLERANCE;}-int main()+// "-msaa N": ask for a multisampled back buffer, then build the pairing+// DX8Wrapper::_Get_Non_MultiSampled_Depth_Buffer exists for. A multisampled back buffer+// gets a multisampled auto depth/stencil, and Direct3D requires the depth buffer to match+// the render target, so the plain render-target textures the screen filters and the water+// reflection draw into need a non-multisampled depth buffer of their own.+static int requested_multisample_level(int argument_count, char ** arguments){+ for (int index = 1; index + 1 < argument_count; ++index) {+ if (strcmp(arguments[index], "-msaa") == 0) {+ return atoi(arguments[index + 1]);+ }+ }+ return 0;+}++static D3DMULTISAMPLE_TYPE highest_supported_multisample(IDirect3D9 * direct3D,+ const D3DPRESENT_PARAMETERS & present, int requested)+{+ const int MAX_SAMPLES = 16;+ if (requested < 2) {+ return D3DMULTISAMPLE_NONE;+ }+ for (int samples = requested > MAX_SAMPLES ? MAX_SAMPLES : requested; samples >= 2; --samples) {+ const D3DMULTISAMPLE_TYPE type = (D3DMULTISAMPLE_TYPE)samples;+ if (FAILED(direct3D->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,+ present.BackBufferFormat, TRUE, type, NULL))) continue;+ if (FAILED(direct3D->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,+ present.AutoDepthStencilFormat, TRUE, type, NULL))) continue;+ return type;+ }+ return D3DMULTISAMPLE_NONE;+}++int main(int argument_count, char ** arguments)+{+ const int requested_samples = requested_multisample_level(argument_count, arguments);+Owned<IDirect3D9> direct3D;direct3D.Take(Direct3DCreate9(D3D_SDK_VERSION));if (direct3D == NULL) {@@ -181,6 +216,19 @@ int main()present.AutoDepthStencilFormat = D3DFMT_D16;present.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;+ const D3DMULTISAMPLE_TYPE multisample =+ highest_supported_multisample(direct3D, present, requested_samples);+ if (requested_samples >= 2) {+ printf("msaa: %dx requested, %dx supported\n", requested_samples, (int)multisample);+ if (multisample == D3DMULTISAMPLE_NONE) {+ printf("FAIL: no multisample level up to %dx is supported\n", requested_samples);+ return 1;+ }+ present.MultiSampleType = multisample;+ present.SwapEffect = D3DSWAPEFFECT_DISCARD; // the only swap effect D3D allows with multisampling+ present.Flags = 0; // and a multisampled back buffer cannot be lockable+ }+Owned<IDirect3DDevice9> device;HRESULT created = direct3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window.Get(),D3DCREATE_MIXED_VERTEXPROCESSING, &present, &device);@@ -197,8 +245,9 @@ int main()}// Read the cleared back buffer back, so a runtime that created a device and painted- // nothing does not pass.- {+ // nothing does not pass. A multisampled back buffer cannot be locked, so that run+ // proves itself with the render-to-texture readback below instead.+ if (multisample == D3DMULTISAMPLE_NONE) {Owned<IDirect3DSurface9> backBuffer;if (FAILED(device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer))) {printf("FAIL: GetBackBuffer\n");@@ -257,6 +306,57 @@ int main()}printf("replacements: sampler states, depth bias, offscreen surface, render-target readback\n");+ // The pairing DX8Wrapper::_Get_Non_MultiSampled_Depth_Buffer exists for: a plain render+ // target texture with a depth buffer of its own, while the back buffer is multisampled.+ // Cleared green and read back, so a target that was bound but never drawn into fails.+ if (multisample != D3DMULTISAMPLE_NONE) {+ Owned<IDirect3DTexture9> renderTexture;+ Owned<IDirect3DSurface9> renderSurface;+ Owned<IDirect3DSurface9> renderDepth;+ Owned<IDirect3DSurface9> backBuffer;+ Owned<IDirect3DSurface9> autoDepth;+ Owned<IDirect3DSurface9> readback;+ if (FAILED(device->CreateTexture(RENDER_TARGET_EDGE, RENDER_TARGET_EDGE, 1,+ D3DUSAGE_RENDERTARGET, present.BackBufferFormat, D3DPOOL_DEFAULT, &renderTexture, NULL))+ || FAILED(renderTexture->GetSurfaceLevel(0, &renderSurface))+ || FAILED(device->CreateDepthStencilSurface(RENDER_TARGET_EDGE, RENDER_TARGET_EDGE,+ present.AutoDepthStencilFormat, D3DMULTISAMPLE_NONE, 0, TRUE, &renderDepth, NULL))+ || FAILED(device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer))+ || FAILED(device->GetDepthStencilSurface(&autoDepth))+ || FAILED(device->CreateOffscreenPlainSurface(RENDER_TARGET_EDGE, RENDER_TARGET_EDGE,+ present.BackBufferFormat, D3DPOOL_SYSTEMMEM, &readback, NULL))) {+ printf("FAIL: could not build the render-to-texture surfaces\n");+ return 1;+ }++ if (FAILED(device->SetRenderTarget(0, renderSurface))+ || FAILED(device->SetDepthStencilSurface(renderDepth))+ || FAILED(device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,+ D3DCOLOR_XRGB(0, 255, 0), MAX_DEPTH, 0))+ || FAILED(device->GetRenderTargetData(renderSurface, readback))+ || FAILED(device->SetRenderTarget(0, backBuffer))+ || FAILED(device->SetDepthStencilSurface(autoDepth))) {+ printf("FAIL: render to a plain target with its own depth buffer\n");+ return 1;+ }++ D3DLOCKED_RECT locked;+ if (FAILED(readback->LockRect(&locked, NULL, D3DLOCK_READONLY))) {+ printf("FAIL: the render texture readback is not lockable\n");+ return 1;+ }+ const BYTE * pixel = (const BYTE *)locked.pBits+ + SAMPLE_PIXEL_OFFSET * locked.Pitch + SAMPLE_PIXEL_OFFSET * BYTES_PER_PIXEL;+ const bool holdsGreen = channel_is_near(pixel[1], 255) && channel_is_near(pixel[0], 0)+ && channel_is_near(pixel[2], 0);+ printf("render texture pixel (b,g,r) = (%u,%u,%u)\n", pixel[0], pixel[1], pixel[2]);+ readback->UnlockRect();+ if (!holdsGreen) {+ printf("FAIL: the render texture does not hold the clear colour (0,255,0)\n");+ return 1;+ }+ }+// d3dx9runtime.cpp is what the renderer will bind, so the test binds the same thing// rather than a copy of it: a missing entry point fails here before it fails in a match.BoundD3DX9Runtime d3dx9;