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): skip d3d11 state object lookups while render states hold
Olcay Seygane34c0acb006e
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cppindex dc18e612b..2e9b0fa77 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp@@ -251,6 +251,7 @@ DX11BackendClass::DX11BackendClass()EngineConstantsHeld = false;ConstantsChanged = true;PipelineChanged = true;+ StateObjectsChanged = true;for (unsigned sampler = 0; sampler < DX11_BACKEND_TEXTURE_STAGES; ++sampler) {SamplerChanged[sampler] = true;}@@ -341,19 +342,19 @@ void DX11BackendClass::Release_Cached()}Pipelines.clear();- for (std::map<std::string, ID3D11BlendState *>::iterator entry = BlendStates.begin();+ for (BlendStateMap::iterator entry = BlendStates.begin();entry != BlendStates.end(); ++entry) {entry->second->Release();}BlendStates.clear();- for (std::map<std::string, ID3D11DepthStencilState *>::iterator entry+ for (DepthStencilStateMap::iterator entry= DepthStencilStates.begin(); entry != DepthStencilStates.end(); ++entry) {entry->second->Release();}DepthStencilStates.clear();- for (std::map<std::string, ID3D11RasterizerState *>::iterator entry = RasterizerStates.begin();+ for (RasterizerStateMap::iterator entry = RasterizerStates.begin();entry != RasterizerStates.end(); ++entry) {entry->second->Release();}@@ -369,7 +370,7 @@ void DX11BackendClass::Release_Cached()CurrentTarget = NULL;CurrentDepth = NULL;- for (std::map<std::string, ID3D11SamplerState *>::iterator entry = SamplerStates.begin();+ for (SamplerStateMap::iterator entry = SamplerStates.begin();entry != SamplerStates.end(); ++entry) {entry->second->Release();}@@ -667,6 +668,7 @@ void DX11BackendClass::Set_Render_State(D3DRENDERSTATETYPE state, DWORD value)RenderStates.Set_Render_State(state, value);ConstantsChanged = true;PipelineChanged = true;+ StateObjectsChanged = true;}}@@ -1762,8 +1764,7 @@ ID3D11BlendState * DX11BackendClass::Blend_State()return LastBlendState;}- const std::string key(reinterpret_cast<const char *>(&description), sizeof(description));- std::map<std::string, ID3D11BlendState *>::const_iterator existing = BlendStates.find(key);+ BlendStateMap::const_iterator existing = BlendStates.find(description);if (existing != BlendStates.end()) {LastBlendDescription = description;LastBlendState = existing->second;@@ -1778,7 +1779,7 @@ ID3D11BlendState * DX11BackendClass::Blend_State()Note_Refusal("the device refused a blend state");return NULL;}- BlendStates[key] = state;+ BlendStates[description] = state;LastBlendDescription = description;LastBlendState = state;return state;@@ -1793,9 +1794,7 @@ ID3D11DepthStencilState * DX11BackendClass::Depth_Stencil_State()return LastDepthStencilState;}- const std::string key(reinterpret_cast<const char *>(&description), sizeof(description));- std::map<std::string, ID3D11DepthStencilState *>::const_iterator existing- = DepthStencilStates.find(key);+ DepthStencilStateMap::const_iterator existing = DepthStencilStates.find(description);if (existing != DepthStencilStates.end()) {LastDepthStencilDescription = description;LastDepthStencilState = existing->second;@@ -1807,7 +1806,7 @@ ID3D11DepthStencilState * DX11BackendClass::Depth_Stencil_State()Note_Refusal("the device refused a depth stencil state");return NULL;}- DepthStencilStates[key] = state;+ DepthStencilStates[description] = state;LastDepthStencilDescription = description;LastDepthStencilState = state;return state;@@ -1822,9 +1821,7 @@ ID3D11RasterizerState * DX11BackendClass::Rasterizer_State()return LastRasterizerState;}- const std::string key(reinterpret_cast<const char *>(&description), sizeof(description));- std::map<std::string, ID3D11RasterizerState *>::const_iterator existing- = RasterizerStates.find(key);+ RasterizerStateMap::const_iterator existing = RasterizerStates.find(description);if (existing != RasterizerStates.end()) {LastRasterizerDescription = description;LastRasterizerState = existing->second;@@ -1836,7 +1833,7 @@ ID3D11RasterizerState * DX11BackendClass::Rasterizer_State()Note_Refusal("the device refused a rasterizer state");return NULL;}- RasterizerStates[key] = state;+ RasterizerStates[description] = state;LastRasterizerDescription = description;LastRasterizerState = state;return state;@@ -1858,8 +1855,7 @@ ID3D11SamplerState * DX11BackendClass::Sampler_State(unsigned sampler)return LastSamplerStates[sampler];}- const std::string key(reinterpret_cast<const char *>(&description), sizeof(description));- std::map<std::string, ID3D11SamplerState *>::const_iterator existing = SamplerStates.find(key);+ SamplerStateMap::const_iterator existing = SamplerStates.find(description);if (existing != SamplerStates.end()) {LastSamplerDescriptions[sampler] = description;LastSamplerStates[sampler] = existing->second;@@ -1872,7 +1868,7 @@ ID3D11SamplerState * DX11BackendClass::Sampler_State(unsigned sampler)SamplerChanged[sampler] = true;return NULL;}- SamplerStates[key] = state;+ SamplerStates[description] = state;LastSamplerDescriptions[sampler] = description;LastSamplerStates[sampler] = state;return state;@@ -1895,19 +1891,28 @@ void DX11BackendClass::Bind_State_Objects()ID3D11DeviceContext * context = Device->Get_Context();const bool known = Bound.Known;- ID3D11BlendState * blend = Blend_State();+ // The memos stand for what the render states would build while none of them has changed; a+ // lookup that came back null keeps the flag up, so the next draw asks again as it always did.+ ID3D11BlendState * blend = LastBlendState;+ ID3D11DepthStencilState * depth_stencil = LastDepthStencilState;+ ID3D11RasterizerState * rasterizer = LastRasterizerState;+ if (StateObjectsChanged || blend == NULL || depth_stencil == NULL || rasterizer == NULL) {+ blend = Blend_State();+ depth_stencil = Depth_Stencil_State();+ rasterizer = Rasterizer_State();+ StateObjectsChanged = blend == NULL || depth_stencil == NULL || rasterizer == NULL;+ }+if (!known || blend != Bound.Blend) {context->OMSetBlendState(blend, NULL, 0xffffffff);Bound.Blend = blend;}- ID3D11DepthStencilState * depth_stencil = Depth_Stencil_State();const UINT stencil_reference = RenderStates.Get_Stencil_Reference();if (!known || depth_stencil != Bound.DepthStencil || stencil_reference != Bound.StencilReference) {context->OMSetDepthStencilState(depth_stencil, stencil_reference);Bound.DepthStencil = depth_stencil;Bound.StencilReference = stencil_reference;}- ID3D11RasterizerState * rasterizer = Rasterizer_State();if (!known || rasterizer != Bound.Rasterizer) {context->RSSetState(rasterizer);Bound.Rasterizer = rasterizer;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.hindex 7d6786195..2badf8822 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h@@ -49,6 +49,7 @@#include <d3d9.h>#include <map>+#include <string.h>#include <string>#include <vector>@@ -446,10 +447,13 @@ private:bool ConstantsChanged;bool PipelineChanged;bool SamplerChanged[DX11_BACKEND_TEXTURE_STAGES];+ // A render state, or the shadow caster pass that answers for some of them, changed since the+ // three state objects were last looked up.+ bool StateObjectsChanged;// Whether Memos[LastMemo] is what the last Resolve handed back. A refusal clears it, so a draw// that was refused is refused again through the whole path and counted the same way.bool LastResolveHeld;- void Note_Shadow_State_Changed() { ConstantsChanged = true; PipelineChanged = true; }+ void Note_Shadow_State_Changed() { ConstantsChanged = true; PipelineChanged = true; StateObjectsChanged = true; }EngineShaderProgram VertexProgram;EngineShaderProgram PixelProgram;@@ -542,18 +546,35 @@ private:ID3D11ShaderResourceView * TargetCheckedViews[DX11_BACKEND_TEXTURE_STAGES];bool TargetCheckedIsTarget[DX11_BACKEND_TEXTURE_STAGES];- std::map<std::string, ID3D11BlendState *> BlendStates;- std::map<std::string, ID3D11DepthStencilState *> DepthStencilStates;- std::map<std::string, ID3D11RasterizerState *> RasterizerStates;- std::map<std::string, ID3D11SamplerState *> SamplerStates;+ // Keyed by the description itself and ordered by its bytes, which is safe for the ResolveMemo's+ // reason: every Build_*_Description memsets first. They were keyed by the bytes copied into a+ // std::string, longer than the string's own small buffer, so every lookup allocated and freed.+ struct Description_Bytes_Less+ {+ template <class Description>+ bool operator()(const Description & left, const Description & right) const+ {+ return memcmp(&left, &right, sizeof(Description)) < 0;+ }+ };+ typedef std::map<D3D11_BLEND_DESC, ID3D11BlendState *, Description_Bytes_Less> BlendStateMap;+ typedef std::map<D3D11_DEPTH_STENCIL_DESC, ID3D11DepthStencilState *, Description_Bytes_Less>+ DepthStencilStateMap;+ typedef std::map<D3D11_RASTERIZER_DESC, ID3D11RasterizerState *, Description_Bytes_Less>+ RasterizerStateMap;+ typedef std::map<D3D11_SAMPLER_DESC, ID3D11SamplerState *, Description_Bytes_Less> SamplerStateMap;+ BlendStateMap BlendStates;+ DepthStencilStateMap DepthStencilStates;+ RasterizerStateMap RasterizerStates;+ SamplerStateMap SamplerStates;// The last description each kind of state object was looked up by, and the object it got.//- // The maps above are keyed by a description's bytes in a std::string, and every description is- // longer than the string's own small buffer, so each lookup allocated and freed. Bind_State_Objects- // makes seven of them a draw, and a particle-heavy frame is several hundred draws whose states- // change far less often than that. memcmp is safe for the ResolveMemo's reason: every- // Build_*_Description memsets first. A null object means no memo; Release_Cached nulls them all.+ // Bind_State_Objects makes seven lookups a draw, and a particle-heavy frame is several hundred+ // draws whose states change far less often than that. A null object means no memo;+ // Release_Cached nulls them all. The blend, depth stencil and rasterizer descriptions are built+ // from the render states alone, so while StateObjectsChanged is down the three memos are what a+ // build would find and the draw skips building and comparing them.D3D11_BLEND_DESC LastBlendDescription;ID3D11BlendState * LastBlendState;D3D11_DEPTH_STENCIL_DESC LastDepthStencilDescription;