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.

feat(ww3d2): draw fullscreen games through direct3d 11

Olcay Seyganf4503a8dec6c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 846c663ee..7d9bf1ba4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -210,7 +210,8 @@ found and fixed — EA's own, not port damage.**
- The game draws its picture through Direct3D 11 now, and falls back to the old renderer on a machine that cannot make a Direct3D 11 device. Eight views over five maps come out within one step of one colour channel of the old picture, which is the smallest difference a screen can show.
- The Direct3D 11 picture comes with its finishing passes on, no switch needed. Explosions glow past their own edges, because the battlefield is kept brighter than white until the glow is worked out; jagged edges are smoothed; and a light sharpening brings the 2003 textures back up after the smoothing. All three run over the battlefield only, before the health bars and the command bar go on top, so the lettering is untouched. In a screen of thirty inferno cannons firing the frame averaged 11.89ms with them against 11.82ms without. The antialiasing setting on the display page is for the old renderer; `-dx11post off` on the command line gives you the plain picture, and `-dx11post fxaa` picks effects one by one. The old renderer's rolling-wave sea never went missing in a real match either: both of the game's settings files turn it off, so no map ever drew it.
-- Alt-tabbing out of a fullscreen match and back no longer leaves every building already on the map as a dark shadow with nothing standing in it. Fullscreen starts on the old picture, and coming back from the desktop switched the game over to Direct3D 11 halfway through the match. The new picture only knew about what was built after the switch, so older buildings drew as their shadow alone while a new one beside them looked fine. A match keeps the picture it started with now.
+- Fullscreen gets the Direct3D 11 picture too. The old renderer took the whole display for itself and refused the new one its window, so every fullscreen game quietly ran on Direct3D 9, and the DX9 in the corner readout was the only sign. Fullscreen is now your chosen resolution set on the monitor with the game's window over it: alt-tab drops you on the desktop at its own resolution and brightness, and coming back puts the game's on again. A 1280x720 match on a 1920x1080 screen drew 40.7 million times without one draw refused, before and after an alt-tab.
+- Alt-tabbing out of a fullscreen match and back no longer leaves every building already on the map as a dark shadow with nothing standing in it. Coming back from the desktop switched the game over to Direct3D 11 halfway through the match, and the new picture only knew about what was built after the switch, so older buildings drew as their shadow alone while a new one beside them looked fine. A match keeps the picture it started with now.
- 481 base-game textures at four times the resolution now beat Zero Hour's downscaled copies.
- A long thin texture loads at the size it was drawn at. Anything wider than eight to one used to be stretched onto a bigger, blurrier one, because that was the limit of a 2002 graphics card; the card is asked now, and modern ones have no such limit.
- Anything standing still keeps its shadow in the fog. A town you have already walked through used
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
index bd56f5f6c..68fcb07d4 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
@@ -677,6 +677,13 @@ void Reset_D3D_Device(bool active)
{
if (TheDisplay && WW3D::Is_Initted() && !TheDisplay->getWindowed())
{
+ // Under the Direct3D 11 picture nothing owns the display exclusively, so leaving loses no
+ // device: the mode and the window covering it go, and come back with the game.
+ if (Direct3D11_Present_Is_Enabled())
+ {
+ DX8Wrapper::Apply_Fullscreen_Display(active);
+ return;
+ }
if (active)
{
//switch back to desired mode when user alt-tabs back into game
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
index da6b0928c..27cad5eab 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
@@ -113,6 +113,17 @@ void Direct3D11_Release()
Active = false;
}
+bool Direct3D11_Resize(unsigned width, unsigned height)
+{
+ if (!Active) {
+ return false;
+ }
+ if (Device.Get_Width() == width && Device.Get_Height() == height) {
+ return true;
+ }
+ return Device.Resize(width, height);
+}
+
bool Direct3D11_Is_Active()
{
return Active;
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
index 60e1189e5..2d6aeb016 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
@@ -49,6 +49,9 @@ bool Direct3D11_Is_Enabled();
bool Direct3D11_Create(HWND window, unsigned width, unsigned height);
void Direct3D11_Release();
+// The swap chain's buffers at a new resolution. Nothing if they already are that size.
+bool Direct3D11_Resize(unsigned width, unsigned height);
+
// True only between a successful Create and the Release that follows it.
bool Direct3D11_Is_Active();
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
index 737ddc68b..1f8eb0c63 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
@@ -521,6 +521,8 @@ bool DX8Wrapper::Init(void * hwnd, bool lite)
return(true);
}
+static void restore_desktop_display();
+
void DX8Wrapper::Shutdown(void)
{
// Before the device goes: the inventory is read off it, and the device owns every shader the
@@ -528,6 +530,7 @@ void DX8Wrapper::Shutdown(void)
FixedFunctionProbe_Dump("ffprobe.txt");
CombinerShaderCache_Release();
Direct3D11_Release();
+ restore_desktop_display();
if (D3DDevice) {
@@ -866,6 +869,66 @@ bool DX8Wrapper::Create_Device(void)
return true;
}
+// What the fullscreen display under the Direct3D 11 picture has changed on the desktop, so leaving
+// the game can put it back. The gamma is the desktop's own ramp, read before the game's first one.
+static bool DisplayModeChanged = false;
+static bool DesktopGammaSaved = false;
+static bool GameGammaSet = false;
+static D3DGAMMARAMP DesktopGammaRamp;
+static D3DGAMMARAMP GameGammaRamp;
+
+static void set_desktop_gamma(D3DGAMMARAMP * ramp)
+{
+ HDC hdc = GetDC(NULL);
+ if (hdc) {
+ SetDeviceGammaRamp(hdc, ramp);
+ ReleaseDC(NULL, hdc);
+ }
+}
+
+static void restore_desktop_display()
+{
+ if (DesktopGammaSaved) {
+ set_desktop_gamma(&DesktopGammaRamp);
+ }
+ if (DisplayModeChanged) {
+ ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);
+ DisplayModeChanged = false;
+ }
+}
+
+void DX8Wrapper::Apply_Fullscreen_Display(bool shown)
+{
+ const bool owns_display = !IsWindowed && Direct3D11_Present_Is_Enabled();
+ if (!owns_display || !shown) {
+ restore_desktop_display();
+ if (owns_display) {
+ ::ShowWindow(_Hwnd, SW_MINIMIZE);
+ }
+ return;
+ }
+
+ DEVMODE mode;
+ ZeroMemory(&mode, sizeof(mode));
+ mode.dmSize = sizeof(mode);
+ mode.dmPelsWidth = ResolutionWidth;
+ mode.dmPelsHeight = ResolutionHeight;
+ mode.dmBitsPerPel = BitDepth;
+ mode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
+ if (ChangeDisplaySettingsEx(NULL, &mode, NULL, CDS_FULLSCREEN, NULL) == DISP_CHANGE_SUCCESSFUL) {
+ DisplayModeChanged = true;
+ }
+
+ if (::IsIconic(_Hwnd)) {
+ ::ShowWindow(_Hwnd, SW_RESTORE);
+ }
+ ::SetWindowPos(_Hwnd, HWND_TOPMOST, 0, 0, ResolutionWidth, ResolutionHeight, SWP_SHOWWINDOW);
+
+ if (GameGammaSet) {
+ set_desktop_gamma(&GameGammaRamp);
+ }
+}
+
bool DX8Wrapper::Reset_Device(bool reload_assets)
{
WWDEBUG_SAY(("Resetting device.\n"));
@@ -1221,6 +1284,23 @@ bool DX8Wrapper::Set_Render_Device(int dev, int width, int height, int bits, int
#endif
//must be either resetting existing device or creating a new one.
WWASSERT(reset_device || D3DDevice == NULL);
+
+ // The Direct3D 11 device is built beside the Direct3D 9 one rather than instead of it: 236
+ // places still call the D3D9 device directly, so taking it away would be a black screen. It
+ // is on unless -d3d9 or -headless turned it off. It comes first, so everything the Direct3D 9
+ // device makes has a Direct3D 11 copy, and only with a new device: a device refused at startup
+ // and created on a reset (an Alt-Tab) drew every building already standing as its shadow alone.
+ if (!reset_device && Direct3D11_Is_Enabled() && !Direct3D11_Is_Active()) {
+ const bool created = Direct3D11_Create((HWND)_Hwnd, ResolutionWidth, ResolutionHeight);
+ WWDEBUG_SAY(("-dx11: Direct3D 11 device %s\n", created ? "created" : "refused"));
+ }
+
+ // While Direct3D 11 presents, the Direct3D 9 device is windowed even in a fullscreen game. A
+ // Direct3D 9 device that owns the display refuses the Direct3D 11 swap chain its window, which
+ // kept every fullscreen game on the old picture. The mode goes on before the device is made or
+ // reset, so it is made in the mode it will run in.
+ const bool device_windowed = IsWindowed || Direct3D11_Present_Is_Enabled();
+ Apply_Fullscreen_Display(true);
/*
** Initialize values for D3DPRESENT_PARAMETERS members.
@@ -1229,13 +1309,13 @@ bool DX8Wrapper::Set_Render_Device(int dev, int width, int height, int bits, int
_PresentParameters.BackBufferWidth = ResolutionWidth;
_PresentParameters.BackBufferHeight = ResolutionHeight;
- _PresentParameters.BackBufferCount = IsWindowed ? 1 : 2;
+ _PresentParameters.BackBufferCount = device_windowed ? 1 : 2;
_PresentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
//I changed this to discard all the time (even when full-screen) since that the most efficient. 07-16-03 MW:
_PresentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;//IsWindowed ? D3DSWAPEFFECT_DISCARD : D3DSWAPEFFECT_FLIP; // Shouldn't this be D3DSWAPEFFECT_FLIP?
_PresentParameters.hDeviceWindow = _Hwnd;
- _PresentParameters.Windowed = IsWindowed;
+ _PresentParameters.Windowed = device_windowed;
_PresentParameters.EnableAutoDepthStencil = TRUE; // Driver will attempt to match Z-buffer depth
_PresentParameters.Flags=0; // We're not going to lock the backbuffer
@@ -1255,7 +1335,7 @@ bool DX8Wrapper::Set_Render_Device(int dev, int width, int height, int bits, int
** - if in windowed mode, the backbuffer must use the current display format.
** - the depth buffer must use
*/
- if (IsWindowed) {
+ if (device_windowed) {
D3DDISPLAYMODE desktop_mode;
::ZeroMemory(&desktop_mode, sizeof(D3DDISPLAYMODE));
@@ -1362,18 +1442,7 @@ bool DX8Wrapper::Set_Render_Device(int dev, int width, int height, int bits, int
WWDEBUG_SAY(("Reset/Create_Device done, reset_device=%d, restore_assets=%d\n", reset_device, restore_assets));
- // The Direct3D 11 device is built beside the Direct3D 9 one rather than instead of it: 236
- // places still call the D3D9 device directly, so taking it away would be a black screen. It
- // is on unless -d3d9 or -headless turned it off. Only a new device gets one: a buffer or texture
- // made before this point has no Direct3D 11 copy, so a device that was refused at startup and
- // then created on a reset (an Alt-Tab out of fullscreen) drew every building already standing
- // as nothing but its shadow.
- if (!reset_device && Direct3D11_Is_Enabled() && !Direct3D11_Is_Active()) {
- const bool created = Direct3D11_Create((HWND)_Hwnd,
- _PresentParameters.BackBufferWidth, _PresentParameters.BackBufferHeight);
- WWDEBUG_SAY(("-dx11: Direct3D 11 device %s\n", created ? "created" : "refused"));
- }
-
+ Direct3D11_Resize(ResolutionWidth, ResolutionHeight);
return ret;
}
@@ -1539,7 +1608,10 @@ bool DX8Wrapper::Set_Device_Resolution(int width,int height,int bits,int windowe
}
}
#pragma message("TODO: support changing windowed status and changing the bit depth")
- return Reset_Device();
+ Apply_Fullscreen_Display(true);
+ const bool reset = Reset_Device();
+ Direct3D11_Resize(ResolutionWidth, ResolutionHeight);
+ return reset;
} else {
return false;
}
@@ -2014,8 +2086,15 @@ void DX8Wrapper::End_Scene(bool flip_frames)
// device (DEVICELOST on the next call, then a null shroud surface when a match starts).
// Direct3D11_End_Scene has already shown the frame.
//
+ // Nothing presents on the Direct3D 9 device then, so the lost device Present reports has to
+ // be asked for: a display mode change loses a windowed device too, and every view update
+ // waits for TestCooperativeLevel to say D3D_OK before it draws anything.
+ //
if (Direct3D11_Present_Is_Enabled()) {
- hr = D3D_OK;
+ hr = _Get_D3D_Device()->TestCooperativeLevel();
+ if (hr == D3DERR_DEVICENOTRESET) {
+ hr = D3DERR_DEVICELOST;
+ }
} else {
hr=_Get_D3D_Device()->Present(NULL, NULL, NULL, NULL);
}
@@ -4207,8 +4286,22 @@ void DX8Wrapper::Set_Gamma(float gamma,float bright,float contrast,bool calibrat
ramp.blue[i]=(WORD) (out*65535);
}
- if (Get_Current_Caps()->Support_Gamma()) {
+ if (Get_Current_Caps()->Support_Gamma() && !_PresentParameters.Windowed) {
DX8Wrapper::_Get_D3D_Device()->SetGammaRamp(PRIMARY_SWAP_CHAIN,flag,&ramp);
+ } else if (Direct3D11_Present_Is_Enabled()) {
+ // A windowed Direct3D 9 device ignores its gamma ramp, so the fullscreen display under the
+ // Direct3D 11 picture sets the desktop's, keeps the desktop's own to give back on the way out,
+ // and puts the game's on again when the game comes back.
+ if (!DesktopGammaSaved) {
+ HDC hdc = GetDC(NULL);
+ if (hdc) {
+ DesktopGammaSaved = GetDeviceGammaRamp(hdc, &DesktopGammaRamp) != FALSE;
+ ReleaseDC(NULL, hdc);
+ }
+ }
+ GameGammaRamp = ramp;
+ GameGammaSet = true;
+ set_desktop_gamma(&GameGammaRamp);
} else {
HWND hwnd = GetDesktopWindow();
HDC hdc = GetDC(hwnd);
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h
index 29c86b8d0..77c6dcddd 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h
@@ -594,6 +594,12 @@ public:
static WW3DFormat getBackBufferFormat( void );
static bool Reset_Device(bool reload_assets=true);
+ // While Direct3D 11 presents, the Direct3D 9 device is always windowed and a fullscreen game is a
+ // display mode plus a window covering it, both set here. shown false gives the desktop its own
+ // mode and gamma back and minimises the window, which is what leaving the game does; true puts
+ // the game's back. A windowed game, or one Direct3D 9 presents, only gets the desktop restored.
+ static void Apply_Fullscreen_Display(bool shown);
+
static const DX8Caps* Get_Current_Caps() { WWASSERT(CurrentCaps); return CurrentCaps; }
static bool Registry_Save_Render_Device( const char * sub_key );