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 per-draw rebuilds when nothing was set
Olcay Seyganc46ee9e25934
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cppindex 240f2539c..dc18e612b 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp@@ -249,6 +249,12 @@ DX11BackendClass::DX11BackendClass()VertexConstantsHeld = false;PixelConstantsHeld = false;EngineConstantsHeld = false;+ ConstantsChanged = true;+ PipelineChanged = true;+ for (unsigned sampler = 0; sampler < DX11_BACKEND_TEXTURE_STAGES; ++sampler) {+ SamplerChanged[sampler] = true;+ }+ LastResolveHeld = false;memset(StageStates, 0, sizeof(StageStates));memset(Textures, 0, sizeof(Textures));memset(EngineConstants, 0, sizeof(EngineConstants));@@ -324,6 +330,7 @@ void DX11BackendClass::Release_Cached()}LastMemo = 0;NextMemo = 0;+ LastResolveHeld = false;Forget_Last_State_Objects();for (std::map<std::string, Pipeline>::iterator entry = Pipelines.begin();@@ -419,6 +426,7 @@ void DX11BackendClass::Shutdown()ShadowMapSize = 0;ShadowMapBound = false;RenderStates.Set_Shadow_Caster_Pass(false);+ Note_Shadow_State_Changed();Device = NULL;}@@ -491,6 +499,7 @@ bool DX11BackendClass::Begin_Shadow_Map(unsigned size)ShadowMapSavedTarget = CurrentTarget;ShadowMapBound = true;RenderStates.Set_Shadow_Caster_Pass(true);+ Note_Shadow_State_Changed();Set_Viewport(0, 0, ShadowMapSize, ShadowMapSize);Forget_Bindings();return true;@@ -504,6 +513,7 @@ void DX11BackendClass::End_Shadow_Map()ShadowMapBound = false;RenderStates.Set_Shadow_Caster_Pass(false);+ Note_Shadow_State_Changed();// What the sun was looking through, kept for the draws that will read the map.multiply(View, Projection, SunViewProjection);ShadowFromClipValid = false;@@ -533,12 +543,14 @@ void DX11BackendClass::Set_Shadow_Parameters(float bias, float strength,ShadowUnitsPerDepth = units_per_unit_of_depth;ShadowSkyFill = sky_fill;ShadowReceiving = true;+ Note_Shadow_State_Changed();}void DX11BackendClass::Clear_Shadow_Parameters(){ShadowReceiving = false;ShadowFromClipValid = false;+ Note_Shadow_State_Changed();}/** The inverse of a four by four, by cofactors. Nothing else in the backend needed one: every@@ -650,33 +662,49 @@ std::string DX11BackendClass::Shadow_Map_Report()void DX11BackendClass::Set_Render_State(D3DRENDERSTATETYPE state, DWORD value){- RenderStates.Set_Render_State(state, value);+ // The raw value, not Get_Render_State: that one answers for the caster pass while it runs.+ if (RenderStates.Get_Stored_Render_State(state) != value) {+ RenderStates.Set_Render_State(state, value);+ ConstantsChanged = true;+ PipelineChanged = true;+ }}void DX11BackendClass::Set_Texture_Stage_State(unsigned stage, D3DTEXTURESTAGESTATETYPE state,DWORD value){- if (stage < DX11_BACKEND_TEXTURE_STAGES && static_cast<unsigned>(state) < DX11_BACKEND_STAGE_STATES) {+ if (stage < DX11_BACKEND_TEXTURE_STAGES && static_cast<unsigned>(state) < DX11_BACKEND_STAGE_STATES+ && StageStates[stage][state] != value) {StageStates[stage][state] = value;+ PipelineChanged = true;}}void DX11BackendClass::Set_Sampler_State(unsigned sampler, D3DSAMPLERSTATETYPE state, DWORD value){- if (sampler < DX11_BACKEND_TEXTURE_STAGES) {+ if (sampler < DX11_BACKEND_TEXTURE_STAGES+ && Samplers[sampler].Get_Sampler_State(state) != value) {Samplers[sampler].Set_Sampler_State(state, value);+ SamplerChanged[sampler] = true;}}+// Only whether a stage has a texture reaches a description; which one is bound is read at bind time.void DX11BackendClass::Set_Texture(unsigned stage, ID3D11ShaderResourceView * texture){if (stage < DX11_BACKEND_TEXTURE_STAGES) {+ if ((Textures[stage] == NULL) != (texture == NULL)) {+ PipelineChanged = true;+ }Textures[stage] = texture;}}void DX11BackendClass::Set_Normal_Map(ID3D11ShaderResourceView * normal_map){+ if ((NormalMap == NULL) != (normal_map == NULL)) {+ PipelineChanged = true;+ }NormalMap = normal_map;}@@ -701,17 +729,27 @@ bool DX11BackendClass::Terrain_Bumped() constvoid DX11BackendClass::Set_Terrain_Sun(const float direction[3]){memcpy(TerrainSun, direction, sizeof(TerrainSun));+ ConstantsChanged = true;}void DX11BackendClass::Set_Vertex_Format(DWORD fvf){- VertexFormat = fvf;+ if (VertexFormat != fvf) {+ VertexFormat = fvf;+ PipelineChanged = true;+ }}void DX11BackendClass::Set_Pixel_Program(EngineShaderProgram program, bool bound,const char * file_name){- PixelProgram = bound ? program : ENGINE_SHADER_NONE;+ // The wrapper clears both programs on every vertex buffer it applies, so only a different one+ // counts. A foreign shader is refused before Resolve and is no input to it.+ const EngineShaderProgram resolved = bound ? program : ENGINE_SHADER_NONE;+ if (PixelProgram != resolved) {+ PipelineChanged = true;+ }+ PixelProgram = resolved;ForeignPixelShader = bound && program == ENGINE_SHADER_NONE;ForeignPixelName = ForeignPixelShader ? file_name : "";}@@ -719,7 +757,11 @@ void DX11BackendClass::Set_Pixel_Program(EngineShaderProgram program, bool boundvoid DX11BackendClass::Set_Vertex_Program(EngineShaderProgram program, bool bound,const char * file_name){- VertexProgram = bound ? program : ENGINE_SHADER_NONE;+ const EngineShaderProgram resolved = bound ? program : ENGINE_SHADER_NONE;+ if (VertexProgram != resolved) {+ PipelineChanged = true;+ }+ VertexProgram = resolved;ForeignVertexShader = bound && program == ENGINE_SHADER_NONE;ForeignVertexName = ForeignVertexShader ? file_name : "";}@@ -782,17 +824,22 @@ void DX11BackendClass::Set_Indices(ID3D11Buffer * buffer, DXGI_FORMAT format)void DX11BackendClass::Set_Transform(D3DTRANSFORMSTATETYPE state, const float matrix[16]){+ float * held = NULL;switch (state) {- case D3DTS_WORLD: memcpy(World, matrix, sizeof(World)); break;- case D3DTS_VIEW: memcpy(View, matrix, sizeof(View)); break;- case D3DTS_PROJECTION: memcpy(Projection, matrix, sizeof(Projection)); break;+ case D3DTS_WORLD: held = World; break;+ case D3DTS_VIEW: held = View; break;+ case D3DTS_PROJECTION: held = Projection; break;default:if (state >= D3DTS_TEXTURE0&& state < D3DTS_TEXTURE0 + (int)DX11_BACKEND_TEXTURE_STAGES) {- memcpy(TextureTransforms[state - D3DTS_TEXTURE0], matrix, sizeof(float) * 16);+ held = TextureTransforms[state - D3DTS_TEXTURE0];}break;}+ if (held != NULL && memcmp(held, matrix, sizeof(float) * 16) != 0) {+ memcpy(held, matrix, sizeof(float) * 16);+ ConstantsChanged = true;+ }}void DX11BackendClass::Set_Material(const float ambient[4], const float diffuse[4],@@ -803,6 +850,7 @@ void DX11BackendClass::Set_Material(const float ambient[4], const float diffuse[memcpy(MaterialSpecular, specular, sizeof(MaterialSpecular));memcpy(MaterialEmissive, emissive, sizeof(MaterialEmissive));MaterialPower = power;+ ConstantsChanged = true;}void DX11BackendClass::Set_Light(unsigned index, DWORD type, const float position[4],@@ -822,12 +870,16 @@ void DX11BackendClass::Set_Light(unsigned index, DWORD type, const float positiomemcpy(light.Specular, specular, sizeof(light.Specular));memcpy(light.Attenuation, attenuation, sizeof(light.Attenuation));memcpy(light.Spot, spot, sizeof(light.Spot));+ ConstantsChanged = true;+ PipelineChanged = true;}void DX11BackendClass::Disable_Light(unsigned index){- if (index < MAXIMUM_VERTEX_LIGHTS) {+ if (index < MAXIMUM_VERTEX_LIGHTS && Lights[index].Enabled) {Lights[index].Enabled = false;+ ConstantsChanged = true;+ PipelineChanged = true;}}@@ -1129,6 +1181,7 @@ void DX11BackendClass::Set_Viewport(unsigned x, unsigned y, unsigned width, unsiViewportWidth = (width == 0) ? 1 : width;ViewportHeight = (height == 0) ? 1 : height;+ ConstantsChanged = true;}void DX11BackendClass::Clear(bool colour, bool depth, const float colour_value[4])@@ -1264,6 +1317,16 @@ bool DX11BackendClass::Build_Vertex_Description(VertexPipelineDescription & descbool DX11BackendClass::Resolve(Pipeline & pipeline){+ // Nothing either description is built from has been set since the last draw resolved.+ if (!PipelineChanged && LastResolveHeld) {+ ResolveMemo & memo = Memos[LastMemo];+ pipeline = memo.Resolved;+ ++memo.Use->Draws;+ return true;+ }+ PipelineChanged = false;+ LastResolveHeld = false;+VertexPipelineDescription vertex_description;CombinerDescription combiner_description;const bool built_combiners = Build_Combiner_Description(combiner_description);@@ -1292,6 +1355,7 @@ bool DX11BackendClass::Resolve(Pipeline & pipeline)pipeline = memo.Resolved;++memo.Use->Draws;LastMemo = entry;+ LastResolveHeld = true;return true;}}@@ -1414,6 +1478,7 @@ void DX11BackendClass::Remember_Resolution(const std::string & key, const Pipelimemo.PixelProgram = PixelProgram;memo.Resolved = resolved;memo.Use = Record_Use(key);+ LastResolveHeld = true;LastMemo = NextMemo;NextMemo = (NextMemo + 1) % RESOLVE_MEMO_ENTRIES;}@@ -1474,6 +1539,25 @@ const char * DX11BackendClass::Pipeline_Report(unsigned index)void DX11BackendClass::Upload_Constants(){ID3D11DeviceContext * context = Device->Get_Context();+ D3D11_MAPPED_SUBRESOURCE mapped;++ // A transcribed program reads the engine's own register bank instead of the block below, so+ // that bank goes up only on the draws that take one.+ if (VertexProgram != ENGINE_SHADER_NONE+ && (!EngineConstantsHeld+ || memcmp(HeldEngineConstants, EngineConstants, sizeof(EngineConstants)) != 0)+ && SUCCEEDED(context->Map(EngineConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped))) {+ memcpy(mapped.pData, EngineConstants, sizeof(EngineConstants));+ context->Unmap(EngineConstantBuffer, 0);+ memcpy(HeldEngineConstants, EngineConstants, sizeof(EngineConstants));+ EngineConstantsHeld = true;+ }++ // Nothing either block is built from has been set since both buffers were last written.+ if (!ConstantsChanged && VertexConstantsHeld && PixelConstantsHeld) {+ return;+ }+ ConstantsChanged = false;VertexConstantBlock vertex_block;memset(&vertex_block, 0, sizeof(vertex_block));@@ -1527,26 +1611,16 @@ void DX11BackendClass::Upload_Constants()// See HeldVertexConstants in the header: the buffer is only written through when the bytes// going into it differ from the bytes already there.- D3D11_MAPPED_SUBRESOURCE mapped;- if ((!VertexConstantsHeld- || memcmp(&HeldVertexConstants, &vertex_block, sizeof(vertex_block)) != 0)- && SUCCEEDED(context->Map(VertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped))) {- memcpy(mapped.pData, &vertex_block, sizeof(vertex_block));- context->Unmap(VertexConstantBuffer, 0);- HeldVertexConstants = vertex_block;- VertexConstantsHeld = true;- }-- // A transcribed program reads the engine's own register bank instead of the block above, so- // that bank goes up only on the draws that take one.- if (VertexProgram != ENGINE_SHADER_NONE- && (!EngineConstantsHeld- || memcmp(HeldEngineConstants, EngineConstants, sizeof(EngineConstants)) != 0)- && SUCCEEDED(context->Map(EngineConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped))) {- memcpy(mapped.pData, EngineConstants, sizeof(EngineConstants));- context->Unmap(EngineConstantBuffer, 0);- memcpy(HeldEngineConstants, EngineConstants, sizeof(EngineConstants));- EngineConstantsHeld = true;+ // A failed Map leaves the buffer unknown, which also keeps the next draw off the early return.+ if (!VertexConstantsHeld+ || memcmp(&HeldVertexConstants, &vertex_block, sizeof(vertex_block)) != 0) {+ VertexConstantsHeld = false;+ if (SUCCEEDED(context->Map(VertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped))) {+ memcpy(mapped.pData, &vertex_block, sizeof(vertex_block));+ context->Unmap(VertexConstantBuffer, 0);+ HeldVertexConstants = vertex_block;+ VertexConstantsHeld = true;+ }}PixelConstantBlock pixel_block;@@ -1653,13 +1727,15 @@ void DX11BackendClass::Upload_Constants()}pixel_block.SkyUp[3] = SKY_HORIZON_SHARE;- if ((!PixelConstantsHeld- || memcmp(&HeldPixelConstants, &pixel_block, sizeof(pixel_block)) != 0)- && SUCCEEDED(context->Map(PixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped))) {- memcpy(mapped.pData, &pixel_block, sizeof(pixel_block));- context->Unmap(PixelConstantBuffer, 0);- HeldPixelConstants = pixel_block;- PixelConstantsHeld = true;+ if (!PixelConstantsHeld+ || memcmp(&HeldPixelConstants, &pixel_block, sizeof(pixel_block)) != 0) {+ PixelConstantsHeld = false;+ if (SUCCEEDED(context->Map(PixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped))) {+ memcpy(mapped.pData, &pixel_block, sizeof(pixel_block));+ context->Unmap(PixelConstantBuffer, 0);+ HeldPixelConstants = pixel_block;+ PixelConstantsHeld = true;+ }}}@@ -1768,6 +1844,13 @@ ID3D11RasterizerState * DX11BackendClass::Rasterizer_State()ID3D11SamplerState * DX11BackendClass::Sampler_State(unsigned sampler){+ // Nothing set on this stage since its object was last looked up. Forget_Last_State_Objects+ // nulls the object, which sends the next draw down the whole path.+ if (!SamplerChanged[sampler] && LastSamplerStates[sampler] != NULL) {+ return LastSamplerStates[sampler];+ }+ SamplerChanged[sampler] = false;+D3D11_SAMPLER_DESC description;Samplers[sampler].Build_Sampler_Description(description);if (LastSamplerStates[sampler] != NULL@@ -1786,6 +1869,7 @@ ID3D11SamplerState * DX11BackendClass::Sampler_State(unsigned sampler)ID3D11SamplerState * state = NULL;if (FAILED(Device->Get_Device()->CreateSamplerState(&description, &state))) {Note_Refusal("the device refused a sampler state");+ SamplerChanged[sampler] = true;return NULL;}SamplerStates[key] = state;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.hindex c20baa68f..7d6786195 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h@@ -420,9 +420,9 @@ private:// and the lights are set once for a whole batch - so the block is built into a local, compared// against the copy here, and written through only when it differs.//- // Comparing bytes rather than flagging the setters dirty is deliberate: there are a dozen ways- // into these blocks, including three render states read straight out of the state block, and a- // setter nobody flagged would draw with the previous batch's lighting.+ // The setters do flag the blocks now (ConstantsChanged below), but only to skip building them.+ // The byte compare still decides the write: a flag says something was set, often to the value+ // it already had in another order, and only the bytes say whether the buffer is different.VertexConstantBlock HeldVertexConstants;PixelConstantBlock HeldPixelConstants;float HeldEngineConstants[ENGINE_SHADER_CONSTANTS][4];@@ -434,6 +434,22 @@ private:bool VertexConstantsHeld;bool PixelConstantsHeld;bool EngineConstantsHeld;++ // Whether anything the draws read has changed since the last draw used it, so a draw that+ // follows another with nothing set in between skips building what it would build identically:+ // the two constant blocks with their three matrix products, both pipeline descriptions, and a+ // sampler description a stage. The terrain is the case this is for, hundreds of tiles in a row+ // under one transform and one state. Every setter that writes something a block, a description+ // or a sampler reads sets the matching flag, and only when the value actually differs. A new+ // input to any of them needs its setter to raise the flag here, or the draw after it reads the+ // previous batch's lighting.+ bool ConstantsChanged;+ bool PipelineChanged;+ bool SamplerChanged[DX11_BACKEND_TEXTURE_STAGES];+ // 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; }EngineShaderProgram VertexProgram;EngineShaderProgram PixelProgram;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.hindex b38f8cace..605079a16 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.h@@ -58,6 +58,9 @@ public:void Set_Render_State(D3DRENDERSTATETYPE state, DWORD value);DWORD Get_Render_State(D3DRENDERSTATETYPE state) const;+ // What was set, before the caster pass has its say.+ DWORD Get_Stored_Render_State(D3DRENDERSTATETYPE state) const+ { return (static_cast<unsigned>(state) < RENDER_STATE_COUNT) ? RenderStates[state] : 0; }// While the sun's depth pass draws, every caster writes depth whatever its material says, and a// see-through one writes it only where its texture is solid. The states the engine set arediff --git a/GeneralsMD/Code/Tests/test_dx11backend.cpp b/GeneralsMD/Code/Tests/test_dx11backend.cppindex a4e749497..e3264b480 100644--- a/GeneralsMD/Code/Tests/test_dx11backend.cpp+++ b/GeneralsMD/Code/Tests/test_dx11backend.cpp@@ -529,6 +529,24 @@ TEST(dx11backend_the_alpha_test_cuts_the_pixels_the_comparison_rejects)CHECK_EQ(kept[2], EXPECTED_RED);device.Get_Context()->Unmap(staging, 0);+ // Only the world moves now, the quad off the target to the right. Nothing a pipeline is built+ // from changed, so this is the draw that skips the descriptions and the constants both unless+ // Set_Transform says otherwise, and skipped wrongly it paints the quad where it was.+ float moved[16];+ set_identity(moved);+ moved[12] = 4.0f;+ device.Get_Context()->ClearRenderTargetView(target_view, clear_colour);+ backend.Set_Transform(D3DTS_WORLD, moved);+ CHECK(backend.Draw_Indexed_Triangles(6, 0, 0));++ device.Get_Context()->CopyResource(staging, target);+ CHECK(SUCCEEDED(device.Get_Context()->Map(staging, 0, D3D11_MAP_READ, 0, &mapped)));+ const unsigned char * gone = static_cast<const unsigned char *>(mapped.pData)+ + mapped.RowPitch * (TARGET_SIZE / 2) + (TARGET_SIZE / 2) * 4;+ CHECK_EQ(gone[1], 0xff);+ CHECK_EQ(gone[0], 0x00);+ device.Get_Context()->Unmap(staging, 0);+// The comparison is part of the program and the reference is not, so two comparisons are two// pipelines and the reference could have changed between them for free.unsigned pipelines = 0;@@ -536,7 +554,7 @@ TEST(dx11backend_the_alpha_test_cuts_the_pixels_the_comparison_rejects)unsigned long long refused = 0;backend.Statistics(pipelines, made, refused);CHECK_EQ(pipelines, 2u);- CHECK_EQ(made, 2ull);+ CHECK_EQ(made, 3ull);indices->Release();vertices->Release();