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
fix(w3ddevice): plain smoke casts into the sun's map, blob goes per system
Olcay Seygane2317500b128
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/ParticleSys.h b/GeneralsMD/Code/GameEngine/Include/GameClient/ParticleSys.hindex 7dece37f7..c17685d41 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/ParticleSys.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/ParticleSys.h@@ -336,6 +336,14 @@ extern void particleShadowBlobAdd( ParticleShadowBlob *blob, Real x, Real y, Reaextern Bool particleShadowBlobResolve( const ParticleShadowBlob *blob, Real *centerX, Real *centerY,Real *sizeX, Real *sizeY, Int *opacity );+/**+ * The optical depth through the middle of one particle in the sun's smoke map: the one its own+ * alpha implies, the one the eye sees through it, times how many layers it is drawn. A plain+ * billboard is one layer whatever getVolumeParticleDepth says (it answers 0 for anything that is+ * not a volume particle). Zero for a particle too faint to cast.+ */+extern Real particleSunMapOpticalDepth( Real alpha, UnsignedInt layers );+/*** What "-smoke <thickness>" does to one smoke system. A single number on the command line is* spent across three separate properties, because spending all of it on any one of them looks@@ -721,6 +729,12 @@ public:? MAX_VOLUME_PARTICLE_DEPTH : m_volumeParticleDepth;}+ /// The renderer put at least one of this system's particles into the sun's smoke map on its+ /// last frame, so the cloud shades the ground already and its blob would shade it twice.+ /// Written by the shadow pass every frame it runs, read by shouldCastGroundShadow.+ void setInSunMap( Bool inSunMap ) { m_inSunMap = inSunMap; }+ Bool isInSunMap( void ) const { return m_inSunMap; }+Bool shouldBillboard( void ) { return !m_isGroundAligned; }ParticleShaderType getShaderType( void ) { return m_shaderType; }@@ -807,6 +821,7 @@ protected:ParticleShadowBlob m_pendingShadowBlob; ///< the survivors' footprint, gathered by updateParticlesMarkBool m_pendingCastsGroundShadow; ///< decided by updateEmission, where update() always decided itUnsignedInt m_pendingDeaths; ///< particles updateParticlesMark marked; the reap stops once it has removed them+ Bool m_inSunMap; ///< setInSunMap: client-only, never xferedprotected:Particle * m_systemParticlesHead;@@ -980,10 +995,6 @@ private:/// The particle system manager singletonextern ParticleSystemManager *TheParticleSystemManager;-/// The renderer put the smoke into the sun's map on its last frame, so the clouds already shade the-/// ground and the blob under each one would shade it twice. Written by the shadow pass.-extern Bool TheSmokeInSunMap;-class DebugDisplayInterface;extern void ParticleSystemDebugDisplay( DebugDisplayInterface *dd, void *, FILE *fp = NULL );diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cppindex 7d5f9704d..bf462e918 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp@@ -82,7 +82,6 @@ static UnsignedInt particleTimerFrames( Real value )// the singletonParticleSystemManager *TheParticleSystemManager = NULL;-Bool TheSmokeInSunMap = FALSE;//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////@@ -363,6 +362,21 @@ Particle::~Particle()//DEBUG_ASSERTLOG(!(totalParticleCount % 100 == 0), ( "TotalParticleCount = %d\n", m_totalParticleCount ));}+// ------------------------------------------------------------------------------------------------+/** See ParticleSys.h. The density scales the optical depth the alpha implies and is untuned; a+ * particle fainter than the thinnest casts nothing. */+// ------------------------------------------------------------------------------------------------+Real particleSunMapOpticalDepth( Real alpha, UnsignedInt layers )+{+ const Real SUN_MAP_DENSITY = 1.0f;+ const Real SUN_MAP_THINNEST = 0.02f;+ if (alpha < SUN_MAP_THINNEST)+ return 0.0f;+ if (alpha > 0.95f)+ alpha = 0.95f;+ return -(Real)log( 1.0f - alpha ) * (Real)( layers > 1 ? layers : 1 ) * SUN_MAP_DENSITY;+}+// ------------------------------------------------------------------------------------------------/** The soft blob a whole particle system drops on the terrain. See ParticleSys.h - free of* ParticleSystem so the whole decision is testable without a renderer. */@@ -1421,6 +1435,7 @@ ParticleSystem::ParticleSystem( const ParticleSystemTemplate *sysTemplate,m_personalityStore = 0;m_controlParticle = NULL;m_groundShadow = NULL;+ m_inSunMap = FALSE;// A system built without an id yet - the load path does that, so it can restore the saved id// first - registers itself once it has one, rather than here where the manager would file it@@ -2514,7 +2529,7 @@ Bool ParticleSystem::shouldCastGroundShadow( void ) constreturn FALSE;// the sun's map holds this cloud already and shades the ground under it through its own density- if (m_shaderType == ALPHA && TheSmokeInSunMap)+ if (m_inSunMap)return FALSE;if (m_isGroundAligned)diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cppindex 22f865869..493d13ab7 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp@@ -73,7 +73,7 @@#include "Platform/RenderTypes.h"#include "Lib/Clock.h" // Clock_Ticks: QueryPerformanceCounter on Windows, a monotonic clock elsewhere#include "GameClient/ParticleSys.h"-#include <math.h>+#include <algorithm>#include <vector>#ifdef _INTERNAL@@ -139,12 +139,9 @@ const Real cosAngleToCare = cos ((0.2 * PI) / 180.0); //1.5 degree difference#define SHADOW_MAP_SKY_FILL 0.12f// The smoke in the sun's light. The strength is how dark the thickest cloud leaves the ground and// the smoke behind it, under the solid casters' 0.45 because the sky still lights the ground under a-// cloud and the smoke itself scatters some of the sun on. The density scales the optical depth a-// particle's own alpha implies, the one the eye sees through it; a particle fainter than the-// thinnest casts nothing. Untuned: chosen from the arithmetic, not from a frame.+// cloud and the smoke itself scatters some of the sun on. How thick each particle is to the sun is+// particleSunMapOpticalDepth's. Untuned: chosen from the arithmetic, not from a frame.#define SMOKE_SHADOW_STRENGTH 0.4f-#define SMOKE_SHADOW_DENSITY 1.0f-#define SMOKE_SHADOW_THINNEST 0.02f// Whether the sun's map took this frame. The volumes read it to know whether to stand down, and it// is false on a machine with no Direct3D 11 device, which is what keeps that machine's shadows.@@ -3840,23 +3837,51 @@ DECLARE_PERF_TIMER(stencilShadows)DECLARE_PERF_TIMER(shadowVolumeUpdate)DECLARE_PERF_TIMER(shadowVolumeSubmit)+/** Nothing of this frame's smoke is in the map: every system keeps its blob. */+static void forgetSmokeInSunMap( void )+{+ if (TheParticleSystemManager == NULL)+ return;+ ParticleSystemManager::ParticleSystemList &systems = TheParticleSystemManager->getAllParticleSystems();+ for (ParticleSystemManager::ParticleSystemListIt it = systems.begin(); it != systems.end(); ++it)+ if (*it)+ (*it)->setInSunMap( FALSE );+}++/** One particle on its way into the smoke map, with the system it came from and how far it is from+ what the camera looks at, which decides who goes in when there are more than the map takes. */+struct SmokeCaster+{+ Real x, y, z, radius, opticalDepth;+ Real distanceSqr;+ ParticleSystem *system;+ bool operator<( const SmokeCaster &other ) const { return distanceSqr < other.distanceSqr; }+};+/** The smoke into the sun's light: every alpha-blended billboard particle as a soft ball, its optical- depth the one its own alpha implies, handed to the backend's smoke map. The systems are read- as the last client update left them, because the pass runs before the frame's particles are- drawn. Fire is additive and casts nothing; a ground-aligned system is a decal already. Says- whether the smoke is in the map, which is what takes the blob off the ground under each cloud;- with the option off this is a frame with no smoke, so the backend stops reading the map. */-static Bool fillSmokeMap( void )+ depth the one its own alpha implies (particleSunMapOpticalDepth), handed to the backend's smoke+ map. The systems are read as the last client update left them, because the pass runs before+ the frame's particles are drawn. Fire is additive and casts nothing; a ground-aligned system is+ a decal already. Only what stands in the sun's box counts, and past the map's limit the+ particles nearest the camera's focus win, so a new fire on screen is never starved by old smoke+ at the edge. Every system with a particle in the map is marked, and those lose their blob;+ with the option off, or the map refused, none is. */+static void fillSmokeMap( const Matrix3D &sunTransform, const Vector3 &focus ){- static std::vector<Real> casters; // kept: a burning base is thousands of particles every frame- casters.clear();+ static std::vector<SmokeCaster> found; // kept: a burning base is thousands of particles every frame+ static std::vector<Real> packed;+ static Int systemsLastReport = -1;+ static UnsignedInt nextReportFrame = 0;+ const size_t mostCasters = 16384; // the backend's own SMOKE_MOST_CASTERS++ forgetSmokeInSunMap();+ found.clear();+ packed.clear();if (TheGlobalData->m_volumetricSmokeShadows && TheParticleSystemManager != NULL){- const size_t mostCasters = 16384;ParticleSystemManager::ParticleSystemList &systems = TheParticleSystemManager->getAllParticleSystems();- for (ParticleSystemManager::ParticleSystemListIt it = systems.begin();- it != systems.end() && casters.size() < mostCasters * 5; ++it)+ for (ParticleSystemManager::ParticleSystemListIt it = systems.begin(); it != systems.end(); ++it){ParticleSystem *sys = *it;if (sys == NULL || sys->getShaderType() != ParticleSystemInfo::ALPHA || !sys->shouldBillboard()@@ -3866,28 +3891,82 @@ static Bool fillSmokeMap( void )if (*((UnsignedInt *)sys->getParticleTypeName().str()) == 0x44554D53)continue;- // a volume particle is drawn that many layers deep, and is that much thicker to the sun- const Real layers = (Real)sys->getVolumeParticleDepth();- for (Particle *p = sys->getFirstParticle(); p && casters.size() < mostCasters * 5; p = p->m_systemNext)+ const UnsignedInt layers = sys->getVolumeParticleDepth();+ for (Particle *p = sys->getFirstParticle(); p; p = p->m_systemNext){- Real alpha = p->getAlpha();- if (alpha < SMOKE_SHADOW_THINNEST)+ const Real opticalDepth = particleSunMapOpticalDepth( p->getAlpha(), layers );+ if (opticalDepth <= 0.0f)continue;- if (alpha > 0.95f)- alpha = 0.95f;const Coord3D *pos = p->getPosition();- casters.push_back( pos->x );- casters.push_back( pos->y );- casters.push_back( pos->z );- casters.push_back( p->getSize() * 0.5f ); // the billboard's size is its full width- casters.push_back( -(Real)log( 1.0f - alpha ) * layers * SMOKE_SHADOW_DENSITY );+ const Real radius = p->getSize() * 0.5f; // the billboard's size is its full width+ if (radius <= 0.0f)+ continue;++ // the box the depth map covers, in the sun's own frame: it looks down its -Z+ Vector3 inSun;+ Matrix3D::Inverse_Transform_Vector( sunTransform, Vector3( pos->x, pos->y, pos->z ), &inSun );+ const Real reach = SHADOW_MAP_HALF_WIDTH + radius;+ if (inSun.X < -reach || inSun.X > reach || inSun.Y < -reach || inSun.Y > reach+ || -inSun.Z < SHADOW_MAP_NEAR_CLIP - radius || -inSun.Z > SHADOW_MAP_FAR_CLIP + radius)+ continue;++ SmokeCaster caster;+ caster.x = pos->x;+ caster.y = pos->y;+ caster.z = pos->z;+ caster.radius = radius;+ caster.opticalDepth = opticalDepth;+ const Real dx = pos->x - focus.X;+ const Real dy = pos->y - focus.Y;+ caster.distanceSqr = dx * dx + dy * dy;+ caster.system = sys;+ found.push_back( caster );}}}- const Bool held = Direct3D11_Fill_Smoke_Map( casters.empty() ? NULL : &casters[ 0 ],- (unsigned)( casters.size() / 5 ), SMOKE_SHADOW_STRENGTH );- return held && TheGlobalData->m_volumetricSmokeShadows;+ const size_t inBox = found.size();+ if (found.size() > mostCasters)+ {+ std::nth_element( found.begin(), found.begin() + mostCasters, found.end() );+ found.resize( mostCasters );+ }++ packed.reserve( found.size() * 5 );+ for (size_t i = 0; i < found.size(); ++i)+ {+ packed.push_back( found[ i ].x );+ packed.push_back( found[ i ].y );+ packed.push_back( found[ i ].z );+ packed.push_back( found[ i ].radius );+ packed.push_back( found[ i ].opticalDepth );+ }++ const Bool held = Direct3D11_Fill_Smoke_Map( packed.empty() ? NULL : &packed[ 0 ],+ (unsigned)found.size(), SMOKE_SHADOW_STRENGTH );+ if (!held)+ return;++ Int systemsHeld = 0;+ for (size_t i = 0; i < found.size(); ++i)+ {+ if (!found[ i ].system->isInSunMap())+ {+ found[ i ].system->setInSunMap( TRUE );+ ++systemsHeld;+ }+ }++ // A run's log says whether the smoke was in the map at all: the first frame it held any, and+ // every ten seconds while it does.+ const UnsignedInt frame = TheGameLogic ? TheGameLogic->getFrame() : 0;+ if (!found.empty() && (systemsLastReport < 0 || frame >= nextReportFrame))+ {+ nextReportFrame = frame + 10 * LOGICFRAMES_PER_SECOND;+ systemsLastReport = systemsHeld;+ DEBUG_LOG(("SMOKEMAP: frame %u, %d casters from %d systems in the sun's map, %d more past its limit\n",+ frame, (Int)found.size(), systemsHeld, (Int)( inBox - found.size() )));+ }}/** The sun's depth pass. The casters are the ones that cast a volume today, drawn again from the@@ -3899,14 +3978,14 @@ static Bool fillSmokeMap( void )void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera ){theShadowMapHoldsTheFrame = FALSE;- TheSmokeInSunMap = FALSE;if (!TheGlobalData->m_shadowMap || m_shadowList == NULL || TheTacticalView == NULL|| !Direct3D11_Begin_Shadow_Map( SHADOW_MAP_TEXELS )){- //no Direct3D 11 device, or it refused the surface: the volumes keep the frame, and no smoke is- //read out of a map the last frame filled+ //no Direct3D 11 device, or it refused the surface: the volumes keep the frame, no smoke is+ //read out of a map the last frame filled, and every cloud keeps its blobDirect3D11_Fill_Smoke_Map( NULL, 0, 0.0f );+ forgetSmokeInSunMap();return;}@@ -4029,13 +4108,24 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )Direct3D11_End_Shadow_Map();// The smoke goes into a map of its own through the sun the casters were just drawn with.- TheSmokeInSunMap = fillSmokeMap();+ fillSmokeMap( transform, focus );// 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();+ /* And its view said to the backend outright. The sorted particles are written in this camera's+ space and drawn with an identity view, and their pixels go back to the world through this+ matrix; a guess at it from whatever perspective draw came last picked up the camera-relative+ view the aligned spheres draw with. In the layout DX8Wrapper hands the device. */+ {+ Matrix4x4 view;+ DX8Wrapper::Get_Transform( D3DTS_VIEW, view );+ const Matrix4x4 deviceView = view.Transpose();+ Direct3D11_Set_Scene_View( (const float *)&deviceView );+ }+/* And what turns the map into a shadow. The matrix that takes a pixel from the frame's clipspace into the sun's is built in the backend, out of the sun's own view and projection as itheld them during the pass and the frame's as it holds them now: both are already there, indiff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cppindex f2c7fd7a3..f59edc1b2 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp@@ -305,6 +305,7 @@ DX11BackendClass::DX11BackendClass()set_identity(View);set_identity(Projection);set_identity(SceneView);+ SceneViewKnown = false;SmokeMapSurface = NULL;SmokeMapTarget = NULL;SmokeMapTexture = NULL;@@ -842,13 +843,13 @@ bool DX11BackendClass::Fill_Smoke_Map(const float * casters, unsigned count, floif (FAILED(device->CreateBuffer(&buffer, NULL, &SmokeSplatInstances))) {Note_Refusal("the device refused the smoke casters' buffer");SmokeSplatInstances = NULL;- return true;+ return false;}SmokeSplatCapacity = capacity;}D3D11_MAPPED_SUBRESOURCE mapped;if (FAILED(context->Map(SmokeSplatInstances, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped))) {- return true;+ return false;}memcpy(mapped.pData, &SmokeSplats[0], splats * SMOKE_SPLAT_FLOATS * sizeof(float));context->Unmap(SmokeSplatInstances, 0);@@ -899,6 +900,44 @@ bool DX11BackendClass::Camera_Space_Draw() constreturn Projection[11] != 0.0f && memcmp(View, IDENTITY, sizeof(IDENTITY)) == 0;}+void DX11BackendClass::Set_Scene_View(const float view[16])+{+ if (!SceneViewKnown || memcmp(SceneView, view, sizeof(SceneView)) != 0) {+ memcpy(SceneView, view, sizeof(SceneView));+ SceneViewKnown = true;+ ConstantsChanged = true;+ }+}++bool DX11BackendClass::Views_Current_Target(unsigned stage, ID3D11ShaderResourceView * texture) const+{+ if (texture == NULL || CurrentTarget == NULL) {+ return false;+ }+ // The whole scene is drawn into a target when the screen filters are on, and asking every+ // texture of every draw which resource it views went through the runtime twice a stage: 4% of+ // the fireball frame. The answer only changes with the view or the target, and a new target+ // calls Forget_Bindings, which clears these.+ if (texture != TargetCheckedViews[stage]) {+ ID3D11Resource * resource = NULL;+ texture->GetResource(&resource);+ resource->Release();+ TargetCheckedViews[stage] = texture;+ TargetCheckedIsTarget[stage] = resource == CurrentTargetResource;+ }+ return TargetCheckedIsTarget[stage];+}++bool DX11BackendClass::Samples_Current_Target() const+{+ for (unsigned stage = 0; stage < DX11_BACKEND_TEXTURE_STAGES; ++stage) {+ if (Views_Current_Target(stage, Textures[stage])) {+ return true;+ }+ }+ return false;+}+/** The inverse of a four by four, by cofactors. Nothing else in the backend needed one: everyother matrix it holds arrives ready to use, and this is the one journey that goes the otherway, out of the frame's clip space and back into the world. */@@ -1043,6 +1082,16 @@ void DX11BackendClass::Set_Texture(unsigned stage, ID3D11ShaderResourceView * teif ((Textures[stage] == NULL) != (texture == NULL)) {PipelineChanged = true;}+ // Sampling the current target takes a draw off the shadow receivers (Shadow_Receiving), so a+ // change into or out of that is a change of pipeline. The old view's answer is in the cache+ // when it was drawn with; when it is not, it is taken as yes.+ else if (texture != Textures[stage] && CurrentTarget != NULL) {+ const bool was_target = (TargetCheckedViews[stage] == Textures[stage])+ ? TargetCheckedIsTarget[stage] : true;+ if (was_target || Views_Current_Target(stage, texture)) {+ PipelineChanged = true;+ }+ }Textures[stage] = texture;}}@@ -1612,6 +1661,9 @@ bool DX11BackendClass::Shadow_Receiving() constif ((VertexFormat & D3DFVF_XYZRHW) != 0) {return false; // already in screen space: the interface, the filters, the darkening quad}+ if (Samples_Current_Target()) {+ return false; // the heat haze: a copy of a picture that took its shadows already+ }if (RenderStates.Get_Render_State(D3DRS_ALPHABLENDENABLE) != FALSE) {const DWORD source = RenderStates.Get_Render_State(D3DRS_SRCBLEND);const DWORD destination = RenderStates.Get_Render_State(D3DRS_DESTBLEND);@@ -2035,12 +2087,9 @@ void DX11BackendClass::Upload_Constants()}// A camera space draw's pixels go back to the world through the scene camera's view, not through- // the identity it was drawn with; see SceneView in the header.+ // the identity it was drawn with; see Set_Scene_View in the header.const bool camera_space = Camera_Space_Draw();- if (!camera_space && Projection[11] != 0.0f) {- memcpy(SceneView, View, sizeof(View));- }- const float * const world_to_camera = camera_space ? SceneView : View;+ const float * const world_to_camera = (camera_space && SceneViewKnown) ? SceneView : View;if (ShadowReceiving) {if (!ShadowFromClipValid@@ -2248,6 +2297,9 @@ void DX11BackendClass::Forget_Bindings()memset(&Bound, 0, sizeof(Bound));memset(TargetCheckedViews, 0, sizeof(TargetCheckedViews));memset(TargetCheckedIsTarget, 0, sizeof(TargetCheckedIsTarget));+ // Whether a draw samples its own target is part of its pipeline (Shadow_Receiving), and every+ // target change comes through here.+ PipelineChanged = true;}void DX11BackendClass::Bind_State_Objects()@@ -2378,22 +2430,7 @@ void DX11BackendClass::Bind_Pipeline(const Pipeline & pipeline, ID3D11Buffer * vID3D11ShaderResourceView * DX11BackendClass::Readable_Texture(unsigned stage,ID3D11ShaderResourceView * texture){- if (texture == NULL || CurrentTarget == NULL) {- return texture;- }-- // The whole scene is drawn into a target when the screen filters are on, and asking every- // texture of every draw which resource it views went through the runtime twice a stage: 4% of- // the fireball frame. The answer only changes with the view or the target, and a new target- // calls Forget_Bindings, which clears these.- if (texture != TargetCheckedViews[stage]) {- ID3D11Resource * resource = NULL;- texture->GetResource(&resource);- resource->Release();- TargetCheckedViews[stage] = texture;- TargetCheckedIsTarget[stage] = resource == CurrentTargetResource;- }- if (!TargetCheckedIsTarget[stage]) {+ if (!Views_Current_Target(stage, texture)) {return texture;}ID3D11Resource * resource = CurrentTargetResource;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.hindex 71199a361..ae059d56c 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h@@ -189,12 +189,20 @@ public:// it held. A caster is five floats: its centre in world units, its radius and the optical// depth through its middle. Strength is how dark the thickest smoke leaves what is behind it;// zero casters or zero strength is a frame with no smoke in the light, and so is a frame that- // never calls this. False when the device cannot hold the map at all (no blendable, filterable- // four channel 32-bit float target), which the caller reads as the smoke not being in the sun's- // light and keeps its older shade. ffshader.h, VOLUMETRIC_SAMPLING, says what the map holds.+ // never calls this. True means the map holds every caster handed over that stands in the sun's+ // box; false that it holds none of them (no blendable, filterable four channel 32-bit float+ // target, or a buffer the device refused), which the caller reads as the smoke not being in the+ // sun's light and keeps its older shade. ffshader.h, VOLUMETRIC_SAMPLING, says what the map holds.bool Fill_Smoke_Map(const float * casters, unsigned count, float strength);ID3D11ShaderResourceView * Smoke_Map() const { return SmokeMapFilled ? SmokeMapTexture : NULL; }+ // The scene camera's view, which a camera space draw's pixels go back to the world through: the+ // sorted particles are written in camera space and drawn with an identity world and view+ // (PointGroupClass::Insert_Sorted_Billboards), so the view the backend holds for them says+ // nothing about where they are. Set by the shadow pass every frame it fills the map, after the+ // frame's camera is back; until the first one, such a draw uses the view it was drawn with.+ void Set_Scene_View(const float view[16]);+// 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.@@ -413,13 +421,13 @@ private:float ShadowFromClipProjection[16];bool ShadowFromClipValid;- // The sorted particles are written in camera space and drawn with an identity world and view- // (PointGroupClass::Insert_Sorted_Billboards), so the view the backend holds for them says- // nothing about where they are, and a shadow looked up through it lands somewhere else in the- // world. This is the last view a perspective draw held that was not the identity, which is the- // scene camera's: the matrix a camera space draw's pixels go back to the world through.+ // Set_Scene_View's matrix, and whether it has been set.float SceneView[16];+ bool SceneViewKnown;bool Camera_Space_Draw() const;+ // A stage samples the target the draw is going into: the heat haze, which bends a picture that+ // already took its shadows, and would take them a second time.+ bool Samples_Current_Target() const;// The smoke's map (Fill_Smoke_Map), the program that splats the casters into it and what it// draws with. Made on the first fill and kept; refused for good if the device cannot.@@ -600,9 +608,11 @@ private:ID3D11Texture2D * TargetCopy;ID3D11ShaderResourceView * TargetCopyView;ID3D11ShaderResourceView * Readable_Texture(unsigned stage, ID3D11ShaderResourceView * texture);- // Per stage, the last view asked about and whether it views the current target.- ID3D11ShaderResourceView * TargetCheckedViews[DX11_BACKEND_TEXTURE_STAGES];- bool TargetCheckedIsTarget[DX11_BACKEND_TEXTURE_STAGES];+ // Per stage, the last view asked about and whether it views the current target. Mutable because+ // Shadow_Receiving asks the same question while the pipeline is described.+ bool Views_Current_Target(unsigned stage, ID3D11ShaderResourceView * texture) const;+ mutable ID3D11ShaderResourceView * TargetCheckedViews[DX11_BACKEND_TEXTURE_STAGES];+ mutable bool TargetCheckedIsTarget[DX11_BACKEND_TEXTURE_STAGES];// 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 adiff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cppindex 9121acdf8..6904b6d2f 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp@@ -327,6 +327,13 @@ bool Direct3D11_Fill_Smoke_Map(const float * casters, unsigned count, float strereturn Active ? Backend.Fill_Smoke_Map(casters, count, strength) : false;}+void Direct3D11_Set_Scene_View(const float view[16])+{+ if (Active) {+ Backend.Set_Scene_View(view);+ }+}+void Direct3D11_Clear_Shadow_Parameters(){if (Active) {diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.hindex 557b72880..2d9a8ae36 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h@@ -147,6 +147,11 @@ void Direct3D11_Clear_Shadow_Parameters();// caller keeps its older shade under the clouds.bool Direct3D11_Fill_Smoke_Map(const float * casters, unsigned count, float strength);+// The scene camera's view, in the device's layout, set by the shadow pass once the frame's camera is+// back. A draw made in camera space (identity view, perspective projection: the sorted particles)+// looks its shadow up through it.+void Direct3D11_Set_Scene_View(const float view[16]);+// 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);diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cppindex 291503e54..c4f4e732c 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp@@ -72,6 +72,7 @@ std::string Direct3D11_Shadow_Map_Report() { return std::string("no Direct3D 11void Direct3D11_Set_Shadow_Parameters(float, float, float, float, float, float, float) {}void Direct3D11_Clear_Shadow_Parameters() {}bool Direct3D11_Fill_Smoke_Map(const float *, unsigned, float) { return false; }+void Direct3D11_Set_Scene_View(const float [16]) {}void Direct3D11_Mark_Surface_Dirty(struct IDirect3DSurface9 *) {}void Direct3D11_Mirror_Render_Target(struct IDirect3DSurface9 *) {}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 315cb1441..9a543d256 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -2106,6 +2106,20 @@ static void blobFeed(ParticleShadowBlob *blob, Int count, Real size, Real alpha)particleShadowBlobAdd(blob, (Real)i, 0.0f, size, alpha);}+// A plain billboard system answers 0 to getVolumeParticleDepth, and the first cut of the smoke map+// multiplied by that: every ordinary smoke particle went in with no thickness at all, the backend+// dropped it, and its cloud lost the blob as well. One layer, whatever the depth says.+TEST(sun_map_takes_a_plain_particle_as_one_layer)+{+ const Real plain = particleSunMapOpticalDepth(0.5f, 0);+ CHECK_NEAR(plain, 0.6931f, 0.001f);+ CHECK_NEAR(particleSunMapOpticalDepth(0.5f, 1), plain, 0.0001f);+ CHECK_NEAR(particleSunMapOpticalDepth(0.5f, 4), plain * 4.0f, 0.001f);+ // too faint to cast, and a fully opaque one held short of infinity+ CHECK_EQ(particleSunMapOpticalDepth(0.01f, 0), 0.0f);+ CHECK(particleSunMapOpticalDepth(1.0f, 0) < 3.1f);+}+TEST(blob_reset_leaves_nothing_to_resolve){ParticleShadowBlob blob;