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(gameenginedevice): add an opt-in bloom pass to the screen filter

Olcay Seygane3554b37cabf
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2fd5bede8..50655ad95 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -265,6 +265,20 @@ to keep: the thing never moves, so its shadow is worked out once.
All of it is switchable the same way: `ShadowsForProjectiles = No`, `ShadowsForParticles = No`
and `ShadowsForProps = No` in `GameData.ini`.
+## Bright things can glow
+
+Put `Bloom = 60` in your `Options.ini` and the sun off a windscreen, a muzzle flash, a burning
+building and the glare on water bleed a little light into the air around them, the way a camera
+does. Nothing else changes — the effect only touches what is already bright, and everything below
+the line stays exactly as it was.
+
+It is off until you put that line in, and you get the two dials that matter. `Bloom = 40` for a
+hint of it, `Bloom = 100` for the full thing. `BloomThreshold = 80` raises the bar so only the
+genuinely blinding things glow; lower it and more of the picture joins in. This game's artwork was
+painted in 2003 for a screen with no glow at all, so there is no setting that is right for
+everyone — 100 with a low threshold visibly washes out a beach. Start at 40 and walk it up until it
+looks right to you.
+
## It does not crash
- **It used to crash at the main menu.** Two blocks of hand-written assembly, untouched since 2003,
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
index b53696c82..937b1b0c9 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
@@ -340,6 +340,8 @@ public:
Bool m_showPlacementRangeRing; ///< while placing a structure, ring its weapon range
Bool m_workersReturnToSupply; ///< a worker that finishes a build job goes back to the dock it left
Bool m_detailedBuildTooltips; ///< put build time, weapon range and damage in the build tooltip
+ Int m_bloomIntensity; ///< bloom strength in percent, 0 = off
+ Int m_bloomThreshold; ///< brightness in percent below which nothing blooms
Bool m_buildMapCache;
AsciiString m_initialFile; ///< If this is specified, load a specific map/replay from the command-line
AsciiString m_pendingFile; ///< If this is specified, use this map at the next game start
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/UserPreferences.h b/GeneralsMD/Code/GameEngine/Include/Common/UserPreferences.h
index e93520b41..f7fdff7bc 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/UserPreferences.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/UserPreferences.h
@@ -119,6 +119,8 @@ public:
Bool getShowPlacementRangeRing(void);
Bool getWorkersReturnToSupply(void);
Bool getDetailedBuildTooltips(void);
+ Int getBloomIntensity(void);
+ Int getBloomThreshold(void);
Int getTextureReduction(void);
void getResolution(Int *xres, Int *yres);
Bool get3DShadowsEnabled(void);
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameLOD.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameLOD.cpp
index 8754646c5..dd739156f 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GameLOD.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameLOD.cpp
@@ -376,6 +376,10 @@ void GameLODManager::init(void)
TheWritableGlobalData->m_workersReturnToSupply = optionPref.getWorkersReturnToSupply();
TheWritableGlobalData->m_detailedBuildTooltips = optionPref.getDetailedBuildTooltips();
+ // Bloom is a look, not a detail level - it is honored at every preset, like the zoom ceiling.
+ TheWritableGlobalData->m_bloomIntensity = optionPref.getBloomIntensity();
+ TheWritableGlobalData->m_bloomThreshold = optionPref.getBloomThreshold();
+
setStaticLODLevel(userSetDetail);
}
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
index 48bc2eb96..045fc32ef 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
@@ -196,6 +196,8 @@ GlobalData* GlobalData::m_theOriginal = NULL;
{ "ShowPlacementRangeRing", INI::parseBool, NULL, offsetof( GlobalData, m_showPlacementRangeRing ) },
{ "WorkersReturnToSupply", INI::parseBool, NULL, offsetof( GlobalData, m_workersReturnToSupply ) },
{ "DetailedBuildTooltips", INI::parseBool, NULL, offsetof( GlobalData, m_detailedBuildTooltips ) },
+ { "Bloom", INI::parseInt, NULL, offsetof( GlobalData, m_bloomIntensity ) },
+ { "BloomThreshold", INI::parseInt, NULL, offsetof( GlobalData, m_bloomThreshold ) },
{ "MinCameraHeight", INI::parseReal, NULL, offsetof( GlobalData, m_minCameraHeight ) },
{ "TerrainHeightAtEdgeOfMap", INI::parseReal, NULL, offsetof( GlobalData, m_terrainHeightAtEdgeOfMap ) },
{ "UnitDamagedThreshold", INI::parseReal, NULL, offsetof( GlobalData, m_unitDamagedThresh ) },
@@ -1021,6 +1023,13 @@ GlobalData::GlobalData()
m_showPlacementRangeRing = TRUE;
m_workersReturnToSupply = TRUE;
m_detailedBuildTooltips = TRUE;
+
+ // Bloom is the one that does NOT default on: it changes how the game looks rather than what it
+ // can do, and the artwork was painted in 2003 for a screen with no glow at all. "Bloom = 60"
+ // in Options.ini turns it on at a restrained strength; "BloomThreshold" is the brightness,
+ // in percent, below which nothing glows.
+ m_bloomIntensity = 0;
+ m_bloomThreshold = 65;
m_animateWindows = TRUE;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenu.cpp
index 7f2bde769..9b10e14a6 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenu.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenu.cpp
@@ -836,6 +836,35 @@ Bool OptionPreferences::getDetailedBuildTooltips(void)
return stricmp(it->second.str(), "yes") == 0;
}
+Int OptionPreferences::getBloomIntensity(void)
+{
+ // Not written by the options menu - a power-user Options.ini key. Percent, 0 = off, which is
+ // what the GameData.ini default is: the game's artwork has no HDR range in it, so how much
+ // glow looks right is a matter of taste rather than something to pick on the player's behalf.
+ OptionPreferences::const_iterator it = find("Bloom");
+ if (it == end())
+ return TheGlobalData->m_bloomIntensity;
+
+ const Int intensity = atoi(it->second.str());
+ if (intensity < 0) return 0;
+ if (intensity > 100) return 100;
+ return intensity;
+}
+
+Int OptionPreferences::getBloomThreshold(void)
+{
+ // Percent: the brightness below which nothing blooms at all. Lower it and more of the picture
+ // joins in; raise it and only the genuinely blinding things glow.
+ OptionPreferences::const_iterator it = find("BloomThreshold");
+ if (it == end())
+ return TheGlobalData->m_bloomThreshold;
+
+ const Int threshold = atoi(it->second.str());
+ if (threshold < 0) return 0;
+ if (threshold > 100) return 100;
+ return threshold;
+}
+
Int OptionPreferences::getTextureReduction(void)
{
OptionPreferences::const_iterator it = find("TextureReduction");
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cpp
index 74d218dd6..83c81f5e4 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DShaderManager.cpp
@@ -142,6 +142,7 @@ class ScreenDefaultFilter : public W3DFilterInterface
{
public:
virtual Int init(void); ///<perform any one time initialization and validation
+ virtual Int shutdown(void); ///<release the bloom render targets
virtual Bool preRender(Bool &skipRender, CustomScenePassModes &scenePassMode); ///< Set up at start of render. Only applies to screen filter shaders.
virtual Bool postRender(enum FilterModes mode, Coord2D &scrollDelta,Bool &doExtraRender); ///< Called after render. Only applies to screen filter shaders.
virtual Bool setup(enum FilterModes mode){return true;} ///< Called when the filter is started, one time before the first prerender.
@@ -161,6 +162,172 @@ W3DFilterInterface *ScreenDefaultFilterList[]=
NULL
};
+/*========= Bloom =============================================================*/
+/// Retail had no bloom and every texture in the game is LDR, so this is opt-in and tunable
+/// rather than a fixed look: "Bloom" in Options.ini is the strength in percent (0, off, unless
+/// it is set) and "BloomThreshold" the brightness, in percent, below which nothing glows.
+/// It rides on ScreenDefaultFilter, which already renders the scene into a full-screen texture
+/// on the frames the smudge effects need one, so the only new work is a quarter-size bright
+/// pass, two 4-tap blur passes and one additive composite. All fixed function on purpose - no
+/// pixel shader, so it does not depend on LoadAndCreateD3DShader (which refuses outright on any
+/// card the chipset table reads as older than a GeForce3).
+
+#define BLOOM_DOWNSAMPLE 4 ///< bright pass and blur run at 1/4 of the screen in each axis
+
+static IDirect3DTexture8 *s_bloomTexture[2]; ///< ping-pong render targets for the blur passes
+static IDirect3DSurface8 *s_bloomSurface[2];
+static Int s_bloomWidth, s_bloomHeight;
+
+/** Bloom strength in percent - Options.ini's "Bloom" - 0 when it was never set. Read every
+ frame rather than cached, so editing the key and reloading the options takes effect. */
+static Int bloomIntensity(void)
+{
+ if (TheGlobalData == NULL) return 0;
+ Int intensity = TheGlobalData->m_bloomIntensity;
+ if (intensity < 0) return 0;
+ if (intensity > 100) return 100; //the composite is a single additive draw, so 100% is all there is
+ return intensity;
+}
+
+/** Brightness below which nothing blooms, as a 0..255 channel value - Options.ini's
+ "BloomThreshold", which is in percent. */
+static Int bloomThreshold(void)
+{
+ const Int threshold = (TheGlobalData != NULL ? TheGlobalData->m_bloomThreshold : 65) * 255 / 100;
+ if (threshold < 0) return 0;
+ if (threshold > 255) return 255;
+ return threshold;
+}
+
+static void releaseBloomTargets(void)
+{
+ for (Int i = 0; i < 2; i++)
+ {
+ if (s_bloomSurface[i]) { s_bloomSurface[i]->Release(); s_bloomSurface[i] = NULL; }
+ if (s_bloomTexture[i]) { s_bloomTexture[i]->Release(); s_bloomTexture[i] = NULL; }
+ }
+}
+
+/** Allocates the two quarter-size render targets, in the format of the scene texture they will
+ be downsampling. Lazily, because these live in D3DPOOL_DEFAULT and do not survive a device
+ reset - shutdown() drops them and the next frame builds them again. */
+static Bool createBloomTargets(IDirect3DTexture8 *sceneTexture)
+{
+ if (s_bloomTexture[0] && s_bloomTexture[1]) return TRUE;
+
+ D3DSURFACE_DESC desc;
+ if (FAILED(sceneTexture->GetLevelDesc(0, &desc))) return FALSE;
+ s_bloomWidth = desc.Width / BLOOM_DOWNSAMPLE;
+ s_bloomHeight = desc.Height / BLOOM_DOWNSAMPLE;
+ if (s_bloomWidth < 1 || s_bloomHeight < 1) return FALSE;
+
+ LPDIRECT3DDEVICE8 pDev = DX8Wrapper::_Get_D3D_Device8();
+ for (Int i = 0; i < 2; i++)
+ {
+ if (FAILED(pDev->CreateTexture(s_bloomWidth, s_bloomHeight, 1, D3DUSAGE_RENDERTARGET,
+ desc.Format, D3DPOOL_DEFAULT, &s_bloomTexture[i]))
+ || FAILED(s_bloomTexture[i]->GetSurfaceLevel(0, &s_bloomSurface[i])))
+ {
+ releaseBloomTargets();
+ DEBUG_LOG(("Bloom: could not create %dx%d render targets - disabled\n", s_bloomWidth, s_bloomHeight));
+ return FALSE;
+ }
+ }
+ DEBUG_LOG(("Bloom: %dx%d, intensity %d%%, threshold %d/255\n",
+ s_bloomWidth, s_bloomHeight, bloomIntensity(), bloomThreshold()));
+ return TRUE;
+}
+
+/** One screen-aligned quad covering the given rectangle of the current render target, sampling
+ [u0,v0]..[u1,v1] of whatever texture is in stage 0, modulated by colour. */
+static void drawBloomQuad(Real x, Real y, Real w, Real h,
+ Real u0, Real v0, Real u1, Real v1, DWORD color)
+{
+ struct _TRANS_LIT_TEX_VERTEX {
+ D3DXVECTOR4 p;
+ DWORD color;
+ Real u, v;
+ } v[4];
+
+ //bottom right, top right, bottom left, top left - a strip, the same winding the other
+ //filters draw their viewport quad with
+ v[0].p = D3DXVECTOR4(x + w - 0.5f, y + h - 0.5f, 0.0f, 1.0f); v[0].u = u1; v[0].v = v1;
+ v[1].p = D3DXVECTOR4(x + w - 0.5f, y - 0.5f, 0.0f, 1.0f); v[1].u = u1; v[1].v = v0;
+ v[2].p = D3DXVECTOR4(x - 0.5f, y + h - 0.5f, 0.0f, 1.0f); v[2].u = u0; v[2].v = v1;
+ v[3].p = D3DXVECTOR4(x - 0.5f, y - 0.5f, 0.0f, 1.0f); v[3].u = u0; v[3].v = v0;
+ for (Int i = 0; i < 4; i++) v[i].color = color;
+
+ LPDIRECT3DDEVICE8 pDev = DX8Wrapper::_Get_D3D_Device8();
+ pDev->SetVertexShader(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
+ pDev->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, v, sizeof(_TRANS_LIT_TEX_VERTEX));
+}
+
+/** Bright pass, blur, additive composite. Called with the back buffer as the render target and
+ the finished scene already blitted onto it; sceneTexture is that same scene and
+ [u0,v0]..[u1,v1] is the part of it the viewport occupies. Every state here is set straight
+ on the device, like the rest of the filters do - ScreenDefaultFilter::reset() invalidates the
+ wrapper's whole state cache afterwards, so nothing needs putting back by hand. */
+static void renderBloom(IDirect3DTexture8 *sceneTexture, Real x, Real y, Real w, Real h,
+ Real u0, Real v0, Real u1, Real v1)
+{
+ if (!createBloomTargets(sceneTexture)) return;
+
+ LPDIRECT3DDEVICE8 pDev = DX8Wrapper::_Get_D3D_Device8();
+ IDirect3DSurface8 *oldTarget = NULL, *oldDepth = NULL;
+ if (FAILED(pDev->GetRenderTarget(&oldTarget)) || oldTarget == NULL) return;
+ pDev->GetDepthStencilSurface(&oldDepth); //can legitimately be NULL
+
+ //Bright pass. Anything darker than the threshold subtracts away to black and what survives
+ //is how far above it the pixel was - a soft knee rather than a hard cut, which is what stops
+ //sunlit sand from popping in and out of the effect as the camera moves.
+ const Int t = bloomThreshold();
+ pDev->SetRenderTarget(s_bloomSurface[0], NULL); //no depth buffer: none of this is depth tested
+ pDev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
+ pDev->SetRenderState(D3DRS_TEXTUREFACTOR, D3DCOLOR_ARGB(255, t, t, t));
+ pDev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SUBTRACT);
+ pDev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
+ pDev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
+ pDev->SetTexture(0, sceneTexture);
+ drawBloomQuad(0.0f, 0.0f, (Real)s_bloomWidth, (Real)s_bloomHeight, u0, v0, u1, v1, 0xffffffff);
+
+ //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
+ //three adding, so the four average out.
+ pDev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
+ pDev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
+ pDev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
+ pDev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
+ for (Int pass = 0; pass < 2; pass++)
+ {
+ const Int src = pass & 1;
+ const Int dst = (pass + 1) & 1;
+ const Real ox = (pass ? 3.5f : 1.5f) / (Real)s_bloomWidth;
+ const Real oy = (pass ? 3.5f : 1.5f) / (Real)s_bloomHeight;
+ pDev->SetRenderTarget(s_bloomSurface[dst], NULL);
+ pDev->SetTexture(0, s_bloomTexture[src]);
+ for (Int tap = 0; tap < 4; tap++)
+ {
+ const Real sx = (tap & 1) ? ox : -ox;
+ const Real sy = (tap & 2) ? oy : -oy;
+ pDev->SetRenderState(D3DRS_ALPHABLENDENABLE, tap != 0);
+ drawBloomQuad(0.0f, 0.0f, (Real)s_bloomWidth, (Real)s_bloomHeight,
+ sx, sy, 1.0f + sx, 1.0f + sy, 0x40404040);
+ }
+ }
+
+ //Composite: add the blurred highlights back over the scene.
+ const Int i = bloomIntensity() * 255 / 100;
+ pDev->SetRenderTarget(oldTarget, oldDepth);
+ pDev->SetTexture(0, s_bloomTexture[0]); //the second blur pass wrote target 0
+ pDev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
+ drawBloomQuad(x, y, w, h, 0.0f, 0.0f, 1.0f, 1.0f, D3DCOLOR_ARGB(255, i, i, i));
+
+ pDev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
+ pDev->SetTexture(0, NULL);
+ oldTarget->Release();
+ if (oldDepth) oldDepth->Release();
+}
+
Int ScreenDefaultFilter::init(void)
{
if (!W3DShaderManager::canRenderToTexture()) {
@@ -194,8 +361,9 @@ Int ScreenDefaultFilter::init(void)
Bool ScreenDefaultFilter::preRender(Bool &skipRender, CustomScenePassModes &scenePassMode)
{
- //Right now this filter is only used for smudges, so don't bother if none are present.
- if (TheSmudgeManager)
+ //Right now this filter is only used for smudges, so don't bother if none are present -
+ //unless bloom is on, which needs the scene in a texture on every frame.
+ if (TheSmudgeManager && bloomIntensity() == 0)
{ if (((W3DSmudgeManager *)TheSmudgeManager)->getSmudgeCountLastFrame() == 0)
return FALSE;
}
@@ -249,10 +417,21 @@ Bool ScreenDefaultFilter::postRender(enum FilterModes mode, Coord2D &scrollDelta
pDev->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, v, sizeof(_TRANS_LIT_TEX_VERTEX));
+ //v[3] is the top left corner of the viewport inside the scene texture, v[0] the bottom right
+ if (bloomIntensity() > 0)
+ renderBloom(tex, (Real)xpos, (Real)ypos, (Real)width, (Real)height,
+ v[3].u, v[3].v, v[0].u, v[0].v);
+
reset();
return true;
}
+Int ScreenDefaultFilter::shutdown(void)
+{
+ releaseBloomTargets(); //D3DPOOL_DEFAULT, so these do not survive a device reset
+ return TRUE;
+}
+
Int ScreenDefaultFilter::set(enum FilterModes mode)
{
VertexMaterialClass *vmat=VertexMaterialClass::Get_Preset(VertexMaterialClass::PRELIT_DIFFUSE);
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/test_dx8_smoke.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/test_dx8_smoke.cpp
index cbb236993..4de982496 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/test_dx8_smoke.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/test_dx8_smoke.cpp
@@ -196,6 +196,83 @@ int main(int argc, char **argv)
rttSurf->Release(); rtt->Release();
}
+ // The bloom bright pass (W3DShaderManager.cpp's renderBloom) is fixed function on purpose,
+ // and it leans on two things no caps bit here guarantees through d3d8to9: D3DTOP_SUBTRACT
+ // against D3DTA_TFACTOR, and binding a render target with a NULL depth surface. Run exactly
+ // that: a flat 200/255 source texture minus a 166/255 threshold must come back as 34.
+ {
+ IDirect3DTexture8 * src = NULL, * dst = NULL;
+ IDirect3DSurface8 * dstSurf = NULL, * bb = NULL, * autoZ = NULL, * copySurf = NULL;
+ D3DLOCKED_RECT lr;
+ bool built = SUCCEEDED(dev->CreateTexture(4, 4, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &src))
+ && SUCCEEDED(dev->CreateTexture(16, 16, 1, D3DUSAGE_RENDERTARGET, pp.BackBufferFormat,
+ D3DPOOL_DEFAULT, &dst))
+ && SUCCEEDED(dst->GetSurfaceLevel(0, &dstSurf))
+ && SUCCEEDED(dev->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &bb))
+ && SUCCEEDED(dev->CreateImageSurface(16, 16, pp.BackBufferFormat, &copySurf))
+ && SUCCEEDED(src->LockRect(0, &lr, NULL, 0));
+ if (!built) {
+ printf("FAIL: could not build the bloom bright pass surfaces\n");
+ dev->Release(); d3d->Release(); DestroyWindow(hwnd);
+ return 1;
+ }
+ dev->GetDepthStencilSurface(&autoZ); // may be NULL, that is fine
+ for (int y = 0; y < 4; y++) {
+ unsigned long * row = (unsigned long *)((unsigned char *)lr.pBits + y * lr.Pitch);
+ for (int x = 0; x < 4; x++) row[x] = 0xffc8c8c8; // opaque 200,200,200
+ }
+ src->UnlockRect(0);
+
+ struct QuadVertex { float x, y, z, rhw; unsigned long color; float u, v; };
+ const QuadVertex quad[4] = {
+ { 15.5f, 15.5f, 0.0f, 1.0f, 0xffffffff, 1.0f, 1.0f },
+ { 15.5f, -0.5f, 0.0f, 1.0f, 0xffffffff, 1.0f, 0.0f },
+ { -0.5f, 15.5f, 0.0f, 1.0f, 0xffffffff, 0.0f, 1.0f },
+ { -0.5f, -0.5f, 0.0f, 1.0f, 0xffffffff, 0.0f, 0.0f },
+ };
+ HRESULT bound = dev->SetRenderTarget(dstSurf, NULL); // NULL depth: nothing here is depth tested
+ dev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
+ dev->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
+ dev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
+ dev->SetRenderState(D3DRS_LIGHTING, FALSE);
+ dev->SetRenderState(D3DRS_TEXTUREFACTOR, D3DCOLOR_ARGB(255, 166, 166, 166));
+ dev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SUBTRACT);
+ dev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
+ dev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
+ dev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
+ dev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
+ dev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_POINT);
+ dev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_POINT);
+ dev->SetTexture(0, src);
+ dev->SetVertexShader(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
+ HRESULT begun = dev->BeginScene();
+ HRESULT drawn = dev->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(QuadVertex));
+ dev->EndScene();
+ HRESULT copied = dev->CopyRects(dstSurf, NULL, 0, copySurf, NULL);
+ HRESULT restored = dev->SetRenderTarget(bb, autoZ);
+ dev->SetTexture(0, NULL);
+ dev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
+ dev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
+ printf("bloom: bind(null z) hr=0x%08lx, begin hr=0x%08lx, draw hr=0x%08lx, copy hr=0x%08lx, restore hr=0x%08lx\n",
+ (unsigned long)bound, (unsigned long)begun, (unsigned long)drawn,
+ (unsigned long)copied, (unsigned long)restored);
+ int got = -1;
+ if (SUCCEEDED(copySurf->LockRect(&lr, NULL, D3DLOCK_READONLY))) {
+ const unsigned char * px = (const unsigned char *)lr.pBits + 8 * lr.Pitch + 8 * 4;
+ printf("bloom: 200 - 166 -> (b,g,r) = (%u,%u,%u), want 34\n", px[0], px[1], px[2]);
+ if (px[0] == px[1] && px[1] == px[2]) got = px[0];
+ copySurf->UnlockRect();
+ }
+ copySurf->Release();
+ if (autoZ) autoZ->Release();
+ bb->Release(); dstSurf->Release(); dst->Release(); src->Release();
+ if (FAILED(bound) || FAILED(drawn) || FAILED(copied) || FAILED(restored) || got < 30 || got > 38) {
+ printf("FAIL: the bloom bright pass (D3DTOP_SUBTRACT of D3DTA_TFACTOR into a render target with no depth buffer) did not work\n");
+ dev->Release(); d3d->Release(); DestroyWindow(hwnd);
+ return 1;
+ }
+ }
+
HRESULT phr = dev->Present(NULL, NULL, NULL, NULL);
printf("Present hr=0x%08lx\n", (unsigned long)phr);
if (show) {