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
feat(w3ddevice): screen warp for nuke and scud blasts
Olcay Seygan1f6eef138ec2
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e9ab64c9d..4347689d2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1336,6 +1336,7 @@ The whole HUD is on the hardware. Four colours carry it, and they mean the same- Explosions open on a white-hot flash. A tank shell, a rocket, a bomb truck or a dying tank now starts with a white core that cools through yellow and orange in a third of a second before EA's fireball takes over, and the core is as big as the blast: about a tank's width for a shell, much wider for a Scud Storm missile, a MOAB or a Daisy Cutter. On the default renderer the core spills into the screen's glow.- Big blasts light the ground further and for longer. Every explosion's light is warmer and holds a little longer, and a bomb truck, a Scud, a bunker buster, a carpet bomb or a nuke throws a wide orange glow that fades over one to two seconds. Falling buildings, the Daisy Cutter, the MOAB, nukes, exploding cars and suicide bombers used to light nothing at all.- The heaviest blasts leave bigger craters and send a ring of heat haze out across the ground for a second and a half to two seconds, if Heat Effects is on. Heat haze bends what is behind it by a few pixels now. On a blast that size it used to lay a second copy of the ground over the first, shifted by as much as twenty pixels, so the trees and ridges inside a nuke's ring came out doubled.+- A nuke pulls the picture into itself. For a quarter of a second the ground around the blast bends inward toward it, then a ring rolls out well past the fireball and shoves the battlefield aside as it passes, weaker the further it gets. Each Scud Storm missile does a smaller version of the same, and Nuke Cannon shells, Scud Launcher missiles, Tomahawks and bomb trucks give a short kick that is gone in half a second, so a battery firing all match never turns the screen to jelly. This is the default renderer; Classic and the old renderer keep only the heat ring.- Up to a thousand craters stay on the ground in a long match, where the game kept five hundred and quietly dropped the oldest.- An explosion lights everything around it, smoothly. On the default renderer the light of a blast reaches every tank, soldier, building and patch of ground it can see, fading out with distance, where the ground used to brighten in blocky squares and a model took only four lights at once, so in a big fight half the tanks next to a burning wreck stayed dark. The 32 blasts nearest the camera light at the same time, and a wall with its back to the fire stays in shadow. The light brightens what it falls on by three quarters at most: a nuke's took the ground to white within a few frames until it was held there.- A bright cloud no longer shows what is behind it as a line drawing. Inside a nuke's white cloud, or a falling War Factory's fireball, every tree and wreck under it used to come out as a thin grey outline, and the picture's sharpening ringed each dark piece in white. Both stop where the frame is bright.diff --git a/GeneralsMD/Code/Data/INI/FXListReforged.ini b/GeneralsMD/Code/Data/INI/FXListReforged.iniindex 4cec7ae97..c66e619a7 100644--- a/GeneralsMD/Code/Data/INI/FXListReforged.ini+++ b/GeneralsMD/Code/Data/INI/FXListReforged.ini@@ -19,6 +19,15 @@; ----------------------------------------------FXList ScudStormMissileDetonation+ ; the picture drawn into the blast, then a ring pushing it out (Direct3D 11 post chain only)+ ScreenDistortion+ Radius = 180+ PullTime = 180+ WaveTime = 1200+ PullStrength = 10%+ WaveStrength = 11%+ WaveWidth = 16%+ EndViewShakeType = STRONGEnd@@ -905,6 +914,15 @@ End; ----------------------------------------------FXList WeaponFX_SCUDMissileDetonationAnthrax+ ; a short bend, the launcher fires often+ ScreenDistortion+ Radius = 80+ PullTime = 100+ WaveTime = 650+ PullStrength = 7%+ WaveStrength = 8%+ WaveWidth = 16%+ EndViewShakeType = SEVEREEnd@@ -938,6 +956,15 @@ End; ----------------------------------------------FXList WeaponFX_SCUDMissileDetonationToxin+ ; a short bend, the launcher fires often+ ScreenDistortion+ Radius = 80+ PullTime = 100+ WaveTime = 650+ PullStrength = 7%+ WaveStrength = 8%+ WaveWidth = 16%+ EndViewShakeType = SEVEREEnd@@ -1549,6 +1576,15 @@ End; ----------------------------------------------FXList WeaponFX_SCUDMissileDetonationExplosive+ ; a short bend, the launcher fires often+ ScreenDistortion+ Radius = 80+ PullTime = 100+ WaveTime = 650+ PullStrength = 7%+ WaveStrength = 8%+ WaveWidth = 16%+ EndViewShakeType = SEVERE@@ -1690,6 +1726,15 @@ End; ----------------------------------------------FXList WeaponFX_BombTruckDefaultBombDetonation+ ; the Tomahawk's impact as well as the bomb truck's: a short bend, it fires often+ ScreenDistortion+ Radius = 70+ PullTime = 100+ WaveTime = 600+ PullStrength = 7%+ WaveStrength = 8%+ WaveWidth = 16%+ EndViewShakeType = SEVEREEnd@@ -2355,6 +2400,15 @@ End; ----------------------------------------------FXList GC_Chem_WeaponFX_SCUDMissileDetonationAnthrax+ ; a short bend, the launcher fires often+ ScreenDistortion+ Radius = 80+ PullTime = 100+ WaveTime = 650+ PullStrength = 7%+ WaveStrength = 8%+ WaveWidth = 16%+ EndViewShakeType = SEVEREEnd@@ -2534,6 +2588,15 @@ End; ----------------------------------------------FXList Demo_ScudStormMissileDetonation+ ; the picture drawn into the blast, then a ring pushing it out (Direct3D 11 post chain only)+ ScreenDistortion+ Radius = 180+ PullTime = 180+ WaveTime = 1200+ PullStrength = 10%+ WaveStrength = 11%+ WaveWidth = 16%+ EndViewShakeType = STRONGEnd@@ -2612,6 +2675,15 @@ End; ----------------------------------------------FXList WeaponFX_DEMO_SCUDMissileDetonationExplosive+ ; a short bend, the launcher fires often+ ScreenDistortion+ Radius = 80+ PullTime = 100+ WaveTime = 650+ PullStrength = 7%+ WaveStrength = 8%+ WaveWidth = 16%+ EndViewShakeType = SEVERE@@ -3150,6 +3222,16 @@ FXList FX_NukeType = SEVEREEnd+ ; the picture drawn into the blast, then a ring pushing it out (Direct3D 11 post chain only)+ ScreenDistortion+ Radius = 300+ PullTime = 250+ WaveTime = 1800+ PullStrength = 13%+ WaveStrength = 14%+ WaveWidth = 18%+ End+SoundName = ExplosionNeutronEnd@@ -3241,6 +3323,16 @@ FXList SupW_FX_NukeType = SEVEREEnd+ ; the picture drawn into the blast, then a ring pushing it out (Direct3D 11 post chain only)+ ScreenDistortion+ Radius = 300+ PullTime = 250+ WaveTime = 1800+ PullStrength = 13%+ WaveStrength = 14%+ WaveWidth = 18%+ End+SoundName = ExplosionNeutronEnd@@ -3405,6 +3497,15 @@ End; ----------------------------------------------FXList WeaponFX_NukeCannon+ ; a short bend, the cannon fires often+ ScreenDistortion+ Radius = 80+ PullTime = 120+ WaveTime = 700+ PullStrength = 9%+ WaveStrength = 9%+ WaveWidth = 16%+ EndViewShakeType = SEVERE@@ -3484,6 +3585,15 @@ End; EA declares this one twice in FXList.ini and the second copy, which this is,; is the one that loads.FXList Nuke_WeaponFX_NukeCannon+ ; a short bend, the cannon fires often+ ScreenDistortion+ Radius = 80+ PullTime = 120+ WaveTime = 700+ PullStrength = 9%+ WaveStrength = 9%+ WaveWidth = 16%+ EndViewShakeType = SEVEREdiff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/View.h b/GeneralsMD/Code/GameEngine/Include/GameClient/View.hindex 8dc87f5f2..ea4fb0b03 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/View.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/View.h@@ -67,6 +67,22 @@ enum PickTypePICK_TYPE_ALL_DRAWABLES = (PICK_TYPE_SELECTABLE | PICK_TYPE_SHRUBBERY | PICK_TYPE_MINES | PICK_TYPE_FORCEATTACKABLE)};+// ------------------------------------------------------------------------------------------------+/** A blast bending the picture around it, as the ScreenDistortion FX nugget describes it: the frame+ * drawn in toward the point for PullFrames, then a ring travelling out to Radius over WaveFrames+ * and pushing the picture outward as it passes. Radius is in world units; the strengths and the+ * ring's width are fractions of it. */+// ------------------------------------------------------------------------------------------------+struct ScreenDistortionInfo+{+ Real m_radius;+ UnsignedInt m_pullFrames;+ UnsignedInt m_waveFrames;+ Real m_pullStrength;+ Real m_waveStrength;+ Real m_waveWidth;+};+// ------------------------------------------------------------------------------------------------/** The implementation of common view functionality. */// ------------------------------------------------------------------------------------------------@@ -238,6 +254,9 @@ public:/// Add an impulse force to shake the cameravirtual void shake( const Coord3D *epicenter, CameraShakeType shakeType ) { };+ /// Bend the picture around a blast; only the Direct3D 11 post chain draws it+ virtual void addScreenDistortion( const Coord3D *epicenter, const ScreenDistortionInfo &info ) { }+virtual Real getFXPitch( void ) const { return 1.0f; } ///< returns the FX pitch anglevirtual void forceCameraConstraintRecalc(void) {}virtual void applyCameraConstraint( void ) {} ///< move the look point inside the camera constraint now, where the next draw woulddiff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/MemoryInit.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/MemoryInit.cppindex a81ffc482..aa2e54aae 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/System/MemoryInit.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/MemoryInit.cpp@@ -543,6 +543,7 @@ static PoolSizeRec sizes[] ={ "RayEffectFXNugget", 32, 32 },{ "LightPulseFXNugget", 68, 32 },{ "ViewShakeFXNugget", 140, 32 },+ { "ScreenDistortionFXNugget", 16, 16 },{ "TerrainScorchFXNugget", 48, 32 },{ "ParticleSystemFXNugget", 832, 32 },{ "FXListAtBonePosFXNugget", 32, 32 },diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/FXList.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/FXList.cppindex 76dc83327..ab9d38181 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/FXList.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/FXList.cpp@@ -440,6 +440,55 @@ private:};EMPTY_DTOR(ViewShakeFXNugget)+//-------------------------------------------------------------------------------------------------+/** The picture drawn in toward the blast and then pushed out by a ring, for a nuke. A client-only+ * picture: it goes to the tactical view, which hands it to the Direct3D 11 post chain, and does+ * nothing on a device without one. */+//-------------------------------------------------------------------------------------------------+class ScreenDistortionFXNugget : public FXNugget+{+ MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE(ScreenDistortionFXNugget, "ScreenDistortionFXNugget")+public:++ ScreenDistortionFXNugget()+ {+ m_info.m_radius = 0.0f;+ m_info.m_pullFrames = 0;+ m_info.m_waveFrames = 0;+ m_info.m_pullStrength = 0.0f;+ m_info.m_waveStrength = 0.0f;+ m_info.m_waveWidth = 0.0f;+ }++ virtual void doFXPos(const Coord3D *primary, const Matrix3D* /*primaryMtx*/, const Real /*primarySpeed*/, const Coord3D * /*secondary*/, const Real /*overrideRadius*/ ) const+ {+ if (TheTacticalView)+ TheTacticalView->addScreenDistortion(primary, m_info);+ }++ static void parse(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)+ {+ static const FieldParse myFieldParse[] =+ {+ { "Radius", INI::parseReal, NULL, offsetof( ScreenDistortionFXNugget, m_info.m_radius ) },+ { "PullTime", INI::parseDurationUnsignedInt, NULL, offsetof( ScreenDistortionFXNugget, m_info.m_pullFrames ) },+ { "WaveTime", INI::parseDurationUnsignedInt, NULL, offsetof( ScreenDistortionFXNugget, m_info.m_waveFrames ) },+ { "PullStrength", INI::parsePercentToReal, NULL, offsetof( ScreenDistortionFXNugget, m_info.m_pullStrength ) },+ { "WaveStrength", INI::parsePercentToReal, NULL, offsetof( ScreenDistortionFXNugget, m_info.m_waveStrength ) },+ { "WaveWidth", INI::parsePercentToReal, NULL, offsetof( ScreenDistortionFXNugget, m_info.m_waveWidth ) },+ { 0, 0, 0, 0 }+ };++ ScreenDistortionFXNugget* nugget = newInstance( ScreenDistortionFXNugget );+ ini->initFromINI(nugget, myFieldParse);+ ((FXList*)instance)->addFXNugget(nugget);+ }++private:+ ScreenDistortionInfo m_info;+};+EMPTY_DTOR(ScreenDistortionFXNugget)+//-------------------------------------------------------------------------------------------------class TerrainScorchFXNugget : public FXNugget{@@ -777,7 +826,8 @@ static const FieldParse TheFXListFieldParse[] ={ "RayEffect", RayEffectFXNugget::parse, 0, 0},{ "Tracer", TracerFXNugget::parse, 0, 0},{ "LightPulse", LightPulseFXNugget::parse, 0, 0},- { "ViewShake", ViewShakeFXNugget::parse, 0, 0},+ { "ViewShake", ViewShakeFXNugget::parse, 0, 0},+ { "ScreenDistortion", ScreenDistortionFXNugget::parse, 0, 0},{ "TerrainScorch", TerrainScorchFXNugget::parse, 0, 0},{ "ParticleSystem", ParticleSystemFXNugget::parse, 0, 0},{ "FXListAtBonePos", FXListAtBonePosFXNugget::parse, 0, 0},diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.hindex a889780dc..b871d1e5d 100644--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.h+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.h@@ -222,7 +222,9 @@ public:/// Add an impulse force to shake the cameravirtual void shake( const Coord3D *epicenter, CameraShakeType shakeType );-++ virtual void addScreenDistortion( const Coord3D *epicenter, const ScreenDistortionInfo &info );+virtual Real getFXPitch( void ) const { return m_FXPitch; } ///< returns the FX pitch anglevirtual Bool setViewFilterMode(enum FilterModes filterMode); ///< Turns on viewport special effect (black & white mode)@@ -273,6 +275,17 @@ private:Real m_shakeIntensity; ///< the intensity of the oscillationVector3 m_shakerAngles; //WST 11/12/2002 new multiple instance camera shaker system+ enum { MAX_SCREEN_DISTORTIONS = 4 }; ///< the post chain's warp limit+ struct ScreenDistortion+ {+ Coord3D m_epicenter;+ UnsignedInt m_startFrame; ///< client frame the blast went off on+ ScreenDistortionInfo m_info;+ };+ ScreenDistortion m_distortions[MAX_SCREEN_DISTORTIONS];+ Int m_distortionCount;+ void updateScreenDistortions( void ); ///< age the blasts and hand the live ones to the post chain in screen terms+TRotateCameraInfo m_rcInfo;Bool m_doingRotateCamera; ///< True if we are doing a camera rotate.diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cppindex 2bee7d5d7..8c3f716cb 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp@@ -104,6 +104,9 @@#include "WW3D2/predlod.h"#include "WW3D2/ww3d.h"#include "WW3D2/dx11runtime.h"+#if defined(_WIN32)+#include "dx11post.h" //DX11Post_Set_Warps; the library is not built off Windows+#endif#include "W3DDevice/GameClient/camerashakesystem.h"@@ -205,6 +208,7 @@ W3DView::W3DView()m_shakerAngles.X =0.0f; // Proper camera shake generator & sourcesm_shakerAngles.Y =0.0f;m_shakerAngles.Z =0.0f;+ m_distortionCount = 0;m_recalcCamera = false;m_isometricApplied = false;@@ -1230,6 +1234,9 @@ void W3DView::reset( void )Coord2D gb = { 0,0 };setGuardBandBias( &gb );++ // a blast from the last game must not come back on the first frames of the next one+ m_distortionCount = 0;}//-------------------------------------------------------------------------------------------------@@ -2598,6 +2605,7 @@ void W3DView::draw( void )Int64 tPostChainStart, tPostChainEnd, tIconStart, tIconEnd;tPostChainStart = Clock_Ticks();#endif+ updateScreenDistortions();Direct3D11_Finish_Scene();#ifdef DEBUG_LOGGINGtPostChainEnd = Clock_Ticks();@@ -4229,6 +4237,139 @@ void W3DView::shake( const Coord3D *epicenter, CameraShakeType shakeType )m_shakeIntensity = TheGlobalData->m_maxShakeIntensity;}+//-------------------------------------------------------------------------------------------------+/** A blast that bends the picture. Only the Direct3D 11 post chain draws it: under Direct3D 9 the+ * warps are handed over all the same and no pass reads them, and off Windows the list is dropped+ * at the next draw. A fifth blast on a full list pushes the oldest off. */+//-------------------------------------------------------------------------------------------------+void W3DView::addScreenDistortion( const Coord3D *epicenter, const ScreenDistortionInfo &info )+{+ // A Scud Storm missile plays its detonation FX twice, once from the missile and once from the+ // damage weapon it fires, and the Chemical general's adds a Scud launcher's on top. One blast+ // in one place on one frame bends the picture once: the first to arrive is kept.+ const UnsignedInt now = TheGameClient->getFrame();+ for (Int i = 0; i < m_distortionCount; ++i)+ {+ const ScreenDistortion &other = m_distortions[i];+ const Real dx = other.m_epicenter.x - epicenter->x;+ const Real dy = other.m_epicenter.y - epicenter->y;+ const Real sameSpot = 0.25f * other.m_info.m_radius;+ if (now - other.m_startFrame <= 1 && dx * dx + dy * dy < sameSpot * sameSpot)+ return;+ }++ if (m_distortionCount == MAX_SCREEN_DISTORTIONS)+ {+ for (Int i = 1; i < MAX_SCREEN_DISTORTIONS; ++i)+ m_distortions[i - 1] = m_distortions[i];+ --m_distortionCount;+ }+ ScreenDistortion &added = m_distortions[m_distortionCount++];+ added.m_epicenter = *epicenter;+ added.m_startFrame = now;+ added.m_info = info;+}++static const Real MAX_DISTORTION_SCREEN_RADIUS = 0.35f; ///< fraction of the screen's height++/** How far past the radius the ring runs. Stopping at the radius it spent most of its strength+ * under the fireball, where nothing behind it shows the bend; carried half as far again it rolls+ * out over open ground while it still has the strength to be seen. */+static const Real RING_TRAVEL = 1.5f;++static Real distortionEase( Real x )+{+ return x * x * (3.0f - 2.0f * x);+}++//-------------------------------------------------------------------------------------------------+/** Ages the blasts on client frames, so a paused game holds the ring where it is, and blends into+ * the next frame the way smooth motion blends the units, so at 120 fps the ring grows every frame+ * rather than in thirtieths of a second. The pull rises over the first 70% of its time and lets+ * go over the rest, then the ring runs out past the radius, decelerating and widening, its+ * strength falling off in a straight line so it is still there when it reaches clear ground.+ * The world radius becomes a screen one by projecting the blast and a point a radius away along+ * the camera's right vector, which is square to the line of sight and so gives the same size+ * whichever way the camera is turned. */+//-------------------------------------------------------------------------------------------------+void W3DView::updateScreenDistortions( void )+{+#if defined(_WIN32)+ DX11PostWarp warps[MAX_SCREEN_DISTORTIONS];+ Int warpCount = 0;+ const Real displayWidth = (Real)TheDisplay->getWidth();+ const Real displayHeight = (Real)TheDisplay->getHeight();+ const UnsignedInt frame = TheGameClient->getFrame();++ Int kept = 0;+ for (Int i = 0; i < m_distortionCount; ++i)+ {+ const ScreenDistortion &blast = m_distortions[i];+ const ScreenDistortionInfo &info = blast.m_info;+ const Real pullFrames = (Real)info.m_pullFrames;+ const Real waveFrames = (Real)info.m_waveFrames;+ Real age = (Real)(frame - blast.m_startFrame) + TheSmoothMotionAlpha - 1.0f;+ if (age < 0.0f)+ age = 0.0f;+ if (age >= pullFrames + waveFrames)+ continue; // spent, or from a game before this one+ m_distortions[kept++] = blast;++ Real pull = 0.0f;+ Real ringReach = 0.0f;+ Real ringWidth = info.m_waveWidth;+ Real ringStrength = 0.0f;+ if (age < pullFrames)+ {+ const Real s = age / pullFrames;+ pull = info.m_pullStrength * (s < 0.7f ? distortionEase(s / 0.7f) : 1.0f - distortionEase((s - 0.7f) / 0.3f));+ }+ else+ {+ const Real u = (age - pullFrames) / waveFrames;+ const Real left = 1.0f - u;+ ringReach = RING_TRAVEL * (1.0f - left * left);+ ringWidth = info.m_waveWidth * (0.5f + u);+ ringStrength = info.m_waveStrength * left * (u < 0.06f ? u / 0.06f : 1.0f);+ }++ ICoord2D centre;+ if (worldToScreenTriReturn(&blast.m_epicenter, ¢re) == WTS_INVALID)+ continue;+ const Vector3 right = m_3DCamera->Get_Transform().Get_X_Vector();+ Coord3D edge = blast.m_epicenter;+ edge.x += right.X * info.m_radius;+ edge.y += right.Y * info.m_radius;+ edge.z += right.Z * info.m_radius;+ ICoord2D edgeScreen;+ if (worldToScreenTriReturn(&edge, &edgeScreen) == WTS_INVALID)+ continue;+ const Real dx = (Real)(edgeScreen.x - centre.x);+ const Real dy = (Real)(edgeScreen.y - centre.y);+ const Real screenRadius = sqrtf(dx * dx + dy * dy);+ if (screenRadius < 1.0f)+ continue;++ DX11PostWarp &warp = warps[warpCount++];+ warp.CentreX = (Real)centre.x / displayWidth;+ warp.CentreY = (Real)centre.y / displayHeight;+ // Zoomed in, a nuke's radius is wider than the screen and the pull moved the whole picture;+ // held to about a third of the height it stays a blast in the frame rather than the frame.+ warp.Radius = screenRadius / displayHeight;+ if (warp.Radius > MAX_DISTORTION_SCREEN_RADIUS)+ warp.Radius = MAX_DISTORTION_SCREEN_RADIUS;+ warp.Pull = pull;+ warp.RingRadius = warp.Radius * ringReach;+ warp.RingWidth = warp.Radius * ringWidth;+ warp.RingStrength = ringStrength;+ }+ m_distortionCount = kept;+ DX11Post_Set_Warps(warps, (unsigned)warpCount);+#else+ m_distortionCount = 0;+#endif+}+//-------------------------------------------------------------------------------------------------/** Transformt he screen pixel coord passed in, to a world coordinate at the specified* z value */diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.cppindex 4aa436e37..aed91aec6 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.cpp@@ -86,6 +86,17 @@ void DX11Post_Set_Bloom(float threshold, float intensity)BloomIntensity = (intensity > 0.0f) ? intensity : 0.0f;}+static DX11PostWarp Warps[DX11_POST_WARP_LIMIT];+static unsigned WarpCount = 0;++void DX11Post_Set_Warps(const DX11PostWarp * warps, unsigned count)+{+ WarpCount = (count > DX11_POST_WARP_LIMIT) ? DX11_POST_WARP_LIMIT : count;+ for (unsigned index = 0; index < WarpCount; ++index) {+ Warps[index] = warps[index];+ }+}+// How dark a fully occluded pixel goes, how far a neighbour may be in front before it counts as a// different object rather than a corner, and how wide the ring reaches at one unit of depth. The// clip planes come from the frame itself.@@ -104,6 +115,12 @@ struct PostConstantBlock// Threshold, intensity, knee.float Tuning[4];++ // How many warps, and the width of the frame over its height. Then per warp the centre, the+ // radius and the pull, and the ring's radius, width and strength.+ float WarpInfo[4];+ float WarpCentre[DX11_POST_WARP_LIMIT][4];+ float WarpRing[DX11_POST_WARP_LIMIT][4];};// A triangle big enough to cover the screen, made out of nothing. No vertex buffer and no input@@ -141,6 +158,9 @@ static const char * const PIXEL_SHADER_PROLOGUE =" float4 TexelSize;\n"" float4 BlurDirection;\n"" float4 Tuning;\n"+ " float4 WarpInfo;\n"+ " float4 WarpCentre[4];\n"+ " float4 WarpRing[4];\n""};\n""\n"// Below the knee nothing moves, so a frame with nothing overbright in it comes back out as the@@ -354,6 +374,45 @@ static const char * const BLOOM_COMPOSITE_SHADER_BODY =" return float4(tone_curve(scene + glow * Tuning.y), 1.0);\n""}\n";+// A blast bending the picture, one read per pixel whatever the number of warps: every warp adds+// to where the pixel reads from, and the frame is sampled once at the sum.+//+// The pull reads from further out than the pixel is, so the picture slides toward the centre. Its+// shape is nothing at the centre, where the direction is undefined, and nothing at the radius, so+// there is no seam; in between it peaks a third of the way out at the strength asked for. The ring+// reads from nearer the centre, so what was inside it is carried outward on its crest, and across+// its width it falls off as a gaussian, which keeps the band soft rather than a cut line.+//+// The whole shift dies away over the last 8% of the frame toward every edge. A read past the edge+// comes back as the edge's own texel, and on film that drew the border out into streaks thirty to+// forty pixels long; held to zero at the edge, a pixel near it never reads further out than it is+// from the edge, as long as no warp moves the picture by more than 8% of the frame.+static const char * const WARP_SHADER_BODY =+ "float4 main(VertexOutput input) : SV_TARGET\n"+ "{\n"+ " float aspect = WarpInfo.y;\n"+ " float2 shift = float2(0.0, 0.0);\n"+ " for (int index = 0; index < 4; ++index)\n"+ " {\n"+ " if (index >= (int)WarpInfo.x) break;\n"+ " float4 centre = WarpCentre[index];\n"+ " float4 ring = WarpRing[index];\n"+ " float2 away = (input.Texture - centre.xy) * float2(aspect, 1.0);\n"+ " float span = length(away);\n"+ " float2 outward = away / max(span, 0.0001);\n"+ " float reach = saturate(span / max(centre.z, 0.0001));\n"+ " float inward = centre.w * centre.z * 6.75 * reach * (1.0 - reach) * (1.0 - reach);\n"+ " float across = (span - ring.x) / max(ring.y, 0.0001);\n"+ " float push = ring.z * centre.z * exp(-across * across);\n"+ " shift += outward * (inward - push);\n"+ " }\n"+ " shift.x /= aspect;\n"+ " float2 border = min(input.Texture, 1.0 - input.Texture);\n"+ " float2 settle = saturate(border / 0.08);\n"+ " shift *= settle.x * settle.y;\n"+ " return float4(Source.Sample(Sampler, input.Texture + shift).rgb, 1.0);\n"+ "}\n";+static D3DCompileFunction compiler_function(){static D3DCompileFunction compiler = NULL;@@ -472,6 +531,7 @@ DX11PostProcessClass::DX11PostProcessClass(), FarPlane(1200.0f), BloomBlurShader(NULL), BloomCompositeShader(NULL)+ , WarpShader(NULL), Sampler(NULL), BlendState(NULL), DepthState(NULL)@@ -527,6 +587,7 @@ void DX11PostProcessClass::Shutdown()release_interface(reinterpret_cast<IUnknown **>(&DepthState));release_interface(reinterpret_cast<IUnknown **>(&BlendState));release_interface(reinterpret_cast<IUnknown **>(&Sampler));+ release_interface(reinterpret_cast<IUnknown **>(&WarpShader));release_interface(reinterpret_cast<IUnknown **>(&BloomCompositeShader));release_interface(reinterpret_cast<IUnknown **>(&BloomBlurShader));release_interface(reinterpret_cast<IUnknown **>(&BloomExtractShader));@@ -608,7 +669,8 @@ bool DX11PostProcessClass::Create_Shaders()&& compile_pixel_shader(compiler, device, BLOOM_EXTRACT_SHADER_BODY, &BloomExtractShader)&& compile_pixel_shader(compiler, device, BLOOM_BLUR_SHADER_BODY, &BloomBlurShader)&& compile_pixel_shader(compiler, device, BLOOM_COMPOSITE_SHADER_BODY,- &BloomCompositeShader);+ &BloomCompositeShader)+ && compile_pixel_shader(compiler, device, WARP_SHADER_BODY, &WarpShader);}bool DX11PostProcessClass::Create_States()@@ -815,6 +877,21 @@ void DX11PostProcessClass::Draw_Pass(const PassSetup & pass)block.Tuning[2] = AO_RADIUS;block.Tuning[3] = AO_SPREAD;}+ block.WarpInfo[0] = static_cast<float>(WarpCount);+ block.WarpInfo[1] = static_cast<float>(pass.SourceWidth) / static_cast<float>(pass.SourceHeight);+ block.WarpInfo[2] = 0.0f;+ block.WarpInfo[3] = 0.0f;+ for (unsigned index = 0; index < DX11_POST_WARP_LIMIT; ++index) {+ const DX11PostWarp & warp = Warps[index];+ block.WarpCentre[index][0] = warp.CentreX;+ block.WarpCentre[index][1] = warp.CentreY;+ block.WarpCentre[index][2] = warp.Radius;+ block.WarpCentre[index][3] = warp.Pull;+ block.WarpRing[index][0] = warp.RingRadius;+ block.WarpRing[index][1] = warp.RingWidth;+ block.WarpRing[index][2] = warp.RingStrength;+ block.WarpRing[index][3] = 0.0f;+ }memcpy(mapped.pData, &block, sizeof(block));context->Unmap(ConstantBuffer, 0);@@ -915,12 +992,16 @@ bool DX11PostProcessClass::Finish(const DX11PostEffect * effects, unsigned countreturn false;}+ // A blast on screen puts one more pass on the end of the chain, so whatever was last writes a+ // chain target instead of the screen and the warp writes the screen from it.+ const bool warp = (WarpCount > 0);+unsigned first = 0;ID3D11ShaderResourceView * source = SceneTarget.Resource;if (effects[0] == DX11_POST_BLOOM) {// Bloom is the only effect that reads the half float scene, and the frame is eight bits by// the time it hands over. If it is also the last thing in the chain it writes the screen.- const bool alone = (count == 1);+ const bool alone = (count == 1) && !warp;ID3D11RenderTargetView * bloom_destination = alone ? back_buffer : ChainTargets[0].View;if (BloomIntensity > 0.0f) {source = Run_Bloom(bloom_destination);@@ -957,7 +1038,7 @@ bool DX11PostProcessClass::Finish(const DX11PostEffect * effects, unsigned countpass.SourceWidth = Width;pass.SourceHeight = Height;for (unsigned index = first; index < count; ++index) {- const bool last = (index + 1 == count);+ const bool last = (index + 1 == count) && !warp;pass.Shader = PixelShaders[effects[index]];// The occlusion pass is the one effect that reads something other than the frame: the// frame's own depth, which the device keeps as a texture as well as a depth buffer.@@ -970,6 +1051,15 @@ bool DX11PostProcessClass::Finish(const DX11PostEffect * effects, unsigned countdestination = 1 - destination;}+ if (warp) {+ pass.Shader = WarpShader;+ pass.Extra = NULL;+ pass.Occlusion = false;+ pass.Source = source;+ pass.Destination = back_buffer;+ Draw_Pass(pass);+ }+Resolved = true;return true;}diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.hindex 2ba1c2619..1babf6899 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.h@@ -108,6 +108,34 @@ const char * DX11Post_Effect_Name(DX11PostEffect effect);// runs at the values it was tuned at, which is what the tests measure.void DX11Post_Set_Bloom(float threshold, float intensity);+// A blast warping the picture around it: the frame drawn toward a point and then pushed out by a+// ring that travels away from it. Not an effect in the chain and not a switch: whatever chain is+// set, one more pass goes on its end for every frame that has a warp in it, and a frame with none+// costs a branch. With no chain at all there is no scene texture to bend and nothing happens.+//+// Everything is in screen terms, because the game has already done the projecting. The centre is+// in texture coordinates, nought to one across the frame and down it, and may lie off the screen.+// The radii are fractions of the frame's height, so a circle stays round on a wide screen. Pull+// is how far, as a fraction of Radius, the picture is drawn in at its strongest; RingStrength is how+// far the ring pushes it out at its crest, a fraction of Radius as well.+struct DX11PostWarp+{+ float CentreX;+ float CentreY;+ float Radius;+ float Pull;+ float RingRadius;+ float RingWidth;+ float RingStrength;+};++// Four blasts on one screen at once is already more than anyone can read.+const unsigned DX11_POST_WARP_LIMIT = 4;++// The game sets the frame's warps every frame, and a count of zero clears them. A count past the+// limit keeps the first four.+void DX11Post_Set_Warps(const DX11PostWarp * warps, unsigned count);+class DX11PostProcessClass{public:@@ -197,6 +225,7 @@ private:ID3D11PixelShader * BloomExtractShader;ID3D11PixelShader * BloomBlurShader;ID3D11PixelShader * BloomCompositeShader;+ ID3D11PixelShader * WarpShader;ID3D11SamplerState * Sampler;ID3D11BlendState * BlendState;ID3D11DepthStencilState * DepthState;diff --git a/GeneralsMD/Code/Tests/test_dx11post.cpp b/GeneralsMD/Code/Tests/test_dx11post.cppindex ae5551443..2c9189982 100644--- a/GeneralsMD/Code/Tests/test_dx11post.cpp+++ b/GeneralsMD/Code/Tests/test_dx11post.cpp@@ -325,6 +325,53 @@ TEST(dx11post_copy_gives_every_pixel_back_unchanged)delete [] picture;}+TEST(dx11post_a_warp_bends_the_edge_its_ring_crosses_and_clearing_it_gives_the_frame_back)+{+ DeviceFixture fixture;+ if (!fixture.Create()) {+ printf("skip: no Direct3D 11 device on this machine\n");+ return;+ }++ unsigned char * picture = new unsigned char[SURFACE_SIZE * SURFACE_SIZE * BYTES_A_PIXEL];+ fill_diagonal(picture);++ // The blast sits in the dark half and its ring touches the diagonal square on, where pushing+ // the picture outward moves the edge rather than sliding it along itself.+ DX11PostWarp warp;+ warp.CentreX = 0.3f;+ warp.CentreY = 0.7f;+ warp.Radius = 0.4f;+ warp.Pull = 0.0f;+ warp.RingRadius = 0.28f;+ warp.RingWidth = 0.05f;+ warp.RingStrength = 0.1f;+ DX11Post_Set_Warps(&warp, 1);++ const DX11PostEffect chain[] = { DX11_POST_COPY };+ unsigned pitch = 0;+ unsigned char * result = run_chain(fixture, chain, 1, picture, pitch);+ CHECK(result != NULL);+ if (result != NULL) {+ CHECK(differing_pixels(picture, result, pitch) > 8u);+ // Far from the ring nothing moves.+ CHECK_EQ(DARK_LEVEL, pixel_level(result, pitch, 1, SURFACE_SIZE - 2));+ CHECK_EQ(LIGHT_LEVEL, pixel_level(result, pitch, SURFACE_SIZE - 2, 1));+ delete [] result;+ }++ // The game clears the warps on the frame after the last blast ends, and that frame has to be+ // the copy pass's exact one again, not a frame with a last trace of the ring in it.+ DX11Post_Set_Warps(NULL, 0);+ result = run_chain(fixture, chain, 1, picture, pitch);+ CHECK(result != NULL);+ if (result != NULL) {+ CHECK_EQ(0u, differing_pixels(picture, result, pitch));+ delete [] result;+ }+ delete [] picture;+}+TEST(dx11post_fxaa_leaves_a_flat_picture_alone){DeviceFixture fixture;