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(ww3d2): heat haze over flames behind -heathaze

Olcay Seyganaabde190e348
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
index ec32a75fc..ac359efb4 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
@@ -552,6 +552,7 @@ public:
Bool m_waterFoam; ///< "WaterFoam": foam, shore band and the sun's shadow on water (Direct3D 11, client only)
Bool m_groundMarks; ///< "GroundMarks": craters, stains and longer treads that fade over minutes (Direct3D 11, client only)
Bool m_firelight; ///< "Firelight": flames light the ground, walls and units around them (Direct3D 11, client only)
+ Bool m_heatHaze; ///< "-heathaze": the air over FireLook flames shimmers (Direct3D 11, client only, prototype)
Bool m_hemiAmbient; ///< "HemiAmbient": models take the ambient from the sky above and the ground below (Direct3D 11, client only)
Bool m_ambientOcclusion; ///< "AmbientOcclusion": corners and the ground under what stands on it darken, half size in world units (Direct3D 11, client only)
Bool m_weatherEffects; ///< "Weather": rain, blowing sand or snow by the map's ground, and a wind on trees and smoke (Direct3D 11, client only)
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
index bb588da30..6a627773a 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
@@ -1101,6 +1101,14 @@ Int parseSmoothMotion(char *args[], int)
return 1;
}
+/* -heathaze: the air over the FireLook flames shimmers on Direct3D 11, a prototype. Client only. */
+Int parseHeatHaze(char *args[], int)
+{
+ if (TheWritableGlobalData)
+ TheWritableGlobalData->m_heatHaze = TRUE;
+ return 1;
+}
+
/* -nosmokefirelight: smoke left unlit by the fire beside it, for one run. */
Int parseNoSmokeFireLight(char *args[], int)
{
@@ -2911,6 +2919,7 @@ static CommandLineParam params[] =
{ "-nosmoothmotion", parseNoSmoothMotion },
{ "-smoothmotion", parseSmoothMotion },
{ "-nosmokefirelight", parseNoSmokeFireLight },
+ { "-heathaze", parseHeatHaze },
{ "-lighting", parseLightingStyle },
{ "-quickstart", parseQuickStart },
/* In every build: EA kept them to Debug and Internal, so a Release run could not skip the logo or the
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
index 279dfe68e..9c834d06c 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
@@ -800,6 +800,7 @@ GlobalData::GlobalData()
m_waterFoam = FALSE;
m_groundMarks = FALSE;
m_firelight = FALSE;
+ m_heatHaze = FALSE;
m_hemiAmbient = FALSE;
m_ambientOcclusion = FALSE;
m_weatherEffects = FALSE;
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DParticleSys.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DParticleSys.cpp
index 34058ac76..2a6c95a77 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DParticleSys.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DParticleSys.cpp
@@ -345,6 +345,87 @@ static void gatherFirelights( const AABoxClass &view, UnsignedInt frame, const s
#endif
}
+/** -heathaze, a prototype: each flame system in view (m_frameFires, so no new INI field) as one heat
+ * splat for the Direct3D 11 post pass (Direct3D11_Set_Heat_Splats has the layout). The particles
+ * are summed as the Firelight sums them (sumFlames: brightness times size), and the splat is a
+ * column over their weighted centre, as wide as their spread and capped in world units, so a big
+ * explosion or a plume of grown particles cannot bend half the screen. Its shift scales with its
+ * height on the screen and with how much flame the system holds. Only ground well behind the
+ * flame bends (HEAT_HAZE_CLEARANCE), so a unit standing in the fire keeps its outline. The waves
+ * themselves are in dx11post.cpp (HEAT_FREQUENCY). */
+static const Real HEAT_HAZE_MIN_RADIUS = 6.0f; ///< world units: a single flame's column half width
+static const Real HEAT_HAZE_MAX_RADIUS = 22.0f; ///< and the widest any system's gets
+static const Real HEAT_HAZE_LIFT = 1.3f; ///< the column's centre above the flames' centre, in its half widths
+static const Real HEAT_HAZE_STRETCH = 2.2f; ///< its half height over its half width
+static const Real HEAT_HAZE_AMPLITUDE = 0.18f; ///< the strongest shift, a share of the splat's half height on the screen
+static const Real HEAT_HAZE_MAX_SHIFT = 0.00525f; ///< and never more than this share of the frame; at normal zoom a column reaches it
+static const Real HEAT_HAZE_FULL = 40.0f; ///< summed brightness times size at which a system shimmers at full strength
+static const Real HEAT_HAZE_CLEARANCE = 12.0f; ///< world units behind the flames before a surface bends at all
+static const Real HEAT_HAZE_SOFTNESS = 20.0f; ///< and the further distance over which it comes up to full
+
+static std::vector<float> s_heatSplats; ///< kept so the gather does not allocate
+
+static void gatherHeatHaze( CameraClass &camera, const std::vector<ParticleSystem *> &systems, const std::vector<Real> &fades )
+{
+ s_heatSplats.clear();
+ Matrix4x4 projection;
+ camera.Get_D3D_Projection_Matrix( &projection );
+ Matrix3D view;
+ camera.Get_View_Matrix( &view );
+ float zNear, zFar;
+ camera.Get_Clip_Planes( zNear, zFar );
+ // the camera's viewport in the frame, as CameraClass::Apply sets the device's
+ const ViewportClass &viewport = camera.Get_Viewport();
+ const Real viewWidth = viewport.Max.X - viewport.Min.X;
+ const Real viewHeight = viewport.Max.Y - viewport.Min.Y;
+ const float seconds = (float)( WW3D::Get_Sync_Time() % 3600000u ) * 0.001f;
+ Int count = 0;
+ for (size_t i = 0; i < systems.size() && count < (Int)DIRECT3D11_HEAT_SPLAT_LIMIT; ++i)
+ {
+ FireLights::Flames flames;
+ sumFlames( systems[ i ], flames );
+ if (flames.w <= 0.0f)
+ continue;
+ Real strength = flames.w * fades[ i ] / HEAT_HAZE_FULL;
+ if (strength > 1.0f)
+ strength = 1.0f;
+ const Vector3 centre( flames.x / flames.w, flames.y / flames.w, flames.z / flames.w );
+ const Real spread = flames.sq / flames.w - centre.Length2();
+ Real radius = spread > 0.0f ? WWMath::Sqrt( spread ) : 0.0f;
+ radius = radius < HEAT_HAZE_MIN_RADIUS ? HEAT_HAZE_MIN_RADIUS : radius > HEAT_HAZE_MAX_RADIUS ? HEAT_HAZE_MAX_RADIUS : radius;
+
+ // the flames' own depth decides what stands in front; the column is drawn above them
+ Vector3 eye, columnEye;
+ Matrix3D::Transform_Vector( view, centre, &eye );
+ Matrix3D::Transform_Vector( view, Vector3( centre.X, centre.Y, centre.Z + radius * HEAT_HAZE_LIFT ), &columnEye );
+ if (-eye.Z <= zNear || -columnEye.Z <= zNear)
+ continue;
+ const Vector4 flameClip = projection * eye;
+ const Vector4 clip = projection * columnEye;
+ if (clip.W <= 0.0f || flameClip.W <= 0.0f)
+ continue;
+ const Real x = clip.X / clip.W;
+ const Real y = clip.Y / clip.W;
+ const Real rx = radius * projection[ 0 ][ 0 ] / clip.W;
+ const Real ry = radius * HEAT_HAZE_STRETCH * projection[ 1 ][ 1 ] / clip.W;
+ if (WWMath::Fabs( x ) - rx > 1.0f || WWMath::Fabs( y ) - ry > 1.0f)
+ continue;
+ Real shift = HEAT_HAZE_AMPLITUDE * ry * 0.5f * viewHeight;
+ if (shift > HEAT_HAZE_MAX_SHIFT)
+ shift = HEAT_HAZE_MAX_SHIFT;
+ const float splat[ DIRECT3D11_HEAT_SPLAT_FLOATS ] = {
+ // the camera's clip space into the frame's
+ (viewport.Min.X + ( x * 0.5f + 0.5f ) * viewWidth) * 2.0f - 1.0f,
+ 1.0f - (viewport.Min.Y + ( 0.5f - y * 0.5f ) * viewHeight) * 2.0f,
+ rx * viewWidth, ry * viewHeight,
+ flameClip.Z / flameClip.W, shift * strength, seconds, 0.0f,
+ zNear, zFar, HEAT_HAZE_CLEARANCE, HEAT_HAZE_SOFTNESS };
+ s_heatSplats.insert( s_heatSplats.end(), splat, splat + DIRECT3D11_HEAT_SPLAT_FLOATS );
+ ++count;
+ }
+ Direct3D11_Set_Heat_Splats( s_heatSplats.empty() ? NULL : &s_heatSplats[ 0 ], (unsigned)count );
+}
+
/** The same for a GPU system, from the spawns it keeps (ParticleSystem::getGpuSample) aged in closed
* form to the last particle update, as a CPU particle stands then.
* ponytail: GPU_SPAWN_SAMPLES spawns stand in for the whole fire, so a big one weighs as a small
@@ -2217,6 +2298,8 @@ void W3DParticleSystemManager::doParticles(RenderInfoClass &rinfo)
s_firelightCount = 0;
if (TheGlobalData->m_firelight && Direct3D11_Lights_Per_Pixel())
gatherFirelights( bbox, m_lastLogicFrameUpdate, m_frameFires, m_frameFireFades );
+ if (TheGlobalData->m_heatHaze && !TheGlobalData->m_classicGraphics && Direct3D11_Present_Is_Enabled())
+ gatherHeatHaze( rinfo.Camera, m_frameFires, m_frameFireFades );
}
// Only a CPU drawn smoke system takes a light (pickSmokeLights); with none, the walk over every
// fire, a GPU one's samples aged in closed form, is a third of a frame thrown away.
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.cpp
index 4d5d61824..09c4db85d 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.cpp
@@ -20,6 +20,7 @@
#include "dx11device.h"
#include "dx11resource.h"
+#include "dx11runtime.h"
#include <stdio.h>
#include <string.h>
@@ -1002,6 +1003,111 @@ static const char * const WARP_SHADER_BODY =
" return float4(Source.Sample(Sampler, input.Texture + shift).rgb, 1.0);\n"
"}\n";
+// -heathaze. Every flame particle is a splat, six vertices made out of SV_VertexID like the full
+// screen triangle, its numbers read out of a buffer three float4 a splat (dx11runtime.h has the
+// layout). The splat writes a shift in texture coordinates into rg and its weight into b, added up
+// over every splat that covers the pixel; the composite divides by the weight once it passes one,
+// so a stack of flames shimmers as hard as one does and only the edges fade.
+//
+// The shift is one field over the whole screen rather than per splat, so overlapping flames bend
+// the same way and add up instead of cancelling. Its waves run up the screen with the time: heat
+// rises. The tuning is here: HEAT_FREQUENCY is the waves across the frame's height, HEAT_RISE how
+// fast they climb, HEAT_SOFTNESS_* how a surface nearer than the flame takes the shimmer off.
+static const char * const HEAT_SPLAT_VERTEX_SOURCE =
+ "Buffer<float4> Splats : register(t0);\n"
+ "\n"
+ "struct SplatOutput\n"
+ "{\n"
+ " float4 Position : SV_POSITION;\n"
+ " float2 Corner : TEXCOORD0;\n"
+ " float2 Screen : TEXCOORD1;\n"
+ " float4 Heat : TEXCOORD2;\n"
+ " float4 Planes : TEXCOORD3;\n"
+ "};\n"
+ "\n"
+ "static const float2 CORNERS[6] =\n"
+ "{\n"
+ " float2(-1.0, -1.0), float2(-1.0, 1.0), float2(1.0, -1.0),\n"
+ " float2(1.0, -1.0), float2(-1.0, 1.0), float2(1.0, 1.0)\n"
+ "};\n"
+ "\n"
+ "SplatOutput main(uint index : SV_VertexID)\n"
+ "{\n"
+ " uint splat = index / 6;\n"
+ " float2 corner = CORNERS[index % 6];\n"
+ " float4 shape = Splats.Load(splat * 3);\n"
+ " SplatOutput output;\n"
+ " float2 clip = shape.xy + corner * shape.zw;\n"
+ " output.Position = float4(clip, 0.0, 1.0);\n"
+ " output.Corner = corner;\n"
+ " output.Screen = clip * float2(0.5, -0.5) + 0.5;\n"
+ " output.Heat = Splats.Load(splat * 3 + 1);\n"
+ " output.Planes = Splats.Load(splat * 3 + 2);\n"
+ " return output;\n"
+ "}\n";
+
+// Extra (t1) is the scene's depth here, read texel by texel at full size under the half size pixel.
+static const char * const HEAT_SPLAT_SHADER_BODY =
+ "struct SplatOutput\n"
+ "{\n"
+ " float4 Position : SV_POSITION;\n"
+ " float2 Corner : TEXCOORD0;\n"
+ " float2 Screen : TEXCOORD1;\n"
+ " float4 Heat : TEXCOORD2;\n"
+ " float4 Planes : TEXCOORD3;\n"
+ "};\n"
+ "\n"
+ "static const float HEAT_FREQUENCY = 50.0;\n"
+ "static const float HEAT_RISE = 2.0;\n"
+ "\n"
+ "float linear_depth(float depth, float4 planes)\n"
+ "{\n"
+ " return planes.x * planes.y / max(planes.y - depth * (planes.y - planes.x), 0.0001);\n"
+ "}\n"
+ "\n"
+ "float4 main(SplatOutput input) : SV_TARGET\n"
+ "{\n"
+ " float reach = dot(input.Corner, input.Corner);\n"
+ " if (reach >= 1.0) discard;\n"
+ " float weight = (1.0 - reach) * (1.0 - reach) * (1.0 - reach);\n"
+ " uint width, height;\n"
+ " Extra.GetDimensions(width, height);\n"
+ " int2 texel = min(int2(input.Position.xy * 2.0), int2(width - 1, height - 1));\n"
+ " float scene = linear_depth(Extra.Load(int3(texel, 0)).r, input.Planes);\n"
+ " float flame = linear_depth(input.Heat.x, input.Planes);\n"
+ // Only what lies well behind the flames bends: a unit standing in the fire, or in front of
+ // it, keeps its outline.
+ " weight *= saturate((scene - flame - input.Planes.z) / max(input.Planes.w, 0.0001));\n"
+ " if (weight <= 0.0) discard;\n"
+ " float aspect = (float)width / (float)height;\n"
+ " float2 p = float2(input.Screen.x * aspect, input.Screen.y) * HEAT_FREQUENCY;\n"
+ " float t = input.Heat.z * HEAT_RISE;\n"
+ // Bands across the column climbing it, each swaying sideways a little along its length, so the
+ // air above a fire wavers left and right as it rises.
+ " float rise = p.y + t * 3.0 + 0.8 * sin(p.x * 0.35 + t * 0.7);\n"
+ " float2 shift;\n"
+ " shift.x = sin(rise) + 0.3 * sin(rise * 1.9 + p.x * 0.5 - t * 1.3);\n"
+ " shift.y = 0.25 * sin(rise * 0.6 + p.x * 0.4 - t * 0.9);\n"
+ " return float4(shift * input.Heat.y * weight, weight, 0.0);\n"
+ "}\n";
+
+// Source is the copy of the scene, Extra the summed shifts at half size. A pixel no splat touched
+// reads its own texel, so the frame outside the shimmer is the frame that came in. The shift dies
+// away over the last 2% toward every edge, the warp's rule, so nothing reads past the frame.
+static const char * const HEAT_HAZE_SHADER_BODY =
+ "float4 main(VertexOutput input) : SV_TARGET\n"
+ "{\n"
+ " float4 heat = Extra.Sample(Sampler, input.Texture);\n"
+ " float2 shift = heat.xy / max(heat.z, 1.0);\n"
+ // Never read from a pixel the splats left alone, which is where a unit in the fire stands, so
+ // its colour is not pulled out over the ground beside it.
+ " shift *= saturate(Extra.Sample(Sampler, input.Texture + shift).z * 4.0);\n"
+ " float2 border = min(input.Texture, 1.0 - input.Texture);\n"
+ " float2 settle = saturate(border / 0.02);\n"
+ " shift *= settle.x * settle.y;\n"
+ " return Source.Sample(Sampler, input.Texture + shift);\n"
+ "}\n";
+
static D3DCompileFunction compiler_function()
{
static D3DCompileFunction compiler = NULL;
@@ -1149,7 +1255,15 @@ DX11PostProcessClass::DX11PostProcessClass()
, HazeShroudView(NULL)
, AtmosphereRefused(false)
, WorldAoRefused(false)
+ , HeatSplatBuffer(NULL)
+ , HeatSplatView(NULL)
+ , HeatSplatVertexShader(NULL)
+ , HeatSplatShader(NULL)
+ , HeatHazeShader(NULL)
+ , HeatBlendState(NULL)
+ , HeatHazeRefused(false)
{
+ memset(&HeatTarget, 0, sizeof(HeatTarget));
memset(HazeWorldFromClip, 0, sizeof(HazeWorldFromClip));
memset(WorldAoShaders, 0, sizeof(WorldAoShaders));
memset(AtmosphereShaders, 0, sizeof(AtmosphereShaders));
@@ -1193,6 +1307,9 @@ bool DX11PostProcessClass::Initialise(DX11DeviceClass * device)
void DX11PostProcessClass::Shutdown()
{
Release_Targets();
+ Release_Heat_Haze();
+ HeatSplats.clear();
+ HeatHazeRefused = false;
HazeShroudView = NULL;
HazeShroudSource = NULL;
AtmosphereSky = NULL;
@@ -1465,6 +1582,7 @@ void DX11PostProcessClass::Release_Targets()
Release_Target(SceneCopy);
Release_Target(WorldAoTargets[0]);
Release_Target(WorldAoTargets[1]);
+ Release_Target(HeatTarget);
const unsigned chain_target_count = sizeof(ChainTargets) / sizeof(ChainTargets[0]);
for (unsigned index = 0; index < chain_target_count; ++index) {
Release_Target(ChainTargets[index]);
@@ -1825,6 +1943,192 @@ bool DX11PostProcessClass::Occlude_Scene(ID3D11ShaderResourceView * depth)
return true;
}
+void DX11PostProcessClass::Set_Heat_Splats(const float * splats, unsigned count)
+{
+ if (count > DIRECT3D11_HEAT_SPLAT_LIMIT) {
+ count = DIRECT3D11_HEAT_SPLAT_LIMIT;
+ }
+ HeatSplats.assign(splats, splats + count * DIRECT3D11_HEAT_SPLAT_FLOATS);
+}
+
+void DX11PostProcessClass::Release_Heat_Haze()
+{
+ release_interface(reinterpret_cast<IUnknown **>(&HeatBlendState));
+ release_interface(reinterpret_cast<IUnknown **>(&HeatHazeShader));
+ release_interface(reinterpret_cast<IUnknown **>(&HeatSplatShader));
+ release_interface(reinterpret_cast<IUnknown **>(&HeatSplatVertexShader));
+ release_interface(reinterpret_cast<IUnknown **>(&HeatSplatView));
+ release_interface(reinterpret_cast<IUnknown **>(&HeatSplatBuffer));
+}
+
+bool DX11PostProcessClass::Wants_Heat_Haze_Programs() const
+{
+ return ShadersReady && HeatHazeShader == NULL && !HeatHazeRefused;
+}
+
+const char * DX11PostProcessClass::Heat_Haze_Program_Source(unsigned index) const
+{
+ static const std::string sources[HEAT_HAZE_PROGRAMS] = {
+ std::string(HEAT_SPLAT_VERTEX_SOURCE),
+ std::string(PIXEL_SHADER_PROLOGUE) + HEAT_SPLAT_SHADER_BODY,
+ std::string(PIXEL_SHADER_PROLOGUE) + HEAT_HAZE_SHADER_BODY
+ };
+ return sources[index].c_str();
+}
+
+const char * DX11PostProcessClass::Heat_Haze_Program_Profile(unsigned index) const
+{
+ return (index == 0) ? VERTEX_PROFILE : PIXEL_PROFILE;
+}
+
+void DX11PostProcessClass::Load_Heat_Haze_Program(unsigned index, const void * bytecode, SIZE_T size)
+{
+ if (HeatHazeRefused) {
+ return;
+ }
+ ID3D11Device * device = Device->Get_Device();
+ HRESULT result = E_FAIL;
+ if (bytecode != NULL) {
+ result = (index == 0) ? device->CreateVertexShader(bytecode, size, NULL, &HeatSplatVertexShader)
+ : device->CreatePixelShader(bytecode, size, NULL, (index == 1) ? &HeatSplatShader : &HeatHazeShader);
+ }
+ // the last program in makes the rest of what the pass needs
+ if (FAILED(result) || (index + 1 == HEAT_HAZE_PROGRAMS && !Create_Heat_Haze())) {
+ Release_Heat_Haze();
+ HeatHazeRefused = true;
+ }
+}
+
+void DX11PostProcessClass::Compile_Heat_Haze_Programs()
+{
+ D3DCompileFunction compiler = compiler_function();
+ for (unsigned index = 0; index < HEAT_HAZE_PROGRAMS; ++index) {
+ ID3DBlob * code = NULL;
+ ID3DBlob * errors = NULL;
+ const char * source = Heat_Haze_Program_Source(index);
+ HRESULT result = (compiler == NULL) ? E_FAIL : compiler(source, strlen(source), "dx11heat", NULL, NULL,
+ ENTRY_POINT, Heat_Haze_Program_Profile(index), 0, 0, &code, &errors);
+ release_interface(reinterpret_cast<IUnknown **>(&errors));
+ const bool made = SUCCEEDED(result) && code != NULL;
+ Load_Heat_Haze_Program(index, made ? code->GetBufferPointer() : NULL, made ? code->GetBufferSize() : 0);
+ release_interface(reinterpret_cast<IUnknown **>(&code));
+ }
+}
+
+bool DX11PostProcessClass::Create_Heat_Haze()
+{
+ ID3D11Device * device = Device->Get_Device();
+ D3D11_BUFFER_DESC buffer;
+ ZeroMemory(&buffer, sizeof(buffer));
+ buffer.ByteWidth = DIRECT3D11_HEAT_SPLAT_LIMIT * DIRECT3D11_HEAT_SPLAT_FLOATS * sizeof(float);
+ buffer.Usage = D3D11_USAGE_DYNAMIC;
+ buffer.BindFlags = D3D11_BIND_SHADER_RESOURCE;
+ buffer.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ if (FAILED(device->CreateBuffer(&buffer, NULL, &HeatSplatBuffer))) {
+ return false;
+ }
+ D3D11_SHADER_RESOURCE_VIEW_DESC view;
+ ZeroMemory(&view, sizeof(view));
+ view.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
+ view.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
+ view.Buffer.FirstElement = 0;
+ view.Buffer.NumElements = DIRECT3D11_HEAT_SPLAT_LIMIT * DIRECT3D11_HEAT_SPLAT_FLOATS / 4;
+ if (FAILED(device->CreateShaderResourceView(HeatSplatBuffer, &view, &HeatSplatView))) {
+ return false;
+ }
+
+ // Every splat's shift and weight added to what the target holds.
+ D3D11_BLEND_DESC blend;
+ ZeroMemory(&blend, sizeof(blend));
+ blend.RenderTarget[0].BlendEnable = TRUE;
+ blend.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
+ blend.RenderTarget[0].DestBlend = D3D11_BLEND_ONE;
+ blend.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
+ blend.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
+ blend.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
+ blend.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
+ blend.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
+ return SUCCEEDED(device->CreateBlendState(&blend, &HeatBlendState));
+}
+
+bool DX11PostProcessClass::Heat_Haze_Scene(ID3D11ShaderResourceView * depth)
+{
+ if (HeatSplats.empty()) {
+ return false;
+ }
+ const unsigned count = static_cast<unsigned>(HeatSplats.size() / DIRECT3D11_HEAT_SPLAT_FLOATS);
+ std::vector<float> splats;
+ splats.swap(HeatSplats); // spent whether or not this frame can draw them
+ if (Resolved || !ShadersReady || HeatHazeRefused || depth == NULL || SceneTarget.View == NULL
+ || Device->Get_Scene_View() != SceneTarget.View) {
+ return false;
+ }
+ // loaded when the switch was first seen on (Direct3D11_Set_Heat_Splats), never on a frame's draw
+ if (HeatHazeShader == NULL || HeatBlendState == NULL) {
+ return false;
+ }
+ const unsigned half_width = (Width + 1) / 2;
+ const unsigned half_height = (Height + 1) / 2;
+ if (HeatTarget.Texture == NULL
+ && !Create_Target(HeatTarget, half_width, half_height, DXGI_FORMAT_R16G16B16A16_FLOAT)) {
+ Release_Target(HeatTarget);
+ return false;
+ }
+ if (SceneCopy.Texture == NULL
+ && !Create_Target(SceneCopy, Width, Height, TargetsAreFloat ? FLOAT_FORMAT : EIGHT_BIT_FORMAT)) {
+ Release_Target(SceneCopy);
+ return false;
+ }
+
+ ID3D11DeviceContext * context = Device->Get_Context();
+ D3D11_MAPPED_SUBRESOURCE mapped;
+ if (FAILED(context->Map(HeatSplatBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped))) {
+ return false;
+ }
+ memcpy(mapped.pData, &splats[0], splats.size() * sizeof(float));
+ context->Unmap(HeatSplatBuffer, 0);
+
+ const float nothing[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
+ context->ClearRenderTargetView(HeatTarget.View, nothing);
+ context->OMSetRenderTargets(1, &HeatTarget.View, NULL);
+ D3D11_VIEWPORT viewport;
+ viewport.TopLeftX = 0.0f;
+ viewport.TopLeftY = 0.0f;
+ viewport.Width = static_cast<float>(half_width);
+ viewport.Height = static_cast<float>(half_height);
+ viewport.MinDepth = 0.0f;
+ viewport.MaxDepth = 1.0f;
+ context->RSSetViewports(1, &viewport);
+ context->OMSetBlendState(HeatBlendState, nothing, 0xffffffff);
+ context->OMSetDepthStencilState(DepthState, 0);
+ context->RSSetState(RasterizerState);
+ context->IASetInputLayout(NULL);
+ context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
+ context->VSSetShader(HeatSplatVertexShader, NULL, 0);
+ context->VSSetShaderResources(0, 1, &HeatSplatView);
+ context->PSSetShader(HeatSplatShader, NULL, 0);
+ context->PSSetShaderResources(1, 1, &depth);
+ context->Draw(count * 6, 0);
+ ID3D11ShaderResourceView * none = NULL;
+ context->VSSetShaderResources(0, 1, &none);
+ context->PSSetShaderResources(1, 1, &none);
+
+ DX11Resource_Copy_Readable(context, SceneCopy.Texture, SceneTarget.Texture);
+
+ PassSetup pass;
+ memset(&pass, 0, sizeof(pass));
+ pass.Shader = HeatHazeShader;
+ pass.Source = SceneCopy.Resource;
+ pass.Extra = HeatTarget.Resource;
+ pass.Destination = SceneTarget.View;
+ pass.DestinationWidth = Width;
+ pass.DestinationHeight = Height;
+ pass.SourceWidth = Width;
+ pass.SourceHeight = Height;
+ Draw_Pass(pass);
+ return true;
+}
+
void DX11PostProcessClass::Draw_Pass(const PassSetup & pass)
{
ID3D11DeviceContext * context = Device->Get_Context();
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.h
index 3fba08117..a258feef7 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11post.h
@@ -55,6 +55,7 @@
#define DX11POST_H
#include <d3d11.h>
+#include <vector>
class DX11DeviceClass;
@@ -275,6 +276,23 @@ public:
void Compile_World_Ao_Programs();
static const unsigned WORLD_AO_PROGRAMS = 3;
+ // -heathaze, a prototype. The flames' splats for this frame (Direct3D11_Set_Heat_Splats has the
+ // layout); Heat_Haze_Scene then draws each into a half size target of screen shifts, added up,
+ // and puts a copy of the scene back into the scene target read through them. Once a frame,
+ // before the chain; the splats are spent either way. With none set it returns before binding
+ // anything, so a run without the switch draws what it always drew. False when it drew nothing.
+ void Set_Heat_Splats(const float * splats, unsigned count);
+ bool Heat_Haze_Scene(ID3D11ShaderResourceView * depth);
+ // Its three programs (a vertex one, then two pixel ones) come through the backend's cache as the
+ // haze's do, loaded the first time the switch is seen on, so no frame with a fire pays for them.
+ // The tests compile them here instead.
+ static const unsigned HEAT_HAZE_PROGRAMS = 3;
+ bool Wants_Heat_Haze_Programs() const;
+ const char * Heat_Haze_Program_Source(unsigned index) const;
+ const char * Heat_Haze_Program_Profile(unsigned index) const;
+ void Load_Heat_Haze_Program(unsigned index, const void * bytecode, SIZE_T size);
+ void Compile_Heat_Haze_Programs();
+
// Call once at the very top of a frame, before any scene of it (a mirror's included) begins.
// Arms Occlude_Scene again.
void Open_Frame();
@@ -411,6 +429,20 @@ private:
ID3D11PixelShader * WorldAoShaders[WORLD_AO_PROGRAMS];
float WorldAoProjection[4];
bool WorldAoRefused;
+ // -heathaze: the splats waiting for the next Heat_Haze_Scene, the half size shift target (made
+ // with the program, released with the other targets), the splat buffer the vertex program reads,
+ // its programs and its additive blend. Refused once means never tried again.
+ bool Create_Heat_Haze();
+ void Release_Heat_Haze();
+ std::vector<float> HeatSplats;
+ Target HeatTarget;
+ ID3D11Buffer * HeatSplatBuffer;
+ ID3D11ShaderResourceView * HeatSplatView;
+ ID3D11VertexShader * HeatSplatVertexShader;
+ ID3D11PixelShader * HeatSplatShader;
+ ID3D11PixelShader * HeatHazeShader;
+ ID3D11BlendState * HeatBlendState;
+ bool HeatHazeRefused;
char Diagnostic[192];
};
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
index 80f30eab5..61be9efe5 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
@@ -712,6 +712,27 @@ void Direct3D11_Set_Fire_Look(bool fire)
}
}
+void Direct3D11_Set_Heat_Splats(const float * splats, unsigned count)
+{
+ if (!Active) {
+ return;
+ }
+ // Called every particle pass while the switch is on, fires or none, so the programs are made on
+ // the first one (the shell map, a match's first frame) and through the cache.
+ if (Post.Wants_Heat_Haze_Programs()) {
+ for (unsigned index = 0; index < DX11PostProcessClass::HEAT_HAZE_PROGRAMS; ++index) {
+ std::vector<unsigned char> code;
+ if (Backend.Compile_Program(Post.Heat_Haze_Program_Source(index), "dx11heat",
+ Post.Heat_Haze_Program_Profile(index), code) && !code.empty()) {
+ Post.Load_Heat_Haze_Program(index, &code[0], code.size());
+ } else {
+ Post.Load_Heat_Haze_Program(index, NULL, 0);
+ }
+ }
+ }
+ Post.Set_Heat_Splats(splats, count);
+}
+
void Direct3D11_Set_Emissive(bool emissive)
{
if (Active) {
@@ -988,6 +1009,9 @@ void Direct3D11_Finish_Scene()
// The chain's occlusion and whatever tests against the depth over the interface read it
// single-sampled.
Backend.Resolve_Scene_Depth();
+ // -heathaze bends the finished world in its own target before the chain reads it. With no
+ // splats set (the switch off) it returns before touching anything.
+ Post.Heat_Haze_Scene(Device.Get_Depth_Texture());
}
if (Active && Post.Run_Chain()) {
take_the_frame_to_the_screen();
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
index 499d02b9c..4ed89011c 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
@@ -237,6 +237,17 @@ void Direct3D11_Set_Smoke_Glow(bool glow);
// seed in the second coordinate set (DX11BackendClass::Set_Fire_Look). The sorting pool sets it.
void Direct3D11_Set_Fire_Look(bool fire);
+// -heathaze: the flames in view as heat splats for this frame, consumed by the next
+// Direct3D11_Finish_Scene (DX11PostProcessClass::Heat_Haze_Scene). Each splat is
+// DIRECT3D11_HEAT_SPLAT_FLOATS floats: centre x, y and half width, half height in the frame's
+// clip space; the hardware depth that hides it, its strongest shift in texture coordinates, the
+// time in seconds, 0; the near and far planes, then in world units how far behind the flame a
+// surface starts to bend and over how much further it comes up to full. Past
+// DIRECT3D11_HEAT_SPLAT_LIMIT the rest are dropped. A count of zero clears them.
+const unsigned DIRECT3D11_HEAT_SPLAT_FLOATS = 12;
+const unsigned DIRECT3D11_HEAT_SPLAT_LIMIT = 2048;
+void Direct3D11_Set_Heat_Splats(const float * splats, unsigned count);
+
// The draws that follow are an Emissive particle system's billboards, which take no shadow and no
// smoke shade (DX11BackendClass::Set_Emissive). The sorting pool sets it.
void Direct3D11_Set_Emissive(bool emissive);
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp
index 58dd9f3f8..f547058a8 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp
@@ -85,6 +85,7 @@ unsigned Direct3D11_Shadow_Skin_Generation() { return 0; }
void Direct3D11_Set_Scene_View(const float [16]) {}
void Direct3D11_Set_Smoke_Glow(bool) {}
void Direct3D11_Set_Fire_Look(bool) {}
+void Direct3D11_Set_Heat_Splats(const float *, unsigned) {}
void Direct3D11_Set_Emissive(bool) {}
void Direct3D11_Set_Team_Glow(bool) {}
void Direct3D11_Set_Hemi_Ambient(bool) {}
diff --git a/GeneralsMD/Code/Tests/test_dx11post.cpp b/GeneralsMD/Code/Tests/test_dx11post.cpp
index 25e857c64..f306c5d78 100644
--- a/GeneralsMD/Code/Tests/test_dx11post.cpp
+++ b/GeneralsMD/Code/Tests/test_dx11post.cpp
@@ -35,6 +35,7 @@
#include "dx11device.h"
#include "dx11post.h"
+#include "dx11runtime.h"
#include <d3d11.h>
#include <windows.h>
@@ -373,6 +374,66 @@ TEST(dx11post_a_warp_bends_the_edge_its_ring_crosses_and_clearing_it_gives_the_f
delete [] picture;
}
+// -heathaze: one splat in the middle of the frame over a far depth bends the diagonal under it and
+// nothing in the corners; the next frame, with no splat set, is the copy pass's exact one, and so is
+// one whose depth stands at the flame's.
+TEST(dx11post_a_heat_splat_bends_the_picture_under_it_and_none_leaves_it_alone)
+{
+ DeviceFixture fixture;
+ if (!fixture.Create()) {
+ printf("skip: no Direct3D 11 device on this machine\n");
+ return;
+ }
+ DX11DeviceClass & device = fixture.Get_Device();
+ DX11PostProcessClass & post = fixture.Get_Post();
+
+ unsigned char * picture = new unsigned char[SURFACE_SIZE * SURFACE_SIZE * BYTES_A_PIXEL];
+ fill_diagonal(picture);
+ const DX11PostEffect chain[] = { DX11_POST_COPY };
+ post.Set_Chain(chain, 1);
+ CHECK(post.Wants_Heat_Haze_Programs());
+ post.Compile_Heat_Haze_Programs();
+ CHECK(!post.Wants_Heat_Haze_Programs());
+
+ // Centre, half widths, then a flame at depth a half, a shift of 5% of the frame, time 0, then
+ // planes 1 and 100, a clearance of 5 and a softness of 10: the cleared depth of 1 is the far
+ // plane, far behind it.
+ const float splat[DIRECT3D11_HEAT_SPLAT_FLOATS] = {
+ 0.0f, 0.0f, 0.5f, 0.5f,
+ 0.5f, 0.05f, 0.0f, 0.0f,
+ 1.0f, 100.0f, 5.0f, 10.0f };
+ // Frame 2 sets the splat again over a depth of a half, the flame's own: a unit standing in the
+ // fire, which has to keep every pixel.
+ for (unsigned frame = 0; frame < 3; ++frame) {
+ post.Open_Frame();
+ post.Begin_Frame();
+ device.Set_Scene_View(post.Scene_View());
+ device.Get_Context()->ClearDepthStencilView(device.Get_Depth_Stencil_View(), D3D11_CLEAR_DEPTH,
+ (frame == 2) ? 0.5f : 1.0f, 0);
+ CHECK(write_scene(device, post, picture));
+ if (frame != 1) {
+ post.Set_Heat_Splats(splat, 1);
+ }
+ CHECK_EQ(frame != 1, post.Heat_Haze_Scene(device.Get_Depth_Texture()));
+ CHECK(post.Run_Chain());
+ unsigned pitch = 0;
+ unsigned char * result = read_back_buffer(device, pitch);
+ CHECK(result != NULL);
+ if (result != NULL) {
+ if (frame == 0) {
+ CHECK(differing_pixels(picture, result, pitch) > 8u);
+ CHECK_EQ(DARK_LEVEL, pixel_level(result, pitch, 1, SURFACE_SIZE - 2));
+ CHECK_EQ(LIGHT_LEVEL, pixel_level(result, pitch, SURFACE_SIZE - 2, 1));
+ } else {
+ CHECK_EQ(0u, differing_pixels(picture, result, pitch));
+ }
+ delete [] result;
+ }
+ }
+ device.Set_Scene_View(NULL);
+ delete [] picture;
+}
+
TEST(dx11post_fxaa_leaves_a_flat_picture_alone)
{
DeviceFixture fixture;