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(build): Direct3D 12 opt-in (-d3d12), back-buffer F12 shots, anisotropic filtering

Olcay Seyganc8537e7668ef
diff --git a/GeneralsMD/Code/CMakeLists.txt b/GeneralsMD/Code/CMakeLists.txt
index 67d12bdda..ac38a55de 100644
--- a/GeneralsMD/Code/CMakeLists.txt
+++ b/GeneralsMD/Code/CMakeLists.txt
@@ -223,7 +223,9 @@ target_compile_options(ww3d2 PRIVATE /Zc:forScope-)
add_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 + OS d3d8.dll create a real device.
+# 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)
@@ -516,7 +518,14 @@ set_target_properties(test_gameengine PROPERTIES CXX_STANDARD 17)
# 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)
if(RELEASE_DEBUG_LOGGING)
target_compile_definitions(gameengine PRIVATE RELEASE_DEBUG_LOGGING)
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
index eda4371ca..e3922d004 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
@@ -783,6 +783,17 @@ 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)
+ 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)"));
+
//Check if level was never set and default to setting most suitable for system.
if (TheGameLODManager->getStaticLODLevel() == STATIC_GAME_LOD_UNKNOWN)
TheGameLODManager->setStaticLODLevel(TheGameLODManager->findStaticLODLevel());
@@ -1668,6 +1679,9 @@ Int W3DDisplay::getLastFrameDrawCalls()
/** Draw the entire W3D Display */
//=============================================================================
//DECLARE_PERF_TIMER(W3DDisplay_draw)
+static Bool s_screenShotPending = FALSE; // F12 pressed: save the frame at the end of the next draw()
+static void saveScreenShot(void);
+
void W3DDisplay::draw( void )
{
//USE_PERF_TIMER(W3DDisplay_draw)
@@ -1994,8 +2008,13 @@ AGAIN:
TheGraphDraw->render();
TheGraphDraw->clear();
#endif
+ if (s_screenShotPending)
+ {
+ s_screenShotPending = FALSE;
+ saveScreenShot();
+ }
// render is all done!
- WW3D::End_Render();
+ WW3D::End_Render();
}
else
{
@@ -3012,8 +3031,40 @@ static void CreateBMPFile(LPTSTR pszFile, char *image, Int width, Int height)
LocalFree( (HLOCAL) pbmi);
}
-///Save Screen Capture to a file
+// A system-memory copy of the back buffer (32-bit, not multisampled), NULL when that is
+// not possible. Taken at the end of draw(), before Present: the front-buffer path is a
+// desktop capture, and on the Direct3D 12 (9On12) runtime the window is presented through
+// a DXGI flip swap chain that desktop captures do not see - the old code saved black.
+static IDirect3DSurface8 *captureBackBuffer(void)
+{
+ IDirect3DDevice8 *dev = DX8Wrapper::_Get_D3D_Device8();
+ IDirect3DSurface8 *bb = NULL;
+ if (dev == NULL || FAILED(dev->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &bb)) || bb == NULL)
+ return NULL;
+
+ D3DSURFACE_DESC desc;
+ bb->GetDesc(&desc);
+ IDirect3DSurface8 *copy = NULL;
+ if ((desc.Format == D3DFMT_X8R8G8B8 || desc.Format == D3DFMT_A8R8G8B8) && desc.MultiSampleType == D3DMULTISAMPLE_NONE)
+ {
+ if (SUCCEEDED(dev->CreateImageSurface(desc.Width, desc.Height, desc.Format, &copy)) && copy != NULL
+ && FAILED(dev->CopyRects(bb, NULL, 0, copy, NULL)))
+ {
+ copy->Release();
+ copy = NULL;
+ }
+ }
+ bb->Release();
+ return copy;
+}
+
+///Save Screen Capture to a file - deferred to the end of the next rendered frame
void W3DDisplay::takeScreenShot(void)
+{
+ s_screenShotPending = TRUE;
+}
+
+static void saveScreenShot(void)
{
char leafname[256];
char pathname[1024];
@@ -3033,24 +3084,29 @@ void W3DDisplay::takeScreenShot(void)
done = true;
}
- // Lock front buffer and copy
+ RECT bounds;
+ IDirect3DSurface8 *fb = captureBackBuffer();
+ if (fb != NULL)
+ {
+ D3DSURFACE_DESC desc;
+ fb->GetDesc(&desc);
+ SetRect(&bounds, 0, 0, desc.Width, desc.Height);
+ }
+ else
+ {
+ // Lock front buffer and copy
+ fb=DX8Wrapper::_Get_DX8_Front_Buffer();
- IDirect3DSurface8 *fb;
- fb=DX8Wrapper::_Get_DX8_Front_Buffer();
- D3DSURFACE_DESC desc;
- fb->GetDesc(&desc);
+ POINT point;
+ GetClientRect(ApplicationHWnd,&bounds);
+ point.x=bounds.left; point.y=bounds.top;
+ ClientToScreen(ApplicationHWnd, &point);
+ bounds.left=point.x; bounds.top=point.y;
+ point.x=bounds.right; point.y=bounds.bottom;
+ ClientToScreen(ApplicationHWnd, &point);
+ bounds.right=point.x; bounds.bottom=point.y;
+ }
- RECT bounds;
- POINT point;
-
- GetClientRect(ApplicationHWnd,&bounds);
- point.x=bounds.left; point.y=bounds.top;
- ClientToScreen(ApplicationHWnd, &point);
- bounds.left=point.x; bounds.top=point.y;
- point.x=bounds.right; point.y=bounds.bottom;
- ClientToScreen(ApplicationHWnd, &point);
- bounds.right=point.x; bounds.bottom=point.y;
-
D3DLOCKED_RECT lrect;
DX8_ErrorCode(fb->LockRect(&lrect,&bounds,D3DLOCK_READONLY));
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/test_dx8_smoke.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/test_dx8_smoke.cpp
index 815129def..560b22c31 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/test_dx8_smoke.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/test_dx8_smoke.cpp
@@ -1,13 +1,46 @@
-// Phase 1 checkpoint: prove the vendored DX8 SDK headers/libs and the OS's
-// d3d8.dll actually work - create a windowed device on a hidden window, clear,
-// present. Prints adapter info; "DX8 smoke OK" means pass.
+// Phase 1 checkpoint: prove the vendored DX8 SDK headers/libs and the d3d8.dll
+// we end up with actually work - create a windowed device on a hidden window,
+// clear, present. Prints adapter info and which runtime the device landed on;
+// "DX8 smoke OK" means pass. The build copies our d3d8to9 d3d8.dll next to the
+// exe, so the CTest runs go through it like the game does: plain (d3d8to9's
+// default, Direct3D 9) and with "-d3d12" (the opt-in, Direct3D 9On12 ->
+// Direct3D 12); each run requires to have really landed on its runtime.
#include <windows.h>
#include <d3d8.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
-int main(void)
+int main(int argc, char **argv)
{
+ bool wantD3D12 = false;
+ bool show = false; // "-show": a visible window, cleared purple and presented for 8 s - for a human to check the present path
+ bool exact = false; // "-exact": client area exactly the back buffer size (like the game; overlay/direct-flip eligible)
+ bool popup = false; // "-popup": the game's window style + black background brush
+ bool big = false; // "-big": 1280x768 back buffer instead of 640x480
+ for (int i = 1; i < argc; i++) {
+ if (strcmp(argv[i], "-d3d12") == 0) wantD3D12 = true;
+ if (strcmp(argv[i], "-show") == 0) show = true;
+ if (strcmp(argv[i], "-exact") == 0) exact = true;
+ if (strcmp(argv[i], "-popup") == 0) popup = true;
+ if (strcmp(argv[i], "-big") == 0) big = true;
+ }
+ bool vsync = false, copy = false, flip = false, bb2 = false; // present-parameter variants for the human check
+ for (int i = 1; i < argc; i++) {
+ if (strcmp(argv[i], "-vsync") == 0) vsync = true; // D3DPRESENT_INTERVAL_ONE (d3d8 windowed is always IMMEDIATE otherwise)
+ if (strcmp(argv[i], "-copy") == 0) copy = true; // D3DSWAPEFFECT_COPY
+ if (strcmp(argv[i], "-flip") == 0) flip = true; // D3DSWAPEFFECT_FLIP
+ if (strcmp(argv[i], "-bb2") == 0) bb2 = true; // BackBufferCount = 2
+ }
+ int bbW = big ? 1280 : 640, bbH = big ? 768 : 480, posX = 100, posY = 100;
+ for (int i = 1; i + 1 < argc; i++) { // "-w N -h N": back buffer size, "-x N -y N": window position
+ if (strcmp(argv[i], "-w") == 0) bbW = atoi(argv[i + 1]);
+ if (strcmp(argv[i], "-h") == 0) bbH = atoi(argv[i + 1]);
+ if (strcmp(argv[i], "-x") == 0) posX = atoi(argv[i + 1]);
+ if (strcmp(argv[i], "-y") == 0) posY = atoi(argv[i + 1]);
+ }
+
IDirect3D8 * d3d = Direct3DCreate8(D3D_SDK_VERSION);
if (!d3d) { printf("FAIL: Direct3DCreate8 returned NULL\n"); return 1; }
@@ -20,31 +53,102 @@ int main(void)
wc.lpfnWndProc = DefWindowProcA;
wc.hInstance = GetModuleHandleA(NULL);
wc.lpszClassName = "dx8smoke";
+ if (popup) wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // the game's class (WinMain.cpp)
RegisterClassA(&wc);
- HWND hwnd = CreateWindowA("dx8smoke", "dx8smoke", WS_OVERLAPPEDWINDOW,
- 0, 0, 64, 64, NULL, NULL, wc.hInstance, NULL);
+ DWORD style = popup ? (WS_POPUP | WS_DLGFRAME | WS_CAPTION | WS_SYSMENU) : WS_OVERLAPPEDWINDOW;
+ if (show) style |= WS_VISIBLE;
+ RECT wr = { 0, 0, show ? bbW : 64, show ? bbH : 64 };
+ if (exact) AdjustWindowRect(&wr, style, FALSE); // client == back buffer, as the game sizes its window
+ HWND hwnd = CreateWindowA("dx8smoke", "dx8smoke - should be PURPLE", style,
+ posX, posY, wr.right - wr.left, wr.bottom - wr.top, NULL, NULL, wc.hInstance, NULL);
if (!hwnd) { printf("FAIL: CreateWindow\n"); d3d->Release(); return 1; }
D3DDISPLAYMODE mode;
d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode);
+ // the game's own choices (dx8wrapper.cpp): windowed, discard, auto depth, mixed vertex
+ // processing - plus a lockable back buffer so the result can be read back and checked
D3DPRESENT_PARAMETERS pp = {0};
+ if (show) { pp.BackBufferWidth = bbW; pp.BackBufferHeight = bbH; }
pp.Windowed = TRUE;
pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
pp.BackBufferFormat = mode.Format;
pp.hDeviceWindow = hwnd;
+ pp.EnableAutoDepthStencil = TRUE;
+ pp.AutoDepthStencilFormat = D3DFMT_D16;
+ pp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
+ // d3d8to9 makes every windowed present IMMEDIATE; only D3DSWAPEFFECT_COPY_VSYNC reaches D3D9 as COPY + INTERVAL_ONE
+ if (vsync) pp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
+ if (copy) pp.SwapEffect = D3DSWAPEFFECT_COPY;
+ if (flip) pp.SwapEffect = D3DSWAPEFFECT_FLIP;
+ if (bb2) pp.BackBufferCount = 2;
IDirect3DDevice8 * dev = NULL;
HRESULT hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
- D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pp, &dev);
+ D3DCREATE_MIXED_VERTEXPROCESSING, &pp, &dev);
if (hr != D3D_OK || !dev) {
printf("FAIL: CreateDevice hr=0x%08lx\n", (unsigned long)hr);
d3d->Release();
return 1;
}
- dev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(64, 0, 128), 1.0f, 0);
- dev->Present(NULL, NULL, NULL, NULL);
+ dev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(64, 0, 128), 1.0f, 0);
+
+ // read the cleared back buffer back: proves the runtime really rendered, not just created
+ // (a D3D12 path that only returned a device but painted nothing would pass otherwise)
+ {
+ IDirect3DSurface8 * bb = NULL;
+ if (dev->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &bb) == D3D_OK && bb) {
+ D3DLOCKED_RECT lr;
+ if (bb->LockRect(&lr, NULL, D3DLOCK_READONLY) == D3D_OK) {
+ const unsigned char * px = (const unsigned char *)lr.pBits + 10 * lr.Pitch + 10 * 4;
+ printf("back buffer pixel (b,g,r) = (%u,%u,%u)\n", px[0], px[1], px[2]);
+ bool ok = px[2] > 48 && px[2] < 80 && px[1] < 16 && px[0] > 112 && px[0] < 144;
+ bb->UnlockRect();
+ if (!ok) {
+ printf("FAIL: back buffer does not hold the clear colour (64,0,128)\n");
+ bb->Release(); dev->Release(); d3d->Release(); DestroyWindow(hwnd);
+ return 1;
+ }
+ } else {
+ printf("(back buffer not lockable on this runtime - pixel check skipped)\n");
+ }
+ bb->Release();
+ }
+ }
+ HRESULT phr = dev->Present(NULL, NULL, NULL, NULL);
+ printf("Present hr=0x%08lx\n", (unsigned long)phr);
+ if (show) {
+ // keep clearing + presenting for 8 s while pumping messages, so a human can look
+ const DWORD until = GetTickCount() + 8000;
+ while (GetTickCount() < until) {
+ MSG msg;
+ while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessageA(&msg); }
+ dev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(160, 0, 200), 1.0f, 0);
+ dev->Present(NULL, NULL, NULL, NULL);
+ Sleep(16);
+ }
+ }
+
+ // which runtime the d3d8.dll we ended up with put the device on: the system d3d8.dll
+ // (Direct3D 8), our d3d8to9 (Direct3D 9), or its "-d3d12" / D3D8TO9_D3D12 opt-in
+ // (Direct3D 9On12 -> Direct3D 12). Checked while the device is alive: d3d9 unloads
+ // d3d9on12.dll/d3d12.dll again once the last object is released.
+ printf("runtime: %s\n", GetModuleHandleA("d3d9on12.dll") ? "Direct3D 12 (9On12)"
+ : GetModuleHandleA("d3d9.dll") ? "Direct3D 9"
+ : "Direct3D 8");
+ printf("loaded: d3d9=%d d3d9on12=%d d3d12=%d D3D12Core=%d\n",
+ GetModuleHandleA("d3d9.dll") != NULL, GetModuleHandleA("d3d9on12.dll") != NULL,
+ GetModuleHandleA("d3d12.dll") != NULL, GetModuleHandleA("D3D12Core.dll") != NULL);
+ const bool onD3D12 = GetModuleHandleA("d3d9on12.dll") != NULL && GetModuleHandleA("d3d12.dll") != NULL;
+ if (wantD3D12 ? !onD3D12 : onD3D12) {
+ printf(wantD3D12 ? "FAIL: -d3d12 requested but the device is not on Direct3D 12 (d3d8to9 fell back, or this is not our d3d8.dll)\n"
+ : "FAIL: the device is on Direct3D 9On12 without -d3d12\n");
+ dev->Release();
+ d3d->Release();
+ DestroyWindow(hwnd);
+ return 1;
+ }
dev->Release();
d3d->Release();
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/texturefilter.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/texturefilter.cpp
index e8c2d4725..59cab76b0 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/texturefilter.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/texturefilter.cpp
@@ -187,12 +187,20 @@ void TextureFilterClass::_Init_Filters(TextureFilterMode filter_type)
}
// Set default to best. The level of best filter mode is controlled by the input parameter.
+ // Anisotropy level: retail hard-coded 2x (2003 fill-rate budget); take what the device
+ // offers, capped at 16x, which is what every modern GPU does at no visible cost.
+ DWORD maxAnisotropy = 1;
+ if (filter_type==TEXTURE_FILTER_ANISOTROPIC) {
+ maxAnisotropy = dx8caps.MaxAnisotropy;
+ if (maxAnisotropy > 16) maxAnisotropy = 16;
+ if (maxAnisotropy < 1) maxAnisotropy = 1;
+ }
for (i=0;i<MAX_TEXTURE_STAGES;++i) {
_MinTextureFilters[i][FILTER_TYPE_DEFAULT]=_MinTextureFilters[i][FILTER_TYPE_BEST];
_MagTextureFilters[i][FILTER_TYPE_DEFAULT]=_MagTextureFilters[i][FILTER_TYPE_BEST];
_MipMapFilters[i][FILTER_TYPE_DEFAULT]=_MipMapFilters[i][FILTER_TYPE_BEST];
- DX8Wrapper::Set_DX8_Texture_Stage_State(i,D3DTSS_MAXANISOTROPY,2);
+ DX8Wrapper::Set_DX8_Texture_Stage_State(i,D3DTSS_MAXANISOTROPY,maxAnisotropy);
}
}
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ww3d.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ww3d.cpp
index 78933852f..1708146a9 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ww3d.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ww3d.cpp
@@ -226,7 +226,11 @@ static bool _LargeTextureExtraReductionEnabled = false;
int WW3D::LastFrameMemoryAllocations;
int WW3D::LastFrameMemoryFrees;
-int WW3D::TextureFilter = 0;
+// Nothing in the game ever calls Set_Texture_Filter, so this default IS the game's texture
+// filtering. Retail shipped 0 = bilinear with point mip selection (a 2003 fill-rate
+// budget); any GPU of this century does anisotropic + trilinear for free, so that is the
+// default now (the level is taken from the device caps in TextureFilterClass::_Init_Filters).
+int WW3D::TextureFilter = TextureFilterClass::TEXTURE_FILTER_ANISOTROPIC;
bool WW3D::Lite = false;
diff --git a/GeneralsMD/Code/Libraries/Source/d3d8to9-d3d9on12.patch b/GeneralsMD/Code/Libraries/Source/d3d8to9-d3d9on12.patch
new file mode 100644
index 000000000..db3e28ade
--- /dev/null
+++ b/GeneralsMD/Code/Libraries/Source/d3d8to9-d3d9on12.patch
@@ -0,0 +1,120 @@
+--- a/source/d3d8to9.cpp
++++ b/source/d3d8to9.cpp
+@@ -5,6 +5,9 @@
+
+ #include "d3dx9.hpp"
+ #include "d3d8to9.hpp"
++#include <d3d9on12.h>
++#include <cstring>
++#include <fstream>
+
+ PFN_D3DXAssembleShader D3DXAssembleShader = nullptr;
+ PFN_D3DXDisassembleShader D3DXDisassembleShader = nullptr;
+@@ -149,7 +152,66 @@
+ LOG << "> Passing on to 'Direct3DCreate9':" << std::endl;
+ #endif
+
+- IDirect3D9 *const d3d = Direct3DCreate9(D3D_SDK_VERSION);
++ // Local patch (CnC Generals ZH port): "-d3d12" on the process command line, or the
++ // environment variable D3D8TO9_D3D12, creates the Direct3D 9 object through Direct3D9On12
++ // so the whole Direct3D 8 renderer above runs on the Direct3D 12 runtime and driver.
++ // Opt-in, not default: the frames render correctly (the game's own back-buffer screenshot
++ // is pixel-correct) but on at least one Windows 11 machine the 9On12 windowed present
++ // shows a black window. Needs Windows 10 1903+ (d3d9.dll exports Direct3DCreate9On12);
++ // anything else, or a failed 9On12 creation, falls back to the plain Direct3D 9 path below.
++ IDirect3D9 *d3d = nullptr;
++ {
++ // The UNICODE command line, not the ANSI one: the game's WinMain tokenizes lpCmdLine in
++ // place, and that IS the process's ANSI command-line buffer, so GetCommandLineA() is
++ // already cut short (".. generals.exe -win") by the time the renderer gets here. The
++ // wide buffer is untouched.
++ const wchar_t *const cmdLine = GetCommandLineW();
++ const bool wantD3D12 = (cmdLine != nullptr && wcsstr(cmdLine, L"-d3d12") != nullptr) ||
++ GetEnvironmentVariableA("D3D8TO9_D3D12", nullptr, 0) > 0;
++ {
++ // always leave a trace of the decision next to the exe (one short file) - the
++ // process can be started in ways where neither switch reaches us, and without
++ // this there is no way to tell that from a 9On12 failure
++ std::ofstream trace("d3d8to9_d3d12.log", std::ios::trunc);
++ if (trace.is_open())
++ {
++ trace << "Direct3DCreate8: cmdline='";
++ for (const wchar_t *c = cmdLine; c != nullptr && *c != 0; ++c)
++ trace << static_cast<char>(*c < 128 ? *c : '?');
++ trace << "' env D3D8TO9_D3D12=" << GetEnvironmentVariableA("D3D8TO9_D3D12", nullptr, 0)
++ << " -> " << (wantD3D12 ? "Direct3D 12 requested" : "Direct3D 9 (default)") << std::endl;
++ }
++ }
++ if (wantD3D12)
++ {
++ const HMODULE d3d9Module = LoadLibrary(TEXT("d3d9.dll"));
++ const PFN_Direct3DCreate9On12 create9On12 = d3d9Module != nullptr
++ ? reinterpret_cast<PFN_Direct3DCreate9On12>(GetProcAddress(d3d9Module, "Direct3DCreate9On12"))
++ : nullptr;
++ if (create9On12 != nullptr)
++ {
++ D3D9ON12_ARGS args = {};
++ args.Enable9On12 = TRUE;
++ d3d = create9On12(D3D_SDK_VERSION, &args, 1);
++ }
++ // Say what happened - the game's own log cannot see in here and the fallback below
++ // is otherwise silent. One line, next to the exe.
++ const char *const outcome = create9On12 == nullptr ? "Direct3DCreate9On12 not exported by this d3d9.dll (needs Windows 10 1903+), using Direct3D 9"
++ : d3d == nullptr ? "Direct3DCreate9On12 failed, using Direct3D 9"
++ : "Direct3D 9On12 created, rendering on the Direct3D 12 runtime";
++ std::ofstream d3d12Log("d3d8to9_d3d12.log", std::ios::app);
++ if (d3d12Log.is_open())
++ d3d12Log << outcome << std::endl;
++#ifndef D3D8TO9NOLOG
++ LOG << "> Direct3D 12: " << outcome << std::endl;
++#endif
++ }
++ }
++
++ if (d3d == nullptr)
++ {
++ d3d = Direct3DCreate9(D3D_SDK_VERSION);
++ }
+
+ if (d3d == nullptr)
+ {
+--- a/source/d3d8to9_base.cpp
++++ b/source/d3d8to9_base.cpp
+@@ -4,6 +4,7 @@
+ */
+
+ #include "d3d8to9.hpp"
++#include <fstream>
+
+ static const D3DFORMAT AdapterFormats[] = {
+ D3DFMT_A8R8G8B8,
+@@ -182,7 +183,28 @@
+
+ IDirect3DDevice9 *DeviceInterface = nullptr;
+
+- const HRESULT hr = ProxyInterface->CreateDevice(Adapter, DeviceType, hFocusWindow, BehaviorFlags, &PresentParams, &DeviceInterface);
++ // Local patch (CnC Generals ZH port): a WINDOWED device never goes on Direct3D 9On12.
++ // On a Windows 11 25H2 / GeForce RTX 4060 Ti machine 9On12 renders every frame correctly
++ // but its windowed present shows nothing (the window keeps its GDI background) once the
++ // back buffer is larger than about 640x480 - found by bisection with dx8_smoke -show:
++ // 640x481, 800x480, 640x600, 1024x240 are fine, 800x600 and up are blank, regardless of
++ // window style, swap effect, back buffer count or present interval; fullscreen is fine.
++ // So a windowed device is created from a plain Direct3D 9 object instead (the caps the
++ // app already queried came from 9On12, same adapter - close enough for a Direct3D 8 app).
++ IDirect3D9 *CreateFrom = ProxyInterface;
++ if (PresentParams.Windowed && GetModuleHandle(TEXT("d3d9on12.dll")) != nullptr)
++ {
++ static IDirect3D9 *PlainD3D9 = nullptr; // kept for the process lifetime, like the 9On12 one
++ if (PlainD3D9 == nullptr)
++ PlainD3D9 = Direct3DCreate9(D3D_SDK_VERSION);
++ if (PlainD3D9 != nullptr)
++ CreateFrom = PlainD3D9;
++ std::ofstream d3d12Log("d3d8to9_d3d12.log", std::ios::app);
++ if (d3d12Log.is_open())
++ d3d12Log << "CreateDevice: windowed -> " << (CreateFrom == PlainD3D9 ? "plain Direct3D 9 (9On12 windowed present is blank above ~640x480)" : "9On12 (Direct3DCreate9 failed)") << std::endl;
++ }
++
++ const HRESULT hr = CreateFrom->CreateDevice(Adapter, DeviceType, hFocusWindow, BehaviorFlags, &PresentParams, &DeviceInterface);
+ if (FAILED(hr))
+ return hr;
+