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): soft particles and per-pixel blast lights on Direct3D 11

Olcay Seygan0b6515103d39
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
index 9861a851d..9722e34fb 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
@@ -93,6 +93,7 @@
#include "W3DDevice/GameClient/W3DWater.h"
#include "W3DDevice/GameClient/W3DShroud.h"
#include "WW3D2/dx8wrapper.h"
+#include "WW3D2/dx11runtime.h"
#include "WW3D2/light.h"
#include "WW3D2/scene.h"
#include "W3DDevice/GameClient/W3DPoly.h"
@@ -1444,10 +1445,21 @@ void HeightMapRenderObjClass::On_Frame_Update(void)
const Int xCoordMax = xCoordMin + m_map->getDrawWidth();
const Int yCoordMax = yCoordMin + m_map->getDrawHeight();
+ // On the Direct3D 11 frame every pixel of the ground takes these lights itself
+ // (BLAST_LIGHT_SAMPLING, RTS3DScene's handBlastLights); relit here as well it took them twice, in
+ // ten-unit steps. A light lit into the tiles at the moment that turns on stays there until the
+ // tiles are next rebuilt; it only turns on with the shadow map, at the match's first frame or
+ // from the options.
+ const Bool lightsPerPixel = Direct3D11_Lights_Per_Pixel();
+
for (pDynamicLightsIterator.First(); !pDynamicLightsIterator.Is_Done(); pDynamicLightsIterator.Next())
- {
+ {
W3DDynamicLight *pLight = (W3DDynamicLight*)pDynamicLightsIterator.Peek_Obj();
pLight->m_processMe = false;
+ if (lightsPerPixel) {
+ pLight->m_priorEnable = false;
+ continue;
+ }
if (pLight->m_enabled || pLight->m_priorEnable) {
Real range = pLight->Get_Attenuation_Range();
if (pLight->m_priorEnable) {
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
index 6721b4b04..9d781bdfe 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
@@ -2714,10 +2714,11 @@ AGAIN:
unsigned pipelines = 0;
double textureMS = 0.0;
unsigned textures = 0;
- Direct3D11_Take_Frame_Cost( pipelineMS, pipelines, textureMS, textures );
+ unsigned depthCopies = 0;
+ Direct3D11_Take_Frame_Cost( pipelineMS, pipelines, textureMS, textures, depthCopies );
if( pipelineMS + textureMS > DX11_FRAME_COST_REPORT_MS )
- DEBUG_LOG(("DX11 FRAME COST frame %d: %u pipelines built in %.1f ms, %u textures copied in %.1f ms\n",
- TheGameLogic->getFrame(), pipelines, pipelineMS, textures, textureMS));
+ DEBUG_LOG(("DX11 FRAME COST frame %d: %u pipelines built in %.1f ms, %u textures copied in %.1f ms, %u soft particle depth copies\n",
+ TheGameLogic->getFrame(), pipelines, pipelineMS, textures, textureMS, depthCopies));
long presentResult = 0;
if( Direct3D11_Take_Present_Failure( presentResult ) )
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp
index 690d138b5..c62646fca 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp
@@ -71,7 +71,12 @@
#include "WW3D2/colorspace.h"
#include "WW3D2/shdlib.h"
+#include "WW3D2/dx11runtime.h"
+#include "WW3D2/ffshader.h"
#include "Platform/RenderTypes.h"
+
+#include <algorithm>
+#include <vector>
#ifdef _INTERNAL
// for occasional debugging...
//#pragma optimize("", off)
@@ -807,7 +812,9 @@ void RTS3DScene::renderOneObject(RenderInfoClass &rinfo, RenderObjClass *robj, I
}
}
- if( draw && draw->getReceivesDynamicLights() )
+ // On the Direct3D 11 frame the dynamic lights reach every pixel instead (handBlastLights), and
+ // added here as well they would light the model twice.
+ if( draw && draw->getReceivesDynamicLights() && !Direct3D11_Lights_Per_Pixel() )
{
// dynamic lights
RefRenderObjListIterator dynaLightIt(&m_dynamicLightList);
@@ -1160,6 +1167,67 @@ void RTS3DScene::Render(RenderInfoClass & rinfo)
/** Custom render method for the RTS3DScene, custom render properties for our
* particular game go here */
//=============================================================================
+//-------------------------------------------------------------------------------------------------
+/** The dynamic point lights (explosions, muzzle flashes, burning wrecks) for the Direct3D 11 frame
+ to light per pixel (BLAST_LIGHT_SAMPLING), the BLAST_LIGHT_SLOTS nearest the camera. The
+ colours go over divided by the map's terrain light, the way the headlights' gain is worked out
+ (W3DModelDraw::lightHeadlights), so the shader multiplies the lit pixel by one plus the sum.
+ Lights the terrain relight would skip (a near reach under a tenth of a unit) are skipped here
+ too. Client only: the lights are what FX lists put up and nothing in logic reads them. */
+static void handBlastLights(RefRenderObjListClass &dynamicLights, const Vector3 &camera)
+{
+ if (!Direct3D11_Lights_Per_Pixel())
+ return;
+
+ static std::vector< std::pair<Real, W3DDynamicLight*> > nearest;
+ nearest.clear();
+ RefRenderObjListIterator it(&dynamicLights);
+ for (it.First(); !it.Is_Done(); it.Next())
+ {
+ W3DDynamicLight *light = (W3DDynamicLight*)it.Peek_Obj();
+ if (!light->isEnabled() || light->Get_Type() != LightClass::POINT)
+ continue;
+ float nearReach, farReach;
+ light->Get_Far_Attenuation_Range(nearReach, farReach);
+ if (nearReach < 0.1f || farReach <= 0.0f)
+ continue;
+ const Vector3 d = light->Get_Position() - camera;
+ nearest.push_back(std::make_pair(d.Length2(), light));
+ }
+ // ponytail: nearest the camera, not nearest the screen's middle; weigh by on-screen reach if a
+ // barrage just off the edge ever steals slots from the fight in view.
+ std::sort(nearest.begin(), nearest.end(),
+ [](const std::pair<Real, W3DDynamicLight*> &a, const std::pair<Real, W3DDynamicLight*> &b)
+ { return a.first < b.first; });
+
+ const RGBColor &ambient = TheGlobalData->m_terrainAmbient[0];
+ const RGBColor &diffuse = TheGlobalData->m_terrainDiffuse[0];
+ const Real map[3] = { ambient.red + diffuse.red, ambient.green + diffuse.green, ambient.blue + diffuse.blue };
+ const Real mean = (map[0] + map[1] + map[2]) / 3.0f;
+ Real base[3];
+ for (Int c = 0; c < 3; ++c)
+ base[c] = sqrtf((map[c] > 0.15f ? map[c] : 0.15f) * (mean > 0.15f ? mean : 0.15f));
+
+ static float lights[BLAST_LIGHT_SLOTS][BLAST_LIGHT_FLOATS];
+ unsigned count = 0;
+ for (size_t i = 0; i < nearest.size() && count < BLAST_LIGHT_SLOTS; ++i)
+ {
+ W3DDynamicLight *light = nearest[i].second;
+ float nearReach, farReach;
+ light->Get_Far_Attenuation_Range(nearReach, farReach);
+ Vector3 position = light->Get_Position(), lightDiffuse, lightAmbient;
+ light->Get_Diffuse(&lightDiffuse);
+ light->Get_Ambient(&lightAmbient);
+ float *slot = lights[count++];
+ slot[0] = position.X; slot[1] = position.Y; slot[2] = position.Z; slot[3] = farReach;
+ slot[4] = lightDiffuse.X / base[0]; slot[5] = lightDiffuse.Y / base[1]; slot[6] = lightDiffuse.Z / base[2];
+ slot[7] = nearReach;
+ slot[8] = lightAmbient.X / base[0]; slot[9] = lightAmbient.Y / base[1]; slot[10] = lightAmbient.Z / base[2];
+ slot[11] = 0.0f;
+ }
+ Direct3D11_Set_Blast_Lights(&lights[0][0], count);
+}
+
void RTS3DScene::Customized_Render( RenderInfoClass &rinfo )
{
#ifdef DIRTY_CONDITION_FLAGS
@@ -1214,6 +1282,10 @@ void RTS3DScene::Customized_Render( RenderInfoClass &rinfo )
}
}
+ // After the lights' fade has stepped and before the terrain and the models are lit.
+ if (m_customPassMode == SCENE_PASS_DEFAULT && !ShaderClass::Is_Backface_Culling_Inverted())
+ handBlastLights(m_dynamicLightList, rinfo.Camera.Get_Position());
+
//terrain needs to be rendered first
if (terrainObject) // Don't check visibility - terrain is always visible. jba.
{
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
index b84db977d..557fb9e53 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp
@@ -308,6 +308,14 @@ DX11BackendClass::DX11BackendClass()
memset(Headlights, 0, sizeof(Headlights));
HeadlightCount = 0;
memset(HeadlightGain, 0, sizeof(HeadlightGain));
+ memset(BlastLights, 0, sizeof(BlastLights));
+ BlastLightCount = 0;
+ SoftParticles = false;
+ SceneDepthCopy = NULL;
+ SceneDepthCopyView = NULL;
+ SceneDepthStale = true;
+ SceneDepthRefused = false;
+ FrameDepthCopies = 0;
set_identity(WorldFromClip);
for (unsigned stage = 0; stage < DX11_BACKEND_TEXTURE_STAGES; ++stage) {
set_identity(TextureTransforms[stage]);
@@ -467,6 +475,7 @@ void DX11BackendClass::Shutdown()
ShadowMapSize = 0;
ShadowMapBound = false;
Release_Smoke_Map();
+ Release_Scene_Depth();
RenderStates.Set_Shadow_Caster_Pass(false);
Note_Shadow_State_Changed();
Device = NULL;
@@ -610,6 +619,9 @@ static const unsigned SMOKE_MOST_CASTERS = 16384;
// side, and at 32 soot went flat.
static const float SMOKE_SELF_SHADOW_GAIN = 0.8f;
static const float SMOKE_SELF_SHADOW_CURVE = 16.0f;
+// How far in front of what is behind it a particle sprite has fully faded in, in world units. A
+// tank is about thirty long; a smoke puff is ten to forty across.
+static const float SOFT_PARTICLE_FADE_UNITS = 12.0f;
// Each caster is a disc facing the sun, drawn as a four corner strip whose corners come from the
// vertex number, so the only buffer is the one holding the casters.
@@ -925,6 +937,138 @@ bool DX11BackendClass::Smoke_Glow() const
&& (VertexFormat & D3DFVF_NORMAL) != 0 && (VertexFormat & D3DFVF_XYZRHW) == 0;
}
+void DX11BackendClass::Set_Soft_Particles(bool soft)
+{
+ if (SoftParticles != soft) {
+ SoftParticles = soft;
+ PipelineChanged = true;
+ ConstantsChanged = true;
+ }
+}
+
+/** Which fade a draw takes. Only the sorted billboards, which the sorting pool marks, and only
+ while the depth being tested against is the scene's own: a mirror or a filter target of another
+ size has a depth buffer of its own that nothing here copies. A multiplicative blend is left
+ hard, because faded towards zero it would darken rather than vanish. */
+unsigned DX11BackendClass::Soft_Particle() const
+{
+ if (!SoftParticles || SceneDepthCopyView == NULL || ShadowMapBound) {
+ return SOFT_PARTICLE_NONE;
+ }
+ if (VertexProgram != ENGINE_SHADER_NONE || PixelProgram != ENGINE_SHADER_NONE
+ || (VertexFormat & D3DFVF_XYZRHW) != 0 || !Camera_Space_Draw()) {
+ return SOFT_PARTICLE_NONE;
+ }
+ if (CurrentTarget != NULL && CurrentDepth != Device->Get_Depth_Stencil_View()) {
+ return SOFT_PARTICLE_NONE;
+ }
+ if (Device->Get_Depth_Texture() == NULL
+ || RenderStates.Get_Render_State(D3DRS_ALPHABLENDENABLE) == FALSE) {
+ return SOFT_PARTICLE_NONE;
+ }
+ const DWORD source = RenderStates.Get_Render_State(D3DRS_SRCBLEND);
+ const DWORD destination = RenderStates.Get_Render_State(D3DRS_DESTBLEND);
+ if (source == D3DBLEND_DESTCOLOR || source == D3DBLEND_ZERO || destination == D3DBLEND_SRCCOLOR) {
+ return SOFT_PARTICLE_NONE;
+ }
+ return (source == D3DBLEND_ONE) ? SOFT_PARTICLE_COLOUR : SOFT_PARTICLE_ALPHA;
+}
+
+void DX11BackendClass::Release_Scene_Depth()
+{
+ if (SceneDepthCopyView != NULL) {
+ SceneDepthCopyView->Release();
+ SceneDepthCopyView = NULL;
+ }
+ if (SceneDepthCopy != NULL) {
+ SceneDepthCopy->Release();
+ SceneDepthCopy = NULL;
+ }
+ SceneDepthStale = true;
+}
+
+/** The scene's depth as the soft particles read it, taken once at the top of each sorted flush and
+ before any of its draws resolves a pipeline: a sorted mesh that writes depth between two
+ particle runs would otherwise cost a full copy per run, and a copy the device refuses would be
+ found only after a fading program was chosen, which then read nothing and drew nothing. The
+ copy is made at the depth buffer's own size and typeless format, made again when a resize
+ changes either, and refreshed only when a draw has written depth since the last one. */
+void DX11BackendClass::Take_Scene_Depth()
+{
+ if (SceneDepthRefused || Device == NULL) {
+ return;
+ }
+ ID3D11ShaderResourceView * const source_view = Device->Get_Depth_Texture();
+ if (source_view == NULL) {
+ return;
+ }
+ ID3D11Resource * source = NULL;
+ source_view->GetResource(&source);
+ if (source == NULL) {
+ return;
+ }
+
+ D3D11_TEXTURE2D_DESC description;
+ static_cast<ID3D11Texture2D *>(source)->GetDesc(&description);
+ if (SceneDepthCopy != NULL) {
+ D3D11_TEXTURE2D_DESC held;
+ SceneDepthCopy->GetDesc(&held);
+ if (held.Width != description.Width || held.Height != description.Height
+ || held.Format != description.Format) {
+ Release_Scene_Depth();
+ }
+ }
+ if (SceneDepthCopy == NULL) {
+ description.BindFlags = D3D11_BIND_SHADER_RESOURCE;
+ description.MiscFlags = 0;
+ description.Usage = D3D11_USAGE_DEFAULT;
+ description.CPUAccessFlags = 0;
+ D3D11_SHADER_RESOURCE_VIEW_DESC view;
+ source_view->GetDesc(&view);
+ if (FAILED(Device->Get_Device()->CreateTexture2D(&description, NULL, &SceneDepthCopy))
+ || FAILED(Device->Get_Device()->CreateShaderResourceView(SceneDepthCopy, &view,
+ &SceneDepthCopyView))) {
+ Note_Refusal("the device refused a copy of the scene's depth for the soft particles");
+ Release_Scene_Depth();
+ SceneDepthRefused = true;
+ PipelineChanged = true;
+ source->Release();
+ return;
+ }
+ SceneDepthStale = true;
+ PipelineChanged = true; // Soft_Particle reads whether the copy exists
+ }
+ if (SceneDepthStale) {
+ Device->Get_Context()->CopyResource(SceneDepthCopy, source);
+ SceneDepthStale = false;
+ ++FrameDepthCopies;
+ }
+ source->Release();
+}
+
+unsigned DX11BackendClass::Take_Frame_Depth_Copies()
+{
+ const unsigned copies = FrameDepthCopies;
+ FrameDepthCopies = 0;
+ return copies;
+}
+
+void DX11BackendClass::Set_Blast_Lights(const float * lights, unsigned count)
+{
+ if (count > BLAST_LIGHT_SLOTS) {
+ count = BLAST_LIGHT_SLOTS;
+ }
+ if (count == BlastLightCount
+ && (count == 0 || memcmp(BlastLights, lights, sizeof(float) * BLAST_LIGHT_FLOATS * count) == 0)) {
+ return;
+ }
+ if (count > 0) {
+ memcpy(BlastLights, lights, sizeof(float) * BLAST_LIGHT_FLOATS * count);
+ }
+ BlastLightCount = count;
+ ConstantsChanged = true;
+}
+
bool DX11BackendClass::Views_Current_Target(unsigned stage, ID3D11ShaderResourceView * texture) const
{
if (texture == NULL || CurrentTarget == NULL) {
@@ -1081,6 +1225,8 @@ void DX11BackendClass::Set_Render_State(D3DRENDERSTATETYPE state, DWORD value)
case D3DRS_TEXTUREFACTOR:
case D3DRS_ALPHAREF:
case D3DRS_ALPHABLENDENABLE:
+ case D3DRS_SRCBLEND: // a multiply pass takes no blast light (Upload_Pixel_Constants)
+ case D3DRS_DESTBLEND:
PixelConstantsChanged = true;
break;
default:
@@ -1482,6 +1628,8 @@ void DX11BackendClass::Dump_Program(const std::string & key, const std::string &
void DX11BackendClass::Begin_Scene()
{
+ SceneDepthStale = true;
+
// A scene can begin with a render target already set: the water's reflection and the shadow
// projector both set theirs and then call WW3D::Begin_Render, and forcing the back buffer here
// sent the reflection scene, and its clear, over the picture that was already drawn.
@@ -1653,6 +1801,7 @@ void DX11BackendClass::Clear(bool colour, bool depth, const float colour_value[4
if (depth && depth_view != NULL) {
Device->Get_Context()->ClearDepthStencilView(depth_view,
D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
+ SceneDepthStale = true;
}
}
@@ -1689,6 +1838,7 @@ bool DX11BackendClass::Build_Combiner_Description(CombinerDescription & descript
}
description.ShadowReceiving = description.StageCount > 0 && Shadow_Receiving();
description.SmokeGlow = Smoke_Glow();
+ description.SoftParticle = Soft_Particle();
return description.StageCount > 0;
}
@@ -2203,6 +2353,29 @@ void DX11BackendClass::Upload_Pixel_Constants()
memcpy(pixel_block.HeadlightPosition[slot], &Headlights[slot][0], sizeof(float) * 4);
memcpy(pixel_block.HeadlightDirection[slot], &Headlights[slot][4], sizeof(float) * 4);
}
+ // None for a particle: the smoke takes these lights on the CPU (BLAST_LIGHT_SAMPLING). None
+ // for a pass that multiplies what is under it (ZERO, SRCCOLOR): the base pass took the light
+ // already, and gained again the surface came out lit twice.
+ const bool multiplies = RenderStates.Get_Render_State(D3DRS_ALPHABLENDENABLE) != FALSE
+ && (RenderStates.Get_Render_State(D3DRS_SRCBLEND) == D3DBLEND_ZERO
+ || RenderStates.Get_Render_State(D3DRS_DESTBLEND) == D3DBLEND_SRCCOLOR);
+ const unsigned blasts = (camera_space || multiplies) ? 0 : BlastLightCount;
+ pixel_block.BlastLightParameters[0] = static_cast<float>(blasts);
+ for (unsigned slot = 0; slot < blasts; ++slot) {
+ memcpy(pixel_block.BlastLightPosition[slot], &BlastLights[slot][0], sizeof(float) * 4);
+ memcpy(pixel_block.BlastLightDiffuse[slot], &BlastLights[slot][4], sizeof(float) * 4);
+ memcpy(pixel_block.BlastLightAmbient[slot], &BlastLights[slot][8], sizeof(float) * 4);
+ }
+ }
+
+ // The particles' projection, the one the world behind them was drawn with: the distance from a
+ // depth is SoftParticleDepth.x / (depth * .y - .z) (SOFT_PARTICLE_SAMPLING). Written only around
+ // the sorted billboards, so every other draw keeps the bytes it had.
+ if (SoftParticles && camera_space) {
+ pixel_block.SoftParticleDepth[0] = Projection[11] * Projection[14];
+ pixel_block.SoftParticleDepth[1] = Projection[11];
+ pixel_block.SoftParticleDepth[2] = Projection[10];
+ pixel_block.SoftParticleDepth[3] = 1.0f / SOFT_PARTICLE_FADE_UNITS;
}
if (!PixelConstantsHeld
@@ -2458,6 +2631,16 @@ void DX11BackendClass::Bind_State_Objects()
}
}
}
+
+ // The scene's depth at t7 for a soft particle, read with Load, so it wants no sampler. The copy
+ // was taken at the top of the sorted flush (Take_Scene_Depth) and is refreshed in place.
+ if (SoftParticles && Soft_Particle() != SOFT_PARTICLE_NONE) {
+ ID3D11ShaderResourceView * const depth = SceneDepthCopyView;
+ if (!known || depth != Bound.SceneDepth) {
+ context->PSSetShaderResources(DX11_BACKEND_TEXTURE_STAGES + 3, 1, &depth);
+ Bound.SceneDepth = depth;
+ }
+ }
}
void DX11BackendClass::Bind_Pipeline(const Pipeline & pipeline, ID3D11Buffer * vertices, UINT stride,
@@ -2597,6 +2780,12 @@ bool DX11BackendClass::Draw_Indexed(unsigned index_count, unsigned start_index,
context->DrawIndexed(index_count, start_index, base_vertex);
++DrawsMade;
+ // The soft particles' copy of the depth is behind whatever this wrote into the scene's depth
+ // (Take_Scene_Depth); a mirror or a filter target's own depth is not what they read.
+ if (RenderStates.Get_Render_State(D3DRS_ZWRITEENABLE) != FALSE
+ && (CurrentTarget == NULL || CurrentDepth == Device->Get_Depth_Stencil_View())) {
+ SceneDepthStale = true;
+ }
if (CurrentTarget != NULL) {
++DrawsIntoTargets;
MaskWhileTargeted |= RenderStates.Get_Render_State(D3DRS_COLORWRITEENABLE);
@@ -2637,6 +2826,12 @@ bool DX11BackendClass::Draw_Triangles(unsigned vertex_count, unsigned start_vert
context->Draw(vertex_count, start_vertex);
++DrawsMade;
+ // The soft particles' copy of the depth is behind whatever this wrote into the scene's depth
+ // (Take_Scene_Depth); a mirror or a filter target's own depth is not what they read.
+ if (RenderStates.Get_Render_State(D3DRS_ZWRITEENABLE) != FALSE
+ && (CurrentTarget == NULL || CurrentDepth == Device->Get_Depth_Stencil_View())) {
+ SceneDepthStale = true;
+ }
if (CurrentTarget != NULL) {
++DrawsIntoTargets;
MaskWhileTargeted |= RenderStates.Get_Render_State(D3DRS_COLORWRITEENABLE);
@@ -2799,6 +2994,12 @@ bool DX11BackendClass::Draw_User_Strip(const void * vertices, unsigned primitive
context->Draw(list_vertex_count, 0);
++DrawsMade;
+ // The soft particles' copy of the depth is behind whatever this wrote into the scene's depth
+ // (Take_Scene_Depth); a mirror or a filter target's own depth is not what they read.
+ if (RenderStates.Get_Render_State(D3DRS_ZWRITEENABLE) != FALSE
+ && (CurrentTarget == NULL || CurrentDepth == Device->Get_Depth_Stencil_View())) {
+ SceneDepthStale = true;
+ }
if (CurrentTarget != NULL) {
++DrawsIntoTargets;
MaskWhileTargeted |= RenderStates.Get_Render_State(D3DRS_COLORWRITEENABLE);
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
index 7cc1cedf9..0a4c54ec4 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h
@@ -94,6 +94,26 @@ public:
// is what a pixel gains per channel per unit of light (HEADLIGHT_SAMPLING).
void Set_Headlights(const float * lights, unsigned count, const float gain[3]);
+ // The scene's dynamic point lights for the frame, BLAST_LIGHT_FLOATS a light: world position and
+ // far reach, diffuse colour and near reach, ambient colour and one unused. The colours are
+ // already divided by the map's terrain light (BLAST_LIGHT_SAMPLING). Past BLAST_LIGHT_SLOTS the
+ // rest are dropped; the caller sorts them nearest first.
+ void Set_Blast_Lights(const float * lights, unsigned count);
+
+ // Whether the shadow-receiving programs, which carry the per-pixel lights, are drawing this
+ // frame. While they are, the engine leaves the dynamic lights out of its own vertex lighting.
+ bool Lights_Per_Pixel() const { return ShadowReceiving && ShadowMapTexture != NULL; }
+
+ // The draws that follow are sorted particle billboards, which fade where they meet the scene's
+ // depth (SOFT_PARTICLE_SAMPLING). Only the sorting pool sets it, around the billboards' runs.
+ void Set_Soft_Particles(bool soft);
+
+ // Copy the scene's depth for the soft particles if anything wrote it since the last copy. The
+ // sorting pool calls it at the top of each flush, before any particle run resolves its program.
+ void Take_Scene_Depth();
+ // How many depth copies Take_Scene_Depth made since the last call. Taking it resets it.
+ unsigned Take_Frame_Depth_Copies();
+
// The engine bound a texture at this stage that has no D3D11 copy - a render target it drew
// into, most often. Sampling white there paints a full screen quad over the frame, so a draw
// that reads one is refused instead.
@@ -345,6 +365,15 @@ private:
float HeadlightParameters[4];
float HeadlightPosition[HEADLIGHT_SLOTS][4];
float HeadlightDirection[HEADLIGHT_SLOTS][4];
+ // The dynamic point lights (BLAST_LIGHT_SAMPLING): how many are lit, then each slot's
+ // position and far reach, diffuse and near reach, and ambient.
+ float BlastLightParameters[4];
+ float BlastLightPosition[BLAST_LIGHT_SLOTS][4];
+ float BlastLightDiffuse[BLAST_LIGHT_SLOTS][4];
+ float BlastLightAmbient[BLAST_LIGHT_SLOTS][4];
+ // A soft particle's way from a depth back to a distance, and one over the distance its fade
+ // takes (SOFT_PARTICLE_SAMPLING). Last, so only a program that fades has to declare it.
+ float SoftParticleDepth[4];
};
// A draw that is painting the world and can take a shadow from the sun's map.
bool Shadow_Receiving() const;
@@ -461,6 +490,20 @@ private:
float Headlights[HEADLIGHT_SLOTS][8];
unsigned HeadlightCount;
float HeadlightGain[3];
+ // Set_Blast_Lights' slots and how many are lit.
+ float BlastLights[BLAST_LIGHT_SLOTS][BLAST_LIGHT_FLOATS];
+ unsigned BlastLightCount;
+ // Set_Soft_Particles, and which fade the current draw takes (a SOFT_PARTICLE_* value).
+ bool SoftParticles;
+ unsigned Soft_Particle() const;
+ // The scene's depth as a texture, copied when a soft particle first needs it after anything
+ // wrote depth: a depth buffer cannot be sampled while it is the one being tested against.
+ ID3D11Texture2D * SceneDepthCopy;
+ ID3D11ShaderResourceView * SceneDepthCopyView;
+ bool SceneDepthStale;
+ bool SceneDepthRefused; ///< the device would not make the copy; no draw fades again
+ unsigned FrameDepthCopies;
+ void Release_Scene_Depth();
bool Make_Smoke_Map();
void Release_Smoke_Map();
@@ -732,6 +775,8 @@ private:
// second sampler is null when only the first was set.
ID3D11ShaderResourceView * Maps[2];
ID3D11SamplerState * MapSamplers[2];
+ // The scene's depth copy at t7, for the soft particles.
+ ID3D11ShaderResourceView * SceneDepth;
ID3D11InputLayout * Layout;
ID3D11Buffer * VertexBuffer;
UINT VertexStride;
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
index 149d49b2d..9478af414 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp
@@ -263,6 +263,32 @@ void Direct3D11_Set_Headlights(const float * lights, unsigned count, const float
}
}
+void Direct3D11_Set_Blast_Lights(const float * lights, unsigned count)
+{
+ if (Active) {
+ Backend.Set_Blast_Lights(lights, count);
+ }
+}
+
+bool Direct3D11_Lights_Per_Pixel()
+{
+ return Active && Backend.Lights_Per_Pixel();
+}
+
+void Direct3D11_Set_Soft_Particles(bool soft)
+{
+ if (Active) {
+ Backend.Set_Soft_Particles(soft);
+ }
+}
+
+void Direct3D11_Take_Scene_Depth()
+{
+ if (Active) {
+ Backend.Take_Scene_Depth();
+ }
+}
+
bool Direct3D11_Begin_Shadow_Map(unsigned size)
{
return Active ? Backend.Begin_Shadow_Map(size) : false;
@@ -738,12 +764,14 @@ void Direct3D11_Program_Statistics(unsigned & shipped, unsigned & held)
}
void Direct3D11_Take_Frame_Cost(double & pipeline_milliseconds, unsigned & pipelines,
- double & texture_milliseconds, unsigned & textures)
+ double & texture_milliseconds, unsigned & textures, unsigned & depth_copies)
{
pipeline_milliseconds = 0.0;
pipelines = 0;
+ depth_copies = 0;
if (Active) {
Backend.Take_Frame_Build_Cost(pipeline_milliseconds, pipelines);
+ depth_copies = Backend.Take_Frame_Depth_Copies();
}
DX11Texture_Take_Frame_Cost(texture_milliseconds, textures);
}
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
index e61da0735..3afaf0323 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h
@@ -115,6 +115,24 @@ void Direct3D11_Mirror_Texture(unsigned stage, struct IDirect3DBaseTexture9 * te
// per unit of their light. A Direct3D 9 run ignores them.
void Direct3D11_Set_Headlights(const float * lights, unsigned count, const float gain[3]);
+// The scene's dynamic point lights for the frame, nearest the camera first, twelve floats each
+// (BLAST_LIGHT_FLOATS): world position and far reach, diffuse over the map's terrain light and near
+// reach, ambient over the same and one unused. A Direct3D 9 run ignores them.
+void Direct3D11_Set_Blast_Lights(const float * lights, unsigned count);
+
+// True while the Direct3D 11 frame's shadow-receiving programs are lighting the dynamic lights per
+// pixel, which is when the engine's own vertex lighting has to leave them out or light them twice.
+// False under -d3d9, -headless and the Classic graphics setting.
+bool Direct3D11_Lights_Per_Pixel();
+
+// The draws that follow are sorted particle billboards, which fade where they meet the scene's
+// depth. The sorting pool sets it around the billboards' runs.
+void Direct3D11_Set_Soft_Particles(bool soft);
+
+// Copy the scene's depth for the soft particles, if anything wrote it since the last copy. The
+// sorting pool calls it at the top of each flush, before any particle run resolves its program.
+void Direct3D11_Take_Scene_Depth();
+
// The sun's depth buffer. Between Begin and End every draw lands in it and nowhere else, which is
// how the caster pass is written without the engine knowing what a render target is. False from
// Begin means there is no Direct3D 11 backend or the device refused the surface, and the caller
@@ -291,9 +309,9 @@ void Direct3D11_Statistics(unsigned & pipelines_built, unsigned long long & draw
void Direct3D11_Program_Statistics(unsigned & shipped, unsigned & held);
// What the frame since the last call spent building pipelines and copying textures, and how many
-// of each. Taking it resets it.
+// of each, and how many times the soft particles copied the scene's depth. Taking it resets it.
void Direct3D11_Take_Frame_Cost(double & pipeline_milliseconds, unsigned & pipelines,
- double & texture_milliseconds, unsigned & textures);
+ double & texture_milliseconds, unsigned & textures, unsigned & depth_copies);
// The refusals split by cause: no buffer bound, no texture stage enabled, a vertex format with no
// input layout, and a program that could not be generated or compiled.
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp
index 1a03edec4..d8776fd6e 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp
@@ -60,6 +60,10 @@ DX11BufferTwinClass * Direct3D11_Twin_Index_Buffer(unsigned, bool) { return NULL
void Direct3D11_Mirror_Texture(unsigned, struct IDirect3DBaseTexture9 *) {}
void Direct3D11_Set_Headlights(const float *, unsigned, const float [3]) {}
+void Direct3D11_Set_Blast_Lights(const float *, unsigned) {}
+bool Direct3D11_Lights_Per_Pixel() { return false; }
+void Direct3D11_Set_Soft_Particles(bool) {}
+void Direct3D11_Take_Scene_Depth() {}
bool Direct3D11_Begin_Shadow_Map(unsigned) { return false; }
void Direct3D11_End_Shadow_Map() {}
@@ -146,12 +150,13 @@ void Direct3D11_Program_Statistics(unsigned & shipped, unsigned & held)
held = 0;
}
void Direct3D11_Take_Frame_Cost(double & pipeline_milliseconds, unsigned & pipelines,
- double & texture_milliseconds, unsigned & textures)
+ double & texture_milliseconds, unsigned & textures, unsigned & depth_copies)
{
pipeline_milliseconds = 0.0;
pipelines = 0;
texture_milliseconds = 0.0;
textures = 0;
+ depth_copies = 0;
}
void Direct3D11_Refusals(unsigned long long & no_buffer, unsigned long long & no_stage,
unsigned long long & no_layout, unsigned long long & no_program,
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.cpp
index d88dc4fde..3851553b2 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.cpp
@@ -252,6 +252,9 @@ bool CombinerShader_Generate(const CombinerDescription & description, CombinerSh
static bool generate_combiners(const CombinerDescription & description,
CombinerShaderTarget target, std::string & hlsl, bool volumetric)
{
+ // The soft particles ride on the Direct3D 11 text for the smoke's reason: the SDL3 backend binds
+ // nothing at t7 and uploads nothing past SkyUp.
+ const bool soft = volumetric && description.SoftParticle != SOFT_PARTICLE_NONE;
volumetric = volumetric && description.ShadowReceiving;
if (description.StageCount == 0 || description.StageCount > MAXIMUM_COMBINER_STAGES) {
return false;
@@ -355,8 +358,9 @@ static bool generate_combiners(const CombinerDescription & description,
// A shader declares a prefix of the block the backend uploads, so a field is found by what
// comes before it and not by its name. The shadow fields sit behind the old normal mapped
// ones and the terrain's sun, so a program that reads them has to declare those too, unused:
- // without them a shadow matrix lands where the first light's direction is.
- if (description.ShadowReceiving) {
+ // without them a shadow matrix lands where the first light's direction is. A soft particle's
+ // field closes the block, so a program that fades declares all of it.
+ if (description.ShadowReceiving || soft) {
char line[256];
snprintf(line, sizeof(line),
" float4 NormalLightDirection[%u];\n"
@@ -377,7 +381,7 @@ static bool generate_combiners(const CombinerDescription & description,
" float4 Sky;\n"
" float4 SkyUp;\n";
}
- if (volumetric) {
+ if (volumetric || soft) {
hlsl += VOLUMETRIC_CONSTANTS;
}
hlsl += "};\n";
@@ -387,6 +391,9 @@ static bool generate_combiners(const CombinerDescription & description,
if (volumetric) {
hlsl += VOLUMETRIC_SAMPLING;
}
+ if (soft) {
+ hlsl += SOFT_PARTICLE_SAMPLING;
+ }
}
else {
for (unsigned stage = 0; stage < MAXIMUM_COMBINER_STAGES; ++stage) {
@@ -454,6 +461,14 @@ static bool generate_combiners(const CombinerDescription & description,
return false;
}
+ // After the alpha test: faded before it, a sprite's edge against the ground would be clipped to
+ // a hard line again, only a little further out.
+ if (soft) {
+ hlsl += (description.SoftParticle == SOFT_PARTICLE_COLOUR)
+ ? " current *= soft_particle_fade(input.Position);\n"
+ : " current.a *= soft_particle_fade(input.Position);\n";
+ }
+
hlsl +=
" return current;\n"
"}\n";
@@ -528,5 +543,9 @@ std::string CombinerShader_Key(const CombinerDescription & description)
if (description.SmokeGlow) {
key += ":G";
}
+ if (description.SoftParticle != SOFT_PARTICLE_NONE) {
+ snprintf(field, sizeof(field), ":Z%u", description.SoftParticle);
+ key += field;
+ }
return key;
}
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.h
index 329713dea..41eba853b 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffshader.h
@@ -105,6 +105,20 @@ struct CombinerDescription
// glow of a fire behind a plume went dark with the plume's far side. Replaces SpecularAdd, whose
// slot it takes. Initialised here for a caller that fills the rest field by field.
bool SmokeGlow = false;
+
+ // D3D11 only: a sorted particle billboard fades out where it comes within a few units of what
+ // is already drawn behind it, read from a copy of the scene's depth at t7 (SOFT_PARTICLE_SAMPLING).
+ // Without it a smoke or fire sprite cuts a straight line wherever it passes through the ground or
+ // a building. SOFT_PARTICLE_ALPHA fades the alpha alone, which is what a SRCALPHA blend reads;
+ // SOFT_PARTICLE_COLOUR fades the colour as well, for a blend that adds the colour unweighted.
+ unsigned SoftParticle = 0;
+};
+
+enum
+{
+ SOFT_PARTICLE_NONE = 0,
+ SOFT_PARTICLE_ALPHA = 1,
+ SOFT_PARTICLE_COLOUR = 2
};
// The constant block still carries this many normal mapped light slots, unread, so the shadow and
@@ -300,7 +314,8 @@ const unsigned NORMAL_MAPPED_LIGHTS = 4;
" return sun * smoke_reaching(position);\n" \
"}\n" \
"\n" \
- HEADLIGHT_SAMPLING
+ HEADLIGHT_SAMPLING \
+ BLAST_LIGHT_SAMPLING
// Vehicle headlights on a night map, Direct3D 11 only and riding on the smoke's text for that
// reason: one spot light per lit vehicle, the nearest HEADLIGHT_SLOTS to the camera
@@ -345,22 +360,91 @@ const unsigned NORMAL_MAPPED_LIGHTS = 4;
"}\n" \
"\n"
+// The dynamic point lights of the scene (explosions, muzzle flashes, burning wrecks), per pixel on
+// the Direct3D 11 frame: RTS3DScene hands the BLAST_LIGHT_SLOTS nearest the camera over every frame
+// (Direct3D11_Set_Blast_Lights) and, while this runs, leaves them out of the per-object vertex
+// lights and the terrain's per-vertex relight, which held four to an object and lit the ground in
+// ten-unit steps. Each slot is the world position with the far reach in w, the diffuse colour
+// with the near reach (full strength inside it) in w, and the ambient colour. The falloff and the
+// facing are the terrain relight's (HeightMapRenderObjClass::doTheDynamicLight); the colours come
+// over divided by the map's own terrain light, so the result is a gain on the lit pixel the way the
+// headlights' is, and the shroud stays black. The surface is turned to face the eye, which the
+// derivatives alone do not say, so a wall with its back to a blast stays dark. Particles skip it,
+// the backend handing a camera space draw a count of zero: the smoke takes the same lights on the
+// CPU already (W3DParticleSys.cpp).
+#define BLAST_LIGHT_SLOTS 32
+#define BLAST_LIGHT_SLOTS_TEXT "32"
+#define BLAST_LIGHT_FLOATS 12
+#define BLAST_LIGHT_SAMPLING \
+ "float3 blast_reaching(float4 position)\n" \
+ "{\n" \
+ " float2 ndc = float2(position.x * ShadowViewport.x * 2.0 - 1.0,\n" \
+ " 1.0 - position.y * ShadowViewport.y * 2.0);\n" \
+ " float4 world = mul(float4(ndc, position.z, 1.0), WorldFromClip);\n" \
+ " world.xyz /= world.w;\n" \
+ " float4 eye = mul(float4(ndc, 0.0, 1.0), WorldFromClip);\n" \
+ " eye.xyz /= eye.w;\n" \
+ " float3 facing = cross(ddx(world.xyz), ddy(world.xyz));\n" \
+ " float3 surface = facing * rsqrt(max(dot(facing, facing), 1e-20));\n" \
+ " if (dot(surface, eye.xyz - world.xyz) < 0.0) surface = -surface;\n" \
+ " float3 light = float3(0.0, 0.0, 0.0);\n" \
+ " int count = (int)BlastLightParameters.x;\n" \
+ " [loop] for (int i = 0; i < count; ++i) {\n" \
+ " float3 to = BlastLightPosition[i].xyz - world.xyz;\n" \
+ " float dist = length(to);\n" \
+ " float reach = BlastLightPosition[i].w;\n" \
+ " if (dist >= reach) continue;\n" \
+ " float inner = BlastLightDiffuse[i].w;\n" \
+ " float fall = saturate(1.0 - (dist - inner) / max(reach - inner, 0.001));\n" \
+ " float shade = saturate(dot(surface, to / max(dist, 0.001)));\n" \
+ " light += fall * (shade * BlastLightDiffuse[i].rgb + BlastLightAmbient[i].rgb);\n" \
+ " }\n" \
+ " return light;\n" \
+ "}\n" \
+ "\n"
+
+// A particle sprite fading out where it meets what is drawn behind it. The scene's depth is a copy
+// taken after the opaque world was drawn (DX11BackendClass::Soft_Particle_Depth), read texel for
+// texel at the pixel's own position, so the viewport's half pixel shift applies to both alike. Both
+// depths go back to the distance from the eye through the projection the particles are drawn with,
+// w = SoftParticleDepth.x / (z * SoftParticleDepth.y - SoftParticleDepth.z), which is the same
+// matrix the world was drawn with; .w is one over the distance the fade takes, in world units.
+#define SOFT_PARTICLE_SAMPLING \
+ "Texture2D SceneDepth : register(t7);\n" \
+ "\n" \
+ "float soft_particle_fade(float4 position)\n" \
+ "{\n" \
+ " float scene = SceneDepth.Load(int3(position.xy, 0)).r;\n" \
+ " float behind = SoftParticleDepth.x / (scene * SoftParticleDepth.y - SoftParticleDepth.z);\n" \
+ " float here = SoftParticleDepth.x / (position.z * SoftParticleDepth.y - SoftParticleDepth.z);\n" \
+ " return saturate((behind - here) * SoftParticleDepth.w);\n" \
+ "}\n" \
+ "\n"
+
// The constant block's fields for it, declared after SkyUp: DX11BackendClass::PixelConstantBlock.
+// The soft particles' field closes it, so a program that fades declares the whole block.
#define VOLUMETRIC_CONSTANTS \
" float4 VolumeParameters;\n" \
" row_major float4x4 WorldFromClip;\n" \
" float4 HeadlightParameters;\n" \
" float4 HeadlightPosition[" HEADLIGHT_SLOTS_TEXT "];\n" \
- " float4 HeadlightDirection[" HEADLIGHT_SLOTS_TEXT "];\n"
+ " float4 HeadlightDirection[" HEADLIGHT_SLOTS_TEXT "];\n" \
+ " float4 BlastLightParameters;\n" \
+ " float4 BlastLightPosition[" BLAST_LIGHT_SLOTS_TEXT "];\n" \
+ " float4 BlastLightDiffuse[" BLAST_LIGHT_SLOTS_TEXT "];\n" \
+ " float4 BlastLightAmbient[" BLAST_LIGHT_SLOTS_TEXT "];\n" \
+ " float4 SoftParticleDepth;\n"
// SHADOW_APPLY with the smoke in it. A particle takes the shade whatever its own brightness: the
// threshold is there for ground under the shroud, and on smoke it tied the shade to the colour, so
// the fire's glow, lifting a dark plume over the threshold, made it take more shade and go darker.
-// The headlights come after the shade: a shadow is the sun's, and a lamp shines into it.
+// The headlights and the blasts come after the shade: a shadow is the sun's, and a lamp shines into
+// it.
#define VOLUMETRIC_SHADOW_APPLY SHADOW_LIT \
" if (VolumeParameters.z > 0.5) shadow_lit = 1.0;\n" \
" current.rgb *= lerp(1.0, light_reaching(input.Position), shadow_lit);\n" \
- " current.rgb = saturate(current.rgb * (1.0 + headlight_reaching(input.Position)));\n"
+ " float3 lamp_gain = headlight_reaching(input.Position) + blast_reaching(input.Position);\n" \
+ " current.rgb = saturate(current.rgb * (1.0 + lamp_gain));\n"
// The HLSL for one description, or false when the description names an operation or an argument
// this does not generate. A refusal is not a failure: the caller keeps the fixed-function path for
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/sortingrenderer.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/sortingrenderer.cpp
index bcc608b19..24970ef7f 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/sortingrenderer.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/sortingrenderer.cpp
@@ -698,9 +698,13 @@ static void Flush_Sorting_Batch(const TempIndexStruct* entries,unsigned entry_co
if (entry_node!=node_id) {
SortingNodeStruct* state=overlapping_nodes[node_id];
SortingNodeStruct* next=overlapping_nodes[entry_node];
- if (state->glow!=next->glow || !Same_Draw_State(state->sorting_state,next->sorting_state)) {
+ // A run of billboards fades into the depth behind it and a run of meshes does not, so the
+ // two never share a draw even in one state.
+ if (state->glow!=next->glow || state->quads!=next->quads ||
+ !Same_Draw_State(state->sorting_state,next->sorting_state)) {
Apply_Render_State(state->sorting_state);
Direct3D11_Set_Smoke_Glow(state->glow);
+ Direct3D11_Set_Soft_Particles(state->quads);
DX8Wrapper::Draw_Triangles(run_first_index,run_triangles,run_first_vertex,vertex_cursor-run_first_vertex);
run_first_index=index_cursor;
run_first_vertex=vertex_cursor;
@@ -715,8 +719,10 @@ static void Flush_Sorting_Batch(const TempIndexStruct* entries,unsigned entry_co
Apply_Render_State(overlapping_nodes[node_id]->sorting_state);
Direct3D11_Set_Smoke_Glow(overlapping_nodes[node_id]->glow);
+ Direct3D11_Set_Soft_Particles(overlapping_nodes[node_id]->quads);
DX8Wrapper::Draw_Triangles(run_first_index,run_triangles,run_first_vertex,vertex_cursor-run_first_vertex);
Direct3D11_Set_Smoke_Glow(false);
+ Direct3D11_Set_Soft_Particles(false);
tDrawEnd = Clock_Ticks();
flush_profile_draw_ms += flushProfileElapsedMS(tCopyEnd, tDrawEnd);
@@ -738,6 +744,16 @@ void SortingRendererClass::Flush_Sorting_Pool()
SNAPSHOT_SAY(("SortingSystem - Flush \n"));
+ // Before any run resolves its program: the soft particles fade into this copy of the depth, and
+ // a sorted mesh writing depth between two of their runs costs no second one. A pool with no
+ // billboards in it takes none.
+ for (unsigned node=0;node<overlapping_node_count;++node) {
+ if (overlapping_nodes[node]->quads) {
+ Direct3D11_Take_Scene_Depth();
+ break;
+ }
+ }
+
flush_profile_entries += overlapping_entry_count;
long long tSortStart, tSortEnd;
tSortStart = Clock_Ticks();
diff --git a/GeneralsMD/Code/Tests/test_ffshadercompile.cpp b/GeneralsMD/Code/Tests/test_ffshadercompile.cpp
index 74c5a9069..8d025ac0c 100644
--- a/GeneralsMD/Code/Tests/test_ffshadercompile.cpp
+++ b/GeneralsMD/Code/Tests/test_ffshadercompile.cpp
@@ -277,3 +277,54 @@ TEST(ffshadercompile_the_smoke_glow_is_added_after_the_shade)
}
CHECK(compiles(compile, eleven, D3D11_PROFILE));
}
+
+// A particle sprite fades into the scene's depth at t7 on Direct3D 11, after the alpha test so its
+// edge is not clipped hard again. An additive sprite takes no shadow and still has to declare the
+// whole constant block, whose last field the fade reads; a smoke sprite takes both. The receiving
+// programs carry the blast lights after the shade. SDL3 and D3D9 get neither.
+TEST(ffshadercompile_a_soft_particle_fades_into_the_scene_depth_on_d3d11_only)
+{
+ CombinerDescription description;
+ memset(&description, 0, sizeof(description));
+ description.StageCount = 1;
+ description.Stages[0] = one_stage(D3DTOP_MODULATE, D3DTA_TEXTURE, D3DTA_DIFFUSE,
+ D3DTOP_MODULATE, D3DTA_TEXTURE, D3DTA_DIFFUSE, 0, true);
+ description.PixelPipeline.AlphaTestEnabled = true;
+ description.PixelPipeline.AlphaFunction = D3DCMP_GREATEREQUAL;
+ const std::string hard_key = CombinerShader_Key(description);
+
+ description.SoftParticle = SOFT_PARTICLE_COLOUR;
+ std::string additive;
+ CHECK(CombinerShader_Generate(description, COMBINER_SHADER_TARGET_D3D11, additive));
+ CHECK(additive.find("register(t7)") != std::string::npos);
+ CHECK(additive.find("SoftParticleDepth") > additive.find("BlastLightAmbient"));
+ CHECK(additive.find("register(t5)") == std::string::npos);
+ const size_t clip = additive.find("clip(");
+ const size_t fade = additive.find("current *= soft_particle_fade(input.Position);");
+ CHECK(clip != std::string::npos);
+ CHECK(fade != std::string::npos);
+ CHECK(clip < fade);
+ CHECK(CombinerShader_Key(description) == hard_key + ":Z2");
+
+ std::string sdl;
+ std::string nine;
+ CHECK(CombinerShader_Generate(description, COMBINER_SHADER_TARGET_SDL3_GPU, sdl));
+ CHECK(CombinerShader_Generate(description, COMBINER_SHADER_TARGET_D3D9, nine));
+ CHECK(sdl.find("SceneDepth") == std::string::npos);
+ CHECK(nine.find("SceneDepth") == std::string::npos);
+
+ description.ShadowReceiving = true;
+ description.SoftParticle = SOFT_PARTICLE_ALPHA;
+ std::string smoke;
+ CHECK(CombinerShader_Generate(description, COMBINER_SHADER_TARGET_D3D11, smoke));
+ CHECK(smoke.find("current.a *= soft_particle_fade(input.Position);") != std::string::npos);
+ CHECK(smoke.find("blast_reaching(input.Position)") > smoke.find("light_reaching(input.Position)"));
+
+ D3DCompileFunction compile = load_compiler();
+ if (compile == NULL) {
+ printf(" d3dcompiler_47.dll not present, skipping the compile\n");
+ return;
+ }
+ CHECK(compiles(compile, additive, D3D11_PROFILE));
+ CHECK(compiles(compile, smoke, D3D11_PROFILE));
+}