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(w3ddevice): widen the shadow filter with the gap under the caster

Olcay Seyganf2ce3a12be67
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
index d4c625afb..8a794f00f 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
@@ -110,7 +110,16 @@ const Real cosAngleToCare = cos ((0.2 * PI) / 180.0); //1.5 degree difference
// decides between a surface shadowing itself in stripes and a shadow lifting off its own caster.
#define SHADOW_MAP_DEPTH_BIAS 0.0015f
#define SHADOW_MAP_STRENGTH 0.45f
-#define SHADOW_MAP_FILTER_TEXELS 1.0f
+// How wide the filter may open and how narrow it stays on the ground, in texels of the map, and how
+// 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.
+#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_SKY_FILL 0.12f
//#define SV_DEBUG
//#define SV_DEBUG_BOUNDS
@@ -3847,6 +3856,10 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
DX8Wrapper::Set_DX8_Render_State( D3DRS_ZFUNC, D3DCMP_LESSEQUAL );
DX8Wrapper::Set_DX8_Render_State( D3DRS_ALPHABLENDENABLE, FALSE );
DX8Wrapper::Set_DX8_Render_State( D3DRS_STENCILENABLE, FALSE );
+ /* Back faces only. What the map holds is then the far side of every caster, which is behind
+ the surface that receives the light by the thickness of the object: a hull cannot shadow
+ itself, and the bias has only the map's own texel to cover rather than a whole tank. */
+ DX8Wrapper::Set_DX8_Render_State( D3DRS_CULLMODE, D3DCULL_CW );
RenderInfoClass sunInfo( sun );
Int casters = 0;
@@ -3861,6 +3874,12 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
++casters;
}
TheDX8MeshRenderer.Flush();
+ /* A mesh the material system calls translucent goes to the sort lists rather than to the mesh
+ renderer, and a helicopter's rotor disc is one of them: without this the map holds the
+ fuselage alone, and a fuselage is too thin a thing to read as a shadow once the filter opens.
+ 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 );
DX8Wrapper::Set_DX8_Render_State( D3DRS_COLORWRITEENABLE,
D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE
@@ -3878,8 +3897,16 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
held them during the pass and the frame's as it holds them now: both are already there, in
one convention, and a matrix assembled on this side would have to agree with a layout it
cannot see. SHADOW-MAP-PLAN.md phase 2. */
+ /* The two conversions the filter needs, both of them the box's own arithmetic. A texel is this
+ many world units across, and a unit of depth is the whole of the near to far range, because
+ an orthographic projection puts depth on a straight line. */
+ const Real worldPerTexel = (2.0f * SHADOW_MAP_HALF_WIDTH) / (Real)SHADOW_MAP_TEXELS;
+ const Real texelsPerUnitOfGap = SHADOW_MAP_PENUMBRA_PER_UNIT / worldPerTexel;
+ const Real unitsPerUnitOfDepth = SHADOW_MAP_FAR_CLIP - SHADOW_MAP_NEAR_CLIP;
+
Direct3D11_Set_Shadow_Parameters( SHADOW_MAP_DEPTH_BIAS, SHADOW_MAP_STRENGTH,
- SHADOW_MAP_FILTER_TEXELS );
+ SHADOW_MAP_WIDEST_TEXELS, SHADOW_MAP_NARROWEST_TEXELS, texelsPerUnitOfGap,
+ unitsPerUnitOfDepth, SHADOW_MAP_SKY_FILL );
// The report costs a full stall of the pipeline, so it is one line a second rather than one a
// frame: what it answers is whether the pass draws the world at all, and that does not change
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
index 6537f6933..8f4787a78 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
@@ -212,6 +212,10 @@ DX11BackendClass::DX11BackendClass()
, ShadowBias(0.0f)
, ShadowStrength(0.0f)
, ShadowRadius(1.0f)
+ , ShadowNarrowestRadius(1.0f)
+ , ShadowTexelsPerGap(0.0f)
+ , ShadowUnitsPerDepth(0.0f)
+ , ShadowSkyFill(0.0f)
, ShadowReceiving(false)
, NormalMappedDraws(0)
, DrawsMade(0)
@@ -501,7 +505,9 @@ void DX11BackendClass::End_Shadow_Map()
Forget_Bindings();
}
-void DX11BackendClass::Set_Shadow_Parameters(float bias, float strength, float radius_in_texels)
+void DX11BackendClass::Set_Shadow_Parameters(float bias, float strength,
+ float widest_radius_in_texels, float narrowest_radius_in_texels, float texels_per_unit_of_gap,
+ float units_per_unit_of_depth, float sky_fill)
{
if (ShadowMapTexture == NULL) {
Clear_Shadow_Parameters();
@@ -509,7 +515,11 @@ void DX11BackendClass::Set_Shadow_Parameters(float bias, float strength, float r
}
ShadowBias = bias;
ShadowStrength = strength;
- ShadowRadius = radius_in_texels;
+ ShadowRadius = widest_radius_in_texels;
+ ShadowNarrowestRadius = narrowest_radius_in_texels;
+ ShadowTexelsPerGap = texels_per_unit_of_gap;
+ ShadowUnitsPerDepth = units_per_unit_of_depth;
+ ShadowSkyFill = sky_fill;
ShadowReceiving = true;
}
@@ -1611,6 +1621,10 @@ void DX11BackendClass::Upload_Constants()
? 1.0f / static_cast<float>(ViewportWidth) : 0.0f;
pixel_block.ShadowViewport[1] = (ViewportHeight > 0)
? 1.0f / static_cast<float>(ViewportHeight) : 0.0f;
+ pixel_block.ShadowSoftness[0] = ShadowNarrowestRadius;
+ pixel_block.ShadowSoftness[1] = ShadowTexelsPerGap;
+ pixel_block.ShadowSoftness[2] = ShadowUnitsPerDepth;
+ pixel_block.ShadowSoftness[3] = ShadowSkyFill;
}
if ((!PixelConstantsHeld
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
index 0fc7206d3..aa26be5b0 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
@@ -161,7 +161,9 @@ public:
// takes a pixel into the sun's clip space is built here rather than handed in: the sun's view
// and projection are the ones this held while the map was bound, and the frame's are the ones
// it holds when the draw arrives, so both halves are already in one convention.
- void Set_Shadow_Parameters(float bias, float strength, float radius_in_texels);
+ void Set_Shadow_Parameters(float bias, float strength, float widest_radius_in_texels,
+ float narrowest_radius_in_texels, float texels_per_unit_of_gap, float units_per_unit_of_depth,
+ float sky_fill);
void Clear_Shadow_Parameters();
bool Shadow_Map_Bound() const { return ShadowMapBound; }
ID3D11ShaderResourceView * Shadow_Map() const { return ShadowMapTexture; }
@@ -283,6 +285,10 @@ private:
float ShadowFromClip[16];
float ShadowParameters[4];
float ShadowViewport[4];
+ // The narrowest the filter goes, how many texels it opens per world unit of gap between a
+ // caster and what it falls on, how many world units a unit of depth is, and how much of a
+ // wide shadow the sky fills back in.
+ float ShadowSoftness[4];
};
// A model under directional lights, drawn by generated programs.
bool Normal_Mapped() const;
@@ -359,6 +365,10 @@ private:
float ShadowBias;
float ShadowStrength;
float ShadowRadius;
+ float ShadowNarrowestRadius;
+ float ShadowTexelsPerGap;
+ float ShadowUnitsPerDepth;
+ float ShadowSkyFill;
bool ShadowReceiving;
// The frame's clip space to the sun's, worked out from a scene view and projection pair and
// kept until one of them changes, because an inverse a draw does not need is an inverse nobody
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
index efb560063..59b4effa3 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
@@ -292,10 +292,13 @@ bool Direct3D11_Shadow_Map_Bound()
return Active ? Backend.Shadow_Map_Bound() : false;
}
-void Direct3D11_Set_Shadow_Parameters(float bias, float strength, float radius_in_texels)
+void Direct3D11_Set_Shadow_Parameters(float bias, float strength, float widest_radius_in_texels,
+ float narrowest_radius_in_texels, float texels_per_unit_of_gap, float units_per_unit_of_depth,
+ float sky_fill)
{
if (Active) {
- Backend.Set_Shadow_Parameters(bias, strength, radius_in_texels);
+ Backend.Set_Shadow_Parameters(bias, strength, widest_radius_in_texels,
+ narrowest_radius_in_texels, texels_per_unit_of_gap, units_per_unit_of_depth, sky_fill);
}
}
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
index 5b34d2ba3..c66539352 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
@@ -112,7 +112,9 @@ std::string Direct3D11_Shadow_Map_Report();
// sun's clip space, the depth bias that keeps a surface from shadowing itself, how dark a fully
// blocked pixel goes and how wide the filter reaches, in texels. Set once a frame, cleared when
// the frame has no map.
-void Direct3D11_Set_Shadow_Parameters(float bias, float strength, float radius_in_texels);
+void Direct3D11_Set_Shadow_Parameters(float bias, float strength, float widest_radius_in_texels,
+ float narrowest_radius_in_texels, float texels_per_unit_of_gap, float units_per_unit_of_depth,
+ float sky_fill);
void Direct3D11_Clear_Shadow_Parameters();
// The CPU has just written this surface. The next bind of its texture fills the Direct3D 11 copy
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp
index 557f4e012..203fd7c70 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp
@@ -243,7 +243,8 @@ static void write_pixel_preamble(std::string & hlsl, bool bumped = false)
hlsl +=
" row_major float4x4 ShadowFromClip;\n"
" float4 ShadowParameters;\n"
- " float4 ShadowViewport;\n";
+ " float4 ShadowViewport;\n"
+ " float4 ShadowSoftness;\n";
hlsl += "};\n";
if (bumped) {
hlsl += "Texture2D NormalMap : register(t4);\n";
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.cpp
index 0bb129648..b98036877 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.cpp
@@ -375,7 +375,8 @@ bool CombinerShader_Generate(const CombinerDescription & description, CombinerSh
// D3D9 matrix and D3D9 stores one by rows.
" row_major float4x4 ShadowFromClip;\n"
" float4 ShadowParameters;\n"
- " float4 ShadowViewport;\n";
+ " float4 ShadowViewport;\n"
+ " float4 ShadowSoftness;\n";
}
hlsl += "};\n";
if (description.NormalMapped) {
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.h
index da6bafa33..44291937d 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.h
@@ -124,16 +124,53 @@ const unsigned NORMAL_MAPPED_LIGHTS = 4;
" if (sun.z < 0.0 || sun.z > 1.0) return 1.0;\n" \
" float texel = ShadowParameters.x;\n" \
" float bias = ShadowParameters.y;\n" \
- " float radius = ShadowParameters.w;\n" \
+ " float widest = ShadowParameters.w;\n" \
+ " float narrowest = ShadowSoftness.x;\n" \
+ "\n" \
+ " // What is casting the shadow and how far above this pixel it is. The search is the\n" \
+ " // widest the filter is allowed to be, because a blocker it does not find is a blocker\n" \
+ " // whose penumbra never opens.\n" \
+ " float blocker_depth = 0.0;\n" \
+ " float blockers = 0.0;\n" \
+ " for (int sy = -2; sy <= 2; ++sy) {\n" \
+ " for (int sx = -2; sx <= 2; ++sx) {\n" \
+ " float2 at = map + float2(sx, sy) * texel * widest * 0.5;\n" \
+ " float depth = ShadowMap.SampleLevel(ShadowSampler, at, 0).r;\n" \
+ " if (depth + bias < sun.z) { blocker_depth += depth; blockers += 1.0; }\n" \
+ " }\n" \
+ " }\n" \
+ " if (blockers < 0.5) return 1.0;\n" \
+ " blocker_depth /= blockers;\n" \
+ "\n" \
+ " // The gap between the caster and this pixel, in world units, is what opens the filter:\n" \
+ " // a track on the ground stays hard, a helicopter's shadow spreads.\n" \
+ " float gap = max(sun.z - blocker_depth, 0.0) * ShadowSoftness.z;\n" \
+ " float radius = clamp(narrowest + gap * ShadowSoftness.y, narrowest, widest);\n" \
+ "\n" \
+ " // Five by five rather than three by three: opened up to nine texels, nine taps stand so\n" \
+ " // far apart that a body as narrow as a helicopter's falls between them and casts nothing.\n" \
+ " // The grid is turned by an angle taken from the pixel's own place on the screen, which\n" \
+ " // trades the steps a fixed grid leaves across a wide penumbra for noise the eye reads as\n" \
+ " // a gradient.\n" \
+ " float turn = frac(sin(dot(position.xy, float2(12.9898, 78.233))) * 43758.5453) * 6.2831853;\n" \
+ " float2 turn_cos_sin = float2(cos(turn), sin(turn));\n" \
" float blocked = 0.0;\n" \
- " for (int y = -1; y <= 1; ++y) {\n" \
- " for (int x = -1; x <= 1; ++x) {\n" \
- " float2 at = map + float2(x, y) * texel * radius;\n" \
+ " for (int y = -2; y <= 2; ++y) {\n" \
+ " for (int x = -2; x <= 2; ++x) {\n" \
+ " float2 step = float2(x, y) * 0.5;\n" \
+ " step = float2(step.x * turn_cos_sin.x - step.y * turn_cos_sin.y,\n" \
+ " step.x * turn_cos_sin.y + step.y * turn_cos_sin.x);\n" \
+ " float2 at = map + step * texel * radius;\n" \
" float depth = ShadowMap.SampleLevel(ShadowSampler, at, 0).r;\n" \
" blocked += (depth + bias < sun.z) ? 1.0 : 0.0;\n" \
" }\n" \
" }\n" \
- " return 1.0 - ShadowParameters.z * (blocked / 9.0);\n" \
+ "\n" \
+ " // The sky is the other light in the scene and it fills a shadow back in the further its\n" \
+ " // caster is, which is why a shadow from high up reads pale as well as soft.\n" \
+ " float openness = saturate((radius - narrowest) / max(widest - narrowest, 1e-3));\n" \
+ " float strength = ShadowParameters.z * (1.0 - ShadowSoftness.w * openness);\n" \
+ " return 1.0 - strength * (blocked / 25.0);\n" \
"}\n" \
"\n"