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.

fix(w3ddevice): give a hovering helicopter its shadow back

Olcay Seygan9cd0b1aa6c90
diff --git a/CHANGELOG.md b/CHANGELOG.md
index aee9dfc97..90034c9fb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -833,6 +833,10 @@ The whole HUD is on the hardware. Four colours carry it, and they mean the same
picture was computed rather than photographed. This is what the game draws with now; a machine
that cannot run the newer picture keeps the old shadows on its own, and `-noshadowmap` puts them
back on any machine.
+- A hovering helicopter has its shadow back, rotor blades and all. The softening spread a
+ Comanche's thin body so wide that nothing of it was left on the ground, and the spinning blades
+ never reached the shadow at all. Now the blades turn in the shadow as a star under the fuselage,
+ still softer than a tank's.
- A building stands on the ground instead of sitting on top of it. There is a soft patch under every
structure's footprint, the darkening a corner traps in daylight, and the shadow it casts starts at
its own wall rather than a step away from it. The frame's corners and creases get the same
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
index cdd3bcaca..3d849ccab 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
@@ -65,6 +65,7 @@
#include "WW3D2/camera.h"
#include "WW3D2/dx8renderer.h"
#include "WW3D2/dx11runtime.h"
+#include "WW3D2/sortingrenderer.h"
#include "GameClient/View.h"
#ifdef _INTERNAL
@@ -114,11 +115,12 @@ const Real cosAngleToCare = cos ((0.2 * PI) / 180.0); //1.5 degree difference
// much penumbra a unit of gap between a caster and what its shadow falls on is worth. The last is
// the number the whole picture turns on: a tank's tracks are on the ground and keep a hard edge, a
// helicopter twenty metres up spreads. It is not the sun's own half degree, which at this scale
-// would be under a pixel; it is what the sky filling a shadow back in looks like, chosen against
-// the three panel comparison the plan came from.
+// would be under a pixel; it is what the sky filling a shadow back in looks like. It was 0.04,
+// chosen on a frame of tanks, and a Comanche hovering 150 units up then cast nothing at all: the
+// filter spread a fuselage four units wide over fourteen. At 0.01 its blades read as a star.
#define SHADOW_MAP_WIDEST_TEXELS 9.0f
#define SHADOW_MAP_NARROWEST_TEXELS 0.9f
-#define SHADOW_MAP_PENUMBRA_PER_UNIT 0.04f
+#define SHADOW_MAP_PENUMBRA_PER_UNIT 0.01f
#define SHADOW_MAP_SKY_FILL 0.12f
// Whether the sun's map took this frame. The volumes read it to know whether to stand down, and it
@@ -3909,6 +3911,11 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
Draining them here is safe because the pass runs before the frame queues anything of its
own, so everything in those lists was put there by the loop above. */
WW3D::Render_And_Clear_Static_Sort_Lists( sunInfo );
+ /* And the rotor itself goes further still: its mesh carries the SORT flag, so the mesh renderer
+ hands it to the sorting renderer, which holds it for the end of the scene and would draw it
+ onto the screen rather than into the map. The backend writes depth for a blended caster
+ while the pass runs and cuts it at its alpha, which keeps the blades. */
+ SortingRendererClass::Flush();
DX8Wrapper::Set_DX8_Render_State( D3DRS_COLORWRITEENABLE,
D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
index 2f4d0b2ee..240f2539c 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
@@ -418,6 +418,7 @@ void DX11BackendClass::Shutdown()
}
ShadowMapSize = 0;
ShadowMapBound = false;
+ RenderStates.Set_Shadow_Caster_Pass(false);
Device = NULL;
}
@@ -489,6 +490,7 @@ bool DX11BackendClass::Begin_Shadow_Map(unsigned size)
ShadowMapSavedHeight = ViewportHeight;
ShadowMapSavedTarget = CurrentTarget;
ShadowMapBound = true;
+ RenderStates.Set_Shadow_Caster_Pass(true);
Set_Viewport(0, 0, ShadowMapSize, ShadowMapSize);
Forget_Bindings();
return true;
@@ -501,6 +503,7 @@ void DX11BackendClass::End_Shadow_Map()
}
ShadowMapBound = false;
+ RenderStates.Set_Shadow_Caster_Pass(false);
// What the sun was looking through, kept for the draws that will read the map.
multiply(View, Projection, SunViewProjection);
ShadowFromClipValid = false;
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.cpp
index 193fa7f4e..ca209ac77 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.cpp
@@ -133,11 +133,22 @@ D3D11_FILL_MODE DX11State_Translate_Fill_Mode(DWORD d3d9_fill_mode)
}
}
+// Where a see-through caster counts as solid in the sun's depth pass, as an eight bit alpha level.
+// A spinning rotor is a set of faint translucent blades rather than a disc: at 0x50 not one blade
+// of a Comanche's rotor reached the map, at 0x08 it casts the star the stencil volumes drew.
+static const DWORD SHADOW_CASTER_ALPHA_REFERENCE = 0x08;
+
DX11StateBlockClass::DX11StateBlockClass()
+ : ShadowCasterPass(false)
{
Reset_To_Defaults();
}
+void DX11StateBlockClass::Set_Shadow_Caster_Pass(bool casting)
+{
+ ShadowCasterPass = casting;
+}
+
void DX11StateBlockClass::Reset_To_Defaults()
{
memset(RenderStates, 0, sizeof(RenderStates));
@@ -177,10 +188,33 @@ void DX11StateBlockClass::Set_Render_State(D3DRENDERSTATETYPE state, DWORD value
DWORD DX11StateBlockClass::Get_Render_State(D3DRENDERSTATETYPE state) const
{
- if (static_cast<unsigned>(state) < RENDER_STATE_COUNT) {
+ if (static_cast<unsigned>(state) >= RENDER_STATE_COUNT) {
+ return 0;
+ }
+ if (!ShadowCasterPass) {
+ return RenderStates[state];
+ }
+
+ // A material that blends turns off its own depth writes, which is right on the screen and left
+ // a helicopter's rotor out of the sun's map altogether. One that adds light casts nothing.
+ const bool blended = RenderStates[D3DRS_ALPHABLENDENABLE] != FALSE;
+ const bool adds_light = blended
+ && (RenderStates[D3DRS_DESTBLEND] == D3DBLEND_ONE || RenderStates[D3DRS_SRCBLEND] == D3DBLEND_ONE);
+ switch (state) {
+ case D3DRS_ZWRITEENABLE:
+ return TRUE;
+ case D3DRS_ALPHATESTENABLE:
+ return blended ? TRUE : RenderStates[state];
+ case D3DRS_ALPHAFUNC:
+ if (!blended) {
+ return RenderStates[state];
+ }
+ return adds_light ? D3DCMP_NEVER : D3DCMP_GREATEREQUAL;
+ case D3DRS_ALPHAREF:
+ return blended ? SHADOW_CASTER_ALPHA_REFERENCE : RenderStates[state];
+ default:
return RenderStates[state];
}
- return 0;
}
void DX11StateBlockClass::Build_Blend_Description(D3D11_BLEND_DESC & description) const
@@ -214,7 +248,7 @@ void DX11StateBlockClass::Build_Depth_Stencil_Description(D3D11_DEPTH_STENCIL_DE
memset(&description, 0, sizeof(description));
description.DepthEnable = (RenderStates[D3DRS_ZENABLE] != D3DZB_FALSE) ? TRUE : FALSE;
- description.DepthWriteMask = (RenderStates[D3DRS_ZWRITEENABLE] != FALSE)
+ description.DepthWriteMask = (Get_Render_State(D3DRS_ZWRITEENABLE) != FALSE)
? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO;
description.DepthFunc = DX11State_Translate_Comparison(RenderStates[D3DRS_ZFUNC]);
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.h
index 8751678e9..b38f8cace 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11state.h
@@ -59,6 +59,11 @@ public:
void Set_Render_State(D3DRENDERSTATETYPE state, DWORD value);
DWORD Get_Render_State(D3DRENDERSTATETYPE state) const;
+ // 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 are
+ // kept underneath and come back as they were when the pass ends.
+ void Set_Shadow_Caster_Pass(bool casting);
+
void Build_Blend_Description(D3D11_BLEND_DESC & description) const;
void Build_Depth_Stencil_Description(D3D11_DEPTH_STENCIL_DESC & description) const;
void Build_Rasterizer_Description(D3D11_RASTERIZER_DESC & description) const;
@@ -70,6 +75,7 @@ public:
private:
DWORD RenderStates[RENDER_STATE_COUNT];
+ bool ShadowCasterPass;
};
// D3D9 sets a sampler one state at a time the way it sets a render state; D3D11 wants a sampler
diff --git a/GeneralsMD/Code/Tests/test_dx11state.cpp b/GeneralsMD/Code/Tests/test_dx11state.cpp
index cd64a84c1..e221f161f 100644
--- a/GeneralsMD/Code/Tests/test_dx11state.cpp
+++ b/GeneralsMD/Code/Tests/test_dx11state.cpp
@@ -293,6 +293,34 @@ TEST(dx11state_a_fresh_sampler_block_is_the_d3d9_default_sampler)
CHECK_EQ(description.MaxAnisotropy, 1u);
}
+// A helicopter's rotor is a blended material with its depth writes off, and in the sun's depth pass
+// that left it out of the map. The pass writes depth for everything and cuts a blended caster at
+// its alpha; one that adds light casts nothing; and the material's own states are untouched after.
+TEST(dx11state_the_shadow_caster_pass_writes_depth_for_a_blended_caster)
+{
+ DX11StateBlockClass block;
+ block.Set_Render_State(D3DRS_ALPHABLENDENABLE, TRUE);
+ block.Set_Render_State(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
+ block.Set_Render_State(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
+ block.Set_Render_State(D3DRS_ZWRITEENABLE, FALSE);
+
+ block.Set_Shadow_Caster_Pass(true);
+ D3D11_DEPTH_STENCIL_DESC depth;
+ block.Build_Depth_Stencil_Description(depth);
+ CHECK_EQ(depth.DepthWriteMask, D3D11_DEPTH_WRITE_MASK_ALL);
+ CHECK_EQ(block.Get_Render_State(D3DRS_ALPHATESTENABLE), (DWORD)TRUE);
+ CHECK_EQ(block.Get_Render_State(D3DRS_ALPHAFUNC), (DWORD)D3DCMP_GREATEREQUAL);
+ CHECK(block.Get_Render_State(D3DRS_ALPHAREF) > 0u);
+
+ block.Set_Render_State(D3DRS_DESTBLEND, D3DBLEND_ONE);
+ CHECK_EQ(block.Get_Render_State(D3DRS_ALPHAFUNC), (DWORD)D3DCMP_NEVER);
+
+ block.Set_Shadow_Caster_Pass(false);
+ block.Build_Depth_Stencil_Description(depth);
+ CHECK_EQ(depth.DepthWriteMask, D3D11_DEPTH_WRITE_MASK_ZERO);
+ CHECK_EQ(block.Get_Render_State(D3DRS_ALPHATESTENABLE), (DWORD)FALSE);
+}
+
// A state above the block's range is one D3D11 has nowhere to put anyway. Writing it must not
// walk off the end of the array, and reading it back reads zero rather than whatever was there.
TEST(dx11state_a_state_outside_the_block_is_dropped_not_written_past_the_end)