Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(dx11): glow only on fire and lasers, kept through heat haze
Olcay Seygan491331378a65
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 866bc76b0..45979cd09 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -416,7 +416,7 @@ found and fixed — EA's own, not port damage.**- The game draws its picture through Direct3D 11 now, and falls back to the old renderer on a machine that cannot make a Direct3D 11 device. Eight views over five maps come out within one step of one colour channel of the old picture, which is the smallest difference a screen can show.- The Direct3D 11 picture comes with its finishing passes on, no switch needed. Explosions glow past their own edges, because the battlefield is kept brighter than white until the glow is worked out; jagged edges are smoothed; and a light sharpening brings the 2003 textures back up after the smoothing. All three run over the battlefield only, before the health bars and the command bar go on top, so the lettering is untouched. In a screen of thirty inferno cannons firing the frame averaged 11.89ms with them against 11.82ms without. The antialiasing setting on the display page is for the old renderer; `-dx11post off` on the command line gives you the plain picture, and `-dx11post fxaa` picks effects one by one. The old renderer's rolling-wave sea never went missing in a real match either: both of the game's settings files turn it off, so no map ever drew it.-- Glow on the Graphics page lights the fight now. It used to change next to nothing you could see, because the game's paint stops at white and the glow only picked up what was brighter than that. Fire, explosions, muzzle flashes and laser beams now burn past white where they land, and their light spills onto the ground and the tanks around them. Off, Low, Medium, High and Ultra set how hot and how far, and a new install starts on Medium. In one frame of a tank fight the share of the battle burning white went from 6% with the glow off to 19% on Ultra. Buildings, sky, snow and pale sand never glow at any step: a snowed-in American base with nothing firing comes out exactly as bright on Ultra as on Off. Ultra cost nothing we could measure, 4.5 to 4.8ms a frame against 4.8ms off in a screen of thirty Inferno Cannons. The old Direct3D 9 renderer gets a softer version, where only the white-hot middle of a fire or a beam blooms. The second glow box, the brightness the glow started at, is gone, and a level you saved before keeps its name.+- Glow on the Graphics page lights the fight now. It used to change next to nothing you could see, because the game's paint stops at white and the glow only picked up what was brighter than that. Fire, explosions, muzzle flashes and laser beams now burn past white where they land, and their light spills onto the ground and the tanks around them. Off, Low, Medium, High and Ultra set how hot and how far, and a new install starts on Medium. In one frame of a tank fight the share of the battle burning white went from 5% with the glow off to 19% on Ultra. Buildings, sky, snow and pale sand never glow at any step: a snowed-in American base with nothing firing comes out exactly as bright on Ultra as on Off. Ultra cost nothing we could measure, 4.5 to 4.8ms a frame against 4.8ms off in a screen of thirty Inferno Cannons. The old Direct3D 9 renderer gets a softer version, where only the white-hot middle of a fire or a beam blooms. Water and the markers drawn on the ground keep the colour they were painted, and a Microwave Tank's heat haze no longer puts every fire on screen out while it fires. Classic Graphics keeps 2003's look, so there only the laser halos follow the setting. The second glow box, the brightness the glow started at, is gone, and a level you saved before keeps its name.- Fullscreen gets the Direct3D 11 picture too. The old renderer took the whole display for itself and refused the new one its window, so every fullscreen game quietly ran on Direct3D 9, and the DX9 in the corner readout was the only sign. Fullscreen is now the game's window over the monitor: alt-tab drops you on the desktop at its own brightness, and coming back puts the game's on again. A 1280x720 match on a 1920x1080 screen drew 40.7 million times without one draw refused, before and after an alt-tab.- Moving textures move in the Direct3D 11 picture. Fountain water and tank treads stood still in it, and the fountains came out a dull grey-cyan where the old renderer drew them bright. They match the old renderer now.- The game fits the screen on a PC with Windows display scaling turned up. The game never told Windows it counts real pixels, so at 125% Windows blew the whole window up by a quarter: a 1920x1080 game on a 1920x1080 screen came out 2400x1350 and the right and bottom of it hung off the edge. Now the window is the size you picked.diff --git a/GeneralsMD/Code/Data/German/Generals.str b/GeneralsMD/Code/Data/German/Generals.strindex 36006498a..12cc9f9ae 100644--- a/GeneralsMD/Code/Data/German/Generals.str+++ b/GeneralsMD/Code/Data/German/Generals.str@@ -14606,7 +14606,7 @@ GUI:Bloom4ENDTOOLTIP:Bloom-"Wie stark Feuer, Explosionen, Mündungsfeuer und Laser leuchten und wie weit ihr Licht auf die Umgebung übergreift. Gebäude, Himmel, Schnee und Sand leuchten nie. Aus ist das, wofür die Grafiken gemalt wurden. Der Direct3D-9-Renderer zeigt eine weichere Fassung."+"Wie stark Feuer, Explosionen, Mündungsfeuer und Laser leuchten und wie weit ihr Licht auf die Umgebung übergreift. Gebäude, Himmel, Schnee und Sand leuchten nie. Aus ist das, wofür die Grafiken gemalt wurden. Der Direct3D-9-Renderer zeigt eine weichere Fassung, und mit Klassischer Grafik richten sich nur die Laserschimmer danach."ENDGUI:VSyncdiff --git a/GeneralsMD/Code/Data/Patch.str b/GeneralsMD/Code/Data/Patch.strindex e891b7a53..b2ce1a7de 100644--- a/GeneralsMD/Code/Data/Patch.str+++ b/GeneralsMD/Code/Data/Patch.str@@ -774,7 +774,7 @@ GUI:Bloom4ENDTOOLTIP:Bloom-"How hard fire, explosions, muzzle flashes and lasers burn and how far their light spills over what is around them. Buildings, the sky, snow and sand never glow. Off is the look the artwork was painted for. The Direct3D 9 renderer gets a softer version."+"How hard fire, explosions, muzzle flashes and lasers burn and how far their light spills over what is around them. Buildings, the sky, snow and sand never glow. Off is the look the artwork was painted for. The Direct3D 9 renderer gets a softer version, and under Classic Graphics only the laser halos follow it."ENDGUI:VSyncdiff --git a/GeneralsMD/Code/Data/Scenarios/glowmicrowave.txt b/GeneralsMD/Code/Data/Scenarios/glowmicrowave.txtnew file mode 100644index 000000000..8338a1ff4--- /dev/null+++ b/GeneralsMD/Code/Data/Scenarios/glowmicrowave.txt@@ -0,0 +1,19 @@+# The Glow option under heat haze: three Microwave Tanks burning Red Guard while two Dragon Tanks+# flame two Crusaders beside them. The haze makes the engine draw the scene into its render+# texture, which on Direct3D 11 used to clip every fire at white for as long as a Microwave Tank+# fired. Draws, so -win:+#+# generals.exe -win -xres 1920 -yres 1080 -msaa 0 -quickstart -noshellmap -observer -multiInstance+# -fps 120 -noFPSLimit -particlecap 2500 -map "Maps\Twilight Flame\Twilight Flame.map"+# -autoskirmish 2 -takeover -side 0 FactionAmerica -side 1 FactionChina -seed 7+# -scenario glowmicrowave -cinema nohud -freecam 3150 3660 420 45 -40 -screenshot 172 -maxframes 182+#+# with Bloom = 4 (Ultra) in the Options.ini of a ZH_USER_DATA_DIR of its own.++30 spawn 0 AmericaTankMicrowave 3 3340 3880 30+30 spawn 1 ChinaInfantryRedguard 8 3270 3970 15+60 attack 0 AmericaTankMicrowave 1 ChinaInfantryRedguard++30 spawn 0 AmericaTankCrusader 2 3440 3800 30+30 spawn 1 ChinaTankDragon 2 3450 3900 30+60 attack 1 ChinaTankDragon 0 AmericaTankCrusaderdiff --git a/GeneralsMD/Code/Data/Turkish/Generals.str b/GeneralsMD/Code/Data/Turkish/Generals.strindex def309534..5c04c66e1 100644--- a/GeneralsMD/Code/Data/Turkish/Generals.str+++ b/GeneralsMD/Code/Data/Turkish/Generals.str@@ -15262,7 +15262,7 @@ GUI:Bloom4ENDTOOLTIP:Bloom-"Ateşin, patlamaların, namlu alevlerinin ve lazerlerin ne kadar parlak yandığı ve ışıklarının çevreye ne kadar taştığı. Binalar, gökyüzü, kar ve kum hiçbir zaman parlamaz. Çizimler Kapalı ayarına göre yapıldı. Direct3D 9 çizicisi bunun daha yumuşak bir sürümünü gösterir."+"Ateşin, patlamaların, namlu alevlerinin ve lazerlerin ne kadar parlak yandığı ve ışıklarının çevreye ne kadar taştığı. Binalar, gökyüzü, kar ve kum hiçbir zaman parlamaz. Çizimler Kapalı ayarına göre yapıldı. Direct3D 9 çizicisi bunun daha yumuşak bir sürümünü gösterir; Klasik Grafikler açıkken bu ayara yalnızca lazer haleleri uyar."ENDGUI:VSyncdiff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DLaserDraw.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DLaserDraw.cppindex c97e81514..5886ddf84 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DLaserDraw.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DLaserDraw.cpp@@ -71,6 +71,7 @@static const Real GLOW_HALO_WIDTH_SCALE = 2.8f; // halo width over the beam's widest layerstatic const Real GLOW_HALO_MIN_WIDTH = 16.0f;static const Real GLOW_HALO_INTENSITY = 0.75f;+static const Real GLOW_HALO_SPREAD = 0.5f; // halo width gained per unit of strength past Mediumstatic const Real GLOW_CORE_WIDTH_SCALE = 0.3f;static const Real GLOW_CORE_MIN_WIDTH = 3.0f;static const Real GLOW_CORE_WHITEN = 0.7f; // how far the core's hue is run toward white@@ -159,13 +160,23 @@ static Vector3 glowHue( Color inner, Color outer )}// What the Glow option makes of the two glow lines: nothing at Off, which leaves EA's beams alone,-// and the look they were tuned at for Medium, twice it at Ultra. A beam is a few pixels wide, so-// unlike a fireball it needs its own brightness raised to get past white and bloom.+// and the look they were tuned at for Medium, twice it at Ultra. A line's colour goes to the device+// as eight bits a channel and wraps past one, so the brightness stops at Medium's; past it the halo+// widens instead, and the Direct3D 11 gain takes the lines over white (GLOW_ENABLE below).static Real glowStrength(){return TheGlobalData->m_bloomIntensity / 50.0f;}+// The plain additive preset, opted into the Glow option's gain: EA's beam layers and the glow+// lines alike are light.+static ShaderClass glowingAdditiveShader()+{+ ShaderClass shader = ShaderClass::_PresetAdditiveShader;+ shader.Set_Glow( ShaderClass::GLOW_ENABLE );+ return shader;+}+// PUBLIC FUNCTIONS /////////////////////////////////////////////////////////////////////////////////-------------------------------------------------------------------------------------------------@@ -304,7 +315,7 @@ W3DLaserDraw::W3DLaserDraw( Thing *thing, const ModuleData* moduleData ) :if( line ){line->Set_Texture( m_texture );- line->Set_Shader( ShaderClass::_PresetAdditiveShader ); //pick the alpha blending mode you want - see shader.h for others.+ line->Set_Shader( glowingAdditiveShader() ); //pick the alpha blending mode you want - see shader.h for others.line->Set_Width( width );line->Set_Color( Vector3( red, green, blue ) );line->Set_UV_Offset_Rate( Vector2(0.0f, data->m_scrollRate) ); //amount to scroll texture on each draw@@ -337,7 +348,7 @@ W3DLaserDraw::W3DLaserDraw( Thing *thing, const ModuleData* moduleData ) :SegmentedLineClass *line = NEW SegmentedLineClass;m_glow3D[ g ] = line;line->Set_Texture( glowTexture );- line->Set_Shader( ShaderClass::_PresetAdditiveShader );+ line->Set_Shader( glowingAdditiveShader() );line->Set_Texture_Mapping_Mode( SegLineRendererClass::UNIFORM_WIDTH_TEXTURE_MAP ); // u runs across the widthW3DDisplay::m_3DScene->Add_Render_Object( line );line->Set_Visible( 0 );@@ -565,22 +576,25 @@ void W3DLaserDraw::doDrawModule(const Matrix3D* transformMtx)if( m_glow3D ){+ // set with the points rather than once, so a change of the Glow option reaches the+ // next beam drawn+ const Real strength = glowStrength();+ const Real brightness = MIN( strength, 1.0f );+ const Real spread = 1.0f + GLOW_HALO_SPREAD * MAX( strength - 1.0f, 0.0f );Real widest = MAX( data->m_innerBeamWidth, data->m_outerBeamWidth );Real widthScale = update->getWidthScale();Real widths[ GLOW_LAYERS ] ={- MAX( widest * GLOW_HALO_WIDTH_SCALE, GLOW_HALO_MIN_WIDTH ) * widthScale,+ MAX( widest * GLOW_HALO_WIDTH_SCALE, GLOW_HALO_MIN_WIDTH ) * widthScale * spread,MAX( widest * GLOW_CORE_WIDTH_SCALE, GLOW_CORE_MIN_WIDTH ) * widthScale};- // set with the points rather than once, so a change of the Glow option reaches the- // next beam drawn- const Real strength = glowStrength();+ // glowHue's brightest channel is one, so neither colour passes oneVector3 hue = glowHue( data->m_innerColor, data->m_outerColor );Vector3 white( 1.0f, 1.0f, 1.0f );Vector3 colors[ GLOW_LAYERS ] ={- hue * ( GLOW_HALO_INTENSITY * strength ),- ( hue * ( 1.0f - GLOW_CORE_WHITEN ) + white * GLOW_CORE_WHITEN ) * strength+ hue * ( GLOW_HALO_INTENSITY * brightness ),+ ( hue * ( 1.0f - GLOW_CORE_WHITEN ) + white * GLOW_CORE_WHITEN ) * brightness};for( Int g = 0; g < GLOW_LAYERS; g++ ){diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cppindex 3c30756f4..ba5428d43 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cpp@@ -369,10 +369,14 @@ static void renderBloom(IDirect3DTexture9 *sceneTexture, Real x, Real y, Real w,DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLOROP, D3DTOP_MODULATE4X);DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLORARG1, D3DTA_CURRENT);DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLORARG2, D3DTA_DIFFUSE);+ //a stage with its colour on may not have its alpha off: D3D9 leaves that undefined+ DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);+ DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAARG1, D3DTA_CURRENT);DX8Wrapper::Set_DX8_Texture(0, sceneTexture);DX8Wrapper::Set_DX8_Texture(1, NULL);drawBloomQuad(0.0f, 0.0f, (Real)s_bloomWidth, (Real)s_bloomHeight, u0, v0, u1, v1, 0xffffffff);DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_COLOROP, D3DTOP_DISABLE);+ DX8Wrapper::Set_DX8_Texture_Stage_State(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);//Blur: two 4-tap box passes ping-ponging between the targets, the second one twice as wide.//Each tap is a quarter weight carried in the diffuse colour, the first writing and the other@@ -462,6 +466,9 @@ Bool ScreenDefaultFilter::postRender(enum FilterModes mode, Coord2D &scrollDeltaIDirect3DTexture9 * tex = W3DShaderManager::endRenderToTexture();DEBUG_ASSERTCRASH(tex, ("Require rendered texture."));if (!tex) return false;+ // Direct3D 11 never left its own scene target (Direct3D11_Set_Scene_Stand_In): there is nothing+ // to copy back, and the copy would clip the Glow option's fires at white.+ if (Direct3D11_Is_Active()) return true;if (!set(mode)) return false;@@ -2928,6 +2935,9 @@ void W3DShaderManager::init(void)}}}+ // Direct3D 11 keeps the scene in its own float target while this texture is the target, so the+ // smudges' redirect does not clip the Glow option's fires at white.+ Direct3D11_Set_Scene_Stand_In(m_renderTexture, m_newRenderSurface);W3DShaderInterface **shaders;@@ -2960,6 +2970,7 @@ void W3DShaderManager::init(void)//=============================================================================void W3DShaderManager::shutdown(void){+ Direct3D11_Set_Scene_Stand_In(NULL, NULL);if (m_newRenderSurface) m_newRenderSurface->Release();if (m_renderTexture) m_renderTexture->Release();if (m_oldRenderSurface) m_oldRenderSurface->Release();diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cppindex 7112044c4..1cb3259af 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp@@ -245,6 +245,8 @@ DX11BackendClass::DX11BackendClass(), ShadowReceiving(false), SmokeGlow(false), AdditiveGain(1.0f)+ , GlowDraw(false)+ , SceneStandIn(NULL), DrawsMade(0), DrawsRefused(0), RefusedNoBuffer(0)@@ -458,6 +460,7 @@ void DX11BackendClass::Shutdown()TargetCopyView = NULL;TargetCopy = NULL;}+ SceneStandIn = NULL;if (ShadowMapTexture != NULL) {ShadowMapTexture->Release();ShadowMapTexture = NULL;@@ -961,13 +964,13 @@ void DX11BackendClass::Set_Additive_Gain(float gain)}}-/** An additive draw into the scene, the one blend the Glow option boosts. ONE, ONE is what the- engine's additive presets set (fire and explosion particles, the laser lines, muzzle flash and- glow meshes); a source alpha blend into ONE is left alone, since the blend factor would replace- the alpha it weighs by. */+/** An additive draw into the scene that opted in, the one blend the Glow option boosts: fire and+ explosion particles, laser lines, and the additive meshes the art ships (muzzle flashes, glows).+ A source alpha blend into ONE is left alone, since the blend factor would replace the alpha it+ weighs by. */bool DX11BackendClass::Additive_Glow() const{- return AdditiveGain != 1.0f && CurrentTarget == NULL && !ShadowMapBound+ return AdditiveGain != 1.0f && GlowDraw && CurrentTarget == NULL && !ShadowMapBound&& (VertexFormat & D3DFVF_XYZRHW) == 0&& RenderStates.Get_Render_State(D3DRS_ALPHABLENDENABLE) != FALSE&& RenderStates.Get_Render_State(D3DRS_SRCBLEND) == D3DBLEND_ONE@@ -1116,6 +1119,9 @@ void DX11BackendClass::Set_Blast_Lights(const float * lights, unsigned count)bool DX11BackendClass::Views_Current_Target(unsigned stage, ID3D11ShaderResourceView * texture) const{+ if (texture != NULL && texture == SceneStandIn) {+ return true;+ }if (texture == NULL || CurrentTarget == NULL) {return false;}@@ -1311,7 +1317,8 @@ void DX11BackendClass::Set_Texture(unsigned stage, ID3D11ShaderResourceView * te// Sampling the current target takes a draw off the shadow receivers (Shadow_Receiving), so a// change into or out of that is a change of pipeline. The old view's answer is in the cache// when it was drawn with; when it is not, it is taken as yes.- else if (texture != Textures[stage] && CurrentTarget != NULL) {+ else if (texture != Textures[stage] && (CurrentTarget != NULL || texture == SceneStandIn+ || Textures[stage] == SceneStandIn)) {const bool was_target = (TargetCheckedViews[stage] == Textures[stage])? TargetCheckedIsTarget[stage] : true;if (was_target || Views_Current_Target(stage, texture)) {@@ -2758,6 +2765,10 @@ ID3D11ShaderResourceView * DX11BackendClass::Readable_Texture(unsigned stage,return texture;}ID3D11Resource * resource = CurrentTargetResource;+ if (texture == SceneStandIn) {+ Device->Get_Scene_View()->GetResource(&resource);+ resource->Release(); // the device holds the scene target for as long as it lives+ }// Set_Render_Target keeps only a target that is a two dimensional texture.D3D11_TEXTURE2D_DESC description;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.hindex dba9fb9d0..c3c7b6aaf 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.h@@ -243,6 +243,17 @@ public:void Set_Additive_Gain(float gain);float Additive_Gain() const { return AdditiveGain; }+ // Whether the draws that follow opted into that gain (ShaderClass::GLOW_ENABLE, sent by+ // ShaderClass::Apply). Additive water, waypoint lines, decals and heat vision are ONE, ONE as+ // well and stay as they were painted.+ void Set_Glow_Draw(bool glow) { GlowDraw = glow; }++ // The texture the engine draws the scene into for its screen filters (W3DShaderManager's render+ // texture). Here the scene stays in the scene target, where the gain and the bloom work: a+ // redirect into this texture's 8-bit twin would clip every fire at white. A draw that samples+ // the view samples a copy of the scene instead (Readable_Texture). NULL when there is none.+ void Set_Scene_Stand_In(ID3D11ShaderResourceView * view) { SceneStandIn = view; }+// What is in the map, read back through a staging copy: how much of it was drawn into and how// near the nearest thing is. A caster pass that drew nothing leaves a map that is all one// value, and no draw count tells that apart from a pass that drew the world.@@ -484,7 +495,9 @@ private:bool SmokeGlow;bool Smoke_Glow() const;float AdditiveGain;+ bool GlowDraw;bool Additive_Glow() const;+ ID3D11ShaderResourceView * SceneStandIn;// A stage samples the target the draw is going into: the heat haze, which bends a picture that// already took its shadows, and would take them a second time.bool Samples_Current_Target() const;diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cppindex bd55a9ca1..29e404aa2 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.cpp@@ -244,6 +244,10 @@ void Direct3D11_Mark_Surface_Dirty(struct IDirect3DSurface9 * surface)DX11Texture_Mark_Dirty(surface);}+// Direct3D11_Set_Scene_Stand_In's texture and surface.+static struct IDirect3DBaseTexture9 * SceneStandInTexture = NULL;+static struct IDirect3DSurface9 * SceneStandInSurface = NULL;+void Direct3D11_Mirror_Texture(unsigned stage, struct IDirect3DBaseTexture9 * texture){if (!Active) {@@ -252,6 +256,9 @@ void Direct3D11_Mirror_Texture(unsigned stage, struct IDirect3DBaseTexture9 * teID3D11ShaderResourceView * view = texture == NULL? NULL: DX11Texture_Mirror(Device.Get_Device(), Device.Get_Context(), texture);+ if (texture != NULL && texture == SceneStandInTexture) {+ Backend.Set_Scene_Stand_In(view);+ }Backend.Set_Texture(stage, view);Backend.Set_Texture_Missing(stage, texture != NULL && view == NULL);}@@ -340,6 +347,23 @@ void Direct3D11_Set_Smoke_Glow(bool glow)}}+void Direct3D11_Set_Glow_Draw(bool glow)+{+ if (Active) {+ Backend.Set_Glow_Draw(glow);+ }+}++void Direct3D11_Set_Scene_Stand_In(struct IDirect3DBaseTexture9 * texture,+ struct IDirect3DSurface9 * surface)+{+ SceneStandInTexture = texture;+ SceneStandInSurface = surface;+ if (Active && texture == NULL) {+ Backend.Set_Scene_Stand_In(NULL);+ }+}+void Direct3D11_Clear_Shadow_Parameters(){if (Active) {@@ -357,8 +381,10 @@ void Direct3D11_Mirror_Render_Target(struct IDirect3DSurface9 * surface)if (!Active) {return;}- Backend.Set_Render_Target(DX11Texture_Target(Device.Get_Device(), Device.Get_Context(),- surface));+ // The scene's stand-in is not drawn into here: the scene stays where the gain and the bloom are.+ Backend.Set_Render_Target((surface != NULL && surface == SceneStandInSurface)+ ? NULL+ : DX11Texture_Target(Device.Get_Device(), Device.Get_Context(), surface));}void Direct3D11_Mirror_Surface_Copy(struct IDirect3DSurface9 * destination,diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.hindex c5ff6a5af..40311be71 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime.h@@ -171,6 +171,17 @@ void Direct3D11_Set_Scene_View(const float view[16]);// (DX11BackendClass::Set_Smoke_Glow). The sorting pool sets it around the smoke billboards' runs.void Direct3D11_Set_Smoke_Glow(bool glow);+// Whether the draws that follow take the Glow option's additive gain (DX11BackendClass::+// Set_Glow_Draw). ShaderClass::Apply sends it from the shader's GLOW_ENABLE bit.+void Direct3D11_Set_Glow_Draw(bool glow);++// The texture and its level-zero surface the engine redirects the scene into for its screen+// filters (W3DShaderManager's render texture), or both NULL. The Direct3D 11 frame keeps the scene+// in its own target while that surface is bound, and a draw sampling the texture samples a copy of+// the scene (DX11BackendClass::Set_Scene_Stand_In).+void Direct3D11_Set_Scene_Stand_In(struct IDirect3DBaseTexture9 * texture,+ struct IDirect3DSurface9 * surface);+// The CPU has just written this surface. The next bind of its texture fills the Direct3D 11 copy// again. A no-op when the backend is not running.void Direct3D11_Mark_Surface_Dirty(struct IDirect3DSurface9 * surface);diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cppindex 6189974c7..5d347ad0c 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11runtime_posix.cpp@@ -75,6 +75,8 @@ void Direct3D11_Clear_Shadow_Parameters() {}bool Direct3D11_Fill_Smoke_Map(const float *, unsigned, float) { return false; }void Direct3D11_Set_Scene_View(const float [16]) {}void Direct3D11_Set_Smoke_Glow(bool) {}+void Direct3D11_Set_Glow_Draw(bool) {}+void Direct3D11_Set_Scene_Stand_In(struct IDirect3DBaseTexture9 *, struct IDirect3DSurface9 *) {}void Direct3D11_Mark_Surface_Dirty(struct IDirect3DSurface9 *) {}void Direct3D11_Mirror_Render_Target(struct IDirect3DSurface9 *) {}diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/shader.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/shader.cppindex c98850fb5..f9871712c 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/shader.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/shader.cpp@@ -47,6 +47,7 @@#include "wwdebug.h"#include "dx8wrapper.h"#include "dx8caps.h"+#include "dx11runtime.h"bool ShaderClass::ShaderDirty=true;@@ -134,7 +135,9 @@ ShaderClass ShaderClass::_PresetAlpha2DShader(SC_ALPHA_2D);SRCBLEND_ONE, DSTBLEND_ONE, FOG_DISABLE, GRADIENT_DISABLE, SECONDARY_GRADIENT_DISABLE, \TEXTURING_ENABLE, ALPHATEST_DISABLE, CULL_MODE_ENABLE, \DETAILCOLOR_DISABLE, DETAILALPHA_DISABLE) )-ShaderClass ShaderClass::_PresetAdditiveSpriteShader(SC_ADD_SPRITE);+// Every additive particle, streak and projectile stream draws with this one, so it carries the+// Glow option's opt-in; the plain additive preset (water, waypoint lines, decals) does not.+ShaderClass ShaderClass::_PresetAdditiveSpriteShader(SC_ADD_SPRITE | (ShaderClass::GLOW_ENABLE << SHIFT_GLOW));// Texturing, default zbuffer reading, no zbuffer writing, no gradients,// alpha blending, no fogging - mostly for use in alpha-blended sprite@@ -428,6 +431,10 @@ void ShaderClass::Apply()CurrentShader=ShaderBits;ShaderDirty=false;++ if (diff & ShaderClass::MASK_GLOW)+ Direct3D11_Set_Glow_Draw(Get_Glow() == ShaderClass::GLOW_ENABLE);+// COLOR MASKif(diff & (ShaderClass::MASK_COLORMASK | ShaderClass::MASK_SRCBLEND | ShaderClass::MASK_DSTBLEND | ShaderClass::MASK_ALPHATEST))diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/shader.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/shader.hindex e8754cc65..55b3ad3c2 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/shader.h+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/shader.h@@ -73,7 +73,8 @@ enum ShaderShiftConstantsSHIFT_ALPHATEST = 18, // bit shift for alpha test settingSHIFT_CULLMODE = 19, // bit shift for cullmode settingSHIFT_POSTDETAILCOLORFUNC = 20, // bit shift for post-detail color function setting- SHIFT_POSTDETAILALPHAFUNC = 24 // bit shift for post-detail alpha function setting+ SHIFT_POSTDETAILALPHAFUNC = 24, // bit shift for post-detail alpha function setting+ SHIFT_GLOW = 27 // bit shift for the Glow option's opt-in (1 bit)};#define SHADE_CNST(depth_compare, depth_mask, color_mask, src_blend, dst_blend, fog, pri_grad, sec_grad, texture, alpha_test, cullmode, post_det_color, post_det_alpha) \@@ -170,6 +171,16 @@ public:NPATCH_TYPE_MAX};+ /** Whether an additive draw is a light the Glow option brightens: fire, explosions, muzzle+ flashes, laser beams. Only the Direct3D 11 frame reads it (DX11BackendClass::Additive_Glow);+ water, waypoint lines, decals and heat vision are additive too and leave it off. */+ enum GlowType+ {+ GLOW_DISABLE=0,+ GLOW_ENABLE,+ GLOW_MAX+ };+enum DstBlendFuncType{DSTBLEND_ZERO=0, // destination pixel doesn't affect blending (default)@@ -248,7 +259,8 @@ public:MASK_ALPHATEST = (1<<18), // mask for alpha test enableMASK_CULLMODE = (1<<19), // mask for cullmode settingMASK_POSTDETAILCOLORFUNC= (15<<20), // mask for post detail color function setting- MASK_POSTDETAILALPHAFUNC= (7<<24) // mask for post detail alpha function setting+ MASK_POSTDETAILALPHAFUNC= (7<<24), // mask for post detail alpha function setting+ MASK_GLOW = (1<<27) // mask for the Glow option's opt-in};ShaderClass(void)@@ -322,6 +334,7 @@ public:inline SrcBlendFuncType Get_Src_Blend_Func(void) const { return (SrcBlendFuncType)((ShaderBits&MASK_SRCBLEND)>>SHIFT_SRCBLEND); }inline TexturingType Get_Texturing(void) const { return (TexturingType)((ShaderBits&MASK_TEXTURING)>>SHIFT_TEXTURING); }inline NPatchEnableType Get_NPatch_Enable(void) const { return (NPatchEnableType)((ShaderBits&MASK_NPATCHENABLE)>>SHIFT_NPATCHENABLE); }+ inline GlowType Get_Glow(void) const { return (GlowType)((ShaderBits&MASK_GLOW)>>SHIFT_GLOW); }inline void Set_Depth_Compare(DepthCompareType x) { ShaderBits&=~MASK_DEPTHCOMPARE;ShaderBits|=(x<<SHIFT_DEPTHCOMPARE); }inline void Set_Depth_Mask(DepthMaskType x) { ShaderBits&=~MASK_DEPTHMASK; ShaderBits|=(x<<SHIFT_DEPTHMASK); }@@ -337,6 +350,7 @@ public:inline void Set_Src_Blend_Func(SrcBlendFuncType x) { ShaderBits&=~MASK_SRCBLEND;ShaderBits|=(x<<SHIFT_SRCBLEND); }inline void Set_Texturing(TexturingType x) { ShaderBits&=~MASK_TEXTURING; ShaderBits|=(x<<SHIFT_TEXTURING); }inline void Set_NPatch_Enable(NPatchEnableType x) { ShaderBits&=~MASK_NPATCHENABLE; ShaderBits|=(x<<SHIFT_NPATCHENABLE); }+ inline void Set_Glow(GlowType x) { ShaderBits&=~MASK_GLOW; ShaderBits|=(x<<SHIFT_GLOW); }void Init_From_Material3(const W3dMaterial3Struct & mat3);void Enable_Fog (const char *source);diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/w3d_util.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/w3d_util.cppindex 849febe34..9423bbdf1 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/w3d_util.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/w3d_util.cpp@@ -124,6 +124,12 @@ void W3dUtilityClass::Convert_Shader(const W3dShaderStruct & shader,ShaderClass// set->Set_Post_Detail_Alpha_Func ((ShaderClass::DetailAlphaFuncType) W3d_Shader_Get_Post_Detail_Alpha_Func (&shader));set->Set_Post_Detail_Color_Func ((ShaderClass::DetailColorFuncType) W3d_Shader_Get_Detail_Color_Func (&shader));set->Set_Post_Detail_Alpha_Func ((ShaderClass::DetailAlphaFuncType) W3d_Shader_Get_Detail_Alpha_Func (&shader));+ // An additive surface in the art is a light: a muzzle flash, a glow card, an explosion's shell.+ // It takes the Glow option's gain (ShaderClass::GLOW_ENABLE).+ if (set->Get_Src_Blend_Func() == ShaderClass::SRCBLEND_ONE+ && set->Get_Dst_Blend_Func() == ShaderClass::DSTBLEND_ONE) {+ set->Set_Glow(ShaderClass::GLOW_ENABLE);+ }}void W3dUtilityClass::Convert_Shader(const ShaderClass & shader,W3dShaderStruct * set)diff --git a/GeneralsMD/Code/Tests/test_dx11backend.cpp b/GeneralsMD/Code/Tests/test_dx11backend.cppindex c248fe8d9..a7a05a023 100644--- a/GeneralsMD/Code/Tests/test_dx11backend.cpp+++ b/GeneralsMD/Code/Tests/test_dx11backend.cpp@@ -511,16 +511,19 @@ TEST(dx11backend_an_additive_draw_takes_the_glow_gain_past_white)backend.Set_Indices(indices, DXGI_FORMAT_R16_UINT);backend.Set_Render_State(D3DRS_ALPHABLENDENABLE, TRUE);- // Blue of the centre pixel after one draw over a 0.25 clear, at this gain and this blend.- struct Case { float Gain; DWORD Source; DWORD Destination; float Blue; };+ // Blue of the centre pixel after one draw over a 0.25 clear, at this gain, this blend and with+ // or without the draw's opt-in (ShaderClass::GLOW_ENABLE).+ struct Case { float Gain; bool Glow; DWORD Source; DWORD Destination; float Blue; };const Case cases[] = {- { 4.0f, D3DBLEND_ONE, D3DBLEND_ONE, 0.25f + 0.2f * 4.0f },- { 1.0f, D3DBLEND_ONE, D3DBLEND_ONE, 0.25f + 0.2f },- { 4.0f, D3DBLEND_SRCALPHA, D3DBLEND_INVSRCALPHA, 0.2f },+ { 4.0f, true, D3DBLEND_ONE, D3DBLEND_ONE, 0.25f + 0.2f * 4.0f },+ { 1.0f, true, D3DBLEND_ONE, D3DBLEND_ONE, 0.25f + 0.2f },+ { 4.0f, true, D3DBLEND_SRCALPHA, D3DBLEND_INVSRCALPHA, 0.2f },+ { 4.0f, false, D3DBLEND_ONE, D3DBLEND_ONE, 0.25f + 0.2f },};for (unsigned index = 0; index < sizeof(cases) / sizeof(cases[0]); ++index) {const float clear_colour[4] = { 0.25f, 0.25f, 0.25f, 1.0f };device.Get_Context()->ClearRenderTargetView(target_view, clear_colour);+ backend.Set_Glow_Draw(cases[index].Glow);backend.Set_Additive_Gain(cases[index].Gain);backend.Set_Render_State(D3DRS_SRCBLEND, cases[index].Source);backend.Set_Render_State(D3DRS_DESTBLEND, cases[index].Destination);@@ -831,6 +834,98 @@ TEST(dx11backend_a_draw_that_samples_its_own_target_reads_what_was_drawn)backend.Shutdown();}+// W3DShaderManager's render texture stands in for the scene: Direct3D 11 never draws into it, so a+// draw sampling it (the heat haze) has to read the scene, where the frame really is. Without the+// stand-in it reads the texture's own black and the centre comes back 00 00 00.+TEST(dx11backend_a_draw_sampling_the_scene_stand_in_reads_the_scene)+{+ DX11DeviceClass device;+ CHECK(device.Create_Offscreen());+ ID3D11Device * d3d = device.Get_Device();++ DX11BackendClass backend;+ CHECK(backend.Initialise(&device));++ D3D11_TEXTURE2D_DESC description;+ memset(&description, 0, sizeof(description));+ description.Width = TARGET_SIZE;+ description.Height = TARGET_SIZE;+ description.MipLevels = 1;+ description.ArraySize = 1;+ description.Format = DXGI_FORMAT_B8G8R8A8_UNORM;+ description.SampleDesc.Count = 1;+ description.Usage = D3D11_USAGE_DEFAULT;+ description.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;++ ID3D11Texture2D * scene = NULL;+ CHECK(SUCCEEDED(d3d->CreateTexture2D(&description, NULL, &scene)));+ ID3D11Texture2D * stand_in = NULL;+ CHECK(SUCCEEDED(d3d->CreateTexture2D(&description, NULL, &stand_in)));+ D3D11_TEXTURE2D_DESC staging_description = description;+ staging_description.Usage = D3D11_USAGE_STAGING;+ staging_description.BindFlags = 0;+ staging_description.CPUAccessFlags = D3D11_CPU_ACCESS_READ;+ ID3D11Texture2D * staging = NULL;+ CHECK(SUCCEEDED(d3d->CreateTexture2D(&staging_description, NULL, &staging)));++ ID3D11RenderTargetView * scene_view = NULL;+ CHECK(SUCCEEDED(d3d->CreateRenderTargetView(scene, NULL, &scene_view)));+ ID3D11RenderTargetView * stand_in_target = NULL;+ CHECK(SUCCEEDED(d3d->CreateRenderTargetView(stand_in, NULL, &stand_in_target)));+ ID3D11ShaderResourceView * stand_in_texture = NULL;+ CHECK(SUCCEEDED(d3d->CreateShaderResourceView(stand_in, NULL, &stand_in_texture)));++ const float white[4] = { 1.0f, 1.0f, 1.0f, 1.0f };+ const float black[4] = { 0.0f, 0.0f, 0.0f, 1.0f };+ device.Get_Context()->ClearRenderTargetView(scene_view, white);+ device.Get_Context()->ClearRenderTargetView(stand_in_target, black);++ ID3D11Buffer * vertices = NULL;+ CHECK(DX11Resource_Create_Vertex_Buffer(d3d, sizeof(QUAD_VERTICES), D3DPOOL_MANAGED, 0,+ QUAD_VERTICES, &vertices));+ ID3D11Buffer * indices = NULL;+ CHECK(DX11Resource_Create_Index_Buffer(d3d, sizeof(QUAD_INDICES), D3DPOOL_MANAGED, 0,+ QUAD_INDICES, &indices));++ device.Set_Scene_View(scene_view);+ backend.Set_Scene_Stand_In(stand_in_texture);+ backend.Set_Render_Target(NULL);+ backend.Set_Viewport(0, 0, TARGET_SIZE, TARGET_SIZE); // an offscreen device has no size of its own+ configure_unlit_pass_through(backend);+ backend.Set_Texture_Stage_State(0, D3DTSS_COLOROP, D3DTOP_MODULATE);+ backend.Set_Texture_Stage_State(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);+ backend.Set_Texture_Stage_State(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);+ backend.Set_Texture(0, stand_in_texture);+ backend.Set_Stream_Source(vertices, sizeof(BackendVertex), 0);+ backend.Set_Indices(indices, DXGI_FORMAT_R16_UINT);+ CHECK(backend.Draw_Indexed_Triangles(6, 0, 0));++ device.Get_Context()->CopyResource(staging, scene);+ D3D11_MAPPED_SUBRESOURCE mapped;+ CHECK(SUCCEEDED(device.Get_Context()->Map(staging, 0, D3D11_MAP_READ, 0, &mapped)));+ const unsigned char * centre = static_cast<const unsigned char *>(mapped.pData)+ + mapped.RowPitch * (TARGET_SIZE / 2) + (TARGET_SIZE / 2) * 4;+ if (centre[0] != EXPECTED_BLUE) {+ printf(" centre pixel bgra %02x %02x %02x %02x\n",+ centre[0], centre[1], centre[2], centre[3]);+ }+ CHECK_EQ(centre[0], EXPECTED_BLUE);+ CHECK_EQ(centre[1], EXPECTED_GREEN);+ CHECK_EQ(centre[2], EXPECTED_RED);+ device.Get_Context()->Unmap(staging, 0);++ device.Set_Scene_View(NULL);+ indices->Release();+ vertices->Release();+ stand_in_texture->Release();+ stand_in_target->Release();+ scene_view->Release();+ staging->Release();+ stand_in->Release();+ scene->Release();+ backend.Shutdown();+}+// A draw with no stream bound is refused rather than attempted. D3D9 would have drawn nothing and// said nothing; a count that goes up is the difference between a missing pass and a silent one.TEST(dx11backend_a_draw_with_nothing_bound_is_counted_as_refused)diff --git a/GeneralsMD/Code/Tests/test_ww3d2.cpp b/GeneralsMD/Code/Tests/test_ww3d2.cppindex 7373ae31b..beb1d4ed0 100644--- a/GeneralsMD/Code/Tests/test_ww3d2.cpp+++ b/GeneralsMD/Code/Tests/test_ww3d2.cpp@@ -630,6 +630,7 @@ TEST(shader_every_field_round_trips_over_its_whole_range)CHECK_FIELD(Set_Src_Blend_Func, Get_Src_Blend_Func, SrcBlendFuncType, SRCBLEND_MAX);CHECK_FIELD(Set_Texturing, Get_Texturing, TexturingType, TEXTURING_MAX);CHECK_FIELD(Set_NPatch_Enable, Get_NPatch_Enable, NPatchEnableType, NPATCH_TYPE_MAX);+ CHECK_FIELD(Set_Glow, Get_Glow, GlowType, GLOW_MAX);CHECK_FIELD(Set_Alpha_Test, Get_Alpha_Test, AlphaTestType, ALPHATEST_MAX);CHECK_FIELD(Set_Cull_Mode, Get_Cull_Mode, CullModeType, CULL_MODE_MAX);CHECK_FIELD(Set_Post_Detail_Color_Func, Get_Post_Detail_Color_Func, DetailColorFuncType, DETAILCOLOR_MAX);@@ -1184,8 +1185,8 @@ TEST(no_preset_enables_fog_or_npatches)CHECK_EQ(presets[i]->Get_Color_Mask(), ShaderClass::COLOR_WRITE_ENABLE);CHECK_EQ(presets[i]->Get_Cull_Mode(), ShaderClass::CULL_MODE_ENABLE);CHECK_EQ(presets[i]->Get_Secondary_Gradient(), ShaderClass::SECONDARY_GRADIENT_DISABLE);- /* Unallocated high bits stay clear. */- CHECK_EQ(presets[i]->Get_Bits() & 0xf8000000u, 0u);+ /* Unallocated high bits stay clear; bit 27 is the Glow option's opt-in. */+ CHECK_EQ(presets[i]->Get_Bits() & 0xf0000000u, 0u);}}@@ -1529,6 +1530,28 @@ TEST(convert_shader_round_trips_the_carried_fields)CHECK_EQ(loaded.Get_Alpha_Test(), ShaderClass::ALPHATEST_ENABLE);}+/* The Glow option brightens the art's additive surfaces (muzzle flashes, glow cards) and the+ particles' additive preset, and nothing else that blends ONE, ONE: water, waypoint lines and+ decals use the plain additive preset. */+TEST(additive_art_and_particles_opt_into_the_glow_and_nothing_else_does)+{+ W3dShaderStruct file;+ ShaderClass loaded;++ W3dUtilityClass::Convert_Shader(ShaderClass::_PresetAdditiveShader, &file);+ W3dUtilityClass::Convert_Shader(file, &loaded);+ CHECK_EQ(loaded.Get_Glow(), ShaderClass::GLOW_ENABLE);++ ShaderClass alpha;+ W3dUtilityClass::Convert_Shader(ShaderClass::_PresetAlphaShader, &file);+ W3dUtilityClass::Convert_Shader(file, &alpha);+ CHECK_EQ(alpha.Get_Glow(), ShaderClass::GLOW_DISABLE);++ CHECK_EQ(ShaderClass::_PresetAdditiveSpriteShader.Get_Glow(), ShaderClass::GLOW_ENABLE);+ CHECK_EQ(ShaderClass::_PresetAdditiveShader.Get_Glow(), ShaderClass::GLOW_DISABLE);+ CHECK_EQ(ShaderClass::_PresetAdditiveSolidShader.Get_Glow(), ShaderClass::GLOW_DISABLE);+}+TEST(convert_shader_drops_the_fields_the_file_format_has_no_room_for){ShaderClass original;