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): draw the casters into the sun depth buffer

Olcay Seygan586452abfb06
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
index db4a9baf5..de69c5547 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
@@ -175,6 +175,8 @@ public:
Bool m_startAtMaxZoom; // "StartAtMaxZoom": a game opens as far out as the wheel goes, not at the map's own default
Bool m_shadowsForProps; // "ShadowsForProps": fences, rubbish, shrubs - scenery the art gave no shadow at all
Bool m_shadowsForParticles; // "ShadowsForParticles": big alpha-blended particle clouds drop a soft blob on the ground
+ Bool m_shadowMap; // "-shadowmap": draw the casters into the sun's depth buffer as well
+ Bool m_shadowMapReport; // "-shadowmapreport": log what ended up in that buffer, once a second
Bool m_particleGroundBounce; // "-particlebounce": terrain collision on for every particle system
Real m_smokeThickness; // "-smoke": how much longer and thicker every smoke system runs, 0 for shipped behaviour
Int m_particleCapOverride; // "-particlecap": stand in for the options slider's MaxParticleCount, 0 to use it
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
index 66efe5960..e2529a700 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
@@ -917,6 +917,33 @@ Int parseParticleCap(char *args[], int num)
return 2;
}
+/* -shadowmap: draw every shadow caster into the sun's depth buffer as well as its stencil volume.
+
+ Nothing samples that buffer yet, so on its own this switch changes no pixel of the frame and
+ costs a second draw of every caster. That is deliberate: it makes the sun's camera, the box it
+ covers and the depth pass provable on their own, before anything is read back out of it.
+ -shadowmapreport then logs what the pass actually left in the buffer, which is the only thing
+ that tells a pass that drew the world apart from a pass that drew nothing. Direct3D 11 only;
+ the map has no Direct3D 9 twin. SHADOW-MAP-PLAN.md phase 1. */
+Int parseShadowMap(char *args[], int num)
+{
+ if (TheWritableGlobalData)
+ {
+ TheWritableGlobalData->m_shadowMap = TRUE;
+ }
+ return 1;
+}
+
+Int parseShadowMapReport(char *args[], int num)
+{
+ if (TheWritableGlobalData)
+ {
+ TheWritableGlobalData->m_shadowMap = TRUE;
+ TheWritableGlobalData->m_shadowMapReport = TRUE;
+ }
+ return 1;
+}
+
/* -noparticleshadows: take the soft blob back off the ground under every particle cloud.
ShadowsForParticles is on by default and the shipped INI has no entry for it, so without this
@@ -2143,6 +2170,8 @@ static CommandLineParam params[] =
{ "-particlebounce", parseParticleBounce },
{ "-smoke", parseSmoke },
{ "-particlecap", parseParticleCap },
+ { "-shadowmap", parseShadowMap },
+ { "-shadowmapreport", parseShadowMapReport },
{ "-noparticleshadows", parseNoParticleShadows },
{ "-quickstart", parseQuickStart },
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
index 366c120a6..93f03f71a 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
@@ -679,6 +679,8 @@ GlobalData::GlobalData()
m_startAtMaxZoom = TRUE; //open a game framed as wide as the player could zoom by hand
m_shadowsForProps = TRUE; //likewise: scenery with no shadow of its own gets one
m_shadowsForParticles = TRUE; //on by default: the shipped INI has no entry for it
+ m_shadowMap = FALSE; //the sun's depth pass is off until something reads it
+ m_shadowMapReport = FALSE;
m_textureReductionFactor = -1;
m_enableBehindBuildingMarkers = TRUE;
m_scriptDebug = FALSE;
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DVolumetricShadow.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DVolumetricShadow.h
index 2540f06ea..aecb4f9e5 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DVolumetricShadow.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DVolumetricShadow.h
@@ -76,6 +76,10 @@ public:
// rendering
void renderShadows( Bool forceStencilFill );
+
+ /// Draw every caster into the sun's depth buffer, before the frame itself is drawn. Nothing
+ /// reads that buffer yet; SHADOW-MAP-PLAN.md phase 2 is what makes it a shadow.
+ void renderShadowMap( CameraClass &sceneCamera );
void ReleaseResources(void);
Bool ReAcquireResources(void);
@@ -122,6 +126,7 @@ class W3DVolumetricShadow : public Shadow
const W3DShadowGeometry *getGeometry(void) {return m_geometry;}
void setRenderObject( RenderObjClass *robj) {assert(m_robj==NULL); m_robj=robj;}
+ RenderObjClass *getRenderObject( void ) {return m_robj;} ///< the mesh this shadow is of, for the sun's depth pass
void setRenderObjExtent ( Real extent) { m_robjExtent = extent; }
// called once per frame, updates shadow volume when necessary
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
index 12df84a40..90b99abb5 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
@@ -57,10 +57,15 @@
#include "WW3D2/statistics.h"
#include "Common/PerfTimer.h"
#include "GameLogic/TerrainLogic.h"
+#include "GameLogic/GameLogic.h"
#include "WW3D2/DX8Caps.h"
#include "GameClient/Drawable.h"
#include "wwshade/shdmesh.h"
#include "wwshade/shdsubmesh.h"
+#include "WW3D2/camera.h"
+#include "WW3D2/dx8renderer.h"
+#include "WW3D2/dx11runtime.h"
+#include "GameClient/View.h"
#ifdef _INTERNAL
// for occasional debugging...
@@ -91,6 +96,16 @@ const Real cosAngleToCare = cos ((0.2 * PI) / 180.0); //1.5 degree difference
#define SHADOW_SAMPLING_INTERVAL (MAP_XY_FACTOR * 2.0f) //stepsize along ray used to find lowest point on terrain within shadow's reach.
#define OVERHANGING_OBJECT_CLAMP_ANGLE (80.0f/180.0f*PI) //for objects that are right on a cliff edge, clamp light angle to cast a nearly vertical shadow.
+// The sun's depth buffer and the box it covers. 2048 texels over 1800 world units is about one
+// texel to the metre at the game's own scale, which is finer than the stencil edge it replaces;
+// the box is centred on what the tactical camera looks at and a zoom wider than it loses the
+// shadows at the edge of the screen before it loses anything the player is watching.
+#define SHADOW_MAP_TEXELS 2048
+#define SHADOW_MAP_HALF_WIDTH 900.0f
+#define SHADOW_MAP_SUN_DISTANCE 2000.0f
+#define SHADOW_MAP_NEAR_CLIP 10.0f
+#define SHADOW_MAP_FAR_CLIP 4000.0f
+
//#define SV_DEBUG
//#define SV_DEBUG_BOUNDS
@@ -3780,6 +3795,94 @@ DECLARE_PERF_TIMER(stencilShadows)
DECLARE_PERF_TIMER(shadowVolumeUpdate)
DECLARE_PERF_TIMER(shadowVolumeSubmit)
+/** The sun's depth pass. The casters are the ones that cast a volume today, drawn again from the
+ sun into a depth buffer nothing samples yet, so this phase can be proved on its own: with it
+ off the frame is what it was, and with it on the map has the world in it and the frame is
+ still what it was. The box is square and centred on what the tactical camera is looking at,
+ which is the ground the player can see; a cascade is only worth it once the box has to cover
+ more than one view. SHADOW-MAP-PLAN.md phase 1. */
+void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
+{
+ if (!TheGlobalData->m_shadowMap || m_shadowList == NULL || TheTacticalView == NULL)
+ return;
+
+ if (!Direct3D11_Begin_Shadow_Map( SHADOW_MAP_TEXELS ))
+ return;
+
+ Coord3D look;
+ TheTacticalView->getPosition( &look );
+ const Vector3 focus( look.x, look.y, TheTerrainLogic->getGroundHeight( look.x, look.y ) );
+
+ Vector3 toSun = TheW3DShadowManager->getLightPosWorld( 0 );
+ toSun.Normalize();
+
+ CameraClass sun;
+ sun.Set_Projection_Type( CameraClass::ORTHO );
+ sun.Set_View_Plane( Vector2( -SHADOW_MAP_HALF_WIDTH, -SHADOW_MAP_HALF_WIDTH ),
+ Vector2( SHADOW_MAP_HALF_WIDTH, SHADOW_MAP_HALF_WIDTH ) );
+ sun.Set_Clip_Planes( SHADOW_MAP_NEAR_CLIP, SHADOW_MAP_FAR_CLIP );
+
+ Matrix3D transform;
+ transform.Look_At( focus + toSun * SHADOW_MAP_SUN_DISTANCE, focus, 0.0f );
+ sun.Set_Transform( transform );
+
+ Matrix4x4 projection;
+ sun.Get_D3D_Projection_Matrix( &projection );
+ DX8Wrapper::Set_Projection_Transform_With_Z_Bias( projection, SHADOW_MAP_NEAR_CLIP,
+ SHADOW_MAP_FAR_CLIP );
+ Matrix3D view;
+ transform.Get_Orthogonal_Inverse( view );
+ DX8Wrapper::Set_Transform( D3DTS_VIEW, view );
+
+ // Depth is the whole point of the pass and colour is the whole cost of it.
+ DX8Wrapper::Set_DX8_Render_State( D3DRS_COLORWRITEENABLE, 0 );
+ DX8Wrapper::Set_DX8_Render_State( D3DRS_ZENABLE, TRUE );
+ DX8Wrapper::Set_DX8_Render_State( D3DRS_ZWRITEENABLE, TRUE );
+ 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 );
+
+ RenderInfoClass sunInfo( sun );
+ Int casters = 0;
+ for (W3DVolumetricShadow *shadow = m_shadowList; shadow; shadow = shadow->m_next)
+ {
+ RenderObjClass *robj = shadow->getRenderObject();
+ if (robj == NULL || !robj->Is_Really_Visible() || !shadow->isRenderEnabled()
+ || shadow->isInvisibleEnabled())
+ continue;
+
+ robj->Render( sunInfo );
+ ++casters;
+ }
+ TheDX8MeshRenderer.Flush();
+
+ DX8Wrapper::Set_DX8_Render_State( D3DRS_COLORWRITEENABLE,
+ D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE
+ | D3DCOLORWRITEENABLE_ALPHA );
+
+ Direct3D11_End_Shadow_Map();
+
+ // The frame's own camera, put back: the view, the projection and the viewport all went with the
+ // sun. Without this everything drawn after the pass is drawn from the sun's seat, which is the
+ // whole picture rather than a corner of it.
+ sceneCamera.Apply();
+
+ // 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
+ // thirty times a second.
+ if (TheGlobalData->m_shadowMapReport && casters > 0)
+ {
+ static UnsignedInt nextReportFrame = 0;
+ const UnsignedInt frame = TheGameLogic ? TheGameLogic->getFrame() : 0;
+ if (frame >= nextReportFrame)
+ {
+ nextReportFrame = frame + LOGICFRAMES_PER_SECOND;
+ DEBUG_LOG(("SHADOWMAP: %d casters, %s\n", casters,
+ Direct3D11_Shadow_Map_Report().c_str()));
+ }
+ }
+}
+
void W3DVolumetricShadowManager::renderShadows( Bool forceStencilFill )
{
USE_PERF_TIMER(stencilShadows)
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp
index 6cbab15fa..91e47b996 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp
@@ -52,6 +52,7 @@
#include "W3DDevice/GameClient/W3DDynamicLight.h"
#include "W3DDevice/GameClient/W3DGranny.h"
#include "W3DDevice/GameClient/W3DShadow.h"
+#include "W3DDevice/GameClient/W3DVolumetricShadow.h"
#include "W3DDevice/GameClient/W3DStatusCircle.h"
#include "W3DDevice/GameClient/W3DCustomScene.h"
#include "W3DDevice/GameClient/W3DShroud.h"
@@ -1146,6 +1147,13 @@ void RTS3DScene::Customized_Render( RenderInfoClass &rinfo )
m_translucentObjectsCount = 0; //start of new frame so no translucent objects
m_occludedObjectsCount = 0;
+ /* The sun's depth pass runs before the frame rather than with the shadows, because it flushes
+ the mesh renderer and drains the sort lists: run in the middle of a frame it drew the
+ helicopters' rotor discs into the shadow map instead of onto the screen, and they went
+ missing from both. Here nothing is queued yet. SHADOW-MAP-PLAN.md phase 1. */
+ if (TheW3DVolumetricShadowManager != NULL)
+ TheW3DVolumetricShadowManager->renderShadowMap(rinfo.Camera);
+
Int localPlayerIndex = ThePlayerList ? ThePlayerList->getLocalPlayer()->getPlayerIndex() : 0;
#define USE_LIGHT_ENV 1
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
index aef077073..3ee63e754 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
@@ -200,6 +200,14 @@ DX11BackendClass::DX11BackendClass()
, TargetCopy(NULL)
, TargetCopyView(NULL)
, NormalMap(NULL)
+ , ShadowMapSurface(NULL)
+ , ShadowMapDepth(NULL)
+ , ShadowMapTexture(NULL)
+ , ShadowMapSize(0)
+ , ShadowMapBound(false)
+ , ShadowMapSavedWidth(0)
+ , ShadowMapSavedHeight(0)
+ , ShadowMapSavedTarget(NULL)
, NormalMappedDraws(0)
, DrawsMade(0)
, DrawsRefused(0)
@@ -374,9 +382,172 @@ void DX11BackendClass::Shutdown()
TargetCopyView = NULL;
TargetCopy = NULL;
}
+ if (ShadowMapTexture != NULL) {
+ ShadowMapTexture->Release();
+ ShadowMapTexture = NULL;
+ }
+ if (ShadowMapDepth != NULL) {
+ ShadowMapDepth->Release();
+ ShadowMapDepth = NULL;
+ }
+ if (ShadowMapSurface != NULL) {
+ ShadowMapSurface->Release();
+ ShadowMapSurface = NULL;
+ }
+ ShadowMapSize = 0;
+ ShadowMapBound = false;
Device = NULL;
}
+/** The sun's depth buffer. One surface with two views of it, because a depth buffer that is also
+ sampled cannot be made as a depth format: the surface is typeless and each view says how its
+ bits are to be read. Made at the first size asked for and kept at that size. */
+bool DX11BackendClass::Begin_Shadow_Map(unsigned size)
+{
+ if (Device == NULL || size == 0 || ShadowMapBound) {
+ return false;
+ }
+
+ if (ShadowMapSurface == NULL) {
+ D3D11_TEXTURE2D_DESC description;
+ memset(&description, 0, sizeof(description));
+ description.Width = size;
+ description.Height = size;
+ description.MipLevels = 1;
+ description.ArraySize = 1;
+ description.Format = DXGI_FORMAT_R24G8_TYPELESS;
+ description.SampleDesc.Count = 1;
+ description.Usage = D3D11_USAGE_DEFAULT;
+ description.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
+ if (FAILED(Device->Get_Device()->CreateTexture2D(&description, NULL, &ShadowMapSurface))) {
+ Note_Refusal("the device refused the sun's depth buffer");
+ return false;
+ }
+
+ D3D11_DEPTH_STENCIL_VIEW_DESC depth_description;
+ memset(&depth_description, 0, sizeof(depth_description));
+ depth_description.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
+ depth_description.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
+ if (FAILED(Device->Get_Device()->CreateDepthStencilView(ShadowMapSurface, &depth_description,
+ &ShadowMapDepth))) {
+ Note_Refusal("the device refused a depth view of the sun's depth buffer");
+ ShadowMapSurface->Release();
+ ShadowMapSurface = NULL;
+ return false;
+ }
+
+ D3D11_SHADER_RESOURCE_VIEW_DESC texture_description;
+ memset(&texture_description, 0, sizeof(texture_description));
+ texture_description.Format = DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
+ texture_description.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
+ texture_description.Texture2D.MipLevels = 1;
+ if (FAILED(Device->Get_Device()->CreateShaderResourceView(ShadowMapSurface,
+ &texture_description, &ShadowMapTexture))) {
+ Note_Refusal("the device refused a texture view of the sun's depth buffer");
+ ShadowMapDepth->Release();
+ ShadowMapDepth = NULL;
+ ShadowMapSurface->Release();
+ ShadowMapSurface = NULL;
+ return false;
+ }
+
+ ShadowMapSize = size;
+ }
+
+ // The map cannot be read and written at once, and the frame before this one left it bound to
+ // whichever sampler reads it.
+ ID3D11ShaderResourceView * const none[DX11_BACKEND_TEXTURE_STAGES] = { NULL };
+ Device->Get_Context()->PSSetShaderResources(0, DX11_BACKEND_TEXTURE_STAGES, none);
+
+ Device->Get_Context()->OMSetRenderTargets(0, NULL, ShadowMapDepth);
+ Device->Get_Context()->ClearDepthStencilView(ShadowMapDepth,
+ D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
+
+ ShadowMapSavedWidth = ViewportWidth;
+ ShadowMapSavedHeight = ViewportHeight;
+ ShadowMapSavedTarget = CurrentTarget;
+ ShadowMapBound = true;
+ Set_Viewport(0, 0, ShadowMapSize, ShadowMapSize);
+ Forget_Bindings();
+ return true;
+}
+
+void DX11BackendClass::End_Shadow_Map()
+{
+ if (!ShadowMapBound) {
+ return;
+ }
+
+ ShadowMapBound = false;
+ // Back to whatever the frame was drawing into, which is a texture the post chain shows and not
+ // the back buffer.
+ Set_Render_Target(ShadowMapSavedTarget);
+ ShadowMapSavedTarget = NULL;
+ if (ShadowMapSavedWidth != 0 && ShadowMapSavedHeight != 0) {
+ Set_Viewport(0, 0, ShadowMapSavedWidth, ShadowMapSavedHeight);
+ }
+ Forget_Bindings();
+}
+
+/** What is in the map, read back once. A caster pass that drew nothing leaves every texel at the
+ clear value, which no draw count tells apart from a pass that drew the whole world. */
+std::string DX11BackendClass::Shadow_Map_Report()
+{
+ if (ShadowMapSurface == NULL) {
+ return "no shadow map";
+ }
+
+ D3D11_TEXTURE2D_DESC description;
+ ShadowMapSurface->GetDesc(&description);
+ description.Usage = D3D11_USAGE_STAGING;
+ description.BindFlags = 0;
+ description.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
+ description.MiscFlags = 0;
+
+ ID3D11Texture2D * staging = NULL;
+ if (FAILED(Device->Get_Device()->CreateTexture2D(&description, NULL, &staging))) {
+ return "no staging copy of the shadow map";
+ }
+
+ Device->Get_Context()->CopyResource(staging, ShadowMapSurface);
+
+ std::string answer = "unreadable shadow map";
+ D3D11_MAPPED_SUBRESOURCE mapped;
+ if (SUCCEEDED(Device->Get_Context()->Map(staging, 0, D3D11_MAP_READ, 0, &mapped))) {
+ const unsigned DEPTH_BITS = 24;
+ const float FURTHEST = (float)((1u << DEPTH_BITS) - 1);
+ unsigned drawn = 0;
+ float nearest = 1.0f;
+ double sum = 0.0;
+ for (unsigned row = 0; row < description.Height; ++row) {
+ const unsigned * line = (const unsigned *)((const unsigned char *)mapped.pData
+ + row * mapped.RowPitch);
+ for (unsigned column = 0; column < description.Width; ++column) {
+ const float depth = (float)(line[column] & 0x00FFFFFF) / FURTHEST;
+ if (depth < 1.0f) {
+ ++drawn;
+ sum += depth;
+ if (depth < nearest) {
+ nearest = depth;
+ }
+ }
+ }
+ }
+ Device->Get_Context()->Unmap(staging, 0);
+
+ char line[160];
+ const unsigned texels = description.Width * description.Height;
+ snprintf(line, sizeof(line),
+ "%ux%u, %u texels drawn into (%.1f%%), nearest %.4f, mean %.4f",
+ description.Width, description.Height, drawn, 100.0f * (float)drawn / (float)texels,
+ nearest, (drawn > 0) ? sum / drawn : 1.0);
+ answer = line;
+ }
+
+ staging->Release();
+ return answer;
+}
+
void DX11BackendClass::Set_Render_State(D3DRENDERSTATETYPE state, DWORD value)
{
RenderStates.Set_Render_State(state, value);
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
index 93307528a..37feecbb6 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
@@ -150,6 +150,20 @@ public:
// size a target comes in and kept; the engine uses two or three of them in a match.
void Set_Render_Target(ID3D11RenderTargetView * target);
+ // The sun's own depth buffer, which the caster pass draws into and everything that receives a
+ // shadow samples. Begin binds it as the only output, takes the viewport with it and clears it;
+ // End puts the back buffer and the viewport back. The texture is made once, at the first size
+ // asked for. SHADOW-MAP-PLAN.md phase 1.
+ bool Begin_Shadow_Map(unsigned size);
+ void End_Shadow_Map();
+ bool Shadow_Map_Bound() const { return ShadowMapBound; }
+ ID3D11ShaderResourceView * Shadow_Map() const { return ShadowMapTexture; }
+
+ // What is in the map, read back through a staging copy: how much of it was drawn into and how
+ // near the nearest thing is. A caster pass that drew nothing leaves a map that is all one
+ // value, and no draw count tells that apart from a pass that drew the world.
+ std::string Shadow_Map_Report();
+
// The two draws. Both resolve the shadow state into a pipeline first, and both return false
// when some part of that state has no D3D11 answer, which leaves the draw undone rather than
// drawn wrongly.
@@ -312,6 +326,21 @@ private:
unsigned ViewportWidth;
unsigned ViewportHeight;
+ // The sun's depth buffer and the two views of it: one to draw into, one to sample. The
+ // viewport the frame was using is kept while it is bound, because the map is square and the
+ // screen is not.
+ ID3D11Texture2D * ShadowMapSurface;
+ ID3D11DepthStencilView * ShadowMapDepth;
+ ID3D11ShaderResourceView * ShadowMapTexture;
+ unsigned ShadowMapSize;
+ bool ShadowMapBound;
+ unsigned ShadowMapSavedWidth;
+ unsigned ShadowMapSavedHeight;
+ // What the frame was drawing into when the pass took the device. Almost never the back buffer:
+ // the scene goes into a texture and the post chain puts it on the screen, so restoring the back
+ // buffer here left the rest of the frame painting somewhere nobody shows.
+ ID3D11RenderTargetView * ShadowMapSavedTarget;
+
float MaterialAmbient[4];
float MaterialDiffuse[4];
float MaterialSpecular[4];
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
index 7e2b16d0c..5a6ef3db0 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
@@ -275,6 +275,28 @@ unsigned long long Direct3D11_Normal_Mapped_Draws()
return Active ? Backend.Normal_Mapped_Draw_Count() : 0;
}
+bool Direct3D11_Begin_Shadow_Map(unsigned size)
+{
+ return Active ? Backend.Begin_Shadow_Map(size) : false;
+}
+
+void Direct3D11_End_Shadow_Map()
+{
+ if (Active) {
+ Backend.End_Shadow_Map();
+ }
+}
+
+bool Direct3D11_Shadow_Map_Bound()
+{
+ return Active ? Backend.Shadow_Map_Bound() : false;
+}
+
+std::string Direct3D11_Shadow_Map_Report()
+{
+ return Active ? Backend.Shadow_Map_Report() : std::string("no Direct3D 11 backend");
+}
+
void Direct3D11_Mirror_Render_Target(struct IDirect3DSurface9 * surface)
{
if (!Active) {
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
index 1498559d7..c1039da65 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
@@ -36,6 +36,8 @@
#include <windows.h>
+#include <string>
+
class DX11BackendClass;
class DX11BufferTwinClass;
class DX11DeviceClass;
@@ -97,6 +99,15 @@ void Direct3D11_Mirror_Normal_Map(struct IDirect3DBaseTexture9 * normal_map);
void Direct3D11_Set_Terrain_Sun(const float direction[3]);
unsigned long long Direct3D11_Normal_Mapped_Draws();
+// The sun's depth buffer. Between Begin and End every draw lands in it and nowhere else, which is
+// how the caster pass is written without the engine knowing what a render target is. False from
+// Begin means there is no Direct3D 11 backend or the device refused the surface, and the caller
+// draws nothing rather than drawing the casters over the frame. SHADOW-MAP-PLAN.md.
+bool Direct3D11_Begin_Shadow_Map(unsigned size);
+void Direct3D11_End_Shadow_Map();
+bool Direct3D11_Shadow_Map_Bound();
+std::string Direct3D11_Shadow_Map_Report();
+
// The CPU has just written this surface. The next bind of its texture fills the Direct3D 11 copy
// again. A no-op when the backend is not running.
void Direct3D11_Mark_Surface_Dirty(struct IDirect3DSurface9 * surface);