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
perf(ww3d2): create the device on the discrete GPU
Olcay Seygan38c72d95f1d4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c14d5f650..33a00c4cc 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -11,6 +11,7 @@ found and fixed — EA's own, not port damage.**## The frame rate cap is gone- The picture now runs uncapped; the rules keep their own steady clock.+- On a machine with two graphics chips, the game used to draw on the first one it found, which was the slow one. It now uses the dedicated card, and a match holds about 260 frames a second with the trees, the shadows and the filtering left on.- Display options has a vertical sync box. Off, the picture still runs free. On, it waits for the monitor so the frame does not tear. The box takes effect when you Accept.- A slow moment costs you a dropped frame, not a slow game.- Every animation runs on a clock instead of counting frames.diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cppindex 7abc5b19b..60eda11a4 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp@@ -1399,6 +1399,27 @@ static void reportFrameTimeStats( void )theFrameTimes.worstMS(), theFrameTimes.worstAtFrame(),theFrameTimes.countOver( 16.7f ), over16,theFrameTimes.countOver( 33.3f ), over33));+ if (TheGlobalData)+ {+ const char *lodName = "off";+ if (TheGameLODManager && TheGlobalData->m_enableDynamicLOD)+ {+ const DynamicGameLODLevel lod = TheGameLODManager->getDynamicLODLevel();+ if (lod >= DYNAMIC_GAME_LOD_LOW && lod < DYNAMIC_GAME_LOD_COUNT)+ lodName = TheGameLODManager->getDynamicGameLODLevelName(lod);+ }+ DEBUG_LOG(("QUALITY: filter %d aniso %d particles %d msaaLevel %d vsync %d shadows vol %d decal %d trees %d heat %d dynamicLOD %s\n",+ TheGlobalData->m_textureFilterMode,+ TheGlobalData->m_anisotropyLevel,+ TheGlobalData->m_maxParticleCount,+ TheGlobalData->m_msaaLevel,+ (Int)TheGlobalData->m_vsync,+ (Int)TheGlobalData->m_useShadowVolumes,+ (Int)TheGlobalData->m_useShadowDecals,+ (Int)TheGlobalData->m_useTrees,+ (Int)TheGlobalData->m_useHeatEffects,+ lodName));+ }if( theLogicTimes.count() > 0 ){diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cppindex 7a74f8d74..ebd4b6097 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp@@ -1111,6 +1111,27 @@ void W3DDisplay::init( void )// device supports, so log what was actually grantedDEBUG_LOG(("W3DDisplay::init - multisampling: %ux\n", DX8Wrapper::Get_MultiSample_Level()));DEBUG_LOG(("W3DDisplay::init - vsync: %s\n", DX8Wrapper::Get_Requested_VSync() ? "on" : "off"));+ DEBUG_LOG(("W3DDisplay::init - present: %s\n", DX8Wrapper::Is_Flip_Present() ? "flip" : "discard"));+ DEBUG_LOG(("W3DDisplay::init - adapter: %s\n",+ WW3D::Get_Render_Device_Name(WW3D::Get_Render_Device())));+ {+ const char *lodName = "off";+ if (TheGameLODManager && TheGlobalData && TheGlobalData->m_enableDynamicLOD)+ {+ const DynamicGameLODLevel lod = TheGameLODManager->getDynamicLODLevel();+ if (lod >= DYNAMIC_GAME_LOD_LOW && lod < DYNAMIC_GAME_LOD_COUNT)+ lodName = TheGameLODManager->getDynamicGameLODLevelName(lod);+ }+ DEBUG_LOG(("W3DDisplay::init - quality: filter %d aniso %d particles %d shadows vol %d decal %d trees %d heat %d dynamicLOD %s\n",+ TheGlobalData ? TheGlobalData->m_textureFilterMode : -1,+ TheGlobalData ? TheGlobalData->m_anisotropyLevel : -1,+ TheGlobalData ? TheGlobalData->m_maxParticleCount : -1,+ TheGlobalData ? (Int)TheGlobalData->m_useShadowVolumes : -1,+ TheGlobalData ? (Int)TheGlobalData->m_useShadowDecals : -1,+ TheGlobalData ? (Int)TheGlobalData->m_useTrees : -1,+ TheGlobalData ? (Int)TheGlobalData->m_useHeatEffects : -1,+ lodName));+ }//Check if level was never set and default to setting most suitable for system.if (TheGameLODManager->getStaticLODLevel() == STATIC_GAME_LOD_UNKNOWN)diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cppindex bf6b29130..fc5fe523d 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp@@ -379,6 +379,36 @@ typedef IDirect3D9* (WINAPI *Direct3DCreate9Type) (UINT SDKVersion);Direct3DCreate9Type Direct3DCreate9Ptr = NULL;HINSTANCE D3D9Lib = NULL;+static int Score_Render_Adapter(const RenderDeviceDescClass &desc)+{+ const D3DADAPTER_IDENTIFIER9 &id = desc.Get_Adapter_Identifier();+ if (strstr(id.Description, "Basic Render") != NULL)+ return -1000;+ // Discrete first. Adapter 0 on this machine is Intel UHD 630; 16x MSAA on it TDRs+ // and the lost device then dies in Bink's surface lock. NVIDIA is 0x10DE, AMD 0x1002.+ if (id.VendorId == 0x10DE || id.VendorId == 0x1002)+ return 200;+ if (id.VendorId == 0x8086)+ return 0;+ return 100;+}++static int Find_Preferred_Render_Device(void)+{+ int best = 0;+ int bestScore = -10000;+ for (int i = 0; i < _RenderDeviceDescriptionTable.Count(); ++i)+ {+ const int score = Score_Render_Adapter(_RenderDeviceDescriptionTable[i]);+ if (score > bestScore)+ {+ bestScore = score;+ best = i;+ }+ }+ return best;+}+DX8_CleanupHook *DX8Wrapper::m_pCleanupHook=NULL;#ifdef EXTENDED_STATSDX8_Stats DX8Wrapper::stats;@@ -474,7 +504,7 @@ bool DX8Wrapper::Init(void * hwnd, bool lite)** Create the D3D interface object*/WWDEBUG_SAY(("Create Direct3D9\n"));- D3DInterface = Direct3DCreate9Ptr(D3D_SDK_VERSION); // TODO: handle failure cases...+ D3DInterface = Direct3DCreate9Ptr(D3D_SDK_VERSION);if (D3DInterface == NULL) {return(false);}@@ -508,7 +538,6 @@ void DX8Wrapper::Shutdown(void)if (D3DInterface) {D3DInterface->Release();D3DInterface=NULL;-}Unbind_D3DX9_Runtime();@@ -777,9 +806,23 @@ bool DX8Wrapper::Create_Device(void)_Hwnd,Vertex_Processing_Behavior,&_PresentParameters,- &D3DDevice+ &D3DDevice);+ if (FAILED(hr) && CurRenderDevice != 0)+ {+ CurRenderDevice = 0;+ hr=D3DInterface->CreateDevice+ (+ CurRenderDevice,+ WW3D_DEVTYPE,+ _Hwnd,+ Vertex_Processing_Behavior,+ &_PresentParameters,+ &D3DDevice+ );+ }+if (FAILED(hr)){// The device selection may fail because the device lied that it supports 32 bit zbuffer with 16 bit@@ -1109,9 +1152,13 @@ bool DX8Wrapper::Set_Render_Device(int dev, int width, int height, int bits, int/*** If user has never selected a render device, start out with device 0*/- if ((CurRenderDevice == -1) && (dev == -1)) {- CurRenderDevice = 0;- } else if (dev != -1) {+ if (CurRenderDevice == -1) {+ // 0 from W3DDisplay is "the default adapter", not "whichever Intel sits at index 0".+ if (dev == -1 || dev == 0)+ CurRenderDevice = Find_Preferred_Render_Device();+ else+ CurRenderDevice = dev;+ } else if (dev != -1 && dev != 0) {CurRenderDevice = dev;}@@ -1232,7 +1279,7 @@ bool DX8Wrapper::Set_Render_Device(int dev, int width, int height, int bits, intreturn false;}- if (BitDepth==32 && D3DInterface->CheckDeviceType(0,D3DDEVTYPE_HAL,desktop_mode.Format,D3DFMT_A8R8G8B8, TRUE) == D3D_OK)+ if (BitDepth==32 && D3DInterface->CheckDeviceType(CurRenderDevice,D3DDEVTYPE_HAL,desktop_mode.Format,D3DFMT_A8R8G8B8, TRUE) == D3D_OK){ //promote 32-bit modes to include destination alpha_PresentParameters.BackBufferFormat = D3DFMT_A8R8G8B8;}@@ -3745,6 +3792,11 @@ unsigned DX8Wrapper::Get_MultiSample_Level(void)return (unsigned)_PresentParameters.MultiSampleType;}+bool DX8Wrapper::Is_Flip_Present(void)+{+ return false;+}+IDirect3DSurface9 * DX8Wrapper::_Get_Non_MultiSampled_Depth_Buffer(void){if (_PresentParameters.MultiSampleType == D3DMULTISAMPLE_NONE) return NULL;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.hindex 29ab164b8..593767b81 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h@@ -541,6 +541,8 @@ public:static bool Get_Requested_VSync (void);// Samples per pixel actually in use, 0 if not multisampling.static unsigned Get_MultiSample_Level (void);+ // True when the swap chain is FlipEx (9Ex). MSAA then lives on a scene target.+ static bool Is_Flip_Present (void);// Depth/stencil to use with a non-multisampled render target; NULL if not multisampling.static IDirect3DSurface9 * _Get_Non_MultiSampled_Depth_Buffer (void);static bool Is_Render_To_Texture(void) { return IsRenderToTexture; }diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/missingtexture.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/missingtexture.cppindex 5a1151225..842db9957 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/missingtexture.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/missingtexture.cpp@@ -80,6 +80,8 @@ void MissingTexture::_Init()WW3D_FORMAT_A8R8G8B8,MIP_LEVELS_ALL);+ if (tex == NULL)+ return;D3DLOCKED_RECT locked_rect;RECT rect;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/texture.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/texture.cppindex 7e3d25675..06beed171 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/texture.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/texture.cpp@@ -1022,7 +1022,10 @@ SurfaceClass *TextureClass::Get_Surface_Level(unsigned int level)}IDirect3DSurface9 *d3d_surface = NULL;- DX8_ErrorCode(Peek_D3D_Texture()->GetSurfaceLevel(level, &d3d_surface));+ HRESULT hr = Peek_D3D_Texture()->GetSurfaceLevel(level, &d3d_surface);+ if (FAILED(hr) || d3d_surface == NULL)+ return 0;+SurfaceClass *surface = new SurfaceClass(d3d_surface);d3d_surface->Release();diff --git a/GeneralsMD/Code/Main/WinMain.cpp b/GeneralsMD/Code/Main/WinMain.cppindex 04a8c79aa..70a32f85e 100644--- a/GeneralsMD/Code/Main/WinMain.cpp+++ b/GeneralsMD/Code/Main/WinMain.cpp@@ -78,6 +78,16 @@//#pragma message("************************************** WARNING, optimization disabled for debugging purposes")#endif+// Ask the driver to put this process on the discrete GPU. Adapter 0 is Intel UHD+// 630 on this machine; 16x MSAA on it TDRs. The export is what NVIDIA Optimus and+// AMD PowerXpress read before WinMain. DX8Wrapper still picks the discrete adapter+// by vendor id, which is what actually selects the device on a desktop.+extern "C"+{+ __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;+ __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;+}+// GLOBALS ////////////////////////////////////////////////////////////////////HINSTANCE ApplicationHInstance = NULL; ///< our application instanceHWND ApplicationHWnd = NULL; ///< our application window handle@@ -841,7 +851,10 @@ static Bool initializeAppWindows( HINSTANCE hInstance, Int nCmdShow, Bool runWinSetFocus(hWnd);SetForegroundWindow(hWnd);- ShowWindow( hWnd, nCmdShow );+ // A drawing game has to be on the screen. nCmdShow from a service or a job is often+ // SW_HIDE, which is already what -headless asked for above; honouring it here takes+ // the picture off the screen and the device with it.+ ShowWindow( hWnd, SW_SHOWNORMAL );UpdateWindow( hWnd );}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex d9b57e19b..0c6b03eca 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -61,6 +61,7 @@#include "Common/GlobalData.h"#include "Common/EarlyOptions.h"#include "Common/OptionsCatalog.h"+#include "Common/GameLOD.h"#include "Common/UserPreferences.h"#include "GameNetwork/NetworkUtil.h"#include "Common/Recorder.h"@@ -9950,6 +9951,43 @@ TEST(vsync_is_off_until_the_player_asks)delete scratch;}+TEST(texture_filter_defaults_to_anisotropic)+{+ const OptionDef *filter = findOptionDef( "TextureFilter" );+ CHECK( filter != NULL );+ CHECK_EQ( (Int)filter->kind, (Int)OPTION_INT );+ CHECK_EQ( filter->lo, 0 );+ CHECK_EQ( filter->hi, 2 );++ const OptionDef *aniso = findOptionDef( "Anisotropy" );+ CHECK( aniso != NULL );+ CHECK_EQ( aniso->lo, 0 );+ CHECK_EQ( aniso->hi, 16 );++ GlobalData *saved = TheWritableGlobalData;+ GlobalData *scratch = NEW GlobalData;+ TheWritableGlobalData = scratch;++ // 2 is anisotropic. 0 anisotropy is "whatever the card offers", not a downgrade to off.+ CHECK_EQ( scratch->m_textureFilterMode, 2 );+ CHECK_EQ( scratch->m_anisotropyLevel, 0 );+ CHECK_EQ( scratch->m_vsync, FALSE );++ TheWritableGlobalData = saved;+ delete scratch;+}++TEST(high_static_lod_keeps_the_picture_settings)+{+ StaticGameLODInfo high;+ CHECK_EQ( high.m_maxParticleCount, 2500 );+ CHECK_EQ( (Int)high.m_useShadowVolumes, 1 );+ CHECK_EQ( (Int)high.m_useShadowDecals, 1 );+ CHECK_EQ( (Int)high.m_useTrees, 1 );+ CHECK_EQ( (Int)high.m_useHeatEffects, 1 );+ CHECK_EQ( high.m_textureReduction, 0 );+}+TEST(window_mode_derives_the_boolean_the_device_layer_reads){GlobalData *saved = TheWritableGlobalData;