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
feat(ww3d2): count the fixed-function work phase 2 has to write
Olcay Seygan7d1a744f14b2
diff --git a/GeneralsMD/Code/CMakeLists.txt b/GeneralsMD/Code/CMakeLists.txtindex c66b55b9c..504447b14 100644--- a/GeneralsMD/Code/CMakeLists.txt+++ b/GeneralsMD/Code/CMakeLists.txt@@ -212,7 +212,8 @@ add_library(ww3d2 STATIC${WW}/WW3D2/dx8caps.cpp ${WW}/WW3D2/dx8fvf.cpp ${WW}/WW3D2/dx8indexbuffer.cpp${WW}/WW3D2/dx8polygonrenderer.cpp ${WW}/WW3D2/dx8renderer.cpp ${WW}/WW3D2/dx8rendererdebugger.cpp${WW}/WW3D2/dx8texman.cpp ${WW}/WW3D2/dx8vertexbuffer.cpp ${WW}/WW3D2/dx8webbrowser.cpp- ${WW}/WW3D2/dx8wrapper.cpp ${WW}/WW3D2/dynamesh.cpp ${WW}/WW3D2/font3d.cpp+ ${WW}/WW3D2/dx8wrapper.cpp ${WW}/WW3D2/dynamesh.cpp ${WW}/WW3D2/ffprobe.cpp+ ${WW}/WW3D2/font3d.cpp${WW}/WW3D2/formconv.cpp ${WW}/WW3D2/hanim.cpp ${WW}/WW3D2/hanimmgr.cpp${WW}/WW3D2/hcanim.cpp ${WW}/WW3D2/hlod.cpp ${WW}/WW3D2/hmdldef.cpp${WW}/WW3D2/hmorphanim.cpp ${WW}/WW3D2/hrawanim.cpp ${WW}/WW3D2/htree.cppdiff --git a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.hindex c9b32eba7..06ee0438c 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h@@ -121,6 +121,7 @@ public:Int m_windowMode; ///< WindowModeType: fullscreen, borderless or windowed. m_windowed///< is derived from it and is what the device layer reads.Int m_msaaLevel; ///< multisampling, as an index into the levels the options menu offers+ Bool m_fixedFunctionProbe; ///< -ffprobe: count the fixed-function combinations the frame actually usesInt m_xResolution;Int m_yResolution;Int m_maxShellScreens; ///< this many shells layouts can be loaded at oncediff --git a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cppindex 6e7c9416c..32c1df0fa 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp@@ -1329,6 +1329,23 @@ Int parseMSAA(char *args[], int num)return 1;}+/* -ffprobe: count the fixed-function combinations the frame actually uses.+ *+ * RENDERER-ROADMAP.md's phase 2 replaces fixed-function multitexture with HLSL, and the size of+ * that job is not the number of combinations shader.h can express - tens of thousands - but the+ * number the game sets during a match. This reads the stage combiners and the pixel-affecting+ * render states back off the device at every draw call, so a call site that set its own states+ * without going through DX8Wrapper is counted too, and writes Run/ffprobe.txt at shutdown, most+ * used first. Reading forty states per draw is slow; pair it with -maxframes. */+Int parseFixedFunctionProbe(char *args[], int num)+{+ if (TheWritableGlobalData)+ {+ TheWritableGlobalData->m_fixedFunctionProbe = TRUE;+ }+ return 1;+}+/* -camera <x> <y>: point the camera at one map position and leave it there.** -screenshot only made a picture; it could not say of what. The camera starts at the local@@ -2084,6 +2101,7 @@ static CommandLineParam params[] ={ "-maxframes", parseMaxGameFrames },{ "-screenshot", parseScreenShot },{ "-msaa", parseMSAA },+ { "-ffprobe", parseFixedFunctionProbe },{ "-autocamera", parseAutoCamera },{ "-camera", parseCameraLook },{ "-tracemove", parseTraceMove },diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cppindex a1a31a7e5..ee60b1c50 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp@@ -646,6 +646,7 @@ GlobalData::GlobalData()m_windowed = 0;m_windowMode = WINDOW_MODE_FULLSCREEN;m_msaaLevel = 0;+ m_fixedFunctionProbe = FALSE;m_xResolution = 800;m_yResolution = 600;m_maxShellScreens = 0;diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cppindex c84960c72..144b866f1 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp@@ -46,6 +46,7 @@ static void drawFramerateBar(void);#include "Common/GlobalData.h"#include "Common/OptionsCatalog.h"#include "dx8wrapper.h"+#include "ffprobe.h"#include "Common/PerfTimer.h"#include "Common/FileSystem.h"#include "Common/LocalFileSystem.h"@@ -925,6 +926,9 @@ void W3DDisplay::init( void )// 4, 8 or 16; the device degrades an unsupported one on its own.DX8Wrapper::Set_Requested_MultiSample_Level( msaaSamplesForLevel( TheGlobalData->m_msaaLevel ) );+ // Same reason: WW3D2 cannot see GlobalData, so -ffprobe is pushed in from here.+ FixedFunctionProbe_Enable( TheGlobalData->m_fixedFunctionProbe != FALSE );+// Same problem, same answer: the filter table is built the moment the device exists and WW3D2// cannot see GlobalData, so the player's texture filtering goes in here. Nothing in the game// ever called Set_Texture_Filter, so before this the mode was whatever the WW3D2 default was.diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cppindex 22d0773ef..828c84060 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp@@ -72,6 +72,7 @@#include "thread.h"#include <stdio.h>#include "d3dx9runtime.h"+#include "ffprobe.h"#include "pot.h"#include "wwprofile.h"#include "ffactory.h"@@ -454,6 +455,9 @@ bool DX8Wrapper::Init(void * hwnd, bool lite)void DX8Wrapper::Shutdown(void){+ // Before the device goes, because the inventory is read off it.+ FixedFunctionProbe_Dump("ffprobe.txt");+if (D3DDevice) {Set_Render_Target ((IDirect3DSurface9 *)NULL);@@ -2253,6 +2257,13 @@ void DX8Wrapper::Draw(Apply_Render_State_Changes();+ // -ffprobe only: the fixed-function inventory phase 2 has to write as HLSL, counted off the+ // device rather than off this wrapper's cache, so a call site that set its own states directly+ // is seen too.+ if (FixedFunctionProbe_Is_Enabled()) {+ FixedFunctionProbe_Record(D3DDevice);+ }+// Debug feature to disable triangle drawing...if (!_Is_Triangle_Draw_Enabled()) return;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffprobe.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffprobe.cppnew file mode 100644index 000000000..5cbeb43c6--- /dev/null+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffprobe.cpp@@ -0,0 +1,253 @@+/*+** Command & Conquer Generals Zero Hour(tm)+** Copyright 2025 Electronic Arts Inc.+**+** This program is free software: you can redistribute it and/or modify+** it under the terms of the GNU General Public License as published by+** the Free Software Foundation, either version 3 of the License, or+** (at your option) any later version.+**+** This program is distributed in the hope that it will be useful,+** but WITHOUT ANY WARRANTY; without even the implied warranty of+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+** GNU General Public License for more details.+**+** You should have received a copy of the GNU General Public License+** along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++#include "ffprobe.h"++#include <map>+#include <stdio.h>+#include <vector>++// A stage is read until one turns its colour operation off, which is where the fixed-function+// pipeline stops looking as well. Eight is the device maximum the wrapper allows for.+static const unsigned MAXIMUM_STAGES = 8;++// The per-stage states that decide what the stage computes. Filtering, addressing and the+// LOD bias are sampler states in D3D9 and do not change the arithmetic, so they are not here.+static const D3DTEXTURESTAGESTATETYPE STAGE_STATES[] =+{+ D3DTSS_COLOROP, D3DTSS_COLORARG1, D3DTSS_COLORARG2, D3DTSS_COLORARG0,+ D3DTSS_ALPHAOP, D3DTSS_ALPHAARG1, D3DTSS_ALPHAARG2, D3DTSS_ALPHAARG0,+ D3DTSS_RESULTARG, D3DTSS_TEXCOORDINDEX, D3DTSS_TEXTURETRANSFORMFLAGS+};++// The render states that reach the pixel as well, and so become part of the shader or of the+// state object it is paired with rather than something the shader can ignore.+//+// D3DRS_TEXTUREFACTOR and D3DRS_ALPHAREF are deliberately not here. They are uniforms: two draws+// that differ only by the shade of a texture factor are one thing to write, and keying on their+// values would count a fade as a new combination on every frame of it.+static const D3DRENDERSTATETYPE PIXEL_RENDER_STATES[] =+{+ D3DRS_LIGHTING, D3DRS_SPECULARENABLE, D3DRS_COLORVERTEX,+ D3DRS_FOGENABLE, D3DRS_FOGTABLEMODE, D3DRS_FOGVERTEXMODE,+ D3DRS_ALPHATESTENABLE, D3DRS_ALPHAFUNC,+ D3DRS_ALPHABLENDENABLE, D3DRS_SRCBLEND, D3DRS_DESTBLEND+};++static const size_t STAGE_STATE_COUNT = sizeof(STAGE_STATES)/sizeof(STAGE_STATES[0]);+static const size_t PIXEL_RENDER_STATE_COUNT = sizeof(PIXEL_RENDER_STATES)/sizeof(PIXEL_RENDER_STATES[0]);++// A combination is the render states, then one block of stage states per live stage, then a flag+// for whether a vertex shader was doing the transform. Compared and ordered as a plain sequence,+// which is all a std::map needs of it.+typedef std::vector<DWORD> Combination;++static bool _Enabled = false;+static std::map<Combination, unsigned> _Counts;+static std::map<IDirect3DPixelShader9 *, unsigned> _PixelShaderIds;+static unsigned long long _DrawsSeen = 0;++void FixedFunctionProbe_Enable(bool enabled)+{+ _Enabled = enabled;+}++bool FixedFunctionProbe_Is_Enabled()+{+ return _Enabled;+}++void FixedFunctionProbe_Record(IDirect3DDevice9 * device)+{+ if (!_Enabled || device == NULL) {+ return;+ }+ ++_DrawsSeen;++ Combination combination;+ combination.reserve(PIXEL_RENDER_STATE_COUNT + MAXIMUM_STAGES*STAGE_STATE_COUNT + 2);++ for (size_t index = 0; index < PIXEL_RENDER_STATE_COUNT; ++index) {+ DWORD value = 0;+ device->GetRenderState(PIXEL_RENDER_STATES[index], &value);+ combination.push_back(value);+ }++ IDirect3DVertexShader9 * vertex_shader = NULL;+ device->GetVertexShader(&vertex_shader);+ combination.push_back(vertex_shader != NULL ? 1 : 0);+ if (vertex_shader != NULL) {+ vertex_shader->Release();+ }++ // A bound pixel shader replaces the stage combiners outright, so the stage states are stale+ // values the device is ignoring and recording them would count noise as variety. What matters+ // for such a draw is which shader it is, numbered in the order the probe first sees each one.+ IDirect3DPixelShader9 * pixel_shader = NULL;+ device->GetPixelShader(&pixel_shader);+ if (pixel_shader != NULL) {+ std::map<IDirect3DPixelShader9 *, unsigned>::const_iterator known = _PixelShaderIds.find(pixel_shader);+ unsigned identifier;+ if (known != _PixelShaderIds.end()) {+ identifier = known->second;+ }+ else {+ identifier = (unsigned)_PixelShaderIds.size() + 1;+ _PixelShaderIds[pixel_shader] = identifier;+ }+ combination.push_back(identifier);+ pixel_shader->Release();+ ++_Counts[combination];+ return;+ }+ combination.push_back(0);++ for (unsigned stage = 0; stage < MAXIMUM_STAGES; ++stage) {+ DWORD colour_operation = D3DTOP_DISABLE;+ device->GetTextureStageState(stage, D3DTSS_COLOROP, &colour_operation);+ if (colour_operation == D3DTOP_DISABLE) {+ break;+ }+ for (size_t index = 0; index < STAGE_STATE_COUNT; ++index) {+ DWORD value = 0;+ device->GetTextureStageState(stage, STAGE_STATES[index], &value);+ combination.push_back(value);+ }+ // Whether a texture is bound changes what the stage's arguments mean, and it is one bit+ // rather than the texture's identity: two draws differing only by which grass they sample+ // are the same thing to write in HLSL.+ IDirect3DBaseTexture9 * texture = NULL;+ device->GetTexture(stage, &texture);+ combination.push_back(texture != NULL ? 1 : 0);+ if (texture != NULL) {+ texture->Release();+ }+ }++ ++_Counts[combination];+}++static const char * stage_state_name(D3DTEXTURESTAGESTATETYPE state)+{+ switch (state) {+ case D3DTSS_COLOROP: return "COLOROP";+ case D3DTSS_COLORARG1: return "COLORARG1";+ case D3DTSS_COLORARG2: return "COLORARG2";+ case D3DTSS_COLORARG0: return "COLORARG0";+ case D3DTSS_ALPHAOP: return "ALPHAOP";+ case D3DTSS_ALPHAARG1: return "ALPHAARG1";+ case D3DTSS_ALPHAARG2: return "ALPHAARG2";+ case D3DTSS_ALPHAARG0: return "ALPHAARG0";+ case D3DTSS_RESULTARG: return "RESULTARG";+ case D3DTSS_TEXCOORDINDEX: return "TEXCOORDINDEX";+ case D3DTSS_TEXTURETRANSFORMFLAGS: return "TEXTURETRANSFORMFLAGS";+ default: return "?";+ }+}++static const char * render_state_name(D3DRENDERSTATETYPE state)+{+ switch (state) {+ case D3DRS_LIGHTING: return "LIGHTING";+ case D3DRS_SPECULARENABLE: return "SPECULARENABLE";+ case D3DRS_COLORVERTEX: return "COLORVERTEX";+ case D3DRS_FOGENABLE: return "FOGENABLE";+ case D3DRS_FOGTABLEMODE: return "FOGTABLEMODE";+ case D3DRS_FOGVERTEXMODE: return "FOGVERTEXMODE";+ case D3DRS_ALPHATESTENABLE: return "ALPHATESTENABLE";+ case D3DRS_ALPHAFUNC: return "ALPHAFUNC";+ case D3DRS_ALPHAREF: return "ALPHAREF";+ case D3DRS_ALPHABLENDENABLE: return "ALPHABLENDENABLE";+ case D3DRS_SRCBLEND: return "SRCBLEND";+ case D3DRS_DESTBLEND: return "DESTBLEND";+ case D3DRS_TEXTUREFACTOR: return "TEXTUREFACTOR";+ default: return "?";+ }+}++void FixedFunctionProbe_Dump(const char * path)+{+ if (_Counts.empty()) {+ return;+ }++ FILE * file = fopen(path, "wt");+ if (file == NULL) {+ return;+ }++ // Most used first: the head of this list is what an HLSL replacement has to get right before+ // anything else, and the tail is what can be left to a slow general path.+ std::multimap<unsigned, const Combination *> by_use;+ for (std::map<Combination, unsigned>::const_iterator entry = _Counts.begin();+ entry != _Counts.end(); ++entry) {+ by_use.insert(std::make_pair(entry->second, &entry->first));+ }++ // Two populations, and they are two different jobs. A draw with a pixel shader already has its+ // arithmetic written down and only has to be translated; a draw without one is arithmetic that+ // exists nowhere but in the stage combiners and has to be written from scratch.+ unsigned shader_combinations = 0;+ unsigned shader_draws = 0;+ for (std::map<Combination, unsigned>::const_iterator entry = _Counts.begin();+ entry != _Counts.end(); ++entry) {+ if (entry->first[PIXEL_RENDER_STATE_COUNT + 1] != 0) {+ ++shader_combinations;+ shader_draws += entry->second;+ }+ }++ fprintf(file, "%u distinct combinations over %llu draw calls\n",+ (unsigned)_Counts.size(), _DrawsSeen);+ fprintf(file, "%u of them fixed function (%llu draws), %u driven by a pixel shader (%u draws)\n",+ (unsigned)_Counts.size() - shader_combinations, _DrawsSeen - shader_draws,+ shader_combinations, shader_draws);+ fprintf(file, "%u distinct pixel shaders seen\n\n", (unsigned)_PixelShaderIds.size());++ unsigned rank = 0;+ for (std::multimap<unsigned, const Combination *>::const_reverse_iterator entry = by_use.rbegin();+ entry != by_use.rend(); ++entry) {+ const Combination & combination = *entry->second;+ fprintf(file, "#%u %u draws (%.2f%%)\n", ++rank, entry->first,+ 100.0 * entry->first / (double)(_DrawsSeen ? _DrawsSeen : 1));++ size_t position = 0;+ for (size_t index = 0; index < PIXEL_RENDER_STATE_COUNT; ++index, ++position) {+ fprintf(file, " %-22s %u\n", render_state_name(PIXEL_RENDER_STATES[index]),+ combination[position]);+ }+ fprintf(file, " %-22s %u\n", "vertex shader", combination[position++]);+ fprintf(file, " %-22s %u\n", "pixel shader", combination[position++]);++ unsigned stage = 0;+ while (position < combination.size()) {+ fprintf(file, " stage %u\n", stage++);+ for (size_t index = 0; index < STAGE_STATE_COUNT; ++index, ++position) {+ fprintf(file, " %-22s %u\n", stage_state_name(STAGE_STATES[index]),+ combination[position]);+ }+ fprintf(file, " %-22s %u\n", "texture bound", combination[position++]);+ }+ fprintf(file, "\n");+ }++ fclose(file);+ _Counts.clear();+ _PixelShaderIds.clear();+ _DrawsSeen = 0;+}diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffprobe.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffprobe.hnew file mode 100644index 000000000..6cac87c10--- /dev/null+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffprobe.h@@ -0,0 +1,48 @@+/*+** Command & Conquer Generals Zero Hour(tm)+** Copyright 2025 Electronic Arts Inc.+**+** This program is free software: you can redistribute it and/or modify+** it under the terms of the GNU General Public License as published by+** the Free Software Foundation, either version 3 of the License, or+** (at your option) any later version.+**+** This program is distributed in the hope that it will be useful,+** but WITHOUT ANY WARRANTY; without even the implied warranty of+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+** GNU General Public License for more details.+**+** You should have received a copy of the GNU General Public License+** along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++/*+** The fixed-function inventory.+**+** RENDERER-ROADMAP.md's phase 2 replaces fixed-function multitexture with HLSL, and the first thing+** that has to be known is how many different things the pipeline is actually asked to compute. The+** bit layout in shader.h can express tens of thousands of combinations; what the game sets during a+** match is a far smaller set, and it is the smaller set that has to be written as shaders.+**+** This reads the configuration back off the device at every draw, so it sees what a call site set+** directly as well as what went through DX8Wrapper, and counts each distinct combination once.+** Started with -ffprobe and dumped to Run/ffprobe.txt at shutdown.+*/++#ifndef FFPROBE_H+#define FFPROBE_H++#include <d3d9.h>++// Off unless -ffprobe was given. Reading forty states back per draw call is not free.+void FixedFunctionProbe_Enable(bool enabled);+bool FixedFunctionProbe_Is_Enabled();++// One draw call's worth: reads the stage combiners and the pixel-affecting render states off the+// device and counts the combination.+void FixedFunctionProbe_Record(IDirect3DDevice9 * device);++// Writes the inventory, most used first, and forgets it. Does nothing when nothing was recorded.+void FixedFunctionProbe_Dump(const char * path);++#endif